Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263.c @ 935442b5

History | View | Annotate | Download (37.9 KB)

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

    
28
static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
29
                              int n);
30
static void h263_encode_motion(MpegEncContext * s, int val);
31
static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
32
                               int n);
33
static int h263_decode_motion(MpegEncContext * s, int pred);
34
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
35
                             int n, int coded);
36
static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
37
                              int n, int coded);
38

    
39
int h263_get_picture_format(int width, int height)
40
{
41
    int format;
42

    
43
    if (width == 128 && height == 96)
44
        format = 1;
45
    else if (width == 176 && height == 144)
46
        format = 2;
47
    else if (width == 352 && height == 288)
48
        format = 3;
49
    else if (width == 704 && height == 576)
50
        format = 4;
51
    else if (width == 1408 && height == 1152)
52
        format = 5;
53
    else
54
        format = 7;
55
    return format;
56
}
57

    
58
void h263_encode_picture_header(MpegEncContext * s, int picture_number)
59
{
60
    int format, umvplus;
61

    
62
    align_put_bits(&s->pb);
63
    put_bits(&s->pb, 22, 0x20);
64
    put_bits(&s->pb, 8, ((s->picture_number * 30 * FRAME_RATE_BASE) / 
65
                         s->frame_rate) & 0xff);
66

    
67
    put_bits(&s->pb, 1, 1);        /* marker */
68
    put_bits(&s->pb, 1, 0);        /* h263 id */
69
    put_bits(&s->pb, 1, 0);        /* split screen off */
70
    put_bits(&s->pb, 1, 0);        /* camera  off */
71
    put_bits(&s->pb, 1, 0);        /* freeze picture release off */
72

    
73
    if (!s->h263_plus) {
74
        /* H.263v1 */
75
        format = h263_get_picture_format(s->width, s->height);
76
        put_bits(&s->pb, 3, format);
77
        put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
78
        /* By now UMV IS DISABLED ON H.263v1, since the restrictions
79
        of H.263v1 UMV implies to check the predicted MV after
80
        calculation of the current MB to see if we're on the limits */
81
        put_bits(&s->pb, 1, 0);        /* unrestricted motion vector: off */
82
        put_bits(&s->pb, 1, 0);        /* SAC: off */
83
        put_bits(&s->pb, 1, 0);        /* advanced prediction mode: off */
84
        put_bits(&s->pb, 1, 0);        /* not PB frame */
85
        put_bits(&s->pb, 5, s->qscale);
86
        put_bits(&s->pb, 1, 0);        /* Continuous Presence Multipoint mode: off */
87
    } else {
88
        /* H.263v2 */
89
        /* H.263 Plus PTYPE */
90
        put_bits(&s->pb, 3, 7);
91
        put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
92
        put_bits(&s->pb,3,6); /* Custom Source Format */
93
        put_bits(&s->pb,1,0); /* Custom PCF: off */
94
        umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
95
        put_bits(&s->pb, 1, umvplus); /* Unrestricted Motion Vector */
96
        put_bits(&s->pb,1,0); /* SAC: off */
97
        put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
98
        put_bits(&s->pb,1,0); /* Advanced Intra Coding: off */
99
        put_bits(&s->pb,1,0); /* Deblocking Filter: off */
100
        put_bits(&s->pb,1,0); /* Slice Structured: off */
101
        put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
102
        put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
103
        put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
104
        put_bits(&s->pb,1,0); /* Modified Quantization: off */
105
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
106
        put_bits(&s->pb,3,0); /* Reserved */
107
                
108
        put_bits(&s->pb, 3, s->pict_type == P_TYPE);
109
                
110
        put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
111
        put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
112
        put_bits(&s->pb,1,0); /* Rounding Type */
113
        put_bits(&s->pb,2,0); /* Reserved */
114
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
115
                
116
        /* This should be here if PLUSPTYPE */
117
        put_bits(&s->pb, 1, 0);        /* Continuous Presence Multipoint mode: off */
118
                
119
        /* Custom Picture Format (CPFMT) */
120
                
121
        put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
122
        put_bits(&s->pb,9,(s->width >> 2) - 1);
123
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
124
        put_bits(&s->pb,9,(s->height >> 2));
125
        /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
126
        if (umvplus)
127
            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
128
        put_bits(&s->pb, 5, s->qscale);
129
    }
130

    
131
    put_bits(&s->pb, 1, 0);        /* no PEI */
132
}
133

    
134
void h263_encode_mb(MpegEncContext * s,
135
                    DCTELEM block[6][64],
136
                    int motion_x, int motion_y)
137
{
138
    int cbpc, cbpy, i, cbp, pred_x, pred_y;
139

    
140
    //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
141
    if (!s->mb_intra) {
142
        /* compute cbp */
143
        cbp = 0;
144
        for (i = 0; i < 6; i++) {
145
            if (s->block_last_index[i] >= 0)
146
                cbp |= 1 << (5 - i);
147
        }
148
        if ((cbp | motion_x | motion_y) == 0) {
149
            /* skip macroblock */
150
            put_bits(&s->pb, 1, 1);
151
            return;
152
        }
153
        put_bits(&s->pb, 1, 0);        /* mb coded */
154
        cbpc = cbp & 3;
155
        put_bits(&s->pb,
156
                 inter_MCBPC_bits[cbpc],
157
                 inter_MCBPC_code[cbpc]);
158
        cbpy = cbp >> 2;
159
        cbpy ^= 0xf;
160
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
161

    
162
        /* motion vectors: 16x16 mode only now */
163
        h263_pred_motion(s, 0, &pred_x, &pred_y);
164
        
165
        h263_encode_motion(s, motion_x - pred_x);
166
        h263_encode_motion(s, motion_y - pred_y);
167
    } else {
168
        /* compute cbp */
169
        cbp = 0;
170
        for (i = 0; i < 6; i++) {
171
            if (s->block_last_index[i] >= 1)
172
                cbp |= 1 << (5 - i);
173
        }
174

    
175
        cbpc = cbp & 3;
176
        if (s->pict_type == I_TYPE) {
177
            put_bits(&s->pb,
178
                     intra_MCBPC_bits[cbpc],
179
                     intra_MCBPC_code[cbpc]);
180
        } else {
181
            put_bits(&s->pb, 1, 0);        /* mb coded */
182
            put_bits(&s->pb,
183
                     inter_MCBPC_bits[cbpc + 4],
184
                     inter_MCBPC_code[cbpc + 4]);
185
        }
186
        if (s->h263_pred) {
187
            /* XXX: currently, we do not try to use ac prediction */
188
            put_bits(&s->pb, 1, 0);        /* no ac prediction */
189
        }
190
        cbpy = cbp >> 2;
191
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
192
    }
193

    
194
    /* encode each block */
195
    if (s->h263_pred) {
196
        for (i = 0; i < 6; i++) {
197
            mpeg4_encode_block(s, block[i], i);
198
        }
199
    } else {
200
        for (i = 0; i < 6; i++) {
201
            h263_encode_block(s, block[i], i);
202
        }
203
    }
204
}
205

    
206
static inline int mid_pred(int a, int b, int c)
207
{
208
    int vmin, vmax;
209
    vmin = a;
210
    if (b < vmin)
211
        vmin = b;
212
    if (c < vmin)
213
        vmin = c;
214

    
215
    vmax = a;
216
    if (b > vmax)
217
        vmax = b;
218
    if (c > vmax)
219
        vmax = c;
220

    
221
    return a + b + c - vmin - vmax;
222
}
223

    
224
INT16 *h263_pred_motion(MpegEncContext * s, int block, 
225
                        int *px, int *py)
226
{
227
    int x, y, wrap;
228
    INT16 *A, *B, *C, *mot_val;
229

    
230
    x = 2 * s->mb_x + 1 + (block & 1);
231
    y = 2 * s->mb_y + 1 + ((block >> 1) & 1);
232
    wrap = 2 * s->mb_width + 2;
233

    
234
    mot_val = s->motion_val[(x) + (y) * wrap];
235

    
236
    /* special case for first line */
237
    if (y == 1 || s->first_slice_line) {
238
        A = s->motion_val[(x-1) + (y) * wrap];
239
        *px = A[0];
240
        *py = A[1];
241
    } else {
242
        switch(block) {
243
        default:
244
        case 0:
245
            A = s->motion_val[(x-1) + (y) * wrap];
246
            B = s->motion_val[(x) + (y-1) * wrap];
247
            C = s->motion_val[(x+2) + (y-1) * wrap];
248
            break;
249
        case 1:
250
        case 2:
251
            A = s->motion_val[(x-1) + (y) * wrap];
252
            B = s->motion_val[(x) + (y-1) * wrap];
253
            C = s->motion_val[(x+1) + (y-1) * wrap];
254
            break;
255
        case 3:
256
            A = s->motion_val[(x-1) + (y) * wrap];
257
            B = s->motion_val[(x-1) + (y-1) * wrap];
258
            C = s->motion_val[(x) + (y-1) * wrap];
259
            break;
260
        }
261
        *px = mid_pred(A[0], B[0], C[0]);
262
        *py = mid_pred(A[1], B[1], C[1]);
263
    }
264
    return mot_val;
265
}
266

    
267

    
268
static void h263_encode_motion(MpegEncContext * s, int val)
269
{
270
    int range, l, m, bit_size, sign, code, bits;
271

    
272
    if (val == 0) {
273
        /* zero vector */
274
        code = 0;
275
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
276
    } else {
277
        bit_size = s->f_code - 1;
278
        range = 1 << bit_size;
279
        /* modulo encoding */
280
        l = range * 32;
281
        m = 2 * l;
282
        if (val < -l) {
283
            val += m;
284
        } else if (val >= l) {
285
            val -= m;
286
        }
287

    
288
        if (val >= 0) {
289
            val--;
290
            code = (val >> bit_size) + 1;
291
            bits = val & (range - 1);
292
            sign = 0;
293
        } else {
294
            val = -val;
295
            val--;
296
            code = (val >> bit_size) + 1;
297
            bits = val & (range - 1);
298
            sign = 1;
299
        }
300

    
301
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
302
        if (bit_size > 0) {
303
            put_bits(&s->pb, bit_size, bits);
304
        }
305
    }
306
}
307

    
308
void h263_encode_init_vlc(MpegEncContext *s)
309
{
310
    static int done = 0;
311

    
312
    if (!done) {
313
        done = 1;
314
        init_rl(&rl_inter);
315
        init_rl(&rl_intra);
316
    }
317
}
318

    
319
static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
320
{
321
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
322
    int code;
323
    RLTable *rl = &rl_inter;
324

    
325
    if (s->mb_intra) {
326
        /* DC coef */
327
        level = block[0];
328
        /* 255 cannot be represented, so we clamp */
329
        if (level > 254) {
330
            level = 254;
331
            block[0] = 254;
332
        }
333
        if (level == 128)
334
            put_bits(&s->pb, 8, 0xff);
335
        else
336
            put_bits(&s->pb, 8, level & 0xff);
337
        i = 1;
338
    } else {
339
        i = 0;
340
    }
341

    
342
    /* AC coefs */
343
    last_index = s->block_last_index[n];
344
    last_non_zero = i - 1;
345
    for (; i <= last_index; i++) {
346
        j = zigzag_direct[i];
347
        level = block[j];
348
        if (level) {
349
            run = i - last_non_zero - 1;
350
            last = (i == last_index);
351
            sign = 0;
352
            slevel = level;
353
            if (level < 0) {
354
                sign = 1;
355
                level = -level;
356
            }
357
            code = get_rl_index(rl, last, run, level);
358
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
359
            if (code == rl->n) {
360
                put_bits(&s->pb, 1, last);
361
                put_bits(&s->pb, 6, run);
362
                put_bits(&s->pb, 8, slevel & 0xff);
363
            } else {
364
                put_bits(&s->pb, 1, sign);
365
            }
366
            last_non_zero = i;
367
        }
368
    }
369
}
370

    
371
/***************************************************/
372

    
373
/* write mpeg4 VOP header */
374
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
375
{
376
    align_put_bits(&s->pb);
377

    
378
    put_bits(&s->pb, 32, 0x1B6);        /* vop header */
379
    put_bits(&s->pb, 2, s->pict_type - 1);        /* pict type: I = 0 , P = 1 */
380
    /* XXX: time base + 1 not always correct */
381
    put_bits(&s->pb, 1, 1);
382
    put_bits(&s->pb, 1, 0);
383

    
384
    put_bits(&s->pb, 1, 1);        /* marker */
385
    put_bits(&s->pb, 4, 1);        /* XXX: correct time increment */
386
    put_bits(&s->pb, 1, 1);        /* marker */
387
    put_bits(&s->pb, 1, 1);        /* vop coded */
388
    if (s->pict_type == P_TYPE) {
389
        s->no_rounding = 0;
390
        put_bits(&s->pb, 1, s->no_rounding);        /* rounding type */
391
    }
392
    put_bits(&s->pb, 3, 0);        /* intra dc VLC threshold */
393

    
394
    put_bits(&s->pb, 5, s->qscale);
395

    
396
    if (s->pict_type != I_TYPE)
397
        put_bits(&s->pb, 3, s->f_code);        /* fcode_for */
398
    //    printf("****frame %d\n", picture_number);
399
}
400

    
401
void h263_dc_scale(MpegEncContext * s)
402
{
403
    int quant;
404

    
405
    quant = s->qscale;
406
    /* luminance */
407
    if (quant < 5)
408
        s->y_dc_scale = 8;
409
    else if (quant > 4 && quant < 9)
410
        s->y_dc_scale = (2 * quant);
411
    else if (quant > 8 && quant < 25)
412
        s->y_dc_scale = (quant + 8);
413
    else
414
        s->y_dc_scale = (2 * quant - 16);
415
    /* chrominance */
416
    if (quant < 5)
417
        s->c_dc_scale = 8;
418
    else if (quant > 4 && quant < 25)
419
        s->c_dc_scale = ((quant + 13) / 2);
420
    else
421
        s->c_dc_scale = (quant - 6);
422
}
423

    
424
static int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
425
{
426
    int a, b, c, x, y, wrap, pred, scale;
427
    UINT16 *dc_val;
428

    
429
    /* find prediction */
430
    if (n < 4) {
431
        x = 2 * s->mb_x + 1 + (n & 1);
432
        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
433
        wrap = s->mb_width * 2 + 2;
434
        dc_val = s->dc_val[0];
435
        scale = s->y_dc_scale;
436
    } else {
437
        x = s->mb_x + 1;
438
        y = s->mb_y + 1;
439
        wrap = s->mb_width + 2;
440
        dc_val = s->dc_val[n - 4 + 1];
441
        scale = s->c_dc_scale;
442
    }
443

    
444
    /* B C
445
     * A X 
446
     */
447
    a = dc_val[(x - 1) + (y) * wrap];
448
    b = dc_val[(x - 1) + (y - 1) * wrap];
449
    c = dc_val[(x) + (y - 1) * wrap];
450

    
451
    if (abs(a - b) < abs(b - c)) {
452
        pred = c;
453
        *dir_ptr = 1; /* top */
454
    } else {
455
        pred = a;
456
        *dir_ptr = 0; /* left */
457
    }
458
    /* we assume pred is positive */
459
    pred = (pred + (scale >> 1)) / scale;
460

    
461
    /* prepare address for prediction update */
462
    *dc_val_ptr = &dc_val[(x) + (y) * wrap];
463

    
464
    return pred;
465
}
466

    
467
void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n, 
468
                   int dir)
469
{
470
    int x, y, wrap, i;
471
    INT16 *ac_val, *ac_val1;
472

    
473
    /* find prediction */
474
    if (n < 4) {
475
        x = 2 * s->mb_x + 1 + (n & 1);
476
        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
477
        wrap = s->mb_width * 2 + 2;
478
        ac_val = s->ac_val[0][0];
479
    } else {
480
        x = s->mb_x + 1;
481
        y = s->mb_y + 1;
482
        wrap = s->mb_width + 2;
483
        ac_val = s->ac_val[n - 4 + 1][0];
484
    }
485
    ac_val += ((y) * wrap + (x)) * 16;
486
    ac_val1 = ac_val;
487
    if (s->ac_pred) {
488
        if (dir == 0) {
489
            /* left prediction */
490
            ac_val -= 16;
491
            for(i=1;i<8;i++) {
492
                block[i*8] += ac_val[i];
493
            }
494
        } else {
495
            /* top prediction */
496
            ac_val -= 16 * wrap;
497
            for(i=1;i<8;i++) {
498
                block[i] += ac_val[i + 8];
499
            }
500
        }
501
    }
502
    /* left copy */
503
    for(i=1;i<8;i++)
504
        ac_val1[i] = block[i * 8];
505
    /* top copy */
506
    for(i=1;i<8;i++)
507
        ac_val1[8 + i] = block[i];
508
}
509

    
510
static inline void mpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
511
{
512
    int size, v, pred;
513
    UINT16 *dc_val;
514

    
515
    pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
516
    if (n < 4) {
517
        *dc_val = level * s->y_dc_scale;
518
    } else {
519
        *dc_val = level * s->c_dc_scale;
520
    }
521

    
522
    /* do the prediction */
523
    level -= pred;
524
    /* find number of bits */
525
    size = 0;
526
    v = abs(level);
527
    while (v) {
528
        v >>= 1;
529
        size++;
530
    }
531

    
532
    if (n < 4) {
533
        /* luminance */
534
        put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
535
    } else {
536
        /* chrominance */
537
        put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
538
    }
539

    
540
    /* encode remaining bits */
541
    if (size > 0) {
542
        if (level < 0)
543
            level = (-level) ^ ((1 << size) - 1);
544
        put_bits(&s->pb, size, level);
545
        if (size > 8)
546
            put_bits(&s->pb, 1, 1);
547
    }
548
}
549

    
550
static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
551
{
552
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
553
    int code, dc_pred_dir;
554
    const RLTable *rl;
555

    
556
    if (s->mb_intra) {
557
        /* mpeg4 based DC predictor */
558
        mpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
559
        i = 1;
560
        rl = &rl_intra;
561
    } else {
562
        i = 0;
563
        rl = &rl_inter;
564
    }
565

    
566
    /* AC coefs */
567
    last_index = s->block_last_index[n];
568
    last_non_zero = i - 1;
569
    for (; i <= last_index; i++) {
570
        j = zigzag_direct[i];
571
        level = block[j];
572
        if (level) {
573
            run = i - last_non_zero - 1;
574
            last = (i == last_index);
575
            sign = 0;
576
            slevel = level;
577
            if (level < 0) {
578
                sign = 1;
579
                level = -level;
580
            }
581
            code = get_rl_index(rl, last, run, level);
582
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
583
            if (code == rl->n) {
584
                int level1, run1;
585
                level1 = level - rl->max_level[last][run];
586
                if (level1 < 1) 
587
                    goto esc2;
588
                code = get_rl_index(rl, last, run, level1);
589
                if (code == rl->n) {
590
                esc2:
591
                    put_bits(&s->pb, 1, 1);
592
                    if (level > MAX_LEVEL)
593
                        goto esc3;
594
                    run1 = run - rl->max_run[last][level] - 1;
595
                    if (run1 < 0)
596
                        goto esc3;
597
                    code = get_rl_index(rl, last, run1, level);
598
                    if (code == rl->n) {
599
                    esc3:
600
                        /* third escape */
601
                        put_bits(&s->pb, 1, 1);
602
                        put_bits(&s->pb, 1, last);
603
                        put_bits(&s->pb, 6, run);
604
                        put_bits(&s->pb, 1, 1);
605
                        put_bits(&s->pb, 12, slevel & 0xfff);
606
                        put_bits(&s->pb, 1, 1);
607
                    } else {
608
                        /* second escape */
609
                        put_bits(&s->pb, 1, 0);
610
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
611
                        put_bits(&s->pb, 1, sign);
612
                    }
613
                } else {
614
                    /* first escape */
615
                    put_bits(&s->pb, 1, 0);
616
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
617
                    put_bits(&s->pb, 1, sign);
618
                }
619
            } else {
620
                put_bits(&s->pb, 1, sign);
621
            }
622
            last_non_zero = i;
623
        }
624
    }
625
}
626

    
627

    
628

    
629
/***********************************************/
630
/* decoding */
631

    
632
static VLC intra_MCBPC_vlc;
633
static VLC inter_MCBPC_vlc;
634
static VLC cbpy_vlc;
635
static VLC mv_vlc;
636
static VLC dc_lum, dc_chrom;
637

    
638
void init_rl(RLTable *rl)
639
{
640
    INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
641
    UINT8 index_run[MAX_RUN+1];
642
    int last, run, level, start, end, i;
643

    
644
    /* compute max_level[], max_run[] and index_run[] */
645
    for(last=0;last<2;last++) {
646
        if (last == 0) {
647
            start = 0;
648
            end = rl->last;
649
        } else {
650
            start = rl->last;
651
            end = rl->n;
652
        }
653

    
654
        memset(max_level, 0, MAX_RUN + 1);
655
        memset(max_run, 0, MAX_LEVEL + 1);
656
        memset(index_run, rl->n, MAX_RUN + 1);
657
        for(i=start;i<end;i++) {
658
            run = rl->table_run[i];
659
            level = rl->table_level[i];
660
            if (index_run[run] == rl->n)
661
                index_run[run] = i;
662
            if (level > max_level[run])
663
                max_level[run] = level;
664
            if (run > max_run[level])
665
                max_run[level] = run;
666
        }
667
        rl->max_level[last] = malloc(MAX_RUN + 1);
668
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
669
        rl->max_run[last] = malloc(MAX_LEVEL + 1);
670
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
671
        rl->index_run[last] = malloc(MAX_RUN + 1);
672
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
673
    }
674
}
675

    
676
void init_vlc_rl(RLTable *rl)
677
{
678
    init_vlc(&rl->vlc, 9, rl->n + 1, 
679
             &rl->table_vlc[0][1], 4, 2,
680
             &rl->table_vlc[0][0], 4, 2);
681
}
682

    
683
/* init vlcs */
684

    
685
/* XXX: find a better solution to handle static init */
686
void h263_decode_init_vlc(MpegEncContext *s)
687
{
688
    static int done = 0;
689

    
690
    if (!done) {
691
        done = 1;
692

    
693
        init_vlc(&intra_MCBPC_vlc, 6, 8, 
694
                 intra_MCBPC_bits, 1, 1,
695
                 intra_MCBPC_code, 1, 1);
696
        init_vlc(&inter_MCBPC_vlc, 9, 20, 
697
                 inter_MCBPC_bits, 1, 1,
698
                 inter_MCBPC_code, 1, 1);
699
        init_vlc(&cbpy_vlc, 6, 16,
700
                 &cbpy_tab[0][1], 2, 1,
701
                 &cbpy_tab[0][0], 2, 1);
702
        init_vlc(&mv_vlc, 9, 33,
703
                 &mvtab[0][1], 2, 1,
704
                 &mvtab[0][0], 2, 1);
705
        init_rl(&rl_inter);
706
        init_rl(&rl_intra);
707
        init_vlc_rl(&rl_inter);
708
        init_vlc_rl(&rl_intra);
709
        init_vlc(&dc_lum, 9, 13,
710
                 &DCtab_lum[0][1], 2, 1,
711
                 &DCtab_lum[0][0], 2, 1);
712
        init_vlc(&dc_chrom, 9, 13,
713
                 &DCtab_chrom[0][1], 2, 1,
714
                 &DCtab_chrom[0][0], 2, 1);
715
    }
716
}
717

    
718
int h263_decode_mb(MpegEncContext *s,
719
                   DCTELEM block[6][64])
720
{
721
    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
722
    INT16 *mot_val;
723
    static UINT8 quant_tab[4] = { -1, -2, 1, 2 };
724

    
725
    if (s->pict_type == P_TYPE) {
726
        if (get_bits1(&s->gb)) {
727
            /* skip mb */
728
            s->mb_intra = 0;
729
            for(i=0;i<6;i++)
730
                s->block_last_index[i] = -1;
731
            s->mv_dir = MV_DIR_FORWARD;
732
            s->mv_type = MV_TYPE_16X16;
733
            s->mv[0][0][0] = 0;
734
            s->mv[0][0][1] = 0;
735
            s->mb_skiped = 1;
736
            return 0;
737
        }
738
        cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc);
739
        if (cbpc < 0)
740
            return -1;
741
        dquant = cbpc & 8;
742
        s->mb_intra = ((cbpc & 4) != 0);
743
    } else {
744
        cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
745
        if (cbpc < 0)
746
            return -1;
747
        dquant = cbpc & 4;
748
        s->mb_intra = 1;
749
    }
750

    
751
    if (!s->mb_intra) {
752
        cbpy = get_vlc(&s->gb, &cbpy_vlc);
753
        cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
754
        if (dquant) {
755
            s->qscale += quant_tab[get_bits(&s->gb, 2)];
756
            if (s->qscale < 1)
757
                s->qscale = 1;
758
            else if (s->qscale > 31)
759
                s->qscale = 31;
760
        }
761
        s->mv_dir = MV_DIR_FORWARD;
762
        if ((cbpc & 16) == 0) {
763
            /* 16x16 motion prediction */
764
            s->mv_type = MV_TYPE_16X16;
765
            h263_pred_motion(s, 0, &pred_x, &pred_y);
766
            mx = h263_decode_motion(s, pred_x);
767
            if (mx >= 0xffff)
768
                return -1;
769
            my = h263_decode_motion(s, pred_y);
770
            if (my >= 0xffff)
771
                return -1;
772
            s->mv[0][0][0] = mx;
773
            s->mv[0][0][1] = my;
774
        } else {
775
            s->mv_type = MV_TYPE_8X8;
776
            for(i=0;i<4;i++) {
777
                mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
778
                mx = h263_decode_motion(s, pred_x);
779
                if (mx >= 0xffff)
780
                    return -1;
781
                my = h263_decode_motion(s, pred_y);
782
                if (my >= 0xffff)
783
                    return -1;
784
                s->mv[0][i][0] = mx;
785
                s->mv[0][i][1] = my;
786
                mot_val[0] = mx;
787
                mot_val[1] = my;
788
            }
789
        }
790
    } else {
791
        s->ac_pred = 0;
792
        if (s->h263_pred) {
793
            s->ac_pred = get_bits1(&s->gb);
794
        }
795
        cbpy = get_vlc(&s->gb, &cbpy_vlc);
796
        cbp = (cbpc & 3) | (cbpy << 2);
797
        if (dquant) {
798
            s->qscale += quant_tab[get_bits(&s->gb, 2)];
799
            if (s->qscale < 1)
800
                s->qscale = 1;
801
            else if (s->qscale > 31)
802
                s->qscale = 31;
803
        }
804
    }
805

    
806
    /* decode each block */
807
    if (s->h263_pred) {
808
        for (i = 0; i < 6; i++) {
809
            if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
810
                return -1;
811
        }
812
    } else {
813
        for (i = 0; i < 6; i++) {
814
            if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
815
                return -1;
816
        }
817
    }
818
    return 0;
819
}
820

    
821
static int h263_decode_motion(MpegEncContext * s, int pred)
822
{
823
    int code, val, sign, shift, l, m;
824

    
825
    code = get_vlc(&s->gb, &mv_vlc);
826
    if (code < 0)
827
        return 0xffff;
828

    
829
    if (code == 0)
830
        return pred;
831
    sign = get_bits1(&s->gb);
832
    shift = s->f_code - 1;
833
    val = (code - 1) << shift;
834
    if (shift > 0)
835
        val |= get_bits(&s->gb, shift);
836
    val++;
837
    if (sign)
838
        val = -val;
839
    val += pred;
840
    
841
    /* modulo decoding */
842
    if (!s->h263_long_vectors) {
843
        l = (1 << (s->f_code - 1)) * 32;
844
        m = 2 * l;
845
        if (val < -l) {
846
            val += m;
847
        } else if (val >= l) {
848
            val -= m;
849
        }
850
    } else {
851
        /* horrible h263 long vector mode */
852
        if (pred < -31 && val < -63)
853
            val += 64;
854
        if (pred > 32 && val > 63)
855
            val -= 64;
856
    }
857
    return val;
858
}
859

    
860
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
861
                             int n, int coded)
862
{
863
    int code, level, i, j, last, run;
864
    RLTable *rl = &rl_inter;
865

    
866
    if (s->mb_intra) {
867
        /* DC coef */
868
        if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
869
            int component, diff;
870
            component = (n <= 3 ? 0 : n - 4 + 1);
871
            level = s->last_dc[component];
872
            if (s->rv10_first_dc_coded[component]) {
873
                diff = rv_decode_dc(s, n);
874
                if (diff == 0xffff)
875
                    return -1;
876
                level += diff;
877
                level = level & 0xff; /* handle wrap round */
878
                s->last_dc[component] = level;
879
            } else {
880
                s->rv10_first_dc_coded[component] = 1;
881
            }
882
        } else {
883
            level = get_bits(&s->gb, 8);
884
            if (level == 255)
885
                level = 128;
886
        }
887
        block[0] = level;
888
        i = 1;
889
    } else {
890
        i = 0;
891
    }
892
    if (!coded) {
893
        s->block_last_index[n] = i - 1;
894
        return 0;
895
    }
896

    
897
    for(;;) {
898
        code = get_vlc(&s->gb, &rl->vlc);
899
        if (code < 0)
900
            return -1;
901
        if (code == rl->n) {
902
            /* escape */
903
            last = get_bits1(&s->gb);
904
            run = get_bits(&s->gb, 6);
905
            level = (INT8)get_bits(&s->gb, 8);
906
            if (s->h263_rv10 && level == -128) {
907
                /* XXX: should patch encoder too */
908
                level = get_bits(&s->gb, 12);
909
                level = (level << 20) >> 20;
910
            }
911
        } else {
912
            run = rl->table_run[code];
913
            level = rl->table_level[code];
914
            last = code >= rl->last;
915
            if (get_bits1(&s->gb))
916
                level = -level;
917
        }
918
        i += run;
919
        if (i >= 64)
920
            return -1;
921
        j = zigzag_direct[i];
922
        block[j] = level;
923
        if (last)
924
            break;
925
        i++;
926
    }
927
    s->block_last_index[n] = i;
928
    return 0;
929
}
930

    
931
static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
932
{
933
    int level, pred, code;
934
    UINT16 *dc_val;
935

    
936
    if (n < 4) 
937
        code = get_vlc(&s->gb, &dc_lum);
938
    else 
939
        code = get_vlc(&s->gb, &dc_chrom);
940
    if (code < 0)
941
        return -1;
942
    if (code == 0) {
943
        level = 0;
944
    } else {
945
        level = get_bits(&s->gb, code);
946
        if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
947
            level = - (level ^ ((1 << code) - 1));
948
        if (code > 8)
949
            skip_bits1(&s->gb); /* marker */
950
    }
951

    
952
    pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
953
    level += pred;
954
    if (level < 0)
955
        level = 0;
956
    if (n < 4) {
957
        *dc_val = level * s->y_dc_scale;
958
    } else {
959
        *dc_val = level * s->c_dc_scale;
960
    }
961
    return level;
962
}
963

    
964
static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
965
                              int n, int coded)
966
{
967
    int code, level, i, j, last, run;
968
    int dc_pred_dir;
969
    RLTable *rl;
970
    const UINT8 *scan_table;
971

    
972
    if (s->mb_intra) {
973
        /* DC coef */
974
        level = mpeg4_decode_dc(s, n, &dc_pred_dir);
975
        if (level < 0)
976
            return -1;
977
        block[0] = level;
978
        i = 1;
979
        if (!coded) 
980
            goto not_coded;
981
        rl = &rl_intra;
982
        if (s->ac_pred) {
983
            if (dc_pred_dir == 0) 
984
                scan_table = ff_alternate_vertical_scan; /* left */
985
            else
986
                scan_table = ff_alternate_horizontal_scan; /* top */
987
        } else {
988
            scan_table = zigzag_direct;
989
        }
990
    } else {
991
        i = 0;
992
        if (!coded) {
993
            s->block_last_index[n] = i - 1;
994
            return 0;
995
        }
996
        rl = &rl_inter;
997
        scan_table = zigzag_direct;
998
    }
999

    
1000
    for(;;) {
1001
        code = get_vlc(&s->gb, &rl->vlc);
1002
        if (code < 0)
1003
            return -1;
1004
        if (code == rl->n) {
1005
            /* escape */
1006
            if (get_bits1(&s->gb) != 0) {
1007
                if (get_bits1(&s->gb) != 0) {
1008
                    /* third escape */
1009
                    last = get_bits1(&s->gb);
1010
                    run = get_bits(&s->gb, 6);
1011
                    get_bits1(&s->gb); /* marker */
1012
                    level = get_bits(&s->gb, 12);
1013
                    level = (level << 20) >> 20; /* sign extend */
1014
                    skip_bits1(&s->gb); /* marker */
1015
                } else {
1016
                    /* second escape */
1017
                    code = get_vlc(&s->gb, &rl->vlc);
1018
                    if (code < 0 || code >= rl->n)
1019
                        return -1;
1020
                    run = rl->table_run[code];
1021
                    level = rl->table_level[code];
1022
                    last = code >= rl->last;
1023
                    run += rl->max_run[last][level] + 1;
1024
                    if (get_bits1(&s->gb))
1025
                        level = -level;
1026
                }
1027
            } else {
1028
                /* first escape */
1029
                code = get_vlc(&s->gb, &rl->vlc);
1030
                if (code < 0 || code >= rl->n)
1031
                    return -1;
1032
                run = rl->table_run[code];
1033
                level = rl->table_level[code];
1034
                last = code >= rl->last;
1035
                level += rl->max_level[last][run];
1036
                if (get_bits1(&s->gb))
1037
                    level = -level;
1038
            }
1039
        } else {
1040
            run = rl->table_run[code];
1041
            level = rl->table_level[code];
1042
            last = code >= rl->last;
1043
            if (get_bits1(&s->gb))
1044
                level = -level;
1045
        }
1046
        i += run;
1047
        if (i >= 64)
1048
            return -1;
1049
        j = scan_table[i];
1050
        block[j] = level;
1051
        i++;
1052
        if (last)
1053
            break;
1054
    }
1055
 not_coded:
1056
    if (s->mb_intra) {
1057
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
1058
        if (s->ac_pred) {
1059
            i = 64; /* XXX: not optimal */
1060
        }
1061
    }
1062
    s->block_last_index[n] = i - 1;
1063
    return 0;
1064
}
1065

    
1066
/* most is hardcoded. should extend to handle all h263 streams */
1067
int h263_decode_picture_header(MpegEncContext *s)
1068
{
1069
    int format, width, height;
1070

    
1071
    /* picture header */
1072
    if (get_bits(&s->gb, 22) != 0x20)
1073
        return -1;
1074
    skip_bits(&s->gb, 8); /* picture timestamp */
1075

    
1076
    if (get_bits1(&s->gb) != 1)
1077
        return -1;        /* marker */
1078
    if (get_bits1(&s->gb) != 0)
1079
        return -1;        /* h263 id */
1080
    skip_bits1(&s->gb);        /* split screen off */
1081
    skip_bits1(&s->gb);        /* camera  off */
1082
    skip_bits1(&s->gb);        /* freeze picture release off */
1083

    
1084
    format = get_bits(&s->gb, 3);
1085

    
1086
    if (format != 7) {
1087
        s->h263_plus = 0;
1088
        /* H.263v1 */
1089
        width = h263_format[format][0];
1090
        height = h263_format[format][1];
1091
        if (!width)
1092
            return -1;
1093

    
1094
        s->pict_type = I_TYPE + get_bits1(&s->gb);
1095

    
1096
        s->unrestricted_mv = get_bits1(&s->gb); 
1097
        s->h263_long_vectors = s->unrestricted_mv;
1098

    
1099
        if (get_bits1(&s->gb) != 0)
1100
            return -1;        /* SAC: off */
1101
        if (get_bits1(&s->gb) != 0)
1102
            return -1;        /* advanced prediction mode: off */
1103
        if (get_bits1(&s->gb) != 0)
1104
            return -1;        /* not PB frame */
1105

    
1106
        s->qscale = get_bits(&s->gb, 5);
1107
        skip_bits1(&s->gb);        /* Continuous Presence Multipoint mode: off */
1108
    } else {
1109
        s->h263_plus = 1;
1110
        /* H.263v2 */
1111
        if (get_bits(&s->gb, 3) != 1)
1112
            return -1;
1113
        if (get_bits(&s->gb, 3) != 6) /* custom source format */
1114
            return -1;
1115
        skip_bits(&s->gb, 12);
1116
        skip_bits(&s->gb, 3);
1117
        s->pict_type = get_bits(&s->gb, 3) + 1;
1118
        if (s->pict_type != I_TYPE &&
1119
            s->pict_type != P_TYPE)
1120
            return -1;
1121
        skip_bits(&s->gb, 7);
1122
        skip_bits(&s->gb, 4); /* aspect ratio */
1123
        width = (get_bits(&s->gb, 9) + 1) * 4;
1124
        skip_bits1(&s->gb);
1125
        height = get_bits(&s->gb, 9) * 4;
1126
        if (height == 0)
1127
            return -1;
1128
        s->qscale = get_bits(&s->gb, 5);
1129
    }
1130
    /* PEI */
1131
    while (get_bits1(&s->gb) != 0) {
1132
        skip_bits(&s->gb, 8);
1133
    }
1134
    s->f_code = 1;
1135
    s->width = width;
1136
    s->height = height;
1137
    return 0;
1138
}
1139

    
1140
/* decode mpeg4 VOP header */
1141
int mpeg4_decode_picture_header(MpegEncContext * s)
1142
{
1143
    int time_incr, startcode, state, v;
1144

    
1145
 redo:
1146
    /* search next start code */
1147
    align_get_bits(&s->gb);
1148
    state = 0xff;
1149
    for(;;) {
1150
        v = get_bits(&s->gb, 8);
1151
        if (state == 0x000001) {
1152
            state = ((state << 8) | v) & 0xffffff;
1153
            startcode = state;
1154
            break;
1155
        }
1156
        state = ((state << 8) | v) & 0xffffff;
1157
        /* XXX: really detect end of frame */
1158
        if (state == 0)
1159
            return -1;
1160
    }
1161

    
1162
    if (startcode == 0x120) {
1163
        int time_increment_resolution, width, height, vo_ver_id;
1164

    
1165
        /* vol header */
1166
        skip_bits(&s->gb, 1); /* random access */
1167
        skip_bits(&s->gb, 8); /* vo_type */
1168
        if (get_bits1(&s->gb) != 0) { /* is_ol_id */
1169
            vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */
1170
            skip_bits(&s->gb, 3); /* vo_priority */
1171
        } else {
1172
            vo_ver_id = 1;
1173
        }
1174
        
1175
        skip_bits(&s->gb, 4); /* aspect_ratio_info */
1176
        skip_bits1(&s->gb); /* vol control parameter */
1177
        s->shape = get_bits(&s->gb, 2); /* vol shape */
1178
        skip_bits1(&s->gb);   /* marker */
1179
        
1180
        time_increment_resolution = get_bits(&s->gb, 16);
1181
        s->time_increment_bits = av_log2(time_increment_resolution - 1) + 1;
1182
        if (s->time_increment_bits < 1)
1183
            s->time_increment_bits = 1;
1184
        skip_bits1(&s->gb);   /* marker */
1185

    
1186
        if (get_bits1(&s->gb) != 0) {   /* fixed_vop_rate  */
1187
            skip_bits(&s->gb, s->time_increment_bits);
1188
        }
1189

    
1190
        if (s->shape != 2) {
1191
            if (s->shape == 0) {
1192
                skip_bits1(&s->gb);   /* marker */
1193
                width = get_bits(&s->gb, 13);
1194
                skip_bits1(&s->gb);   /* marker */
1195
                height = get_bits(&s->gb, 13);
1196
                skip_bits1(&s->gb);   /* marker */
1197
            }
1198
            
1199
            skip_bits1(&s->gb);   /* interlaced */
1200
            skip_bits1(&s->gb);   /* OBMC */
1201
            if (vo_ver_id == 1) {
1202
                s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */
1203
            } else {
1204
                s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */
1205
            }
1206
            if (get_bits1(&s->gb) == 1) {   /* not_8_bit */
1207
                s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */
1208
                skip_bits(&s->gb, 4); /* bits_per_pixel */
1209
            } else {
1210
                s->quant_precision = 5;
1211
            }
1212
            
1213
            skip_bits1(&s->gb);   /* vol_quant_type */
1214
            skip_bits1(&s->gb);   /* vol_quarter_pixel */
1215
            skip_bits1(&s->gb);   /* complexity_estimation_disabled */
1216
            skip_bits1(&s->gb);   /* resync_marker_disabled */
1217
            skip_bits1(&s->gb);   /* data_partioning_enabled */
1218
            if (get_bits1(&s->gb) != 0) { /* scalability */
1219
                printf("bad scalability!!!\n");
1220
                return -1;
1221
            }
1222
        }
1223
        goto redo;
1224
    } else if (startcode != 0x1b6) {
1225
        goto redo;
1226
    }
1227

    
1228
    s->pict_type = get_bits(&s->gb, 2) + 1;        /* pict type: I = 0 , P = 1 */
1229
    if (s->pict_type != I_TYPE &&
1230
        s->pict_type != P_TYPE)
1231
        return -1;
1232
    
1233
    /* XXX: parse time base */
1234
    time_incr = 0;
1235
    while (get_bits1(&s->gb) != 0) 
1236
        time_incr++;
1237

    
1238
    skip_bits1(&s->gb);           /* marker */
1239
    skip_bits(&s->gb, s->time_increment_bits);
1240
    skip_bits1(&s->gb);           /* marker */
1241
    /* vop coded */
1242
    if (get_bits1(&s->gb) != 1)
1243
        goto redo;
1244
    
1245
    if (s->shape != 2 && s->pict_type == P_TYPE) {
1246
        /* rounding type for motion estimation */
1247
        s->no_rounding = get_bits1(&s->gb);
1248
    } else {
1249
        s->no_rounding = 0;
1250
    }
1251
    
1252
     if (s->shape != 0) {
1253
         if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {
1254
             int width, height, hor_spat_ref, ver_spat_ref;
1255
 
1256
             width = get_bits(&s->gb, 13);
1257
             skip_bits1(&s->gb);   /* marker */
1258
             height = get_bits(&s->gb, 13);
1259
             skip_bits1(&s->gb);   /* marker */
1260
             hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */
1261
             skip_bits1(&s->gb);   /* marker */
1262
             ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */
1263
         }
1264
         skip_bits1(&s->gb); /* change_CR_disable */
1265
 
1266
         if (get_bits1(&s->gb) != 0) {
1267
             skip_bits(&s->gb, 8); /* constant_alpha_value */
1268
         }
1269
     }
1270

    
1271
     if (s->shape != 2) {
1272
         skip_bits(&s->gb, 3); /* intra dc VLC threshold */
1273
  
1274
         /* note: we do not use quant_precision to avoid problem if no
1275
            MPEG4 vol header as it is found on some old opendivx
1276
            movies */
1277
         s->qscale = get_bits(&s->gb, 5);
1278
  
1279
         if (s->pict_type != I_TYPE) {
1280
             s->f_code = get_bits(&s->gb, 3);        /* fcode_for */
1281
         }
1282
         if (s->shape && (s->pict_type != I_TYPE)) {
1283
             skip_bits1(&s->gb); // vop shape coding type
1284
         }
1285
     }
1286
     return 0;
1287
}
1288

    
1289
/* don't understand why they choose a different header ! */
1290
int intel_h263_decode_picture_header(MpegEncContext *s)
1291
{
1292
    int format;
1293

    
1294
    /* picture header */
1295
    if (get_bits(&s->gb, 22) != 0x20)
1296
        return -1;
1297
    skip_bits(&s->gb, 8); /* picture timestamp */
1298

    
1299
    if (get_bits1(&s->gb) != 1)
1300
        return -1;        /* marker */
1301
    if (get_bits1(&s->gb) != 0)
1302
        return -1;        /* h263 id */
1303
    skip_bits1(&s->gb);        /* split screen off */
1304
    skip_bits1(&s->gb);        /* camera  off */
1305
    skip_bits1(&s->gb);        /* freeze picture release off */
1306

    
1307
    format = get_bits(&s->gb, 3);
1308
    if (format != 7)
1309
        return -1;
1310

    
1311
    s->h263_plus = 0;
1312

    
1313
    s->pict_type = I_TYPE + get_bits1(&s->gb);
1314
    
1315
    s->unrestricted_mv = get_bits1(&s->gb); 
1316
    s->h263_long_vectors = s->unrestricted_mv;
1317

    
1318
    if (get_bits1(&s->gb) != 0)
1319
        return -1;        /* SAC: off */
1320
    if (get_bits1(&s->gb) != 0)
1321
        return -1;        /* advanced prediction mode: off */
1322
    if (get_bits1(&s->gb) != 0)
1323
        return -1;        /* not PB frame */
1324

    
1325
    /* skip unknown header garbage */
1326
    skip_bits(&s->gb, 41);
1327

    
1328
    s->qscale = get_bits(&s->gb, 5);
1329
    skip_bits1(&s->gb);        /* Continuous Presence Multipoint mode: off */
1330

    
1331
    /* PEI */
1332
    while (get_bits1(&s->gb) != 0) {
1333
        skip_bits(&s->gb, 8);
1334
    }
1335
    s->f_code = 1;
1336
    return 0;
1337
}