Statistics
| Branch: | Revision:

ffmpeg / libavcodec / wmv2dec.c @ ce5e49b0

History | View | Annotate | Download (14.3 KB)

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 "h263.h"
25
#include "mathops.h"
26
#include "msmpeg4.h"
27
#include "msmpeg4data.h"
28
#include "intrax8.h"
29
#include "wmv2.h"
30

    
31

    
32
static void parse_mb_skip(Wmv2Context * w){
33
    int mb_x, mb_y;
34
    MpegEncContext * const s= &w->s;
35
    uint32_t * const mb_type= s->current_picture_ptr->mb_type;
36

    
37
    w->skip_type= get_bits(&s->gb, 2);
38
    switch(w->skip_type){
39
    case SKIP_TYPE_NONE:
40
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
41
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
42
                mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_16x16 | MB_TYPE_L0;
43
            }
44
        }
45
        break;
46
    case SKIP_TYPE_MPEG:
47
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
48
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
49
                mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
50
            }
51
        }
52
        break;
53
    case SKIP_TYPE_ROW:
54
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
55
            if(get_bits1(&s->gb)){
56
                for(mb_x=0; mb_x<s->mb_width; mb_x++){
57
                    mb_type[mb_y*s->mb_stride + mb_x]=  MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
58
                }
59
            }else{
60
                for(mb_x=0; mb_x<s->mb_width; mb_x++){
61
                    mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
62
                }
63
            }
64
        }
65
        break;
66
    case SKIP_TYPE_COL:
67
        for(mb_x=0; mb_x<s->mb_width; mb_x++){
68
            if(get_bits1(&s->gb)){
69
                for(mb_y=0; mb_y<s->mb_height; mb_y++){
70
                    mb_type[mb_y*s->mb_stride + mb_x]=  MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
71
                }
72
            }else{
73
                for(mb_y=0; mb_y<s->mb_height; mb_y++){
74
                    mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
75
                }
76
            }
77
        }
78
        break;
79
    }
80
}
81

    
82
static int decode_ext_header(Wmv2Context *w){
83
    MpegEncContext * const s= &w->s;
84
    GetBitContext gb;
85
    int fps;
86
    int code;
87

    
88
    if(s->avctx->extradata_size<4) return -1;
89

    
90
    init_get_bits(&gb, s->avctx->extradata, 32);
91

    
92
    fps                = get_bits(&gb, 5);
93
    s->bit_rate        = get_bits(&gb, 11)*1024;
94
    w->mspel_bit       = get_bits1(&gb);
95
    s->loop_filter     = get_bits1(&gb);
96
    w->abt_flag        = get_bits1(&gb);
97
    w->j_type_bit      = get_bits1(&gb);
98
    w->top_left_mv_flag= get_bits1(&gb);
99
    w->per_mb_rl_bit   = get_bits1(&gb);
100
    code               = get_bits(&gb, 3);
101

    
102
    if(code==0) return -1;
103

    
104
    s->slice_height = s->mb_height / code;
105

    
106
    if(s->avctx->debug&FF_DEBUG_PICT_INFO){
107
        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",
108
        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,
109
        code);
110
    }
111
    return 0;
112
}
113

    
114
int ff_wmv2_decode_picture_header(MpegEncContext * s)
115
{
116
    Wmv2Context * const w= (Wmv2Context*)s;
117
    int code;
118

    
119
    if(s->picture_number==0)
120
        decode_ext_header(w);
121

    
122
    s->pict_type = get_bits1(&s->gb) + 1;
123
    if(s->pict_type == AV_PICTURE_TYPE_I){
124
        code = get_bits(&s->gb, 7);
125
        av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code);
126
    }
127
    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
128
    if(s->qscale <= 0)
129
       return -1;
130

    
131
    return 0;
132
}
133

    
134
int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s)
135
{
136
    Wmv2Context * const w= (Wmv2Context*)s;
137

    
138
    if (s->pict_type == AV_PICTURE_TYPE_I) {
139
        if(w->j_type_bit) w->j_type= get_bits1(&s->gb);
140
        else              w->j_type= 0; //FIXME check
141

    
142
        if(!w->j_type){
143
            if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
144
            else                 s->per_mb_rl_table= 0;
145

    
146
            if(!s->per_mb_rl_table){
147
                s->rl_chroma_table_index = decode012(&s->gb);
148
                s->rl_table_index = decode012(&s->gb);
149
            }
150

    
151
            s->dc_table_index = get_bits1(&s->gb);
152
        }
153
        s->inter_intra_pred= 0;
154
        s->no_rounding = 1;
155
        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
156
            av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
157
                s->qscale,
158
                s->rl_chroma_table_index,
159
                s->rl_table_index,
160
                s->dc_table_index,
161
                s->per_mb_rl_table,
162
                w->j_type);
163
        }
164
    }else{
165
        int cbp_index;
166
        w->j_type=0;
167

    
168
        parse_mb_skip(w);
169
        cbp_index= decode012(&s->gb);
170
        if(s->qscale <= 10){
171
            int map[3]= {0,2,1};
172
            w->cbp_table_index= map[cbp_index];
173
        }else if(s->qscale <= 20){
174
            int map[3]= {1,0,2};
175
            w->cbp_table_index= map[cbp_index];
176
        }else{
177
            int map[3]= {2,1,0};
178
            w->cbp_table_index= map[cbp_index];
179
        }
180

    
181
        if(w->mspel_bit) s->mspel= get_bits1(&s->gb);
182
        else             s->mspel= 0; //FIXME check
183

    
184
        if(w->abt_flag){
185
            w->per_mb_abt= get_bits1(&s->gb)^1;
186
            if(!w->per_mb_abt){
187
                w->abt_type= decode012(&s->gb);
188
            }
189
        }
190

    
191
        if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
192
        else                 s->per_mb_rl_table= 0;
193

    
194
        if(!s->per_mb_rl_table){
195
            s->rl_table_index = decode012(&s->gb);
196
            s->rl_chroma_table_index = s->rl_table_index;
197
        }
198

    
199
        s->dc_table_index = get_bits1(&s->gb);
200
        s->mv_table_index = get_bits1(&s->gb);
201

    
202
        s->inter_intra_pred= 0;//(s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
203
        s->no_rounding ^= 1;
204

    
205
        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
206
            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_table_index,
208
                s->rl_chroma_table_index,
209
                s->dc_table_index,
210
                s->mv_table_index,
211
                s->per_mb_rl_table,
212
                s->qscale,
213
                s->mspel,
214
                w->per_mb_abt,
215
                w->abt_type,
216
                w->cbp_table_index,
217
                s->inter_intra_pred);
218
        }
219
    }
220
    s->esc3_level_length= 0;
221
    s->esc3_run_length= 0;
222

    
223
s->picture_number++; //FIXME ?
224

    
225

    
226
    if(w->j_type){
227
        ff_intrax8_decode_picture(&w->x8, 2*s->qscale, (s->qscale-1)|1 );
228
        return 1;
229
    }
230

    
231
    return 0;
232
}
233

    
234
static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){
235
    MpegEncContext * const s= &w->s;
236
    int ret;
237

    
238
    ret= ff_msmpeg4_decode_motion(s, mx_ptr, my_ptr);
239

    
240
    if(ret<0) return -1;
241

    
242
    if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel)
243
        w->hshift= get_bits1(&s->gb);
244
    else
245
        w->hshift= 0;
246

    
247
//printf("%d %d  ", *mx_ptr, *my_ptr);
248

    
249
    return 0;
250
}
251

    
252
static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){
253
    MpegEncContext * const s= &w->s;
254
    int xy, wrap, diff, type;
255
    int16_t *A, *B, *C, *mot_val;
256

    
257
    wrap = s->b8_stride;
258
    xy = s->block_index[0];
259

    
260
    mot_val = s->current_picture.motion_val[0][xy];
261

    
262
    A = s->current_picture.motion_val[0][xy - 1];
263
    B = s->current_picture.motion_val[0][xy - wrap];
264
    C = s->current_picture.motion_val[0][xy + 2 - wrap];
265

    
266
    if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
267
        diff= FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
268
    else
269
        diff=0;
270

    
271
    if(diff >= 8)
272
        type= get_bits1(&s->gb);
273
    else
274
        type= 2;
275

    
276
    if(type == 0){
277
        *px= A[0];
278
        *py= A[1];
279
    }else if(type == 1){
280
        *px= B[0];
281
        *py= B[1];
282
    }else{
283
        /* special case for first (slice) line */
284
        if (s->first_slice_line) {
285
            *px = A[0];
286
            *py = A[1];
287
        } else {
288
            *px = mid_pred(A[0], B[0], C[0]);
289
            *py = mid_pred(A[1], B[1], C[1]);
290
        }
291
    }
292

    
293
    return mot_val;
294
}
295

    
296
static inline int wmv2_decode_inter_block(Wmv2Context *w, DCTELEM *block, int n, int cbp){
297
    MpegEncContext * const s= &w->s;
298
    static const int sub_cbp_table[3]= {2,3,1};
299
    int sub_cbp;
300

    
301
    if(!cbp){
302
        s->block_last_index[n] = -1;
303

    
304
        return 0;
305
    }
306

    
307
    if(w->per_block_abt)
308
        w->abt_type= decode012(&s->gb);
309
    w->abt_type_table[n]= w->abt_type;
310

    
311
    if(w->abt_type){
312
//        const uint8_t *scantable= w->abt_scantable[w->abt_type-1].permutated;
313
        const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable;
314
//        const uint8_t *scantable= w->abt_type-1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable;
315

    
316
        sub_cbp= sub_cbp_table[ decode012(&s->gb) ];
317
//        printf("S%d", sub_cbp);
318

    
319
        if(sub_cbp&1){
320
            if (ff_msmpeg4_decode_block(s, block, n, 1, scantable) < 0)
321
                return -1;
322
        }
323

    
324
        if(sub_cbp&2){
325
            if (ff_msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0)
326
                return -1;
327
        }
328
        s->block_last_index[n] = 63;
329

    
330
        return 0;
331
    }else{
332
        return ff_msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated);
333
    }
334
}
335

    
336

    
337
int ff_wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
338
{
339
    Wmv2Context * const w= (Wmv2Context*)s;
340
    int cbp, code, i;
341
    uint8_t *coded_val;
342

    
343
    if(w->j_type) return 0;
344

    
345
    if (s->pict_type == AV_PICTURE_TYPE_P) {
346
        if(IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])){
347
            /* skip mb */
348
            s->mb_intra = 0;
349
            for(i=0;i<6;i++)
350
                s->block_last_index[i] = -1;
351
            s->mv_dir = MV_DIR_FORWARD;
352
            s->mv_type = MV_TYPE_16X16;
353
            s->mv[0][0][0] = 0;
354
            s->mv[0][0][1] = 0;
355
            s->mb_skipped = 1;
356
            w->hshift=0;
357
            return 0;
358
        }
359

    
360
        code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3);
361
        if (code < 0)
362
            return -1;
363
        s->mb_intra = (~code & 0x40) >> 6;
364

    
365
        cbp = code & 0x3f;
366
    } else {
367
        s->mb_intra = 1;
368
        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
369
        if (code < 0){
370
            av_log(s->avctx, AV_LOG_ERROR, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
371
            return -1;
372
        }
373
        /* predict coded block pattern */
374
        cbp = 0;
375
        for(i=0;i<6;i++) {
376
            int val = ((code >> (5 - i)) & 1);
377
            if (i < 4) {
378
                int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
379
                val = val ^ pred;
380
                *coded_val = val;
381
            }
382
            cbp |= val << (5 - i);
383
        }
384
    }
385

    
386
    if (!s->mb_intra) {
387
        int mx, my;
388
//printf("P at %d %d\n", s->mb_x, s->mb_y);
389
        wmv2_pred_motion(w, &mx, &my);
390

    
391
        if(cbp){
392
            s->dsp.clear_blocks(s->block[0]);
393
            if(s->per_mb_rl_table){
394
                s->rl_table_index = decode012(&s->gb);
395
                s->rl_chroma_table_index = s->rl_table_index;
396
            }
397

    
398
            if(w->abt_flag && w->per_mb_abt){
399
                w->per_block_abt= get_bits1(&s->gb);
400
                if(!w->per_block_abt)
401
                    w->abt_type= decode012(&s->gb);
402
            }else
403
                w->per_block_abt=0;
404
        }
405

    
406
        if (wmv2_decode_motion(w, &mx, &my) < 0)
407
            return -1;
408

    
409
        s->mv_dir = MV_DIR_FORWARD;
410
        s->mv_type = MV_TYPE_16X16;
411
        s->mv[0][0][0] = mx;
412
        s->mv[0][0][1] = my;
413

    
414
        for (i = 0; i < 6; i++) {
415
            if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0)
416
            {
417
                av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
418
                return -1;
419
            }
420
        }
421
    } else {
422
//if(s->pict_type==AV_PICTURE_TYPE_P)
423
//   printf("%d%d ", s->inter_intra_pred, cbp);
424
//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));
425
        s->ac_pred = get_bits1(&s->gb);
426
        if(s->inter_intra_pred){
427
            s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
428
//            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
429
        }
430
        if(s->per_mb_rl_table && cbp){
431
            s->rl_table_index = decode012(&s->gb);
432
            s->rl_chroma_table_index = s->rl_table_index;
433
        }
434

    
435
        s->dsp.clear_blocks(s->block[0]);
436
        for (i = 0; i < 6; i++) {
437
            if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
438
            {
439
                av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
440
                return -1;
441
            }
442
        }
443
    }
444

    
445
    return 0;
446
}
447

    
448
static av_cold int wmv2_decode_init(AVCodecContext *avctx){
449
    Wmv2Context * const w= avctx->priv_data;
450

    
451
    if(avctx->idct_algo==FF_IDCT_AUTO){
452
        avctx->idct_algo=FF_IDCT_WMV2;
453
    }
454

    
455
    if(ff_msmpeg4_decode_init(avctx) < 0)
456
        return -1;
457

    
458
    ff_wmv2_common_init(w);
459

    
460
    ff_intrax8_common_init(&w->x8,&w->s);
461

    
462
    return 0;
463
}
464

    
465
static av_cold int wmv2_decode_end(AVCodecContext *avctx)
466
{
467
    Wmv2Context *w = avctx->priv_data;
468

    
469
    ff_intrax8_common_end(&w->x8);
470
    return ff_h263_decode_end(avctx);
471
}
472

    
473
AVCodec ff_wmv2_decoder = {
474
    "wmv2",
475
    AVMEDIA_TYPE_VIDEO,
476
    CODEC_ID_WMV2,
477
    sizeof(Wmv2Context),
478
    wmv2_decode_init,
479
    NULL,
480
    wmv2_decode_end,
481
    ff_h263_decode_frame,
482
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
483
    .max_lowres = 3,
484
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 8"),
485
    .pix_fmts= ff_pixfmt_list_420,
486
};