Mercurial > libavcodec.hg
annotate vp3.c @ 11473:8b6722b58f15 libavcodec
vp3: Make fragment_width _height by plane
author | conrad |
---|---|
date | Sat, 13 Mar 2010 05:56:15 +0000 |
parents | 258d773b8feb |
children | 66b2c909d530 |
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]; | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
467 if (current_fragment >= s->fragment_count) { |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1518
diff
changeset
|
468 av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n", |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
469 current_fragment, s->fragment_count); |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
470 return 1; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
471 } |
1224 | 472 if (current_fragment != -1) { |
11229 | 473 int coded = s->superblock_coding[i]; |
1224 | 474 |
11229 | 475 if (s->superblock_coding[i] == SB_PARTIALLY_CODED) { |
1224 | 476 |
477 /* fragment may or may not be coded; this is the case | |
478 * that cares about the fragment coding runs */ | |
2705 | 479 if (current_run-- == 0) { |
1224 | 480 bit ^= 1; |
2967 | 481 current_run = get_vlc2(gb, |
2705 | 482 s->fragment_run_length_vlc.table, 5, 2); |
1224 | 483 } |
11229 | 484 coded = bit; |
485 } | |
1224 | 486 |
11229 | 487 if (coded) { |
2967 | 488 /* 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
|
489 * the next phase */ |
2967 | 490 s->all_fragments[current_fragment].coding_method = |
1224 | 491 MODE_INTER_NO_MV; |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
492 s->coded_fragment_list[plane][num_coded_frags++] = |
1224 | 493 current_fragment; |
494 } else { | |
495 /* not coded; copy this fragment from the prior frame */ | |
496 s->all_fragments[current_fragment].coding_method = | |
497 MODE_COPY; | |
498 } | |
499 } | |
500 } | |
501 } | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
502 s->total_num_coded_frags += num_coded_frags; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
503 for (i = 0; i < 64; i++) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
504 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
|
505 if (plane < 2) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
506 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
|
507 } |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
508 return 0; |
1224 | 509 } |
510 | |
511 /* | |
512 * This function unpacks all the coding mode data for individual macroblocks | |
513 * from the bitstream. | |
514 */ | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
515 static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb) |
1224 | 516 { |
11149 | 517 int i, j, k, sb_x, sb_y; |
1224 | 518 int scheme; |
519 int current_macroblock; | |
520 int current_fragment; | |
521 int coding_mode; | |
7139
03fe3194eff7
make ModeAlphabet read-only and use a custom mode alphabet
stefang
parents:
7040
diff
changeset
|
522 int custom_mode_alphabet[CODING_MODE_COUNT]; |
11153 | 523 const int *alphabet; |
1224 | 524 |
525 if (s->keyframe) { | |
526 for (i = 0; i < s->fragment_count; i++) | |
527 s->all_fragments[i].coding_method = MODE_INTRA; | |
528 | |
529 } else { | |
530 | |
531 /* fetch the mode coding scheme for this frame */ | |
532 scheme = get_bits(gb, 3); | |
533 | |
534 /* is it a custom coding scheme? */ | |
535 if (scheme == 0) { | |
536 for (i = 0; i < 8; i++) | |
8736
f973fff63599
VP3: Prevent stack corruption from an unset custom coding method.
alexc
parents:
8718
diff
changeset
|
537 custom_mode_alphabet[i] = MODE_INTER_NO_MV; |
f973fff63599
VP3: Prevent stack corruption from an unset custom coding method.
alexc
parents:
8718
diff
changeset
|
538 for (i = 0; i < 8; i++) |
7139
03fe3194eff7
make ModeAlphabet read-only and use a custom mode alphabet
stefang
parents:
7040
diff
changeset
|
539 custom_mode_alphabet[get_bits(gb, 3)] = i; |
11153 | 540 alphabet = custom_mode_alphabet; |
541 } else | |
542 alphabet = ModeAlphabet[scheme-1]; | |
1224 | 543 |
544 /* iterate through all of the macroblocks that contain 1 or more | |
545 * coded fragments */ | |
11149 | 546 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { |
547 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { | |
1224 | 548 |
549 for (j = 0; j < 4; j++) { | |
11149 | 550 int mb_x = 2*sb_x + (j>>1); |
551 int mb_y = 2*sb_y + (((j>>1)+j)&1); | |
552 current_macroblock = mb_y * s->macroblock_width + mb_x; | |
553 | |
11151 | 554 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height) |
1224 | 555 continue; |
556 | |
11150 | 557 #define BLOCK_X (2*mb_x + (k&1)) |
558 #define BLOCK_Y (2*mb_y + (k>>1)) | |
11151 | 559 /* coding modes are only stored if the macroblock has at least one |
560 * luma block coded, otherwise it must be INTER_NO_MV */ | |
561 for (k = 0; k < 4; k++) { | |
11473 | 562 current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X; |
11151 | 563 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) |
564 break; | |
565 } | |
566 if (k == 4) { | |
567 s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV; | |
568 continue; | |
569 } | |
11150 | 570 |
1224 | 571 /* mode 7 means get 3 bits for each coding mode */ |
572 if (scheme == 7) | |
573 coding_mode = get_bits(gb, 3); | |
574 else | |
11153 | 575 coding_mode = alphabet |
2702
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
576 [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)]; |
1224 | 577 |
1240
c95ff60bc1a1
fix motion vector decoding bug and reinstate interframes
tmmm
parents:
1239
diff
changeset
|
578 s->macroblock_coding[current_macroblock] = coding_mode; |
11150 | 579 for (k = 0; k < 4; k++) { |
2967 | 580 current_fragment = |
11473 | 581 BLOCK_Y*s->fragment_width[0] + BLOCK_X; |
11150 | 582 if (s->all_fragments[current_fragment].coding_method != |
583 MODE_COPY) | |
584 s->all_fragments[current_fragment].coding_method = | |
585 coding_mode; | |
586 } | |
587 for (k = 0; k < 2; k++) { | |
588 current_fragment = s->fragment_start[k+1] + | |
11473 | 589 mb_y*s->fragment_width[1] + mb_x; |
2967 | 590 if (s->all_fragments[current_fragment].coding_method != |
1224 | 591 MODE_COPY) |
592 s->all_fragments[current_fragment].coding_method = | |
593 coding_mode; | |
594 } | |
595 } | |
11149 | 596 } |
1224 | 597 } |
598 } | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
599 |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
600 return 0; |
1229
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
601 } |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
602 |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
603 /* |
1224 | 604 * This function unpacks all the motion vectors for the individual |
605 * macroblocks from the bitstream. | |
606 */ | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
607 static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb) |
1224 | 608 { |
11152
b3ff7fcfffc4
Directly check whether a fragment is coded for 4MV mode instead of iterating
conrad
parents:
11151
diff
changeset
|
609 int j, k, sb_x, sb_y; |
1224 | 610 int coding_mode; |
11472 | 611 int motion_x[4]; |
612 int motion_y[4]; | |
1224 | 613 int last_motion_x = 0; |
614 int last_motion_y = 0; | |
615 int prior_last_motion_x = 0; | |
616 int prior_last_motion_y = 0; | |
617 int current_macroblock; | |
618 int current_fragment; | |
619 | |
7970 | 620 if (s->keyframe) |
7969 | 621 return 0; |
7971 | 622 |
7972 | 623 /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */ |
624 coding_mode = get_bits1(gb); | |
1224 | 625 |
7972 | 626 /* iterate through all of the macroblocks that contain 1 or more |
627 * coded fragments */ | |
11149 | 628 for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) { |
629 for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) { | |
1224 | 630 |
7972 | 631 for (j = 0; j < 4; j++) { |
11149 | 632 int mb_x = 2*sb_x + (j>>1); |
633 int mb_y = 2*sb_y + (((j>>1)+j)&1); | |
634 current_macroblock = mb_y * s->macroblock_width + mb_x; | |
635 | |
636 if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height || | |
7972 | 637 (s->macroblock_coding[current_macroblock] == MODE_COPY)) |
638 continue; | |
639 | |
640 switch (s->macroblock_coding[current_macroblock]) { | |
641 | |
642 case MODE_INTER_PLUS_MV: | |
643 case MODE_GOLDEN_MV: | |
644 /* all 6 fragments use the same motion vector */ | |
645 if (coding_mode == 0) { | |
646 motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; | |
647 motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; | |
648 } else { | |
649 motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)]; | |
650 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
|
651 } |
1224 | 652 |
7972 | 653 /* vector maintenance, only on MODE_INTER_PLUS_MV */ |
654 if (s->macroblock_coding[current_macroblock] == | |
655 MODE_INTER_PLUS_MV) { | |
1224 | 656 prior_last_motion_x = last_motion_x; |
657 prior_last_motion_y = last_motion_y; | |
658 last_motion_x = motion_x[0]; | |
659 last_motion_y = motion_y[0]; | |
7972 | 660 } |
661 break; | |
662 | |
663 case MODE_INTER_FOURMV: | |
664 /* vector maintenance */ | |
665 prior_last_motion_x = last_motion_x; | |
666 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
|
667 |
7972 | 668 /* fetch 4 vectors from the bitstream, one for each |
669 * Y fragment, then average for the C fragment vectors */ | |
670 for (k = 0; k < 4; k++) { | |
11473 | 671 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
|
672 if (s->all_fragments[current_fragment].coding_method != MODE_COPY) { |
7972 | 673 if (coding_mode == 0) { |
674 motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; | |
675 motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)]; | |
676 } else { | |
677 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)]; | |
678 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)]; | |
679 } | |
680 last_motion_x = motion_x[k]; | |
681 last_motion_y = motion_y[k]; | |
682 } else { | |
683 motion_x[k] = 0; | |
684 motion_y[k] = 0; | |
685 } | |
1224 | 686 } |
7972 | 687 break; |
688 | |
689 case MODE_INTER_LAST_MV: | |
690 /* all 6 fragments use the last motion vector */ | |
691 motion_x[0] = last_motion_x; | |
692 motion_y[0] = last_motion_y; | |
693 | |
694 /* no vector maintenance (last vector remains the | |
695 * last vector) */ | |
696 break; | |
697 | |
698 case MODE_INTER_PRIOR_LAST: | |
699 /* all 6 fragments use the motion vector prior to the | |
700 * last motion vector */ | |
701 motion_x[0] = prior_last_motion_x; | |
702 motion_y[0] = prior_last_motion_y; | |
703 | |
704 /* vector maintenance */ | |
705 prior_last_motion_x = last_motion_x; | |
706 prior_last_motion_y = last_motion_y; | |
707 last_motion_x = motion_x[0]; | |
708 last_motion_y = motion_y[0]; | |
709 break; | |
710 | |
711 default: | |
712 /* covers intra, inter without MV, golden without MV */ | |
10696
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
713 motion_x[0] = 0; |
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
714 motion_y[0] = 0; |
7972 | 715 |
716 /* no vector maintenance */ | |
717 break; | |
718 } | |
719 | |
720 /* assign the motion vectors to the correct fragments */ | |
11150 | 721 for (k = 0; k < 4; k++) { |
7972 | 722 current_fragment = |
11473 | 723 BLOCK_Y*s->fragment_width[0] + BLOCK_X; |
10696
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
724 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { |
10697 | 725 s->all_fragments[current_fragment].motion_x = motion_x[k]; |
726 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
|
727 } else { |
a80851eebd2e
Optimize unpack_vectors() by not shuffling around redundant vectors.
melanson
parents:
10652
diff
changeset
|
728 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
|
729 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
|
730 } |
1224 | 731 } |
11472 | 732 if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) { |
733 motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2); | |
734 motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2); | |
735 } | |
11150 | 736 for (k = 0; k < 2; k++) { |
737 current_fragment = s->fragment_start[k+1] + | |
11473 | 738 mb_y*s->fragment_width[1] + mb_x; |
11150 | 739 s->all_fragments[current_fragment].motion_x = motion_x[0]; |
740 s->all_fragments[current_fragment].motion_y = motion_y[0]; | |
741 } | |
1224 | 742 } |
11149 | 743 } |
7972 | 744 } |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
745 |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
746 return 0; |
1224 | 747 } |
748 | |
9731 | 749 static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb) |
750 { | |
751 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
|
752 int num_blocks = s->total_num_coded_frags; |
9731 | 753 |
754 for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) { | |
755 i = blocks_decoded = num_blocks_at_qpi = 0; | |
756 | |
757 bit = get_bits1(gb); | |
758 | |
759 do { | |
760 run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1; | |
761 if (run_length == 34) | |
762 run_length += get_bits(gb, 12); | |
763 blocks_decoded += run_length; | |
764 | |
765 if (!bit) | |
766 num_blocks_at_qpi += run_length; | |
767 | |
768 for (j = 0; j < run_length; i++) { | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
769 if (i >= s->total_num_coded_frags) |
9731 | 770 return -1; |
771 | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
772 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
|
773 s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit; |
9731 | 774 j++; |
775 } | |
776 } | |
777 | |
11226
386c361bf136
Make the special 4129 case for long-run bit strings a #define and explain it
conrad
parents:
11225
diff
changeset
|
778 if (run_length == MAXIMUM_LONG_BIT_RUN) |
9731 | 779 bit = get_bits1(gb); |
780 else | |
781 bit ^= 1; | |
782 } while (blocks_decoded < num_blocks); | |
783 | |
784 num_blocks -= num_blocks_at_qpi; | |
785 } | |
786 | |
787 return 0; | |
788 } | |
789 | |
2967 | 790 /* |
1224 | 791 * This function is called by unpack_dct_coeffs() to extract the VLCs from |
792 * the bitstream. The VLCs encode tokens which are used to unpack DCT | |
793 * data. This function unpacks all the VLCs for either the Y plane or both | |
794 * C planes, and is called for DC coefficients or different AC coefficient | |
795 * levels (since different coefficient types require different VLC tables. | |
796 * | |
797 * This function returns a residual eob run. E.g, if a particular token gave | |
798 * instructions to EOB the next 5 fragments and there were only 2 fragments | |
799 * left in the current fragment range, 3 would be returned so that it could | |
800 * be passed into the next call to this same function. | |
801 */ | |
802 static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb, | |
803 VLC *table, int coeff_index, | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
804 int plane, |
1224 | 805 int eob_run) |
806 { | |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
807 int i, j = 0; |
1224 | 808 int token; |
2712
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
809 int zero_run = 0; |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
810 DCTELEM coeff = 0; |
10202
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
811 int bits_to_get; |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
812 int blocks_ended; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
813 int coeff_i = 0; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
814 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
|
815 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
|
816 |
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
817 /* 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
|
818 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
|
819 Vp3Fragment *all_fragments = s->all_fragments; |
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
820 VLC_TYPE (*vlc_table)[2] = table->table; |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
821 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
822 if (num_coeffs < 0) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
823 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
|
824 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
825 if (eob_run > num_coeffs) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
826 coeff_i = blocks_ended = num_coeffs; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
827 eob_run -= num_coeffs; |
10619
d930f99edbf9
Use a list to track which fragments coded in this frame still have
melanson
parents:
10258
diff
changeset
|
828 } else { |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
829 coeff_i = blocks_ended = eob_run; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
830 eob_run = 0; |
1244
a02df1ba6c7f
fix image buffer leak on keyframes, add more error condition checks
tmmm
parents:
1240
diff
changeset
|
831 } |
a02df1ba6c7f
fix image buffer leak on keyframes, add more error condition checks
tmmm
parents:
1240
diff
changeset
|
832 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
833 // 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
|
834 if (blocks_ended) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
835 dct_tokens[j++] = blocks_ended << 2; |
7037
480faa9f79dd
Almost-cosmetics: split out coeff_count from all_fragments struct into
reimar
parents:
6903
diff
changeset
|
836 |
11453 | 837 while (coeff_i < num_coeffs && get_bits_left(gb) > 0) { |
1224 | 838 /* decode a VLC into a token */ |
10202
d0456fd306d2
Modify unpack_vlcs() so that there are fewer dereferences through the
melanson
parents:
10140
diff
changeset
|
839 token = get_vlc2(gb, vlc_table, 5, 3); |
1224 | 840 /* 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
|
841 if (token <= 6) { |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
842 eob_run = eob_run_base[token]; |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
843 if (eob_run_get_bits[token]) |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
844 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
|
845 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
846 // 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
|
847 // 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
|
848 if (eob_run > num_coeffs - coeff_i) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
849 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
|
850 blocks_ended += num_coeffs - coeff_i; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
851 eob_run -= num_coeffs - coeff_i; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
852 coeff_i = num_coeffs; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
853 } else { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
854 dct_tokens[j++] = TOKEN_EOB(eob_run); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
855 blocks_ended += eob_run; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
856 coeff_i += eob_run; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
857 eob_run = 0; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
858 } |
2712
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
859 } else { |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
860 bits_to_get = coeff_get_bits[token]; |
10230
afaf58d1e894
Another micro-optimization for unpack_vlcs(): Eliminate a possible
melanson
parents:
10223
diff
changeset
|
861 if (bits_to_get) |
afaf58d1e894
Another micro-optimization for unpack_vlcs(): Eliminate a possible
melanson
parents:
10223
diff
changeset
|
862 bits_to_get = get_bits(gb, bits_to_get); |
afaf58d1e894
Another micro-optimization for unpack_vlcs(): Eliminate a possible
melanson
parents:
10223
diff
changeset
|
863 coeff = coeff_tables[token][bits_to_get]; |
2712
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
864 |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
865 zero_run = zero_run_base[token]; |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
866 if (zero_run_get_bits[token]) |
9c1a436dac6b
replace unpack_token() with a series of lookup tables
melanson
parents:
2709
diff
changeset
|
867 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
|
868 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
869 if (zero_run) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
870 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
|
871 } else { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
872 // 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
|
873 // 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
|
874 // 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
|
875 // however, or else the structure collapses on itself. |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
876 if (!coeff_index) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
877 all_fragments[coded_fragment_list[coeff_i]].dc = coeff; |
1224 | 878 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
879 dct_tokens[j++] = TOKEN_COEFF(coeff); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
880 } |
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 if (coeff_index + zero_run > 64) { |
11418 | 883 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
|
884 " %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
|
885 zero_run = 64 - coeff_index; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
886 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
887 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
888 // zero runs code multiple coefficients, |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
889 // 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
|
890 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
|
891 s->num_coded_frags[plane][i]--; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
892 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
|
893 } |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
894 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
895 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
896 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
|
897 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
|
898 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
899 // 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
|
900 // EOB run at this level |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
901 if (blocks_ended) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
902 for (i = coeff_index+1; i < 64; i++) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
903 s->num_coded_frags[plane][i] -= blocks_ended; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
904 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
905 // setup the next buffer |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
906 if (plane < 2) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
907 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
|
908 else if (coeff_index < 63) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
909 s->dct_tokens[0][coeff_index+1] = dct_tokens + j; |
1224 | 910 |
911 return eob_run; | |
912 } | |
913 | |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
914 static void reverse_dc_prediction(Vp3DecodeContext *s, |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
915 int first_fragment, |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
916 int fragment_width, |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
917 int fragment_height); |
1224 | 918 /* |
919 * This function unpacks all of the DCT coefficient data from the | |
920 * bitstream. | |
921 */ | |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
922 static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb) |
1224 | 923 { |
924 int i; | |
925 int dc_y_table; | |
926 int dc_c_table; | |
927 int ac_y_table; | |
928 int ac_c_table; | |
929 int residual_eob_run = 0; | |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
930 VLC *y_tables[64]; |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
931 VLC *c_tables[64]; |
1224 | 932 |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
933 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
|
934 |
6903 | 935 /* fetch the DC table indexes */ |
1224 | 936 dc_y_table = get_bits(gb, 4); |
937 dc_c_table = get_bits(gb, 4); | |
938 | |
939 /* unpack the Y plane DC coefficients */ | |
2967 | 940 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
|
941 0, residual_eob_run); |
1224 | 942 |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
943 /* reverse prediction of the Y-plane DC coefficients */ |
11473 | 944 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
|
945 |
1224 | 946 /* unpack the C plane DC coefficients */ |
947 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
|
948 1, residual_eob_run); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
949 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
|
950 2, residual_eob_run); |
1224 | 951 |
10223
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
952 /* reverse prediction of the C-plane DC coefficients */ |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
953 if (!(s->avctx->flags & CODEC_FLAG_GRAY)) |
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 reverse_dc_prediction(s, s->fragment_start[1], |
11473 | 956 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
|
957 reverse_dc_prediction(s, s->fragment_start[2], |
11473 | 958 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
|
959 } |
b08865f6d4e3
Perform the DC prediction reversal immediately after decoding all of
melanson
parents:
10202
diff
changeset
|
960 |
6903 | 961 /* fetch the AC table indexes */ |
1224 | 962 ac_y_table = get_bits(gb, 4); |
963 ac_c_table = get_bits(gb, 4); | |
964 | |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
965 /* build tables of AC VLC tables */ |
1224 | 966 for (i = 1; i <= 5; i++) { |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
967 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
|
968 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
|
969 } |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
970 for (i = 6; i <= 14; i++) { |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
971 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
|
972 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
|
973 } |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
974 for (i = 15; i <= 27; i++) { |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
975 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
|
976 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
|
977 } |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
978 for (i = 28; i <= 63; i++) { |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
979 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
|
980 c_tables[i] = &s->ac_vlc_4[ac_c_table]; |
1224 | 981 } |
982 | |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
983 /* decode all AC coefficents */ |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
984 for (i = 1; i <= 63; i++) { |
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
985 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
|
986 0, residual_eob_run); |
1224 | 987 |
10620
972b17631531
Small refactoring: Instead of 4 loops for decoding AC coefficients based
melanson
parents:
10619
diff
changeset
|
988 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
|
989 1, residual_eob_run); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
990 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
|
991 2, residual_eob_run); |
1224 | 992 } |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
993 |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
994 return 0; |
1224 | 995 } |
996 | |
997 /* | |
998 * This function reverses the DC prediction for each coded fragment in | |
2967 | 999 * the frame. Much of this function is adapted directly from the original |
1224 | 1000 * VP3 source code. |
1001 */ | |
1002 #define COMPATIBLE_FRAME(x) \ | |
1003 (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
|
1004 #define DC_COEFF(u) s->all_fragments[u].dc |
1224 | 1005 |
1006 static void reverse_dc_prediction(Vp3DecodeContext *s, | |
1007 int first_fragment, | |
1008 int fragment_width, | |
2967 | 1009 int fragment_height) |
1224 | 1010 { |
1011 | |
1012 #define PUL 8 | |
1013 #define PU 4 | |
1014 #define PUR 2 | |
1015 #define PL 1 | |
1016 | |
1017 int x, y; | |
1018 int i = first_fragment; | |
1019 | |
3940 | 1020 int predicted_dc; |
1224 | 1021 |
1022 /* DC values for the left, up-left, up, and up-right fragments */ | |
1023 int vl, vul, vu, vur; | |
1024 | |
6903 | 1025 /* indexes for the left, up-left, up, and up-right fragments */ |
1224 | 1026 int l, ul, u, ur; |
1027 | |
2967 | 1028 /* |
1224 | 1029 * The 6 fields mean: |
1030 * 0: up-left multiplier | |
1031 * 1: up multiplier | |
1032 * 2: up-right multiplier | |
1033 * 3: left multiplier | |
1034 */ | |
10133 | 1035 static const int predictor_transform[16][4] = { |
3501 | 1036 { 0, 0, 0, 0}, |
1037 { 0, 0, 0,128}, // PL | |
1038 { 0, 0,128, 0}, // PUR | |
1039 { 0, 0, 53, 75}, // PUR|PL | |
1040 { 0,128, 0, 0}, // PU | |
1041 { 0, 64, 0, 64}, // PU|PL | |
1042 { 0,128, 0, 0}, // PU|PUR | |
1043 { 0, 0, 53, 75}, // PU|PUR|PL | |
1044 {128, 0, 0, 0}, // PUL | |
1045 { 0, 0, 0,128}, // PUL|PL | |
1046 { 64, 0, 64, 0}, // PUL|PUR | |
1047 { 0, 0, 53, 75}, // PUL|PUR|PL | |
1048 { 0,128, 0, 0}, // PUL|PU | |
1049 {-104,116, 0,116}, // PUL|PU|PL | |
1050 { 24, 80, 24, 0}, // PUL|PU|PUR | |
1051 {-104,116, 0,116} // PUL|PU|PUR|PL | |
1224 | 1052 }; |
1053 | |
1054 /* This table shows which types of blocks can use other blocks for | |
1055 * prediction. For example, INTRA is the only mode in this table to | |
1056 * have a frame number of 0. That means INTRA blocks can only predict | |
2967 | 1057 * from other INTRA blocks. There are 2 golden frame coding types; |
1224 | 1058 * blocks encoding in these modes can only predict from other blocks |
1059 * that were encoded with these 1 of these 2 modes. */ | |
10650 | 1060 static const unsigned char compatible_frame[9] = { |
1224 | 1061 1, /* MODE_INTER_NO_MV */ |
1062 0, /* MODE_INTRA */ | |
1063 1, /* MODE_INTER_PLUS_MV */ | |
1064 1, /* MODE_INTER_LAST_MV */ | |
1065 1, /* MODE_INTER_PRIOR_MV */ | |
1066 2, /* MODE_USING_GOLDEN */ | |
1067 2, /* MODE_GOLDEN_MV */ | |
10650 | 1068 1, /* MODE_INTER_FOUR_MV */ |
1069 3 /* MODE_COPY */ | |
1224 | 1070 }; |
1071 int current_frame_type; | |
1072 | |
1073 /* there is a last DC predictor for each of the 3 frame types */ | |
1074 short last_dc[3]; | |
1075 | |
1076 int transform = 0; | |
1077 | |
1078 vul = vu = vur = vl = 0; | |
1079 last_dc[0] = last_dc[1] = last_dc[2] = 0; | |
1080 | |
1081 /* for each fragment row... */ | |
1082 for (y = 0; y < fragment_height; y++) { | |
1083 | |
1084 /* for each fragment in a row... */ | |
1085 for (x = 0; x < fragment_width; x++, i++) { | |
1086 | |
1087 /* reverse prediction if this block was coded */ | |
1088 if (s->all_fragments[i].coding_method != MODE_COPY) { | |
1089 | |
2967 | 1090 current_frame_type = |
1224 | 1091 compatible_frame[s->all_fragments[i].coding_method]; |
3500 | 1092 |
1093 transform= 0; | |
1094 if(x){ | |
1095 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
|
1096 vl = DC_COEFF(l); |
10650 | 1097 if(COMPATIBLE_FRAME(l)) |
3501 | 1098 transform |= PL; |
3500 | 1099 } |
1100 if(y){ | |
1101 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
|
1102 vu = DC_COEFF(u); |
10650 | 1103 if(COMPATIBLE_FRAME(u)) |
3501 | 1104 transform |= PU; |
3500 | 1105 if(x){ |
1106 ul= i-fragment_width-1; | |
1107 vul = DC_COEFF(ul); | |
10650 | 1108 if(COMPATIBLE_FRAME(ul)) |
3501 | 1109 transform |= PUL; |
3500 | 1110 } |
1111 if(x + 1 < fragment_width){ | |
1112 ur= i-fragment_width+1; | |
1113 vur = DC_COEFF(ur); | |
10650 | 1114 if(COMPATIBLE_FRAME(ur)) |
3501 | 1115 transform |= PUR; |
3500 | 1116 } |
1224 | 1117 } |
1118 | |
1119 if (transform == 0) { | |
1120 | |
1121 /* if there were no fragments to predict from, use last | |
1122 * 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
|
1123 predicted_dc = last_dc[current_frame_type]; |
1224 | 1124 } else { |
1125 | |
1126 /* apply the appropriate predictor transform */ | |
1127 predicted_dc = | |
1128 (predictor_transform[transform][0] * vul) + | |
1129 (predictor_transform[transform][1] * vu) + | |
1130 (predictor_transform[transform][2] * vur) + | |
1131 (predictor_transform[transform][3] * vl); | |
1132 | |
3502 | 1133 predicted_dc /= 128; |
1224 | 1134 |
1135 /* check for outranging on the [ul u l] and | |
1136 * [ul u ur l] predictors */ | |
10649
a34d38ec5a0b
Check transform==15 first, since it's more common than 13.
cehoyos
parents:
10648
diff
changeset
|
1137 if ((transform == 15) || (transform == 13)) { |
4001 | 1138 if (FFABS(predicted_dc - vu) > 128) |
1224 | 1139 predicted_dc = vu; |
4001 | 1140 else if (FFABS(predicted_dc - vl) > 128) |
1224 | 1141 predicted_dc = vl; |
4001 | 1142 else if (FFABS(predicted_dc - vul) > 128) |
1224 | 1143 predicted_dc = vul; |
1144 } | |
1145 } | |
1146 | |
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
|
1147 /* at long last, apply the predictor */ |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1148 DC_COEFF(i) += predicted_dc; |
1224 | 1149 /* 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
|
1150 last_dc[current_frame_type] = DC_COEFF(i); |
1224 | 1151 } |
1152 } | |
1153 } | |
1154 } | |
1155 | |
11130 | 1156 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
|
1157 { |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1158 int x, y; |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1159 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
|
1160 |
11473 | 1161 int width = s->fragment_width[!!plane]; |
1162 int height = s->fragment_height[!!plane]; | |
11131 | 1163 int fragment = s->fragment_start [plane] + ystart * width; |
1164 int stride = s->current_frame.linesize[plane]; | |
1165 uint8_t *plane_data = s->current_frame.data [plane]; | |
1166 if (!s->flipped_image) stride = -stride; | |
11133 | 1167 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
|
1168 |
11131 | 1169 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
|
1170 |
11131 | 1171 for (x = 0; x < width; x++) { |
1172 /* This code basically just deblocks on the edges of coded blocks. | |
1173 * However, it has to be much more complicated because of the | |
1174 * braindamaged deblock ordering used in VP3/Theora. Order matters | |
1175 * because some pixels get filtered twice. */ | |
1176 if( s->all_fragments[fragment].coding_method != MODE_COPY ) | |
1177 { | |
1178 /* do not perform left edge filter for left columns frags */ | |
1179 if (x > 0) { | |
1180 s->dsp.vp3_h_loop_filter( | |
11133 | 1181 plane_data + 8*x, |
11131 | 1182 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
|
1183 } |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1184 |
11131 | 1185 /* do not perform top edge filter for top row fragments */ |
1186 if (y > 0) { | |
1187 s->dsp.vp3_v_loop_filter( | |
11133 | 1188 plane_data + 8*x, |
11131 | 1189 stride, bounding_values); |
1190 } | |
1191 | |
1192 /* do not perform right edge filter for right column | |
1193 * fragments or if right fragment neighbor is also coded | |
1194 * in this frame (it will be filtered in next iteration) */ | |
1195 if ((x < width - 1) && | |
1196 (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) { | |
1197 s->dsp.vp3_h_loop_filter( | |
11133 | 1198 plane_data + 8*x + 8, |
11131 | 1199 stride, bounding_values); |
1200 } | |
1201 | |
1202 /* do not perform bottom edge filter for bottom row | |
1203 * fragments or if bottom fragment neighbor is also coded | |
1204 * in this frame (it will be filtered in the next row) */ | |
1205 if ((y < height - 1) && | |
1206 (s->all_fragments[fragment + width].coding_method == MODE_COPY)) { | |
1207 s->dsp.vp3_v_loop_filter( | |
11133 | 1208 plane_data + 8*x + 8*stride, |
11131 | 1209 stride, bounding_values); |
1210 } | |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1211 } |
11131 | 1212 |
1213 fragment++; | |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1214 } |
11133 | 1215 plane_data += 8*stride; |
11131 | 1216 } |
11129
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1217 } |
0bca1b2ac58d
Move apply_loop_filter above render_slice, it'll be used by the latter soon
conrad
parents:
11128
diff
changeset
|
1218 |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1219 /** |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1220 * 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
|
1221 * for the next block in coding order |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1222 */ |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1223 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
|
1224 int plane, int inter, DCTELEM block[64]) |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1225 { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1226 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
|
1227 uint8_t *perm = s->scantable.permutated; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1228 int i = 0; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1229 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1230 do { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1231 int token = *s->dct_tokens[plane][i]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1232 switch (token & 3) { |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1233 case 0: // EOB |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1234 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
|
1235 s->dct_tokens[plane][i]++; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1236 else |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1237 *s->dct_tokens[plane][i] = token & ~3; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1238 goto end; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1239 case 1: // zero run |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1240 s->dct_tokens[plane][i]++; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1241 i += (token >> 2) & 0x7f; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1242 block[perm[i]] = (token >> 9) * dequantizer[perm[i]]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1243 i++; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1244 break; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1245 case 2: // coeff |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1246 block[perm[i]] = (token >> 2) * dequantizer[perm[i]]; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1247 s->dct_tokens[plane][i++]++; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1248 break; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1249 default: |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1250 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
|
1251 return i; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1252 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1253 } while (i < 64); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1254 end: |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1255 // 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
|
1256 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
|
1257 return i; |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1258 } |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1259 |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1260 /** |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1261 * 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
|
1262 */ |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1263 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
|
1264 { |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1265 int h, cy; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1266 int offset[4]; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1267 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1268 if(s->avctx->draw_horiz_band==NULL) |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1269 return; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1270 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1271 h= y - s->last_slice_end; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1272 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 if (!s->flipped_image) { |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1275 if (y == 0) |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1276 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
|
1277 y = s->height - y - h; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1278 } |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1279 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1280 cy = y >> 1; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1281 offset[0] = s->current_frame.linesize[0]*y; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1282 offset[1] = s->current_frame.linesize[1]*cy; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1283 offset[2] = s->current_frame.linesize[2]*cy; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1284 offset[3] = 0; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1285 |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1286 emms_c(); |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1287 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
|
1288 s->last_slice_end= y + h; |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1289 } |
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1290 |
1224 | 1291 /* |
2722 | 1292 * Perform the final rendering for a particular slice of data. |
11352 | 1293 * The slice number ranges from 0..(c_superblock_height - 1). |
2722 | 1294 */ |
1295 static void render_slice(Vp3DecodeContext *s, int slice) | |
1296 { | |
11352 | 1297 int x, y, i, j; |
11195 | 1298 LOCAL_ALIGNED_16(DCTELEM, block, [64]); |
2722 | 1299 int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef; |
1300 int motion_halfpel_index; | |
1301 uint8_t *motion_source; | |
11352 | 1302 int plane, first_pixel; |
2722 | 1303 |
11352 | 1304 if (slice >= s->c_superblock_height) |
2722 | 1305 return; |
1306 | |
1307 for (plane = 0; plane < 3; plane++) { | |
11133 | 1308 uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane]; |
1309 uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane]; | |
1310 uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane]; | |
3498 | 1311 int stride = s->current_frame.linesize[plane]; |
1312 int plane_width = s->width >> !!plane; | |
1313 int plane_height = s->height >> !!plane; | |
11352 | 1314 |
1315 int sb_x, sb_y = slice << !plane; | |
1316 int slice_height = sb_y + (plane ? 1 : 2); | |
1317 int slice_width = plane ? s->c_superblock_width : s->y_superblock_width; | |
1318 | |
11473 | 1319 int fragment_width = s->fragment_width[!!plane]; |
1320 int fragment_height = s->fragment_height[!!plane]; | |
11352 | 1321 int fragment_start = s->fragment_start[plane]; |
3498 | 1322 |
1323 if (!s->flipped_image) stride = -stride; | |
11154 | 1324 if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY)) |
1325 continue; | |
3498 | 1326 |
2967 | 1327 |
4001 | 1328 if(FFABS(stride) > 2048) |
2722 | 1329 return; //various tables are fixed size |
1330 | |
11352 | 1331 /* for each superblock row in the slice (both of them)... */ |
1332 for (; sb_y < slice_height; sb_y++) { | |
1333 | |
1334 /* for each superblock in a row... */ | |
1335 for (sb_x = 0; sb_x < slice_width; sb_x++) { | |
2722 | 1336 |
11352 | 1337 /* for each block in a superblock... */ |
1338 for (j = 0; j < 16; j++) { | |
1339 x = 4*sb_x + hilbert_offset[j][0]; | |
1340 y = 4*sb_y + hilbert_offset[j][1]; | |
2722 | 1341 |
11352 | 1342 i = fragment_start + y*fragment_width + x; |
1343 | |
1344 // bounds check | |
1345 if (x >= fragment_width || y >= fragment_height) | |
1346 continue; | |
1347 | |
1348 first_pixel = 8*y*stride + 8*x; | |
2722 | 1349 |
1350 /* transform if this block was coded */ | |
11154 | 1351 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
|
1352 int intra = s->all_fragments[i].coding_method == MODE_INTRA; |
2722 | 1353 |
1354 if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) || | |
1355 (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) | |
1356 motion_source= golden_plane; | |
2967 | 1357 else |
2722 | 1358 motion_source= last_plane; |
1359 | |
11133 | 1360 motion_source += first_pixel; |
2722 | 1361 motion_halfpel_index = 0; |
1362 | |
1363 /* sort out the motion vector if this fragment is coded | |
1364 * using a motion vector method */ | |
1365 if ((s->all_fragments[i].coding_method > MODE_INTRA) && | |
1366 (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) { | |
1367 int src_x, src_y; | |
1368 motion_x = s->all_fragments[i].motion_x; | |
1369 motion_y = s->all_fragments[i].motion_y; | |
1370 if(plane){ | |
1371 motion_x= (motion_x>>1) | (motion_x&1); | |
1372 motion_y= (motion_y>>1) | (motion_y&1); | |
1373 } | |
1374 | |
11352 | 1375 src_x= (motion_x>>1) + 8*x; |
1376 src_y= (motion_y>>1) + 8*y; | |
2722 | 1377 |
1378 motion_halfpel_index = motion_x & 0x01; | |
1379 motion_source += (motion_x >> 1); | |
1380 | |
1381 motion_halfpel_index |= (motion_y & 0x01) << 1; | |
1382 motion_source += ((motion_y >> 1) * stride); | |
1383 | |
1384 if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){ | |
1385 uint8_t *temp= s->edge_emu_buffer; | |
1386 if(stride<0) temp -= 9*stride; | |
1387 else temp += 9*stride; | |
1388 | |
1389 ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height); | |
1390 motion_source= temp; | |
1391 } | |
1392 } | |
2967 | 1393 |
2722 | 1394 |
1395 /* first, take care of copying a block from either the | |
1396 * previous or the golden frame */ | |
1397 if (s->all_fragments[i].coding_method != MODE_INTRA) { | |
2967 | 1398 /* Note, it is possible to implement all MC cases with |
1399 put_no_rnd_pixels_l2 which would look more like the | |
1400 VP3 source but this would be slower as | |
2722 | 1401 put_no_rnd_pixels_tab is better optimzed */ |
1402 if(motion_halfpel_index != 3){ | |
1403 s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index]( | |
11133 | 1404 output_plane + first_pixel, |
2722 | 1405 motion_source, stride, 8); |
1406 }else{ | |
1407 int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1 | |
1408 s->dsp.put_no_rnd_pixels_l2[1]( | |
11133 | 1409 output_plane + first_pixel, |
2967 | 1410 motion_source - d, |
1411 motion_source + stride + 1 + d, | |
2722 | 1412 stride, 8); |
1413 } | |
1414 } | |
1415 | |
8288 | 1416 s->dsp.clear_block(block); |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1417 vp3_dequant(s, s->all_fragments + i, plane, !intra, block); |
2722 | 1418 |
1419 /* invert DCT and place (or add) in final output */ | |
2967 | 1420 |
2722 | 1421 if (s->all_fragments[i].coding_method == MODE_INTRA) { |
1422 if(s->avctx->idct_algo!=FF_IDCT_VP3) | |
1423 block[0] += 128<<3; | |
1424 s->dsp.idct_put( | |
11133 | 1425 output_plane + first_pixel, |
2722 | 1426 stride, |
1427 block); | |
1428 } else { | |
1429 s->dsp.idct_add( | |
11133 | 1430 output_plane + first_pixel, |
2722 | 1431 stride, |
1432 block); | |
1433 } | |
1434 } else { | |
1435 | |
1436 /* copy directly from the previous frame */ | |
1437 s->dsp.put_pixels_tab[1][0]( | |
11133 | 1438 output_plane + first_pixel, |
1439 last_plane + first_pixel, | |
2722 | 1440 stride, 8); |
1441 | |
1442 } | |
11352 | 1443 } |
2722 | 1444 } |
11352 | 1445 |
1446 // Filter up to the last row in the superblock row | |
1447 apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1)); | |
2722 | 1448 } |
1449 } | |
1450 | |
1451 /* this looks like a good place for slice dispatch... */ | |
1452 /* algorithm: | |
2723
5ae980827158
apply the loop filter to fragments as they are rendered into the final
melanson
parents:
2722
diff
changeset
|
1453 * 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
|
1454 * 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
|
1455 * else if (slice > 0) |
2722 | 1456 * dispatch (slice - 1); |
1457 */ | |
1458 | |
11430 | 1459 vp3_draw_horiz_band(s, FFMIN(64*slice + 64-16, s->height-16)); |
2722 | 1460 } |
1461 | |
2967 | 1462 /* |
1224 | 1463 * This is the ffmpeg/libavcodec API init function. |
1464 */ | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6448
diff
changeset
|
1465 static av_cold int vp3_decode_init(AVCodecContext *avctx) |
1224 | 1466 { |
1467 Vp3DecodeContext *s = avctx->priv_data; | |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1468 int i, inter, plane; |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1469 int c_width; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1470 int c_height; |
1224 | 1471 |
1664 | 1472 if (avctx->codec_tag == MKTAG('V','P','3','0')) |
2979 | 1473 s->version = 0; |
1664 | 1474 else |
2979 | 1475 s->version = 1; |
1664 | 1476 |
1224 | 1477 s->avctx = avctx; |
9686
bc32976d6d9d
Move ALIGN macro to libavutil/common.h and use it in various places
conrad
parents:
9637
diff
changeset
|
1478 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
|
1479 s->height = FFALIGN(avctx->height, 16); |
1224 | 1480 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
|
1481 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; |
2693 | 1482 if(avctx->idct_algo==FF_IDCT_AUTO) |
1483 avctx->idct_algo=FF_IDCT_VP3; | |
1224 | 1484 dsputil_init(&s->dsp, avctx); |
2967 | 1485 |
2694 | 1486 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); |
1224 | 1487 |
1488 /* initialize to an impossible value which will force a recalculation | |
1489 * in the first frame decode */ | |
9731 | 1490 for (i = 0; i < 3; i++) |
1491 s->qps[i] = -1; | |
1224 | 1492 |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1493 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
|
1494 s->y_superblock_height = (s->height + 31) / 32; |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1495 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
|
1496 |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1497 /* 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
|
1498 c_width = s->width / 2; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1499 c_height = s->height / 2; |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1500 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
|
1501 s->c_superblock_height = (c_height + 31) / 32; |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1502 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
|
1503 |
11351
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1504 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
|
1505 s->u_superblock_start = s->y_superblock_count; |
0f55f380a1b2
Explictly separate decoding whether fragments are coded by plane
conrad
parents:
11314
diff
changeset
|
1506 s->v_superblock_start = s->u_superblock_start + s->c_superblock_count; |
1224 | 1507 s->superblock_coding = av_malloc(s->superblock_count); |
1508 | |
1509 s->macroblock_width = (s->width + 15) / 16; | |
1510 s->macroblock_height = (s->height + 15) / 16; | |
1511 s->macroblock_count = s->macroblock_width * s->macroblock_height; | |
1512 | |
11473 | 1513 s->fragment_width[0] = s->width / FRAGMENT_PIXELS; |
1514 s->fragment_height[0] = s->height / FRAGMENT_PIXELS; | |
1515 s->fragment_width[1] = s->fragment_width[0] >> 1; | |
1516 s->fragment_height[1] = s->fragment_height[0] >> 1; | |
1224 | 1517 |
1518 /* fragment count covers all 8x8 blocks for all 3 planes */ | |
11473 | 1519 s->fragment_count = s->fragment_width[0] * s->fragment_height[0] * 3 / 2; |
1520 s->fragment_start[1] = s->fragment_width[0] * s->fragment_height[0]; | |
1521 s->fragment_start[2] = s->fragment_width[0] * s->fragment_height[0] * 5 / 4; | |
1224 | 1522 |
1523 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
|
1524 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
|
1525 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
|
1526 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
|
1527 !s->coded_fragment_list[0]) { |
10258 | 1528 vp3_decode_end(avctx); |
1529 return -1; | |
1530 } | |
1224 | 1531 |
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
|
1532 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
|
1533 { |
3585 | 1534 for (i = 0; i < 64; i++) { |
2979 | 1535 s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i]; |
1536 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
|
1537 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
|
1538 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
|
1539 s->base_matrix[2][i] = vp31_inter_dequant[i]; |
2979 | 1540 s->filter_limit_values[i] = vp31_filter_limit_values[i]; |
3585 | 1541 } |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1542 |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1543 for(inter=0; inter<2; inter++){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1544 for(plane=0; plane<3; plane++){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1545 s->qr_count[inter][plane]= 1; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1546 s->qr_size [inter][plane][0]= 63; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1547 s->qr_base [inter][plane][0]= |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1548 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
|
1549 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1550 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1551 |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1552 /* init VLC tables */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1553 for (i = 0; i < 16; i++) { |
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 /* DC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1556 init_vlc(&s->dc_vlc[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1557 &dc_bias[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1558 &dc_bias[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 1 AC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1561 init_vlc(&s->ac_vlc_1[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1562 &ac_bias_0[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1563 &ac_bias_0[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 2 AC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1566 init_vlc(&s->ac_vlc_2[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1567 &ac_bias_1[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1568 &ac_bias_1[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 3 AC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1571 init_vlc(&s->ac_vlc_3[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1572 &ac_bias_2[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1573 &ac_bias_2[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 /* group 4 AC histograms */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1576 init_vlc(&s->ac_vlc_4[i], 5, 32, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1577 &ac_bias_3[i][0][1], 4, 2, |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1578 &ac_bias_3[i][0][0], 4, 2, 0); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1579 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1580 } else { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1581 for (i = 0; i < 16; i++) { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1582 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1583 /* DC histograms */ |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1584 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
|
1585 &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
|
1586 &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
|
1587 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1588 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1589 /* 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
|
1590 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
|
1591 &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
|
1592 &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
|
1593 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1594 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1595 /* 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
|
1596 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
|
1597 &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
|
1598 &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
|
1599 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1600 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1601 /* 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
|
1602 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
|
1603 &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
|
1604 &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
|
1605 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1606 |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1607 /* 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
|
1608 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
|
1609 &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
|
1610 &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
|
1611 goto vlc_fail; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1612 } |
1224 | 1613 } |
1614 | |
2703
3817945001ce
replace get_superblock_run_length() with a VLC table
melanson
parents:
2702
diff
changeset
|
1615 init_vlc(&s->superblock_run_length_vlc, 6, 34, |
3817945001ce
replace get_superblock_run_length() with a VLC table
melanson
parents:
2702
diff
changeset
|
1616 &superblock_run_length_vlc_table[0][1], 4, 2, |
3817945001ce
replace get_superblock_run_length() with a VLC table
melanson
parents:
2702
diff
changeset
|
1617 &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
|
1618 |
2942
868c48736d1c
fixed long standing off-by-one bug (fixes playback on ppc)
alex
parents:
2866
diff
changeset
|
1619 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
|
1620 &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
|
1621 &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
|
1622 |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1623 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
|
1624 &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
|
1625 &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
|
1626 |
5a4e5225cbb6
use VLCs for in place of get_fragment_run_length(), get_mode_code(), and
melanson
parents:
2699
diff
changeset
|
1627 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
|
1628 &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
|
1629 &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
|
1630 |
1224 | 1631 /* work out the block mapping tables */ |
1632 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
|
1633 s->macroblock_coding = av_malloc(s->macroblock_count + 1); |
11150 | 1634 if (!s->superblock_fragments || !s->macroblock_coding) { |
10258 | 1635 vp3_decode_end(avctx); |
1636 return -1; | |
1637 } | |
1224 | 1638 init_block_mapping(s); |
1639 | |
1229
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1640 for (i = 0; i < 3; i++) { |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1641 s->current_frame.data[i] = NULL; |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1642 s->last_frame.data[i] = NULL; |
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1643 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
|
1644 } |
184c480cefc3
fix decoder so that ffmpeg does not crash, at least not right away
tmmm
parents:
1224
diff
changeset
|
1645 |
1224 | 1646 return 0; |
9923
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1647 |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1648 vlc_fail: |
ba58c3abf832
Make decode_init fail if the huffman tables are invalid and thus init_vlc fails.
reimar
parents:
9922
diff
changeset
|
1649 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
|
1650 return -1; |
1224 | 1651 } |
1652 | |
1653 /* | |
1654 * This is the ffmpeg/libavcodec API frame decode function. | |
1655 */ | |
2967 | 1656 static int vp3_decode_frame(AVCodecContext *avctx, |
1224 | 1657 void *data, int *data_size, |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
9007
diff
changeset
|
1658 AVPacket *avpkt) |
1224 | 1659 { |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
9007
diff
changeset
|
1660 const uint8_t *buf = avpkt->data; |
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
9007
diff
changeset
|
1661 int buf_size = avpkt->size; |
1224 | 1662 Vp3DecodeContext *s = avctx->priv_data; |
1663 GetBitContext gb; | |
1664 static int counter = 0; | |
2722 | 1665 int i; |
1224 | 1666 |
1667 init_get_bits(&gb, buf, buf_size * 8); | |
2967 | 1668 |
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
|
1669 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
|
1670 { |
2979 | 1671 av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n"); |
1672 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
|
1673 } |
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
|
1674 |
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
|
1675 s->keyframe = !get_bits1(&gb); |
1664 | 1676 if (!s->theora) |
2979 | 1677 skip_bits(&gb, 1); |
9731 | 1678 for (i = 0; i < 3; i++) |
1679 s->last_qps[i] = s->qps[i]; | |
3492
fa3794c46f21
parse all QIS values (we still ignore them though ...)
michael
parents:
3491
diff
changeset
|
1680 |
9731 | 1681 s->nqps=0; |
3492
fa3794c46f21
parse all QIS values (we still ignore them though ...)
michael
parents:
3491
diff
changeset
|
1682 do{ |
9731 | 1683 s->qps[s->nqps++]= get_bits(&gb, 6); |
1684 } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb)); | |
1685 for (i = s->nqps; i < 3; i++) | |
1686 s->qps[i] = -1; | |
1224 | 1687 |
1667 | 1688 if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
2979 | 1689 av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n", |
9731 | 1690 s->keyframe?"key":"", counter, s->qps[0]); |
1224 | 1691 counter++; |
1692 | |
9731 | 1693 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
|
1694 init_loop_filter(s); |
9731 | 1695 |
1696 for (i = 0; i < s->nqps; i++) | |
1697 // reinit all dequantizers if the first one changed, because | |
1698 // the DC of the first quantizer must be used for all matrices | |
1699 if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0]) | |
1700 init_dequantizer(s, i); | |
1292
f7b3fa4bb7ae
revising and fixing motion vectors, squished block unpacking bug that
tmmm
parents:
1282
diff
changeset
|
1701 |
7938
66226ee647a4
Use skip_frame for keyframe-only decoding rather than #ifdef
conrad
parents:
7875
diff
changeset
|
1702 if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe) |
66226ee647a4
Use skip_frame for keyframe-only decoding rather than #ifdef
conrad
parents:
7875
diff
changeset
|
1703 return buf_size; |
66226ee647a4
Use skip_frame for keyframe-only decoding rather than #ifdef
conrad
parents:
7875
diff
changeset
|
1704 |
11419 | 1705 s->current_frame.reference = 3; |
11423 | 1706 s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE; |
11419 | 1707 if (avctx->get_buffer(avctx, &s->current_frame) < 0) { |
1708 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
11420 | 1709 goto error; |
11419 | 1710 } |
1711 | |
1224 | 1712 if (s->keyframe) { |
2979 | 1713 if (!s->theora) |
1714 { | |
1715 skip_bits(&gb, 4); /* width code */ | |
1716 skip_bits(&gb, 4); /* height code */ | |
1717 if (s->version) | |
1718 { | |
1719 s->version = get_bits(&gb, 5); | |
1720 if (counter == 1) | |
1721 av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version); | |
1722 } | |
1723 } | |
1724 if (s->version || s->theora) | |
1725 { | |
1726 if (get_bits1(&gb)) | |
1727 av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n"); | |
1728 skip_bits(&gb, 2); /* reserved? */ | |
1729 } | |
1224 | 1730 } else { |
11133 | 1731 if (!s->golden_frame.data[0]) { |
11422
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1732 av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n"); |
11423 | 1733 |
11422
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1734 s->golden_frame.reference = 3; |
11423 | 1735 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
|
1736 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
|
1737 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
|
1738 goto error; |
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1739 } |
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1740 s->last_frame = s->golden_frame; |
56266eb23fb7
vp3: Allocate a dummy reference frame if we have no keyframe
conrad
parents:
11421
diff
changeset
|
1741 s->last_frame.type = FF_BUFFER_TYPE_COPY; |
1224 | 1742 } |
1743 } | |
1744 | |
1407 | 1745 s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame |
1746 s->current_frame.qstride= 0; | |
1747 | |
11471 | 1748 memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment)); |
1224 | 1749 |
2687 | 1750 if (unpack_superblocks(s, &gb)){ |
1751 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n"); | |
11420 | 1752 goto error; |
2687 | 1753 } |
1754 if (unpack_modes(s, &gb)){ | |
1755 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n"); | |
11420 | 1756 goto error; |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1757 } |
2687 | 1758 if (unpack_vectors(s, &gb)){ |
1759 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n"); | |
11420 | 1760 goto error; |
2687 | 1761 } |
9731 | 1762 if (unpack_block_qpis(s, &gb)){ |
1763 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n"); | |
11420 | 1764 goto error; |
9731 | 1765 } |
2687 | 1766 if (unpack_dct_coeffs(s, &gb)){ |
1767 av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n"); | |
11420 | 1768 goto error; |
2687 | 1769 } |
1224 | 1770 |
11133 | 1771 for (i = 0; i < 3; i++) { |
1772 if (s->flipped_image) | |
1773 s->data_offset[i] = 0; | |
1774 else | |
1775 s->data_offset[i] = ((s->height>>!!i)-1) * s->current_frame.linesize[i]; | |
1776 } | |
1777 | |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1778 s->last_slice_end = 0; |
11352 | 1779 for (i = 0; i < s->c_superblock_height; i++) |
2722 | 1780 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
|
1781 |
11130 | 1782 // filter the last row |
1783 for (i = 0; i < 3; i++) { | |
1784 int row = (s->height >> (3+!!i)) - 1; | |
1785 apply_loop_filter(s, i, row, row+1); | |
1786 } | |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
1787 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
|
1788 |
1224 | 1789 *data_size=sizeof(AVFrame); |
1790 *(AVFrame*)data= s->current_frame; | |
1791 | |
1229
cefec170ee46
fixed buffer allocation logic (hopefully) so that decoder does not crash
tmmm
parents:
1227
diff
changeset
|
1792 /* 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
|
1793 * golden frame */ |
11421 | 1794 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
|
1795 avctx->release_buffer(avctx, &s->last_frame); |
1224 | 1796 |
1227
184c480cefc3
fix decoder so that ffmpeg does not crash, at least not right away
tmmm
parents:
1224
diff
changeset
|
1797 /* shuffle frames (last = current) */ |
3486 | 1798 s->last_frame= s->current_frame; |
11419 | 1799 |
1800 if (s->keyframe) { | |
1801 if (s->golden_frame.data[0]) | |
1802 avctx->release_buffer(avctx, &s->golden_frame); | |
1803 s->golden_frame = s->current_frame; | |
11421 | 1804 s->last_frame.type = FF_BUFFER_TYPE_COPY; |
11419 | 1805 } |
1806 | |
1405
e69f99ade5b0
fix AVFrame.reference (the frames are used for decoding future frames so it should be !=0)
michaelni
parents:
1355
diff
changeset
|
1807 s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */ |
1224 | 1808 |
1809 return buf_size; | |
11420 | 1810 |
1811 error: | |
1812 if (s->current_frame.data[0]) | |
1813 avctx->release_buffer(avctx, &s->current_frame); | |
1814 return -1; | |
1224 | 1815 } |
1816 | |
1817 /* | |
1818 * This is the ffmpeg/libavcodec API module cleanup function. | |
1819 */ | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6448
diff
changeset
|
1820 static av_cold int vp3_decode_end(AVCodecContext *avctx) |
1224 | 1821 { |
1822 Vp3DecodeContext *s = avctx->priv_data; | |
6393 | 1823 int i; |
1824 | |
1825 av_free(s->superblock_coding); | |
1224 | 1826 av_free(s->all_fragments); |
11353
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1827 av_free(s->coded_fragment_list[0]); |
1ea249b4c6e7
Delay translating DCT tokens into coefficients until immediately before IDCT
conrad
parents:
11352
diff
changeset
|
1828 av_free(s->dct_tokens_base); |
1224 | 1829 av_free(s->superblock_fragments); |
1240
c95ff60bc1a1
fix motion vector decoding bug and reinstate interframes
tmmm
parents:
1239
diff
changeset
|
1830 av_free(s->macroblock_coding); |
2967 | 1831 |
6393 | 1832 for (i = 0; i < 16; i++) { |
1833 free_vlc(&s->dc_vlc[i]); | |
1834 free_vlc(&s->ac_vlc_1[i]); | |
1835 free_vlc(&s->ac_vlc_2[i]); | |
1836 free_vlc(&s->ac_vlc_3[i]); | |
1837 free_vlc(&s->ac_vlc_4[i]); | |
1838 } | |
1839 | |
1840 free_vlc(&s->superblock_run_length_vlc); | |
1841 free_vlc(&s->fragment_run_length_vlc); | |
1842 free_vlc(&s->mode_code_vlc); | |
1843 free_vlc(&s->motion_vector_vlc); | |
1844 | |
1224 | 1845 /* release all frames */ |
11421 | 1846 if (s->golden_frame.data[0]) |
1238
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1847 avctx->release_buffer(avctx, &s->golden_frame); |
11421 | 1848 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
|
1849 avctx->release_buffer(avctx, &s->last_frame); |
6eab0df78d47
squashed a bunch of subtle array indexing bugs, fixed block mapping
tmmm
parents:
1236
diff
changeset
|
1850 /* 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
|
1851 * to the same frame as either the golden or last frame */ |
1224 | 1852 |
1853 return 0; | |
1854 } | |
1855 | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1856 static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb) |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1857 { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1858 Vp3DecodeContext *s = avctx->priv_data; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1859 |
5513 | 1860 if (get_bits1(gb)) { |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1861 int token; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1862 if (s->entries >= 32) { /* overflow */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1863 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
|
1864 return -1; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1865 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1866 token = get_bits(gb, 5); |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1867 //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
|
1868 s->huffman_table[s->hti][token][0] = s->hbits; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1869 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
|
1870 s->entries++; |
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 else { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1873 if (s->huff_code_size >= 32) {/* overflow */ |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1874 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
|
1875 return -1; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1876 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1877 s->huff_code_size++; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1878 s->hbits <<= 1; |
8770 | 1879 if (read_huffman_tree(avctx, gb)) |
1880 return -1; | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1881 s->hbits |= 1; |
8770 | 1882 if (read_huffman_tree(avctx, gb)) |
1883 return -1; | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1884 s->hbits >>= 1; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1885 s->huff_code_size--; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1886 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1887 return 0; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1888 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
1889 |
8590 | 1890 #if CONFIG_THEORA_DECODER |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1891 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
|
1892 { |
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
|
1893 Vp3DecodeContext *s = avctx->priv_data; |
11128 | 1894 int visible_width, visible_height, colorspace; |
3483 | 1895 |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1896 s->theora = get_bits_long(gb, 24); |
6349
aed237dd11d8
Print Theora bitstream version at DEBUG, not at VERBOSE level.
diego
parents:
6282
diff
changeset
|
1897 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
|
1898 |
2678 | 1899 /* 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
|
1900 /* but previous versions have the image flipped relative to vp3 */ |
2678 | 1901 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
|
1902 { |
2979 | 1903 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
|
1904 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
|
1905 } |
f74ae637ffa9
finally working with old theora bitstream (flipped image), the only sample I have is decoded successfully (theora.ogg)
alex
parents:
1598
diff
changeset
|
1906 |
7967
6ac2c8312a2b
Visible width/height fields were added in Theora 3.2
conrad
parents:
7966
diff
changeset
|
1907 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
|
1908 visible_height = s->height = get_bits(gb, 16) << 4; |
2967 | 1909 |
2422 | 1910 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
|
1911 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height); |
2422 | 1912 s->width= s->height= 0; |
1913 return -1; | |
1914 } | |
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
|
1915 |
7967
6ac2c8312a2b
Visible width/height fields were added in Theora 3.2
conrad
parents:
7966
diff
changeset
|
1916 if (s->theora >= 0x030200) { |
7968 | 1917 visible_width = get_bits_long(gb, 24); |
1918 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
|
1919 |
4936 | 1920 skip_bits(gb, 8); /* offset x */ |
1921 skip_bits(gb, 8); /* offset y */ | |
1922 } | |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1923 |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1924 skip_bits(gb, 32); /* fps numerator */ |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1925 skip_bits(gb, 32); /* fps denumerator */ |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1926 skip_bits(gb, 24); /* aspect numerator */ |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1927 skip_bits(gb, 24); /* aspect denumerator */ |
2967 | 1928 |
2678 | 1929 if (s->theora < 0x030200) |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1930 skip_bits(gb, 5); /* keyframe frequency force */ |
11128 | 1931 colorspace = get_bits(gb, 8); |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1932 skip_bits(gb, 24); /* bitrate */ |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1933 |
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1934 skip_bits(gb, 6); /* quality hint */ |
2967 | 1935 |
2678 | 1936 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
|
1937 { |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1938 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
|
1939 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
|
1940 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
|
1941 } |
2967 | 1942 |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1943 // align_get_bits(gb); |
2967 | 1944 |
4935
b34f448e1a12
fix display of theora videos with visible size smaller than encoded size
aurel
parents:
4826
diff
changeset
|
1945 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
|
1946 && 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
|
1947 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
|
1948 else |
b34f448e1a12
fix display of theora videos with visible size smaller than encoded size
aurel
parents:
4826
diff
changeset
|
1949 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
|
1950 |
11128 | 1951 if (colorspace == 1) { |
1952 avctx->color_primaries = AVCOL_PRI_BT470M; | |
1953 } else if (colorspace == 2) { | |
1954 avctx->color_primaries = AVCOL_PRI_BT470BG; | |
1955 } | |
1956 if (colorspace == 1 || colorspace == 2) { | |
1957 avctx->colorspace = AVCOL_SPC_BT470BG; | |
1958 avctx->color_trc = AVCOL_TRC_BT709; | |
1959 } | |
1960 | |
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
|
1961 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
|
1962 } |
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
|
1963 |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1964 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
|
1965 { |
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
|
1966 Vp3DecodeContext *s = avctx->priv_data; |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
1967 int i, n, matrices, inter, plane; |
2678 | 1968 |
1969 if (s->theora >= 0x030200) { | |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1970 n = get_bits(gb, 3); |
2731
0b4b57c4a8f5
read loop filter limit values from Theora header, courtesy of Matthieu
melanson
parents:
2726
diff
changeset
|
1971 /* 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
|
1972 for (i = 0; i < 64; i++) { |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1973 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
|
1974 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
|
1975 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
|
1976 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
|
1977 } |
ffdd1d32c40c
Ensure that the filter limit values do not exceed the maximum allowed value of 127.
reimar
parents:
9921
diff
changeset
|
1978 } |
2678 | 1979 } |
2967 | 1980 |
2678 | 1981 if (s->theora >= 0x030200) |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1982 n = get_bits(gb, 4) + 1; |
2678 | 1983 else |
1984 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
|
1985 /* 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
|
1986 for (i = 0; i < 64; i++) |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1987 s->coded_ac_scale_factor[i] = get_bits(gb, n); |
2678 | 1988 |
1989 if (s->theora >= 0x030200) | |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1990 n = get_bits(gb, 4) + 1; |
2678 | 1991 else |
1992 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
|
1993 /* 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
|
1994 for (i = 0; i < 64; i++) |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1995 s->coded_dc_scale_factor[i] = get_bits(gb, n); |
2678 | 1996 |
1997 if (s->theora >= 0x030200) | |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
1998 matrices = get_bits(gb, 9) + 1; |
2678 | 1999 else |
2944
3785497656c9
Correctly skip unknown matrices (patch by Matthieu Castet) and disable comment reading
alex
parents:
2943
diff
changeset
|
2000 matrices = 3; |
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 if(matrices > 384){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2003 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
|
2004 return -1; |
2678 | 2005 } |
3489
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(n=0; n<matrices; n++){ |
2979 | 2008 for (i = 0; i < 64; i++) |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2009 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
|
2010 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2011 |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2012 for (inter = 0; inter <= 1; inter++) { |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2013 for (plane = 0; plane <= 2; plane++) { |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2014 int newqr= 1; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2015 if (inter || plane > 0) |
5513 | 2016 newqr = get_bits1(gb); |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2017 if (!newqr) { |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2018 int qtj, plj; |
5513 | 2019 if(inter && get_bits1(gb)){ |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2020 qtj = 0; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2021 plj = plane; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2022 }else{ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2023 qtj= (3*inter + plane - 1) / 3; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2024 plj= (plane + 2) % 3; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2025 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2026 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
|
2027 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
|
2028 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
|
2029 } else { |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2030 int qri= 0; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2031 int qi = 0; |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2032 |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2033 for(;;){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2034 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
|
2035 if(i>= matrices){ |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2036 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
|
2037 return -1; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2038 } |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2039 s->qr_base[inter][plane][qri]= i; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2040 if(qi >= 63) |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2041 break; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2042 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
|
2043 s->qr_size[inter][plane][qri++]= i; |
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2044 qi += i; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2045 } |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2046 |
2944
3785497656c9
Correctly skip unknown matrices (patch by Matthieu Castet) and disable comment reading
alex
parents:
2943
diff
changeset
|
2047 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
|
2048 av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi); |
2979 | 2049 return -1; |
2050 } | |
3489
9aacd9410e57
attempt to implement xiphs useless and stupid quantization matrix mess
michael
parents:
3488
diff
changeset
|
2051 s->qr_count[inter][plane]= qri; |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2052 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2053 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2054 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2055 |
2944
3785497656c9
Correctly skip unknown matrices (patch by Matthieu Castet) and disable comment reading
alex
parents:
2943
diff
changeset
|
2056 /* Huffman tables */ |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2057 for (s->hti = 0; s->hti < 80; s->hti++) { |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2058 s->entries = 0; |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2059 s->huff_code_size = 1; |
5513 | 2060 if (!get_bits1(gb)) { |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2061 s->hbits = 0; |
8770 | 2062 if(read_huffman_tree(avctx, gb)) |
2063 return -1; | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2064 s->hbits = 1; |
8770 | 2065 if(read_huffman_tree(avctx, gb)) |
2066 return -1; | |
2718
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2067 } |
158dc437c783
read Huffman tables from Theora header (patch courtesy of
melanson
parents:
2714
diff
changeset
|
2068 } |
2967 | 2069 |
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
|
2070 s->theora_tables = 1; |
2967 | 2071 |
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
|
2072 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
|
2073 } |
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 |
9007
043574c5c153
Add missing av_cold in static init/close functions.
stefano
parents:
8770
diff
changeset
|
2075 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
|
2076 { |
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
|
2077 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
|
2078 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
|
2079 int ptype; |
4723
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2080 uint8_t *header_start[3]; |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2081 int header_len[3]; |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2082 int i; |
2967 | 2083 |
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
|
2084 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
|
2085 |
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 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
|
2087 { |
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
|
2088 av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n"); |
2979 | 2089 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
|
2090 } |
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
|
2091 |
4723
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2092 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
|
2093 42, header_start, header_len) < 0) { |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2094 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
|
2095 return -1; |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2096 } |
b62a3a46856c
use generic xiph header spliting func to split theora headers
aurel
parents:
4594
diff
changeset
|
2097 |
2533 | 2098 for(i=0;i<3;i++) { |
10247 | 2099 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
|
2100 |
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
|
2101 ptype = get_bits(&gb, 8); |
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 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
|
2104 { |
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
|
2105 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
|
2106 // return -1; |
2967 | 2107 } |
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
|
2108 |
4561 | 2109 // FIXME: Check for this as well. |
9637 | 2110 skip_bits_long(&gb, 6*8); /* "theora" */ |
2967 | 2111 |
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
|
2112 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
|
2113 { |
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
|
2114 case 0x80: |
3484
9a70c6d3c653
check how many bits are left after decoding extradata
michael
parents:
3483
diff
changeset
|
2115 theora_decode_header(avctx, &gb); |
2979 | 2116 break; |
2117 case 0x81: | |
2944
3785497656c9
Correctly skip unknown matrices (patch by Matthieu Castet) and disable comment reading
alex
parents:
2943
diff
changeset
|
2118 // FIXME: is this needed? it breaks sometimes |
2979 | 2119 // theora_decode_comments(avctx, gb); |
2120 break; | |
2121 case 0x82: | |
8770 | 2122 if (theora_decode_tables(avctx, &gb)) |
2123 return -1; | |
2979 | 2124 break; |
2125 default: | |
2126 av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80); | |
2127 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
|
2128 } |
7943
3ff31e4454cd
Downgrade severity of leftover bits in header packets, and don't check for the comment header
conrad
parents:
7938
diff
changeset
|
2129 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
|
2130 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
|
2131 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
|
2132 break; |
2533 | 2133 } |
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
|
2134 |
9925
2978d5f0ac0a
Remove useless ret variable added in last revision again.
reimar
parents:
9924
diff
changeset
|
2135 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
|
2136 } |
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 |
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 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
|
2139 "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
|
2140 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
|
2141 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
|
2142 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
|
2143 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
|
2144 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
|
2145 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
|
2146 vp3_decode_frame, |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
2147 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, |
6710 | 2148 NULL, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
7038
diff
changeset
|
2149 .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
|
2150 }; |
4823
cff8c7ecac71
Fix compilation when Theora decoder is disabled, but VP3 is enabled.
diego
parents:
4801
diff
changeset
|
2151 #endif |
4825
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2152 |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2153 AVCodec vp3_decoder = { |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2154 "vp3", |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2155 CODEC_TYPE_VIDEO, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2156 CODEC_ID_VP3, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2157 sizeof(Vp3DecodeContext), |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2158 vp3_decode_init, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2159 NULL, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2160 vp3_decode_end, |
3f32f0e5a77b
cosmetics: Move one code block to save an #ifdef in the next commit.
diego
parents:
4824
diff
changeset
|
2161 vp3_decode_frame, |
11132
449c12b6c3a0
Implement CODEC_CAP_DRAW_HORIZ_BAND for VP3 decoder
conrad
parents:
11131
diff
changeset
|
2162 CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND, |
6710 | 2163 NULL, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
7038
diff
changeset
|
2164 .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
|
2165 }; |