FFmpeg
Loading...
Searching...
No Matches
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 <stddef.h>
32
34#include "libavutil/mem.h"
35
36#include "avcodec.h"
37
38#define SMKTREE_BITS 9
39#define SMK_NODE 0x80000000
40
41#define SMKTREE_DECODE_MAX_RECURSION FFMIN(32, 3 * SMKTREE_BITS)
42#define SMKTREE_DECODE_BIG_MAX_RECURSION 500
43
44/* The maximum possible unchecked overread happens in decode_header_trees:
45 * Decoding the MMAP tree can overread by 6 * SMKTREE_BITS + 1, followed by
46 * three get_bits1, followed by at most 2 + 3 * 16 read bits when reading
47 * the TYPE tree before the next check. 64 is because of 64 bit reads. */
48#if (6 * SMKTREE_BITS + 1 + 3 + (2 + 3 * 16) + 64) <= 8 * AV_INPUT_BUFFER_PADDING_SIZE
49#define UNCHECKED_BITSTREAM_READER 1
50#endif
51#define BITSTREAM_READER_LE
52#include "bytestream.h"
53#include "codec_internal.h"
54#include "decode.h"
55#include "get_bits.h"
56
64
65typedef struct HuffEntry {
66 uint8_t value;
67 uint8_t length;
68} HuffEntry;
69
70/**
71 * Context used for code reconstructing
72 */
73typedef struct HuffContext {
77
78/* common parameters used for decode_bigtree */
79typedef struct DBCtx {
81 int *values;
82 VLC *v1, *v2;
83 uint8_t vals[2];
84 int escapes[3];
85 int *last;
86} DBCtx;
87
88/* possible runs of blocks */
89static const int block_runs[64] = {
90 1, 2, 3, 4, 5, 6, 7, 8,
91 9, 10, 11, 12, 13, 14, 15, 16,
92 17, 18, 19, 20, 21, 22, 23, 24,
93 25, 26, 27, 28, 29, 30, 31, 32,
94 33, 34, 35, 36, 37, 38, 39, 40,
95 41, 42, 43, 44, 45, 46, 47, 48,
96 49, 50, 51, 52, 53, 54, 55, 56,
97 57, 58, 59, 128, 256, 512, 1024, 2048 };
98
104
105/**
106 * Decode local frame tree
107 *
108 * Can read SMKTREE_DECODE_MAX_RECURSION before the first check;
109 * does not overread gb on success.
110 */
111static int smacker_decode_tree(AVCodecContext *avctx, GetBitContext *gb, HuffContext *hc, int length)
112{
113 if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
114 av_log(avctx, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
115 return AVERROR_INVALIDDATA;
116 }
117
118 if(!get_bits1(gb)){ //Leaf
119 if (hc->current >= 256) {
120 av_log(avctx, AV_LOG_ERROR, "Tree size exceeded!\n");
121 return AVERROR_INVALIDDATA;
122 }
123 if (get_bits_left(gb) < 8)
124 return AVERROR_INVALIDDATA;
125 hc->entries[hc->current++] = (HuffEntry){ get_bits(gb, 8), length };
126 return 0;
127 } else { //Node
128 int r;
129 length++;
130 r = smacker_decode_tree(avctx, gb, hc, length);
131 if(r)
132 return r;
133 return smacker_decode_tree(avctx, gb, hc, length);
134 }
135}
136
137/**
138 * Decode header tree
139 *
140 * Checks before the first read, can overread by 6 * SMKTREE_BITS on success.
141 */
142static int smacker_decode_bigtree(AVCodecContext *avctx, GetBitContext *gb, DBCtx *ctx, int length)
143{
144 // Larger length can cause segmentation faults due to too deep recursion.
146 av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
147 return AVERROR_INVALIDDATA;
148 }
149
150 if (ctx->current >= ctx->length) {
151 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
152 return AVERROR_INVALIDDATA;
153 }
154 if (get_bits_left(gb) <= 0)
155 return AVERROR_INVALIDDATA;
156 if(!get_bits1(gb)){ //Leaf
157 int val, i1, i2;
158 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3)
159 : ctx->vals[0];
160 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3)
161 : ctx->vals[1];
162 val = i1 | (i2 << 8);
163 if(val == ctx->escapes[0]) {
164 ctx->last[0] = ctx->current;
165 val = 0;
166 } else if(val == ctx->escapes[1]) {
167 ctx->last[1] = ctx->current;
168 val = 0;
169 } else if(val == ctx->escapes[2]) {
170 ctx->last[2] = ctx->current;
171 val = 0;
172 }
173
174 ctx->values[ctx->current++] = val;
175 return 1;
176 } else { //Node
177 int r = 0, r_new, t;
178
179 t = ctx->current++;
180 r = smacker_decode_bigtree(avctx, gb, ctx, length + 1);
181 if(r < 0)
182 return r;
183 ctx->values[t] = SMK_NODE | r;
184 r++;
185 r_new = smacker_decode_bigtree(avctx, gb, ctx, length + 1);
186 if (r_new < 0)
187 return r_new;
188 return r + r_new;
189 }
190}
191
192/**
193 * Store large tree as FFmpeg's vlc codes
194 *
195 * Can read FFMAX(1 + SMKTREE_DECODE_MAX_RECURSION, 2 + 3 * 16) bits
196 * before the first check; can overread by 6 * SMKTREE_BITS + 1 on success.
197 */
198static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
199{
200 VLC vlc[2] = { { 0 } };
201 int escapes[3];
202 DBCtx ctx;
203 int err;
204
205 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
206 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
207 return AVERROR_INVALIDDATA;
208 }
209
210 for (int i = 0; i < 2; i++) {
212 h.current = 0;
213 if (!get_bits1(gb)) {
214 ctx.vals[i] = 0;
215 av_log(smk->avctx, AV_LOG_ERROR, "Skipping %s bytes tree\n",
216 i ? "high" : "low");
217 continue;
218 }
219 err = smacker_decode_tree(smk->avctx, gb, &h, 0);
220 if (err < 0)
221 goto error;
222 skip_bits1(gb);
223 if (h.current > 1) {
224 err = ff_vlc_init_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
225 &h.entries[0].length, sizeof(*h.entries),
226 &h.entries[0].value, sizeof(*h.entries), 1,
227 0, VLC_INIT_OUTPUT_LE, smk->avctx);
228 if (err < 0) {
229 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
230 goto error;
231 }
232 } else
233 ctx.vals[i] = h.entries[0].value;
234 }
235
236 escapes[0] = get_bits(gb, 16);
237 escapes[1] = get_bits(gb, 16);
238 escapes[2] = get_bits(gb, 16);
239
240 last[0] = last[1] = last[2] = -1;
241
242 ctx.escapes[0] = escapes[0];
243 ctx.escapes[1] = escapes[1];
244 ctx.escapes[2] = escapes[2];
245 ctx.v1 = &vlc[0];
246 ctx.v2 = &vlc[1];
247 ctx.last = last;
248 ctx.length = (size + 3) >> 2;
249 ctx.current = 0;
250 ctx.values = av_malloc_array(ctx.length + 3, sizeof(ctx.values[0]));
251 if (!ctx.values) {
252 err = AVERROR(ENOMEM);
253 goto error;
254 }
255 *recodes = ctx.values;
256
257 err = smacker_decode_bigtree(smk->avctx, gb, &ctx, 0);
258 if (err < 0)
259 goto error;
260 skip_bits1(gb);
261 if (ctx.last[0] == -1) ctx.last[0] = ctx.current++;
262 if (ctx.last[1] == -1) ctx.last[1] = ctx.current++;
263 if (ctx.last[2] == -1) ctx.last[2] = ctx.current++;
264
265 err = 0;
266error:
267 for (int i = 0; i < 2; i++) {
268 ff_vlc_free(&vlc[i]);
269 }
270
271 return err;
272}
273
275 GetBitContext gb;
276 int mmap_size, mclr_size, full_size, type_size, ret;
277 int skip = 0;
278
279 mmap_size = AV_RL32(smk->avctx->extradata);
280 mclr_size = AV_RL32(smk->avctx->extradata + 4);
281 full_size = AV_RL32(smk->avctx->extradata + 8);
282 type_size = AV_RL32(smk->avctx->extradata + 12);
283
284 ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
285 if (ret < 0)
286 return ret;
287
288 if(!get_bits1(&gb)) {
289 skip ++;
290 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
291 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
292 if (!smk->mmap_tbl)
293 return AVERROR(ENOMEM);
294 smk->mmap_tbl[0] = 0;
295 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
296 } else {
297 ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
298 if (ret < 0)
299 return ret;
300 }
301 if(!get_bits1(&gb)) {
302 skip ++;
303 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
304 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
305 if (!smk->mclr_tbl)
306 return AVERROR(ENOMEM);
307 smk->mclr_tbl[0] = 0;
308 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
309 } else {
310 ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
311 if (ret < 0)
312 return ret;
313 }
314 if(!get_bits1(&gb)) {
315 skip ++;
316 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
317 smk->full_tbl = av_malloc(sizeof(int) * 2);
318 if (!smk->full_tbl)
319 return AVERROR(ENOMEM);
320 smk->full_tbl[0] = 0;
321 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
322 } else {
323 ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
324 if (ret < 0)
325 return ret;
326 }
327 if(!get_bits1(&gb)) {
328 skip ++;
329 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
330 smk->type_tbl = av_malloc(sizeof(int) * 2);
331 if (!smk->type_tbl)
332 return AVERROR(ENOMEM);
333 smk->type_tbl[0] = 0;
334 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
335 } else {
336 ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
337 if (ret < 0)
338 return ret;
339 }
340 if (skip == 4 || get_bits_left(&gb) < 0)
341 return AVERROR_INVALIDDATA;
342
343 return 0;
344}
345
346static av_always_inline void last_reset(int *recode, int *last) {
347 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
348}
349
350/* Get code and update history.
351 * Checks before reading, does not overread. */
352static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
353 register int *table = recode;
354 int v;
355
356 while(*table & SMK_NODE) {
357 if (get_bits_left(gb) < 1)
358 return AVERROR_INVALIDDATA;
359 if(get_bits1(gb))
360 table += (*table) & (~SMK_NODE);
361 table++;
362 }
363 v = *table;
364
365 if(v != recode[last[0]]) {
366 recode[last[2]] = recode[last[1]];
367 recode[last[1]] = recode[last[0]];
368 recode[last[0]] = v;
369 }
370 return v;
371}
372
373static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
374 int *got_frame, AVPacket *avpkt)
375{
376 SmackVContext * const smk = avctx->priv_data;
377 uint8_t *out;
378 uint32_t *pal;
379 GetByteContext gb2;
380 GetBitContext gb;
381 int blocks, blk, bw, bh;
382 int i, ret;
383 int stride;
384 int flags;
385
386 if (avpkt->size <= 769)
387 return AVERROR_INVALIDDATA;
388
389 if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
390 return ret;
391
392 /* make the palette available on the way out */
393 pal = (uint32_t*)smk->pic->data[1];
394 bytestream2_init(&gb2, avpkt->data, avpkt->size);
395 flags = bytestream2_get_byteu(&gb2);
396 if (flags & 2) {
397 smk->pic->flags |= AV_FRAME_FLAG_KEY;
399 } else {
402 }
403
404 for(i = 0; i < 256; i++)
405 *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
406
407 last_reset(smk->mmap_tbl, smk->mmap_last);
408 last_reset(smk->mclr_tbl, smk->mclr_last);
409 last_reset(smk->full_tbl, smk->full_last);
410 last_reset(smk->type_tbl, smk->type_last);
411 if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
412 return ret;
413
414 blk = 0;
415 bw = avctx->width >> 2;
416 bh = avctx->height >> 2;
417 blocks = bw * bh;
418 stride = smk->pic->linesize[0];
419 while(blk < blocks) {
420 int type, run, mode;
421 uint16_t pix;
422
423 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
424 if (type < 0)
425 return type;
426 run = block_runs[(type >> 2) & 0x3F];
427 switch(type & 3){
428 case SMK_BLK_MONO:
429 while(run-- && blk < blocks){
430 int clr, map;
431 int hi, lo;
432 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
433 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
434 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
435 hi = clr >> 8;
436 lo = clr & 0xFF;
437 for(i = 0; i < 4; i++) {
438 if(map & 1) out[0] = hi; else out[0] = lo;
439 if(map & 2) out[1] = hi; else out[1] = lo;
440 if(map & 4) out[2] = hi; else out[2] = lo;
441 if(map & 8) out[3] = hi; else out[3] = lo;
442 map >>= 4;
443 out += stride;
444 }
445 blk++;
446 }
447 break;
448 case SMK_BLK_FULL:
449 mode = 0;
450 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
451 if (get_bits_left(&gb) < 1)
452 return AVERROR_INVALIDDATA;
453 if(get_bits1(&gb)) mode = 1;
454 else if(get_bits1(&gb)) mode = 2;
455 }
456 while(run-- && blk < blocks){
457 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
458 switch(mode){
459 case 0:
460 for(i = 0; i < 4; i++) {
461 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
462 AV_WL16(out+2,pix);
463 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
464 AV_WL16(out,pix);
465 out += stride;
466 }
467 break;
468 case 1:
469 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
470 out[0] = out[1] = pix & 0xFF;
471 out[2] = out[3] = pix >> 8;
472 out += stride;
473 out[0] = out[1] = pix & 0xFF;
474 out[2] = out[3] = pix >> 8;
475 out += stride;
476 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
477 out[0] = out[1] = pix & 0xFF;
478 out[2] = out[3] = pix >> 8;
479 out += stride;
480 out[0] = out[1] = pix & 0xFF;
481 out[2] = out[3] = pix >> 8;
482 break;
483 case 2:
484 for(i = 0; i < 2; i++) {
485 uint16_t pix1, pix2;
486 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
487 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
488 AV_WL16(out,pix1);
489 AV_WL16(out+2,pix2);
490 out += stride;
491 AV_WL16(out,pix1);
492 AV_WL16(out+2,pix2);
493 out += stride;
494 }
495 break;
496 }
497 blk++;
498 }
499 break;
500 case SMK_BLK_SKIP:
501 while(run-- && blk < blocks)
502 blk++;
503 break;
504 case SMK_BLK_FILL:
505 mode = type >> 8;
506 while(run-- && blk < blocks){
507 uint32_t col;
508 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
509 col = mode * 0x01010101U;
510 for(i = 0; i < 4; i++) {
511 *((uint32_t*)out) = col;
512 out += stride;
513 }
514 blk++;
515 }
516 break;
517 }
518
519 }
520
521 if ((ret = av_frame_ref(rframe, smk->pic)) < 0)
522 return ret;
523
524 *got_frame = 1;
525
526 /* always report that the buffer was completely consumed */
527 return avpkt->size;
528}
529
530
532{
533 SmackVContext * const smk = avctx->priv_data;
534
535 av_freep(&smk->mmap_tbl);
536 av_freep(&smk->mclr_tbl);
537 av_freep(&smk->full_tbl);
538 av_freep(&smk->type_tbl);
539
540 av_frame_free(&smk->pic);
541
542 return 0;
543}
544
545
547{
548 SmackVContext * const c = avctx->priv_data;
549 int ret;
550
551 c->avctx = avctx;
552
553 avctx->pix_fmt = AV_PIX_FMT_PAL8;
554
555 c->pic = av_frame_alloc();
556 if (!c->pic)
557 return AVERROR(ENOMEM);
558
559 /* decode huffman trees from extradata */
560 if (avctx->extradata_size <= 16){
561 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
562 return AVERROR(EINVAL);
563 }
564
565 ret = decode_header_trees(c);
566 if (ret < 0) {
567 return ret;
568 }
569
570 return 0;
571}
572
573
575{
576 int channels = avctx->ch_layout.nb_channels;
578 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
579 return AVERROR_INVALIDDATA;
580 }
584
585 return 0;
586}
587
588/**
589 * Decode Smacker audio data
590 */
592 int *got_frame_ptr, AVPacket *avpkt)
593{
594 const uint8_t *buf = avpkt->data;
595 int buf_size = avpkt->size;
596 GetBitContext gb;
597 VLC vlc[4] = { { 0 } };
598 int16_t *samples;
599 uint8_t *samples8;
600 uint8_t values[4];
601 int i, res, ret;
602 int unp_size;
603 int bits, stereo;
604 unsigned pred[2], val, val2;
605
606 if (buf_size <= 4) {
607 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
608 return AVERROR_INVALIDDATA;
609 }
610
611 unp_size = AV_RL32(buf);
612
613 if (unp_size > (1U<<24)) {
614 av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
615 return AVERROR_INVALIDDATA;
616 }
617
618 if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
619 return ret;
620
621 if(!get_bits1(&gb)){
622 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
623 *got_frame_ptr = 0;
624 return 1;
625 }
626 stereo = get_bits1(&gb);
627 bits = get_bits1(&gb);
628 if (stereo ^ (avctx->ch_layout.nb_channels != 1)) {
629 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
630 return AVERROR_INVALIDDATA;
631 }
632 if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
633 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
634 return AVERROR_INVALIDDATA;
635 }
636
637 /* get output buffer */
638 frame->nb_samples = unp_size / (avctx->ch_layout.nb_channels * (bits + 1));
639 if (unp_size % (avctx->ch_layout.nb_channels * (bits + 1))) {
640 av_log(avctx, AV_LOG_ERROR,
641 "The buffer does not contain an integer number of samples\n");
642 return AVERROR_INVALIDDATA;
643 }
644
645 // Initialize
646 for(i = 0; i < (1 << (bits + stereo)); i++) {
648 h.current = 0;
649 skip_bits1(&gb);
650 if ((ret = smacker_decode_tree(avctx, &gb, &h, 0)) < 0)
651 goto error;
652 skip_bits1(&gb);
653 if (h.current > 1) {
654 ret = ff_vlc_init_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
655 &h.entries[0].length, sizeof(*h.entries),
656 &h.entries[0].value, sizeof(*h.entries), 1,
657 0, VLC_INIT_OUTPUT_LE, avctx);
658 if (ret < 0) {
659 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
660 goto error;
661 }
662 } else
663 values[i] = h.entries[0].value;
664 }
665 if (get_bits_left(&gb) < (stereo+1) * (bits+1) * 8) {
667 goto error;
668 }
669
670 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
671 goto error;
672 samples = (int16_t *)frame->data[0];
673 samples8 = frame->data[0];
674
675 /* this codec relies on wraparound instead of clipping audio */
676 if(bits) { //decode 16-bit data
677 for(i = stereo; i >= 0; i--)
678 pred[i] = av_bswap16(get_bits(&gb, 16));
679 for(i = 0; i <= stereo; i++)
680 *samples++ = pred[i];
681 unp_size /= 2;
682
683 if (vlc[0 ].table || vlc[ 1].table ||
684 vlc[2*stereo].table || vlc[2*stereo+1].table) {
685 for(; i < unp_size ; i++) {
686 unsigned idx = 2 * (i & stereo);
687 if (get_bits_left(&gb) < 0) {
689 goto error;
690 }
691 if (vlc[idx].table)
692 res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
693 else
694 res = values[idx];
695 val = res;
696 if (vlc[++idx].table)
697 res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
698 else
699 res = values[idx];
700 val |= res << 8;
701 pred[idx / 2] += val;
702 *samples++ = pred[idx / 2];
703 }
704 } else if (stereo) {
705 val = 256*values[1] + values[0];
706 val2 = 256*values[3] + values[2];
707 for(; i < unp_size; i+=2) {
708 pred[0] += val;
709 pred[1] += val2;
710 *samples++ = pred[0];
711 *samples++ = pred[1];
712 }
713 } else {
714 val = 256*values[1] + values[0];
715 for(; i < unp_size; i++) {
716 pred[0] += val;
717 *samples++ = pred[0];
718 }
719 }
720 } else { //8-bit data
721 for(i = stereo; i >= 0; i--)
722 pred[i] = get_bits(&gb, 8);
723 for(i = 0; i <= stereo; i++)
724 *samples8++ = pred[i];
725 for(; i < unp_size; i++) {
726 unsigned idx = i & stereo;
727 if (get_bits_left(&gb) < 0) {
729 goto error;
730 }
731 if (vlc[idx].table)
732 val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
733 else
734 val = values[idx];
735 pred[idx] += val;
736 *samples8++ = pred[idx];
737 }
738 }
739
740 *got_frame_ptr = 1;
741 ret = buf_size;
742
743error:
744 for(i = 0; i < 4; i++) {
745 ff_vlc_free(&vlc[i]);
746 }
747
748 return ret;
749}
750
752 .p.name = "smackvid",
753 CODEC_LONG_NAME("Smacker video"),
754 .p.type = AVMEDIA_TYPE_VIDEO,
756 .priv_data_size = sizeof(SmackVContext),
757 .init = decode_init,
758 .close = decode_end,
760 .p.capabilities = AV_CODEC_CAP_DR1,
761 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
762};
763
765 .p.name = "smackaud",
766 CODEC_LONG_NAME("Smacker audio"),
767 .p.type = AVMEDIA_TYPE_AUDIO,
769 .init = smka_decode_init,
771 .p.capabilities = AV_CODEC_CAP_DR1,
772};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_smackaud_decoder
Definition smacker.c:764
const FFCodec ff_smacker_decoder
Definition smacker.c:751
channels
Definition aptx.h:31
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition decode.c:1906
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static const uint8_t bits[8]
Definition fastaudio.c:100
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_SMACKVIDEO
Definition codec_id.h:133
@ AV_CODEC_ID_SMACKAUDIO
Definition codec_id.h:476
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
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:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition samplefmt.h:57
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
cl_device_type type
const VDPAUPixFmtMap * map
#define r
Definition input.c:42
#define AV_RL32(p)
#define AV_WL16(p, v)
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:980
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
SmkBlockTypes
Definition smacker.c:99
@ SMK_BLK_SKIP
Definition smacker.c:102
@ SMK_BLK_MONO
Definition smacker.c:100
@ SMK_BLK_FULL
Definition smacker.c:101
@ SMK_BLK_FILL
Definition smacker.c:103
static int smacker_decode_bigtree(AVCodecContext *avctx, GetBitContext *gb, DBCtx *ctx, int length)
Decode header tree.
Definition smacker.c:142
#define SMKTREE_DECODE_MAX_RECURSION
Definition smacker.c:41
#define SMKTREE_BITS
Definition smacker.c:38
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition smacker.c:373
static int smka_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode Smacker audio data.
Definition smacker.c:591
static int smacker_decode_tree(AVCodecContext *avctx, GetBitContext *gb, HuffContext *hc, int length)
Decode local frame tree.
Definition smacker.c:111
static int decode_header_trees(SmackVContext *smk)
Definition smacker.c:274
static av_cold int decode_init(AVCodecContext *avctx)
Definition smacker.c:546
static const int block_runs[64]
Definition smacker.c:89
static av_cold int decode_end(AVCodecContext *avctx)
Definition smacker.c:531
static av_cold int smka_decode_init(AVCodecContext *avctx)
Definition smacker.c:574
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
Definition smacker.c:352
static av_always_inline void last_reset(int *recode, int *last)
Definition smacker.c:346
#define SMK_NODE
Definition smacker.c:39
#define SMKTREE_DECODE_BIG_MAX_RECURSION
Definition smacker.c:42
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
Store large tree as FFmpeg's vlc codes.
Definition smacker.c:198
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
enum AVPixelFormat pix
Definition ohcodec.c:55
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
static const uint16_t table[]
Definition prosumer.c:203
#define av_bswap16
Definition bswap.h:28
#define blk(i)
Definition sha.c:55
static const float pred[4]
Definition siprdata.h:259
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition frame.h:716
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
enum AVPictureType pict_type
Picture type of the frame.
Definition frame.h:564
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int current
Definition smacker.c:80
int * last
Definition smacker.c:85
int length
Definition smacker.c:80
VLC * v2
Definition smacker.c:82
VLC * v1
Definition smacker.c:82
uint8_t vals[2]
Definition smacker.c:83
int * values
Definition smacker.c:81
int escapes[3]
Definition smacker.c:84
Context used for code reconstructing.
Definition smacker.c:73
HuffEntry entries[256]
Definition smacker.c:75
int current
Definition smacker.c:74
uint8_t value
Definition smacker.c:66
uint8_t length
Definition smacker.c:67
int * mclr_tbl
Definition smacker.c:61
int * full_tbl
Definition smacker.c:61
AVCodecContext * avctx
Definition smacker.c:58
AVFrame * pic
Definition smacker.c:59
int type_last[3]
Definition smacker.c:62
int mclr_last[3]
Definition smacker.c:62
int full_last[3]
Definition smacker.c:62
int * mmap_tbl
Definition smacker.c:61
int mmap_last[3]
Definition smacker.c:62
int * type_tbl
Definition smacker.c:61
Definition vlc.h:50
Definition swscale.c:71
uint8_t run
Definition svq3.c:207
#define stride
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
int size
int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition vlc.c:306
void ff_vlc_free(VLC *vlc)
Definition vlc.c:580
#define VLC_INIT_OUTPUT_LE
Definition vlc.h:196
static double c[64]