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 #include "libavutil/mem.h"
35 
36 #include "avcodec.h"
37 
38 #define SMKTREE_BITS 9
39 #define SMK_NODE 0x80000000
40 
41 #define SMKTREE_DECODE_MAX_RECURSION FFMIN(32, 3 * SMKTREE_BITS)
42 #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
43 
44 /* The maximum possible unchecked overread happens in decode_header_trees:
45  * Decoding the MMAP tree can overread by 6 * SMKTREE_BITS + 1, followed by
46  * three get_bits1, followed by at most 2 + 3 * 16 read bits when reading
47  * the TYPE tree before the next check. 64 is because of 64 bit reads. */
48 #if (6 * SMKTREE_BITS + 1 + 3 + (2 + 3 * 16) + 64) <= 8 * AV_INPUT_BUFFER_PADDING_SIZE
49 #define UNCHECKED_BITSTREAM_READER 1
50 #endif
51 #define BITSTREAM_READER_LE
52 #include "bytestream.h"
53 #include "codec_internal.h"
54 #include "decode.h"
55 #include "get_bits.h"
56 
57 typedef struct SmackVContext {
60 
62  int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
64 
65 typedef struct HuffEntry {
66  uint8_t value;
67  uint8_t length;
68 } HuffEntry;
69 
70 /**
71  * Context used for code reconstructing
72  */
73 typedef struct HuffContext {
74  int current;
76 } HuffContext;
77 
78 /* common parameters used for decode_bigtree */
79 typedef struct DBCtx {
81  int *values;
82  VLC *v1, *v2;
83  uint8_t vals[2];
84  int escapes[3];
85  int *last;
86 } DBCtx;
87 
88 /* possible runs of blocks */
89 static const int block_runs[64] = {
90  1, 2, 3, 4, 5, 6, 7, 8,
91  9, 10, 11, 12, 13, 14, 15, 16,
92  17, 18, 19, 20, 21, 22, 23, 24,
93  25, 26, 27, 28, 29, 30, 31, 32,
94  33, 34, 35, 36, 37, 38, 39, 40,
95  41, 42, 43, 44, 45, 46, 47, 48,
96  49, 50, 51, 52, 53, 54, 55, 56,
97  57, 58, 59, 128, 256, 512, 1024, 2048 };
98 
104 
105 /**
106  * Decode local frame tree
107  *
108  * Can read SMKTREE_DECODE_MAX_RECURSION before the first check;
109  * does not overread gb on success.
110  */
111 static int smacker_decode_tree(AVCodecContext *avctx, GetBitContext *gb, HuffContext *hc, int length)
112 {
113  if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
114  av_log(avctx, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
115  return AVERROR_INVALIDDATA;
116  }
117 
118  if(!get_bits1(gb)){ //Leaf
119  if (hc->current >= 256) {
120  av_log(avctx, AV_LOG_ERROR, "Tree size exceeded!\n");
121  return AVERROR_INVALIDDATA;
122  }
123  if (get_bits_left(gb) < 8)
124  return AVERROR_INVALIDDATA;
125  hc->entries[hc->current++] = (HuffEntry){ get_bits(gb, 8), length };
126  return 0;
127  } else { //Node
128  int r;
129  length++;
130  r = smacker_decode_tree(avctx, gb, hc, length);
131  if(r)
132  return r;
133  return smacker_decode_tree(avctx, gb, hc, length);
134  }
135 }
136 
137 /**
138  * Decode header tree
139  *
140  * Checks before the first read, can overread by 6 * SMKTREE_BITS on success.
141  */
142 static int smacker_decode_bigtree(AVCodecContext *avctx, GetBitContext *gb, DBCtx *ctx, int length)
143 {
144  // Larger length can cause segmentation faults due to too deep recursion.
145  if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
146  av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
147  return AVERROR_INVALIDDATA;
148  }
149 
150  if (ctx->current >= ctx->length) {
151  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
152  return AVERROR_INVALIDDATA;
153  }
154  if (get_bits_left(gb) <= 0)
155  return AVERROR_INVALIDDATA;
156  if(!get_bits1(gb)){ //Leaf
157  int val, i1, i2;
158  i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3)
159  : ctx->vals[0];
160  i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3)
161  : ctx->vals[1];
162  val = i1 | (i2 << 8);
163  if(val == ctx->escapes[0]) {
164  ctx->last[0] = ctx->current;
165  val = 0;
166  } else if(val == ctx->escapes[1]) {
167  ctx->last[1] = ctx->current;
168  val = 0;
169  } else if(val == ctx->escapes[2]) {
170  ctx->last[2] = ctx->current;
171  val = 0;
172  }
173 
174  ctx->values[ctx->current++] = val;
175  return 1;
176  } else { //Node
177  int r = 0, r_new, t;
178 
179  t = ctx->current++;
180  r = smacker_decode_bigtree(avctx, gb, ctx, length + 1);
181  if(r < 0)
182  return r;
183  ctx->values[t] = SMK_NODE | r;
184  r++;
185  r_new = smacker_decode_bigtree(avctx, gb, ctx, length + 1);
186  if (r_new < 0)
187  return r_new;
188  return r + r_new;
189  }
190 }
191 
192 /**
193  * Store large tree as FFmpeg's vlc codes
194  *
195  * Can read FFMAX(1 + SMKTREE_DECODE_MAX_RECURSION, 2 + 3 * 16) bits
196  * before the first check; can overread by 6 * SMKTREE_BITS + 1 on success.
197  */
198 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
199 {
200  VLC vlc[2] = { { 0 } };
201  int escapes[3];
202  DBCtx ctx;
203  int err;
204 
205  if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
206  av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
207  return AVERROR_INVALIDDATA;
208  }
209 
210  for (int i = 0; i < 2; i++) {
211  HuffContext h;
212  h.current = 0;
213  if (!get_bits1(gb)) {
214  ctx.vals[i] = 0;
215  av_log(smk->avctx, AV_LOG_ERROR, "Skipping %s bytes tree\n",
216  i ? "high" : "low");
217  continue;
218  }
219  err = smacker_decode_tree(smk->avctx, gb, &h, 0);
220  if (err < 0)
221  goto error;
222  skip_bits1(gb);
223  if (h.current > 1) {
224  err = ff_vlc_init_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
225  &h.entries[0].length, sizeof(*h.entries),
226  &h.entries[0].value, sizeof(*h.entries), 1,
227  0, VLC_INIT_OUTPUT_LE, smk->avctx);
228  if (err < 0) {
229  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
230  goto error;
231  }
232  } else
233  ctx.vals[i] = h.entries[0].value;
234  }
235 
236  escapes[0] = get_bits(gb, 16);
237  escapes[1] = get_bits(gb, 16);
238  escapes[2] = get_bits(gb, 16);
239 
240  last[0] = last[1] = last[2] = -1;
241 
242  ctx.escapes[0] = escapes[0];
243  ctx.escapes[1] = escapes[1];
244  ctx.escapes[2] = escapes[2];
245  ctx.v1 = &vlc[0];
246  ctx.v2 = &vlc[1];
247  ctx.last = last;
248  ctx.length = (size + 3) >> 2;
249  ctx.current = 0;
250  ctx.values = av_malloc_array(ctx.length + 3, sizeof(ctx.values[0]));
251  if (!ctx.values) {
252  err = AVERROR(ENOMEM);
253  goto error;
254  }
255  *recodes = ctx.values;
256 
257  err = smacker_decode_bigtree(smk->avctx, gb, &ctx, 0);
258  if (err < 0)
259  goto error;
260  skip_bits1(gb);
261  if (ctx.last[0] == -1) ctx.last[0] = ctx.current++;
262  if (ctx.last[1] == -1) ctx.last[1] = ctx.current++;
263  if (ctx.last[2] == -1) ctx.last[2] = ctx.current++;
264 
265  err = 0;
266 error:
267  for (int i = 0; i < 2; i++) {
268  ff_vlc_free(&vlc[i]);
269  }
270 
271  return err;
272 }
273 
275  GetBitContext gb;
276  int mmap_size, mclr_size, full_size, type_size, ret;
277  int skip = 0;
278 
279  mmap_size = AV_RL32(smk->avctx->extradata);
280  mclr_size = AV_RL32(smk->avctx->extradata + 4);
281  full_size = AV_RL32(smk->avctx->extradata + 8);
282  type_size = AV_RL32(smk->avctx->extradata + 12);
283 
284  ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
285  if (ret < 0)
286  return ret;
287 
288  if(!get_bits1(&gb)) {
289  skip ++;
290  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
291  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
292  if (!smk->mmap_tbl)
293  return AVERROR(ENOMEM);
294  smk->mmap_tbl[0] = 0;
295  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
296  } else {
297  ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
298  if (ret < 0)
299  return ret;
300  }
301  if(!get_bits1(&gb)) {
302  skip ++;
303  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
304  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
305  if (!smk->mclr_tbl)
306  return AVERROR(ENOMEM);
307  smk->mclr_tbl[0] = 0;
308  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
309  } else {
310  ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
311  if (ret < 0)
312  return ret;
313  }
314  if(!get_bits1(&gb)) {
315  skip ++;
316  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
317  smk->full_tbl = av_malloc(sizeof(int) * 2);
318  if (!smk->full_tbl)
319  return AVERROR(ENOMEM);
320  smk->full_tbl[0] = 0;
321  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
322  } else {
323  ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
324  if (ret < 0)
325  return ret;
326  }
327  if(!get_bits1(&gb)) {
328  skip ++;
329  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
330  smk->type_tbl = av_malloc(sizeof(int) * 2);
331  if (!smk->type_tbl)
332  return AVERROR(ENOMEM);
333  smk->type_tbl[0] = 0;
334  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
335  } else {
336  ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
337  if (ret < 0)
338  return ret;
339  }
340  if (skip == 4 || get_bits_left(&gb) < 0)
341  return AVERROR_INVALIDDATA;
342 
343  return 0;
344 }
345 
346 static av_always_inline void last_reset(int *recode, int *last) {
347  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
348 }
349 
350 /* Get code and update history.
351  * Checks before reading, does not overread. */
352 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
353  register int *table = recode;
354  int v;
355 
356  while(*table & SMK_NODE) {
357  if (get_bits_left(gb) < 1)
358  return AVERROR_INVALIDDATA;
359  if(get_bits1(gb))
360  table += (*table) & (~SMK_NODE);
361  table++;
362  }
363  v = *table;
364 
365  if(v != recode[last[0]]) {
366  recode[last[2]] = recode[last[1]];
367  recode[last[1]] = recode[last[0]];
368  recode[last[0]] = v;
369  }
370  return v;
371 }
372 
373 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
374  int *got_frame, AVPacket *avpkt)
375 {
376  SmackVContext * const smk = avctx->priv_data;
377  uint8_t *out;
378  uint32_t *pal;
379  GetByteContext gb2;
380  GetBitContext gb;
381  int blocks, blk, bw, bh;
382  int i, ret;
383  int stride;
384  int flags;
385 
386  if (avpkt->size <= 769)
387  return AVERROR_INVALIDDATA;
388 
389  if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
390  return ret;
391 
392  /* make the palette available on the way out */
393  pal = (uint32_t*)smk->pic->data[1];
394  bytestream2_init(&gb2, avpkt->data, avpkt->size);
395  flags = bytestream2_get_byteu(&gb2);
396 #if FF_API_PALETTE_HAS_CHANGED
398  smk->pic->palette_has_changed = flags & 1;
400 #endif
401  if (flags & 2) {
402  smk->pic->flags |= AV_FRAME_FLAG_KEY;
404  } else {
405  smk->pic->flags &= ~AV_FRAME_FLAG_KEY;
407  }
408 
409  for(i = 0; i < 256; i++)
410  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
411 
412  last_reset(smk->mmap_tbl, smk->mmap_last);
413  last_reset(smk->mclr_tbl, smk->mclr_last);
414  last_reset(smk->full_tbl, smk->full_last);
415  last_reset(smk->type_tbl, smk->type_last);
416  if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
417  return ret;
418 
419  blk = 0;
420  bw = avctx->width >> 2;
421  bh = avctx->height >> 2;
422  blocks = bw * bh;
423  stride = smk->pic->linesize[0];
424  while(blk < blocks) {
425  int type, run, mode;
426  uint16_t pix;
427 
428  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
429  if (type < 0)
430  return type;
431  run = block_runs[(type >> 2) & 0x3F];
432  switch(type & 3){
433  case SMK_BLK_MONO:
434  while(run-- && blk < blocks){
435  int clr, map;
436  int hi, lo;
437  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
438  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
439  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
440  hi = clr >> 8;
441  lo = clr & 0xFF;
442  for(i = 0; i < 4; i++) {
443  if(map & 1) out[0] = hi; else out[0] = lo;
444  if(map & 2) out[1] = hi; else out[1] = lo;
445  if(map & 4) out[2] = hi; else out[2] = lo;
446  if(map & 8) out[3] = hi; else out[3] = lo;
447  map >>= 4;
448  out += stride;
449  }
450  blk++;
451  }
452  break;
453  case SMK_BLK_FULL:
454  mode = 0;
455  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
456  if (get_bits_left(&gb) < 1)
457  return AVERROR_INVALIDDATA;
458  if(get_bits1(&gb)) mode = 1;
459  else if(get_bits1(&gb)) mode = 2;
460  }
461  while(run-- && blk < blocks){
462  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
463  switch(mode){
464  case 0:
465  for(i = 0; i < 4; i++) {
466  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
467  AV_WL16(out+2,pix);
468  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
469  AV_WL16(out,pix);
470  out += stride;
471  }
472  break;
473  case 1:
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  out += stride;
481  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
482  out[0] = out[1] = pix & 0xFF;
483  out[2] = out[3] = pix >> 8;
484  out += stride;
485  out[0] = out[1] = pix & 0xFF;
486  out[2] = out[3] = pix >> 8;
487  break;
488  case 2:
489  for(i = 0; i < 2; i++) {
490  uint16_t pix1, pix2;
491  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
492  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
493  AV_WL16(out,pix1);
494  AV_WL16(out+2,pix2);
495  out += stride;
496  AV_WL16(out,pix1);
497  AV_WL16(out+2,pix2);
498  out += stride;
499  }
500  break;
501  }
502  blk++;
503  }
504  break;
505  case SMK_BLK_SKIP:
506  while(run-- && blk < blocks)
507  blk++;
508  break;
509  case SMK_BLK_FILL:
510  mode = type >> 8;
511  while(run-- && blk < blocks){
512  uint32_t col;
513  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
514  col = mode * 0x01010101U;
515  for(i = 0; i < 4; i++) {
516  *((uint32_t*)out) = col;
517  out += stride;
518  }
519  blk++;
520  }
521  break;
522  }
523 
524  }
525 
526  if ((ret = av_frame_ref(rframe, smk->pic)) < 0)
527  return ret;
528 
529  *got_frame = 1;
530 
531  /* always report that the buffer was completely consumed */
532  return avpkt->size;
533 }
534 
535 
537 {
538  SmackVContext * const smk = avctx->priv_data;
539 
540  av_freep(&smk->mmap_tbl);
541  av_freep(&smk->mclr_tbl);
542  av_freep(&smk->full_tbl);
543  av_freep(&smk->type_tbl);
544 
545  av_frame_free(&smk->pic);
546 
547  return 0;
548 }
549 
550 
552 {
553  SmackVContext * const c = avctx->priv_data;
554  int ret;
555 
556  c->avctx = avctx;
557 
558  avctx->pix_fmt = AV_PIX_FMT_PAL8;
559 
560  c->pic = av_frame_alloc();
561  if (!c->pic)
562  return AVERROR(ENOMEM);
563 
564  /* decode huffman trees from extradata */
565  if (avctx->extradata_size <= 16){
566  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
567  return AVERROR(EINVAL);
568  }
569 
571  if (ret < 0) {
572  return ret;
573  }
574 
575  return 0;
576 }
577 
578 
580 {
581  int channels = avctx->ch_layout.nb_channels;
583  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
584  return AVERROR_INVALIDDATA;
585  }
589 
590  return 0;
591 }
592 
593 /**
594  * Decode Smacker audio data
595  */
597  int *got_frame_ptr, AVPacket *avpkt)
598 {
599  const uint8_t *buf = avpkt->data;
600  int buf_size = avpkt->size;
601  GetBitContext gb;
602  VLC vlc[4] = { { 0 } };
603  int16_t *samples;
604  uint8_t *samples8;
605  uint8_t values[4];
606  int i, res, ret;
607  int unp_size;
608  int bits, stereo;
609  unsigned pred[2], val, val2;
610 
611  if (buf_size <= 4) {
612  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
613  return AVERROR_INVALIDDATA;
614  }
615 
616  unp_size = AV_RL32(buf);
617 
618  if (unp_size > (1U<<24)) {
619  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
620  return AVERROR_INVALIDDATA;
621  }
622 
623  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
624  return ret;
625 
626  if(!get_bits1(&gb)){
627  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
628  *got_frame_ptr = 0;
629  return 1;
630  }
631  stereo = get_bits1(&gb);
632  bits = get_bits1(&gb);
633  if (stereo ^ (avctx->ch_layout.nb_channels != 1)) {
634  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
635  return AVERROR_INVALIDDATA;
636  }
637  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
638  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
639  return AVERROR_INVALIDDATA;
640  }
641 
642  /* get output buffer */
643  frame->nb_samples = unp_size / (avctx->ch_layout.nb_channels * (bits + 1));
644  if (unp_size % (avctx->ch_layout.nb_channels * (bits + 1))) {
645  av_log(avctx, AV_LOG_ERROR,
646  "The buffer does not contain an integer number of samples\n");
647  return AVERROR_INVALIDDATA;
648  }
649  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
650  return ret;
651  samples = (int16_t *)frame->data[0];
652  samples8 = frame->data[0];
653 
654  // Initialize
655  for(i = 0; i < (1 << (bits + stereo)); i++) {
656  HuffContext h;
657  h.current = 0;
658  skip_bits1(&gb);
659  if ((ret = smacker_decode_tree(avctx, &gb, &h, 0)) < 0)
660  goto error;
661  skip_bits1(&gb);
662  if (h.current > 1) {
663  ret = ff_vlc_init_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
664  &h.entries[0].length, sizeof(*h.entries),
665  &h.entries[0].value, sizeof(*h.entries), 1,
666  0, VLC_INIT_OUTPUT_LE, avctx);
667  if (ret < 0) {
668  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
669  goto error;
670  }
671  } else
672  values[i] = h.entries[0].value;
673  }
674  /* this codec relies on wraparound instead of clipping audio */
675  if(bits) { //decode 16-bit data
676  for(i = stereo; i >= 0; i--)
677  pred[i] = av_bswap16(get_bits(&gb, 16));
678  for(i = 0; i <= stereo; i++)
679  *samples++ = pred[i];
680  unp_size /= 2;
681 
682  if (vlc[0 ].table || vlc[ 1].table ||
683  vlc[2*stereo].table || vlc[2*stereo+1].table) {
684  for(; i < unp_size ; i++) {
685  unsigned idx = 2 * (i & stereo);
686  if (get_bits_left(&gb) < 0) {
688  goto error;
689  }
690  if (vlc[idx].table)
691  res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
692  else
693  res = values[idx];
694  val = res;
695  if (vlc[++idx].table)
696  res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
697  else
698  res = values[idx];
699  val |= res << 8;
700  pred[idx / 2] += val;
701  *samples++ = pred[idx / 2];
702  }
703  } else if (stereo) {
704  val = 256*values[1] + values[0];
705  val2 = 256*values[3] + values[2];
706  for(; i < unp_size; i+=2) {
707  pred[0] += val;
708  pred[1] += val2;
709  *samples++ = pred[0];
710  *samples++ = pred[1];
711  }
712  } else {
713  val = 256*values[1] + values[0];
714  for(; i < unp_size; i++) {
715  pred[0] += val;
716  *samples++ = pred[0];
717  }
718  }
719  } else { //8-bit data
720  for(i = stereo; i >= 0; i--)
721  pred[i] = get_bits(&gb, 8);
722  for(i = 0; i <= stereo; i++)
723  *samples8++ = pred[i];
724  for(; i < unp_size; i++) {
725  unsigned idx = i & stereo;
726  if (get_bits_left(&gb) < 0) {
728  goto error;
729  }
730  if (vlc[idx].table)
731  val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
732  else
733  val = values[idx];
734  pred[idx] += val;
735  *samples8++ = pred[idx];
736  }
737  }
738 
739  *got_frame_ptr = 1;
740  ret = buf_size;
741 
742 error:
743  for(i = 0; i < 4; i++) {
744  ff_vlc_free(&vlc[i]);
745  }
746 
747  return ret;
748 }
749 
751  .p.name = "smackvid",
752  CODEC_LONG_NAME("Smacker video"),
753  .p.type = AVMEDIA_TYPE_VIDEO,
754  .p.id = AV_CODEC_ID_SMACKVIDEO,
755  .priv_data_size = sizeof(SmackVContext),
756  .init = decode_init,
757  .close = decode_end,
759  .p.capabilities = AV_CODEC_CAP_DR1,
760  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
761 };
762 
764  .p.name = "smackaud",
765  CODEC_LONG_NAME("Smacker audio"),
766  .p.type = AVMEDIA_TYPE_AUDIO,
767  .p.id = AV_CODEC_ID_SMACKAUDIO,
768  .init = smka_decode_init,
770  .p.capabilities = AV_CODEC_CAP_DR1,
771 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
ff_smacker_decoder
const FFCodec ff_smacker_decoder
Definition: smacker.c:750
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
ff_vlc_init_from_lengths
int ff_vlc_init_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:306
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:43
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
r
const char * r
Definition: vf_curves.c:127
AVFrame::palette_has_changed
attribute_deprecated int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:596
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:101
HuffContext
Context used for code reconstructing.
Definition: smacker.c:73
out
FILE * out
Definition: movenc.c:55
GetByteContext
Definition: bytestream.h:33
SmackVContext::full_last
int full_last[3]
Definition: smacker.c:62
SMK_BLK_SKIP
@ SMK_BLK_SKIP
Definition: smacker.c:102
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
block_runs
static const int block_runs[64]
Definition: smacker.c:89
mode
Definition: swscale.c:52
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:403
AVPacket::data
uint8_t * data
Definition: packet.h:539
table
static const uint16_t table[]
Definition: prosumer.c:203
FFCodec
Definition: codec_internal.h:127
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:675
smacker_decode_bigtree
static int smacker_decode_bigtree(AVCodecContext *avctx, GetBitContext *gb, DBCtx *ctx, int length)
Decode header tree.
Definition: smacker.c:142
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:328
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:424
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
HuffEntry::value
uint8_t value
Definition: smacker.c:66
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
DBCtx::length
int length
Definition: smacker.c:80
ff_smackaud_decoder
const FFCodec ff_smackaud_decoder
Definition: smacker.c:763
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1079
SmackVContext
Definition: smacker.c:57
GetBitContext
Definition: get_bits.h:108
AV_CODEC_ID_SMACKAUDIO
@ AV_CODEC_ID_SMACKAUDIO
Definition: codec_id.h:470
SMKTREE_DECODE_BIG_MAX_RECURSION
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition: smacker.c:42
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:62
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:151
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
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:545
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:654
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:551
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:538
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
smka_decode_init
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:579
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
bits
uint8_t bits
Definition: vp3data.h:128
ctx
AVFormatContext * ctx
Definition: movenc.c:49
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:62
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
HuffContext::entries
HuffEntry entries[256]
Definition: smacker.c:75
DBCtx::escapes
int escapes[3]
Definition: smacker.c:84
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:596
NULL
#define NULL
Definition: coverity.c:32
run
uint8_t run
Definition: svq3.c:204
SmkBlockTypes
SmkBlockTypes
Definition: smacker.c:99
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
HuffEntry::length
uint8_t length
Definition: smacker.c:67
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:373
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:652
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:41
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:198
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:505
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:540
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:401
codec_internal.h
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:536
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1071
size
int size
Definition: twinvq_data.h:10344
SMK_BLK_FILL
@ SMK_BLK_FILL
Definition: smacker.c:103
SmackVContext::avctx
AVCodecContext * avctx
Definition: smacker.c:58
DBCtx::v1
VLC * v1
Definition: smacker.c:82
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:408
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:413
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
SmackVContext::mmap_tbl
int * mmap_tbl
Definition: smacker.c:61
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:838
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1586
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:537
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:194
DBCtx::values
int * values
Definition: smacker.c:81
AVCodecContext::height
int height
Definition: avcodec.h:632
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:671
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:84
AV_CODEC_ID_SMACKVIDEO
@ AV_CODEC_ID_SMACKVIDEO
Definition: codec_id.h:135
ff_vlc_free
void ff_vlc_free(VLC *vlc)
Definition: vlc.c:580
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:1815
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:82
last_reset
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:346
DBCtx::vals
uint8_t vals[2]
Definition: smacker.c:83
SmackVContext::type_tbl
int * type_tbl
Definition: smacker.c:61
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:451
SMKTREE_BITS
#define SMKTREE_BITS
Definition: smacker.c:38
SmackVContext::mclr_tbl
int * mclr_tbl
Definition: smacker.c:61
channel_layout.h
SmackVContext::mmap_last
int mmap_last[3]
Definition: smacker.c:62
mode
mode
Definition: ebur128.h:83
VLC
Definition: vlc.h:36
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:441
SmackVContext::full_tbl
int * full_tbl
Definition: smacker.c:61
HuffContext::current
int current
Definition: smacker.c:74
SmackVContext::pic
AVFrame * pic
Definition: smacker.c:59
DBCtx::last
int * last
Definition: smacker.c:85
HuffEntry
Definition: exr.c:95
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:79
SMK_BLK_MONO
@ SMK_BLK_MONO
Definition: smacker.c:100
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:80
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
SMK_NODE
#define SMK_NODE
Definition: smacker.c:39
VLC_INIT_OUTPUT_LE
#define VLC_INIT_OUTPUT_LE
Definition: vlc.h:188
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:476
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
decode_header_trees
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:274
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:632
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:482
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:448
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:2070
av_bswap16
#define av_bswap16
Definition: bswap.h:28
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:375
smk_get_code
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition: smacker.c:352
smacker_decode_tree
static int smacker_decode_tree(AVCodecContext *avctx, GetBitContext *gb, HuffContext *hc, int length)
Decode local frame tree.
Definition: smacker.c:111