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 
29 typedef struct QpegContext{
32  uint8_t *refdata;
33  uint32_t pal[256];
34 } QpegContext;
35 
36 static void qpeg_decode_intra(const uint8_t *src, uint8_t *dst, int size,
37  int stride, int width, int height)
38 {
39  int i;
40  int code;
41  int c0, c1;
42  int run, copy;
43  int filled = 0;
44  int rows_to_go;
45 
46  rows_to_go = height;
47  height--;
48  dst = dst + height * stride;
49 
50  while((size > 0) && (rows_to_go > 0)) {
51  code = *src++;
52  size--;
53  run = copy = 0;
54  if(code == 0xFC) /* end-of-picture code */
55  break;
56  if(code >= 0xF8) { /* very long run */
57  c0 = *src++;
58  c1 = *src++;
59  size -= 2;
60  run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
61  } else if (code >= 0xF0) { /* long run */
62  c0 = *src++;
63  size--;
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 = *src++;
69  c1 = *src++;
70  size -= 2;
71  copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
72  } else if (code >= 0x80) { /* long copy */
73  c0 = *src++;
74  size--;
75  copy = ((code & 0x7F) << 8) + c0 + 1;
76  } else { /* short copy */
77  copy = code + 1;
78  }
79 
80  /* perform actual run or copy */
81  if(run) {
82  int p;
83 
84  p = *src++;
85  size--;
86  for(i = 0; i < run; i++) {
87  dst[filled++] = p;
88  if (filled >= width) {
89  filled = 0;
90  dst -= stride;
91  rows_to_go--;
92  if(rows_to_go <= 0)
93  break;
94  }
95  }
96  } else {
97  size -= copy;
98  for(i = 0; i < copy; i++) {
99  dst[filled++] = *src++;
100  if (filled >= width) {
101  filled = 0;
102  dst -= stride;
103  rows_to_go--;
104  if(rows_to_go <= 0)
105  break;
106  }
107  }
108  }
109  }
110 }
111 
112 static const int qpeg_table_h[16] =
113  { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
114 static const int qpeg_table_w[16] =
115  { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
116 
117 /* Decodes delta frames */
118 static void qpeg_decode_inter(const uint8_t *src, uint8_t *dst, int size,
119  int stride, int width, int height,
120  int delta, const uint8_t *ctable, uint8_t *refdata)
121 {
122  int i, j;
123  int code;
124  int filled = 0;
125  int orig_height;
126 
127  /* copy prev frame */
128  for(i = 0; i < height; i++)
129  memcpy(refdata + (i * width), dst + (i * stride), width);
130 
131  orig_height = height;
132  height--;
133  dst = dst + height * stride;
134 
135  while((size > 0) && (height >= 0)) {
136  code = *src++;
137  size--;
138 
139  if(delta) {
140  /* motion compensation */
141  while((code & 0xF0) == 0xF0) {
142  if(delta == 1) {
143  int me_idx;
144  int me_w, me_h, me_x, me_y;
145  uint8_t *me_plane;
146  int corr, val;
147 
148  /* get block size by index */
149  me_idx = code & 0xF;
150  me_w = qpeg_table_w[me_idx];
151  me_h = qpeg_table_h[me_idx];
152 
153  /* extract motion vector */
154  corr = *src++;
155  size--;
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(NULL, 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) * width;
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 * width)];
179  }
180  }
181  }
182  code = *src++;
183  size--;
184  }
185  }
186 
187  if(code == 0xE0) /* end-of-picture code */
188  break;
189  if(code > 0xE0) { /* run code: 0xE1..0xFF */
190  int p;
191 
192  code &= 0x1F;
193  p = *src++;
194  size--;
195  for(i = 0; i <= code; i++) {
196  dst[filled++] = p;
197  if(filled >= width) {
198  filled = 0;
199  dst -= stride;
200  height--;
201  if (height < 0)
202  break;
203  }
204  }
205  } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
206  code &= 0x1F;
207 
208  for(i = 0; i <= code; i++) {
209  dst[filled++] = *src++;
210  if(filled >= width) {
211  filled = 0;
212  dst -= stride;
213  height--;
214  if (height < 0)
215  break;
216  }
217  }
218  size -= code + 1;
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 = (*src++) + 64;
227  else if(code == 1)
228  skip = (*src++) + 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  else
244  filled++;
245  if(filled >= width) {
246  filled = 0;
247  dst -= stride;
248  height--;
249  }
250  }
251  }
252 }
253 
254 static int decode_frame(AVCodecContext *avctx,
255  void *data, int *data_size,
256  AVPacket *avpkt)
257 {
258  const uint8_t *buf = avpkt->data;
259  int buf_size = avpkt->size;
260  QpegContext * const a = avctx->priv_data;
261  AVFrame * const p= (AVFrame*)&a->pic;
262  uint8_t* outdata;
263  int delta;
264  const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
265 
266  p->reference = 3;
267  if (avctx->reget_buffer(avctx, p) < 0) {
268  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
269  return -1;
270  }
271  outdata = a->pic.data[0];
272  if(buf[0x85] == 0x10) {
273  qpeg_decode_intra(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height);
274  } else {
275  delta = buf[0x85];
276  qpeg_decode_inter(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height, delta, buf + 4, a->refdata);
277  }
278 
279  /* make the palette available on the way out */
280  if (pal) {
281  a->pic.palette_has_changed = 1;
282  memcpy(a->pal, pal, AVPALETTE_SIZE);
283  }
284  memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
285 
286  *data_size = sizeof(AVFrame);
287  *(AVFrame*)data = a->pic;
288 
289  return buf_size;
290 }
291 
292 static av_cold int decode_init(AVCodecContext *avctx){
293  QpegContext * const a = avctx->priv_data;
294 
295  a->avctx = avctx;
296  avctx->pix_fmt= PIX_FMT_PAL8;
297  a->refdata = av_malloc(avctx->width * avctx->height);
298 
299  return 0;
300 }
301 
302 static av_cold int decode_end(AVCodecContext *avctx){
303  QpegContext * const a = avctx->priv_data;
304  AVFrame * const p= (AVFrame*)&a->pic;
305 
306  if(p->data[0])
307  avctx->release_buffer(avctx, p);
308 
309  av_free(a->refdata);
310  return 0;
311 }
312 
314  .name = "qpeg",
315  .type = AVMEDIA_TYPE_VIDEO,
316  .id = CODEC_ID_QPEG,
317  .priv_data_size = sizeof(QpegContext),
318  .init = decode_init,
319  .close = decode_end,
320  .decode = decode_frame,
321  .capabilities = CODEC_CAP_DR1,
322  .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
323 };