Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpeg12.c @ 05f361f0

History | View | Annotate | Download (48 KB)

1 de6d9b64 Fabrice Bellard
/*
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 a74127c0 Fabrice Bellard
//#define DEBUG
20 de6d9b64 Fabrice Bellard
#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
static void mpeg1_encode_block(MpegEncContext *s, 
37
                         DCTELEM *block, 
38
                         int component);
39
static void mpeg1_encode_motion(MpegEncContext *s, int val);
40
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
41
static int mpeg1_decode_block(MpegEncContext *s, 
42
                              DCTELEM *block, 
43
                              int n);
44
static int mpeg2_decode_block_non_intra(MpegEncContext *s, 
45
                                        DCTELEM *block, 
46
                                        int n);
47
static int mpeg2_decode_block_intra(MpegEncContext *s, 
48
                                    DCTELEM *block, 
49
                                    int n);
50
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
51
52
static void put_header(MpegEncContext *s, int header)
53
{
54
    align_put_bits(&s->pb);
55
    put_bits(&s->pb, 32, header);
56
}
57
58
/* put sequence header if needed */
59
static void mpeg1_encode_sequence_header(MpegEncContext *s)
60
{
61
        unsigned int vbv_buffer_size;
62 d78647e8 Fabrice Bellard
        unsigned int fps, v;
63
        int n;
64 de6d9b64 Fabrice Bellard
        UINT64 time_code;
65
        
66
        if ((s->picture_number % s->gop_size) == 0) {
67
            /* mpeg1 header repeated every gop */
68
            put_header(s, SEQ_START_CODE);
69
            
70
            /* search closest frame rate */
71
            {
72
                int i, dmin, d;
73
                s->frame_rate_index = 0;
74
                dmin = 0x7fffffff;
75
                for(i=1;i<9;i++) {
76
                    d = abs(s->frame_rate - frame_rate_tab[i]);
77
                    if (d < dmin) {
78
                        dmin = d;
79
                        s->frame_rate_index = i;
80
                    }
81
                }
82
            }
83
 
84
            put_bits(&s->pb, 12, s->width);
85
            put_bits(&s->pb, 12, s->height);
86
            put_bits(&s->pb, 4, 1); /* 1/1 aspect ratio */
87
            put_bits(&s->pb, 4, s->frame_rate_index);
88
            v = s->bit_rate / 400;
89
            if (v > 0x3ffff)
90
                v = 0x3ffff;
91
            put_bits(&s->pb, 18, v);
92
            put_bits(&s->pb, 1, 1); /* marker */
93
            /* vbv buffer size: slightly greater than an I frame. We add
94
               some margin just in case */
95
            vbv_buffer_size = (3 * s->I_frame_bits) / (2 * 8);
96
            put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); 
97
            put_bits(&s->pb, 1, 1); /* constrained parameter flag */
98
            put_bits(&s->pb, 1, 0); /* no custom intra matrix */
99
            put_bits(&s->pb, 1, 0); /* no custom non intra matrix */
100
101
            put_header(s, GOP_START_CODE);
102
            put_bits(&s->pb, 1, 0); /* do drop frame */
103
            /* time code : we must convert from the real frame rate to a
104
               fake mpeg frame rate in case of low frame rate */
105
            fps = frame_rate_tab[s->frame_rate_index];
106
            time_code = s->fake_picture_number * FRAME_RATE_BASE;
107
            s->gop_picture_number = s->fake_picture_number;
108 1a565432 Fabrice Bellard
            put_bits(&s->pb, 5, (UINT32)((time_code / (fps * 3600)) % 24));
109
            put_bits(&s->pb, 6, (UINT32)((time_code / (fps * 60)) % 60));
110 de6d9b64 Fabrice Bellard
            put_bits(&s->pb, 1, 1);
111 1a565432 Fabrice Bellard
            put_bits(&s->pb, 6, (UINT32)((time_code / fps) % 60));
112
            put_bits(&s->pb, 6, (UINT32)((time_code % fps) / FRAME_RATE_BASE));
113 de6d9b64 Fabrice Bellard
            put_bits(&s->pb, 1, 1); /* closed gop */
114
            put_bits(&s->pb, 1, 0); /* broken link */
115
        }
116
117
        if (s->frame_rate < (24 * FRAME_RATE_BASE) && s->picture_number > 0) {
118
            /* insert empty P pictures to slow down to the desired
119
               frame rate. Each fake pictures takes about 20 bytes */
120
            fps = frame_rate_tab[s->frame_rate_index];
121
            n = ((s->picture_number * fps) / s->frame_rate) - 1;
122
            while (s->fake_picture_number < n) {
123
                mpeg1_skip_picture(s, s->fake_picture_number - 
124
                                   s->gop_picture_number); 
125
                s->fake_picture_number++;
126
            }
127
128
        }
129
        s->fake_picture_number++;
130
}
131
132
133
/* insert a fake P picture */
134
static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
135
{
136
    unsigned int mb_incr;
137
138
    /* mpeg1 picture header */
139
    put_header(s, PICTURE_START_CODE);
140
    /* temporal reference */
141
    put_bits(&s->pb, 10, pict_num & 0x3ff); 
142
    
143
    put_bits(&s->pb, 3, P_TYPE);
144
    put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
145
    
146
    put_bits(&s->pb, 1, 1); /* integer coordinates */
147
    put_bits(&s->pb, 3, 1); /* forward_f_code */
148
    
149
    put_bits(&s->pb, 1, 0); /* extra bit picture */
150
    
151
    /* only one slice */
152
    put_header(s, SLICE_MIN_START_CODE);
153
    put_bits(&s->pb, 5, 1); /* quantizer scale */
154
    put_bits(&s->pb, 1, 0); /* slice extra information */
155
    
156
    mb_incr = 1;
157
    put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], 
158
             mbAddrIncrTable[mb_incr - 1][0]);
159
    
160
    /* empty macroblock */
161
    put_bits(&s->pb, 3, 1); /* motion only */
162
    
163
    /* zero motion x & y */
164
    put_bits(&s->pb, 1, 1); 
165
    put_bits(&s->pb, 1, 1); 
166
167
    /* output a number of empty slice */
168
    mb_incr = s->mb_width * s->mb_height - 1;
169
    while (mb_incr > 33) {
170
        put_bits(&s->pb, 11, 0x008);
171
        mb_incr -= 33;
172
    }
173
    put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], 
174
             mbAddrIncrTable[mb_incr - 1][0]);
175
    
176
    /* empty macroblock */
177
    put_bits(&s->pb, 3, 1); /* motion only */
178
    
179
    /* zero motion x & y */
180
    put_bits(&s->pb, 1, 1); 
181
    put_bits(&s->pb, 1, 1); 
182
}
183
184
void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
185
{
186
    static int done;
187
188
    if (!done) {
189
        done = 1;
190
        init_rl(&rl_mpeg1);
191
    }
192
    mpeg1_encode_sequence_header(s);
193
194
    /* mpeg1 picture header */
195
    put_header(s, PICTURE_START_CODE);
196
    /* temporal reference */
197
    put_bits(&s->pb, 10, (s->fake_picture_number - 
198
                          s->gop_picture_number) & 0x3ff); 
199
    
200
    put_bits(&s->pb, 3, s->pict_type);
201
    put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
202
    
203
    if (s->pict_type == P_TYPE) {
204
        put_bits(&s->pb, 1, 0); /* half pel coordinates */
205
        put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
206
    }
207
    
208
    put_bits(&s->pb, 1, 0); /* extra bit picture */
209
    
210
    /* only one slice */
211
    put_header(s, SLICE_MIN_START_CODE);
212
    put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
213
    put_bits(&s->pb, 1, 0); /* slice extra information */
214
}
215
216
void mpeg1_encode_mb(MpegEncContext *s,
217
                     DCTELEM block[6][64],
218
                     int motion_x, int motion_y)
219
{
220
    int mb_incr, i, cbp, mb_x, mb_y;
221
222
    mb_x = s->mb_x;
223
    mb_y = s->mb_y;
224
225
    /* compute cbp */
226
    cbp = 0;
227
    for(i=0;i<6;i++) {
228
        if (s->block_last_index[i] >= 0)
229
            cbp |= 1 << (5 - i);
230
    }
231
232
    /* skip macroblock, except if first or last macroblock of a slice */
233
    if ((cbp | motion_x | motion_y) == 0 &&
234
        (!((mb_x | mb_y) == 0 ||
235
           (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) {
236
        s->mb_incr++;
237
    } else {
238
        /* output mb incr */
239
        mb_incr = s->mb_incr;
240
241
        while (mb_incr > 33) {
242
            put_bits(&s->pb, 11, 0x008);
243
            mb_incr -= 33;
244
        }
245
        put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], 
246
                 mbAddrIncrTable[mb_incr - 1][0]);
247
        
248
        if (s->pict_type == I_TYPE) {
249
            put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
250
        } else {
251
            if (s->mb_intra) {
252
                put_bits(&s->pb, 5, 0x03);
253
            } else {
254
                if (cbp != 0) {
255
                    if (motion_x == 0 && motion_y == 0) {
256
                        put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
257
                        put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
258
                    } else {
259
                        put_bits(&s->pb, 1, 1); /* motion + cbp */
260
                        mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); 
261
                        mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); 
262
                        put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
263
                    }
264
                } else {
265
                    put_bits(&s->pb, 3, 1); /* motion only */
266
                    mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); 
267
                    mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); 
268
                }
269
            }
270
        }
271
        for(i=0;i<6;i++) {
272
            if (cbp & (1 << (5 - i))) {
273
                mpeg1_encode_block(s, block[i], i);
274
            }
275
        }
276
        s->mb_incr = 1;
277
    }
278
    s->last_mv[0][0][0] = motion_x;
279
    s->last_mv[0][0][1] = motion_y;
280
}
281
282
static void mpeg1_encode_motion(MpegEncContext *s, int val)
283
{
284
    int code, bit_size, l, m, bits, range, sign;
285
286
    if (val == 0) {
287
        /* zero vector */
288
        code = 0;
289
        put_bits(&s->pb,
290
                 mbMotionVectorTable[0][1], 
291
                 mbMotionVectorTable[0][0]); 
292
    } else {
293
        bit_size = s->f_code - 1;
294
        range = 1 << bit_size;
295
        /* modulo encoding */
296
        l = 16 * range;
297
        m = 2 * l;
298
        if (val < -l) {
299
            val += m;
300
        } else if (val >= l) {
301
            val -= m;
302
        }
303
304
        if (val >= 0) {
305
            val--;
306
            code = (val >> bit_size) + 1;
307
            bits = val & (range - 1);
308
            sign = 0;
309
        } else {
310
            val = -val;
311
            val--;
312
            code = (val >> bit_size) + 1;
313
            bits = val & (range - 1);
314
            sign = 1;
315
        }
316
        put_bits(&s->pb,
317
                 mbMotionVectorTable[code][1], 
318
                 mbMotionVectorTable[code][0]); 
319
        put_bits(&s->pb, 1, sign);
320
        if (bit_size > 0) {
321
            put_bits(&s->pb, bit_size, bits);
322
        }
323
    }
324
}
325
326
static inline void encode_dc(MpegEncContext *s, int diff, int component)
327
{
328
    int adiff, index;
329
330
    adiff = abs(diff);
331
    index = vlc_dc_table[adiff];
332
    if (component == 0) {
333
        put_bits(&s->pb, vlc_dc_lum_bits[index], vlc_dc_lum_code[index]);
334
    } else {
335
        put_bits(&s->pb, vlc_dc_chroma_bits[index], vlc_dc_chroma_code[index]);
336
    }
337
    if (diff > 0) {
338
        put_bits(&s->pb, index, (diff & ((1 << index) - 1)));
339
    } else if (diff < 0) {
340
        put_bits(&s->pb, index, ((diff - 1) & ((1 << index) - 1)));
341
    }
342
}
343
344
static void mpeg1_encode_block(MpegEncContext *s, 
345
                               DCTELEM *block, 
346
                               int n)
347
{
348
    int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
349
    int code, component;
350
    RLTable *rl = &rl_mpeg1;
351
352
    last_index = s->block_last_index[n];
353
354
    /* DC coef */
355
    if (s->mb_intra) {
356
        component = (n <= 3 ? 0 : n - 4 + 1);
357
        dc = block[0]; /* overflow is impossible */
358
        diff = dc - s->last_dc[component];
359
        encode_dc(s, diff, component);
360
        s->last_dc[component] = dc;
361
        i = 1;
362
    } else {
363
        /* encode the first coefficient : needs to be done here because
364
           it is handled slightly differently */
365
        level = block[0];
366
        if (abs(level) == 1) {
367
                code = ((UINT32)level >> 31); /* the sign bit */
368
                put_bits(&s->pb, 2, code | 0x02);
369
                i = 1;
370
        } else {
371
            i = 0;
372
            last_non_zero = -1;
373
            goto next_coef;
374
        }
375
    }
376
377
    /* now quantify & encode AC coefs */
378
    last_non_zero = i - 1;
379
    for(;i<=last_index;i++) {
380
        j = zigzag_direct[i];
381
        level = block[j];
382
    next_coef:
383
#if 0
384
        if (level != 0)
385
            dprintf("level[%d]=%d\n", i, level);
386
#endif            
387
        /* encode using VLC */
388
        if (level != 0) {
389
            run = i - last_non_zero - 1;
390
            sign = 0;
391
            alevel = level;
392
            if (alevel < 0) {
393
                sign = 1;
394
                alevel = -alevel;
395
            }
396
            code = get_rl_index(rl, 0, run, alevel);
397
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
398
            if (code != rl->n) {
399
                put_bits(&s->pb, 1, sign);
400
            } else {
401
                /* escape: only clip in this case */
402
                put_bits(&s->pb, 6, run);
403
                if (alevel < 128) {
404
                    put_bits(&s->pb, 8, level & 0xff);
405
                } else {
406
                    if (level < 0) {
407
                        put_bits(&s->pb, 16, 0x8001 + level + 255);
408
                    } else {
409
                        put_bits(&s->pb, 16, level & 0xffff);
410
                    }
411
                }
412
            }
413
            last_non_zero = i;
414
        }
415
    }
416
    /* end of block */
417
    put_bits(&s->pb, 2, 0x2);
418
}
419
420
/******************************************/
421
/* decoding */
422
423
static VLC dc_lum_vlc;
424
static VLC dc_chroma_vlc;
425
static VLC mv_vlc;
426
static VLC mbincr_vlc;
427
static VLC mb_ptype_vlc;
428
static VLC mb_btype_vlc;
429
static VLC mb_pat_vlc;
430
431
void mpeg1_init_vlc(MpegEncContext *s)
432
{
433
    static int done = 0;
434
435
    if (!done) {
436
437
        init_vlc(&dc_lum_vlc, 9, 12, 
438
                 vlc_dc_lum_bits, 1, 1,
439
                 vlc_dc_lum_code, 2, 2);
440
        init_vlc(&dc_chroma_vlc, 9, 12, 
441
                 vlc_dc_chroma_bits, 1, 1,
442
                 vlc_dc_chroma_code, 2, 2);
443
        init_vlc(&mv_vlc, 9, 17, 
444
                 &mbMotionVectorTable[0][1], 2, 1,
445
                 &mbMotionVectorTable[0][0], 2, 1);
446 b9ecd1ee Fabrice Bellard
        init_vlc(&mbincr_vlc, 9, 35, 
447 de6d9b64 Fabrice Bellard
                 &mbAddrIncrTable[0][1], 2, 1,
448
                 &mbAddrIncrTable[0][0], 2, 1);
449
        init_vlc(&mb_pat_vlc, 9, 63, 
450
                 &mbPatTable[0][1], 2, 1,
451
                 &mbPatTable[0][0], 2, 1);
452
        
453
        init_vlc(&mb_ptype_vlc, 6, 32, 
454
                 &table_mb_ptype[0][1], 2, 1,
455
                 &table_mb_ptype[0][0], 2, 1);
456
        init_vlc(&mb_btype_vlc, 6, 32, 
457
                 &table_mb_btype[0][1], 2, 1,
458
                 &table_mb_btype[0][0], 2, 1);
459
        init_rl(&rl_mpeg1);
460
        init_rl(&rl_mpeg2);
461
        /* cannot use generic init because we must add the EOB code */
462
        init_vlc(&rl_mpeg1.vlc, 9, rl_mpeg1.n + 2, 
463
                 &rl_mpeg1.table_vlc[0][1], 4, 2,
464
                 &rl_mpeg1.table_vlc[0][0], 4, 2);
465
        init_vlc(&rl_mpeg2.vlc, 9, rl_mpeg2.n + 2, 
466
                 &rl_mpeg2.table_vlc[0][1], 4, 2,
467
                 &rl_mpeg2.table_vlc[0][0], 4, 2);
468
    }
469
}
470
471
static inline int get_dmv(MpegEncContext *s)
472
{
473 612476ef Arpi
    if(get_bits1(&s->gb)) 
474
        return 1 - (get_bits1(&s->gb) << 1);
475 de6d9b64 Fabrice Bellard
    else
476
        return 0;
477
}
478
479 0ee50938 Fabrice Bellard
static inline int get_qscale(MpegEncContext *s)
480
{
481
    int qscale;
482
    if (s->mpeg2) {
483
        if (s->q_scale_type) {
484
            qscale = non_linear_qscale[get_bits(&s->gb, 5)];
485
        } else {
486
            qscale = get_bits(&s->gb, 5) << 1;
487
        }
488
    } else {
489
        /* for mpeg1, we use the generic unquant code */
490
        qscale = get_bits(&s->gb, 5);
491
    }
492
    return qscale;
493
}
494
495 de6d9b64 Fabrice Bellard
/* motion type (for mpeg2) */
496
#define MT_FIELD 1
497
#define MT_FRAME 2
498
#define MT_16X8  2
499
#define MT_DMV   3
500
501
static int mpeg_decode_mb(MpegEncContext *s,
502
                          DCTELEM block[6][64])
503
{
504
    int i, j, k, cbp, val, code, mb_type, motion_type;
505
    
506
    /* skip mb handling */
507
    if (s->mb_incr == 0) {
508
        /* read again increment */
509
        s->mb_incr = 1;
510
        for(;;) {
511
            code = get_vlc(&s->gb, &mbincr_vlc);
512
            if (code < 0)
513
                return 1; /* error = end of slice */
514
            if (code >= 33) {
515
                if (code == 33) {
516
                    s->mb_incr += 33;
517
                }
518
                /* otherwise, stuffing, nothing to do */
519
            } else {
520
                s->mb_incr += code;
521
                break;
522
            }
523
        }
524
    }
525
    if (++s->mb_x >= s->mb_width) {
526
        s->mb_x = 0;
527
        if (s->mb_y >= (s->mb_height - 1))
528
            return -1;
529
        s->mb_y++;
530
    }
531
    dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
532
533
    if (--s->mb_incr != 0) {
534
        /* skip mb */
535
        s->mb_intra = 0;
536
        for(i=0;i<6;i++)
537
            s->block_last_index[i] = -1;
538
        s->mv_type = MV_TYPE_16X16;
539
        if (s->pict_type == P_TYPE) {
540
            /* if P type, zero motion vector is implied */
541
            s->mv_dir = MV_DIR_FORWARD;
542
            s->mv[0][0][0] = s->mv[0][0][1] = 0;
543
            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
544 25ed7f92 Fabrice Bellard
            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
545 de6d9b64 Fabrice Bellard
        } else {
546
            /* if B type, reuse previous vectors and directions */
547
            s->mv[0][0][0] = s->last_mv[0][0][0];
548
            s->mv[0][0][1] = s->last_mv[0][0][1];
549
            s->mv[1][0][0] = s->last_mv[1][0][0];
550
            s->mv[1][0][1] = s->last_mv[1][0][1];
551
        }
552 3bb4e23a Fabrice Bellard
        s->mb_skiped = 1;
553 de6d9b64 Fabrice Bellard
        return 0;
554
    }
555
556
    switch(s->pict_type) {
557
    default:
558
    case I_TYPE:
559 612476ef Arpi
        if (get_bits1(&s->gb) == 0) {
560
            if (get_bits1(&s->gb) == 0)
561 de6d9b64 Fabrice Bellard
                return -1;
562
            mb_type = MB_QUANT | MB_INTRA;
563
        } else {
564
            mb_type = MB_INTRA;
565
        }
566
        break;
567
    case P_TYPE:
568
        mb_type = get_vlc(&s->gb, &mb_ptype_vlc);
569
        if (mb_type < 0)
570
            return -1;
571
        break;
572
    case B_TYPE:
573
        mb_type = get_vlc(&s->gb, &mb_btype_vlc);
574
        if (mb_type < 0)
575
            return -1;
576
        break;
577
    }
578
    dprintf("mb_type=%x\n", mb_type);
579
    motion_type = 0; /* avoid warning */
580
    if (mb_type & (MB_FOR|MB_BACK)) {
581
        /* get additionnal motion vector type */
582
        if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) 
583
            motion_type = MT_FRAME;
584
        else
585
            motion_type = get_bits(&s->gb, 2);
586
    }
587
    /* compute dct type */
588
    if (s->picture_structure == PICT_FRAME && 
589
        !s->frame_pred_frame_dct &&
590
        (mb_type & (MB_PAT | MB_INTRA))) {
591 612476ef Arpi
        s->interlaced_dct = get_bits1(&s->gb);
592 de6d9b64 Fabrice Bellard
#ifdef DEBUG
593
        if (s->interlaced_dct)
594
            printf("interlaced_dct\n");
595
#endif
596
    } else {
597
        s->interlaced_dct = 0; /* frame based */
598
    }
599
600
    if (mb_type & MB_QUANT) {
601 0ee50938 Fabrice Bellard
        s->qscale = get_qscale(s);
602 de6d9b64 Fabrice Bellard
    }
603
    if (mb_type & MB_INTRA) {
604
        if (s->concealment_motion_vectors) {
605
            /* just parse them */
606
            if (s->picture_structure != PICT_FRAME) 
607 612476ef Arpi
                skip_bits1(&s->gb); /* field select */
608 de6d9b64 Fabrice Bellard
            mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
609
            mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
610
        }
611
        s->mb_intra = 1;
612
        cbp = 0x3f;
613
        memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
614
    } else {
615
        s->mb_intra = 0;
616
        cbp = 0;
617
    }
618
    /* special case of implicit zero motion vector */
619
    if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
620
        s->mv_dir = MV_DIR_FORWARD;
621
        s->mv_type = MV_TYPE_16X16;
622
        s->last_mv[0][0][0] = 0;
623
        s->last_mv[0][0][1] = 0;
624 d753173a Fabrice Bellard
        s->last_mv[0][1][0] = 0;
625
        s->last_mv[0][1][1] = 0;
626 de6d9b64 Fabrice Bellard
        s->mv[0][0][0] = 0;
627
        s->mv[0][0][1] = 0;
628
    } else if (mb_type & (MB_FOR | MB_BACK)) {
629
        /* motion vectors */
630
        s->mv_dir = 0;
631
        for(i=0;i<2;i++) {
632
            if (mb_type & (MB_FOR >> i)) {
633
                s->mv_dir |= (MV_DIR_FORWARD >> i);
634 d753173a Fabrice Bellard
                dprintf("motion_type=%d\n", motion_type);
635 de6d9b64 Fabrice Bellard
                switch(motion_type) {
636
                case MT_FRAME: /* or MT_16X8 */
637
                    if (s->picture_structure == PICT_FRAME) {
638
                        /* MT_FRAME */
639
                        s->mv_type = MV_TYPE_16X16;
640
                        for(k=0;k<2;k++) {
641
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], 
642
                                                     s->last_mv[i][0][k]);
643
                            s->last_mv[i][0][k] = val;
644
                            s->last_mv[i][1][k] = val;
645
                            /* full_pel: only for mpeg1 */
646
                            if (s->full_pel[i])
647
                                val = val << 1;
648
                            s->mv[i][0][k] = val;
649
                            dprintf("mv%d: %d\n", k, val);
650
                        }
651
                    } else {
652
                        /* MT_16X8 */
653
                        s->mv_type = MV_TYPE_16X8;
654
                        for(j=0;j<2;j++) {
655 612476ef Arpi
                            s->field_select[i][j] = get_bits1(&s->gb);
656 de6d9b64 Fabrice Bellard
                            for(k=0;k<2;k++) {
657
                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
658
                                                         s->last_mv[i][j][k]);
659
                                s->last_mv[i][j][k] = val;
660
                                s->mv[i][j][k] = val;
661
                            }
662
                        }
663
                    }
664
                    break;
665
                case MT_FIELD:
666
                    if (s->picture_structure == PICT_FRAME) {
667
                        s->mv_type = MV_TYPE_FIELD;
668
                        for(j=0;j<2;j++) {
669 612476ef Arpi
                            s->field_select[i][j] = get_bits1(&s->gb);
670 de6d9b64 Fabrice Bellard
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
671
                                                     s->last_mv[i][j][0]);
672
                            s->last_mv[i][j][0] = val;
673
                            s->mv[i][j][0] = val;
674
                            dprintf("fmx=%d\n", val);
675
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
676
                                                     s->last_mv[i][j][1] >> 1);
677
                            s->last_mv[i][j][1] = val << 1;
678
                            s->mv[i][j][1] = val;
679
                            dprintf("fmy=%d\n", val);
680
                        }
681
                    } else {
682
                        s->mv_type = MV_TYPE_16X16;
683 612476ef Arpi
                        s->field_select[i][0] = get_bits1(&s->gb);
684 de6d9b64 Fabrice Bellard
                        for(k=0;k<2;k++) {
685
                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
686
                                                     s->last_mv[i][0][k]);
687
                            s->last_mv[i][0][k] = val;
688
                            s->last_mv[i][1][k] = val;
689
                            s->mv[i][0][k] = val;
690
                        }
691
                    }
692
                    break;
693
                case MT_DMV:
694
                    {
695
                        int dmx, dmy, mx, my, m;
696
697
                        mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
698
                                                s->last_mv[i][0][0]);
699
                        s->last_mv[i][0][0] = mx;
700
                        s->last_mv[i][1][0] = mx;
701
                        dmx = get_dmv(s);
702
                        my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
703
                                                s->last_mv[i][0][1] >> 1);
704
                        dmy = get_dmv(s);
705
                        s->mv_type = MV_TYPE_DMV;
706
                        /* XXX: totally broken */
707
                        if (s->picture_structure == PICT_FRAME) {
708
                            s->last_mv[i][0][1] = my << 1;
709
                            s->last_mv[i][1][1] = my << 1;
710
711
                            m = s->top_field_first ? 1 : 3;
712
                            /* top -> top pred */
713
                            s->mv[i][0][0] = mx; 
714
                            s->mv[i][0][1] = my << 1;
715
                            s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
716
                            s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
717
                            m = 4 - m;
718
                            s->mv[i][2][0] = mx;
719
                            s->mv[i][2][1] = my << 1;
720
                            s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
721
                            s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
722
                        } else {
723
                            s->last_mv[i][0][1] = my;
724
                            s->last_mv[i][1][1] = my;
725
                            s->mv[i][0][0] = mx;
726
                            s->mv[i][0][1] = my;
727
                            s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
728
                            s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 
729
                                /* + 2 * cur_field */;
730
                        }
731
                    }
732
                    break;
733
                }
734
            }
735
        }
736
    }
737
738
    if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
739 612476ef Arpi
        skip_bits1(&s->gb); /* marker */
740 de6d9b64 Fabrice Bellard
    }
741
    
742
    if (mb_type & MB_PAT) {
743
        cbp = get_vlc(&s->gb, &mb_pat_vlc);
744
        if (cbp < 0)
745
            return -1;
746
        cbp++;
747
    }
748
    dprintf("cbp=%x\n", cbp);
749
750
    if (s->mpeg2) {
751
        if (s->mb_intra) {
752
            for(i=0;i<6;i++) {
753
                if (cbp & (1 << (5 - i))) {
754
                    if (mpeg2_decode_block_intra(s, block[i], i) < 0)
755
                        return -1;
756
                }
757
            }
758
        } else {
759
            for(i=0;i<6;i++) {
760
                if (cbp & (1 << (5 - i))) {
761
                    if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
762
                        return -1;
763
                }
764
            }
765
        }
766
    } else {
767
        for(i=0;i<6;i++) {
768
            if (cbp & (1 << (5 - i))) {
769
                if (mpeg1_decode_block(s, block[i], i) < 0)
770
                    return -1;
771
            }
772
        }
773
    }
774
    return 0;
775
}
776
777
/* as h263, but only 17 codes */
778
static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
779
{
780
    int code, sign, val, m, l, shift;
781
782
    code = get_vlc(&s->gb, &mv_vlc);
783
    if (code < 0) {
784
        return 0xffff;
785
    }
786
    if (code == 0) {
787
        return pred;
788
    }
789 612476ef Arpi
    sign = get_bits1(&s->gb);
790 de6d9b64 Fabrice Bellard
    shift = fcode - 1;
791
    val = (code - 1) << shift;
792
    if (shift > 0)
793
        val |= get_bits(&s->gb, shift);
794
    val++;
795
    if (sign)
796
        val = -val;
797
    val += pred;
798
    
799
    /* modulo decoding */
800
    l = (1 << shift) * 16;
801
    m = 2 * l;
802
    if (val < -l) {
803
        val += m;
804
    } else if (val >= l) {
805
        val -= m;
806
    }
807
    return val;
808
}
809
810
static inline int decode_dc(MpegEncContext *s, int component)
811
{
812
    int code, diff;
813
814
    if (component == 0) {
815
        code = get_vlc(&s->gb, &dc_lum_vlc);
816
    } else {
817
        code = get_vlc(&s->gb, &dc_chroma_vlc);
818
    }
819
    if (code < 0)
820
        return 0xffff;
821
    if (code == 0) {
822
        diff = 0;
823
    } else {
824
        diff = get_bits(&s->gb, code);
825
        if ((diff & (1 << (code - 1))) == 0) 
826
            diff = (-1 << code) | (diff + 1);
827
    }
828
    return diff;
829
}
830
831
static int mpeg1_decode_block(MpegEncContext *s, 
832
                               DCTELEM *block, 
833
                               int n)
834
{
835
    int level, dc, diff, i, j, run;
836
    int code, component;
837
    RLTable *rl = &rl_mpeg1;
838
839
    if (s->mb_intra) {
840
        /* DC coef */
841
        component = (n <= 3 ? 0 : n - 4 + 1);
842
        diff = decode_dc(s, component);
843
        if (diff >= 0xffff)
844
            return -1;
845
        dc = s->last_dc[component];
846
        dc += diff;
847
        s->last_dc[component] = dc;
848
        block[0] = dc;
849
        dprintf("dc=%d diff=%d\n", dc, diff);
850
        i = 1;
851
    } else {
852
        int bit_cnt, v;
853
        UINT32 bit_buf;
854
        UINT8 *buf_ptr;
855
        i = 0;
856
        /* special case for the first coef. no need to add a second vlc table */
857
        SAVE_BITS(&s->gb);
858
        SHOW_BITS(&s->gb, v, 2);
859
        if (v & 2) {
860
            run = 0;
861
            level = 1 - ((v & 1) << 1);
862
            FLUSH_BITS(2);
863
            RESTORE_BITS(&s->gb);
864
            goto add_coef;
865
        }
866
        RESTORE_BITS(&s->gb);
867
    }
868
869
    /* now quantify & encode AC coefs */
870
    for(;;) {
871
        code = get_vlc(&s->gb, &rl->vlc);
872
        if (code < 0) {
873
            return -1;
874
        }
875
        if (code == 112) {
876
            break;
877
        } else if (code == 111) {
878
            /* escape */
879
            run = get_bits(&s->gb, 6);
880
            level = get_bits(&s->gb, 8);
881
            level = (level << 24) >> 24;
882
            if (level == -128) {
883
                level = get_bits(&s->gb, 8) - 256;
884
            } else if (level == 0) {
885
                level = get_bits(&s->gb, 8);
886
            }
887
        } else {
888
            run = rl->table_run[code];
889
            level = rl->table_level[code];
890 612476ef Arpi
            if (get_bits1(&s->gb))
891 de6d9b64 Fabrice Bellard
                level = -level;
892
        }
893
        i += run;
894
        if (i >= 64)
895
            return -1;
896
    add_coef:
897
        dprintf("%d: run=%d level=%d\n", n, run, level);
898
        j = zigzag_direct[i];
899
        block[j] = level;
900
        i++;
901
    }
902
    s->block_last_index[n] = i;
903
    return 0;
904
}
905
906
/* Also does unquantization here, since I will never support mpeg2
907
   encoding */
908
static int mpeg2_decode_block_non_intra(MpegEncContext *s, 
909
                                        DCTELEM *block, 
910
                                        int n)
911
{
912
    int level, i, j, run;
913
    int code;
914
    RLTable *rl = &rl_mpeg1;
915
    const UINT8 *scan_table;
916
    const UINT16 *matrix;
917
    int mismatch;
918
919
    if (s->alternate_scan)
920
        scan_table = ff_alternate_vertical_scan;
921
    else
922
        scan_table = zigzag_direct;
923
    mismatch = 1;
924
925
    {
926
        int bit_cnt, v;
927
        UINT32 bit_buf;
928
        UINT8 *buf_ptr;
929
        i = 0;
930
        if (n < 4) 
931
            matrix = s->non_intra_matrix;
932
        else
933
            matrix = s->chroma_non_intra_matrix;
934
            
935
        /* special case for the first coef. no need to add a second vlc table */
936
        SAVE_BITS(&s->gb);
937
        SHOW_BITS(&s->gb, v, 2);
938
        if (v & 2) {
939
            run = 0;
940
            level = 1 - ((v & 1) << 1);
941
            FLUSH_BITS(2);
942
            RESTORE_BITS(&s->gb);
943
            goto add_coef;
944
        }
945
        RESTORE_BITS(&s->gb);
946
    }
947
948
    /* now quantify & encode AC coefs */
949
    for(;;) {
950
        code = get_vlc(&s->gb, &rl->vlc);
951
        if (code < 0)
952
            return -1;
953
        if (code == 112) {
954
            break;
955
        } else if (code == 111) {
956
            /* escape */
957
            run = get_bits(&s->gb, 6);
958
            level = get_bits(&s->gb, 12);
959
            level = (level << 20) >> 20;
960
        } else {
961
            run = rl->table_run[code];
962
            level = rl->table_level[code];
963 612476ef Arpi
            if (get_bits1(&s->gb))
964 de6d9b64 Fabrice Bellard
                level = -level;
965
        }
966
        i += run;
967
        if (i >= 64)
968
            return -1;
969
    add_coef:
970
        j = scan_table[i];
971
        dprintf("%d: run=%d level=%d\n", n, run, level);
972 25ed7f92 Fabrice Bellard
        /* XXX: optimize */
973
        if (level > 0) {
974
            level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
975
        } else {
976
            level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
977
            level = -level;
978
        }
979 de6d9b64 Fabrice Bellard
        /* XXX: is it really necessary to saturate since the encoder
980
           knows whats going on ? */
981
        mismatch ^= level;
982
        block[j] = level;
983
        i++;
984
    }
985
    block[63] ^= (mismatch & 1);
986
    s->block_last_index[n] = i;
987
    return 0;
988
}
989
990
static int mpeg2_decode_block_intra(MpegEncContext *s, 
991
                                    DCTELEM *block, 
992
                                    int n)
993
{
994
    int level, dc, diff, i, j, run;
995
    int code, component;
996
    RLTable *rl;
997
    const UINT8 *scan_table;
998
    const UINT16 *matrix;
999
    int mismatch;
1000
1001
    if (s->alternate_scan)
1002
        scan_table = ff_alternate_vertical_scan;
1003
    else
1004
        scan_table = zigzag_direct;
1005
1006
    /* DC coef */
1007
    component = (n <= 3 ? 0 : n - 4 + 1);
1008
    diff = decode_dc(s, component);
1009
    if (diff >= 0xffff)
1010
        return -1;
1011
    dc = s->last_dc[component];
1012
    dc += diff;
1013
    s->last_dc[component] = dc;
1014
    block[0] = dc << (3 - s->intra_dc_precision);
1015
    dprintf("dc=%d\n", block[0]);
1016 d753173a Fabrice Bellard
    mismatch = block[0] ^ 1;
1017 de6d9b64 Fabrice Bellard
    i = 1;
1018
    if (s->intra_vlc_format)
1019
        rl = &rl_mpeg2;
1020
    else
1021
        rl = &rl_mpeg1;
1022
    if (n < 4) 
1023
        matrix = s->intra_matrix;
1024
    else
1025
        matrix = s->chroma_intra_matrix;
1026 25ed7f92 Fabrice Bellard
1027 de6d9b64 Fabrice Bellard
    /* now quantify & encode AC coefs */
1028
    for(;;) {
1029
        code = get_vlc(&s->gb, &rl->vlc);
1030
        if (code < 0)
1031
            return -1;
1032
        if (code == 112) {
1033
            break;
1034
        } else if (code == 111) {
1035
            /* escape */
1036
            run = get_bits(&s->gb, 6);
1037
            level = get_bits(&s->gb, 12);
1038
            level = (level << 20) >> 20;
1039
        } else {
1040
            run = rl->table_run[code];
1041
            level = rl->table_level[code];
1042 612476ef Arpi
            if (get_bits1(&s->gb))
1043 de6d9b64 Fabrice Bellard
                level = -level;
1044
        }
1045
        i += run;
1046
        if (i >= 64)
1047
            return -1;
1048
        j = scan_table[i];
1049
        dprintf("%d: run=%d level=%d\n", n, run, level);
1050
        level = (level * s->qscale * matrix[j]) / 16;
1051
        /* XXX: is it really necessary to saturate since the encoder
1052
           knows whats going on ? */
1053
        mismatch ^= level;
1054
        block[j] = level;
1055
        i++;
1056
    }
1057
    block[63] ^= (mismatch & 1);
1058
    s->block_last_index[n] = i;
1059
    return 0;
1060
}
1061
1062
/* compressed picture size */
1063
#define PICTURE_BUFFER_SIZE 100000
1064
1065
typedef struct Mpeg1Context {
1066
    MpegEncContext mpeg_enc_ctx;
1067
    UINT32 header_state;
1068
    int start_code; /* current start code */
1069
    UINT8 buffer[PICTURE_BUFFER_SIZE]; 
1070
    UINT8 *buf_ptr;
1071
    int buffer_size;
1072
    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1073
} Mpeg1Context;
1074
1075
static int mpeg_decode_init(AVCodecContext *avctx)
1076
{
1077
    Mpeg1Context *s = avctx->priv_data;
1078
1079
    s->header_state = 0xff;
1080
    s->mpeg_enc_ctx_allocated = 0;
1081
    s->buffer_size = PICTURE_BUFFER_SIZE;
1082
    s->start_code = -1;
1083
    s->buf_ptr = s->buffer;
1084
    s->mpeg_enc_ctx.picture_number = 0;
1085
    return 0;
1086
}
1087
1088
/* return the 8 bit start code value and update the search
1089
   state. Return -1 if no start code found */
1090
static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end, 
1091
                           UINT32 *header_state)
1092
{
1093
    UINT8 *buf_ptr;
1094
    unsigned int state, v;
1095
    int val;
1096
1097
    state = *header_state;
1098
    buf_ptr = *pbuf_ptr;
1099
    while (buf_ptr < buf_end) {
1100
        v = *buf_ptr++;
1101
        if (state == 0x000001) {
1102
            state = ((state << 8) | v) & 0xffffff;
1103
            val = state;
1104
            goto found;
1105
        }
1106
        state = ((state << 8) | v) & 0xffffff;
1107
    }
1108
    val = -1;
1109
 found:
1110
    *pbuf_ptr = buf_ptr;
1111
    *header_state = state;
1112
    return val;
1113
}
1114
1115
static int mpeg1_decode_picture(AVCodecContext *avctx, 
1116
                                UINT8 *buf, int buf_size)
1117
{
1118
    Mpeg1Context *s1 = avctx->priv_data;
1119
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1120
    int ref, f_code;
1121
1122
    init_get_bits(&s->gb, buf, buf_size);
1123
1124
    ref = get_bits(&s->gb, 10); /* temporal ref */
1125
    s->pict_type = get_bits(&s->gb, 3);
1126 b9ecd1ee Fabrice Bellard
    dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
1127 612476ef Arpi
    skip_bits(&s->gb, 16);
1128 de6d9b64 Fabrice Bellard
    if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
1129 612476ef Arpi
        s->full_pel[0] = get_bits1(&s->gb);
1130 de6d9b64 Fabrice Bellard
        f_code = get_bits(&s->gb, 3);
1131
        if (f_code == 0)
1132
            return -1;
1133
        s->mpeg_f_code[0][0] = f_code;
1134
        s->mpeg_f_code[0][1] = f_code;
1135
    }
1136
    if (s->pict_type == B_TYPE) {
1137 612476ef Arpi
        s->full_pel[1] = get_bits1(&s->gb);
1138 de6d9b64 Fabrice Bellard
        f_code = get_bits(&s->gb, 3);
1139
        if (f_code == 0)
1140
            return -1;
1141
        s->mpeg_f_code[1][0] = f_code;
1142
        s->mpeg_f_code[1][1] = f_code;
1143
    }
1144
    s->y_dc_scale = 8;
1145
    s->c_dc_scale = 8;
1146
    s->first_slice = 1;
1147
    return 0;
1148
}
1149
1150
static void mpeg_decode_sequence_extension(MpegEncContext *s)
1151
{
1152
    int horiz_size_ext, vert_size_ext;
1153
    int bit_rate_ext, vbv_buf_ext, low_delay;
1154
    int frame_rate_ext_n, frame_rate_ext_d;
1155
1156 612476ef Arpi
    skip_bits(&s->gb, 8); /* profil and level */
1157
    skip_bits(&s->gb, 1); /* progressive_sequence */
1158
    skip_bits(&s->gb, 2); /* chroma_format */
1159 de6d9b64 Fabrice Bellard
    horiz_size_ext = get_bits(&s->gb, 2);
1160
    vert_size_ext = get_bits(&s->gb, 2);
1161
    s->width |= (horiz_size_ext << 12);
1162
    s->height |= (vert_size_ext << 12);
1163
    bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
1164
    s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
1165 612476ef Arpi
    skip_bits1(&s->gb); /* marker */
1166 de6d9b64 Fabrice Bellard
    vbv_buf_ext = get_bits(&s->gb, 8);
1167 612476ef Arpi
    low_delay = get_bits1(&s->gb);
1168 de6d9b64 Fabrice Bellard
    frame_rate_ext_n = get_bits(&s->gb, 2);
1169
    frame_rate_ext_d = get_bits(&s->gb, 5);
1170
    if (frame_rate_ext_d >= 1)
1171
        s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
1172
    dprintf("sequence extension\n");
1173
    s->mpeg2 = 1;
1174
}
1175
1176
static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1177
{
1178 60832448 Fabrice Bellard
    int i, v, j;
1179 de6d9b64 Fabrice Bellard
1180 25ed7f92 Fabrice Bellard
    dprintf("matrix extension\n");
1181
1182 612476ef Arpi
    if (get_bits1(&s->gb)) {
1183 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
1184
            v = get_bits(&s->gb, 8);
1185 25ed7f92 Fabrice Bellard
            j = zigzag_direct[i];
1186 60832448 Fabrice Bellard
            s->intra_matrix[j] = v;
1187
            s->chroma_intra_matrix[j] = v;
1188 de6d9b64 Fabrice Bellard
        }
1189
    }
1190 612476ef Arpi
    if (get_bits1(&s->gb)) {
1191 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
1192
            v = get_bits(&s->gb, 8);
1193 25ed7f92 Fabrice Bellard
            j = zigzag_direct[i];
1194 60832448 Fabrice Bellard
            s->non_intra_matrix[j] = v;
1195
            s->chroma_non_intra_matrix[j] = v;
1196 de6d9b64 Fabrice Bellard
        }
1197
    }
1198 612476ef Arpi
    if (get_bits1(&s->gb)) {
1199 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
1200
            v = get_bits(&s->gb, 8);
1201 25ed7f92 Fabrice Bellard
            j = zigzag_direct[i];
1202 60832448 Fabrice Bellard
            s->chroma_intra_matrix[j] = v;
1203 de6d9b64 Fabrice Bellard
        }
1204
    }
1205 612476ef Arpi
    if (get_bits1(&s->gb)) {
1206 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
1207
            v = get_bits(&s->gb, 8);
1208 25ed7f92 Fabrice Bellard
            j = zigzag_direct[i];
1209 60832448 Fabrice Bellard
            s->chroma_non_intra_matrix[j] = v;
1210 de6d9b64 Fabrice Bellard
        }
1211
    }
1212
}
1213
1214
static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
1215
{
1216
    s->full_pel[0] = s->full_pel[1] = 0;
1217
    s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1218
    s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1219
    s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1220
    s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1221
    s->intra_dc_precision = get_bits(&s->gb, 2);
1222
    s->picture_structure = get_bits(&s->gb, 2);
1223 612476ef Arpi
    s->top_field_first = get_bits1(&s->gb);
1224
    s->frame_pred_frame_dct = get_bits1(&s->gb);
1225
    s->concealment_motion_vectors = get_bits1(&s->gb);
1226
    s->q_scale_type = get_bits1(&s->gb);
1227
    s->intra_vlc_format = get_bits1(&s->gb);
1228
    s->alternate_scan = get_bits1(&s->gb);
1229
    s->repeat_first_field = get_bits1(&s->gb);
1230
    s->chroma_420_type = get_bits1(&s->gb);
1231
    s->progressive_frame = get_bits1(&s->gb);
1232 de6d9b64 Fabrice Bellard
    /* composite display not parsed */
1233 25ed7f92 Fabrice Bellard
    dprintf("intra_dc_precion=%d\n", s->intra_dc_precision);
1234
    dprintf("picture_structure=%d\n", s->picture_structure);
1235 de6d9b64 Fabrice Bellard
    dprintf("conceal=%d\n", s->concealment_motion_vectors);
1236 25ed7f92 Fabrice Bellard
    dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
1237
    dprintf("alternate_scan=%d\n", s->alternate_scan);
1238 de6d9b64 Fabrice Bellard
    dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1239
}
1240
1241
static void mpeg_decode_extension(AVCodecContext *avctx, 
1242
                                  UINT8 *buf, int buf_size)
1243
{
1244
    Mpeg1Context *s1 = avctx->priv_data;
1245
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1246
    int ext_type;
1247
1248
    init_get_bits(&s->gb, buf, buf_size);
1249
    
1250
    ext_type = get_bits(&s->gb, 4);
1251
    switch(ext_type) {
1252
    case 0x1:
1253
        /* sequence ext */
1254
        mpeg_decode_sequence_extension(s);
1255
        break;
1256
    case 0x3:
1257
        /* quant matrix extension */
1258
        mpeg_decode_quant_matrix_extension(s);
1259
        break;
1260
    case 0x8:
1261
        /* picture extension */
1262
        mpeg_decode_picture_coding_extension(s);
1263
        break;
1264
    }
1265
}
1266
1267
/* return 1 if end of frame */
1268
static int mpeg_decode_slice(AVCodecContext *avctx, 
1269
                              AVPicture *pict,
1270
                              int start_code,
1271
                              UINT8 *buf, int buf_size)
1272
{
1273
    Mpeg1Context *s1 = avctx->priv_data;
1274
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1275
    int ret;
1276
1277
    start_code = (start_code - 1) & 0xff;
1278
    if (start_code >= s->mb_height)
1279
        return -1;
1280
    s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
1281
    s->last_dc[1] = s->last_dc[0];
1282
    s->last_dc[2] = s->last_dc[0];
1283
    memset(s->last_mv, 0, sizeof(s->last_mv));
1284
    s->mb_x = -1;
1285
    s->mb_y = start_code;
1286
    s->mb_incr = 0;
1287
1288
    /* start frame decoding */
1289
    if (s->first_slice) {
1290
        s->first_slice = 0;
1291
        MPV_frame_start(s);
1292
    }
1293
1294
    init_get_bits(&s->gb, buf, buf_size);
1295
1296 0ee50938 Fabrice Bellard
    s->qscale = get_qscale(s);
1297 de6d9b64 Fabrice Bellard
    /* extra slice info */
1298 612476ef Arpi
    while (get_bits1(&s->gb) != 0) {
1299
        skip_bits(&s->gb, 8);
1300 de6d9b64 Fabrice Bellard
    }
1301
1302
    for(;;) {
1303 b7ec19d3 Fabrice Bellard
        memset(s->block, 0, sizeof(s->block));
1304
        ret = mpeg_decode_mb(s, s->block);
1305 de6d9b64 Fabrice Bellard
        dprintf("ret=%d\n", ret);
1306
        if (ret < 0)
1307
            return -1;
1308
        if (ret == 1)
1309
            break;
1310 b7ec19d3 Fabrice Bellard
        MPV_decode_mb(s, s->block);
1311 de6d9b64 Fabrice Bellard
    }
1312
    
1313
    /* end of slice reached */
1314
    if (s->mb_x == (s->mb_width - 1) &&
1315
        s->mb_y == (s->mb_height - 1)) {
1316
        /* end of image */
1317
        UINT8 **picture;
1318
1319
        MPV_frame_end(s);
1320
1321
        /* XXX: incorrect reported qscale for mpeg2 */
1322
        if (s->pict_type == B_TYPE) {
1323
            picture = s->current_picture;
1324
            avctx->quality = s->qscale;
1325
        } else {
1326
            /* latency of 1 frame for I and P frames */
1327
            /* XXX: use another variable than picture_number */
1328
            if (s->picture_number == 0) {
1329
                picture = NULL;
1330
            } else {
1331
                picture = s->last_picture;
1332
                avctx->quality = s->last_qscale;
1333
            }
1334
            s->last_qscale = s->qscale;
1335
            s->picture_number++;
1336
        }
1337
        if (picture) {
1338
            pict->data[0] = picture[0];
1339
            pict->data[1] = picture[1];
1340
            pict->data[2] = picture[2];
1341
            pict->linesize[0] = s->linesize;
1342
            pict->linesize[1] = s->linesize / 2;
1343
            pict->linesize[2] = s->linesize / 2;
1344
            return 1;
1345
        } else {
1346
            return 0;
1347
        }
1348
    } else {
1349
        return 0;
1350
    }
1351
}
1352
1353
static int mpeg1_decode_sequence(AVCodecContext *avctx, 
1354
                                 UINT8 *buf, int buf_size)
1355
{
1356
    Mpeg1Context *s1 = avctx->priv_data;
1357
    MpegEncContext *s = &s1->mpeg_enc_ctx;
1358 60832448 Fabrice Bellard
    int width, height, i, v, j;
1359 de6d9b64 Fabrice Bellard
    
1360
    init_get_bits(&s->gb, buf, buf_size);
1361
1362
    width = get_bits(&s->gb, 12);
1363
    height = get_bits(&s->gb, 12);
1364 612476ef Arpi
    skip_bits(&s->gb, 4);
1365 de6d9b64 Fabrice Bellard
    s->frame_rate_index = get_bits(&s->gb, 4);
1366
    if (s->frame_rate_index == 0)
1367
        return -1;
1368
    s->bit_rate = get_bits(&s->gb, 18) * 400;
1369 612476ef Arpi
    if (get_bits1(&s->gb) == 0) /* marker */
1370 de6d9b64 Fabrice Bellard
        return -1;
1371
    if (width <= 0 || height <= 0 ||
1372
        (width % 2) != 0 || (height % 2) != 0)
1373
        return -1;
1374
    if (width != s->width ||
1375
        height != s->height) {
1376
        /* start new mpeg1 context decoding */
1377
        s->out_format = FMT_MPEG1;
1378
        if (s1->mpeg_enc_ctx_allocated) {
1379
            MPV_common_end(s);
1380
        }
1381
        s->width = width;
1382
        s->height = height;
1383
        s->has_b_frames = 1;
1384 0c23ead1 Fabrice Bellard
        s->avctx = avctx;
1385 de6d9b64 Fabrice Bellard
        avctx->width = width;
1386
        avctx->height = height;
1387
        avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
1388
        avctx->bit_rate = s->bit_rate;
1389
        
1390
        if (MPV_common_init(s) < 0)
1391
            return -1;
1392
        mpeg1_init_vlc(s);
1393
        s1->mpeg_enc_ctx_allocated = 1;
1394
    }
1395
1396 612476ef Arpi
    skip_bits(&s->gb, 10); /* vbv_buffer_size */
1397
    skip_bits(&s->gb, 1);
1398 de6d9b64 Fabrice Bellard
1399
    /* get matrix */
1400 612476ef Arpi
    if (get_bits1(&s->gb)) {
1401 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
1402
            v = get_bits(&s->gb, 8);
1403 25ed7f92 Fabrice Bellard
            j = zigzag_direct[i];
1404 60832448 Fabrice Bellard
            s->intra_matrix[j] = v;
1405
            s->chroma_intra_matrix[j] = v;
1406 de6d9b64 Fabrice Bellard
        }
1407 25ed7f92 Fabrice Bellard
#ifdef DEBUG
1408
        dprintf("intra matrix present\n");
1409
        for(i=0;i<64;i++)
1410
            dprintf(" %d", s->intra_matrix[zigzag_direct[i]]);
1411
        printf("\n");
1412
#endif
1413 de6d9b64 Fabrice Bellard
    } else {
1414
        for(i=0;i<64;i++) {
1415
            v = default_intra_matrix[i];
1416
            s->intra_matrix[i] = v;
1417
            s->chroma_intra_matrix[i] = v;
1418
        }
1419
    }
1420 612476ef Arpi
    if (get_bits1(&s->gb)) {
1421 de6d9b64 Fabrice Bellard
        for(i=0;i<64;i++) {
1422
            v = get_bits(&s->gb, 8);
1423 25ed7f92 Fabrice Bellard
            j = zigzag_direct[i];
1424 60832448 Fabrice Bellard
            s->non_intra_matrix[j] = v;
1425
            s->chroma_non_intra_matrix[j] = v;
1426 de6d9b64 Fabrice Bellard
        }
1427 25ed7f92 Fabrice Bellard
#ifdef DEBUG
1428
        dprintf("non intra matrix present\n");
1429
        for(i=0;i<64;i++)
1430
            dprintf(" %d", s->non_intra_matrix[zigzag_direct[i]]);
1431
        printf("\n");
1432
#endif
1433 de6d9b64 Fabrice Bellard
    } else {
1434
        for(i=0;i<64;i++) {
1435
            v = default_non_intra_matrix[i];
1436
            s->non_intra_matrix[i] = v;
1437
            s->chroma_non_intra_matrix[i] = v;
1438
        }
1439
    }
1440
1441
    /* we set mpeg2 parameters so that it emulates mpeg1 */
1442
    s->progressive_sequence = 1;
1443
    s->progressive_frame = 1;
1444
    s->picture_structure = PICT_FRAME;
1445
    s->frame_pred_frame_dct = 1;
1446
    s->mpeg2 = 0;
1447
    return 0;
1448
}
1449
1450
/* handle buffering and image synchronisation */
1451
static int mpeg_decode_frame(AVCodecContext *avctx, 
1452
                             void *data, int *data_size,
1453
                             UINT8 *buf, int buf_size)
1454
{
1455
    Mpeg1Context *s = avctx->priv_data;
1456
    UINT8 *buf_end, *buf_ptr, *buf_start;
1457
    int len, start_code_found, ret, code, start_code, input_size;
1458
    AVPicture *picture = data;
1459
1460
    dprintf("fill_buffer\n");
1461
1462
    *data_size = 0;
1463
    /* special case for last picture */
1464
    if (buf_size == 0) {
1465
        MpegEncContext *s2 = &s->mpeg_enc_ctx;
1466
        if (s2->picture_number > 0) {
1467
            picture->data[0] = s2->next_picture[0];
1468
            picture->data[1] = s2->next_picture[1];
1469
            picture->data[2] = s2->next_picture[2];
1470
            picture->linesize[0] = s2->linesize;
1471
            picture->linesize[1] = s2->linesize / 2;
1472
            picture->linesize[2] = s2->linesize / 2;
1473
            *data_size = sizeof(AVPicture);
1474
        }
1475
        return 0;
1476
    }
1477
1478
    buf_ptr = buf;
1479
    buf_end = buf + buf_size;
1480
    while (buf_ptr < buf_end) {
1481
        buf_start = buf_ptr;
1482
        /* find start next code */
1483
        code = find_start_code(&buf_ptr, buf_end, &s->header_state);
1484
        if (code >= 0) {
1485
            start_code_found = 1;
1486
        } else {
1487
            start_code_found = 0;
1488
        }
1489
        /* copy to buffer */
1490
        len = buf_ptr - buf_start;
1491
        if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
1492
            /* data too big : flush */
1493
            s->buf_ptr = s->buffer;
1494
            if (start_code_found)
1495
                s->start_code = code;
1496
        } else {
1497
            memcpy(s->buf_ptr, buf_start, len);
1498
            s->buf_ptr += len;
1499
            
1500
            if (start_code_found) {
1501
                /* prepare data for next start code */
1502
                input_size = s->buf_ptr - s->buffer;
1503
                start_code = s->start_code;
1504
                s->buf_ptr = s->buffer;
1505
                s->start_code = code;
1506
                switch(start_code) {
1507
                case SEQ_START_CODE:
1508
                    mpeg1_decode_sequence(avctx, s->buffer, 
1509
                                          input_size);
1510
                    break;
1511
                            
1512
                case PICTURE_START_CODE:
1513
                    /* we have a complete image : we try to decompress it */
1514
                    mpeg1_decode_picture(avctx, 
1515
                                         s->buffer, input_size);
1516
                    break;
1517
                case EXT_START_CODE:
1518
                    mpeg_decode_extension(avctx,
1519
                                          s->buffer, input_size);
1520
                    break;
1521
                default:
1522
                    if (start_code >= SLICE_MIN_START_CODE &&
1523
                        start_code <= SLICE_MAX_START_CODE) {
1524
                        ret = mpeg_decode_slice(avctx, picture,
1525
                                                start_code, s->buffer, input_size);
1526
                        if (ret == 1) {
1527
                            /* got a picture: exit */
1528
                            *data_size = sizeof(AVPicture);
1529
                            goto the_end;
1530
                        }
1531
                    }
1532
                    break;
1533
                }
1534
            }
1535
        }
1536
    }
1537
 the_end:
1538
    return buf_ptr - buf;
1539
}
1540
1541
static int mpeg_decode_end(AVCodecContext *avctx)
1542
{
1543
    Mpeg1Context *s = avctx->priv_data;
1544
1545
    if (s->mpeg_enc_ctx_allocated)
1546
        MPV_common_end(&s->mpeg_enc_ctx);
1547
    return 0;
1548
}
1549
1550
AVCodec mpeg_decoder = {
1551
    "mpegvideo",
1552
    CODEC_TYPE_VIDEO,
1553
    CODEC_ID_MPEG1VIDEO,
1554
    sizeof(Mpeg1Context),
1555
    mpeg_decode_init,
1556
    NULL,
1557
    mpeg_decode_end,
1558
    mpeg_decode_frame,
1559
};