libavcodec/smacker.c
Go to the documentation of this file.
1 /*
2  * Smacker decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Smacker decoder
25  */
26 
27 /*
28  * Based on http://wiki.multimedia.cx/index.php?title=Smacker
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "mathops.h"
38 
39 #define BITSTREAM_READER_LE
40 #include "get_bits.h"
41 #include "bytestream.h"
42 
43 #define SMKTREE_BITS 9
44 #define SMK_NODE 0x80000000
45 
46 /*
47  * Decoder context
48  */
49 typedef struct SmackVContext {
52 
54  int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
56 
57 /**
58  * Context used for code reconstructing
59  */
60 typedef struct HuffContext {
61  int length;
62  int maxlength;
63  int current;
64  uint32_t *bits;
65  int *lengths;
66  int *values;
67 } HuffContext;
68 
69 /* common parameters used for decode_bigtree */
70 typedef struct DBCtx {
71  VLC *v1, *v2;
72  int *recode1, *recode2;
73  int escapes[3];
74  int *last;
75  int lcur;
76 } DBCtx;
77 
78 /* possible runs of blocks */
79 static const int block_runs[64] = {
80  1, 2, 3, 4, 5, 6, 7, 8,
81  9, 10, 11, 12, 13, 14, 15, 16,
82  17, 18, 19, 20, 21, 22, 23, 24,
83  25, 26, 27, 28, 29, 30, 31, 32,
84  33, 34, 35, 36, 37, 38, 39, 40,
85  41, 42, 43, 44, 45, 46, 47, 48,
86  49, 50, 51, 52, 53, 54, 55, 56,
87  57, 58, 59, 128, 256, 512, 1024, 2048 };
88 
93  SMK_BLK_FILL = 3 };
94 
95 /**
96  * Decode local frame tree
97  */
98 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
99 {
100  if(length > 32 || length > 3*SMKTREE_BITS) {
101  av_log(NULL, AV_LOG_ERROR, "length too long\n");
102  return AVERROR_INVALIDDATA;
103  }
104  if(!get_bits1(gb)){ //Leaf
105  if(hc->current >= 256){
106  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
107  return AVERROR_INVALIDDATA;
108  }
109  if(length){
110  hc->bits[hc->current] = prefix;
111  hc->lengths[hc->current] = length;
112  } else {
113  hc->bits[hc->current] = 0;
114  hc->lengths[hc->current] = 0;
115  }
116  hc->values[hc->current] = get_bits(gb, 8);
117  hc->current++;
118  if(hc->maxlength < length)
119  hc->maxlength = length;
120  return 0;
121  } else { //Node
122  int r;
123  length++;
124  r = smacker_decode_tree(gb, hc, prefix, length);
125  if(r)
126  return r;
127  return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
128  }
129 }
130 
131 /**
132  * Decode header tree
133  */
135 {
136  if (hc->current + 1 >= hc->length) {
137  av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
138  return AVERROR_INVALIDDATA;
139  }
140  if(!get_bits1(gb)){ //Leaf
141  int val, i1, i2;
142  i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
143  i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
144  if (i1 < 0 || i2 < 0)
145  return AVERROR_INVALIDDATA;
146  val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
147  if(val == ctx->escapes[0]) {
148  ctx->last[0] = hc->current;
149  val = 0;
150  } else if(val == ctx->escapes[1]) {
151  ctx->last[1] = hc->current;
152  val = 0;
153  } else if(val == ctx->escapes[2]) {
154  ctx->last[2] = hc->current;
155  val = 0;
156  }
157 
158  hc->values[hc->current++] = val;
159  return 1;
160  } else { //Node
161  int r = 0, r_new, t;
162 
163  t = hc->current++;
164  r = smacker_decode_bigtree(gb, hc, ctx);
165  if(r < 0)
166  return r;
167  hc->values[t] = SMK_NODE | r;
168  r++;
169  r_new = smacker_decode_bigtree(gb, hc, ctx);
170  if (r_new < 0)
171  return r_new;
172  return r + r_new;
173  }
174 }
175 
176 /**
177  * Store large tree as FFmpeg's vlc codes
178  */
179 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
180 {
181  int res;
182  HuffContext huff;
183  HuffContext tmp1, tmp2;
184  VLC vlc[2] = { { 0 } };
185  int escapes[3];
186  DBCtx ctx;
187  int err = 0;
188 
189  if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
190  av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
191  return AVERROR_INVALIDDATA;
192  }
193 
194  tmp1.length = 256;
195  tmp1.maxlength = 0;
196  tmp1.current = 0;
197  tmp1.bits = av_mallocz(256 * 4);
198  tmp1.lengths = av_mallocz(256 * sizeof(int));
199  tmp1.values = av_mallocz(256 * sizeof(int));
200 
201  tmp2.length = 256;
202  tmp2.maxlength = 0;
203  tmp2.current = 0;
204  tmp2.bits = av_mallocz(256 * 4);
205  tmp2.lengths = av_mallocz(256 * sizeof(int));
206  tmp2.values = av_mallocz(256 * sizeof(int));
207 
208  if(get_bits1(gb)) {
209  res = smacker_decode_tree(gb, &tmp1, 0, 0);
210  if (res < 0)
211  return res;
212  skip_bits1(gb);
213  if(tmp1.current > 1) {
214  res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
215  tmp1.lengths, sizeof(int), sizeof(int),
216  tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
217  if(res < 0) {
218  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
219  return AVERROR_INVALIDDATA;
220  }
221  }
222  }
223  if (!vlc[0].table) {
224  av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
225  }
226  if(get_bits1(gb)){
227  res = smacker_decode_tree(gb, &tmp2, 0, 0);
228  if (res < 0)
229  return res;
230  skip_bits1(gb);
231  if(tmp2.current > 1) {
232  res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
233  tmp2.lengths, sizeof(int), sizeof(int),
234  tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
235  if(res < 0) {
236  av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
237  return AVERROR_INVALIDDATA;
238  }
239  }
240  }
241  if (!vlc[1].table) {
242  av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
243  }
244 
245  escapes[0] = get_bits(gb, 16);
246  escapes[1] = get_bits(gb, 16);
247  escapes[2] = get_bits(gb, 16);
248 
249  last[0] = last[1] = last[2] = -1;
250 
251  ctx.escapes[0] = escapes[0];
252  ctx.escapes[1] = escapes[1];
253  ctx.escapes[2] = escapes[2];
254  ctx.v1 = &vlc[0];
255  ctx.v2 = &vlc[1];
256  ctx.recode1 = tmp1.values;
257  ctx.recode2 = tmp2.values;
258  ctx.last = last;
259 
260  huff.length = ((size + 3) >> 2) + 3;
261  huff.maxlength = 0;
262  huff.current = 0;
263  huff.values = av_mallocz(huff.length * sizeof(int));
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(huff.current > huff.length){
272  ctx.last[0] = ctx.last[1] = ctx.last[2] = 1;
273  av_log(smk->avctx, AV_LOG_ERROR, "bigtree damaged\n");
274  return AVERROR_INVALIDDATA;
275  }
276 
277  *recodes = huff.values;
278 
279  if(vlc[0].table)
280  ff_free_vlc(&vlc[0]);
281  if(vlc[1].table)
282  ff_free_vlc(&vlc[1]);
283  av_free(tmp1.bits);
284  av_free(tmp1.lengths);
285  av_free(tmp1.values);
286  av_free(tmp2.bits);
287  av_free(tmp2.lengths);
288  av_free(tmp2.values);
289 
290  return err;
291 }
292 
294  GetBitContext gb;
295  int mmap_size, mclr_size, full_size, type_size;
296 
297  mmap_size = AV_RL32(smk->avctx->extradata);
298  mclr_size = AV_RL32(smk->avctx->extradata + 4);
299  full_size = AV_RL32(smk->avctx->extradata + 8);
300  type_size = AV_RL32(smk->avctx->extradata + 12);
301 
302  init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
303 
304  if(!get_bits1(&gb)) {
305  av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
306  smk->mmap_tbl = av_malloc(sizeof(int) * 2);
307  smk->mmap_tbl[0] = 0;
308  smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
309  } else {
310  if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
311  return AVERROR_INVALIDDATA;
312  }
313  if(!get_bits1(&gb)) {
314  av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
315  smk->mclr_tbl = av_malloc(sizeof(int) * 2);
316  smk->mclr_tbl[0] = 0;
317  smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
318  } else {
319  if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
320  return AVERROR_INVALIDDATA;
321  }
322  if(!get_bits1(&gb)) {
323  av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
324  smk->full_tbl = av_malloc(sizeof(int) * 2);
325  smk->full_tbl[0] = 0;
326  smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
327  } else {
328  if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
329  return AVERROR_INVALIDDATA;
330  }
331  if(!get_bits1(&gb)) {
332  av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
333  smk->type_tbl = av_malloc(sizeof(int) * 2);
334  smk->type_tbl[0] = 0;
335  smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
336  } else {
337  if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
338  return AVERROR_INVALIDDATA;
339  }
340 
341  return 0;
342 }
343 
344 static av_always_inline void last_reset(int *recode, int *last) {
345  recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
346 }
347 
348 /* get code and update history */
349 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
350  register int *table = recode;
351  int v;
352 
353  while(*table & SMK_NODE) {
354  if(get_bits1(gb))
355  table += (*table) & (~SMK_NODE);
356  table++;
357  }
358  v = *table;
359 
360  if(v != recode[last[0]]) {
361  recode[last[2]] = recode[last[1]];
362  recode[last[1]] = recode[last[0]];
363  recode[last[0]] = v;
364  }
365  return v;
366 }
367 
368 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
369  AVPacket *avpkt)
370 {
371  SmackVContext * const smk = avctx->priv_data;
372  uint8_t *out;
373  uint32_t *pal;
374  GetByteContext gb2;
375  GetBitContext gb;
376  int blocks, blk, bw, bh;
377  int i, ret;
378  int stride;
379  int flags;
380 
381  if (avpkt->size <= 769)
382  return AVERROR_INVALIDDATA;
383 
384  if ((ret = ff_reget_buffer(avctx, &smk->pic)) < 0)
385  return ret;
386 
387  /* make the palette available on the way out */
388  pal = (uint32_t*)smk->pic.data[1];
389  bytestream2_init(&gb2, avpkt->data, avpkt->size);
390  flags = bytestream2_get_byteu(&gb2);
391  smk->pic.palette_has_changed = flags & 1;
392  smk->pic.key_frame = !!(flags & 2);
393  if(smk->pic.key_frame)
395  else
397 
398  for(i = 0; i < 256; i++)
399  *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
400 
401  last_reset(smk->mmap_tbl, smk->mmap_last);
402  last_reset(smk->mclr_tbl, smk->mclr_last);
403  last_reset(smk->full_tbl, smk->full_last);
404  last_reset(smk->type_tbl, smk->type_last);
405  init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
406 
407  blk = 0;
408  bw = avctx->width >> 2;
409  bh = avctx->height >> 2;
410  blocks = bw * bh;
411  out = smk->pic.data[0];
412  stride = smk->pic.linesize[0];
413  while(blk < blocks) {
414  int type, run, mode;
415  uint16_t pix;
416 
417  type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
418  run = block_runs[(type >> 2) & 0x3F];
419  switch(type & 3){
420  case SMK_BLK_MONO:
421  while(run-- && blk < blocks){
422  int clr, map;
423  int hi, lo;
424  clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
425  map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
426  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
427  hi = clr >> 8;
428  lo = clr & 0xFF;
429  for(i = 0; i < 4; i++) {
430  if(map & 1) out[0] = hi; else out[0] = lo;
431  if(map & 2) out[1] = hi; else out[1] = lo;
432  if(map & 4) out[2] = hi; else out[2] = lo;
433  if(map & 8) out[3] = hi; else out[3] = lo;
434  map >>= 4;
435  out += stride;
436  }
437  blk++;
438  }
439  break;
440  case SMK_BLK_FULL:
441  mode = 0;
442  if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
443  if(get_bits1(&gb)) mode = 1;
444  else if(get_bits1(&gb)) mode = 2;
445  }
446  while(run-- && blk < blocks){
447  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
448  switch(mode){
449  case 0:
450  for(i = 0; i < 4; i++) {
451  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
452  AV_WL16(out+2,pix);
453  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
454  AV_WL16(out,pix);
455  out += stride;
456  }
457  break;
458  case 1:
459  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
460  out[0] = out[1] = pix & 0xFF;
461  out[2] = out[3] = pix >> 8;
462  out += stride;
463  out[0] = out[1] = pix & 0xFF;
464  out[2] = out[3] = pix >> 8;
465  out += stride;
466  pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
467  out[0] = out[1] = pix & 0xFF;
468  out[2] = out[3] = pix >> 8;
469  out += stride;
470  out[0] = out[1] = pix & 0xFF;
471  out[2] = out[3] = pix >> 8;
472  out += stride;
473  break;
474  case 2:
475  for(i = 0; i < 2; i++) {
476  uint16_t pix1, pix2;
477  pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
478  pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
479  AV_WL16(out,pix1);
480  AV_WL16(out+2,pix2);
481  out += stride;
482  AV_WL16(out,pix1);
483  AV_WL16(out+2,pix2);
484  out += stride;
485  }
486  break;
487  }
488  blk++;
489  }
490  break;
491  case SMK_BLK_SKIP:
492  while(run-- && blk < blocks)
493  blk++;
494  break;
495  case SMK_BLK_FILL:
496  mode = type >> 8;
497  while(run-- && blk < blocks){
498  uint32_t col;
499  out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
500  col = mode * 0x01010101;
501  for(i = 0; i < 4; i++) {
502  *((uint32_t*)out) = col;
503  out += stride;
504  }
505  blk++;
506  }
507  break;
508  }
509 
510  }
511 
512  if ((ret = av_frame_ref(data, &smk->pic)) < 0)
513  return ret;
514 
515  *got_frame = 1;
516 
517  /* always report that the buffer was completely consumed */
518  return avpkt->size;
519 }
520 
521 
522 
523 /*
524  *
525  * Init smacker decoder
526  *
527  */
529 {
530  SmackVContext * const c = avctx->priv_data;
531 
532  c->avctx = avctx;
533 
534  avctx->pix_fmt = AV_PIX_FMT_PAL8;
536 
537  /* decode huffman trees from extradata */
538  if(avctx->extradata_size < 16){
539  av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
540  return AVERROR(EINVAL);
541  }
542 
543  if (decode_header_trees(c))
544  return AVERROR_INVALIDDATA;
545 
546  return 0;
547 }
548 
549 
550 
551 /*
552  *
553  * Uninit smacker decoder
554  *
555  */
557 {
558  SmackVContext * const smk = avctx->priv_data;
559 
560  av_freep(&smk->mmap_tbl);
561  av_freep(&smk->mclr_tbl);
562  av_freep(&smk->full_tbl);
563  av_freep(&smk->type_tbl);
564 
565  av_frame_unref(&smk->pic);
566 
567  return 0;
568 }
569 
570 
572 {
573  if (avctx->channels < 1 || avctx->channels > 2) {
574  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
575  return AVERROR(EINVAL);
576  }
579 
580  return 0;
581 }
582 
583 /**
584  * Decode Smacker audio data
585  */
587  int *got_frame_ptr, AVPacket *avpkt)
588 {
589  AVFrame *frame = data;
590  const uint8_t *buf = avpkt->data;
591  int buf_size = avpkt->size;
592  GetBitContext gb;
593  HuffContext h[4] = { { 0 } };
594  VLC vlc[4] = { { 0 } };
595  int16_t *samples;
596  uint8_t *samples8;
597  int val;
598  int i, res, ret;
599  int unp_size;
600  int bits, stereo;
601  int pred[2] = {0, 0};
602 
603  if (buf_size <= 4) {
604  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
605  return AVERROR(EINVAL);
606  }
607 
608  unp_size = AV_RL32(buf);
609 
610  if (unp_size > (1U<<24)) {
611  av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
612  return AVERROR_INVALIDDATA;
613  }
614 
615  init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
616 
617  if(!get_bits1(&gb)){
618  av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
619  *got_frame_ptr = 0;
620  return 1;
621  }
622  stereo = get_bits1(&gb);
623  bits = get_bits1(&gb);
624  if (stereo ^ (avctx->channels != 1)) {
625  av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
626  return AVERROR(EINVAL);
627  }
628  if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
629  av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
630  return AVERROR(EINVAL);
631  }
632 
633  /* get output buffer */
634  frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
635  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
636  return ret;
637  samples = (int16_t *)frame->data[0];
638  samples8 = frame->data[0];
639 
640  // Initialize
641  for(i = 0; i < (1 << (bits + stereo)); i++) {
642  h[i].length = 256;
643  h[i].maxlength = 0;
644  h[i].current = 0;
645  h[i].bits = av_mallocz(256 * 4);
646  h[i].lengths = av_mallocz(256 * sizeof(int));
647  h[i].values = av_mallocz(256 * sizeof(int));
648  skip_bits1(&gb);
649  res = smacker_decode_tree(&gb, &h[i], 0, 0);
650  if (res < 0)
651  return res;
652  skip_bits1(&gb);
653  if(h[i].current > 1) {
654  res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
655  h[i].lengths, sizeof(int), sizeof(int),
656  h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
657  if(res < 0) {
658  av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
659  return AVERROR_INVALIDDATA;
660  }
661  }
662  }
663  if(bits) { //decode 16-bit data
664  for(i = stereo; i >= 0; i--)
665  pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
666  for(i = 0; i <= stereo; i++)
667  *samples++ = pred[i];
668  for(; i < unp_size / 2; i++) {
669  if(get_bits_left(&gb)<0)
670  return AVERROR_INVALIDDATA;
671  if(i & stereo) {
672  if(vlc[2].table)
673  res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
674  else
675  res = 0;
676  if (res < 0) {
677  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
678  return AVERROR_INVALIDDATA;
679  }
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  if (res < 0) {
686  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
687  return AVERROR_INVALIDDATA;
688  }
689  val |= h[3].values[res] << 8;
690  pred[1] += sign_extend(val, 16);
691  *samples++ = av_clip_int16(pred[1]);
692  } else {
693  if(vlc[0].table)
694  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
695  else
696  res = 0;
697  if (res < 0) {
698  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
699  return AVERROR_INVALIDDATA;
700  }
701  val = h[0].values[res];
702  if(vlc[1].table)
703  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
704  else
705  res = 0;
706  if (res < 0) {
707  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
708  return AVERROR_INVALIDDATA;
709  }
710  val |= h[1].values[res] << 8;
711  pred[0] += sign_extend(val, 16);
712  *samples++ = av_clip_int16(pred[0]);
713  }
714  }
715  } else { //8-bit data
716  for(i = stereo; i >= 0; i--)
717  pred[i] = get_bits(&gb, 8);
718  for(i = 0; i <= stereo; i++)
719  *samples8++ = pred[i];
720  for(; i < unp_size; i++) {
721  if(get_bits_left(&gb)<0)
722  return AVERROR_INVALIDDATA;
723  if(i & stereo){
724  if(vlc[1].table)
725  res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
726  else
727  res = 0;
728  if (res < 0) {
729  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
730  return AVERROR_INVALIDDATA;
731  }
732  pred[1] += sign_extend(h[1].values[res], 8);
733  *samples8++ = av_clip_uint8(pred[1]);
734  } else {
735  if(vlc[0].table)
736  res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
737  else
738  res = 0;
739  if (res < 0) {
740  av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
741  return AVERROR_INVALIDDATA;
742  }
743  pred[0] += sign_extend(h[0].values[res], 8);
744  *samples8++ = av_clip_uint8(pred[0]);
745  }
746  }
747  }
748 
749  for(i = 0; i < 4; i++) {
750  if(vlc[i].table)
751  ff_free_vlc(&vlc[i]);
752  av_free(h[i].bits);
753  av_free(h[i].lengths);
754  av_free(h[i].values);
755  }
756 
757  *got_frame_ptr = 1;
758 
759  return buf_size;
760 }
761 
763  .name = "smackvid",
764  .type = AVMEDIA_TYPE_VIDEO,
766  .priv_data_size = sizeof(SmackVContext),
767  .init = decode_init,
768  .close = decode_end,
769  .decode = decode_frame,
770  .capabilities = CODEC_CAP_DR1,
771  .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
772 };
773 
775  .name = "smackaud",
776  .type = AVMEDIA_TYPE_AUDIO,
778  .init = smka_decode_init,
779  .decode = smka_decode_frame,
780  .capabilities = CODEC_CAP_DR1,
781  .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
782 };
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
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
#define SMK_NODE
float v
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static const int block_runs[64]
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int * recode1
int * recode2
#define av_bswap16
Definition: sh4/bswap.h:31
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
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:136
#define AV_CH_LAYOUT_STEREO
signed 16 bits
Definition: samplefmt.h:52
#define blk(i)
Definition: sha.c:169
static av_cold int decode_init(AVCodecContext *avctx)
int stride
Definition: mace.c:144
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]
uint8_t bits
Definition: crc.c:216
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define av_cold
Definition: attributes.h:78
AV_SAMPLE_FMT_U8
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
mode
Definition: f_perms.c:27
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static av_cold int smka_decode_init(AVCodecContext *avctx)
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
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).
frame
Definition: stft.m:14
#define U(x)
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:557
AVCodec ff_smacker_decoder
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
SmkBlockTypes
#define AV_WL16(p, darg)
Definition: intreadwrite.h:250
static const struct endianess table[]
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
Decode local frame tree.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
const char * r
Definition: vf_curves.c:94
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
int size
Definition: get_bits.h:63
uint64_t channel_layout
Audio channel layout.
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
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...
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
Context used for code reconstructing.
t
Definition: genspecsines3.m:6
static av_cold int decode_end(AVCodecContext *avctx)
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:524
#define AV_RL32
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
Store large tree as FFmpeg&#39;s vlc codes.
static const float pred[4]
Definition: siprdata.h:259
NULL
Definition: eval.c:55
AVCodec ff_smackaud_decoder
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:426
void * buf
Definition: avisynth_c.h:594
#define INIT_VLC_LE
Definition: get_bits.h:442
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:298
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:73
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
synthesis window for stochastic i
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
Decode header tree.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:280
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:123
static int flags
Definition: cpu.c:23
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
AVCodecContext * avctx
common internal api header.
uint32_t * bits
static double c[64]
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
#define SMKTREE_BITS
int channels
number of audio channels
struct SmackVContext SmackVContext
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
#define AV_LOG_INFO
Definition: log.h:156
Filter the word “frame” indicates either a video frame or a group of audio samples
#define av_always_inline
Definition: attributes.h:41
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
const char int length
Definition: avisynth_c.h:668
static int decode_header_trees(SmackVContext *smk)
static av_always_inline void last_reset(int *recode, int *last)
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:344
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
Predicted.
Definition: avutil.h:217
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame This method is called when a frame is wanted on an output For an it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values