]> git.sesse.net Git - ffmpeg/blob - libavcodec/qpeg.c
Merge commit 'eec93e57096aa4804862d62760442380c70d489b'
[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 #include "internal.h"
30
31 typedef struct QpegContext{
32     AVCodecContext *avctx;
33     AVFrame *pic, *ref;
34     uint32_t pal[256];
35     GetByteContext buffer;
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                 int step = FFMIN(run - i, width - filled);
84                 memset(dst+filled, p, step);
85                 filled += step;
86                 i      += step - 1;
87                 if (filled >= width) {
88                     filled = 0;
89                     dst -= stride;
90                     rows_to_go--;
91                     while (run - i > width && rows_to_go > 0) {
92                         memset(dst, p, width);
93                         dst -= stride;
94                         rows_to_go--;
95                         i += width;
96                     }
97                     if(rows_to_go <= 0)
98                         break;
99                 }
100             }
101         } else {
102             for(i = 0; i < copy; i++) {
103                 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
104                 if (filled >= width) {
105                     filled = 0;
106                     dst -= stride;
107                     rows_to_go--;
108                     if(rows_to_go <= 0)
109                         break;
110                 }
111             }
112         }
113     }
114 }
115
116 static const int qpeg_table_h[16] =
117  { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
118 static const int qpeg_table_w[16] =
119  { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
120
121 /* Decodes delta frames */
122 static void av_noinline qpeg_decode_inter(QpegContext *qctx, uint8_t *dst,
123                               int stride, int width, int height,
124                               int delta, const uint8_t *ctable,
125                               uint8_t *refdata)
126 {
127     int i, j;
128     int code;
129     int filled = 0;
130     int orig_height;
131
132     if (refdata) {
133         /* copy prev frame */
134         for (i = 0; i < height; i++)
135             memcpy(dst + (i * stride), refdata + (i * stride), width);
136     } else {
137         refdata = dst;
138     }
139
140     orig_height = height;
141     height--;
142     dst = dst + height * stride;
143
144     while ((bytestream2_get_bytes_left(&qctx->buffer) > 0) && (height >= 0)) {
145         code = bytestream2_get_byte(&qctx->buffer);
146
147         if(delta) {
148             /* motion compensation */
149             while(bytestream2_get_bytes_left(&qctx->buffer) > 0 && (code & 0xF0) == 0xF0) {
150                 if(delta == 1) {
151                     int me_idx;
152                     int me_w, me_h, me_x, me_y;
153                     uint8_t *me_plane;
154                     int corr, val;
155
156                     /* get block size by index */
157                     me_idx = code & 0xF;
158                     me_w = qpeg_table_w[me_idx];
159                     me_h = qpeg_table_h[me_idx];
160
161                     /* extract motion vector */
162                     corr = bytestream2_get_byte(&qctx->buffer);
163
164                     val = corr >> 4;
165                     if(val > 7)
166                         val -= 16;
167                     me_x = val;
168
169                     val = corr & 0xF;
170                     if(val > 7)
171                         val -= 16;
172                     me_y = val;
173
174                     /* check motion vector */
175                     if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
176                        (height - me_y - me_h < 0) || (height - me_y >= orig_height) ||
177                        (filled + me_w > width) || (height - me_h < 0))
178                         av_log(qctx->avctx, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
179                                me_x, me_y, me_w, me_h, filled, height);
180                     else {
181                         /* do motion compensation */
182                         me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
183                         for(j = 0; j < me_h; j++) {
184                             for(i = 0; i < me_w; i++)
185                                 dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
186                         }
187                     }
188                 }
189                 code = bytestream2_get_byte(&qctx->buffer);
190             }
191         }
192
193         if(code == 0xE0) /* end-of-picture code */
194             break;
195         if(code > 0xE0) { /* run code: 0xE1..0xFF */
196             int p;
197
198             code &= 0x1F;
199             p = bytestream2_get_byte(&qctx->buffer);
200             for(i = 0; i <= code; i++) {
201                 dst[filled++] = p;
202                 if(filled >= width) {
203                     filled = 0;
204                     dst -= stride;
205                     height--;
206                     if (height < 0)
207                         break;
208                 }
209             }
210         } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
211             code &= 0x1F;
212
213             if(code + 1 > bytestream2_get_bytes_left(&qctx->buffer))
214                 break;
215
216             for(i = 0; i <= code; i++) {
217                 dst[filled++] = bytestream2_get_byte(&qctx->buffer);
218                 if(filled >= width) {
219                     filled = 0;
220                     dst -= stride;
221                     height--;
222                     if (height < 0)
223                         break;
224                 }
225             }
226         } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
227             int skip;
228
229             code &= 0x3F;
230             /* codes 0x80 and 0x81 are actually escape codes,
231                skip value minus constant is in the next byte */
232             if(!code)
233                 skip = bytestream2_get_byte(&qctx->buffer) +  64;
234             else if(code == 1)
235                 skip = bytestream2_get_byte(&qctx->buffer) + 320;
236             else
237                 skip = code;
238             filled += skip;
239             while( filled >= width) {
240                 filled -= width;
241                 dst -= stride;
242                 height--;
243                 if(height < 0)
244                     break;
245             }
246         } else {
247             /* zero code treated as one-pixel skip */
248             if(code) {
249                 dst[filled++] = ctable[code & 0x7F];
250             }
251             else
252                 filled++;
253             if(filled >= width) {
254                 filled = 0;
255                 dst -= stride;
256                 height--;
257             }
258         }
259     }
260 }
261
262 static int decode_frame(AVCodecContext *avctx,
263                         void *data, int *got_frame,
264                         AVPacket *avpkt)
265 {
266     uint8_t ctable[128];
267     QpegContext * const a = avctx->priv_data;
268     AVFrame * const p = a->pic;
269     AVFrame * const ref = a->ref;
270     uint8_t* outdata;
271     int delta, ret;
272     int pal_size;
273     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
274
275     if (avpkt->size < 0x86) {
276         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
277         return AVERROR_INVALIDDATA;
278     }
279
280     bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
281
282     av_frame_unref(ref);
283     av_frame_move_ref(ref, p);
284
285     if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
286         return ret;
287     outdata = p->data[0];
288     bytestream2_skip(&a->buffer, 4);
289     bytestream2_get_buffer(&a->buffer, ctable, 128);
290     bytestream2_skip(&a->buffer, 1);
291
292     delta = bytestream2_get_byte(&a->buffer);
293     if(delta == 0x10) {
294         qpeg_decode_intra(a, outdata, p->linesize[0], avctx->width, avctx->height);
295     } else {
296         qpeg_decode_inter(a, outdata, p->linesize[0], avctx->width, avctx->height, delta, ctable, ref->data[0]);
297     }
298
299     /* make the palette available on the way out */
300     if (pal && pal_size == AVPALETTE_SIZE) {
301         p->palette_has_changed = 1;
302         memcpy(a->pal, pal, AVPALETTE_SIZE);
303     } else if (pal) {
304         av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
305     }
306     memcpy(p->data[1], a->pal, AVPALETTE_SIZE);
307
308     if ((ret = av_frame_ref(data, p)) < 0)
309         return ret;
310
311     *got_frame      = 1;
312
313     return avpkt->size;
314 }
315
316 static void decode_flush(AVCodecContext *avctx){
317     QpegContext * const a = avctx->priv_data;
318     int i, pal_size;
319     const uint8_t *pal_src;
320
321     pal_size = FFMIN(1024U, avctx->extradata_size);
322     pal_src = avctx->extradata + avctx->extradata_size - pal_size;
323
324     for (i=0; i<pal_size/4; i++)
325         a->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
326 }
327
328 static av_cold int decode_end(AVCodecContext *avctx)
329 {
330     QpegContext * const a = avctx->priv_data;
331
332     av_frame_free(&a->pic);
333     av_frame_free(&a->ref);
334
335     return 0;
336 }
337
338 static av_cold int decode_init(AVCodecContext *avctx){
339     QpegContext * const a = avctx->priv_data;
340
341     a->avctx = avctx;
342     avctx->pix_fmt= AV_PIX_FMT_PAL8;
343
344     decode_flush(avctx);
345
346     a->pic = av_frame_alloc();
347     a->ref = av_frame_alloc();
348     if (!a->pic || !a->ref) {
349         decode_end(avctx);
350         return AVERROR(ENOMEM);
351     }
352
353     return 0;
354 }
355
356 AVCodec ff_qpeg_decoder = {
357     .name           = "qpeg",
358     .long_name      = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
359     .type           = AVMEDIA_TYPE_VIDEO,
360     .id             = AV_CODEC_ID_QPEG,
361     .priv_data_size = sizeof(QpegContext),
362     .init           = decode_init,
363     .close          = decode_end,
364     .decode         = decode_frame,
365     .flush          = decode_flush,
366     .capabilities   = AV_CODEC_CAP_DR1,
367 };