FFmpeg
aliaspixenc.c
Go to the documentation of this file.
1 /*
2  * Alias PIX image encoder
3  * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 
28 #define ALIAS_HEADER_SIZE 10
29 
31  const AVFrame *frame, int *got_packet)
32 {
33  int width, height, bits_pixel, i, j, length, ret;
34  uint8_t *in_buf, *buf;
35 
36 #if FF_API_CODED_FRAME
39  avctx->coded_frame->key_frame = 1;
41 #endif
42 
43  width = avctx->width;
44  height = avctx->height;
45 
46  if (width > 65535 || height > 65535 ||
47  width * height >= INT_MAX / 4 - ALIAS_HEADER_SIZE) {
48  av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", width, height);
49  return AVERROR_INVALIDDATA;
50  }
51 
52  switch (avctx->pix_fmt) {
53  case AV_PIX_FMT_GRAY8:
54  bits_pixel = 8;
55  break;
56  case AV_PIX_FMT_BGR24:
57  bits_pixel = 24;
58  break;
59  default:
60  return AVERROR(EINVAL);
61  }
62 
63  length = ALIAS_HEADER_SIZE + 4 * width * height; // max possible
64  if ((ret = ff_alloc_packet2(avctx, pkt, length, ALIAS_HEADER_SIZE + height*2)) < 0) {
65  av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
66  return ret;
67  }
68 
69  buf = pkt->data;
70 
71  /* Encode header. */
72  bytestream_put_be16(&buf, width);
73  bytestream_put_be16(&buf, height);
74  bytestream_put_be32(&buf, 0); /* X, Y offset */
75  bytestream_put_be16(&buf, bits_pixel);
76 
77  for (j = 0; j < height; j++) {
78  in_buf = frame->data[0] + frame->linesize[0] * j;
79  for (i = 0; i < width; ) {
80  int count = 0;
81  int pixel;
82 
83  if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
84  pixel = *in_buf;
85  while (count < 255 && count + i < width && pixel == *in_buf) {
86  count++;
87  in_buf++;
88  }
89  bytestream_put_byte(&buf, count);
90  bytestream_put_byte(&buf, pixel);
91  } else { /* AV_PIX_FMT_BGR24 */
92  pixel = AV_RB24(in_buf);
93  while (count < 255 && count + i < width &&
94  pixel == AV_RB24(in_buf)) {
95  count++;
96  in_buf += 3;
97  }
98  bytestream_put_byte(&buf, count);
99  bytestream_put_be24(&buf, pixel);
100  }
101  i += count;
102  }
103  }
104 
105  /* Total length */
106  av_shrink_packet(pkt, buf - pkt->data);
108  *got_packet = 1;
109 
110  return 0;
111 }
112 
114  .name = "alias_pix",
115  .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
116  .type = AVMEDIA_TYPE_VIDEO,
117  .id = AV_CODEC_ID_ALIAS_PIX,
118  .encode2 = encode_frame,
119  .pix_fmts = (const enum AVPixelFormat[]) {
121  },
122 };
AVCodec
AVCodec.
Definition: codec.h:190
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: aliaspixenc.c:30
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:388
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:103
ALIAS_HEADER_SIZE
#define ALIAS_HEADER_SIZE
Definition: aliaspixenc.c:28
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
width
#define width
intreadwrite.h
AV_CODEC_ID_ALIAS_PIX
@ AV_CODEC_ID_ALIAS_PIX
Definition: codec_id.h:226
ff_alias_pix_encoder
AVCodec ff_alias_pix_encoder
Definition: aliaspixenc.c:113
pixel
uint8_t pixel
Definition: tiny_ssim.c:42
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
height
#define height
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
AVCodecContext::height
int height
Definition: avcodec.h:699
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext::coded_frame
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1776
AVCodecContext
main external API structure.
Definition: avcodec.h:526
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:332
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:699
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:93
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32