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