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 for (i = 0; i < w; i++) {
88 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
91 } else if (gdv->scale_h) {
92 for (j = 0; j < h; j++) {
94 for (x = 0; x < w; x++) {
95 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x + (y/2) * w];
98 } else if (gdv->scale_v) {
99 for (j = 0; j < h; j++) {
101 for (i = 0; i < w; i++) {
103 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x/2 + y * (w/2)];
108 if (scale_h && scale_v) {
109 for (y = 0; y < h/2; y++) {
110 for (x = 0; x < w/2; x++) {
111 dst[PREAMBLE_SIZE + x + y * (w/2)] = dst[PREAMBLE_SIZE + x*2 + y*2 * w];
114 } else if (scale_h) {
115 for (y = 0; y < h/2; y++) {
116 for (x = 0; x < w; x++) {
117 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x + y*2 * w];
120 } else if (scale_v) {
121 for (y = 0; y < h; y++) {
122 for (x = 0; x < w/2; x++) {
123 dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x*2 + y * w];
128 gdv->scale_v = scale_v;
129 gdv->scale_h = scale_h;
132 static int read_bits2(Bits8 *bits, GetByteContext *gb)
136 if (bits->fill == 0) {
137 bits->queue |= bytestream2_get_byte(gb);
140 res = bits->queue >> 6;
147 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
149 bits->queue = bytestream2_get_le32(gb);
153 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
155 int res = bits->queue & ((1 << nbits) - 1);
157 bits->queue >>= nbits;
159 if (bits->fill <= 16) {
160 bits->queue |= bytestream2_get_le16(gb) << bits->fill;
167 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
174 bytestream2_seek(g2, bytestream2_tell_p(pb) - 1, SEEK_SET);
175 c = bytestream2_get_byte(g2);
176 for (i = 0; i < len; i++) {
177 bytestream2_put_byte(pb, c);
179 } else if (offset < 0) {
180 int start = bytestream2_tell_p(pb) - (-offset);
182 bytestream2_seek(g2, start, SEEK_SET);
183 for (i = 0; i < len; i++) {
184 bytestream2_put_byte(pb, bytestream2_get_byte(g2));
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));
196 static int decompress_2(AVCodecContext *avctx)
198 GDVContext *gdv = avctx->priv_data;
199 GetByteContext *gb = &gdv->gb;
200 GetByteContext *g2 = &gdv->g2;
201 PutByteContext *pb = &gdv->pb;
205 bytestream2_init(g2, gdv->frame, gdv->frame_size);
206 bytestream2_skip_p(pb, PREAMBLE_SIZE);
208 for (c = 0; c < 256; c++) {
209 for (i = 0; i < 16; i++) {
210 gdv->frame[c * 16 + i] = c;
214 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
215 int tag = read_bits2(&bits, gb);
217 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
218 } else if (tag == 1) {
219 int b = bytestream2_get_byte(gb);
220 int len = (b & 0xF) + 3;
221 int top = (b >> 4) & 0xF;
222 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
223 lz_copy(pb, g2, off, len);
224 } else if (tag == 2) {
225 int len = (bytestream2_get_byte(gb)) + 2;
226 bytestream2_skip_p(pb, len);
234 static int decompress_5(AVCodecContext *avctx, unsigned skip)
236 GDVContext *gdv = avctx->priv_data;
237 GetByteContext *gb = &gdv->gb;
238 GetByteContext *g2 = &gdv->g2;
239 PutByteContext *pb = &gdv->pb;
242 bytestream2_init(g2, gdv->frame, gdv->frame_size);
243 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
245 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
246 int tag = read_bits2(&bits, gb);
248 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
249 } else if (tag == 1) {
250 int b = bytestream2_get_byte(gb);
251 int len = (b & 0xF) + 3;
253 int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
254 lz_copy(pb, g2, off, len);
255 } else if (tag == 2) {
257 int b = bytestream2_get_byte(gb);
264 len = bytestream2_get_le16(gb);
266 bytestream2_skip_p(pb, len + 1);
268 int b = bytestream2_get_byte(gb);
269 int len = (b & 0x3) + 2;
270 int off = -(b >> 2) - 1;
271 lz_copy(pb, g2, off, len);
277 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
279 GDVContext *gdv = avctx->priv_data;
280 GetByteContext *gb = &gdv->gb;
281 GetByteContext *g2 = &gdv->g2;
282 PutByteContext *pb = &gdv->pb;
285 bytestream2_init(g2, gdv->frame, gdv->frame_size);
286 bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
287 fill_bits32(&bits, gb);
289 while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
290 int tag = read_bits32(&bits, gb, 2);
292 int b = read_bits32(&bits, gb, 1);
294 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
302 val = read_bits32(&bits, gb, lbits);
304 if (val != ((1 << lbits) - 1)) {
309 for (i = 0; i < len; i++) {
310 bytestream2_put_byte(pb, bytestream2_get_byte(gb));
313 } else if (tag == 1) {
314 int b = read_bits32(&bits, gb, 1);
318 len = (read_bits32(&bits, gb, 4)) + 2;
320 int bb = bytestream2_get_byte(gb);
321 if ((bb & 0x80) == 0) {
324 int top = (bb & 0x7F) << 8;
325 len = top + bytestream2_get_byte(gb) + 146;
328 bytestream2_skip_p(pb, len);
329 } else if (tag == 2) {
330 int i, subtag = read_bits32(&bits, gb, 2);
333 int top = (read_bits32(&bits, gb, 4)) << 8;
334 int offs = top + bytestream2_get_byte(gb);
335 if ((subtag != 0) || (offs <= 0xF80)) {
336 int len = (subtag) + 3;
337 lz_copy(pb, g2, (offs) - 4096, len);
339 int real_off, len, c1, c2;
345 real_off = ((offs >> 4) & 0x7) + 1;
346 len = ((offs & 0xF) + 2) * 2;
347 c1 = gdv->frame[bytestream2_tell_p(pb) - real_off];
348 c2 = gdv->frame[bytestream2_tell_p(pb) - real_off + 1];
349 for (i = 0; i < len/2; i++) {
350 bytestream2_put_byte(pb, c1);
351 bytestream2_put_byte(pb, c2);
355 int b = bytestream2_get_byte(gb);
356 int off = ((b & 0x7F)) + 1;
357 int len = ((b & 0x80) == 0) ? 2 : 3;
359 lz_copy(pb, g2, -off, len);
365 int q, b = bytestream2_get_byte(gb);
366 if ((b & 0xC0) == 0xC0) {
367 len = ((b & 0x3F)) + 8;
368 q = read_bits32(&bits, gb, 4);
369 off = (q << 8) + (bytestream2_get_byte(gb)) + 1;
372 if ((b & 0x80) == 0) {
373 len = ((b >> 4)) + 6;
376 len = ((b & 0x3F)) + 14;
377 ofs1 = read_bits32(&bits, gb, 4);
379 off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
382 int ofs1, b = bytestream2_get_byte(gb);
384 if ((b >> 4) == 0xF) {
385 len = bytestream2_get_byte(gb) + 21;
390 off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
392 lz_copy(pb, g2, off, len);
399 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
400 int *got_frame, AVPacket *avpkt)
402 GDVContext *gdv = avctx->priv_data;
403 GetByteContext *gb = &gdv->gb;
404 PutByteContext *pb = &gdv->pb;
405 AVFrame *frame = data;
406 int ret, i, pal_size;
407 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &pal_size);
412 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
414 if (pal && pal_size == AVPALETTE_SIZE)
415 memcpy(gdv->pal, pal, AVPALETTE_SIZE);
417 bytestream2_init(gb, avpkt->data, avpkt->size);
418 bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
420 flags = bytestream2_get_le32(gb);
421 compression = flags & 0xF;
423 rescale(gdv, gdv->frame, avctx->width, avctx->height,
424 !!(flags & 0x10), !!(flags & 0x20));
426 switch (compression) {
428 memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
430 if (bytestream2_get_bytes_left(gb) < 256*3)
431 return AVERROR_INVALIDDATA;
432 for (i = 0; i < 256; i++) {
433 unsigned r = bytestream2_get_byte(gb);
434 unsigned g = bytestream2_get_byte(gb);
435 unsigned b = bytestream2_get_byte(gb);
436 gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
440 ret = decompress_2(avctx);
445 ret = decompress_5(avctx, flags >> 8);
448 ret = decompress_68(avctx, flags >> 8, 0);
451 ret = decompress_68(avctx, flags >> 8, 1);
454 return AVERROR_INVALIDDATA;
457 memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
458 dst = frame->data[0];
460 if (!gdv->scale_v && !gdv->scale_h) {
461 int sidx = PREAMBLE_SIZE, didx = 0;
464 for (y = 0; y < avctx->height; y++) {
465 for (x = 0; x < avctx->width; x++) {
466 dst[x+didx] = gdv->frame[x+sidx];
468 sidx += avctx->width;
469 didx += frame->linesize[0];
472 int sidx = PREAMBLE_SIZE, didx = 0;
475 for (y = 0; y < avctx->height; y++) {
477 for (x = 0; x < avctx->width; x++) {
478 dst[didx + x] = gdv->frame[sidx + x];
481 for (x = 0; x < avctx->width; x++) {
482 dst[didx + x] = gdv->frame[sidx + x/2];
485 if (!gdv->scale_h || ((y & 1) == 1)) {
486 sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
488 didx += frame->linesize[0];
494 return ret < 0 ? ret : avpkt->size;
497 static av_cold int gdv_decode_close(AVCodecContext *avctx)
499 GDVContext *gdv = avctx->priv_data;
500 av_freep(&gdv->frame);
504 AVCodec ff_gdv_decoder = {
506 .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
507 .type = AVMEDIA_TYPE_VIDEO,
508 .id = AV_CODEC_ID_GDV,
509 .priv_data_size = sizeof(GDVContext),
510 .init = gdv_decode_init,
511 .close = gdv_decode_close,
512 .decode = gdv_decode_frame,
513 .capabilities = AV_CODEC_CAP_DR1,
514 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,