OSDN Git Service

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