ffmpeg / libavcodec / vc1dec.c @ 4778b4dd
History | View | Annotate | Download (124 KB)
1 |
/*
|
---|---|
2 |
* VC-1 and WMV3 decoder
|
3 |
* Copyright (c) 2006-2007 Konstantin Shishkov
|
4 |
* Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
|
5 |
*
|
6 |
* This file is part of FFmpeg.
|
7 |
*
|
8 |
* FFmpeg is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU Lesser General Public
|
10 |
* License as published by the Free Software Foundation; either
|
11 |
* version 2.1 of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* FFmpeg is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
16 |
* Lesser General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU Lesser General Public
|
19 |
* License along with FFmpeg; if not, write to the Free Software
|
20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
21 |
*/
|
22 |
|
23 |
/**
|
24 |
* @file
|
25 |
* VC-1 and WMV3 decoder
|
26 |
*
|
27 |
*/
|
28 |
#include "internal.h" |
29 |
#include "dsputil.h" |
30 |
#include "avcodec.h" |
31 |
#include "mpegvideo.h" |
32 |
#include "h263.h" |
33 |
#include "vc1.h" |
34 |
#include "vc1data.h" |
35 |
#include "vc1acdata.h" |
36 |
#include "msmpeg4data.h" |
37 |
#include "unary.h" |
38 |
#include "simple_idct.h" |
39 |
#include "mathops.h" |
40 |
#include "vdpau_internal.h" |
41 |
|
42 |
#undef NDEBUG
|
43 |
#include <assert.h> |
44 |
|
45 |
#define MB_INTRA_VLC_BITS 9 |
46 |
#define DC_VLC_BITS 9 |
47 |
#define AC_VLC_BITS 9 |
48 |
static const uint16_t table_mb_intra[64][2]; |
49 |
|
50 |
|
51 |
static const uint16_t vlc_offs[] = { |
52 |
0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436, |
53 |
2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8620, |
54 |
9262, 10202, 10756, 11310, 12228, 15078 |
55 |
}; |
56 |
|
57 |
/**
|
58 |
* Init VC-1 specific tables and VC1Context members
|
59 |
* @param v The VC1Context to initialize
|
60 |
* @return Status
|
61 |
*/
|
62 |
static int vc1_init_common(VC1Context *v) |
63 |
{ |
64 |
static int done = 0; |
65 |
int i = 0; |
66 |
static VLC_TYPE vlc_table[15078][2]; |
67 |
|
68 |
v->hrd_rate = v->hrd_buffer = NULL;
|
69 |
|
70 |
/* VLC tables */
|
71 |
if(!done)
|
72 |
{ |
73 |
INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
|
74 |
ff_vc1_bfraction_bits, 1, 1, |
75 |
ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS); |
76 |
INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
|
77 |
ff_vc1_norm2_bits, 1, 1, |
78 |
ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS); |
79 |
INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
|
80 |
ff_vc1_norm6_bits, 1, 1, |
81 |
ff_vc1_norm6_codes, 2, 2, 556); |
82 |
INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
|
83 |
ff_vc1_imode_bits, 1, 1, |
84 |
ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS); |
85 |
for (i=0; i<3; i++) |
86 |
{ |
87 |
ff_vc1_ttmb_vlc[i].table = &vlc_table[vlc_offs[i*3+0]]; |
88 |
ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i*3+1] - vlc_offs[i*3+0]; |
89 |
init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
|
90 |
ff_vc1_ttmb_bits[i], 1, 1, |
91 |
ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
92 |
ff_vc1_ttblk_vlc[i].table = &vlc_table[vlc_offs[i*3+1]]; |
93 |
ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i*3+2] - vlc_offs[i*3+1]; |
94 |
init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
|
95 |
ff_vc1_ttblk_bits[i], 1, 1, |
96 |
ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC); |
97 |
ff_vc1_subblkpat_vlc[i].table = &vlc_table[vlc_offs[i*3+2]]; |
98 |
ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i*3+3] - vlc_offs[i*3+2]; |
99 |
init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
|
100 |
ff_vc1_subblkpat_bits[i], 1, 1, |
101 |
ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC); |
102 |
} |
103 |
for(i=0; i<4; i++) |
104 |
{ |
105 |
ff_vc1_4mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i*3+9]]; |
106 |
ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i*3+10] - vlc_offs[i*3+9]; |
107 |
init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
|
108 |
ff_vc1_4mv_block_pattern_bits[i], 1, 1, |
109 |
ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC); |
110 |
ff_vc1_cbpcy_p_vlc[i].table = &vlc_table[vlc_offs[i*3+10]]; |
111 |
ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i*3+11] - vlc_offs[i*3+10]; |
112 |
init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
|
113 |
ff_vc1_cbpcy_p_bits[i], 1, 1, |
114 |
ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
115 |
ff_vc1_mv_diff_vlc[i].table = &vlc_table[vlc_offs[i*3+11]]; |
116 |
ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i*3+12] - vlc_offs[i*3+11]; |
117 |
init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
|
118 |
ff_vc1_mv_diff_bits[i], 1, 1, |
119 |
ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
120 |
} |
121 |
for(i=0; i<8; i++){ |
122 |
ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i+21]];
|
123 |
ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i+22] - vlc_offs[i+21]; |
124 |
init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i], |
125 |
&vc1_ac_tables[i][0][1], 8, 4, |
126 |
&vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC); |
127 |
} |
128 |
done = 1;
|
129 |
} |
130 |
|
131 |
/* Other defaults */
|
132 |
v->pq = -1;
|
133 |
v->mvrange = 0; /* 7.1.1.18, p80 */ |
134 |
|
135 |
return 0; |
136 |
} |
137 |
|
138 |
/***********************************************************************/
|
139 |
/**
|
140 |
* @defgroup vc1bitplane VC-1 Bitplane decoding
|
141 |
* @see 8.7, p56
|
142 |
* @{
|
143 |
*/
|
144 |
|
145 |
/**
|
146 |
* Imode types
|
147 |
* @{
|
148 |
*/
|
149 |
enum Imode {
|
150 |
IMODE_RAW, |
151 |
IMODE_NORM2, |
152 |
IMODE_DIFF2, |
153 |
IMODE_NORM6, |
154 |
IMODE_DIFF6, |
155 |
IMODE_ROWSKIP, |
156 |
IMODE_COLSKIP |
157 |
}; |
158 |
/** @} */ //imode defines |
159 |
|
160 |
|
161 |
/** @} */ //Bitplane group |
162 |
|
163 |
static void vc1_loop_filter_iblk(VC1Context *v, int pq) |
164 |
{ |
165 |
MpegEncContext *s = &v->s; |
166 |
int j;
|
167 |
if (!s->first_slice_line) {
|
168 |
v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
|
169 |
if (s->mb_x)
|
170 |
v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16*s->linesize, s->linesize, pq); |
171 |
v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16*s->linesize+8, s->linesize, pq); |
172 |
for(j = 0; j < 2; j++){ |
173 |
v->vc1dsp.vc1_v_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
|
174 |
if (s->mb_x)
|
175 |
v->vc1dsp.vc1_h_loop_filter8(s->dest[j+1]-8*s->uvlinesize, s->uvlinesize, pq); |
176 |
} |
177 |
} |
178 |
v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8*s->linesize, s->linesize, pq); |
179 |
|
180 |
if (s->mb_y == s->mb_height-1) { |
181 |
if (s->mb_x) {
|
182 |
v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
|
183 |
v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
|
184 |
v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
|
185 |
} |
186 |
v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq); |
187 |
} |
188 |
} |
189 |
|
190 |
/** Do motion compensation over 1 macroblock
|
191 |
* Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
|
192 |
*/
|
193 |
static void vc1_mc_1mv(VC1Context *v, int dir) |
194 |
{ |
195 |
MpegEncContext *s = &v->s; |
196 |
DSPContext *dsp = &v->s.dsp; |
197 |
uint8_t *srcY, *srcU, *srcV; |
198 |
int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
|
199 |
|
200 |
if(!v->s.last_picture.data[0])return; |
201 |
|
202 |
mx = s->mv[dir][0][0]; |
203 |
my = s->mv[dir][0][1]; |
204 |
|
205 |
// store motion vectors for further use in B frames
|
206 |
if(s->pict_type == FF_P_TYPE) {
|
207 |
s->current_picture.motion_val[1][s->block_index[0]][0] = mx; |
208 |
s->current_picture.motion_val[1][s->block_index[0]][1] = my; |
209 |
} |
210 |
uvmx = (mx + ((mx & 3) == 3)) >> 1; |
211 |
uvmy = (my + ((my & 3) == 3)) >> 1; |
212 |
if(v->fastuvmc) {
|
213 |
uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1)); |
214 |
uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1)); |
215 |
} |
216 |
if(!dir) {
|
217 |
srcY = s->last_picture.data[0];
|
218 |
srcU = s->last_picture.data[1];
|
219 |
srcV = s->last_picture.data[2];
|
220 |
} else {
|
221 |
srcY = s->next_picture.data[0];
|
222 |
srcU = s->next_picture.data[1];
|
223 |
srcV = s->next_picture.data[2];
|
224 |
} |
225 |
|
226 |
src_x = s->mb_x * 16 + (mx >> 2); |
227 |
src_y = s->mb_y * 16 + (my >> 2); |
228 |
uvsrc_x = s->mb_x * 8 + (uvmx >> 2); |
229 |
uvsrc_y = s->mb_y * 8 + (uvmy >> 2); |
230 |
|
231 |
if(v->profile != PROFILE_ADVANCED){
|
232 |
src_x = av_clip( src_x, -16, s->mb_width * 16); |
233 |
src_y = av_clip( src_y, -16, s->mb_height * 16); |
234 |
uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8); |
235 |
uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8); |
236 |
}else{
|
237 |
src_x = av_clip( src_x, -17, s->avctx->coded_width);
|
238 |
src_y = av_clip( src_y, -18, s->avctx->coded_height + 1); |
239 |
uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1); |
240 |
uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1); |
241 |
} |
242 |
|
243 |
srcY += src_y * s->linesize + src_x; |
244 |
srcU += uvsrc_y * s->uvlinesize + uvsrc_x; |
245 |
srcV += uvsrc_y * s->uvlinesize + uvsrc_x; |
246 |
|
247 |
/* for grayscale we should not try to read from unknown area */
|
248 |
if(s->flags & CODEC_FLAG_GRAY) {
|
249 |
srcU = s->edge_emu_buffer + 18 * s->linesize;
|
250 |
srcV = s->edge_emu_buffer + 18 * s->linesize;
|
251 |
} |
252 |
|
253 |
if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
|
254 |
|| (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3 |
255 |
|| (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){ |
256 |
uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
|
257 |
|
258 |
srcY -= s->mspel * (1 + s->linesize);
|
259 |
s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2, |
260 |
src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos); |
261 |
srcY = s->edge_emu_buffer; |
262 |
s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1, |
263 |
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); |
264 |
s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1, |
265 |
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); |
266 |
srcU = uvbuf; |
267 |
srcV = uvbuf + 16;
|
268 |
/* if we deal with range reduction we need to scale source blocks */
|
269 |
if(v->rangeredfrm) {
|
270 |
int i, j;
|
271 |
uint8_t *src, *src2; |
272 |
|
273 |
src = srcY; |
274 |
for(j = 0; j < 17 + s->mspel*2; j++) { |
275 |
for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128; |
276 |
src += s->linesize; |
277 |
} |
278 |
src = srcU; src2 = srcV; |
279 |
for(j = 0; j < 9; j++) { |
280 |
for(i = 0; i < 9; i++) { |
281 |
src[i] = ((src[i] - 128) >> 1) + 128; |
282 |
src2[i] = ((src2[i] - 128) >> 1) + 128; |
283 |
} |
284 |
src += s->uvlinesize; |
285 |
src2 += s->uvlinesize; |
286 |
} |
287 |
} |
288 |
/* if we deal with intensity compensation we need to scale source blocks */
|
289 |
if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
|
290 |
int i, j;
|
291 |
uint8_t *src, *src2; |
292 |
|
293 |
src = srcY; |
294 |
for(j = 0; j < 17 + s->mspel*2; j++) { |
295 |
for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]]; |
296 |
src += s->linesize; |
297 |
} |
298 |
src = srcU; src2 = srcV; |
299 |
for(j = 0; j < 9; j++) { |
300 |
for(i = 0; i < 9; i++) { |
301 |
src[i] = v->lutuv[src[i]]; |
302 |
src2[i] = v->lutuv[src2[i]]; |
303 |
} |
304 |
src += s->uvlinesize; |
305 |
src2 += s->uvlinesize; |
306 |
} |
307 |
} |
308 |
srcY += s->mspel * (1 + s->linesize);
|
309 |
} |
310 |
|
311 |
if(s->mspel) {
|
312 |
dxy = ((my & 3) << 2) | (mx & 3); |
313 |
v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
|
314 |
v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd); |
315 |
srcY += s->linesize * 8;
|
316 |
v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd); |
317 |
v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd); |
318 |
} else { // hpel mc - always used for luma |
319 |
dxy = (my & 2) | ((mx & 2) >> 1); |
320 |
|
321 |
if(!v->rnd)
|
322 |
dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16); |
323 |
else
|
324 |
dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16); |
325 |
} |
326 |
|
327 |
if(s->flags & CODEC_FLAG_GRAY) return; |
328 |
/* Chroma MC always uses qpel bilinear */
|
329 |
uvmx = (uvmx&3)<<1; |
330 |
uvmy = (uvmy&3)<<1; |
331 |
if(!v->rnd){
|
332 |
dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy); |
333 |
dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy); |
334 |
}else{
|
335 |
v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy); |
336 |
v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy); |
337 |
} |
338 |
} |
339 |
|
340 |
/** Do motion compensation for 4-MV macroblock - luminance block
|
341 |
*/
|
342 |
static void vc1_mc_4mv_luma(VC1Context *v, int n) |
343 |
{ |
344 |
MpegEncContext *s = &v->s; |
345 |
DSPContext *dsp = &v->s.dsp; |
346 |
uint8_t *srcY; |
347 |
int dxy, mx, my, src_x, src_y;
|
348 |
int off;
|
349 |
|
350 |
if(!v->s.last_picture.data[0])return; |
351 |
mx = s->mv[0][n][0]; |
352 |
my = s->mv[0][n][1]; |
353 |
srcY = s->last_picture.data[0];
|
354 |
|
355 |
off = s->linesize * 4 * (n&2) + (n&1) * 8; |
356 |
|
357 |
src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2); |
358 |
src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2); |
359 |
|
360 |
if(v->profile != PROFILE_ADVANCED){
|
361 |
src_x = av_clip( src_x, -16, s->mb_width * 16); |
362 |
src_y = av_clip( src_y, -16, s->mb_height * 16); |
363 |
}else{
|
364 |
src_x = av_clip( src_x, -17, s->avctx->coded_width);
|
365 |
src_y = av_clip( src_y, -18, s->avctx->coded_height + 1); |
366 |
} |
367 |
|
368 |
srcY += src_y * s->linesize + src_x; |
369 |
|
370 |
if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
|
371 |
|| (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2 |
372 |
|| (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){ |
373 |
srcY -= s->mspel * (1 + s->linesize);
|
374 |
s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2, |
375 |
src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos); |
376 |
srcY = s->edge_emu_buffer; |
377 |
/* if we deal with range reduction we need to scale source blocks */
|
378 |
if(v->rangeredfrm) {
|
379 |
int i, j;
|
380 |
uint8_t *src; |
381 |
|
382 |
src = srcY; |
383 |
for(j = 0; j < 9 + s->mspel*2; j++) { |
384 |
for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128; |
385 |
src += s->linesize; |
386 |
} |
387 |
} |
388 |
/* if we deal with intensity compensation we need to scale source blocks */
|
389 |
if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
|
390 |
int i, j;
|
391 |
uint8_t *src; |
392 |
|
393 |
src = srcY; |
394 |
for(j = 0; j < 9 + s->mspel*2; j++) { |
395 |
for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]]; |
396 |
src += s->linesize; |
397 |
} |
398 |
} |
399 |
srcY += s->mspel * (1 + s->linesize);
|
400 |
} |
401 |
|
402 |
if(s->mspel) {
|
403 |
dxy = ((my & 3) << 2) | (mx & 3); |
404 |
v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
|
405 |
} else { // hpel mc - always used for luma |
406 |
dxy = (my & 2) | ((mx & 2) >> 1); |
407 |
if(!v->rnd)
|
408 |
dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8); |
409 |
else
|
410 |
dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8); |
411 |
} |
412 |
} |
413 |
|
414 |
static inline int median4(int a, int b, int c, int d) |
415 |
{ |
416 |
if(a < b) {
|
417 |
if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2; |
418 |
else return (FFMIN(b, c) + FFMAX(a, d)) / 2; |
419 |
} else {
|
420 |
if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2; |
421 |
else return (FFMIN(a, c) + FFMAX(b, d)) / 2; |
422 |
} |
423 |
} |
424 |
|
425 |
|
426 |
/** Do motion compensation for 4-MV macroblock - both chroma blocks
|
427 |
*/
|
428 |
static void vc1_mc_4mv_chroma(VC1Context *v) |
429 |
{ |
430 |
MpegEncContext *s = &v->s; |
431 |
DSPContext *dsp = &v->s.dsp; |
432 |
uint8_t *srcU, *srcV; |
433 |
int uvmx, uvmy, uvsrc_x, uvsrc_y;
|
434 |
int i, idx, tx = 0, ty = 0; |
435 |
int mvx[4], mvy[4], intra[4]; |
436 |
static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; |
437 |
|
438 |
if(!v->s.last_picture.data[0])return; |
439 |
if(s->flags & CODEC_FLAG_GRAY) return; |
440 |
|
441 |
for(i = 0; i < 4; i++) { |
442 |
mvx[i] = s->mv[0][i][0]; |
443 |
mvy[i] = s->mv[0][i][1]; |
444 |
intra[i] = v->mb_type[0][s->block_index[i]];
|
445 |
} |
446 |
|
447 |
/* calculate chroma MV vector from four luma MVs */
|
448 |
idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0]; |
449 |
if(!idx) { // all blocks are inter |
450 |
tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]); |
451 |
ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]); |
452 |
} else if(count[idx] == 1) { // 3 inter blocks |
453 |
switch(idx) {
|
454 |
case 0x1: |
455 |
tx = mid_pred(mvx[1], mvx[2], mvx[3]); |
456 |
ty = mid_pred(mvy[1], mvy[2], mvy[3]); |
457 |
break;
|
458 |
case 0x2: |
459 |
tx = mid_pred(mvx[0], mvx[2], mvx[3]); |
460 |
ty = mid_pred(mvy[0], mvy[2], mvy[3]); |
461 |
break;
|
462 |
case 0x4: |
463 |
tx = mid_pred(mvx[0], mvx[1], mvx[3]); |
464 |
ty = mid_pred(mvy[0], mvy[1], mvy[3]); |
465 |
break;
|
466 |
case 0x8: |
467 |
tx = mid_pred(mvx[0], mvx[1], mvx[2]); |
468 |
ty = mid_pred(mvy[0], mvy[1], mvy[2]); |
469 |
break;
|
470 |
} |
471 |
} else if(count[idx] == 2) { |
472 |
int t1 = 0, t2 = 0; |
473 |
for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;} |
474 |
for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;} |
475 |
tx = (mvx[t1] + mvx[t2]) / 2;
|
476 |
ty = (mvy[t1] + mvy[t2]) / 2;
|
477 |
} else {
|
478 |
s->current_picture.motion_val[1][s->block_index[0]][0] = 0; |
479 |
s->current_picture.motion_val[1][s->block_index[0]][1] = 0; |
480 |
return; //no need to do MC for inter blocks |
481 |
} |
482 |
|
483 |
s->current_picture.motion_val[1][s->block_index[0]][0] = tx; |
484 |
s->current_picture.motion_val[1][s->block_index[0]][1] = ty; |
485 |
uvmx = (tx + ((tx&3) == 3)) >> 1; |
486 |
uvmy = (ty + ((ty&3) == 3)) >> 1; |
487 |
if(v->fastuvmc) {
|
488 |
uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1)); |
489 |
uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1)); |
490 |
} |
491 |
|
492 |
uvsrc_x = s->mb_x * 8 + (uvmx >> 2); |
493 |
uvsrc_y = s->mb_y * 8 + (uvmy >> 2); |
494 |
|
495 |
if(v->profile != PROFILE_ADVANCED){
|
496 |
uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8); |
497 |
uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8); |
498 |
}else{
|
499 |
uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1); |
500 |
uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1); |
501 |
} |
502 |
|
503 |
srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
|
504 |
srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
|
505 |
if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
|
506 |
|| (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9 |
507 |
|| (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){ |
508 |
s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1, |
509 |
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); |
510 |
s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1, |
511 |
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); |
512 |
srcU = s->edge_emu_buffer; |
513 |
srcV = s->edge_emu_buffer + 16;
|
514 |
|
515 |
/* if we deal with range reduction we need to scale source blocks */
|
516 |
if(v->rangeredfrm) {
|
517 |
int i, j;
|
518 |
uint8_t *src, *src2; |
519 |
|
520 |
src = srcU; src2 = srcV; |
521 |
for(j = 0; j < 9; j++) { |
522 |
for(i = 0; i < 9; i++) { |
523 |
src[i] = ((src[i] - 128) >> 1) + 128; |
524 |
src2[i] = ((src2[i] - 128) >> 1) + 128; |
525 |
} |
526 |
src += s->uvlinesize; |
527 |
src2 += s->uvlinesize; |
528 |
} |
529 |
} |
530 |
/* if we deal with intensity compensation we need to scale source blocks */
|
531 |
if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
|
532 |
int i, j;
|
533 |
uint8_t *src, *src2; |
534 |
|
535 |
src = srcU; src2 = srcV; |
536 |
for(j = 0; j < 9; j++) { |
537 |
for(i = 0; i < 9; i++) { |
538 |
src[i] = v->lutuv[src[i]]; |
539 |
src2[i] = v->lutuv[src2[i]]; |
540 |
} |
541 |
src += s->uvlinesize; |
542 |
src2 += s->uvlinesize; |
543 |
} |
544 |
} |
545 |
} |
546 |
|
547 |
/* Chroma MC always uses qpel bilinear */
|
548 |
uvmx = (uvmx&3)<<1; |
549 |
uvmy = (uvmy&3)<<1; |
550 |
if(!v->rnd){
|
551 |
dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy); |
552 |
dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy); |
553 |
}else{
|
554 |
v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy); |
555 |
v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy); |
556 |
} |
557 |
} |
558 |
|
559 |
/***********************************************************************/
|
560 |
/**
|
561 |
* @defgroup vc1block VC-1 Block-level functions
|
562 |
* @see 7.1.4, p91 and 8.1.1.7, p(1)04
|
563 |
* @{
|
564 |
*/
|
565 |
|
566 |
/**
|
567 |
* @def GET_MQUANT
|
568 |
* @brief Get macroblock-level quantizer scale
|
569 |
*/
|
570 |
#define GET_MQUANT() \
|
571 |
if (v->dquantfrm) \
|
572 |
{ \ |
573 |
int edges = 0; \ |
574 |
if (v->dqprofile == DQPROFILE_ALL_MBS) \
|
575 |
{ \ |
576 |
if (v->dqbilevel) \
|
577 |
{ \ |
578 |
mquant = (get_bits1(gb)) ? v->altpq : v->pq; \ |
579 |
} \ |
580 |
else \
|
581 |
{ \ |
582 |
mqdiff = get_bits(gb, 3); \
|
583 |
if (mqdiff != 7) mquant = v->pq + mqdiff; \ |
584 |
else mquant = get_bits(gb, 5); \ |
585 |
} \ |
586 |
} \ |
587 |
if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
|
588 |
edges = 1 << v->dqsbedge; \
|
589 |
else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \ |
590 |
edges = (3 << v->dqsbedge) % 15; \ |
591 |
else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \ |
592 |
edges = 15; \
|
593 |
if((edges&1) && !s->mb_x) \ |
594 |
mquant = v->altpq; \ |
595 |
if((edges&2) && s->first_slice_line) \ |
596 |
mquant = v->altpq; \ |
597 |
if((edges&4) && s->mb_x == (s->mb_width - 1)) \ |
598 |
mquant = v->altpq; \ |
599 |
if((edges&8) && s->mb_y == (s->mb_height - 1)) \ |
600 |
mquant = v->altpq; \ |
601 |
} |
602 |
|
603 |
/**
|
604 |
* @def GET_MVDATA(_dmv_x, _dmv_y)
|
605 |
* @brief Get MV differentials
|
606 |
* @see MVDATA decoding from 8.3.5.2, p(1)20
|
607 |
* @param _dmv_x Horizontal differential for decoded MV
|
608 |
* @param _dmv_y Vertical differential for decoded MV
|
609 |
*/
|
610 |
#define GET_MVDATA(_dmv_x, _dmv_y) \
|
611 |
index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
|
612 |
VC1_MV_DIFF_VLC_BITS, 2); \
|
613 |
if (index > 36) \ |
614 |
{ \ |
615 |
mb_has_coeffs = 1; \
|
616 |
index -= 37; \
|
617 |
} \ |
618 |
else mb_has_coeffs = 0; \ |
619 |
s->mb_intra = 0; \
|
620 |
if (!index) { _dmv_x = _dmv_y = 0; } \ |
621 |
else if (index == 35) \ |
622 |
{ \ |
623 |
_dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
|
624 |
_dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
|
625 |
} \ |
626 |
else if (index == 36) \ |
627 |
{ \ |
628 |
_dmv_x = 0; \
|
629 |
_dmv_y = 0; \
|
630 |
s->mb_intra = 1; \
|
631 |
} \ |
632 |
else \
|
633 |
{ \ |
634 |
index1 = index%6; \
|
635 |
if (!s->quarter_sample && index1 == 5) val = 1; \ |
636 |
else val = 0; \ |
637 |
if(size_table[index1] - val > 0) \ |
638 |
val = get_bits(gb, size_table[index1] - val); \ |
639 |
else val = 0; \ |
640 |
sign = 0 - (val&1); \ |
641 |
_dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
|
642 |
\ |
643 |
index1 = index/6; \
|
644 |
if (!s->quarter_sample && index1 == 5) val = 1; \ |
645 |
else val = 0; \ |
646 |
if(size_table[index1] - val > 0) \ |
647 |
val = get_bits(gb, size_table[index1] - val); \ |
648 |
else val = 0; \ |
649 |
sign = 0 - (val&1); \ |
650 |
_dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
|
651 |
} |
652 |
|
653 |
/** Predict and set motion vector
|
654 |
*/
|
655 |
static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra) |
656 |
{ |
657 |
int xy, wrap, off = 0; |
658 |
int16_t *A, *B, *C; |
659 |
int px, py;
|
660 |
int sum;
|
661 |
|
662 |
/* scale MV difference to be quad-pel */
|
663 |
dmv_x <<= 1 - s->quarter_sample;
|
664 |
dmv_y <<= 1 - s->quarter_sample;
|
665 |
|
666 |
wrap = s->b8_stride; |
667 |
xy = s->block_index[n]; |
668 |
|
669 |
if(s->mb_intra){
|
670 |
s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0; |
671 |
s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0; |
672 |
s->current_picture.motion_val[1][xy][0] = 0; |
673 |
s->current_picture.motion_val[1][xy][1] = 0; |
674 |
if(mv1) { /* duplicate motion data for 1-MV block */ |
675 |
s->current_picture.motion_val[0][xy + 1][0] = 0; |
676 |
s->current_picture.motion_val[0][xy + 1][1] = 0; |
677 |
s->current_picture.motion_val[0][xy + wrap][0] = 0; |
678 |
s->current_picture.motion_val[0][xy + wrap][1] = 0; |
679 |
s->current_picture.motion_val[0][xy + wrap + 1][0] = 0; |
680 |
s->current_picture.motion_val[0][xy + wrap + 1][1] = 0; |
681 |
s->current_picture.motion_val[1][xy + 1][0] = 0; |
682 |
s->current_picture.motion_val[1][xy + 1][1] = 0; |
683 |
s->current_picture.motion_val[1][xy + wrap][0] = 0; |
684 |
s->current_picture.motion_val[1][xy + wrap][1] = 0; |
685 |
s->current_picture.motion_val[1][xy + wrap + 1][0] = 0; |
686 |
s->current_picture.motion_val[1][xy + wrap + 1][1] = 0; |
687 |
} |
688 |
return;
|
689 |
} |
690 |
|
691 |
C = s->current_picture.motion_val[0][xy - 1]; |
692 |
A = s->current_picture.motion_val[0][xy - wrap];
|
693 |
if(mv1)
|
694 |
off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2; |
695 |
else {
|
696 |
//in 4-MV mode different blocks have different B predictor position
|
697 |
switch(n){
|
698 |
case 0: |
699 |
off = (s->mb_x > 0) ? -1 : 1; |
700 |
break;
|
701 |
case 1: |
702 |
off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1; |
703 |
break;
|
704 |
case 2: |
705 |
off = 1;
|
706 |
break;
|
707 |
case 3: |
708 |
off = -1;
|
709 |
} |
710 |
} |
711 |
B = s->current_picture.motion_val[0][xy - wrap + off];
|
712 |
|
713 |
if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds |
714 |
if(s->mb_width == 1) { |
715 |
px = A[0];
|
716 |
py = A[1];
|
717 |
} else {
|
718 |
px = mid_pred(A[0], B[0], C[0]); |
719 |
py = mid_pred(A[1], B[1], C[1]); |
720 |
} |
721 |
} else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds |
722 |
px = C[0];
|
723 |
py = C[1];
|
724 |
} else {
|
725 |
px = py = 0;
|
726 |
} |
727 |
/* Pullback MV as specified in 8.3.5.3.4 */
|
728 |
{ |
729 |
int qx, qy, X, Y;
|
730 |
qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0); |
731 |
qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0); |
732 |
X = (s->mb_width << 6) - 4; |
733 |
Y = (s->mb_height << 6) - 4; |
734 |
if(mv1) {
|
735 |
if(qx + px < -60) px = -60 - qx; |
736 |
if(qy + py < -60) py = -60 - qy; |
737 |
} else {
|
738 |
if(qx + px < -28) px = -28 - qx; |
739 |
if(qy + py < -28) py = -28 - qy; |
740 |
} |
741 |
if(qx + px > X) px = X - qx;
|
742 |
if(qy + py > Y) py = Y - qy;
|
743 |
} |
744 |
/* Calculate hybrid prediction as specified in 8.3.5.3.5 */
|
745 |
if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) { |
746 |
if(is_intra[xy - wrap])
|
747 |
sum = FFABS(px) + FFABS(py); |
748 |
else
|
749 |
sum = FFABS(px - A[0]) + FFABS(py - A[1]); |
750 |
if(sum > 32) { |
751 |
if(get_bits1(&s->gb)) {
|
752 |
px = A[0];
|
753 |
py = A[1];
|
754 |
} else {
|
755 |
px = C[0];
|
756 |
py = C[1];
|
757 |
} |
758 |
} else {
|
759 |
if(is_intra[xy - 1]) |
760 |
sum = FFABS(px) + FFABS(py); |
761 |
else
|
762 |
sum = FFABS(px - C[0]) + FFABS(py - C[1]); |
763 |
if(sum > 32) { |
764 |
if(get_bits1(&s->gb)) {
|
765 |
px = A[0];
|
766 |
py = A[1];
|
767 |
} else {
|
768 |
px = C[0];
|
769 |
py = C[1];
|
770 |
} |
771 |
} |
772 |
} |
773 |
} |
774 |
/* store MV using signed modulus of MV range defined in 4.11 */
|
775 |
s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x; |
776 |
s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y; |
777 |
if(mv1) { /* duplicate motion data for 1-MV block */ |
778 |
s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0]; |
779 |
s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1]; |
780 |
s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0]; |
781 |
s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1]; |
782 |
s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0]; |
783 |
s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1]; |
784 |
} |
785 |
} |
786 |
|
787 |
/** Motion compensation for direct or interpolated blocks in B-frames
|
788 |
*/
|
789 |
static void vc1_interp_mc(VC1Context *v) |
790 |
{ |
791 |
MpegEncContext *s = &v->s; |
792 |
DSPContext *dsp = &v->s.dsp; |
793 |
uint8_t *srcY, *srcU, *srcV; |
794 |
int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
|
795 |
|
796 |
if(!v->s.next_picture.data[0])return; |
797 |
|
798 |
mx = s->mv[1][0][0]; |
799 |
my = s->mv[1][0][1]; |
800 |
uvmx = (mx + ((mx & 3) == 3)) >> 1; |
801 |
uvmy = (my + ((my & 3) == 3)) >> 1; |
802 |
if(v->fastuvmc) {
|
803 |
uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1)); |
804 |
uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1)); |
805 |
} |
806 |
srcY = s->next_picture.data[0];
|
807 |
srcU = s->next_picture.data[1];
|
808 |
srcV = s->next_picture.data[2];
|
809 |
|
810 |
src_x = s->mb_x * 16 + (mx >> 2); |
811 |
src_y = s->mb_y * 16 + (my >> 2); |
812 |
uvsrc_x = s->mb_x * 8 + (uvmx >> 2); |
813 |
uvsrc_y = s->mb_y * 8 + (uvmy >> 2); |
814 |
|
815 |
if(v->profile != PROFILE_ADVANCED){
|
816 |
src_x = av_clip( src_x, -16, s->mb_width * 16); |
817 |
src_y = av_clip( src_y, -16, s->mb_height * 16); |
818 |
uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8); |
819 |
uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8); |
820 |
}else{
|
821 |
src_x = av_clip( src_x, -17, s->avctx->coded_width);
|
822 |
src_y = av_clip( src_y, -18, s->avctx->coded_height + 1); |
823 |
uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1); |
824 |
uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1); |
825 |
} |
826 |
|
827 |
srcY += src_y * s->linesize + src_x; |
828 |
srcU += uvsrc_y * s->uvlinesize + uvsrc_x; |
829 |
srcV += uvsrc_y * s->uvlinesize + uvsrc_x; |
830 |
|
831 |
/* for grayscale we should not try to read from unknown area */
|
832 |
if(s->flags & CODEC_FLAG_GRAY) {
|
833 |
srcU = s->edge_emu_buffer + 18 * s->linesize;
|
834 |
srcV = s->edge_emu_buffer + 18 * s->linesize;
|
835 |
} |
836 |
|
837 |
if(v->rangeredfrm
|
838 |
|| (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3 |
839 |
|| (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){ |
840 |
uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
|
841 |
|
842 |
srcY -= s->mspel * (1 + s->linesize);
|
843 |
s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2, |
844 |
src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos); |
845 |
srcY = s->edge_emu_buffer; |
846 |
s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1, |
847 |
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); |
848 |
s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1, |
849 |
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); |
850 |
srcU = uvbuf; |
851 |
srcV = uvbuf + 16;
|
852 |
/* if we deal with range reduction we need to scale source blocks */
|
853 |
if(v->rangeredfrm) {
|
854 |
int i, j;
|
855 |
uint8_t *src, *src2; |
856 |
|
857 |
src = srcY; |
858 |
for(j = 0; j < 17 + s->mspel*2; j++) { |
859 |
for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128; |
860 |
src += s->linesize; |
861 |
} |
862 |
src = srcU; src2 = srcV; |
863 |
for(j = 0; j < 9; j++) { |
864 |
for(i = 0; i < 9; i++) { |
865 |
src[i] = ((src[i] - 128) >> 1) + 128; |
866 |
src2[i] = ((src2[i] - 128) >> 1) + 128; |
867 |
} |
868 |
src += s->uvlinesize; |
869 |
src2 += s->uvlinesize; |
870 |
} |
871 |
} |
872 |
srcY += s->mspel * (1 + s->linesize);
|
873 |
} |
874 |
|
875 |
if(s->mspel) {
|
876 |
dxy = ((my & 3) << 2) | (mx & 3); |
877 |
v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
|
878 |
v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd); |
879 |
srcY += s->linesize * 8;
|
880 |
v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd); |
881 |
v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd); |
882 |
} else { // hpel mc |
883 |
dxy = (my & 2) | ((mx & 2) >> 1); |
884 |
|
885 |
if(!v->rnd)
|
886 |
dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16); |
887 |
else
|
888 |
dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16); |
889 |
} |
890 |
|
891 |
if(s->flags & CODEC_FLAG_GRAY) return; |
892 |
/* Chroma MC always uses qpel blilinear */
|
893 |
uvmx = (uvmx&3)<<1; |
894 |
uvmy = (uvmy&3)<<1; |
895 |
if(!v->rnd){
|
896 |
dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy); |
897 |
dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy); |
898 |
}else{
|
899 |
v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy); |
900 |
v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy); |
901 |
} |
902 |
} |
903 |
|
904 |
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs) |
905 |
{ |
906 |
int n = bfrac;
|
907 |
|
908 |
#if B_FRACTION_DEN==256 |
909 |
if(inv)
|
910 |
n -= 256;
|
911 |
if(!qs)
|
912 |
return 2 * ((value * n + 255) >> 9); |
913 |
return (value * n + 128) >> 8; |
914 |
#else
|
915 |
if(inv)
|
916 |
n -= B_FRACTION_DEN; |
917 |
if(!qs)
|
918 |
return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN)); |
919 |
return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN; |
920 |
#endif
|
921 |
} |
922 |
|
923 |
/** Reconstruct motion vector for B-frame and do motion compensation
|
924 |
*/
|
925 |
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode) |
926 |
{ |
927 |
if(v->use_ic) {
|
928 |
v->mv_mode2 = v->mv_mode; |
929 |
v->mv_mode = MV_PMODE_INTENSITY_COMP; |
930 |
} |
931 |
if(direct) {
|
932 |
vc1_mc_1mv(v, 0);
|
933 |
vc1_interp_mc(v); |
934 |
if(v->use_ic) v->mv_mode = v->mv_mode2;
|
935 |
return;
|
936 |
} |
937 |
if(mode == BMV_TYPE_INTERPOLATED) {
|
938 |
vc1_mc_1mv(v, 0);
|
939 |
vc1_interp_mc(v); |
940 |
if(v->use_ic) v->mv_mode = v->mv_mode2;
|
941 |
return;
|
942 |
} |
943 |
|
944 |
if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
|
945 |
vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD)); |
946 |
if(v->use_ic) v->mv_mode = v->mv_mode2;
|
947 |
} |
948 |
|
949 |
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype) |
950 |
{ |
951 |
MpegEncContext *s = &v->s; |
952 |
int xy, wrap, off = 0; |
953 |
int16_t *A, *B, *C; |
954 |
int px, py;
|
955 |
int sum;
|
956 |
int r_x, r_y;
|
957 |
const uint8_t *is_intra = v->mb_type[0]; |
958 |
|
959 |
r_x = v->range_x; |
960 |
r_y = v->range_y; |
961 |
/* scale MV difference to be quad-pel */
|
962 |
dmv_x[0] <<= 1 - s->quarter_sample; |
963 |
dmv_y[0] <<= 1 - s->quarter_sample; |
964 |
dmv_x[1] <<= 1 - s->quarter_sample; |
965 |
dmv_y[1] <<= 1 - s->quarter_sample; |
966 |
|
967 |
wrap = s->b8_stride; |
968 |
xy = s->block_index[0];
|
969 |
|
970 |
if(s->mb_intra) {
|
971 |
s->current_picture.motion_val[0][xy][0] = |
972 |
s->current_picture.motion_val[0][xy][1] = |
973 |
s->current_picture.motion_val[1][xy][0] = |
974 |
s->current_picture.motion_val[1][xy][1] = 0; |
975 |
return;
|
976 |
} |
977 |
s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample); |
978 |
s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample); |
979 |
s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample); |
980 |
s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample); |
981 |
|
982 |
/* Pullback predicted motion vectors as specified in 8.4.5.4 */
|
983 |
s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6)); |
984 |
s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6)); |
985 |
s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6)); |
986 |
s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6)); |
987 |
if(direct) {
|
988 |
s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0]; |
989 |
s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1]; |
990 |
s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0]; |
991 |
s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1]; |
992 |
return;
|
993 |
} |
994 |
|
995 |
if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
|
996 |
C = s->current_picture.motion_val[0][xy - 2]; |
997 |
A = s->current_picture.motion_val[0][xy - wrap*2]; |
998 |
off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2; |
999 |
B = s->current_picture.motion_val[0][xy - wrap*2 + off]; |
1000 |
|
1001 |
if(!s->mb_x) C[0] = C[1] = 0; |
1002 |
if(!s->first_slice_line) { // predictor A is not out of bounds |
1003 |
if(s->mb_width == 1) { |
1004 |
px = A[0];
|
1005 |
py = A[1];
|
1006 |
} else {
|
1007 |
px = mid_pred(A[0], B[0], C[0]); |
1008 |
py = mid_pred(A[1], B[1], C[1]); |
1009 |
} |
1010 |
} else if(s->mb_x) { // predictor C is not out of bounds |
1011 |
px = C[0];
|
1012 |
py = C[1];
|
1013 |
} else {
|
1014 |
px = py = 0;
|
1015 |
} |
1016 |
/* Pullback MV as specified in 8.3.5.3.4 */
|
1017 |
{ |
1018 |
int qx, qy, X, Y;
|
1019 |
if(v->profile < PROFILE_ADVANCED) {
|
1020 |
qx = (s->mb_x << 5);
|
1021 |
qy = (s->mb_y << 5);
|
1022 |
X = (s->mb_width << 5) - 4; |
1023 |
Y = (s->mb_height << 5) - 4; |
1024 |
if(qx + px < -28) px = -28 - qx; |
1025 |
if(qy + py < -28) py = -28 - qy; |
1026 |
if(qx + px > X) px = X - qx;
|
1027 |
if(qy + py > Y) py = Y - qy;
|
1028 |
} else {
|
1029 |
qx = (s->mb_x << 6);
|
1030 |
qy = (s->mb_y << 6);
|
1031 |
X = (s->mb_width << 6) - 4; |
1032 |
Y = (s->mb_height << 6) - 4; |
1033 |
if(qx + px < -60) px = -60 - qx; |
1034 |
if(qy + py < -60) py = -60 - qy; |
1035 |
if(qx + px > X) px = X - qx;
|
1036 |
if(qy + py > Y) py = Y - qy;
|
1037 |
} |
1038 |
} |
1039 |
/* Calculate hybrid prediction as specified in 8.3.5.3.5 */
|
1040 |
if(0 && !s->first_slice_line && s->mb_x) { |
1041 |
if(is_intra[xy - wrap])
|
1042 |
sum = FFABS(px) + FFABS(py); |
1043 |
else
|
1044 |
sum = FFABS(px - A[0]) + FFABS(py - A[1]); |
1045 |
if(sum > 32) { |
1046 |
if(get_bits1(&s->gb)) {
|
1047 |
px = A[0];
|
1048 |
py = A[1];
|
1049 |
} else {
|
1050 |
px = C[0];
|
1051 |
py = C[1];
|
1052 |
} |
1053 |
} else {
|
1054 |
if(is_intra[xy - 2]) |
1055 |
sum = FFABS(px) + FFABS(py); |
1056 |
else
|
1057 |
sum = FFABS(px - C[0]) + FFABS(py - C[1]); |
1058 |
if(sum > 32) { |
1059 |
if(get_bits1(&s->gb)) {
|
1060 |
px = A[0];
|
1061 |
py = A[1];
|
1062 |
} else {
|
1063 |
px = C[0];
|
1064 |
py = C[1];
|
1065 |
} |
1066 |
} |
1067 |
} |
1068 |
} |
1069 |
/* store MV using signed modulus of MV range defined in 4.11 */
|
1070 |
s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x; |
1071 |
s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y; |
1072 |
} |
1073 |
if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
|
1074 |
C = s->current_picture.motion_val[1][xy - 2]; |
1075 |
A = s->current_picture.motion_val[1][xy - wrap*2]; |
1076 |
off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2; |
1077 |
B = s->current_picture.motion_val[1][xy - wrap*2 + off]; |
1078 |
|
1079 |
if(!s->mb_x) C[0] = C[1] = 0; |
1080 |
if(!s->first_slice_line) { // predictor A is not out of bounds |
1081 |
if(s->mb_width == 1) { |
1082 |
px = A[0];
|
1083 |
py = A[1];
|
1084 |
} else {
|
1085 |
px = mid_pred(A[0], B[0], C[0]); |
1086 |
py = mid_pred(A[1], B[1], C[1]); |
1087 |
} |
1088 |
} else if(s->mb_x) { // predictor C is not out of bounds |
1089 |
px = C[0];
|
1090 |
py = C[1];
|
1091 |
} else {
|
1092 |
px = py = 0;
|
1093 |
} |
1094 |
/* Pullback MV as specified in 8.3.5.3.4 */
|
1095 |
{ |
1096 |
int qx, qy, X, Y;
|
1097 |
if(v->profile < PROFILE_ADVANCED) {
|
1098 |
qx = (s->mb_x << 5);
|
1099 |
qy = (s->mb_y << 5);
|
1100 |
X = (s->mb_width << 5) - 4; |
1101 |
Y = (s->mb_height << 5) - 4; |
1102 |
if(qx + px < -28) px = -28 - qx; |
1103 |
if(qy + py < -28) py = -28 - qy; |
1104 |
if(qx + px > X) px = X - qx;
|
1105 |
if(qy + py > Y) py = Y - qy;
|
1106 |
} else {
|
1107 |
qx = (s->mb_x << 6);
|
1108 |
qy = (s->mb_y << 6);
|
1109 |
X = (s->mb_width << 6) - 4; |
1110 |
Y = (s->mb_height << 6) - 4; |
1111 |
if(qx + px < -60) px = -60 - qx; |
1112 |
if(qy + py < -60) py = -60 - qy; |
1113 |
if(qx + px > X) px = X - qx;
|
1114 |
if(qy + py > Y) py = Y - qy;
|
1115 |
} |
1116 |
} |
1117 |
/* Calculate hybrid prediction as specified in 8.3.5.3.5 */
|
1118 |
if(0 && !s->first_slice_line && s->mb_x) { |
1119 |
if(is_intra[xy - wrap])
|
1120 |
sum = FFABS(px) + FFABS(py); |
1121 |
else
|
1122 |
sum = FFABS(px - A[0]) + FFABS(py - A[1]); |
1123 |
if(sum > 32) { |
1124 |
if(get_bits1(&s->gb)) {
|
1125 |
px = A[0];
|
1126 |
py = A[1];
|
1127 |
} else {
|
1128 |
px = C[0];
|
1129 |
py = C[1];
|
1130 |
} |
1131 |
} else {
|
1132 |
if(is_intra[xy - 2]) |
1133 |
sum = FFABS(px) + FFABS(py); |
1134 |
else
|
1135 |
sum = FFABS(px - C[0]) + FFABS(py - C[1]); |
1136 |
if(sum > 32) { |
1137 |
if(get_bits1(&s->gb)) {
|
1138 |
px = A[0];
|
1139 |
py = A[1];
|
1140 |
} else {
|
1141 |
px = C[0];
|
1142 |
py = C[1];
|
1143 |
} |
1144 |
} |
1145 |
} |
1146 |
} |
1147 |
/* store MV using signed modulus of MV range defined in 4.11 */
|
1148 |
|
1149 |
s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x; |
1150 |
s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y; |
1151 |
} |
1152 |
s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0]; |
1153 |
s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1]; |
1154 |
s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0]; |
1155 |
s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1]; |
1156 |
} |
1157 |
|
1158 |
/** Get predicted DC value for I-frames only
|
1159 |
* prediction dir: left=0, top=1
|
1160 |
* @param s MpegEncContext
|
1161 |
* @param overlap flag indicating that overlap filtering is used
|
1162 |
* @param pq integer part of picture quantizer
|
1163 |
* @param[in] n block index in the current MB
|
1164 |
* @param dc_val_ptr Pointer to DC predictor
|
1165 |
* @param dir_ptr Prediction direction for use in AC prediction
|
1166 |
*/
|
1167 |
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n, |
1168 |
int16_t **dc_val_ptr, int *dir_ptr)
|
1169 |
{ |
1170 |
int a, b, c, wrap, pred, scale;
|
1171 |
int16_t *dc_val; |
1172 |
static const uint16_t dcpred[32] = { |
1173 |
-1, 1024, 512, 341, 256, 205, 171, 146, 128, |
1174 |
114, 102, 93, 85, 79, 73, 68, 64, |
1175 |
60, 57, 54, 51, 49, 47, 45, 43, |
1176 |
41, 39, 38, 37, 35, 34, 33 |
1177 |
}; |
1178 |
|
1179 |
/* find prediction - wmv3_dc_scale always used here in fact */
|
1180 |
if (n < 4) scale = s->y_dc_scale; |
1181 |
else scale = s->c_dc_scale;
|
1182 |
|
1183 |
wrap = s->block_wrap[n]; |
1184 |
dc_val= s->dc_val[0] + s->block_index[n];
|
1185 |
|
1186 |
/* B A
|
1187 |
* C X
|
1188 |
*/
|
1189 |
c = dc_val[ - 1];
|
1190 |
b = dc_val[ - 1 - wrap];
|
1191 |
a = dc_val[ - wrap]; |
1192 |
|
1193 |
if (pq < 9 || !overlap) |
1194 |
{ |
1195 |
/* Set outer values */
|
1196 |
if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale]; |
1197 |
if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale]; |
1198 |
} |
1199 |
else
|
1200 |
{ |
1201 |
/* Set outer values */
|
1202 |
if (s->first_slice_line && (n!=2 && n!=3)) b=a=0; |
1203 |
if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0; |
1204 |
} |
1205 |
|
1206 |
if (abs(a - b) <= abs(b - c)) {
|
1207 |
pred = c; |
1208 |
*dir_ptr = 1;//left |
1209 |
} else {
|
1210 |
pred = a; |
1211 |
*dir_ptr = 0;//top |
1212 |
} |
1213 |
|
1214 |
/* update predictor */
|
1215 |
*dc_val_ptr = &dc_val[0];
|
1216 |
return pred;
|
1217 |
} |
1218 |
|
1219 |
|
1220 |
/** Get predicted DC value
|
1221 |
* prediction dir: left=0, top=1
|
1222 |
* @param s MpegEncContext
|
1223 |
* @param overlap flag indicating that overlap filtering is used
|
1224 |
* @param pq integer part of picture quantizer
|
1225 |
* @param[in] n block index in the current MB
|
1226 |
* @param a_avail flag indicating top block availability
|
1227 |
* @param c_avail flag indicating left block availability
|
1228 |
* @param dc_val_ptr Pointer to DC predictor
|
1229 |
* @param dir_ptr Prediction direction for use in AC prediction
|
1230 |
*/
|
1231 |
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n, |
1232 |
int a_avail, int c_avail, |
1233 |
int16_t **dc_val_ptr, int *dir_ptr)
|
1234 |
{ |
1235 |
int a, b, c, wrap, pred;
|
1236 |
int16_t *dc_val; |
1237 |
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
|
1238 |
int q1, q2 = 0; |
1239 |
|
1240 |
wrap = s->block_wrap[n]; |
1241 |
dc_val= s->dc_val[0] + s->block_index[n];
|
1242 |
|
1243 |
/* B A
|
1244 |
* C X
|
1245 |
*/
|
1246 |
c = dc_val[ - 1];
|
1247 |
b = dc_val[ - 1 - wrap];
|
1248 |
a = dc_val[ - wrap]; |
1249 |
/* scale predictors if needed */
|
1250 |
q1 = s->current_picture.qscale_table[mb_pos]; |
1251 |
if(c_avail && (n!= 1 && n!=3)) { |
1252 |
q2 = s->current_picture.qscale_table[mb_pos - 1];
|
1253 |
if(q2 && q2 != q1)
|
1254 |
c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18; |
1255 |
} |
1256 |
if(a_avail && (n!= 2 && n!=3)) { |
1257 |
q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride]; |
1258 |
if(q2 && q2 != q1)
|
1259 |
a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18; |
1260 |
} |
1261 |
if(a_avail && c_avail && (n!=3)) { |
1262 |
int off = mb_pos;
|
1263 |
if(n != 1) off--; |
1264 |
if(n != 2) off -= s->mb_stride; |
1265 |
q2 = s->current_picture.qscale_table[off]; |
1266 |
if(q2 && q2 != q1)
|
1267 |
b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18; |
1268 |
} |
1269 |
|
1270 |
if(a_avail && c_avail) {
|
1271 |
if(abs(a - b) <= abs(b - c)) {
|
1272 |
pred = c; |
1273 |
*dir_ptr = 1;//left |
1274 |
} else {
|
1275 |
pred = a; |
1276 |
*dir_ptr = 0;//top |
1277 |
} |
1278 |
} else if(a_avail) { |
1279 |
pred = a; |
1280 |
*dir_ptr = 0;//top |
1281 |
} else if(c_avail) { |
1282 |
pred = c; |
1283 |
*dir_ptr = 1;//left |
1284 |
} else {
|
1285 |
pred = 0;
|
1286 |
*dir_ptr = 1;//left |
1287 |
} |
1288 |
|
1289 |
/* update predictor */
|
1290 |
*dc_val_ptr = &dc_val[0];
|
1291 |
return pred;
|
1292 |
} |
1293 |
|
1294 |
/** @} */ // Block group |
1295 |
|
1296 |
/**
|
1297 |
* @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
|
1298 |
* @see 7.1.4, p91 and 8.1.1.7, p(1)04
|
1299 |
* @{
|
1300 |
*/
|
1301 |
|
1302 |
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr) |
1303 |
{ |
1304 |
int xy, wrap, pred, a, b, c;
|
1305 |
|
1306 |
xy = s->block_index[n]; |
1307 |
wrap = s->b8_stride; |
1308 |
|
1309 |
/* B C
|
1310 |
* A X
|
1311 |
*/
|
1312 |
a = s->coded_block[xy - 1 ];
|
1313 |
b = s->coded_block[xy - 1 - wrap];
|
1314 |
c = s->coded_block[xy - wrap]; |
1315 |
|
1316 |
if (b == c) {
|
1317 |
pred = a; |
1318 |
} else {
|
1319 |
pred = c; |
1320 |
} |
1321 |
|
1322 |
/* store value */
|
1323 |
*coded_block_ptr = &s->coded_block[xy]; |
1324 |
|
1325 |
return pred;
|
1326 |
} |
1327 |
|
1328 |
/**
|
1329 |
* Decode one AC coefficient
|
1330 |
* @param v The VC1 context
|
1331 |
* @param last Last coefficient
|
1332 |
* @param skip How much zero coefficients to skip
|
1333 |
* @param value Decoded AC coefficient value
|
1334 |
* @param codingset set of VLC to decode data
|
1335 |
* @see 8.1.3.4
|
1336 |
*/
|
1337 |
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset) |
1338 |
{ |
1339 |
GetBitContext *gb = &v->s.gb; |
1340 |
int index, escape, run = 0, level = 0, lst = 0; |
1341 |
|
1342 |
index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
|
1343 |
if (index != vc1_ac_sizes[codingset] - 1) { |
1344 |
run = vc1_index_decode_table[codingset][index][0];
|
1345 |
level = vc1_index_decode_table[codingset][index][1];
|
1346 |
lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
|
1347 |
if(get_bits1(gb))
|
1348 |
level = -level; |
1349 |
} else {
|
1350 |
escape = decode210(gb); |
1351 |
if (escape != 2) { |
1352 |
index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
|
1353 |
run = vc1_index_decode_table[codingset][index][0];
|
1354 |
level = vc1_index_decode_table[codingset][index][1];
|
1355 |
lst = index >= vc1_last_decode_table[codingset]; |
1356 |
if(escape == 0) { |
1357 |
if(lst)
|
1358 |
level += vc1_last_delta_level_table[codingset][run]; |
1359 |
else
|
1360 |
level += vc1_delta_level_table[codingset][run]; |
1361 |
} else {
|
1362 |
if(lst)
|
1363 |
run += vc1_last_delta_run_table[codingset][level] + 1;
|
1364 |
else
|
1365 |
run += vc1_delta_run_table[codingset][level] + 1;
|
1366 |
} |
1367 |
if(get_bits1(gb))
|
1368 |
level = -level; |
1369 |
} else {
|
1370 |
int sign;
|
1371 |
lst = get_bits1(gb); |
1372 |
if(v->s.esc3_level_length == 0) { |
1373 |
if(v->pq < 8 || v->dquantfrm) { // table 59 |
1374 |
v->s.esc3_level_length = get_bits(gb, 3);
|
1375 |
if(!v->s.esc3_level_length)
|
1376 |
v->s.esc3_level_length = get_bits(gb, 2) + 8; |
1377 |
} else { //table 60 |
1378 |
v->s.esc3_level_length = get_unary(gb, 1, 6) + 2; |
1379 |
} |
1380 |
v->s.esc3_run_length = 3 + get_bits(gb, 2); |
1381 |
} |
1382 |
run = get_bits(gb, v->s.esc3_run_length); |
1383 |
sign = get_bits1(gb); |
1384 |
level = get_bits(gb, v->s.esc3_level_length); |
1385 |
if(sign)
|
1386 |
level = -level; |
1387 |
} |
1388 |
} |
1389 |
|
1390 |
*last = lst; |
1391 |
*skip = run; |
1392 |
*value = level; |
1393 |
} |
1394 |
|
1395 |
/** Decode intra block in intra frames - should be faster than decode_intra_block
|
1396 |
* @param v VC1Context
|
1397 |
* @param block block to decode
|
1398 |
* @param[in] n subblock index
|
1399 |
* @param coded are AC coeffs present or not
|
1400 |
* @param codingset set of VLC to decode data
|
1401 |
*/
|
1402 |
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset) |
1403 |
{ |
1404 |
GetBitContext *gb = &v->s.gb; |
1405 |
MpegEncContext *s = &v->s; |
1406 |
int dc_pred_dir = 0; /* Direction of the DC prediction used */ |
1407 |
int i;
|
1408 |
int16_t *dc_val; |
1409 |
int16_t *ac_val, *ac_val2; |
1410 |
int dcdiff;
|
1411 |
|
1412 |
/* Get DC differential */
|
1413 |
if (n < 4) { |
1414 |
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
1415 |
} else {
|
1416 |
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
1417 |
} |
1418 |
if (dcdiff < 0){ |
1419 |
av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
|
1420 |
return -1; |
1421 |
} |
1422 |
if (dcdiff)
|
1423 |
{ |
1424 |
if (dcdiff == 119 /* ESC index value */) |
1425 |
{ |
1426 |
/* TODO: Optimize */
|
1427 |
if (v->pq == 1) dcdiff = get_bits(gb, 10); |
1428 |
else if (v->pq == 2) dcdiff = get_bits(gb, 9); |
1429 |
else dcdiff = get_bits(gb, 8); |
1430 |
} |
1431 |
else
|
1432 |
{ |
1433 |
if (v->pq == 1) |
1434 |
dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3; |
1435 |
else if (v->pq == 2) |
1436 |
dcdiff = (dcdiff<<1) + get_bits1(gb) - 1; |
1437 |
} |
1438 |
if (get_bits1(gb))
|
1439 |
dcdiff = -dcdiff; |
1440 |
} |
1441 |
|
1442 |
/* Prediction */
|
1443 |
dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir); |
1444 |
*dc_val = dcdiff; |
1445 |
|
1446 |
/* Store the quantized DC coeff, used for prediction */
|
1447 |
if (n < 4) { |
1448 |
block[0] = dcdiff * s->y_dc_scale;
|
1449 |
} else {
|
1450 |
block[0] = dcdiff * s->c_dc_scale;
|
1451 |
} |
1452 |
/* Skip ? */
|
1453 |
if (!coded) {
|
1454 |
goto not_coded;
|
1455 |
} |
1456 |
|
1457 |
//AC Decoding
|
1458 |
i = 1;
|
1459 |
|
1460 |
{ |
1461 |
int last = 0, skip, value; |
1462 |
const uint8_t *zz_table;
|
1463 |
int scale;
|
1464 |
int k;
|
1465 |
|
1466 |
scale = v->pq * 2 + v->halfpq;
|
1467 |
|
1468 |
if(v->s.ac_pred) {
|
1469 |
if(!dc_pred_dir)
|
1470 |
zz_table = v->zz_8x8[2];
|
1471 |
else
|
1472 |
zz_table = v->zz_8x8[3];
|
1473 |
} else
|
1474 |
zz_table = v->zz_8x8[1];
|
1475 |
|
1476 |
ac_val = s->ac_val[0][0] + s->block_index[n] * 16; |
1477 |
ac_val2 = ac_val; |
1478 |
if(dc_pred_dir) //left |
1479 |
ac_val -= 16;
|
1480 |
else //top |
1481 |
ac_val -= 16 * s->block_wrap[n];
|
1482 |
|
1483 |
while (!last) {
|
1484 |
vc1_decode_ac_coeff(v, &last, &skip, &value, codingset); |
1485 |
i += skip; |
1486 |
if(i > 63) |
1487 |
break;
|
1488 |
block[zz_table[i++]] = value; |
1489 |
} |
1490 |
|
1491 |
/* apply AC prediction if needed */
|
1492 |
if(s->ac_pred) {
|
1493 |
if(dc_pred_dir) { //left |
1494 |
for(k = 1; k < 8; k++) |
1495 |
block[k] += ac_val[k]; |
1496 |
} else { //top |
1497 |
for(k = 1; k < 8; k++) |
1498 |
block[k << 3] += ac_val[k + 8]; |
1499 |
} |
1500 |
} |
1501 |
/* save AC coeffs for further prediction */
|
1502 |
for(k = 1; k < 8; k++) { |
1503 |
ac_val2[k] = block[k]; |
1504 |
ac_val2[k + 8] = block[k << 3]; |
1505 |
} |
1506 |
|
1507 |
/* scale AC coeffs */
|
1508 |
for(k = 1; k < 64; k++) |
1509 |
if(block[k]) {
|
1510 |
block[k] *= scale; |
1511 |
if(!v->pquantizer)
|
1512 |
block[k] += (block[k] < 0) ? -v->pq : v->pq;
|
1513 |
} |
1514 |
|
1515 |
if(s->ac_pred) i = 63; |
1516 |
} |
1517 |
|
1518 |
not_coded:
|
1519 |
if(!coded) {
|
1520 |
int k, scale;
|
1521 |
ac_val = s->ac_val[0][0] + s->block_index[n] * 16; |
1522 |
ac_val2 = ac_val; |
1523 |
|
1524 |
i = 0;
|
1525 |
scale = v->pq * 2 + v->halfpq;
|
1526 |
memset(ac_val2, 0, 16 * 2); |
1527 |
if(dc_pred_dir) {//left |
1528 |
ac_val -= 16;
|
1529 |
if(s->ac_pred)
|
1530 |
memcpy(ac_val2, ac_val, 8 * 2); |
1531 |
} else {//top |
1532 |
ac_val -= 16 * s->block_wrap[n];
|
1533 |
if(s->ac_pred)
|
1534 |
memcpy(ac_val2 + 8, ac_val + 8, 8 * 2); |
1535 |
} |
1536 |
|
1537 |
/* apply AC prediction if needed */
|
1538 |
if(s->ac_pred) {
|
1539 |
if(dc_pred_dir) { //left |
1540 |
for(k = 1; k < 8; k++) { |
1541 |
block[k] = ac_val[k] * scale; |
1542 |
if(!v->pquantizer && block[k])
|
1543 |
block[k] += (block[k] < 0) ? -v->pq : v->pq;
|
1544 |
} |
1545 |
} else { //top |
1546 |
for(k = 1; k < 8; k++) { |
1547 |
block[k << 3] = ac_val[k + 8] * scale; |
1548 |
if(!v->pquantizer && block[k << 3]) |
1549 |
block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq; |
1550 |
} |
1551 |
} |
1552 |
i = 63;
|
1553 |
} |
1554 |
} |
1555 |
s->block_last_index[n] = i; |
1556 |
|
1557 |
return 0; |
1558 |
} |
1559 |
|
1560 |
/** Decode intra block in intra frames - should be faster than decode_intra_block
|
1561 |
* @param v VC1Context
|
1562 |
* @param block block to decode
|
1563 |
* @param[in] n subblock number
|
1564 |
* @param coded are AC coeffs present or not
|
1565 |
* @param codingset set of VLC to decode data
|
1566 |
* @param mquant quantizer value for this macroblock
|
1567 |
*/
|
1568 |
static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant) |
1569 |
{ |
1570 |
GetBitContext *gb = &v->s.gb; |
1571 |
MpegEncContext *s = &v->s; |
1572 |
int dc_pred_dir = 0; /* Direction of the DC prediction used */ |
1573 |
int i;
|
1574 |
int16_t *dc_val; |
1575 |
int16_t *ac_val, *ac_val2; |
1576 |
int dcdiff;
|
1577 |
int a_avail = v->a_avail, c_avail = v->c_avail;
|
1578 |
int use_pred = s->ac_pred;
|
1579 |
int scale;
|
1580 |
int q1, q2 = 0; |
1581 |
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
|
1582 |
|
1583 |
/* Get DC differential */
|
1584 |
if (n < 4) { |
1585 |
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
1586 |
} else {
|
1587 |
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
1588 |
} |
1589 |
if (dcdiff < 0){ |
1590 |
av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
|
1591 |
return -1; |
1592 |
} |
1593 |
if (dcdiff)
|
1594 |
{ |
1595 |
if (dcdiff == 119 /* ESC index value */) |
1596 |
{ |
1597 |
/* TODO: Optimize */
|
1598 |
if (mquant == 1) dcdiff = get_bits(gb, 10); |
1599 |
else if (mquant == 2) dcdiff = get_bits(gb, 9); |
1600 |
else dcdiff = get_bits(gb, 8); |
1601 |
} |
1602 |
else
|
1603 |
{ |
1604 |
if (mquant == 1) |
1605 |
dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3; |
1606 |
else if (mquant == 2) |
1607 |
dcdiff = (dcdiff<<1) + get_bits1(gb) - 1; |
1608 |
} |
1609 |
if (get_bits1(gb))
|
1610 |
dcdiff = -dcdiff; |
1611 |
} |
1612 |
|
1613 |
/* Prediction */
|
1614 |
dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir); |
1615 |
*dc_val = dcdiff; |
1616 |
|
1617 |
/* Store the quantized DC coeff, used for prediction */
|
1618 |
if (n < 4) { |
1619 |
block[0] = dcdiff * s->y_dc_scale;
|
1620 |
} else {
|
1621 |
block[0] = dcdiff * s->c_dc_scale;
|
1622 |
} |
1623 |
|
1624 |
//AC Decoding
|
1625 |
i = 1;
|
1626 |
|
1627 |
/* check if AC is needed at all */
|
1628 |
if(!a_avail && !c_avail) use_pred = 0; |
1629 |
ac_val = s->ac_val[0][0] + s->block_index[n] * 16; |
1630 |
ac_val2 = ac_val; |
1631 |
|
1632 |
scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0); |
1633 |
|
1634 |
if(dc_pred_dir) //left |
1635 |
ac_val -= 16;
|
1636 |
else //top |
1637 |
ac_val -= 16 * s->block_wrap[n];
|
1638 |
|
1639 |
q1 = s->current_picture.qscale_table[mb_pos]; |
1640 |
if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1]; |
1641 |
if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
|
1642 |
if(dc_pred_dir && n==1) q2 = q1; |
1643 |
if(!dc_pred_dir && n==2) q2 = q1; |
1644 |
if(n==3) q2 = q1; |
1645 |
|
1646 |
if(coded) {
|
1647 |
int last = 0, skip, value; |
1648 |
const uint8_t *zz_table;
|
1649 |
int k;
|
1650 |
|
1651 |
if(v->s.ac_pred) {
|
1652 |
if(!dc_pred_dir)
|
1653 |
zz_table = v->zz_8x8[2];
|
1654 |
else
|
1655 |
zz_table = v->zz_8x8[3];
|
1656 |
} else
|
1657 |
zz_table = v->zz_8x8[1];
|
1658 |
|
1659 |
while (!last) {
|
1660 |
vc1_decode_ac_coeff(v, &last, &skip, &value, codingset); |
1661 |
i += skip; |
1662 |
if(i > 63) |
1663 |
break;
|
1664 |
block[zz_table[i++]] = value; |
1665 |
} |
1666 |
|
1667 |
/* apply AC prediction if needed */
|
1668 |
if(use_pred) {
|
1669 |
/* scale predictors if needed*/
|
1670 |
if(q2 && q1!=q2) {
|
1671 |
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1; |
1672 |
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1; |
1673 |
|
1674 |
if(dc_pred_dir) { //left |
1675 |
for(k = 1; k < 8; k++) |
1676 |
block[k] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18; |
1677 |
} else { //top |
1678 |
for(k = 1; k < 8; k++) |
1679 |
block[k << 3] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18; |
1680 |
} |
1681 |
} else {
|
1682 |
if(dc_pred_dir) { //left |
1683 |
for(k = 1; k < 8; k++) |
1684 |
block[k] += ac_val[k]; |
1685 |
} else { //top |
1686 |
for(k = 1; k < 8; k++) |
1687 |
block[k << 3] += ac_val[k + 8]; |
1688 |
} |
1689 |
} |
1690 |
} |
1691 |
/* save AC coeffs for further prediction */
|
1692 |
for(k = 1; k < 8; k++) { |
1693 |
ac_val2[k] = block[k]; |
1694 |
ac_val2[k + 8] = block[k << 3]; |
1695 |
} |
1696 |
|
1697 |
/* scale AC coeffs */
|
1698 |
for(k = 1; k < 64; k++) |
1699 |
if(block[k]) {
|
1700 |
block[k] *= scale; |
1701 |
if(!v->pquantizer)
|
1702 |
block[k] += (block[k] < 0) ? -mquant : mquant;
|
1703 |
} |
1704 |
|
1705 |
if(use_pred) i = 63; |
1706 |
} else { // no AC coeffs |
1707 |
int k;
|
1708 |
|
1709 |
memset(ac_val2, 0, 16 * 2); |
1710 |
if(dc_pred_dir) {//left |
1711 |
if(use_pred) {
|
1712 |
memcpy(ac_val2, ac_val, 8 * 2); |
1713 |
if(q2 && q1!=q2) {
|
1714 |
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1; |
1715 |
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1; |
1716 |
for(k = 1; k < 8; k++) |
1717 |
ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18; |
1718 |
} |
1719 |
} |
1720 |
} else {//top |
1721 |
if(use_pred) {
|
1722 |
memcpy(ac_val2 + 8, ac_val + 8, 8 * 2); |
1723 |
if(q2 && q1!=q2) {
|
1724 |
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1; |
1725 |
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1; |
1726 |
for(k = 1; k < 8; k++) |
1727 |
ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18; |
1728 |
} |
1729 |
} |
1730 |
} |
1731 |
|
1732 |
/* apply AC prediction if needed */
|
1733 |
if(use_pred) {
|
1734 |
if(dc_pred_dir) { //left |
1735 |
for(k = 1; k < 8; k++) { |
1736 |
block[k] = ac_val2[k] * scale; |
1737 |
if(!v->pquantizer && block[k])
|
1738 |
block[k] += (block[k] < 0) ? -mquant : mquant;
|
1739 |
} |
1740 |
} else { //top |
1741 |
for(k = 1; k < 8; k++) { |
1742 |
block[k << 3] = ac_val2[k + 8] * scale; |
1743 |
if(!v->pquantizer && block[k << 3]) |
1744 |
block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant; |
1745 |
} |
1746 |
} |
1747 |
i = 63;
|
1748 |
} |
1749 |
} |
1750 |
s->block_last_index[n] = i; |
1751 |
|
1752 |
return 0; |
1753 |
} |
1754 |
|
1755 |
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
|
1756 |
* @param v VC1Context
|
1757 |
* @param block block to decode
|
1758 |
* @param[in] n subblock index
|
1759 |
* @param coded are AC coeffs present or not
|
1760 |
* @param mquant block quantizer
|
1761 |
* @param codingset set of VLC to decode data
|
1762 |
*/
|
1763 |
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset) |
1764 |
{ |
1765 |
GetBitContext *gb = &v->s.gb; |
1766 |
MpegEncContext *s = &v->s; |
1767 |
int dc_pred_dir = 0; /* Direction of the DC prediction used */ |
1768 |
int i;
|
1769 |
int16_t *dc_val; |
1770 |
int16_t *ac_val, *ac_val2; |
1771 |
int dcdiff;
|
1772 |
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
|
1773 |
int a_avail = v->a_avail, c_avail = v->c_avail;
|
1774 |
int use_pred = s->ac_pred;
|
1775 |
int scale;
|
1776 |
int q1, q2 = 0; |
1777 |
|
1778 |
s->dsp.clear_block(block); |
1779 |
|
1780 |
/* XXX: Guard against dumb values of mquant */
|
1781 |
mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant ); |
1782 |
|
1783 |
/* Set DC scale - y and c use the same */
|
1784 |
s->y_dc_scale = s->y_dc_scale_table[mquant]; |
1785 |
s->c_dc_scale = s->c_dc_scale_table[mquant]; |
1786 |
|
1787 |
/* Get DC differential */
|
1788 |
if (n < 4) { |
1789 |
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
1790 |
} else {
|
1791 |
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
1792 |
} |
1793 |
if (dcdiff < 0){ |
1794 |
av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
|
1795 |
return -1; |
1796 |
} |
1797 |
if (dcdiff)
|
1798 |
{ |
1799 |
if (dcdiff == 119 /* ESC index value */) |
1800 |
{ |
1801 |
/* TODO: Optimize */
|
1802 |
if (mquant == 1) dcdiff = get_bits(gb, 10); |
1803 |
else if (mquant == 2) dcdiff = get_bits(gb, 9); |
1804 |
else dcdiff = get_bits(gb, 8); |
1805 |
} |
1806 |
else
|
1807 |
{ |
1808 |
if (mquant == 1) |
1809 |
dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3; |
1810 |
else if (mquant == 2) |
1811 |
dcdiff = (dcdiff<<1) + get_bits1(gb) - 1; |
1812 |
} |
1813 |
if (get_bits1(gb))
|
1814 |
dcdiff = -dcdiff; |
1815 |
} |
1816 |
|
1817 |
/* Prediction */
|
1818 |
dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir); |
1819 |
*dc_val = dcdiff; |
1820 |
|
1821 |
/* Store the quantized DC coeff, used for prediction */
|
1822 |
|
1823 |
if (n < 4) { |
1824 |
block[0] = dcdiff * s->y_dc_scale;
|
1825 |
} else {
|
1826 |
block[0] = dcdiff * s->c_dc_scale;
|
1827 |
} |
1828 |
|
1829 |
//AC Decoding
|
1830 |
i = 1;
|
1831 |
|
1832 |
/* check if AC is needed at all and adjust direction if needed */
|
1833 |
if(!a_avail) dc_pred_dir = 1; |
1834 |
if(!c_avail) dc_pred_dir = 0; |
1835 |
if(!a_avail && !c_avail) use_pred = 0; |
1836 |
ac_val = s->ac_val[0][0] + s->block_index[n] * 16; |
1837 |
ac_val2 = ac_val; |
1838 |
|
1839 |
scale = mquant * 2 + v->halfpq;
|
1840 |
|
1841 |
if(dc_pred_dir) //left |
1842 |
ac_val -= 16;
|
1843 |
else //top |
1844 |
ac_val -= 16 * s->block_wrap[n];
|
1845 |
|
1846 |
q1 = s->current_picture.qscale_table[mb_pos]; |
1847 |
if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1]; |
1848 |
if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
|
1849 |
if(dc_pred_dir && n==1) q2 = q1; |
1850 |
if(!dc_pred_dir && n==2) q2 = q1; |
1851 |
if(n==3) q2 = q1; |
1852 |
|
1853 |
if(coded) {
|
1854 |
int last = 0, skip, value; |
1855 |
int k;
|
1856 |
|
1857 |
while (!last) {
|
1858 |
vc1_decode_ac_coeff(v, &last, &skip, &value, codingset); |
1859 |
i += skip; |
1860 |
if(i > 63) |
1861 |
break;
|
1862 |
block[v->zz_8x8[0][i++]] = value;
|
1863 |
} |
1864 |
|
1865 |
/* apply AC prediction if needed */
|
1866 |
if(use_pred) {
|
1867 |
/* scale predictors if needed*/
|
1868 |
if(q2 && q1!=q2) {
|
1869 |
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1; |
1870 |
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1; |
1871 |
|
1872 |
if(dc_pred_dir) { //left |
1873 |
for(k = 1; k < 8; k++) |
1874 |
block[k] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18; |
1875 |
} else { //top |
1876 |
for(k = 1; k < 8; k++) |
1877 |
block[k << 3] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18; |
1878 |
} |
1879 |
} else {
|
1880 |
if(dc_pred_dir) { //left |
1881 |
for(k = 1; k < 8; k++) |
1882 |
block[k] += ac_val[k]; |
1883 |
} else { //top |
1884 |
for(k = 1; k < 8; k++) |
1885 |
block[k << 3] += ac_val[k + 8]; |
1886 |
} |
1887 |
} |
1888 |
} |
1889 |
/* save AC coeffs for further prediction */
|
1890 |
for(k = 1; k < 8; k++) { |
1891 |
ac_val2[k] = block[k]; |
1892 |
ac_val2[k + 8] = block[k << 3]; |
1893 |
} |
1894 |
|
1895 |
/* scale AC coeffs */
|
1896 |
for(k = 1; k < 64; k++) |
1897 |
if(block[k]) {
|
1898 |
block[k] *= scale; |
1899 |
if(!v->pquantizer)
|
1900 |
block[k] += (block[k] < 0) ? -mquant : mquant;
|
1901 |
} |
1902 |
|
1903 |
if(use_pred) i = 63; |
1904 |
} else { // no AC coeffs |
1905 |
int k;
|
1906 |
|
1907 |
memset(ac_val2, 0, 16 * 2); |
1908 |
if(dc_pred_dir) {//left |
1909 |
if(use_pred) {
|
1910 |
memcpy(ac_val2, ac_val, 8 * 2); |
1911 |
if(q2 && q1!=q2) {
|
1912 |
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1; |
1913 |
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1; |
1914 |
for(k = 1; k < 8; k++) |
1915 |
ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18; |
1916 |
} |
1917 |
} |
1918 |
} else {//top |
1919 |
if(use_pred) {
|
1920 |
memcpy(ac_val2 + 8, ac_val + 8, 8 * 2); |
1921 |
if(q2 && q1!=q2) {
|
1922 |
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1; |
1923 |
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1; |
1924 |
for(k = 1; k < 8; k++) |
1925 |
ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18; |
1926 |
} |
1927 |
} |
1928 |
} |
1929 |
|
1930 |
/* apply AC prediction if needed */
|
1931 |
if(use_pred) {
|
1932 |
if(dc_pred_dir) { //left |
1933 |
for(k = 1; k < 8; k++) { |
1934 |
block[k] = ac_val2[k] * scale; |
1935 |
if(!v->pquantizer && block[k])
|
1936 |
block[k] += (block[k] < 0) ? -mquant : mquant;
|
1937 |
} |
1938 |
} else { //top |
1939 |
for(k = 1; k < 8; k++) { |
1940 |
block[k << 3] = ac_val2[k + 8] * scale; |
1941 |
if(!v->pquantizer && block[k << 3]) |
1942 |
block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant; |
1943 |
} |
1944 |
} |
1945 |
i = 63;
|
1946 |
} |
1947 |
} |
1948 |
s->block_last_index[n] = i; |
1949 |
|
1950 |
return 0; |
1951 |
} |
1952 |
|
1953 |
/** Decode P block
|
1954 |
*/
|
1955 |
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block, |
1956 |
uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left) |
1957 |
{ |
1958 |
MpegEncContext *s = &v->s; |
1959 |
GetBitContext *gb = &s->gb; |
1960 |
int i, j;
|
1961 |
int subblkpat = 0; |
1962 |
int scale, off, idx, last, skip, value;
|
1963 |
int ttblk = ttmb & 7; |
1964 |
int pat = 0; |
1965 |
|
1966 |
s->dsp.clear_block(block); |
1967 |
|
1968 |
if(ttmb == -1) { |
1969 |
ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
|
1970 |
} |
1971 |
if(ttblk == TT_4X4) {
|
1972 |
subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1); |
1973 |
} |
1974 |
if((ttblk != TT_8X8 && ttblk != TT_4X4)
|
1975 |
&& ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block)) |
1976 |
|| (!v->res_rtm_flag && !first_block))) { |
1977 |
subblkpat = decode012(gb); |
1978 |
if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits |
1979 |
if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
|
1980 |
if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
|
1981 |
} |
1982 |
scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0); |
1983 |
|
1984 |
// convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
|
1985 |
if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
|
1986 |
subblkpat = 2 - (ttblk == TT_8X4_TOP);
|
1987 |
ttblk = TT_8X4; |
1988 |
} |
1989 |
if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
|
1990 |
subblkpat = 2 - (ttblk == TT_4X8_LEFT);
|
1991 |
ttblk = TT_4X8; |
1992 |
} |
1993 |
switch(ttblk) {
|
1994 |
case TT_8X8:
|
1995 |
pat = 0xF;
|
1996 |
i = 0;
|
1997 |
last = 0;
|
1998 |
while (!last) {
|
1999 |
vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2); |
2000 |
i += skip; |
2001 |
if(i > 63) |
2002 |
break;
|
2003 |
idx = v->zz_8x8[0][i++];
|
2004 |
block[idx] = value * scale; |
2005 |
if(!v->pquantizer)
|
2006 |
block[idx] += (block[idx] < 0) ? -mquant : mquant;
|
2007 |
} |
2008 |
if(!skip_block){
|
2009 |
if(i==1) |
2010 |
v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block); |
2011 |
else{
|
2012 |
v->vc1dsp.vc1_inv_trans_8x8_add(dst, linesize, block); |
2013 |
} |
2014 |
if(apply_filter && cbp_top & 0xC) |
2015 |
v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq); |
2016 |
if(apply_filter && cbp_left & 0xA) |
2017 |
v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq); |
2018 |
} |
2019 |
break;
|
2020 |
case TT_4X4:
|
2021 |
pat = ~subblkpat & 0xF;
|
2022 |
for(j = 0; j < 4; j++) { |
2023 |
last = subblkpat & (1 << (3 - j)); |
2024 |
i = 0;
|
2025 |
off = (j & 1) * 4 + (j & 2) * 16; |
2026 |
while (!last) {
|
2027 |
vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2); |
2028 |
i += skip; |
2029 |
if(i > 15) |
2030 |
break;
|
2031 |
idx = ff_vc1_simple_progressive_4x4_zz[i++]; |
2032 |
block[idx + off] = value * scale; |
2033 |
if(!v->pquantizer)
|
2034 |
block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
|
2035 |
} |
2036 |
if(!(subblkpat & (1 << (3 - j))) && !skip_block){ |
2037 |
if(i==1) |
2038 |
v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off); |
2039 |
else
|
2040 |
v->vc1dsp.vc1_inv_trans_4x4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off); |
2041 |
if(apply_filter && (j&2 ? pat & (1<<(j-2)) : (cbp_top & (1 << (j + 2))))) |
2042 |
v->vc1dsp.vc1_v_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq); |
2043 |
if(apply_filter && (j&1 ? pat & (1<<(j-1)) : (cbp_left & (1 << (j + 1))))) |
2044 |
v->vc1dsp.vc1_h_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq); |
2045 |
} |
2046 |
} |
2047 |
break;
|
2048 |
case TT_8X4:
|
2049 |
pat = ~((subblkpat & 2)*6 + (subblkpat & 1)*3) & 0xF; |
2050 |
for(j = 0; j < 2; j++) { |
2051 |
last = subblkpat & (1 << (1 - j)); |
2052 |
i = 0;
|
2053 |
off = j * 32;
|
2054 |
while (!last) {
|
2055 |
vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2); |
2056 |
i += skip; |
2057 |
if(i > 31) |
2058 |
break;
|
2059 |
idx = v->zz_8x4[i++]+off; |
2060 |
block[idx] = value * scale; |
2061 |
if(!v->pquantizer)
|
2062 |
block[idx] += (block[idx] < 0) ? -mquant : mquant;
|
2063 |
} |
2064 |
if(!(subblkpat & (1 << (1 - j))) && !skip_block){ |
2065 |
if(i==1) |
2066 |
v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j*4*linesize, linesize, block + off);
|
2067 |
else
|
2068 |
v->vc1dsp.vc1_inv_trans_8x4(dst + j*4*linesize, linesize, block + off);
|
2069 |
if(apply_filter && j ? pat & 0x3 : (cbp_top & 0xC)) |
2070 |
v->vc1dsp.vc1_v_loop_filter8(dst + j*4*linesize, linesize, v->pq);
|
2071 |
if(apply_filter && cbp_left & (2 << j)) |
2072 |
v->vc1dsp.vc1_h_loop_filter4(dst + j*4*linesize, linesize, v->pq);
|
2073 |
} |
2074 |
} |
2075 |
break;
|
2076 |
case TT_4X8:
|
2077 |
pat = ~(subblkpat*5) & 0xF; |
2078 |
for(j = 0; j < 2; j++) { |
2079 |
last = subblkpat & (1 << (1 - j)); |
2080 |
i = 0;
|
2081 |
off = j * 4;
|
2082 |
while (!last) {
|
2083 |
vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2); |
2084 |
i += skip; |
2085 |
if(i > 31) |
2086 |
break;
|
2087 |
idx = v->zz_4x8[i++]+off; |
2088 |
block[idx] = value * scale; |
2089 |
if(!v->pquantizer)
|
2090 |
block[idx] += (block[idx] < 0) ? -mquant : mquant;
|
2091 |
} |
2092 |
if(!(subblkpat & (1 << (1 - j))) && !skip_block){ |
2093 |
if(i==1) |
2094 |
v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j*4, linesize, block + off);
|
2095 |
else
|
2096 |
v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
|
2097 |
if(apply_filter && cbp_top & (2 << j)) |
2098 |
v->vc1dsp.vc1_v_loop_filter4(dst + j*4, linesize, v->pq);
|
2099 |
if(apply_filter && j ? pat & 0x5 : (cbp_left & 0xA)) |
2100 |
v->vc1dsp.vc1_h_loop_filter8(dst + j*4, linesize, v->pq);
|
2101 |
} |
2102 |
} |
2103 |
break;
|
2104 |
} |
2105 |
return pat;
|
2106 |
} |
2107 |
|
2108 |
/** @} */ // Macroblock group |
2109 |
|
2110 |
static const int size_table [6] = { 0, 2, 3, 4, 5, 8 }; |
2111 |
static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 }; |
2112 |
|
2113 |
/** Decode one P-frame MB (in Simple/Main profile)
|
2114 |
*/
|
2115 |
static int vc1_decode_p_mb(VC1Context *v) |
2116 |
{ |
2117 |
MpegEncContext *s = &v->s; |
2118 |
GetBitContext *gb = &s->gb; |
2119 |
int i;
|
2120 |
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
|
2121 |
int cbp; /* cbp decoding stuff */ |
2122 |
int mqdiff, mquant; /* MB quantization */ |
2123 |
int ttmb = v->ttfrm; /* MB Transform type */ |
2124 |
|
2125 |
int mb_has_coeffs = 1; /* last_flag */ |
2126 |
int dmv_x, dmv_y; /* Differential MV components */ |
2127 |
int index, index1; /* LUT indexes */ |
2128 |
int val, sign; /* temp values */ |
2129 |
int first_block = 1; |
2130 |
int dst_idx, off;
|
2131 |
int skipped, fourmv;
|
2132 |
int block_cbp = 0, pat; |
2133 |
int apply_loop_filter;
|
2134 |
|
2135 |
mquant = v->pq; /* Loosy initialization */
|
2136 |
|
2137 |
if (v->mv_type_is_raw)
|
2138 |
fourmv = get_bits1(gb); |
2139 |
else
|
2140 |
fourmv = v->mv_type_mb_plane[mb_pos]; |
2141 |
if (v->skip_is_raw)
|
2142 |
skipped = get_bits1(gb); |
2143 |
else
|
2144 |
skipped = v->s.mbskip_table[mb_pos]; |
2145 |
|
2146 |
apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY); |
2147 |
if (!fourmv) /* 1MV mode */ |
2148 |
{ |
2149 |
if (!skipped)
|
2150 |
{ |
2151 |
vc1_idct_func idct8x8_fn; |
2152 |
|
2153 |
GET_MVDATA(dmv_x, dmv_y); |
2154 |
|
2155 |
if (s->mb_intra) {
|
2156 |
s->current_picture.motion_val[1][s->block_index[0]][0] = 0; |
2157 |
s->current_picture.motion_val[1][s->block_index[0]][1] = 0; |
2158 |
} |
2159 |
s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16; |
2160 |
vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]); |
2161 |
|
2162 |
/* FIXME Set DC val for inter block ? */
|
2163 |
if (s->mb_intra && !mb_has_coeffs)
|
2164 |
{ |
2165 |
GET_MQUANT(); |
2166 |
s->ac_pred = get_bits1(gb); |
2167 |
cbp = 0;
|
2168 |
} |
2169 |
else if (mb_has_coeffs) |
2170 |
{ |
2171 |
if (s->mb_intra) s->ac_pred = get_bits1(gb);
|
2172 |
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
|
2173 |
GET_MQUANT(); |
2174 |
} |
2175 |
else
|
2176 |
{ |
2177 |
mquant = v->pq; |
2178 |
cbp = 0;
|
2179 |
} |
2180 |
s->current_picture.qscale_table[mb_pos] = mquant; |
2181 |
|
2182 |
if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
|
2183 |
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, |
2184 |
VC1_TTMB_VLC_BITS, 2);
|
2185 |
if(!s->mb_intra) vc1_mc_1mv(v, 0); |
2186 |
dst_idx = 0;
|
2187 |
idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[!!v->rangeredfrm]; |
2188 |
for (i=0; i<6; i++) |
2189 |
{ |
2190 |
s->dc_val[0][s->block_index[i]] = 0; |
2191 |
dst_idx += i >> 2;
|
2192 |
val = ((cbp >> (5 - i)) & 1); |
2193 |
off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize); |
2194 |
v->mb_type[0][s->block_index[i]] = s->mb_intra;
|
2195 |
if(s->mb_intra) {
|
2196 |
/* check if prediction blocks A and C are available */
|
2197 |
v->a_avail = v->c_avail = 0;
|
2198 |
if(i == 2 || i == 3 || !s->first_slice_line) |
2199 |
v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
|
2200 |
if(i == 1 || i == 3 || s->mb_x) |
2201 |
v->c_avail = v->mb_type[0][s->block_index[i] - 1]; |
2202 |
|
2203 |
vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
|
2204 |
if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue; |
2205 |
idct8x8_fn(s->dest[dst_idx] + off, |
2206 |
i & 4 ? s->uvlinesize : s->linesize,
|
2207 |
s->block[i]); |
2208 |
if(v->pq >= 9 && v->overlap) { |
2209 |
if(v->c_avail)
|
2210 |
v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
|
2211 |
if(v->a_avail)
|
2212 |
v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
|
2213 |
} |
2214 |
if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){ |
2215 |
int left_cbp, top_cbp;
|
2216 |
if(i & 4){ |
2217 |
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4); |
2218 |
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
|
2219 |
}else{
|
2220 |
left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4)); |
2221 |
top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4)); |
2222 |
} |
2223 |
if(left_cbp & 0xC) |
2224 |
v->vc1dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
|
2225 |
if(top_cbp & 0xA) |
2226 |
v->vc1dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
|
2227 |
} |
2228 |
block_cbp |= 0xF << (i << 2); |
2229 |
} else if(val) { |
2230 |
int left_cbp = 0, top_cbp = 0, filter = 0; |
2231 |
if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){ |
2232 |
filter = 1;
|
2233 |
if(i & 4){ |
2234 |
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4); |
2235 |
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
|
2236 |
}else{
|
2237 |
left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4)); |
2238 |
top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4)); |
2239 |
} |
2240 |
if(left_cbp & 0xC) |
2241 |
v->vc1dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
|
2242 |
if(top_cbp & 0xA) |
2243 |
v->vc1dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
|
2244 |
} |
2245 |
pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp); |
2246 |
block_cbp |= pat << (i << 2);
|
2247 |
if(!v->ttmbf && ttmb < 8) ttmb = -1; |
2248 |
first_block = 0;
|
2249 |
} |
2250 |
} |
2251 |
} |
2252 |
else //Skipped |
2253 |
{ |
2254 |
s->mb_intra = 0;
|
2255 |
for(i = 0; i < 6; i++) { |
2256 |
v->mb_type[0][s->block_index[i]] = 0; |
2257 |
s->dc_val[0][s->block_index[i]] = 0; |
2258 |
} |
2259 |
s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP; |
2260 |
s->current_picture.qscale_table[mb_pos] = 0;
|
2261 |
vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]); |
2262 |
vc1_mc_1mv(v, 0);
|
2263 |
return 0; |
2264 |
} |
2265 |
} //1MV mode
|
2266 |
else //4MV mode |
2267 |
{ |
2268 |
if (!skipped /* unskipped MB */) |
2269 |
{ |
2270 |
int intra_count = 0, coded_inter = 0; |
2271 |
int is_intra[6], is_coded[6]; |
2272 |
vc1_idct_func idct8x8_fn; |
2273 |
/* Get CBPCY */
|
2274 |
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
|
2275 |
for (i=0; i<6; i++) |
2276 |
{ |
2277 |
val = ((cbp >> (5 - i)) & 1); |
2278 |
s->dc_val[0][s->block_index[i]] = 0; |
2279 |
s->mb_intra = 0;
|
2280 |
if(i < 4) { |
2281 |
dmv_x = dmv_y = 0;
|
2282 |
s->mb_intra = 0;
|
2283 |
mb_has_coeffs = 0;
|
2284 |
if(val) {
|
2285 |
GET_MVDATA(dmv_x, dmv_y); |
2286 |
} |
2287 |
vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]); |
2288 |
if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
|
2289 |
intra_count += s->mb_intra; |
2290 |
is_intra[i] = s->mb_intra; |
2291 |
is_coded[i] = mb_has_coeffs; |
2292 |
} |
2293 |
if(i&4){ |
2294 |
is_intra[i] = (intra_count >= 3);
|
2295 |
is_coded[i] = val; |
2296 |
} |
2297 |
if(i == 4) vc1_mc_4mv_chroma(v); |
2298 |
v->mb_type[0][s->block_index[i]] = is_intra[i];
|
2299 |
if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
|
2300 |
} |
2301 |
// if there are no coded blocks then don't do anything more
|
2302 |
if(!intra_count && !coded_inter) return 0; |
2303 |
dst_idx = 0;
|
2304 |
GET_MQUANT(); |
2305 |
s->current_picture.qscale_table[mb_pos] = mquant; |
2306 |
/* test if block is intra and has pred */
|
2307 |
{ |
2308 |
int intrapred = 0; |
2309 |
for(i=0; i<6; i++) |
2310 |
if(is_intra[i]) {
|
2311 |
if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]]) |
2312 |
|| ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) { |
2313 |
intrapred = 1;
|
2314 |
break;
|
2315 |
} |
2316 |
} |
2317 |
if(intrapred)s->ac_pred = get_bits1(gb);
|
2318 |
else s->ac_pred = 0; |
2319 |
} |
2320 |
if (!v->ttmbf && coded_inter)
|
2321 |
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
|
2322 |
idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[!!v->rangeredfrm]; |
2323 |
for (i=0; i<6; i++) |
2324 |
{ |
2325 |
dst_idx += i >> 2;
|
2326 |
off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize); |
2327 |
s->mb_intra = is_intra[i]; |
2328 |
if (is_intra[i]) {
|
2329 |
/* check if prediction blocks A and C are available */
|
2330 |
v->a_avail = v->c_avail = 0;
|
2331 |
if(i == 2 || i == 3 || !s->first_slice_line) |
2332 |
v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
|
2333 |
if(i == 1 || i == 3 || s->mb_x) |
2334 |
v->c_avail = v->mb_type[0][s->block_index[i] - 1]; |
2335 |
|
2336 |
vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
|
2337 |
if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue; |
2338 |
idct8x8_fn(s->dest[dst_idx] + off, |
2339 |
(i&4)?s->uvlinesize:s->linesize,
|
2340 |
s->block[i]); |
2341 |
if(v->pq >= 9 && v->overlap) { |
2342 |
if(v->c_avail)
|
2343 |
v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
|
2344 |
if(v->a_avail)
|
2345 |
v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
|
2346 |
} |
2347 |
if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){ |
2348 |
int left_cbp, top_cbp;
|
2349 |
if(i & 4){ |
2350 |
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4); |
2351 |
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
|
2352 |
}else{
|
2353 |
left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4)); |
2354 |
top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4)); |
2355 |
} |
2356 |
if(left_cbp & 0xC) |
2357 |
v->vc1dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
|
2358 |
if(top_cbp & 0xA) |
2359 |
v->vc1dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
|
2360 |
} |
2361 |
block_cbp |= 0xF << (i << 2); |
2362 |
} else if(is_coded[i]) { |
2363 |
int left_cbp = 0, top_cbp = 0, filter = 0; |
2364 |
if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){ |
2365 |
filter = 1;
|
2366 |
if(i & 4){ |
2367 |
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4); |
2368 |
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
|
2369 |
}else{
|
2370 |
left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4)); |
2371 |
top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4)); |
2372 |
} |
2373 |
if(left_cbp & 0xC) |
2374 |
v->vc1dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
|
2375 |
if(top_cbp & 0xA) |
2376 |
v->vc1dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
|
2377 |
} |
2378 |
pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp); |
2379 |
block_cbp |= pat << (i << 2);
|
2380 |
if(!v->ttmbf && ttmb < 8) ttmb = -1; |
2381 |
first_block = 0;
|
2382 |
} |
2383 |
} |
2384 |
return 0; |
2385 |
} |
2386 |
else //Skipped MB |
2387 |
{ |
2388 |
s->mb_intra = 0;
|
2389 |
s->current_picture.qscale_table[mb_pos] = 0;
|
2390 |
for (i=0; i<6; i++) { |
2391 |
v->mb_type[0][s->block_index[i]] = 0; |
2392 |
s->dc_val[0][s->block_index[i]] = 0; |
2393 |
} |
2394 |
for (i=0; i<4; i++) |
2395 |
{ |
2396 |
vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]); |
2397 |
vc1_mc_4mv_luma(v, i); |
2398 |
} |
2399 |
vc1_mc_4mv_chroma(v); |
2400 |
s->current_picture.qscale_table[mb_pos] = 0;
|
2401 |
return 0; |
2402 |
} |
2403 |
} |
2404 |
v->cbp[s->mb_x] = block_cbp; |
2405 |
|
2406 |
/* Should never happen */
|
2407 |
return -1; |
2408 |
} |
2409 |
|
2410 |
/** Decode one B-frame MB (in Main profile)
|
2411 |
*/
|
2412 |
static void vc1_decode_b_mb(VC1Context *v) |
2413 |
{ |
2414 |
MpegEncContext *s = &v->s; |
2415 |
GetBitContext *gb = &s->gb; |
2416 |
int i;
|
2417 |
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
|
2418 |
int cbp = 0; /* cbp decoding stuff */ |
2419 |
int mqdiff, mquant; /* MB quantization */ |
2420 |
int ttmb = v->ttfrm; /* MB Transform type */ |
2421 |
int mb_has_coeffs = 0; /* last_flag */ |
2422 |
int index, index1; /* LUT indexes */ |
2423 |
int val, sign; /* temp values */ |
2424 |
int first_block = 1; |
2425 |
int dst_idx, off;
|
2426 |
int skipped, direct;
|
2427 |
int dmv_x[2], dmv_y[2]; |
2428 |
int bmvtype = BMV_TYPE_BACKWARD;
|
2429 |
vc1_idct_func idct8x8_fn; |
2430 |
|
2431 |
mquant = v->pq; /* Loosy initialization */
|
2432 |
s->mb_intra = 0;
|
2433 |
|
2434 |
if (v->dmb_is_raw)
|
2435 |
direct = get_bits1(gb); |
2436 |
else
|
2437 |
direct = v->direct_mb_plane[mb_pos]; |
2438 |
if (v->skip_is_raw)
|
2439 |
skipped = get_bits1(gb); |
2440 |
else
|
2441 |
skipped = v->s.mbskip_table[mb_pos]; |
2442 |
|
2443 |
dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0; |
2444 |
for(i = 0; i < 6; i++) { |
2445 |
v->mb_type[0][s->block_index[i]] = 0; |
2446 |
s->dc_val[0][s->block_index[i]] = 0; |
2447 |
} |
2448 |
s->current_picture.qscale_table[mb_pos] = 0;
|
2449 |
|
2450 |
if (!direct) {
|
2451 |
if (!skipped) {
|
2452 |
GET_MVDATA(dmv_x[0], dmv_y[0]); |
2453 |
dmv_x[1] = dmv_x[0]; |
2454 |
dmv_y[1] = dmv_y[0]; |
2455 |
} |
2456 |
if(skipped || !s->mb_intra) {
|
2457 |
bmvtype = decode012(gb); |
2458 |
switch(bmvtype) {
|
2459 |
case 0: |
2460 |
bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
|
2461 |
break;
|
2462 |
case 1: |
2463 |
bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
|
2464 |
break;
|
2465 |
case 2: |
2466 |
bmvtype = BMV_TYPE_INTERPOLATED; |
2467 |
dmv_x[0] = dmv_y[0] = 0; |
2468 |
} |
2469 |
} |
2470 |
} |
2471 |
for(i = 0; i < 6; i++) |
2472 |
v->mb_type[0][s->block_index[i]] = s->mb_intra;
|
2473 |
|
2474 |
if (skipped) {
|
2475 |
if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
|
2476 |
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype); |
2477 |
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); |
2478 |
return;
|
2479 |
} |
2480 |
if (direct) {
|
2481 |
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
|
2482 |
GET_MQUANT(); |
2483 |
s->mb_intra = 0;
|
2484 |
s->current_picture.qscale_table[mb_pos] = mquant; |
2485 |
if(!v->ttmbf)
|
2486 |
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
|
2487 |
dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0; |
2488 |
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype); |
2489 |
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); |
2490 |
} else {
|
2491 |
if(!mb_has_coeffs && !s->mb_intra) {
|
2492 |
/* no coded blocks - effectively skipped */
|
2493 |
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype); |
2494 |
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); |
2495 |
return;
|
2496 |
} |
2497 |
if(s->mb_intra && !mb_has_coeffs) {
|
2498 |
GET_MQUANT(); |
2499 |
s->current_picture.qscale_table[mb_pos] = mquant; |
2500 |
s->ac_pred = get_bits1(gb); |
2501 |
cbp = 0;
|
2502 |
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype); |
2503 |
} else {
|
2504 |
if(bmvtype == BMV_TYPE_INTERPOLATED) {
|
2505 |
GET_MVDATA(dmv_x[0], dmv_y[0]); |
2506 |
if(!mb_has_coeffs) {
|
2507 |
/* interpolated skipped block */
|
2508 |
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype); |
2509 |
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); |
2510 |
return;
|
2511 |
} |
2512 |
} |
2513 |
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype); |
2514 |
if(!s->mb_intra) {
|
2515 |
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); |
2516 |
} |
2517 |
if(s->mb_intra)
|
2518 |
s->ac_pred = get_bits1(gb); |
2519 |
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
|
2520 |
GET_MQUANT(); |
2521 |
s->current_picture.qscale_table[mb_pos] = mquant; |
2522 |
if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
|
2523 |
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
|
2524 |
} |
2525 |
} |
2526 |
dst_idx = 0;
|
2527 |
idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[!!v->rangeredfrm]; |
2528 |
for (i=0; i<6; i++) |
2529 |
{ |
2530 |
s->dc_val[0][s->block_index[i]] = 0; |
2531 |
dst_idx += i >> 2;
|
2532 |
val = ((cbp >> (5 - i)) & 1); |
2533 |
off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize); |
2534 |
v->mb_type[0][s->block_index[i]] = s->mb_intra;
|
2535 |
if(s->mb_intra) {
|
2536 |
/* check if prediction blocks A and C are available */
|
2537 |
v->a_avail = v->c_avail = 0;
|
2538 |
if(i == 2 || i == 3 || !s->first_slice_line) |
2539 |
v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
|
2540 |
if(i == 1 || i == 3 || s->mb_x) |
2541 |
v->c_avail = v->mb_type[0][s->block_index[i] - 1]; |
2542 |
|
2543 |
vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
|
2544 |
if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue; |
2545 |
idct8x8_fn(s->dest[dst_idx] + off, |
2546 |
i & 4 ? s->uvlinesize : s->linesize,
|
2547 |
s->block[i]); |
2548 |
} else if(val) { |
2549 |
vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), 0, 0, 0); |
2550 |
if(!v->ttmbf && ttmb < 8) ttmb = -1; |
2551 |
first_block = 0;
|
2552 |
} |
2553 |
} |
2554 |
} |
2555 |
|
2556 |
/** Decode blocks of I-frame
|
2557 |
*/
|
2558 |
static void vc1_decode_i_blocks(VC1Context *v) |
2559 |
{ |
2560 |
int k;
|
2561 |
MpegEncContext *s = &v->s; |
2562 |
int cbp, val;
|
2563 |
uint8_t *coded_val; |
2564 |
int mb_pos;
|
2565 |
vc1_idct_func idct8x8_fn; |
2566 |
|
2567 |
/* select codingmode used for VLC tables selection */
|
2568 |
switch(v->y_ac_table_index){
|
2569 |
case 0: |
2570 |
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
|
2571 |
break;
|
2572 |
case 1: |
2573 |
v->codingset = CS_HIGH_MOT_INTRA; |
2574 |
break;
|
2575 |
case 2: |
2576 |
v->codingset = CS_MID_RATE_INTRA; |
2577 |
break;
|
2578 |
} |
2579 |
|
2580 |
switch(v->c_ac_table_index){
|
2581 |
case 0: |
2582 |
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
|
2583 |
break;
|
2584 |
case 1: |
2585 |
v->codingset2 = CS_HIGH_MOT_INTER; |
2586 |
break;
|
2587 |
case 2: |
2588 |
v->codingset2 = CS_MID_RATE_INTER; |
2589 |
break;
|
2590 |
} |
2591 |
|
2592 |
/* Set DC scale - y and c use the same */
|
2593 |
s->y_dc_scale = s->y_dc_scale_table[v->pq]; |
2594 |
s->c_dc_scale = s->c_dc_scale_table[v->pq]; |
2595 |
|
2596 |
//do frame decode
|
2597 |
s->mb_x = s->mb_y = 0;
|
2598 |
s->mb_intra = 1;
|
2599 |
s->first_slice_line = 1;
|
2600 |
if(v->pq >= 9 && v->overlap) { |
2601 |
idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[!!v->rangeredfrm]; |
2602 |
} else
|
2603 |
idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put[!!v->rangeredfrm]; |
2604 |
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { |
2605 |
s->mb_x = 0;
|
2606 |
ff_init_block_index(s); |
2607 |
for(; s->mb_x < s->mb_width; s->mb_x++) {
|
2608 |
uint8_t *dst[6];
|
2609 |
ff_update_block_index(s); |
2610 |
dst[0] = s->dest[0]; |
2611 |
dst[1] = dst[0] + 8; |
2612 |
dst[2] = s->dest[0] + s->linesize * 8; |
2613 |
dst[3] = dst[2] + 8; |
2614 |
dst[4] = s->dest[1]; |
2615 |
dst[5] = s->dest[2]; |
2616 |
s->dsp.clear_blocks(s->block[0]);
|
2617 |
mb_pos = s->mb_x + s->mb_y * s->mb_width; |
2618 |
s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA; |
2619 |
s->current_picture.qscale_table[mb_pos] = v->pq; |
2620 |
s->current_picture.motion_val[1][s->block_index[0]][0] = 0; |
2621 |
s->current_picture.motion_val[1][s->block_index[0]][1] = 0; |
2622 |
|
2623 |
// do actual MB decoding and displaying
|
2624 |
cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
|
2625 |
v->s.ac_pred = get_bits1(&v->s.gb); |
2626 |
|
2627 |
for(k = 0; k < 6; k++) { |
2628 |
val = ((cbp >> (5 - k)) & 1); |
2629 |
|
2630 |
if (k < 4) { |
2631 |
int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
|
2632 |
val = val ^ pred; |
2633 |
*coded_val = val; |
2634 |
} |
2635 |
cbp |= val << (5 - k);
|
2636 |
|
2637 |
vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
|
2638 |
|
2639 |
if (k > 3 && (s->flags & CODEC_FLAG_GRAY)) continue; |
2640 |
idct8x8_fn(dst[k], |
2641 |
k & 4 ? s->uvlinesize : s->linesize,
|
2642 |
s->block[k]); |
2643 |
} |
2644 |
|
2645 |
if(v->pq >= 9 && v->overlap) { |
2646 |
if(s->mb_x) {
|
2647 |
v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
|
2648 |
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize); |
2649 |
if(!(s->flags & CODEC_FLAG_GRAY)) {
|
2650 |
v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
|
2651 |
v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
|
2652 |
} |
2653 |
} |
2654 |
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize); |
2655 |
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize); |
2656 |
if(!s->first_slice_line) {
|
2657 |
v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
|
2658 |
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize); |
2659 |
if(!(s->flags & CODEC_FLAG_GRAY)) {
|
2660 |
v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
|
2661 |
v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
|
2662 |
} |
2663 |
} |
2664 |
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize); |
2665 |
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize); |
2666 |
} |
2667 |
if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
|
2668 |
|
2669 |
if(get_bits_count(&s->gb) > v->bits) {
|
2670 |
ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)); |
2671 |
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
|
2672 |
return;
|
2673 |
} |
2674 |
} |
2675 |
if (!v->s.loop_filter)
|
2676 |
ff_draw_horiz_band(s, s->mb_y * 16, 16); |
2677 |
else if (s->mb_y) |
2678 |
ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16); |
2679 |
|
2680 |
s->first_slice_line = 0;
|
2681 |
} |
2682 |
if (v->s.loop_filter)
|
2683 |
ff_draw_horiz_band(s, (s->mb_height-1)*16, 16); |
2684 |
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END)); |
2685 |
} |
2686 |
|
2687 |
/** Decode blocks of I-frame for advanced profile
|
2688 |
*/
|
2689 |
static void vc1_decode_i_blocks_adv(VC1Context *v) |
2690 |
{ |
2691 |
int k;
|
2692 |
MpegEncContext *s = &v->s; |
2693 |
int cbp, val;
|
2694 |
uint8_t *coded_val; |
2695 |
int mb_pos;
|
2696 |
int mquant = v->pq;
|
2697 |
int mqdiff;
|
2698 |
int overlap;
|
2699 |
GetBitContext *gb = &s->gb; |
2700 |
vc1_idct_func idct8x8_fn; |
2701 |
|
2702 |
/* select codingmode used for VLC tables selection */
|
2703 |
switch(v->y_ac_table_index){
|
2704 |
case 0: |
2705 |
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
|
2706 |
break;
|
2707 |
case 1: |
2708 |
v->codingset = CS_HIGH_MOT_INTRA; |
2709 |
break;
|
2710 |
case 2: |
2711 |
v->codingset = CS_MID_RATE_INTRA; |
2712 |
break;
|
2713 |
} |
2714 |
|
2715 |
switch(v->c_ac_table_index){
|
2716 |
case 0: |
2717 |
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
|
2718 |
break;
|
2719 |
case 1: |
2720 |
v->codingset2 = CS_HIGH_MOT_INTER; |
2721 |
break;
|
2722 |
case 2: |
2723 |
v->codingset2 = CS_MID_RATE_INTER; |
2724 |
break;
|
2725 |
} |
2726 |
|
2727 |
//do frame decode
|
2728 |
s->mb_x = s->mb_y = 0;
|
2729 |
s->mb_intra = 1;
|
2730 |
s->first_slice_line = 1;
|
2731 |
idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[0];
|
2732 |
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { |
2733 |
s->mb_x = 0;
|
2734 |
ff_init_block_index(s); |
2735 |
for(;s->mb_x < s->mb_width; s->mb_x++) {
|
2736 |
uint8_t *dst[6];
|
2737 |
ff_update_block_index(s); |
2738 |
dst[0] = s->dest[0]; |
2739 |
dst[1] = dst[0] + 8; |
2740 |
dst[2] = s->dest[0] + s->linesize * 8; |
2741 |
dst[3] = dst[2] + 8; |
2742 |
dst[4] = s->dest[1]; |
2743 |
dst[5] = s->dest[2]; |
2744 |
s->dsp.clear_blocks(s->block[0]);
|
2745 |
mb_pos = s->mb_x + s->mb_y * s->mb_stride; |
2746 |
s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA; |
2747 |
s->current_picture.motion_val[1][s->block_index[0]][0] = 0; |
2748 |
s->current_picture.motion_val[1][s->block_index[0]][1] = 0; |
2749 |
|
2750 |
// do actual MB decoding and displaying
|
2751 |
cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
|
2752 |
if(v->acpred_is_raw)
|
2753 |
v->s.ac_pred = get_bits1(&v->s.gb); |
2754 |
else
|
2755 |
v->s.ac_pred = v->acpred_plane[mb_pos]; |
2756 |
|
2757 |
if(v->condover == CONDOVER_SELECT) {
|
2758 |
if(v->overflg_is_raw)
|
2759 |
overlap = get_bits1(&v->s.gb); |
2760 |
else
|
2761 |
overlap = v->over_flags_plane[mb_pos]; |
2762 |
} else
|
2763 |
overlap = (v->condover == CONDOVER_ALL); |
2764 |
|
2765 |
GET_MQUANT(); |
2766 |
|
2767 |
s->current_picture.qscale_table[mb_pos] = mquant; |
2768 |
/* Set DC scale - y and c use the same */
|
2769 |
s->y_dc_scale = s->y_dc_scale_table[mquant]; |
2770 |
s->c_dc_scale = s->c_dc_scale_table[mquant]; |
2771 |
|
2772 |
for(k = 0; k < 6; k++) { |
2773 |
val = ((cbp >> (5 - k)) & 1); |
2774 |
|
2775 |
if (k < 4) { |
2776 |
int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
|
2777 |
val = val ^ pred; |
2778 |
*coded_val = val; |
2779 |
} |
2780 |
cbp |= val << (5 - k);
|
2781 |
|
2782 |
v->a_avail = !s->first_slice_line || (k==2 || k==3); |
2783 |
v->c_avail = !!s->mb_x || (k==1 || k==3); |
2784 |
|
2785 |
vc1_decode_i_block_adv(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2, mquant);
|
2786 |
|
2787 |
if (k > 3 && (s->flags & CODEC_FLAG_GRAY)) continue; |
2788 |
idct8x8_fn(dst[k], |
2789 |
k & 4 ? s->uvlinesize : s->linesize,
|
2790 |
s->block[k]); |
2791 |
} |
2792 |
|
2793 |
if(overlap) {
|
2794 |
if(s->mb_x) {
|
2795 |
v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
|
2796 |
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize); |
2797 |
if(!(s->flags & CODEC_FLAG_GRAY)) {
|
2798 |
v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
|
2799 |
v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
|
2800 |
} |
2801 |
} |
2802 |
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize); |
2803 |
v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize); |
2804 |
if(!s->first_slice_line) {
|
2805 |
v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
|
2806 |
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize); |
2807 |
if(!(s->flags & CODEC_FLAG_GRAY)) {
|
2808 |
v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
|
2809 |
v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
|
2810 |
} |
2811 |
} |
2812 |
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize); |
2813 |
v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize); |
2814 |
} |
2815 |
if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
|
2816 |
|
2817 |
if(get_bits_count(&s->gb) > v->bits) {
|
2818 |
ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)); |
2819 |
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
|
2820 |
return;
|
2821 |
} |
2822 |
} |
2823 |
if (!v->s.loop_filter)
|
2824 |
ff_draw_horiz_band(s, s->mb_y * 16, 16); |
2825 |
else if (s->mb_y) |
2826 |
ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16); |
2827 |
s->first_slice_line = 0;
|
2828 |
} |
2829 |
if (v->s.loop_filter)
|
2830 |
ff_draw_horiz_band(s, (s->mb_height-1)*16, 16); |
2831 |
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END)); |
2832 |
} |
2833 |
|
2834 |
static void vc1_decode_p_blocks(VC1Context *v) |
2835 |
{ |
2836 |
MpegEncContext *s = &v->s; |
2837 |
|
2838 |
/* select codingmode used for VLC tables selection */
|
2839 |
switch(v->c_ac_table_index){
|
2840 |
case 0: |
2841 |
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
|
2842 |
break;
|
2843 |
case 1: |
2844 |
v->codingset = CS_HIGH_MOT_INTRA; |
2845 |
break;
|
2846 |
case 2: |
2847 |
v->codingset = CS_MID_RATE_INTRA; |
2848 |
break;
|
2849 |
} |
2850 |
|
2851 |
switch(v->c_ac_table_index){
|
2852 |
case 0: |
2853 |
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
|
2854 |
break;
|
2855 |
case 1: |
2856 |
v->codingset2 = CS_HIGH_MOT_INTER; |
2857 |
break;
|
2858 |
case 2: |
2859 |
v->codingset2 = CS_MID_RATE_INTER; |
2860 |
break;
|
2861 |
} |
2862 |
|
2863 |
s->first_slice_line = 1;
|
2864 |
memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride); |
2865 |
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { |
2866 |
s->mb_x = 0;
|
2867 |
ff_init_block_index(s); |
2868 |
for(; s->mb_x < s->mb_width; s->mb_x++) {
|
2869 |
ff_update_block_index(s); |
2870 |
|
2871 |
vc1_decode_p_mb(v); |
2872 |
if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) { |
2873 |
ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)); |
2874 |
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
|
2875 |
return;
|
2876 |
} |
2877 |
} |
2878 |
memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride); |
2879 |
ff_draw_horiz_band(s, s->mb_y * 16, 16); |
2880 |
s->first_slice_line = 0;
|
2881 |
} |
2882 |
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END)); |
2883 |
} |
2884 |
|
2885 |
static void vc1_decode_b_blocks(VC1Context *v) |
2886 |
{ |
2887 |
MpegEncContext *s = &v->s; |
2888 |
|
2889 |
/* select codingmode used for VLC tables selection */
|
2890 |
switch(v->c_ac_table_index){
|
2891 |
case 0: |
2892 |
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
|
2893 |
break;
|
2894 |
case 1: |
2895 |
v->codingset = CS_HIGH_MOT_INTRA; |
2896 |
break;
|
2897 |
case 2: |
2898 |
v->codingset = CS_MID_RATE_INTRA; |
2899 |
break;
|
2900 |
} |
2901 |
|
2902 |
switch(v->c_ac_table_index){
|
2903 |
case 0: |
2904 |
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
|
2905 |
break;
|
2906 |
case 1: |
2907 |
v->codingset2 = CS_HIGH_MOT_INTER; |
2908 |
break;
|
2909 |
case 2: |
2910 |
v->codingset2 = CS_MID_RATE_INTER; |
2911 |
break;
|
2912 |
} |
2913 |
|
2914 |
s->first_slice_line = 1;
|
2915 |
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { |
2916 |
s->mb_x = 0;
|
2917 |
ff_init_block_index(s); |
2918 |
for(; s->mb_x < s->mb_width; s->mb_x++) {
|
2919 |
ff_update_block_index(s); |
2920 |
|
2921 |
vc1_decode_b_mb(v); |
2922 |
if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) { |
2923 |
ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)); |
2924 |
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
|
2925 |
return;
|
2926 |
} |
2927 |
if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
|
2928 |
} |
2929 |
if (!v->s.loop_filter)
|
2930 |
ff_draw_horiz_band(s, s->mb_y * 16, 16); |
2931 |
else if (s->mb_y) |
2932 |
ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16); |
2933 |
s->first_slice_line = 0;
|
2934 |
} |
2935 |
if (v->s.loop_filter)
|
2936 |
ff_draw_horiz_band(s, (s->mb_height-1)*16, 16); |
2937 |
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END)); |
2938 |
} |
2939 |
|
2940 |
static void vc1_decode_skip_blocks(VC1Context *v) |
2941 |
{ |
2942 |
MpegEncContext *s = &v->s; |
2943 |
|
2944 |
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END)); |
2945 |
s->first_slice_line = 1;
|
2946 |
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { |
2947 |
s->mb_x = 0;
|
2948 |
ff_init_block_index(s); |
2949 |
ff_update_block_index(s); |
2950 |
memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16); |
2951 |
memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8); |
2952 |
memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8); |
2953 |
ff_draw_horiz_band(s, s->mb_y * 16, 16); |
2954 |
s->first_slice_line = 0;
|
2955 |
} |
2956 |
s->pict_type = FF_P_TYPE; |
2957 |
} |
2958 |
|
2959 |
static void vc1_decode_blocks(VC1Context *v) |
2960 |
{ |
2961 |
|
2962 |
v->s.esc3_level_length = 0;
|
2963 |
if(v->x8_type){
|
2964 |
ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
|
2965 |
}else{
|
2966 |
|
2967 |
switch(v->s.pict_type) {
|
2968 |
case FF_I_TYPE:
|
2969 |
if(v->profile == PROFILE_ADVANCED)
|
2970 |
vc1_decode_i_blocks_adv(v); |
2971 |
else
|
2972 |
vc1_decode_i_blocks(v); |
2973 |
break;
|
2974 |
case FF_P_TYPE:
|
2975 |
if(v->p_frame_skipped)
|
2976 |
vc1_decode_skip_blocks(v); |
2977 |
else
|
2978 |
vc1_decode_p_blocks(v); |
2979 |
break;
|
2980 |
case FF_B_TYPE:
|
2981 |
if(v->bi_type){
|
2982 |
if(v->profile == PROFILE_ADVANCED)
|
2983 |
vc1_decode_i_blocks_adv(v); |
2984 |
else
|
2985 |
vc1_decode_i_blocks(v); |
2986 |
}else
|
2987 |
vc1_decode_b_blocks(v); |
2988 |
break;
|
2989 |
} |
2990 |
} |
2991 |
} |
2992 |
|
2993 |
/** Initialize a VC1/WMV3 decoder
|
2994 |
* @todo TODO: Handle VC-1 IDUs (Transport level?)
|
2995 |
* @todo TODO: Decypher remaining bits in extra_data
|
2996 |
*/
|
2997 |
static av_cold int vc1_decode_init(AVCodecContext *avctx) |
2998 |
{ |
2999 |
VC1Context *v = avctx->priv_data; |
3000 |
MpegEncContext *s = &v->s; |
3001 |
GetBitContext gb; |
3002 |
int i;
|
3003 |
|
3004 |
if (!avctx->extradata_size || !avctx->extradata) return -1; |
3005 |
if (!(avctx->flags & CODEC_FLAG_GRAY))
|
3006 |
avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts); |
3007 |
else
|
3008 |
avctx->pix_fmt = PIX_FMT_GRAY8; |
3009 |
avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt); |
3010 |
v->s.avctx = avctx; |
3011 |
avctx->flags |= CODEC_FLAG_EMU_EDGE; |
3012 |
v->s.flags |= CODEC_FLAG_EMU_EDGE; |
3013 |
|
3014 |
if(avctx->idct_algo==FF_IDCT_AUTO){
|
3015 |
avctx->idct_algo=FF_IDCT_WMV2; |
3016 |
} |
3017 |
|
3018 |
if(ff_msmpeg4_decode_init(avctx) < 0) |
3019 |
return -1; |
3020 |
if (vc1_init_common(v) < 0) return -1; |
3021 |
ff_vc1dsp_init(&v->vc1dsp); |
3022 |
for (i = 0; i < 64; i++) { |
3023 |
#define transpose(x) ((x>>3) | ((x&7)<<3)) |
3024 |
v->zz_8x8[0][i] = transpose(wmv1_scantable[0][i]); |
3025 |
v->zz_8x8[1][i] = transpose(wmv1_scantable[1][i]); |
3026 |
v->zz_8x8[2][i] = transpose(wmv1_scantable[2][i]); |
3027 |
v->zz_8x8[3][i] = transpose(wmv1_scantable[3][i]); |
3028 |
} |
3029 |
|
3030 |
avctx->coded_width = avctx->width; |
3031 |
avctx->coded_height = avctx->height; |
3032 |
if (avctx->codec_id == CODEC_ID_WMV3)
|
3033 |
{ |
3034 |
int count = 0; |
3035 |
|
3036 |
// looks like WMV3 has a sequence header stored in the extradata
|
3037 |
// advanced sequence header may be before the first frame
|
3038 |
// the last byte of the extradata is a version number, 1 for the
|
3039 |
// samples we can decode
|
3040 |
|
3041 |
init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
|
3042 |
|
3043 |
if (vc1_decode_sequence_header(avctx, v, &gb) < 0) |
3044 |
return -1; |
3045 |
|
3046 |
count = avctx->extradata_size*8 - get_bits_count(&gb);
|
3047 |
if (count>0) |
3048 |
{ |
3049 |
av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
|
3050 |
count, get_bits(&gb, count)); |
3051 |
} |
3052 |
else if (count < 0) |
3053 |
{ |
3054 |
av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
|
3055 |
} |
3056 |
} else { // VC1/WVC1 |
3057 |
const uint8_t *start = avctx->extradata;
|
3058 |
uint8_t *end = avctx->extradata + avctx->extradata_size; |
3059 |
const uint8_t *next;
|
3060 |
int size, buf2_size;
|
3061 |
uint8_t *buf2 = NULL;
|
3062 |
int seq_initialized = 0, ep_initialized = 0; |
3063 |
|
3064 |
if(avctx->extradata_size < 16) { |
3065 |
av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
|
3066 |
return -1; |
3067 |
} |
3068 |
|
3069 |
buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); |
3070 |
start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
|
3071 |
next = start; |
3072 |
for(; next < end; start = next){
|
3073 |
next = find_next_marker(start + 4, end);
|
3074 |
size = next - start - 4;
|
3075 |
if(size <= 0) continue; |
3076 |
buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
|
3077 |
init_get_bits(&gb, buf2, buf2_size * 8);
|
3078 |
switch(AV_RB32(start)){
|
3079 |
case VC1_CODE_SEQHDR:
|
3080 |
if(vc1_decode_sequence_header(avctx, v, &gb) < 0){ |
3081 |
av_free(buf2); |
3082 |
return -1; |
3083 |
} |
3084 |
seq_initialized = 1;
|
3085 |
break;
|
3086 |
case VC1_CODE_ENTRYPOINT:
|
3087 |
if(vc1_decode_entry_point(avctx, v, &gb) < 0){ |
3088 |
av_free(buf2); |
3089 |
return -1; |
3090 |
} |
3091 |
ep_initialized = 1;
|
3092 |
break;
|
3093 |
} |
3094 |
} |
3095 |
av_free(buf2); |
3096 |
if(!seq_initialized || !ep_initialized){
|
3097 |
av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
|
3098 |
return -1; |
3099 |
} |
3100 |
} |
3101 |
avctx->has_b_frames= !!(avctx->max_b_frames); |
3102 |
s->low_delay = !avctx->has_b_frames; |
3103 |
|
3104 |
s->mb_width = (avctx->coded_width+15)>>4; |
3105 |
s->mb_height = (avctx->coded_height+15)>>4; |
3106 |
|
3107 |
/* Allocate mb bitplanes */
|
3108 |
v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height); |
3109 |
v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height); |
3110 |
v->acpred_plane = av_malloc(s->mb_stride * s->mb_height); |
3111 |
v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height); |
3112 |
|
3113 |
v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride); |
3114 |
v->cbp = v->cbp_base + s->mb_stride; |
3115 |
|
3116 |
/* allocate block type info in that way so it could be used with s->block_index[] */
|
3117 |
v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2); |
3118 |
v->mb_type[0] = v->mb_type_base + s->b8_stride + 1; |
3119 |
v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1; |
3120 |
v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1); |
3121 |
|
3122 |
/* Init coded blocks info */
|
3123 |
if (v->profile == PROFILE_ADVANCED)
|
3124 |
{ |
3125 |
// if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
|
3126 |
// return -1;
|
3127 |
// if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
|
3128 |
// return -1;
|
3129 |
} |
3130 |
|
3131 |
ff_intrax8_common_init(&v->x8,s); |
3132 |
return 0; |
3133 |
} |
3134 |
|
3135 |
|
3136 |
/** Decode a VC1/WMV3 frame
|
3137 |
* @todo TODO: Handle VC-1 IDUs (Transport level?)
|
3138 |
*/
|
3139 |
static int vc1_decode_frame(AVCodecContext *avctx, |
3140 |
void *data, int *data_size, |
3141 |
AVPacket *avpkt) |
3142 |
{ |
3143 |
const uint8_t *buf = avpkt->data;
|
3144 |
int buf_size = avpkt->size;
|
3145 |
VC1Context *v = avctx->priv_data; |
3146 |
MpegEncContext *s = &v->s; |
3147 |
AVFrame *pict = data; |
3148 |
uint8_t *buf2 = NULL;
|
3149 |
const uint8_t *buf_start = buf;
|
3150 |
|
3151 |
/* no supplementary picture */
|
3152 |
if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) { |
3153 |
/* special case for last picture */
|
3154 |
if (s->low_delay==0 && s->next_picture_ptr) { |
3155 |
*pict= *(AVFrame*)s->next_picture_ptr; |
3156 |
s->next_picture_ptr= NULL;
|
3157 |
|
3158 |
*data_size = sizeof(AVFrame);
|
3159 |
} |
3160 |
|
3161 |
return 0; |
3162 |
} |
3163 |
|
3164 |
/* We need to set current_picture_ptr before reading the header,
|
3165 |
* otherwise we cannot store anything in there. */
|
3166 |
if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){ |
3167 |
int i= ff_find_unused_picture(s, 0); |
3168 |
s->current_picture_ptr= &s->picture[i]; |
3169 |
} |
3170 |
|
3171 |
if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
|
3172 |
if (v->profile < PROFILE_ADVANCED)
|
3173 |
avctx->pix_fmt = PIX_FMT_VDPAU_WMV3; |
3174 |
else
|
3175 |
avctx->pix_fmt = PIX_FMT_VDPAU_VC1; |
3176 |
} |
3177 |
|
3178 |
//for advanced profile we may need to parse and unescape data
|
3179 |
if (avctx->codec_id == CODEC_ID_VC1) {
|
3180 |
int buf_size2 = 0; |
3181 |
buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); |
3182 |
|
3183 |
if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */ |
3184 |
const uint8_t *start, *end, *next;
|
3185 |
int size;
|
3186 |
|
3187 |
next = buf; |
3188 |
for(start = buf, end = buf + buf_size; next < end; start = next){
|
3189 |
next = find_next_marker(start + 4, end);
|
3190 |
size = next - start - 4;
|
3191 |
if(size <= 0) continue; |
3192 |
switch(AV_RB32(start)){
|
3193 |
case VC1_CODE_FRAME:
|
3194 |
if (avctx->hwaccel ||
|
3195 |
s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) |
3196 |
buf_start = start; |
3197 |
buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
|
3198 |
break;
|
3199 |
case VC1_CODE_ENTRYPOINT: /* it should be before frame data */ |
3200 |
buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
|
3201 |
init_get_bits(&s->gb, buf2, buf_size2*8);
|
3202 |
vc1_decode_entry_point(avctx, v, &s->gb); |
3203 |
break;
|
3204 |
case VC1_CODE_SLICE:
|
3205 |
av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
|
3206 |
av_free(buf2); |
3207 |
return -1; |
3208 |
} |
3209 |
} |
3210 |
}else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */ |
3211 |
const uint8_t *divider;
|
3212 |
|
3213 |
divider = find_next_marker(buf, buf + buf_size); |
3214 |
if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
|
3215 |
av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
|
3216 |
av_free(buf2); |
3217 |
return -1; |
3218 |
} |
3219 |
|
3220 |
buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2); |
3221 |
// TODO
|
3222 |
if(!v->warn_interlaced++)
|
3223 |
av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced WVC1 support is not implemented\n");
|
3224 |
av_free(buf2);return -1; |
3225 |
}else{
|
3226 |
buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2); |
3227 |
} |
3228 |
init_get_bits(&s->gb, buf2, buf_size2*8);
|
3229 |
} else
|
3230 |
init_get_bits(&s->gb, buf, buf_size*8);
|
3231 |
// do parse frame header
|
3232 |
if(v->profile < PROFILE_ADVANCED) {
|
3233 |
if(vc1_parse_frame_header(v, &s->gb) == -1) { |
3234 |
av_free(buf2); |
3235 |
return -1; |
3236 |
} |
3237 |
} else {
|
3238 |
if(vc1_parse_frame_header_adv(v, &s->gb) == -1) { |
3239 |
av_free(buf2); |
3240 |
return -1; |
3241 |
} |
3242 |
} |
3243 |
|
3244 |
if(v->res_sprite && (s->pict_type!=FF_I_TYPE)){
|
3245 |
av_free(buf2); |
3246 |
return -1; |
3247 |
} |
3248 |
|
3249 |
// for hurry_up==5
|
3250 |
s->current_picture.pict_type= s->pict_type; |
3251 |
s->current_picture.key_frame= s->pict_type == FF_I_TYPE; |
3252 |
|
3253 |
/* skip B-frames if we don't have reference frames */
|
3254 |
if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){ |
3255 |
av_free(buf2); |
3256 |
return -1;//buf_size; |
3257 |
} |
3258 |
/* skip b frames if we are in a hurry */
|
3259 |
if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size; |
3260 |
if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
|
3261 |
|| (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE) |
3262 |
|| avctx->skip_frame >= AVDISCARD_ALL) { |
3263 |
av_free(buf2); |
3264 |
return buf_size;
|
3265 |
} |
3266 |
/* skip everything if we are in a hurry>=5 */
|
3267 |
if(avctx->hurry_up>=5) { |
3268 |
av_free(buf2); |
3269 |
return -1;//buf_size; |
3270 |
} |
3271 |
|
3272 |
if(s->next_p_frame_damaged){
|
3273 |
if(s->pict_type==FF_B_TYPE)
|
3274 |
return buf_size;
|
3275 |
else
|
3276 |
s->next_p_frame_damaged=0;
|
3277 |
} |
3278 |
|
3279 |
if(MPV_frame_start(s, avctx) < 0) { |
3280 |
av_free(buf2); |
3281 |
return -1; |
3282 |
} |
3283 |
|
3284 |
s->me.qpel_put= s->dsp.put_qpel_pixels_tab; |
3285 |
s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab; |
3286 |
|
3287 |
if ((CONFIG_VC1_VDPAU_DECODER)
|
3288 |
&&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) |
3289 |
ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start); |
3290 |
else if (avctx->hwaccel) { |
3291 |
if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0) |
3292 |
return -1; |
3293 |
if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0) |
3294 |
return -1; |
3295 |
if (avctx->hwaccel->end_frame(avctx) < 0) |
3296 |
return -1; |
3297 |
} else {
|
3298 |
ff_er_frame_start(s); |
3299 |
|
3300 |
v->bits = buf_size * 8;
|
3301 |
vc1_decode_blocks(v); |
3302 |
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
|
3303 |
// if(get_bits_count(&s->gb) > buf_size * 8)
|
3304 |
// return -1;
|
3305 |
ff_er_frame_end(s); |
3306 |
} |
3307 |
|
3308 |
MPV_frame_end(s); |
3309 |
|
3310 |
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type); |
3311 |
assert(s->current_picture.pict_type == s->pict_type); |
3312 |
if (s->pict_type == FF_B_TYPE || s->low_delay) {
|
3313 |
*pict= *(AVFrame*)s->current_picture_ptr; |
3314 |
} else if (s->last_picture_ptr != NULL) { |
3315 |
*pict= *(AVFrame*)s->last_picture_ptr; |
3316 |
} |
3317 |
|
3318 |
if(s->last_picture_ptr || s->low_delay){
|
3319 |
*data_size = sizeof(AVFrame);
|
3320 |
ff_print_debug_info(s, pict); |
3321 |
} |
3322 |
|
3323 |
av_free(buf2); |
3324 |
return buf_size;
|
3325 |
} |
3326 |
|
3327 |
|
3328 |
/** Close a VC1/WMV3 decoder
|
3329 |
* @warning Initial try at using MpegEncContext stuff
|
3330 |
*/
|
3331 |
static av_cold int vc1_decode_end(AVCodecContext *avctx) |
3332 |
{ |
3333 |
VC1Context *v = avctx->priv_data; |
3334 |
|
3335 |
av_freep(&v->hrd_rate); |
3336 |
av_freep(&v->hrd_buffer); |
3337 |
MPV_common_end(&v->s); |
3338 |
av_freep(&v->mv_type_mb_plane); |
3339 |
av_freep(&v->direct_mb_plane); |
3340 |
av_freep(&v->acpred_plane); |
3341 |
av_freep(&v->over_flags_plane); |
3342 |
av_freep(&v->mb_type_base); |
3343 |
av_freep(&v->cbp_base); |
3344 |
ff_intrax8_common_end(&v->x8); |
3345 |
return 0; |
3346 |
} |
3347 |
|
3348 |
|
3349 |
AVCodec ff_vc1_decoder = { |
3350 |
"vc1",
|
3351 |
AVMEDIA_TYPE_VIDEO, |
3352 |
CODEC_ID_VC1, |
3353 |
sizeof(VC1Context),
|
3354 |
vc1_decode_init, |
3355 |
NULL,
|
3356 |
vc1_decode_end, |
3357 |
vc1_decode_frame, |
3358 |
CODEC_CAP_DR1 | CODEC_CAP_DELAY, |
3359 |
NULL,
|
3360 |
.long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
|
3361 |
.pix_fmts = ff_hwaccel_pixfmt_list_420 |
3362 |
}; |
3363 |
|
3364 |
#if CONFIG_WMV3_DECODER
|
3365 |
AVCodec ff_wmv3_decoder = { |
3366 |
"wmv3",
|
3367 |
AVMEDIA_TYPE_VIDEO, |
3368 |
CODEC_ID_WMV3, |
3369 |
sizeof(VC1Context),
|
3370 |
vc1_decode_init, |
3371 |
NULL,
|
3372 |
vc1_decode_end, |
3373 |
vc1_decode_frame, |
3374 |
CODEC_CAP_DR1 | CODEC_CAP_DELAY, |
3375 |
NULL,
|
3376 |
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
|
3377 |
.pix_fmts = ff_hwaccel_pixfmt_list_420 |
3378 |
}; |
3379 |
#endif
|
3380 |
|
3381 |
#if CONFIG_WMV3_VDPAU_DECODER
|
3382 |
AVCodec ff_wmv3_vdpau_decoder = { |
3383 |
"wmv3_vdpau",
|
3384 |
AVMEDIA_TYPE_VIDEO, |
3385 |
CODEC_ID_WMV3, |
3386 |
sizeof(VC1Context),
|
3387 |
vc1_decode_init, |
3388 |
NULL,
|
3389 |
vc1_decode_end, |
3390 |
vc1_decode_frame, |
3391 |
CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU, |
3392 |
NULL,
|
3393 |
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
|
3394 |
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE} |
3395 |
}; |
3396 |
#endif
|
3397 |
|
3398 |
#if CONFIG_VC1_VDPAU_DECODER
|
3399 |
AVCodec ff_vc1_vdpau_decoder = { |
3400 |
"vc1_vdpau",
|
3401 |
AVMEDIA_TYPE_VIDEO, |
3402 |
CODEC_ID_VC1, |
3403 |
sizeof(VC1Context),
|
3404 |
vc1_decode_init, |
3405 |
NULL,
|
3406 |
vc1_decode_end, |
3407 |
vc1_decode_frame, |
3408 |
CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU, |
3409 |
NULL,
|
3410 |
.long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
|
3411 |
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_VC1, PIX_FMT_NONE} |
3412 |
}; |
3413 |
#endif
|