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  }
194  }
195  } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
196  code &= 0x1F;
197 
198  for(i = 0; i <= code; i++) {
199  dst[filled++] = bytestream2_get_byte(&qctx->buffer);
200  if(filled >= width) {
201  filled = 0;
202  dst -= stride;
203  height--;
204  }
205  }
206  } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
207  int skip;
208 
209  code &= 0x3F;
210  /* codes 0x80 and 0x81 are actually escape codes,
211  skip value minus constant is in the next byte */
212  if(!code)
213  skip = bytestream2_get_byte(&qctx->buffer) + 64;
214  else if(code == 1)
215  skip = bytestream2_get_byte(&qctx->buffer) + 320;
216  else
217  skip = code;
218  filled += skip;
219  while( filled >= width) {
220  filled -= width;
221  dst -= stride;
222  height--;
223  if(height < 0)
224  break;
225  }
226  } else {
227  /* zero code treated as one-pixel skip */
228  if(code) {
229  dst[filled++] = ctable[code & 0x7F];
230  }
231  else
232  filled++;
233  if(filled >= width) {
234  filled = 0;
235  dst -= stride;
236  height--;
237  }
238  }
239  }
240 }
241 
242 static int decode_frame(AVCodecContext *avctx,
243  void *data, int *got_frame,
244  AVPacket *avpkt)
245 {
246  uint8_t ctable[128];
247  QpegContext * const a = avctx->priv_data;
248  AVFrame * const p = &a->pic;
249  uint8_t* outdata;
250  int delta;
252 
253  if (avpkt->size < 0x86) {
254  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
255  return AVERROR_INVALIDDATA;
256  }
257 
258  bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
259  p->reference = 3;
260  if (avctx->reget_buffer(avctx, p) < 0) {
261  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
262  return -1;
263  }
264  outdata = a->pic.data[0];
265  bytestream2_skip(&a->buffer, 4);
266  bytestream2_get_buffer(&a->buffer, ctable, 128);
267  bytestream2_skip(&a->buffer, 1);
268 
269  delta = bytestream2_get_byte(&a->buffer);
270  if(delta == 0x10) {
271  qpeg_decode_intra(a, outdata, a->pic.linesize[0], avctx->width, avctx->height);
272  } else {
273  qpeg_decode_inter(a, outdata, a->pic.linesize[0], avctx->width, avctx->height, delta, ctable, a->refdata);
274  }
275 
276  /* make the palette available on the way out */
277  if (pal) {
278  a->pic.palette_has_changed = 1;
279  memcpy(a->pal, pal, AVPALETTE_SIZE);
280  }
281  memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
282 
283  *got_frame = 1;
284  *(AVFrame*)data = a->pic;
285 
286  return avpkt->size;
287 }
288 
289 static av_cold int decode_init(AVCodecContext *avctx){
290  QpegContext * const a = avctx->priv_data;
291 
292  a->avctx = avctx;
293  avctx->pix_fmt= AV_PIX_FMT_PAL8;
294  a->refdata = av_malloc(avctx->width * avctx->height);
295 
296  return 0;
297 }
298 
299 static av_cold int decode_end(AVCodecContext *avctx){
300  QpegContext * const a = avctx->priv_data;
301  AVFrame * const p = &a->pic;
302 
303  if(p->data[0])
304  avctx->release_buffer(avctx, p);
305 
306  av_free(a->refdata);
307  return 0;
308 }
309 
311  .name = "qpeg",
312  .type = AVMEDIA_TYPE_VIDEO,
313  .id = AV_CODEC_ID_QPEG,
314  .priv_data_size = sizeof(QpegContext),
315  .init = decode_init,
316  .close = decode_end,
317  .decode = decode_frame,
318  .capabilities = CODEC_CAP_DR1,
319  .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
320 };