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 #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 #define SMKTREE_DECODE_MAX_RECURSION 32
46 #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
47 
48 /*
49  * Decoder context
50  */
51 typedef struct SmackVContext {
54 
56  int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
58 
62 typedef struct HuffContext {
63  int length;
64  int maxlength;
65  int current;
66  uint32_t *bits;
67  int *lengths;
68  int *values;
69 } HuffContext;
70 
71 /* common parameters used for decode_bigtree */
72 typedef struct DBCtx {
73  VLC *v1, *v2;
74  int *recode1, *recode2;
75  int escapes[3];
76  int *last;
77  int lcur;
78 } DBCtx;
79 
80 /* possible runs of blocks */
81 static const int block_runs[64] = {
82  1, 2, 3, 4, 5, 6, 7, 8,
83  9, 10, 11, 12, 13, 14, 15, 16,
84  17, 18, 19, 20, 21, 22, 23, 24,
85  25, 26, 27, 28, 29, 30, 31, 32,
86  33, 34, 35, 36, 37, 38, 39, 40,
87  41, 42, 43, 44, 45, 46, 47, 48,
88  49, 50, 51, 52, 53, 54, 55, 56,
89  57, 58, 59, 128, 256, 512, 1024, 2048 };
90 
95  SMK_BLK_FILL = 3 };
96 
100 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
101 {
102  if (length > SMKTREE_DECODE_MAX_RECURSION) {
103  av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
104  return AVERROR_INVALIDDATA;
105  }
106 
107  if(!get_bits1(gb)){ //Leaf
108  if(hc->current >= 256){
109  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
110  return -1;
111  }
112  if(length){
113  hc->bits[hc->current] = prefix;
114  hc->lengths[hc->current] = length;
115  } else {
116  hc->bits[hc->current] = 0;
117  hc->lengths[hc->current] = 0;
118  }
119  hc->values[hc->current] = get_bits(gb, 8);
120  hc->current++;
121  if(hc->maxlength < length)
122  hc->maxlength = length;
123  return 0;
124  } else { //Node
125  int r;
126  length++;
127  r = smacker_decode_tree(gb, hc, prefix, length);
128  if(r)
129  return r;
130  return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
131  }
132 }
133 
138  DBCtx *ctx, int length)
139 {
140  // Larger length can cause segmentation faults due to too deep recursion.
141  if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
142  av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
143  return AVERROR_INVALIDDATA;
144  }
145 
146  if (hc->current + 1 >= hc->length) {
147  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
148  return -1;
149  }
150  if(!get_bits1(gb)){ //Leaf
151  int val, i1, i2;
152  i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
153  i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
154  if (i1 < 0 || i2 < 0)
155  return -1;
156  val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
157  if(val == ctx->escapes[0]) {
158  ctx->last[0] = hc->current;
159  val = 0;
160  } else if(val == ctx->escapes[1]) {
161  ctx->last[1] = hc->current;
162  val = 0;
163  } else if(val == ctx->escapes[2]) {
164  ctx->last[2] = hc->current;
165  val = 0;
166  }
167 
168  hc->values[hc->current++] = val;
169  return 1;
170  } else { //Node
171  int r = 0, r_new, t;
172 
173  t = hc->current++;
174  r = smacker_decode_bigtree(gb, hc, ctx, length + 1);
175  if(r < 0)
176  return r;
177  hc->values[t] = SMK_NODE | r;
178  r++;
179  r_new = smacker_decode_bigtree(gb, hc, ctx, length + 1);
180  if (r_new < 0)
181  return r_new;
182  return r + r_new;
183  }
184 }
185 
189 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
190 {
191  int res;
192  HuffContext huff;
193  HuffContext tmp1, tmp2;
194  VLC vlc[2] = { { 0 } };
195  int escapes[3];
196  DBCtx ctx;
197  int err = 0;
198 
199  if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
200  av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
201  return -1;
202  }
203 
204  tmp1.length = 256;
205  tmp1.maxlength = 0;
206  tmp1.current = 0;
207  tmp1.bits = av_mallocz(256 * 4);
208  tmp1.lengths = av_mallocz(256 * sizeof(int));
209  tmp1.values = av_mallocz(256 * sizeof(int));
210 
211  tmp2.length = 256;
212  tmp2.maxlength = 0;
213  tmp2.current = 0;
214  tmp2.bits = av_mallocz(256 * 4);
215  tmp2.lengths = av_mallocz(256 * sizeof(int));
216  tmp2.values = av_mallocz(256 * sizeof(int));
217  if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
218  !tmp2.bits || !tmp2.lengths || !tmp2.values) {
219  err = AVERROR(ENOMEM);
220  goto error;
221  }
222 
223  if(get_bits1(gb)) {
224  smacker_decode_tree(gb, &tmp1, 0, 0);
225  skip_bits1(gb);
226  res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
227  tmp1.lengths, sizeof(int), sizeof(int),
228  tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
229  if(res < 0) {
230  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
231  err = res;
232  goto error;
233  }
234  } else {
235  av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
236  }
237  if(get_bits1(gb)){
238  smacker_decode_tree(gb, &tmp2, 0, 0);
239  skip_bits1(gb);
240  res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
241  tmp2.lengths, sizeof(int), sizeof(int),
242  tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
243  if(res < 0) {
244  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
245  err = res;
246  goto error;
247  }
248  } else {
249  av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
250  }
251 
252  escapes[0] = get_bits(gb, 8);
253  escapes[0] |= get_bits(gb, 8) << 8;
254  escapes[1] = get_bits(gb, 8);
255  escapes[1] |= get_bits(gb, 8) << 8;
256  escapes[2] = get_bits(gb, 8);
257  escapes[2] |= get_bits(gb, 8) << 8;
258 
259  last[0] = last[1] = last[2] = -1;
260 
261  ctx.escapes[0] = escapes[0];
262  ctx.escapes[1] = escapes[1];
263  ctx.escapes[2] = escapes[2];
264  ctx.v1 = &vlc[0];
265  ctx.v2 = &vlc[1];
266  ctx.recode1 = tmp1.values;
267  ctx.recode2 = tmp2.values;
268  ctx.last = last;
269 
270  huff.length = ((size + 3) >> 2) + 4;
271  huff.maxlength = 0;
272  huff.current = 0;
273  huff.values = av_mallocz(huff.length * sizeof(int));
274  if (!huff.values) {
275  err = AVERROR(ENOMEM);
276  goto error;
277  }
278 
279  if (smacker_decode_bigtree(gb, &huff, &ctx, 0) < 0)
280  err = -1;
281  skip_bits1(gb);
282  if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
283  if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
284  if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
285  if (ctx.last[0] >= huff.length ||
286  ctx.last[1] >= huff.length ||
287  ctx.last[2] >= huff.length) {
288  av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
289  err = AVERROR_INVALIDDATA;
290  }
291 
292  *recodes = huff.values;
293 
294 error:
295  if(vlc[0].table)
296  ff_free_vlc(&vlc[0]);
297  if(vlc[1].table)
298  ff_free_vlc(&vlc[1]);
299  av_free(tmp1.bits);
300  av_free(tmp1.lengths);
301  av_free(tmp1.values);
302  av_free(tmp2.bits);
303  av_free(tmp2.lengths);
304  av_free(tmp2.values);
305 
306  return err;
307 }
308 
310  GetBitContext gb;
311  int mmap_size, mclr_size, full_size, type_size;
312 
313  mmap_size = AV_RL32(smk->avctx->extradata);
314  mclr_size = AV_RL32(smk->avctx->extradata + 4);
315  full_size = AV_RL32(smk->avctx->extradata + 8);
316  type_size = AV_RL32(smk->avctx->extradata + 12);
317 
318  init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
319 
320  if(!get_bits1(&gb)) {
321  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
322  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
323  if (!smk->mmap_tbl)
324  return AVERROR(ENOMEM);
325  smk->mmap_tbl[0] = 0;
326  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
327  } else {
328  if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
329  return -1;
330  }
331  if(!get_bits1(&gb)) {
332  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
333  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
334  if (!smk->mclr_tbl)
335  return AVERROR(ENOMEM);
336  smk->mclr_tbl[0] = 0;
337  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
338  } else {
339  if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
340  return -1;
341  }
342  if(!get_bits1(&gb)) {
343  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
344  smk->full_tbl = av_malloc(sizeof(int) * 2);
345  if (!smk->full_tbl)
346  return AVERROR(ENOMEM);
347  smk->full_tbl[0] = 0;
348  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
349  } else {
350  if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
351  return -1;
352  }
353  if(!get_bits1(&gb)) {
354  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
355  smk->type_tbl = av_malloc(sizeof(int) * 2);
356  if (!smk->type_tbl)
357  return AVERROR(ENOMEM);
358  smk->type_tbl[0] = 0;
359  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
360  } else {
361  if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
362  return -1;
363  }
364 
365  return 0;
366 }
367 
368 static av_always_inline void last_reset(int *recode, int *last) {
369  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
370 }
371 
372 /* get code and update history */
373 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
374  register int *table = recode;
375  int v;
376 
377  while(*table & SMK_NODE) {
378  if(get_bits1(gb))
379  table += (*table) & (~SMK_NODE);
380  table++;
381  }
382  v = *table;
383 
384  if(v != recode[last[0]]) {
385  recode[last[2]] = recode[last[1]];
386  recode[last[1]] = recode[last[0]];
387  recode[last[0]] = v;
388  }
389  return v;
390 }
391 
392 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
393  AVPacket *avpkt)
394 {
395  SmackVContext * const smk = avctx->priv_data;
396  uint8_t *out;
397  uint32_t *pal;
398  GetByteContext gb2;
399  GetBitContext gb;
400  int blocks, blk, bw, bh;
401  int i, ret;
402  int stride;
403  int flags;
404 
405  if (avpkt->size <= 769)
406  return 0;
407 
408  if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0) {
409  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
410  return ret;
411  }
412 
413  /* make the palette available on the way out */
414  pal = (uint32_t*)smk->pic->data[1];
415  bytestream2_init(&gb2, avpkt->data, avpkt->size);
416  flags = bytestream2_get_byteu(&gb2);
417  smk->pic->palette_has_changed = flags & 1;
418  smk->pic->key_frame = !!(flags & 2);
419  if(smk->pic->key_frame)
421  else
423 
424  for(i = 0; i < 256; i++)
425  *pal++ = bytestream2_get_be24u(&gb2);
426 
427  last_reset(smk->mmap_tbl, smk->mmap_last);
428  last_reset(smk->mclr_tbl, smk->mclr_last);
429  last_reset(smk->full_tbl, smk->full_last);
430  last_reset(smk->type_tbl, smk->type_last);
431  init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
432 
433  blk = 0;
434  bw = avctx->width >> 2;
435  bh = avctx->height >> 2;
436  blocks = bw * bh;
437  out = smk->pic->data[0];
438  stride = smk->pic->linesize[0];
439  while(blk < blocks) {
440  int type, run, mode;
441  uint16_t pix;
442 
443  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
444  run = block_runs[(type >> 2) & 0x3F];
445  switch(type & 3){
446  case SMK_BLK_MONO:
447  while(run-- && blk < blocks){
448  int clr, map;
449  int hi, lo;
450  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
451  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
452  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
453  hi = clr >> 8;
454  lo = clr & 0xFF;
455  for(i = 0; i < 4; i++) {
456  if(map & 1) out[0] = hi; else out[0] = lo;
457  if(map & 2) out[1] = hi; else out[1] = lo;
458  if(map & 4) out[2] = hi; else out[2] = lo;
459  if(map & 8) out[3] = hi; else out[3] = lo;
460  map >>= 4;
461  out += stride;
462  }
463  blk++;
464  }
465  break;
466  case SMK_BLK_FULL:
467  mode = 0;
468  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
469  if(get_bits1(&gb)) mode = 1;
470  else if(get_bits1(&gb)) mode = 2;
471  }
472  while(run-- && blk < blocks){
473  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
474  switch(mode){
475  case 0:
476  for(i = 0; i < 4; i++) {
477  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
478  AV_WL16(out+2,pix);
479  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
480  AV_WL16(out,pix);
481  out += stride;
482  }
483  break;
484  case 1:
485  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
486  out[0] = out[1] = pix & 0xFF;
487  out[2] = out[3] = pix >> 8;
488  out += stride;
489  out[0] = out[1] = pix & 0xFF;
490  out[2] = out[3] = pix >> 8;
491  out += stride;
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  break;
500  case 2:
501  for(i = 0; i < 2; i++) {
502  uint16_t pix1, pix2;
503  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
504  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
505  AV_WL16(out,pix1);
506  AV_WL16(out+2,pix2);
507  out += stride;
508  AV_WL16(out,pix1);
509  AV_WL16(out+2,pix2);
510  out += stride;
511  }
512  break;
513  }
514  blk++;
515  }
516  break;
517  case SMK_BLK_SKIP:
518  while(run-- && blk < blocks)
519  blk++;
520  break;
521  case SMK_BLK_FILL:
522  mode = type >> 8;
523  while(run-- && blk < blocks){
524  uint32_t col;
525  out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
526  col = mode * 0x01010101;
527  for(i = 0; i < 4; i++) {
528  *((uint32_t*)out) = col;
529  out += stride;
530  }
531  blk++;
532  }
533  break;
534  }
535 
536  }
537 
538  if ((ret = av_frame_ref(data, smk->pic)) < 0)
539  return ret;
540 
541  *got_frame = 1;
542 
543  /* always report that the buffer was completely consumed */
544  return avpkt->size;
545 }
546 
547 
548 
549 /*
550  *
551  * Uninit smacker decoder
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 
569 /*
570  *
571  * Init smacker decoder
572  *
573  */
575 {
576  SmackVContext * const c = avctx->priv_data;
577 
578  c->avctx = avctx;
579 
580  avctx->pix_fmt = AV_PIX_FMT_PAL8;
581 
582  c->pic = av_frame_alloc();
583  if (!c->pic)
584  return AVERROR(ENOMEM);
585 
586  /* decode huffman trees from extradata */
587  if(avctx->extradata_size < 16){
588  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
589  return -1;
590  }
591 
592  if (decode_header_trees(c)) {
593  decode_end(avctx);
594  return -1;
595  }
596 
597  return 0;
598 }
599 
600 
601 
603 {
604  if (avctx->channels < 1 || avctx->channels > 2) {
605  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
606  return AVERROR(EINVAL);
607  }
610 
611  return 0;
612 }
613 
617 static int smka_decode_frame(AVCodecContext *avctx, void *data,
618  int *got_frame_ptr, AVPacket *avpkt)
619 {
620  AVFrame *frame = data;
621  const uint8_t *buf = avpkt->data;
622  int buf_size = avpkt->size;
623  GetBitContext gb;
624  HuffContext h[4] = { { 0 } };
625  VLC vlc[4] = { { 0 } };
626  int16_t *samples;
627  uint8_t *samples8;
628  int val;
629  int i, res, ret;
630  int unp_size;
631  int bits, stereo;
632  int pred[2] = {0, 0};
633 
634  if (buf_size <= 4) {
635  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
636  return AVERROR(EINVAL);
637  }
638 
639  unp_size = AV_RL32(buf);
640 
641  init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
642 
643  if(!get_bits1(&gb)){
644  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
645  *got_frame_ptr = 0;
646  return 1;
647  }
648  stereo = get_bits1(&gb);
649  bits = get_bits1(&gb);
650  if (stereo ^ (avctx->channels != 1)) {
651  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
652  return AVERROR(EINVAL);
653  }
654  if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
655  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
656  return AVERROR(EINVAL);
657  }
658  if (unp_size % (avctx->channels * (bits + 1))) {
659  av_log(avctx, AV_LOG_ERROR,
660  "The buffer does not contain an integer number of samples\n");
661  return AVERROR(EINVAL);
662  }
663 
664  /* get output buffer */
665  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
666  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
667  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
668  return ret;
669  }
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(i & stereo) {
710  if(vlc[2].table)
711  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
712  else
713  res = 0;
714  val = h[2].values[res];
715  if(vlc[3].table)
716  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
717  else
718  res = 0;
719  val |= h[3].values[res] << 8;
720  pred[1] += sign_extend(val, 16);
721  *samples++ = pred[1];
722  } else {
723  if(vlc[0].table)
724  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
725  else
726  res = 0;
727  val = h[0].values[res];
728  if(vlc[1].table)
729  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
730  else
731  res = 0;
732  val |= h[1].values[res] << 8;
733  pred[0] += sign_extend(val, 16);
734  *samples++ = pred[0];
735  }
736  }
737  } else { //8-bit data
738  for(i = stereo; i >= 0; i--)
739  pred[i] = get_bits(&gb, 8);
740  for(i = 0; i <= stereo; i++)
741  *samples8++ = pred[i];
742  for(; i < unp_size; i++) {
743  if(i & stereo){
744  if(vlc[1].table)
745  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
746  else
747  res = 0;
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  pred[0] += sign_extend(h[0].values[res], 8);
756  *samples8++ = pred[0];
757  }
758  }
759  }
760 
761  *got_frame_ptr = 1;
762  ret = buf_size;
763 
764 error:
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  return ret;
774 }
775 
777  .name = "smackvid",
778  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
779  .type = AVMEDIA_TYPE_VIDEO,
781  .priv_data_size = sizeof(SmackVContext),
782  .init = decode_init,
783  .close = decode_end,
784  .decode = decode_frame,
785  .capabilities = CODEC_CAP_DR1,
786 };
787 
789  .name = "smackaud",
790  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
791  .type = AVMEDIA_TYPE_AUDIO,
793  .init = smka_decode_init,
794  .decode = smka_decode_frame,
795  .capabilities = CODEC_CAP_DR1,
796 };
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx, int length)
Decode header tree.
Definition: smacker.c:137
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:392
int type_last[3]
Definition: smacker.c:56
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:135
int lcur
Definition: smacker.c:77
static const int block_runs[64]
Definition: smacker.c:81
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
int current
Definition: smacker.c:65
int length
Definition: smacker.c:63
int * recode1
Definition: smacker.c:74
int * recode2
Definition: smacker.c:74
int size
Definition: avcodec.h:974
#define av_bswap16
Definition: bswap.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
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:146
#define AV_CH_LAYOUT_STEREO
#define blk(i)
Definition: sha.c:173
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:574
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2812
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:198
int escapes[3]
Definition: smacker.c:75
VLC * v2
Definition: smacker.c:73
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t bits
Definition: crc.c:251
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1815
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:57
VLC * v1
Definition: smacker.c:73
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
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:188
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:602
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
Definition: smacker.c:72
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
int maxlength
Definition: smacker.c:64
bitstream reader API header.
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition: smacker.c:46
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2523
#define r
Definition: input.c:51
int * type_tbl
Definition: smacker.c:55
int full_last[3]
Definition: smacker.c:56
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVCodec ff_smacker_decoder
Definition: smacker.c:776
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define SMKTREE_DECODE_MAX_RECURSION
Definition: smacker.c:45
#define AVERROR(e)
Definition: error.h:43
SmkBlockTypes
Definition: smacker.c:91
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
Decode local frame tree.
Definition: smacker.c:100
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
AVFrame * pic
Definition: smacker.c:53
int * mmap_tbl
Definition: smacker.c:55
Definition: get_bits.h:64
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:617
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:829
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
int mmap_last[3]
Definition: smacker.c:56
int width
picture width / height.
Definition: avcodec.h:1229
Context used for code reconstructing.
Definition: smacker.c:62
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:522
#define AV_RL32
Definition: intreadwrite.h:146
int * mclr_tbl
Definition: smacker.c:55
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
Store large tree as Libav's vlc codes.
Definition: smacker.c:189
static const float pred[4]
Definition: siprdata.h:259
int * full_tbl
Definition: smacker.c:55
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
Libavcodec external API header.
AVCodec ff_smackaud_decoder
Definition: smacker.c:788
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:621
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:424
int extradata_size
Definition: avcodec.h:1165
#define INIT_VLC_LE
Definition: get_bits.h:440
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:296
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:330
int * last
Definition: smacker.c:76
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:127
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int * lengths
Definition: smacker.c:67
AVCodecContext * avctx
Definition: smacker.c:52
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
common internal api header.
uint32_t * bits
Definition: smacker.c:66
signed 16 bits
Definition: samplefmt.h:64
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition: smacker.c:373
#define SMKTREE_BITS
Definition: smacker.c:43
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
int channels
number of audio channels
Definition: avcodec.h:1808
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:66
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
int mclr_last[3]
Definition: smacker.c:56
int * values
Definition: smacker.c:68
#define av_always_inline
Definition: attributes.h:40
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:309
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:368
#define AV_WL16(p, d)
Definition: intreadwrite.h:225
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:950
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:333
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
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:205
Predicted.
Definition: avutil.h:254