2 * Gremlin Digital Video (GDV) decoder
3 * Copyright (c) 2017 Konstantin Shishkov
4 * Copyright (c) 2017 Paul B Mahol
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/common.h"
25 #include "bytestream.h"
29 typedef struct GDVContext {
30 AVCodecContext *avctx;
39 unsigned scale_h, scale_v;
42 typedef struct Bits8 {
47 typedef struct Bits32 {
52 #define PREAMBLE_SIZE 4096
54 static av_cold int gdv_decode_init(AVCodecContext *avctx)
56 GDVContext *gdv = avctx->priv_data;
59 avctx->pix_fmt = AV_PIX_FMT_PAL8;
60 gdv->frame_size = avctx->width * avctx->height + PREAMBLE_SIZE;
61 gdv->frame = av_calloc(gdv->frame_size, 1);
63 return AVERROR(ENOMEM);
65 for (i = 0; i < 2; i++) {
66 for (j = 0; j < 256; j++) {
67 for (k = 0; k < 8; k++) {
68 gdv->frame[i * 2048 + j * 8 + k] = j;
76 static void scaleup(uint8_t *dst, const uint8_t *src, int w)
79 for (x = 0; x < w - 7; x+=8) {
81 dst[x + 1] = src[(x>>1) + 0];
83 dst[x + 3] = src[(x>>1) + 1];
85 dst[x + 5] = src[(x>>1) + 2];
87 dst[x + 7] = src[(x>>1) + 3];
94 static void scaleup_rev(uint8_t *dst, const uint8_t *src, int w)
98 for (x = w - 1; (x+1) & 7; x--) {
101 for (x -= 7; x >= 0; x -= 8) {
103 dst[x + 7] = src[(x>>1) + 3];
105 dst[x + 5] = src[(x>>1) + 2];
107 dst[x + 3] = src[(x>>1) + 1];
109 dst[x + 1] = src[(x>>1) + 0];
113 static void scaledown(uint8_t *dst, const uint8_t *src, int w)
116 for (x = 0; x < w - 7; x+=8) {
117 dst[x + 0] = src[2*x + 0];
118 dst[x + 1] = src[2*x + 2];
119 dst[x + 2] = src[2*x + 4];
120 dst[x + 3] = src[2*x + 6];
121 dst[x + 4] = src[2*x + 8];
122 dst[x + 5] = src[2*x +10];
123 dst[x + 6] = src[2*x +12];
124 dst[x + 7] = src[2*x +14];
131 static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
135 if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
140 for (j = 0; j < h; j++) {
142 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
143 uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>!!gdv->scale_h) * (w>>1);
145 scaleup_rev(dst1, src1, w);
147 } else if (gdv->scale_h) {
148 for (j = 0; j < h; j++) {
150 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
151 uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
152 memcpy(dst1, src1, w);
156 if (scale_h && scale_v) {
157 for (y = 0; y < (h>>1); y++) {
158 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
159 uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
160 scaledown(dst1, src1, w>>1);
162 } else if (scale_h) {
163 for (y = 0; y < (h>>1); y++) {
164 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
165 uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
166 memcpy(dst1, src1, w);
168 } else if (scale_v) {
169 for (y = 0; y < h; y++) {
170 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
171 scaledown(dst1, dst1, w>>1);
175 gdv->scale_v = scale_v;
176 gdv->scale_h = scale_h;
179 static int read_bits2(Bits8 *bits, GetByteContext *gb)
183 if (bits->fill == 0) {
184 bits->queue |= bytestream2_get_byte(gb);
187 res = bits->queue >> 6;
194 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
196 bits->queue = bytestream2_get_le32(gb);
200 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
202 int res = bits->queue & ((1 << nbits) - 1);
204 bits->queue >>= nbits;
206 if (bits->fill <= 16) {
207 bits->queue |= bytestream2_get_le16(gb) << bits->fill;
214 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
221 bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
222 c = bytestream2_get_byte(g2);
223 for (i = 0; i < len; i++) {
224 bytestream2_put_byte(pb, c);
226 } else if (offset < 0) {
227 int start = bytestream2_tell_p(pb) - (-offset);
229 bytestream2_seek(g2, start, SEEK_SET);
230 for (i = 0; i < len; i++) {
231 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
234 int start = bytestream2_tell_p(pb) + offset;
236 bytestream2_seek(g2, start, SEEK_SET);
237 for (i = 0; i < len; i++) {
238 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
243 static int decompress_2(AVCodecContext *avctx)
245 GDVContext *gdv = avctx->priv_data;
246 GetByteContext *gb = &gdv->gb;
247 GetByteContext *g2 = &gdv->g2;
248 PutByteContext *pb = &gdv->pb;
252 bytestream2_init(g2, gdv->frame, gdv->frame_size);
253 bytestream2_skip_p(pb, PREAMBLE_SIZE);
255 for (c = 0; c < 256; c++) {
256 for (i = 0; i < 16; i++) {
257 gdv->frame[c * 16 + i] = c;
261 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
262 int tag = read_bits2(&bits, gb);
264 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
265 } else if (tag == 1) {
266 int b = bytestream2_get_byte(gb);
267 int len = (b & 0xF) + 3;
268 int top = (b >> 4) & 0xF;
269 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
270 lz_copy(pb, g2, off, len);
271 } else if (tag == 2) {
272 int len = (bytestream2_get_byte(gb)) + 2;
273 bytestream2_skip_p(pb, len);
279 if (bytestream2_get_bytes_left_p(pb) > 0)
280 return AVERROR_INVALIDDATA;
285 static int decompress_5(AVCodecContext *avctx, unsigned skip)
287 GDVContext *gdv = avctx->priv_data;
288 GetByteContext *gb = &gdv->gb;
289 GetByteContext *g2 = &gdv->g2;
290 PutByteContext *pb = &gdv->pb;
293 bytestream2_init(g2, gdv->frame, gdv->frame_size);
294 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
296 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
297 int tag = read_bits2(&bits, gb);
298 if (bytestream2_get_bytes_left(gb) < 1)
299 return AVERROR_INVALIDDATA;
301 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
302 } else if (tag == 1) {
303 int b = bytestream2_get_byte(gb);
304 int len = (b & 0xF) + 3;
306 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
307 lz_copy(pb, g2, off, len);
308 } else if (tag == 2) {
310 int b = bytestream2_get_byte(gb);
317 len = bytestream2_get_le16(gb);
319 bytestream2_skip_p(pb, len + 1);
321 int b = bytestream2_get_byte(gb);
322 int len = (b & 0x3) + 2;
323 int off = -(b >> 2) - 1;
324 lz_copy(pb, g2, off, len);
327 if (bytestream2_get_bytes_left_p(pb) > 0)
328 return AVERROR_INVALIDDATA;
332 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
334 GDVContext *gdv = avctx->priv_data;
335 GetByteContext *gb = &gdv->gb;
336 GetByteContext *g2 = &gdv->g2;
337 PutByteContext *pb = &gdv->pb;
340 bytestream2_init(g2, gdv->frame, gdv->frame_size);
341 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
342 fill_bits32(&bits, gb);
344 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
345 int tag = read_bits32(&bits, gb, 2);
347 int b = read_bits32(&bits, gb, 1);
349 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
357 val = read_bits32(&bits, gb, lbits);
359 if (val != ((1 << lbits) - 1)) {
363 return AVERROR_INVALIDDATA;
365 for (i = 0; i < len; i++) {
366 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
369 } else if (tag == 1) {
370 int b = read_bits32(&bits, gb, 1);
374 len = (read_bits32(&bits, gb, 4)) + 2;
376 int bb = bytestream2_get_byte(gb);
377 if ((bb & 0x80) == 0) {
380 int top = (bb & 0x7F) << 8;
381 len = top + bytestream2_get_byte(gb) + 146;
384 bytestream2_skip_p(pb, len);
385 } else if (tag == 2) {
386 int i, subtag = read_bits32(&bits, gb, 2);
389 int top = (read_bits32(&bits, gb, 4)) << 8;
390 int offs = top + bytestream2_get_byte(gb);
391 if ((subtag != 0) || (offs <= 0xF80)) {
392 int len = (subtag) + 3;
393 lz_copy(pb, g2, (offs) - 4096, len);
395 int real_off, len, c1, c2;
401 real_off = ((offs >> 4) & 0x7) + 1;
402 len = ((offs & 0xF) + 2) * 2;
403 c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
404 c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
405 for (i = 0; i < len/2; i++) {
406 bytestream2_put_byte(pb, c1);
407 bytestream2_put_byte(pb, c2);
411 int b = bytestream2_get_byte(gb);
412 int off = ((b & 0x7F)) + 1;
413 int len = ((b & 0x80) == 0) ? 2 : 3;
415 lz_copy(pb, g2, -off, len);
421 int q, b = bytestream2_get_byte(gb);
422 if ((b & 0xC0) == 0xC0) {
423 len = ((b & 0x3F)) + 8;
424 q = read_bits32(&bits, gb, 4);
425 off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
428 if ((b & 0x80) == 0) {
429 len = ((b >> 4)) + 6;
432 len = ((b & 0x3F)) + 14;
433 ofs1 = read_bits32(&bits, gb, 4);
435 off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
438 int ofs1, b = bytestream2_get_byte(gb);
440 if ((b >> 4) == 0xF) {
441 len = bytestream2_get_byte(gb) + 21;
446 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
448 lz_copy(pb, g2, off, len);
452 if (bytestream2_get_bytes_left_p(pb) > 0)
453 return AVERROR_INVALIDDATA;
458 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
459 int *got_frame, AVPacket *avpkt)
461 GDVContext *gdv = avctx->priv_data;
462 GetByteContext *gb = &gdv->gb;
463 PutByteContext *pb = &gdv->pb;
464 AVFrame *frame = data;
470 bytestream2_init(gb, avpkt->data, avpkt->size);
471 bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
473 flags = bytestream2_get_le32(gb);
474 compression = flags & 0xF;
476 if (compression == 4 || compression == 7 || compression > 8)
477 return AVERROR_INVALIDDATA;
479 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
481 ff_copy_palette(gdv->pal, avpkt, avctx);
483 if (compression < 2 && bytestream2_get_bytes_left(gb) < 256*3)
484 return AVERROR_INVALIDDATA;
485 rescale(gdv, gdv->frame, avctx->width, avctx->height,
486 !!(flags & 0x10), !!(flags & 0x20));
488 switch (compression) {
490 memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
492 for (i = 0; i < 256; i++) {
493 unsigned r = bytestream2_get_byte(gb);
494 unsigned g = bytestream2_get_byte(gb);
495 unsigned b = bytestream2_get_byte(gb);
496 gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
500 ret = decompress_2(avctx);
505 ret = decompress_5(avctx, flags >> 8);
508 ret = decompress_68(avctx, flags >> 8, 0);
511 ret = decompress_68(avctx, flags >> 8, 1);
519 memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
520 dst = frame->data[0];
522 if (!gdv->scale_v && !gdv->scale_h) {
523 int sidx = PREAMBLE_SIZE, didx = 0;
526 for (y = 0; y < avctx->height; y++) {
527 memcpy(dst + didx, gdv->frame + sidx, avctx->width);
528 sidx += avctx->width;
529 didx += frame->linesize[0];
532 int sidx = PREAMBLE_SIZE, didx = 0;
535 for (y = 0; y < avctx->height; y++) {
537 memcpy(dst + didx, gdv->frame + sidx, avctx->width);
539 uint8_t *dst2 = dst + didx;
540 uint8_t *src2 = gdv->frame + sidx;
542 scaleup(dst2, src2, avctx->width);
544 if (!gdv->scale_h || ((y & 1) == 1)) {
545 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
547 didx += frame->linesize[0];
556 static av_cold int gdv_decode_close(AVCodecContext *avctx)
558 GDVContext *gdv = avctx->priv_data;
559 av_freep(&gdv->frame);
563 AVCodec ff_gdv_decoder = {
565 .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
566 .type = AVMEDIA_TYPE_VIDEO,
567 .id = AV_CODEC_ID_GDV,
568 .priv_data_size = sizeof(GDVContext),
569 .init = gdv_decode_init,
570 .close = gdv_decode_close,
571 .decode = gdv_decode_frame,
572 .capabilities = AV_CODEC_CAP_DR1,
573 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,