FFmpeg
qpeg.c
Go to the documentation of this file.
1 /*
2  * QPEG codec
3  * Copyright (c) 2004 Konstantin Shishkov
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 /**
23  * @file
24  * QPEG codec.
25  */
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "codec_internal.h"
30 #include "decode.h"
31 #include "internal.h"
32 
33 typedef struct QpegContext{
36  uint32_t pal[256];
38 } QpegContext;
39 
40 static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
41  int stride, int width, int height)
42 {
43  int i;
44  int code;
45  int c0, c1;
46  int run, copy;
47  int filled = 0;
48  int rows_to_go;
49 
50  rows_to_go = height;
51  height--;
52  dst = dst + height * stride;
53 
54  while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
55  code = bytestream2_get_byte(&qctx->buffer);
56  run = copy = 0;
57  if(code == 0xFC) /* end-of-picture code */
58  break;
59  if(code >= 0xF8) { /* very long run */
60  c0 = bytestream2_get_byte(&qctx->buffer);
61  c1 = bytestream2_get_byte(&qctx->buffer);
62  run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
63  } else if (code >= 0xF0) { /* long run */
64  c0 = bytestream2_get_byte(&qctx->buffer);
65  run = ((code & 0xF) << 8) + c0 + 2;
66  } else if (code >= 0xE0) { /* short run */
67  run = (code & 0x1F) + 2;
68  } else if (code >= 0xC0) { /* very long copy */
69  c0 = bytestream2_get_byte(&qctx->buffer);
70  c1 = bytestream2_get_byte(&qctx->buffer);
71  copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
72  } else if (code >= 0x80) { /* long copy */
73  c0 = bytestream2_get_byte(&qctx->buffer);
74  copy = ((code & 0x7F) << 8) + c0 + 1;
75  } else { /* short copy */
76  copy = code + 1;
77  }
78 
79  /* perform actual run or copy */
80  if(run) {
81  int p;
82 
83  p = bytestream2_get_byte(&qctx->buffer);
84  for(i = 0; i < run; i++) {
85  int step = FFMIN(run - i, width - filled);
86  memset(dst+filled, p, step);
87  filled += step;
88  i += step - 1;
89  if (filled >= width) {
90  filled = 0;
91  dst -= stride;
92  rows_to_go--;
93  while (run - i > width && rows_to_go > 0) {
94  memset(dst, p, width);
95  dst -= stride;
96  rows_to_go--;
97  i += width;
98  }
99  if(rows_to_go <= 0)
100  break;
101  }
102  }
103  } else {
104  if (bytestream2_get_bytes_left(&qctx->buffer) < copy)
106  while (copy > 0) {
107  int step = FFMIN(copy, width - filled);
108  bytestream2_get_bufferu(&qctx->buffer, dst + filled, step);
109  filled += step;
110  copy -= step;
111  if (filled >= width) {
112  filled = 0;
113  dst -= stride;
114  rows_to_go--;
115  if(rows_to_go <= 0)
116  break;
117  }
118  }
119  }
120  }
121 }
122 
123 static const uint8_t qpeg_table_h[16] =
124  { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
125 static const uint8_t qpeg_table_w[16] =
126  { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
127 
128 /* Decodes delta frames */
129 static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
130  int stride, int width, int height,
131  int delta, const uint8_t *ctable,
132  uint8_t *refdata)
133 {
134  int i, j;
135  int code;
136  int filled = 0;
137  int orig_height;
138 
139  if (refdata) {
140  /* copy prev frame */
141  for (i = 0; i < height; i++)
142  memcpy(dst + (i * stride), refdata + (i * stride), width);
143  } else {
144  refdata = dst;
145  }
146 
147  orig_height = height;
148  height--;
149  dst = dst + height * stride;
150 
151  while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
152  code = bytestream2_get_byte(&qctx->buffer);
153 
154  if(delta) {
155  /* motion compensation */
156  while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
157  if(delta == 1) {
158  int me_idx;
159  int me_w, me_h, me_x, me_y;
160  uint8_t *me_plane;
161  int corr, val;
162 
163  /* get block size by index */
164  me_idx = code & 0xF;
165  me_w = qpeg_table_w[me_idx];
166  me_h = qpeg_table_h[me_idx];
167 
168  /* extract motion vector */
169  corr = bytestream2_get_byte(&qctx->buffer);
170 
171  val = corr >> 4;
172  if(val > 7)
173  val -= 16;
174  me_x = val;
175 
176  val = corr & 0xF;
177  if(val > 7)
178  val -= 16;
179  me_y = val;
180 
181  /* check motion vector */
182  if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
183  (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
184  (filled + me_w > width) || (height - me_h < 0))
185  av_log(qctx->avctx, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
186  me_x, me_y, me_w, me_h, filled, height);
187  else {
188  /* do motion compensation */
189  me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
190  for(j = 0; j < me_h; j++) {
191  for(i = 0; i < me_w; i++)
192  dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
193  }
194  }
195  }
196  code = bytestream2_get_byte(&qctx->buffer);
197  }
198  }
199 
200  if(code == 0xE0) /* end-of-picture code */
201  break;
202  if(code > 0xE0) { /* run code: 0xE1..0xFF */
203  int p;
204 
205  code &= 0x1F;
206  p = bytestream2_get_byte(&qctx->buffer);
207  for(i = 0; i <= code; i++) {
208  dst[filled++] = p;
209  if(filled >= width) {
210  filled = 0;
211  dst -= stride;
212  height--;
213  if (height < 0)
214  break;
215  }
216  }
217  } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
218  code &= 0x1F;
219 
220  if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
221  break;
222 
223  for(i = 0; i <= code; i++) {
224  dst[filled++] = bytestream2_get_byte(&qctx->buffer);
225  if(filled >= width) {
226  filled = 0;
227  dst -= stride;
228  height--;
229  if (height < 0)
230  break;
231  }
232  }
233  } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
234  int skip;
235 
236  code &= 0x3F;
237  /* codes 0x80 and 0x81 are actually escape codes,
238  skip value minus constant is in the next byte */
239  if(!code)
240  skip = bytestream2_get_byte(&qctx->buffer) + 64;
241  else if(code == 1)
242  skip = bytestream2_get_byte(&qctx->buffer) + 320;
243  else
244  skip = code;
245  filled += skip;
246  while( filled >= width) {
247  filled -= width;
248  dst -= stride;
249  height--;
250  if(height < 0)
251  break;
252  }
253  } else {
254  /* zero code treated as one-pixel skip */
255  if(code) {
256  dst[filled++] = ctable[code & 0x7F];
257  }
258  else
259  filled++;
260  if(filled >= width) {
261  filled = 0;
262  dst -= stride;
263  height--;
264  }
265  }
266  }
267 }
268 
269 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
270  int *got_frame, AVPacket *avpkt)
271 {
272  uint8_t ctable[128];
273  QpegContext * const a = avctx->priv_data;
274  AVFrame * const ref = a->ref;
275  uint8_t* outdata;
276  int delta, intra, ret;
277 
278  if (avpkt->size < 0x86) {
279  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
280  return AVERROR_INVALIDDATA;
281  }
282 
283  bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
284 
285  if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
286  return ret;
287  outdata = p->data[0];
288  bytestream2_skip(&a->buffer, 4);
289  bytestream2_get_buffer(&a->buffer, ctable, 128);
290  bytestream2_skip(&a->buffer, 1);
291 
292  delta = bytestream2_get_byte(&a->buffer);
293  intra = delta == 0x10;
294  if (intra) {
295  qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
296  } else {
297  qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, ref->data[0]);
298  }
299 
300  /* make the palette available on the way out */
301  p->palette_has_changed = ff_copy_palette(a->pal, avpkt, avctx);
302  memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
303 
305  if ((ret = av_frame_ref(ref, p)) < 0)
306  return ret;
307 
308  p->key_frame = intra;
310 
311  *got_frame = 1;
312 
313  return avpkt->size;
314 }
315 
316 static void decode_flush(AVCodecContext *avctx){
317  QpegContext * const a = avctx->priv_data;
318  int i, pal_size;
319  const uint8_t *pal_src;
320 
321  av_frame_unref(a->ref);
322 
323  pal_size = FFMIN(1024U, avctx->extradata_size);
324  pal_src = avctx->extradata + avctx->extradata_size - pal_size;
325 
326  for (i=0; i<pal_size/4; i++)
327  a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
328 }
329 
331 {
332  QpegContext * const a = avctx->priv_data;
333 
334  av_frame_free(&a->ref);
335 
336  return 0;
337 }
338 
339 static av_cold int decode_init(AVCodecContext *avctx){
340  QpegContext * const a = avctx->priv_data;
341 
342  a->avctx = avctx;
343  avctx->pix_fmt= AV_PIX_FMT_PAL8;
344 
345  a->ref = av_frame_alloc();
346  if (!a->ref)
347  return AVERROR(ENOMEM);
348 
349  decode_flush(avctx);
350 
351  return 0;
352 }
353 
355  .p.name = "qpeg",
356  .p.long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
357  .p.type = AVMEDIA_TYPE_VIDEO,
358  .p.id = AV_CODEC_ID_QPEG,
359  .priv_data_size = sizeof(QpegContext),
360  .init = decode_init,
361  .close = decode_end,
363  .flush = decode_flush,
364  .p.capabilities = AV_CODEC_CAP_DR1,
365  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
367 };
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:39
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
qpeg_decode_inter
static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst, int stride, int width, int height, int delta, const uint8_t *ctable, uint8_t *refdata)
Definition: qpeg.c:129
GetByteContext
Definition: bytestream.h:33
qpeg_decode_intra
static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst, int stride, int width, int height)
Definition: qpeg.c:40
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
decode_flush
static void decode_flush(AVCodecContext *avctx)
Definition: qpeg.c:316
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:112
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: qpeg.c:269
c1
static const uint64_t c1
Definition: murmur3.c:51
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
init
static int init
Definition: av_tx.c:47
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
U
#define U(x)
Definition: vp56_arith.h:37
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:417
val
static double val(void *priv, double ch)
Definition: aeval.c:77
av_noinline
#define av_noinline
Definition: attributes.h:72
qpeg_table_h
static const uint8_t qpeg_table_h[16]
Definition: qpeg.c:123
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:491
QpegContext::buffer
GetByteContext buffer
Definition: qpeg.c:37
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
QpegContext::pal
uint32_t pal[256]
Definition: qpeg.c:36
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:367
decode.h
run
uint8_t run
Definition: svq3.c:205
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
qpeg_table_w
static const uint8_t qpeg_table_w[16]
Definition: qpeg.c:125
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:422
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1403
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
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:117
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:187
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:343
codec_internal.h
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: qpeg.c:330
QpegContext::avctx
AVCodecContext * avctx
Definition: qpeg.c:34
height
#define height
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AV_CODEC_ID_QPEG
@ AV_CODEC_ID_QPEG
Definition: codec_id.h:110
ff_qpeg_decoder
const FFCodec ff_qpeg_decoder
Definition: qpeg.c:354
QpegContext
Definition: qpeg.c:33
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:490
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:477
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: qpeg.c:339
ret
ret
Definition: filter_design.txt:187
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:389
AVFrame::palette_has_changed
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:487
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
QpegContext::ref
AVFrame * ref
Definition: qpeg.c:35
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
bytestream2_get_bufferu
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:370
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_copy_palette
int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
Check whether the side-data of src contains a palette of size AVPALETTE_SIZE; if so,...
Definition: decode.c:1624