Mercurial > libavcodec.hg
annotate vp3.c @ 11474:66b2c909d530 libavcodec
vp3: remove unneeded error, this is internal and doesn't happen
author | conrad |
---|---|
date | Sat, 13 Mar 2010 05:59:18 +0000 |
parents | 8b6722b58f15 |
children | b7744b7bee4b |
rev | line source |
---|---|
1224 | 1 /* |
1867
7f7aa6ac3723
cut over to using new VP3 DSP functions and remove the old ones; bring
melanson
parents:
1864
diff
changeset
|
2 * Copyright (C) 2003-2004 the ffmpeg project |
1224 | 3 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3940
diff
changeset
|
4 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3940
diff
changeset
|
5 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3940
diff
changeset
|
6 * FFmpeg is free software; you can redistribute it and/or |
1224 | 7 * modify it under the terms of the GNU Lesser General Public |
8 * 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:
3940
diff
changeset
|
9 * version 2.1 of the License, or (at your option) any later version. |
1224 | 10 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3940
diff
changeset
|
11 * FFmpeg is distributed in the hope that it will be useful, |
1224 | 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * 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:
3940
diff
changeset
|
17 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1224 | 19 */ |
20 | |
21 /** | |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8590
diff
changeset
|
22 * @file libavcodec/vp3.c |
1224 | 23 * On2 VP3 Video Decoder |
2702
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
24 * |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
25 * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx) |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
26 * For more information about the VP3 coding process, visit: |
6747
b9a57d71425d
Add complete multimedia Wiki URL, patch by Sisir Koppaka.
diego
parents:
6710
diff
changeset
|
27 * http://wiki.multimedia.cx/index.php?title=On2_VP3 |
2702
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
28 * |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
29 * Theora decoder by Alex Beregszaszi |
1224 | 30 */ |
31 | |
32 #include <stdio.h> | |
33 #include <stdlib.h> | |
34 #include <string.h> | |
35 | |
36 #include "avcodec.h" | |
37 #include "dsputil.h" | |
9428 | 38 #include "get_bits.h" |
1224 | 39 |
40 #include "vp3data.h" | |
4723
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
41 #include "xiph.h" |
1224 | 42 |
43 #define FRAGMENT_PIXELS 8 | |
44 | |
10258 | 45 static av_cold int vp3_decode_end(AVCodecContext *avctx); |
46 | |
2714
b008c78467e6
use O(number of non zero coeffs) instead of O(number of coeffs) storage for the coefficient colleting/reordering
michael
parents:
2712
diff
changeset
|
47 //FIXME split things out into their own arrays |
1224 | 48 typedef struct Vp3Fragment { |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
49 int16_t dc; |
2706 | 50 uint8_t coding_method; |
51 int8_t motion_x; | |
52 int8_t motion_y; | |
9731 | 53 uint8_t qpi; |
1224 | 54 } Vp3Fragment; |
55 | |
56 #define SB_NOT_CODED 0 | |
57 #define SB_PARTIALLY_CODED 1 | |
58 #define SB_FULLY_CODED 2 | |
59 | |
11226
386c361bf136
Make the special 4129 case for long-run bit strings a #define and explain it
conrad
parents:
11225
diff
changeset
|
60 // This is the maximum length of a single long bit run that can be encoded |
386c361bf136
Make the special 4129 case for long-run bit strings a #define and explain it
conrad
parents:
11225
diff
changeset
|
61 // for superblock coding or block qps. Theora special-cases this to read a |
386c361bf136
Make the special 4129 case for long-run bit strings a #define and explain it
conrad
parents:
11225
diff
changeset
|
62 // bit instead of flipping the current bit to allow for runs longer than 4129. |
386c361bf136
Make the special 4129 case for long-run bit strings a #define and explain it
conrad
parents:
11225
diff
changeset
|
63 #define MAXIMUM_LONG_BIT_RUN 4129 |
386c361bf136
Make the special 4129 case for long-run bit strings a #define and explain it
conrad
parents:
11225
diff
changeset
|
64 |
1224 | 65 #define MODE_INTER_NO_MV 0 |
66 #define MODE_INTRA 1 | |
67 #define MODE_INTER_PLUS_MV 2 | |
68 #define MODE_INTER_LAST_MV 3 | |
69 #define MODE_INTER_PRIOR_LAST 4 | |
70 #define MODE_USING_GOLDEN 5 | |
71 #define MODE_GOLDEN_MV 6 | |
72 #define MODE_INTER_FOURMV 7 | |
73 #define CODING_MODE_COUNT 8 | |
74 | |
75 /* special internal mode */ | |
76 #define MODE_COPY 8 | |
77 | |
78 /* There are 6 preset schemes, plus a free-form scheme */ | |
7139
03fe3194eff7
make ModeAlphabet read-only and use a custom mode alphabet
stefang
parents:
7040
diff
changeset
|
79 static const int ModeAlphabet[6][CODING_MODE_COUNT] = |
1224 | 80 { |
81 /* scheme 1: Last motion vector dominates */ | |
2967 | 82 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, |
1224 | 83 MODE_INTER_PLUS_MV, MODE_INTER_NO_MV, |
2967 | 84 MODE_INTRA, MODE_USING_GOLDEN, |
1224 | 85 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
86 | |
87 /* scheme 2 */ | |
2967 | 88 { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, |
1224 | 89 MODE_INTER_NO_MV, MODE_INTER_PLUS_MV, |
2967 | 90 MODE_INTRA, MODE_USING_GOLDEN, |
1224 | 91 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
92 | |
93 /* scheme 3 */ | |
2967 | 94 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV, |
1224 | 95 MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV, |
2967 | 96 MODE_INTRA, MODE_USING_GOLDEN, |
1224 | 97 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
98 | |
99 /* scheme 4 */ | |
2967 | 100 { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV, |
1224 | 101 MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST, |
2967 | 102 MODE_INTRA, MODE_USING_GOLDEN, |
1224 | 103 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
104 | |
105 /* scheme 5: No motion vector dominates */ | |
2967 | 106 { MODE_INTER_NO_MV, MODE_INTER_LAST_MV, |
1224 | 107 MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV, |
2967 | 108 MODE_INTRA, MODE_USING_GOLDEN, |
1224 | 109 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
110 | |
111 /* scheme 6 */ | |
2967 | 112 { MODE_INTER_NO_MV, MODE_USING_GOLDEN, |
1224 | 113 MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST, |
2967 | 114 MODE_INTER_PLUS_MV, MODE_INTRA, |
1224 | 115 MODE_GOLDEN_MV, MODE_INTER_FOURMV }, |
116 | |
117 }; | |
118 | |
11352 | 119 static const uint8_t hilbert_offset[16][2] = { |
120 {0,0}, {1,0}, {1,1}, {0,1}, | |
121 {0,2}, {0,3}, {1,3}, {1,2}, | |
122 {2,2}, {2,3}, {3,3}, {3,2}, | |
123 {3,1}, {2,1}, {2,0}, {3,0} | |
124 }; | |
125 | |
1224 | 126 #define MIN_DEQUANT_VAL 2 |
127 | |
128 typedef struct Vp3DecodeContext { | |
129 AVCodecContext *avctx; | |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
130 int theora, theora_tables; |
1664 | 131 int version; |
1224 | 132 int width, height; |
133 AVFrame golden_frame; | |
134 AVFrame last_frame; | |
135 AVFrame current_frame; | |
136 int keyframe; | |
137 DSPContext dsp; | |
1626
f74ae637ffa9
finally working with old theora bitstream (flipped image), the only sample I have is decoded successfully (theora.ogg)
alex
parents:
1598
diff
changeset
|
138 int flipped_image; |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
139 int last_slice_end; |
1224 | 140 |
9731 | 141 int qps[3]; |
142 int nqps; | |
143 int last_qps[3]; | |
1224 | 144 |
145 int superblock_count; | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
146 int y_superblock_width; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
147 int y_superblock_height; |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
148 int y_superblock_count; |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
149 int c_superblock_width; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
150 int c_superblock_height; |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
151 int c_superblock_count; |
1224 | 152 int u_superblock_start; |
153 int v_superblock_start; | |
154 unsigned char *superblock_coding; | |
155 | |
156 int macroblock_count; | |
157 int macroblock_width; | |
158 int macroblock_height; | |
159 | |
160 int fragment_count; | |
11473 | 161 int fragment_width[2]; |
162 int fragment_height[2]; | |
1224 | 163 |
164 Vp3Fragment *all_fragments; | |
3498 | 165 int fragment_start[3]; |
11133 | 166 int data_offset[3]; |
2967 | 167 |
2694 | 168 ScanTable scantable; |
2967 | 169 |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
170 /* tables */ |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
171 uint16_t coded_dc_scale_factor[64]; |
1867
7f7aa6ac3723
cut over to using new VP3 DSP functions and remove the old ones; bring
melanson
parents:
1864
diff
changeset
|
172 uint32_t coded_ac_scale_factor[64]; |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
173 uint8_t base_matrix[384][64]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
174 uint8_t qr_count[2][3]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
175 uint8_t qr_size [2][3][64]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
176 uint16_t qr_base[2][3][64]; |
1224 | 177 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
178 /** |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
179 * This is a list of all tokens in bitstream order. Reordering takes place |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
180 * by pulling from each level during IDCT. As a consequence, IDCT must be |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
181 * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
182 * otherwise. The 32 different tokens with up to 12 bits of extradata are |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
183 * collapsed into 3 types, packed as follows: |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
184 * (from the low to high bits) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
185 * |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
186 * 2 bits: type (0,1,2) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
187 * 0: EOB run, 14 bits for run length (12 needed) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
188 * 1: zero run, 7 bits for run length |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
189 * 7 bits for the next coefficient (3 needed) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
190 * 2: coefficient, 14 bits (11 needed) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
191 * |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
192 * Coefficients are signed, so are packed in the highest bits for automatic |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
193 * sign extension. |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
194 */ |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
195 int16_t *dct_tokens[3][64]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
196 int16_t *dct_tokens_base; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
197 #define TOKEN_EOB(eob_run) ((eob_run) << 2) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
198 #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
199 #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
200 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
201 /** |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
202 * number of blocks that contain DCT coefficients at the given level or higher |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
203 */ |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
204 int num_coded_frags[3][64]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
205 int total_num_coded_frags; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
206 |
6903 | 207 /* this is a list of indexes into the all_fragments array indicating |
1224 | 208 * which of the fragments are coded */ |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
209 int *coded_fragment_list[3]; |
10619
d930f99edbf9
Use a list to track which fragments coded in this frame still have
melanson
parents:
10258
diff
changeset
|
210 |
1224 | 211 VLC dc_vlc[16]; |
212 VLC ac_vlc_1[16]; | |
213 VLC ac_vlc_2[16]; | |
214 VLC ac_vlc_3[16]; | |
215 VLC ac_vlc_4[16]; | |
216 | |
2702
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
217 VLC superblock_run_length_vlc; |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
218 VLC fragment_run_length_vlc; |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
219 VLC mode_code_vlc; |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
220 VLC motion_vector_vlc; |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
221 |
1972 | 222 /* these arrays need to be on 16-byte boundaries since SSE2 operations |
223 * index into them */ | |
11369 | 224 DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][plane] |
1224 | 225 |
226 /* This table contains superblock_count * 16 entries. Each set of 16 | |
6903 | 227 * numbers corresponds to the fragment indexes 0..15 of the superblock. |
1224 | 228 * An entry will be -1 to indicate that no entry corresponds to that |
229 * index. */ | |
230 int *superblock_fragments; | |
231 | |
2967 | 232 /* This is an array that indicates how a particular macroblock |
1244
a02df1ba6c7f
fix image buffer leak on keyframes, add more error condition checks
tmmm
parents:
1240
diff
changeset
|
233 * is coded. */ |
1240
c95ff60bc1a1
fix motion vector decoding bug and reinstate interframes
tmmm
parents:
1239
diff
changeset
|
234 unsigned char *macroblock_coding; |
1224 | 235 |
1406 | 236 uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc |
3776 | 237 int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16 |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
238 |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
239 /* Huffman decode */ |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
240 int hti; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
241 unsigned int hbits; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
242 int entries; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
243 int huff_code_size; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
244 uint16_t huffman_table[80][32][2]; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
245 |
7966
14a49e087126
filter_limit_values only needs 7 bits, make its tables smaller
conrad
parents:
7961
diff
changeset
|
246 uint8_t filter_limit_values[64]; |
11369 | 247 DECLARE_ALIGNED(8, int, bounding_values_array)[256+2]; |
1224 | 248 } Vp3DecodeContext; |
249 | |
250 /************************************************************************ | |
251 * VP3 specific functions | |
252 ************************************************************************/ | |
253 | |
254 /* | |
255 * This function sets up all of the various blocks mappings: | |
256 * superblocks <-> fragments, macroblocks <-> fragments, | |
257 * superblocks <-> macroblocks | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
258 * |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
259 * Returns 0 is successful; returns 1 if *anything* went wrong. |
1224 | 260 */ |
2967 | 261 static int init_block_mapping(Vp3DecodeContext *s) |
1224 | 262 { |
11469 | 263 int sb_x, sb_y, plane; |
264 int x, y, i, j = 0; | |
1224 | 265 |
11469 | 266 for (plane = 0; plane < 3; plane++) { |
267 int sb_width = plane ? s->c_superblock_width : s->y_superblock_width; | |
268 int sb_height = plane ? s->c_superblock_height : s->y_superblock_height; | |
11473 | 269 int frag_width = s->fragment_width[!!plane]; |
270 int frag_height = s->fragment_height[!!plane]; | |
1224 | 271 |
11469 | 272 for (sb_y = 0; sb_y < sb_height; sb_y++) |
273 for (sb_x = 0; sb_x < sb_width; sb_x++) | |
274 for (i = 0; i < 16; i++) { | |
275 x = 4*sb_x + hilbert_offset[i][0]; | |
276 y = 4*sb_y + hilbert_offset[i][1]; | |
1224 | 277 |
11469 | 278 if (x < frag_width && y < frag_height) |
279 s->superblock_fragments[j++] = s->fragment_start[plane] + y*frag_width + x; | |
280 else | |
281 s->superblock_fragments[j++] = -1; | |
282 } | |
1224 | 283 } |
284 | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
285 return 0; /* successful path out */ |
1224 | 286 } |
287 | |
288 /* | |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
289 * This function sets up the dequantization tables used for a particular |
1224 | 290 * frame. |
291 */ | |
9731 | 292 static void init_dequantizer(Vp3DecodeContext *s, int qpi) |
1224 | 293 { |
9731 | 294 int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]]; |
295 int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]]; | |
3920
3a151ccc6ed7
Remove unused variables and the corresponding warnings along with them.
diego
parents:
3776
diff
changeset
|
296 int i, plane, inter, qri, bmi, bmj, qistart; |
1224 | 297 |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
298 for(inter=0; inter<2; inter++){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
299 for(plane=0; plane<3; plane++){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
300 int sum=0; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
301 for(qri=0; qri<s->qr_count[inter][plane]; qri++){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
302 sum+= s->qr_size[inter][plane][qri]; |
9731 | 303 if(s->qps[qpi] <= sum) |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
304 break; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
305 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
306 qistart= sum - s->qr_size[inter][plane][qri]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
307 bmi= s->qr_base[inter][plane][qri ]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
308 bmj= s->qr_base[inter][plane][qri+1]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
309 for(i=0; i<64; i++){ |
9731 | 310 int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i] |
311 - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i] | |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
312 + s->qr_size[inter][plane][qri]) |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
313 / (2*s->qr_size[inter][plane][qri]); |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
314 |
3491 | 315 int qmin= 8<<(inter + !i); |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
316 int qscale= i ? ac_scale_factor : dc_scale_factor; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
317 |
9731 | 318 s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096); |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
319 } |
9731 | 320 // all DC coefficients use the same quant so as not to interfere with DC prediction |
321 s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0]; | |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
322 } |
1224 | 323 } |
2967 | 324 |
9731 | 325 memset(s->qscale_table, (FFMAX(s->qmat[0][0][0][1], s->qmat[0][0][1][1])+8)/16, 512); //FIXME finetune |
1224 | 326 } |
327 | |
328 /* | |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
329 * This function initializes the loop filter boundary limits if the frame's |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
330 * quality index is different from the previous frame's. |
9921
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
331 * |
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
332 * The filter_limit_values may not be larger than 127. |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
333 */ |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
334 static void init_loop_filter(Vp3DecodeContext *s) |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
335 { |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
336 int *bounding_values= s->bounding_values_array+127; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
337 int filter_limit; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
338 int x; |
9921
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
339 int value; |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
340 |
9731 | 341 filter_limit = s->filter_limit_values[s->qps[0]]; |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
342 |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
343 /* set up the bounding values */ |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
344 memset(s->bounding_values_array, 0, 256 * sizeof(int)); |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
345 for (x = 0; x < filter_limit; x++) { |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
346 bounding_values[-x] = -x; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
347 bounding_values[x] = x; |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
348 } |
9921
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
349 for (x = value = filter_limit; x < 128 && value; x++, value--) { |
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
350 bounding_values[ x] = value; |
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
351 bounding_values[-x] = -value; |
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
352 } |
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
353 if (value) |
f42402d1778b
Extend init_loop_filter to work for filter limit values up to 127 instead
reimar
parents:
9811
diff
changeset
|
354 bounding_values[128] = value; |
8032 | 355 bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202; |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
356 } |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
357 |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
358 /* |
2967 | 359 * This function unpacks all of the superblock/macroblock/fragment coding |
1224 | 360 * information from the bitstream. |
361 */ | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
362 static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb) |
1224 | 363 { |
11361 | 364 int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start }; |
1224 | 365 int bit = 0; |
366 int current_superblock = 0; | |
367 int current_run = 0; | |
11227
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
368 int num_partial_superblocks = 0; |
1224 | 369 |
370 int i, j; | |
371 int current_fragment; | |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
372 int plane; |
1224 | 373 |
374 if (s->keyframe) { | |
375 memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count); | |
376 | |
377 } else { | |
378 | |
379 /* unpack the list of partially-coded superblocks */ | |
5513 | 380 bit = get_bits1(gb); |
1224 | 381 while (current_superblock < s->superblock_count) { |
2967 | 382 current_run = get_vlc2(gb, |
11225
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
383 s->superblock_run_length_vlc.table, 6, 2) + 1; |
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
384 if (current_run == 34) |
2703
3817945001ce
replace get_superblock_run_length() with a VLC table
melanson
parents:
2702
diff
changeset
|
385 current_run += get_bits(gb, 12); |
1224 | 386 |
11225
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
387 if (current_superblock + current_run > s->superblock_count) { |
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
388 av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n"); |
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
389 return -1; |
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
390 } |
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
391 |
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
392 memset(s->superblock_coding + current_superblock, bit, current_run); |
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
393 |
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
394 current_superblock += current_run; |
11227
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
395 if (bit) |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
396 num_partial_superblocks += current_run; |
11225
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
397 |
11228
6cb4b2e4af1c
Handle Theora's continued runs in superblock coding.
conrad
parents:
11227
diff
changeset
|
398 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN) |
6cb4b2e4af1c
Handle Theora's continued runs in superblock coding.
conrad
parents:
11227
diff
changeset
|
399 bit = get_bits1(gb); |
6cb4b2e4af1c
Handle Theora's continued runs in superblock coding.
conrad
parents:
11227
diff
changeset
|
400 else |
11225
5811a86f55f1
Use memset to set the runs partially coded superblocks
conrad
parents:
11195
diff
changeset
|
401 bit ^= 1; |
1224 | 402 } |
403 | |
404 /* unpack the list of fully coded superblocks if any of the blocks were | |
405 * not marked as partially coded in the previous step */ | |
11227
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
406 if (num_partial_superblocks < s->superblock_count) { |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
407 int superblocks_decoded = 0; |
1224 | 408 |
409 current_superblock = 0; | |
5513 | 410 bit = get_bits1(gb); |
11227
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
411 while (superblocks_decoded < s->superblock_count - num_partial_superblocks) { |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
412 current_run = get_vlc2(gb, |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
413 s->superblock_run_length_vlc.table, 6, 2) + 1; |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
414 if (current_run == 34) |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
415 current_run += get_bits(gb, 12); |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
416 |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
417 for (j = 0; j < current_run; current_superblock++) { |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
418 if (current_superblock >= s->superblock_count) { |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
419 av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n"); |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
420 return -1; |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
421 } |
1224 | 422 |
423 /* skip any superblocks already marked as partially coded */ | |
424 if (s->superblock_coding[current_superblock] == SB_NOT_CODED) { | |
11227
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
425 s->superblock_coding[current_superblock] = 2*bit; |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
426 j++; |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
427 } |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
428 } |
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
429 superblocks_decoded += current_run; |
1224 | 430 |
11228
6cb4b2e4af1c
Handle Theora's continued runs in superblock coding.
conrad
parents:
11227
diff
changeset
|
431 if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN) |
6cb4b2e4af1c
Handle Theora's continued runs in superblock coding.
conrad
parents:
11227
diff
changeset
|
432 bit = get_bits1(gb); |
6cb4b2e4af1c
Handle Theora's continued runs in superblock coding.
conrad
parents:
11227
diff
changeset
|
433 else |
11227
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
434 bit ^= 1; |
1224 | 435 } |
436 } | |
437 | |
438 /* if there were partial blocks, initialize bitstream for | |
439 * unpacking fragment codings */ | |
11227
9aea1eaefe16
Decode fully coded superblocks in the same manner as partial superblocks and qpi
conrad
parents:
11226
diff
changeset
|
440 if (num_partial_superblocks) { |
1224 | 441 |
442 current_run = 0; | |
5513 | 443 bit = get_bits1(gb); |
2967 | 444 /* toggle the bit because as soon as the first run length is |
1224 | 445 * fetched the bit will be toggled again */ |
446 bit ^= 1; | |
447 } | |
448 } | |
449 | |
450 /* figure out which fragments are coded; iterate through each | |
451 * superblock (all planes) */ | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
452 s->total_num_coded_frags = 0; |
1240
c95ff60bc1a1
fix motion vector decoding bug and reinstate interframes
tmmm
parents:
1239
diff
changeset
|
453 memset(s->macroblock_coding, MODE_COPY, s->macroblock_count); |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
454 |
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
455 for (plane = 0; plane < 3; plane++) { |
11361 | 456 int sb_start = superblock_starts[plane]; |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
457 int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count); |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
458 int num_coded_frags = 0; |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
459 |
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
460 for (i = sb_start; i < sb_end; i++) { |
1224 | 461 |
462 /* iterate through all 16 fragments in a superblock */ | |
463 for (j = 0; j < 16; j++) { | |
464 | |
465 /* if the fragment is in bounds, check its coding status */ | |
466 current_fragment = s->superblock_fragments[i * 16 + j]; | |
467 if (current_fragment != -1) { | |
11229 | 468 int coded = s->superblock_coding[i]; |
1224 | 469 |
11229 | 470 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) { |
1224 | 471 |
472 /* fragment may or may not be coded; this is the case | |
473 * that cares about the fragment coding runs */ | |
2705 | 474 if (current_run-- == 0) { |
1224 | 475 bit ^= 1; |
2967 | 476 current_run = get_vlc2(gb, |
2705 | 477 s->fragment_run_length_vlc.table, 5, 2); |
1224 | 478 } |
11229 | 479 coded = bit; |
480 } | |
1224 | 481 |
11229 | 482 if (coded) { |
2967 | 483 /* default mode; actual mode will be decoded in |
1272
777d4145cdfb
fix subtle logic problem in block unpacker that leads to incorrect token
tmmm
parents:
1244
diff
changeset
|
484 * the next phase */ |
2967 | 485 s->all_fragments[current_fragment].coding_method = |
1224 | 486 MODE_INTER_NO_MV; |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
487 s->coded_fragment_list[plane][num_coded_frags++] = |
1224 | 488 current_fragment; |
489 } else { | |
490 /* not coded; copy this fragment from the prior frame */ | |
491 s->all_fragments[current_fragment].coding_method = | |
492 MODE_COPY; | |
493 } | |
494 } | |
495 } | |
496 } | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
497 s->total_num_coded_frags += num_coded_frags; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
498 for (i = 0; i < 64; i++) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
499 s->num_coded_frags[plane][i] = num_coded_frags; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
500 if (plane < 2) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
501 s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + num_coded_frags; |
10619
d930f99edbf9
Use a list to track which fragments coded in this frame still have
melanson
parents:
10258
diff
changeset
|
502 } |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
503 return 0; |
1224 | 504 } |
505 | |
506 /* | |
507 * This function unpacks all the coding mode data for individual macroblocks | |
508 * from the bitstream. | |
509 */ | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
510 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb) |
1224 | 511 { |
11149 | 512 int i, j, k, sb_x, sb_y; |
1224 | 513 int scheme; |
514 int current_macroblock; | |
515 int current_fragment; | |
516 int coding_mode; | |
7139
03fe3194eff7
make ModeAlphabet read-only and use a custom mode alphabet
stefang
parents:
7040
diff
changeset
|
517 int custom_mode_alphabet[CODING_MODE_COUNT]; |
11153 | 518 const int *alphabet; |
1224 | 519 |
520 if (s->keyframe) { | |
521 for (i = 0; i < s->fragment_count; i++) | |
522 s->all_fragments[i].coding_method = MODE_INTRA; | |
523 | |
524 } else { | |
525 | |
526 /* fetch the mode coding scheme for this frame */ | |
527 scheme = get_bits(gb, 3); | |
528 | |
529 /* is it a custom coding scheme? */ | |
530 if (scheme == 0) { | |
531 for (i = 0; i < 8; i++) | |
8736
f973fff63599
VP3: Prevent stack corruption from an unset custom coding method.
alexc
parents:
8718
diff
changeset
|
532 custom_mode_alphabet[i] = MODE_INTER_NO_MV; |
f973fff63599
VP3: Prevent stack corruption from an unset custom coding method.
alexc
parents:
8718
diff
changeset
|
533 for (i = 0; i < 8; i++) |
7139
03fe3194eff7
make ModeAlphabet read-only and use a custom mode alphabet
stefang
parents:
7040
diff
changeset
|
534 custom_mode_alphabet[get_bits(gb, 3)] = i; |
11153 | 535 alphabet = custom_mode_alphabet; |
536 } else | |
537 alphabet = ModeAlphabet[scheme-1]; | |
1224 | 538 |
539 /* iterate through all of the macroblocks that contain 1 or more | |
540 * coded fragments */ | |
11149 | 541 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { |
542 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { | |
1224 | 543 |
544 for (j = 0; j < 4; j++) { | |
11149 | 545 int mb_x = 2*sb_x + (j>>1); |
546 int mb_y = 2*sb_y + (((j>>1)+j)&1); | |
547 current_macroblock = mb_y * s->macroblock_width + mb_x; | |
548 | |
11151 | 549 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height) |
1224 | 550 continue; |
551 | |
11150 | 552 #define BLOCK_X (2*mb_x + (k&1)) |
553 #define BLOCK_Y (2*mb_y + (k>>1)) | |
11151 | 554 /* coding modes are only stored if the macroblock has at least one |
555 * luma block coded, otherwise it must be INTER_NO_MV */ | |
556 for (k = 0; k < 4; k++) { | |
11473 | 557 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X; |
11151 | 558 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) |
559 break; | |
560 } | |
561 if (k == 4) { | |
562 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV; | |
563 continue; | |
564 } | |
11150 | 565 |
1224 | 566 /* mode 7 means get 3 bits for each coding mode */ |
567 if (scheme == 7) | |
568 coding_mode = get_bits(gb, 3); | |
569 else | |
11153 | 570 coding_mode = alphabet |
2702
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
571 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)]; |
1224 | 572 |
1240
c95ff60bc1a1
fix motion vector decoding bug and reinstate interframes
tmmm
parents:
1239
diff
changeset
|
573 s->macroblock_coding[current_macroblock] = coding_mode; |
11150 | 574 for (k = 0; k < 4; k++) { |
2967 | 575 current_fragment = |
11473 | 576 BLOCK_Y*s->fragment_width[0] + BLOCK_X; |
11150 | 577 if (s->all_fragments[current_fragment].coding_method != |
578 MODE_COPY) | |
579 s->all_fragments[current_fragment].coding_method = | |
580 coding_mode; | |
581 } | |
582 for (k = 0; k < 2; k++) { | |
583 current_fragment = s->fragment_start[k+1] + | |
11473 | 584 mb_y*s->fragment_width[1] + mb_x; |
2967 | 585 if (s->all_fragments[current_fragment].coding_method != |
1224 | 586 MODE_COPY) |
587 s->all_fragments[current_fragment].coding_method = | |
588 coding_mode; | |
589 } | |
590 } | |
11149 | 591 } |
1224 | 592 } |
593 } | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
594 |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
595 return 0; |
1229
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
596 } |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
597 |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
598 /* |
1224 | 599 * This function unpacks all the motion vectors for the individual |
600 * macroblocks from the bitstream. | |
601 */ | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
602 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) |
1224 | 603 { |
11152
b3ff7fcfffc4
Directly check whether a fragment is coded for 4MV mode instead of iterating
conrad
parents:
11151
diff
changeset
|
604 int j, k, sb_x, sb_y; |
1224 | 605 int coding_mode; |
11472 | 606 int motion_x[4]; |
607 int motion_y[4]; | |
1224 | 608 int last_motion_x = 0; |
609 int last_motion_y = 0; | |
610 int prior_last_motion_x = 0; | |
611 int prior_last_motion_y = 0; | |
612 int current_macroblock; | |
613 int current_fragment; | |
614 | |
7970 | 615 if (s->keyframe) |
7969 | 616 return 0; |
7971 | 617 |
7972 | 618 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */ |
619 coding_mode = get_bits1(gb); | |
1224 | 620 |
7972 | 621 /* iterate through all of the macroblocks that contain 1 or more |
622 * coded fragments */ | |
11149 | 623 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { |
624 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { | |
1224 | 625 |
7972 | 626 for (j = 0; j < 4; j++) { |
11149 | 627 int mb_x = 2*sb_x + (j>>1); |
628 int mb_y = 2*sb_y + (((j>>1)+j)&1); | |
629 current_macroblock = mb_y * s->macroblock_width + mb_x; | |
630 | |
631 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height || | |
7972 | 632 (s->macroblock_coding[current_macroblock] == MODE_COPY)) |
633 continue; | |
634 | |
635 switch (s->macroblock_coding[current_macroblock]) { | |
636 | |
637 case MODE_INTER_PLUS_MV: | |
638 case MODE_GOLDEN_MV: | |
639 /* all 6 fragments use the same motion vector */ | |
640 if (coding_mode == 0) { | |
641 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; | |
642 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; | |
643 } else { | |
644 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)]; | |
645 motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)]; | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
646 } |
1224 | 647 |
7972 | 648 /* vector maintenance, only on MODE_INTER_PLUS_MV */ |
649 if (s->macroblock_coding[current_macroblock] == | |
650 MODE_INTER_PLUS_MV) { | |
1224 | 651 prior_last_motion_x = last_motion_x; |
652 prior_last_motion_y = last_motion_y; | |
653 last_motion_x = motion_x[0]; | |
654 last_motion_y = motion_y[0]; | |
7972 | 655 } |
656 break; | |
657 | |
658 case MODE_INTER_FOURMV: | |
659 /* vector maintenance */ | |
660 prior_last_motion_x = last_motion_x; | |
661 prior_last_motion_y = last_motion_y; | |
1229
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
662 |
7972 | 663 /* fetch 4 vectors from the bitstream, one for each |
664 * Y fragment, then average for the C fragment vectors */ | |
665 for (k = 0; k < 4; k++) { | |
11473 | 666 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X; |
11152
b3ff7fcfffc4
Directly check whether a fragment is coded for 4MV mode instead of iterating
conrad
parents:
11151
diff
changeset
|
667 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) { |
7972 | 668 if (coding_mode == 0) { |
669 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; | |
670 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; | |
671 } else { | |
672 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)]; | |
673 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)]; | |
674 } | |
675 last_motion_x = motion_x[k]; | |
676 last_motion_y = motion_y[k]; | |
677 } else { | |
678 motion_x[k] = 0; | |
679 motion_y[k] = 0; | |
680 } | |
1224 | 681 } |
7972 | 682 break; |
683 | |
684 case MODE_INTER_LAST_MV: | |
685 /* all 6 fragments use the last motion vector */ | |
686 motion_x[0] = last_motion_x; | |
687 motion_y[0] = last_motion_y; | |
688 | |
689 /* no vector maintenance (last vector remains the | |
690 * last vector) */ | |
691 break; | |
692 | |
693 case MODE_INTER_PRIOR_LAST: | |
694 /* all 6 fragments use the motion vector prior to the | |
695 * last motion vector */ | |
696 motion_x[0] = prior_last_motion_x; | |
697 motion_y[0] = prior_last_motion_y; | |
698 | |
699 /* vector maintenance */ | |
700 prior_last_motion_x = last_motion_x; | |
701 prior_last_motion_y = last_motion_y; | |
702 last_motion_x = motion_x[0]; | |
703 last_motion_y = motion_y[0]; | |
704 break; | |
705 | |
706 default: | |
707 /* covers intra, inter without MV, golden without MV */ | |
10696
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
708 motion_x[0] = 0; |
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
709 motion_y[0] = 0; |
7972 | 710 |
711 /* no vector maintenance */ | |
712 break; | |
713 } | |
714 | |
715 /* assign the motion vectors to the correct fragments */ | |
11150 | 716 for (k = 0; k < 4; k++) { |
7972 | 717 current_fragment = |
11473 | 718 BLOCK_Y*s->fragment_width[0] + BLOCK_X; |
10696
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
719 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { |
10697 | 720 s->all_fragments[current_fragment].motion_x = motion_x[k]; |
721 s->all_fragments[current_fragment].motion_y = motion_y[k]; | |
10696
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
722 } else { |
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
723 s->all_fragments[current_fragment].motion_x = motion_x[0]; |
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
724 s->all_fragments[current_fragment].motion_y = motion_y[0]; |
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
725 } |
1224 | 726 } |
11472 | 727 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { |
728 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2); | |
729 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2); | |
730 } | |
11150 | 731 for (k = 0; k < 2; k++) { |
732 current_fragment = s->fragment_start[k+1] + | |
11473 | 733 mb_y*s->fragment_width[1] + mb_x; |
11150 | 734 s->all_fragments[current_fragment].motion_x = motion_x[0]; |
735 s->all_fragments[current_fragment].motion_y = motion_y[0]; | |
736 } | |
1224 | 737 } |
11149 | 738 } |
7972 | 739 } |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
740 |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
741 return 0; |
1224 | 742 } |
743 | |
9731 | 744 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb) |
745 { | |
746 int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi; | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
747 int num_blocks = s->total_num_coded_frags; |
9731 | 748 |
749 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) { | |
750 i = blocks_decoded = num_blocks_at_qpi = 0; | |
751 | |
752 bit = get_bits1(gb); | |
753 | |
754 do { | |
755 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1; | |
756 if (run_length == 34) | |
757 run_length += get_bits(gb, 12); | |
758 blocks_decoded += run_length; | |
759 | |
760 if (!bit) | |
761 num_blocks_at_qpi += run_length; | |
762 | |
763 for (j = 0; j < run_length; i++) { | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
764 if (i >= s->total_num_coded_frags) |
9731 | 765 return -1; |
766 | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
767 if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
768 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit; |
9731 | 769 j++; |
770 } | |
771 } | |
772 | |
11226
386c361bf136
Make the special 4129 case for long-run bit strings a #define and explain it
conrad
parents:
11225
diff
changeset
|
773 if (run_length == MAXIMUM_LONG_BIT_RUN) |
9731 | 774 bit = get_bits1(gb); |
775 else | |
776 bit ^= 1; | |
777 } while (blocks_decoded < num_blocks); | |
778 | |
779 num_blocks -= num_blocks_at_qpi; | |
780 } | |
781 | |
782 return 0; | |
783 } | |
784 | |
2967 | 785 /* |
1224 | 786 * This function is called by unpack_dct_coeffs() to extract the VLCs from |
787 * the bitstream. The VLCs encode tokens which are used to unpack DCT | |
788 * data. This function unpacks all the VLCs for either the Y plane or both | |
789 * C planes, and is called for DC coefficients or different AC coefficient | |
790 * levels (since different coefficient types require different VLC tables. | |
791 * | |
792 * This function returns a residual eob run. E.g, if a particular token gave | |
793 * instructions to EOB the next 5 fragments and there were only 2 fragments | |
794 * left in the current fragment range, 3 would be returned so that it could | |
795 * be passed into the next call to this same function. | |
796 */ | |
797 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, | |
798 VLC *table, int coeff_index, | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
799 int plane, |
1224 | 800 int eob_run) |
801 { | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
802 int i, j = 0; |
1224 | 803 int token; |
2712
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
804 int zero_run = 0; |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
805 DCTELEM coeff = 0; |
10202
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
806 int bits_to_get; |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
807 int blocks_ended; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
808 int coeff_i = 0; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
809 int num_coeffs = s->num_coded_frags[plane][coeff_index]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
810 int16_t *dct_tokens = s->dct_tokens[plane][coeff_index]; |
10202
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
811 |
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
812 /* local references to structure members to avoid repeated deferences */ |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
813 int *coded_fragment_list = s->coded_fragment_list[plane]; |
10202
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
814 Vp3Fragment *all_fragments = s->all_fragments; |
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
815 VLC_TYPE (*vlc_table)[2] = table->table; |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
816 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
817 if (num_coeffs < 0) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
818 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index); |
1244
a02df1ba6c7f
fix image buffer leak on keyframes, add more error condition checks
tmmm
parents:
1240
diff
changeset
|
819 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
820 if (eob_run > num_coeffs) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
821 coeff_i = blocks_ended = num_coeffs; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
822 eob_run -= num_coeffs; |
10619
d930f99edbf9
Use a list to track which fragments coded in this frame still have
melanson
parents:
10258
diff
changeset
|
823 } else { |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
824 coeff_i = blocks_ended = eob_run; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
825 eob_run = 0; |
1244
a02df1ba6c7f
fix image buffer leak on keyframes, add more error condition checks
tmmm
parents:
1240
diff
changeset
|
826 } |
a02df1ba6c7f
fix image buffer leak on keyframes, add more error condition checks
tmmm
parents:
1240
diff
changeset
|
827 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
828 // insert fake EOB token to cover the split between planes or zzi |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
829 if (blocks_ended) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
830 dct_tokens[j++] = blocks_ended << 2; |
7037
480faa9f79dd
Almost-cosmetics: split out coeff_count from all_fragments struct into
reimar
parents:
6903
diff
changeset
|
831 |
11453 | 832 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) { |
1224 | 833 /* decode a VLC into a token */ |
10202
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
834 token = get_vlc2(gb, vlc_table, 5, 3); |
1224 | 835 /* use the token to get a zero run, a coefficient, and an eob run */ |
2712
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
836 if (token <= 6) { |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
837 eob_run = eob_run_base[token]; |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
838 if (eob_run_get_bits[token]) |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
839 eob_run += get_bits(gb, eob_run_get_bits[token]); |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
840 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
841 // record only the number of blocks ended in this plane, |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
842 // any spill will be recorded in the next plane. |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
843 if (eob_run > num_coeffs - coeff_i) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
844 dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
845 blocks_ended += num_coeffs - coeff_i; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
846 eob_run -= num_coeffs - coeff_i; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
847 coeff_i = num_coeffs; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
848 } else { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
849 dct_tokens[j++] = TOKEN_EOB(eob_run); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
850 blocks_ended += eob_run; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
851 coeff_i += eob_run; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
852 eob_run = 0; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
853 } |
2712
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
854 } else { |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
855 bits_to_get = coeff_get_bits[token]; |
10230
afaf58d1e894
Another micro-optimization for unpack_vlcs(): Eliminate a possible
melanson
parents:
10223
diff
changeset
|
856 if (bits_to_get) |
afaf58d1e894
Another micro-optimization for unpack_vlcs(): Eliminate a possible
melanson
parents:
10223
diff
changeset
|
857 bits_to_get = get_bits(gb, bits_to_get); |
afaf58d1e894
Another micro-optimization for unpack_vlcs(): Eliminate a possible
melanson
parents:
10223
diff
changeset
|
858 coeff = coeff_tables[token][bits_to_get]; |
2712
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
859 |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
860 zero_run = zero_run_base[token]; |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
861 if (zero_run_get_bits[token]) |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
862 zero_run += get_bits(gb, zero_run_get_bits[token]); |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
863 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
864 if (zero_run) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
865 dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
866 } else { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
867 // Save DC into the fragment structure. DC prediction is |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
868 // done in raster order, so the actual DC can't be in with |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
869 // other tokens. We still need the token in dct_tokens[] |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
870 // however, or else the structure collapses on itself. |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
871 if (!coeff_index) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
872 all_fragments[coded_fragment_list[coeff_i]].dc = coeff; |
1224 | 873 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
874 dct_tokens[j++] = TOKEN_COEFF(coeff); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
875 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
876 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
877 if (coeff_index + zero_run > 64) { |
11418 | 878 av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with" |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
879 " %d coeffs left\n", zero_run, 64-coeff_index); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
880 zero_run = 64 - coeff_index; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
881 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
882 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
883 // zero runs code multiple coefficients, |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
884 // so don't try to decode coeffs for those higher levels |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
885 for (i = coeff_index+1; i <= coeff_index+zero_run; i++) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
886 s->num_coded_frags[plane][i]--; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
887 coeff_i++; |
2714
b008c78467e6
use O(number of non zero coeffs) instead of O(number of coeffs) storage for the coefficient colleting/reordering
michael
parents:
2712
diff
changeset
|
888 } |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
889 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
890 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
891 if (blocks_ended > s->num_coded_frags[plane][coeff_index]) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
892 av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n"); |
10619
d930f99edbf9
Use a list to track which fragments coded in this frame still have
melanson
parents:
10258
diff
changeset
|
893 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
894 // decrement the number of blocks that have higher coeffecients for each |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
895 // EOB run at this level |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
896 if (blocks_ended) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
897 for (i = coeff_index+1; i < 64; i++) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
898 s->num_coded_frags[plane][i] -= blocks_ended; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
899 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
900 // setup the next buffer |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
901 if (plane < 2) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
902 s->dct_tokens[plane+1][coeff_index] = dct_tokens + j; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
903 else if (coeff_index < 63) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
904 s->dct_tokens[0][coeff_index+1] = dct_tokens + j; |
1224 | 905 |
906 return eob_run; | |
907 } | |
908 | |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
909 static void reverse_dc_prediction(Vp3DecodeContext *s, |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
910 int first_fragment, |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
911 int fragment_width, |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
912 int fragment_height); |
1224 | 913 /* |
914 * This function unpacks all of the DCT coefficient data from the | |
915 * bitstream. | |
916 */ | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
917 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) |
1224 | 918 { |
919 int i; | |
920 int dc_y_table; | |
921 int dc_c_table; | |
922 int ac_y_table; | |
923 int ac_c_table; | |
924 int residual_eob_run = 0; | |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
925 VLC *y_tables[64]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
926 VLC *c_tables[64]; |
1224 | 927 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
928 s->dct_tokens[0][0] = s->dct_tokens_base; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
929 |
6903 | 930 /* fetch the DC table indexes */ |
1224 | 931 dc_y_table = get_bits(gb, 4); |
932 dc_c_table = get_bits(gb, 4); | |
933 | |
934 /* unpack the Y plane DC coefficients */ | |
2967 | 935 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0, |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
936 0, residual_eob_run); |
1224 | 937 |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
938 /* reverse prediction of the Y-plane DC coefficients */ |
11473 | 939 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]); |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
940 |
1224 | 941 /* unpack the C plane DC coefficients */ |
942 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
943 1, residual_eob_run); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
944 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0, |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
945 2, residual_eob_run); |
1224 | 946 |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
947 /* reverse prediction of the C-plane DC coefficients */ |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
948 if (!(s->avctx->flags & CODEC_FLAG_GRAY)) |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
949 { |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
950 reverse_dc_prediction(s, s->fragment_start[1], |
11473 | 951 s->fragment_width[1], s->fragment_height[1]); |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
952 reverse_dc_prediction(s, s->fragment_start[2], |
11473 | 953 s->fragment_width[1], s->fragment_height[1]); |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
954 } |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
955 |
6903 | 956 /* fetch the AC table indexes */ |
1224 | 957 ac_y_table = get_bits(gb, 4); |
958 ac_c_table = get_bits(gb, 4); | |
959 | |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
960 /* build tables of AC VLC tables */ |
1224 | 961 for (i = 1; i <= 5; i++) { |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
962 y_tables[i] = &s->ac_vlc_1[ac_y_table]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
963 c_tables[i] = &s->ac_vlc_1[ac_c_table]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
964 } |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
965 for (i = 6; i <= 14; i++) { |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
966 y_tables[i] = &s->ac_vlc_2[ac_y_table]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
967 c_tables[i] = &s->ac_vlc_2[ac_c_table]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
968 } |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
969 for (i = 15; i <= 27; i++) { |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
970 y_tables[i] = &s->ac_vlc_3[ac_y_table]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
971 c_tables[i] = &s->ac_vlc_3[ac_c_table]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
972 } |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
973 for (i = 28; i <= 63; i++) { |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
974 y_tables[i] = &s->ac_vlc_4[ac_y_table]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
975 c_tables[i] = &s->ac_vlc_4[ac_c_table]; |
1224 | 976 } |
977 | |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
978 /* decode all AC coefficents */ |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
979 for (i = 1; i <= 63; i++) { |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
980 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i, |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
981 0, residual_eob_run); |
1224 | 982 |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
983 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
984 1, residual_eob_run); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
985 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i, |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
986 2, residual_eob_run); |
1224 | 987 } |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
988 |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
989 return 0; |
1224 | 990 } |
991 | |
992 /* | |
993 * This function reverses the DC prediction for each coded fragment in | |
2967 | 994 * the frame. Much of this function is adapted directly from the original |
1224 | 995 * VP3 source code. |
996 */ | |
997 #define COMPATIBLE_FRAME(x) \ | |
998 (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type) | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
999 #define DC_COEFF(u) s->all_fragments[u].dc |
1224 | 1000 |
1001 static void reverse_dc_prediction(Vp3DecodeContext *s, | |
1002 int first_fragment, | |
1003 int fragment_width, | |
2967 | 1004 int fragment_height) |
1224 | 1005 { |
1006 | |
1007 #define PUL 8 | |
1008 #define PU 4 | |
1009 #define PUR 2 | |
1010 #define PL 1 | |
1011 | |
1012 int x, y; | |
1013 int i = first_fragment; | |
1014 | |
3940 | 1015 int predicted_dc; |
1224 | 1016 |
1017 /* DC values for the left, up-left, up, and up-right fragments */ | |
1018 int vl, vul, vu, vur; | |
1019 | |
6903 | 1020 /* indexes for the left, up-left, up, and up-right fragments */ |
1224 | 1021 int l, ul, u, ur; |
1022 | |
2967 | 1023 /* |
1224 | 1024 * The 6 fields mean: |
1025 * 0: up-left multiplier | |
1026 * 1: up multiplier | |
1027 * 2: up-right multiplier | |
1028 * 3: left multiplier | |
1029 */ | |
10133 | 1030 static const int predictor_transform[16][4] = { |
3501 | 1031 { 0, 0, 0, 0}, |
1032 { 0, 0, 0,128}, // PL | |
1033 { 0, 0,128, 0}, // PUR | |
1034 { 0, 0, 53, 75}, // PUR|PL | |
1035 { 0,128, 0, 0}, // PU | |
1036 { 0, 64, 0, 64}, // PU|PL | |
1037 { 0,128, 0, 0}, // PU|PUR | |
1038 { 0, 0, 53, 75}, // PU|PUR|PL | |
1039 {128, 0, 0, 0}, // PUL | |
1040 { 0, 0, 0,128}, // PUL|PL | |
1041 { 64, 0, 64, 0}, // PUL|PUR | |
1042 { 0, 0, 53, 75}, // PUL|PUR|PL | |
1043 { 0,128, 0, 0}, // PUL|PU | |
1044 {-104,116, 0,116}, // PUL|PU|PL | |
1045 { 24, 80, 24, 0}, // PUL|PU|PUR | |
1046 {-104,116, 0,116} // PUL|PU|PUR|PL | |
1224 | 1047 }; |
1048 | |
1049 /* This table shows which types of blocks can use other blocks for | |
1050 * prediction. For example, INTRA is the only mode in this table to | |
1051 * have a frame number of 0. That means INTRA blocks can only predict | |
2967 | 1052 * from other INTRA blocks. There are 2 golden frame coding types; |
1224 | 1053 * blocks encoding in these modes can only predict from other blocks |
1054 * that were encoded with these 1 of these 2 modes. */ | |
10650 | 1055 static const unsigned char compatible_frame[9] = { |
1224 | 1056 1, /* MODE_INTER_NO_MV */ |
1057 0, /* MODE_INTRA */ | |
1058 1, /* MODE_INTER_PLUS_MV */ | |
1059 1, /* MODE_INTER_LAST_MV */ | |
1060 1, /* MODE_INTER_PRIOR_MV */ | |
1061 2, /* MODE_USING_GOLDEN */ | |
1062 2, /* MODE_GOLDEN_MV */ | |
10650 | 1063 1, /* MODE_INTER_FOUR_MV */ |
1064 3 /* MODE_COPY */ | |
1224 | 1065 }; |
1066 int current_frame_type; | |
1067 | |
1068 /* there is a last DC predictor for each of the 3 frame types */ | |
1069 short last_dc[3]; | |
1070 | |
1071 int transform = 0; | |
1072 | |
1073 vul = vu = vur = vl = 0; | |
1074 last_dc[0] = last_dc[1] = last_dc[2] = 0; | |
1075 | |
1076 /* for each fragment row... */ | |
1077 for (y = 0; y < fragment_height; y++) { | |
1078 | |
1079 /* for each fragment in a row... */ | |
1080 for (x = 0; x < fragment_width; x++, i++) { | |
1081 | |
1082 /* reverse prediction if this block was coded */ | |
1083 if (s->all_fragments[i].coding_method != MODE_COPY) { | |
1084 | |
2967 | 1085 current_frame_type = |
1224 | 1086 compatible_frame[s->all_fragments[i].coding_method]; |
3500 | 1087 |
1088 transform= 0; | |
1089 if(x){ | |
1090 l= i-1; | |
2714
b008c78467e6
use O(number of non zero coeffs) instead of O(number of coeffs) storage for the coefficient colleting/reordering
michael
parents:
2712
diff
changeset
|
1091 vl = DC_COEFF(l); |
10650 | 1092 if(COMPATIBLE_FRAME(l)) |
3501 | 1093 transform |= PL; |
3500 | 1094 } |
1095 if(y){ | |
1096 u= i-fragment_width; | |
2714
b008c78467e6
use O(number of non zero coeffs) instead of O(number of coeffs) storage for the coefficient colleting/reordering
michael
parents:
2712
diff
changeset
|
1097 vu = DC_COEFF(u); |
10650 | 1098 if(COMPATIBLE_FRAME(u)) |
3501 | 1099 transform |= PU; |
3500 | 1100 if(x){ |
1101 ul= i-fragment_width-1; | |
1102 vul = DC_COEFF(ul); | |
10650 | 1103 if(COMPATIBLE_FRAME(ul)) |
3501 | 1104 transform |= PUL; |
3500 | 1105 } |
1106 if(x + 1 < fragment_width){ | |
1107 ur= i-fragment_width+1; | |
1108 vur = DC_COEFF(ur); | |
10650 | 1109 if(COMPATIBLE_FRAME(ur)) |
3501 | 1110 transform |= PUR; |
3500 | 1111 } |
1224 | 1112 } |
1113 | |
1114 if (transform == 0) { | |
1115 | |
1116 /* if there were no fragments to predict from, use last | |
1117 * DC saved */ | |
2714
b008c78467e6
use O(number of non zero coeffs) instead of O(number of coeffs) storage for the coefficient colleting/reordering
michael
parents:
2712
diff
changeset
|
1118 predicted_dc = last_dc[current_frame_type]; |
1224 | 1119 } else { |
1120 | |
1121 /* apply the appropriate predictor transform */ | |
1122 predicted_dc = | |
1123 (predictor_transform[transform][0] * vul) + | |
1124 (predictor_transform[transform][1] * vu) + | |
1125 (predictor_transform[transform][2] * vur) + | |
1126 (predictor_transform[transform][3] * vl); | |
1127 | |
3502 | 1128 predicted_dc /= 128; |
1224 | 1129 |
1130 /* check for outranging on the [ul u l] and | |
1131 * [ul u ur l] predictors */ | |
10649
a34d38ec5a0b
Check transform==15 first, since it's more common than 13.
cehoyos
parents:
10648
diff
changeset
|
1132 if ((transform == 15) || (transform == 13)) { |
4001 | 1133 if (FFABS(predicted_dc - vu) > 128) |
1224 | 1134 predicted_dc = vu; |
4001 | 1135 else if (FFABS(predicted_dc - vl) > 128) |
1224 | 1136 predicted_dc = vl; |
4001 | 1137 else if (FFABS(predicted_dc - vul) > 128) |
1224 | 1138 predicted_dc = vul; |
1139 } | |
1140 } | |
1141 | |
2714
b008c78467e6
use O(number of non zero coeffs) instead of O(number of coeffs) storage for the coefficient colleting/reordering
michael
parents:
2712
diff
changeset
|
1142 /* at long last, apply the predictor */ |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1143 DC_COEFF(i) += predicted_dc; |
1224 | 1144 /* save the DC */ |
2714
b008c78467e6
use O(number of non zero coeffs) instead of O(number of coeffs) storage for the coefficient colleting/reordering
michael
parents:
2712
diff
changeset
|
1145 last_dc[current_frame_type] = DC_COEFF(i); |
1224 | 1146 } |
1147 } | |
1148 } | |
1149 } | |
1150 | |
11130 | 1151 static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend) |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1152 { |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1153 int x, y; |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1154 int *bounding_values= s->bounding_values_array+127; |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1155 |
11473 | 1156 int width = s->fragment_width[!!plane]; |
1157 int height = s->fragment_height[!!plane]; | |
11131 | 1158 int fragment = s->fragment_start [plane] + ystart * width; |
1159 int stride = s->current_frame.linesize[plane]; | |
1160 uint8_t *plane_data = s->current_frame.data [plane]; | |
1161 if (!s->flipped_image) stride = -stride; | |
11133 | 1162 plane_data += s->data_offset[plane] + 8*ystart*stride; |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1163 |
11131 | 1164 for (y = ystart; y < yend; y++) { |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1165 |
11131 | 1166 for (x = 0; x < width; x++) { |
1167 /* This code basically just deblocks on the edges of coded blocks. | |
1168 * However, it has to be much more complicated because of the | |
1169 * braindamaged deblock ordering used in VP3/Theora. Order matters | |
1170 * because some pixels get filtered twice. */ | |
1171 if( s->all_fragments[fragment].coding_method != MODE_COPY ) | |
1172 { | |
1173 /* do not perform left edge filter for left columns frags */ | |
1174 if (x > 0) { | |
1175 s->dsp.vp3_h_loop_filter( | |
11133 | 1176 plane_data + 8*x, |
11131 | 1177 stride, bounding_values); |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1178 } |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1179 |
11131 | 1180 /* do not perform top edge filter for top row fragments */ |
1181 if (y > 0) { | |
1182 s->dsp.vp3_v_loop_filter( | |
11133 | 1183 plane_data + 8*x, |
11131 | 1184 stride, bounding_values); |
1185 } | |
1186 | |
1187 /* do not perform right edge filter for right column | |
1188 * fragments or if right fragment neighbor is also coded | |
1189 * in this frame (it will be filtered in next iteration) */ | |
1190 if ((x < width - 1) && | |
1191 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) { | |
1192 s->dsp.vp3_h_loop_filter( | |
11133 | 1193 plane_data + 8*x + 8, |
11131 | 1194 stride, bounding_values); |
1195 } | |
1196 | |
1197 /* do not perform bottom edge filter for bottom row | |
1198 * fragments or if bottom fragment neighbor is also coded | |
1199 * in this frame (it will be filtered in the next row) */ | |
1200 if ((y < height - 1) && | |
1201 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) { | |
1202 s->dsp.vp3_v_loop_filter( | |
11133 | 1203 plane_data + 8*x + 8*stride, |
11131 | 1204 stride, bounding_values); |
1205 } | |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1206 } |
11131 | 1207 |
1208 fragment++; | |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1209 } |
11133 | 1210 plane_data += 8*stride; |
11131 | 1211 } |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1212 } |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1213 |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1214 /** |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1215 * Pulls DCT tokens from the 64 levels to decode and dequant the coefficients |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1216 * for the next block in coding order |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1217 */ |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1218 static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag, |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1219 int plane, int inter, DCTELEM block[64]) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1220 { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1221 int16_t *dequantizer = s->qmat[frag->qpi][inter][plane]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1222 uint8_t *perm = s->scantable.permutated; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1223 int i = 0; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1224 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1225 do { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1226 int token = *s->dct_tokens[plane][i]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1227 switch (token & 3) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1228 case 0: // EOB |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1229 if (--token < 4) // 0-3 are token types, so the EOB run must now be 0 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1230 s->dct_tokens[plane][i]++; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1231 else |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1232 *s->dct_tokens[plane][i] = token & ~3; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1233 goto end; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1234 case 1: // zero run |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1235 s->dct_tokens[plane][i]++; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1236 i += (token >> 2) & 0x7f; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1237 block[perm[i]] = (token >> 9) * dequantizer[perm[i]]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1238 i++; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1239 break; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1240 case 2: // coeff |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1241 block[perm[i]] = (token >> 2) * dequantizer[perm[i]]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1242 s->dct_tokens[plane][i++]++; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1243 break; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1244 default: |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1245 av_log(s->avctx, AV_LOG_ERROR, "internal: invalid token type\n"); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1246 return i; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1247 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1248 } while (i < 64); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1249 end: |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1250 // the actual DC+prediction is in the fragment structure |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1251 block[0] = frag->dc * s->qmat[0][inter][plane][0]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1252 return i; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1253 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1254 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1255 /** |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1256 * called when all pixels up to row y are complete |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1257 */ |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1258 static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y) |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1259 { |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1260 int h, cy; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1261 int offset[4]; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1262 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1263 if(s->avctx->draw_horiz_band==NULL) |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1264 return; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1265 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1266 h= y - s->last_slice_end; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1267 y -= h; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1268 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1269 if (!s->flipped_image) { |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1270 if (y == 0) |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1271 h -= s->height - s->avctx->height; // account for non-mod16 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1272 y = s->height - y - h; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1273 } |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1274 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1275 cy = y >> 1; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1276 offset[0] = s->current_frame.linesize[0]*y; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1277 offset[1] = s->current_frame.linesize[1]*cy; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1278 offset[2] = s->current_frame.linesize[2]*cy; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1279 offset[3] = 0; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1280 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1281 emms_c(); |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1282 s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h); |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1283 s->last_slice_end= y + h; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1284 } |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1285 |
1224 | 1286 /* |
2722 | 1287 * Perform the final rendering for a particular slice of data. |
11352 | 1288 * The slice number ranges from 0..(c_superblock_height - 1). |
2722 | 1289 */ |
1290 static void render_slice(Vp3DecodeContext *s, int slice) | |
1291 { | |
11352 | 1292 int x, y, i, j; |
11195 | 1293 LOCAL_ALIGNED_16(DCTELEM, block, [64]); |
2722 | 1294 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef; |
1295 int motion_halfpel_index; | |
1296 uint8_t *motion_source; | |
11352 | 1297 int plane, first_pixel; |
2722 | 1298 |
11352 | 1299 if (slice >= s->c_superblock_height) |
2722 | 1300 return; |
1301 | |
1302 for (plane = 0; plane < 3; plane++) { | |
11133 | 1303 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane]; |
1304 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane]; | |
1305 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane]; | |
3498 | 1306 int stride = s->current_frame.linesize[plane]; |
1307 int plane_width = s->width >> !!plane; | |
1308 int plane_height = s->height >> !!plane; | |
11352 | 1309 |
1310 int sb_x, sb_y = slice << !plane; | |
1311 int slice_height = sb_y + (plane ? 1 : 2); | |
1312 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width; | |
1313 | |
11473 | 1314 int fragment_width = s->fragment_width[!!plane]; |
1315 int fragment_height = s->fragment_height[!!plane]; | |
11352 | 1316 int fragment_start = s->fragment_start[plane]; |
3498 | 1317 |
1318 if (!s->flipped_image) stride = -stride; | |
11154 | 1319 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY)) |
1320 continue; | |
3498 | 1321 |
2967 | 1322 |
4001 | 1323 if(FFABS(stride) > 2048) |
2722 | 1324 return; //various tables are fixed size |
1325 | |
11352 | 1326 /* for each superblock row in the slice (both of them)... */ |
1327 for (; sb_y < slice_height; sb_y++) { | |
1328 | |
1329 /* for each superblock in a row... */ | |
1330 for (sb_x = 0; sb_x < slice_width; sb_x++) { | |
2722 | 1331 |
11352 | 1332 /* for each block in a superblock... */ |
1333 for (j = 0; j < 16; j++) { | |
1334 x = 4*sb_x + hilbert_offset[j][0]; | |
1335 y = 4*sb_y + hilbert_offset[j][1]; | |
2722 | 1336 |
11352 | 1337 i = fragment_start + y*fragment_width + x; |
1338 | |
1339 // bounds check | |
1340 if (x >= fragment_width || y >= fragment_height) | |
1341 continue; | |
1342 | |
1343 first_pixel = 8*y*stride + 8*x; | |
2722 | 1344 |
1345 /* transform if this block was coded */ | |
11154 | 1346 if (s->all_fragments[i].coding_method != MODE_COPY) { |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1347 int intra = s->all_fragments[i].coding_method == MODE_INTRA; |
2722 | 1348 |
1349 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) || | |
1350 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) | |
1351 motion_source= golden_plane; | |
2967 | 1352 else |
2722 | 1353 motion_source= last_plane; |
1354 | |
11133 | 1355 motion_source += first_pixel; |
2722 | 1356 motion_halfpel_index = 0; |
1357 | |
1358 /* sort out the motion vector if this fragment is coded | |
1359 * using a motion vector method */ | |
1360 if ((s->all_fragments[i].coding_method > MODE_INTRA) && | |
1361 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) { | |
1362 int src_x, src_y; | |
1363 motion_x = s->all_fragments[i].motion_x; | |
1364 motion_y = s->all_fragments[i].motion_y; | |
1365 if(plane){ | |
1366 motion_x= (motion_x>>1) | (motion_x&1); | |
1367 motion_y= (motion_y>>1) | (motion_y&1); | |
1368 } | |
1369 | |
11352 | 1370 src_x= (motion_x>>1) + 8*x; |
1371 src_y= (motion_y>>1) + 8*y; | |
2722 | 1372 |
1373 motion_halfpel_index = motion_x & 0x01; | |
1374 motion_source += (motion_x >> 1); | |
1375 | |
1376 motion_halfpel_index |= (motion_y & 0x01) << 1; | |
1377 motion_source += ((motion_y >> 1) * stride); | |
1378 | |
1379 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){ | |
1380 uint8_t *temp= s->edge_emu_buffer; | |
1381 if(stride<0) temp -= 9*stride; | |
1382 else temp += 9*stride; | |
1383 | |
1384 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height); | |
1385 motion_source= temp; | |
1386 } | |
1387 } | |
2967 | 1388 |
2722 | 1389 |
1390 /* first, take care of copying a block from either the | |
1391 * previous or the golden frame */ | |
1392 if (s->all_fragments[i].coding_method != MODE_INTRA) { | |
2967 | 1393 /* Note, it is possible to implement all MC cases with |
1394 put_no_rnd_pixels_l2 which would look more like the | |
1395 VP3 source but this would be slower as | |
2722 | 1396 put_no_rnd_pixels_tab is better optimzed */ |
1397 if(motion_halfpel_index != 3){ | |
1398 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index]( | |
11133 | 1399 output_plane + first_pixel, |
2722 | 1400 motion_source, stride, 8); |
1401 }else{ | |
1402 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1 | |
1403 s->dsp.put_no_rnd_pixels_l2[1]( | |
11133 | 1404 output_plane + first_pixel, |
2967 | 1405 motion_source - d, |
1406 motion_source + stride + 1 + d, | |
2722 | 1407 stride, 8); |
1408 } | |
1409 } | |
1410 | |
8288 | 1411 s->dsp.clear_block(block); |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1412 vp3_dequant(s, s->all_fragments + i, plane, !intra, block); |
2722 | 1413 |
1414 /* invert DCT and place (or add) in final output */ | |
2967 | 1415 |
2722 | 1416 if (s->all_fragments[i].coding_method == MODE_INTRA) { |
1417 if(s->avctx->idct_algo!=FF_IDCT_VP3) | |
1418 block[0] += 128<<3; | |
1419 s->dsp.idct_put( | |
11133 | 1420 output_plane + first_pixel, |
2722 | 1421 stride, |
1422 block); | |
1423 } else { | |
1424 s->dsp.idct_add( | |
11133 | 1425 output_plane + first_pixel, |
2722 | 1426 stride, |
1427 block); | |
1428 } | |
1429 } else { | |
1430 | |
1431 /* copy directly from the previous frame */ | |
1432 s->dsp.put_pixels_tab[1][0]( | |
11133 | 1433 output_plane + first_pixel, |
1434 last_plane + first_pixel, | |
2722 | 1435 stride, 8); |
1436 | |
1437 } | |
11352 | 1438 } |
2722 | 1439 } |
11352 | 1440 |
1441 // Filter up to the last row in the superblock row | |
1442 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1)); | |
2722 | 1443 } |
1444 } | |
1445 | |
1446 /* this looks like a good place for slice dispatch... */ | |
1447 /* algorithm: | |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
1448 * if (slice == s->macroblock_height - 1) |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
1449 * dispatch (both last slice & 2nd-to-last slice); |
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
1450 * else if (slice > 0) |
2722 | 1451 * dispatch (slice - 1); |
1452 */ | |
1453 | |
11430 | 1454 vp3_draw_horiz_band(s, FFMIN(64*slice + 64-16, s->height-16)); |
2722 | 1455 } |
1456 | |
2967 | 1457 /* |
1224 | 1458 * This is the ffmpeg/libavcodec API init function. |
1459 */ | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6448
diff
changeset
|
1460 static av_cold int vp3_decode_init(AVCodecContext *avctx) |
1224 | 1461 { |
1462 Vp3DecodeContext *s = avctx->priv_data; | |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1463 int i, inter, plane; |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1464 int c_width; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1465 int c_height; |
1224 | 1466 |
1664 | 1467 if (avctx->codec_tag == MKTAG('V','P','3','0')) |
2979 | 1468 s->version = 0; |
1664 | 1469 else |
2979 | 1470 s->version = 1; |
1664 | 1471 |
1224 | 1472 s->avctx = avctx; |
9686
bc32976d6d9d
Move ALIGN macro to libavutil/common.h and use it in various places
conrad
parents:
9637
diff
changeset
|
1473 s->width = FFALIGN(avctx->width, 16); |
bc32976d6d9d
Move ALIGN macro to libavutil/common.h and use it in various places
conrad
parents:
9637
diff
changeset
|
1474 s->height = FFALIGN(avctx->height, 16); |
1224 | 1475 avctx->pix_fmt = PIX_FMT_YUV420P; |
9626
bd3e11b60ccd
Add a chroma_sample_location field to define positioning of chroma samples
conrad
parents:
9591
diff
changeset
|
1476 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; |
2693 | 1477 if(avctx->idct_algo==FF_IDCT_AUTO) |
1478 avctx->idct_algo=FF_IDCT_VP3; | |
1224 | 1479 dsputil_init(&s->dsp, avctx); |
2967 | 1480 |
2694 | 1481 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); |
1224 | 1482 |
1483 /* initialize to an impossible value which will force a recalculation | |
1484 * in the first frame decode */ | |
9731 | 1485 for (i = 0; i < 3; i++) |
1486 s->qps[i] = -1; | |
1224 | 1487 |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1488 s->y_superblock_width = (s->width + 31) / 32; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1489 s->y_superblock_height = (s->height + 31) / 32; |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1490 s->y_superblock_count = s->y_superblock_width * s->y_superblock_height; |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1491 |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1492 /* work out the dimensions for the C planes */ |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1493 c_width = s->width / 2; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1494 c_height = s->height / 2; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1495 s->c_superblock_width = (c_width + 31) / 32; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1496 s->c_superblock_height = (c_height + 31) / 32; |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1497 s->c_superblock_count = s->c_superblock_width * s->c_superblock_height; |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1498 |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1499 s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2); |
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1500 s->u_superblock_start = s->y_superblock_count; |
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1501 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count; |
1224 | 1502 s->superblock_coding = av_malloc(s->superblock_count); |
1503 | |
1504 s->macroblock_width = (s->width + 15) / 16; | |
1505 s->macroblock_height = (s->height + 15) / 16; | |
1506 s->macroblock_count = s->macroblock_width * s->macroblock_height; | |
1507 | |
11473 | 1508 s->fragment_width[0] = s->width / FRAGMENT_PIXELS; |
1509 s->fragment_height[0] = s->height / FRAGMENT_PIXELS; | |
1510 s->fragment_width[1] = s->fragment_width[0] >> 1; | |
1511 s->fragment_height[1] = s->fragment_height[0] >> 1; | |
1224 | 1512 |
1513 /* fragment count covers all 8x8 blocks for all 3 planes */ | |
11473 | 1514 s->fragment_count = s->fragment_width[0] * s->fragment_height[0] * 3 / 2; |
1515 s->fragment_start[1] = s->fragment_width[0] * s->fragment_height[0]; | |
1516 s->fragment_start[2] = s->fragment_width[0] * s->fragment_height[0] * 5 / 4; | |
1224 | 1517 |
1518 s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment)); | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1519 s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int)); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1520 s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base)); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1521 if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base || |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1522 !s->coded_fragment_list[0]) { |
10258 | 1523 vp3_decode_end(avctx); |
1524 return -1; | |
1525 } | |
1224 | 1526 |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1527 if (!s->theora_tables) |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1528 { |
3585 | 1529 for (i = 0; i < 64; i++) { |
2979 | 1530 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i]; |
1531 s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i]; | |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1532 s->base_matrix[0][i] = vp31_intra_y_dequant[i]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1533 s->base_matrix[1][i] = vp31_intra_c_dequant[i]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1534 s->base_matrix[2][i] = vp31_inter_dequant[i]; |
2979 | 1535 s->filter_limit_values[i] = vp31_filter_limit_values[i]; |
3585 | 1536 } |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1537 |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1538 for(inter=0; inter<2; inter++){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1539 for(plane=0; plane<3; plane++){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1540 s->qr_count[inter][plane]= 1; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1541 s->qr_size [inter][plane][0]= 63; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1542 s->qr_base [inter][plane][0]= |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1543 s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1544 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1545 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1546 |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1547 /* init VLC tables */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1548 for (i = 0; i < 16; i++) { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1549 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1550 /* DC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1551 init_vlc(&s->dc_vlc[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1552 &dc_bias[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1553 &dc_bias[i][0][0], 4, 2, 0); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1554 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1555 /* group 1 AC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1556 init_vlc(&s->ac_vlc_1[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1557 &ac_bias_0[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1558 &ac_bias_0[i][0][0], 4, 2, 0); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1559 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1560 /* group 2 AC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1561 init_vlc(&s->ac_vlc_2[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1562 &ac_bias_1[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1563 &ac_bias_1[i][0][0], 4, 2, 0); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1564 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1565 /* group 3 AC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1566 init_vlc(&s->ac_vlc_3[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1567 &ac_bias_2[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1568 &ac_bias_2[i][0][0], 4, 2, 0); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1569 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1570 /* group 4 AC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1571 init_vlc(&s->ac_vlc_4[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1572 &ac_bias_3[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1573 &ac_bias_3[i][0][0], 4, 2, 0); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1574 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1575 } else { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1576 for (i = 0; i < 16; i++) { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1577 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1578 /* DC histograms */ |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1579 if (init_vlc(&s->dc_vlc[i], 5, 32, |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1580 &s->huffman_table[i][0][1], 4, 2, |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1581 &s->huffman_table[i][0][0], 4, 2, 0) < 0) |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1582 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1583 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1584 /* group 1 AC histograms */ |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1585 if (init_vlc(&s->ac_vlc_1[i], 5, 32, |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1586 &s->huffman_table[i+16][0][1], 4, 2, |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1587 &s->huffman_table[i+16][0][0], 4, 2, 0) < 0) |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1588 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1589 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1590 /* group 2 AC histograms */ |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1591 if (init_vlc(&s->ac_vlc_2[i], 5, 32, |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1592 &s->huffman_table[i+16*2][0][1], 4, 2, |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1593 &s->huffman_table[i+16*2][0][0], 4, 2, 0) < 0) |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1594 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1595 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1596 /* group 3 AC histograms */ |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1597 if (init_vlc(&s->ac_vlc_3[i], 5, 32, |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1598 &s->huffman_table[i+16*3][0][1], 4, 2, |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1599 &s->huffman_table[i+16*3][0][0], 4, 2, 0) < 0) |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1600 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1601 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1602 /* group 4 AC histograms */ |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1603 if (init_vlc(&s->ac_vlc_4[i], 5, 32, |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1604 &s->huffman_table[i+16*4][0][1], 4, 2, |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1605 &s->huffman_table[i+16*4][0][0], 4, 2, 0) < 0) |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1606 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1607 } |
1224 | 1608 } |
1609 | |
2703
3817945001ce
replace get_superblock_run_length() with a VLC table
melanson
parents:
2702
diff
changeset
|
1610 init_vlc(&s->superblock_run_length_vlc, 6, 34, |
3817945001ce
replace get_superblock_run_length() with a VLC table
melanson
parents:
2702
diff
changeset
|
1611 &superblock_run_length_vlc_table[0][1], 4, 2, |
3817945001ce
replace get_superblock_run_length() with a VLC table
melanson
parents:
2702
diff
changeset
|
1612 &superblock_run_length_vlc_table[0][0], 4, 2, 0); |
3817945001ce
replace get_superblock_run_length() with a VLC table
melanson
parents:
2702
diff
changeset
|
1613 |
2942
868c48736d1c
fixed long standing off-by-one bug (fixes playback on ppc)
alex
parents:
2866
diff
changeset
|
1614 init_vlc(&s->fragment_run_length_vlc, 5, 30, |
2702
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1615 &fragment_run_length_vlc_table[0][1], 4, 2, |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1616 &fragment_run_length_vlc_table[0][0], 4, 2, 0); |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1617 |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1618 init_vlc(&s->mode_code_vlc, 3, 8, |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1619 &mode_code_vlc_table[0][1], 2, 1, |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1620 &mode_code_vlc_table[0][0], 2, 1, 0); |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1621 |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1622 init_vlc(&s->motion_vector_vlc, 6, 63, |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1623 &motion_vector_vlc_table[0][1], 2, 1, |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1624 &motion_vector_vlc_table[0][0], 2, 1, 0); |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1625 |
1224 | 1626 /* work out the block mapping tables */ |
1627 s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int)); | |
1240
c95ff60bc1a1
fix motion vector decoding bug and reinstate interframes
tmmm
parents:
1239
diff
changeset
|
1628 s->macroblock_coding = av_malloc(s->macroblock_count + 1); |
11150 | 1629 if (!s->superblock_fragments || !s->macroblock_coding) { |
10258 | 1630 vp3_decode_end(avctx); |
1631 return -1; | |
1632 } | |
1224 | 1633 init_block_mapping(s); |
1634 | |
1229
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1635 for (i = 0; i < 3; i++) { |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1636 s->current_frame.data[i] = NULL; |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1637 s->last_frame.data[i] = NULL; |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1638 s->golden_frame.data[i] = NULL; |
1227
184c480cefc3
fix decoder so that ffmpeg does not crash, at least not right away
tmmm
parents:
1224
diff
changeset
|
1639 } |
184c480cefc3
fix decoder so that ffmpeg does not crash, at least not right away
tmmm
parents:
1224
diff
changeset
|
1640 |
1224 | 1641 return 0; |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1642 |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1643 vlc_fail: |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1644 av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n"); |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1645 return -1; |
1224 | 1646 } |
1647 | |
1648 /* | |
1649 * This is the ffmpeg/libavcodec API frame decode function. | |
1650 */ | |
2967 | 1651 static int vp3_decode_frame(AVCodecContext *avctx, |
1224 | 1652 void *data, int *data_size, |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
9007
diff
changeset
|
1653 AVPacket *avpkt) |
1224 | 1654 { |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
9007
diff
changeset
|
1655 const uint8_t *buf = avpkt->data; |
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
9007
diff
changeset
|
1656 int buf_size = avpkt->size; |
1224 | 1657 Vp3DecodeContext *s = avctx->priv_data; |
1658 GetBitContext gb; | |
1659 static int counter = 0; | |
2722 | 1660 int i; |
1224 | 1661 |
1662 init_get_bits(&gb, buf, buf_size * 8); | |
2967 | 1663 |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1664 if (s->theora && get_bits1(&gb)) |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1665 { |
2979 | 1666 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n"); |
1667 return -1; | |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1668 } |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1669 |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1670 s->keyframe = !get_bits1(&gb); |
1664 | 1671 if (!s->theora) |
2979 | 1672 skip_bits(&gb, 1); |
9731 | 1673 for (i = 0; i < 3; i++) |
1674 s->last_qps[i] = s->qps[i]; | |
3492
fa3794c46f21
parse all QIS values (we still ignore them though ...)
michael
parents:
3491
diff
changeset
|
1675 |
9731 | 1676 s->nqps=0; |
3492
fa3794c46f21
parse all QIS values (we still ignore them though ...)
michael
parents:
3491
diff
changeset
|
1677 do{ |
9731 | 1678 s->qps[s->nqps++]= get_bits(&gb, 6); |
1679 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb)); | |
1680 for (i = s->nqps; i < 3; i++) | |
1681 s->qps[i] = -1; | |
1224 | 1682 |
1667 | 1683 if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
2979 | 1684 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n", |
9731 | 1685 s->keyframe?"key":"", counter, s->qps[0]); |
1224 | 1686 counter++; |
1687 | |
9731 | 1688 if (s->qps[0] != s->last_qps[0]) |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
1689 init_loop_filter(s); |
9731 | 1690 |
1691 for (i = 0; i < s->nqps; i++) | |
1692 // reinit all dequantizers if the first one changed, because | |
1693 // the DC of the first quantizer must be used for all matrices | |
1694 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0]) | |
1695 init_dequantizer(s, i); | |
1292
f7b3fa4bb7ae
revising and fixing motion vectors, squished block unpacking bug that
tmmm
parents:
1282
diff
changeset
|
1696 |
7938
66226ee647a4
Use skip_frame for keyframe-only decoding rather than #ifdef
conrad
parents:
7875
diff
changeset
|
1697 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe) |
66226ee647a4
Use skip_frame for keyframe-only decoding rather than #ifdef
conrad
parents:
7875
diff
changeset
|
1698 return buf_size; |
66226ee647a4
Use skip_frame for keyframe-only decoding rather than #ifdef
conrad
parents:
7875
diff
changeset
|
1699 |
11419 | 1700 s->current_frame.reference = 3; |
11423 | 1701 s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE; |
11419 | 1702 if (avctx->get_buffer(avctx, &s->current_frame) < 0) { |
1703 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
11420 | 1704 goto error; |
11419 | 1705 } |
1706 | |
1224 | 1707 if (s->keyframe) { |
2979 | 1708 if (!s->theora) |
1709 { | |
1710 skip_bits(&gb, 4); /* width code */ | |
1711 skip_bits(&gb, 4); /* height code */ | |
1712 if (s->version) | |
1713 { | |
1714 s->version = get_bits(&gb, 5); | |
1715 if (counter == 1) | |
1716 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version); | |
1717 } | |
1718 } | |
1719 if (s->version || s->theora) | |
1720 { | |
1721 if (get_bits1(&gb)) | |
1722 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n"); | |
1723 skip_bits(&gb, 2); /* reserved? */ | |
1724 } | |
1224 | 1725 } else { |
11133 | 1726 if (!s->golden_frame.data[0]) { |
11422
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1727 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n"); |
11423 | 1728 |
11422
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1729 s->golden_frame.reference = 3; |
11423 | 1730 s->golden_frame.pict_type = FF_I_TYPE; |
11422
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1731 if (avctx->get_buffer(avctx, &s->golden_frame) < 0) { |
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1732 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); |
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1733 goto error; |
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1734 } |
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1735 s->last_frame = s->golden_frame; |
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1736 s->last_frame.type = FF_BUFFER_TYPE_COPY; |
1224 | 1737 } |
1738 } | |
1739 | |
1407 | 1740 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame |
1741 s->current_frame.qstride= 0; | |
1742 | |
11471 | 1743 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment)); |
1224 | 1744 |
2687 | 1745 if (unpack_superblocks(s, &gb)){ |
1746 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n"); | |
11420 | 1747 goto error; |
2687 | 1748 } |
1749 if (unpack_modes(s, &gb)){ | |
1750 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n"); | |
11420 | 1751 goto error; |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1752 } |
2687 | 1753 if (unpack_vectors(s, &gb)){ |
1754 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n"); | |
11420 | 1755 goto error; |
2687 | 1756 } |
9731 | 1757 if (unpack_block_qpis(s, &gb)){ |
1758 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n"); | |
11420 | 1759 goto error; |
9731 | 1760 } |
2687 | 1761 if (unpack_dct_coeffs(s, &gb)){ |
1762 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); | |
11420 | 1763 goto error; |
2687 | 1764 } |
1224 | 1765 |
11133 | 1766 for (i = 0; i < 3; i++) { |
1767 if (s->flipped_image) | |
1768 s->data_offset[i] = 0; | |
1769 else | |
1770 s->data_offset[i] = ((s->height>>!!i)-1) * s->current_frame.linesize[i]; | |
1771 } | |
1772 | |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1773 s->last_slice_end = 0; |
11352 | 1774 for (i = 0; i < s->c_superblock_height; i++) |
2722 | 1775 render_slice(s, i); |
7038
110080ab2391
Remove the START_TIMER/STOP_TIMER from vp3.c, they clutter the output and
reimar
parents:
7037
diff
changeset
|
1776 |
11130 | 1777 // filter the last row |
1778 for (i = 0; i < 3; i++) { | |
1779 int row = (s->height >> (3+!!i)) - 1; | |
1780 apply_loop_filter(s, i, row, row+1); | |
1781 } | |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1782 vp3_draw_horiz_band(s, s->height); |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1783 |
1224 | 1784 *data_size=sizeof(AVFrame); |
1785 *(AVFrame*)data= s->current_frame; | |
1786 | |
1229
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1787 /* release the last frame, if it is allocated and if it is not the |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1788 * golden frame */ |
11421 | 1789 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY) |
1229
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1790 avctx->release_buffer(avctx, &s->last_frame); |
1224 | 1791 |
1227
184c480cefc3
fix decoder so that ffmpeg does not crash, at least not right away
tmmm
parents:
1224
diff
changeset
|
1792 /* shuffle frames (last = current) */ |
3486 | 1793 s->last_frame= s->current_frame; |
11419 | 1794 |
1795 if (s->keyframe) { | |
1796 if (s->golden_frame.data[0]) | |
1797 avctx->release_buffer(avctx, &s->golden_frame); | |
1798 s->golden_frame = s->current_frame; | |
11421 | 1799 s->last_frame.type = FF_BUFFER_TYPE_COPY; |
11419 | 1800 } |
1801 | |
1405
e69f99ade5b0
fix AVFrame.reference (the frames are used for decoding future frames so it should be !=0)
michaelni
parents:
1355
diff
changeset
|
1802 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */ |
1224 | 1803 |
1804 return buf_size; | |
11420 | 1805 |
1806 error: | |
1807 if (s->current_frame.data[0]) | |
1808 avctx->release_buffer(avctx, &s->current_frame); | |
1809 return -1; | |
1224 | 1810 } |
1811 | |
1812 /* | |
1813 * This is the ffmpeg/libavcodec API module cleanup function. | |
1814 */ | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6448
diff
changeset
|
1815 static av_cold int vp3_decode_end(AVCodecContext *avctx) |
1224 | 1816 { |
1817 Vp3DecodeContext *s = avctx->priv_data; | |
6393 | 1818 int i; |
1819 | |
1820 av_free(s->superblock_coding); | |
1224 | 1821 av_free(s->all_fragments); |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1822 av_free(s->coded_fragment_list[0]); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1823 av_free(s->dct_tokens_base); |
1224 | 1824 av_free(s->superblock_fragments); |
1240
c95ff60bc1a1
fix motion vector decoding bug and reinstate interframes
tmmm
parents:
1239
diff
changeset
|
1825 av_free(s->macroblock_coding); |
2967 | 1826 |
6393 | 1827 for (i = 0; i < 16; i++) { |
1828 free_vlc(&s->dc_vlc[i]); | |
1829 free_vlc(&s->ac_vlc_1[i]); | |
1830 free_vlc(&s->ac_vlc_2[i]); | |
1831 free_vlc(&s->ac_vlc_3[i]); | |
1832 free_vlc(&s->ac_vlc_4[i]); | |
1833 } | |
1834 | |
1835 free_vlc(&s->superblock_run_length_vlc); | |
1836 free_vlc(&s->fragment_run_length_vlc); | |
1837 free_vlc(&s->mode_code_vlc); | |
1838 free_vlc(&s->motion_vector_vlc); | |
1839 | |
1224 | 1840 /* release all frames */ |
11421 | 1841 if (s->golden_frame.data[0]) |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1842 avctx->release_buffer(avctx, &s->golden_frame); |
11421 | 1843 if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY) |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1844 avctx->release_buffer(avctx, &s->last_frame); |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1845 /* no need to release the current_frame since it will always be pointing |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1846 * to the same frame as either the golden or last frame */ |
1224 | 1847 |
1848 return 0; | |
1849 } | |
1850 | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1851 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb) |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1852 { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1853 Vp3DecodeContext *s = avctx->priv_data; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1854 |
5513 | 1855 if (get_bits1(gb)) { |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1856 int token; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1857 if (s->entries >= 32) { /* overflow */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1858 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1859 return -1; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1860 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1861 token = get_bits(gb, 5); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1862 //av_log(avctx, AV_LOG_DEBUG, "hti %d hbits %x token %d entry : %d size %d\n", s->hti, s->hbits, token, s->entries, s->huff_code_size); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1863 s->huffman_table[s->hti][token][0] = s->hbits; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1864 s->huffman_table[s->hti][token][1] = s->huff_code_size; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1865 s->entries++; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1866 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1867 else { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1868 if (s->huff_code_size >= 32) {/* overflow */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1869 av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n"); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1870 return -1; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1871 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1872 s->huff_code_size++; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1873 s->hbits <<= 1; |
8770 | 1874 if (read_huffman_tree(avctx, gb)) |
1875 return -1; | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1876 s->hbits |= 1; |
8770 | 1877 if (read_huffman_tree(avctx, gb)) |
1878 return -1; | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1879 s->hbits >>= 1; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1880 s->huff_code_size--; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1881 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1882 return 0; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1883 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1884 |
8590 | 1885 #if CONFIG_THEORA_DECODER |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1886 static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1887 { |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1888 Vp3DecodeContext *s = avctx->priv_data; |
11128 | 1889 int visible_width, visible_height, colorspace; |
3483 | 1890 |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1891 s->theora = get_bits_long(gb, 24); |
6349
aed237dd11d8
Print Theora bitstream version at DEBUG, not at VERBOSE level.
diego
parents:
6282
diff
changeset
|
1892 av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora); |
1627
a80b15c0b9d5
theora alpha3 support (with maintaining backward compatibility, maybe we should remove all backward compatibility codes after final theora release?)
alex
parents:
1626
diff
changeset
|
1893 |
2678 | 1894 /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */ |
1626
f74ae637ffa9
finally working with old theora bitstream (flipped image), the only sample I have is decoded successfully (theora.ogg)
alex
parents:
1598
diff
changeset
|
1895 /* but previous versions have the image flipped relative to vp3 */ |
2678 | 1896 if (s->theora < 0x030200) |
1626
f74ae637ffa9
finally working with old theora bitstream (flipped image), the only sample I have is decoded successfully (theora.ogg)
alex
parents:
1598
diff
changeset
|
1897 { |
2979 | 1898 s->flipped_image = 1; |
1626
f74ae637ffa9
finally working with old theora bitstream (flipped image), the only sample I have is decoded successfully (theora.ogg)
alex
parents:
1598
diff
changeset
|
1899 av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n"); |
f74ae637ffa9
finally working with old theora bitstream (flipped image), the only sample I have is decoded successfully (theora.ogg)
alex
parents:
1598
diff
changeset
|
1900 } |
f74ae637ffa9
finally working with old theora bitstream (flipped image), the only sample I have is decoded successfully (theora.ogg)
alex
parents:
1598
diff
changeset
|
1901 |
7967
6ac2c8312a2b
Visible width/height fields were added in Theora 3.2
conrad
parents:
7966
diff
changeset
|
1902 visible_width = s->width = get_bits(gb, 16) << 4; |
6ac2c8312a2b
Visible width/height fields were added in Theora 3.2
conrad
parents:
7966
diff
changeset
|
1903 visible_height = s->height = get_bits(gb, 16) << 4; |
2967 | 1904 |
2422 | 1905 if(avcodec_check_dimensions(avctx, s->width, s->height)){ |
2943
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
1906 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height); |
2422 | 1907 s->width= s->height= 0; |
1908 return -1; | |
1909 } | |
2943
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
1910 |
7967
6ac2c8312a2b
Visible width/height fields were added in Theora 3.2
conrad
parents:
7966
diff
changeset
|
1911 if (s->theora >= 0x030200) { |
7968 | 1912 visible_width = get_bits_long(gb, 24); |
1913 visible_height = get_bits_long(gb, 24); | |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1914 |
4936 | 1915 skip_bits(gb, 8); /* offset x */ |
1916 skip_bits(gb, 8); /* offset y */ | |
1917 } | |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1918 |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1919 skip_bits(gb, 32); /* fps numerator */ |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1920 skip_bits(gb, 32); /* fps denumerator */ |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1921 skip_bits(gb, 24); /* aspect numerator */ |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1922 skip_bits(gb, 24); /* aspect denumerator */ |
2967 | 1923 |
2678 | 1924 if (s->theora < 0x030200) |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1925 skip_bits(gb, 5); /* keyframe frequency force */ |
11128 | 1926 colorspace = get_bits(gb, 8); |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1927 skip_bits(gb, 24); /* bitrate */ |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1928 |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1929 skip_bits(gb, 6); /* quality hint */ |
2967 | 1930 |
2678 | 1931 if (s->theora >= 0x030200) |
1627
a80b15c0b9d5
theora alpha3 support (with maintaining backward compatibility, maybe we should remove all backward compatibility codes after final theora release?)
alex
parents:
1626
diff
changeset
|
1932 { |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1933 skip_bits(gb, 5); /* keyframe frequency force */ |
11127
2f7baf2042a3
Theora 3.4 doesn't exist; these fields were misunderstandings of the spec
conrad
parents:
11126
diff
changeset
|
1934 skip_bits(gb, 2); /* pixel format: 420,res,422,444 */ |
2f7baf2042a3
Theora 3.4 doesn't exist; these fields were misunderstandings of the spec
conrad
parents:
11126
diff
changeset
|
1935 skip_bits(gb, 3); /* reserved */ |
1627
a80b15c0b9d5
theora alpha3 support (with maintaining backward compatibility, maybe we should remove all backward compatibility codes after final theora release?)
alex
parents:
1626
diff
changeset
|
1936 } |
2967 | 1937 |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1938 // align_get_bits(gb); |
2967 | 1939 |
4935
b34f448e1a12
fix display of theora videos with visible size smaller than encoded size
aurel
parents:
4826
diff
changeset
|
1940 if ( visible_width <= s->width && visible_width > s->width-16 |
b34f448e1a12
fix display of theora videos with visible size smaller than encoded size
aurel
parents:
4826
diff
changeset
|
1941 && visible_height <= s->height && visible_height > s->height-16) |
b34f448e1a12
fix display of theora videos with visible size smaller than encoded size
aurel
parents:
4826
diff
changeset
|
1942 avcodec_set_dimensions(avctx, visible_width, visible_height); |
b34f448e1a12
fix display of theora videos with visible size smaller than encoded size
aurel
parents:
4826
diff
changeset
|
1943 else |
b34f448e1a12
fix display of theora videos with visible size smaller than encoded size
aurel
parents:
4826
diff
changeset
|
1944 avcodec_set_dimensions(avctx, s->width, s->height); |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1945 |
11128 | 1946 if (colorspace == 1) { |
1947 avctx->color_primaries = AVCOL_PRI_BT470M; | |
1948 } else if (colorspace == 2) { | |
1949 avctx->color_primaries = AVCOL_PRI_BT470BG; | |
1950 } | |
1951 if (colorspace == 1 || colorspace == 2) { | |
1952 avctx->colorspace = AVCOL_SPC_BT470BG; | |
1953 avctx->color_trc = AVCOL_TRC_BT709; | |
1954 } | |
1955 | |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1956 return 0; |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1957 } |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1958 |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1959 static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb) |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1960 { |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1961 Vp3DecodeContext *s = avctx->priv_data; |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1962 int i, n, matrices, inter, plane; |
2678 | 1963 |
1964 if (s->theora >= 0x030200) { | |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1965 n = get_bits(gb, 3); |
2731
0b4b57c4a8f5
read loop filter limit values from Theora header, courtesy of Matthieu
melanson
parents:
2726
diff
changeset
|
1966 /* loop filter limit values table */ |
9922
ffdd1d32c40c
Ensure that the filter limit values do not exceed the maximum allowed value of 127.
reimar
parents:
9921
diff
changeset
|
1967 for (i = 0; i < 64; i++) { |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1968 s->filter_limit_values[i] = get_bits(gb, n); |
9922
ffdd1d32c40c
Ensure that the filter limit values do not exceed the maximum allowed value of 127.
reimar
parents:
9921
diff
changeset
|
1969 if (s->filter_limit_values[i] > 127) { |
ffdd1d32c40c
Ensure that the filter limit values do not exceed the maximum allowed value of 127.
reimar
parents:
9921
diff
changeset
|
1970 av_log(avctx, AV_LOG_ERROR, "filter limit value too large (%i > 127), clamping\n", s->filter_limit_values[i]); |
ffdd1d32c40c
Ensure that the filter limit values do not exceed the maximum allowed value of 127.
reimar
parents:
9921
diff
changeset
|
1971 s->filter_limit_values[i] = 127; |
ffdd1d32c40c
Ensure that the filter limit values do not exceed the maximum allowed value of 127.
reimar
parents:
9921
diff
changeset
|
1972 } |
ffdd1d32c40c
Ensure that the filter limit values do not exceed the maximum allowed value of 127.
reimar
parents:
9921
diff
changeset
|
1973 } |
2678 | 1974 } |
2967 | 1975 |
2678 | 1976 if (s->theora >= 0x030200) |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1977 n = get_bits(gb, 4) + 1; |
2678 | 1978 else |
1979 n = 16; | |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1980 /* quality threshold table */ |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1981 for (i = 0; i < 64; i++) |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1982 s->coded_ac_scale_factor[i] = get_bits(gb, n); |
2678 | 1983 |
1984 if (s->theora >= 0x030200) | |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1985 n = get_bits(gb, 4) + 1; |
2678 | 1986 else |
1987 n = 16; | |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1988 /* dc scale factor table */ |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
1989 for (i = 0; i < 64; i++) |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1990 s->coded_dc_scale_factor[i] = get_bits(gb, n); |
2678 | 1991 |
1992 if (s->theora >= 0x030200) | |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1993 matrices = get_bits(gb, 9) + 1; |
2678 | 1994 else |
2944
3785497656c9
Correctly skip unknown matrices (patch by Matthieu Castet) and disable comment reading
alex
parents:
2943
diff
changeset
|
1995 matrices = 3; |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1996 |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1997 if(matrices > 384){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1998 av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n"); |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1999 return -1; |
2678 | 2000 } |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2001 |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2002 for(n=0; n<matrices; n++){ |
2979 | 2003 for (i = 0; i < 64; i++) |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2004 s->base_matrix[n][i]= get_bits(gb, 8); |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2005 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2006 |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2007 for (inter = 0; inter <= 1; inter++) { |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2008 for (plane = 0; plane <= 2; plane++) { |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2009 int newqr= 1; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2010 if (inter || plane > 0) |
5513 | 2011 newqr = get_bits1(gb); |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2012 if (!newqr) { |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2013 int qtj, plj; |
5513 | 2014 if(inter && get_bits1(gb)){ |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2015 qtj = 0; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2016 plj = plane; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2017 }else{ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2018 qtj= (3*inter + plane - 1) / 3; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2019 plj= (plane + 2) % 3; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2020 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2021 s->qr_count[inter][plane]= s->qr_count[qtj][plj]; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2022 memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0])); |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2023 memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0])); |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2024 } else { |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2025 int qri= 0; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2026 int qi = 0; |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2027 |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2028 for(;;){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2029 i= get_bits(gb, av_log2(matrices-1)+1); |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2030 if(i>= matrices){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2031 av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n"); |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2032 return -1; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2033 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2034 s->qr_base[inter][plane][qri]= i; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2035 if(qi >= 63) |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2036 break; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2037 i = get_bits(gb, av_log2(63-qi)+1) + 1; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2038 s->qr_size[inter][plane][qri++]= i; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2039 qi += i; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2040 } |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2041 |
2944
3785497656c9
Correctly skip unknown matrices (patch by Matthieu Castet) and disable comment reading
alex
parents:
2943
diff
changeset
|
2042 if (qi > 63) { |
2943
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
2043 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); |
2979 | 2044 return -1; |
2045 } | |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2046 s->qr_count[inter][plane]= qri; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2047 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2048 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2049 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2050 |
2944
3785497656c9
Correctly skip unknown matrices (patch by Matthieu Castet) and disable comment reading
alex
parents:
2943
diff
changeset
|
2051 /* Huffman tables */ |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2052 for (s->hti = 0; s->hti < 80; s->hti++) { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2053 s->entries = 0; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2054 s->huff_code_size = 1; |
5513 | 2055 if (!get_bits1(gb)) { |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2056 s->hbits = 0; |
8770 | 2057 if(read_huffman_tree(avctx, gb)) |
2058 return -1; | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2059 s->hbits = 1; |
8770 | 2060 if(read_huffman_tree(avctx, gb)) |
2061 return -1; | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2062 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2063 } |
2967 | 2064 |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2065 s->theora_tables = 1; |
2967 | 2066 |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2067 return 0; |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2068 } |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2069 |
9007
043574c5c153
Add missing av_cold in static init/close functions.
stefano
parents:
8770
diff
changeset
|
2070 static av_cold int theora_decode_init(AVCodecContext *avctx) |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2071 { |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2072 Vp3DecodeContext *s = avctx->priv_data; |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2073 GetBitContext gb; |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2074 int ptype; |
4723
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2075 uint8_t *header_start[3]; |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2076 int header_len[3]; |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2077 int i; |
2967 | 2078 |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2079 s->theora = 1; |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2080 |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2081 if (!avctx->extradata_size) |
2943
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
2082 { |
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
2083 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n"); |
2979 | 2084 return -1; |
2943
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
2085 } |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2086 |
4723
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2087 if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size, |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2088 42, header_start, header_len) < 0) { |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2089 av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n"); |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2090 return -1; |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2091 } |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2092 |
2533 | 2093 for(i=0;i<3;i++) { |
10247 | 2094 init_get_bits(&gb, header_start[i], header_len[i] * 8); |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2095 |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2096 ptype = get_bits(&gb, 8); |
2967 | 2097 |
2943
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
2098 if (!(ptype & 0x80)) |
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
2099 { |
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
2100 av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n"); |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
2101 // return -1; |
2967 | 2102 } |
2943
6bf62c1cf1b6
Fixing theora _again_. DONT BOTHER BREAKING THIS AGAIN. Added support for Theora 3.4 and stream created by Elphel cameras are decodable.
alex
parents:
2942
diff
changeset
|
2103 |
4561 | 2104 // FIXME: Check for this as well. |
9637 | 2105 skip_bits_long(&gb, 6*8); /* "theora" */ |
2967 | 2106 |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2107 switch(ptype) |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2108 { |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2109 case 0x80: |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
2110 theora_decode_header(avctx, &gb); |
2979 | 2111 break; |
2112 case 0x81: | |
2944
3785497656c9
Correctly skip unknown matrices (patch by Matthieu Castet) and disable comment reading
alex
parents:
2943
diff
changeset
|
2113 // FIXME: is this needed? it breaks sometimes |
2979 | 2114 // theora_decode_comments(avctx, gb); |
2115 break; | |
2116 case 0x82: | |
8770 | 2117 if (theora_decode_tables(avctx, &gb)) |
2118 return -1; | |
2979 | 2119 break; |
2120 default: | |
2121 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80); | |
2122 break; | |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2123 } |
7943
3ff31e4454cd
Downgrade severity of leftover bits in header packets, and don't check for the comment header
conrad
parents:
7938
diff
changeset
|
2124 if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb)) |
3ff31e4454cd
Downgrade severity of leftover bits in header packets, and don't check for the comment header
conrad
parents:
7938
diff
changeset
|
2125 av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype); |
3490
b43d45362038
fix playback of theora.ogg on mphq patch by (matthieu castet %% castet dat matthieu ot free dat fr %%)
michael
parents:
3489
diff
changeset
|
2126 if (s->theora < 0x030200) |
b43d45362038
fix playback of theora.ogg on mphq patch by (matthieu castet %% castet dat matthieu ot free dat fr %%)
michael
parents:
3489
diff
changeset
|
2127 break; |
2533 | 2128 } |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2129 |
9925
2978d5f0ac0a
Remove useless ret variable added in last revision again.
reimar
parents:
9924
diff
changeset
|
2130 return vp3_decode_init(avctx); |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2131 } |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2132 |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2133 AVCodec theora_decoder = { |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2134 "theora", |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2135 CODEC_TYPE_VIDEO, |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2136 CODEC_ID_THEORA, |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2137 sizeof(Vp3DecodeContext), |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2138 theora_decode_init, |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2139 NULL, |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2140 vp3_decode_end, |
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2141 vp3_decode_frame, |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
2142 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, |
6710 | 2143 NULL, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
7038
diff
changeset
|
2144 .long_name = NULL_IF_CONFIG_SMALL("Theora"), |
1516
0f0e9dfa6723
theora decoding support (only keyframes for now, because by theora the frame isn't flipped so the motion vectors are getting screwed up)
alex
parents:
1463
diff
changeset
|
2145 }; |
4823
cff8c7ecac71
Fix compilation when Theora decoder is disabled, but VP3 is enabled.
diego
parents:
4801
diff
changeset
|
2146 #endif |
4825
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2147 |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2148 AVCodec vp3_decoder = { |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2149 "vp3", |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2150 CODEC_TYPE_VIDEO, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2151 CODEC_ID_VP3, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2152 sizeof(Vp3DecodeContext), |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2153 vp3_decode_init, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2154 NULL, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2155 vp3_decode_end, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2156 vp3_decode_frame, |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
2157 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, |
6710 | 2158 NULL, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
7038
diff
changeset
|
2159 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"), |
4825
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2160 }; |