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