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)) {
84 for (j = 0; j < h; j++) {
86 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
87 uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>!!gdv->scale_h) * (w>>1);
88 for (x = w - 1; x >= 0; x--) {
89 dst1[x] = src1[(x>>1)];
92 } else if (gdv->scale_h) {
93 for (j = 0; j < h; j++) {
95 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
96 uint8_t *src1 = dst + PREAMBLE_SIZE + (y>>1) * w;
97 memcpy(dst1, src1, w);
101 if (scale_h && scale_v) {
102 for (y = 0; y < (h>>1); y++) {
103 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * (w>>1);
104 uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
105 for (x = 0; x < (w>>1); x++) {
109 } else if (scale_h) {
110 for (y = 0; y < (h>>1); y++) {
111 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
112 uint8_t *src1 = dst + PREAMBLE_SIZE + y*2 * w;
113 memcpy(dst1, src1, w);
115 } else if (scale_v) {
116 for (y = 0; y < h; y++) {
117 uint8_t *dst1 = dst + PREAMBLE_SIZE + y * w;
118 for (x = 0; x < (w>>1); x++) {
124 gdv->scale_v = scale_v;
125 gdv->scale_h = scale_h;
128 static int read_bits2(Bits8 *bits, GetByteContext *gb)
132 if (bits->fill == 0) {
133 bits->queue |= bytestream2_get_byte(gb);
136 res = bits->queue >> 6;
143 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
145 bits->queue = bytestream2_get_le32(gb);
149 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
151 int res = bits->queue & ((1 << nbits) - 1);
153 bits->queue >>= nbits;
155 if (bits->fill <= 16) {
156 bits->queue |= bytestream2_get_le16(gb) << bits->fill;
163 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
170 bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
171 c = bytestream2_get_byte(g2);
172 for (i = 0; i < len; i++) {
173 bytestream2_put_byte(pb, c);
175 } else if (offset < 0) {
176 int start = bytestream2_tell_p(pb) - (-offset);
178 bytestream2_seek(g2, start, SEEK_SET);
179 for (i = 0; i < len; i++) {
180 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
183 int start = bytestream2_tell_p(pb) + offset;
185 bytestream2_seek(g2, start, SEEK_SET);
186 for (i = 0; i < len; i++) {
187 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
192 static int decompress_2(AVCodecContext *avctx)
194 GDVContext *gdv = avctx->priv_data;
195 GetByteContext *gb = &gdv->gb;
196 GetByteContext *g2 = &gdv->g2;
197 PutByteContext *pb = &gdv->pb;
201 bytestream2_init(g2, gdv->frame, gdv->frame_size);
202 bytestream2_skip_p(pb, PREAMBLE_SIZE);
204 for (c = 0; c < 256; c++) {
205 for (i = 0; i < 16; i++) {
206 gdv->frame[c * 16 + i] = c;
210 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
211 int tag = read_bits2(&bits, gb);
213 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
214 } else if (tag == 1) {
215 int b = bytestream2_get_byte(gb);
216 int len = (b & 0xF) + 3;
217 int top = (b >> 4) & 0xF;
218 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
219 lz_copy(pb, g2, off, len);
220 } else if (tag == 2) {
221 int len = (bytestream2_get_byte(gb)) + 2;
222 bytestream2_skip_p(pb, len);
230 static int decompress_5(AVCodecContext *avctx, unsigned skip)
232 GDVContext *gdv = avctx->priv_data;
233 GetByteContext *gb = &gdv->gb;
234 GetByteContext *g2 = &gdv->g2;
235 PutByteContext *pb = &gdv->pb;
238 bytestream2_init(g2, gdv->frame, gdv->frame_size);
239 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
241 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
242 int tag = read_bits2(&bits, gb);
244 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
245 } else if (tag == 1) {
246 int b = bytestream2_get_byte(gb);
247 int len = (b & 0xF) + 3;
249 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
250 lz_copy(pb, g2, off, len);
251 } else if (tag == 2) {
253 int b = bytestream2_get_byte(gb);
260 len = bytestream2_get_le16(gb);
262 bytestream2_skip_p(pb, len + 1);
264 int b = bytestream2_get_byte(gb);
265 int len = (b & 0x3) + 2;
266 int off = -(b >> 2) - 1;
267 lz_copy(pb, g2, off, len);
273 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
275 GDVContext *gdv = avctx->priv_data;
276 GetByteContext *gb = &gdv->gb;
277 GetByteContext *g2 = &gdv->g2;
278 PutByteContext *pb = &gdv->pb;
281 bytestream2_init(g2, gdv->frame, gdv->frame_size);
282 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
283 fill_bits32(&bits, gb);
285 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
286 int tag = read_bits32(&bits, gb, 2);
288 int b = read_bits32(&bits, gb, 1);
290 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
298 val = read_bits32(&bits, gb, lbits);
300 if (val != ((1 << lbits) - 1)) {
305 for (i = 0; i < len; i++) {
306 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
309 } else if (tag == 1) {
310 int b = read_bits32(&bits, gb, 1);
314 len = (read_bits32(&bits, gb, 4)) + 2;
316 int bb = bytestream2_get_byte(gb);
317 if ((bb & 0x80) == 0) {
320 int top = (bb & 0x7F) << 8;
321 len = top + bytestream2_get_byte(gb) + 146;
324 bytestream2_skip_p(pb, len);
325 } else if (tag == 2) {
326 int i, subtag = read_bits32(&bits, gb, 2);
329 int top = (read_bits32(&bits, gb, 4)) << 8;
330 int offs = top + bytestream2_get_byte(gb);
331 if ((subtag != 0) || (offs <= 0xF80)) {
332 int len = (subtag) + 3;
333 lz_copy(pb, g2, (offs) - 4096, len);
335 int real_off, len, c1, c2;
341 real_off = ((offs >> 4) & 0x7) + 1;
342 len = ((offs & 0xF) + 2) * 2;
343 c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
344 c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
345 for (i = 0; i < len/2; i++) {
346 bytestream2_put_byte(pb, c1);
347 bytestream2_put_byte(pb, c2);
351 int b = bytestream2_get_byte(gb);
352 int off = ((b & 0x7F)) + 1;
353 int len = ((b & 0x80) == 0) ? 2 : 3;
355 lz_copy(pb, g2, -off, len);
361 int q, b = bytestream2_get_byte(gb);
362 if ((b & 0xC0) == 0xC0) {
363 len = ((b & 0x3F)) + 8;
364 q = read_bits32(&bits, gb, 4);
365 off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
368 if ((b & 0x80) == 0) {
369 len = ((b >> 4)) + 6;
372 len = ((b & 0x3F)) + 14;
373 ofs1 = read_bits32(&bits, gb, 4);
375 off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
378 int ofs1, b = bytestream2_get_byte(gb);
380 if ((b >> 4) == 0xF) {
381 len = bytestream2_get_byte(gb) + 21;
386 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
388 lz_copy(pb, g2, off, len);
395 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
396 int *got_frame, AVPacket *avpkt)
398 GDVContext *gdv = avctx->priv_data;
399 GetByteContext *gb = &gdv->gb;
400 PutByteContext *pb = &gdv->pb;
401 AVFrame *frame = data;
402 int ret, i, pal_size;
403 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
408 bytestream2_init(gb, avpkt->data, avpkt->size);
409 bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
411 flags = bytestream2_get_le32(gb);
412 compression = flags & 0xF;
414 if (compression == 4 || compression == 7 || compression > 8)
415 return AVERROR_INVALIDDATA;
417 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
419 if (pal && pal_size == AVPALETTE_SIZE)
420 memcpy(gdv->pal, pal, AVPALETTE_SIZE);
422 rescale(gdv, gdv->frame, avctx->width, avctx->height,
423 !!(flags & 0x10), !!(flags & 0x20));
425 switch (compression) {
427 memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
429 if (bytestream2_get_bytes_left(gb) < 256*3)
430 return AVERROR_INVALIDDATA;
431 for (i = 0; i < 256; i++) {
432 unsigned r = bytestream2_get_byte(gb);
433 unsigned g = bytestream2_get_byte(gb);
434 unsigned b = bytestream2_get_byte(gb);
435 gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
439 ret = decompress_2(avctx);
444 ret = decompress_5(avctx, flags >> 8);
447 ret = decompress_68(avctx, flags >> 8, 0);
450 ret = decompress_68(avctx, flags >> 8, 1);
456 memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
457 dst = frame->data[0];
459 if (!gdv->scale_v && !gdv->scale_h) {
460 int sidx = PREAMBLE_SIZE, didx = 0;
463 for (y = 0; y < avctx->height; y++) {
464 for (x = 0; x < avctx->width; x++) {
465 dst[x+didx] = gdv->frame[x+sidx];
467 sidx += avctx->width;
468 didx += frame->linesize[0];
471 int sidx = PREAMBLE_SIZE, didx = 0;
474 for (y = 0; y < avctx->height; y++) {
476 for (x = 0; x < avctx->width; x++) {
477 dst[didx + x] = gdv->frame[sidx + x];
480 for (x = 0; x < avctx->width; x++) {
481 dst[didx + x] = gdv->frame[sidx + x/2];
484 if (!gdv->scale_h || ((y & 1) == 1)) {
485 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
487 didx += frame->linesize[0];
493 return ret < 0 ? ret : avpkt->size;
496 static av_cold int gdv_decode_close(AVCodecContext *avctx)
498 GDVContext *gdv = avctx->priv_data;
499 av_freep(&gdv->frame);
503 AVCodec ff_gdv_decoder = {
505 .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
506 .type = AVMEDIA_TYPE_VIDEO,
507 .id = AV_CODEC_ID_GDV,
508 .priv_data_size = sizeof(GDVContext),
509 .init = gdv_decode_init,
510 .close = gdv_decode_close,
511 .decode = gdv_decode_frame,
512 .capabilities = AV_CODEC_CAP_DR1,
513 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,