Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpeg12.c @ 8f2ab833

History | View | Annotate | Download (94.4 KB)

1
/*
2
 * MPEG1 codec / MPEG2 decoder
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> 
5
 *
6
 * This library 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 of the License, or (at your option) any later version.
10
 *
11
 * This library 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 this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
/**
22
 * @file mpeg12.c
23
 * MPEG1/2 codec
24
 */
25
 
26
//#define DEBUG
27
#include "avcodec.h"
28
#include "dsputil.h"
29
#include "mpegvideo.h"
30

    
31
#include "mpeg12data.h"
32

    
33
//#undef NDEBUG
34
//#include <assert.h>
35

    
36

    
37
/* Start codes. */
38
#define SEQ_END_CODE                0x000001b7
39
#define SEQ_START_CODE                0x000001b3
40
#define GOP_START_CODE                0x000001b8
41
#define PICTURE_START_CODE        0x00000100
42
#define SLICE_MIN_START_CODE        0x00000101
43
#define SLICE_MAX_START_CODE        0x000001af
44
#define EXT_START_CODE                0x000001b5
45
#define USER_START_CODE                0x000001b2
46

    
47
#define DC_VLC_BITS 9
48
#define MV_VLC_BITS 9
49
#define MBINCR_VLC_BITS 9
50
#define MB_PAT_VLC_BITS 9
51
#define MB_PTYPE_VLC_BITS 6
52
#define MB_BTYPE_VLC_BITS 6
53
#define TEX_VLC_BITS 9
54

    
55
#ifdef CONFIG_ENCODERS
56
static void mpeg1_encode_block(MpegEncContext *s, 
57
                         DCTELEM *block, 
58
                         int component);
59
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code);    // RAL: f_code parameter added
60
#endif //CONFIG_ENCODERS
61
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
62
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
63
                              DCTELEM *block, 
64
                              int n);
65
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
66
                              DCTELEM *block, 
67
                              int n);
68
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
69
                                        DCTELEM *block, 
70
                                        int n);
71
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
72
                                    DCTELEM *block, 
73
                                    int n);
74
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
75
static void exchange_uv(MpegEncContext *s);
76

    
77
#ifdef HAVE_XVMC
78
extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
79
extern int XVMC_field_end(MpegEncContext *s);
80
extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
81
extern void XVMC_init_block(MpegEncContext *s);//set s->block
82
#endif
83

    
84
#ifdef CONFIG_ENCODERS
85
static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
86
static uint8_t fcode_tab[MAX_MV*2+1];
87

    
88
static uint32_t uni_mpeg1_ac_vlc_bits[64*64*2];
89
static uint8_t  uni_mpeg1_ac_vlc_len [64*64*2];
90

    
91
/* simple include everything table for dc, first byte is bits number next 3 are code*/
92
static uint32_t mpeg1_lum_dc_uni[512];
93
static uint32_t mpeg1_chr_dc_uni[512];
94

    
95
static uint8_t mpeg1_index_run[2][64];
96
static int8_t mpeg1_max_level[2][64];
97
#endif //CONFIG_ENCODERS
98

    
99
static void init_2d_vlc_rl(RLTable *rl)
100
{
101
    int i;
102
    
103
    init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2, 
104
             &rl->table_vlc[0][1], 4, 2,
105
             &rl->table_vlc[0][0], 4, 2);
106

    
107
    
108
    rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
109
    for(i=0; i<rl->vlc.table_size; i++){
110
        int code= rl->vlc.table[i][0];
111
        int len = rl->vlc.table[i][1];
112
        int level, run;
113
    
114
        if(len==0){ // illegal code
115
            run= 65;
116
            level= MAX_LEVEL;
117
        }else if(len<0){ //more bits needed
118
            run= 0;
119
            level= code;
120
        }else{
121
            if(code==rl->n){ //esc
122
                run= 65;
123
                level= 0;
124
            }else if(code==rl->n+1){ //eob
125
                run= 0;
126
                level= 127;
127
            }else{
128
                run=   rl->table_run  [code] + 1;
129
                level= rl->table_level[code];
130
            }
131
        }
132
        rl->rl_vlc[0][i].len= len;
133
        rl->rl_vlc[0][i].level= level;
134
        rl->rl_vlc[0][i].run= run;
135
    }
136
}
137

    
138
#ifdef CONFIG_ENCODERS
139
static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni_ac_vlc_len){
140
    int i;
141

    
142
    for(i=0; i<128; i++){
143
        int level= i-64;
144
        int run;
145
        for(run=0; run<64; run++){
146
            int len, bits, code;
147
            
148
            int alevel= ABS(level);
149
            int sign= (level>>31)&1;
150

    
151
            if (alevel > rl->max_level[0][run])
152
                code= 111; /*rl->n*/
153
            else
154
                code= rl->index_run[0][run] + alevel - 1;
155

    
156
            if (code < 111 /* rl->n */) {
157
                    /* store the vlc & sign at once */
158
                len=   mpeg1_vlc[code][1]+1;
159
                bits= (mpeg1_vlc[code][0]<<1) + sign;
160
            } else {
161
                len=  mpeg1_vlc[111/*rl->n*/][1]+6;
162
                bits= mpeg1_vlc[111/*rl->n*/][0]<<6;
163

    
164
                bits|= run;
165
                if (alevel < 128) {
166
                    bits<<=8; len+=8;
167
                    bits|= level & 0xff;
168
                } else {
169
                    bits<<=16; len+=16;
170
                    bits|= level & 0xff;
171
                    if (level < 0) {
172
                        bits|= 0x8001 + level + 255;
173
                    } else {
174
                        bits|= level & 0xffff;
175
                    }
176
                }
177
            }
178

    
179
            uni_ac_vlc_bits[UNI_AC_ENC_INDEX(run, i)]= bits;
180
            uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
181
        }
182
    }
183
}
184

    
185
static void put_header(MpegEncContext *s, int header)
186
{
187
    align_put_bits(&s->pb);
188
    put_bits(&s->pb, 16, header>>16);
189
    put_bits(&s->pb, 16, header&0xFFFF);
190
}
191

    
192
/* put sequence header if needed */
193
static void mpeg1_encode_sequence_header(MpegEncContext *s)
194
{
195
        unsigned int vbv_buffer_size;
196
        unsigned int fps, v;
197
        int n, i;
198
        uint64_t time_code;
199
        float best_aspect_error= 1E10;
200
        float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
201
        int constraint_parameter_flag;
202
        
203
        if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
204
        
205
        if (s->current_picture.key_frame) {
206
            /* mpeg1 header repeated every gop */
207
            put_header(s, SEQ_START_CODE);
208
            
209
            /* search closest frame rate */
210
            {
211
                int i, dmin, d;
212
                s->frame_rate_index = 0;
213
                dmin = 0x7fffffff;
214
                for(i=1;i<14;i++) {
215
                    if(s->avctx->strict_std_compliance >= 0 && i>=9) break;
216
                     
217
                    d = abs(MPEG1_FRAME_RATE_BASE*(int64_t)s->avctx->frame_rate/s->avctx->frame_rate_base - frame_rate_tab[i]);
218
                    if (d < dmin) {
219
                        dmin = d;
220
                        s->frame_rate_index = i;
221
                    }
222
                }
223
            }
224
 
225
            put_bits(&s->pb, 12, s->width);
226
            put_bits(&s->pb, 12, s->height);
227
            
228
            for(i=1; i<15; i++){
229
                float error= aspect_ratio;
230
                if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
231
                    error-= 1.0/mpeg1_aspect[i];
232
                else
233
                    error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
234
             
235
                error= ABS(error);
236
                
237
                if(error < best_aspect_error){
238
                    best_aspect_error= error;
239
                    s->aspect_ratio_info= i;
240
                }
241
            }
242
            
243
            put_bits(&s->pb, 4, s->aspect_ratio_info);
244
            put_bits(&s->pb, 4, s->frame_rate_index);
245
            
246
            if(s->avctx->rc_max_rate){
247
                v = (s->avctx->rc_max_rate + 399) / 400;
248
                if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
249
                    v = 0x3ffff;
250
            }else{
251
                v= 0x3FFFF;
252
            }
253

    
254
            if(s->avctx->rc_buffer_size)
255
                vbv_buffer_size = s->avctx->rc_buffer_size;
256
            else
257
                /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
258
                vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
259
            vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
260

    
261
            put_bits(&s->pb, 18, v & 0x3FFFF);
262
            put_bits(&s->pb, 1, 1); /* marker */
263
            put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
264

    
265
            constraint_parameter_flag= 
266
                s->width <= 768 && s->height <= 576 && 
267
                s->mb_width * s->mb_height <= 396 &&
268
                s->mb_width * s->mb_height * frame_rate_tab[s->frame_rate_index] <= MPEG1_FRAME_RATE_BASE*396*25 &&
269
                frame_rate_tab[s->frame_rate_index] <= MPEG1_FRAME_RATE_BASE*30 &&
270
                vbv_buffer_size <= 20 &&
271
                v <= 1856000/400 &&
272
                s->codec_id == CODEC_ID_MPEG1VIDEO;
273
                
274
            put_bits(&s->pb, 1, constraint_parameter_flag);
275
            
276
            ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
277
            ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
278

    
279
            if(s->codec_id == CODEC_ID_MPEG2VIDEO){
280
                put_header(s, EXT_START_CODE);
281
                put_bits(&s->pb, 4, 1); //seq ext
282
                put_bits(&s->pb, 1, 0); //esc
283
                put_bits(&s->pb, 3, 4); //profile
284
                put_bits(&s->pb, 4, 8); //level
285
                put_bits(&s->pb, 1, s->progressive_sequence);
286
                put_bits(&s->pb, 2, 1); //chroma format 4:2:0
287
                put_bits(&s->pb, 2, 0); //horizontal size ext
288
                put_bits(&s->pb, 2, 0); //vertical size ext
289
                put_bits(&s->pb, 12, v>>18); //bitrate ext
290
                put_bits(&s->pb, 1, 1); //marker
291
                put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
292
                put_bits(&s->pb, 1, s->low_delay);
293
                put_bits(&s->pb, 2, 0); // frame_rate_ext_n
294
                put_bits(&s->pb, 5, 0); // frame_rate_ext_d
295
            }
296
            
297
            put_header(s, GOP_START_CODE);
298
            put_bits(&s->pb, 1, 0); /* do drop frame */
299
            /* time code : we must convert from the real frame rate to a
300
               fake mpeg frame rate in case of low frame rate */
301
            fps = (frame_rate_tab[s->frame_rate_index] + MPEG1_FRAME_RATE_BASE/2)/ MPEG1_FRAME_RATE_BASE;
302
            time_code = s->fake_picture_number;
303
            s->gop_picture_number = s->fake_picture_number;
304
            put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
305
            put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
306
            put_bits(&s->pb, 1, 1);
307
            put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
308
            put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
309
            put_bits(&s->pb, 1, 0); /* closed gop */
310
            put_bits(&s->pb, 1, 0); /* broken link */
311
        }
312

    
313
        if (s->avctx->frame_rate < (23 * s->avctx->frame_rate_base) && s->picture_number > 0) {
314
            /* insert empty P pictures to slow down to the desired
315
               frame rate. Each fake pictures takes about 20 bytes */
316
            fps = frame_rate_tab[s->frame_rate_index];
317
            n = av_rescale((int64_t)s->picture_number * s->avctx->frame_rate_base, fps, s->avctx->frame_rate) / MPEG1_FRAME_RATE_BASE - 1;
318
            while (s->fake_picture_number < n) {
319
                mpeg1_skip_picture(s, s->fake_picture_number - 
320
                                   s->gop_picture_number); 
321
                s->fake_picture_number++;
322
            }
323

    
324
        }
325
}
326

    
327
static inline void encode_mb_skip_run(MpegEncContext *s, int run){
328
    while (run >= 33) {
329
        put_bits(&s->pb, 11, 0x008);
330
        run -= 33;
331
    }
332
    put_bits(&s->pb, mbAddrIncrTable[run][1], 
333
             mbAddrIncrTable[run][0]);
334
}
335

    
336
/* insert a fake P picture */
337
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
338
{
339
    assert(s->codec_id == CODEC_ID_MPEG1VIDEO); // mpeg2 can do these repeat things
340

    
341
    /* mpeg1 picture header */
342
    put_header(s, PICTURE_START_CODE);
343
    /* temporal reference */
344
    put_bits(&s->pb, 10, pict_num & 0x3ff); 
345
    
346
    put_bits(&s->pb, 3, P_TYPE);
347
    put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
348
    
349
    put_bits(&s->pb, 1, 1); /* integer coordinates */
350
    put_bits(&s->pb, 3, 1); /* forward_f_code */
351
    
352
    put_bits(&s->pb, 1, 0); /* extra bit picture */
353
    
354
    /* only one slice */
355
    put_header(s, SLICE_MIN_START_CODE);
356
    put_bits(&s->pb, 5, 1); /* quantizer scale */
357
    put_bits(&s->pb, 1, 0); /* slice extra information */
358
    
359
    encode_mb_skip_run(s, 0);
360
    
361
    /* empty macroblock */
362
    put_bits(&s->pb, 3, 1); /* motion only */
363
    
364
    /* zero motion x & y */
365
    put_bits(&s->pb, 1, 1); 
366
    put_bits(&s->pb, 1, 1); 
367

    
368
    /* output a number of empty slice */
369
    encode_mb_skip_run(s, s->mb_width * s->mb_height - 2);
370
    
371
    /* empty macroblock */
372
    put_bits(&s->pb, 3, 1); /* motion only */
373
    
374
    /* zero motion x & y */
375
    put_bits(&s->pb, 1, 1); 
376
    put_bits(&s->pb, 1, 1); 
377
}
378
#endif //CONFIG_ENCODERS
379

    
380
static void common_init(MpegEncContext *s)
381
{
382
    s->y_dc_scale_table=
383
    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
384
}
385

    
386
void ff_mpeg1_clean_buffers(MpegEncContext *s){
387
    s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
388
    s->last_dc[1] = s->last_dc[0];
389
    s->last_dc[2] = s->last_dc[0];
390
    memset(s->last_mv, 0, sizeof(s->last_mv));
391
}
392

    
393
#ifdef CONFIG_ENCODERS
394

    
395
void ff_mpeg1_encode_slice_header(MpegEncContext *s){
396
    put_header(s, SLICE_MIN_START_CODE + s->mb_y);
397
    put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
398
    put_bits(&s->pb, 1, 0); /* slice extra information */
399
}
400

    
401
void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
402
{
403
    mpeg1_encode_sequence_header(s);
404

    
405
    /* mpeg1 picture header */
406
    put_header(s, PICTURE_START_CODE);
407
    /* temporal reference */
408

    
409
    // RAL: s->picture_number instead of s->fake_picture_number
410
    put_bits(&s->pb, 10, (s->picture_number - 
411
                          s->gop_picture_number) & 0x3ff); 
412
    s->fake_picture_number++;
413
    
414
    put_bits(&s->pb, 3, s->pict_type);
415

    
416
    s->vbv_delay_ptr= s->pb.buf + get_bit_count(&s->pb)/8;
417
    put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
418
    
419
    // RAL: Forward f_code also needed for B frames
420
    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
421
        put_bits(&s->pb, 1, 0); /* half pel coordinates */
422
        if(s->codec_id == CODEC_ID_MPEG1VIDEO)
423
            put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
424
        else
425
            put_bits(&s->pb, 3, 7); /* forward_f_code */
426
    }
427
    
428
    // RAL: Backward f_code necessary for B frames
429
    if (s->pict_type == B_TYPE) {
430
        put_bits(&s->pb, 1, 0); /* half pel coordinates */
431
        if(s->codec_id == CODEC_ID_MPEG1VIDEO)
432
            put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
433
        else
434
            put_bits(&s->pb, 3, 7); /* backward_f_code */
435
    }
436

    
437
    put_bits(&s->pb, 1, 0); /* extra bit picture */
438

    
439
    s->frame_pred_frame_dct = 1;
440
    if(s->codec_id == CODEC_ID_MPEG2VIDEO){
441
        put_header(s, EXT_START_CODE);
442
        put_bits(&s->pb, 4, 8); //pic ext
443
        if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
444
            put_bits(&s->pb, 4, s->f_code);
445
            put_bits(&s->pb, 4, s->f_code);
446
        }else{
447
            put_bits(&s->pb, 8, 255);
448
        }
449
        if (s->pict_type == B_TYPE) {
450
            put_bits(&s->pb, 4, s->b_code);
451
            put_bits(&s->pb, 4, s->b_code);
452
        }else{
453
            put_bits(&s->pb, 8, 255);
454
        }
455
        put_bits(&s->pb, 2, s->intra_dc_precision);
456
        put_bits(&s->pb, 2, s->picture_structure= PICT_FRAME);
457
        if (s->progressive_sequence) {
458
            put_bits(&s->pb, 1, 0); /* no repeat */
459
        } else {
460
            put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
461
        }
462
        /* XXX: optimize the generation of this flag with entropy
463
           measures */
464
        s->frame_pred_frame_dct = s->progressive_sequence;
465
        
466
        put_bits(&s->pb, 1, s->frame_pred_frame_dct);
467
        put_bits(&s->pb, 1, s->concealment_motion_vectors);
468
        put_bits(&s->pb, 1, s->q_scale_type);
469
        put_bits(&s->pb, 1, s->intra_vlc_format);
470
        put_bits(&s->pb, 1, s->alternate_scan);
471
        put_bits(&s->pb, 1, s->repeat_first_field);
472
        put_bits(&s->pb, 1, s->chroma_420_type=1);
473
        s->progressive_frame = s->progressive_sequence;
474
        put_bits(&s->pb, 1, s->progressive_frame);
475
        put_bits(&s->pb, 1, 0); //composite_display_flag
476
    }
477
    if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
478
        int i;
479

    
480
        put_header(s, USER_START_CODE);
481
        for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
482
            put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
483
        }
484
    }
485
    
486
    s->mb_y=0;
487
    ff_mpeg1_encode_slice_header(s);
488
}
489

    
490
static inline void put_mb_modes(MpegEncContext *s, int n, int bits, 
491
                                int has_mv, int field_motion)
492
{
493
    put_bits(&s->pb, n, bits);
494
    if (!s->frame_pred_frame_dct) {
495
        if (has_mv) 
496
            put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
497
        put_bits(&s->pb, 1, s->interlaced_dct);
498
    }
499
}
500

    
501
void mpeg1_encode_mb(MpegEncContext *s,
502
                     DCTELEM block[6][64],
503
                     int motion_x, int motion_y)
504
{
505
    int i, cbp;
506
    const int mb_x = s->mb_x;
507
    const int mb_y = s->mb_y;
508
    const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
509

    
510
    /* compute cbp */
511
    cbp = 0;
512
    for(i=0;i<6;i++) {
513
        if (s->block_last_index[i] >= 0)
514
            cbp |= 1 << (5 - i);
515
    }
516
    
517
    if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) && 
518
        ((s->pict_type == P_TYPE && s->mv_type == MV_TYPE_16X16 && (motion_x | motion_y) == 0) ||
519
        (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
520
        ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
521
        s->mb_skip_run++;
522
        s->qscale -= s->dquant;
523
        s->skip_count++;
524
        s->misc_bits++;
525
        s->last_bits++;
526
        if(s->pict_type == P_TYPE){
527
            s->last_mv[0][1][0]= s->last_mv[0][0][0]= 
528
            s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
529
        }
530
    } else {
531
        if(first_mb){
532
            assert(s->mb_skip_run == 0);
533
            encode_mb_skip_run(s, s->mb_x);
534
        }else{
535
            encode_mb_skip_run(s, s->mb_skip_run);
536
        }
537
        
538
        if (s->pict_type == I_TYPE) {
539
            if(s->dquant && cbp){
540
                put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
541
                put_bits(&s->pb, 5, s->qscale);
542
            }else{
543
                put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
544
                s->qscale -= s->dquant;
545
            }
546
            s->misc_bits+= get_bits_diff(s);
547
            s->i_count++;
548
        } else if (s->mb_intra) {
549
            if(s->dquant && cbp){
550
                put_mb_modes(s, 6, 0x01, 0, 0);
551
                put_bits(&s->pb, 5, s->qscale);
552
            }else{
553
                put_mb_modes(s, 5, 0x03, 0, 0);
554
                s->qscale -= s->dquant;
555
            }
556
            s->misc_bits+= get_bits_diff(s);
557
            s->i_count++;
558
            memset(s->last_mv, 0, sizeof(s->last_mv));
559
        } else if (s->pict_type == P_TYPE) { 
560
            if(s->mv_type == MV_TYPE_16X16){
561
                if (cbp != 0) {
562
                    if ((motion_x|motion_y) == 0) {
563
                        if(s->dquant){
564
                            put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
565
                            put_bits(&s->pb, 5, s->qscale);
566
                        }else{
567
                            put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
568
                        }
569
                        s->misc_bits+= get_bits_diff(s);
570
                    } else {
571
                        if(s->dquant){
572
                            put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
573
                            put_bits(&s->pb, 5, s->qscale);
574
                        }else{
575
                            put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
576
                        }
577
                        s->misc_bits+= get_bits_diff(s);
578
                        mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);    // RAL: f_code parameter added
579
                        mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);    // RAL: f_code parameter added
580
                        s->mv_bits+= get_bits_diff(s);
581
                    }
582
                } else {
583
                    put_bits(&s->pb, 3, 1); /* motion only */
584
                    if (!s->frame_pred_frame_dct)
585
                        put_bits(&s->pb, 2, 2); /* motion_type: frame */
586
                    s->misc_bits+= get_bits_diff(s);
587
                    mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);    // RAL: f_code parameter added
588
                    mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);    // RAL: f_code parameter added
589
                    s->qscale -= s->dquant;
590
                    s->mv_bits+= get_bits_diff(s);
591
                }
592
                s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
593
                s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
594
            }else{
595
                assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
596

    
597
                if (cbp) {
598
                    if(s->dquant){
599
                        put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
600
                        put_bits(&s->pb, 5, s->qscale);
601
                    }else{
602
                        put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
603
                    }
604
                } else {
605
                    put_bits(&s->pb, 3, 1); /* motion only */
606
                    put_bits(&s->pb, 2, 1); /* motion_type: field */
607
                    s->qscale -= s->dquant;
608
                }
609
                s->misc_bits+= get_bits_diff(s);
610
                for(i=0; i<2; i++){
611
                    put_bits(&s->pb, 1, s->field_select[0][i]);
612
                    mpeg1_encode_motion(s, s->mv[0][i][0] -  s->last_mv[0][i][0]    , s->f_code);
613
                    mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
614
                    s->last_mv[0][i][0]=   s->mv[0][i][0];
615
                    s->last_mv[0][i][1]= 2*s->mv[0][i][1];
616
                }
617
                s->mv_bits+= get_bits_diff(s);
618
            }
619
            if(cbp)
620
                put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
621
            s->f_count++;
622
        } else{  
623
            static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi
624

    
625
            if(s->mv_type == MV_TYPE_16X16){
626
                if (cbp){    // With coded bloc pattern
627
                    if (s->dquant) {
628
                        if(s->mv_dir == MV_DIR_FORWARD)
629
                            put_mb_modes(s, 6, 3, 1, 0);
630
                        else
631
                            put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 0);
632
                        put_bits(&s->pb, 5, s->qscale);
633
                    } else {
634
                        put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 0);
635
                    }
636
                }else{    // No coded bloc pattern
637
                    put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
638
                    if (!s->frame_pred_frame_dct)
639
                        put_bits(&s->pb, 2, 2); /* motion_type: frame */
640
                    s->qscale -= s->dquant;
641
                }
642
                s->misc_bits += get_bits_diff(s);
643
                if (s->mv_dir&MV_DIR_FORWARD){
644
                    mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); 
645
                    mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); 
646
                    s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
647
                    s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
648
                    s->f_count++;
649
                }
650
                if (s->mv_dir&MV_DIR_BACKWARD){
651
                    mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); 
652
                    mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); 
653
                    s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
654
                    s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
655
                    s->b_count++;
656
                }
657
            }else{
658
                assert(s->mv_type == MV_TYPE_FIELD);
659
                assert(!s->frame_pred_frame_dct);
660
                if (cbp){    // With coded bloc pattern
661
                    if (s->dquant) {
662
                        if(s->mv_dir == MV_DIR_FORWARD)
663
                            put_mb_modes(s, 6, 3, 1, 1);
664
                        else
665
                            put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 1);
666
                        put_bits(&s->pb, 5, s->qscale);
667
                    } else {
668
                        put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 1);
669
                    }
670
                }else{    // No coded bloc pattern
671
                    put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
672
                    put_bits(&s->pb, 2, 1); /* motion_type: field */
673
                    s->qscale -= s->dquant;
674
                }
675
                s->misc_bits += get_bits_diff(s);
676
                if (s->mv_dir&MV_DIR_FORWARD){
677
                    for(i=0; i<2; i++){
678
                        put_bits(&s->pb, 1, s->field_select[0][i]);
679
                        mpeg1_encode_motion(s, s->mv[0][i][0] -  s->last_mv[0][i][0]    , s->f_code);
680
                        mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
681
                        s->last_mv[0][i][0]=   s->mv[0][i][0];
682
                        s->last_mv[0][i][1]= 2*s->mv[0][i][1];
683
                    }
684
                    s->f_count++;
685
                }
686
                if (s->mv_dir&MV_DIR_BACKWARD){
687
                    for(i=0; i<2; i++){
688
                        put_bits(&s->pb, 1, s->field_select[1][i]);
689
                        mpeg1_encode_motion(s, s->mv[1][i][0] -  s->last_mv[1][i][0]    , s->b_code);
690
                        mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
691
                        s->last_mv[1][i][0]=   s->mv[1][i][0];
692
                        s->last_mv[1][i][1]= 2*s->mv[1][i][1];
693
                    }
694
                    s->b_count++;
695
                }
696
            }
697
            s->mv_bits += get_bits_diff(s);
698
            if(cbp)
699
                put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
700
        }
701
        for(i=0;i<6;i++) {
702
            if (cbp & (1 << (5 - i))) {
703
                mpeg1_encode_block(s, block[i], i);
704
            }
705
        }
706
        s->mb_skip_run = 0;
707
        if(s->mb_intra)
708
            s->i_tex_bits+= get_bits_diff(s);
709
        else
710
            s->p_tex_bits+= get_bits_diff(s);
711
    }
712
}
713

    
714
// RAL: Parameter added: f_or_b_code
715
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
716
{
717
    int code, bit_size, l, m, bits, range, sign;
718

    
719
    if (val == 0) {
720
        /* zero vector */
721
        code = 0;
722
        put_bits(&s->pb,
723
                 mbMotionVectorTable[0][1], 
724
                 mbMotionVectorTable[0][0]); 
725
    } else {
726
        bit_size = f_or_b_code - 1;
727
        range = 1 << bit_size;
728
        /* modulo encoding */
729
        l = 16 * range;
730
        m = 2 * l;
731
        if (val < -l) {
732
            val += m;
733
        } else if (val >= l) {
734
            val -= m;
735
        }
736

    
737
        if (val >= 0) {
738
            val--;
739
            code = (val >> bit_size) + 1;
740
            bits = val & (range - 1);
741
            sign = 0;
742
        } else {
743
            val = -val;
744
            val--;
745
            code = (val >> bit_size) + 1;
746
            bits = val & (range - 1);
747
            sign = 1;
748
        }
749

    
750
        assert(code > 0 && code <= 16);
751

    
752
        put_bits(&s->pb,
753
                 mbMotionVectorTable[code][1], 
754
                 mbMotionVectorTable[code][0]); 
755

    
756
        put_bits(&s->pb, 1, sign);
757
        if (bit_size > 0) {
758
            put_bits(&s->pb, bit_size, bits);
759
        }
760
    }
761
}
762

    
763
void ff_mpeg1_encode_init(MpegEncContext *s)
764
{
765
    static int done=0;
766

    
767
    common_init(s);
768

    
769
    if(!done){
770
        int f_code;
771
        int mv;
772
        int i;
773

    
774
        done=1;
775
        init_rl(&rl_mpeg1);
776

    
777
        for(i=0; i<64; i++)
778
        {
779
                mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
780
                mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
781
        }
782
        
783
        init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_bits, uni_mpeg1_ac_vlc_len);
784

    
785
        /* build unified dc encoding tables */
786
        for(i=-255; i<256; i++)
787
        {
788
                int adiff, index;
789
                int bits, code;
790
                int diff=i;
791

    
792
                adiff = ABS(diff);
793
                if(diff<0) diff--;
794
                index = vlc_dc_table[adiff];
795

    
796
                bits= vlc_dc_lum_bits[index] + index;
797
                code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
798
                mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
799
                
800
                bits= vlc_dc_chroma_bits[index] + index;
801
                code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
802
                mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
803
        }
804

    
805
        mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
806

    
807
        for(f_code=1; f_code<=MAX_FCODE; f_code++){
808
            for(mv=-MAX_MV; mv<=MAX_MV; mv++){
809
                int len;
810

    
811
                if(mv==0) len= mbMotionVectorTable[0][1];
812
                else{
813
                    int val, bit_size, range, code;
814

    
815
                    bit_size = s->f_code - 1;
816
                    range = 1 << bit_size;
817

    
818
                    val=mv;
819
                    if (val < 0) 
820
                        val = -val;
821
                    val--;
822
                    code = (val >> bit_size) + 1;
823
                    if(code<17){
824
                        len= mbMotionVectorTable[code][1] + 1 + bit_size;
825
                    }else{
826
                        len= mbMotionVectorTable[16][1] + 2 + bit_size;
827
                    }
828
                }
829

    
830
                mv_penalty[f_code][mv+MAX_MV]= len;
831
            }
832
        }
833
        
834

    
835
        for(f_code=MAX_FCODE; f_code>0; f_code--){
836
            for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
837
                fcode_tab[mv+MAX_MV]= f_code;
838
            }
839
        }
840
    }
841
    s->me.mv_penalty= mv_penalty;
842
    s->fcode_tab= fcode_tab;
843
    if(s->codec_id == CODEC_ID_MPEG1VIDEO){
844
        s->min_qcoeff=-255;
845
        s->max_qcoeff= 255;
846
    }else{
847
        s->min_qcoeff=-2047;
848
        s->max_qcoeff= 2047;
849
    }
850
    s->intra_ac_vlc_length=
851
    s->inter_ac_vlc_length=
852
    s->intra_ac_vlc_last_length=
853
    s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
854
}
855

    
856
static inline void encode_dc(MpegEncContext *s, int diff, int component)
857
{
858
    if (component == 0) {
859
        put_bits(
860
            &s->pb, 
861
            mpeg1_lum_dc_uni[diff+255]&0xFF,
862
            mpeg1_lum_dc_uni[diff+255]>>8);
863
    } else {
864
        put_bits(
865
            &s->pb, 
866
            mpeg1_chr_dc_uni[diff+255]&0xFF,
867
            mpeg1_chr_dc_uni[diff+255]>>8);
868
    }
869
}
870

    
871
static void mpeg1_encode_block(MpegEncContext *s, 
872
                               DCTELEM *block, 
873
                               int n)
874
{
875
    int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
876
    int code, component;
877
//    RLTable *rl = &rl_mpeg1;
878

    
879
    last_index = s->block_last_index[n];
880

    
881
    /* DC coef */
882
    if (s->mb_intra) {
883
        component = (n <= 3 ? 0 : n - 4 + 1);
884
        dc = block[0]; /* overflow is impossible */
885
        diff = dc - s->last_dc[component];
886
        encode_dc(s, diff, component);
887
        s->last_dc[component] = dc;
888
        i = 1;
889
/*
890
        if (s->intra_vlc_format)
891
            rl = &rl_mpeg2;
892
        else
893
            rl = &rl_mpeg1;
894
*/
895
    } else {
896
        /* encode the first coefficient : needs to be done here because
897
           it is handled slightly differently */
898
        level = block[0];
899
        if (abs(level) == 1) {
900
                code = ((uint32_t)level >> 31); /* the sign bit */
901
                put_bits(&s->pb, 2, code | 0x02);
902
                i = 1;
903
        } else {
904
            i = 0;
905
            last_non_zero = -1;
906
            goto next_coef;
907
        }
908
    }
909

    
910
    /* now quantify & encode AC coefs */
911
    last_non_zero = i - 1;
912

    
913
    for(;i<=last_index;i++) {
914
        j = s->intra_scantable.permutated[i];
915
        level = block[j];
916
    next_coef:
917
#if 0
918
        if (level != 0)
919
            dprintf("level[%d]=%d\n", i, level);
920
#endif            
921
        /* encode using VLC */
922
        if (level != 0) {
923
            run = i - last_non_zero - 1;
924
            
925
            alevel= level;
926
            MASK_ABS(sign, alevel)
927
            sign&=1;
928

    
929
//            code = get_rl_index(rl, 0, run, alevel);
930
            if (alevel <= mpeg1_max_level[0][run]){
931
                code= mpeg1_index_run[0][run] + alevel - 1;
932
                    /* store the vlc & sign at once */
933
                put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
934
            } else {
935
                /* escape seems to be pretty rare <5% so i dont optimize it */
936
                put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
937
                /* escape: only clip in this case */
938
                put_bits(&s->pb, 6, run);
939
                if(s->codec_id == CODEC_ID_MPEG1VIDEO){
940
                    if (alevel < 128) {
941
                        put_bits(&s->pb, 8, level & 0xff);
942
                    } else {
943
                        if (level < 0) {
944
                            put_bits(&s->pb, 16, 0x8001 + level + 255);
945
                        } else {
946
                            put_bits(&s->pb, 16, level & 0xffff);
947
                        }
948
                    }
949
                }else{
950
                    put_bits(&s->pb, 12, level & 0xfff);
951
                }
952
            }
953
            last_non_zero = i;
954
        }
955
    }
956
    /* end of block */
957
    put_bits(&s->pb, 2, 0x2);
958
}
959
#endif //CONFIG_ENCODERS
960

    
961
/******************************************/
962
/* decoding */
963

    
964
static VLC dc_lum_vlc;
965
static VLC dc_chroma_vlc;
966
static VLC mv_vlc;
967
static VLC mbincr_vlc;
968
static VLC mb_ptype_vlc;
969
static VLC mb_btype_vlc;
970
static VLC mb_pat_vlc;
971

    
972
static void init_vlcs()
973
{
974
    static int done = 0;
975

    
976
    if (!done) {
977
        done = 1;
978

    
979
        init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12, 
980
                 vlc_dc_lum_bits, 1, 1,
981
                 vlc_dc_lum_code, 2, 2);
982
        init_vlc(&dc_chroma_vlc,  DC_VLC_BITS, 12, 
983
                 vlc_dc_chroma_bits, 1, 1,
984
                 vlc_dc_chroma_code, 2, 2);
985
        init_vlc(&mv_vlc, MV_VLC_BITS, 17, 
986
                 &mbMotionVectorTable[0][1], 2, 1,
987
                 &mbMotionVectorTable[0][0], 2, 1);
988
        init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36, 
989
                 &mbAddrIncrTable[0][1], 2, 1,
990
                 &mbAddrIncrTable[0][0], 2, 1);
991
        init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63, 
992
                 &mbPatTable[0][1], 2, 1,
993
                 &mbPatTable[0][0], 2, 1);
994
        
995
        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7, 
996
                 &table_mb_ptype[0][1], 2, 1,
997
                 &table_mb_ptype[0][0], 2, 1);
998
        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11, 
999
                 &table_mb_btype[0][1], 2, 1,
1000
                 &table_mb_btype[0][0], 2, 1);
1001
        init_rl(&rl_mpeg1);
1002
        init_rl(&rl_mpeg2);
1003

    
1004
        init_2d_vlc_rl(&rl_mpeg1);
1005
        init_2d_vlc_rl(&rl_mpeg2);
1006
    }
1007
}
1008

    
1009
static inline int get_dmv(MpegEncContext *s)
1010
{
1011
    if(get_bits1(&s->gb)) 
1012
        return 1 - (get_bits1(&s->gb) << 1);
1013
    else
1014
        return 0;
1015
}
1016

    
1017
static inline int get_qscale(MpegEncContext *s)
1018
{
1019
    int qscale = get_bits(&s->gb, 5);
1020
    if (s->q_scale_type) {
1021
        return non_linear_qscale[qscale];
1022
    } else {
1023
        return qscale << 1;
1024
    }
1025
}
1026

    
1027
/* motion type (for mpeg2) */
1028
#define MT_FIELD 1
1029
#define MT_FRAME 2
1030
#define MT_16X8  2
1031
#define MT_DMV   3
1032

    
1033
static int mpeg_decode_mb(MpegEncContext *s,
1034
                          DCTELEM block[6][64])
1035
{
1036
    int i, j, k, cbp, val, mb_type, motion_type;
1037
    
1038
    dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
1039

    
1040
    assert(s->mb_skiped==0);
1041

    
1042
    if (s->mb_skip_run-- != 0) {
1043
        if(s->pict_type == I_TYPE){
1044
            av_log(s->avctx, AV_LOG_ERROR, "skiped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1045
            return -1;
1046
        }
1047
    
1048
        /* skip mb */
1049
        s->mb_intra = 0;
1050
        for(i=0;i<6;i++)
1051
            s->block_last_index[i] = -1;
1052
        s->mv_type = MV_TYPE_16X16;
1053
        if (s->pict_type == P_TYPE) {
1054
            /* if P type, zero motion vector is implied */
1055
            s->mv_dir = MV_DIR_FORWARD;
1056
            s->mv[0][0][0] = s->mv[0][0][1] = 0;
1057
            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1058
            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1059
            s->mb_skiped = 1;
1060
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
1061
        } else {
1062
            /* if B type, reuse previous vectors and directions */
1063
            s->mv[0][0][0] = s->last_mv[0][0][0];
1064
            s->mv[0][0][1] = s->last_mv[0][0][1];
1065
            s->mv[1][0][0] = s->last_mv[1][0][0];
1066
            s->mv[1][0][1] = s->last_mv[1][0][1];
1067

    
1068
            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= 
1069
                s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1] | MB_TYPE_SKIP;
1070
//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
1071

    
1072
            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) 
1073
                s->mb_skiped = 1;
1074
        }
1075

    
1076
        return 0;
1077
    }
1078

    
1079
    switch(s->pict_type) {
1080
    default:
1081
    case I_TYPE:
1082
        if (get_bits1(&s->gb) == 0) {
1083
            if (get_bits1(&s->gb) == 0){
1084
                av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
1085
                return -1;
1086
            }
1087
            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
1088
        } else {
1089
            mb_type = MB_TYPE_INTRA;
1090
        }
1091
        break;
1092
    case P_TYPE:
1093
        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
1094
        if (mb_type < 0){
1095
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
1096
            return -1;
1097
        }
1098
        mb_type = ptype2mb_type[ mb_type ];
1099
        break;
1100
    case B_TYPE:
1101
        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
1102
        if (mb_type < 0){
1103
            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
1104
            return -1;
1105
        }
1106
        mb_type = btype2mb_type[ mb_type ];
1107
        break;
1108
    }
1109
    dprintf("mb_type=%x\n", mb_type);
1110
//    motion_type = 0; /* avoid warning */
1111
    if (IS_INTRA(mb_type)) {
1112
        /* compute dct type */
1113
        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1114
            !s->frame_pred_frame_dct) {
1115
            s->interlaced_dct = get_bits1(&s->gb);
1116
        }
1117

    
1118
        if (IS_QUANT(mb_type))
1119
            s->qscale = get_qscale(s);
1120
        
1121
        if (s->concealment_motion_vectors) {
1122
            /* just parse them */
1123
            if (s->picture_structure != PICT_FRAME) 
1124
                skip_bits1(&s->gb); /* field select */
1125
            
1126
            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] = 
1127
                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
1128
            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] = 
1129
                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
1130

    
1131
            skip_bits1(&s->gb); /* marker */
1132
        }else
1133
            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
1134
        s->mb_intra = 1;
1135
#ifdef HAVE_XVMC
1136
        //one 1 we memcpy blocks in xvmcvideo
1137
        if(s->avctx->xvmc_acceleration > 1){
1138
            XVMC_pack_pblocks(s,-1);//inter are always full blocks
1139
            if(s->swap_uv){
1140
                exchange_uv(s);
1141
            }
1142
        }
1143
#endif
1144

    
1145
        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1146
            for(i=0;i<6;i++) {
1147
                if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
1148
                    return -1;
1149
            }
1150
        } else {
1151
            for(i=0;i<6;i++) {
1152
                if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
1153
                    return -1;
1154
            }
1155
        }
1156
    } else {
1157
        if (mb_type & MB_TYPE_ZERO_MV){
1158
            assert(mb_type & MB_TYPE_CBP);
1159

    
1160
            /* compute dct type */
1161
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1162
                !s->frame_pred_frame_dct) {
1163
                s->interlaced_dct = get_bits1(&s->gb);
1164
            }
1165

    
1166
            if (IS_QUANT(mb_type))
1167
                s->qscale = get_qscale(s);
1168

    
1169
            s->mv_dir = MV_DIR_FORWARD;
1170
            s->mv_type = MV_TYPE_16X16;
1171
            s->last_mv[0][0][0] = 0;
1172
            s->last_mv[0][0][1] = 0;
1173
            s->last_mv[0][1][0] = 0;
1174
            s->last_mv[0][1][1] = 0;
1175
            s->mv[0][0][0] = 0;
1176
            s->mv[0][0][1] = 0;
1177
        }else{
1178
            assert(mb_type & MB_TYPE_L0L1);
1179
//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
1180
            /* get additionnal motion vector type */
1181
            if (s->frame_pred_frame_dct) 
1182
                motion_type = MT_FRAME;
1183
            else{
1184
                motion_type = get_bits(&s->gb, 2);
1185
            }
1186

    
1187
            /* compute dct type */
1188
            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
1189
                !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
1190
                s->interlaced_dct = get_bits1(&s->gb);
1191
            }
1192

    
1193
            if (IS_QUANT(mb_type))
1194
                s->qscale = get_qscale(s);
1195

    
1196
            /* motion vectors */
1197
            s->mv_dir = 0;
1198
            for(i=0;i<2;i++) {
1199
                if (USES_LIST(mb_type, i)) {
1200
                    s->mv_dir |= (MV_DIR_FORWARD >> i);
1201
                    dprintf("motion_type=%d\n", motion_type);
1202
                    switch(motion_type) {
1203
                    case MT_FRAME: /* or MT_16X8 */
1204
                        if (s->picture_structure == PICT_FRAME) {
1205
                            /* MT_FRAME */
1206
                            mb_type |= MB_TYPE_16x16; 
1207
                            s->mv_type = MV_TYPE_16X16;
1208
                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] = 
1209
                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
1210
                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] = 
1211
                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
1212
                            /* full_pel: only for mpeg1 */
1213
                            if (s->full_pel[i]){
1214
                                s->mv[i][0][0] <<= 1;
1215
                                s->mv[i][0][1] <<= 1;
1216
                            }
1217
                        } else {
1218
                            /* MT_16X8 */
1219
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; 
1220
                            s->mv_type = MV_TYPE_16X8;
1221
                            for(j=0;j<2;j++) {
1222
                                s->field_select[i][j] = get_bits1(&s->gb);
1223
                                for(k=0;k<2;k++) {
1224
                                    val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1225
                                                             s->last_mv[i][j][k]);
1226
                                    s->last_mv[i][j][k] = val;
1227
                                    s->mv[i][j][k] = val;
1228
                                }
1229
                            }
1230
                        }
1231
                        break;
1232
                    case MT_FIELD:
1233
                        s->mv_type = MV_TYPE_FIELD;
1234
                        if (s->picture_structure == PICT_FRAME) {
1235
                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; 
1236
                            for(j=0;j<2;j++) {
1237
                                s->field_select[i][j] = get_bits1(&s->gb);
1238
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
1239
                                                         s->last_mv[i][j][0]);
1240
                                s->last_mv[i][j][0] = val;
1241
                                s->mv[i][j][0] = val;
1242
                                dprintf("fmx=%d\n", val);
1243
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1244
                                                         s->last_mv[i][j][1] >> 1);
1245
                                s->last_mv[i][j][1] = val << 1;
1246
                                s->mv[i][j][1] = val;
1247
                                dprintf("fmy=%d\n", val);
1248
                            }
1249
                        } else {
1250
                            mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; 
1251
                            s->field_select[i][0] = get_bits1(&s->gb);
1252
                            for(k=0;k<2;k++) {
1253
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
1254
                                                         s->last_mv[i][0][k]);
1255
                                s->last_mv[i][0][k] = val;
1256
                                s->last_mv[i][1][k] = val;
1257
                                s->mv[i][0][k] = val;
1258
                            }
1259
                        }
1260
                        break;
1261
                    case MT_DMV:
1262
                        {
1263
                            int dmx, dmy, mx, my, m;
1264

    
1265
                            mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
1266
                                                    s->last_mv[i][0][0]);
1267
                            s->last_mv[i][0][0] = mx;
1268
                            s->last_mv[i][1][0] = mx;
1269
                            dmx = get_dmv(s);
1270
                            my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
1271
                                                    s->last_mv[i][0][1] >> 1);
1272
                            dmy = get_dmv(s);
1273
                            s->mv_type = MV_TYPE_DMV;
1274

    
1275

    
1276
                            s->last_mv[i][0][1] = my<<1;
1277
                            s->last_mv[i][1][1] = my<<1;
1278

    
1279
                            s->mv[i][0][0] = mx;
1280
                            s->mv[i][0][1] = my;
1281
                            s->mv[i][1][0] = mx;//not used
1282
                            s->mv[i][1][1] = my;//not used
1283

    
1284
                            if (s->picture_structure == PICT_FRAME) {
1285
                                mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; 
1286

    
1287
                                //m = 1 + 2 * s->top_field_first;
1288
                                m = s->top_field_first ? 1 : 3;
1289

    
1290
                                /* top -> top pred */
1291
                                s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1292
                                s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1293
                                m = 4 - m;
1294
                                s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1295
                                s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1296
                            } else {
1297
                                mb_type |= MB_TYPE_16x16;
1298

    
1299
                                s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1300
                                s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1301
                                if(s->picture_structure == PICT_TOP_FIELD)
1302
                                    s->mv[i][2][1]--;
1303
                                else 
1304
                                    s->mv[i][2][1]++;
1305
                            }
1306
                        }
1307
                        break;
1308
                    default:
1309
                        av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1310
                        return -1;
1311
                    }
1312
                }
1313
            }
1314
        }
1315
        
1316
        s->mb_intra = 0;
1317

    
1318
        if (HAS_CBP(mb_type)) {
1319
            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1320
            if (cbp < 0){
1321
                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1322
                return -1;
1323
            }
1324
            cbp++;
1325

    
1326
#ifdef HAVE_XVMC
1327
            //on 1 we memcpy blocks in xvmcvideo
1328
            if(s->avctx->xvmc_acceleration > 1){
1329
                XVMC_pack_pblocks(s,cbp);
1330
                if(s->swap_uv){
1331
                    exchange_uv(s);
1332
                }
1333
            }    
1334
#endif
1335

    
1336
            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1337
                for(i=0;i<6;i++) {
1338
                    if (cbp & 32) {
1339
                        if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
1340
                            return -1;
1341
                    } else {
1342
                        s->block_last_index[i] = -1;
1343
                    }
1344
                    cbp+=cbp;
1345
                }
1346
            } else {
1347
                for(i=0;i<6;i++) {
1348
                    if (cbp & 32) {
1349
                        if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
1350
                            return -1;
1351
                    } else {
1352
                        s->block_last_index[i] = -1;
1353
                    }
1354
                    cbp+=cbp;
1355
                }
1356
            }
1357
        }else{
1358
            for(i=0;i<6;i++)
1359
                s->block_last_index[i] = -1;
1360
        }
1361
    }
1362

    
1363
    s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
1364

    
1365
    return 0;
1366
}
1367

    
1368
/* as h263, but only 17 codes */
1369
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
1370
{
1371
    int code, sign, val, l, shift;
1372

    
1373
    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
1374
    if (code == 0) {
1375
        return pred;
1376
    }
1377
    if (code < 0) {
1378
        return 0xffff;
1379
    }
1380

    
1381
    sign = get_bits1(&s->gb);
1382
    shift = fcode - 1;
1383
    val = code;
1384
    if (shift) {
1385
        val = (val - 1) << shift;
1386
        val |= get_bits(&s->gb, shift);
1387
        val++;
1388
    }
1389
    if (sign)
1390
        val = -val;
1391
    val += pred;
1392
    
1393
    /* modulo decoding */
1394
    l = 1 << (shift+4);
1395
    val = ((val + l)&(l*2-1)) - l;
1396
    return val;
1397
}
1398

    
1399
static inline int decode_dc(GetBitContext *gb, int component)
1400
{
1401
    int code, diff;
1402

    
1403
    if (component == 0) {
1404
        code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
1405
    } else {
1406
        code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
1407
    }
1408
    if (code < 0){
1409
        av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
1410
        return 0xffff;
1411
    }
1412
    if (code == 0) {
1413
        diff = 0;
1414
    } else {
1415
        diff = get_xbits(gb, code);
1416
    }
1417
    return diff;
1418
}
1419

    
1420
static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
1421
                               DCTELEM *block, 
1422
                               int n)
1423
{
1424
    int level, dc, diff, i, j, run;
1425
    int component;
1426
    RLTable *rl = &rl_mpeg1;
1427
    uint8_t * const scantable= s->intra_scantable.permutated;
1428
    const uint16_t *quant_matrix= s->intra_matrix;
1429
    const int qscale= s->qscale;
1430

    
1431
    /* DC coef */
1432
    component = (n <= 3 ? 0 : n - 4 + 1);
1433
    diff = decode_dc(&s->gb, component);
1434
    if (diff >= 0xffff)
1435
        return -1;
1436
    dc = s->last_dc[component];
1437
    dc += diff;
1438
    s->last_dc[component] = dc;
1439
    block[0] = dc<<3;
1440
    dprintf("dc=%d diff=%d\n", dc, diff);
1441
    i = 0;
1442
    {
1443
        OPEN_READER(re, &s->gb);    
1444
        /* now quantify & encode AC coefs */
1445
        for(;;) {
1446
            UPDATE_CACHE(re, &s->gb);
1447
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1448
            
1449
            if(level == 127){
1450
                break;
1451
            } else if(level != 0) {
1452
                i += run;
1453
                j = scantable[i];
1454
                level= (level*qscale*quant_matrix[j])>>4;
1455
                level= (level-1)|1;
1456
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1457
                LAST_SKIP_BITS(re, &s->gb, 1);
1458
            } else {
1459
                /* escape */
1460
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1461
                UPDATE_CACHE(re, &s->gb);
1462
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1463
                if (level == -128) {
1464
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1465
                } else if (level == 0) {
1466
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
1467
                }
1468
                i += run;
1469
                j = scantable[i];
1470
                if(level<0){
1471
                    level= -level;
1472
                    level= (level*qscale*quant_matrix[j])>>4;
1473
                    level= (level-1)|1;
1474
                    level= -level;
1475
                }else{
1476
                    level= (level*qscale*quant_matrix[j])>>4;
1477
                    level= (level-1)|1;
1478
                }
1479
            }
1480
            if (i > 63){
1481
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1482
                return -1;
1483
            }
1484

    
1485
            block[j] = level;
1486
        }
1487
        CLOSE_READER(re, &s->gb);
1488
    }
1489
    s->block_last_index[n] = i;
1490
   return 0;
1491
}
1492

    
1493
static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
1494
                               DCTELEM *block, 
1495
                               int n)
1496
{
1497
    int level, i, j, run;
1498
    RLTable *rl = &rl_mpeg1;
1499
    uint8_t * const scantable= s->intra_scantable.permutated;
1500
    const uint16_t *quant_matrix= s->inter_matrix;
1501
    const int qscale= s->qscale;
1502

    
1503
    {
1504
        int v;
1505
        OPEN_READER(re, &s->gb);
1506
        i = -1;
1507
        /* special case for the first coef. no need to add a second vlc table */
1508
        UPDATE_CACHE(re, &s->gb);
1509
        v= SHOW_UBITS(re, &s->gb, 2);
1510
        if (v & 2) {
1511
            LAST_SKIP_BITS(re, &s->gb, 2);
1512
            level= (3*qscale*quant_matrix[0])>>5;
1513
            level= (level-1)|1;
1514
            if(v&1)
1515
                level= -level;
1516
            block[0] = level;
1517
            i++;
1518
        }
1519

    
1520
        /* now quantify & encode AC coefs */
1521
        for(;;) {
1522
            UPDATE_CACHE(re, &s->gb);
1523
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1524
            
1525
            if(level == 127){
1526
                break;
1527
            } else if(level != 0) {
1528
                i += run;
1529
                j = scantable[i];
1530
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1531
                level= (level-1)|1;
1532
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1533
                LAST_SKIP_BITS(re, &s->gb, 1);
1534
            } else {
1535
                /* escape */
1536
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1537
                UPDATE_CACHE(re, &s->gb);
1538
                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
1539
                if (level == -128) {
1540
                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
1541
                } else if (level == 0) {
1542
                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
1543
                }
1544
                i += run;
1545
                j = scantable[i];
1546
                if(level<0){
1547
                    level= -level;
1548
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1549
                    level= (level-1)|1;
1550
                    level= -level;
1551
                }else{
1552
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1553
                    level= (level-1)|1;
1554
                }
1555
            }
1556
            if (i > 63){
1557
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1558
                return -1;
1559
            }
1560

    
1561
            block[j] = level;
1562
        }
1563
        CLOSE_READER(re, &s->gb);
1564
    }
1565
    s->block_last_index[n] = i;
1566
    return 0;
1567
}
1568

    
1569
/* Also does unquantization here, since I will never support mpeg2
1570
   encoding */
1571
static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
1572
                               DCTELEM *block, 
1573
                               int n)
1574
{
1575
    int level, i, j, run;
1576
    RLTable *rl = &rl_mpeg1;
1577
    uint8_t * const scantable= s->intra_scantable.permutated;
1578
    const uint16_t *quant_matrix;
1579
    const int qscale= s->qscale;
1580
    int mismatch;
1581

    
1582
    mismatch = 1;
1583

    
1584
    {
1585
        int v;
1586
        OPEN_READER(re, &s->gb);
1587
        i = -1;
1588
        if (n < 4)
1589
            quant_matrix = s->inter_matrix;
1590
        else
1591
            quant_matrix = s->chroma_inter_matrix;
1592

    
1593
        /* special case for the first coef. no need to add a second vlc table */
1594
        UPDATE_CACHE(re, &s->gb);
1595
        v= SHOW_UBITS(re, &s->gb, 2);
1596
        if (v & 2) {
1597
            LAST_SKIP_BITS(re, &s->gb, 2);
1598
            level= (3*qscale*quant_matrix[0])>>5;
1599
            if(v&1)
1600
                level= -level;
1601
            block[0] = level;
1602
            mismatch ^= level;
1603
            i++;
1604
        }
1605

    
1606
        /* now quantify & encode AC coefs */
1607
        for(;;) {
1608
            UPDATE_CACHE(re, &s->gb);
1609
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1610
            
1611
            if(level == 127){
1612
                break;
1613
            } else if(level != 0) {
1614
                i += run;
1615
                j = scantable[i];
1616
                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1617
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1618
                LAST_SKIP_BITS(re, &s->gb, 1);
1619
            } else {
1620
                /* escape */
1621
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1622
                UPDATE_CACHE(re, &s->gb);
1623
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1624

    
1625
                i += run;
1626
                j = scantable[i];
1627
                if(level<0){
1628
                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
1629
                    level= -level;
1630
                }else{
1631
                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
1632
                }
1633
            }
1634
            if (i > 63){
1635
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1636
                return -1;
1637
            }
1638
            
1639
            mismatch ^= level;
1640
            block[j] = level;
1641
        }
1642
        CLOSE_READER(re, &s->gb);
1643
    }
1644
    block[63] ^= (mismatch & 1);
1645
    
1646
    s->block_last_index[n] = i;
1647
    return 0;
1648
}
1649

    
1650
static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
1651
                               DCTELEM *block, 
1652
                               int n)
1653
{
1654
    int level, dc, diff, i, j, run;
1655
    int component;
1656
    RLTable *rl;
1657
    uint8_t * const scantable= s->intra_scantable.permutated;
1658
    const uint16_t *quant_matrix;
1659
    const int qscale= s->qscale;
1660
    int mismatch;
1661

    
1662
    /* DC coef */
1663
    if (n < 4){
1664
        quant_matrix = s->intra_matrix;
1665
        component = 0; 
1666
    }else{
1667
        quant_matrix = s->chroma_intra_matrix;
1668
        component = n - 3;
1669
    }
1670
    diff = decode_dc(&s->gb, component);
1671
    if (diff >= 0xffff)
1672
        return -1;
1673
    dc = s->last_dc[component];
1674
    dc += diff;
1675
    s->last_dc[component] = dc;
1676
    block[0] = dc << (3 - s->intra_dc_precision);
1677
    dprintf("dc=%d\n", block[0]);
1678
    mismatch = block[0] ^ 1;
1679
    i = 0;
1680
    if (s->intra_vlc_format)
1681
        rl = &rl_mpeg2;
1682
    else
1683
        rl = &rl_mpeg1;
1684

    
1685
    {
1686
        OPEN_READER(re, &s->gb);    
1687
        /* now quantify & encode AC coefs */
1688
        for(;;) {
1689
            UPDATE_CACHE(re, &s->gb);
1690
            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
1691
            
1692
            if(level == 127){
1693
                break;
1694
            } else if(level != 0) {
1695
                i += run;
1696
                j = scantable[i];
1697
                level= (level*qscale*quant_matrix[j])>>4;
1698
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1699
                LAST_SKIP_BITS(re, &s->gb, 1);
1700
            } else {
1701
                /* escape */
1702
                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1703
                UPDATE_CACHE(re, &s->gb);
1704
                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1705
                i += run;
1706
                j = scantable[i];
1707
                if(level<0){
1708
                    level= (-level*qscale*quant_matrix[j])>>4;
1709
                    level= -level;
1710
                }else{
1711
                    level= (level*qscale*quant_matrix[j])>>4;
1712
                }
1713
            }
1714
            if (i > 63){
1715
                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1716
                return -1;
1717
            }
1718
            
1719
            mismatch^= level;
1720
            block[j] = level;
1721
        }
1722
        CLOSE_READER(re, &s->gb);
1723
    }
1724
    block[63]^= mismatch&1;
1725
    
1726
    s->block_last_index[n] = i;
1727
    return 0;
1728
}
1729

    
1730
typedef struct Mpeg1Context {
1731
    MpegEncContext mpeg_enc_ctx;
1732
    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1733
    int repeat_field; /* true if we must repeat the field */
1734
    AVPanScan pan_scan; /** some temporary storage for the panscan */
1735
} Mpeg1Context;
1736

    
1737
static int mpeg_decode_init(AVCodecContext *avctx)
1738
{
1739
    Mpeg1Context *s = avctx->priv_data;
1740
    
1741
    s->mpeg_enc_ctx.avctx= avctx;
1742
    s->mpeg_enc_ctx.flags= avctx->flags;
1743
    common_init(&s->mpeg_enc_ctx);
1744
    init_vlcs();
1745

    
1746
    s->mpeg_enc_ctx_allocated = 0;
1747
    s->mpeg_enc_ctx.picture_number = 0;
1748
    s->repeat_field = 0;
1749
    s->mpeg_enc_ctx.codec_id= avctx->codec->id;
1750
    return 0;
1751
}
1752

    
1753
/* return the 8 bit start code value and update the search
1754
   state. Return -1 if no start code found */
1755
static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end)
1756
{
1757
    uint8_t *buf_ptr;
1758
    unsigned int state=0xFFFFFFFF, v;
1759
    int val;
1760

    
1761
    buf_ptr = *pbuf_ptr;
1762
    while (buf_ptr < buf_end) {
1763
        v = *buf_ptr++;
1764
        if (state == 0x000001) {
1765
            state = ((state << 8) | v) & 0xffffff;
1766
            val = state;
1767
            goto found;
1768
        }
1769
        state = ((state << 8) | v) & 0xffffff;
1770
    }
1771
    val = -1;
1772
 found:
1773
    *pbuf_ptr = buf_ptr;
1774
    return val;
1775
}
1776

    
1777
static int mpeg1_decode_picture(AVCodecContext *avctx, 
1778
                                uint8_t *buf, int buf_size)
1779
{
1780
    Mpeg1Context *s1 = avctx->priv_data;
1781
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1782
    int ref, f_code, vbv_delay;
1783

    
1784
    init_get_bits(&s->gb, buf, buf_size*8);
1785

    
1786
    ref = get_bits(&s->gb, 10); /* temporal ref */
1787
    s->pict_type = get_bits(&s->gb, 3);
1788

    
1789
    vbv_delay= get_bits(&s->gb, 16);
1790
    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
1791
        s->full_pel[0] = get_bits1(&s->gb);
1792
        f_code = get_bits(&s->gb, 3);
1793
        if (f_code == 0)
1794
            return -1;
1795
        s->mpeg_f_code[0][0] = f_code;
1796
        s->mpeg_f_code[0][1] = f_code;
1797
    }
1798
    if (s->pict_type == B_TYPE) {
1799
        s->full_pel[1] = get_bits1(&s->gb);
1800
        f_code = get_bits(&s->gb, 3);
1801
        if (f_code == 0)
1802
            return -1;
1803
        s->mpeg_f_code[1][0] = f_code;
1804
        s->mpeg_f_code[1][1] = f_code;
1805
    }
1806
    s->current_picture.pict_type= s->pict_type;
1807
    s->current_picture.key_frame= s->pict_type == I_TYPE;
1808
    
1809
//    if(avctx->debug & FF_DEBUG_PICT_INFO)
1810
//        av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d\n", vbv_delay, ref);
1811
    
1812
    s->y_dc_scale = 8;
1813
    s->c_dc_scale = 8;
1814
    s->first_slice = 1;
1815
    return 0;
1816
}
1817

    
1818
static void mpeg_decode_sequence_extension(MpegEncContext *s)
1819
{
1820
    int horiz_size_ext, vert_size_ext;
1821
    int bit_rate_ext;
1822
    int frame_rate_ext_n, frame_rate_ext_d;
1823
    int level, profile;
1824

    
1825
    skip_bits(&s->gb, 1); /* profil and level esc*/
1826
    profile= get_bits(&s->gb, 3);
1827
    level= get_bits(&s->gb, 4);
1828
    s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1829
    skip_bits(&s->gb, 2); /* chroma_format */
1830
    horiz_size_ext = get_bits(&s->gb, 2);
1831
    vert_size_ext = get_bits(&s->gb, 2);
1832
    s->width |= (horiz_size_ext << 12);
1833
    s->height |= (vert_size_ext << 12);
1834
    bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
1835
    s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
1836
    skip_bits1(&s->gb); /* marker */
1837
    s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
1838

    
1839
    s->low_delay = get_bits1(&s->gb);
1840
    if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
1841

    
1842
    frame_rate_ext_n = get_bits(&s->gb, 2);
1843
    frame_rate_ext_d = get_bits(&s->gb, 5);
1844
    av_reduce(
1845
        &s->avctx->frame_rate, 
1846
        &s->avctx->frame_rate_base, 
1847
        frame_rate_tab[s->frame_rate_index] * (frame_rate_ext_n+1),
1848
        MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1),
1849
        1<<30);
1850

    
1851
    dprintf("sequence extension\n");
1852
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
1853
    s->avctx->sub_id = 2; /* indicates mpeg2 found */
1854

    
1855
    if(s->aspect_ratio_info <= 1)
1856
        s->avctx->sample_aspect_ratio= mpeg2_aspect[s->aspect_ratio_info];
1857
    else{
1858
        s->avctx->sample_aspect_ratio= 
1859
            av_div_q(
1860
                mpeg2_aspect[s->aspect_ratio_info], 
1861
                (AVRational){s->width, s->height}
1862
            );
1863
    }
1864
    
1865
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1866
        av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n", 
1867
               profile, level, s->avctx->rc_buffer_size, s->bit_rate);
1868
}
1869

    
1870
static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1871
{
1872
    MpegEncContext *s= &s1->mpeg_enc_ctx;
1873
    int color_description, w, h;
1874

    
1875
    skip_bits(&s->gb, 3); /* video format */
1876
    color_description= get_bits1(&s->gb);
1877
    if(color_description){
1878
        skip_bits(&s->gb, 8); /* color primaries */
1879
        skip_bits(&s->gb, 8); /* transfer_characteristics */
1880
        skip_bits(&s->gb, 8); /* matrix_coefficients */
1881
    }
1882
    w= get_bits(&s->gb, 14);
1883
    skip_bits(&s->gb, 1); //marker
1884
    h= get_bits(&s->gb, 14);
1885
    skip_bits(&s->gb, 1); //marker
1886
    
1887
    s1->pan_scan.width= 16*w;
1888
    s1->pan_scan.height=16*h;
1889

    
1890
    if(s->aspect_ratio_info > 1)
1891
        s->avctx->sample_aspect_ratio= 
1892
            av_div_q(
1893
                mpeg2_aspect[s->aspect_ratio_info], 
1894
                (AVRational){w, h}
1895
            );
1896
    
1897
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1898
        av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1899
}
1900

    
1901
static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1902
{
1903
    MpegEncContext *s= &s1->mpeg_enc_ctx;
1904
    int i;
1905

    
1906
    for(i=0; i<1; i++){ //FIXME count
1907
        s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
1908
        skip_bits(&s->gb, 1); //marker
1909
        s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
1910
        skip_bits(&s->gb, 1); //marker
1911
    }
1912
   
1913
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1914
        av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n", 
1915
            s1->pan_scan.position[0][0], s1->pan_scan.position[0][1], 
1916
            s1->pan_scan.position[1][0], s1->pan_scan.position[1][1], 
1917
            s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
1918
        );
1919
}
1920

    
1921
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1922
{
1923
    int i, v, j;
1924

    
1925
    dprintf("matrix extension\n");
1926

    
1927
    if (get_bits1(&s->gb)) {
1928
        for(i=0;i<64;i++) {
1929
            v = get_bits(&s->gb, 8);
1930
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1931
            s->intra_matrix[j] = v;
1932
            s->chroma_intra_matrix[j] = v;
1933
        }
1934
    }
1935
    if (get_bits1(&s->gb)) {
1936
        for(i=0;i<64;i++) {
1937
            v = get_bits(&s->gb, 8);
1938
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1939
            s->inter_matrix[j] = v;
1940
            s->chroma_inter_matrix[j] = v;
1941
        }
1942
    }
1943
    if (get_bits1(&s->gb)) {
1944
        for(i=0;i<64;i++) {
1945
            v = get_bits(&s->gb, 8);
1946
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1947
            s->chroma_intra_matrix[j] = v;
1948
        }
1949
    }
1950
    if (get_bits1(&s->gb)) {
1951
        for(i=0;i<64;i++) {
1952
            v = get_bits(&s->gb, 8);
1953
            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1954
            s->chroma_inter_matrix[j] = v;
1955
        }
1956
    }
1957
}
1958

    
1959
static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
1960
{
1961
    s->full_pel[0] = s->full_pel[1] = 0;
1962
    s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1963
    s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1964
    s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1965
    s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1966
    s->intra_dc_precision = get_bits(&s->gb, 2);
1967
    s->picture_structure = get_bits(&s->gb, 2);
1968
    s->top_field_first = get_bits1(&s->gb);
1969
    s->frame_pred_frame_dct = get_bits1(&s->gb);
1970
    s->concealment_motion_vectors = get_bits1(&s->gb);
1971
    s->q_scale_type = get_bits1(&s->gb);
1972
    s->intra_vlc_format = get_bits1(&s->gb);
1973
    s->alternate_scan = get_bits1(&s->gb);
1974
    s->repeat_first_field = get_bits1(&s->gb);
1975
    s->chroma_420_type = get_bits1(&s->gb);
1976
    s->progressive_frame = get_bits1(&s->gb);
1977

    
1978
    if(s->picture_structure == PICT_FRAME)
1979
        s->first_field=0;
1980
    else{
1981
        s->first_field ^= 1;
1982
        memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
1983
    }
1984
    
1985
    if(s->alternate_scan){
1986
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
1987
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
1988
    }else{
1989
        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
1990
        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
1991
    }
1992
    
1993
    /* composite display not parsed */
1994
    dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
1995
    dprintf("picture_structure=%d\n", s->picture_structure);
1996
    dprintf("top field first=%d\n", s->top_field_first);
1997
    dprintf("repeat first field=%d\n", s->repeat_first_field);
1998
    dprintf("conceal=%d\n", s->concealment_motion_vectors);
1999
    dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
2000
    dprintf("alternate_scan=%d\n", s->alternate_scan);
2001
    dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
2002
    dprintf("progressive_frame=%d\n", s->progressive_frame);
2003
}
2004

    
2005
static void mpeg_decode_extension(AVCodecContext *avctx, 
2006
                                  uint8_t *buf, int buf_size)
2007
{
2008
    Mpeg1Context *s1 = avctx->priv_data;
2009
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2010
    int ext_type;
2011

    
2012
    init_get_bits(&s->gb, buf, buf_size*8);
2013
    
2014
    ext_type = get_bits(&s->gb, 4);
2015
    switch(ext_type) {
2016
    case 0x1:
2017
        mpeg_decode_sequence_extension(s);
2018
        break;
2019
    case 0x2:
2020
        mpeg_decode_sequence_display_extension(s1);
2021
        break;
2022
    case 0x3:
2023
        mpeg_decode_quant_matrix_extension(s);
2024
        break;
2025
    case 0x7:
2026
        mpeg_decode_picture_display_extension(s1);
2027
        break;
2028
    case 0x8:
2029
        mpeg_decode_picture_coding_extension(s);
2030
        break;
2031
    }
2032
}
2033

    
2034
static void exchange_uv(MpegEncContext *s){
2035
short * tmp;
2036

    
2037
    tmp = s->pblocks[4];
2038
    s->pblocks[4] = s->pblocks[5];
2039
    s->pblocks[5] = tmp;
2040
}
2041

    
2042
#define DECODE_SLICE_FATAL_ERROR -2
2043
#define DECODE_SLICE_ERROR -1
2044
#define DECODE_SLICE_OK 0
2045

    
2046
/**
2047
 * decodes a slice.
2048
 * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
2049
 *         DECODE_SLICE_ERROR if the slice is damaged<br>
2050
 *         DECODE_SLICE_OK if this slice is ok<br>
2051
 */
2052
static int mpeg_decode_slice(AVCodecContext *avctx, 
2053
                              AVFrame *pict,
2054
                              int start_code,
2055
                              uint8_t **buf, int buf_size)
2056
{
2057
    Mpeg1Context *s1 = avctx->priv_data;
2058
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2059
    int ret;
2060
    const int field_pic= s->picture_structure != PICT_FRAME;
2061

    
2062
    s->resync_mb_x= s->mb_x = 
2063
    s->resync_mb_y= s->mb_y = -1;
2064
    
2065
    start_code = (start_code - 1) & 0xff;
2066
    if (start_code >= s->mb_height){
2067
        av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", start_code, s->mb_height);
2068
        return -1;
2069
    }
2070
    
2071
    ff_mpeg1_clean_buffers(s);
2072
    s->interlaced_dct = 0;
2073
        
2074
    /* start frame decoding */
2075
    if (s->first_slice) {
2076
      if(s->first_field || s->picture_structure==PICT_FRAME){
2077
        if(MPV_frame_start(s, avctx) < 0)
2078
            return DECODE_SLICE_FATAL_ERROR;
2079

    
2080
        ff_er_frame_start(s);
2081

    
2082
        /* first check if we must repeat the frame */
2083
        s->current_picture_ptr->repeat_pict = 0;
2084
        if (s->repeat_first_field) {
2085
            if (s->progressive_sequence) {
2086
                if (s->top_field_first)
2087
                    s->current_picture_ptr->repeat_pict = 4;
2088
                else
2089
                    s->current_picture_ptr->repeat_pict = 2;
2090
            } else if (s->progressive_frame) {
2091
                s->current_picture_ptr->repeat_pict = 1;
2092
            }
2093
        }         
2094

    
2095
        *s->current_picture_ptr->pan_scan= s1->pan_scan;
2096
      }else{ //second field
2097
            int i;
2098
            
2099
            if(!s->current_picture_ptr){
2100
                av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
2101
                return -1;
2102
            }
2103
            
2104
            for(i=0; i<4; i++){
2105
                s->current_picture.data[i] = s->current_picture_ptr->data[i];
2106
                if(s->picture_structure == PICT_BOTTOM_FIELD){
2107
                    s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
2108
                } 
2109
            }
2110
      }
2111
#ifdef HAVE_XVMC
2112
// MPV_frame_start will call this function too,
2113
// but we need to call it on every field
2114
      if(s->avctx->xvmc_acceleration)
2115
         XVMC_field_start(s,avctx);
2116
#endif
2117
    }//fi(s->first_slice)
2118

    
2119
    init_get_bits(&s->gb, *buf, buf_size*8);
2120

    
2121
    s->qscale = get_qscale(s);
2122
    if (s->first_slice && (s->first_field || s->picture_structure==PICT_FRAME)) {
2123
        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2124
             av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n", 
2125
                 s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
2126
                 s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")), 
2127
                 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"", 
2128
                 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
2129
                 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
2130
        }
2131
    }
2132

    
2133
    s->first_slice = 0;
2134
    if(s->qscale == 0){
2135
        av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
2136
        return -1;
2137
    }
2138
    
2139
    /* extra slice info */
2140
    while (get_bits1(&s->gb) != 0) {
2141
        skip_bits(&s->gb, 8);
2142
    }
2143
    
2144
    s->mb_x=0;
2145

    
2146
    for(;;) {
2147
        int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
2148
        if (code < 0){
2149
            av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
2150
            return -1;
2151
        }
2152
        if (code >= 33) {
2153
            if (code == 33) {
2154
                s->mb_x += 33;
2155
            }
2156
            /* otherwise, stuffing, nothing to do */
2157
        } else {
2158
            s->mb_x += code;
2159
            break;
2160
        }
2161
    }
2162
    
2163
    s->resync_mb_x= s->mb_x;
2164
    s->resync_mb_y= s->mb_y = start_code;
2165
    s->mb_skip_run= 0;
2166
    ff_init_block_index(s);
2167

    
2168
    for(;;) {
2169
#ifdef HAVE_XVMC
2170
        //one 1 we memcpy blocks in xvmcvideo
2171
        if(s->avctx->xvmc_acceleration > 1)
2172
            XVMC_init_block(s);//set s->block
2173
#endif
2174

    
2175
        s->dsp.clear_blocks(s->block[0]);
2176

    
2177
        ret = mpeg_decode_mb(s, s->block);
2178
        s->chroma_qscale= s->qscale;
2179

    
2180
        dprintf("ret=%d\n", ret);
2181
        if (ret < 0)
2182
            return -1;
2183

    
2184
        if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
2185
            const int wrap = field_pic ? 2*s->block_wrap[0] : s->block_wrap[0];
2186
            int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
2187
            int motion_for_top_x, motion_for_top_y, motion_back_top_x, motion_back_top_y;
2188
            int motion_for_bottom_x, motion_for_bottom_y, motion_back_bottom_x, motion_back_bottom_y;
2189
            if(field_pic && !s->first_field)
2190
                xy += wrap/2;
2191

    
2192
            if (s->mb_intra) {
2193
                motion_for_top_x = motion_for_top_y = motion_back_top_x = motion_back_top_y =
2194
                motion_for_bottom_x = motion_for_bottom_y = motion_back_bottom_x = motion_back_bottom_y = 0;
2195
            }else if (s->mv_type == MV_TYPE_16X16){
2196
                motion_for_top_x = motion_for_bottom_x = s->mv[0][0][0];
2197
                motion_for_top_y = motion_for_bottom_y = s->mv[0][0][1];
2198
                motion_back_top_x = motion_back_bottom_x = s->mv[1][0][0];
2199
                motion_back_top_y = motion_back_bottom_y = s->mv[1][0][1];
2200
            } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
2201
                motion_for_top_x = s->mv[0][0][0];
2202
                motion_for_top_y = s->mv[0][0][1];
2203
                motion_for_bottom_x = s->mv[0][1][0];
2204
                motion_for_bottom_y = s->mv[0][1][1];
2205
                motion_back_top_x = s->mv[1][0][0];
2206
                motion_back_top_y = s->mv[1][0][1];
2207
                motion_back_bottom_x = s->mv[1][1][0];
2208
                motion_back_bottom_y = s->mv[1][1][1];
2209
            }
2210

    
2211
            s->current_picture.motion_val[0][xy][0] = motion_for_top_x;
2212
            s->current_picture.motion_val[0][xy][1] = motion_for_top_y;
2213
            s->current_picture.motion_val[0][xy + 1][0] = motion_for_top_x;
2214
            s->current_picture.motion_val[0][xy + 1][1] = motion_for_top_y;
2215
            s->current_picture.motion_val[0][xy + wrap][0] = motion_for_bottom_x;
2216
            s->current_picture.motion_val[0][xy + wrap][1] = motion_for_bottom_y;
2217
            s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_for_bottom_x;
2218
            s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_for_bottom_y;
2219

    
2220
            if(s->pict_type != B_TYPE){
2221
                motion_back_top_x = motion_back_top_y = motion_back_bottom_x = motion_back_bottom_y = 0;
2222
            }
2223

    
2224
            s->current_picture.motion_val[1][xy][0] = motion_back_top_x;
2225
            s->current_picture.motion_val[1][xy][1] = motion_back_top_y;
2226
            s->current_picture.motion_val[1][xy + 1][0] = motion_back_top_x;
2227
            s->current_picture.motion_val[1][xy + 1][1] = motion_back_top_y;
2228
            s->current_picture.motion_val[1][xy + wrap][0] = motion_back_bottom_x;
2229
            s->current_picture.motion_val[1][xy + wrap][1] = motion_back_bottom_y;
2230
            s->current_picture.motion_val[1][xy + 1 + wrap][0] = motion_back_bottom_x;
2231
            s->current_picture.motion_val[1][xy + 1 + wrap][1] = motion_back_bottom_y;
2232
        }
2233

    
2234
        s->dest[0] += 16;
2235
        s->dest[1] += 8;
2236
        s->dest[2] += 8;
2237

    
2238
        MPV_decode_mb(s, s->block);
2239
        
2240
        if (++s->mb_x >= s->mb_width) {
2241

    
2242
            ff_draw_horiz_band(s, 16*s->mb_y, 16);
2243

    
2244
            s->mb_x = 0;
2245
            s->mb_y++;
2246

    
2247
            if(s->mb_y<<field_pic >= s->mb_height){
2248
                int left= s->gb.size_in_bits - get_bits_count(&s->gb);
2249

    
2250
                if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)))
2251
                   || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
2252
                    av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d\n", left);
2253
                    return -1;
2254
                }else
2255
                    goto eos;
2256
            }
2257
            
2258
            ff_init_block_index(s);
2259
        }
2260

    
2261
        /* skip mb handling */
2262
        if (s->mb_skip_run == -1) {
2263
            /* read again increment */
2264
            s->mb_skip_run = 0;
2265
            for(;;) {
2266
                int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
2267
                if (code < 0){
2268
                    av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
2269
                    return -1;
2270
                }
2271
                if (code >= 33) {
2272
                    if (code == 33) {
2273
                        s->mb_skip_run += 33;
2274
                    }else if(code == 35){
2275
                        if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
2276
                            av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
2277
                            return -1;
2278
                        }
2279
                        goto eos; /* end of slice */
2280
                    }
2281
                    /* otherwise, stuffing, nothing to do */
2282
                } else {
2283
                    s->mb_skip_run += code;
2284
                    break;
2285
                }
2286
            }
2287
        }
2288
    }
2289
eos: // end of slice
2290
    *buf += get_bits_count(&s->gb)/8 - 1;
2291
//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
2292
    return 0;
2293
}
2294

    
2295
/**
2296
 * handles slice ends.
2297
 * @return 1 if it seems to be the last slice of 
2298
 */
2299
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2300
{
2301
    Mpeg1Context *s1 = avctx->priv_data;
2302
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2303
       
2304
    if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2305
        return 0;
2306

    
2307
#ifdef HAVE_XVMC
2308
    if(s->avctx->xvmc_acceleration)
2309
        XVMC_field_end(s);
2310
#endif
2311
    /* end of slice reached */
2312
    if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
2313
        /* end of image */
2314

    
2315
        s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
2316

    
2317
        ff_er_frame_end(s);
2318

    
2319
        MPV_frame_end(s);
2320

    
2321
        if (s->pict_type == B_TYPE || s->low_delay) {
2322
            *pict= *(AVFrame*)s->current_picture_ptr;
2323
            ff_print_debug_info(s, pict);
2324
        } else {
2325
            s->picture_number++;
2326
            /* latency of 1 frame for I and P frames */
2327
            /* XXX: use another variable than picture_number */
2328
            if (s->last_picture_ptr != NULL) {
2329
                *pict= *(AVFrame*)s->last_picture_ptr;
2330
                 ff_print_debug_info(s, pict);
2331
            }
2332
        }
2333

    
2334
        return 1;
2335
    } else {
2336
        return 0;
2337
    }
2338
}
2339

    
2340
static int mpeg1_decode_sequence(AVCodecContext *avctx, 
2341
                                 uint8_t *buf, int buf_size)
2342
{
2343
    Mpeg1Context *s1 = avctx->priv_data;
2344
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2345
    int width, height, i, v, j;
2346
    float aspect;
2347

    
2348
    init_get_bits(&s->gb, buf, buf_size*8);
2349

    
2350
    width = get_bits(&s->gb, 12);
2351
    height = get_bits(&s->gb, 12);
2352
    s->aspect_ratio_info= get_bits(&s->gb, 4);
2353
    if (s->aspect_ratio_info == 0)
2354
        return -1;
2355
    aspect= 1.0/mpeg1_aspect[s->aspect_ratio_info];
2356
    avctx->sample_aspect_ratio= av_d2q(aspect, 255);
2357

    
2358
    s->frame_rate_index = get_bits(&s->gb, 4);
2359
    if (s->frame_rate_index == 0)
2360
        return -1;
2361
    s->bit_rate = get_bits(&s->gb, 18) * 400;
2362
    if (get_bits1(&s->gb) == 0) /* marker */
2363
        return -1;
2364
    if (width <= 0 || height <= 0 ||
2365
        (width % 2) != 0 || (height % 2) != 0)
2366
        return -1;
2367
    if (width != s->width ||
2368
        height != s->height) {
2369
        /* start new mpeg1 context decoding */
2370
        s->out_format = FMT_MPEG1;
2371
        if (s1->mpeg_enc_ctx_allocated) {
2372
            MPV_common_end(s);
2373
        }
2374
        s->width = width;
2375
        s->height = height;
2376
        avctx->has_b_frames= 1;
2377
        avctx->width = width;
2378
        avctx->height = height;
2379
        av_reduce(
2380
            &avctx->frame_rate, 
2381
            &avctx->frame_rate_base,
2382
            frame_rate_tab[s->frame_rate_index],
2383
            MPEG1_FRAME_RATE_BASE, //FIXME store in allready reduced form 
2384
            1<<30
2385
            );
2386
        avctx->bit_rate = s->bit_rate;
2387
        
2388
        //get_format() or set_video(width,height,aspect,pix_fmt);
2389
        //until then pix_fmt may be changed right after codec init
2390
        if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
2391
            if( avctx->idct_algo == FF_IDCT_AUTO )
2392
                avctx->idct_algo = FF_IDCT_SIMPLE;
2393

    
2394
        if (MPV_common_init(s) < 0)
2395
            return -1;
2396
        s1->mpeg_enc_ctx_allocated = 1;
2397
        s->swap_uv = 0;//just in case vcr2 and mpeg2 stream have been concatinated
2398
    }
2399

    
2400
    s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
2401
    skip_bits(&s->gb, 1);
2402

    
2403
    /* get matrix */
2404
    if (get_bits1(&s->gb)) {
2405
        for(i=0;i<64;i++) {
2406
            v = get_bits(&s->gb, 8);
2407
            j = s->intra_scantable.permutated[i];
2408
            s->intra_matrix[j] = v;
2409
            s->chroma_intra_matrix[j] = v;
2410
        }
2411
#ifdef DEBUG
2412
        dprintf("intra matrix present\n");
2413
        for(i=0;i<64;i++)
2414
            dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
2415
        printf("\n");
2416
#endif
2417
    } else {
2418
        for(i=0;i<64;i++) {
2419
            int j= s->dsp.idct_permutation[i];
2420
            v = ff_mpeg1_default_intra_matrix[i];
2421
            s->intra_matrix[j] = v;
2422
            s->chroma_intra_matrix[j] = v;
2423
        }
2424
    }
2425
    if (get_bits1(&s->gb)) {
2426
        for(i=0;i<64;i++) {
2427
            v = get_bits(&s->gb, 8);
2428
            j = s->intra_scantable.permutated[i];
2429
            s->inter_matrix[j] = v;
2430
            s->chroma_inter_matrix[j] = v;
2431
        }
2432
#ifdef DEBUG
2433
        dprintf("non intra matrix present\n");
2434
        for(i=0;i<64;i++)
2435
            dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
2436
        printf("\n");
2437
#endif
2438
    } else {
2439
        for(i=0;i<64;i++) {
2440
            int j= s->dsp.idct_permutation[i];
2441
            v = ff_mpeg1_default_non_intra_matrix[i];
2442
            s->inter_matrix[j] = v;
2443
            s->chroma_inter_matrix[j] = v;
2444
        }
2445
    }
2446

    
2447
    /* we set mpeg2 parameters so that it emulates mpeg1 */
2448
    s->progressive_sequence = 1;
2449
    s->progressive_frame = 1;
2450
    s->picture_structure = PICT_FRAME;
2451
    s->frame_pred_frame_dct = 1;
2452
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
2453
    avctx->sub_id = 1; /* indicates mpeg1 */
2454
    if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2455
    
2456
    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2457
        av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n", 
2458
               s->avctx->rc_buffer_size, s->bit_rate);
2459
    
2460
    return 0;
2461
}
2462

    
2463
static int vcr2_init_sequence(AVCodecContext *avctx)
2464
{
2465
    Mpeg1Context *s1 = avctx->priv_data;
2466
    MpegEncContext *s = &s1->mpeg_enc_ctx;
2467
    int i, v;
2468

    
2469
    /* start new mpeg1 context decoding */
2470
    s->out_format = FMT_MPEG1;
2471
    if (s1->mpeg_enc_ctx_allocated) {
2472
        MPV_common_end(s);
2473
    }
2474
    s->width = avctx->width;
2475
    s->height = avctx->height;
2476
    avctx->has_b_frames= 0; //true?
2477
    s->low_delay= 1;
2478

    
2479
    //get_format() or set_video(width,height,aspect,pix_fmt);
2480
    //until then pix_fmt may be changed right after codec init
2481
    if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
2482
        if( avctx->idct_algo == FF_IDCT_AUTO )
2483
            avctx->idct_algo = FF_IDCT_SIMPLE;
2484
    
2485
    if (MPV_common_init(s) < 0)
2486
        return -1;
2487
    exchange_uv(s);//common init reset pblocks, so we swap them here
2488
    s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB 
2489
    s1->mpeg_enc_ctx_allocated = 1;
2490

    
2491
    for(i=0;i<64;i++) {
2492
        int j= s->dsp.idct_permutation[i];
2493
        v = ff_mpeg1_default_intra_matrix[i];
2494
        s->intra_matrix[j] = v;
2495
        s->chroma_intra_matrix[j] = v;
2496

    
2497
        v = ff_mpeg1_default_non_intra_matrix[i];
2498
        s->inter_matrix[j] = v;
2499
        s->chroma_inter_matrix[j] = v;
2500
    }
2501

    
2502
    s->progressive_sequence = 1;
2503
    s->progressive_frame = 1;
2504
    s->picture_structure = PICT_FRAME;
2505
    s->frame_pred_frame_dct = 1;
2506
    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2507
    avctx->sub_id = 2; /* indicates mpeg2 */
2508
    return 0;
2509
}
2510

    
2511

    
2512
static void mpeg_decode_user_data(AVCodecContext *avctx, 
2513
                                  const uint8_t *buf, int buf_size)
2514
{
2515
    const uint8_t *p;
2516
    int len, flags;
2517
    p = buf;
2518
    len = buf_size;
2519

    
2520
    /* we parse the DTG active format information */
2521
    if (len >= 5 &&
2522
        p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2523
        flags = p[4];
2524
        p += 5;
2525
        len -= 5;
2526
        if (flags & 0x80) {
2527
            /* skip event id */
2528
            if (len < 2)
2529
                return;
2530
            p += 2;
2531
            len -= 2;
2532
        }
2533
        if (flags & 0x40) {
2534
            if (len < 1)
2535
                return;
2536
            avctx->dtg_active_format = p[0] & 0x0f;
2537
        }
2538
    }
2539
}
2540

    
2541
/**
2542
 * finds the end of the current frame in the bitstream.
2543
 * @return the position of the first byte of the next frame, or -1
2544
 */
2545
static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
2546
    ParseContext *pc= &s->parse_context;
2547
    int i;
2548
    uint32_t state;
2549
    
2550
    state= pc->state;
2551
    
2552
    i=0;
2553
    if(!pc->frame_start_found){
2554
        for(i=0; i<buf_size; i++){
2555
            state= (state<<8) | buf[i];
2556
            if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
2557
                i++;
2558
                pc->frame_start_found=1;
2559
                break;
2560
            }
2561
        }
2562
    }
2563
    
2564
    if(pc->frame_start_found){
2565
        for(; i<buf_size; i++){
2566
            state= (state<<8) | buf[i];
2567
            if((state&0xFFFFFF00) == 0x100){
2568
                if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
2569
                    pc->frame_start_found=0;
2570
                    pc->state=-1; 
2571
                    return i-3;
2572
                }
2573
            }
2574
        }
2575
    }        
2576
    pc->state= state;
2577
    return END_NOT_FOUND;
2578
}
2579

    
2580
/* handle buffering and image synchronisation */
2581
static int mpeg_decode_frame(AVCodecContext *avctx, 
2582
                             void *data, int *data_size,
2583
                             uint8_t *buf, int buf_size)
2584
{
2585
    Mpeg1Context *s = avctx->priv_data;
2586
    uint8_t *buf_end, *buf_ptr;
2587
    int ret, start_code, input_size;
2588
    AVFrame *picture = data;
2589
    MpegEncContext *s2 = &s->mpeg_enc_ctx;
2590
    dprintf("fill_buffer\n");
2591

    
2592
    *data_size = 0;
2593

    
2594
    /* special case for last picture */
2595
    if (buf_size == 0 && s2->low_delay==0 && s2->next_picture_ptr) {
2596
        *picture= *(AVFrame*)s2->next_picture_ptr;
2597
        s2->next_picture_ptr= NULL;
2598

    
2599
        *data_size = sizeof(AVFrame);
2600
        return 0;
2601
    }
2602

    
2603
    if(s2->flags&CODEC_FLAG_TRUNCATED){
2604
        int next= mpeg1_find_frame_end(s2, buf, buf_size);
2605
        
2606
        if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
2607
            return buf_size;
2608
    }    
2609
    
2610
    buf_ptr = buf;
2611
    buf_end = buf + buf_size;
2612

    
2613
#if 0    
2614
    if (s->repeat_field % 2 == 1) { 
2615
        s->repeat_field++;
2616
        //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
2617
        //        s2->picture_number, s->repeat_field);
2618
        if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
2619
            *data_size = sizeof(AVPicture);
2620
            goto the_end;
2621
        }
2622
    }
2623
#endif
2624

    
2625
    if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
2626
        vcr2_init_sequence(avctx);
2627

    
2628
    for(;;) {
2629
        /* find start next code */
2630
        start_code = find_start_code(&buf_ptr, buf_end);
2631
        if (start_code < 0){
2632
            if(s2->pict_type != B_TYPE || avctx->hurry_up==0){
2633
                if (slice_end(avctx, picture)) {
2634
                    if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2635
                        *data_size = sizeof(AVPicture);
2636
                }
2637
            }
2638
            return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2639
        }
2640
        
2641
        input_size = buf_end - buf_ptr;
2642

    
2643
        if(avctx->debug & FF_DEBUG_STARTCODE){
2644
            av_log(avctx, AV_LOG_DEBUG, "%3X at %d left %d\n", start_code, buf_ptr-buf, input_size);
2645
        }
2646

    
2647
                /* prepare data for next start code */
2648
                switch(start_code) {
2649
                case SEQ_START_CODE:
2650
                    mpeg1_decode_sequence(avctx, buf_ptr, 
2651
                                          input_size);
2652
                    break;
2653
                            
2654
                case PICTURE_START_CODE:
2655
                    /* we have a complete image : we try to decompress it */
2656
                    mpeg1_decode_picture(avctx, 
2657
                                         buf_ptr, input_size);
2658
                    break;
2659
                case EXT_START_CODE:
2660
                    mpeg_decode_extension(avctx,
2661
                                          buf_ptr, input_size);
2662
                    break;
2663
                case USER_START_CODE:
2664
                    mpeg_decode_user_data(avctx, 
2665
                                          buf_ptr, input_size);
2666
                    break;
2667
                case GOP_START_CODE:
2668
                    s2->first_field=0;
2669
                    break;
2670
                default:
2671
                    if (start_code >= SLICE_MIN_START_CODE &&
2672
                        start_code <= SLICE_MAX_START_CODE) {
2673
                        
2674
                        /* skip b frames if we dont have reference frames */
2675
                        if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
2676
                        /* skip b frames if we are in a hurry */
2677
                        if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
2678
                        /* skip everything if we are in a hurry>=5 */
2679
                        if(avctx->hurry_up>=5) break;
2680
                        
2681
                        if (!s->mpeg_enc_ctx_allocated) break;
2682

    
2683
                        ret = mpeg_decode_slice(avctx, picture,
2684
                                                start_code, &buf_ptr, input_size);
2685
                        emms_c();
2686

    
2687
                        if(ret < 0){
2688
                            if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
2689
                                ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
2690
                            if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
2691
                        }else{
2692
                            ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
2693
                        }
2694
                    }
2695
                    break;
2696
                }
2697
    }
2698
}
2699

    
2700
static int mpeg_decode_end(AVCodecContext *avctx)
2701
{
2702
    Mpeg1Context *s = avctx->priv_data;
2703

    
2704
    if (s->mpeg_enc_ctx_allocated)
2705
        MPV_common_end(&s->mpeg_enc_ctx);
2706
    return 0;
2707
}
2708

    
2709
AVCodec mpeg1video_decoder = {
2710
    "mpeg1video",
2711
    CODEC_TYPE_VIDEO,
2712
    CODEC_ID_MPEG1VIDEO,
2713
    sizeof(Mpeg1Context),
2714
    mpeg_decode_init,
2715
    NULL,
2716
    mpeg_decode_end,
2717
    mpeg_decode_frame,
2718
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
2719
    .flush= ff_mpeg_flush,
2720
};
2721

    
2722
AVCodec mpeg2video_decoder = {
2723
    "mpeg2video",
2724
    CODEC_TYPE_VIDEO,
2725
    CODEC_ID_MPEG2VIDEO,
2726
    sizeof(Mpeg1Context),
2727
    mpeg_decode_init,
2728
    NULL,
2729
    mpeg_decode_end,
2730
    mpeg_decode_frame,
2731
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
2732
    .flush= ff_mpeg_flush,
2733
};
2734

    
2735
//legacy decoder
2736
AVCodec mpegvideo_decoder = {
2737
    "mpegvideo",
2738
    CODEC_TYPE_VIDEO,
2739
    CODEC_ID_MPEG2VIDEO,
2740
    sizeof(Mpeg1Context),
2741
    mpeg_decode_init,
2742
    NULL,
2743
    mpeg_decode_end,
2744
    mpeg_decode_frame,
2745
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
2746
    .flush= ff_mpeg_flush,
2747
};
2748

    
2749
#ifdef HAVE_XVMC
2750
static int mpeg_mc_decode_init(AVCodecContext *avctx){
2751
    Mpeg1Context *s;
2752

    
2753
    if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2754
        return -1;
2755
    if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
2756
        dprintf("mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2757
    }
2758
    mpeg_decode_init(avctx);
2759
    s = avctx->priv_data;
2760

    
2761
    avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
2762
    avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
2763

    
2764
    return 0;
2765
}
2766

    
2767
AVCodec mpeg_xvmc_decoder = {
2768
    "mpegvideo_xvmc",
2769
    CODEC_TYPE_VIDEO,
2770
    CODEC_ID_MPEG2VIDEO_XVMC,
2771
    sizeof(Mpeg1Context),
2772
    mpeg_mc_decode_init,
2773
    NULL,
2774
    mpeg_decode_end,
2775
    mpeg_decode_frame,
2776
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
2777
};
2778

    
2779
#endif
2780

    
2781
/* this is ugly i know, but the alternative is too make 
2782
   hundreds of vars global and prefix them with ff_mpeg1_
2783
   which is far uglier. */
2784
#include "mdec.c"