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 #define BITSTREAM_READER_LE
37 #include "avcodec.h"
38 #include "bytestream.h"
39 #include "get_bits.h"
40 #include "internal.h"
41 #include "mathops.h"
42 
43 #define SMKTREE_BITS 9
44 #define SMK_NODE 0x80000000
45 
46 #define SMKTREE_DECODE_MAX_RECURSION 32
47 #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
48 
49 typedef struct SmackVContext {
52 
54  int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
56 
57 /**
58  * Context used for code reconstructing
59  */
60 typedef struct HuffContext {
61  int length;
62  int maxlength;
63  int current;
64  uint32_t *bits;
65  int *lengths;
66  int *values;
67 } HuffContext;
68 
69 /* common parameters used for decode_bigtree */
70 typedef struct DBCtx {
71  VLC *v1, *v2;
72  int *recode1, *recode2;
73  int escapes[3];
74  int *last;
75  int lcur;
76 } DBCtx;
77 
78 /* possible runs of blocks */
79 static const int block_runs[64] = {
80  1, 2, 3, 4, 5, 6, 7, 8,
81  9, 10, 11, 12, 13, 14, 15, 16,
82  17, 18, 19, 20, 21, 22, 23, 24,
83  25, 26, 27, 28, 29, 30, 31, 32,
84  33, 34, 35, 36, 37, 38, 39, 40,
85  41, 42, 43, 44, 45, 46, 47, 48,
86  49, 50, 51, 52, 53, 54, 55, 56,
87  57, 58, 59, 128, 256, 512, 1024, 2048 };
88 
93  SMK_BLK_FILL = 3 };
94 
95 /**
96  * Decode local frame tree
97  */
98 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
99 {
101  av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
102  return AVERROR_INVALIDDATA;
103  }
104 
105  if(!get_bits1(gb)){ //Leaf
106  if(hc->current >= hc->length){
107  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
108  return AVERROR_INVALIDDATA;
109  }
110  if(length){
111  hc->bits[hc->current] = prefix;
112  hc->lengths[hc->current] = length;
113  } else {
114  hc->bits[hc->current] = 0;
115  hc->lengths[hc->current] = 0;
116  }
117  hc->values[hc->current] = get_bits(gb, 8);
118  hc->current++;
119  if(hc->maxlength < length)
120  hc->maxlength = length;
121  return 0;
122  } else { //Node
123  int r;
124  length++;
125  r = smacker_decode_tree(gb, hc, prefix, length);
126  if(r)
127  return r;
128  return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
129  }
130 }
131 
132 /**
133  * Decode header tree
134  */
136  DBCtx *ctx, int length)
137 {
138  // Larger length can cause segmentation faults due to too deep recursion.
140  av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
141  return AVERROR_INVALIDDATA;
142  }
143 
144  if (hc->current + 1 >= hc->length) {
145  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
146  return AVERROR_INVALIDDATA;
147  }
148  if(!get_bits1(gb)){ //Leaf
149  int val, i1, i2;
150  i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
151  i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
152  if (i1 < 0 || i2 < 0)
153  return AVERROR_INVALIDDATA;
154  val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
155  if(val == ctx->escapes[0]) {
156  ctx->last[0] = hc->current;
157  val = 0;
158  } else if(val == ctx->escapes[1]) {
159  ctx->last[1] = hc->current;
160  val = 0;
161  } else if(val == ctx->escapes[2]) {
162  ctx->last[2] = hc->current;
163  val = 0;
164  }
165 
166  hc->values[hc->current++] = val;
167  return 1;
168  } else { //Node
169  int r = 0, r_new, t;
170 
171  t = hc->current++;
172  r = smacker_decode_bigtree(gb, hc, ctx, length + 1);
173  if(r < 0)
174  return r;
175  hc->values[t] = SMK_NODE | r;
176  r++;
177  r_new = smacker_decode_bigtree(gb, hc, ctx, length + 1);
178  if (r_new < 0)
179  return r_new;
180  return r + r_new;
181  }
182 }
183 
184 /**
185  * Store large tree as FFmpeg's vlc codes
186  */
187 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
188 {
189  int res;
190  HuffContext huff;
191  HuffContext tmp1, tmp2;
192  VLC vlc[2] = { { 0 } };
193  int escapes[3];
194  DBCtx ctx;
195  int err = 0;
196 
197  if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
198  av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
199  return AVERROR_INVALIDDATA;
200  }
201 
202  tmp1.length = 256;
203  tmp1.maxlength = 0;
204  tmp1.current = 0;
205  tmp1.bits = av_mallocz(256 * 4);
206  tmp1.lengths = av_mallocz(256 * sizeof(int));
207  tmp1.values = av_mallocz(256 * sizeof(int));
208 
209  tmp2.length = 256;
210  tmp2.maxlength = 0;
211  tmp2.current = 0;
212  tmp2.bits = av_mallocz(256 * 4);
213  tmp2.lengths = av_mallocz(256 * sizeof(int));
214  tmp2.values = av_mallocz(256 * sizeof(int));
215  if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
216  !tmp2.bits || !tmp2.lengths || !tmp2.values) {
217  err = AVERROR(ENOMEM);
218  goto error;
219  }
220 
221  if(get_bits1(gb)) {
222  res = smacker_decode_tree(gb, &tmp1, 0, 0);
223  if (res < 0) {
224  err = res;
225  goto error;
226  }
227  skip_bits1(gb);
228  if(tmp1.current > 1) {
229  res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
230  tmp1.lengths, sizeof(int), sizeof(int),
231  tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
232  if(res < 0) {
233  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
234  err = res;
235  goto error;
236  }
237  }
238  }
239  if (!vlc[0].table) {
240  av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
241  }
242  if(get_bits1(gb)){
243  res = smacker_decode_tree(gb, &tmp2, 0, 0);
244  if (res < 0) {
245  err = res;
246  goto error;
247  }
248  skip_bits1(gb);
249  if(tmp2.current > 1) {
250  res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
251  tmp2.lengths, sizeof(int), sizeof(int),
252  tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
253  if(res < 0) {
254  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
255  err = res;
256  goto error;
257  }
258  }
259  }
260  if (!vlc[1].table) {
261  av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
262  }
263 
264  escapes[0] = get_bits(gb, 16);
265  escapes[1] = get_bits(gb, 16);
266  escapes[2] = get_bits(gb, 16);
267 
268  last[0] = last[1] = last[2] = -1;
269 
270  ctx.escapes[0] = escapes[0];
271  ctx.escapes[1] = escapes[1];
272  ctx.escapes[2] = escapes[2];
273  ctx.v1 = &vlc[0];
274  ctx.v2 = &vlc[1];
275  ctx.recode1 = tmp1.values;
276  ctx.recode2 = tmp2.values;
277  ctx.last = last;
278 
279  huff.length = ((size + 3) >> 2) + 4;
280  huff.maxlength = 0;
281  huff.current = 0;
282  huff.values = av_mallocz_array(huff.length, sizeof(int));
283  if (!huff.values) {
284  err = AVERROR(ENOMEM);
285  goto error;
286  }
287 
288  res = smacker_decode_bigtree(gb, &huff, &ctx, 0);
289  if (res < 0)
290  err = res;
291  skip_bits1(gb);
292  if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
293  if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
294  if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
295  if (ctx.last[0] >= huff.length ||
296  ctx.last[1] >= huff.length ||
297  ctx.last[2] >= huff.length) {
298  av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
299  err = AVERROR_INVALIDDATA;
300  }
301 
302  *recodes = huff.values;
303 
304 error:
305  if(vlc[0].table)
306  ff_free_vlc(&vlc[0]);
307  if(vlc[1].table)
308  ff_free_vlc(&vlc[1]);
309  av_free(tmp1.bits);
310  av_free(tmp1.lengths);
311  av_free(tmp1.values);
312  av_free(tmp2.bits);
313  av_free(tmp2.lengths);
314  av_free(tmp2.values);
315 
316  return err;
317 }
318 
320  GetBitContext gb;
321  int mmap_size, mclr_size, full_size, type_size, ret;
322 
323  mmap_size = AV_RL32(smk->avctx->extradata);
324  mclr_size = AV_RL32(smk->avctx->extradata + 4);
325  full_size = AV_RL32(smk->avctx->extradata + 8);
326  type_size = AV_RL32(smk->avctx->extradata + 12);
327 
328  ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
329  if (ret < 0)
330  return ret;
331 
332  if(!get_bits1(&gb)) {
333  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
334  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
335  if (!smk->mmap_tbl)
336  return AVERROR(ENOMEM);
337  smk->mmap_tbl[0] = 0;
338  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
339  } else {
340  ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
341  if (ret < 0)
342  return ret;
343  }
344  if(!get_bits1(&gb)) {
345  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
346  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
347  if (!smk->mclr_tbl)
348  return AVERROR(ENOMEM);
349  smk->mclr_tbl[0] = 0;
350  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
351  } else {
352  ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
353  if (ret < 0)
354  return ret;
355  }
356  if(!get_bits1(&gb)) {
357  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
358  smk->full_tbl = av_malloc(sizeof(int) * 2);
359  if (!smk->full_tbl)
360  return AVERROR(ENOMEM);
361  smk->full_tbl[0] = 0;
362  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
363  } else {
364  ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
365  if (ret < 0)
366  return ret;
367  }
368  if(!get_bits1(&gb)) {
369  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
370  smk->type_tbl = av_malloc(sizeof(int) * 2);
371  if (!smk->type_tbl)
372  return AVERROR(ENOMEM);
373  smk->type_tbl[0] = 0;
374  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
375  } else {
376  ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
377  if (ret < 0)
378  return ret;
379  }
380 
381  return 0;
382 }
383 
384 static av_always_inline void last_reset(int *recode, int *last) {
385  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
386 }
387 
388 /* get code and update history */
389 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
390  register int *table = recode;
391  int v;
392 
393  while(*table & SMK_NODE) {
394  if (get_bits_left(gb) < 1)
395  return AVERROR_INVALIDDATA;
396  if(get_bits1(gb))
397  table += (*table) & (~SMK_NODE);
398  table++;
399  }
400  v = *table;
401 
402  if(v != recode[last[0]]) {
403  recode[last[2]] = recode[last[1]];
404  recode[last[1]] = recode[last[0]];
405  recode[last[0]] = v;
406  }
407  return v;
408 }
409 
410 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
411  AVPacket *avpkt)
412 {
413  SmackVContext * const smk = avctx->priv_data;
414  uint8_t *out;
415  uint32_t *pal;
416  GetByteContext gb2;
417  GetBitContext gb;
418  int blocks, blk, bw, bh;
419  int i, ret;
420  int stride;
421  int flags;
422 
423  if (avpkt->size <= 769)
424  return AVERROR_INVALIDDATA;
425 
426  if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0)
427  return ret;
428 
429  /* make the palette available on the way out */
430  pal = (uint32_t*)smk->pic->data[1];
431  bytestream2_init(&gb2, avpkt->data, avpkt->size);
432  flags = bytestream2_get_byteu(&gb2);
433  smk->pic->palette_has_changed = flags & 1;
434  smk->pic->key_frame = !!(flags & 2);
435  if (smk->pic->key_frame)
437  else
439 
440  for(i = 0; i < 256; i++)
441  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
442 
443  last_reset(smk->mmap_tbl, smk->mmap_last);
444  last_reset(smk->mclr_tbl, smk->mclr_last);
445  last_reset(smk->full_tbl, smk->full_last);
446  last_reset(smk->type_tbl, smk->type_last);
447  if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
448  return ret;
449 
450  blk = 0;
451  bw = avctx->width >> 2;
452  bh = avctx->height >> 2;
453  blocks = bw * bh;
454  stride = smk->pic->linesize[0];
455  while(blk < blocks) {
456  int type, run, mode;
457  uint16_t pix;
458 
459  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
460  if (type < 0)
461  return type;
462  run = block_runs[(type >> 2) & 0x3F];
463  switch(type & 3){
464  case SMK_BLK_MONO:
465  while(run-- && blk < blocks){
466  int clr, map;
467  int hi, lo;
468  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
469  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
470  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
471  hi = clr >> 8;
472  lo = clr & 0xFF;
473  for(i = 0; i < 4; i++) {
474  if(map & 1) out[0] = hi; else out[0] = lo;
475  if(map & 2) out[1] = hi; else out[1] = lo;
476  if(map & 4) out[2] = hi; else out[2] = lo;
477  if(map & 8) out[3] = hi; else out[3] = lo;
478  map >>= 4;
479  out += stride;
480  }
481  blk++;
482  }
483  break;
484  case SMK_BLK_FULL:
485  mode = 0;
486  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
487  if (get_bits_left(&gb) < 1)
488  return AVERROR_INVALIDDATA;
489  if(get_bits1(&gb)) mode = 1;
490  else if(get_bits1(&gb)) mode = 2;
491  }
492  while(run-- && blk < blocks){
493  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
494  switch(mode){
495  case 0:
496  for(i = 0; i < 4; i++) {
497  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
498  AV_WL16(out+2,pix);
499  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
500  AV_WL16(out,pix);
501  out += stride;
502  }
503  break;
504  case 1:
505  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
506  out[0] = out[1] = pix & 0xFF;
507  out[2] = out[3] = pix >> 8;
508  out += stride;
509  out[0] = out[1] = pix & 0xFF;
510  out[2] = out[3] = pix >> 8;
511  out += stride;
512  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
513  out[0] = out[1] = pix & 0xFF;
514  out[2] = out[3] = pix >> 8;
515  out += stride;
516  out[0] = out[1] = pix & 0xFF;
517  out[2] = out[3] = pix >> 8;
518  break;
519  case 2:
520  for(i = 0; i < 2; i++) {
521  uint16_t pix1, pix2;
522  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
523  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
524  AV_WL16(out,pix1);
525  AV_WL16(out+2,pix2);
526  out += stride;
527  AV_WL16(out,pix1);
528  AV_WL16(out+2,pix2);
529  out += stride;
530  }
531  break;
532  }
533  blk++;
534  }
535  break;
536  case SMK_BLK_SKIP:
537  while(run-- && blk < blocks)
538  blk++;
539  break;
540  case SMK_BLK_FILL:
541  mode = type >> 8;
542  while(run-- && blk < blocks){
543  uint32_t col;
544  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
545  col = mode * 0x01010101U;
546  for(i = 0; i < 4; i++) {
547  *((uint32_t*)out) = col;
548  out += stride;
549  }
550  blk++;
551  }
552  break;
553  }
554 
555  }
556 
557  if ((ret = av_frame_ref(data, smk->pic)) < 0)
558  return ret;
559 
560  *got_frame = 1;
561 
562  /* always report that the buffer was completely consumed */
563  return avpkt->size;
564 }
565 
566 
568 {
569  SmackVContext * const smk = avctx->priv_data;
570 
571  av_freep(&smk->mmap_tbl);
572  av_freep(&smk->mclr_tbl);
573  av_freep(&smk->full_tbl);
574  av_freep(&smk->type_tbl);
575 
576  av_frame_free(&smk->pic);
577 
578  return 0;
579 }
580 
581 
583 {
584  SmackVContext * const c = avctx->priv_data;
585  int ret;
586 
587  c->avctx = avctx;
588 
589  avctx->pix_fmt = AV_PIX_FMT_PAL8;
590 
591  c->pic = av_frame_alloc();
592  if (!c->pic)
593  return AVERROR(ENOMEM);
594 
595  /* decode huffman trees from extradata */
596  if(avctx->extradata_size < 16){
597  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
598  decode_end(avctx);
599  return AVERROR(EINVAL);
600  }
601 
603  if (ret < 0) {
604  decode_end(avctx);
605  return ret;
606  }
607 
608  return 0;
609 }
610 
611 
613 {
614  if (avctx->channels < 1 || avctx->channels > 2) {
615  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
616  return AVERROR_INVALIDDATA;
617  }
620 
621  return 0;
622 }
623 
624 /**
625  * Decode Smacker audio data
626  */
627 static int smka_decode_frame(AVCodecContext *avctx, void *data,
628  int *got_frame_ptr, AVPacket *avpkt)
629 {
630  AVFrame *frame = data;
631  const uint8_t *buf = avpkt->data;
632  int buf_size = avpkt->size;
633  GetBitContext gb;
634  HuffContext h[4] = { { 0 } };
635  VLC vlc[4] = { { 0 } };
636  int16_t *samples;
637  uint8_t *samples8;
638  int val;
639  int i, res, ret;
640  int unp_size;
641  int bits, stereo;
642  int pred[2] = {0, 0};
643 
644  if (buf_size <= 4) {
645  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
646  return AVERROR_INVALIDDATA;
647  }
648 
649  unp_size = AV_RL32(buf);
650 
651  if (unp_size > (1U<<24)) {
652  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
653  return AVERROR_INVALIDDATA;
654  }
655 
656  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
657  return ret;
658 
659  if(!get_bits1(&gb)){
660  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
661  *got_frame_ptr = 0;
662  return 1;
663  }
664  stereo = get_bits1(&gb);
665  bits = get_bits1(&gb);
666  if (stereo ^ (avctx->channels != 1)) {
667  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
668  return AVERROR_INVALIDDATA;
669  }
670  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
671  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
672  return AVERROR_INVALIDDATA;
673  }
674 
675  /* get output buffer */
676  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
677  if (unp_size % (avctx->channels * (bits + 1))) {
678  av_log(avctx, AV_LOG_ERROR,
679  "The buffer does not contain an integer number of samples\n");
680  return AVERROR_INVALIDDATA;
681  }
682  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
683  return ret;
684  samples = (int16_t *)frame->data[0];
685  samples8 = frame->data[0];
686 
687  // Initialize
688  for(i = 0; i < (1 << (bits + stereo)); i++) {
689  h[i].length = 256;
690  h[i].maxlength = 0;
691  h[i].current = 0;
692  h[i].bits = av_mallocz(256 * 4);
693  h[i].lengths = av_mallocz(256 * sizeof(int));
694  h[i].values = av_mallocz(256 * sizeof(int));
695  if (!h[i].bits || !h[i].lengths || !h[i].values) {
696  ret = AVERROR(ENOMEM);
697  goto error;
698  }
699  skip_bits1(&gb);
700  if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
702  goto error;
703  }
704  skip_bits1(&gb);
705  if(h[i].current > 1) {
706  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
707  h[i].lengths, sizeof(int), sizeof(int),
708  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
709  if(res < 0) {
710  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
712  goto error;
713  }
714  }
715  }
716  /* this codec relies on wraparound instead of clipping audio */
717  if(bits) { //decode 16-bit data
718  for(i = stereo; i >= 0; i--)
719  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
720  for(i = 0; i <= stereo; i++)
721  *samples++ = pred[i];
722  for(; i < unp_size / 2; i++) {
723  if(get_bits_left(&gb)<0)
724  return AVERROR_INVALIDDATA;
725  if(i & stereo) {
726  if(vlc[2].table)
727  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
728  else
729  res = 0;
730  if (res < 0) {
731  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
732  return AVERROR_INVALIDDATA;
733  }
734  val = h[2].values[res];
735  if(vlc[3].table)
736  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
737  else
738  res = 0;
739  if (res < 0) {
740  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
741  return AVERROR_INVALIDDATA;
742  }
743  val |= h[3].values[res] << 8;
744  pred[1] += (unsigned)sign_extend(val, 16);
745  *samples++ = pred[1];
746  } else {
747  if(vlc[0].table)
748  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
749  else
750  res = 0;
751  if (res < 0) {
752  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
753  return AVERROR_INVALIDDATA;
754  }
755  val = h[0].values[res];
756  if(vlc[1].table)
757  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
758  else
759  res = 0;
760  if (res < 0) {
761  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
762  return AVERROR_INVALIDDATA;
763  }
764  val |= h[1].values[res] << 8;
765  pred[0] += (unsigned)sign_extend(val, 16);
766  *samples++ = pred[0];
767  }
768  }
769  } else { //8-bit data
770  for(i = stereo; i >= 0; i--)
771  pred[i] = get_bits(&gb, 8);
772  for(i = 0; i <= stereo; i++)
773  *samples8++ = pred[i];
774  for(; i < unp_size; i++) {
775  if(get_bits_left(&gb)<0)
776  return AVERROR_INVALIDDATA;
777  if(i & stereo){
778  if(vlc[1].table)
779  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
780  else
781  res = 0;
782  if (res < 0) {
783  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
784  return AVERROR_INVALIDDATA;
785  }
786  pred[1] += sign_extend(h[1].values[res], 8);
787  *samples8++ = pred[1];
788  } else {
789  if(vlc[0].table)
790  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
791  else
792  res = 0;
793  if (res < 0) {
794  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
795  return AVERROR_INVALIDDATA;
796  }
797  pred[0] += sign_extend(h[0].values[res], 8);
798  *samples8++ = pred[0];
799  }
800  }
801  }
802 
803  *got_frame_ptr = 1;
804  ret = buf_size;
805 
806 error:
807  for(i = 0; i < 4; i++) {
808  if(vlc[i].table)
809  ff_free_vlc(&vlc[i]);
810  av_free(h[i].bits);
811  av_free(h[i].lengths);
812  av_free(h[i].values);
813  }
814 
815  return ret;
816 }
817 
819  .name = "smackvid",
820  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
821  .type = AVMEDIA_TYPE_VIDEO,
823  .priv_data_size = sizeof(SmackVContext),
824  .init = decode_init,
825  .close = decode_end,
826  .decode = decode_frame,
827  .capabilities = AV_CODEC_CAP_DR1,
828 };
829 
831  .name = "smackaud",
832  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
833  .type = AVMEDIA_TYPE_AUDIO,
835  .init = smka_decode_init,
836  .decode = smka_decode_frame,
837  .capabilities = AV_CODEC_CAP_DR1,
838 };
AVCodec
AVCodec.
Definition: avcodec.h:3481
stride
int stride
Definition: mace.c:144
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
r
const char * r
Definition: vf_curves.c:114
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:91
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2276
HuffContext
Context used for code reconstructing.
Definition: smacker.c:60
HuffContext::maxlength
int maxlength
Definition: smacker.c:62
out
FILE * out
Definition: movenc.c:54
GetByteContext
Definition: bytestream.h:33
SmackVContext::full_last
int full_last[3]
Definition: smacker.c:54
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:366
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
SMK_BLK_SKIP
@ SMK_BLK_SKIP
Definition: smacker.c:92
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
block_runs
static const int block_runs[64]
Definition: smacker.c:79
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
init_vlc
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
table
static const uint16_t table[]
Definition: prosumer.c:206
data
const char data[16]
Definition: mxf.c:91
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191
get_vlc2
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
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:2012
INIT_VLC_LE
#define INIT_VLC_LE
Definition: vlc.h:54
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
U
#define U(x)
Definition: vp56_arith.h:37
SmackVContext
Definition: smacker.c:49
GetBitContext
Definition: get_bits.h:61
AV_CODEC_ID_SMACKAUDIO
@ AV_CODEC_ID_SMACKAUDIO
Definition: avcodec.h:587
SMKTREE_DECODE_BIG_MAX_RECURSION
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition: smacker.c:47
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
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
DBCtx::recode2
int * recode2
Definition: smacker.c:72
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
SmackVContext::mclr_last
int mclr_last[3]
Definition: smacker.c:54
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
av_cold
#define av_cold
Definition: attributes.h:84
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:582
ff_smacker_decoder
AVCodec ff_smacker_decoder
Definition: smacker.c:818
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:1667
DBCtx::recode1
int * recode1
Definition: smacker.c:72
smka_decode_init
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:612
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
bits
uint8_t bits
Definition: vp3data.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
smka_decode_frame
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:627
ff_free_vlc
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
blk
#define blk(i)
Definition: sha.c:185
SmackVContext::type_last
int type_last[3]
Definition: smacker.c:54
DBCtx::escapes
int escapes[3]
Definition: smacker.c:73
NULL
#define NULL
Definition: coverity.c:32
smacker_decode_tree
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
Decode local frame tree.
Definition: smacker.c:98
run
uint8_t run
Definition: svq3.c:206
HuffContext::length
int length
Definition: smacker.c:61
SmkBlockTypes
SmkBlockTypes
Definition: smacker.c:89
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
mathops.h
ff_smackaud_decoder
AVCodec ff_smackaud_decoder
Definition: smacker.c:830
AV_SAMPLE_FMT_U8
AV_SAMPLE_FMT_U8
Definition: audio_convert.c:194
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:46
error
static void error(const char *err)
Definition: target_dec_fuzzer.c:61
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:187
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1965
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
HuffContext::values
int * values
Definition: smacker.c:66
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
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:443
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:567
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2233
size
int size
Definition: twinvq_data.h:11134
SMK_BLK_FILL
@ SMK_BLK_FILL
Definition: smacker.c:93
SmackVContext::avctx
AVCodecContext * avctx
Definition: smacker.c:50
DBCtx::v1
VLC * v1
Definition: smacker.c:71
val
const char const char void * val
Definition: avisynth_c.h:863
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
av_bswap16
#define av_bswap16
Definition: bswap.h:31
HuffContext::bits
uint32_t * bits
Definition: smacker.c:64
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2226
SmackVContext::mmap_tbl
int * mmap_tbl
Definition: smacker.c:53
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2789
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
av_always_inline
#define av_always_inline
Definition: attributes.h:43
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
AVCodecContext::height
int height
Definition: avcodec.h:1738
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
AV_CODEC_ID_SMACKVIDEO
@ AV_CODEC_ID_SMACKVIDEO
Definition: avcodec.h:301
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:71
last_reset
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:384
SmackVContext::type_tbl
int * type_tbl
Definition: smacker.c:53
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
SMKTREE_BITS
#define SMKTREE_BITS
Definition: smacker.c:43
SmackVContext::mclr_tbl
int * mclr_tbl
Definition: smacker.c:53
channel_layout.h
SmackVContext::mmap_last
int mmap_last[3]
Definition: smacker.c:54
AVFrame::palette_has_changed
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:452
mode
mode
Definition: ebur128.h:83
VLC
Definition: vlc.h:26
SmackVContext::full_tbl
int * full_tbl
Definition: smacker.c:53
HuffContext::current
int current
Definition: smacker.c:63
SmackVContext::pic
AVFrame * pic
Definition: smacker.c:51
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
DBCtx::last
int * last
Definition: smacker.c:74
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:70
SMK_BLK_MONO
@ SMK_BLK_MONO
Definition: smacker.c:90
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
HuffContext::lengths
int * lengths
Definition: smacker.c:65
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
SMK_NODE
#define SMK_NODE
Definition: smacker.c:44
decode_frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:410
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:85
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
decode_header_trees
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:319
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1738
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
length
const char int length
Definition: avisynth_c.h:860
h
h
Definition: vp9dsp_template.c:2038
DBCtx::lcur
int lcur
Definition: smacker.c:75
smacker_decode_bigtree
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx, int length)
Decode header tree.
Definition: smacker.c:135
smk_get_code
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition: smacker.c:389