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 
204  if(get_bits1(gb)) {
205  smacker_decode_tree(gb, &tmp1, 0, 0);
206  skip_bits1(gb);
207  res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
208  tmp1.lengths, sizeof(int), sizeof(int),
209  tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
210  if(res < 0) {
211  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
212  return -1;
213  }
214  } else {
215  av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
216  }
217  if(get_bits1(gb)){
218  smacker_decode_tree(gb, &tmp2, 0, 0);
219  skip_bits1(gb);
220  res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
221  tmp2.lengths, sizeof(int), sizeof(int),
222  tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
223  if(res < 0) {
224  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
225  return -1;
226  }
227  } else {
228  av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
229  }
230 
231  escapes[0] = get_bits(gb, 8);
232  escapes[0] |= get_bits(gb, 8) << 8;
233  escapes[1] = get_bits(gb, 8);
234  escapes[1] |= get_bits(gb, 8) << 8;
235  escapes[2] = get_bits(gb, 8);
236  escapes[2] |= get_bits(gb, 8) << 8;
237 
238  last[0] = last[1] = last[2] = -1;
239 
240  ctx.escapes[0] = escapes[0];
241  ctx.escapes[1] = escapes[1];
242  ctx.escapes[2] = escapes[2];
243  ctx.v1 = &vlc[0];
244  ctx.v2 = &vlc[1];
245  ctx.recode1 = tmp1.values;
246  ctx.recode2 = tmp2.values;
247  ctx.last = last;
248 
249  huff.length = ((size + 3) >> 2) + 4;
250  huff.maxlength = 0;
251  huff.current = 0;
252  huff.values = av_mallocz(huff.length * sizeof(int));
253 
254  if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
255  err = -1;
256  skip_bits1(gb);
257  if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
258  if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
259  if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
260  if (ctx.last[0] >= huff.length ||
261  ctx.last[1] >= huff.length ||
262  ctx.last[2] >= huff.length) {
263  av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
264  err = AVERROR_INVALIDDATA;
265  }
266 
267  *recodes = huff.values;
268 
269  if(vlc[0].table)
270  ff_free_vlc(&vlc[0]);
271  if(vlc[1].table)
272  ff_free_vlc(&vlc[1]);
273  av_free(tmp1.bits);
274  av_free(tmp1.lengths);
275  av_free(tmp1.values);
276  av_free(tmp2.bits);
277  av_free(tmp2.lengths);
278  av_free(tmp2.values);
279 
280  return err;
281 }
282 
284  GetBitContext gb;
285  int mmap_size, mclr_size, full_size, type_size;
286 
287  mmap_size = AV_RL32(smk->avctx->extradata);
288  mclr_size = AV_RL32(smk->avctx->extradata + 4);
289  full_size = AV_RL32(smk->avctx->extradata + 8);
290  type_size = AV_RL32(smk->avctx->extradata + 12);
291 
292  init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
293 
294  if(!get_bits1(&gb)) {
295  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
296  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
297  smk->mmap_tbl[0] = 0;
298  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
299  } else {
300  if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
301  return -1;
302  }
303  if(!get_bits1(&gb)) {
304  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
305  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
306  smk->mclr_tbl[0] = 0;
307  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
308  } else {
309  if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
310  return -1;
311  }
312  if(!get_bits1(&gb)) {
313  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
314  smk->full_tbl = av_malloc(sizeof(int) * 2);
315  smk->full_tbl[0] = 0;
316  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
317  } else {
318  if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
319  return -1;
320  }
321  if(!get_bits1(&gb)) {
322  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
323  smk->type_tbl = av_malloc(sizeof(int) * 2);
324  smk->type_tbl[0] = 0;
325  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
326  } else {
327  if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
328  return -1;
329  }
330 
331  return 0;
332 }
333 
334 static av_always_inline void last_reset(int *recode, int *last) {
335  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
336 }
337 
338 /* get code and update history */
339 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
340  register int *table = recode;
341  int v;
342 
343  while(*table & SMK_NODE) {
344  if(get_bits1(gb))
345  table += (*table) & (~SMK_NODE);
346  table++;
347  }
348  v = *table;
349 
350  if(v != recode[last[0]]) {
351  recode[last[2]] = recode[last[1]];
352  recode[last[1]] = recode[last[0]];
353  recode[last[0]] = v;
354  }
355  return v;
356 }
357 
358 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
359  AVPacket *avpkt)
360 {
361  SmackVContext * const smk = avctx->priv_data;
362  uint8_t *out;
363  uint32_t *pal;
364  GetByteContext gb2;
365  GetBitContext gb;
366  int blocks, blk, bw, bh;
367  int i;
368  int stride;
369  int flags;
370 
371  if (avpkt->size <= 769)
372  return 0;
373 
374  smk->pic.reference = 1;
376  if(avctx->reget_buffer(avctx, &smk->pic) < 0){
377  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
378  return -1;
379  }
380 
381  /* make the palette available on the way out */
382  pal = (uint32_t*)smk->pic.data[1];
383  bytestream2_init(&gb2, avpkt->data, avpkt->size);
384  flags = bytestream2_get_byteu(&gb2);
385  smk->pic.palette_has_changed = flags & 1;
386  smk->pic.key_frame = !!(flags & 2);
387  if(smk->pic.key_frame)
389  else
391 
392  for(i = 0; i < 256; i++)
393  *pal++ = bytestream2_get_be24u(&gb2);
394 
395  last_reset(smk->mmap_tbl, smk->mmap_last);
396  last_reset(smk->mclr_tbl, smk->mclr_last);
397  last_reset(smk->full_tbl, smk->full_last);
398  last_reset(smk->type_tbl, smk->type_last);
399  init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
400 
401  blk = 0;
402  bw = avctx->width >> 2;
403  bh = avctx->height >> 2;
404  blocks = bw * bh;
405  out = smk->pic.data[0];
406  stride = smk->pic.linesize[0];
407  while(blk < blocks) {
408  int type, run, mode;
409  uint16_t pix;
410 
411  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
412  run = block_runs[(type >> 2) & 0x3F];
413  switch(type & 3){
414  case SMK_BLK_MONO:
415  while(run-- && blk < blocks){
416  int clr, map;
417  int hi, lo;
418  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
419  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
420  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
421  hi = clr >> 8;
422  lo = clr & 0xFF;
423  for(i = 0; i < 4; i++) {
424  if(map & 1) out[0] = hi; else out[0] = lo;
425  if(map & 2) out[1] = hi; else out[1] = lo;
426  if(map & 4) out[2] = hi; else out[2] = lo;
427  if(map & 8) out[3] = hi; else out[3] = lo;
428  map >>= 4;
429  out += stride;
430  }
431  blk++;
432  }
433  break;
434  case SMK_BLK_FULL:
435  mode = 0;
436  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
437  if(get_bits1(&gb)) mode = 1;
438  else if(get_bits1(&gb)) mode = 2;
439  }
440  while(run-- && blk < blocks){
441  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
442  switch(mode){
443  case 0:
444  for(i = 0; i < 4; i++) {
445  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
446  AV_WL16(out+2,pix);
447  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
448  AV_WL16(out,pix);
449  out += stride;
450  }
451  break;
452  case 1:
453  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
454  out[0] = out[1] = pix & 0xFF;
455  out[2] = out[3] = pix >> 8;
456  out += stride;
457  out[0] = out[1] = pix & 0xFF;
458  out[2] = out[3] = pix >> 8;
459  out += stride;
460  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
461  out[0] = out[1] = pix & 0xFF;
462  out[2] = out[3] = pix >> 8;
463  out += stride;
464  out[0] = out[1] = pix & 0xFF;
465  out[2] = out[3] = pix >> 8;
466  out += stride;
467  break;
468  case 2:
469  for(i = 0; i < 2; i++) {
470  uint16_t pix1, pix2;
471  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
472  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
473  AV_WL16(out,pix1);
474  AV_WL16(out+2,pix2);
475  out += stride;
476  AV_WL16(out,pix1);
477  AV_WL16(out+2,pix2);
478  out += stride;
479  }
480  break;
481  }
482  blk++;
483  }
484  break;
485  case SMK_BLK_SKIP:
486  while(run-- && blk < blocks)
487  blk++;
488  break;
489  case SMK_BLK_FILL:
490  mode = type >> 8;
491  while(run-- && blk < blocks){
492  uint32_t col;
493  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
494  col = mode * 0x01010101;
495  for(i = 0; i < 4; i++) {
496  *((uint32_t*)out) = col;
497  out += stride;
498  }
499  blk++;
500  }
501  break;
502  }
503 
504  }
505 
506  *got_frame = 1;
507  *(AVFrame*)data = smk->pic;
508 
509  /* always report that the buffer was completely consumed */
510  return avpkt->size;
511 }
512 
513 
514 
515 /*
516  *
517  * Init smacker decoder
518  *
519  */
521 {
522  SmackVContext * const c = avctx->priv_data;
523 
524  c->avctx = avctx;
525 
526  avctx->pix_fmt = AV_PIX_FMT_PAL8;
527 
528 
529  /* decode huffman trees from extradata */
530  if(avctx->extradata_size < 16){
531  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
532  return -1;
533  }
534 
535  if (decode_header_trees(c))
536  return -1;
537 
538  return 0;
539 }
540 
541 
542 
543 /*
544  *
545  * Uninit smacker decoder
546  *
547  */
549 {
550  SmackVContext * const smk = avctx->priv_data;
551 
552  av_freep(&smk->mmap_tbl);
553  av_freep(&smk->mclr_tbl);
554  av_freep(&smk->full_tbl);
555  av_freep(&smk->type_tbl);
556 
557  if (smk->pic.data[0])
558  avctx->release_buffer(avctx, &smk->pic);
559 
560  return 0;
561 }
562 
563 
564 typedef struct SmackerAudioContext {
567 
569 {
570  SmackerAudioContext *s = avctx->priv_data;
571 
572  if (avctx->channels < 1 || avctx->channels > 2) {
573  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
574  return AVERROR(EINVAL);
575  }
578 
580  avctx->coded_frame = &s->frame;
581 
582  return 0;
583 }
584 
588 static int smka_decode_frame(AVCodecContext *avctx, void *data,
589  int *got_frame_ptr, AVPacket *avpkt)
590 {
591  SmackerAudioContext *s = avctx->priv_data;
592  const uint8_t *buf = avpkt->data;
593  int buf_size = avpkt->size;
594  GetBitContext gb;
595  HuffContext h[4] = { { 0 } };
596  VLC vlc[4] = { { 0 } };
597  int16_t *samples;
598  uint8_t *samples8;
599  int val;
600  int i, res, ret;
601  int unp_size;
602  int bits, stereo;
603  int pred[2] = {0, 0};
604 
605  if (buf_size <= 4) {
606  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
607  return AVERROR(EINVAL);
608  }
609 
610  unp_size = AV_RL32(buf);
611 
612  init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
613 
614  if(!get_bits1(&gb)){
615  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
616  *got_frame_ptr = 0;
617  return 1;
618  }
619  stereo = get_bits1(&gb);
620  bits = get_bits1(&gb);
621  if (stereo ^ (avctx->channels != 1)) {
622  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
623  return AVERROR(EINVAL);
624  }
625  if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
626  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
627  return AVERROR(EINVAL);
628  }
629 
630  /* get output buffer */
631  s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));
632  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
633  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
634  return ret;
635  }
636  samples = (int16_t *)s->frame.data[0];
637  samples8 = s->frame.data[0];
638 
639  // Initialize
640  for(i = 0; i < (1 << (bits + stereo)); i++) {
641  h[i].length = 256;
642  h[i].maxlength = 0;
643  h[i].current = 0;
644  h[i].bits = av_mallocz(256 * 4);
645  h[i].lengths = av_mallocz(256 * sizeof(int));
646  h[i].values = av_mallocz(256 * sizeof(int));
647  skip_bits1(&gb);
648  if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
649  for (; i >= 0; i--) {
650  if (vlc[i].table)
651  ff_free_vlc(&vlc[i]);
652  av_free(h[i].bits);
653  av_free(h[i].lengths);
654  av_free(h[i].values);
655  }
656  return AVERROR_INVALIDDATA;
657  }
658  skip_bits1(&gb);
659  if(h[i].current > 1) {
660  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
661  h[i].lengths, sizeof(int), sizeof(int),
662  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
663  if(res < 0) {
664  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
665  return -1;
666  }
667  }
668  }
669  if(bits) { //decode 16-bit data
670  for(i = stereo; i >= 0; i--)
671  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
672  for(i = 0; i <= stereo; i++)
673  *samples++ = pred[i];
674  for(; i < unp_size / 2; i++) {
675  if(i & stereo) {
676  if(vlc[2].table)
677  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
678  else
679  res = 0;
680  val = h[2].values[res];
681  if(vlc[3].table)
682  res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
683  else
684  res = 0;
685  val |= h[3].values[res] << 8;
686  pred[1] += sign_extend(val, 16);
687  *samples++ = av_clip_int16(pred[1]);
688  } else {
689  if(vlc[0].table)
690  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
691  else
692  res = 0;
693  val = h[0].values[res];
694  if(vlc[1].table)
695  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
696  else
697  res = 0;
698  val |= h[1].values[res] << 8;
699  pred[0] += sign_extend(val, 16);
700  *samples++ = av_clip_int16(pred[0]);
701  }
702  }
703  } else { //8-bit data
704  for(i = stereo; i >= 0; i--)
705  pred[i] = get_bits(&gb, 8);
706  for(i = 0; i <= stereo; i++)
707  *samples8++ = pred[i];
708  for(; i < unp_size; i++) {
709  if(i & stereo){
710  if(vlc[1].table)
711  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
712  else
713  res = 0;
714  pred[1] += sign_extend(h[1].values[res], 8);
715  *samples8++ = av_clip_uint8(pred[1]);
716  } else {
717  if(vlc[0].table)
718  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
719  else
720  res = 0;
721  pred[0] += sign_extend(h[0].values[res], 8);
722  *samples8++ = av_clip_uint8(pred[0]);
723  }
724  }
725  }
726 
727  for(i = 0; i < 4; i++) {
728  if(vlc[i].table)
729  ff_free_vlc(&vlc[i]);
730  av_free(h[i].bits);
731  av_free(h[i].lengths);
732  av_free(h[i].values);
733  }
734 
735  *got_frame_ptr = 1;
736  *(AVFrame *)data = s->frame;
737 
738  return buf_size;
739 }
740 
742  .name = "smackvid",
743  .type = AVMEDIA_TYPE_VIDEO,
745  .priv_data_size = sizeof(SmackVContext),
746  .init = decode_init,
747  .close = decode_end,
748  .decode = decode_frame,
749  .capabilities = CODEC_CAP_DR1,
750  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
751 };
752 
754  .name = "smackaud",
755  .type = AVMEDIA_TYPE_AUDIO,
757  .priv_data_size = sizeof(SmackerAudioContext),
760  .capabilities = CODEC_CAP_DR1,
761  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
762 };
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: smacker.c:358
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:61
#define SMK_NODE
Definition: smacker.c:44
struct SmackerAudioContext SmackerAudioContext
static int16_t * samples
int size
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int lcur
Definition: smacker.c:75
static const int block_runs[64]
Definition: smacker.c:79
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1253
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
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:916
#define av_bswap16
Definition: bswap.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
struct DBCtx DBCtx
uint8_t run
Definition: svq3.c:124
#define AV_CH_LAYOUT_STEREO
signed 16 bits
Definition: samplefmt.h:52
#define blk(i)
Definition: sha.c:171
static av_cold int decode_init(AVCodecContext *avctx)
Definition: smacker.c:520
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2273
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:151
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:228
uint8_t bits
Definition: crc.c:31
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
uint8_t
AV_SAMPLE_FMT_U8
VLC * v1
Definition: smacker.c:71
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition: smacker.c:568
Definition: smacker.c:70
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int flags
Definition: log.c:42
int maxlength
Definition: smacker.c:62
bitstream reader API header.
struct HuffContext HuffContext
Context used for code reconstructing.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2704
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define r
Definition: input.c:51
int * type_tbl
Definition: smacker.c:53
int full_last[3]
Definition: smacker.c:54
AVCodec ff_smacker_decoder
Definition: smacker.c:741
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
SmkBlockTypes
Definition: smacker.c:89
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:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int * mmap_tbl
Definition: smacker.c:53
Definition: get_bits.h:63
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2165
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition: smacker.c:588
AVFrame pic
Definition: smacker.c:51
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int mmap_last[3]
Definition: smacker.c:54
int width
picture width / height.
Definition: avcodec.h:1508
Context used for code reconstructing.
Definition: smacker.c:60
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static av_cold int decode_end(AVCodecContext *avctx)
Definition: smacker.c:548
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:515
#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:52
external API header
AVCodec ff_smackaud_decoder
Definition: smacker.c:753
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1365
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:418
int extradata_size
Definition: avcodec.h:1455
#define INIT_VLC_LE
Definition: get_bits.h:432
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:293
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
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:372
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
int * last
Definition: smacker.c:74
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:123
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int * lengths
Definition: smacker.c:65
AVCodecContext * avctx
Definition: smacker.c:50
common internal api header.
uint32_t * bits
Definition: smacker.c:64
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition: smacker.c:339
#define SMKTREE_BITS
Definition: smacker.c:43
void * priv_data
Definition: avcodec.h:1382
int channels
number of audio channels
Definition: avcodec.h:2105
struct SmackVContext SmackVContext
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
int mclr_last[3]
Definition: smacker.c:54
int * values
Definition: smacker.c:66
static int decode_header_trees(SmackVContext *smk)
Definition: smacker.c:283
static av_always_inline void last_reset(int *recode, int *last)
Definition: smacker.c:334
#define AV_WL16(p, d)
Definition: intreadwrite.h:225
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:898
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:322
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
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:158
Predicted.
Definition: avutil.h:246