]> git.sesse.net Git - ffmpeg/blob - libavcodec/gdv.c
avcodec/gdv: Error out if we had insufficent input for the output frame in decompress_2()
[ffmpeg] / libavcodec / gdv.c
1 /*
2  * Gremlin Digital Video (GDV) decoder
3  * Copyright (c) 2017 Konstantin Shishkov
4  * Copyright (c) 2017 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/common.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27
28 typedef struct GDVContext {
29     AVCodecContext *avctx;
30
31     GetByteContext gb;
32     GetByteContext g2;
33     PutByteContext pb;
34
35     uint32_t pal[256];
36     uint8_t *frame;
37     unsigned frame_size;
38     unsigned scale_h, scale_v;
39 } GDVContext;
40
41 typedef struct Bits8 {
42     uint8_t queue;
43     uint8_t fill;
44 } Bits8;
45
46 typedef struct Bits32 {
47     uint32_t queue;
48     uint8_t  fill;
49 } Bits32;
50
51 #define PREAMBLE_SIZE 4096
52
53 static av_cold int gdv_decode_init(AVCodecContext *avctx)
54 {
55     GDVContext *gdv = avctx->priv_data;
56     int i, j, k;
57
58     avctx->pix_fmt  = AV_PIX_FMT_PAL8;
59     gdv->frame_size = avctx->width * avctx->height + PREAMBLE_SIZE;
60     gdv->frame = av_calloc(gdv->frame_size, 1);
61     if (!gdv->frame)
62         return AVERROR(ENOMEM);
63
64     for (i = 0; i < 2; i++) {
65         for (j = 0; j < 256; j++) {
66             for (k = 0; k < 8; k++) {
67                 gdv->frame[i * 2048 + j * 8 + k] = j;
68             }
69         }
70     }
71
72     return 0;
73 }
74
75 static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
76 {
77     int i, j, y, x;
78
79     if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
80         return;
81     }
82
83     if (gdv->scale_v) {
84         for (j = 0; j < h; j++) {
85             int y = h - j - 1;
86             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
87             uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>!!gdv->scale_h) * (w>>1);
88
89             for (x = w - 1; x >= 0 && !(x&1); x--) {
90                 dst1[x] = src1[(x>>1)];
91             }
92
93             for (x--; x >= 0; x-=2) {
94                 dst1[x  ] =
95                 dst1[x+1] = src1[(x>>1)];
96             }
97         }
98     } else if (gdv->scale_h) {
99         for (j = 0; j < h; j++) {
100             int y = h - j - 1;
101             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
102             uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
103             memcpy(dst1, src1, w);
104         }
105     }
106
107     if (scale_h && scale_v) {
108         for (y = 0; y < (h>>1); y++) {
109             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
110             uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
111             for (x = 0; x < (w>>1); x++) {
112                 dst1[x] = src1[x*2];
113             }
114         }
115     } else if (scale_h) {
116         for (y = 0; y < (h>>1); y++) {
117             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
118             uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
119             memcpy(dst1, src1, w);
120         }
121     } else if (scale_v) {
122         for (y = 0; y < h; y++) {
123             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
124             for (x = 0; x < (w>>1); x++) {
125                 dst1[x] = dst1[x*2];
126             }
127         }
128     }
129
130     gdv->scale_v = scale_v;
131     gdv->scale_h = scale_h;
132 }
133
134 static int read_bits2(Bits8 *bits, GetByteContext *gb)
135 {
136     int res;
137
138     if (bits->fill == 0) {
139         bits->queue |= bytestream2_get_byte(gb);
140         bits->fill   = 8;
141     }
142     res = bits->queue >> 6;
143     bits->queue <<= 2;
144     bits->fill   -= 2;
145
146     return res;
147 }
148
149 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
150 {
151     bits->queue = bytestream2_get_le32(gb);
152     bits->fill  = 32;
153 }
154
155 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
156 {
157     int res = bits->queue & ((1 << nbits) - 1);
158
159     bits->queue >>= nbits;
160     bits->fill   -= nbits;
161     if (bits->fill <= 16) {
162         bits->queue |= bytestream2_get_le16(gb) << bits->fill;
163         bits->fill  += 16;
164     }
165
166     return res;
167 }
168
169 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
170 {
171     int i;
172
173     if (offset == -1) {
174         int c;
175
176         bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
177         c = bytestream2_get_byte(g2);
178         for (i = 0; i < len; i++) {
179             bytestream2_put_byte(pb, c);
180         }
181     } else if (offset < 0) {
182         int start = bytestream2_tell_p(pb) - (-offset);
183
184         bytestream2_seek(g2, start, SEEK_SET);
185         for (i = 0; i < len; i++) {
186             bytestream2_put_byte(pb, bytestream2_get_byte(g2));
187         }
188     } else {
189         int start = bytestream2_tell_p(pb) + offset;
190
191         bytestream2_seek(g2, start, SEEK_SET);
192         for (i = 0; i < len; i++) {
193             bytestream2_put_byte(pb, bytestream2_get_byte(g2));
194         }
195     }
196 }
197
198 static int decompress_2(AVCodecContext *avctx)
199 {
200     GDVContext *gdv = avctx->priv_data;
201     GetByteContext *gb = &gdv->gb;
202     GetByteContext *g2 = &gdv->g2;
203     PutByteContext *pb = &gdv->pb;
204     Bits8 bits = { 0 };
205     int c, i;
206
207     bytestream2_init(g2, gdv->frame, gdv->frame_size);
208     bytestream2_skip_p(pb, PREAMBLE_SIZE);
209
210     for (c = 0; c < 256; c++) {
211         for (i = 0; i < 16; i++) {
212             gdv->frame[c * 16 + i] = c;
213         }
214     }
215
216     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
217         int tag = read_bits2(&bits, gb);
218         if (tag == 0) {
219             bytestream2_put_byte(pb, bytestream2_get_byte(gb));
220         } else if (tag == 1) {
221             int b = bytestream2_get_byte(gb);
222             int len = (b & 0xF) + 3;
223             int top = (b >> 4) & 0xF;
224             int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
225             lz_copy(pb, g2, off, len);
226         } else if (tag == 2) {
227             int len = (bytestream2_get_byte(gb)) + 2;
228             bytestream2_skip_p(pb, len);
229         } else {
230             break;
231         }
232     }
233
234     if (bytestream2_get_bytes_left_p(pb) > 0)
235         return AVERROR_INVALIDDATA;
236
237     return 0;
238 }
239
240 static int decompress_5(AVCodecContext *avctx, unsigned skip)
241 {
242     GDVContext *gdv = avctx->priv_data;
243     GetByteContext *gb = &gdv->gb;
244     GetByteContext *g2 = &gdv->g2;
245     PutByteContext *pb = &gdv->pb;
246     Bits8 bits = { 0 };
247
248     bytestream2_init(g2, gdv->frame, gdv->frame_size);
249     bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
250
251     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
252         int tag = read_bits2(&bits, gb);
253         if (tag == 0) {
254             bytestream2_put_byte(pb, bytestream2_get_byte(gb));
255         } else if (tag == 1) {
256             int b = bytestream2_get_byte(gb);
257             int len = (b & 0xF) + 3;
258             int top = b >> 4;
259             int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
260             lz_copy(pb, g2, off, len);
261         } else if (tag == 2) {
262             int len;
263             int b = bytestream2_get_byte(gb);
264             if (b == 0) {
265                 break;
266             }
267             if (b != 0xFF) {
268                 len = b;
269             } else {
270                 len = bytestream2_get_le16(gb);
271             }
272             bytestream2_skip_p(pb, len + 1);
273         } else {
274             int b = bytestream2_get_byte(gb);
275             int len = (b & 0x3) + 2;
276             int off = -(b >> 2) - 1;
277             lz_copy(pb, g2, off, len);
278         }
279     }
280     return 0;
281 }
282
283 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
284 {
285     GDVContext *gdv = avctx->priv_data;
286     GetByteContext *gb = &gdv->gb;
287     GetByteContext *g2 = &gdv->g2;
288     PutByteContext *pb = &gdv->pb;
289     Bits32 bits;
290
291     bytestream2_init(g2, gdv->frame, gdv->frame_size);
292     bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
293     fill_bits32(&bits, gb);
294
295     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
296         int tag = read_bits32(&bits, gb, 2);
297         if (tag == 0) {
298             int b = read_bits32(&bits, gb, 1);
299             if (b == 0) {
300                 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
301             } else {
302                 int i, len = 2;
303                 int lbits = 0;
304                 while (1) {
305                     int val;
306
307                     lbits += 1;
308                     val = read_bits32(&bits, gb, lbits);
309                     len += val;
310                     if (val != ((1 << lbits) - 1)) {
311                         break;
312                     }
313                     assert(lbits < 16);
314                 }
315                 for (i = 0; i < len; i++) {
316                     bytestream2_put_byte(pb, bytestream2_get_byte(gb));
317                 }
318             }
319         } else if (tag == 1) {
320             int b = read_bits32(&bits, gb, 1);
321             int len;
322
323             if (b == 0) {
324                 len = (read_bits32(&bits, gb, 4)) + 2;
325             } else {
326                 int bb = bytestream2_get_byte(gb);
327                 if ((bb & 0x80) == 0) {
328                     len = bb + 18;
329                 } else {
330                     int top = (bb & 0x7F) << 8;
331                     len = top + bytestream2_get_byte(gb) + 146;
332                 }
333             }
334             bytestream2_skip_p(pb, len);
335         } else if (tag == 2) {
336             int i, subtag = read_bits32(&bits, gb, 2);
337
338             if (subtag != 3) {
339                 int top = (read_bits32(&bits, gb, 4)) << 8;
340                 int offs = top + bytestream2_get_byte(gb);
341                 if ((subtag != 0) || (offs <= 0xF80)) {
342                     int len = (subtag) + 3;
343                     lz_copy(pb, g2, (offs) - 4096, len);
344                 } else {
345                     int real_off, len, c1, c2;
346
347                     if (offs == 0xFFF) {
348                         return 0;
349                     }
350
351                     real_off = ((offs >> 4) & 0x7) + 1;
352                     len = ((offs & 0xF) + 2) * 2;
353                     c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
354                     c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
355                     for (i = 0; i < len/2; i++) {
356                         bytestream2_put_byte(pb, c1);
357                         bytestream2_put_byte(pb, c2);
358                     }
359                 }
360             } else {
361                 int b = bytestream2_get_byte(gb);
362                 int off = ((b & 0x7F)) + 1;
363                 int len = ((b & 0x80) == 0) ? 2 : 3;
364
365                 lz_copy(pb, g2, -off, len);
366             }
367         } else {
368             int len;
369             int off;
370             if (use8) {
371                 int q, b = bytestream2_get_byte(gb);
372                 if ((b & 0xC0) == 0xC0) {
373                     len = ((b & 0x3F)) + 8;
374                     q = read_bits32(&bits, gb, 4);
375                     off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
376                 } else {
377                     int ofs1;
378                     if ((b & 0x80) == 0) {
379                         len = ((b >> 4)) + 6;
380                         ofs1 = (b & 0xF);
381                     } else {
382                         len = ((b & 0x3F)) + 14;
383                         ofs1 = read_bits32(&bits, gb, 4);
384                     }
385                     off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
386                 }
387             } else {
388                 int ofs1, b = bytestream2_get_byte(gb);
389
390                 if ((b >> 4) == 0xF) {
391                     len = bytestream2_get_byte(gb) + 21;
392                 } else {
393                     len = (b >> 4) + 6;
394                 }
395                 ofs1 = (b & 0xF);
396                 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
397             }
398             lz_copy(pb, g2, off, len);
399         }
400     }
401
402     return 0;
403 }
404
405 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
406                             int *got_frame, AVPacket *avpkt)
407 {
408     GDVContext *gdv = avctx->priv_data;
409     GetByteContext *gb = &gdv->gb;
410     PutByteContext *pb = &gdv->pb;
411     AVFrame *frame = data;
412     int ret, i, pal_size;
413     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
414     int compression;
415     unsigned flags;
416     uint8_t *dst;
417
418     bytestream2_init(gb, avpkt->data, avpkt->size);
419     bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
420
421     flags = bytestream2_get_le32(gb);
422     compression = flags & 0xF;
423
424     if (compression == 4 || compression == 7 || compression > 8)
425         return AVERROR_INVALIDDATA;
426
427     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
428         return ret;
429     if (pal && pal_size == AVPALETTE_SIZE)
430         memcpy(gdv->pal, pal, AVPALETTE_SIZE);
431
432     rescale(gdv, gdv->frame, avctx->width, avctx->height,
433             !!(flags & 0x10), !!(flags & 0x20));
434
435     switch (compression) {
436     case 1:
437         memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
438     case 0:
439         if (bytestream2_get_bytes_left(gb) < 256*3)
440             return AVERROR_INVALIDDATA;
441         for (i = 0; i < 256; i++) {
442             unsigned r = bytestream2_get_byte(gb);
443             unsigned g = bytestream2_get_byte(gb);
444             unsigned b = bytestream2_get_byte(gb);
445             gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
446         }
447         break;
448     case 2:
449         ret = decompress_2(avctx);
450         break;
451     case 3:
452         break;
453     case 5:
454         ret = decompress_5(avctx, flags >> 8);
455         break;
456     case 6:
457         ret = decompress_68(avctx, flags >> 8, 0);
458         break;
459     case 8:
460         ret = decompress_68(avctx, flags >> 8, 1);
461         break;
462     default:
463         av_assert0(0);
464     }
465     if (ret < 0)
466         return ret;
467
468     memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
469     dst = frame->data[0];
470
471     if (!gdv->scale_v && !gdv->scale_h) {
472         int sidx = PREAMBLE_SIZE, didx = 0;
473         int y, x;
474
475         for (y = 0; y < avctx->height; y++) {
476             for (x = 0; x < avctx->width; x++) {
477                 dst[x+didx] = gdv->frame[x+sidx];
478             }
479             sidx += avctx->width;
480             didx += frame->linesize[0];
481         }
482     } else {
483         int sidx = PREAMBLE_SIZE, didx = 0;
484         int y, x;
485
486         for (y = 0; y < avctx->height; y++) {
487             if (!gdv->scale_v) {
488                 memcpy(dst + didx, gdv->frame + sidx, avctx->width);
489             } else {
490                 for (x = 0; x < avctx->width - 1; x+=2) {
491                     dst[didx + x    ] =
492                     dst[didx + x + 1] = gdv->frame[sidx + (x>>1)];
493                 }
494                 for (; x < avctx->width; x++) {
495                     dst[didx + x] = gdv->frame[sidx + (x>>1)];
496                 }
497             }
498             if (!gdv->scale_h || ((y & 1) == 1)) {
499                 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
500             }
501             didx += frame->linesize[0];
502         }
503     }
504
505     *got_frame = 1;
506
507     return ret < 0 ? ret : avpkt->size;
508 }
509
510 static av_cold int gdv_decode_close(AVCodecContext *avctx)
511 {
512     GDVContext *gdv = avctx->priv_data;
513     av_freep(&gdv->frame);
514     return 0;
515 }
516
517 AVCodec ff_gdv_decoder = {
518     .name           = "gdv",
519     .long_name      = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
520     .type           = AVMEDIA_TYPE_VIDEO,
521     .id             = AV_CODEC_ID_GDV,
522     .priv_data_size = sizeof(GDVContext),
523     .init           = gdv_decode_init,
524     .close          = gdv_decode_close,
525     .decode         = gdv_decode_frame,
526     .capabilities   = AV_CODEC_CAP_DR1,
527     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
528 };