FFmpeg  4.2.3
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 {
100  if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
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.
139  if (length > SMKTREE_DECODE_BIG_MAX_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_bits1(&gb)) mode = 1;
488  else if(get_bits1(&gb)) mode = 2;
489  }
490  while(run-- && blk < blocks){
491  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
492  switch(mode){
493  case 0:
494  for(i = 0; i < 4; i++) {
495  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
496  AV_WL16(out+2,pix);
497  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
498  AV_WL16(out,pix);
499  out += stride;
500  }
501  break;
502  case 1:
503  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
504  out[0] = out[1] = pix & 0xFF;
505  out[2] = out[3] = pix >> 8;
506  out += stride;
507  out[0] = out[1] = pix & 0xFF;
508  out[2] = out[3] = pix >> 8;
509  out += stride;
510  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
511  out[0] = out[1] = pix & 0xFF;
512  out[2] = out[3] = pix >> 8;
513  out += stride;
514  out[0] = out[1] = pix & 0xFF;
515  out[2] = out[3] = pix >> 8;
516  break;
517  case 2:
518  for(i = 0; i < 2; i++) {
519  uint16_t pix1, pix2;
520  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
521  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
522  AV_WL16(out,pix1);
523  AV_WL16(out+2,pix2);
524  out += stride;
525  AV_WL16(out,pix1);
526  AV_WL16(out+2,pix2);
527  out += stride;
528  }
529  break;
530  }
531  blk++;
532  }
533  break;
534  case SMK_BLK_SKIP:
535  while(run-- && blk < blocks)
536  blk++;
537  break;
538  case SMK_BLK_FILL:
539  mode = type >> 8;
540  while(run-- && blk < blocks){
541  uint32_t col;
542  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
543  col = mode * 0x01010101U;
544  for(i = 0; i < 4; i++) {
545  *((uint32_t*)out) = col;
546  out += stride;
547  }
548  blk++;
549  }
550  break;
551  }
552 
553  }
554 
555  if ((ret = av_frame_ref(data, smk->pic)) < 0)
556  return ret;
557 
558  *got_frame = 1;
559 
560  /* always report that the buffer was completely consumed */
561  return avpkt->size;
562 }
563 
564 
566 {
567  SmackVContext * const smk = avctx->priv_data;
568 
569  av_freep(&smk->mmap_tbl);
570  av_freep(&smk->mclr_tbl);
571  av_freep(&smk->full_tbl);
572  av_freep(&smk->type_tbl);
573 
574  av_frame_free(&smk->pic);
575 
576  return 0;
577 }
578 
579 
581 {
582  SmackVContext * const c = avctx->priv_data;
583  int ret;
584 
585  c->avctx = avctx;
586 
587  avctx->pix_fmt = AV_PIX_FMT_PAL8;
588 
589  c->pic = av_frame_alloc();
590  if (!c->pic)
591  return AVERROR(ENOMEM);
592 
593  /* decode huffman trees from extradata */
594  if(avctx->extradata_size < 16){
595  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
596  decode_end(avctx);
597  return AVERROR(EINVAL);
598  }
599 
600  ret = decode_header_trees(c);
601  if (ret < 0) {
602  decode_end(avctx);
603  return ret;
604  }
605 
606  return 0;
607 }
608 
609 
611 {
612  if (avctx->channels < 1 || avctx->channels > 2) {
613  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
614  return AVERROR_INVALIDDATA;
615  }
618 
619  return 0;
620 }
621 
622 /**
623  * Decode Smacker audio data
624  */
626  int *got_frame_ptr, AVPacket *avpkt)
627 {
628  AVFrame *frame = data;
629  const uint8_t *buf = avpkt->data;
630  int buf_size = avpkt->size;
631  GetBitContext gb;
632  HuffContext h[4] = { { 0 } };
633  VLC vlc[4] = { { 0 } };
634  int16_t *samples;
635  uint8_t *samples8;
636  int val;
637  int i, res, ret;
638  int unp_size;
639  int bits, stereo;
640  int pred[2] = {0, 0};
641 
642  if (buf_size <= 4) {
643  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
644  return AVERROR_INVALIDDATA;
645  }
646 
647  unp_size = AV_RL32(buf);
648 
649  if (unp_size > (1U<<24)) {
650  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
651  return AVERROR_INVALIDDATA;
652  }
653 
654  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
655  return ret;
656 
657  if(!get_bits1(&gb)){
658  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
659  *got_frame_ptr = 0;
660  return 1;
661  }
662  stereo = get_bits1(&gb);
663  bits = get_bits1(&gb);
664  if (stereo ^ (avctx->channels != 1)) {
665  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
666  return AVERROR_INVALIDDATA;
667  }
668  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
669  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
670  return AVERROR_INVALIDDATA;
671  }
672 
673  /* get output buffer */
674  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
675  if (unp_size % (avctx->channels * (bits + 1))) {
676  av_log(avctx, AV_LOG_ERROR,
677  "The buffer does not contain an integer number of samples\n");
678  return AVERROR_INVALIDDATA;
679  }
680  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
681  return ret;
682  samples = (int16_t *)frame->data[0];
683  samples8 = frame->data[0];
684 
685  // Initialize
686  for(i = 0; i < (1 << (bits + stereo)); i++) {
687  h[i].length = 256;
688  h[i].maxlength = 0;
689  h[i].current = 0;
690  h[i].bits = av_mallocz(256 * 4);
691  h[i].lengths = av_mallocz(256 * sizeof(int));
692  h[i].values = av_mallocz(256 * sizeof(int));
693  if (!h[i].bits || !h[i].lengths || !h[i].values) {
694  ret = AVERROR(ENOMEM);
695  goto error;
696  }
697  skip_bits1(&gb);
698  if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
699  ret = AVERROR_INVALIDDATA;
700  goto error;
701  }
702  skip_bits1(&gb);
703  if(h[i].current > 1) {
704  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
705  h[i].lengths, sizeof(int), sizeof(int),
706  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
707  if(res < 0) {
708  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
709  ret = AVERROR_INVALIDDATA;
710  goto error;
711  }
712  }
713  }
714  /* this codec relies on wraparound instead of clipping audio */
715  if(bits) { //decode 16-bit data
716  for(i = stereo; i >= 0; i--)
717  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
718  for(i = 0; i <= stereo; i++)
719  *samples++ = pred[i];
720  for(; i < unp_size / 2; i++) {
721  if(get_bits_left(&gb)<0)
722  return AVERROR_INVALIDDATA;
723  if(i & stereo) {
724  if(vlc[2].table)
725  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
726  else
727  res = 0;
728  if (res < 0) {
729  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
730  return AVERROR_INVALIDDATA;
731  }
732  val = h[2].values[res];
733  if(vlc[3].table)
734  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
735  else
736  res = 0;
737  if (res < 0) {
738  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
739  return AVERROR_INVALIDDATA;
740  }
741  val |= h[3].values[res] << 8;
742  pred[1] += (unsigned)sign_extend(val, 16);
743  *samples++ = pred[1];
744  } else {
745  if(vlc[0].table)
746  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
747  else
748  res = 0;
749  if (res < 0) {
750  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
751  return AVERROR_INVALIDDATA;
752  }
753  val = h[0].values[res];
754  if(vlc[1].table)
755  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
756  else
757  res = 0;
758  if (res < 0) {
759  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
760  return AVERROR_INVALIDDATA;
761  }
762  val |= h[1].values[res] << 8;
763  pred[0] += (unsigned)sign_extend(val, 16);
764  *samples++ = pred[0];
765  }
766  }
767  } else { //8-bit data
768  for(i = stereo; i >= 0; i--)
769  pred[i] = get_bits(&gb, 8);
770  for(i = 0; i <= stereo; i++)
771  *samples8++ = pred[i];
772  for(; i < unp_size; i++) {
773  if(get_bits_left(&gb)<0)
774  return AVERROR_INVALIDDATA;
775  if(i & stereo){
776  if(vlc[1].table)
777  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
778  else
779  res = 0;
780  if (res < 0) {
781  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
782  return AVERROR_INVALIDDATA;
783  }
784  pred[1] += sign_extend(h[1].values[res], 8);
785  *samples8++ = pred[1];
786  } else {
787  if(vlc[0].table)
788  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
789  else
790  res = 0;
791  if (res < 0) {
792  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
793  return AVERROR_INVALIDDATA;
794  }
795  pred[0] += sign_extend(h[0].values[res], 8);
796  *samples8++ = pred[0];
797  }
798  }
799  }
800 
801  *got_frame_ptr = 1;
802  ret = buf_size;
803 
804 error:
805  for(i = 0; i < 4; i++) {
806  if(vlc[i].table)
807  ff_free_vlc(&vlc[i]);
808  av_free(h[i].bits);
809  av_free(h[i].lengths);
810  av_free(h[i].values);
811  }
812 
813  return ret;
814 }
815 
817  .name = "smackvid",
818  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
819  .type = AVMEDIA_TYPE_VIDEO,
821  .priv_data_size = sizeof(SmackVContext),
822  .init = decode_init,
823  .close = decode_end,
824  .decode = decode_frame,
825  .capabilities = AV_CODEC_CAP_DR1,
826 };
827 
829  .name = "smackaud",
830  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
831  .type = AVMEDIA_TYPE_AUDIO,
833  .init = smka_decode_init,
834  .decode = smka_decode_frame,
835  .capabilities = AV_CODEC_CAP_DR1,
836 };
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx, int length)
Decode header tree.
Definition: smacker.c:135
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:410
int type_last[3]
Definition: smacker.c:54
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:863
#define SMK_NODE
Definition: smacker.c:44
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
int lcur
Definition: smacker.c:75
static const int block_runs[64]
Definition: smacker.c:79
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:63
int length
Definition: smacker.c:61
int * recode1
Definition: smacker.c:72
int * recode2
Definition: smacker.c:72
int size
Definition: avcodec.h:1478
#define av_bswap16
Definition: bswap.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
uint8_t run
Definition: svq3.c:206
#define AV_CH_LAYOUT_STEREO
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
#define blk(i)
Definition: sha.c:185
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:580
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3481
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
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:2011
int escapes[3]
Definition: smacker.c:73
VLC * v2
Definition: smacker.c:71
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2233
uint8_t
#define av_cold
Definition: attributes.h:82
#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:189
VLC * v1
Definition: smacker.c:71
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
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
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:610
static AVFrame * frame
Definition: smacker.c:70
const char data[16]
Definition: mxf.c:91
uint8_t * data
Definition: avcodec.h:1477
int maxlength
Definition: smacker.c:62
bitstream reader API header.
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition: smacker.c:47
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2789
#define av_log(a,...)
static const uint16_t table[]
Definition: prosumer.c:206
int * type_tbl
Definition: smacker.c:53
int full_last[3]
Definition: smacker.c:54
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVCodec ff_smacker_decoder
Definition: smacker.c:816
#define SMKTREE_DECODE_MAX_RECURSION
Definition: smacker.c:46
#define AVERROR(e)
Definition: error.h:43
SmkBlockTypes
Definition: smacker.c:89
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
Decode local frame tree.
Definition: smacker.c:98
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * r
Definition: vf_curves.c:114
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
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
uint8_t bits
Definition: vp3data.h:202
AVFrame * pic
Definition: smacker.c:51
int * mmap_tbl
Definition: smacker.c:53
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2276
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:625
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
int mmap_last[3]
Definition: smacker.c:54
int width
picture width / height.
Definition: avcodec.h:1738
Context used for code reconstructing.
Definition: smacker.c:60
AVFormatContext * ctx
Definition: movenc.c:48
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:565
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
#define AV_RL32
Definition: intreadwrite.h:146
static void error(const char *err)
int * mclr_tbl
Definition: smacker.c:53
#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:187
static const float pred[4]
Definition: siprdata.h:259
int * full_tbl
Definition: smacker.c:53
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
AVCodec ff_smackaud_decoder
Definition: smacker.c:828
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
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:1565
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:1590
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1964
void * buf
Definition: avisynth_c.h:766
int extradata_size
Definition: avcodec.h:1667
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:452
cl_device_type type
const VDPAUPixFmtMap * map
int * last
Definition: smacker.c:74
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
#define flags(name, subs,...)
Definition: cbs_av1.c:564
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
int * lengths
Definition: smacker.c:65
AVCodecContext * avctx
Definition: smacker.c:50
common internal api header.
uint32_t * bits
Definition: smacker.c:64
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
#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:389
#define SMKTREE_BITS
Definition: smacker.c:43
void * priv_data
Definition: avcodec.h:1592
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:2226
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
int mclr_last[3]
Definition: smacker.c:54
int * values
Definition: smacker.c:66
FILE * out
Definition: movenc.c:54
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:39
const char int length
Definition: avisynth_c.h:860
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:319
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:384
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:366
This structure stores compressed data.
Definition: avcodec.h:1454
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
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:361
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
Predicted.
Definition: avutil.h:275
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:191