annotate smacker.c @ 12428:06453ad84991 libavcodec

enable a flexible lifetime
author bindhammer
date Thu, 26 Aug 2010 10:03:03 +0000
parents 8b28e74de2c0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
1 /*
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
2 * Smacker decoder
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
3 * Copyright (c) 2006 Konstantin Shishkov
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
16 *
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3800
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
20 */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
21
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
22 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11560
diff changeset
23 * @file
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
24 * Smacker decoder
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
25 */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
26
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
27 /*
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
28 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
29 */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
30
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
31 #include <stdio.h>
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
32 #include <stdlib.h>
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
33
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
34 #include "avcodec.h"
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
35
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
36 #define ALT_BITSTREAM_READER_LE
9428
0dce4fe6e6f3 Rename bitstream.h to get_bits.h.
stefano
parents: 9355
diff changeset
37 #include "get_bits.h"
6316
80f5ff30ba58 Use bytestream_get_be24 to simplify palette parsing.
reimar
parents: 6251
diff changeset
38 #include "bytestream.h"
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
39
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
40 #define SMKTREE_BITS 9
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
41 #define SMK_NODE 0x80000000
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
42
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
43 /*
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
44 * Decoder context
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
45 */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
46 typedef struct SmackVContext {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
47 AVCodecContext *avctx;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
48 AVFrame pic;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
49
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
50 int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
51 int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
52 } SmackVContext;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
53
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
54 /**
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
55 * Context used for code reconstructing
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
56 */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
57 typedef struct HuffContext {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
58 int length;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
59 int maxlength;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
60 int current;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
61 uint32_t *bits;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
62 int *lengths;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
63 int *values;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
64 } HuffContext;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
65
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
66 /* common parameters used for decode_bigtree */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
67 typedef struct DBCtx {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
68 VLC *v1, *v2;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
69 int *recode1, *recode2;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
70 int escapes[3];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
71 int *last;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
72 int lcur;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
73 } DBCtx;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
74
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
75 /* possible runs of blocks */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
76 static const int block_runs[64] = {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
77 1, 2, 3, 4, 5, 6, 7, 8,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
78 9, 10, 11, 12, 13, 14, 15, 16,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
79 17, 18, 19, 20, 21, 22, 23, 24,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
80 25, 26, 27, 28, 29, 30, 31, 32,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
81 33, 34, 35, 36, 37, 38, 39, 40,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
82 41, 42, 43, 44, 45, 46, 47, 48,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
83 49, 50, 51, 52, 53, 54, 55, 56,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
84 57, 58, 59, 128, 256, 512, 1024, 2048 };
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
85
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
86 enum SmkBlockTypes {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
87 SMK_BLK_MONO = 0,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
88 SMK_BLK_FULL = 1,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
89 SMK_BLK_SKIP = 2,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
90 SMK_BLK_FILL = 3 };
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
91
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
92 /**
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
93 * Decode local frame tree
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
94 */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
95 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
96 {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
97 if(!get_bits1(gb)){ //Leaf
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
98 if(hc->current >= 256){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
99 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
100 return -1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
101 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
102 if(length){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
103 hc->bits[hc->current] = prefix;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
104 hc->lengths[hc->current] = length;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
105 } else {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
106 hc->bits[hc->current] = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
107 hc->lengths[hc->current] = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
108 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
109 hc->values[hc->current] = get_bits(gb, 8);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
110 hc->current++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
111 if(hc->maxlength < length)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
112 hc->maxlength = length;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
113 return 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
114 } else { //Node
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
115 int r;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
116 length++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
117 r = smacker_decode_tree(gb, hc, prefix, length);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
118 if(r)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
119 return r;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
120 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
121 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
122 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
123
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
124 /**
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
125 * Decode header tree
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
126 */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
127 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
128 {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
129 if(!get_bits1(gb)){ //Leaf
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
130 int val, i1, i2, b1, b2;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
131 if(hc->current >= hc->length){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
132 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
133 return -1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
134 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
135 b1 = get_bits_count(gb);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
136 i1 = get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
137 b1 = get_bits_count(gb) - b1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
138 b2 = get_bits_count(gb);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
139 i2 = get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
140 b2 = get_bits_count(gb) - b2;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
141 val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
142 if(val == ctx->escapes[0]) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
143 ctx->last[0] = hc->current;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
144 val = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
145 } else if(val == ctx->escapes[1]) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
146 ctx->last[1] = hc->current;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
147 val = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
148 } else if(val == ctx->escapes[2]) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
149 ctx->last[2] = hc->current;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
150 val = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
151 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
152
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
153 hc->values[hc->current++] = val;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
154 return 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
155 } else { //Node
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
156 int r = 0, t;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
157
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
158 t = hc->current++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
159 r = smacker_decode_bigtree(gb, hc, ctx);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
160 if(r < 0)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
161 return r;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
162 hc->values[t] = SMK_NODE | r;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
163 r++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
164 r += smacker_decode_bigtree(gb, hc, ctx);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
165 return r;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
166 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
167 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
168
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
169 /**
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
170 * Store large tree as FFmpeg's vlc codes
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
171 */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
172 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
173 {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
174 int res;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
175 HuffContext huff;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
176 HuffContext tmp1, tmp2;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
177 VLC vlc[2];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
178 int escapes[3];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
179 DBCtx ctx;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
180
3303
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3220
diff changeset
181 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3220
diff changeset
182 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3220
diff changeset
183 return -1;
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3220
diff changeset
184 }
68721b62a528 sanity checks, some might have been exploitable ...
michael
parents: 3220
diff changeset
185
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
186 tmp1.length = 256;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
187 tmp1.maxlength = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
188 tmp1.current = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
189 tmp1.bits = av_mallocz(256 * 4);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
190 tmp1.lengths = av_mallocz(256 * sizeof(int));
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
191 tmp1.values = av_mallocz(256 * sizeof(int));
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
192
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
193 tmp2.length = 256;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
194 tmp2.maxlength = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
195 tmp2.current = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
196 tmp2.bits = av_mallocz(256 * 4);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
197 tmp2.lengths = av_mallocz(256 * sizeof(int));
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
198 tmp2.values = av_mallocz(256 * sizeof(int));
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
199
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
200 memset(&vlc[0], 0, sizeof(VLC));
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
201 memset(&vlc[1], 0, sizeof(VLC));
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
202
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
203 if(get_bits1(gb)) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
204 smacker_decode_tree(gb, &tmp1, 0, 0);
5518
d2ef80f5fd7e use skip_bits where appropriate
alex
parents: 5513
diff changeset
205 skip_bits1(gb);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
206 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
207 tmp1.lengths, sizeof(int), sizeof(int),
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
208 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
209 if(res < 0) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
210 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
211 return -1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
212 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
213 } else {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
214 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
215 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
216 if(get_bits1(gb)){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
217 smacker_decode_tree(gb, &tmp2, 0, 0);
5518
d2ef80f5fd7e use skip_bits where appropriate
alex
parents: 5513
diff changeset
218 skip_bits1(gb);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
219 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
220 tmp2.lengths, sizeof(int), sizeof(int),
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
221 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
222 if(res < 0) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
223 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
224 return -1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
225 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
226 } else {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
227 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
228 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
229
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
230 escapes[0] = get_bits(gb, 8);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
231 escapes[0] |= get_bits(gb, 8) << 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
232 escapes[1] = get_bits(gb, 8);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
233 escapes[1] |= get_bits(gb, 8) << 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
234 escapes[2] = get_bits(gb, 8);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
235 escapes[2] |= get_bits(gb, 8) << 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
236
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
237 last[0] = last[1] = last[2] = -1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
238
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
239 ctx.escapes[0] = escapes[0];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
240 ctx.escapes[1] = escapes[1];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
241 ctx.escapes[2] = escapes[2];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
242 ctx.v1 = &vlc[0];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
243 ctx.v2 = &vlc[1];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
244 ctx.recode1 = tmp1.values;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
245 ctx.recode2 = tmp2.values;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
246 ctx.last = last;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
247
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
248 huff.length = ((size + 3) >> 2) + 3;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
249 huff.maxlength = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
250 huff.current = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
251 huff.values = av_mallocz(huff.length * sizeof(int));
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
252
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
253 smacker_decode_bigtree(gb, &huff, &ctx);
5518
d2ef80f5fd7e use skip_bits where appropriate
alex
parents: 5513
diff changeset
254 skip_bits1(gb);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
255 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
256 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
257 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
258
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
259 *recodes = huff.values;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
260
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
261 if(vlc[0].table)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
262 free_vlc(&vlc[0]);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
263 if(vlc[1].table)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
264 free_vlc(&vlc[1]);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
265 av_free(tmp1.bits);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
266 av_free(tmp1.lengths);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
267 av_free(tmp1.values);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
268 av_free(tmp2.bits);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
269 av_free(tmp2.lengths);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
270 av_free(tmp2.values);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
271
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
272 return 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
273 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
274
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
275 static int decode_header_trees(SmackVContext *smk) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
276 GetBitContext gb;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
277 int mmap_size, mclr_size, full_size, type_size;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
278
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 4283
diff changeset
279 mmap_size = AV_RL32(smk->avctx->extradata);
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 4283
diff changeset
280 mclr_size = AV_RL32(smk->avctx->extradata + 4);
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 4283
diff changeset
281 full_size = AV_RL32(smk->avctx->extradata + 8);
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 4283
diff changeset
282 type_size = AV_RL32(smk->avctx->extradata + 12);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
283
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
284 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
285
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
286 if(!get_bits1(&gb)) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
287 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
288 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
289 smk->mmap_tbl[0] = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
290 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
291 } else {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
292 smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
293 }
5513
9f8219a3b86f use get_bits1(..) instead get_bits(.., 1)
alex
parents: 5215
diff changeset
294 if(!get_bits1(&gb)) {
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
295 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
296 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
297 smk->mclr_tbl[0] = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
298 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
299 } else {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
300 smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
301 }
5513
9f8219a3b86f use get_bits1(..) instead get_bits(.., 1)
alex
parents: 5215
diff changeset
302 if(!get_bits1(&gb)) {
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
303 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
304 smk->full_tbl = av_malloc(sizeof(int) * 2);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
305 smk->full_tbl[0] = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
306 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
307 } else {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
308 smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
309 }
5513
9f8219a3b86f use get_bits1(..) instead get_bits(.., 1)
alex
parents: 5215
diff changeset
310 if(!get_bits1(&gb)) {
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
311 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
312 smk->type_tbl = av_malloc(sizeof(int) * 2);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
313 smk->type_tbl[0] = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
314 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
315 } else {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
316 smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
317 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
318
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
319 return 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
320 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
321
4283
d6f83e2f8804 rename always_inline to av_always_inline and move to common.h
mru
parents: 3947
diff changeset
322 static av_always_inline void last_reset(int *recode, int *last) {
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
323 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
324 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
325
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
326 /* get code and update history */
4283
d6f83e2f8804 rename always_inline to av_always_inline and move to common.h
mru
parents: 3947
diff changeset
327 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
328 register int *table = recode;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
329 int v, b;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
330
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
331 b = get_bits_count(gb);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
332 while(*table & SMK_NODE) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
333 if(get_bits1(gb))
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
334 table += (*table) & (~SMK_NODE);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
335 table++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
336 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
337 v = *table;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
338 b = get_bits_count(gb) - b;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
339
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
340 if(v != recode[last[0]]) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
341 recode[last[2]] = recode[last[1]];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
342 recode[last[1]] = recode[last[0]];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
343 recode[last[0]] = v;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
344 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
345 return v;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
346 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
347
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9219
diff changeset
348 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
349 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9219
diff changeset
350 const uint8_t *buf = avpkt->data;
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9219
diff changeset
351 int buf_size = avpkt->size;
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4801
diff changeset
352 SmackVContext * const smk = avctx->priv_data;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
353 uint8_t *out;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
354 uint32_t *pal;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
355 GetBitContext gb;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
356 int blocks, blk, bw, bh;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
357 int i;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
358 int stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
359
6317
2f44646383c8 100l, since we already check for buf_size == 769 we should also
reimar
parents: 6316
diff changeset
360 if(buf_size <= 769)
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
361 return 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
362 if(smk->pic.data[0])
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
363 avctx->release_buffer(avctx, &smk->pic);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
364
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
365 smk->pic.reference = 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
366 smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
367 if(avctx->reget_buffer(avctx, &smk->pic) < 0){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
368 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
369 return -1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
370 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
371
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
372 /* make the palette available on the way out */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
373 pal = (uint32_t*)smk->pic.data[1];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
374 smk->pic.palette_has_changed = buf[0] & 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
375 smk->pic.key_frame = !!(buf[0] & 2);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
376 if(smk->pic.key_frame)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
377 smk->pic.pict_type = FF_I_TYPE;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
378 else
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
379 smk->pic.pict_type = FF_P_TYPE;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
380
6316
80f5ff30ba58 Use bytestream_get_be24 to simplify palette parsing.
reimar
parents: 6251
diff changeset
381 buf++;
80f5ff30ba58 Use bytestream_get_be24 to simplify palette parsing.
reimar
parents: 6251
diff changeset
382 for(i = 0; i < 256; i++)
80f5ff30ba58 Use bytestream_get_be24 to simplify palette parsing.
reimar
parents: 6251
diff changeset
383 *pal++ = bytestream_get_be24(&buf);
80f5ff30ba58 Use bytestream_get_be24 to simplify palette parsing.
reimar
parents: 6251
diff changeset
384 buf_size -= 769;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
385
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
386 last_reset(smk->mmap_tbl, smk->mmap_last);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
387 last_reset(smk->mclr_tbl, smk->mclr_last);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
388 last_reset(smk->full_tbl, smk->full_last);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
389 last_reset(smk->type_tbl, smk->type_last);
6316
80f5ff30ba58 Use bytestream_get_be24 to simplify palette parsing.
reimar
parents: 6251
diff changeset
390 init_get_bits(&gb, buf, buf_size * 8);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
391
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
392 blk = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
393 bw = avctx->width >> 2;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
394 bh = avctx->height >> 2;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
395 blocks = bw * bh;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
396 out = smk->pic.data[0];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
397 stride = smk->pic.linesize[0];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
398 while(blk < blocks) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
399 int type, run, mode;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
400 uint16_t pix;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
401
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
402 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
403 run = block_runs[(type >> 2) & 0x3F];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
404 switch(type & 3){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
405 case SMK_BLK_MONO:
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
406 while(run-- && blk < blocks){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
407 int clr, map;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
408 int hi, lo;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
409 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
410 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
411 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
412 hi = clr >> 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
413 lo = clr & 0xFF;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
414 for(i = 0; i < 4; i++) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
415 if(map & 1) out[0] = hi; else out[0] = lo;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
416 if(map & 2) out[1] = hi; else out[1] = lo;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
417 if(map & 4) out[2] = hi; else out[2] = lo;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
418 if(map & 8) out[3] = hi; else out[3] = lo;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
419 map >>= 4;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
420 out += stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
421 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
422 blk++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
423 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
424 break;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
425 case SMK_BLK_FULL:
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
426 mode = 0;
3310
48fc664f7348 Now MPlayer should understand Smacker audio and video codecs.
kostya
parents: 3303
diff changeset
427 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
428 if(get_bits1(&gb)) mode = 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
429 else if(get_bits1(&gb)) mode = 2;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
430 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
431 while(run-- && blk < blocks){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
432 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
433 switch(mode){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
434 case 0:
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
435 for(i = 0; i < 4; i++) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
436 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4962
diff changeset
437 AV_WL16(out+2,pix);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
438 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4962
diff changeset
439 AV_WL16(out,pix);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
440 out += stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
441 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
442 break;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
443 case 1:
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
444 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
445 out[0] = out[1] = pix & 0xFF;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
446 out[2] = out[3] = pix >> 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
447 out += stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
448 out[0] = out[1] = pix & 0xFF;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
449 out[2] = out[3] = pix >> 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
450 out += stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
451 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
452 out[0] = out[1] = pix & 0xFF;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
453 out[2] = out[3] = pix >> 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
454 out += stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
455 out[0] = out[1] = pix & 0xFF;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
456 out[2] = out[3] = pix >> 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
457 out += stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
458 break;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
459 case 2:
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
460 for(i = 0; i < 2; i++) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
461 uint16_t pix1, pix2;
8332
57d9d1f7955a Fix one peculiar case in Smacker decoding.
kostya
parents: 8174
diff changeset
462 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
463 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4962
diff changeset
464 AV_WL16(out,pix1);
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4962
diff changeset
465 AV_WL16(out+2,pix2);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
466 out += stride;
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4962
diff changeset
467 AV_WL16(out,pix1);
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4962
diff changeset
468 AV_WL16(out+2,pix2);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
469 out += stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
470 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
471 break;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
472 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
473 blk++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
474 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
475 break;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
476 case SMK_BLK_SKIP:
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
477 while(run-- && blk < blocks)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
478 blk++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
479 break;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
480 case SMK_BLK_FILL:
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
481 mode = type >> 8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
482 while(run-- && blk < blocks){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
483 uint32_t col;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
484 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
485 col = mode * 0x01010101;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
486 for(i = 0; i < 4; i++) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
487 *((uint32_t*)out) = col;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
488 out += stride;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
489 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
490 blk++;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
491 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
492 break;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
493 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
494
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
495 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
496
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
497 *data_size = sizeof(AVFrame);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
498 *(AVFrame*)data = smk->pic;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
499
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
500 /* always report that the buffer was completely consumed */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
501 return buf_size;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
502 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
503
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
504
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
505
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
506 /*
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
507 *
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
508 * Init smacker decoder
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
509 *
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
510 */
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6317
diff changeset
511 static av_cold int decode_init(AVCodecContext *avctx)
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
512 {
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4801
diff changeset
513 SmackVContext * const c = avctx->priv_data;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
514
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
515 c->avctx = avctx;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
516
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
517 avctx->pix_fmt = PIX_FMT_PAL8;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
518
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
519
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
520 /* decode huffman trees from extradata */
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
521 if(avctx->extradata_size < 16){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
522 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
523 return -1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
524 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
525
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
526 decode_header_trees(c);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
527
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
528
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
529 return 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
530 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
531
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
532
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
533
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
534 /*
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
535 *
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
536 * Uninit smacker decoder
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
537 *
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
538 */
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6317
diff changeset
539 static av_cold int decode_end(AVCodecContext *avctx)
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
540 {
4827
b3ee9a1526b0 Get rid of unnecessary pointer casts.
diego
parents: 4801
diff changeset
541 SmackVContext * const smk = avctx->priv_data;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
542
3694
8765ee4eaa45 Drop unneeded checks before av_free() and change to av_freep() where it's more suitable.
kostya
parents: 3310
diff changeset
543 av_freep(&smk->mmap_tbl);
8765ee4eaa45 Drop unneeded checks before av_free() and change to av_freep() where it's more suitable.
kostya
parents: 3310
diff changeset
544 av_freep(&smk->mclr_tbl);
8765ee4eaa45 Drop unneeded checks before av_free() and change to av_freep() where it's more suitable.
kostya
parents: 3310
diff changeset
545 av_freep(&smk->full_tbl);
8765ee4eaa45 Drop unneeded checks before av_free() and change to av_freep() where it's more suitable.
kostya
parents: 3310
diff changeset
546 av_freep(&smk->type_tbl);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
547
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
548 if (smk->pic.data[0])
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
549 avctx->release_buffer(avctx, &smk->pic);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
550
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
551 return 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
552 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
553
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
554
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6317
diff changeset
555 static av_cold int smka_decode_init(AVCodecContext *avctx)
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
556 {
8174
f11197441364 Add channel layout to several audio decoders I maintain
kostya
parents: 7451
diff changeset
557 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
10484
ccad7a2ff75f Set the sample format for Smacker audio in the decoder rather than in the
jbr
parents: 10397
diff changeset
558 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? SAMPLE_FMT_U8 : SAMPLE_FMT_S16;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
559 return 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
560 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
561
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
562 /**
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
563 * Decode Smacker audio data
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
564 */
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9219
diff changeset
565 static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
566 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9219
diff changeset
567 const uint8_t *buf = avpkt->data;
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9219
diff changeset
568 int buf_size = avpkt->size;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
569 GetBitContext gb;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
570 HuffContext h[4];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
571 VLC vlc[4];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
572 int16_t *samples = data;
9219
00a9a7336fc6 Make Smacker audio decoder output audio in original bit depth
kostya
parents: 8718
diff changeset
573 int8_t *samples8 = data;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
574 int val;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
575 int i, res;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
576 int unp_size;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
577 int bits, stereo;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
578 int pred[2] = {0, 0};
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
579
4364
05e932ddaaa9 rename BE/LE_8/16/32 to AV_RL/B_8/16/32
alex
parents: 4283
diff changeset
580 unp_size = AV_RL32(buf);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
581
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
582 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
583
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
584 if(!get_bits1(&gb)){
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
585 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
586 *data_size = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
587 return 1;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
588 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
589 stereo = get_bits1(&gb);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
590 bits = get_bits1(&gb);
9219
00a9a7336fc6 Make Smacker audio decoder output audio in original bit depth
kostya
parents: 8718
diff changeset
591 if (unp_size & 0xC0000000 || unp_size > *data_size) {
5674
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 5518
diff changeset
592 av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 5518
diff changeset
593 return -1;
ca944f1db2b3 Add checks on input/output buffers size for some audio decoders
kostya
parents: 5518
diff changeset
594 }
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
595
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
596 memset(vlc, 0, sizeof(VLC) * 4);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
597 memset(h, 0, sizeof(HuffContext) * 4);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
598 // Initialize
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
599 for(i = 0; i < (1 << (bits + stereo)); i++) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
600 h[i].length = 256;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
601 h[i].maxlength = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
602 h[i].current = 0;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
603 h[i].bits = av_mallocz(256 * 4);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
604 h[i].lengths = av_mallocz(256 * sizeof(int));
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
605 h[i].values = av_mallocz(256 * sizeof(int));
5518
d2ef80f5fd7e use skip_bits where appropriate
alex
parents: 5513
diff changeset
606 skip_bits1(&gb);
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
607 smacker_decode_tree(&gb, &h[i], 0, 0);
5518
d2ef80f5fd7e use skip_bits where appropriate
alex
parents: 5513
diff changeset
608 skip_bits1(&gb);
3220
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
609 if(h[i].current > 1) {
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
610 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
611 h[i].lengths, sizeof(int), sizeof(int),
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
612 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
3220
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
613 if(res < 0) {
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
614 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
615 return -1;
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
616 }
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
617 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
618 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
619 if(bits) { //decode 16-bit data
4693
86a2dfcc5eef Smacker Audio stereo fix
kostya
parents: 4364
diff changeset
620 for(i = stereo; i >= 0; i--)
12129
8b28e74de2c0 Add av_ prefix to bswap macros
mru
parents: 11644
diff changeset
621 pred[i] = av_bswap16(get_bits(&gb, 16));
4693
86a2dfcc5eef Smacker Audio stereo fix
kostya
parents: 4364
diff changeset
622 for(i = 0; i < stereo; i++)
86a2dfcc5eef Smacker Audio stereo fix
kostya
parents: 4364
diff changeset
623 *samples++ = pred[i];
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
624 for(i = 0; i < unp_size / 2; i++) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
625 if(i & stereo) {
3220
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
626 if(vlc[2].table)
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
627 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
628 else
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
629 res = 0;
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
630 val = h[2].values[res];
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
631 if(vlc[3].table)
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
632 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
633 else
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
634 res = 0;
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
635 val |= h[3].values[res] << 8;
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
636 pred[1] += (int16_t)val;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
637 *samples++ = pred[1];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
638 } else {
3220
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
639 if(vlc[0].table)
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
640 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
641 else
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
642 res = 0;
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
643 val = h[0].values[res];
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
644 if(vlc[1].table)
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
645 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
646 else
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
647 res = 0;
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
648 val |= h[1].values[res] << 8;
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
649 pred[0] += val;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
650 *samples++ = pred[0];
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
651 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
652 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
653 } else { //8-bit data
4693
86a2dfcc5eef Smacker Audio stereo fix
kostya
parents: 4364
diff changeset
654 for(i = stereo; i >= 0; i--)
86a2dfcc5eef Smacker Audio stereo fix
kostya
parents: 4364
diff changeset
655 pred[i] = get_bits(&gb, 8);
86a2dfcc5eef Smacker Audio stereo fix
kostya
parents: 4364
diff changeset
656 for(i = 0; i < stereo; i++)
9219
00a9a7336fc6 Make Smacker audio decoder output audio in original bit depth
kostya
parents: 8718
diff changeset
657 *samples8++ = pred[i];
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
658 for(i = 0; i < unp_size; i++) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
659 if(i & stereo){
3220
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
660 if(vlc[1].table)
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
661 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
662 else
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
663 res = 0;
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
664 pred[1] += (int8_t)h[1].values[res];
9219
00a9a7336fc6 Make Smacker audio decoder output audio in original bit depth
kostya
parents: 8718
diff changeset
665 *samples8++ = pred[1];
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
666 } else {
3220
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
667 if(vlc[0].table)
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
668 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
669 else
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
670 res = 0;
a931984ec6ab Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents: 3209
diff changeset
671 pred[0] += (int8_t)h[0].values[res];
9219
00a9a7336fc6 Make Smacker audio decoder output audio in original bit depth
kostya
parents: 8718
diff changeset
672 *samples8++ = pred[0];
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
673 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
674 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
675 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
676
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
677 for(i = 0; i < 4; i++) {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
678 if(vlc[i].table)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
679 free_vlc(&vlc[i]);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
680 if(h[i].bits)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
681 av_free(h[i].bits);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
682 if(h[i].lengths)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
683 av_free(h[i].lengths);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
684 if(h[i].values)
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
685 av_free(h[i].values);
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
686 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
687
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
688 *data_size = unp_size;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
689 return buf_size;
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
690 }
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
691
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
692 AVCodec smacker_decoder = {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
693 "smackvid",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 10484
diff changeset
694 AVMEDIA_TYPE_VIDEO,
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
695 CODEC_ID_SMACKVIDEO,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
696 sizeof(SmackVContext),
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
697 decode_init,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
698 NULL,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
699 decode_end,
6712
5b3acf9fd50a Add long names to AVCodec declarations.
diego
parents: 6517
diff changeset
700 decode_frame,
9807
08b84fcd478c smacker video decoder uses reget_buffer, set CODEC_CAP_DR1
bcoudurier
parents: 9553
diff changeset
701 CODEC_CAP_DR1,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6712
diff changeset
702 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
703 };
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
704
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
705 AVCodec smackaud_decoder = {
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
706 "smackaud",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 10484
diff changeset
707 AVMEDIA_TYPE_AUDIO,
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
708 CODEC_ID_SMACKAUDIO,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
709 0,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
710 smka_decode_init,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
711 NULL,
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
712 NULL,
6712
5b3acf9fd50a Add long names to AVCodec declarations.
diego
parents: 6517
diff changeset
713 smka_decode_frame,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6712
diff changeset
714 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
3209
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
715 };
cc86ebc32143 Smacker demuxer and decoder.
kostya
parents:
diff changeset
716