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 <stdio.h>
32 #include <stdlib.h>
33 
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 "get_bits.h"
54 #include "internal.h"
55 #include "mathops.h"
56 
57 typedef struct SmackVContext {
60 
62  int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
64 
65 /**
66  * Context used for code reconstructing
67  */
68 typedef struct HuffContext {
69  int current;
70  uint32_t bits[256];
71  uint8_t lengths[256];
73 } HuffContext;
74 
75 /* common parameters used for decode_bigtree */
76 typedef struct DBCtx {
77  int current, length;
78  int *values;
79  VLC *v1, *v2;
80  uint8_t vals[2];
81  int escapes[3];
82  int *last;
83 } DBCtx;
84 
85 /* possible runs of blocks */
86 static const int block_runs[64] = {
87  1, 2, 3, 4, 5, 6, 7, 8,
88  9, 10, 11, 12, 13, 14, 15, 16,
89  17, 18, 19, 20, 21, 22, 23, 24,
90  25, 26, 27, 28, 29, 30, 31, 32,
91  33, 34, 35, 36, 37, 38, 39, 40,
92  41, 42, 43, 44, 45, 46, 47, 48,
93  49, 50, 51, 52, 53, 54, 55, 56,
94  57, 58, 59, 128, 256, 512, 1024, 2048 };
95 
101 
102 /**
103  * Decode local frame tree
104  *
105  * Can read SMKTREE_DECODE_MAX_RECURSION before the first check;
106  * does not overread gb on success.
107  */
108 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
109 {
110  if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
111  av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
112  return AVERROR_INVALIDDATA;
113  }
114 
115  if(!get_bits1(gb)){ //Leaf
116  if (hc->current >= 256) {
117  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
118  return AVERROR_INVALIDDATA;
119  }
120  if (get_bits_left(gb) < 8)
121  return AVERROR_INVALIDDATA;
122  hc->bits[hc->current] = prefix;
123  hc->lengths[hc->current] = length;
124  hc->values[hc->current] = get_bits(gb, 8);
125  hc->current++;
126  return 0;
127  } else { //Node
128  int r;
129  length++;
130  r = smacker_decode_tree(gb, hc, prefix, length);
131  if(r)
132  return r;
133  return smacker_decode_tree(gb, hc, prefix | (1U << (length - 1)), 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  */
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(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(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(gb, &h, 0, 0);
220  if (err < 0)
221  goto error;
222  skip_bits1(gb);
223  if (h.current > 1) {
224  err = ff_init_vlc_sparse(&vlc[i], SMKTREE_BITS, h.current,
225  h.lengths, sizeof(*h.lengths), sizeof(*h.lengths),
226  h.bits, sizeof(*h.bits), sizeof(*h.bits),
227  h.values, sizeof(*h.values), sizeof(*h.values),
228  INIT_VLC_LE);
229  if (err < 0) {
230  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
231  goto error;
232  }
233  } else
234  ctx.vals[i] = h.values[0];
235  }
236 
237  escapes[0] = get_bits(gb, 16);
238  escapes[1] = get_bits(gb, 16);
239  escapes[2] = get_bits(gb, 16);
240 
241  last[0] = last[1] = last[2] = -1;
242 
243  ctx.escapes[0] = escapes[0];
244  ctx.escapes[1] = escapes[1];
245  ctx.escapes[2] = escapes[2];
246  ctx.v1 = &vlc[0];
247  ctx.v2 = &vlc[1];
248  ctx.last = last;
249  ctx.length = (size + 3) >> 2;
250  ctx.current = 0;
251  ctx.values = av_malloc_array(ctx.length + 3, sizeof(ctx.values[0]));
252  if (!ctx.values) {
253  err = AVERROR(ENOMEM);
254  goto error;
255  }
256  *recodes = ctx.values;
257 
258  err = smacker_decode_bigtree(gb, &ctx, 0);
259  if (err < 0)
260  goto error;
261  skip_bits1(gb);
262  if (ctx.last[0] == -1) ctx.last[0] = ctx.current++;
263  if (ctx.last[1] == -1) ctx.last[1] = ctx.current++;
264  if (ctx.last[2] == -1) ctx.last[2] = ctx.current++;
265 
266  err = 0;
267 error:
268  for (int i = 0; i < 2; i++) {
269  ff_free_vlc(&vlc[i]);
270  }
271 
272  return err;
273 }
274 
276  GetBitContext gb;
277  int mmap_size, mclr_size, full_size, type_size, ret;
278  int skip = 0;
279 
280  mmap_size = AV_RL32(smk->avctx->extradata);
281  mclr_size = AV_RL32(smk->avctx->extradata + 4);
282  full_size = AV_RL32(smk->avctx->extradata + 8);
283  type_size = AV_RL32(smk->avctx->extradata + 12);
284 
285  ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
286  if (ret < 0)
287  return ret;
288 
289  if(!get_bits1(&gb)) {
290  skip ++;
291  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
292  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
293  if (!smk->mmap_tbl)
294  return AVERROR(ENOMEM);
295  smk->mmap_tbl[0] = 0;
296  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
297  } else {
298  ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
299  if (ret < 0)
300  return ret;
301  }
302  if(!get_bits1(&gb)) {
303  skip ++;
304  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
305  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
306  if (!smk->mclr_tbl)
307  return AVERROR(ENOMEM);
308  smk->mclr_tbl[0] = 0;
309  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
310  } else {
311  ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
312  if (ret < 0)
313  return ret;
314  }
315  if(!get_bits1(&gb)) {
316  skip ++;
317  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
318  smk->full_tbl = av_malloc(sizeof(int) * 2);
319  if (!smk->full_tbl)
320  return AVERROR(ENOMEM);
321  smk->full_tbl[0] = 0;
322  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
323  } else {
324  ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
325  if (ret < 0)
326  return ret;
327  }
328  if(!get_bits1(&gb)) {
329  skip ++;
330  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
331  smk->type_tbl = av_malloc(sizeof(int) * 2);
332  if (!smk->type_tbl)
333  return AVERROR(ENOMEM);
334  smk->type_tbl[0] = 0;
335  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
336  } else {
337  ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
338  if (ret < 0)
339  return ret;
340  }
341  if (skip == 4 || get_bits_left(&gb) < 0)
342  return AVERROR_INVALIDDATA;
343 
344  return 0;
345 }
346 
347 static av_always_inline void last_reset(int *recode, int *last) {
348  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
349 }
350 
351 /* Get code and update history.
352  * Checks before reading, does not overread. */
353 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
354  register int *table = recode;
355  int v;
356 
357  while(*table & SMK_NODE) {
358  if (get_bits_left(gb) < 1)
359  return AVERROR_INVALIDDATA;
360  if(get_bits1(gb))
361  table += (*table) & (~SMK_NODE);
362  table++;
363  }
364  v = *table;
365 
366  if(v != recode[last[0]]) {
367  recode[last[2]] = recode[last[1]];
368  recode[last[1]] = recode[last[0]];
369  recode[last[0]] = v;
370  }
371  return v;
372 }
373 
374 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
375  AVPacket *avpkt)
376 {
377  SmackVContext * const smk = avctx->priv_data;
378  uint8_t *out;
379  uint32_t *pal;
380  GetByteContext gb2;
381  GetBitContext gb;
382  int blocks, blk, bw, bh;
383  int i, ret;
384  int stride;
385  int flags;
386 
387  if (avpkt->size <= 769)
388  return AVERROR_INVALIDDATA;
389 
390  if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
391  return ret;
392 
393  /* make the palette available on the way out */
394  pal = (uint32_t*)smk->pic->data[1];
395  bytestream2_init(&gb2, avpkt->data, avpkt->size);
396  flags = bytestream2_get_byteu(&gb2);
397  smk->pic->palette_has_changed = flags & 1;
398  smk->pic->key_frame = !!(flags & 2);
399  if (smk->pic->key_frame)
401  else
403 
404  for(i = 0; i < 256; i++)
405  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
406 
407  last_reset(smk->mmap_tbl, smk->mmap_last);
408  last_reset(smk->mclr_tbl, smk->mclr_last);
409  last_reset(smk->full_tbl, smk->full_last);
410  last_reset(smk->type_tbl, smk->type_last);
411  if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
412  return ret;
413 
414  blk = 0;
415  bw = avctx->width >> 2;
416  bh = avctx->height >> 2;
417  blocks = bw * bh;
418  stride = smk->pic->linesize[0];
419  while(blk < blocks) {
420  int type, run, mode;
421  uint16_t pix;
422 
423  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
424  if (type < 0)
425  return type;
426  run = block_runs[(type >> 2) & 0x3F];
427  switch(type & 3){
428  case SMK_BLK_MONO:
429  while(run-- && blk < blocks){
430  int clr, map;
431  int hi, lo;
432  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
433  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
434  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
435  hi = clr >> 8;
436  lo = clr & 0xFF;
437  for(i = 0; i < 4; i++) {
438  if(map & 1) out[0] = hi; else out[0] = lo;
439  if(map & 2) out[1] = hi; else out[1] = lo;
440  if(map & 4) out[2] = hi; else out[2] = lo;
441  if(map & 8) out[3] = hi; else out[3] = lo;
442  map >>= 4;
443  out += stride;
444  }
445  blk++;
446  }
447  break;
448  case SMK_BLK_FULL:
449  mode = 0;
450  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
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(data, 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 
563  ret = decode_header_trees(c);
564  if (ret < 0) {
565  return ret;
566  }
567 
568  return 0;
569 }
570 
571 
573 {
574  if (avctx->channels < 1 || avctx->channels > 2) {
575  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
576  return AVERROR_INVALIDDATA;
577  }
580 
581  return 0;
582 }
583 
584 /**
585  * Decode Smacker audio data
586  */
588  int *got_frame_ptr, AVPacket *avpkt)
589 {
590  AVFrame *frame = data;
591  const uint8_t *buf = avpkt->data;
592  int buf_size = avpkt->size;
593  GetBitContext gb;
594  VLC vlc[4] = { { 0 } };
595  int16_t *samples;
596  uint8_t *samples8;
597  uint8_t values[4];
598  int i, res, ret;
599  int unp_size;
600  int bits, stereo;
601  unsigned pred[2], val;
602 
603  if (buf_size <= 4) {
604  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
605  return AVERROR_INVALIDDATA;
606  }
607 
608  unp_size = AV_RL32(buf);
609 
610  if (unp_size > (1U<<24)) {
611  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
612  return AVERROR_INVALIDDATA;
613  }
614 
615  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
616  return ret;
617 
618  if(!get_bits1(&gb)){
619  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
620  *got_frame_ptr = 0;
621  return 1;
622  }
623  stereo = get_bits1(&gb);
624  bits = get_bits1(&gb);
625  if (stereo ^ (avctx->channels != 1)) {
626  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
627  return AVERROR_INVALIDDATA;
628  }
629  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
630  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
631  return AVERROR_INVALIDDATA;
632  }
633 
634  /* get output buffer */
635  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
636  if (unp_size % (avctx->channels * (bits + 1))) {
637  av_log(avctx, AV_LOG_ERROR,
638  "The buffer does not contain an integer number of samples\n");
639  return AVERROR_INVALIDDATA;
640  }
641  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
642  return ret;
643  samples = (int16_t *)frame->data[0];
644  samples8 = frame->data[0];
645 
646  // Initialize
647  for(i = 0; i < (1 << (bits + stereo)); i++) {
648  HuffContext h;
649  h.current = 0;
650  skip_bits1(&gb);
651  if ((ret = smacker_decode_tree(&gb, &h, 0, 0)) < 0)
652  goto error;
653  skip_bits1(&gb);
654  if (h.current > 1) {
655  ret = ff_init_vlc_sparse(&vlc[i], SMKTREE_BITS, h.current,
656  h.lengths, sizeof(*h.lengths), sizeof(*h.lengths),
657  h.bits, sizeof(*h.bits), sizeof(*h.bits),
658  h.values, sizeof(*h.values), sizeof(*h.values),
659  INIT_VLC_LE);
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.values[0];
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  for(; i < unp_size / 2; i++) {
674  unsigned idx = 2 * (i & stereo);
675  if (get_bits_left(&gb) < 0) {
676  ret = AVERROR_INVALIDDATA;
677  goto error;
678  }
679  if (vlc[idx].table)
680  res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
681  else
682  res = values[idx];
683  val = res;
684  if (vlc[++idx].table)
685  res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
686  else
687  res = values[idx];
688  val |= res << 8;
689  pred[idx / 2] += val;
690  *samples++ = pred[idx / 2];
691  }
692  } else { //8-bit data
693  for(i = stereo; i >= 0; i--)
694  pred[i] = get_bits(&gb, 8);
695  for(i = 0; i <= stereo; i++)
696  *samples8++ = pred[i];
697  for(; i < unp_size; i++) {
698  unsigned idx = i & stereo;
699  if (get_bits_left(&gb) < 0) {
700  ret = AVERROR_INVALIDDATA;
701  goto error;
702  }
703  if (vlc[idx].table)
704  val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
705  else
706  val = values[idx];
707  pred[idx] += val;
708  *samples8++ = pred[idx];
709  }
710  }
711 
712  *got_frame_ptr = 1;
713  ret = buf_size;
714 
715 error:
716  for(i = 0; i < 4; i++) {
717  ff_free_vlc(&vlc[i]);
718  }
719 
720  return ret;
721 }
722 
724  .name = "smackvid",
725  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
726  .type = AVMEDIA_TYPE_VIDEO,
728  .priv_data_size = sizeof(SmackVContext),
729  .init = decode_init,
730  .close = decode_end,
731  .decode = decode_frame,
732  .capabilities = AV_CODEC_CAP_DR1,
733  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
734 };
735 
737  .name = "smackaud",
738  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
739  .type = AVMEDIA_TYPE_AUDIO,
741  .init = smka_decode_init,
742  .decode = smka_decode_frame,
743  .capabilities = AV_CODEC_CAP_DR1,
744 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:374
int type_last[3]
Definition: smacker.c:62
#define NULL
Definition: coverity.c:32
int current
Definition: smacker.c:77
#define SMK_NODE
Definition: smacker.c:39
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static const int block_runs[64]
Definition: smacker.c:86
uint8_t values[256]
Definition: smacker.c:72
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int current
Definition: smacker.c:69
int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:270
int size
Definition: packet.h:364
#define av_bswap16
Definition: bswap.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
GLint GLenum type
Definition: opengl_enc.c:104
uint8_t run
Definition: svq3.c:204
#define AV_CH_LAYOUT_STEREO
static void error(const char *err)
#define blk(i)
Definition: sha.c:185
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:544
AVCodec.
Definition: codec.h:190
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
int escapes[3]
Definition: smacker.c:81
VLC * v2
Definition: smacker.c:79
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
AV_SAMPLE_FMT_U8
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
VLC * v1
Definition: smacker.c:79
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
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
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:456
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:572
Definition: smacker.c:76
uint8_t * data
Definition: packet.h:363
uint8_t vals[2]
Definition: smacker.c:80
bitstream reader API header.
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition: smacker.c:42
ptrdiff_t size
Definition: opengl_enc.c:100
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1750
#define av_log(a,...)
static const uint16_t table[]
Definition: prosumer.c:206
int * type_tbl
Definition: smacker.c:61
int length
Definition: smacker.c:77
int full_last[3]
Definition: smacker.c:62
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVCodec ff_smacker_decoder
Definition: smacker.c:723
#define SMKTREE_DECODE_MAX_RECURSION
Definition: smacker.c:41
SmkBlockTypes
Definition: smacker.c:96
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
Decode local frame tree.
Definition: smacker.c:108
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
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:1978
const char * r
Definition: vf_curves.c:114
GLsizei GLsizei * length
Definition: opengl_enc.c:114
const char * name
Name of the codec implementation.
Definition: codec.h:197
uint8_t bits
Definition: vp3data.h:202
AVFrame * pic
Definition: smacker.c:59
int * mmap_tbl
Definition: smacker.c:61
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:587
int * values
Definition: smacker.c:78
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:391
int mmap_last[3]
Definition: smacker.c:62
int width
picture width / height.
Definition: avcodec.h:699
Context used for code reconstructing.
Definition: smacker.c:68
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
AVFormatContext * ctx
Definition: movenc.c:48
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
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:529
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
int * mclr_tbl
Definition: smacker.c:61
#define INIT_VLC_LE
Definition: vlc.h:54
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
Store large tree as FFmpeg&#39;s vlc codes.
Definition: smacker.c:198
static const float pred[4]
Definition: siprdata.h:259
int * full_tbl
Definition: smacker.c:61
static int smacker_decode_bigtree(GetBitContext *gb, DBCtx *ctx, int length)
Decode header tree.
Definition: smacker.c:142
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
Libavcodec external API header.
AVCodec ff_smackaud_decoder
Definition: smacker.c:736
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:339
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
main external API structure.
Definition: avcodec.h:526
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:551
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1872
int extradata_size
Definition: avcodec.h:628
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
uint8_t lengths[256]
Definition: smacker.c:71
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:465
const VDPAUPixFmtMap * map
int * last
Definition: smacker.c:82
#define flags(name, subs,...)
Definition: cbs_av1.c:560
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
AVCodecContext * avctx
Definition: smacker.c:58
uint32_t bits[256]
Definition: smacker.c:70
common internal api header.
signed 16 bits
Definition: samplefmt.h:61
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition: smacker.c:353
#define SMKTREE_BITS
Definition: smacker.c:38
void * priv_data
Definition: avcodec.h:553
int channels
number of audio channels
Definition: avcodec.h:1187
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:386
int mclr_last[3]
Definition: smacker.c:62
FILE * out
Definition: movenc.c:54
Filter the word “frame” indicates either a video frame or a group of audio samples
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:45
#define av_malloc_array(a, b)
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:275
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:347
#define stride
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
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:406
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:340
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:358
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:374
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
int i
Definition: input.c:407
Predicted.
Definition: avutil.h:275