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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 
30 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  for(i = 0; i < copy; i++) {
94  dst[filled++] = bytestream2_get_byte(&qctx->buffer);
95  if (filled >= width) {
96  filled = 0;
97  dst -= stride;
98  rows_to_go--;
99  if(rows_to_go <= 0)
100  break;
101  }
102  }
103  }
104  }
105 }
106 
107 static const int qpeg_table_h[16] =
108  { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
109 static const int qpeg_table_w[16] =
110  { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
111 
112 /* Decodes delta frames */
113 static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
114  int stride, int width, int height,
115  int delta, const uint8_t *ctable,
116  uint8_t *refdata)
117 {
118  int i, j;
119  int code;
120  int filled = 0;
121  int orig_height;
122 
123  /* copy prev frame */
124  for(i = 0; i < height; i++)
125  memcpy(refdata + (i * width), dst + (i * stride), width);
126 
127  orig_height = height;
128  height--;
129  dst = dst + height * stride;
130 
131  while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
132  code = bytestream2_get_byte(&qctx->buffer);
133 
134  if(delta) {
135  /* motion compensation */
136  while((code & 0xF0) == 0xF0) {
137  if(delta == 1) {
138  int me_idx;
139  int me_w, me_h, me_x, me_y;
140  uint8_t *me_plane;
141  int corr, val;
142 
143  /* get block size by index */
144  me_idx = code & 0xF;
145  me_w = qpeg_table_w[me_idx];
146  me_h = qpeg_table_h[me_idx];
147 
148  /* extract motion vector */
149  corr = bytestream2_get_byte(&qctx->buffer);
150 
151  val = corr >> 4;
152  if(val > 7)
153  val -= 16;
154  me_x = val;
155 
156  val = corr & 0xF;
157  if(val > 7)
158  val -= 16;
159  me_y = val;
160 
161  /* check motion vector */
162  if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
163  (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
164  (filled + me_w > width) || (height - me_h < 0))
165  av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
166  me_x, me_y, me_w, me_h, filled, height);
167  else {
168  /* do motion compensation */
169  me_plane = refdata + (filled + me_x) + (height - me_y) * width;
170  for(j = 0; j < me_h; j++) {
171  for(i = 0; i < me_w; i++)
172  dst[filled + i - (j * stride)] = me_plane[i - (j * width)];
173  }
174  }
175  }
176  code = bytestream2_get_byte(&qctx->buffer);
177  }
178  }
179 
180  if(code == 0xE0) /* end-of-picture code */
181  break;
182  if(code > 0xE0) { /* run code: 0xE1..0xFF */
183  int p;
184 
185  code &= 0x1F;
186  p = bytestream2_get_byte(&qctx->buffer);
187  for(i = 0; i <= code; i++) {
188  dst[filled++] = p;
189  if(filled >= width) {
190  filled = 0;
191  dst -= stride;
192  height--;
193  if (height < 0)
194  break;
195  }
196  }
197  } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
198  code &= 0x1F;
199 
200  for(i = 0; i <= code; i++) {
201  dst[filled++] = bytestream2_get_byte(&qctx->buffer);
202  if(filled >= width) {
203  filled = 0;
204  dst -= stride;
205  height--;
206  if (height < 0)
207  break;
208  }
209  }
210  } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
211  int skip;
212 
213  code &= 0x3F;
214  /* codes 0x80 and 0x81 are actually escape codes,
215  skip value minus constant is in the next byte */
216  if(!code)
217  skip = bytestream2_get_byte(&qctx->buffer) + 64;
218  else if(code == 1)
219  skip = bytestream2_get_byte(&qctx->buffer) + 320;
220  else
221  skip = code;
222  filled += skip;
223  while( filled >= width) {
224  filled -= width;
225  dst -= stride;
226  height--;
227  if(height < 0)
228  break;
229  }
230  } else {
231  /* zero code treated as one-pixel skip */
232  if(code) {
233  dst[filled++] = ctable[code & 0x7F];
234  }
235  else
236  filled++;
237  if(filled >= width) {
238  filled = 0;
239  dst -= stride;
240  height--;
241  }
242  }
243  }
244 }
245 
246 static int decode_frame(AVCodecContext *avctx,
247  void *data, int *got_frame,
248  AVPacket *avpkt)
249 {
250  uint8_t ctable[128];
251  QpegContext * const a = avctx->priv_data;
252  AVFrame * const p = &a->pic;
253  uint8_t* outdata;
254  int delta;
256 
257  if (avpkt->size < 0x86) {
258  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
259  return AVERROR_INVALIDDATA;
260  }
261 
262  bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
263  p->reference = 3;
264  if (avctx->reget_buffer(avctx, p) < 0) {
265  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
266  return -1;
267  }
268  outdata = a->pic.data[0];
269  bytestream2_skip(&a->buffer, 4);
270  bytestream2_get_buffer(&a->buffer, ctable, 128);
271  bytestream2_skip(&a->buffer, 1);
272 
273  delta = bytestream2_get_byte(&a->buffer);
274  if(delta == 0x10) {
275  qpeg_decode_intra(a, outdata, a->pic.linesize[0], avctx->width, avctx->height);
276  } else {
277  qpeg_decode_inter(a, outdata, a->pic.linesize[0], avctx->width, avctx->height, delta, ctable, a->refdata);
278  }
279 
280  /* make the palette available on the way out */
281  if (pal) {
282  a->pic.palette_has_changed = 1;
283  memcpy(a->pal, pal, AVPALETTE_SIZE);
284  }
285  memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
286 
287  *got_frame = 1;
288  *(AVFrame*)data = a->pic;
289 
290  return avpkt->size;
291 }
292 
293 static av_cold int decode_init(AVCodecContext *avctx){
294  QpegContext * const a = avctx->priv_data;
295 
296  a->avctx = avctx;
297  avctx->pix_fmt= AV_PIX_FMT_PAL8;
298  a->refdata = av_malloc(avctx->width * avctx->height);
299 
300  return 0;
301 }
302 
303 static av_cold int decode_end(AVCodecContext *avctx){
304  QpegContext * const a = avctx->priv_data;
305  AVFrame * const p = &a->pic;
306 
307  if(p->data[0])
308  avctx->release_buffer(avctx, p);
309 
310  av_free(a->refdata);
311  return 0;
312 }
313 
315  .name = "qpeg",
316  .type = AVMEDIA_TYPE_VIDEO,
317  .id = AV_CODEC_ID_QPEG,
318  .priv_data_size = sizeof(QpegContext),
319  .init = decode_init,
320  .close = decode_end,
321  .decode = decode_frame,
322  .capabilities = CODEC_CAP_DR1,
323  .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
324 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
uint32_t pal[256]
Definition: qpeg.c:34
uint8_t run
Definition: svq3.c:124
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2273
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
float delta
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
AVFrame pic
Definition: qpeg.c:32
uint8_t * refdata
Definition: qpeg.c:33
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
Definition: lzo.c:79
static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst, int stride, int width, int height)
Definition: qpeg.c:38
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static void 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:113
static av_cold int decode_init(AVCodecContext *avctx)
Definition: qpeg.c:293
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:248
struct QpegContext QpegContext
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int width
picture width / height.
Definition: avcodec.h:1508
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qpeg.c:246
AVCodec ff_qpeg_decoder
Definition: qpeg.c:314
static const int qpeg_table_w[16]
Definition: qpeg.c:109
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
static const int qpeg_table_h[16]
Definition: qpeg.c:107
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
GetByteContext buffer
Definition: qpeg.c:35
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
AVCodecContext * avctx
Definition: qpeg.c:31
static av_cold int decode_end(AVCodecContext *avctx)
Definition: qpeg.c:303
void * priv_data
Definition: avcodec.h:1382
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:190
This structure stores compressed data.
Definition: avcodec.h:898
#define c1
Definition: idct_sh4.c:27