FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_bits1(gb))
388  table += (*table) & (~SMK_NODE);
389  table++;
390  }
391  v = *table;
392 
393  if(v != recode[last[0]]) {
394  recode[last[2]] = recode[last[1]];
395  recode[last[1]] = recode[last[0]];
396  recode[last[0]] = v;
397  }
398  return v;
399 }
400 
401 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
402  AVPacket *avpkt)
403 {
404  SmackVContext * const smk = avctx->priv_data;
405  uint8_t *out;
406  uint32_t *pal;
407  GetByteContext gb2;
408  GetBitContext gb;
409  int blocks, blk, bw, bh;
410  int i, ret;
411  int stride;
412  int flags;
413 
414  if (avpkt->size <= 769)
415  return AVERROR_INVALIDDATA;
416 
417  if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0)
418  return ret;
419 
420  /* make the palette available on the way out */
421  pal = (uint32_t*)smk->pic->data[1];
422  bytestream2_init(&gb2, avpkt->data, avpkt->size);
423  flags = bytestream2_get_byteu(&gb2);
424  smk->pic->palette_has_changed = flags & 1;
425  smk->pic->key_frame = !!(flags & 2);
426  if (smk->pic->key_frame)
428  else
430 
431  for(i = 0; i < 256; i++)
432  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
433 
434  last_reset(smk->mmap_tbl, smk->mmap_last);
435  last_reset(smk->mclr_tbl, smk->mclr_last);
436  last_reset(smk->full_tbl, smk->full_last);
437  last_reset(smk->type_tbl, smk->type_last);
438  if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
439  return ret;
440 
441  blk = 0;
442  bw = avctx->width >> 2;
443  bh = avctx->height >> 2;
444  blocks = bw * bh;
445  stride = smk->pic->linesize[0];
446  while(blk < blocks) {
447  int type, run, mode;
448  uint16_t pix;
449 
450  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
451  run = block_runs[(type >> 2) & 0x3F];
452  switch(type & 3){
453  case SMK_BLK_MONO:
454  while(run-- && blk < blocks){
455  int clr, map;
456  int hi, lo;
457  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
458  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
459  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
460  hi = clr >> 8;
461  lo = clr & 0xFF;
462  for(i = 0; i < 4; i++) {
463  if(map & 1) out[0] = hi; else out[0] = lo;
464  if(map & 2) out[1] = hi; else out[1] = lo;
465  if(map & 4) out[2] = hi; else out[2] = lo;
466  if(map & 8) out[3] = hi; else out[3] = lo;
467  map >>= 4;
468  out += stride;
469  }
470  blk++;
471  }
472  break;
473  case SMK_BLK_FULL:
474  mode = 0;
475  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
476  if(get_bits1(&gb)) mode = 1;
477  else if(get_bits1(&gb)) mode = 2;
478  }
479  while(run-- && blk < blocks){
480  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
481  switch(mode){
482  case 0:
483  for(i = 0; i < 4; i++) {
484  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
485  AV_WL16(out+2,pix);
486  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
487  AV_WL16(out,pix);
488  out += stride;
489  }
490  break;
491  case 1:
492  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
493  out[0] = out[1] = pix & 0xFF;
494  out[2] = out[3] = pix >> 8;
495  out += stride;
496  out[0] = out[1] = pix & 0xFF;
497  out[2] = out[3] = pix >> 8;
498  out += stride;
499  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
500  out[0] = out[1] = pix & 0xFF;
501  out[2] = out[3] = pix >> 8;
502  out += stride;
503  out[0] = out[1] = pix & 0xFF;
504  out[2] = out[3] = pix >> 8;
505  break;
506  case 2:
507  for(i = 0; i < 2; i++) {
508  uint16_t pix1, pix2;
509  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
510  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
511  AV_WL16(out,pix1);
512  AV_WL16(out+2,pix2);
513  out += stride;
514  AV_WL16(out,pix1);
515  AV_WL16(out+2,pix2);
516  out += stride;
517  }
518  break;
519  }
520  blk++;
521  }
522  break;
523  case SMK_BLK_SKIP:
524  while(run-- && blk < blocks)
525  blk++;
526  break;
527  case SMK_BLK_FILL:
528  mode = type >> 8;
529  while(run-- && blk < blocks){
530  uint32_t col;
531  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
532  col = mode * 0x01010101;
533  for(i = 0; i < 4; i++) {
534  *((uint32_t*)out) = col;
535  out += stride;
536  }
537  blk++;
538  }
539  break;
540  }
541 
542  }
543 
544  if ((ret = av_frame_ref(data, smk->pic)) < 0)
545  return ret;
546 
547  *got_frame = 1;
548 
549  /* always report that the buffer was completely consumed */
550  return avpkt->size;
551 }
552 
553 
555 {
556  SmackVContext * const smk = avctx->priv_data;
557 
558  av_freep(&smk->mmap_tbl);
559  av_freep(&smk->mclr_tbl);
560  av_freep(&smk->full_tbl);
561  av_freep(&smk->type_tbl);
562 
563  av_frame_free(&smk->pic);
564 
565  return 0;
566 }
567 
568 
570 {
571  SmackVContext * const c = avctx->priv_data;
572  int ret;
573 
574  c->avctx = avctx;
575 
576  avctx->pix_fmt = AV_PIX_FMT_PAL8;
577 
578  c->pic = av_frame_alloc();
579  if (!c->pic)
580  return AVERROR(ENOMEM);
581 
582  /* decode huffman trees from extradata */
583  if(avctx->extradata_size < 16){
584  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
585  decode_end(avctx);
586  return AVERROR(EINVAL);
587  }
588 
589  ret = decode_header_trees(c);
590  if (ret < 0) {
591  decode_end(avctx);
592  return ret;
593  }
594 
595  return 0;
596 }
597 
598 
600 {
601  if (avctx->channels < 1 || avctx->channels > 2) {
602  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
603  return AVERROR(EINVAL);
604  }
607 
608  return 0;
609 }
610 
611 /**
612  * Decode Smacker audio data
613  */
614 static int smka_decode_frame(AVCodecContext *avctx, void *data,
615  int *got_frame_ptr, AVPacket *avpkt)
616 {
617  AVFrame *frame = data;
618  const uint8_t *buf = avpkt->data;
619  int buf_size = avpkt->size;
620  GetBitContext gb;
621  HuffContext h[4] = { { 0 } };
622  VLC vlc[4] = { { 0 } };
623  int16_t *samples;
624  uint8_t *samples8;
625  int val;
626  int i, res, ret;
627  int unp_size;
628  int bits, stereo;
629  int pred[2] = {0, 0};
630 
631  if (buf_size <= 4) {
632  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
633  return AVERROR(EINVAL);
634  }
635 
636  unp_size = AV_RL32(buf);
637 
638  if (unp_size > (1U<<24)) {
639  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
640  return AVERROR_INVALIDDATA;
641  }
642 
643  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
644  return ret;
645 
646  if(!get_bits1(&gb)){
647  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
648  *got_frame_ptr = 0;
649  return 1;
650  }
651  stereo = get_bits1(&gb);
652  bits = get_bits1(&gb);
653  if (stereo ^ (avctx->channels != 1)) {
654  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
655  return AVERROR(EINVAL);
656  }
657  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
658  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
659  return AVERROR(EINVAL);
660  }
661 
662  /* get output buffer */
663  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
664  if (unp_size % (avctx->channels * (bits + 1))) {
665  av_log(avctx, AV_LOG_ERROR, "unp_size %d is odd\n", unp_size);
666  return AVERROR(EINVAL);
667  }
668  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
669  return ret;
670  samples = (int16_t *)frame->data[0];
671  samples8 = frame->data[0];
672 
673  // Initialize
674  for(i = 0; i < (1 << (bits + stereo)); i++) {
675  h[i].length = 256;
676  h[i].maxlength = 0;
677  h[i].current = 0;
678  h[i].bits = av_mallocz(256 * 4);
679  h[i].lengths = av_mallocz(256 * sizeof(int));
680  h[i].values = av_mallocz(256 * sizeof(int));
681  if (!h[i].bits || !h[i].lengths || !h[i].values) {
682  ret = AVERROR(ENOMEM);
683  goto error;
684  }
685  skip_bits1(&gb);
686  if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
687  ret = AVERROR_INVALIDDATA;
688  goto error;
689  }
690  skip_bits1(&gb);
691  if(h[i].current > 1) {
692  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
693  h[i].lengths, sizeof(int), sizeof(int),
694  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
695  if(res < 0) {
696  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
697  ret = AVERROR_INVALIDDATA;
698  goto error;
699  }
700  }
701  }
702  /* this codec relies on wraparound instead of clipping audio */
703  if(bits) { //decode 16-bit data
704  for(i = stereo; i >= 0; i--)
705  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
706  for(i = 0; i <= stereo; i++)
707  *samples++ = pred[i];
708  for(; i < unp_size / 2; i++) {
709  if(get_bits_left(&gb)<0)
710  return AVERROR_INVALIDDATA;
711  if(i & stereo) {
712  if(vlc[2].table)
713  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
714  else
715  res = 0;
716  if (res < 0) {
717  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
718  return AVERROR_INVALIDDATA;
719  }
720  val = h[2].values[res];
721  if(vlc[3].table)
722  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
723  else
724  res = 0;
725  if (res < 0) {
726  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
727  return AVERROR_INVALIDDATA;
728  }
729  val |= h[3].values[res] << 8;
730  pred[1] += sign_extend(val, 16);
731  *samples++ = pred[1];
732  } else {
733  if(vlc[0].table)
734  res = get_vlc2(&gb, vlc[0].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[0].values[res];
742  if(vlc[1].table)
743  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
744  else
745  res = 0;
746  if (res < 0) {
747  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
748  return AVERROR_INVALIDDATA;
749  }
750  val |= h[1].values[res] << 8;
751  pred[0] += sign_extend(val, 16);
752  *samples++ = pred[0];
753  }
754  }
755  } else { //8-bit data
756  for(i = stereo; i >= 0; i--)
757  pred[i] = get_bits(&gb, 8);
758  for(i = 0; i <= stereo; i++)
759  *samples8++ = pred[i];
760  for(; i < unp_size; i++) {
761  if(get_bits_left(&gb)<0)
762  return AVERROR_INVALIDDATA;
763  if(i & stereo){
764  if(vlc[1].table)
765  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
766  else
767  res = 0;
768  if (res < 0) {
769  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
770  return AVERROR_INVALIDDATA;
771  }
772  pred[1] += sign_extend(h[1].values[res], 8);
773  *samples8++ = pred[1];
774  } else {
775  if(vlc[0].table)
776  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
777  else
778  res = 0;
779  if (res < 0) {
780  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
781  return AVERROR_INVALIDDATA;
782  }
783  pred[0] += sign_extend(h[0].values[res], 8);
784  *samples8++ = pred[0];
785  }
786  }
787  }
788 
789  *got_frame_ptr = 1;
790  ret = buf_size;
791 
792 error:
793  for(i = 0; i < 4; i++) {
794  if(vlc[i].table)
795  ff_free_vlc(&vlc[i]);
796  av_free(h[i].bits);
797  av_free(h[i].lengths);
798  av_free(h[i].values);
799  }
800 
801  return ret;
802 }
803 
805  .name = "smackvid",
806  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
807  .type = AVMEDIA_TYPE_VIDEO,
809  .priv_data_size = sizeof(SmackVContext),
810  .init = decode_init,
811  .close = decode_end,
812  .decode = decode_frame,
813  .capabilities = AV_CODEC_CAP_DR1,
814 };
815 
817  .name = "smackaud",
818  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
819  .type = AVMEDIA_TYPE_AUDIO,
821  .init = smka_decode_init,
822  .decode = smka_decode_frame,
823  .capabilities = AV_CODEC_CAP_DR1,
824 };
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:401
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
This structure describes decoded (raw) audio or video data.
Definition: frame.h:190
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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:247
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:1608
#define av_bswap16
Definition: bswap.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1910
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
uint8_t run
Definition: svq3.c:206
#define AV_CH_LAYOUT_STEREO
#define blk(i)
Definition: sha.c:185
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:569
AVCodec.
Definition: avcodec.h:3607
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:2452
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:145
VLC * v1
Definition: smacker.c:69
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
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:385
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1797
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:599
static AVFrame * frame
Definition: smacker.c:68
uint8_t * data
Definition: avcodec.h:1607
static int flags
Definition: log.c:57
int maxlength
Definition: smacker.c:60
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3077
#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:572
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVCodec ff_smacker_decoder
Definition: smacker.c:804
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: vlc.h:38
#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:158
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:176
const char * r
Definition: vf_curves.c:111
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3614
AVFrame * pic
Definition: smacker.c:49
int * mmap_tbl
Definition: smacker.c:51
static void error(const char *err)
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:226
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2495
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:614
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: utils.c:997
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:264
int mmap_last[3]
Definition: smacker.c:52
int width
picture width / height.
Definition: avcodec.h:1869
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:554
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:539
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'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:816
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:221
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:441
main external API structure.
Definition: avcodec.h:1682
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:948
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1714
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1798
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:324
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:338
const VDPAUPixFmtMap * map
int * last
Definition: smacker.c:72
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:139
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:204
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
#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:382
#define SMKTREE_BITS
Definition: smacker.c:43
void * priv_data
Definition: avcodec.h:1724
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:2445
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:259
static AVCodec * c
int mclr_last[3]
Definition: smacker.c:52
int * values
Definition: smacker.c:64
FILE * out
Definition: movenc.c:54
#define av_freep(p)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2045
#define av_always_inline
Definition: attributes.h:39
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 stride
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:342
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1584
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:360
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:247
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
Predicted.
Definition: avutil.h:275