Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpeg12.c @ 3696d2bb

History | View | Annotate | Download (49.2 KB)

1
/*
2
 * MPEG1 encoder / MPEG2 decoder
3
 * Copyright (c) 2000,2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
//#define DEBUG
20
#include "avcodec.h"
21
#include "dsputil.h"
22
#include "mpegvideo.h"
23

    
24
#include "mpeg12data.h"
25

    
26
/* Start codes. */
27
#define SEQ_END_CODE                0x000001b7
28
#define SEQ_START_CODE                0x000001b3
29
#define GOP_START_CODE                0x000001b8
30
#define PICTURE_START_CODE        0x00000100
31
#define SLICE_MIN_START_CODE        0x00000101
32
#define SLICE_MAX_START_CODE        0x000001af
33
#define EXT_START_CODE                0x000001b5
34
#define USER_START_CODE                0x000001b2
35

    
36
#define ABS(a) ((a)<0 ? -(a) : (a))
37

    
38
static void mpeg1_encode_block(MpegEncContext *s, 
39
                         DCTELEM *block, 
40
                         int component);
41
static void mpeg1_encode_motion(MpegEncContext *s, int val);
42
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
43
static int mpeg1_decode_block(MpegEncContext *s, 
44
                              DCTELEM *block, 
45
                              int n);
46
static int mpeg2_decode_block_non_intra(MpegEncContext *s, 
47
                                        DCTELEM *block, 
48
                                        int n);
49
static int mpeg2_decode_block_intra(MpegEncContext *s, 
50
                                    DCTELEM *block, 
51
                                    int n);
52
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
53

    
54
static void put_header(MpegEncContext *s, int header)
55
{
56
    align_put_bits(&s->pb);
57
    put_bits(&s->pb, 16, header>>16);
58
    put_bits(&s->pb, 16, header&0xFFFF);
59
}
60

    
61
/* put sequence header if needed */
62
static void mpeg1_encode_sequence_header(MpegEncContext *s)
63
{
64
        unsigned int vbv_buffer_size;
65
        unsigned int fps, v;
66
        int n;
67
        UINT64 time_code;
68
        
69
        if ((s->picture_number % s->gop_size) == 0) {
70
            /* mpeg1 header repeated every gop */
71
            put_header(s, SEQ_START_CODE);
72
            
73
            /* search closest frame rate */
74
            {
75
                int i, dmin, d;
76
                s->frame_rate_index = 0;
77
                dmin = 0x7fffffff;
78
                for(i=1;i<9;i++) {
79
                    d = abs(s->frame_rate - frame_rate_tab[i]);
80
                    if (d < dmin) {
81
                        dmin = d;
82
                        s->frame_rate_index = i;
83
                    }
84
                }
85
            }
86
 
87
            put_bits(&s->pb, 12, s->width);
88
            put_bits(&s->pb, 12, s->height);
89
            put_bits(&s->pb, 4, 1); /* 1/1 aspect ratio */
90
            put_bits(&s->pb, 4, s->frame_rate_index);
91
            v = s->bit_rate / 400;
92
            if (v > 0x3ffff)
93
                v = 0x3ffff;
94
            put_bits(&s->pb, 18, v);
95
            put_bits(&s->pb, 1, 1); /* marker */
96
            /* vbv buffer size: slightly greater than an I frame. We add
97
               some margin just in case */
98
            vbv_buffer_size = (3 * s->I_frame_bits) / (2 * 8);
99
            put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); 
100
            put_bits(&s->pb, 1, 1); /* constrained parameter flag */
101
            put_bits(&s->pb, 1, 0); /* no custom intra matrix */
102
            put_bits(&s->pb, 1, 0); /* no custom non intra matrix */
103

    
104
            put_header(s, GOP_START_CODE);
105
            put_bits(&s->pb, 1, 0); /* do drop frame */
106
            /* time code : we must convert from the real frame rate to a
107
               fake mpeg frame rate in case of low frame rate */
108
            fps = frame_rate_tab[s->frame_rate_index];
109
            time_code = s->fake_picture_number * FRAME_RATE_BASE;
110
            s->gop_picture_number = s->fake_picture_number;
111
            put_bits(&s->pb, 5, (UINT32)((time_code / (fps * 3600)) % 24));
112
            put_bits(&s->pb, 6, (UINT32)((time_code / (fps * 60)) % 60));
113
            put_bits(&s->pb, 1, 1);
114
            put_bits(&s->pb, 6, (UINT32)((time_code / fps) % 60));
115
            put_bits(&s->pb, 6, (UINT32)((time_code % fps) / FRAME_RATE_BASE));
116
            put_bits(&s->pb, 1, 1); /* closed gop */
117
            put_bits(&s->pb, 1, 0); /* broken link */
118
        }
119

    
120
        if (s->frame_rate < (24 * FRAME_RATE_BASE) && s->picture_number > 0) {
121
            /* insert empty P pictures to slow down to the desired
122
               frame rate. Each fake pictures takes about 20 bytes */
123
            fps = frame_rate_tab[s->frame_rate_index];
124
            n = ((s->picture_number * fps) / s->frame_rate) - 1;
125
            while (s->fake_picture_number < n) {
126
                mpeg1_skip_picture(s, s->fake_picture_number - 
127
                                   s->gop_picture_number); 
128
                s->fake_picture_number++;
129
            }
130

    
131
        }
132
        s->fake_picture_number++;
133
}
134

    
135

    
136
/* insert a fake P picture */
137
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
138
{
139
    unsigned int mb_incr;
140

    
141
    /* mpeg1 picture header */
142
    put_header(s, PICTURE_START_CODE);
143
    /* temporal reference */
144
    put_bits(&s->pb, 10, pict_num & 0x3ff); 
145
    
146
    put_bits(&s->pb, 3, P_TYPE);
147
    put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
148
    
149
    put_bits(&s->pb, 1, 1); /* integer coordinates */
150
    put_bits(&s->pb, 3, 1); /* forward_f_code */
151
    
152
    put_bits(&s->pb, 1, 0); /* extra bit picture */
153
    
154
    /* only one slice */
155
    put_header(s, SLICE_MIN_START_CODE);
156
    put_bits(&s->pb, 5, 1); /* quantizer scale */
157
    put_bits(&s->pb, 1, 0); /* slice extra information */
158
    
159
    mb_incr = 1;
160
    put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], 
161
             mbAddrIncrTable[mb_incr - 1][0]);
162
    
163
    /* empty macroblock */
164
    put_bits(&s->pb, 3, 1); /* motion only */
165
    
166
    /* zero motion x & y */
167
    put_bits(&s->pb, 1, 1); 
168
    put_bits(&s->pb, 1, 1); 
169

    
170
    /* output a number of empty slice */
171
    mb_incr = s->mb_width * s->mb_height - 1;
172
    while (mb_incr > 33) {
173
        put_bits(&s->pb, 11, 0x008);
174
        mb_incr -= 33;
175
    }
176
    put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], 
177
             mbAddrIncrTable[mb_incr - 1][0]);
178
    
179
    /* empty macroblock */
180
    put_bits(&s->pb, 3, 1); /* motion only */
181
    
182
    /* zero motion x & y */
183
    put_bits(&s->pb, 1, 1); 
184
    put_bits(&s->pb, 1, 1); 
185
}
186

    
187
void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
188
{
189
    static int done=0;
190

    
191
    if (!done) {
192
        int i;
193
        done = 1;
194
        init_rl(&rl_mpeg1);
195
        
196
        for(i=0; i<64; i++)
197
        {
198
                mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
199
                mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
200
        }
201

    
202
        /* build unified dc encoding tables */
203
        for(i=-255; i<256; i++)
204
        {
205
                int adiff, index;
206
                int bits, code;
207
                int diff=i;
208

    
209
                adiff = ABS(diff);
210
                if(diff<0) diff--;
211
                index = vlc_dc_table[adiff];
212

    
213
                bits= vlc_dc_lum_bits[index] + index;
214
                code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
215
                mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
216
                
217
                bits= vlc_dc_chroma_bits[index] + index;
218
                code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
219
                mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
220
        }
221
    }
222
    mpeg1_encode_sequence_header(s);
223

    
224
    /* mpeg1 picture header */
225
    put_header(s, PICTURE_START_CODE);
226
    /* temporal reference */
227
    put_bits(&s->pb, 10, (s->fake_picture_number - 
228
                          s->gop_picture_number) & 0x3ff); 
229
    
230
    put_bits(&s->pb, 3, s->pict_type);
231
    put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
232
    
233
    if (s->pict_type == P_TYPE) {
234
        put_bits(&s->pb, 1, 0); /* half pel coordinates */
235
        put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
236
    }
237
    
238
    put_bits(&s->pb, 1, 0); /* extra bit picture */
239
    
240
    /* only one slice */
241
    put_header(s, SLICE_MIN_START_CODE);
242
    put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
243
    put_bits(&s->pb, 1, 0); /* slice extra information */
244
}
245

    
246
void mpeg1_encode_mb(MpegEncContext *s,
247
                     DCTELEM block[6][64],
248
                     int motion_x, int motion_y)
249
{
250
    int mb_incr, i, cbp, mb_x, mb_y;
251

    
252
    mb_x = s->mb_x;
253
    mb_y = s->mb_y;
254

    
255
    /* compute cbp */
256
    cbp = 0;
257
    for(i=0;i<6;i++) {
258
        if (s->block_last_index[i] >= 0)
259
            cbp |= 1 << (5 - i);
260
    }
261

    
262
    /* skip macroblock, except if first or last macroblock of a slice */
263
    if ((cbp | motion_x | motion_y) == 0 &&
264
        (!((mb_x | mb_y) == 0 ||
265
           (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) {
266
        s->mb_incr++;
267
    } else {
268
        /* output mb incr */
269
        mb_incr = s->mb_incr;
270

    
271
        while (mb_incr > 33) {
272
            put_bits(&s->pb, 11, 0x008);
273
            mb_incr -= 33;
274
        }
275
        put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], 
276
                 mbAddrIncrTable[mb_incr - 1][0]);
277
        
278
        if (s->pict_type == I_TYPE) {
279
            put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
280
        } else {
281
            if (s->mb_intra) {
282
                put_bits(&s->pb, 5, 0x03);
283
            } else {
284
                if (cbp != 0) {
285
                    if (motion_x == 0 && motion_y == 0) {
286
                        put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
287
                        put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
288
                    } else {
289
                        put_bits(&s->pb, 1, 1); /* motion + cbp */
290
                        mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); 
291
                        mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); 
292
                        put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
293
                    }
294
                } else {
295
                    put_bits(&s->pb, 3, 1); /* motion only */
296
                    mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); 
297
                    mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); 
298
                }
299
            }
300
        }
301
        for(i=0;i<6;i++) {
302
            if (cbp & (1 << (5 - i))) {
303
                mpeg1_encode_block(s, block[i], i);
304
            }
305
        }
306
        s->mb_incr = 1;
307
    }
308
    s->last_mv[0][0][0] = motion_x;
309
    s->last_mv[0][0][1] = motion_y;
310
}
311

    
312
static void mpeg1_encode_motion(MpegEncContext *s, int val)
313
{
314
    int code, bit_size, l, m, bits, range, sign;
315

    
316
    if (val == 0) {
317
        /* zero vector */
318
        code = 0;
319
        put_bits(&s->pb,
320
                 mbMotionVectorTable[0][1], 
321
                 mbMotionVectorTable[0][0]); 
322
    } else {
323
        bit_size = s->f_code - 1;
324
        range = 1 << bit_size;
325
        /* modulo encoding */
326
        l = 16 * range;
327
        m = 2 * l;
328
        if (val < -l) {
329
            val += m;
330
        } else if (val >= l) {
331
            val -= m;
332
        }
333

    
334
        if (val >= 0) {
335
            val--;
336
            code = (val >> bit_size) + 1;
337
            bits = val & (range - 1);
338
            sign = 0;
339
        } else {
340
            val = -val;
341
            val--;
342
            code = (val >> bit_size) + 1;
343
            bits = val & (range - 1);
344
            sign = 1;
345
        }
346
        put_bits(&s->pb,
347
                 mbMotionVectorTable[code][1], 
348
                 mbMotionVectorTable[code][0]); 
349
        put_bits(&s->pb, 1, sign);
350
        if (bit_size > 0) {
351
            put_bits(&s->pb, bit_size, bits);
352
        }
353
    }
354
}
355

    
356
static inline void encode_dc(MpegEncContext *s, int diff, int component)
357
{
358
    if (component == 0) {
359
        put_bits(
360
            &s->pb, 
361
            mpeg1_lum_dc_uni[diff+255]&0xFF,
362
            mpeg1_lum_dc_uni[diff+255]>>8);
363
    } else {
364
        put_bits(
365
            &s->pb, 
366
            mpeg1_chr_dc_uni[diff+255]&0xFF,
367
            mpeg1_chr_dc_uni[diff+255]>>8);
368
    }
369
}
370

    
371
static void mpeg1_encode_block(MpegEncContext *s, 
372
                               DCTELEM *block, 
373
                               int n)
374
{
375
    int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
376
    int code, component;
377
//    RLTable *rl = &rl_mpeg1;
378

    
379
    last_index = s->block_last_index[n];
380

    
381
    /* DC coef */
382
    if (s->mb_intra) {
383
        component = (n <= 3 ? 0 : n - 4 + 1);
384
        dc = block[0]; /* overflow is impossible */
385
        diff = dc - s->last_dc[component];
386
        encode_dc(s, diff, component);
387
        s->last_dc[component] = dc;
388
        i = 1;
389
    } else {
390
        /* encode the first coefficient : needs to be done here because
391
           it is handled slightly differently */
392
        level = block[0];
393
        if (abs(level) == 1) {
394
                code = ((UINT32)level >> 31); /* the sign bit */
395
                put_bits(&s->pb, 2, code | 0x02);
396
                i = 1;
397
        } else {
398
            i = 0;
399
            last_non_zero = -1;
400
            goto next_coef;
401
        }
402
    }
403

    
404
    /* now quantify & encode AC coefs */
405
    last_non_zero = i - 1;
406

    
407
    for(;i<=last_index;i++) {
408
        j = zigzag_direct[i];
409
        level = block[j];
410
    next_coef:
411
#if 0
412
        if (level != 0)
413
            dprintf("level[%d]=%d\n", i, level);
414
#endif            
415
        /* encode using VLC */
416
        if (level != 0) {
417
            run = i - last_non_zero - 1;
418
#ifdef ARCH_X86
419
            asm volatile(
420
                "movl %2, %1                \n\t"
421
                "movl %1, %0                \n\t"
422
                "addl %1, %1                \n\t"
423
                "sbbl %1, %1                \n\t"
424
                "xorl %1, %0                \n\t"
425
                "subl %1, %0                \n\t"
426
                "andl $1, %1                \n\t"
427
                : "=&r" (alevel), "=&r" (sign)
428
                : "g" (level)
429
            );
430
#else
431
            sign = 0;
432
            alevel = level;
433
            if (alevel < 0) {
434
                sign = 1;
435
                alevel = -alevel;
436
            }
437
#endif
438
//            code = get_rl_index(rl, 0, run, alevel);
439
            if (alevel > mpeg1_max_level[0][run])
440
                code= 111; /*rl->n*/
441
            else
442
                code= mpeg1_index_run[0][run] + alevel - 1;
443

    
444
            if (code < 111 /* rl->n */) {
445
                    /* store the vlc & sign at once */
446
                put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
447
            } else {
448
                /* escape seems to be pretty rare <5% so i dont optimize it */
449
                put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
450
                /* escape: only clip in this case */
451
                put_bits(&s->pb, 6, run);
452
                if (alevel < 128) {
453
                    put_bits(&s->pb, 8, level & 0xff);
454
                } else {
455
                    if (level < 0) {
456
                        put_bits(&s->pb, 16, 0x8001 + level + 255);
457
                    } else {
458
                        put_bits(&s->pb, 16, level & 0xffff);
459
                    }
460
                }
461
            }
462
            last_non_zero = i;
463
        }
464
    }
465
    /* end of block */
466
    put_bits(&s->pb, 2, 0x2);
467
}
468

    
469
/******************************************/
470
/* decoding */
471

    
472
static VLC dc_lum_vlc;
473
static VLC dc_chroma_vlc;
474
static VLC mv_vlc;
475
static VLC mbincr_vlc;
476
static VLC mb_ptype_vlc;
477
static VLC mb_btype_vlc;
478
static VLC mb_pat_vlc;
479

    
480
void mpeg1_init_vlc(MpegEncContext *s)
481
{
482
    static int done = 0;
483

    
484
    if (!done) {
485

    
486
        init_vlc(&dc_lum_vlc, 9, 12, 
487
                 vlc_dc_lum_bits, 1, 1,
488
                 vlc_dc_lum_code, 2, 2);
489
        init_vlc(&dc_chroma_vlc, 9, 12, 
490
                 vlc_dc_chroma_bits, 1, 1,
491
                 vlc_dc_chroma_code, 2, 2);
492
        init_vlc(&mv_vlc, 9, 17, 
493
                 &mbMotionVectorTable[0][1], 2, 1,
494
                 &mbMotionVectorTable[0][0], 2, 1);
495
        init_vlc(&mbincr_vlc, 9, 35, 
496
                 &mbAddrIncrTable[0][1], 2, 1,
497
                 &mbAddrIncrTable[0][0], 2, 1);
498
        init_vlc(&mb_pat_vlc, 9, 63, 
499
                 &mbPatTable[0][1], 2, 1,
500
                 &mbPatTable[0][0], 2, 1);
501
        
502
        init_vlc(&mb_ptype_vlc, 6, 32, 
503
                 &table_mb_ptype[0][1], 2, 1,
504
                 &table_mb_ptype[0][0], 2, 1);
505
        init_vlc(&mb_btype_vlc, 6, 32, 
506
                 &table_mb_btype[0][1], 2, 1,
507
                 &table_mb_btype[0][0], 2, 1);
508
        init_rl(&rl_mpeg1);
509
        init_rl(&rl_mpeg2);
510
        /* cannot use generic init because we must add the EOB code */
511
        init_vlc(&rl_mpeg1.vlc, 9, rl_mpeg1.n + 2, 
512
                 &rl_mpeg1.table_vlc[0][1], 4, 2,
513
                 &rl_mpeg1.table_vlc[0][0], 4, 2);
514
        init_vlc(&rl_mpeg2.vlc, 9, rl_mpeg2.n + 2, 
515
                 &rl_mpeg2.table_vlc[0][1], 4, 2,
516
                 &rl_mpeg2.table_vlc[0][0], 4, 2);
517
    }
518
}
519

    
520
static inline int get_dmv(MpegEncContext *s)
521
{
522
    if(get_bits1(&s->gb)) 
523
        return 1 - (get_bits1(&s->gb) << 1);
524
    else
525
        return 0;
526
}
527

    
528
static inline int get_qscale(MpegEncContext *s)
529
{
530
    int qscale;
531
    if (s->mpeg2) {
532
        if (s->q_scale_type) {
533
            qscale = non_linear_qscale[get_bits(&s->gb, 5)];
534
        } else {
535
            qscale = get_bits(&s->gb, 5) << 1;
536
        }
537
    } else {
538
        /* for mpeg1, we use the generic unquant code */
539
        qscale = get_bits(&s->gb, 5);
540
    }
541
    return qscale;
542
}
543

    
544
/* motion type (for mpeg2) */
545
#define MT_FIELD 1
546
#define MT_FRAME 2
547
#define MT_16X8  2
548
#define MT_DMV   3
549

    
550
static int mpeg_decode_mb(MpegEncContext *s,
551
                          DCTELEM block[6][64])
552
{
553
    int i, j, k, cbp, val, code, mb_type, motion_type;
554
    
555
    /* skip mb handling */
556
    if (s->mb_incr == 0) {
557
        /* read again increment */
558
        s->mb_incr = 1;
559
        for(;;) {
560
            code = get_vlc(&s->gb, &mbincr_vlc);
561
            if (code < 0)
562
                return 1; /* error = end of slice */
563
            if (code >= 33) {
564
                if (code == 33) {
565
                    s->mb_incr += 33;
566
                }
567
                /* otherwise, stuffing, nothing to do */
568
            } else {
569
                s->mb_incr += code;
570
                break;
571
            }
572
        }
573
    }
574
    if (++s->mb_x >= s->mb_width) {
575
        s->mb_x = 0;
576
        if (s->mb_y >= (s->mb_height - 1))
577
            return -1;
578
        s->mb_y++;
579
    }
580
    dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
581

    
582
    if (--s->mb_incr != 0) {
583
        /* skip mb */
584
        s->mb_intra = 0;
585
        for(i=0;i<6;i++)
586
            s->block_last_index[i] = -1;
587
        s->mv_type = MV_TYPE_16X16;
588
        if (s->pict_type == P_TYPE) {
589
            /* if P type, zero motion vector is implied */
590
            s->mv_dir = MV_DIR_FORWARD;
591
            s->mv[0][0][0] = s->mv[0][0][1] = 0;
592
            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
593
            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
594
        } else {
595
            /* if B type, reuse previous vectors and directions */
596
            s->mv[0][0][0] = s->last_mv[0][0][0];
597
            s->mv[0][0][1] = s->last_mv[0][0][1];
598
            s->mv[1][0][0] = s->last_mv[1][0][0];
599
            s->mv[1][0][1] = s->last_mv[1][0][1];
600
        }
601
        s->mb_skiped = 1;
602
        return 0;
603
    }
604

    
605
    switch(s->pict_type) {
606
    default:
607
    case I_TYPE:
608
        if (get_bits1(&s->gb) == 0) {
609
            if (get_bits1(&s->gb) == 0)
610
                return -1;
611
            mb_type = MB_QUANT | MB_INTRA;
612
        } else {
613
            mb_type = MB_INTRA;
614
        }
615
        break;
616
    case P_TYPE:
617
        mb_type = get_vlc(&s->gb, &mb_ptype_vlc);
618
        if (mb_type < 0)
619
            return -1;
620
        break;
621
    case B_TYPE:
622
        mb_type = get_vlc(&s->gb, &mb_btype_vlc);
623
        if (mb_type < 0)
624
            return -1;
625
        break;
626
    }
627
    dprintf("mb_type=%x\n", mb_type);
628
    motion_type = 0; /* avoid warning */
629
    if (mb_type & (MB_FOR|MB_BACK)) {
630
        /* get additionnal motion vector type */
631
        if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) 
632
            motion_type = MT_FRAME;
633
        else
634
            motion_type = get_bits(&s->gb, 2);
635
    }
636
    /* compute dct type */
637
    if (s->picture_structure == PICT_FRAME && 
638
        !s->frame_pred_frame_dct &&
639
        (mb_type & (MB_PAT | MB_INTRA))) {
640
        s->interlaced_dct = get_bits1(&s->gb);
641
#ifdef DEBUG
642
        if (s->interlaced_dct)
643
            printf("interlaced_dct\n");
644
#endif
645
    } else {
646
        s->interlaced_dct = 0; /* frame based */
647
    }
648

    
649
    if (mb_type & MB_QUANT) {
650
        s->qscale = get_qscale(s);
651
    }
652
    if (mb_type & MB_INTRA) {
653
        if (s->concealment_motion_vectors) {
654
            /* just parse them */
655
            if (s->picture_structure != PICT_FRAME) 
656
                skip_bits1(&s->gb); /* field select */
657
            mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
658
            mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
659
        }
660
        s->mb_intra = 1;
661
        cbp = 0x3f;
662
        memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
663
    } else {
664
        s->mb_intra = 0;
665
        cbp = 0;
666
    }
667
    /* special case of implicit zero motion vector */
668
    if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
669
        s->mv_dir = MV_DIR_FORWARD;
670
        s->mv_type = MV_TYPE_16X16;
671
        s->last_mv[0][0][0] = 0;
672
        s->last_mv[0][0][1] = 0;
673
        s->last_mv[0][1][0] = 0;
674
        s->last_mv[0][1][1] = 0;
675
        s->mv[0][0][0] = 0;
676
        s->mv[0][0][1] = 0;
677
    } else if (mb_type & (MB_FOR | MB_BACK)) {
678
        /* motion vectors */
679
        s->mv_dir = 0;
680
        for(i=0;i<2;i++) {
681
            if (mb_type & (MB_FOR >> i)) {
682
                s->mv_dir |= (MV_DIR_FORWARD >> i);
683
                dprintf("motion_type=%d\n", motion_type);
684
                switch(motion_type) {
685
                case MT_FRAME: /* or MT_16X8 */
686
                    if (s->picture_structure == PICT_FRAME) {
687
                        /* MT_FRAME */
688
                        s->mv_type = MV_TYPE_16X16;
689
                        for(k=0;k<2;k++) {
690
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], 
691
                                                     s->last_mv[i][0][k]);
692
                            s->last_mv[i][0][k] = val;
693
                            s->last_mv[i][1][k] = val;
694
                            /* full_pel: only for mpeg1 */
695
                            if (s->full_pel[i])
696
                                val = val << 1;
697
                            s->mv[i][0][k] = val;
698
                            dprintf("mv%d: %d\n", k, val);
699
                        }
700
                    } else {
701
                        /* MT_16X8 */
702
                        s->mv_type = MV_TYPE_16X8;
703
                        for(j=0;j<2;j++) {
704
                            s->field_select[i][j] = get_bits1(&s->gb);
705
                            for(k=0;k<2;k++) {
706
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
707
                                                         s->last_mv[i][j][k]);
708
                                s->last_mv[i][j][k] = val;
709
                                s->mv[i][j][k] = val;
710
                            }
711
                        }
712
                    }
713
                    break;
714
                case MT_FIELD:
715
                    if (s->picture_structure == PICT_FRAME) {
716
                        s->mv_type = MV_TYPE_FIELD;
717
                        for(j=0;j<2;j++) {
718
                            s->field_select[i][j] = get_bits1(&s->gb);
719
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
720
                                                     s->last_mv[i][j][0]);
721
                            s->last_mv[i][j][0] = val;
722
                            s->mv[i][j][0] = val;
723
                            dprintf("fmx=%d\n", val);
724
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
725
                                                     s->last_mv[i][j][1] >> 1);
726
                            s->last_mv[i][j][1] = val << 1;
727
                            s->mv[i][j][1] = val;
728
                            dprintf("fmy=%d\n", val);
729
                        }
730
                    } else {
731
                        s->mv_type = MV_TYPE_16X16;
732
                        s->field_select[i][0] = get_bits1(&s->gb);
733
                        for(k=0;k<2;k++) {
734
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
735
                                                     s->last_mv[i][0][k]);
736
                            s->last_mv[i][0][k] = val;
737
                            s->last_mv[i][1][k] = val;
738
                            s->mv[i][0][k] = val;
739
                        }
740
                    }
741
                    break;
742
                case MT_DMV:
743
                    {
744
                        int dmx, dmy, mx, my, m;
745

    
746
                        mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
747
                                                s->last_mv[i][0][0]);
748
                        s->last_mv[i][0][0] = mx;
749
                        s->last_mv[i][1][0] = mx;
750
                        dmx = get_dmv(s);
751
                        my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
752
                                                s->last_mv[i][0][1] >> 1);
753
                        dmy = get_dmv(s);
754
                        s->mv_type = MV_TYPE_DMV;
755
                        /* XXX: totally broken */
756
                        if (s->picture_structure == PICT_FRAME) {
757
                            s->last_mv[i][0][1] = my << 1;
758
                            s->last_mv[i][1][1] = my << 1;
759

    
760
                            m = s->top_field_first ? 1 : 3;
761
                            /* top -> top pred */
762
                            s->mv[i][0][0] = mx; 
763
                            s->mv[i][0][1] = my << 1;
764
                            s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
765
                            s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
766
                            m = 4 - m;
767
                            s->mv[i][2][0] = mx;
768
                            s->mv[i][2][1] = my << 1;
769
                            s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
770
                            s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
771
                        } else {
772
                            s->last_mv[i][0][1] = my;
773
                            s->last_mv[i][1][1] = my;
774
                            s->mv[i][0][0] = mx;
775
                            s->mv[i][0][1] = my;
776
                            s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
777
                            s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 
778
                                /* + 2 * cur_field */;
779
                        }
780
                    }
781
                    break;
782
                }
783
            }
784
        }
785
    }
786

    
787
    if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
788
        skip_bits1(&s->gb); /* marker */
789
    }
790
    
791
    if (mb_type & MB_PAT) {
792
        cbp = get_vlc(&s->gb, &mb_pat_vlc);
793
        if (cbp < 0)
794
            return -1;
795
        cbp++;
796
    }
797
    dprintf("cbp=%x\n", cbp);
798

    
799
    if (s->mpeg2) {
800
        if (s->mb_intra) {
801
            for(i=0;i<6;i++) {
802
                if (cbp & (1 << (5 - i))) {
803
                    if (mpeg2_decode_block_intra(s, block[i], i) < 0)
804
                        return -1;
805
                }
806
            }
807
        } else {
808
            for(i=0;i<6;i++) {
809
                if (cbp & (1 << (5 - i))) {
810
                    if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
811
                        return -1;
812
                }
813
            }
814
        }
815
    } else {
816
        for(i=0;i<6;i++) {
817
            if (cbp & (1 << (5 - i))) {
818
                if (mpeg1_decode_block(s, block[i], i) < 0)
819
                    return -1;
820
            }
821
        }
822
    }
823
    return 0;
824
}
825

    
826
/* as h263, but only 17 codes */
827
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
828
{
829
    int code, sign, val, m, l, shift;
830

    
831
    code = get_vlc(&s->gb, &mv_vlc);
832
    if (code < 0) {
833
        return 0xffff;
834
    }
835
    if (code == 0) {
836
        return pred;
837
    }
838
    sign = get_bits1(&s->gb);
839
    shift = fcode - 1;
840
    val = (code - 1) << shift;
841
    if (shift > 0)
842
        val |= get_bits(&s->gb, shift);
843
    val++;
844
    if (sign)
845
        val = -val;
846
    val += pred;
847
    
848
    /* modulo decoding */
849
    l = (1 << shift) * 16;
850
    m = 2 * l;
851
    if (val < -l) {
852
        val += m;
853
    } else if (val >= l) {
854
        val -= m;
855
    }
856
    return val;
857
}
858

    
859
static inline int decode_dc(MpegEncContext *s, int component)
860
{
861
    int code, diff;
862

    
863
    if (component == 0) {
864
        code = get_vlc(&s->gb, &dc_lum_vlc);
865
    } else {
866
        code = get_vlc(&s->gb, &dc_chroma_vlc);
867
    }
868
    if (code < 0)
869
        return 0xffff;
870
    if (code == 0) {
871
        diff = 0;
872
    } else {
873
        diff = get_bits(&s->gb, code);
874
        if ((diff & (1 << (code - 1))) == 0) 
875
            diff = (-1 << code) | (diff + 1);
876
    }
877
    return diff;
878
}
879

    
880
static int mpeg1_decode_block(MpegEncContext *s, 
881
                               DCTELEM *block, 
882
                               int n)
883
{
884
    int level, dc, diff, i, j, run;
885
    int code, component;
886
    RLTable *rl = &rl_mpeg1;
887

    
888
    if (s->mb_intra) {
889
        /* DC coef */
890
        component = (n <= 3 ? 0 : n - 4 + 1);
891
        diff = decode_dc(s, component);
892
        if (diff >= 0xffff)
893
            return -1;
894
        dc = s->last_dc[component];
895
        dc += diff;
896
        s->last_dc[component] = dc;
897
        block[0] = dc;
898
        dprintf("dc=%d diff=%d\n", dc, diff);
899
        i = 1;
900
    } else {
901
        int bit_cnt, v;
902
        UINT32 bit_buf;
903
        UINT8 *buf_ptr;
904
        i = 0;
905
        /* special case for the first coef. no need to add a second vlc table */
906
        SAVE_BITS(&s->gb);
907
        SHOW_BITS(&s->gb, v, 2);
908
        if (v & 2) {
909
            run = 0;
910
            level = 1 - ((v & 1) << 1);
911
            FLUSH_BITS(2);
912
            RESTORE_BITS(&s->gb);
913
            goto add_coef;
914
        }
915
        RESTORE_BITS(&s->gb);
916
    }
917

    
918
    /* now quantify & encode AC coefs */
919
    for(;;) {
920
        code = get_vlc(&s->gb, &rl->vlc);
921
        if (code < 0) {
922
            return -1;
923
        }
924
        if (code == 112) {
925
            break;
926
        } else if (code == 111) {
927
            /* escape */
928
            run = get_bits(&s->gb, 6);
929
            level = get_bits(&s->gb, 8);
930
            level = (level << 24) >> 24;
931
            if (level == -128) {
932
                level = get_bits(&s->gb, 8) - 256;
933
            } else if (level == 0) {
934
                level = get_bits(&s->gb, 8);
935
            }
936
        } else {
937
            run = rl->table_run[code];
938
            level = rl->table_level[code];
939
            if (get_bits1(&s->gb))
940
                level = -level;
941
        }
942
        i += run;
943
        if (i >= 64)
944
            return -1;
945
    add_coef:
946
        dprintf("%d: run=%d level=%d\n", n, run, level);
947
        j = zigzag_direct[i];
948
        block[j] = level;
949
        i++;
950
    }
951
    s->block_last_index[n] = i-1;
952
    return 0;
953
}
954

    
955
/* Also does unquantization here, since I will never support mpeg2
956
   encoding */
957
static int mpeg2_decode_block_non_intra(MpegEncContext *s, 
958
                                        DCTELEM *block, 
959
                                        int n)
960
{
961
    int level, i, j, run;
962
    int code;
963
    RLTable *rl = &rl_mpeg1;
964
    const UINT8 *scan_table;
965
    const UINT16 *matrix;
966
    int mismatch;
967

    
968
    if (s->alternate_scan)
969
        scan_table = ff_alternate_vertical_scan;
970
    else
971
        scan_table = zigzag_direct;
972
    mismatch = 1;
973

    
974
    {
975
        int bit_cnt, v;
976
        UINT32 bit_buf;
977
        UINT8 *buf_ptr;
978
        i = 0;
979
        if (n < 4) 
980
            matrix = s->non_intra_matrix;
981
        else
982
            matrix = s->chroma_non_intra_matrix;
983
            
984
        /* special case for the first coef. no need to add a second vlc table */
985
        SAVE_BITS(&s->gb);
986
        SHOW_BITS(&s->gb, v, 2);
987
        if (v & 2) {
988
            run = 0;
989
            level = 1 - ((v & 1) << 1);
990
            FLUSH_BITS(2);
991
            RESTORE_BITS(&s->gb);
992
            goto add_coef;
993
        }
994
        RESTORE_BITS(&s->gb);
995
    }
996

    
997
    /* now quantify & encode AC coefs */
998
    for(;;) {
999
        code = get_vlc(&s->gb, &rl->vlc);
1000
        if (code < 0)
1001
            return -1;
1002
        if (code == 112) {
1003
            break;
1004
        } else if (code == 111) {
1005
            /* escape */
1006
            run = get_bits(&s->gb, 6);
1007
            level = get_bits(&s->gb, 12);
1008
            level = (level << 20) >> 20;
1009
        } else {
1010
            run = rl->table_run[code];
1011
            level = rl->table_level[code];
1012
            if (get_bits1(&s->gb))
1013
                level = -level;
1014
        }
1015
        i += run;
1016
        if (i >= 64)
1017
            return -1;
1018
    add_coef:
1019
        j = scan_table[i];
1020
        dprintf("%d: run=%d level=%d\n", n, run, level);
1021
        /* XXX: optimize */
1022
        if (level > 0) {
1023
            level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
1024
        } else {
1025
            level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
1026
            level = -level;
1027
        }
1028
        /* XXX: is it really necessary to saturate since the encoder
1029
           knows whats going on ? */
1030
        mismatch ^= level;
1031
        block[j] = level;
1032
        i++;
1033
    }
1034
    block[63] ^= (mismatch & 1);
1035
    s->block_last_index[n] = i;
1036
    return 0;
1037
}
1038

    
1039
static int mpeg2_decode_block_intra(MpegEncContext *s, 
1040
                                    DCTELEM *block, 
1041
                                    int n)
1042
{
1043
    int level, dc, diff, i, j, run;
1044
    int code, component;
1045
    RLTable *rl;
1046
    const UINT8 *scan_table;
1047
    const UINT16 *matrix;
1048
    int mismatch;
1049

    
1050
    if (s->alternate_scan)
1051
        scan_table = ff_alternate_vertical_scan;
1052
    else
1053
        scan_table = zigzag_direct;
1054

    
1055
    /* DC coef */
1056
    component = (n <= 3 ? 0 : n - 4 + 1);
1057
    diff = decode_dc(s, component);
1058
    if (diff >= 0xffff)
1059
        return -1;
1060
    dc = s->last_dc[component];
1061
    dc += diff;
1062
    s->last_dc[component] = dc;
1063
    block[0] = dc << (3 - s->intra_dc_precision);
1064
    dprintf("dc=%d\n", block[0]);
1065
    mismatch = block[0] ^ 1;
1066
    i = 1;
1067
    if (s->intra_vlc_format)
1068
        rl = &rl_mpeg2;
1069
    else
1070
        rl = &rl_mpeg1;
1071
    if (n < 4) 
1072
        matrix = s->intra_matrix;
1073
    else
1074
        matrix = s->chroma_intra_matrix;
1075

    
1076
    /* now quantify & encode AC coefs */
1077
    for(;;) {
1078
        code = get_vlc(&s->gb, &rl->vlc);
1079
        if (code < 0)
1080
            return -1;
1081
        if (code == 112) {
1082
            break;
1083
        } else if (code == 111) {
1084
            /* escape */
1085
            run = get_bits(&s->gb, 6);
1086
            level = get_bits(&s->gb, 12);
1087
            level = (level << 20) >> 20;
1088
        } else {
1089
            run = rl->table_run[code];
1090
            level = rl->table_level[code];
1091
            if (get_bits1(&s->gb))
1092
                level = -level;
1093
        }
1094
        i += run;
1095
        if (i >= 64)
1096
            return -1;
1097
        j = scan_table[i];
1098
        dprintf("%d: run=%d level=%d\n", n, run, level);
1099
        level = (level * s->qscale * matrix[j]) / 16;
1100
        /* XXX: is it really necessary to saturate since the encoder
1101
           knows whats going on ? */
1102
        mismatch ^= level;
1103
        block[j] = level;
1104
        i++;
1105
    }
1106
    block[63] ^= (mismatch & 1);
1107
    s->block_last_index[n] = i;
1108
    return 0;
1109
}
1110

    
1111
/* compressed picture size */
1112
#define PICTURE_BUFFER_SIZE 100000
1113

    
1114
typedef struct Mpeg1Context {
1115
    MpegEncContext mpeg_enc_ctx;
1116
    UINT32 header_state;
1117
    int start_code; /* current start code */
1118
    UINT8 buffer[PICTURE_BUFFER_SIZE]; 
1119
    UINT8 *buf_ptr;
1120
    int buffer_size;
1121
    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1122
} Mpeg1Context;
1123

    
1124
static int mpeg_decode_init(AVCodecContext *avctx)
1125
{
1126
    Mpeg1Context *s = avctx->priv_data;
1127

    
1128
    s->header_state = 0xff;
1129
    s->mpeg_enc_ctx_allocated = 0;
1130
    s->buffer_size = PICTURE_BUFFER_SIZE;
1131
    s->start_code = -1;
1132
    s->buf_ptr = s->buffer;
1133
    s->mpeg_enc_ctx.picture_number = 0;
1134
    return 0;
1135
}
1136

    
1137
/* return the 8 bit start code value and update the search
1138
   state. Return -1 if no start code found */
1139
static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end, 
1140
                           UINT32 *header_state)
1141
{
1142
    UINT8 *buf_ptr;
1143
    unsigned int state, v;
1144
    int val;
1145

    
1146
    state = *header_state;
1147
    buf_ptr = *pbuf_ptr;
1148
    while (buf_ptr < buf_end) {
1149
        v = *buf_ptr++;
1150
        if (state == 0x000001) {
1151
            state = ((state << 8) | v) & 0xffffff;
1152
            val = state;
1153
            goto found;
1154
        }
1155
        state = ((state << 8) | v) & 0xffffff;
1156
    }
1157
    val = -1;
1158
 found:
1159
    *pbuf_ptr = buf_ptr;
1160
    *header_state = state;
1161
    return val;
1162
}
1163

    
1164
static int mpeg1_decode_picture(AVCodecContext *avctx, 
1165
                                UINT8 *buf, int buf_size)
1166
{
1167
    Mpeg1Context *s1 = avctx->priv_data;
1168
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1169
    int ref, f_code;
1170

    
1171
    init_get_bits(&s->gb, buf, buf_size);
1172

    
1173
    ref = get_bits(&s->gb, 10); /* temporal ref */
1174
    s->pict_type = get_bits(&s->gb, 3);
1175
    dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
1176
    skip_bits(&s->gb, 16);
1177
    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
1178
        s->full_pel[0] = get_bits1(&s->gb);
1179
        f_code = get_bits(&s->gb, 3);
1180
        if (f_code == 0)
1181
            return -1;
1182
        s->mpeg_f_code[0][0] = f_code;
1183
        s->mpeg_f_code[0][1] = f_code;
1184
    }
1185
    if (s->pict_type == B_TYPE) {
1186
        s->full_pel[1] = get_bits1(&s->gb);
1187
        f_code = get_bits(&s->gb, 3);
1188
        if (f_code == 0)
1189
            return -1;
1190
        s->mpeg_f_code[1][0] = f_code;
1191
        s->mpeg_f_code[1][1] = f_code;
1192
    }
1193
    s->y_dc_scale = 8;
1194
    s->c_dc_scale = 8;
1195
    s->first_slice = 1;
1196
    return 0;
1197
}
1198

    
1199
static void mpeg_decode_sequence_extension(MpegEncContext *s)
1200
{
1201
    int horiz_size_ext, vert_size_ext;
1202
    int bit_rate_ext, vbv_buf_ext, low_delay;
1203
    int frame_rate_ext_n, frame_rate_ext_d;
1204

    
1205
    skip_bits(&s->gb, 8); /* profil and level */
1206
    skip_bits(&s->gb, 1); /* progressive_sequence */
1207
    skip_bits(&s->gb, 2); /* chroma_format */
1208
    horiz_size_ext = get_bits(&s->gb, 2);
1209
    vert_size_ext = get_bits(&s->gb, 2);
1210
    s->width |= (horiz_size_ext << 12);
1211
    s->height |= (vert_size_ext << 12);
1212
    bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
1213
    s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
1214
    skip_bits1(&s->gb); /* marker */
1215
    vbv_buf_ext = get_bits(&s->gb, 8);
1216
    low_delay = get_bits1(&s->gb);
1217
    frame_rate_ext_n = get_bits(&s->gb, 2);
1218
    frame_rate_ext_d = get_bits(&s->gb, 5);
1219
    if (frame_rate_ext_d >= 1)
1220
        s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
1221
    dprintf("sequence extension\n");
1222
    s->mpeg2 = 1;
1223
}
1224

    
1225
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1226
{
1227
    int i, v, j;
1228

    
1229
    dprintf("matrix extension\n");
1230

    
1231
    if (get_bits1(&s->gb)) {
1232
        for(i=0;i<64;i++) {
1233
            v = get_bits(&s->gb, 8);
1234
            j = zigzag_direct[i];
1235
            s->intra_matrix[j] = v;
1236
            s->chroma_intra_matrix[j] = v;
1237
        }
1238
    }
1239
    if (get_bits1(&s->gb)) {
1240
        for(i=0;i<64;i++) {
1241
            v = get_bits(&s->gb, 8);
1242
            j = zigzag_direct[i];
1243
            s->non_intra_matrix[j] = v;
1244
            s->chroma_non_intra_matrix[j] = v;
1245
        }
1246
    }
1247
    if (get_bits1(&s->gb)) {
1248
        for(i=0;i<64;i++) {
1249
            v = get_bits(&s->gb, 8);
1250
            j = zigzag_direct[i];
1251
            s->chroma_intra_matrix[j] = v;
1252
        }
1253
    }
1254
    if (get_bits1(&s->gb)) {
1255
        for(i=0;i<64;i++) {
1256
            v = get_bits(&s->gb, 8);
1257
            j = zigzag_direct[i];
1258
            s->chroma_non_intra_matrix[j] = v;
1259
        }
1260
    }
1261
}
1262

    
1263
static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
1264
{
1265
    s->full_pel[0] = s->full_pel[1] = 0;
1266
    s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1267
    s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1268
    s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1269
    s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1270
    s->intra_dc_precision = get_bits(&s->gb, 2);
1271
    s->picture_structure = get_bits(&s->gb, 2);
1272
    s->top_field_first = get_bits1(&s->gb);
1273
    s->frame_pred_frame_dct = get_bits1(&s->gb);
1274
    s->concealment_motion_vectors = get_bits1(&s->gb);
1275
    s->q_scale_type = get_bits1(&s->gb);
1276
    s->intra_vlc_format = get_bits1(&s->gb);
1277
    s->alternate_scan = get_bits1(&s->gb);
1278
    s->repeat_first_field = get_bits1(&s->gb);
1279
    s->chroma_420_type = get_bits1(&s->gb);
1280
    s->progressive_frame = get_bits1(&s->gb);
1281
    /* composite display not parsed */
1282
    dprintf("intra_dc_precion=%d\n", s->intra_dc_precision);
1283
    dprintf("picture_structure=%d\n", s->picture_structure);
1284
    dprintf("conceal=%d\n", s->concealment_motion_vectors);
1285
    dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
1286
    dprintf("alternate_scan=%d\n", s->alternate_scan);
1287
    dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1288
}
1289

    
1290
static void mpeg_decode_extension(AVCodecContext *avctx, 
1291
                                  UINT8 *buf, int buf_size)
1292
{
1293
    Mpeg1Context *s1 = avctx->priv_data;
1294
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1295
    int ext_type;
1296

    
1297
    init_get_bits(&s->gb, buf, buf_size);
1298
    
1299
    ext_type = get_bits(&s->gb, 4);
1300
    switch(ext_type) {
1301
    case 0x1:
1302
        /* sequence ext */
1303
        mpeg_decode_sequence_extension(s);
1304
        break;
1305
    case 0x3:
1306
        /* quant matrix extension */
1307
        mpeg_decode_quant_matrix_extension(s);
1308
        break;
1309
    case 0x8:
1310
        /* picture extension */
1311
        mpeg_decode_picture_coding_extension(s);
1312
        break;
1313
    }
1314
}
1315

    
1316
/* return 1 if end of frame */
1317
static int mpeg_decode_slice(AVCodecContext *avctx, 
1318
                              AVPicture *pict,
1319
                              int start_code,
1320
                              UINT8 *buf, int buf_size)
1321
{
1322
    Mpeg1Context *s1 = avctx->priv_data;
1323
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1324
    int ret;
1325

    
1326
    start_code = (start_code - 1) & 0xff;
1327
    if (start_code >= s->mb_height)
1328
        return -1;
1329
    s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
1330
    s->last_dc[1] = s->last_dc[0];
1331
    s->last_dc[2] = s->last_dc[0];
1332
    memset(s->last_mv, 0, sizeof(s->last_mv));
1333
    s->mb_x = -1;
1334
    s->mb_y = start_code;
1335
    s->mb_incr = 0;
1336

    
1337
    /* start frame decoding */
1338
    if (s->first_slice) {
1339
        s->first_slice = 0;
1340
        MPV_frame_start(s);
1341
    }
1342

    
1343
    init_get_bits(&s->gb, buf, buf_size);
1344

    
1345
    s->qscale = get_qscale(s);
1346
    /* extra slice info */
1347
    while (get_bits1(&s->gb) != 0) {
1348
        skip_bits(&s->gb, 8);
1349
    }
1350

    
1351
    for(;;) {
1352
        memset(s->block, 0, sizeof(s->block));
1353
        ret = mpeg_decode_mb(s, s->block);
1354
        dprintf("ret=%d\n", ret);
1355
        if (ret < 0)
1356
            return -1;
1357
        if (ret == 1)
1358
            break;
1359
        MPV_decode_mb(s, s->block);
1360
    }
1361
    
1362
    /* end of slice reached */
1363
    if (s->mb_x == (s->mb_width - 1) &&
1364
        s->mb_y == (s->mb_height - 1)) {
1365
        /* end of image */
1366
        UINT8 **picture;
1367

    
1368
        MPV_frame_end(s);
1369

    
1370
        /* XXX: incorrect reported qscale for mpeg2 */
1371
        if (s->pict_type == B_TYPE) {
1372
            picture = s->current_picture;
1373
            avctx->quality = s->qscale;
1374
        } else {
1375
            /* latency of 1 frame for I and P frames */
1376
            /* XXX: use another variable than picture_number */
1377
            if (s->picture_number == 0) {
1378
                picture = NULL;
1379
            } else {
1380
                picture = s->last_picture;
1381
                avctx->quality = s->last_qscale;
1382
            }
1383
            s->last_qscale = s->qscale;
1384
            s->picture_number++;
1385
        }
1386
        if (picture) {
1387
            pict->data[0] = picture[0];
1388
            pict->data[1] = picture[1];
1389
            pict->data[2] = picture[2];
1390
            pict->linesize[0] = s->linesize;
1391
            pict->linesize[1] = s->linesize / 2;
1392
            pict->linesize[2] = s->linesize / 2;
1393
            return 1;
1394
        } else {
1395
            return 0;
1396
        }
1397
    } else {
1398
        return 0;
1399
    }
1400
}
1401

    
1402
static int mpeg1_decode_sequence(AVCodecContext *avctx, 
1403
                                 UINT8 *buf, int buf_size)
1404
{
1405
    Mpeg1Context *s1 = avctx->priv_data;
1406
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1407
    int width, height, i, v, j;
1408
    
1409
    init_get_bits(&s->gb, buf, buf_size);
1410

    
1411
    width = get_bits(&s->gb, 12);
1412
    height = get_bits(&s->gb, 12);
1413
    skip_bits(&s->gb, 4);
1414
    s->frame_rate_index = get_bits(&s->gb, 4);
1415
    if (s->frame_rate_index == 0)
1416
        return -1;
1417
    s->bit_rate = get_bits(&s->gb, 18) * 400;
1418
    if (get_bits1(&s->gb) == 0) /* marker */
1419
        return -1;
1420
    if (width <= 0 || height <= 0 ||
1421
        (width % 2) != 0 || (height % 2) != 0)
1422
        return -1;
1423
    if (width != s->width ||
1424
        height != s->height) {
1425
        /* start new mpeg1 context decoding */
1426
        s->out_format = FMT_MPEG1;
1427
        if (s1->mpeg_enc_ctx_allocated) {
1428
            MPV_common_end(s);
1429
        }
1430
        s->width = width;
1431
        s->height = height;
1432
        s->has_b_frames = 1;
1433
        s->avctx = avctx;
1434
        avctx->width = width;
1435
        avctx->height = height;
1436
        avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
1437
        avctx->bit_rate = s->bit_rate;
1438
        
1439
        if (MPV_common_init(s) < 0)
1440
            return -1;
1441
        mpeg1_init_vlc(s);
1442
        s1->mpeg_enc_ctx_allocated = 1;
1443
    }
1444

    
1445
    skip_bits(&s->gb, 10); /* vbv_buffer_size */
1446
    skip_bits(&s->gb, 1);
1447

    
1448
    /* get matrix */
1449
    if (get_bits1(&s->gb)) {
1450
        for(i=0;i<64;i++) {
1451
            v = get_bits(&s->gb, 8);
1452
            j = zigzag_direct[i];
1453
            s->intra_matrix[j] = v;
1454
            s->chroma_intra_matrix[j] = v;
1455
        }
1456
#ifdef DEBUG
1457
        dprintf("intra matrix present\n");
1458
        for(i=0;i<64;i++)
1459
            dprintf(" %d", s->intra_matrix[zigzag_direct[i]]);
1460
        printf("\n");
1461
#endif
1462
    } else {
1463
        for(i=0;i<64;i++) {
1464
            v = default_intra_matrix[i];
1465
            s->intra_matrix[i] = v;
1466
            s->chroma_intra_matrix[i] = v;
1467
        }
1468
    }
1469
    if (get_bits1(&s->gb)) {
1470
        for(i=0;i<64;i++) {
1471
            v = get_bits(&s->gb, 8);
1472
            j = zigzag_direct[i];
1473
            s->non_intra_matrix[j] = v;
1474
            s->chroma_non_intra_matrix[j] = v;
1475
        }
1476
#ifdef DEBUG
1477
        dprintf("non intra matrix present\n");
1478
        for(i=0;i<64;i++)
1479
            dprintf(" %d", s->non_intra_matrix[zigzag_direct[i]]);
1480
        printf("\n");
1481
#endif
1482
    } else {
1483
        for(i=0;i<64;i++) {
1484
            v = default_non_intra_matrix[i];
1485
            s->non_intra_matrix[i] = v;
1486
            s->chroma_non_intra_matrix[i] = v;
1487
        }
1488
    }
1489

    
1490
    /* we set mpeg2 parameters so that it emulates mpeg1 */
1491
    s->progressive_sequence = 1;
1492
    s->progressive_frame = 1;
1493
    s->picture_structure = PICT_FRAME;
1494
    s->frame_pred_frame_dct = 1;
1495
    s->mpeg2 = 0;
1496
    return 0;
1497
}
1498

    
1499
/* handle buffering and image synchronisation */
1500
static int mpeg_decode_frame(AVCodecContext *avctx, 
1501
                             void *data, int *data_size,
1502
                             UINT8 *buf, int buf_size)
1503
{
1504
    Mpeg1Context *s = avctx->priv_data;
1505
    UINT8 *buf_end, *buf_ptr, *buf_start;
1506
    int len, start_code_found, ret, code, start_code, input_size;
1507
    AVPicture *picture = data;
1508

    
1509
    dprintf("fill_buffer\n");
1510

    
1511
    *data_size = 0;
1512
    /* special case for last picture */
1513
    if (buf_size == 0) {
1514
        MpegEncContext *s2 = &s->mpeg_enc_ctx;
1515
        if (s2->picture_number > 0) {
1516
            picture->data[0] = s2->next_picture[0];
1517
            picture->data[1] = s2->next_picture[1];
1518
            picture->data[2] = s2->next_picture[2];
1519
            picture->linesize[0] = s2->linesize;
1520
            picture->linesize[1] = s2->linesize / 2;
1521
            picture->linesize[2] = s2->linesize / 2;
1522
            *data_size = sizeof(AVPicture);
1523
        }
1524
        return 0;
1525
    }
1526

    
1527
    buf_ptr = buf;
1528
    buf_end = buf + buf_size;
1529
    while (buf_ptr < buf_end) {
1530
        buf_start = buf_ptr;
1531
        /* find start next code */
1532
        code = find_start_code(&buf_ptr, buf_end, &s->header_state);
1533
        if (code >= 0) {
1534
            start_code_found = 1;
1535
        } else {
1536
            start_code_found = 0;
1537
        }
1538
        /* copy to buffer */
1539
        len = buf_ptr - buf_start;
1540
        if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
1541
            /* data too big : flush */
1542
            s->buf_ptr = s->buffer;
1543
            if (start_code_found)
1544
                s->start_code = code;
1545
        } else {
1546
            memcpy(s->buf_ptr, buf_start, len);
1547
            s->buf_ptr += len;
1548
            
1549
            if (start_code_found) {
1550
                /* prepare data for next start code */
1551
                input_size = s->buf_ptr - s->buffer;
1552
                start_code = s->start_code;
1553
                s->buf_ptr = s->buffer;
1554
                s->start_code = code;
1555
                switch(start_code) {
1556
                case SEQ_START_CODE:
1557
                    mpeg1_decode_sequence(avctx, s->buffer, 
1558
                                          input_size);
1559
                    break;
1560
                            
1561
                case PICTURE_START_CODE:
1562
                    /* we have a complete image : we try to decompress it */
1563
                    mpeg1_decode_picture(avctx, 
1564
                                         s->buffer, input_size);
1565
                    break;
1566
                case EXT_START_CODE:
1567
                    mpeg_decode_extension(avctx,
1568
                                          s->buffer, input_size);
1569
                    break;
1570
                default:
1571
                    if (start_code >= SLICE_MIN_START_CODE &&
1572
                        start_code <= SLICE_MAX_START_CODE) {
1573
                        ret = mpeg_decode_slice(avctx, picture,
1574
                                                start_code, s->buffer, input_size);
1575
                        if (ret == 1) {
1576
                            /* got a picture: exit */
1577
                            *data_size = sizeof(AVPicture);
1578
                            goto the_end;
1579
                        }
1580
                    }
1581
                    break;
1582
                }
1583
            }
1584
        }
1585
    }
1586
 the_end:
1587
    return buf_ptr - buf;
1588
}
1589

    
1590
static int mpeg_decode_end(AVCodecContext *avctx)
1591
{
1592
    Mpeg1Context *s = avctx->priv_data;
1593

    
1594
    if (s->mpeg_enc_ctx_allocated)
1595
        MPV_common_end(&s->mpeg_enc_ctx);
1596
    return 0;
1597
}
1598

    
1599
AVCodec mpeg_decoder = {
1600
    "mpegvideo",
1601
    CODEC_TYPE_VIDEO,
1602
    CODEC_ID_MPEG1VIDEO,
1603
    sizeof(Mpeg1Context),
1604
    mpeg_decode_init,
1605
    NULL,
1606
    mpeg_decode_end,
1607
    mpeg_decode_frame,
1608
};