2 * Copyright (c) 2002 The FFmpeg Project
4 * This file is part of FFmpeg.
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.
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.
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
22 #include "mpegvideo.h"
26 #include "msmpeg4data.h"
31 static void parse_mb_skip(Wmv2Context * w){
33 MpegEncContext * const s= &w->s;
34 uint32_t * const mb_type = s->current_picture_ptr->mb_type;
36 w->skip_type= get_bits(&s->gb, 2);
39 for(mb_y=0; mb_y<s->mb_height; mb_y++){
40 for(mb_x=0; mb_x<s->mb_width; mb_x++){
41 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_16x16 | MB_TYPE_L0;
46 for(mb_y=0; mb_y<s->mb_height; mb_y++){
47 for(mb_x=0; mb_x<s->mb_width; mb_x++){
48 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
53 for(mb_y=0; mb_y<s->mb_height; mb_y++){
54 if(get_bits1(&s->gb)){
55 for(mb_x=0; mb_x<s->mb_width; mb_x++){
56 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
59 for(mb_x=0; mb_x<s->mb_width; mb_x++){
60 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
66 for(mb_x=0; mb_x<s->mb_width; mb_x++){
67 if(get_bits1(&s->gb)){
68 for(mb_y=0; mb_y<s->mb_height; mb_y++){
69 mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
72 for(mb_y=0; mb_y<s->mb_height; mb_y++){
73 mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
81 static int decode_ext_header(Wmv2Context *w){
82 MpegEncContext * const s= &w->s;
87 if(s->avctx->extradata_size<4) return -1;
89 init_get_bits(&gb, s->avctx->extradata, 32);
91 fps = get_bits(&gb, 5);
92 s->bit_rate = get_bits(&gb, 11)*1024;
93 w->mspel_bit = get_bits1(&gb);
94 s->loop_filter = get_bits1(&gb);
95 w->abt_flag = get_bits1(&gb);
96 w->j_type_bit = get_bits1(&gb);
97 w->top_left_mv_flag= get_bits1(&gb);
98 w->per_mb_rl_bit = get_bits1(&gb);
99 code = get_bits(&gb, 3);
101 if(code==0) return -1;
103 s->slice_height = s->mb_height / code;
105 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
106 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",
107 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,
113 int ff_wmv2_decode_picture_header(MpegEncContext * s)
115 Wmv2Context * const w= (Wmv2Context*)s;
118 if(s->picture_number==0)
119 decode_ext_header(w);
121 s->pict_type = get_bits1(&s->gb) + 1;
122 if(s->pict_type == AV_PICTURE_TYPE_I){
123 code = get_bits(&s->gb, 7);
124 av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code);
126 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
133 int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s)
135 Wmv2Context * const w= (Wmv2Context*)s;
137 if (s->pict_type == AV_PICTURE_TYPE_I) {
138 if(w->j_type_bit) w->j_type= get_bits1(&s->gb);
139 else w->j_type= 0; //FIXME check
142 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
143 else s->per_mb_rl_table= 0;
145 if(!s->per_mb_rl_table){
146 s->rl_chroma_table_index = decode012(&s->gb);
147 s->rl_table_index = decode012(&s->gb);
150 s->dc_table_index = get_bits1(&s->gb);
152 s->inter_intra_pred= 0;
154 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
155 av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
157 s->rl_chroma_table_index,
168 cbp_index= decode012(&s->gb);
171 w->cbp_table_index= map[cbp_index];
172 }else if(s->qscale <= 20){
174 w->cbp_table_index= map[cbp_index];
177 w->cbp_table_index= map[cbp_index];
180 if(w->mspel_bit) s->mspel= get_bits1(&s->gb);
181 else s->mspel= 0; //FIXME check
184 w->per_mb_abt= get_bits1(&s->gb)^1;
186 w->abt_type= decode012(&s->gb);
190 if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
191 else s->per_mb_rl_table= 0;
193 if(!s->per_mb_rl_table){
194 s->rl_table_index = decode012(&s->gb);
195 s->rl_chroma_table_index = s->rl_table_index;
198 s->dc_table_index = get_bits1(&s->gb);
199 s->mv_table_index = get_bits1(&s->gb);
201 s->inter_intra_pred= 0;//(s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
204 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
205 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",
207 s->rl_chroma_table_index,
216 s->inter_intra_pred);
219 s->esc3_level_length= 0;
220 s->esc3_run_length= 0;
222 s->picture_number++; //FIXME ?
226 ff_intrax8_decode_picture(&w->x8, 2*s->qscale, (s->qscale-1)|1 );
233 static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){
234 MpegEncContext * const s= &w->s;
237 ret= ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr);
241 if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel)
242 w->hshift= get_bits1(&s->gb);
249 static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){
250 MpegEncContext * const s= &w->s;
251 int xy, wrap, diff, type;
252 int16_t *A, *B, *C, *mot_val;
255 xy = s->block_index[0];
257 mot_val = s->current_picture.motion_val[0][xy];
259 A = s->current_picture.motion_val[0][xy - 1];
260 B = s->current_picture.motion_val[0][xy - wrap];
261 C = s->current_picture.motion_val[0][xy + 2 - wrap];
263 if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
264 diff= FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
269 type= get_bits1(&s->gb);
280 /* special case for first (slice) line */
281 if (s->first_slice_line) {
285 *px = mid_pred(A[0], B[0], C[0]);
286 *py = mid_pred(A[1], B[1], C[1]);
293 static inline int wmv2_decode_inter_block(Wmv2Context *w, int16_t *block, int n, int cbp){
294 MpegEncContext * const s= &w->s;
295 static const int sub_cbp_table[3]= {2,3,1};
299 s->block_last_index[n] = -1;
305 w->abt_type= decode012(&s->gb);
306 w->abt_type_table[n]= w->abt_type;
309 // const uint8_t *scantable= w->abt_scantable[w->abt_type-1].permutated;
310 const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable;
311 // const uint8_t *scantable= w->abt_type-1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable;
313 sub_cbp= sub_cbp_table[ decode012(&s->gb) ];
316 if (ff_msmpeg4_decode_block(s, block, n, 1, scantable) < 0)
321 if (ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0)
324 s->block_last_index[n] = 63;
328 return ff_msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated);
333 int ff_wmv2_decode_mb(MpegEncContext *s, int16_t block[6][64])
335 Wmv2Context * const w= (Wmv2Context*)s;
339 if(w->j_type) return 0;
341 if (s->pict_type == AV_PICTURE_TYPE_P) {
342 if (IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])) {
346 s->block_last_index[i] = -1;
347 s->mv_dir = MV_DIR_FORWARD;
348 s->mv_type = MV_TYPE_16X16;
356 code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3);
359 s->mb_intra = (~code & 0x40) >> 6;
364 code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
366 av_log(s->avctx, AV_LOG_ERROR, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
369 /* predict coded block pattern */
372 int val = ((code >> (5 - i)) & 1);
374 int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
378 cbp |= val << (5 - i);
384 wmv2_pred_motion(w, &mx, &my);
387 s->dsp.clear_blocks(s->block[0]);
388 if(s->per_mb_rl_table){
389 s->rl_table_index = decode012(&s->gb);
390 s->rl_chroma_table_index = s->rl_table_index;
393 if(w->abt_flag && w->per_mb_abt){
394 w->per_block_abt= get_bits1(&s->gb);
395 if(!w->per_block_abt)
396 w->abt_type= decode012(&s->gb);
401 if (wmv2_decode_motion(w, &mx, &my) < 0)
404 s->mv_dir = MV_DIR_FORWARD;
405 s->mv_type = MV_TYPE_16X16;
409 for (i = 0; i < 6; i++) {
410 if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0)
412 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
417 if (s->pict_type==AV_PICTURE_TYPE_P)
418 av_dlog(s->avctx, "%d%d ", s->inter_intra_pred, cbp);
419 av_dlog(s->avctx, "I at %d %d %d %06X\n", s->mb_x, s->mb_y,
420 ((cbp & 3) ? 1 : 0) +((cbp & 0x3C)? 2 : 0),
421 show_bits(&s->gb, 24));
422 s->ac_pred = get_bits1(&s->gb);
423 if(s->inter_intra_pred){
424 s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
425 av_dlog(s->avctx, "%d%d %d %d/",
426 s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
428 if(s->per_mb_rl_table && cbp){
429 s->rl_table_index = decode012(&s->gb);
430 s->rl_chroma_table_index = s->rl_table_index;
433 s->dsp.clear_blocks(s->block[0]);
434 for (i = 0; i < 6; i++) {
435 if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
437 av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
446 static av_cold int wmv2_decode_init(AVCodecContext *avctx){
447 Wmv2Context * const w= avctx->priv_data;
449 avctx->flags |= CODEC_FLAG_EMU_EDGE;
451 if(ff_msmpeg4_decode_init(avctx) < 0)
454 ff_wmv2_common_init(w);
456 ff_intrax8_common_init(&w->x8,&w->s);
461 static av_cold int wmv2_decode_end(AVCodecContext *avctx)
463 Wmv2Context *w = avctx->priv_data;
465 ff_intrax8_common_end(&w->x8);
466 return ff_h263_decode_end(avctx);
469 AVCodec ff_wmv2_decoder = {
471 .type = AVMEDIA_TYPE_VIDEO,
472 .id = AV_CODEC_ID_WMV2,
473 .priv_data_size = sizeof(Wmv2Context),
474 .init = wmv2_decode_init,
475 .close = wmv2_decode_end,
476 .decode = ff_h263_decode_frame,
477 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
478 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"),
479 .pix_fmts = ff_pixfmt_list_420,