FFmpeg
smacker.c
Go to the documentation of this file.
1 /*
2  * Smacker decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Smacker decoder
25  */
26 
27 /*
28  * Based on http://wiki.multimedia.cx/index.php?title=Smacker
29  */
30 
31 #include <stddef.h>
32 
34 
35 #include "avcodec.h"
36 
37 #define SMKTREE_BITS 9
38 #define SMK_NODE 0x80000000
39 
40 #define SMKTREE_DECODE_MAX_RECURSION FFMIN(32, 3 * SMKTREE_BITS)
41 #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
42 
43 /* The maximum possible unchecked overread happens in decode_header_trees:
44  * Decoding the MMAP tree can overread by 6 * SMKTREE_BITS + 1, followed by
45  * three get_bits1, followed by at most 2 + 3 * 16 read bits when reading
46  * the TYPE tree before the next check. 64 is because of 64 bit reads. */
47 #if (6 * SMKTREE_BITS + 1 + 3 + (2 + 3 * 16) + 64) <= 8 * AV_INPUT_BUFFER_PADDING_SIZE
48 #define UNCHECKED_BITSTREAM_READER 1
49 #endif
50 #define BITSTREAM_READER_LE
51 #include "bytestream.h"
52 #include "codec_internal.h"
53 #include "decode.h"
54 #include "get_bits.h"
55 
56 typedef struct SmackVContext {
59 
61  int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
63 
64 typedef struct HuffEntry {
65  uint8_t value;
66  uint8_t length;
67 } HuffEntry;
68 
69 /**
70  * Context used for code reconstructing
71  */
72 typedef struct HuffContext {
73  int current;
75 } HuffContext;
76 
77 /* common parameters used for decode_bigtree */
78 typedef struct DBCtx {
80  int *values;
81  VLC *v1, *v2;
82  uint8_t vals[2];
83  int escapes[3];
84  int *last;
85 } DBCtx;
86 
87 /* possible runs of blocks */
88 static const int block_runs[64] = {
89  1, 2, 3, 4, 5, 6, 7, 8,
90  9, 10, 11, 12, 13, 14, 15, 16,
91  17, 18, 19, 20, 21, 22, 23, 24,
92  25, 26, 27, 28, 29, 30, 31, 32,
93  33, 34, 35, 36, 37, 38, 39, 40,
94  41, 42, 43, 44, 45, 46, 47, 48,
95  49, 50, 51, 52, 53, 54, 55, 56,
96  57, 58, 59, 128, 256, 512, 1024, 2048 };
97 
103 
104 /**
105  * Decode local frame tree
106  *
107  * Can read SMKTREE_DECODE_MAX_RECURSION before the first check;
108  * does not overread gb on success.
109  */
110 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, int length)
111 {
112  if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
113  av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
114  return AVERROR_INVALIDDATA;
115  }
116 
117  if(!get_bits1(gb)){ //Leaf
118  if (hc->current >= 256) {
119  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
120  return AVERROR_INVALIDDATA;
121  }
122  if (get_bits_left(gb) < 8)
123  return AVERROR_INVALIDDATA;
124  hc->entries[hc->current++] = (HuffEntry){ get_bits(gb, 8), length };
125  return 0;
126  } else { //Node
127  int r;
128  length++;
129  r = smacker_decode_tree(gb, hc, length);
130  if(r)
131  return r;
132  return smacker_decode_tree(gb, hc, length);
133  }
134 }
135 
136 /**
137  * Decode header tree
138  *
139  * Checks before the first read, can overread by 6 * SMKTREE_BITS on success.
140  */
141 static int smacker_decode_bigtree(GetBitContext *gb, DBCtx *ctx, int length)
142 {
143  // Larger length can cause segmentation faults due to too deep recursion.
144  if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
145  av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
146  return AVERROR_INVALIDDATA;
147  }
148 
149  if (ctx->current >= ctx->length) {
150  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
151  return AVERROR_INVALIDDATA;
152  }
153  if (get_bits_left(gb) <= 0)
154  return AVERROR_INVALIDDATA;
155  if(!get_bits1(gb)){ //Leaf
156  int val, i1, i2;
157  i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3)
158  : ctx->vals[0];
159  i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3)
160  : ctx->vals[1];
161  val = i1 | (i2 << 8);
162  if(val == ctx->escapes[0]) {
163  ctx->last[0] = ctx->current;
164  val = 0;
165  } else if(val == ctx->escapes[1]) {
166  ctx->last[1] = ctx->current;
167  val = 0;
168  } else if(val == ctx->escapes[2]) {
169  ctx->last[2] = ctx->current;
170  val = 0;
171  }
172 
173  ctx->values[ctx->current++] = val;
174  return 1;
175  } else { //Node
176  int r = 0, r_new, t;
177 
178  t = ctx->current++;
179  r = smacker_decode_bigtree(gb, ctx, length + 1);
180  if(r < 0)
181  return r;
182  ctx->values[t] = SMK_NODE | r;
183  r++;
184  r_new = smacker_decode_bigtree(gb, ctx, length + 1);
185  if (r_new < 0)
186  return r_new;
187  return r + r_new;
188  }
189 }
190 
191 /**
192  * Store large tree as FFmpeg's vlc codes
193  *
194  * Can read FFMAX(1 + SMKTREE_DECODE_MAX_RECURSION, 2 + 3 * 16) bits
195  * before the first check; can overread by 6 * SMKTREE_BITS + 1 on success.
196  */
197 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
198 {
199  VLC vlc[2] = { { 0 } };
200  int escapes[3];
201  DBCtx ctx;
202  int err;
203 
204  if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
205  av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
206  return AVERROR_INVALIDDATA;
207  }
208 
209  for (int i = 0; i < 2; i++) {
210  HuffContext h;
211  h.current = 0;
212  if (!get_bits1(gb)) {
213  ctx.vals[i] = 0;
214  av_log(smk->avctx, AV_LOG_ERROR, "Skipping %s bytes tree\n",
215  i ? "high" : "low");
216  continue;
217  }
218  err = smacker_decode_tree(gb, &h, 0);
219  if (err < 0)
220  goto error;
221  skip_bits1(gb);
222  if (h.current > 1) {
223  err = ff_init_vlc_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
224  &h.entries[0].length, sizeof(*h.entries),
225  &h.entries[0].value, sizeof(*h.entries), 1,
226  0, INIT_VLC_OUTPUT_LE, smk->avctx);
227  if (err < 0) {
228  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
229  goto error;
230  }
231  } else
232  ctx.vals[i] = h.entries[0].value;
233  }
234 
235  escapes[0] = get_bits(gb, 16);
236  escapes[1] = get_bits(gb, 16);
237  escapes[2] = get_bits(gb, 16);
238 
239  last[0] = last[1] = last[2] = -1;
240 
241  ctx.escapes[0] = escapes[0];
242  ctx.escapes[1] = escapes[1];
243  ctx.escapes[2] = escapes[2];
244  ctx.v1 = &vlc[0];
245  ctx.v2 = &vlc[1];
246  ctx.last = last;
247  ctx.length = (size + 3) >> 2;
248  ctx.current = 0;
249  ctx.values = av_malloc_array(ctx.length + 3, sizeof(ctx.values[0]));
250  if (!ctx.values) {
251  err = AVERROR(ENOMEM);
252  goto error;
253  }
254  *recodes = ctx.values;
255 
256  err = smacker_decode_bigtree(gb, &ctx, 0);
257  if (err < 0)
258  goto error;
259  skip_bits1(gb);
260  if (ctx.last[0] == -1) ctx.last[0] = ctx.current++;
261  if (ctx.last[1] == -1) ctx.last[1] = ctx.current++;
262  if (ctx.last[2] == -1) ctx.last[2] = ctx.current++;
263 
264  err = 0;
265 error:
266  for (int i = 0; i < 2; i++) {
267  ff_free_vlc(&vlc[i]);
268  }
269 
270  return err;
271 }
272 
274  GetBitContext gb;
275  int mmap_size, mclr_size, full_size, type_size, ret;
276  int skip = 0;
277 
278  mmap_size = AV_RL32(smk->avctx->extradata);
279  mclr_size = AV_RL32(smk->avctx->extradata + 4);
280  full_size = AV_RL32(smk->avctx->extradata + 8);
281  type_size = AV_RL32(smk->avctx->extradata + 12);
282 
283  ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
284  if (ret < 0)
285  return ret;
286 
287  if(!get_bits1(&gb)) {
288  skip ++;
289  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
290  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
291  if (!smk->mmap_tbl)
292  return AVERROR(ENOMEM);
293  smk->mmap_tbl[0] = 0;
294  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
295  } else {
296  ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
297  if (ret < 0)
298  return ret;
299  }
300  if(!get_bits1(&gb)) {
301  skip ++;
302  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
303  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
304  if (!smk->mclr_tbl)
305  return AVERROR(ENOMEM);
306  smk->mclr_tbl[0] = 0;
307  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
308  } else {
309  ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
310  if (ret < 0)
311  return ret;
312  }
313  if(!get_bits1(&gb)) {
314  skip ++;
315  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
316  smk->full_tbl = av_malloc(sizeof(int) * 2);
317  if (!smk->full_tbl)
318  return AVERROR(ENOMEM);
319  smk->full_tbl[0] = 0;
320  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
321  } else {
322  ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
323  if (ret < 0)
324  return ret;
325  }
326  if(!get_bits1(&gb)) {
327  skip ++;
328  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
329  smk->type_tbl = av_malloc(sizeof(int) * 2);
330  if (!smk->type_tbl)
331  return AVERROR(ENOMEM);
332  smk->type_tbl[0] = 0;
333  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
334  } else {
335  ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
336  if (ret < 0)
337  return ret;
338  }
339  if (skip == 4 || get_bits_left(&gb) < 0)
340  return AVERROR_INVALIDDATA;
341 
342  return 0;
343 }
344 
345 static av_always_inline void last_reset(int *recode, int *last) {
346  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
347 }
348 
349 /* Get code and update history.
350  * Checks before reading, does not overread. */
351 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
352  register int *table = recode;
353  int v;
354 
355  while(*table & SMK_NODE) {
356  if (get_bits_left(gb) < 1)
357  return AVERROR_INVALIDDATA;
358  if(get_bits1(gb))
359  table += (*table) & (~SMK_NODE);
360  table++;
361  }
362  v = *table;
363 
364  if(v != recode[last[0]]) {
365  recode[last[2]] = recode[last[1]];
366  recode[last[1]] = recode[last[0]];
367  recode[last[0]] = v;
368  }
369  return v;
370 }
371 
372 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
373  int *got_frame, AVPacket *avpkt)
374 {
375  SmackVContext * const smk = avctx->priv_data;
376  uint8_t *out;
377  uint32_t *pal;
378  GetByteContext gb2;
379  GetBitContext gb;
380  int blocks, blk, bw, bh;
381  int i, ret;
382  int stride;
383  int flags;
384 
385  if (avpkt->size <= 769)
386  return AVERROR_INVALIDDATA;
387 
388  if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
389  return ret;
390 
391  /* make the palette available on the way out */
392  pal = (uint32_t*)smk->pic->data[1];
393  bytestream2_init(&gb2, avpkt->data, avpkt->size);
394  flags = bytestream2_get_byteu(&gb2);
395  smk->pic->palette_has_changed = flags & 1;
396  smk->pic->key_frame = !!(flags & 2);
397  if (smk->pic->key_frame)
399  else
401 
402  for(i = 0; i < 256; i++)
403  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
404 
405  last_reset(smk->mmap_tbl, smk->mmap_last);
406  last_reset(smk->mclr_tbl, smk->mclr_last);
407  last_reset(smk->full_tbl, smk->full_last);
408  last_reset(smk->type_tbl, smk->type_last);
409  if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
410  return ret;
411 
412  blk = 0;
413  bw = avctx->width >> 2;
414  bh = avctx->height >> 2;
415  blocks = bw * bh;
416  stride = smk->pic->linesize[0];
417  while(blk < blocks) {
418  int type, run, mode;
419  uint16_t pix;
420 
421  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
422  if (type < 0)
423  return type;
424  run = block_runs[(type >> 2) & 0x3F];
425  switch(type & 3){
426  case SMK_BLK_MONO:
427  while(run-- && blk < blocks){
428  int clr, map;
429  int hi, lo;
430  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
431  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
432  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
433  hi = clr >> 8;
434  lo = clr & 0xFF;
435  for(i = 0; i < 4; i++) {
436  if(map & 1) out[0] = hi; else out[0] = lo;
437  if(map & 2) out[1] = hi; else out[1] = lo;
438  if(map & 4) out[2] = hi; else out[2] = lo;
439  if(map & 8) out[3] = hi; else out[3] = lo;
440  map >>= 4;
441  out += stride;
442  }
443  blk++;
444  }
445  break;
446  case SMK_BLK_FULL:
447  mode = 0;
448  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
449  if (get_bits_left(&gb) < 1)
450  return AVERROR_INVALIDDATA;
451  if(get_bits1(&gb)) mode = 1;
452  else if(get_bits1(&gb)) mode = 2;
453  }
454  while(run-- && blk < blocks){
455  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
456  switch(mode){
457  case 0:
458  for(i = 0; i < 4; i++) {
459  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
460  AV_WL16(out+2,pix);
461  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
462  AV_WL16(out,pix);
463  out += stride;
464  }
465  break;
466  case 1:
467  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
468  out[0] = out[1] = pix & 0xFF;
469  out[2] = out[3] = pix >> 8;
470  out += stride;
471  out[0] = out[1] = pix & 0xFF;
472  out[2] = out[3] = pix >> 8;
473  out += stride;
474  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
475  out[0] = out[1] = pix & 0xFF;
476  out[2] = out[3] = pix >> 8;
477  out += stride;
478  out[0] = out[1] = pix & 0xFF;
479  out[2] = out[3] = pix >> 8;
480  break;
481  case 2:
482  for(i = 0; i < 2; i++) {
483  uint16_t pix1, pix2;
484  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
485  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
486  AV_WL16(out,pix1);
487  AV_WL16(out+2,pix2);
488  out += stride;
489  AV_WL16(out,pix1);
490  AV_WL16(out+2,pix2);
491  out += stride;
492  }
493  break;
494  }
495  blk++;
496  }
497  break;
498  case SMK_BLK_SKIP:
499  while(run-- && blk < blocks)
500  blk++;
501  break;
502  case SMK_BLK_FILL:
503  mode = type >> 8;
504  while(run-- && blk < blocks){
505  uint32_t col;
506  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
507  col = mode * 0x01010101U;
508  for(i = 0; i < 4; i++) {
509  *((uint32_t*)out) = col;
510  out += stride;
511  }
512  blk++;
513  }
514  break;
515  }
516 
517  }
518 
519  if ((ret = av_frame_ref(rframe, smk->pic)) < 0)
520  return ret;
521 
522  *got_frame = 1;
523 
524  /* always report that the buffer was completely consumed */
525  return avpkt->size;
526 }
527 
528 
530 {
531  SmackVContext * const smk = avctx->priv_data;
532 
533  av_freep(&smk->mmap_tbl);
534  av_freep(&smk->mclr_tbl);
535  av_freep(&smk->full_tbl);
536  av_freep(&smk->type_tbl);
537 
538  av_frame_free(&smk->pic);
539 
540  return 0;
541 }
542 
543 
545 {
546  SmackVContext * const c = avctx->priv_data;
547  int ret;
548 
549  c->avctx = avctx;
550 
551  avctx->pix_fmt = AV_PIX_FMT_PAL8;
552 
553  c->pic = av_frame_alloc();
554  if (!c->pic)
555  return AVERROR(ENOMEM);
556 
557  /* decode huffman trees from extradata */
558  if (avctx->extradata_size <= 16){
559  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
560  return AVERROR(EINVAL);
561  }
562 
564  if (ret < 0) {
565  return ret;
566  }
567 
568  return 0;
569 }
570 
571 
573 {
574  int channels = avctx->ch_layout.nb_channels;
576  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
577  return AVERROR_INVALIDDATA;
578  }
582 
583  return 0;
584 }
585 
586 /**
587  * Decode Smacker audio data
588  */
590  int *got_frame_ptr, AVPacket *avpkt)
591 {
592  const uint8_t *buf = avpkt->data;
593  int buf_size = avpkt->size;
594  GetBitContext gb;
595  VLC vlc[4] = { { 0 } };
596  int16_t *samples;
597  uint8_t *samples8;
598  uint8_t values[4];
599  int i, res, ret;
600  int unp_size;
601  int bits, stereo;
602  unsigned pred[2], val, val2;
603 
604  if (buf_size <= 4) {
605  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
606  return AVERROR_INVALIDDATA;
607  }
608 
609  unp_size = AV_RL32(buf);
610 
611  if (unp_size > (1U<<24)) {
612  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
613  return AVERROR_INVALIDDATA;
614  }
615 
616  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
617  return ret;
618 
619  if(!get_bits1(&gb)){
620  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
621  *got_frame_ptr = 0;
622  return 1;
623  }
624  stereo = get_bits1(&gb);
625  bits = get_bits1(&gb);
626  if (stereo ^ (avctx->ch_layout.nb_channels != 1)) {
627  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
628  return AVERROR_INVALIDDATA;
629  }
630  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
631  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
632  return AVERROR_INVALIDDATA;
633  }
634 
635  /* get output buffer */
636  frame->nb_samples = unp_size / (avctx->ch_layout.nb_channels * (bits + 1));
637  if (unp_size % (avctx->ch_layout.nb_channels * (bits + 1))) {
638  av_log(avctx, AV_LOG_ERROR,
639  "The buffer does not contain an integer number of samples\n");
640  return AVERROR_INVALIDDATA;
641  }
642  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
643  return ret;
644  samples = (int16_t *)frame->data[0];
645  samples8 = frame->data[0];
646 
647  // Initialize
648  for(i = 0; i < (1 << (bits + stereo)); i++) {
649  HuffContext h;
650  h.current = 0;
651  skip_bits1(&gb);
652  if ((ret = smacker_decode_tree(&gb, &h, 0)) < 0)
653  goto error;
654  skip_bits1(&gb);
655  if (h.current > 1) {
656  ret = ff_init_vlc_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
657  &h.entries[0].length, sizeof(*h.entries),
658  &h.entries[0].value, sizeof(*h.entries), 1,
659  0, INIT_VLC_OUTPUT_LE, avctx);
660  if (ret < 0) {
661  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
662  goto error;
663  }
664  } else
665  values[i] = h.entries[0].value;
666  }
667  /* this codec relies on wraparound instead of clipping audio */
668  if(bits) { //decode 16-bit data
669  for(i = stereo; i >= 0; i--)
670  pred[i] = av_bswap16(get_bits(&gb, 16));
671  for(i = 0; i <= stereo; i++)
672  *samples++ = pred[i];
673  unp_size /= 2;
674 
675  if (vlc[0 ].table || vlc[ 1].table ||
676  vlc[2*stereo].table || vlc[2*stereo+1].table) {
677  for(; i < unp_size ; i++) {
678  unsigned idx = 2 * (i & stereo);
679  if (get_bits_left(&gb) < 0) {
681  goto error;
682  }
683  if (vlc[idx].table)
684  res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
685  else
686  res = values[idx];
687  val = res;
688  if (vlc[++idx].table)
689  res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
690  else
691  res = values[idx];
692  val |= res << 8;
693  pred[idx / 2] += val;
694  *samples++ = pred[idx / 2];
695  }
696  } else if (stereo) {
697  val = 256*values[1] + values[0];
698  val2 = 256*values[3] + values[2];
699  for(; i < unp_size; i+=2) {
700  pred[0] += val;
701  pred[1] += val2;
702  *samples++ = pred[0];
703  *samples++ = pred[1];
704  }
705  } else {
706  val = 256*values[1] + values[0];
707  for(; i < unp_size; i++) {
708  pred[0] += val;
709  *samples++ = pred[0];
710  }
711  }
712  } else { //8-bit data
713  for(i = stereo; i >= 0; i--)
714  pred[i] = get_bits(&gb, 8);
715  for(i = 0; i <= stereo; i++)
716  *samples8++ = pred[i];
717  for(; i < unp_size; i++) {
718  unsigned idx = i & stereo;
719  if (get_bits_left(&gb) < 0) {
721  goto error;
722  }
723  if (vlc[idx].table)
724  val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
725  else
726  val = values[idx];
727  pred[idx] += val;
728  *samples8++ = pred[idx];
729  }
730  }
731 
732  *got_frame_ptr = 1;
733  ret = buf_size;
734 
735 error:
736  for(i = 0; i < 4; i++) {
737  ff_free_vlc(&vlc[i]);
738  }
739 
740  return ret;
741 }
742 
744  .p.name = "smackvid",
745  CODEC_LONG_NAME("Smacker video"),
746  .p.type = AVMEDIA_TYPE_VIDEO,
747  .p.id = AV_CODEC_ID_SMACKVIDEO,
748  .priv_data_size = sizeof(SmackVContext),
749  .init = decode_init,
750  .close = decode_end,
752  .p.capabilities = AV_CODEC_CAP_DR1,
753  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
754 };
755 
757  .p.name = "smackaud",
758  CODEC_LONG_NAME("Smacker audio"),
759  .p.type = AVMEDIA_TYPE_AUDIO,
760  .p.id = AV_CODEC_ID_SMACKAUDIO,
761  .init = smka_decode_init,
763  .p.capabilities = AV_CODEC_CAP_DR1,
764 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
ff_smacker_decoder
const FFCodec ff_smacker_decoder
Definition: smacker.c:743
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
r
const char * r
Definition: vf_curves.c:126
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
SMK_BLK_FULL
@ SMK_BLK_FULL
Definition: smacker.c:100
HuffContext
Context used for code reconstructing.
Definition: smacker.c:72
out
FILE * out
Definition: movenc.c:54
INIT_VLC_OUTPUT_LE
#define INIT_VLC_OUTPUT_LE
Definition: vlc.h:98
GetByteContext
Definition: bytestream.h:33
SmackVContext::full_last
int full_last[3]
Definition: smacker.c:61
SMK_BLK_SKIP
@ SMK_BLK_SKIP
Definition: smacker.c:101
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:116
smacker_decode_tree
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, int length)
Decode local frame tree.
Definition: smacker.c:110
block_runs
static const int block_runs[64]
Definition: smacker.c:88
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVPacket::data
uint8_t * data
Definition: packet.h:374
table
static const uint16_t table[]
Definition: prosumer.c:205
FFCodec
Definition: codec_internal.h:119
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
HuffEntry::value
uint8_t value
Definition: smacker.c:65
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
DBCtx::length
int length
Definition: smacker.c:79
ff_smackaud_decoder
const FFCodec ff_smackaud_decoder
Definition: smacker.c:756
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2059
SmackVContext
Definition: smacker.c:56
GetBitContext
Definition: get_bits.h:61
AV_CODEC_ID_SMACKAUDIO
@ AV_CODEC_ID_SMACKAUDIO
Definition: codec_id.h:458
SMKTREE_DECODE_BIG_MAX_RECURSION
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition: smacker.c:41
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:417
val
static double val(void *priv, double ch)
Definition: aeval.c:77
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
SmackVContext::mclr_last
int mclr_last[3]
Definition: smacker.c:61
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:104
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:667
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:544
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:500
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
smka_decode_init
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:572
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:363
bits
uint8_t bits
Definition: vp3data.h:128
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:31
decode.h
get_bits.h
blk
#define blk(i)
Definition: sha.c:186
SmackVContext::type_last
int type_last[3]
Definition: smacker.c:61
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
HuffContext::entries
HuffEntry entries[256]
Definition: smacker.c:74
DBCtx::escapes
int escapes[3]
Definition: smacker.c:83
smka_decode_frame
static int smka_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:589
NULL
#define NULL
Definition: coverity.c:32
run
uint8_t run
Definition: svq3.c:203
SmkBlockTypes
SmkBlockTypes
Definition: smacker.c:98
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
ff_init_vlc_from_lengths
int ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: vlc.c:328
HuffEntry::length
uint8_t length
Definition: smacker.c:66
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:372
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:787
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
SMKTREE_DECODE_MAX_RECURSION
#define SMKTREE_DECODE_MAX_RECURSION
Definition: smacker.c:40
smacker_decode_bigtree
static int smacker_decode_bigtree(GetBitContext *gb, DBCtx *ctx, int length)
Decode header tree.
Definition: smacker.c:141
smacker_decode_header_tree
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
Store large tree as FFmpeg's vlc codes.
Definition: smacker.c:197
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1420
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:422
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:353
codec_internal.h
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:529
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1023
size
int size
Definition: twinvq_data.h:10344
SMK_BLK_FILL
@ SMK_BLK_FILL
Definition: smacker.c:102
SmackVContext::avctx
AVCodecContext * avctx
Definition: smacker.c:57
DBCtx::v1
VLC * v1
Definition: smacker.c:81
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
av_bswap16
#define av_bswap16
Definition: bswap.h:31
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
SmackVContext::mmap_tbl
int * mmap_tbl
Definition: smacker.c:60
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:962
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1444
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:499
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
av_always_inline
#define av_always_inline
Definition: attributes.h:49
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
DBCtx::values
int * values
Definition: smacker.c:80
AVCodecContext::height
int height
Definition: avcodec.h:571
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:608
ff_free_vlc
void ff_free_vlc(VLC *vlc)
Definition: vlc.c:375
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
AV_CODEC_ID_SMACKVIDEO
@ AV_CODEC_ID_SMACKVIDEO
Definition: codec_id.h:135
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1538
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
DBCtx::v2
VLC * v2
Definition: smacker.c:81
last_reset
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:345
DBCtx::vals
uint8_t vals[2]
Definition: smacker.c:82
SmackVContext::type_tbl
int * type_tbl
Definition: smacker.c:60
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:398
SMKTREE_BITS
#define SMKTREE_BITS
Definition: smacker.c:37
SmackVContext::mclr_tbl
int * mclr_tbl
Definition: smacker.c:60
channel_layout.h
SmackVContext::mmap_last
int mmap_last[3]
Definition: smacker.c:61
AVFrame::palette_has_changed
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:487
mode
mode
Definition: ebur128.h:83
VLC
Definition: vlc.h:31
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:632
SmackVContext::full_tbl
int * full_tbl
Definition: smacker.c:60
HuffContext::current
int current
Definition: smacker.c:73
SmackVContext::pic
AVFrame * pic
Definition: smacker.c:58
DBCtx::last
int * last
Definition: smacker.c:84
HuffEntry
Definition: exr.c:94
values
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values
Definition: filter_design.txt:263
DBCtx
Definition: smacker.c:78
SMK_BLK_MONO
@ SMK_BLK_MONO
Definition: smacker.c:99
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
DBCtx::current
int current
Definition: smacker.c:79
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
SMK_NODE
#define SMK_NODE
Definition: smacker.c:38
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:423
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
decode_header_trees
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:273
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:571
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:370
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2038
smk_get_code
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition: smacker.c:351