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 #include "avcodec.h"
36 #include "internal.h"
37 #include "mathops.h"
38 
39 #define BITSTREAM_READER_LE
40 #include "get_bits.h"
41 #include "bytestream.h"
42 
43 #define SMKTREE_BITS 9
44 #define SMK_NODE 0x80000000
45 
46 /*
47  * Decoder context
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 > 32 || length > 3*SMKTREE_BITS) {
101  av_log(NULL, AV_LOG_ERROR, "length too long\n");
102  return AVERROR_INVALIDDATA;
103  }
104  if(!get_bits1(gb)){ //Leaf
105  if(hc->current >= 256){
106  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
107  return AVERROR_INVALIDDATA;
108  }
109  if(length){
110  hc->bits[hc->current] = prefix;
111  hc->lengths[hc->current] = length;
112  } else {
113  hc->bits[hc->current] = 0;
114  hc->lengths[hc->current] = 0;
115  }
116  hc->values[hc->current] = get_bits(gb, 8);
117  hc->current++;
118  if(hc->maxlength < length)
119  hc->maxlength = length;
120  return 0;
121  } else { //Node
122  int r;
123  length++;
124  r = smacker_decode_tree(gb, hc, prefix, length);
125  if(r)
126  return r;
127  return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
128  }
129 }
130 
131 /**
132  * Decode header tree
133  */
135 {
136  if (hc->current + 1 >= hc->length) {
137  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
138  return AVERROR_INVALIDDATA;
139  }
140  if(!get_bits1(gb)){ //Leaf
141  int val, i1, i2;
142  i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
143  i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
144  if (i1 < 0 || i2 < 0)
145  return AVERROR_INVALIDDATA;
146  val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
147  if(val == ctx->escapes[0]) {
148  ctx->last[0] = hc->current;
149  val = 0;
150  } else if(val == ctx->escapes[1]) {
151  ctx->last[1] = hc->current;
152  val = 0;
153  } else if(val == ctx->escapes[2]) {
154  ctx->last[2] = hc->current;
155  val = 0;
156  }
157 
158  hc->values[hc->current++] = val;
159  return 1;
160  } else { //Node
161  int r = 0, r_new, t;
162 
163  t = hc->current++;
164  r = smacker_decode_bigtree(gb, hc, ctx);
165  if(r < 0)
166  return r;
167  hc->values[t] = SMK_NODE | r;
168  r++;
169  r_new = smacker_decode_bigtree(gb, hc, ctx);
170  if (r_new < 0)
171  return r_new;
172  return r + r_new;
173  }
174 }
175 
176 /**
177  * Store large tree as FFmpeg's vlc codes
178  */
179 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
180 {
181  int res;
182  HuffContext huff;
183  HuffContext tmp1, tmp2;
184  VLC vlc[2] = { { 0 } };
185  int escapes[3];
186  DBCtx ctx;
187  int err = 0;
188 
189  if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
190  av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
191  return AVERROR_INVALIDDATA;
192  }
193 
194  tmp1.length = 256;
195  tmp1.maxlength = 0;
196  tmp1.current = 0;
197  tmp1.bits = av_mallocz(256 * 4);
198  tmp1.lengths = av_mallocz(256 * sizeof(int));
199  tmp1.values = av_mallocz(256 * sizeof(int));
200 
201  tmp2.length = 256;
202  tmp2.maxlength = 0;
203  tmp2.current = 0;
204  tmp2.bits = av_mallocz(256 * 4);
205  tmp2.lengths = av_mallocz(256 * sizeof(int));
206  tmp2.values = av_mallocz(256 * sizeof(int));
207 
208  if(get_bits1(gb)) {
209  res = smacker_decode_tree(gb, &tmp1, 0, 0);
210  if (res < 0)
211  return res;
212  skip_bits1(gb);
213  if(tmp1.current > 1) {
214  res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
215  tmp1.lengths, sizeof(int), sizeof(int),
216  tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
217  if(res < 0) {
218  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
219  return AVERROR_INVALIDDATA;
220  }
221  }
222  }
223  if (!vlc[0].table) {
224  av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
225  }
226  if(get_bits1(gb)){
227  res = smacker_decode_tree(gb, &tmp2, 0, 0);
228  if (res < 0)
229  return res;
230  skip_bits1(gb);
231  if(tmp2.current > 1) {
232  res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
233  tmp2.lengths, sizeof(int), sizeof(int),
234  tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
235  if(res < 0) {
236  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
237  return AVERROR_INVALIDDATA;
238  }
239  }
240  }
241  if (!vlc[1].table) {
242  av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
243  }
244 
245  escapes[0] = get_bits(gb, 16);
246  escapes[1] = get_bits(gb, 16);
247  escapes[2] = get_bits(gb, 16);
248 
249  last[0] = last[1] = last[2] = -1;
250 
251  ctx.escapes[0] = escapes[0];
252  ctx.escapes[1] = escapes[1];
253  ctx.escapes[2] = escapes[2];
254  ctx.v1 = &vlc[0];
255  ctx.v2 = &vlc[1];
256  ctx.recode1 = tmp1.values;
257  ctx.recode2 = tmp2.values;
258  ctx.last = last;
259 
260  huff.length = ((size + 3) >> 2) + 4;
261  huff.maxlength = 0;
262  huff.current = 0;
263  huff.values = av_mallocz(huff.length * sizeof(int));
264  if (!huff.values)
265  return AVERROR(ENOMEM);
266 
267  if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
268  err = -1;
269  skip_bits1(gb);
270  if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
271  if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
272  if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
273  if(huff.current > huff.length){
274  ctx.last[0] = ctx.last[1] = ctx.last[2] = 1;
275  av_log(smk->avctx, AV_LOG_ERROR, "bigtree damaged\n");
276  return AVERROR_INVALIDDATA;
277  }
278 
279  *recodes = huff.values;
280 
281  if(vlc[0].table)
282  ff_free_vlc(&vlc[0]);
283  if(vlc[1].table)
284  ff_free_vlc(&vlc[1]);
285  av_free(tmp1.bits);
286  av_free(tmp1.lengths);
287  av_free(tmp1.values);
288  av_free(tmp2.bits);
289  av_free(tmp2.lengths);
290  av_free(tmp2.values);
291 
292  return err;
293 }
294 
296  GetBitContext gb;
297  int mmap_size, mclr_size, full_size, type_size, ret;
298 
299  mmap_size = AV_RL32(smk->avctx->extradata);
300  mclr_size = AV_RL32(smk->avctx->extradata + 4);
301  full_size = AV_RL32(smk->avctx->extradata + 8);
302  type_size = AV_RL32(smk->avctx->extradata + 12);
303 
304  init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
305 
306  if(!get_bits1(&gb)) {
307  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
308  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
309  smk->mmap_tbl[0] = 0;
310  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
311  } else {
312  ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
313  if (ret < 0)
314  return ret;
315  }
316  if(!get_bits1(&gb)) {
317  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
318  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
319  smk->mclr_tbl[0] = 0;
320  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
321  } else {
322  ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
323  if (ret < 0)
324  return ret;
325  }
326  if(!get_bits1(&gb)) {
327  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
328  smk->full_tbl = av_malloc(sizeof(int) * 2);
329  smk->full_tbl[0] = 0;
330  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
331  } else {
332  ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
333  if (ret < 0)
334  return ret;
335  }
336  if(!get_bits1(&gb)) {
337  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
338  smk->type_tbl = av_malloc(sizeof(int) * 2);
339  smk->type_tbl[0] = 0;
340  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
341  } else {
342  ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
343  if (ret < 0)
344  return ret;
345  }
346 
347  return 0;
348 }
349 
350 static av_always_inline void last_reset(int *recode, int *last) {
351  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
352 }
353 
354 /* get code and update history */
355 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
356  register int *table = recode;
357  int v;
358 
359  while(*table & SMK_NODE) {
360  if(get_bits1(gb))
361  table += (*table) & (~SMK_NODE);
362  table++;
363  }
364  v = *table;
365 
366  if(v != recode[last[0]]) {
367  recode[last[2]] = recode[last[1]];
368  recode[last[1]] = recode[last[0]];
369  recode[last[0]] = v;
370  }
371  return v;
372 }
373 
374 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
375  AVPacket *avpkt)
376 {
377  SmackVContext * const smk = avctx->priv_data;
378  uint8_t *out;
379  uint32_t *pal;
380  GetByteContext gb2;
381  GetBitContext gb;
382  int blocks, blk, bw, bh;
383  int i, ret;
384  int stride;
385  int flags;
386 
387  if (avpkt->size <= 769)
388  return AVERROR_INVALIDDATA;
389 
390  if ((ret = ff_reget_buffer(avctx, &smk->pic)) < 0)
391  return ret;
392 
393  /* make the palette available on the way out */
394  pal = (uint32_t*)smk->pic.data[1];
395  bytestream2_init(&gb2, avpkt->data, avpkt->size);
396  flags = bytestream2_get_byteu(&gb2);
397  smk->pic.palette_has_changed = flags & 1;
398  smk->pic.key_frame = !!(flags & 2);
399  if(smk->pic.key_frame)
401  else
403 
404  for(i = 0; i < 256; i++)
405  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
406 
407  last_reset(smk->mmap_tbl, smk->mmap_last);
408  last_reset(smk->mclr_tbl, smk->mclr_last);
409  last_reset(smk->full_tbl, smk->full_last);
410  last_reset(smk->type_tbl, smk->type_last);
411  init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
412 
413  blk = 0;
414  bw = avctx->width >> 2;
415  bh = avctx->height >> 2;
416  blocks = bw * bh;
417  out = smk->pic.data[0];
418  stride = smk->pic.linesize[0];
419  while(blk < blocks) {
420  int type, run, mode;
421  uint16_t pix;
422 
423  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
424  run = block_runs[(type >> 2) & 0x3F];
425  switch(type & 3){
426  case SMK_BLK_MONO:
427  while(run-- && blk < blocks){
428  int clr, map;
429  int hi, lo;
430  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
431  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
432  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
433  hi = clr >> 8;
434  lo = clr & 0xFF;
435  for(i = 0; i < 4; i++) {
436  if(map & 1) out[0] = hi; else out[0] = lo;
437  if(map & 2) out[1] = hi; else out[1] = lo;
438  if(map & 4) out[2] = hi; else out[2] = lo;
439  if(map & 8) out[3] = hi; else out[3] = lo;
440  map >>= 4;
441  out += stride;
442  }
443  blk++;
444  }
445  break;
446  case SMK_BLK_FULL:
447  mode = 0;
448  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
449  if(get_bits1(&gb)) mode = 1;
450  else if(get_bits1(&gb)) mode = 2;
451  }
452  while(run-- && blk < blocks){
453  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
454  switch(mode){
455  case 0:
456  for(i = 0; i < 4; i++) {
457  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
458  AV_WL16(out+2,pix);
459  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
460  AV_WL16(out,pix);
461  out += stride;
462  }
463  break;
464  case 1:
465  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
466  out[0] = out[1] = pix & 0xFF;
467  out[2] = out[3] = pix >> 8;
468  out += stride;
469  out[0] = out[1] = pix & 0xFF;
470  out[2] = out[3] = pix >> 8;
471  out += stride;
472  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
473  out[0] = out[1] = pix & 0xFF;
474  out[2] = out[3] = pix >> 8;
475  out += stride;
476  out[0] = out[1] = pix & 0xFF;
477  out[2] = out[3] = pix >> 8;
478  out += stride;
479  break;
480  case 2:
481  for(i = 0; i < 2; i++) {
482  uint16_t pix1, pix2;
483  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
484  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
485  AV_WL16(out,pix1);
486  AV_WL16(out+2,pix2);
487  out += stride;
488  AV_WL16(out,pix1);
489  AV_WL16(out+2,pix2);
490  out += stride;
491  }
492  break;
493  }
494  blk++;
495  }
496  break;
497  case SMK_BLK_SKIP:
498  while(run-- && blk < blocks)
499  blk++;
500  break;
501  case SMK_BLK_FILL:
502  mode = type >> 8;
503  while(run-- && blk < blocks){
504  uint32_t col;
505  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
506  col = mode * 0x01010101;
507  for(i = 0; i < 4; i++) {
508  *((uint32_t*)out) = col;
509  out += stride;
510  }
511  blk++;
512  }
513  break;
514  }
515 
516  }
517 
518  if ((ret = av_frame_ref(data, &smk->pic)) < 0)
519  return ret;
520 
521  *got_frame = 1;
522 
523  /* always report that the buffer was completely consumed */
524  return avpkt->size;
525 }
526 
527 
528 
529 /*
530  *
531  * Init smacker decoder
532  *
533  */
535 {
536  SmackVContext * const c = avctx->priv_data;
537  int ret;
538 
539  c->avctx = avctx;
540 
541  avctx->pix_fmt = AV_PIX_FMT_PAL8;
543 
544  /* decode huffman trees from extradata */
545  if(avctx->extradata_size < 16){
546  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
547  return AVERROR(EINVAL);
548  }
549 
550  ret = decode_header_trees(c);
551  if (ret < 0)
552  return ret;
553 
554  return 0;
555 }
556 
557 
558 
559 /*
560  *
561  * Uninit smacker decoder
562  *
563  */
565 {
566  SmackVContext * const smk = avctx->priv_data;
567 
568  av_freep(&smk->mmap_tbl);
569  av_freep(&smk->mclr_tbl);
570  av_freep(&smk->full_tbl);
571  av_freep(&smk->type_tbl);
572 
573  av_frame_unref(&smk->pic);
574 
575  return 0;
576 }
577 
578 
580 {
581  if (avctx->channels < 1 || avctx->channels > 2) {
582  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
583  return AVERROR(EINVAL);
584  }
587 
588  return 0;
589 }
590 
591 /**
592  * Decode Smacker audio data
593  */
594 static int smka_decode_frame(AVCodecContext *avctx, void *data,
595  int *got_frame_ptr, AVPacket *avpkt)
596 {
597  AVFrame *frame = data;
598  const uint8_t *buf = avpkt->data;
599  int buf_size = avpkt->size;
600  GetBitContext gb;
601  HuffContext h[4] = { { 0 } };
602  VLC vlc[4] = { { 0 } };
603  int16_t *samples;
604  uint8_t *samples8;
605  int val;
606  int i, res, ret;
607  int unp_size;
608  int bits, stereo;
609  int pred[2] = {0, 0};
610 
611  if (buf_size <= 4) {
612  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
613  return AVERROR(EINVAL);
614  }
615 
616  unp_size = AV_RL32(buf);
617 
618  if (unp_size > (1U<<24)) {
619  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
620  return AVERROR_INVALIDDATA;
621  }
622 
623  init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
624 
625  if(!get_bits1(&gb)){
626  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
627  *got_frame_ptr = 0;
628  return 1;
629  }
630  stereo = get_bits1(&gb);
631  bits = get_bits1(&gb);
632  if (stereo ^ (avctx->channels != 1)) {
633  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
634  return AVERROR(EINVAL);
635  }
636  if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
637  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
638  return AVERROR(EINVAL);
639  }
640 
641  /* get output buffer */
642  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
643  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
644  return ret;
645  samples = (int16_t *)frame->data[0];
646  samples8 = frame->data[0];
647 
648  // Initialize
649  for(i = 0; i < (1 << (bits + stereo)); i++) {
650  h[i].length = 256;
651  h[i].maxlength = 0;
652  h[i].current = 0;
653  h[i].bits = av_mallocz(256 * 4);
654  h[i].lengths = av_mallocz(256 * sizeof(int));
655  h[i].values = av_mallocz(256 * sizeof(int));
656  skip_bits1(&gb);
657  if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
658  for (; i >= 0; i--) {
659  if (vlc[i].table)
660  ff_free_vlc(&vlc[i]);
661  av_free(h[i].bits);
662  av_free(h[i].lengths);
663  av_free(h[i].values);
664  }
665  return AVERROR_INVALIDDATA;
666  }
667  skip_bits1(&gb);
668  if(h[i].current > 1) {
669  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
670  h[i].lengths, sizeof(int), sizeof(int),
671  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
672  if(res < 0) {
673  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
674  return AVERROR_INVALIDDATA;
675  }
676  }
677  }
678  /* this codec relies on wraparound instead of clipping audio */
679  if(bits) { //decode 16-bit data
680  for(i = stereo; i >= 0; i--)
681  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
682  for(i = 0; i <= stereo; i++)
683  *samples++ = pred[i];
684  for(; i < unp_size / 2; i++) {
685  if(get_bits_left(&gb)<0)
686  return AVERROR_INVALIDDATA;
687  if(i & stereo) {
688  if(vlc[2].table)
689  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
690  else
691  res = 0;
692  if (res < 0) {
693  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
694  return AVERROR_INVALIDDATA;
695  }
696  val = h[2].values[res];
697  if(vlc[3].table)
698  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
699  else
700  res = 0;
701  if (res < 0) {
702  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
703  return AVERROR_INVALIDDATA;
704  }
705  val |= h[3].values[res] << 8;
706  pred[1] += sign_extend(val, 16);
707  *samples++ = pred[1];
708  } else {
709  if(vlc[0].table)
710  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
711  else
712  res = 0;
713  if (res < 0) {
714  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
715  return AVERROR_INVALIDDATA;
716  }
717  val = h[0].values[res];
718  if(vlc[1].table)
719  res = get_vlc2(&gb, vlc[1].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[1].values[res] << 8;
727  pred[0] += sign_extend(val, 16);
728  *samples++ = pred[0];
729  }
730  }
731  } else { //8-bit data
732  for(i = stereo; i >= 0; i--)
733  pred[i] = get_bits(&gb, 8);
734  for(i = 0; i <= stereo; i++)
735  *samples8++ = pred[i];
736  for(; i < unp_size; i++) {
737  if(get_bits_left(&gb)<0)
738  return AVERROR_INVALIDDATA;
739  if(i & stereo){
740  if(vlc[1].table)
741  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
742  else
743  res = 0;
744  if (res < 0) {
745  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
746  return AVERROR_INVALIDDATA;
747  }
748  pred[1] += sign_extend(h[1].values[res], 8);
749  *samples8++ = pred[1];
750  } else {
751  if(vlc[0].table)
752  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
753  else
754  res = 0;
755  if (res < 0) {
756  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
757  return AVERROR_INVALIDDATA;
758  }
759  pred[0] += sign_extend(h[0].values[res], 8);
760  *samples8++ = pred[0];
761  }
762  }
763  }
764 
765  for(i = 0; i < 4; i++) {
766  if(vlc[i].table)
767  ff_free_vlc(&vlc[i]);
768  av_free(h[i].bits);
769  av_free(h[i].lengths);
770  av_free(h[i].values);
771  }
772 
773  *got_frame_ptr = 1;
774 
775  return buf_size;
776 }
777 
779  .name = "smackvid",
780  .type = AVMEDIA_TYPE_VIDEO,
782  .priv_data_size = sizeof(SmackVContext),
783  .init = decode_init,
784  .close = decode_end,
785  .decode = decode_frame,
786  .capabilities = CODEC_CAP_DR1,
787  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
788 };
789 
791  .name = "smackaud",
792  .type = AVMEDIA_TYPE_AUDIO,
794  .init = smka_decode_init,
795  .decode = smka_decode_frame,
796  .capabilities = CODEC_CAP_DR1,
797  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
798 };