FFmpeg  4.1.5
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 "internal.h"
30 
31 typedef struct QpegContext{
34  uint32_t pal[256];
36 } QpegContext;
37 
38 static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
39  int stride, int width, int height)
40 {
41  int i;
42  int code;
43  int c0, c1;
44  int run, copy;
45  int filled = 0;
46  int rows_to_go;
47 
48  rows_to_go = height;
49  height--;
50  dst = dst + height * stride;
51 
52  while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
53  code = bytestream2_get_byte(&qctx->buffer);
54  run = copy = 0;
55  if(code == 0xFC) /* end-of-picture code */
56  break;
57  if(code >= 0xF8) { /* very long run */
58  c0 = bytestream2_get_byte(&qctx->buffer);
59  c1 = bytestream2_get_byte(&qctx->buffer);
60  run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
61  } else if (code >= 0xF0) { /* long run */
62  c0 = bytestream2_get_byte(&qctx->buffer);
63  run = ((code & 0xF) << 8) + c0 + 2;
64  } else if (code >= 0xE0) { /* short run */
65  run = (code & 0x1F) + 2;
66  } else if (code >= 0xC0) { /* very long copy */
67  c0 = bytestream2_get_byte(&qctx->buffer);
68  c1 = bytestream2_get_byte(&qctx->buffer);
69  copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
70  } else if (code >= 0x80) { /* long copy */
71  c0 = bytestream2_get_byte(&qctx->buffer);
72  copy = ((code & 0x7F) << 8) + c0 + 1;
73  } else { /* short copy */
74  copy = code + 1;
75  }
76 
77  /* perform actual run or copy */
78  if(run) {
79  int p;
80 
81  p = bytestream2_get_byte(&qctx->buffer);
82  for(i = 0; i < run; i++) {
83  dst[filled++] = p;
84  if (filled >= width) {
85  filled = 0;
86  dst -= stride;
87  rows_to_go--;
88  if(rows_to_go <= 0)
89  break;
90  }
91  }
92  } else {
93  if (bytestream2_get_bytes_left(&qctx->buffer) < copy)
94  copy = bytestream2_get_bytes_left(&qctx->buffer);
95  for(i = 0; i < copy; i++) {
96  dst[filled++] = bytestream2_get_byte(&qctx->buffer);
97  if (filled >= width) {
98  filled = 0;
99  dst -= stride;
100  rows_to_go--;
101  if(rows_to_go <= 0)
102  break;
103  }
104  }
105  }
106  }
107 }
108 
109 static const int qpeg_table_h[16] =
110  { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
111 static const int qpeg_table_w[16] =
112  { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
113 
114 /* Decodes delta frames */
116  int stride, int width, int height,
117  int delta, const uint8_t *ctable,
118  uint8_t *refdata)
119 {
120  int i, j;
121  int code;
122  int filled = 0;
123  int orig_height;
124 
125  if (refdata) {
126  /* copy prev frame */
127  for (i = 0; i < height; i++)
128  memcpy(dst + (i * stride), refdata + (i * stride), width);
129  } else {
130  refdata = dst;
131  }
132 
133  orig_height = height;
134  height--;
135  dst = dst + height * stride;
136 
137  while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
138  code = bytestream2_get_byte(&qctx->buffer);
139 
140  if(delta) {
141  /* motion compensation */
142  while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
143  if(delta == 1) {
144  int me_idx;
145  int me_w, me_h, me_x, me_y;
146  uint8_t *me_plane;
147  int corr, val;
148 
149  /* get block size by index */
150  me_idx = code & 0xF;
151  me_w = qpeg_table_w[me_idx];
152  me_h = qpeg_table_h[me_idx];
153 
154  /* extract motion vector */
155  corr = bytestream2_get_byte(&qctx->buffer);
156 
157  val = corr >> 4;
158  if(val > 7)
159  val -= 16;
160  me_x = val;
161 
162  val = corr & 0xF;
163  if(val > 7)
164  val -= 16;
165  me_y = val;
166 
167  /* check motion vector */
168  if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
169  (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
170  (filled + me_w > width) || (height - me_h < 0))
171  av_log(qctx->avctx, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
172  me_x, me_y, me_w, me_h, filled, height);
173  else {
174  /* do motion compensation */
175  me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
176  for(j = 0; j < me_h; j++) {
177  for(i = 0; i < me_w; i++)
178  dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
179  }
180  }
181  }
182  code = bytestream2_get_byte(&qctx->buffer);
183  }
184  }
185 
186  if(code == 0xE0) /* end-of-picture code */
187  break;
188  if(code > 0xE0) { /* run code: 0xE1..0xFF */
189  int p;
190 
191  code &= 0x1F;
192  p = bytestream2_get_byte(&qctx->buffer);
193  for(i = 0; i <= code; i++) {
194  dst[filled++] = p;
195  if(filled >= width) {
196  filled = 0;
197  dst -= stride;
198  height--;
199  if (height < 0)
200  break;
201  }
202  }
203  } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
204  code &= 0x1F;
205 
206  if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
207  break;
208 
209  for(i = 0; i <= code; i++) {
210  dst[filled++] = bytestream2_get_byte(&qctx->buffer);
211  if(filled >= width) {
212  filled = 0;
213  dst -= stride;
214  height--;
215  if (height < 0)
216  break;
217  }
218  }
219  } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
220  int skip;
221 
222  code &= 0x3F;
223  /* codes 0x80 and 0x81 are actually escape codes,
224  skip value minus constant is in the next byte */
225  if(!code)
226  skip = bytestream2_get_byte(&qctx->buffer) + 64;
227  else if(code == 1)
228  skip = bytestream2_get_byte(&qctx->buffer) + 320;
229  else
230  skip = code;
231  filled += skip;
232  while( filled >= width) {
233  filled -= width;
234  dst -= stride;
235  height--;
236  if(height < 0)
237  break;
238  }
239  } else {
240  /* zero code treated as one-pixel skip */
241  if(code) {
242  dst[filled++] = ctable[code & 0x7F];
243  }
244  else
245  filled++;
246  if(filled >= width) {
247  filled = 0;
248  dst -= stride;
249  height--;
250  }
251  }
252  }
253 }
254 
256  void *data, int *got_frame,
257  AVPacket *avpkt)
258 {
259  uint8_t ctable[128];
260  QpegContext * const a = avctx->priv_data;
261  AVFrame * const p = a->pic;
262  AVFrame * const ref = a->ref;
263  uint8_t* outdata;
264  int delta, ret;
265  int pal_size;
266  const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
267 
268  if (avpkt->size < 0x86) {
269  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
270  return AVERROR_INVALIDDATA;
271  }
272 
273  bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
274 
275  av_frame_unref(ref);
276  av_frame_move_ref(ref, p);
277 
278  if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
279  return ret;
280  outdata = p->data[0];
281  bytestream2_skip(&a->buffer, 4);
282  bytestream2_get_buffer(&a->buffer, ctable, 128);
283  bytestream2_skip(&a->buffer, 1);
284 
285  delta = bytestream2_get_byte(&a->buffer);
286  if(delta == 0x10) {
287  qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
288  } else {
289  qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, ref->data[0]);
290  }
291 
292  /* make the palette available on the way out */
293  if (pal && pal_size == AVPALETTE_SIZE) {
294  p->palette_has_changed = 1;
295  memcpy(a->pal, pal, AVPALETTE_SIZE);
296  } else if (pal) {
297  av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
298  }
299  memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
300 
301  if ((ret = av_frame_ref(data, p)) < 0)
302  return ret;
303 
304  *got_frame = 1;
305 
306  return avpkt->size;
307 }
308 
310  QpegContext * const a = avctx->priv_data;
311  int i, pal_size;
312  const uint8_t *pal_src;
313 
314  pal_size = FFMIN(1024U, avctx->extradata_size);
315  pal_src = avctx->extradata + avctx->extradata_size - pal_size;
316 
317  for (i=0; i<pal_size/4; i++)
318  a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
319 }
320 
322 {
323  QpegContext * const a = avctx->priv_data;
324 
325  av_frame_free(&a->pic);
326  av_frame_free(&a->ref);
327 
328  return 0;
329 }
330 
332  QpegContext * const a = avctx->priv_data;
333 
334  a->avctx = avctx;
335  avctx->pix_fmt= AV_PIX_FMT_PAL8;
336 
337  decode_flush(avctx);
338 
339  a->pic = av_frame_alloc();
340  a->ref = av_frame_alloc();
341  if (!a->pic || !a->ref) {
342  decode_end(avctx);
343  return AVERROR(ENOMEM);
344  }
345 
346  return 0;
347 }
348 
350  .name = "qpeg",
351  .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
352  .type = AVMEDIA_TYPE_VIDEO,
353  .id = AV_CODEC_ID_QPEG,
354  .priv_data_size = sizeof(QpegContext),
355  .init = decode_init,
356  .close = decode_end,
357  .decode = decode_frame,
358  .flush = decode_flush,
359  .capabilities = AV_CODEC_CAP_DR1,
360 };
const char const char void * val
Definition: avisynth_c.h:771
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void copy(const float *p1, float *p2, const int length)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
static void flush(AVCodecContext *avctx)
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1446
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:582
uint32_t pal[256]
Definition: qpeg.c:34
uint8_t run
Definition: svq3.c:206
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3424
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
float delta
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
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:443
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1634
const char data[16]
Definition: mxf.c:91
#define height
uint8_t * data
Definition: avcodec.h:1445
static const uint64_t c1
Definition: murmur3.c:49
static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst, int stride, int width, int height)
Definition: qpeg.c:38
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
static av_cold int decode_init(AVCodecContext *avctx)
Definition: qpeg.c:331
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: avcodec.h:1158
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
static void decode_flush(AVCodecContext *avctx)
Definition: qpeg.c:309
#define FFMIN(a, b)
Definition: common.h:96
#define width
int width
picture width / height.
Definition: avcodec.h:1706
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qpeg.c:255
AVFrame * pic
Definition: qpeg.c:33
#define AV_RL32
Definition: intreadwrite.h:146
AVCodec ff_qpeg_decoder
Definition: qpeg.c:349
static const int qpeg_table_w[16]
Definition: qpeg.c:111
static const int qpeg_table_h[16]
Definition: qpeg.c:109
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
main external API structure.
Definition: avcodec.h:1533
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:115
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1918
int extradata_size
Definition: avcodec.h:1635
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:383
GetByteContext buffer
Definition: qpeg.c:35
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
common internal api header.
AVCodecContext * avctx
Definition: qpeg.c:32
static av_cold int decode_end(AVCodecContext *avctx)
Definition: qpeg.c:321
AVFrame * ref
Definition: qpeg.c:33
void * priv_data
Definition: avcodec.h:1560
#define av_noinline
Definition: attributes.h:62
This structure stores compressed data.
Definition: avcodec.h:1422
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1144
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968