Mercurial > libavcodec.hg
annotate wmv2dec.c @ 7059:54e5978dd41a libavcodec
Change iquant tables to int16.
author | michael |
---|---|
date | Tue, 17 Jun 2008 14:03:00 +0000 |
parents | e943e1409077 |
children | d6bab465b82c |
rev | line source |
---|---|
5939 | 1 /* |
2 * Copyright (c) 2002 The FFmpeg Project. | |
3 * | |
4 * This file is part of FFmpeg. | |
5 * | |
6 * FFmpeg is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2.1 of the License, or (at your option) any later version. | |
10 * | |
11 * FFmpeg is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with FFmpeg; if not, write to the Free Software | |
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 */ | |
20 | |
21 #include "avcodec.h" | |
22 #include "dsputil.h" | |
23 #include "mpegvideo.h" | |
24 #include "msmpeg4.h" | |
25 #include "msmpeg4data.h" | |
26 #include "intrax8.h" | |
27 #include "wmv2.h" | |
28 | |
29 | |
30 static void parse_mb_skip(Wmv2Context * w){ | |
31 int mb_x, mb_y; | |
32 MpegEncContext * const s= &w->s; | |
33 uint32_t * const mb_type= s->current_picture_ptr->mb_type; | |
34 | |
35 w->skip_type= get_bits(&s->gb, 2); | |
36 switch(w->skip_type){ | |
37 case SKIP_TYPE_NONE: | |
38 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
39 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
40 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_16x16 | MB_TYPE_L0; | |
41 } | |
42 } | |
43 break; | |
44 case SKIP_TYPE_MPEG: | |
45 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
46 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
47 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0; | |
48 } | |
49 } | |
50 break; | |
51 case SKIP_TYPE_ROW: | |
52 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
53 if(get_bits1(&s->gb)){ | |
54 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
55 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
56 } | |
57 }else{ | |
58 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
59 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0; | |
60 } | |
61 } | |
62 } | |
63 break; | |
64 case SKIP_TYPE_COL: | |
65 for(mb_x=0; mb_x<s->mb_width; mb_x++){ | |
66 if(get_bits1(&s->gb)){ | |
67 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
68 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; | |
69 } | |
70 }else{ | |
71 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
72 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0; | |
73 } | |
74 } | |
75 } | |
76 break; | |
77 } | |
78 } | |
79 | |
80 static int decode_ext_header(Wmv2Context *w){ | |
81 MpegEncContext * const s= &w->s; | |
82 GetBitContext gb; | |
83 int fps; | |
84 int code; | |
85 | |
86 if(s->avctx->extradata_size<4) return -1; | |
87 | |
88 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8); | |
89 | |
90 fps = get_bits(&gb, 5); | |
91 s->bit_rate = get_bits(&gb, 11)*1024; | |
92 w->mspel_bit = get_bits1(&gb); | |
93 s->loop_filter = get_bits1(&gb); | |
94 w->abt_flag = get_bits1(&gb); | |
95 w->j_type_bit = get_bits1(&gb); | |
96 w->top_left_mv_flag= get_bits1(&gb); | |
97 w->per_mb_rl_bit = get_bits1(&gb); | |
98 code = get_bits(&gb, 3); | |
99 | |
100 if(code==0) return -1; | |
101 | |
102 s->slice_height = s->mb_height / code; | |
103 | |
104 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
105 av_log(s->avctx, AV_LOG_DEBUG, "fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, tl_mv_flag:%d, mbrl_bit:%d, code:%d, loop_filter:%d, slices:%d\n", | |
106 fps, s->bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit, w->top_left_mv_flag, w->per_mb_rl_bit, code, s->loop_filter, | |
107 code); | |
108 } | |
109 return 0; | |
110 } | |
111 | |
112 int ff_wmv2_decode_picture_header(MpegEncContext * s) | |
113 { | |
114 Wmv2Context * const w= (Wmv2Context*)s; | |
115 int code; | |
116 | |
117 #if 0 | |
118 { | |
119 int i; | |
120 for(i=0; i<s->gb.size*8; i++) | |
121 printf("%d", get_bits1(&s->gb)); | |
122 // get_bits1(&s->gb); | |
123 printf("END\n"); | |
124 return -1; | |
125 } | |
126 #endif | |
127 if(s->picture_number==0) | |
128 decode_ext_header(w); | |
129 | |
130 s->pict_type = get_bits1(&s->gb) + 1; | |
6481 | 131 if(s->pict_type == FF_I_TYPE){ |
5939 | 132 code = get_bits(&s->gb, 7); |
133 av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code); | |
134 } | |
135 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5); | |
6149 | 136 if(s->qscale <= 0) |
5939 | 137 return -1; |
138 | |
139 return 0; | |
140 } | |
141 | |
142 int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s) | |
143 { | |
144 Wmv2Context * const w= (Wmv2Context*)s; | |
145 | |
6481 | 146 if (s->pict_type == FF_I_TYPE) { |
5939 | 147 if(w->j_type_bit) w->j_type= get_bits1(&s->gb); |
148 else w->j_type= 0; //FIXME check | |
149 | |
150 if(!w->j_type){ | |
151 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb); | |
152 else s->per_mb_rl_table= 0; | |
153 | |
154 if(!s->per_mb_rl_table){ | |
155 s->rl_chroma_table_index = decode012(&s->gb); | |
156 s->rl_table_index = decode012(&s->gb); | |
157 } | |
158 | |
159 s->dc_table_index = get_bits1(&s->gb); | |
160 } | |
161 s->inter_intra_pred= 0; | |
162 s->no_rounding = 1; | |
163 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
164 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n", | |
165 s->qscale, | |
166 s->rl_chroma_table_index, | |
167 s->rl_table_index, | |
168 s->dc_table_index, | |
169 s->per_mb_rl_table, | |
170 w->j_type); | |
171 } | |
172 }else{ | |
173 int cbp_index; | |
174 w->j_type=0; | |
175 | |
176 parse_mb_skip(w); | |
177 cbp_index= decode012(&s->gb); | |
178 if(s->qscale <= 10){ | |
179 int map[3]= {0,2,1}; | |
180 w->cbp_table_index= map[cbp_index]; | |
181 }else if(s->qscale <= 20){ | |
182 int map[3]= {1,0,2}; | |
183 w->cbp_table_index= map[cbp_index]; | |
184 }else{ | |
185 int map[3]= {2,1,0}; | |
186 w->cbp_table_index= map[cbp_index]; | |
187 } | |
188 | |
189 if(w->mspel_bit) s->mspel= get_bits1(&s->gb); | |
190 else s->mspel= 0; //FIXME check | |
191 | |
192 if(w->abt_flag){ | |
193 w->per_mb_abt= get_bits1(&s->gb)^1; | |
194 if(!w->per_mb_abt){ | |
195 w->abt_type= decode012(&s->gb); | |
196 } | |
197 } | |
198 | |
199 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb); | |
200 else s->per_mb_rl_table= 0; | |
201 | |
202 if(!s->per_mb_rl_table){ | |
203 s->rl_table_index = decode012(&s->gb); | |
204 s->rl_chroma_table_index = s->rl_table_index; | |
205 } | |
206 | |
207 s->dc_table_index = get_bits1(&s->gb); | |
208 s->mv_table_index = get_bits1(&s->gb); | |
209 | |
210 s->inter_intra_pred= 0;//(s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE); | |
211 s->no_rounding ^= 1; | |
212 | |
213 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
214 av_log(s->avctx, AV_LOG_DEBUG, "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n", | |
215 s->rl_table_index, | |
216 s->rl_chroma_table_index, | |
217 s->dc_table_index, | |
218 s->mv_table_index, | |
219 s->per_mb_rl_table, | |
220 s->qscale, | |
221 s->mspel, | |
222 w->per_mb_abt, | |
223 w->abt_type, | |
224 w->cbp_table_index, | |
225 s->inter_intra_pred); | |
226 } | |
227 } | |
228 s->esc3_level_length= 0; | |
229 s->esc3_run_length= 0; | |
230 | |
231 s->picture_number++; //FIXME ? | |
232 | |
233 | |
234 if(w->j_type){ | |
235 ff_intrax8_decode_picture(&w->x8, 2*s->qscale, (s->qscale-1)|1 ); | |
236 return 1; | |
237 } | |
238 | |
239 return 0; | |
240 } | |
241 | |
242 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){ | |
243 MpegEncContext * const s= &w->s; | |
244 int ret; | |
245 | |
246 ret= ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr); | |
247 | |
248 if(ret<0) return -1; | |
249 | |
250 if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel) | |
251 w->hshift= get_bits1(&s->gb); | |
252 else | |
253 w->hshift= 0; | |
254 | |
255 //printf("%d %d ", *mx_ptr, *my_ptr); | |
256 | |
257 return 0; | |
258 } | |
259 | |
260 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){ | |
261 MpegEncContext * const s= &w->s; | |
262 int xy, wrap, diff, type; | |
263 int16_t *A, *B, *C, *mot_val; | |
264 | |
265 wrap = s->b8_stride; | |
266 xy = s->block_index[0]; | |
267 | |
268 mot_val = s->current_picture.motion_val[0][xy]; | |
269 | |
270 A = s->current_picture.motion_val[0][xy - 1]; | |
271 B = s->current_picture.motion_val[0][xy - wrap]; | |
272 C = s->current_picture.motion_val[0][xy + 2 - wrap]; | |
273 | |
274 if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag) | |
275 diff= FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1])); | |
276 else | |
277 diff=0; | |
278 | |
279 if(diff >= 8) | |
280 type= get_bits1(&s->gb); | |
281 else | |
282 type= 2; | |
283 | |
284 if(type == 0){ | |
285 *px= A[0]; | |
286 *py= A[1]; | |
287 }else if(type == 1){ | |
288 *px= B[0]; | |
289 *py= B[1]; | |
290 }else{ | |
291 /* special case for first (slice) line */ | |
292 if (s->first_slice_line) { | |
293 *px = A[0]; | |
294 *py = A[1]; | |
295 } else { | |
296 *px = mid_pred(A[0], B[0], C[0]); | |
297 *py = mid_pred(A[1], B[1], C[1]); | |
298 } | |
299 } | |
300 | |
301 return mot_val; | |
302 } | |
303 | |
304 static inline int wmv2_decode_inter_block(Wmv2Context *w, DCTELEM *block, int n, int cbp){ | |
305 MpegEncContext * const s= &w->s; | |
306 static const int sub_cbp_table[3]= {2,3,1}; | |
307 int sub_cbp; | |
308 | |
309 if(!cbp){ | |
310 s->block_last_index[n] = -1; | |
311 | |
312 return 0; | |
313 } | |
314 | |
315 if(w->per_block_abt) | |
316 w->abt_type= decode012(&s->gb); | |
317 #if 0 | |
318 if(w->per_block_abt) | |
319 printf("B%d", w->abt_type); | |
320 #endif | |
321 w->abt_type_table[n]= w->abt_type; | |
322 | |
323 if(w->abt_type){ | |
324 // const uint8_t *scantable= w->abt_scantable[w->abt_type-1].permutated; | |
325 const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable; | |
326 // const uint8_t *scantable= w->abt_type-1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable; | |
327 | |
328 sub_cbp= sub_cbp_table[ decode012(&s->gb) ]; | |
329 // printf("S%d", sub_cbp); | |
330 | |
331 if(sub_cbp&1){ | |
332 if (ff_msmpeg4_decode_block(s, block, n, 1, scantable) < 0) | |
333 return -1; | |
334 } | |
335 | |
336 if(sub_cbp&2){ | |
337 if (ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0) | |
338 return -1; | |
339 } | |
340 s->block_last_index[n] = 63; | |
341 | |
342 return 0; | |
343 }else{ | |
344 return ff_msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated); | |
345 } | |
346 } | |
347 | |
348 | |
349 int ff_wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
350 { | |
351 Wmv2Context * const w= (Wmv2Context*)s; | |
352 int cbp, code, i; | |
353 uint8_t *coded_val; | |
354 | |
355 if(w->j_type) return 0; | |
356 | |
6481 | 357 if (s->pict_type == FF_P_TYPE) { |
5939 | 358 if(IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])){ |
359 /* skip mb */ | |
360 s->mb_intra = 0; | |
361 for(i=0;i<6;i++) | |
362 s->block_last_index[i] = -1; | |
363 s->mv_dir = MV_DIR_FORWARD; | |
364 s->mv_type = MV_TYPE_16X16; | |
365 s->mv[0][0][0] = 0; | |
366 s->mv[0][0][1] = 0; | |
367 s->mb_skipped = 1; | |
368 w->hshift=0; | |
369 return 0; | |
370 } | |
371 | |
372 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3); | |
373 if (code < 0) | |
374 return -1; | |
375 s->mb_intra = (~code & 0x40) >> 6; | |
376 | |
377 cbp = code & 0x3f; | |
378 } else { | |
379 s->mb_intra = 1; | |
380 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2); | |
381 if (code < 0){ | |
382 av_log(s->avctx, AV_LOG_ERROR, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y); | |
383 return -1; | |
384 } | |
385 /* predict coded block pattern */ | |
386 cbp = 0; | |
387 for(i=0;i<6;i++) { | |
388 int val = ((code >> (5 - i)) & 1); | |
389 if (i < 4) { | |
390 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val); | |
391 val = val ^ pred; | |
392 *coded_val = val; | |
393 } | |
394 cbp |= val << (5 - i); | |
395 } | |
396 } | |
397 | |
398 if (!s->mb_intra) { | |
399 int mx, my; | |
400 //printf("P at %d %d\n", s->mb_x, s->mb_y); | |
401 wmv2_pred_motion(w, &mx, &my); | |
402 | |
403 if(cbp){ | |
404 s->dsp.clear_blocks(s->block[0]); | |
405 if(s->per_mb_rl_table){ | |
406 s->rl_table_index = decode012(&s->gb); | |
407 s->rl_chroma_table_index = s->rl_table_index; | |
408 } | |
409 | |
410 if(w->abt_flag && w->per_mb_abt){ | |
411 w->per_block_abt= get_bits1(&s->gb); | |
412 if(!w->per_block_abt) | |
413 w->abt_type= decode012(&s->gb); | |
414 }else | |
415 w->per_block_abt=0; | |
416 } | |
417 | |
418 if (wmv2_decode_motion(w, &mx, &my) < 0) | |
419 return -1; | |
420 | |
421 s->mv_dir = MV_DIR_FORWARD; | |
422 s->mv_type = MV_TYPE_16X16; | |
423 s->mv[0][0][0] = mx; | |
424 s->mv[0][0][1] = my; | |
425 | |
426 for (i = 0; i < 6; i++) { | |
427 if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
428 { | |
429 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
430 return -1; | |
431 } | |
432 } | |
433 } else { | |
6481 | 434 //if(s->pict_type==FF_P_TYPE) |
5939 | 435 // printf("%d%d ", s->inter_intra_pred, cbp); |
436 //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24)); | |
437 s->ac_pred = get_bits1(&s->gb); | |
438 if(s->inter_intra_pred){ | |
439 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1); | |
440 // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y); | |
441 } | |
442 if(s->per_mb_rl_table && cbp){ | |
443 s->rl_table_index = decode012(&s->gb); | |
444 s->rl_chroma_table_index = s->rl_table_index; | |
445 } | |
446 | |
447 s->dsp.clear_blocks(s->block[0]); | |
448 for (i = 0; i < 6; i++) { | |
449 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) | |
450 { | |
451 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
452 return -1; | |
453 } | |
454 } | |
455 } | |
456 | |
457 return 0; | |
458 } | |
459 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6481
diff
changeset
|
460 static av_cold int wmv2_decode_init(AVCodecContext *avctx){ |
5939 | 461 Wmv2Context * const w= avctx->priv_data; |
462 | |
463 if(avctx->idct_algo==FF_IDCT_AUTO){ | |
464 avctx->idct_algo=FF_IDCT_WMV2; | |
465 } | |
466 | |
467 if(ff_h263_decode_init(avctx) < 0) | |
468 return -1; | |
469 | |
470 ff_wmv2_common_init(w); | |
471 | |
472 ff_intrax8_common_init(&w->x8,&w->s); | |
473 | |
474 return 0; | |
475 } | |
476 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6481
diff
changeset
|
477 static av_cold int wmv2_decode_end(AVCodecContext *avctx) |
6176
1d735690e172
Correctly clean up IntraX8Context upon codec close.
andoma
parents:
6149
diff
changeset
|
478 { |
1d735690e172
Correctly clean up IntraX8Context upon codec close.
andoma
parents:
6149
diff
changeset
|
479 Wmv2Context *w = avctx->priv_data; |
1d735690e172
Correctly clean up IntraX8Context upon codec close.
andoma
parents:
6149
diff
changeset
|
480 |
1d735690e172
Correctly clean up IntraX8Context upon codec close.
andoma
parents:
6149
diff
changeset
|
481 ff_intrax8_common_end(&w->x8); |
1d735690e172
Correctly clean up IntraX8Context upon codec close.
andoma
parents:
6149
diff
changeset
|
482 return ff_h263_decode_end(avctx); |
1d735690e172
Correctly clean up IntraX8Context upon codec close.
andoma
parents:
6149
diff
changeset
|
483 } |
1d735690e172
Correctly clean up IntraX8Context upon codec close.
andoma
parents:
6149
diff
changeset
|
484 |
5939 | 485 AVCodec wmv2_decoder = { |
486 "wmv2", | |
487 CODEC_TYPE_VIDEO, | |
488 CODEC_ID_WMV2, | |
489 sizeof(Wmv2Context), | |
490 wmv2_decode_init, | |
491 NULL, | |
6176
1d735690e172
Correctly clean up IntraX8Context upon codec close.
andoma
parents:
6149
diff
changeset
|
492 wmv2_decode_end, |
5939 | 493 ff_h263_decode_frame, |
494 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1, | |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6710
diff
changeset
|
495 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"), |
5939 | 496 }; |