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