Libav
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 #include "internal.h"
30 
31 typedef struct QpegContext{
35  uint32_t pal[256];
37 } QpegContext;
38 
39 static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
40  int stride, int width, int height)
41 {
42  int i;
43  int code;
44  int c0, c1;
45  int run, copy;
46  int filled = 0;
47  int rows_to_go;
48 
49  rows_to_go = height;
50  height--;
51  dst = dst + height * stride;
52 
53  while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
54  code = bytestream2_get_byte(&qctx->buffer);
55  run = copy = 0;
56  if(code == 0xFC) /* end-of-picture code */
57  break;
58  if(code >= 0xF8) { /* very long run */
59  c0 = bytestream2_get_byte(&qctx->buffer);
60  c1 = bytestream2_get_byte(&qctx->buffer);
61  run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
62  } else if (code >= 0xF0) { /* long run */
63  c0 = bytestream2_get_byte(&qctx->buffer);
64  run = ((code & 0xF) << 8) + c0 + 2;
65  } else if (code >= 0xE0) { /* short run */
66  run = (code & 0x1F) + 2;
67  } else if (code >= 0xC0) { /* very long copy */
68  c0 = bytestream2_get_byte(&qctx->buffer);
69  c1 = bytestream2_get_byte(&qctx->buffer);
70  copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
71  } else if (code >= 0x80) { /* long copy */
72  c0 = bytestream2_get_byte(&qctx->buffer);
73  copy = ((code & 0x7F) << 8) + c0 + 1;
74  } else { /* short copy */
75  copy = code + 1;
76  }
77 
78  /* perform actual run or copy */
79  if(run) {
80  int p;
81 
82  p = bytestream2_get_byte(&qctx->buffer);
83  for(i = 0; i < run; i++) {
84  dst[filled++] = p;
85  if (filled >= width) {
86  filled = 0;
87  dst -= stride;
88  rows_to_go--;
89  if(rows_to_go <= 0)
90  break;
91  }
92  }
93  } else {
94  for(i = 0; i < copy; i++) {
95  dst[filled++] = bytestream2_get_byte(&qctx->buffer);
96  if (filled >= width) {
97  filled = 0;
98  dst -= stride;
99  rows_to_go--;
100  if(rows_to_go <= 0)
101  break;
102  }
103  }
104  }
105  }
106 }
107 
108 static const int qpeg_table_h[16] =
109  { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
110 static const int qpeg_table_w[16] =
111  { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
112 
113 /* Decodes delta frames */
114 static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
115  int stride, int width, int height,
116  int delta, const uint8_t *ctable,
117  uint8_t *refdata)
118 {
119  int i, j;
120  int code;
121  int filled = 0;
122  int orig_height;
123 
124  /* copy prev frame */
125  for(i = 0; i < height; i++)
126  memcpy(refdata + (i * width), dst + (i * stride), width);
127 
128  orig_height = height;
129  height--;
130  dst = dst + height * stride;
131 
132  while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
133  code = bytestream2_get_byte(&qctx->buffer);
134 
135  if(delta) {
136  /* motion compensation */
137  while((code & 0xF0) == 0xF0) {
138  if(delta == 1) {
139  int me_idx;
140  int me_w, me_h, me_x, me_y;
141  uint8_t *me_plane;
142  int corr, val;
143 
144  /* get block size by index */
145  me_idx = code & 0xF;
146  me_w = qpeg_table_w[me_idx];
147  me_h = qpeg_table_h[me_idx];
148 
149  /* extract motion vector */
150  corr = bytestream2_get_byte(&qctx->buffer);
151 
152  val = corr >> 4;
153  if(val > 7)
154  val -= 16;
155  me_x = val;
156 
157  val = corr & 0xF;
158  if(val > 7)
159  val -= 16;
160  me_y = val;
161 
162  /* check motion vector */
163  if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
164  (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
165  (filled + me_w > width) || (height - me_h < 0))
166  av_log(qctx->avctx, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
167  me_x, me_y, me_w, me_h, filled, height);
168  else {
169  /* do motion compensation */
170  me_plane = refdata + (filled + me_x) + (height - me_y) * width;
171  for(j = 0; j < me_h; j++) {
172  for(i = 0; i < me_w; i++)
173  dst[filled + i - (j * stride)] = me_plane[i - (j * width)];
174  }
175  }
176  }
177  code = bytestream2_get_byte(&qctx->buffer);
178  }
179  }
180 
181  if(code == 0xE0) /* end-of-picture code */
182  break;
183  if(code > 0xE0) { /* run code: 0xE1..0xFF */
184  int p;
185 
186  code &= 0x1F;
187  p = bytestream2_get_byte(&qctx->buffer);
188  for(i = 0; i <= code; i++) {
189  dst[filled++] = p;
190  if(filled >= width) {
191  filled = 0;
192  dst -= stride;
193  height--;
194  if (height < 0)
195  break;
196  }
197  }
198  } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
199  code &= 0x1F;
200 
201  for(i = 0; i <= code; i++) {
202  dst[filled++] = bytestream2_get_byte(&qctx->buffer);
203  if(filled >= width) {
204  filled = 0;
205  dst -= stride;
206  height--;
207  if (height < 0)
208  break;
209  }
210  }
211  } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
212  int skip;
213 
214  code &= 0x3F;
215  /* codes 0x80 and 0x81 are actually escape codes,
216  skip value minus constant is in the next byte */
217  if(!code)
218  skip = bytestream2_get_byte(&qctx->buffer) + 64;
219  else if(code == 1)
220  skip = bytestream2_get_byte(&qctx->buffer) + 320;
221  else
222  skip = code;
223  filled += skip;
224  while( filled >= width) {
225  filled -= width;
226  dst -= stride;
227  height--;
228  if(height < 0)
229  break;
230  }
231  } else {
232  /* zero code treated as one-pixel skip */
233  if(code) {
234  dst[filled++] = ctable[code & 0x7F];
235  }
236  else
237  filled++;
238  if(filled >= width) {
239  filled = 0;
240  dst -= stride;
241  height--;
242  }
243  }
244  }
245 }
246 
247 static int decode_frame(AVCodecContext *avctx,
248  void *data, int *got_frame,
249  AVPacket *avpkt)
250 {
251  uint8_t ctable[128];
252  QpegContext * const a = avctx->priv_data;
253  AVFrame * const p = a->pic;
254  uint8_t* outdata;
255  int delta, ret;
257 
258  if (avpkt->size < 0x86) {
259  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
260  return AVERROR_INVALIDDATA;
261  }
262 
263  bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
264  if ((ret = ff_reget_buffer(avctx, p)) < 0) {
265  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
266  return ret;
267  }
268  outdata = p->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, p->linesize[0], avctx->width, avctx->height);
276  } else {
277  qpeg_decode_inter(a, outdata, p->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  p->palette_has_changed = 1;
283  memcpy(a->pal, pal, AVPALETTE_SIZE);
284  }
285  memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
286 
287  if ((ret = av_frame_ref(data, p)) < 0)
288  return ret;
289 
290  *got_frame = 1;
291 
292  return avpkt->size;
293 }
294 
296 {
297  QpegContext * const a = avctx->priv_data;
298 
299  av_frame_free(&a->pic);
300 
301  av_free(a->refdata);
302  return 0;
303 }
304 
305 static av_cold int decode_init(AVCodecContext *avctx){
306  QpegContext * const a = avctx->priv_data;
307 
308  a->avctx = avctx;
309  avctx->pix_fmt= AV_PIX_FMT_PAL8;
310  a->refdata = av_malloc(avctx->width * avctx->height);
311 
312  a->pic = av_frame_alloc();
313  if (!a->pic) {
314  decode_end(avctx);
315  return AVERROR(ENOMEM);
316  }
317 
318  return 0;
319 }
320 
322  .name = "qpeg",
323  .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
324  .type = AVMEDIA_TYPE_VIDEO,
325  .id = AV_CODEC_ID_QPEG,
326  .priv_data_size = sizeof(QpegContext),
327  .init = decode_init,
328  .close = decode_end,
329  .decode = decode_frame,
330  .capabilities = CODEC_CAP_DR1,
331 };
#define AVPALETTE_SIZE
Definition: avcodec.h:3051
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:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
uint32_t pal[256]
Definition: qpeg.c:35
uint8_t run
Definition: svq3.c:146
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2812
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
float delta
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
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:188
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
uint8_t * refdata
Definition: qpeg.c:34
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
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:39
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:114
static av_cold int decode_init(AVCodecContext *avctx)
Definition: qpeg.c:305
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:161
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:260
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:820
int width
picture width / height.
Definition: avcodec.h:1229
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qpeg.c:247
AVFrame * pic
Definition: qpeg.c:33
AVCodec ff_qpeg_decoder
Definition: qpeg.c:321
static const int qpeg_table_w[16]
Definition: qpeg.c:110
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
static const int qpeg_table_h[16]
Definition: qpeg.c:108
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:330
GetByteContext buffer
Definition: qpeg.c:36
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int height
Definition: gxfenc.c:72
common internal api header.
AVCodecContext * avctx
Definition: qpeg.c:32
static av_cold int decode_end(AVCodecContext *avctx)
Definition: qpeg.c:295
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:287
This structure stores compressed data.
Definition: avcodec.h:950