]> git.sesse.net Git - ffmpeg/blob - libavcodec/qpeg.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / qpeg.c
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
30 typedef struct QpegContext{
31     AVCodecContext *avctx;
32     AVFrame pic, ref;
33     uint32_t pal[256];
34     GetByteContext buffer;
35 } QpegContext;
36
37 static void qpeg_decode_intra(QpegContext *qctx, uint8_t *dst,
38                               int stride, int width, int height)
39 {
40     int i;
41     int code;
42     int c0, c1;
43     int run, copy;
44     int filled = 0;
45     int rows_to_go;
46
47     rows_to_go = height;
48     height--;
49     dst = dst + height * stride;
50
51     while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (rows_to_go > 0)) {
52         code = bytestream2_get_byte(&qctx->buffer);
53         run = copy = 0;
54         if(code == 0xFC) /* end-of-picture code */
55             break;
56         if(code >= 0xF8) { /* very long run */
57             c0 = bytestream2_get_byte(&qctx->buffer);
58             c1 = bytestream2_get_byte(&qctx->buffer);
59             run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
60         } else if (code >= 0xF0) { /* long run */
61             c0 = bytestream2_get_byte(&qctx->buffer);
62             run = ((code & 0xF) << 8) + c0 + 2;
63         } else if (code >= 0xE0) { /* short run */
64             run = (code & 0x1F) + 2;
65         } else if (code >= 0xC0) { /* very long copy */
66             c0 = bytestream2_get_byte(&qctx->buffer);
67             c1 = bytestream2_get_byte(&qctx->buffer);
68             copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
69         } else if (code >= 0x80) { /* long copy */
70             c0 = bytestream2_get_byte(&qctx->buffer);
71             copy = ((code & 0x7F) << 8) + c0 + 1;
72         } else { /* short copy */
73             copy = code + 1;
74         }
75
76         /* perform actual run or copy */
77         if(run) {
78             int p;
79
80             p = bytestream2_get_byte(&qctx->buffer);
81             for(i = 0; i < run; i++) {
82                 dst[filled++] = p;
83                 if (filled >= width) {
84                     filled = 0;
85                     dst -= stride;
86                     rows_to_go--;
87                     if(rows_to_go <= 0)
88                         break;
89                 }
90             }
91         } else {
92             for(i = 0; i < copy; i++) {
93                 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
94                 if (filled >= width) {
95                     filled = 0;
96                     dst -= stride;
97                     rows_to_go--;
98                     if(rows_to_go <= 0)
99                         break;
100                 }
101             }
102         }
103     }
104 }
105
106 static const int qpeg_table_h[16] =
107  { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
108 static const int qpeg_table_w[16] =
109  { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
110
111 /* Decodes delta frames */
112 static void qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
113                               int stride, int width, int height,
114                               int delta, const uint8_t *ctable,
115                               uint8_t *refdata)
116 {
117     int i, j;
118     int code;
119     int filled = 0;
120     int orig_height;
121
122     if(!refdata)
123         refdata= dst;
124
125     /* copy prev frame */
126     for(i = 0; i < height; i++)
127         memcpy(dst + (i * stride), refdata + (i * stride), width);
128
129     orig_height = height;
130     height--;
131     dst = dst + height * stride;
132
133     while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
134         code = bytestream2_get_byte(&qctx->buffer);
135
136         if(delta) {
137             /* motion compensation */
138             while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
139                 if(delta == 1) {
140                     int me_idx;
141                     int me_w, me_h, me_x, me_y;
142                     uint8_t *me_plane;
143                     int corr, val;
144
145                     /* get block size by index */
146                     me_idx = code & 0xF;
147                     me_w = qpeg_table_w[me_idx];
148                     me_h = qpeg_table_h[me_idx];
149
150                     /* extract motion vector */
151                     corr = bytestream2_get_byte(&qctx->buffer);
152
153                     val = corr >> 4;
154                     if(val > 7)
155                         val -= 16;
156                     me_x = val;
157
158                     val = corr & 0xF;
159                     if(val > 7)
160                         val -= 16;
161                     me_y = val;
162
163                     /* check motion vector */
164                     if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
165                        (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
166                        (filled + me_w > width) || (height - me_h < 0))
167                         av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
168                                me_x, me_y, me_w, me_h, filled, height);
169                     else {
170                         /* do motion compensation */
171                         me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
172                         for(j = 0; j < me_h; j++) {
173                             for(i = 0; i < me_w; i++)
174                                 dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
175                         }
176                     }
177                 }
178                 code = bytestream2_get_byte(&qctx->buffer);
179             }
180         }
181
182         if(code == 0xE0) /* end-of-picture code */
183             break;
184         if(code > 0xE0) { /* run code: 0xE1..0xFF */
185             int p;
186
187             code &= 0x1F;
188             p = bytestream2_get_byte(&qctx->buffer);
189             for(i = 0; i <= code; i++) {
190                 dst[filled++] = p;
191                 if(filled >= width) {
192                     filled = 0;
193                     dst -= stride;
194                     height--;
195                     if(height < 0)
196                         break;
197                 }
198             }
199         } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
200             code &= 0x1F;
201
202             if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
203                 break;
204
205             for(i = 0; i <= code; i++) {
206                 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
207                 if(filled >= width) {
208                     filled = 0;
209                     dst -= stride;
210                     height--;
211                     if(height < 0)
212                         break;
213                 }
214             }
215         } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
216             int skip;
217
218             code &= 0x3F;
219             /* codes 0x80 and 0x81 are actually escape codes,
220                skip value minus constant is in the next byte */
221             if(!code)
222                 skip = bytestream2_get_byte(&qctx->buffer) +  64;
223             else if(code == 1)
224                 skip = bytestream2_get_byte(&qctx->buffer) + 320;
225             else
226                 skip = code;
227             filled += skip;
228             while( filled >= width) {
229                 filled -= width;
230                 dst -= stride;
231                 height--;
232                 if(height < 0)
233                     break;
234             }
235         } else {
236             /* zero code treated as one-pixel skip */
237             if(code) {
238                 dst[filled++] = ctable[code & 0x7F];
239             }
240             else
241                 filled++;
242             if(filled >= width) {
243                 filled = 0;
244                 dst -= stride;
245                 height--;
246             }
247         }
248     }
249 }
250
251 static int decode_frame(AVCodecContext *avctx,
252                         void *data, int *data_size,
253                         AVPacket *avpkt)
254 {
255     uint8_t ctable[128];
256     QpegContext * const a = avctx->priv_data;
257     AVFrame *  p = &a->pic;
258     AVFrame * ref= &a->ref;
259     uint8_t* outdata;
260     int delta;
261     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
262
263     if (avpkt->size < 0x86) {
264         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
265         return AVERROR_INVALIDDATA;
266     }
267
268     bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
269
270     if(ref->data[0])
271         avctx->release_buffer(avctx, ref);
272     FFSWAP(AVFrame, *ref, *p);
273
274     p->reference= 3;
275     if(avctx->get_buffer(avctx, p) < 0){
276         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
277         return -1;
278     }
279     outdata = a->pic.data[0];
280     bytestream2_skip(&a->buffer, 4);
281     bytestream2_get_buffer(&a->buffer, ctable, 128);
282     bytestream2_skip(&a->buffer, 1);
283
284     delta = bytestream2_get_byte(&a->buffer);
285     if(delta == 0x10) {
286         qpeg_decode_intra(a, outdata, a->pic.linesize[0], avctx->width, avctx->height);
287     } else {
288         qpeg_decode_inter(a, outdata, a->pic.linesize[0], avctx->width, avctx->height, delta, ctable, a->ref.data[0]);
289     }
290
291     /* make the palette available on the way out */
292     if (pal) {
293         a->pic.palette_has_changed = 1;
294         memcpy(a->pal, pal, AVPALETTE_SIZE);
295     }
296     memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
297
298     *data_size = sizeof(AVFrame);
299     *(AVFrame*)data = a->pic;
300
301     return avpkt->size;
302 }
303
304 static av_cold int decode_init(AVCodecContext *avctx){
305     QpegContext * const a = avctx->priv_data;
306
307     avcodec_get_frame_defaults(&a->pic);
308     avcodec_get_frame_defaults(&a->ref);
309     a->avctx = avctx;
310     avctx->pix_fmt= PIX_FMT_PAL8;
311
312     return 0;
313 }
314
315 static av_cold int decode_end(AVCodecContext *avctx){
316     QpegContext * const a = avctx->priv_data;
317     AVFrame * const p = &a->pic;
318     AVFrame * const ref= &a->ref;
319
320     if(p->data[0])
321         avctx->release_buffer(avctx, p);
322     if(ref->data[0])
323         avctx->release_buffer(avctx, ref);
324
325     return 0;
326 }
327
328 AVCodec ff_qpeg_decoder = {
329     .name           = "qpeg",
330     .type           = AVMEDIA_TYPE_VIDEO,
331     .id             = CODEC_ID_QPEG,
332     .priv_data_size = sizeof(QpegContext),
333     .init           = decode_init,
334     .close          = decode_end,
335     .decode         = decode_frame,
336     .capabilities   = CODEC_CAP_DR1,
337     .long_name      = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
338 };