]> git.sesse.net Git - ffmpeg/blob - libavcodec/gdv.c
Merge commit '156ea66c91b1986a87916f187216978d686725f6'
[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 scaleup(uint8_t *dst, const uint8_t *src, int w)
76 {
77     int x;
78     for (x = 0; x < w - 7; x+=8) {
79         dst[x + 0] =
80         dst[x + 1] = src[(x>>1) + 0];
81         dst[x + 2] =
82         dst[x + 3] = src[(x>>1) + 1];
83         dst[x + 4] =
84         dst[x + 5] = src[(x>>1) + 2];
85         dst[x + 6] =
86         dst[x + 7] = src[(x>>1) + 3];
87     }
88     for (; x < w; x++) {
89         dst[x] = src[(x>>1)];
90     }
91 }
92
93 static void scaleup_rev(uint8_t *dst, const uint8_t *src, int w)
94 {
95     int x;
96
97     for (x = w - 1; (x+1) & 7; x--) {
98         dst[x] = src[(x>>1)];
99     }
100     for (x -= 7; x >= 0; x -= 8) {
101         dst[x + 6] =
102         dst[x + 7] = src[(x>>1) + 3];
103         dst[x + 4] =
104         dst[x + 5] = src[(x>>1) + 2];
105         dst[x + 2] =
106         dst[x + 3] = src[(x>>1) + 1];
107         dst[x + 0] =
108         dst[x + 1] = src[(x>>1) + 0];
109     }
110 }
111
112 static void scaledown(uint8_t *dst, const uint8_t *src, int w)
113 {
114     int x;
115     for (x = 0; x < w - 7; x+=8) {
116         dst[x + 0] = src[2*x + 0];
117         dst[x + 1] = src[2*x + 2];
118         dst[x + 2] = src[2*x + 4];
119         dst[x + 3] = src[2*x + 6];
120         dst[x + 4] = src[2*x + 8];
121         dst[x + 5] = src[2*x +10];
122         dst[x + 6] = src[2*x +12];
123         dst[x + 7] = src[2*x +14];
124     }
125     for (; x < w; x++) {
126         dst[x] = src[2*x];
127     }
128 }
129
130 static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
131 {
132     int j, y;
133
134     if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
135         return;
136     }
137
138     if (gdv->scale_v) {
139         for (j = 0; j < h; j++) {
140             int y = h - j - 1;
141             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
142             uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>!!gdv->scale_h) * (w>>1);
143
144             scaleup_rev(dst1, src1, w);
145         }
146     } else if (gdv->scale_h) {
147         for (j = 0; j < h; j++) {
148             int y = h - j - 1;
149             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
150             uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
151             memcpy(dst1, src1, w);
152         }
153     }
154
155     if (scale_h && scale_v) {
156         for (y = 0; y < (h>>1); y++) {
157             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
158             uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
159             scaledown(dst1, src1, w>>1);
160         }
161     } else if (scale_h) {
162         for (y = 0; y < (h>>1); y++) {
163             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
164             uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
165             memcpy(dst1, src1, w);
166         }
167     } else if (scale_v) {
168         for (y = 0; y < h; y++) {
169             uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
170             scaledown(dst1, dst1, w>>1);
171         }
172     }
173
174     gdv->scale_v = scale_v;
175     gdv->scale_h = scale_h;
176 }
177
178 static int read_bits2(Bits8 *bits, GetByteContext *gb)
179 {
180     int res;
181
182     if (bits->fill == 0) {
183         bits->queue |= bytestream2_get_byte(gb);
184         bits->fill   = 8;
185     }
186     res = bits->queue >> 6;
187     bits->queue <<= 2;
188     bits->fill   -= 2;
189
190     return res;
191 }
192
193 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
194 {
195     bits->queue = bytestream2_get_le32(gb);
196     bits->fill  = 32;
197 }
198
199 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
200 {
201     int res = bits->queue & ((1 << nbits) - 1);
202
203     bits->queue >>= nbits;
204     bits->fill   -= nbits;
205     if (bits->fill <= 16) {
206         bits->queue |= bytestream2_get_le16(gb) << bits->fill;
207         bits->fill  += 16;
208     }
209
210     return res;
211 }
212
213 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
214 {
215     int i;
216
217     if (offset == -1) {
218         int c;
219
220         bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
221         c = bytestream2_get_byte(g2);
222         for (i = 0; i < len; i++) {
223             bytestream2_put_byte(pb, c);
224         }
225     } else if (offset < 0) {
226         int start = bytestream2_tell_p(pb) - (-offset);
227
228         bytestream2_seek(g2, start, SEEK_SET);
229         for (i = 0; i < len; i++) {
230             bytestream2_put_byte(pb, bytestream2_get_byte(g2));
231         }
232     } else {
233         int start = bytestream2_tell_p(pb) + offset;
234
235         bytestream2_seek(g2, start, SEEK_SET);
236         for (i = 0; i < len; i++) {
237             bytestream2_put_byte(pb, bytestream2_get_byte(g2));
238         }
239     }
240 }
241
242 static int decompress_2(AVCodecContext *avctx)
243 {
244     GDVContext *gdv = avctx->priv_data;
245     GetByteContext *gb = &gdv->gb;
246     GetByteContext *g2 = &gdv->g2;
247     PutByteContext *pb = &gdv->pb;
248     Bits8 bits = { 0 };
249     int c, i;
250
251     bytestream2_init(g2, gdv->frame, gdv->frame_size);
252     bytestream2_skip_p(pb, PREAMBLE_SIZE);
253
254     for (c = 0; c < 256; c++) {
255         for (i = 0; i < 16; i++) {
256             gdv->frame[c * 16 + i] = c;
257         }
258     }
259
260     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
261         int tag = read_bits2(&bits, gb);
262         if (tag == 0) {
263             bytestream2_put_byte(pb, bytestream2_get_byte(gb));
264         } else if (tag == 1) {
265             int b = bytestream2_get_byte(gb);
266             int len = (b & 0xF) + 3;
267             int top = (b >> 4) & 0xF;
268             int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
269             lz_copy(pb, g2, off, len);
270         } else if (tag == 2) {
271             int len = (bytestream2_get_byte(gb)) + 2;
272             bytestream2_skip_p(pb, len);
273         } else {
274             break;
275         }
276     }
277
278     if (bytestream2_get_bytes_left_p(pb) > 0)
279         return AVERROR_INVALIDDATA;
280
281     return 0;
282 }
283
284 static int decompress_5(AVCodecContext *avctx, unsigned skip)
285 {
286     GDVContext *gdv = avctx->priv_data;
287     GetByteContext *gb = &gdv->gb;
288     GetByteContext *g2 = &gdv->g2;
289     PutByteContext *pb = &gdv->pb;
290     Bits8 bits = { 0 };
291
292     bytestream2_init(g2, gdv->frame, gdv->frame_size);
293     bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
294
295     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
296         int tag = read_bits2(&bits, gb);
297         if (tag == 0) {
298             bytestream2_put_byte(pb, bytestream2_get_byte(gb));
299         } else if (tag == 1) {
300             int b = bytestream2_get_byte(gb);
301             int len = (b & 0xF) + 3;
302             int top = b >> 4;
303             int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
304             lz_copy(pb, g2, off, len);
305         } else if (tag == 2) {
306             int len;
307             int b = bytestream2_get_byte(gb);
308             if (b == 0) {
309                 break;
310             }
311             if (b != 0xFF) {
312                 len = b;
313             } else {
314                 len = bytestream2_get_le16(gb);
315             }
316             bytestream2_skip_p(pb, len + 1);
317         } else {
318             int b = bytestream2_get_byte(gb);
319             int len = (b & 0x3) + 2;
320             int off = -(b >> 2) - 1;
321             lz_copy(pb, g2, off, len);
322         }
323     }
324     return 0;
325 }
326
327 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
328 {
329     GDVContext *gdv = avctx->priv_data;
330     GetByteContext *gb = &gdv->gb;
331     GetByteContext *g2 = &gdv->g2;
332     PutByteContext *pb = &gdv->pb;
333     Bits32 bits;
334
335     bytestream2_init(g2, gdv->frame, gdv->frame_size);
336     bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
337     fill_bits32(&bits, gb);
338
339     while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
340         int tag = read_bits32(&bits, gb, 2);
341         if (tag == 0) {
342             int b = read_bits32(&bits, gb, 1);
343             if (b == 0) {
344                 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
345             } else {
346                 int i, len = 2;
347                 int lbits = 0;
348                 while (1) {
349                     int val;
350
351                     lbits += 1;
352                     val = read_bits32(&bits, gb, lbits);
353                     len += val;
354                     if (val != ((1 << lbits) - 1)) {
355                         break;
356                     }
357                     assert(lbits < 16);
358                 }
359                 for (i = 0; i < len; i++) {
360                     bytestream2_put_byte(pb, bytestream2_get_byte(gb));
361                 }
362             }
363         } else if (tag == 1) {
364             int b = read_bits32(&bits, gb, 1);
365             int len;
366
367             if (b == 0) {
368                 len = (read_bits32(&bits, gb, 4)) + 2;
369             } else {
370                 int bb = bytestream2_get_byte(gb);
371                 if ((bb & 0x80) == 0) {
372                     len = bb + 18;
373                 } else {
374                     int top = (bb & 0x7F) << 8;
375                     len = top + bytestream2_get_byte(gb) + 146;
376                 }
377             }
378             bytestream2_skip_p(pb, len);
379         } else if (tag == 2) {
380             int i, subtag = read_bits32(&bits, gb, 2);
381
382             if (subtag != 3) {
383                 int top = (read_bits32(&bits, gb, 4)) << 8;
384                 int offs = top + bytestream2_get_byte(gb);
385                 if ((subtag != 0) || (offs <= 0xF80)) {
386                     int len = (subtag) + 3;
387                     lz_copy(pb, g2, (offs) - 4096, len);
388                 } else {
389                     int real_off, len, c1, c2;
390
391                     if (offs == 0xFFF) {
392                         return 0;
393                     }
394
395                     real_off = ((offs >> 4) & 0x7) + 1;
396                     len = ((offs & 0xF) + 2) * 2;
397                     c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
398                     c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
399                     for (i = 0; i < len/2; i++) {
400                         bytestream2_put_byte(pb, c1);
401                         bytestream2_put_byte(pb, c2);
402                     }
403                 }
404             } else {
405                 int b = bytestream2_get_byte(gb);
406                 int off = ((b & 0x7F)) + 1;
407                 int len = ((b & 0x80) == 0) ? 2 : 3;
408
409                 lz_copy(pb, g2, -off, len);
410             }
411         } else {
412             int len;
413             int off;
414             if (use8) {
415                 int q, b = bytestream2_get_byte(gb);
416                 if ((b & 0xC0) == 0xC0) {
417                     len = ((b & 0x3F)) + 8;
418                     q = read_bits32(&bits, gb, 4);
419                     off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
420                 } else {
421                     int ofs1;
422                     if ((b & 0x80) == 0) {
423                         len = ((b >> 4)) + 6;
424                         ofs1 = (b & 0xF);
425                     } else {
426                         len = ((b & 0x3F)) + 14;
427                         ofs1 = read_bits32(&bits, gb, 4);
428                     }
429                     off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
430                 }
431             } else {
432                 int ofs1, b = bytestream2_get_byte(gb);
433
434                 if ((b >> 4) == 0xF) {
435                     len = bytestream2_get_byte(gb) + 21;
436                 } else {
437                     len = (b >> 4) + 6;
438                 }
439                 ofs1 = (b & 0xF);
440                 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
441             }
442             lz_copy(pb, g2, off, len);
443         }
444     }
445
446     return 0;
447 }
448
449 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
450                             int *got_frame, AVPacket *avpkt)
451 {
452     GDVContext *gdv = avctx->priv_data;
453     GetByteContext *gb = &gdv->gb;
454     PutByteContext *pb = &gdv->pb;
455     AVFrame *frame = data;
456     int ret, i, pal_size;
457     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
458     int compression;
459     unsigned flags;
460     uint8_t *dst;
461
462     bytestream2_init(gb, avpkt->data, avpkt->size);
463     bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
464
465     flags = bytestream2_get_le32(gb);
466     compression = flags & 0xF;
467
468     if (compression == 4 || compression == 7 || compression > 8)
469         return AVERROR_INVALIDDATA;
470
471     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
472         return ret;
473     if (pal && pal_size == AVPALETTE_SIZE)
474         memcpy(gdv->pal, pal, AVPALETTE_SIZE);
475
476     rescale(gdv, gdv->frame, avctx->width, avctx->height,
477             !!(flags & 0x10), !!(flags & 0x20));
478
479     switch (compression) {
480     case 1:
481         memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
482     case 0:
483         if (bytestream2_get_bytes_left(gb) < 256*3)
484             return AVERROR_INVALIDDATA;
485         for (i = 0; i < 256; i++) {
486             unsigned r = bytestream2_get_byte(gb);
487             unsigned g = bytestream2_get_byte(gb);
488             unsigned b = bytestream2_get_byte(gb);
489             gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
490         }
491         break;
492     case 2:
493         ret = decompress_2(avctx);
494         break;
495     case 3:
496         break;
497     case 5:
498         ret = decompress_5(avctx, flags >> 8);
499         break;
500     case 6:
501         ret = decompress_68(avctx, flags >> 8, 0);
502         break;
503     case 8:
504         ret = decompress_68(avctx, flags >> 8, 1);
505         break;
506     default:
507         av_assert0(0);
508     }
509     if (ret < 0)
510         return ret;
511
512     memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
513     dst = frame->data[0];
514
515     if (!gdv->scale_v && !gdv->scale_h) {
516         int sidx = PREAMBLE_SIZE, didx = 0;
517         int y;
518
519         for (y = 0; y < avctx->height; y++) {
520             memcpy(dst + didx, gdv->frame + sidx, avctx->width);
521             sidx += avctx->width;
522             didx += frame->linesize[0];
523         }
524     } else {
525         int sidx = PREAMBLE_SIZE, didx = 0;
526         int y;
527
528         for (y = 0; y < avctx->height; y++) {
529             if (!gdv->scale_v) {
530                 memcpy(dst + didx, gdv->frame + sidx, avctx->width);
531             } else {
532                 uint8_t *dst2 = dst + didx;
533                 uint8_t *src2 = gdv->frame + sidx;
534
535                 scaleup(dst2, src2, avctx->width);
536             }
537             if (!gdv->scale_h || ((y & 1) == 1)) {
538                 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
539             }
540             didx += frame->linesize[0];
541         }
542     }
543
544     *got_frame = 1;
545
546     return ret < 0 ? ret : avpkt->size;
547 }
548
549 static av_cold int gdv_decode_close(AVCodecContext *avctx)
550 {
551     GDVContext *gdv = avctx->priv_data;
552     av_freep(&gdv->frame);
553     return 0;
554 }
555
556 AVCodec ff_gdv_decoder = {
557     .name           = "gdv",
558     .long_name      = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
559     .type           = AVMEDIA_TYPE_VIDEO,
560     .id             = AV_CODEC_ID_GDV,
561     .priv_data_size = sizeof(GDVContext),
562     .init           = gdv_decode_init,
563     .close          = gdv_decode_close,
564     .decode         = gdv_decode_frame,
565     .capabilities   = AV_CODEC_CAP_DR1,
566     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
567 };