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