Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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 
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 
96 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
97 {
98  if(!get_bits1(gb)){ //Leaf
99  if(hc->current >= 256){
100  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
101  return -1;
102  }
103  if(length){
104  hc->bits[hc->current] = prefix;
105  hc->lengths[hc->current] = length;
106  } else {
107  hc->bits[hc->current] = 0;
108  hc->lengths[hc->current] = 0;
109  }
110  hc->values[hc->current] = get_bits(gb, 8);
111  hc->current++;
112  if(hc->maxlength < length)
113  hc->maxlength = length;
114  return 0;
115  } else { //Node
116  int r;
117  length++;
118  r = smacker_decode_tree(gb, hc, prefix, length);
119  if(r)
120  return r;
121  return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
122  }
123 }
124 
129 {
130  if (hc->current + 1 >= hc->length) {
131  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
132  return -1;
133  }
134  if(!get_bits1(gb)){ //Leaf
135  int val, i1, i2;
136  i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
137  i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
138  if (i1 < 0 || i2 < 0)
139  return -1;
140  val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
141  if(val == ctx->escapes[0]) {
142  ctx->last[0] = hc->current;
143  val = 0;
144  } else if(val == ctx->escapes[1]) {
145  ctx->last[1] = hc->current;
146  val = 0;
147  } else if(val == ctx->escapes[2]) {
148  ctx->last[2] = hc->current;
149  val = 0;
150  }
151 
152  hc->values[hc->current++] = val;
153  return 1;
154  } else { //Node
155  int r = 0, r_new, t;
156 
157  t = hc->current++;
158  r = smacker_decode_bigtree(gb, hc, ctx);
159  if(r < 0)
160  return r;
161  hc->values[t] = SMK_NODE | r;
162  r++;
163  r_new = smacker_decode_bigtree(gb, hc, ctx);
164  if (r_new < 0)
165  return r_new;
166  return r + r_new;
167  }
168 }
169 
173 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
174 {
175  int res;
176  HuffContext huff;
177  HuffContext tmp1, tmp2;
178  VLC vlc[2] = { { 0 } };
179  int escapes[3];
180  DBCtx ctx;
181  int err = 0;
182 
183  if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
184  av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
185  return -1;
186  }
187 
188  tmp1.length = 256;
189  tmp1.maxlength = 0;
190  tmp1.current = 0;
191  tmp1.bits = av_mallocz(256 * 4);
192  tmp1.lengths = av_mallocz(256 * sizeof(int));
193  tmp1.values = av_mallocz(256 * sizeof(int));
194 
195  tmp2.length = 256;
196  tmp2.maxlength = 0;
197  tmp2.current = 0;
198  tmp2.bits = av_mallocz(256 * 4);
199  tmp2.lengths = av_mallocz(256 * sizeof(int));
200  tmp2.values = av_mallocz(256 * sizeof(int));
201  if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
202  !tmp2.bits || !tmp2.lengths || !tmp2.values) {
203  err = AVERROR(ENOMEM);
204  goto error;
205  }
206 
207  if(get_bits1(gb)) {
208  smacker_decode_tree(gb, &tmp1, 0, 0);
209  skip_bits1(gb);
210  res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
211  tmp1.lengths, sizeof(int), sizeof(int),
212  tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
213  if(res < 0) {
214  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
215  err = res;
216  goto error;
217  }
218  } else {
219  av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
220  }
221  if(get_bits1(gb)){
222  smacker_decode_tree(gb, &tmp2, 0, 0);
223  skip_bits1(gb);
224  res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
225  tmp2.lengths, sizeof(int), sizeof(int),
226  tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
227  if(res < 0) {
228  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
229  err = res;
230  goto error;
231  }
232  } else {
233  av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
234  }
235 
236  escapes[0] = get_bits(gb, 8);
237  escapes[0] |= get_bits(gb, 8) << 8;
238  escapes[1] = get_bits(gb, 8);
239  escapes[1] |= get_bits(gb, 8) << 8;
240  escapes[2] = get_bits(gb, 8);
241  escapes[2] |= get_bits(gb, 8) << 8;
242 
243  last[0] = last[1] = last[2] = -1;
244 
245  ctx.escapes[0] = escapes[0];
246  ctx.escapes[1] = escapes[1];
247  ctx.escapes[2] = escapes[2];
248  ctx.v1 = &vlc[0];
249  ctx.v2 = &vlc[1];
250  ctx.recode1 = tmp1.values;
251  ctx.recode2 = tmp2.values;
252  ctx.last = last;
253 
254  huff.length = ((size + 3) >> 2) + 4;
255  huff.maxlength = 0;
256  huff.current = 0;
257  huff.values = av_mallocz(huff.length * sizeof(int));
258  if (!huff.values) {
259  err = AVERROR(ENOMEM);
260  goto error;
261  }
262 
263  if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
264  err = -1;
265  skip_bits1(gb);
266  if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
267  if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
268  if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
269  if (ctx.last[0] >= huff.length ||
270  ctx.last[1] >= huff.length ||
271  ctx.last[2] >= huff.length) {
272  av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
273  err = AVERROR_INVALIDDATA;
274  }
275 
276  *recodes = huff.values;
277 
278 error:
279  if(vlc[0].table)
280  ff_free_vlc(&vlc[0]);
281  if(vlc[1].table)
282  ff_free_vlc(&vlc[1]);
283  av_free(tmp1.bits);
284  av_free(tmp1.lengths);
285  av_free(tmp1.values);
286  av_free(tmp2.bits);
287  av_free(tmp2.lengths);
288  av_free(tmp2.values);
289 
290  return err;
291 }
292 
294  GetBitContext gb;
295  int mmap_size, mclr_size, full_size, type_size;
296 
297  mmap_size = AV_RL32(smk->avctx->extradata);
298  mclr_size = AV_RL32(smk->avctx->extradata + 4);
299  full_size = AV_RL32(smk->avctx->extradata + 8);
300  type_size = AV_RL32(smk->avctx->extradata + 12);
301 
302  init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
303 
304  if(!get_bits1(&gb)) {
305  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
306  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
307  if (!smk->mmap_tbl)
308  return AVERROR(ENOMEM);
309  smk->mmap_tbl[0] = 0;
310  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
311  } else {
312  if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
313  return -1;
314  }
315  if(!get_bits1(&gb)) {
316  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
317  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
318  if (!smk->mclr_tbl)
319  return AVERROR(ENOMEM);
320  smk->mclr_tbl[0] = 0;
321  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
322  } else {
323  if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
324  return -1;
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  if (!smk->full_tbl)
330  return AVERROR(ENOMEM);
331  smk->full_tbl[0] = 0;
332  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
333  } else {
334  if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
335  return -1;
336  }
337  if(!get_bits1(&gb)) {
338  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
339  smk->type_tbl = av_malloc(sizeof(int) * 2);
340  if (!smk->type_tbl)
341  return AVERROR(ENOMEM);
342  smk->type_tbl[0] = 0;
343  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
344  } else {
345  if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
346  return -1;
347  }
348 
349  return 0;
350 }
351 
352 static av_always_inline void last_reset(int *recode, int *last) {
353  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
354 }
355 
356 /* get code and update history */
357 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
358  register int *table = recode;
359  int v;
360 
361  while(*table & SMK_NODE) {
362  if(get_bits1(gb))
363  table += (*table) & (~SMK_NODE);
364  table++;
365  }
366  v = *table;
367 
368  if(v != recode[last[0]]) {
369  recode[last[2]] = recode[last[1]];
370  recode[last[1]] = recode[last[0]];
371  recode[last[0]] = v;
372  }
373  return v;
374 }
375 
376 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
377  AVPacket *avpkt)
378 {
379  SmackVContext * const smk = avctx->priv_data;
380  uint8_t *out;
381  uint32_t *pal;
382  GetByteContext gb2;
383  GetBitContext gb;
384  int blocks, blk, bw, bh;
385  int i, ret;
386  int stride;
387  int flags;
388 
389  if (avpkt->size <= 769)
390  return 0;
391 
392  if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0) {
393  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
394  return ret;
395  }
396 
397  /* make the palette available on the way out */
398  pal = (uint32_t*)smk->pic->data[1];
399  bytestream2_init(&gb2, avpkt->data, avpkt->size);
400  flags = bytestream2_get_byteu(&gb2);
401  smk->pic->palette_has_changed = flags & 1;
402  smk->pic->key_frame = !!(flags & 2);
403  if(smk->pic->key_frame)
405  else
407 
408  for(i = 0; i < 256; i++)
409  *pal++ = bytestream2_get_be24u(&gb2);
410 
411  last_reset(smk->mmap_tbl, smk->mmap_last);
412  last_reset(smk->mclr_tbl, smk->mclr_last);
413  last_reset(smk->full_tbl, smk->full_last);
414  last_reset(smk->type_tbl, smk->type_last);
415  init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
416 
417  blk = 0;
418  bw = avctx->width >> 2;
419  bh = avctx->height >> 2;
420  blocks = bw * bh;
421  out = smk->pic->data[0];
422  stride = smk->pic->linesize[0];
423  while(blk < blocks) {
424  int type, run, mode;
425  uint16_t pix;
426 
427  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
428  run = block_runs[(type >> 2) & 0x3F];
429  switch(type & 3){
430  case SMK_BLK_MONO:
431  while(run-- && blk < blocks){
432  int clr, map;
433  int hi, lo;
434  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
435  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
436  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
437  hi = clr >> 8;
438  lo = clr & 0xFF;
439  for(i = 0; i < 4; i++) {
440  if(map & 1) out[0] = hi; else out[0] = lo;
441  if(map & 2) out[1] = hi; else out[1] = lo;
442  if(map & 4) out[2] = hi; else out[2] = lo;
443  if(map & 8) out[3] = hi; else out[3] = lo;
444  map >>= 4;
445  out += stride;
446  }
447  blk++;
448  }
449  break;
450  case SMK_BLK_FULL:
451  mode = 0;
452  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
453  if(get_bits1(&gb)) mode = 1;
454  else if(get_bits1(&gb)) mode = 2;
455  }
456  while(run-- && blk < blocks){
457  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
458  switch(mode){
459  case 0:
460  for(i = 0; i < 4; i++) {
461  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
462  AV_WL16(out+2,pix);
463  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
464  AV_WL16(out,pix);
465  out += stride;
466  }
467  break;
468  case 1:
469  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
470  out[0] = out[1] = pix & 0xFF;
471  out[2] = out[3] = pix >> 8;
472  out += stride;
473  out[0] = out[1] = pix & 0xFF;
474  out[2] = out[3] = pix >> 8;
475  out += stride;
476  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
477  out[0] = out[1] = pix & 0xFF;
478  out[2] = out[3] = pix >> 8;
479  out += stride;
480  out[0] = out[1] = pix & 0xFF;
481  out[2] = out[3] = pix >> 8;
482  out += stride;
483  break;
484  case 2:
485  for(i = 0; i < 2; i++) {
486  uint16_t pix1, pix2;
487  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
488  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
489  AV_WL16(out,pix1);
490  AV_WL16(out+2,pix2);
491  out += stride;
492  AV_WL16(out,pix1);
493  AV_WL16(out+2,pix2);
494  out += stride;
495  }
496  break;
497  }
498  blk++;
499  }
500  break;
501  case SMK_BLK_SKIP:
502  while(run-- && blk < blocks)
503  blk++;
504  break;
505  case SMK_BLK_FILL:
506  mode = type >> 8;
507  while(run-- && blk < blocks){
508  uint32_t col;
509  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
510  col = mode * 0x01010101;
511  for(i = 0; i < 4; i++) {
512  *((uint32_t*)out) = col;
513  out += stride;
514  }
515  blk++;
516  }
517  break;
518  }
519 
520  }
521 
522  if ((ret = av_frame_ref(data, smk->pic)) < 0)
523  return ret;
524 
525  *got_frame = 1;
526 
527  /* always report that the buffer was completely consumed */
528  return avpkt->size;
529 }
530 
531 
533 {
534  SmackVContext * const smk = avctx->priv_data;
535 
536  av_freep(&smk->mmap_tbl);
537  av_freep(&smk->mclr_tbl);
538  av_freep(&smk->full_tbl);
539  av_freep(&smk->type_tbl);
540 
541  av_frame_free(&smk->pic);
542 
543  return 0;
544 }
545 
546 
548 {
549  SmackVContext * const c = avctx->priv_data;
550 
551  c->avctx = avctx;
552 
553  avctx->pix_fmt = AV_PIX_FMT_PAL8;
554 
555  c->pic = av_frame_alloc();
556  if (!c->pic)
557  return AVERROR(ENOMEM);
558 
559  /* decode huffman trees from extradata */
560  if(avctx->extradata_size < 16){
561  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
562  return -1;
563  }
564 
565  if (decode_header_trees(c)) {
566  decode_end(avctx);
567  return -1;
568  }
569 
570  return 0;
571 }
572 
573 
574 
576 {
577  if (avctx->channels < 1 || avctx->channels > 2) {
578  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
579  return AVERROR(EINVAL);
580  }
583 
584  return 0;
585 }
586 
591  int *got_frame_ptr, AVPacket *avpkt)
592 {
593  AVFrame *frame = data;
594  const uint8_t *buf = avpkt->data;
595  int buf_size = avpkt->size;
596  GetBitContext gb;
597  HuffContext h[4] = { { 0 } };
598  VLC vlc[4] = { { 0 } };
599  int16_t *samples;
600  uint8_t *samples8;
601  int val;
602  int i, res, ret;
603  int unp_size;
604  int bits, stereo;
605  int pred[2] = {0, 0};
606 
607  if (buf_size <= 4) {
608  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
609  return AVERROR(EINVAL);
610  }
611 
612  unp_size = AV_RL32(buf);
613 
614  init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
615 
616  if(!get_bits1(&gb)){
617  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
618  *got_frame_ptr = 0;
619  return 1;
620  }
621  stereo = get_bits1(&gb);
622  bits = get_bits1(&gb);
623  if (stereo ^ (avctx->channels != 1)) {
624  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
625  return AVERROR(EINVAL);
626  }
627  if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
628  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
629  return AVERROR(EINVAL);
630  }
631  if (unp_size % (avctx->channels * (bits + 1))) {
632  av_log(avctx, AV_LOG_ERROR,
633  "The buffer does not contain an integer number of samples\n");
634  return AVERROR(EINVAL);
635  }
636 
637  /* get output buffer */
638  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
639  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
640  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
641  return ret;
642  }
643  samples = (int16_t *)frame->data[0];
644  samples8 = frame->data[0];
645 
646  // Initialize
647  for(i = 0; i < (1 << (bits + stereo)); i++) {
648  h[i].length = 256;
649  h[i].maxlength = 0;
650  h[i].current = 0;
651  h[i].bits = av_mallocz(256 * 4);
652  h[i].lengths = av_mallocz(256 * sizeof(int));
653  h[i].values = av_mallocz(256 * sizeof(int));
654  if (!h[i].bits || !h[i].lengths || !h[i].values) {
655  ret = AVERROR(ENOMEM);
656  goto error;
657  }
658  skip_bits1(&gb);
659  if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
660  ret = AVERROR_INVALIDDATA;
661  goto error;
662  }
663  skip_bits1(&gb);
664  if(h[i].current > 1) {
665  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
666  h[i].lengths, sizeof(int), sizeof(int),
667  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
668  if(res < 0) {
669  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
670  ret = AVERROR_INVALIDDATA;
671  goto error;
672  }
673  }
674  }
675  /* this codec relies on wraparound instead of clipping audio */
676  if(bits) { //decode 16-bit data
677  for(i = stereo; i >= 0; i--)
678  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
679  for(i = 0; i <= stereo; i++)
680  *samples++ = pred[i];
681  for(; i < unp_size / 2; i++) {
682  if(i & stereo) {
683  if(vlc[2].table)
684  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
685  else
686  res = 0;
687  val = h[2].values[res];
688  if(vlc[3].table)
689  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
690  else
691  res = 0;
692  val |= h[3].values[res] << 8;
693  pred[1] += sign_extend(val, 16);
694  *samples++ = pred[1];
695  } else {
696  if(vlc[0].table)
697  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
698  else
699  res = 0;
700  val = h[0].values[res];
701  if(vlc[1].table)
702  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
703  else
704  res = 0;
705  val |= h[1].values[res] << 8;
706  pred[0] += sign_extend(val, 16);
707  *samples++ = pred[0];
708  }
709  }
710  } else { //8-bit data
711  for(i = stereo; i >= 0; i--)
712  pred[i] = get_bits(&gb, 8);
713  for(i = 0; i <= stereo; i++)
714  *samples8++ = pred[i];
715  for(; i < unp_size; i++) {
716  if(i & stereo){
717  if(vlc[1].table)
718  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
719  else
720  res = 0;
721  pred[1] += sign_extend(h[1].values[res], 8);
722  *samples8++ = pred[1];
723  } else {
724  if(vlc[0].table)
725  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
726  else
727  res = 0;
728  pred[0] += sign_extend(h[0].values[res], 8);
729  *samples8++ = pred[0];
730  }
731  }
732  }
733 
734  *got_frame_ptr = 1;
735  ret = buf_size;
736 
737 error:
738  for(i = 0; i < 4; i++) {
739  if(vlc[i].table)
740  ff_free_vlc(&vlc[i]);
741  av_free(h[i].bits);
742  av_free(h[i].lengths);
743  av_free(h[i].values);
744  }
745 
746  return ret;
747 }
748 
750  .name = "smackvid",
751  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
752  .type = AVMEDIA_TYPE_VIDEO,
754  .priv_data_size = sizeof(SmackVContext),
755  .init = decode_init,
756  .close = decode_end,
757  .decode = decode_frame,
758  .capabilities = AV_CODEC_CAP_DR1,
759 };
760 
762  .name = "smackaud",
763  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
764  .type = AVMEDIA_TYPE_AUDIO,
766  .init = smka_decode_init,
767  .decode = smka_decode_frame,
768  .capabilities = AV_CODEC_CAP_DR1,
769 };
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:376
int type_last[3]
Definition: smacker.c:52
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define SMK_NODE
Definition: smacker.c:44
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
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:228
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:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define av_bswap16
Definition: bswap.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
uint8_t run
Definition: svq3.c:203
#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:169
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:547
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int escapes[3]
Definition: smacker.c:71
VLC * v2
Definition: smacker.c:69
uint8_t bits
Definition: crc.c:252
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
#define av_cold
Definition: attributes.h:66
AV_SAMPLE_FMT_U8
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
VLC * v1
Definition: smacker.c:69
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:70
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:199
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:575
Definition: smacker.c:68
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
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:2769
#define AV_WL16(p, val)
Definition: intreadwrite.h:231
#define r
Definition: input.c:51
int * type_tbl
Definition: smacker.c:51
int full_last[3]
Definition: smacker.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVCodec ff_smacker_decoder
Definition: smacker.c:749
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#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:80
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:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
AVFrame * pic
Definition: smacker.c:49
int * mmap_tbl
Definition: smacker.c:51
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2203
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:590
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:672
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int mmap_last[3]
Definition: smacker.c:52
int width
picture width / height.
Definition: avcodec.h:1580
Context used for code reconstructing.
Definition: smacker.c:58
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
AVFormatContext * ctx
Definition: movenc.c:48
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:532
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:493
#define AV_RL32
Definition: intreadwrite.h:146
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 Libav&#39;s vlc codes.
Definition: smacker.c:173
static const float pred[4]
Definition: siprdata.h:259
int * full_tbl
Definition: smacker.c:51
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
Libavcodec external API header.
AVCodec ff_smackaud_decoder
Definition: smacker.c:761
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
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:1441
int extradata_size
Definition: avcodec.h:1524
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:292
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
Decode header tree.
Definition: smacker.c:128
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:273
const VDPAUPixFmtMap * map
int * last
Definition: smacker.c:72
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:118
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
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:63
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition: smacker.c:357
#define SMKTREE_BITS
Definition: smacker.c:43
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
int channels
number of audio channels
Definition: avcodec.h:2153
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
int mclr_last[3]
Definition: smacker.c:52
int * values
Definition: smacker.c:64
FILE * out
Definition: movenc.c:54
#define av_always_inline
Definition: attributes.h:40
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:293
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:352
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:256
This structure stores compressed data.
Definition: avcodec.h:1323
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:334
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
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:211
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
Predicted.
Definition: avutil.h:261