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 >= hc->length){
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  if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
208  !tmp2.bits || !tmp2.lengths || !tmp2.values) {
209  err = AVERROR(ENOMEM);
210  goto error;
211  }
212 
213  if(get_bits1(gb)) {
214  res = smacker_decode_tree(gb, &tmp1, 0, 0);
215  if (res < 0) {
216  err = res;
217  goto error;
218  }
219  skip_bits1(gb);
220  if(tmp1.current > 1) {
221  res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
222  tmp1.lengths, sizeof(int), sizeof(int),
223  tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
224  if(res < 0) {
225  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
226  err = res;
227  goto error;
228  }
229  }
230  }
231  if (!vlc[0].table) {
232  av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
233  }
234  if(get_bits1(gb)){
235  res = smacker_decode_tree(gb, &tmp2, 0, 0);
236  if (res < 0) {
237  err = res;
238  goto error;
239  }
240  skip_bits1(gb);
241  if(tmp2.current > 1) {
242  res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
243  tmp2.lengths, sizeof(int), sizeof(int),
244  tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
245  if(res < 0) {
246  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
247  err = res;
248  goto error;
249  }
250  }
251  }
252  if (!vlc[1].table) {
253  av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
254  }
255 
256  escapes[0] = get_bits(gb, 16);
257  escapes[1] = get_bits(gb, 16);
258  escapes[2] = get_bits(gb, 16);
259 
260  last[0] = last[1] = last[2] = -1;
261 
262  ctx.escapes[0] = escapes[0];
263  ctx.escapes[1] = escapes[1];
264  ctx.escapes[2] = escapes[2];
265  ctx.v1 = &vlc[0];
266  ctx.v2 = &vlc[1];
267  ctx.recode1 = tmp1.values;
268  ctx.recode2 = tmp2.values;
269  ctx.last = last;
270 
271  huff.length = ((size + 3) >> 2) + 4;
272  huff.maxlength = 0;
273  huff.current = 0;
274  huff.values = av_mallocz_array(huff.length, sizeof(int));
275  if (!huff.values) {
276  err = AVERROR(ENOMEM);
277  goto error;
278  }
279 
280  if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
281  err = -1;
282  skip_bits1(gb);
283  if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
284  if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
285  if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
286  if (ctx.last[0] >= huff.length ||
287  ctx.last[1] >= huff.length ||
288  ctx.last[2] >= huff.length) {
289  av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
290  err = AVERROR_INVALIDDATA;
291  }
292 
293  *recodes = huff.values;
294 
295 error:
296  if(vlc[0].table)
297  ff_free_vlc(&vlc[0]);
298  if(vlc[1].table)
299  ff_free_vlc(&vlc[1]);
300  av_free(tmp1.bits);
301  av_free(tmp1.lengths);
302  av_free(tmp1.values);
303  av_free(tmp2.bits);
304  av_free(tmp2.lengths);
305  av_free(tmp2.values);
306 
307  return err;
308 }
309 
311  GetBitContext gb;
312  int mmap_size, mclr_size, full_size, type_size, ret;
313 
314  mmap_size = AV_RL32(smk->avctx->extradata);
315  mclr_size = AV_RL32(smk->avctx->extradata + 4);
316  full_size = AV_RL32(smk->avctx->extradata + 8);
317  type_size = AV_RL32(smk->avctx->extradata + 12);
318 
319  ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
320  if (ret < 0)
321  return ret;
322 
323  if(!get_bits1(&gb)) {
324  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
325  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
326  if (!smk->mmap_tbl)
327  return AVERROR(ENOMEM);
328  smk->mmap_tbl[0] = 0;
329  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
330  } else {
331  ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
332  if (ret < 0)
333  return ret;
334  }
335  if(!get_bits1(&gb)) {
336  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
337  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
338  if (!smk->mclr_tbl)
339  return AVERROR(ENOMEM);
340  smk->mclr_tbl[0] = 0;
341  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
342  } else {
343  ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
344  if (ret < 0)
345  return ret;
346  }
347  if(!get_bits1(&gb)) {
348  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
349  smk->full_tbl = av_malloc(sizeof(int) * 2);
350  if (!smk->full_tbl)
351  return AVERROR(ENOMEM);
352  smk->full_tbl[0] = 0;
353  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
354  } else {
355  ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
356  if (ret < 0)
357  return ret;
358  }
359  if(!get_bits1(&gb)) {
360  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
361  smk->type_tbl = av_malloc(sizeof(int) * 2);
362  if (!smk->type_tbl)
363  return AVERROR(ENOMEM);
364  smk->type_tbl[0] = 0;
365  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
366  } else {
367  ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
368  if (ret < 0)
369  return ret;
370  }
371 
372  return 0;
373 }
374 
375 static av_always_inline void last_reset(int *recode, int *last) {
376  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
377 }
378 
379 /* get code and update history */
380 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
381  register int *table = recode;
382  int v;
383 
384  while(*table & SMK_NODE) {
385  if(get_bits1(gb))
386  table += (*table) & (~SMK_NODE);
387  table++;
388  }
389  v = *table;
390 
391  if(v != recode[last[0]]) {
392  recode[last[2]] = recode[last[1]];
393  recode[last[1]] = recode[last[0]];
394  recode[last[0]] = v;
395  }
396  return v;
397 }
398 
399 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
400  AVPacket *avpkt)
401 {
402  SmackVContext * const smk = avctx->priv_data;
403  uint8_t *out;
404  uint32_t *pal;
405  GetByteContext gb2;
406  GetBitContext gb;
407  int blocks, blk, bw, bh;
408  int i, ret;
409  int stride;
410  int flags;
411 
412  if (avpkt->size <= 769)
413  return AVERROR_INVALIDDATA;
414 
415  if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0)
416  return ret;
417 
418  /* make the palette available on the way out */
419  pal = (uint32_t*)smk->pic->data[1];
420  bytestream2_init(&gb2, avpkt->data, avpkt->size);
421  flags = bytestream2_get_byteu(&gb2);
422  smk->pic->palette_has_changed = flags & 1;
423  smk->pic->key_frame = !!(flags & 2);
424  if (smk->pic->key_frame)
426  else
428 
429  for(i = 0; i < 256; i++)
430  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
431 
432  last_reset(smk->mmap_tbl, smk->mmap_last);
433  last_reset(smk->mclr_tbl, smk->mclr_last);
434  last_reset(smk->full_tbl, smk->full_last);
435  last_reset(smk->type_tbl, smk->type_last);
436  if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
437  return ret;
438 
439  blk = 0;
440  bw = avctx->width >> 2;
441  bh = avctx->height >> 2;
442  blocks = bw * bh;
443  stride = smk->pic->linesize[0];
444  while(blk < blocks) {
445  int type, run, mode;
446  uint16_t pix;
447 
448  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
449  run = block_runs[(type >> 2) & 0x3F];
450  switch(type & 3){
451  case SMK_BLK_MONO:
452  while(run-- && blk < blocks){
453  int clr, map;
454  int hi, lo;
455  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
456  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
457  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
458  hi = clr >> 8;
459  lo = clr & 0xFF;
460  for(i = 0; i < 4; i++) {
461  if(map & 1) out[0] = hi; else out[0] = lo;
462  if(map & 2) out[1] = hi; else out[1] = lo;
463  if(map & 4) out[2] = hi; else out[2] = lo;
464  if(map & 8) out[3] = hi; else out[3] = lo;
465  map >>= 4;
466  out += stride;
467  }
468  blk++;
469  }
470  break;
471  case SMK_BLK_FULL:
472  mode = 0;
473  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
474  if(get_bits1(&gb)) mode = 1;
475  else if(get_bits1(&gb)) mode = 2;
476  }
477  while(run-- && blk < blocks){
478  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
479  switch(mode){
480  case 0:
481  for(i = 0; i < 4; i++) {
482  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
483  AV_WL16(out+2,pix);
484  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
485  AV_WL16(out,pix);
486  out += stride;
487  }
488  break;
489  case 1:
490  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
491  out[0] = out[1] = pix & 0xFF;
492  out[2] = out[3] = pix >> 8;
493  out += stride;
494  out[0] = out[1] = pix & 0xFF;
495  out[2] = out[3] = pix >> 8;
496  out += stride;
497  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
498  out[0] = out[1] = pix & 0xFF;
499  out[2] = out[3] = pix >> 8;
500  out += stride;
501  out[0] = out[1] = pix & 0xFF;
502  out[2] = out[3] = pix >> 8;
503  break;
504  case 2:
505  for(i = 0; i < 2; i++) {
506  uint16_t pix1, pix2;
507  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
508  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
509  AV_WL16(out,pix1);
510  AV_WL16(out+2,pix2);
511  out += stride;
512  AV_WL16(out,pix1);
513  AV_WL16(out+2,pix2);
514  out += stride;
515  }
516  break;
517  }
518  blk++;
519  }
520  break;
521  case SMK_BLK_SKIP:
522  while(run-- && blk < blocks)
523  blk++;
524  break;
525  case SMK_BLK_FILL:
526  mode = type >> 8;
527  while(run-- && blk < blocks){
528  uint32_t col;
529  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
530  col = mode * 0x01010101;
531  for(i = 0; i < 4; i++) {
532  *((uint32_t*)out) = col;
533  out += stride;
534  }
535  blk++;
536  }
537  break;
538  }
539 
540  }
541 
542  if ((ret = av_frame_ref(data, smk->pic)) < 0)
543  return ret;
544 
545  *got_frame = 1;
546 
547  /* always report that the buffer was completely consumed */
548  return avpkt->size;
549 }
550 
551 
552 
553 /*
554  *
555  * Uninit smacker decoder
556  *
557  */
559 {
560  SmackVContext * const smk = avctx->priv_data;
561 
562  av_freep(&smk->mmap_tbl);
563  av_freep(&smk->mclr_tbl);
564  av_freep(&smk->full_tbl);
565  av_freep(&smk->type_tbl);
566 
567  av_frame_free(&smk->pic);
568 
569  return 0;
570 }
571 
572 
573 /*
574  *
575  * Init smacker decoder
576  *
577  */
579 {
580  SmackVContext * const c = avctx->priv_data;
581  int ret;
582 
583  c->avctx = avctx;
584 
585  avctx->pix_fmt = AV_PIX_FMT_PAL8;
586 
587  c->pic = av_frame_alloc();
588  if (!c->pic)
589  return AVERROR(ENOMEM);
590 
591  /* decode huffman trees from extradata */
592  if(avctx->extradata_size < 16){
593  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
594  decode_end(avctx);
595  return AVERROR(EINVAL);
596  }
597 
598  ret = decode_header_trees(c);
599  if (ret < 0) {
600  decode_end(avctx);
601  return ret;
602  }
603 
604  return 0;
605 }
606 
607 
609 {
610  if (avctx->channels < 1 || avctx->channels > 2) {
611  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
612  return AVERROR(EINVAL);
613  }
616 
617  return 0;
618 }
619 
620 /**
621  * Decode Smacker audio data
622  */
623 static int smka_decode_frame(AVCodecContext *avctx, void *data,
624  int *got_frame_ptr, AVPacket *avpkt)
625 {
626  AVFrame *frame = data;
627  const uint8_t *buf = avpkt->data;
628  int buf_size = avpkt->size;
629  GetBitContext gb;
630  HuffContext h[4] = { { 0 } };
631  VLC vlc[4] = { { 0 } };
632  int16_t *samples;
633  uint8_t *samples8;
634  int val;
635  int i, res, ret;
636  int unp_size;
637  int bits, stereo;
638  int pred[2] = {0, 0};
639 
640  if (buf_size <= 4) {
641  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
642  return AVERROR(EINVAL);
643  }
644 
645  unp_size = AV_RL32(buf);
646 
647  if (unp_size > (1U<<24)) {
648  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
649  return AVERROR_INVALIDDATA;
650  }
651 
652  if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
653  return ret;
654 
655  if(!get_bits1(&gb)){
656  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
657  *got_frame_ptr = 0;
658  return 1;
659  }
660  stereo = get_bits1(&gb);
661  bits = get_bits1(&gb);
662  if (stereo ^ (avctx->channels != 1)) {
663  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
664  return AVERROR(EINVAL);
665  }
666  if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
667  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
668  return AVERROR(EINVAL);
669  }
670 
671  /* get output buffer */
672  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
673  if (unp_size % (avctx->channels * (bits + 1))) {
674  av_log(avctx, AV_LOG_ERROR, "unp_size %d is odd\n", unp_size);
675  return AVERROR(EINVAL);
676  }
677  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
678  return ret;
679  samples = (int16_t *)frame->data[0];
680  samples8 = frame->data[0];
681 
682  // Initialize
683  for(i = 0; i < (1 << (bits + stereo)); i++) {
684  h[i].length = 256;
685  h[i].maxlength = 0;
686  h[i].current = 0;
687  h[i].bits = av_mallocz(256 * 4);
688  h[i].lengths = av_mallocz(256 * sizeof(int));
689  h[i].values = av_mallocz(256 * sizeof(int));
690  if (!h[i].bits || !h[i].lengths || !h[i].values) {
691  ret = AVERROR(ENOMEM);
692  goto error;
693  }
694  skip_bits1(&gb);
695  if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
696  ret = AVERROR_INVALIDDATA;
697  goto error;
698  }
699  skip_bits1(&gb);
700  if(h[i].current > 1) {
701  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
702  h[i].lengths, sizeof(int), sizeof(int),
703  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
704  if(res < 0) {
705  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
706  ret = AVERROR_INVALIDDATA;
707  goto error;
708  }
709  }
710  }
711  /* this codec relies on wraparound instead of clipping audio */
712  if(bits) { //decode 16-bit data
713  for(i = stereo; i >= 0; i--)
714  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
715  for(i = 0; i <= stereo; i++)
716  *samples++ = pred[i];
717  for(; i < unp_size / 2; i++) {
718  if(get_bits_left(&gb)<0)
719  return AVERROR_INVALIDDATA;
720  if(i & stereo) {
721  if(vlc[2].table)
722  res = get_vlc2(&gb, vlc[2].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[2].values[res];
730  if(vlc[3].table)
731  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
732  else
733  res = 0;
734  if (res < 0) {
735  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
736  return AVERROR_INVALIDDATA;
737  }
738  val |= h[3].values[res] << 8;
739  pred[1] += sign_extend(val, 16);
740  *samples++ = pred[1];
741  } else {
742  if(vlc[0].table)
743  res = get_vlc2(&gb, vlc[0].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[0].values[res];
751  if(vlc[1].table)
752  res = get_vlc2(&gb, vlc[1].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  val |= h[1].values[res] << 8;
760  pred[0] += sign_extend(val, 16);
761  *samples++ = pred[0];
762  }
763  }
764  } else { //8-bit data
765  for(i = stereo; i >= 0; i--)
766  pred[i] = get_bits(&gb, 8);
767  for(i = 0; i <= stereo; i++)
768  *samples8++ = pred[i];
769  for(; i < unp_size; i++) {
770  if(get_bits_left(&gb)<0)
771  return AVERROR_INVALIDDATA;
772  if(i & stereo){
773  if(vlc[1].table)
774  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
775  else
776  res = 0;
777  if (res < 0) {
778  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
779  return AVERROR_INVALIDDATA;
780  }
781  pred[1] += sign_extend(h[1].values[res], 8);
782  *samples8++ = pred[1];
783  } else {
784  if(vlc[0].table)
785  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
786  else
787  res = 0;
788  if (res < 0) {
789  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
790  return AVERROR_INVALIDDATA;
791  }
792  pred[0] += sign_extend(h[0].values[res], 8);
793  *samples8++ = pred[0];
794  }
795  }
796  }
797 
798  *got_frame_ptr = 1;
799  ret = buf_size;
800 
801 error:
802  for(i = 0; i < 4; i++) {
803  if(vlc[i].table)
804  ff_free_vlc(&vlc[i]);
805  av_free(h[i].bits);
806  av_free(h[i].lengths);
807  av_free(h[i].values);
808  }
809 
810  return ret;
811 }
812 
814  .name = "smackvid",
815  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
816  .type = AVMEDIA_TYPE_VIDEO,
818  .priv_data_size = sizeof(SmackVContext),
819  .init = decode_init,
820  .close = decode_end,
821  .decode = decode_frame,
822  .capabilities = AV_CODEC_CAP_DR1,
823 };
824 
826  .name = "smackaud",
827  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
828  .type = AVMEDIA_TYPE_AUDIO,
830  .init = smka_decode_init,
831  .decode = smka_decode_frame,
832  .capabilities = AV_CODEC_CAP_DR1,
833 };
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:399
int type_last[3]
Definition: smacker.c:54
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
#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:181
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int lcur
Definition: smacker.c:75
static const int block_runs[64]
Definition: smacker.c:79
AVFormatContext * ctx
Definition: movenc-test.c:48
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
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:1468
#define av_bswap16
Definition: bswap.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
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:149
#define AV_CH_LAYOUT_STEREO
#define blk(i)
Definition: sha.c:185
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:578
AVCodec.
Definition: avcodec.h:3392
int escapes[3]
Definition: smacker.c:73
VLC * v2
Definition: smacker.c:71
uint8_t bits
Definition: crc.c:295
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2295
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:141
VLC * v1
Definition: smacker.c:71
8 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:74
mode
Definition: f_perms.c:27
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:375
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:608
static AVFrame * frame
Definition: smacker.c:70
uint8_t * data
Definition: avcodec.h:1467
int maxlength
Definition: smacker.c:62
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:2917
#define av_log(a,...)
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:607
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVCodec ff_smacker_decoder
Definition: smacker.c:813
#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:154
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:98
#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:107
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
AVFrame * pic
Definition: smacker.c:51
int * mmap_tbl
Definition: smacker.c:53
Definition: get_bits.h:63
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2338
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:623
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:943
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
int mmap_last[3]
Definition: smacker.c:54
int width
picture width / height.
Definition: avcodec.h:1711
Context used for code reconstructing.
Definition: smacker.c:60
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:558
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:574
int * mclr_tbl
Definition: smacker.c:53
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:179
static const float pred[4]
Definition: siprdata.h:259
FILE * out
Definition: movenc-test.c:54
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:825
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:449
main external API structure.
Definition: avcodec.h:1532
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:894
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1564
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:465
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1648
#define INIT_VLC_LE
Definition: get_bits.h:481
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:312
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:337
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
Decode header tree.
Definition: smacker.c:134
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:322
int * last
Definition: smacker.c:74
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:138
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
int * lengths
Definition: smacker.c:65
AVCodecContext * avctx
Definition: smacker.c:50
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:572
common internal api header.
uint32_t * bits
Definition: smacker.c:64
signed 16 bits
Definition: samplefmt.h:62
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:380
#define SMKTREE_BITS
Definition: smacker.c:43
void * priv_data
Definition: avcodec.h:1574
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:2288
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:247
int mclr_last[3]
Definition: smacker.c:54
int * values
Definition: smacker.c:66
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:39
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:310
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:375
#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:1444
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:360
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:235
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:856
Predicted.
Definition: avutil.h:267