FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gdv.c
Go to the documentation of this file.
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 {
30 
34 
35  uint32_t pal[256];
37  unsigned frame_size;
38  unsigned scale_h, scale_v;
39 } GDVContext;
40 
41 typedef struct Bits8 {
44 } Bits8;
45 
46 typedef struct Bits32 {
47  uint32_t queue;
49 } Bits32;
50 
51 #define PREAMBLE_SIZE 4096
52 
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 rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
76 {
77  int i, j, y, x;
78 
79  if ((gdv->scale_v == scale_v) && (gdv->scale_h == scale_h)) {
80  return;
81  }
82 
83  if (gdv->scale_h && gdv->scale_v) {
84  for (j = 0; j < h; j++) {
85  int y = h - j - 1;
86  for (i = 0; i < w; i++) {
87  int x = w - i - 1;
88  dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x/2 + (y/2) * (w/2)];
89  }
90  }
91  } else if (gdv->scale_h) {
92  for (j = 0; j < h; j++) {
93  int y = h - j - 1;
94  for (x = 0; x < w; x++) {
95  dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x + (y/2) * w];
96  }
97  }
98  } else if (gdv->scale_v) {
99  for (j = 0; j < h; j++) {
100  int y = h - j - 1;
101  for (i = 0; i < w; i++) {
102  int x = w - i - 1;
103  dst[PREAMBLE_SIZE + x + y * w] = dst[PREAMBLE_SIZE + x/2 + y * (w/2)];
104  }
105  }
106  }
107 
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];
112  }
113  }
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];
118  }
119  }
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];
124  }
125  }
126  }
127 
128  gdv->scale_v = scale_v;
129  gdv->scale_h = scale_h;
130 }
131 
132 static int read_bits2(Bits8 *bits, GetByteContext *gb)
133 {
134  int res;
135 
136  if (bits->fill == 0) {
137  bits->queue |= bytestream2_get_byte(gb);
138  bits->fill = 8;
139  }
140  res = bits->queue >> 6;
141  bits->queue <<= 2;
142  bits->fill -= 2;
143 
144  return res;
145 }
146 
147 static void fill_bits32(Bits32 *bits, GetByteContext *gb)
148 {
149  bits->queue = bytestream2_get_le32(gb);
150  bits->fill = 32;
151 }
152 
153 static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
154 {
155  int res = bits->queue & ((1 << nbits) - 1);
156 
157  bits->queue >>= nbits;
158  bits->fill -= nbits;
159  if (bits->fill <= 16) {
160  bits->queue |= bytestream2_get_le16(gb) << bits->fill;
161  bits->fill += 16;
162  }
163 
164  return res;
165 }
166 
167 static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
168 {
169  int i;
170 
171  if (offset == -1) {
172  int c;
173 
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);
178  }
179  } else if (offset < 0) {
180  int start = bytestream2_tell_p(pb) - (-offset);
181 
182  bytestream2_seek(g2, start, SEEK_SET);
183  for (i = 0; i < len; i++) {
184  bytestream2_put_byte(pb, bytestream2_get_byte(g2));
185  }
186  } else {
187  int start = bytestream2_tell_p(pb) + offset;
188 
189  bytestream2_seek(g2, start, SEEK_SET);
190  for (i = 0; i < len; i++) {
191  bytestream2_put_byte(pb, bytestream2_get_byte(g2));
192  }
193  }
194 }
195 
196 static int decompress_2(AVCodecContext *avctx)
197 {
198  GDVContext *gdv = avctx->priv_data;
199  GetByteContext *gb = &gdv->gb;
200  GetByteContext *g2 = &gdv->g2;
201  PutByteContext *pb = &gdv->pb;
202  Bits8 bits = { 0 };
203  int c, i;
204 
205  bytestream2_init(g2, gdv->frame, gdv->frame_size);
207 
208  for (c = 0; c < 256; c++) {
209  for (i = 0; i < 16; i++) {
210  gdv->frame[c * 16 + i] = c;
211  }
212  }
213 
214  while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
215  int tag = read_bits2(&bits, gb);
216  if (tag == 0) {
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);
227  } else {
228  break;
229  }
230  }
231  return 0;
232 }
233 
234 static int decompress_5(AVCodecContext *avctx, unsigned skip)
235 {
236  GDVContext *gdv = avctx->priv_data;
237  GetByteContext *gb = &gdv->gb;
238  GetByteContext *g2 = &gdv->g2;
239  PutByteContext *pb = &gdv->pb;
240  Bits8 bits = { 0 };
241 
242  bytestream2_init(g2, gdv->frame, gdv->frame_size);
243  bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
244 
245  while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
246  int tag = read_bits2(&bits, gb);
247  if (tag == 0) {
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;
252  int top = b >> 4;
253  int off = (bytestream2_get_byte(gb) << 4) + top - 4096;
254  lz_copy(pb, g2, off, len);
255  } else if (tag == 2) {
256  int len;
257  int b = bytestream2_get_byte(gb);
258  if (b == 0) {
259  break;
260  }
261  if (b != 0xFF) {
262  len = b;
263  } else {
264  len = bytestream2_get_le16(gb);
265  }
266  bytestream2_skip_p(pb, len + 1);
267  } else {
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);
272  }
273  }
274  return 0;
275 }
276 
277 static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
278 {
279  GDVContext *gdv = avctx->priv_data;
280  GetByteContext *gb = &gdv->gb;
281  GetByteContext *g2 = &gdv->g2;
282  PutByteContext *pb = &gdv->pb;
283  Bits32 bits;
284 
285  bytestream2_init(g2, gdv->frame, gdv->frame_size);
286  bytestream2_skip_p(pb, skip + PREAMBLE_SIZE);
287  fill_bits32(&bits, gb);
288 
289  while (bytestream2_get_bytes_left_p(pb) > 0 && bytestream2_get_bytes_left(gb) > 0) {
290  int tag = read_bits32(&bits, gb, 2);
291  if (tag == 0) {
292  int b = read_bits32(&bits, gb, 1);
293  if (b == 0) {
294  bytestream2_put_byte(pb, bytestream2_get_byte(gb));
295  } else {
296  int i, len = 2;
297  int lbits = 0;
298  while (1) {
299  int val;
300 
301  lbits += 1;
302  val = read_bits32(&bits, gb, lbits);
303  len += val;
304  if (val != ((1 << lbits) - 1)) {
305  break;
306  }
307  assert(lbits < 16);
308  }
309  for (i = 0; i < len; i++) {
310  bytestream2_put_byte(pb, bytestream2_get_byte(gb));
311  }
312  }
313  } else if (tag == 1) {
314  int b = read_bits32(&bits, gb, 1);
315  int len;
316 
317  if (b == 0) {
318  len = (read_bits32(&bits, gb, 4)) + 2;
319  } else {
320  int bb = bytestream2_get_byte(gb);
321  if ((bb & 0x80) == 0) {
322  len = bb + 18;
323  } else {
324  int top = (bb & 0x7F) << 8;
325  len = top + bytestream2_get_byte(gb) + 146;
326  }
327  }
328  bytestream2_skip_p(pb, len);
329  } else if (tag == 2) {
330  int i, subtag = read_bits32(&bits, gb, 2);
331 
332  if (subtag != 3) {
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);
338  } else {
339  int real_off, len, c1, c2;
340 
341  if (offs == 0xFFF) {
342  return 0;
343  }
344 
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);
352  }
353  }
354  } else {
355  int b = bytestream2_get_byte(gb);
356  int off = ((b & 0x7F)) + 1;
357  int len = ((b & 0x80) == 0) ? 2 : 3;
358 
359  lz_copy(pb, g2, -off, len);
360  }
361  } else {
362  int len;
363  int off;
364  if (use8) {
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;
370  } else {
371  int ofs1;
372  if ((b & 0x80) == 0) {
373  len = ((b >> 4)) + 6;
374  ofs1 = (b & 0xF);
375  } else {
376  len = ((b & 0x3F)) + 14;
377  ofs1 = read_bits32(&bits, gb, 4);
378  }
379  off = (ofs1 << 8) + (bytestream2_get_byte(gb)) - 4096;
380  }
381  } else {
382  int ofs1, b = bytestream2_get_byte(gb);
383 
384  if ((b >> 4) == 0xF) {
385  len = bytestream2_get_byte(gb) + 21;
386  } else {
387  len = (b >> 4) + 6;
388  }
389  ofs1 = (b & 0xF);
390  off = (ofs1 << 8) + bytestream2_get_byte(gb) - 4096;
391  }
392  lz_copy(pb, g2, off, len);
393  }
394  }
395 
396  return 0;
397 }
398 
399 static int gdv_decode_frame(AVCodecContext *avctx, void *data,
400  int *got_frame, AVPacket *avpkt)
401 {
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);
408  int compression;
409  unsigned flags;
410  uint8_t *dst;
411 
412  bytestream2_init(gb, avpkt->data, avpkt->size);
413  bytestream2_init_writer(pb, gdv->frame, gdv->frame_size);
414 
415  flags = bytestream2_get_le32(gb);
416  compression = flags & 0xF;
417 
418  if (compression == 4 || compression == 7 || compression > 8)
419  return AVERROR_INVALIDDATA;
420 
421  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
422  return ret;
423  if (pal && pal_size == AVPALETTE_SIZE)
424  memcpy(gdv->pal, pal, AVPALETTE_SIZE);
425 
426  rescale(gdv, gdv->frame, avctx->width, avctx->height,
427  !!(flags & 0x10), !!(flags & 0x20));
428 
429  switch (compression) {
430  case 1:
431  memset(gdv->frame + PREAMBLE_SIZE, 0, gdv->frame_size - PREAMBLE_SIZE);
432  case 0:
433  if (bytestream2_get_bytes_left(gb) < 256*3)
434  return AVERROR_INVALIDDATA;
435  for (i = 0; i < 256; i++) {
436  unsigned r = bytestream2_get_byte(gb);
437  unsigned g = bytestream2_get_byte(gb);
438  unsigned b = bytestream2_get_byte(gb);
439  gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
440  }
441  break;
442  case 2:
443  ret = decompress_2(avctx);
444  break;
445  case 3:
446  break;
447  case 5:
448  ret = decompress_5(avctx, flags >> 8);
449  break;
450  case 6:
451  ret = decompress_68(avctx, flags >> 8, 0);
452  break;
453  case 8:
454  ret = decompress_68(avctx, flags >> 8, 1);
455  break;
456  default:
457  av_assert0(0);
458  }
459 
460  memcpy(frame->data[1], gdv->pal, AVPALETTE_SIZE);
461  dst = frame->data[0];
462 
463  if (!gdv->scale_v && !gdv->scale_h) {
464  int sidx = PREAMBLE_SIZE, didx = 0;
465  int y, x;
466 
467  for (y = 0; y < avctx->height; y++) {
468  for (x = 0; x < avctx->width; x++) {
469  dst[x+didx] = gdv->frame[x+sidx];
470  }
471  sidx += avctx->width;
472  didx += frame->linesize[0];
473  }
474  } else {
475  int sidx = PREAMBLE_SIZE, didx = 0;
476  int y, x;
477 
478  for (y = 0; y < avctx->height; y++) {
479  if (!gdv->scale_v) {
480  for (x = 0; x < avctx->width; x++) {
481  dst[didx + x] = gdv->frame[sidx + x];
482  }
483  } else {
484  for (x = 0; x < avctx->width; x++) {
485  dst[didx + x] = gdv->frame[sidx + x/2];
486  }
487  }
488  if (!gdv->scale_h || ((y & 1) == 1)) {
489  sidx += !gdv->scale_v ? avctx->width : avctx->width/2;
490  }
491  didx += frame->linesize[0];
492  }
493  }
494 
495  *got_frame = 1;
496 
497  return ret < 0 ? ret : avpkt->size;
498 }
499 
501 {
502  GDVContext *gdv = avctx->priv_data;
503  av_freep(&gdv->frame);
504  return 0;
505 }
506 
508  .name = "gdv",
509  .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
510  .type = AVMEDIA_TYPE_VIDEO,
511  .id = AV_CODEC_ID_GDV,
512  .priv_data_size = sizeof(GDVContext),
514  .close = gdv_decode_close,
516  .capabilities = AV_CODEC_CAP_DR1,
517  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
518 };
const char const char void * val
Definition: avisynth_c.h:771
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
GetByteContext gb
Definition: gdv.c:31
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
const char * g
Definition: vf_curves.c:112
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1431
const char * b
Definition: vf_curves.c:113
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:143
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
AVCodec ff_gdv_decoder
Definition: gdv.c:507
AVCodec.
Definition: avcodec.h:3408
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
unsigned scale_h
Definition: gdv.c:38
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define av_cold
Definition: attributes.h:82
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
static AVFrame * frame
uint8_t fill
Definition: gdv.c:43
uint8_t * data
Definition: avcodec.h:1430
static int flags
Definition: log.c:55
uint32_t tag
Definition: movenc.c:1455
static const uint64_t c1
Definition: murmur3.c:49
static int gdv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: gdv.c:399
unsigned scale_v
Definition: gdv.c:38
uint32_t pal[256]
Definition: gdv.c:35
#define U(x)
Definition: vp56_arith.h:37
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: avcodec.h:1149
static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
Definition: bytestream.h:159
#define AVERROR(e)
Definition: error.h:43
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:350
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * r
Definition: vf_curves.c:111
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
Definition: gdv.c:28
uint8_t queue
Definition: gdv.c:42
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:193
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:176
static void rescale(GDVContext *gdv, uint8_t *dst, int w, int h, int scale_v, int scale_h)
Definition: gdv.c:75
int width
picture width / height.
Definition: avcodec.h:1690
uint8_t w
Definition: llviddspenc.c:38
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
main external API structure.
Definition: avcodec.h:1518
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1891
static av_cold int gdv_decode_close(AVCodecContext *avctx)
Definition: gdv.c:500
uint8_t fill
Definition: gdv.c:48
static void fill_bits32(Bits32 *bits, GetByteContext *gb)
Definition: gdv.c:147
static int decompress_68(AVCodecContext *avctx, unsigned skip, unsigned use8)
Definition: gdv.c:277
static int read_bits32(Bits32 *bits, GetByteContext *gb, int nbits)
Definition: gdv.c:153
static av_cold int gdv_decode_init(AVCodecContext *avctx)
Definition: gdv.c:53
static int decompress_5(AVCodecContext *avctx, unsigned skip)
Definition: gdv.c:234
#define PREAMBLE_SIZE
Definition: gdv.c:51
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
static void lz_copy(PutByteContext *pb, GetByteContext *g2, int offset, unsigned len)
Definition: gdv.c:167
static int decompress_2(AVCodecContext *avctx)
Definition: gdv.c:196
common internal api header.
common internal and external API header
Definition: gdv.c:46
static double c[64]
unsigned frame_size
Definition: gdv.c:37
uint8_t * frame
Definition: gdv.c:36
static const uint64_t c2
Definition: murmur3.c:50
Definition: gdv.c:41
void * priv_data
Definition: avcodec.h:1545
int len
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
AVCodecContext * avctx
Definition: gdv.c:29
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
uint32_t queue
Definition: gdv.c:47
PutByteContext pb
Definition: gdv.c:33
GetByteContext g2
Definition: gdv.c:32
static int read_bits2(Bits8 *bits, GetByteContext *gb)
Definition: gdv.c:132