Statistics
| Branch: | Revision:

ffmpeg / libavcodec / wmv2.c @ 7604246d

History | View | Annotate | Download (24.4 KB)

1
/*
2
 * Copyright (c) 2002 The FFmpeg Project.
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 */
19

    
20
#include "simple_idct.h"
21
 
22
#define SKIP_TYPE_NONE 0
23
#define SKIP_TYPE_MPEG 1
24
#define SKIP_TYPE_ROW  2
25
#define SKIP_TYPE_COL  3
26

    
27

    
28
typedef struct Wmv2Context{
29
    MpegEncContext s;
30
    int j_type_bit;
31
    int j_type;
32
    int flag3;
33
    int flag63;
34
    int abt_flag;
35
    int abt_type;
36
    int abt_type_table[6];
37
    int per_mb_abt;
38
    int per_block_abt;
39
    int mspel_bit;
40
    int cbp_table_index;
41
    int top_left_mv_flag;
42
    int per_mb_rl_bit;
43
    int skip_type;
44
    int hshift;
45
    
46
    ScanTable abt_scantable[2];
47
    DCTELEM abt_block2[6][64] __align8;
48
}Wmv2Context;
49

    
50
static void wmv2_common_init(Wmv2Context * w){
51
    MpegEncContext * const s= &w->s;
52
        
53
    ff_init_scantable(s, &w->abt_scantable[0], wmv2_scantableA);
54
    ff_init_scantable(s, &w->abt_scantable[1], wmv2_scantableB);
55
}
56

    
57
static int encode_ext_header(Wmv2Context *w){
58
    MpegEncContext * const s= &w->s;
59
    PutBitContext pb;
60
    int code;
61
        
62
    init_put_bits(&pb, s->avctx->extradata, s->avctx->extradata_size, NULL, NULL);
63

    
64
    put_bits(&pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29
65
    put_bits(&pb, 11, FFMIN(s->bit_rate/1024, 2047));
66
    
67
    put_bits(&pb, 1, w->mspel_bit=1);
68
    put_bits(&pb, 1, w->flag3=1);
69
    put_bits(&pb, 1, w->abt_flag=1);
70
    put_bits(&pb, 1, w->j_type_bit=1);
71
    put_bits(&pb, 1, w->top_left_mv_flag=0);
72
    put_bits(&pb, 1, w->per_mb_rl_bit=1);
73
    put_bits(&pb, 3, code=1);
74
    
75
    flush_put_bits(&pb);
76

    
77
    s->slice_height = s->mb_height / code;
78
    
79
    return 0;
80
}
81

    
82
#ifdef CONFIG_ENCODERS
83
static int wmv2_encode_init(AVCodecContext *avctx){
84
    Wmv2Context * const w= avctx->priv_data;
85
    
86
    if(MPV_encode_init(avctx) < 0)
87
        return -1;
88
    
89
    wmv2_common_init(w);
90

    
91
    avctx->extradata_size= 4;
92
    avctx->extradata= av_mallocz(avctx->extradata_size + 10);
93
    encode_ext_header(w);
94
    
95
    return 0;
96
}
97

    
98
static int wmv2_encode_end(AVCodecContext *avctx){
99
    Wmv2Context * const w= avctx->priv_data;
100
    
101
    if(MPV_encode_end(avctx) < 0)
102
        return -1;
103
    
104
    avctx->extradata_size= 0;
105
    av_freep(&avctx->extradata);
106
    
107
    return 0;
108
}
109

    
110
int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number)
111
{
112
    Wmv2Context * const w= (Wmv2Context*)s;
113

    
114
    put_bits(&s->pb, 1, s->pict_type - 1);
115
    if(s->pict_type == I_TYPE){
116
        put_bits(&s->pb, 7, 0);
117
    }
118
    put_bits(&s->pb, 5, s->qscale);
119

    
120
    s->dc_table_index = 1;
121
    s->mv_table_index = 1; /* only if P frame */
122
//    s->use_skip_mb_code = 1; /* only if P frame */
123
    s->per_mb_rl_table = 0;
124
    s->mspel= 0;
125
    w->per_mb_abt=0;
126
    w->abt_type=0;
127
    w->j_type=0;
128

    
129
    if (s->pict_type == I_TYPE) {
130
        if(w->j_type_bit) put_bits(&s->pb, 1, w->j_type);
131
        
132
        if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table);
133
        
134
        if(!s->per_mb_rl_table){
135
            code012(&s->pb, s->rl_chroma_table_index);
136
            code012(&s->pb, s->rl_table_index);
137
        }
138

    
139
        put_bits(&s->pb, 1, s->dc_table_index);
140

    
141
        s->inter_intra_pred= 0;
142
        s->no_rounding = 1;
143
    }else{
144
        int cbp_index;
145

    
146
        put_bits(&s->pb, 2, SKIP_TYPE_NONE);
147
        
148
        code012(&s->pb, cbp_index=0);
149
        if(s->qscale <= 10){
150
            int map[3]= {0,2,1};
151
            w->cbp_table_index= map[cbp_index];
152
        }else if(s->qscale <= 20){
153
            int map[3]= {1,0,2};
154
            w->cbp_table_index= map[cbp_index];
155
        }else{
156
            int map[3]= {2,1,0};
157
            w->cbp_table_index= map[cbp_index];
158
        }
159

    
160
        if(w->mspel_bit) put_bits(&s->pb, 1, s->mspel);
161
    
162
        if(w->abt_flag){
163
            put_bits(&s->pb, 1, w->per_mb_abt^1);
164
            if(!w->per_mb_abt){
165
                code012(&s->pb, w->abt_type);
166
            }
167
        }
168

    
169
        if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table);
170
        
171
        if(!s->per_mb_rl_table){
172
            code012(&s->pb, s->rl_table_index);
173
            s->rl_chroma_table_index = s->rl_table_index;
174
        }
175
        put_bits(&s->pb, 1, s->dc_table_index);
176
        put_bits(&s->pb, 1, s->mv_table_index);
177
    
178
        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
179
        s->no_rounding ^= 1;
180
    }
181
    s->esc3_level_length= 0;
182
    s->esc3_run_length= 0;
183

    
184
    return 0;
185
}
186

    
187
// nearly idential to wmv1 but thats just because we dont use the useless M$ crap features
188
// its duplicated here in case someone wants to add support for these carp features
189
void ff_wmv2_encode_mb(MpegEncContext * s, 
190
                       DCTELEM block[6][64],
191
                       int motion_x, int motion_y)
192
{
193
    Wmv2Context * const w= (Wmv2Context*)s;
194
    int cbp, coded_cbp, i;
195
    int pred_x, pred_y;
196
    uint8_t *coded_block;
197

    
198
    handle_slices(s);
199
    
200
    if (!s->mb_intra) {
201
        /* compute cbp */
202
        set_stat(ST_INTER_MB);
203
        cbp = 0;
204
        for (i = 0; i < 6; i++) {
205
            if (s->block_last_index[i] >= 0)
206
                cbp |= 1 << (5 - i);
207
        }
208
        
209
        put_bits(&s->pb, 
210
                 wmv2_inter_table[w->cbp_table_index][cbp + 64][1], 
211
                 wmv2_inter_table[w->cbp_table_index][cbp + 64][0]);
212

    
213
        /* motion vector */
214
        h263_pred_motion(s, 0, &pred_x, &pred_y);
215
        msmpeg4_encode_motion(s, motion_x - pred_x, 
216
                              motion_y - pred_y);
217
    } else {
218
        /* compute cbp */
219
        cbp = 0;
220
        coded_cbp = 0;
221
        for (i = 0; i < 6; i++) {
222
            int val, pred;
223
            val = (s->block_last_index[i] >= 1);
224
            cbp |= val << (5 - i);
225
            if (i < 4) {
226
                /* predict value for close blocks only for luma */
227
                pred = coded_block_pred(s, i, &coded_block);
228
                *coded_block = val;
229
                val = val ^ pred;
230
            }
231
            coded_cbp |= val << (5 - i);
232
        }
233
#if 0
234
        if (coded_cbp)
235
            printf("cbp=%x %x\n", cbp, coded_cbp);
236
#endif
237

    
238
        if (s->pict_type == I_TYPE) {
239
            set_stat(ST_INTRA_MB);
240
            put_bits(&s->pb, 
241
                     table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]);
242
        } else {
243
            put_bits(&s->pb, 
244
                     wmv2_inter_table[w->cbp_table_index][cbp][1], 
245
                     wmv2_inter_table[w->cbp_table_index][cbp][0]);
246
        }
247
        set_stat(ST_INTRA_MB);
248
        put_bits(&s->pb, 1, 0);        /* no AC prediction yet */
249
        if(s->inter_intra_pred){
250
            s->h263_aic_dir=0;
251
            put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
252
        }
253
    }
254

    
255
    for (i = 0; i < 6; i++) {
256
        msmpeg4_encode_block(s, block[i], i);
257
    }
258
}
259
#endif //CONFIG_ENCODERS
260

    
261
static void parse_mb_skip(Wmv2Context * w){
262
    int mb_x, mb_y;
263
    MpegEncContext * const s= &w->s;
264

    
265
    w->skip_type= get_bits(&s->gb, 2);
266
    switch(w->skip_type){
267
    case SKIP_TYPE_NONE:
268
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
269
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
270
                s->mb_type[mb_y*s->mb_width + mb_x]= 0;
271
            }
272
        }
273
        break;
274
    case SKIP_TYPE_MPEG:
275
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
276
            for(mb_x=0; mb_x<s->mb_width; mb_x++){
277
                s->mb_type[mb_y*s->mb_width + mb_x]= get_bits1(&s->gb) ? MB_TYPE_SKIPED : 0;
278
            }
279
        }
280
        break;
281
    case SKIP_TYPE_ROW:
282
        for(mb_y=0; mb_y<s->mb_height; mb_y++){
283
            if(get_bits1(&s->gb)){
284
                for(mb_x=0; mb_x<s->mb_width; mb_x++){
285
                    s->mb_type[mb_y*s->mb_width + mb_x]=  MB_TYPE_SKIPED;
286
                }
287
            }else{
288
                for(mb_x=0; mb_x<s->mb_width; mb_x++){
289
                    s->mb_type[mb_y*s->mb_width + mb_x]= get_bits1(&s->gb) ? MB_TYPE_SKIPED : 0;
290
                }
291
            }
292
        }
293
        break;
294
    case SKIP_TYPE_COL:
295
        for(mb_x=0; mb_x<s->mb_width; mb_x++){
296
            if(get_bits1(&s->gb)){
297
                for(mb_y=0; mb_y<s->mb_height; mb_y++){
298
                    s->mb_type[mb_y*s->mb_width + mb_x]=  MB_TYPE_SKIPED;
299
                }
300
            }else{
301
                for(mb_y=0; mb_y<s->mb_height; mb_y++){
302
                    s->mb_type[mb_y*s->mb_width + mb_x]= get_bits1(&s->gb) ? MB_TYPE_SKIPED : 0;
303
                }
304
            }
305
        }
306
        break;
307
    }
308
}
309

    
310
static int decode_ext_header(Wmv2Context *w){
311
    MpegEncContext * const s= &w->s;
312
    GetBitContext gb;
313
    int fps;
314
    int code;
315

    
316
    if(s->avctx->extradata_size<4) return -1;
317
    
318
    init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
319

    
320
    fps                = get_bits(&gb, 5);
321
    s->bit_rate        = get_bits(&gb, 11)*1024;
322
    w->mspel_bit       = get_bits1(&gb);
323
    w->flag3           = get_bits1(&gb);
324
    w->abt_flag        = get_bits1(&gb);
325
    w->j_type_bit      = get_bits1(&gb);
326
    w->top_left_mv_flag= get_bits1(&gb);
327
    w->per_mb_rl_bit   = get_bits1(&gb);
328
    code               = get_bits(&gb, 3);
329
    
330
    if(code==0) return -1;
331
            
332
    s->slice_height = s->mb_height / code;
333

    
334
    if(s->avctx->debug&FF_DEBUG_PICT_INFO){
335
        printf("fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, tl_mv_flag:%d, mbrl_bit:%d, code:%d, flag3:%d, slices:%d\n", 
336
        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, w->flag3, 
337
        code);
338
    }
339
    return 0;
340
}
341

    
342
int ff_wmv2_decode_picture_header(MpegEncContext * s)
343
{
344
    Wmv2Context * const w= (Wmv2Context*)s;
345
    int code, i;
346

    
347
#if 0
348
{
349
int i;
350
for(i=0; i<s->gb.size*8; i++)
351
    printf("%d", get_bits1(&s->gb));
352
//    get_bits1(&s->gb);
353
printf("END\n");
354
return -1;
355
}
356
#endif
357
    if(s->picture_number==0)
358
        decode_ext_header(w);
359

    
360
    s->pict_type = get_bits(&s->gb, 1) + 1;
361
    if(s->pict_type == I_TYPE){
362
        code = get_bits(&s->gb, 7);
363
        printf("I7:%X/\n", code);
364
    }
365
    s->qscale = get_bits(&s->gb, 5);
366

    
367
    if (s->pict_type == I_TYPE) {
368
        if(w->j_type_bit) w->j_type= get_bits1(&s->gb);
369
        else              w->j_type= 0; //FIXME check
370
        
371
        if(!w->j_type){
372
            if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
373
            else                 s->per_mb_rl_table= 0;
374
        
375
            if(!s->per_mb_rl_table){
376
                s->rl_chroma_table_index = decode012(&s->gb);
377
                s->rl_table_index = decode012(&s->gb);
378
            }
379

    
380
            s->dc_table_index = get_bits1(&s->gb);
381
        }
382
        s->inter_intra_pred= 0;
383
        s->no_rounding = 1;
384
        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
385
            printf("qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n", 
386
                s->qscale,
387
                s->rl_chroma_table_index,
388
                s->rl_table_index, 
389
                s->dc_table_index,
390
                s->per_mb_rl_table,
391
                w->j_type);
392
        }
393
    }else{
394
        int cbp_index;
395
        w->j_type=0;
396

    
397
        parse_mb_skip(w);
398
        cbp_index= decode012(&s->gb);
399
        if(s->qscale <= 10){
400
            int map[3]= {0,2,1};
401
            w->cbp_table_index= map[cbp_index];
402
        }else if(s->qscale <= 20){
403
            int map[3]= {1,0,2};
404
            w->cbp_table_index= map[cbp_index];
405
        }else{
406
            int map[3]= {2,1,0};
407
            w->cbp_table_index= map[cbp_index];
408
        }
409

    
410
        if(w->mspel_bit) s->mspel= get_bits1(&s->gb);
411
        else             s->mspel= 0; //FIXME check
412
    
413
        if(w->abt_flag){
414
            w->per_mb_abt= get_bits1(&s->gb)^1;
415
            if(!w->per_mb_abt){
416
                w->abt_type= decode012(&s->gb);
417
            }
418
        }
419

    
420
        if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
421
        else                 s->per_mb_rl_table= 0;
422
        
423
        if(!s->per_mb_rl_table){
424
            s->rl_table_index = decode012(&s->gb);
425
            s->rl_chroma_table_index = s->rl_table_index;
426
        }
427

    
428
        s->dc_table_index = get_bits1(&s->gb);
429
        s->mv_table_index = get_bits1(&s->gb);
430
    
431
        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
432
        s->no_rounding ^= 1;
433
        
434
        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
435
            printf("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", 
436
                s->rl_table_index, 
437
                s->rl_chroma_table_index, 
438
                s->dc_table_index,
439
                s->mv_table_index,
440
                s->per_mb_rl_table,
441
                s->qscale,
442
                s->mspel,
443
                w->per_mb_abt,
444
                w->abt_type,
445
                w->cbp_table_index,
446
                s->inter_intra_pred);
447
        }
448
    }
449
    s->esc3_level_length= 0;
450
    s->esc3_run_length= 0;
451
    
452
    if(s->avctx->debug&FF_DEBUG_SKIP){
453
        for(i=0; i<s->mb_num; i++){
454
            if(i%s->mb_width==0) printf("\n");
455
            printf("%d", s->mb_type[i]);
456
        }
457
    }
458
s->picture_number++; //FIXME ?
459

    
460

    
461
//    if(w->j_type)
462
//        return wmv2_decode_j_picture(w); //FIXME
463

    
464
    if(w->j_type){
465
        printf("J-type picture isnt supported\n");
466
        return -1;
467
    }
468

    
469
    return 0;
470
}
471

    
472
static void ff_wmv2_decode_init(MpegEncContext *s){
473
}
474

    
475
static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){
476
    MpegEncContext * const s= &w->s;
477
    int ret;
478
   
479
    ret= msmpeg4_decode_motion(s, mx_ptr, my_ptr);
480
   
481
    if(ret<0) return -1;
482
   
483
    if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel)
484
        w->hshift= get_bits1(&s->gb);
485
    else 
486
        w->hshift= 0;
487

    
488
//printf("%d %d  ", *mx_ptr, *my_ptr);
489
   
490
    return 0;
491
}
492

    
493
static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){
494
    MpegEncContext * const s= &w->s;
495
    int xy, wrap, diff, type;
496
    int16_t *A, *B, *C, *mot_val;
497

    
498
    wrap = s->block_wrap[0];
499
    xy = s->block_index[0];
500

    
501
    mot_val = s->motion_val[xy];
502

    
503
    A = s->motion_val[xy - 1];
504
    B = s->motion_val[xy - wrap];
505
    C = s->motion_val[xy + 2 - wrap];
506
    
507
    diff= FFMAX(ABS(A[0] - B[0]), ABS(A[1] - B[1]));
508
    
509
    if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag && diff >= 8)
510
        type= get_bits1(&s->gb);
511
    else
512
        type= 2;
513
    
514
    if(type == 0){
515
        *px= A[0];
516
        *py= A[1];
517
    }else if(type == 1){
518
        *px= B[0];
519
        *py= B[1];
520
    }else{
521
        /* special case for first (slice) line */
522
        if (s->first_slice_line) {
523
            *px = A[0];
524
            *py = A[1];
525
        } else {
526
            *px = mid_pred(A[0], B[0], C[0]);
527
            *py = mid_pred(A[1], B[1], C[1]);
528
        }
529
    }
530

    
531
    return mot_val;
532
}
533

    
534
static inline int wmv2_decode_inter_block(Wmv2Context *w, DCTELEM *block, int n, int cbp){
535
    MpegEncContext * const s= &w->s;
536
    static const int sub_cbp_table[3]= {2,3,1};
537
    int sub_cbp;
538

    
539
    if(!cbp){ 
540
        s->block_last_index[n] = -1;
541

    
542
        return 0;
543
    }
544
    
545
    if(w->per_block_abt)
546
        w->abt_type= decode012(&s->gb);
547
#if 0
548
    if(w->per_block_abt)
549
        printf("B%d", w->abt_type);
550
#endif
551
    w->abt_type_table[n]= w->abt_type;
552

    
553
    if(w->abt_type){
554
//        const uint8_t *scantable= w->abt_scantable[w->abt_type-1].permutated;
555
        const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable;
556
//        const uint8_t *scantable= w->abt_type-1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable;
557

    
558
        sub_cbp= sub_cbp_table[ decode012(&s->gb) ];
559
//        printf("S%d", sub_cbp);
560

    
561
        if(sub_cbp&1){
562
            if (msmpeg4_decode_block(s, block, n, 1, scantable) < 0)
563
                return -1;
564
        }
565
        
566
        if(sub_cbp&2){
567
            if (msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0)
568
                return -1;
569
        }
570
        s->block_last_index[n] = 63;
571

    
572
        return 0;
573
    }else{
574
        return msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated);
575
    }
576
}
577

    
578
static void wmv2_add_block(Wmv2Context *w, DCTELEM *block1, uint8_t *dst, int stride, int n){
579
    MpegEncContext * const s= &w->s;
580
    uint8_t temp[2][64];
581

    
582
    switch(w->abt_type_table[n]){
583
    case 0:
584
        if (s->block_last_index[n] >= 0) {
585
            s->idct_add (dst, stride, block1);
586
        }
587
        break;
588
    case 1:
589
        simple_idct84_add(dst           , stride, block1);
590
        simple_idct84_add(dst + 4*stride, stride, w->abt_block2[n]);
591
        memset(w->abt_block2[n], 0, 64*sizeof(DCTELEM));
592
        break;
593
    case 2:
594
        simple_idct48_add(dst           , stride, block1);
595
        simple_idct48_add(dst + 4       , stride, w->abt_block2[n]);
596
        memset(w->abt_block2[n], 0, 64*sizeof(DCTELEM));
597
        break;
598
    default:
599
        fprintf(stderr, "internal error in WMV2 abt\n");
600
    }
601
}
602

    
603
void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block1[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr){
604
    Wmv2Context * const w= (Wmv2Context*)s;
605

    
606
    wmv2_add_block(w, block1[0], dest_y                    , s->linesize, 0);
607
    wmv2_add_block(w, block1[1], dest_y + 8                , s->linesize, 1);
608
    wmv2_add_block(w, block1[2], dest_y +     8*s->linesize, s->linesize, 2);
609
    wmv2_add_block(w, block1[3], dest_y + 8 + 8*s->linesize, s->linesize, 3);
610
    
611
    if(s->flags&CODEC_FLAG_GRAY) return;
612
    
613
    wmv2_add_block(w, block1[4], dest_cb                   , s->uvlinesize, 4);
614
    wmv2_add_block(w, block1[5], dest_cr                   , s->uvlinesize, 5);
615
}
616

    
617
void ff_mspel_motion(MpegEncContext *s,
618
                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
619
                               uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
620
                               int motion_x, int motion_y, int h)
621
{
622
    Wmv2Context * const w= (Wmv2Context*)s;
623
    uint8_t *ptr;
624
    int dxy, offset, mx, my, src_x, src_y, v_edge_pos, linesize, uvlinesize;
625
    int emu=0;
626
    
627
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
628
    dxy = 2*dxy + w->hshift;
629
    src_x = s->mb_x * 16 + (motion_x >> 1);
630
    src_y = s->mb_y * 16 + (motion_y >> 1);
631
                
632
    /* WARNING: do no forget half pels */
633
    v_edge_pos = s->v_edge_pos;
634
    src_x = clip(src_x, -16, s->width);
635
    src_y = clip(src_y, -16, s->height);
636
    linesize   = s->linesize;
637
    uvlinesize = s->uvlinesize;
638
    ptr = ref_picture[0] + (src_y * linesize) + src_x;
639

    
640
    if(s->flags&CODEC_FLAG_EMU_EDGE){
641
        if(src_x<1 || src_y<1 || src_x + 17  >= s->h_edge_pos
642
                              || src_y + h+1 >= v_edge_pos){
643
            ff_emulated_edge_mc(s, ptr - 1 - s->linesize, s->linesize, 19, 19, 
644
                             src_x-1, src_y-1, s->h_edge_pos, s->v_edge_pos);
645
            ptr= s->edge_emu_buffer + 1 + s->linesize;
646
            emu=1;
647
        }
648
    }
649

    
650
    s->dsp.put_mspel_pixels_tab[dxy](dest_y             , ptr             , linesize);
651
    s->dsp.put_mspel_pixels_tab[dxy](dest_y+8           , ptr+8           , linesize);
652
    s->dsp.put_mspel_pixels_tab[dxy](dest_y  +8*linesize, ptr  +8*linesize, linesize);
653
    s->dsp.put_mspel_pixels_tab[dxy](dest_y+8+8*linesize, ptr+8+8*linesize, linesize);
654

    
655
    if(s->flags&CODEC_FLAG_GRAY) return;
656

    
657
    if (s->out_format == FMT_H263) {
658
        dxy = 0;
659
        if ((motion_x & 3) != 0)
660
            dxy |= 1;
661
        if ((motion_y & 3) != 0)
662
            dxy |= 2;
663
        mx = motion_x >> 2;
664
        my = motion_y >> 2;
665
    } else {
666
        mx = motion_x / 2;
667
        my = motion_y / 2;
668
        dxy = ((my & 1) << 1) | (mx & 1);
669
        mx >>= 1;
670
        my >>= 1;
671
    }
672
    
673
    src_x = s->mb_x * 8 + mx;
674
    src_y = s->mb_y * 8 + my;
675
    src_x = clip(src_x, -8, s->width >> 1);
676
    if (src_x == (s->width >> 1))
677
        dxy &= ~1;
678
    src_y = clip(src_y, -8, s->height >> 1);
679
    if (src_y == (s->height >> 1))
680
        dxy &= ~2;
681
    offset = (src_y * uvlinesize) + src_x;
682
    ptr = ref_picture[1] + offset;
683
    if(emu){
684
        ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, 
685
                         src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
686
        ptr= s->edge_emu_buffer;
687
    }
688
    pix_op[1][dxy](dest_cb, ptr, uvlinesize, h >> 1);
689

    
690
    ptr = ref_picture[2] + offset;
691
    if(emu){
692
        ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, 
693
                         src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
694
        ptr= s->edge_emu_buffer;
695
    }
696
    pix_op[1][dxy](dest_cr, ptr, uvlinesize, h >> 1);
697
}
698

    
699

    
700
static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
701
{
702
    Wmv2Context * const w= (Wmv2Context*)s;
703
    int cbp, code, i;
704
    uint8_t *coded_val;
705

    
706
    if(w->j_type) return 0;
707
    
708
    s->error_status_table[s->mb_x + s->mb_y*s->mb_width]= 0;
709
    
710
    if (s->pict_type == P_TYPE) {
711
        if(s->mb_type[s->mb_y * s->mb_width + s->mb_x]&MB_TYPE_SKIPED){
712
            /* skip mb */
713
            s->mb_intra = 0;
714
            for(i=0;i<6;i++)
715
                s->block_last_index[i] = -1;
716
            s->mv_dir = MV_DIR_FORWARD;
717
            s->mv_type = MV_TYPE_16X16;
718
            s->mv[0][0][0] = 0;
719
            s->mv[0][0][1] = 0;
720
            s->mb_skiped = 1;
721
            return 0;
722
        }
723

    
724
        code = get_vlc2(&s->gb, mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3);
725
        if (code < 0)
726
            return -1;
727
        s->mb_intra = (~code & 0x40) >> 6;
728
            
729
        cbp = code & 0x3f;
730
    } else {
731
        s->mb_intra = 1;
732
        code = get_vlc2(&s->gb, mb_intra_vlc.table, MB_INTRA_VLC_BITS, 2);
733
        if (code < 0){
734
            fprintf(stderr, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
735
            return -1;
736
        }
737
        /* predict coded block pattern */
738
        cbp = 0;
739
        for(i=0;i<6;i++) {
740
            int val = ((code >> (5 - i)) & 1);
741
            if (i < 4) {
742
                int pred = coded_block_pred(s, i, &coded_val);
743
                val = val ^ pred;
744
                *coded_val = val;
745
            }
746
            cbp |= val << (5 - i);
747
        }
748
    }
749

    
750
    if (!s->mb_intra) {
751
        int mx, my;
752
//printf("P at %d %d\n", s->mb_x, s->mb_y);
753
        wmv2_pred_motion(w, &mx, &my);
754
        
755
        if(cbp){
756
            if(s->per_mb_rl_table){
757
                s->rl_table_index = decode012(&s->gb);
758
                s->rl_chroma_table_index = s->rl_table_index;
759
            }
760

    
761
            if(w->abt_flag && w->per_mb_abt){
762
                w->per_block_abt= get_bits1(&s->gb);
763
                if(!w->per_block_abt)
764
                    w->abt_type= decode012(&s->gb);
765
            }else
766
                w->per_block_abt=0;
767
        }
768
        
769
        if (wmv2_decode_motion(w, &mx, &my) < 0)
770
            return -1;
771

    
772
        s->mv_dir = MV_DIR_FORWARD;
773
        s->mv_type = MV_TYPE_16X16;
774
        s->mv[0][0][0] = mx;
775
        s->mv[0][0][1] = my;
776

    
777
        for (i = 0; i < 6; i++) {
778
            if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0)
779
            {
780
                fprintf(stderr,"\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
781
                return -1;
782
            }
783
        }    
784
    } else {
785
//if(s->pict_type==P_TYPE)
786
//   printf("%d%d ", s->inter_intra_pred, cbp);
787
//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));
788
        s->ac_pred = get_bits1(&s->gb);
789
        if(s->inter_intra_pred){
790
            s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
791
//            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
792
        }
793
        if(s->per_mb_rl_table && cbp){
794
            s->rl_table_index = decode012(&s->gb);
795
            s->rl_chroma_table_index = s->rl_table_index;
796
        }
797
    
798
        for (i = 0; i < 6; i++) {
799
            if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
800
            {
801
                fprintf(stderr,"\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
802
                return -1;
803
            }
804
        }    
805
    }
806

    
807
    return 0;
808
}
809

    
810
static int wmv2_decode_init(AVCodecContext *avctx){
811
    Wmv2Context * const w= avctx->priv_data;
812
    
813
    if(ff_h263_decode_init(avctx) < 0)
814
        return -1;
815
    
816
    wmv2_common_init(w);
817
    
818
    return 0;
819
}
820

    
821
AVCodec wmv2_decoder = {
822
    "wmv2",
823
    CODEC_TYPE_VIDEO,
824
    CODEC_ID_WMV2,
825
    sizeof(Wmv2Context),
826
    wmv2_decode_init,
827
    NULL,
828
    ff_h263_decode_end,
829
    ff_h263_decode_frame,
830
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
831
};
832

    
833
#ifdef CONFIG_ENCODERS
834
AVCodec wmv2_encoder = {
835
    "wmv2",
836
    CODEC_TYPE_VIDEO,
837
    CODEC_ID_WMV2,
838
    sizeof(Wmv2Context),
839
    wmv2_encode_init,
840
    MPV_encode_picture,
841
    MPV_encode_end,
842
};
843
#endif