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
38
39 #define BITSTREAM_READER_LE
42
43 #define SMKTREE_BITS 9
44 #define SMK_NODE 0x80000000
45
46 /*
47 * Decoder context
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 }
108 }
109 if(length){
112 } else {
115 }
120 return 0;
121 } else { //Node
123 length++;
125 if(r)
128 }
129 }
130
131 /**
132 * Decode header tree
133 */
135 {
139 }
144 if (i1 < 0 || i2 < 0)
149 val = 0;
150 }
else if(val == ctx->
escapes[1]) {
152 val = 0;
153 }
else if(val == ctx->
escapes[2]) {
155 val = 0;
156 }
157
159 return 1;
160 } else { //Node
162
165 if(r < 0)
168 r++;
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 */
180 {
181 int res;
184 VLC vlc[2] = { { 0 } };
185 int escapes[3];
187 int err = 0;
188
189 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
192 }
193
200
210 goto error;
211 }
212
215 if (res < 0) {
216 err = res;
217 goto error;
218 }
222 tmp1.
lengths,
sizeof(
int),
sizeof(
int),
224 if(res < 0) {
226 err = res;
227 goto error;
228 }
229 }
230 }
233 }
236 if (res < 0) {
237 err = res;
238 goto error;
239 }
243 tmp2.
lengths,
sizeof(
int),
sizeof(
int),
245 if(res < 0) {
247 err = res;
248 goto error;
249 }
250 }
251 }
252 if (!vlc[1].table) {
254 }
255
259
260 last[0] = last[1] = last[2] = -1;
261
270
271 huff.
length = ((size + 3) >> 2) + 4;
277 goto error;
278 }
279
281 err = -1;
291 }
292
294
295 error:
296 if(vlc[0].table)
298 if(vlc[1].table)
306
307 return err;
308 }
309
312 int mmap_size, mclr_size, full_size, type_size, ret;
313
318
320 if (ret < 0)
321 return ret;
322
330 } else {
332 if (ret < 0)
333 return ret;
334 }
342 } else {
344 if (ret < 0)
345 return ret;
346 }
354 } else {
356 if (ret < 0)
357 return ret;
358 }
366 } else {
368 if (ret < 0)
369 return ret;
370 }
371
372 return 0;
373 }
374
376 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
377 }
378
379 /* get code and update history */
381 register int *
table = recode;
382 int v;
383
386 table += (*table) & (~SMK_NODE);
387 table++;
388 }
390
391 if(v != recode[last[0]]) {
392 recode[last[2]] = recode[last[1]];
393 recode[last[1]] = recode[last[0]];
394 recode[last[0]] = v;
395 }
396 return v;
397 }
398
401 {
404 uint32_t *pal;
407 int blocks,
blk, bw, bh;
408 int i, ret;
411
412 if (avpkt->
size <= 769)
414
416 return ret;
417
418 /* make the palette available on the way out */
419 pal = (uint32_t*)smk->
pic->
data[1];
421 flags = bytestream2_get_byteu(&gb2);
426 else
428
429 for(i = 0; i < 256; i++)
430 *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
431
437 return ret;
438
439 blk = 0;
440 bw = avctx->
width >> 2;
442 blocks = bw * bh;
444 while(blk < blocks) {
446 uint16_t pix;
447
450 switch(type & 3){
452 while(run-- && blk < blocks){
453 int clr, map;
454 int hi, lo;
457 out = smk->
pic->
data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
458 hi = clr >> 8;
459 lo = clr & 0xFF;
460 for(i = 0; i < 4; i++) {
461 if(map & 1) out[0] = hi; else out[0] = lo;
462 if(map & 2) out[1] = hi; else out[1] = lo;
463 if(map & 4) out[2] = hi; else out[2] = lo;
464 if(map & 8) out[3] = hi; else out[3] = lo;
465 map >>= 4;
467 }
468 blk++;
469 }
470 break;
472 mode = 0;
473 if(avctx->
codec_tag ==
MKTAG(
'S',
'M',
'K',
'4')) {
// In case of Smacker v4 we have three modes
476 }
477 while(run-- && blk < blocks){
478 out = smk->
pic->
data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
479 switch(mode){
480 case 0:
481 for(i = 0; i < 4; i++) {
487 }
488 break;
489 case 1:
491 out[0] = out[1] = pix & 0xFF;
492 out[2] = out[3] = pix >> 8;
494 out[0] = out[1] = pix & 0xFF;
495 out[2] = out[3] = pix >> 8;
498 out[0] = out[1] = pix & 0xFF;
499 out[2] = out[3] = pix >> 8;
501 out[0] = out[1] = pix & 0xFF;
502 out[2] = out[3] = pix >> 8;
503 break;
504 case 2:
505 for(i = 0; i < 2; i++) {
506 uint16_t pix1, pix2;
515 }
516 break;
517 }
518 blk++;
519 }
520 break;
522 while(run-- && blk < blocks)
523 blk++;
524 break;
526 mode = type >> 8;
527 while(run-- && blk < blocks){
528 uint32_t col;
529 out = smk->
pic->
data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
530 col = mode * 0x01010101;
531 for(i = 0; i < 4; i++) {
532 *((uint32_t*)out) = col;
534 }
535 blk++;
536 }
537 break;
538 }
539
540 }
541
543 return ret;
544
545 *got_frame = 1;
546
547 /* always report that the buffer was completely consumed */
549 }
550
551
552
553 /*
554 *
555 * Uninit smacker decoder
556 *
557 */
559 {
561
566
568
569 return 0;
570 }
571
572
573 /*
574 *
575 * Init smacker decoder
576 *
577 */
579 {
581 int ret;
582
584
586
590
591 /* decode huffman trees from extradata */
596 }
597
599 if (ret < 0) {
601 return ret;
602 }
603
604 return 0;
605 }
606
607
609 {
613 }
616
617 return 0;
618 }
619
620 /**
621 * Decode Smacker audio data
622 */
624 int *got_frame_ptr,
AVPacket *avpkt)
625 {
628 int buf_size = avpkt->
size;
631 VLC vlc[4] = { { 0 } };
632 int16_t *samples;
635 int i, res, ret;
636 int unp_size;
638 int pred[2] = {0, 0};
639
640 if (buf_size <= 4) {
643 }
644
646
647 if (unp_size > (1
U<<24)) {
650 }
651
653 return ret;
654
657 *got_frame_ptr = 0;
658 return 1;
659 }
662 if (stereo ^ (avctx->
channels != 1)) {
665 }
669 }
670
671 /* get output buffer */
673 if (unp_size % (avctx->
channels * (bits + 1))) {
676 }
678 return ret;
679 samples = (int16_t *)frame->
data[0];
680 samples8 = frame->
data[0];
681
682 // Initialize
683 for(i = 0; i < (1 << (bits + stereo)); i++) {
690 if (!h[i].bits || !h[i].lengths || !h[i].values) {
692 goto error;
693 }
697 goto error;
698 }
700 if(h[i].current > 1) {
702 h[i].lengths, sizeof(int), sizeof(int),
703 h[i].bits,
sizeof(uint32_t),
sizeof(uint32_t),
INIT_VLC_LE);
704 if(res < 0) {
707 goto error;
708 }
709 }
710 }
711 /* this codec relies on wraparound instead of clipping audio */
712 if(bits) { //decode 16-bit data
713 for(i = stereo; i >= 0; i--)
715 for(i = 0; i <= stereo; i++)
716 *samples++ = pred[i];
717 for(; i < unp_size / 2; i++) {
720 if(i & stereo) {
723 else
724 res = 0;
725 if (res < 0) {
728 }
730 if(vlc[3].table)
732 else
733 res = 0;
734 if (res < 0) {
737 }
738 val |= h[3].
values[res] << 8;
740 *samples++ = pred[1];
741 } else {
744 else
745 res = 0;
746 if (res < 0) {
749 }
751 if(vlc[1].table)
753 else
754 res = 0;
755 if (res < 0) {
758 }
759 val |= h[1].
values[res] << 8;
761 *samples++ = pred[0];
762 }
763 }
764 } else { //8-bit data
765 for(i = stereo; i >= 0; i--)
767 for(i = 0; i <= stereo; i++)
768 *samples8++ = pred[i];
769 for(; i < unp_size; i++) {
772 if(i & stereo){
775 else
776 res = 0;
777 if (res < 0) {
780 }
782 *samples8++ = pred[1];
783 } else {
786 else
787 res = 0;
788 if (res < 0) {
791 }
793 *samples8++ = pred[0];
794 }
795 }
796 }
797
798 *got_frame_ptr = 1;
799 ret = buf_size;
800
801 error:
802 for(i = 0; i < 4; i++) {
808 }
809
810 return ret;
811 }
812
823 };
824
833 };
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)
#define AV_CH_LAYOUT_STEREO
static av_cold int decode_init(AVCodecContext *avctx)
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
8 bit 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.
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
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.
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.
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 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.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits1(GetBitContext *s)
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
Decode header tree.
int palette_has_changed
Tell user application that palette has changed from previous frame.
static av_const int sign_extend(int val, unsigned bits)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
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 void * av_mallocz_array(size_t nmemb, size_t size)
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)
int nb_samples
number of audio samples (per channel) described by this frame
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.