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
36 #define BITSTREAM_READER_LE
42
43 #define SMKTREE_BITS 9
44 #define SMK_NODE 0x80000000
45
46 #define SMKTREE_DECODE_MAX_RECURSION 32
47 #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
48
52
56
57 /**
58 * Context used for code reconstructing
59 */
68
69 /* common parameters used for decode_bigtree */
77
78 /* possible runs of blocks */
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
94
95 /**
96 * Decode local frame tree
97 */
99 {
103 }
104
109 }
110 if(length){
113 } else {
116 }
121 return 0;
122 } else { //Node
124 length++;
126 if(r)
129 }
130 }
131
132 /**
133 * Decode header tree
134 */
137 {
138 // Larger length can cause segmentation faults due to too deep recursion.
142 }
143
147 }
152 if (i1 < 0 || i2 < 0)
157 val = 0;
158 }
else if(val == ctx->
escapes[1]) {
160 val = 0;
161 }
else if(val == ctx->
escapes[2]) {
163 val = 0;
164 }
165
167 return 1;
168 } else { //Node
170
173 if(r < 0)
176 r++;
178 if (r_new < 0)
179 return r_new;
180 return r + r_new;
181 }
182 }
183
184 /**
185 * Store large tree as FFmpeg's vlc codes
186 */
188 {
189 int res;
192 VLC vlc[2] = { { 0 } };
193 int escapes[3];
195 int err = 0;
196
197 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
200 }
201
208
219 }
220
223 if (res < 0) {
224 err = res;
226 }
230 tmp1.
lengths,
sizeof(
int),
sizeof(
int),
232 if(res < 0) {
234 err = res;
236 }
237 }
238 }
241 }
244 if (res < 0) {
245 err = res;
247 }
251 tmp2.
lengths,
sizeof(
int),
sizeof(
int),
253 if(res < 0) {
255 err = res;
257 }
258 }
259 }
260 if (!vlc[1].table) {
262 }
263
267
268 last[0] = last[1] = last[2] = -1;
269
278
279 huff.
length = ((size + 3) >> 2) + 4;
286 }
287
289 if (res < 0)
290 err = res;
300 }
301
303
305 if(vlc[0].table)
307 if(vlc[1].table)
315
316 return err;
317 }
318
321 int mmap_size, mclr_size, full_size, type_size, ret;
322
327
329 if (ret < 0)
330 return ret;
331
339 } else {
341 if (ret < 0)
342 return ret;
343 }
351 } else {
353 if (ret < 0)
354 return ret;
355 }
363 } else {
365 if (ret < 0)
366 return ret;
367 }
375 } else {
377 if (ret < 0)
378 return ret;
379 }
380
381 return 0;
382 }
383
385 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
386 }
387
388 /* get code and update history */
390 register int *
table = recode;
391 int v;
392
395 table += (*table) & (~SMK_NODE);
396 table++;
397 }
399
400 if(v != recode[last[0]]) {
401 recode[last[2]] = recode[last[1]];
402 recode[last[1]] = recode[last[0]];
403 recode[last[0]] = v;
404 }
405 return v;
406 }
407
410 {
413 uint32_t *pal;
416 int blocks,
blk, bw, bh;
417 int i, ret;
420
421 if (avpkt->
size <= 769)
423
425 return ret;
426
427 /* make the palette available on the way out */
428 pal = (uint32_t*)smk->
pic->
data[1];
430 flags = bytestream2_get_byteu(&gb2);
435 else
437
438 for(i = 0; i < 256; i++)
439 *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
440
446 return ret;
447
448 blk = 0;
449 bw = avctx->
width >> 2;
451 blocks = bw * bh;
453 while(blk < blocks) {
455 uint16_t pix;
456
459 switch(type & 3){
461 while(run-- && blk < blocks){
463 int hi, lo;
466 out = smk->
pic->
data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
467 hi = clr >> 8;
468 lo = clr & 0xFF;
469 for(i = 0; i < 4; i++) {
470 if(map & 1) out[0] = hi; else out[0] = lo;
471 if(map & 2) out[1] = hi; else out[1] = lo;
472 if(map & 4) out[2] = hi; else out[2] = lo;
473 if(map & 8) out[3] = hi; else out[3] = lo;
474 map >>= 4;
476 }
477 blk++;
478 }
479 break;
481 mode = 0;
482 if(avctx->
codec_tag ==
MKTAG(
'S',
'M',
'K',
'4')) {
// In case of Smacker v4 we have three modes
485 }
486 while(run-- && blk < blocks){
487 out = smk->
pic->
data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
488 switch(mode){
489 case 0:
490 for(i = 0; i < 4; i++) {
496 }
497 break;
498 case 1:
500 out[0] = out[1] = pix & 0xFF;
501 out[2] = out[3] = pix >> 8;
503 out[0] = out[1] = pix & 0xFF;
504 out[2] = out[3] = pix >> 8;
507 out[0] = out[1] = pix & 0xFF;
508 out[2] = out[3] = pix >> 8;
510 out[0] = out[1] = pix & 0xFF;
511 out[2] = out[3] = pix >> 8;
512 break;
513 case 2:
514 for(i = 0; i < 2; i++) {
515 uint16_t pix1, pix2;
524 }
525 break;
526 }
527 blk++;
528 }
529 break;
531 while(run-- && blk < blocks)
532 blk++;
533 break;
535 mode = type >> 8;
536 while(run-- && blk < blocks){
537 uint32_t col;
538 out = smk->
pic->
data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
539 col = mode * 0x01010101;
540 for(i = 0; i < 4; i++) {
541 *((uint32_t*)out) = col;
543 }
544 blk++;
545 }
546 break;
547 }
548
549 }
550
552 return ret;
553
554 *got_frame = 1;
555
556 /* always report that the buffer was completely consumed */
558 }
559
560
562 {
564
569
571
572 return 0;
573 }
574
575
577 {
579 int ret;
580
582
584
588
589 /* decode huffman trees from extradata */
594 }
595
597 if (ret < 0) {
599 return ret;
600 }
601
602 return 0;
603 }
604
605
607 {
611 }
614
615 return 0;
616 }
617
618 /**
619 * Decode Smacker audio data
620 */
622 int *got_frame_ptr,
AVPacket *avpkt)
623 {
626 int buf_size = avpkt->
size;
629 VLC vlc[4] = { { 0 } };
630 int16_t *samples;
633 int i, res, ret;
634 int unp_size;
635 int bits, stereo;
636 int pred[2] = {0, 0};
637
638 if (buf_size <= 4) {
641 }
642
644
645 if (unp_size > (1
U<<24)) {
648 }
649
651 return ret;
652
655 *got_frame_ptr = 0;
656 return 1;
657 }
660 if (stereo ^ (avctx->
channels != 1)) {
663 }
667 }
668
669 /* get output buffer */
671 if (unp_size % (avctx->
channels * (bits + 1))) {
673 "The buffer does not contain an integer number of samples\n");
675 }
677 return ret;
678 samples = (int16_t *)frame->
data[0];
679 samples8 = frame->
data[0];
680
681 // Initialize
682 for(i = 0; i < (1 << (bits + stereo)); i++) {
689 if (!h[i].bits || !h[i].lengths || !h[i].values) {
692 }
697 }
699 if(h[i].current > 1) {
701 h[i].lengths, sizeof(int), sizeof(int),
702 h[i].bits,
sizeof(uint32_t),
sizeof(uint32_t),
INIT_VLC_LE);
703 if(res < 0) {
707 }
708 }
709 }
710 /* this codec relies on wraparound instead of clipping audio */
711 if(bits) { //decode 16-bit data
712 for(i = stereo; i >= 0; i--)
714 for(i = 0; i <= stereo; i++)
715 *samples++ = pred[i];
716 for(; i < unp_size / 2; i++) {
719 if(i & stereo) {
722 else
723 res = 0;
724 if (res < 0) {
727 }
729 if(vlc[3].table)
731 else
732 res = 0;
733 if (res < 0) {
736 }
737 val |= h[3].
values[res] << 8;
739 *samples++ = pred[1];
740 } else {
743 else
744 res = 0;
745 if (res < 0) {
748 }
750 if(vlc[1].table)
752 else
753 res = 0;
754 if (res < 0) {
757 }
758 val |= h[1].
values[res] << 8;
760 *samples++ = pred[0];
761 }
762 }
763 } else { //8-bit data
764 for(i = stereo; i >= 0; i--)
766 for(i = 0; i <= stereo; i++)
767 *samples8++ = pred[i];
768 for(; i < unp_size; i++) {
771 if(i & stereo){
774 else
775 res = 0;
776 if (res < 0) {
779 }
781 *samples8++ = pred[1];
782 } else {
785 else
786 res = 0;
787 if (res < 0) {
790 }
792 *samples8++ = pred[0];
793 }
794 }
795 }
796
797 *got_frame_ptr = 1;
798 ret = buf_size;
799
801 for(i = 0; i < 4; i++) {
807 }
808
809 return ret;
810 }
811
822 };
823
832 };
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx, int length)
Decode header tree.
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
const char const char void * val
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
static const int block_runs[64]
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static av_cold int init(AVCodecContext *avctx)
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)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define AV_CH_LAYOUT_STEREO
static av_cold int decode_init(AVCodecContext *avctx)
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
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...
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
8 bits with AV_PIX_FMT_RGB32 palette
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static av_cold int smka_decode_init(AVCodecContext *avctx)
bitstream reader API header.
#define SMKTREE_DECODE_BIG_MAX_RECURSION
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
static int get_bits_left(GetBitContext *gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVCodec ff_smacker_decoder
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
#define SMKTREE_DECODE_MAX_RECURSION
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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. ...
const char * name
Name of the codec implementation.
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.
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
int width
picture width / height.
Context used for code reconstructing.
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.
static void error(const char *err)
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
Store large tree as FFmpeg's vlc codes.
static const float pred[4]
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
AVCodec ff_smackaud_decoder
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
main external API structure.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits1(GetBitContext *s)
int palette_has_changed
Tell user application that palette has changed from previous frame.
const VDPAUPixFmtMap * map
static av_const int sign_extend(int val, unsigned bits)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
common internal api header.
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last)
int channels
number of audio channels
VLC_TYPE(* table)[2]
code, bits
int key_frame
1 -> keyframe, 0-> not
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)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
This structure stores compressed data.
void ff_free_vlc(VLC *vlc)
mode
Use these values in ebur128_init (or'ed).
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
void * av_mallocz_array(size_t nmemb, size_t size)