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(AVCodecContext *avctx, GetBitContext *gb, HuffContext *hc, int length)
111 {
112  if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
113  av_log(avctx, 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(avctx, 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(avctx, gb, hc, length);
130  if(r)
131  return r;
132  return smacker_decode_tree(avctx, 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(AVCodecContext *avctx, 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(avctx, 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(avctx, 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(smk->avctx, gb, &h, 0);
219  if (err < 0)
220  goto error;
221  skip_bits1(gb);
222  if (h.current > 1) {
223  err = ff_vlc_init_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, VLC_INIT_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(smk->avctx, 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_vlc_free(&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 #if FF_API_PALETTE_HAS_CHANGED
397  smk->pic->palette_has_changed = flags & 1;
399 #endif
400  if (flags & 2) {
401  smk->pic->flags |= AV_FRAME_FLAG_KEY;
403  } else {
404  smk->pic->flags &= ~AV_FRAME_FLAG_KEY;
406  }
407 
408  for(i = 0; i < 256; i++)
409  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
410 
411  last_reset(smk->mmap_tbl, smk->mmap_last);
412  last_reset(smk->mclr_tbl, smk->mclr_last);
413  last_reset(smk->full_tbl, smk->full_last);
414  last_reset(smk->type_tbl, smk->type_last);
415  if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
416  return ret;
417 
418  blk = 0;
419  bw = avctx->width >> 2;
420  bh = avctx->height >> 2;
421  blocks = bw * bh;
422  stride = smk->pic->linesize[0];
423  while(blk < blocks) {
424  int type, run, mode;
425  uint16_t pix;
426 
427  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
428  if (type < 0)
429  return type;
430  run = block_runs[(type >> 2) & 0x3F];
431  switch(type & 3){
432  case SMK_BLK_MONO:
433  while(run-- && blk < blocks){
434  int clr, map;
435  int hi, lo;
436  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
437  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
438  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
439  hi = clr >> 8;
440  lo = clr & 0xFF;
441  for(i = 0; i < 4; i++) {
442  if(map & 1) out[0] = hi; else out[0] = lo;
443  if(map & 2) out[1] = hi; else out[1] = lo;
444  if(map & 4) out[2] = hi; else out[2] = lo;
445  if(map & 8) out[3] = hi; else out[3] = lo;
446  map >>= 4;
447  out += stride;
448  }
449  blk++;
450  }
451  break;
452  case SMK_BLK_FULL:
453  mode = 0;
454  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
455  if (get_bits_left(&gb) < 1)
456  return AVERROR_INVALIDDATA;
457  if(get_bits1(&gb)) mode = 1;
458  else if(get_bits1(&gb)) mode = 2;
459  }
460  while(run-- && blk < blocks){
461  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
462  switch(mode){
463  case 0:
464  for(i = 0; i < 4; i++) {
465  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
466  AV_WL16(out+2,pix);
467  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
468  AV_WL16(out,pix);
469  out += stride;
470  }
471  break;
472  case 1:
473  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
474  out[0] = out[1] = pix & 0xFF;
475  out[2] = out[3] = pix >> 8;
476  out += stride;
477  out[0] = out[1] = pix & 0xFF;
478  out[2] = out[3] = pix >> 8;
479  out += stride;
480  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
481  out[0] = out[1] = pix & 0xFF;
482  out[2] = out[3] = pix >> 8;
483  out += stride;
484  out[0] = out[1] = pix & 0xFF;
485  out[2] = out[3] = pix >> 8;
486  break;
487  case 2:
488  for(i = 0; i < 2; i++) {
489  uint16_t pix1, pix2;
490  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
491  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
492  AV_WL16(out,pix1);
493  AV_WL16(out+2,pix2);
494  out += stride;
495  AV_WL16(out,pix1);
496  AV_WL16(out+2,pix2);
497  out += stride;
498  }
499  break;
500  }
501  blk++;
502  }
503  break;
504  case SMK_BLK_SKIP:
505  while(run-- && blk < blocks)
506  blk++;
507  break;
508  case SMK_BLK_FILL:
509  mode = type >> 8;
510  while(run-- && blk < blocks){
511  uint32_t col;
512  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
513  col = mode * 0x01010101U;
514  for(i = 0; i < 4; i++) {
515  *((uint32_t*)out) = col;
516  out += stride;
517  }
518  blk++;
519  }
520  break;
521  }
522 
523  }
524 
525  if ((ret = av_frame_ref(rframe, smk->pic)) < 0)
526  return ret;
527 
528  *got_frame = 1;
529 
530  /* always report that the buffer was completely consumed */
531  return avpkt->size;
532 }
533 
534 
536 {
537  SmackVContext * const smk = avctx->priv_data;
538 
539  av_freep(&smk->mmap_tbl);
540  av_freep(&smk->mclr_tbl);
541  av_freep(&smk->full_tbl);
542  av_freep(&smk->type_tbl);
543 
544  av_frame_free(&smk->pic);
545 
546  return 0;
547 }
548 
549 
551 {
552  SmackVContext * const c = avctx->priv_data;
553  int ret;
554 
555  c->avctx = avctx;
556 
557  avctx->pix_fmt = AV_PIX_FMT_PAL8;
558 
559  c->pic = av_frame_alloc();
560  if (!c->pic)
561  return AVERROR(ENOMEM);
562 
563  /* decode huffman trees from extradata */
564  if (avctx->extradata_size <= 16){
565  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
566  return AVERROR(EINVAL);
567  }
568 
570  if (ret < 0) {
571  return ret;
572  }
573 
574  return 0;
575 }
576 
577 
579 {
580  int channels = avctx->ch_layout.nb_channels;
582  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
583  return AVERROR_INVALIDDATA;
584  }
588 
589  return 0;
590 }
591 
592 /**
593  * Decode Smacker audio data
594  */
596  int *got_frame_ptr, AVPacket *avpkt)
597 {
598  const uint8_t *buf = avpkt->data;
599  int buf_size = avpkt->size;
600  GetBitContext gb;
601  VLC vlc[4] = { { 0 } };
602  int16_t *samples;
603  uint8_t *samples8;
604  uint8_t values[4];
605  int i, res, ret;
606  int unp_size;
607  int bits, stereo;
608  unsigned pred[2], val, val2;
609 
610  if (buf_size <= 4) {
611  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
612  return AVERROR_INVALIDDATA;
613  }
614 
615  unp_size = AV_RL32(buf);
616 
617  if (unp_size > (1U<<24)) {
618  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
619  return AVERROR_INVALIDDATA;
620  }
621 
622  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
623  return ret;
624 
625  if(!get_bits1(&gb)){
626  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
627  *got_frame_ptr = 0;
628  return 1;
629  }
630  stereo = get_bits1(&gb);
631  bits = get_bits1(&gb);
632  if (stereo ^ (avctx->ch_layout.nb_channels != 1)) {
633  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
634  return AVERROR_INVALIDDATA;
635  }
636  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
637  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
638  return AVERROR_INVALIDDATA;
639  }
640 
641  /* get output buffer */
642  frame->nb_samples = unp_size / (avctx->ch_layout.nb_channels * (bits + 1));
643  if (unp_size % (avctx->ch_layout.nb_channels * (bits + 1))) {
644  av_log(avctx, AV_LOG_ERROR,
645  "The buffer does not contain an integer number of samples\n");
646  return AVERROR_INVALIDDATA;
647  }
648  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
649  return ret;
650  samples = (int16_t *)frame->data[0];
651  samples8 = frame->data[0];
652 
653  // Initialize
654  for(i = 0; i < (1 << (bits + stereo)); i++) {
655  HuffContext h;
656  h.current = 0;
657  skip_bits1(&gb);
658  if ((ret = smacker_decode_tree(avctx, &gb, &h, 0)) < 0)
659  goto error;
660  skip_bits1(&gb);
661  if (h.current > 1) {
662  ret = ff_vlc_init_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
663  &h.entries[0].length, sizeof(*h.entries),
664  &h.entries[0].value, sizeof(*h.entries), 1,
665  0, VLC_INIT_OUTPUT_LE, avctx);
666  if (ret < 0) {
667  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
668  goto error;
669  }
670  } else
671  values[i] = h.entries[0].value;
672  }
673  /* this codec relies on wraparound instead of clipping audio */
674  if(bits) { //decode 16-bit data
675  for(i = stereo; i >= 0; i--)
676  pred[i] = av_bswap16(get_bits(&gb, 16));
677  for(i = 0; i <= stereo; i++)
678  *samples++ = pred[i];
679  unp_size /= 2;
680 
681  if (vlc[0 ].table || vlc[ 1].table ||
682  vlc[2*stereo].table || vlc[2*stereo+1].table) {
683  for(; i < unp_size ; i++) {
684  unsigned idx = 2 * (i & stereo);
685  if (get_bits_left(&gb) < 0) {
687  goto error;
688  }
689  if (vlc[idx].table)
690  res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
691  else
692  res = values[idx];
693  val = res;
694  if (vlc[++idx].table)
695  res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
696  else
697  res = values[idx];
698  val |= res << 8;
699  pred[idx / 2] += val;
700  *samples++ = pred[idx / 2];
701  }
702  } else if (stereo) {
703  val = 256*values[1] + values[0];
704  val2 = 256*values[3] + values[2];
705  for(; i < unp_size; i+=2) {
706  pred[0] += val;
707  pred[1] += val2;
708  *samples++ = pred[0];
709  *samples++ = pred[1];
710  }
711  } else {
712  val = 256*values[1] + values[0];
713  for(; i < unp_size; i++) {
714  pred[0] += val;
715  *samples++ = pred[0];
716  }
717  }
718  } else { //8-bit data
719  for(i = stereo; i >= 0; i--)
720  pred[i] = get_bits(&gb, 8);
721  for(i = 0; i <= stereo; i++)
722  *samples8++ = pred[i];
723  for(; i < unp_size; i++) {
724  unsigned idx = i & stereo;
725  if (get_bits_left(&gb) < 0) {
727  goto error;
728  }
729  if (vlc[idx].table)
730  val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
731  else
732  val = values[idx];
733  pred[idx] += val;
734  *samples8++ = pred[idx];
735  }
736  }
737 
738  *got_frame_ptr = 1;
739  ret = buf_size;
740 
741 error:
742  for(i = 0; i < 4; i++) {
743  ff_vlc_free(&vlc[i]);
744  }
745 
746  return ret;
747 }
748 
750  .p.name = "smackvid",
751  CODEC_LONG_NAME("Smacker video"),
752  .p.type = AVMEDIA_TYPE_VIDEO,
753  .p.id = AV_CODEC_ID_SMACKVIDEO,
754  .priv_data_size = sizeof(SmackVContext),
755  .init = decode_init,
756  .close = decode_end,
758  .p.capabilities = AV_CODEC_CAP_DR1,
759  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
760 };
761 
763  .p.name = "smackaud",
764  CODEC_LONG_NAME("Smacker audio"),
765  .p.type = AVMEDIA_TYPE_AUDIO,
766  .p.id = AV_CODEC_ID_SMACKAUDIO,
767  .init = smka_decode_init,
769  .p.capabilities = AV_CODEC_CAP_DR1,
770 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
ff_smacker_decoder
const FFCodec ff_smacker_decoder
Definition: smacker.c:749
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:42
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
r
const char * r
Definition: vf_curves.c:126
AVFrame::palette_has_changed
attribute_deprecated int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:546
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
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:100
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:340
AVPacket::data
uint8_t * data
Definition: packet.h:522
table
static const uint16_t table[]
Definition: prosumer.c:205
FFCodec
Definition: codec_internal.h:127
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:649
smacker_decode_bigtree
static int smacker_decode_bigtree(AVCodecContext *avctx, GetBitContext *gb, DBCtx *ctx, int length)
Decode header tree.
Definition: smacker.c:141
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:323
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
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:335
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
DBCtx::length
int length
Definition: smacker.c:79
ff_smackaud_decoder
const FFCodec ff_smackaud_decoder
Definition: smacker.c:762
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
SmackVContext
Definition: smacker.c:56
GetBitContext
Definition: get_bits.h:108
AV_CODEC_ID_SMACKAUDIO
@ AV_CODEC_ID_SMACKAUDIO
Definition: codec_id.h:466
SMKTREE_DECODE_BIG_MAX_RECURSION
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition: smacker.c:41
val
static double val(void *priv, double ch)
Definition: aeval.c:78
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:88
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:545
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:628
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:550
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:543
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
smka_decode_init
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:578
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
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:595
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:279
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
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: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:40
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:1618
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:442
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
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:523
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:361
codec_internal.h
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:535
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
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:410
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: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:1033
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1510
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:542
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:80
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
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:577
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:1736
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
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:441
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
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:662
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
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
SMK_NODE
#define SMK_NODE
Definition: smacker.c:38
VLC_INIT_OUTPUT_LE
#define VLC_INIT_OUTPUT_LE
Definition: vlc.h:185
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:466
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
AVPacket
This structure stores compressed data.
Definition: packet.h:499
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:621
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:474
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:385
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
av_bswap16
#define av_bswap16
Definition: bswap.h:27
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:351
smacker_decode_tree
static int smacker_decode_tree(AVCodecContext *avctx, GetBitContext *gb, HuffContext *hc, int length)
Decode local frame tree.
Definition: smacker.c:110