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"
28 typedef struct GDVContext {
29 AVCodecContext *avctx;
38 unsigned scale_h, scale_v;
41 typedef struct Bits8 {
46 typedef struct Bits32 {
51 #define PREAMBLE_SIZE 4096
53 static av_cold int gdv_decode_init(AVCodecContext *avctx)
55 GDVContext *gdv = avctx->priv_data;
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);
62 return AVERROR(ENOMEM);
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;
75 static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
79 if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
83 if (gdv->scale_h && gdv->scale_v) {
84 for (j = 0; j < h; j++) {
86 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
87 uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * (w>>1);
88 for (i = 0; i < w; i++) {
90 dst1[x] = src1[(x>>1)];
93 } else if (gdv->scale_h) {
94 for (j = 0; j < h; j++) {
96 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
97 uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
98 memcpy(dst1, src1, w);
100 } else if (gdv->scale_v) {
101 for (j = 0; j < h; j++) {
103 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
104 uint8_t *src1 = dst + PREAMBLE_SIZE + y * (w>>1);
105 for (i = 0; i < w; i++) {
107 dst1[x] = src1[(x>>1)];
112 if (scale_h && scale_v) {
113 for (y = 0; y < (h>>1); y++) {
114 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
115 uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
116 for (x = 0; x < (w>>1); x++) {
120 } else if (scale_h) {
121 for (y = 0; y < (h>>1); y++) {
122 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
123 uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
124 memcpy(dst1, src1, w);
126 } else if (scale_v) {
127 for (y = 0; y < h; y++) {
128 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
129 for (x = 0; x < (w>>1); x++) {
135 gdv->scale_v = scale_v;
136 gdv->scale_h = scale_h;
139 static int read_bits2(Bits8 *bits, GetByteContext *gb)
143 if (bits->fill == 0) {
144 bits->queue |= bytestream2_get_byte(gb);
147 res = bits->queue >> 6;
154 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
156 bits->queue = bytestream2_get_le32(gb);
160 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
162 int res = bits->queue & ((1 << nbits) - 1);
164 bits->queue >>= nbits;
166 if (bits->fill <= 16) {
167 bits->queue |= bytestream2_get_le16(gb) << bits->fill;
174 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
181 bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
182 c = bytestream2_get_byte(g2);
183 for (i = 0; i < len; i++) {
184 bytestream2_put_byte(pb, c);
186 } else if (offset < 0) {
187 int start = bytestream2_tell_p(pb) - (-offset);
189 bytestream2_seek(g2, start, SEEK_SET);
190 for (i = 0; i < len; i++) {
191 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
194 int start = bytestream2_tell_p(pb) + offset;
196 bytestream2_seek(g2, start, SEEK_SET);
197 for (i = 0; i < len; i++) {
198 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
203 static int decompress_2(AVCodecContext *avctx)
205 GDVContext *gdv = avctx->priv_data;
206 GetByteContext *gb = &gdv->gb;
207 GetByteContext *g2 = &gdv->g2;
208 PutByteContext *pb = &gdv->pb;
212 bytestream2_init(g2, gdv->frame, gdv->frame_size);
213 bytestream2_skip_p(pb, PREAMBLE_SIZE);
215 for (c = 0; c < 256; c++) {
216 for (i = 0; i < 16; i++) {
217 gdv->frame[c * 16 + i] = c;
221 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
222 int tag = read_bits2(&bits, gb);
224 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
225 } else if (tag == 1) {
226 int b = bytestream2_get_byte(gb);
227 int len = (b & 0xF) + 3;
228 int top = (b >> 4) & 0xF;
229 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
230 lz_copy(pb, g2, off, len);
231 } else if (tag == 2) {
232 int len = (bytestream2_get_byte(gb)) + 2;
233 bytestream2_skip_p(pb, len);
241 static int decompress_5(AVCodecContext *avctx, unsigned skip)
243 GDVContext *gdv = avctx->priv_data;
244 GetByteContext *gb = &gdv->gb;
245 GetByteContext *g2 = &gdv->g2;
246 PutByteContext *pb = &gdv->pb;
249 bytestream2_init(g2, gdv->frame, gdv->frame_size);
250 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
252 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
253 int tag = read_bits2(&bits, gb);
255 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
256 } else if (tag == 1) {
257 int b = bytestream2_get_byte(gb);
258 int len = (b & 0xF) + 3;
260 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
261 lz_copy(pb, g2, off, len);
262 } else if (tag == 2) {
264 int b = bytestream2_get_byte(gb);
271 len = bytestream2_get_le16(gb);
273 bytestream2_skip_p(pb, len + 1);
275 int b = bytestream2_get_byte(gb);
276 int len = (b & 0x3) + 2;
277 int off = -(b >> 2) - 1;
278 lz_copy(pb, g2, off, len);
284 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
286 GDVContext *gdv = avctx->priv_data;
287 GetByteContext *gb = &gdv->gb;
288 GetByteContext *g2 = &gdv->g2;
289 PutByteContext *pb = &gdv->pb;
292 bytestream2_init(g2, gdv->frame, gdv->frame_size);
293 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
294 fill_bits32(&bits, gb);
296 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
297 int tag = read_bits32(&bits, gb, 2);
299 int b = read_bits32(&bits, gb, 1);
301 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
309 val = read_bits32(&bits, gb, lbits);
311 if (val != ((1 << lbits) - 1)) {
316 for (i = 0; i < len; i++) {
317 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
320 } else if (tag == 1) {
321 int b = read_bits32(&bits, gb, 1);
325 len = (read_bits32(&bits, gb, 4)) + 2;
327 int bb = bytestream2_get_byte(gb);
328 if ((bb & 0x80) == 0) {
331 int top = (bb & 0x7F) << 8;
332 len = top + bytestream2_get_byte(gb) + 146;
335 bytestream2_skip_p(pb, len);
336 } else if (tag == 2) {
337 int i, subtag = read_bits32(&bits, gb, 2);
340 int top = (read_bits32(&bits, gb, 4)) << 8;
341 int offs = top + bytestream2_get_byte(gb);
342 if ((subtag != 0) || (offs <= 0xF80)) {
343 int len = (subtag) + 3;
344 lz_copy(pb, g2, (offs) - 4096, len);
346 int real_off, len, c1, c2;
352 real_off = ((offs >> 4) & 0x7) + 1;
353 len = ((offs & 0xF) + 2) * 2;
354 c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
355 c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
356 for (i = 0; i < len/2; i++) {
357 bytestream2_put_byte(pb, c1);
358 bytestream2_put_byte(pb, c2);
362 int b = bytestream2_get_byte(gb);
363 int off = ((b & 0x7F)) + 1;
364 int len = ((b & 0x80) == 0) ? 2 : 3;
366 lz_copy(pb, g2, -off, len);
372 int q, b = bytestream2_get_byte(gb);
373 if ((b & 0xC0) == 0xC0) {
374 len = ((b & 0x3F)) + 8;
375 q = read_bits32(&bits, gb, 4);
376 off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
379 if ((b & 0x80) == 0) {
380 len = ((b >> 4)) + 6;
383 len = ((b & 0x3F)) + 14;
384 ofs1 = read_bits32(&bits, gb, 4);
386 off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
389 int ofs1, b = bytestream2_get_byte(gb);
391 if ((b >> 4) == 0xF) {
392 len = bytestream2_get_byte(gb) + 21;
397 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
399 lz_copy(pb, g2, off, len);
406 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
407 int *got_frame, AVPacket *avpkt)
409 GDVContext *gdv = avctx->priv_data;
410 GetByteContext *gb = &gdv->gb;
411 PutByteContext *pb = &gdv->pb;
412 AVFrame *frame = data;
413 int ret, i, pal_size;
414 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
419 bytestream2_init(gb, avpkt->data, avpkt->size);
420 bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
422 flags = bytestream2_get_le32(gb);
423 compression = flags & 0xF;
425 if (compression == 4 || compression == 7 || compression > 8)
426 return AVERROR_INVALIDDATA;
428 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
430 if (pal && pal_size == AVPALETTE_SIZE)
431 memcpy(gdv->pal, pal, AVPALETTE_SIZE);
433 rescale(gdv, gdv->frame, avctx->width, avctx->height,
434 !!(flags & 0x10), !!(flags & 0x20));
436 switch (compression) {
438 memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
440 if (bytestream2_get_bytes_left(gb) < 256*3)
441 return AVERROR_INVALIDDATA;
442 for (i = 0; i < 256; i++) {
443 unsigned r = bytestream2_get_byte(gb);
444 unsigned g = bytestream2_get_byte(gb);
445 unsigned b = bytestream2_get_byte(gb);
446 gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
450 ret = decompress_2(avctx);
455 ret = decompress_5(avctx, flags >> 8);
458 ret = decompress_68(avctx, flags >> 8, 0);
461 ret = decompress_68(avctx, flags >> 8, 1);
467 memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
468 dst = frame->data[0];
470 if (!gdv->scale_v && !gdv->scale_h) {
471 int sidx = PREAMBLE_SIZE, didx = 0;
474 for (y = 0; y < avctx->height; y++) {
475 for (x = 0; x < avctx->width; x++) {
476 dst[x+didx] = gdv->frame[x+sidx];
478 sidx += avctx->width;
479 didx += frame->linesize[0];
482 int sidx = PREAMBLE_SIZE, didx = 0;
485 for (y = 0; y < avctx->height; y++) {
487 for (x = 0; x < avctx->width; x++) {
488 dst[didx + x] = gdv->frame[sidx + x];
491 for (x = 0; x < avctx->width; x++) {
492 dst[didx + x] = gdv->frame[sidx + x/2];
495 if (!gdv->scale_h || ((y & 1) == 1)) {
496 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
498 didx += frame->linesize[0];
504 return ret < 0 ? ret : avpkt->size;
507 static av_cold int gdv_decode_close(AVCodecContext *avctx)
509 GDVContext *gdv = avctx->priv_data;
510 av_freep(&gdv->frame);
514 AVCodec ff_gdv_decoder = {
516 .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
517 .type = AVMEDIA_TYPE_VIDEO,
518 .id = AV_CODEC_ID_GDV,
519 .priv_data_size = sizeof(GDVContext),
520 .init = gdv_decode_init,
521 .close = gdv_decode_close,
522 .decode = gdv_decode_frame,
523 .capabilities = AV_CODEC_CAP_DR1,
524 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,