Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263.c @ e7774f11

History | View | Annotate | Download (48.7 KB)

1
/*
2
 * H263/MPEG4 backend for ffmpeg encoder and decoder
3
 * Copyright (c) 2000,2001 Gerard Lantau.
4
 * H263+ support.
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 h263p_encode_umotion(MpegEncContext * s, int val);
32
static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
33
                               int n);
34
static int h263_decode_motion(MpegEncContext * s, int pred);
35
static int h263p_decode_umotion(MpegEncContext * s, int pred);
36
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
37
                             int n, int coded);
38
static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
39
                              int n, int coded);
40

    
41
int h263_get_picture_format(int width, int height)
42
{
43
    int format;
44

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

    
60
void h263_encode_picture_header(MpegEncContext * s, int picture_number)
61
{
62
    int format;
63

    
64
    align_put_bits(&s->pb);
65

    
66
    /* Update the pointer to last GOB */
67
    s->ptr_lastgob = pbBufPtr(&s->pb);
68
    s->gob_number = 0;
69

    
70
    put_bits(&s->pb, 22, 0x20); /* PSC */
71
    put_bits(&s->pb, 8, (((INT64)s->picture_number * 30 * FRAME_RATE_BASE) / 
72
                         s->frame_rate) & 0xff);
73

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

    
145
    put_bits(&s->pb, 1, 0);        /* no PEI */
146
}
147

    
148
int h263_encode_gob_header(MpegEncContext * s, int mb_line)
149
{
150
    int pdif=0;
151
    
152
    /* Check to see if we need to put a new GBSC */
153
    /* for RTP packetization                    */
154
    if (s->rtp_mode) {
155
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
156
        if (pdif >= s->rtp_payload_size) {
157
            /* Bad luck, packet must be cut before */
158
            align_put_bits(&s->pb);
159
            flush_put_bits(&s->pb);
160
            /* Call the RTP callback to send the last GOB */
161
            if (s->rtp_callback) {
162
                pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
163
                s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
164
            }
165
            s->ptr_lastgob = pbBufPtr(&s->pb);
166
            put_bits(&s->pb, 17, 1); /* GBSC */
167
            s->gob_number = mb_line / s->gob_index;
168
            put_bits(&s->pb, 5, s->gob_number); /* GN */
169
            put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
170
            put_bits(&s->pb, 5, s->qscale); /* GQUANT */
171
            //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
172
            return pdif;
173
       } else if (pdif + s->mb_line_avgsize >= s->rtp_payload_size) {
174
           /* Cut the packet before we can't */
175
           align_put_bits(&s->pb);
176
           flush_put_bits(&s->pb);
177
           /* Call the RTP callback to send the last GOB */
178
           if (s->rtp_callback) {
179
               pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
180
               s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
181
           }
182
           s->ptr_lastgob = pbBufPtr(&s->pb);
183
           put_bits(&s->pb, 17, 1); /* GBSC */
184
           s->gob_number = mb_line / s->gob_index;
185
           put_bits(&s->pb, 5, s->gob_number); /* GN */
186
           put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
187
           put_bits(&s->pb, 5, s->qscale); /* GQUANT */
188
           //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
189
           return pdif;
190
       }
191
   }
192
   return 0;
193
}
194
    
195
void h263_encode_mb(MpegEncContext * s,
196
                    DCTELEM block[6][64],
197
                    int motion_x, int motion_y)
198
{
199
    int cbpc, cbpy, i, cbp, pred_x, pred_y;
200
   
201
    //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
202
   if (!s->mb_intra) {
203
           /* compute cbp */
204
           cbp = 0;
205
           for (i = 0; i < 6; i++) {
206
              if (s->block_last_index[i] >= 0)
207
                   cbp |= 1 << (5 - i);
208
           }
209
           if ((cbp | motion_x | motion_y) == 0) {
210
              /* skip macroblock */
211
              put_bits(&s->pb, 1, 1);
212
              return;
213
           }
214
           put_bits(&s->pb, 1, 0);        /* mb coded */
215
           cbpc = cbp & 3;
216
           put_bits(&s->pb,
217
                inter_MCBPC_bits[cbpc],
218
                inter_MCBPC_code[cbpc]);
219
           cbpy = cbp >> 2;
220
           cbpy ^= 0xf;
221
           put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
222

    
223
           /* motion vectors: 16x16 mode only now */
224
      h263_pred_motion(s, 0, &pred_x, &pred_y);
225
      
226
      if (!s->umvplus) {  
227
         h263_encode_motion(s, motion_x - pred_x);
228
         h263_encode_motion(s, motion_y - pred_y);
229
      }
230
      else {
231
         h263p_encode_umotion(s, motion_x - pred_x);
232
         h263p_encode_umotion(s, motion_y - pred_y);
233
         if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
234
            /* To prevent Start Code emulation */
235
            put_bits(&s->pb,1,1);
236
      }
237
   } else {
238
        /* compute cbp */
239
        cbp = 0;
240
        for (i = 0; i < 6; i++) {
241
            if (s->block_last_index[i] >= 1)
242
                cbp |= 1 << (5 - i);
243
        }
244

    
245
        cbpc = cbp & 3;
246
        if (s->pict_type == I_TYPE) {
247
            put_bits(&s->pb,
248
                     intra_MCBPC_bits[cbpc],
249
                     intra_MCBPC_code[cbpc]);
250
        } else {
251
            put_bits(&s->pb, 1, 0);        /* mb coded */
252
            put_bits(&s->pb,
253
                     inter_MCBPC_bits[cbpc + 4],
254
                     inter_MCBPC_code[cbpc + 4]);
255
        }
256
        if (s->h263_pred) {
257
            /* XXX: currently, we do not try to use ac prediction */
258
            put_bits(&s->pb, 1, 0);        /* no ac prediction */
259
        }
260
        cbpy = cbp >> 2;
261
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
262
    }
263

    
264
    /* encode each block */
265
    if (s->h263_pred) {
266
        for (i = 0; i < 6; i++) {
267
            mpeg4_encode_block(s, block[i], i);
268
        }
269
    } else {
270
        for (i = 0; i < 6; i++) {
271
            h263_encode_block(s, block[i], i);
272
        }
273
    }
274
}
275

    
276

    
277
void h263_pred_acdc(MpegEncContext * s, INT16 *block, int n)
278
{
279
    int x, y, wrap, a, c, pred_dc, scale, i;
280
    INT16 *dc_val, *ac_val, *ac_val1;
281

    
282
    /* find prediction */
283
    if (n < 4) {
284
        x = 2 * s->mb_x + 1 + (n & 1);
285
        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
286
        wrap = s->mb_width * 2 + 2;
287
        dc_val = s->dc_val[0];
288
        ac_val = s->ac_val[0][0];
289
        scale = s->y_dc_scale;
290
    } else {
291
        x = s->mb_x + 1;
292
        y = s->mb_y + 1;
293
        wrap = s->mb_width + 2;
294
        dc_val = s->dc_val[n - 4 + 1];
295
        ac_val = s->ac_val[n - 4 + 1][0];
296
        scale = s->c_dc_scale;
297
    }
298
    
299
    ac_val += ((y) * wrap + (x)) * 16;
300
    ac_val1 = ac_val;
301
    
302
    /* B C
303
     * A X 
304
     */
305
    a = dc_val[(x - 1) + (y) * wrap];
306
    c = dc_val[(x) + (y - 1) * wrap];
307
    
308
    pred_dc = 1024;
309
    if (s->ac_pred) {
310
        if (s->h263_aic_dir) {
311
            /* left prediction */
312
            if (a != 1024) {
313
                ac_val -= 16;
314
                for(i=1;i<8;i++) {
315
                    block[block_permute_op(i*8)] += ac_val[i];
316
                }
317
                pred_dc = a;
318
            }
319
        } else {
320
            /* top prediction */
321
            if (c != 1024) {
322
                ac_val -= 16 * wrap;
323
                for(i=1;i<8;i++) {
324
                    block[block_permute_op(i)] += ac_val[i + 8];
325
                }
326
                pred_dc = c;
327
            }
328
        }
329
    } else {
330
        /* just DC prediction */
331
        if (a != 1024 && c != 1024)
332
            pred_dc = (a + c) >> 1;
333
        else if (a != 1024)
334
            pred_dc = a;
335
        else
336
            pred_dc = c;
337
    }
338
    
339
    /* we assume pred is positive */
340
    block[0]=block[0]*scale + pred_dc;
341
    
342
    if (block[0] < 0)
343
        block[0] = 0;
344
    else if (!(block[0] & 1))
345
        block[0]++;
346
    
347
    /* Update AC/DC tables */
348
    dc_val[(x) + (y) * wrap] = block[0];
349
    
350
    /* left copy */
351
    for(i=1;i<8;i++)
352
        ac_val1[i] = block[block_permute_op(i * 8)];
353
    /* top copy */
354
    for(i=1;i<8;i++)
355
        ac_val1[8 + i] = block[block_permute_op(i)];
356
}
357

    
358

    
359
static inline int mid_pred(int a, int b, int c)
360
{
361
    int vmin, vmax;
362
    vmax = vmin = a;
363
    if (b < vmin)
364
        vmin = b;
365
    else
366
        vmax = b;
367

    
368
    if (c < vmin)
369
        vmin = c;
370
    else if (c > vmax)
371
        vmax = c;
372

    
373
    return a + b + c - vmin - vmax;
374
}
375

    
376
INT16 *h263_pred_motion(MpegEncContext * s, int block, 
377
                        int *px, int *py)
378
{
379
    int xy, y, wrap;
380
    INT16 *A, *B, *C, *mot_val;
381

    
382
    wrap = 2 * s->mb_width + 2;
383
    y = xy = 2 * s->mb_y + 1 + ((block >> 1) & 1); // y
384
    xy *= wrap; // y * wrap
385
    xy += 2 * s->mb_x + 1 + (block & 1); // x + y * wrap
386

    
387
    mot_val = s->motion_val[xy];
388

    
389
    /* special case for first line */
390
    if (y == 1 || s->first_slice_line || s->first_gob_line) {
391
        A = s->motion_val[xy - 1];
392
        *px = A[0];
393
        *py = A[1];
394
    } else {
395
        switch(block) {
396
        default:
397
        case 0:
398
            A = s->motion_val[xy - 1];
399
            B = s->motion_val[xy - wrap];
400
            C = s->motion_val[xy + 2 - wrap];
401
            break;
402
        case 1:
403
        case 2:
404
            A = s->motion_val[xy - 1];
405
            B = s->motion_val[xy - wrap];
406
            C = s->motion_val[xy + 1 - wrap];
407
            break;
408
        case 3:
409
            A = s->motion_val[xy - 1];
410
            B = s->motion_val[xy - 1 - wrap];
411
            C = s->motion_val[xy - wrap];
412
            break;
413
        }
414
        *px = mid_pred(A[0], B[0], C[0]);
415
        *py = mid_pred(A[1], B[1], C[1]);
416
    }
417
    return mot_val;
418
}
419

    
420

    
421
static void h263_encode_motion(MpegEncContext * s, int val)
422
{
423
    int range, l, m, bit_size, sign, code, bits;
424

    
425
    if (val == 0) {
426
        /* zero vector */
427
        code = 0;
428
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
429
    } else {
430
        bit_size = s->f_code - 1;
431
        range = 1 << bit_size;
432
        /* modulo encoding */
433
        l = range * 32;
434
        m = 2 * l;
435
        if (val < -l) {
436
            val += m;
437
        } else if (val >= l) {
438
            val -= m;
439
        }
440

    
441
        if (val >= 0) {
442
            val--;
443
            code = (val >> bit_size) + 1;
444
            bits = val & (range - 1);
445
            sign = 0;
446
        } else {
447
            val = -val;
448
            val--;
449
            code = (val >> bit_size) + 1;
450
            bits = val & (range - 1);
451
            sign = 1;
452
        }
453

    
454
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
455
        if (bit_size > 0) {
456
            put_bits(&s->pb, bit_size, bits);
457
        }
458
    }
459
}
460

    
461
/* Encode MV differences on H.263+ with Unrestricted MV mode */
462
static void h263p_encode_umotion(MpegEncContext * s, int val)
463
{
464
    short sval = 0; 
465
    short i = 0;
466
    short n_bits = 0;
467
    short temp_val;
468
    int code = 0;
469
    int tcode;
470
    
471
    if ( val == 0)
472
        put_bits(&s->pb, 1, 1);
473
    else if (val == 1)
474
        put_bits(&s->pb, 3, 0);
475
    else if (val == -1)
476
        put_bits(&s->pb, 3, 2);
477
    else {
478
        
479
        sval = ((val < 0) ? (short)(-val):(short)val);
480
        temp_val = sval;
481
        
482
        while (temp_val != 0) {
483
            temp_val = temp_val >> 1;
484
            n_bits++;
485
        }
486
        
487
        i = n_bits - 1;
488
        while (i > 0) {
489
            tcode = (sval & (1 << (i-1))) >> (i-1);
490
            tcode = (tcode << 1) | 1;
491
            code = (code << 2) | tcode;
492
            i--;
493
        }
494
        code = ((code << 1) | (val < 0)) << 1;
495
        put_bits(&s->pb, (2*n_bits)+1, code);
496
        //printf("\nVal = %d\tCode = %d", sval, code);
497
    }
498
}
499

    
500
void h263_encode_init_vlc(MpegEncContext *s)
501
{
502
    static int done = 0;
503

    
504
    if (!done) {
505
        done = 1;
506
        init_rl(&rl_inter);
507
        init_rl(&rl_intra);
508
    }
509
}
510

    
511
static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
512
{
513
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
514
    int code;
515
    RLTable *rl = &rl_inter;
516

    
517
    if (s->mb_intra) {
518
        /* DC coef */
519
            level = block[0];
520
        /* 255 cannot be represented, so we clamp */
521
        if (level > 254) {
522
            level = 254;
523
            block[0] = 254;
524
        }
525
        /* 0 cannot be represented also */
526
        else if (!level) {
527
            level = 1;
528
            block[0] = 1;
529
        }
530
            if (level == 128)
531
                put_bits(&s->pb, 8, 0xff);
532
            else
533
                put_bits(&s->pb, 8, level & 0xff);
534
            i = 1;
535
    } else {
536
            i = 0;
537
    }
538

    
539
    /* AC coefs */
540
    last_index = s->block_last_index[n];
541
    last_non_zero = i - 1;
542
    for (; i <= last_index; i++) {
543
        j = zigzag_direct[i];
544
        level = block[j];
545
        if (level) {
546
            run = i - last_non_zero - 1;
547
            last = (i == last_index);
548
            sign = 0;
549
            slevel = level;
550
            if (level < 0) {
551
                sign = 1;
552
                level = -level;
553
            }
554
            code = get_rl_index(rl, last, run, level);
555
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
556
            if (code == rl->n) {
557
                put_bits(&s->pb, 1, last);
558
                put_bits(&s->pb, 6, run);
559
                put_bits(&s->pb, 8, slevel & 0xff);
560
            } else {
561
                put_bits(&s->pb, 1, sign);
562
            }
563
            last_non_zero = i;
564
        }
565
    }
566
}
567

    
568
/***************************************************/
569

    
570
/* write mpeg4 VOP header */
571
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
572
{
573
    align_put_bits(&s->pb);
574

    
575
    put_bits(&s->pb, 16, 0);                /* vop header */
576
    put_bits(&s->pb, 16, 0x1B6);        /* vop header */
577
    put_bits(&s->pb, 2, s->pict_type - 1);        /* pict type: I = 0 , P = 1 */
578
    /* XXX: time base + 1 not always correct */
579
    put_bits(&s->pb, 1, 1);
580
    put_bits(&s->pb, 1, 0);
581

    
582
    put_bits(&s->pb, 1, 1);        /* marker */
583
    put_bits(&s->pb, 4, 1);        /* XXX: correct time increment */
584
    put_bits(&s->pb, 1, 1);        /* marker */
585
    put_bits(&s->pb, 1, 1);        /* vop coded */
586
    if (s->pict_type == P_TYPE) {
587
        s->no_rounding = 0;
588
        put_bits(&s->pb, 1, s->no_rounding);        /* rounding type */
589
    }
590
    put_bits(&s->pb, 3, 0);        /* intra dc VLC threshold */
591

    
592
    put_bits(&s->pb, 5, s->qscale);
593

    
594
    if (s->pict_type != I_TYPE)
595
        put_bits(&s->pb, 3, s->f_code);        /* fcode_for */
596
    //    printf("****frame %d\n", picture_number);
597
}
598

    
599
void h263_dc_scale(MpegEncContext * s)
600
{
601
    int quant;
602

    
603
    quant = s->qscale;
604
    /* luminance */
605
    if (quant < 5)
606
        s->y_dc_scale = 8;
607
    else if (quant > 4 && quant < 9)
608
        s->y_dc_scale = (2 * quant);
609
    else if (quant > 8 && quant < 25)
610
        s->y_dc_scale = (quant + 8);
611
    else
612
        s->y_dc_scale = (2 * quant - 16);
613
    /* chrominance */
614
    if (quant < 5)
615
        s->c_dc_scale = 8;
616
    else if (quant > 4 && quant < 25)
617
        s->c_dc_scale = ((quant + 13) / 2);
618
    else
619
        s->c_dc_scale = (quant - 6);
620
}
621

    
622
static int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
623
{
624
    int a, b, c, xy, wrap, pred, scale;
625
    UINT16 *dc_val;
626

    
627
    /* find prediction */
628
    if (n < 4) {
629
        wrap = s->mb_width * 2 + 2;
630
        xy = 2 * s->mb_y + 1 + ((n & 2) >> 1);
631
        xy *= wrap;
632
        xy += 2 * s->mb_x + 1 + (n & 1);
633
        dc_val = s->dc_val[0];
634
        scale = s->y_dc_scale;
635
    } else {
636
        wrap = s->mb_width + 2;
637
        xy = s->mb_y + 1;
638
        xy *= wrap;
639
        xy += s->mb_x + 1;
640
        dc_val = s->dc_val[n - 4 + 1];
641
        scale = s->c_dc_scale;
642
    }
643

    
644
    /* B C
645
     * A X 
646
     */
647
    a = dc_val[xy - 1];
648
    b = dc_val[xy - 1 - wrap];
649
    c = dc_val[xy - wrap];
650

    
651
    if (abs(a - b) < abs(b - c)) {
652
        pred = c;
653
        *dir_ptr = 1; /* top */
654
    } else {
655
        pred = a;
656
        *dir_ptr = 0; /* left */
657
    }
658
    /* we assume pred is positive */
659
    pred = (pred + (scale >> 1)) / scale;
660

    
661
    /* prepare address for prediction update */
662
    *dc_val_ptr = &dc_val[xy];
663

    
664
    return pred;
665
}
666

    
667
void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
668
                   int dir)
669
{
670
    int x, y, wrap, i;
671
    INT16 *ac_val, *ac_val1;
672

    
673
    /* find prediction */
674
    if (n < 4) {
675
        x = 2 * s->mb_x + 1 + (n & 1);
676
        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
677
        wrap = s->mb_width * 2 + 2;
678
        ac_val = s->ac_val[0][0];
679
    } else {
680
        x = s->mb_x + 1;
681
        y = s->mb_y + 1;
682
        wrap = s->mb_width + 2;
683
        ac_val = s->ac_val[n - 4 + 1][0];
684
    }
685
    ac_val += ((y) * wrap + (x)) * 16;
686
    ac_val1 = ac_val;
687
    if (s->ac_pred) {
688
        if (dir == 0) {
689
            /* left prediction */
690
            ac_val -= 16;
691
            for(i=1;i<8;i++) {
692
                block[block_permute_op(i*8)] += ac_val[i];
693
            }
694
        } else {
695
            /* top prediction */
696
            ac_val -= 16 * wrap;
697
            for(i=1;i<8;i++) {
698
                block[block_permute_op(i)] += ac_val[i + 8];
699
            }
700
        }
701
    }
702
    /* left copy */
703
    for(i=1;i<8;i++)
704
        ac_val1[i] = block[block_permute_op(i * 8)];
705
    /* top copy */
706
    for(i=1;i<8;i++)
707
        ac_val1[8 + i] = block[block_permute_op(i)];
708
}
709

    
710
static inline void mpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
711
{
712
    int size, v, pred;
713
    UINT16 *dc_val;
714

    
715
    pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
716
    if (n < 4) {
717
        *dc_val = level * s->y_dc_scale;
718
    } else {
719
        *dc_val = level * s->c_dc_scale;
720
    }
721

    
722
    /* do the prediction */
723
    level -= pred;
724
    /* find number of bits */
725
    size = 0;
726
    v = abs(level);
727
    while (v) {
728
        v >>= 1;
729
        size++;
730
    }
731

    
732
    if (n < 4) {
733
        /* luminance */
734
        put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
735
    } else {
736
        /* chrominance */
737
        put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
738
    }
739

    
740
    /* encode remaining bits */
741
    if (size > 0) {
742
        if (level < 0)
743
            level = (-level) ^ ((1 << size) - 1);
744
        put_bits(&s->pb, size, level);
745
        if (size > 8)
746
            put_bits(&s->pb, 1, 1);
747
    }
748
}
749

    
750
static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
751
{
752
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
753
    int code, dc_pred_dir;
754
    const RLTable *rl;
755

    
756
    if (s->mb_intra) {
757
        /* mpeg4 based DC predictor */
758
        mpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
759
        i = 1;
760
        rl = &rl_intra;
761
    } else {
762
        i = 0;
763
        rl = &rl_inter;
764
    }
765

    
766
    /* AC coefs */
767
    last_index = s->block_last_index[n];
768
    last_non_zero = i - 1;
769
    for (; i <= last_index; i++) {
770
        j = zigzag_direct[i];
771
        level = block[j];
772
        if (level) {
773
            run = i - last_non_zero - 1;
774
            last = (i == last_index);
775
            sign = 0;
776
            slevel = level;
777
            if (level < 0) {
778
                sign = 1;
779
                level = -level;
780
            }
781
            code = get_rl_index(rl, last, run, level);
782
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
783
            if (code == rl->n) {
784
                int level1, run1;
785
                level1 = level - rl->max_level[last][run];
786
                if (level1 < 1) 
787
                    goto esc2;
788
                code = get_rl_index(rl, last, run, level1);
789
                if (code == rl->n) {
790
                esc2:
791
                    put_bits(&s->pb, 1, 1);
792
                    if (level > MAX_LEVEL)
793
                        goto esc3;
794
                    run1 = run - rl->max_run[last][level] - 1;
795
                    if (run1 < 0)
796
                        goto esc3;
797
                    code = get_rl_index(rl, last, run1, level);
798
                    if (code == rl->n) {
799
                    esc3:
800
                        /* third escape */
801
                        put_bits(&s->pb, 1, 1);
802
                        put_bits(&s->pb, 1, last);
803
                        put_bits(&s->pb, 6, run);
804
                        put_bits(&s->pb, 1, 1);
805
                        put_bits(&s->pb, 12, slevel & 0xfff);
806
                        put_bits(&s->pb, 1, 1);
807
                    } else {
808
                        /* second escape */
809
                        put_bits(&s->pb, 1, 0);
810
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
811
                        put_bits(&s->pb, 1, sign);
812
                    }
813
                } else {
814
                    /* first escape */
815
                    put_bits(&s->pb, 1, 0);
816
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
817
                    put_bits(&s->pb, 1, sign);
818
                }
819
            } else {
820
                put_bits(&s->pb, 1, sign);
821
            }
822
            last_non_zero = i;
823
        }
824
    }
825
}
826

    
827

    
828

    
829
/***********************************************/
830
/* decoding */
831

    
832
static VLC intra_MCBPC_vlc;
833
static VLC inter_MCBPC_vlc;
834
static VLC cbpy_vlc;
835
static VLC mv_vlc;
836
static VLC dc_lum, dc_chrom;
837

    
838
void init_rl(RLTable *rl)
839
{
840
    INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
841
    UINT8 index_run[MAX_RUN+1];
842
    int last, run, level, start, end, i;
843

    
844
    /* compute max_level[], max_run[] and index_run[] */
845
    for(last=0;last<2;last++) {
846
        if (last == 0) {
847
            start = 0;
848
            end = rl->last;
849
        } else {
850
            start = rl->last;
851
            end = rl->n;
852
        }
853

    
854
        memset(max_level, 0, MAX_RUN + 1);
855
        memset(max_run, 0, MAX_LEVEL + 1);
856
        memset(index_run, rl->n, MAX_RUN + 1);
857
        for(i=start;i<end;i++) {
858
            run = rl->table_run[i];
859
            level = rl->table_level[i];
860
            if (index_run[run] == rl->n)
861
                index_run[run] = i;
862
            if (level > max_level[run])
863
                max_level[run] = level;
864
            if (run > max_run[level])
865
                max_run[level] = run;
866
        }
867
        rl->max_level[last] = malloc(MAX_RUN + 1);
868
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
869
        rl->max_run[last] = malloc(MAX_LEVEL + 1);
870
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
871
        rl->index_run[last] = malloc(MAX_RUN + 1);
872
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
873
    }
874
}
875

    
876
void init_vlc_rl(RLTable *rl)
877
{
878
    init_vlc(&rl->vlc, 9, rl->n + 1, 
879
             &rl->table_vlc[0][1], 4, 2,
880
             &rl->table_vlc[0][0], 4, 2);
881
}
882

    
883
/* init vlcs */
884

    
885
/* XXX: find a better solution to handle static init */
886
void h263_decode_init_vlc(MpegEncContext *s)
887
{
888
    static int done = 0;
889

    
890
    if (!done) {
891
        done = 1;
892

    
893
        init_vlc(&intra_MCBPC_vlc, 6, 8, 
894
                 intra_MCBPC_bits, 1, 1,
895
                 intra_MCBPC_code, 1, 1);
896
        init_vlc(&inter_MCBPC_vlc, 9, 25, 
897
                 inter_MCBPC_bits, 1, 1,
898
                 inter_MCBPC_code, 1, 1);
899
        init_vlc(&cbpy_vlc, 6, 16,
900
                 &cbpy_tab[0][1], 2, 1,
901
                 &cbpy_tab[0][0], 2, 1);
902
        init_vlc(&mv_vlc, 9, 33,
903
                 &mvtab[0][1], 2, 1,
904
                 &mvtab[0][0], 2, 1);
905
        init_rl(&rl_inter);
906
        init_rl(&rl_intra);
907
        init_rl(&rl_intra_aic);
908
        init_vlc_rl(&rl_inter);
909
        init_vlc_rl(&rl_intra);
910
        init_vlc_rl(&rl_intra_aic);
911
        init_vlc(&dc_lum, 9, 13,
912
                 &DCtab_lum[0][1], 2, 1,
913
                 &DCtab_lum[0][0], 2, 1);
914
        init_vlc(&dc_chrom, 9, 13,
915
                 &DCtab_chrom[0][1], 2, 1,
916
                 &DCtab_chrom[0][0], 2, 1);
917
    }
918
}
919

    
920
int h263_decode_gob_header(MpegEncContext *s)
921
{
922
    unsigned int val, gfid;
923
    
924
    /* Check for GOB Start Code */
925
    val = show_bits(&s->gb, 16);
926
    if (val == 0) {
927
        /* We have a GBSC probably with GSTUFF */
928
        skip_bits(&s->gb, 16); /* Drop the zeros */
929
        while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */
930
#ifdef DEBUG
931
        fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x);
932
#endif
933
        s->gob_number = get_bits(&s->gb, 5); /* GN */
934
        gfid = get_bits(&s->gb, 2); /* GFID */
935
        s->qscale = get_bits(&s->gb, 5); /* GQUANT */
936
#ifdef DEBUG
937
        fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale);
938
#endif
939
        return 1;
940
    }
941
    return 0;
942
            
943
}
944

    
945
int h263_decode_mb(MpegEncContext *s,
946
                   DCTELEM block[6][64])
947
{
948
    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
949
    INT16 *mot_val;
950
    static INT8 quant_tab[4] = { -1, -2, 1, 2 };
951
    
952
    if (s->pict_type == P_TYPE) {
953
        if (get_bits1(&s->gb)) {
954
            /* skip mb */
955
            s->mb_intra = 0;
956
            for(i=0;i<6;i++)
957
                s->block_last_index[i] = -1;
958
            s->mv_dir = MV_DIR_FORWARD;
959
            s->mv_type = MV_TYPE_16X16;
960
            s->mv[0][0][0] = 0;
961
            s->mv[0][0][1] = 0;
962
            s->mb_skiped = 1;
963
            return 0;
964
        }
965
        cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc);
966
        //fprintf(stderr, "\tCBPC: %d", cbpc);
967
        if (cbpc < 0)
968
            return -1;
969
        if (cbpc > 20)
970
            cbpc+=3;
971
        else if (cbpc == 20)
972
            fprintf(stderr, "Stuffing !");
973
        
974
        dquant = cbpc & 8;
975
        s->mb_intra = ((cbpc & 4) != 0);
976
    } else {
977
        cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
978
        if (cbpc < 0)
979
            return -1;
980
        dquant = cbpc & 4;
981
        s->mb_intra = 1;
982
    }
983

    
984
    if (!s->mb_intra) {
985
        cbpy = get_vlc(&s->gb, &cbpy_vlc);
986
        cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
987
        if (dquant) {
988
            s->qscale += quant_tab[get_bits(&s->gb, 2)];
989
            if (s->qscale < 1)
990
                s->qscale = 1;
991
            else if (s->qscale > 31)
992
                s->qscale = 31;
993
        }
994
        s->mv_dir = MV_DIR_FORWARD;
995
        if ((cbpc & 16) == 0) {
996
            /* 16x16 motion prediction */
997
            s->mv_type = MV_TYPE_16X16;
998
            h263_pred_motion(s, 0, &pred_x, &pred_y);
999
            if (s->umvplus_dec)
1000
               mx = h263p_decode_umotion(s, pred_x);
1001
            else
1002
               mx = h263_decode_motion(s, pred_x);
1003
            if (mx >= 0xffff)
1004
                return -1;
1005
            
1006
            if (s->umvplus_dec)
1007
               my = h263p_decode_umotion(s, pred_y);
1008
            else    
1009
               my = h263_decode_motion(s, pred_y);
1010
            if (my >= 0xffff)
1011
                return -1;
1012
            s->mv[0][0][0] = mx;
1013
            s->mv[0][0][1] = my;
1014
            /*fprintf(stderr, "\n MB %d", (s->mb_y * s->mb_width) + s->mb_x);
1015
            fprintf(stderr, "\n\tmvx: %d\t\tpredx: %d", mx, pred_x);
1016
            fprintf(stderr, "\n\tmvy: %d\t\tpredy: %d", my, pred_y);*/
1017
            if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
1018
               skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
1019
                           
1020
        } else {
1021
            s->mv_type = MV_TYPE_8X8;
1022
            for(i=0;i<4;i++) {
1023
                mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
1024
                if (s->umvplus_dec)
1025
                  mx = h263p_decode_umotion(s, pred_x);
1026
                else
1027
                  mx = h263_decode_motion(s, pred_x);
1028
                if (mx >= 0xffff)
1029
                    return -1;
1030
                
1031
                if (s->umvplus_dec)
1032
                  my = h263p_decode_umotion(s, pred_y);
1033
                else    
1034
                  my = h263_decode_motion(s, pred_y);
1035
                if (my >= 0xffff)
1036
                    return -1;
1037
                s->mv[0][i][0] = mx;
1038
                s->mv[0][i][1] = my;
1039
                if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
1040
                  skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
1041
                mot_val[0] = mx;
1042
                mot_val[1] = my;
1043
            }
1044
        }
1045
    } else {
1046
        s->ac_pred = 0;
1047
        if (s->h263_pred || s->h263_aic) {
1048
            s->ac_pred = get_bits1(&s->gb);
1049
            if (s->ac_pred && s->h263_aic)
1050
                s->h263_aic_dir = get_bits1(&s->gb);
1051
        }
1052
        if (s->h263_aic) {
1053
            s->y_dc_scale = 2 * s->qscale;
1054
            s->c_dc_scale = 2 * s->qscale;
1055
        }
1056
        cbpy = get_vlc(&s->gb, &cbpy_vlc);
1057
        cbp = (cbpc & 3) | (cbpy << 2);
1058
        if (dquant) {
1059
            s->qscale += quant_tab[get_bits(&s->gb, 2)];
1060
            if (s->qscale < 1)
1061
                s->qscale = 1;
1062
            else if (s->qscale > 31)
1063
                s->qscale = 31;
1064
        }
1065
    }
1066

    
1067
    /* decode each block */
1068
    if (s->h263_pred) {
1069
        for (i = 0; i < 6; i++) {
1070
            if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1071
                return -1;
1072
        }
1073
    } else {
1074
        for (i = 0; i < 6; i++) {
1075
            if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1076
                return -1;
1077
        }
1078
    }
1079
    return 0;
1080
}
1081

    
1082
static int h263_decode_motion(MpegEncContext * s, int pred)
1083
{
1084
    int code, val, sign, shift, l, m;
1085

    
1086
    code = get_vlc(&s->gb, &mv_vlc);
1087
    if (code < 0)
1088
        return 0xffff;
1089

    
1090
    if (code == 0)
1091
        return pred;
1092
    sign = get_bits1(&s->gb);
1093
    shift = s->f_code - 1;
1094
    val = (code - 1) << shift;
1095
    if (shift > 0)
1096
        val |= get_bits(&s->gb, shift);
1097
    val++;
1098
    if (sign)
1099
        val = -val;
1100
    val += pred;
1101
    
1102
    /* modulo decoding */
1103
    if (!s->h263_long_vectors) {
1104
        l = (1 << (s->f_code - 1)) * 32;
1105
        m = 2 * l;
1106
        if (val < -l) {
1107
            val += m;
1108
        } else if (val >= l) {
1109
            val -= m;
1110
        }
1111
    } else {
1112
        /* horrible h263 long vector mode */
1113
        if (pred < -31 && val < -63)
1114
            val += 64;
1115
        if (pred > 32 && val > 63)
1116
            val -= 64;
1117
        
1118
    }
1119
    return val;
1120
}
1121

    
1122
/* Decodes RVLC of H.263+ UMV */
1123
static int h263p_decode_umotion(MpegEncContext * s, int pred)
1124
{
1125
   int code = 0, sign;
1126
   
1127
   if (get_bits1(&s->gb)) /* Motion difference = 0 */
1128
      return pred;
1129
   
1130
   code = 2 + get_bits1(&s->gb);
1131
   
1132
   while (get_bits1(&s->gb))
1133
   {
1134
      code <<= 1;
1135
      code += get_bits1(&s->gb);
1136
   }
1137
   sign = code & 1;
1138
   code >>= 1;
1139
   
1140
   code = (sign) ? (pred - code) : (pred + code);
1141
#ifdef DEBUG
1142
   fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
1143
#endif
1144
   return code;   
1145

    
1146
}
1147

    
1148
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
1149
                             int n, int coded)
1150
{
1151
    int code, level, i, j, last, run;
1152
    RLTable *rl = &rl_inter;
1153
    const UINT8 *scan_table;
1154

    
1155
    scan_table = zigzag_direct;
1156
    if (s->h263_aic && s->mb_intra) {
1157
        rl = &rl_intra_aic;
1158
        i = 0;
1159
        if (s->ac_pred) {
1160
            if (s->h263_aic_dir) 
1161
                scan_table = ff_alternate_vertical_scan; /* left */
1162
            else
1163
                scan_table = ff_alternate_horizontal_scan; /* top */
1164
        }
1165
    } else if (s->mb_intra) {
1166
        /* DC coef */
1167
        if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
1168
            int component, diff;
1169
            component = (n <= 3 ? 0 : n - 4 + 1);
1170
            level = s->last_dc[component];
1171
            if (s->rv10_first_dc_coded[component]) {
1172
                diff = rv_decode_dc(s, n);
1173
                if (diff == 0xffff)
1174
                    return -1;
1175
                level += diff;
1176
                level = level & 0xff; /* handle wrap round */
1177
                s->last_dc[component] = level;
1178
            } else {
1179
                s->rv10_first_dc_coded[component] = 1;
1180
            }
1181
        } else {
1182
            level = get_bits(&s->gb, 8);
1183
            if (level == 255)
1184
                level = 128;
1185
        }
1186
        block[0] = level;
1187
        i = 1;
1188
    } else {
1189
        i = 0;
1190
    }
1191
    if (!coded) {
1192
        if (s->mb_intra && s->h263_aic)
1193
            goto not_coded;
1194
        s->block_last_index[n] = i - 1;
1195
        return 0;
1196
    }
1197

    
1198
    for(;;) {
1199
        code = get_vlc(&s->gb, &rl->vlc);
1200
        if (code < 0)
1201
            return -1;
1202
        if (code == rl->n) {
1203
            /* escape */
1204
            last = get_bits1(&s->gb);
1205
            run = get_bits(&s->gb, 6);
1206
            level = (INT8)get_bits(&s->gb, 8);
1207
            if (s->h263_rv10 && level == -128) {
1208
                /* XXX: should patch encoder too */
1209
                level = get_bits(&s->gb, 12);
1210
                level = (level << 20) >> 20;
1211
            }
1212
        } else {
1213
            run = rl->table_run[code];
1214
            level = rl->table_level[code];
1215
            last = code >= rl->last;
1216
            if (get_bits1(&s->gb))
1217
                level = -level;
1218
        }
1219
        i += run;
1220
        if (i >= 64)
1221
            return -1;
1222
        j = scan_table[i];
1223
        block[j] = level;
1224
        if (last)
1225
            break;
1226
        i++;
1227
    }
1228
not_coded:    
1229
    if (s->mb_intra && s->h263_aic) {
1230
        h263_pred_acdc(s, block, n);
1231
        i = 64;
1232
    }
1233
    s->block_last_index[n] = i;
1234
    return 0;
1235
}
1236

    
1237
static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1238
{
1239
    int level, pred, code;
1240
    UINT16 *dc_val;
1241

    
1242
    if (n < 4) 
1243
        code = get_vlc(&s->gb, &dc_lum);
1244
    else 
1245
        code = get_vlc(&s->gb, &dc_chrom);
1246
    if (code < 0)
1247
        return -1;
1248
    if (code == 0) {
1249
        level = 0;
1250
    } else {
1251
        level = get_bits(&s->gb, code);
1252
        if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
1253
            level = - (level ^ ((1 << code) - 1));
1254
        if (code > 8)
1255
            skip_bits1(&s->gb); /* marker */
1256
    }
1257

    
1258
    pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1259
    level += pred;
1260
    if (level < 0)
1261
        level = 0;
1262
    if (n < 4) {
1263
        *dc_val = level * s->y_dc_scale;
1264
    } else {
1265
        *dc_val = level * s->c_dc_scale;
1266
    }
1267
    return level;
1268
}
1269

    
1270
static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1271
                              int n, int coded)
1272
{
1273
    int code, level, i, j, last, run;
1274
    int dc_pred_dir;
1275
    RLTable *rl;
1276
    const UINT8 *scan_table;
1277

    
1278
    if (s->mb_intra) {
1279
        /* DC coef */
1280
        level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1281
        if (level < 0)
1282
            return -1;
1283
        block[0] = level;
1284
        i = 1;
1285
        if (!coded) 
1286
            goto not_coded;
1287
        rl = &rl_intra;
1288
        if (s->ac_pred) {
1289
            if (dc_pred_dir == 0) 
1290
                scan_table = ff_alternate_vertical_scan; /* left */
1291
            else
1292
                scan_table = ff_alternate_horizontal_scan; /* top */
1293
        } else {
1294
            scan_table = zigzag_direct;
1295
        }
1296
    } else {
1297
        i = 0;
1298
        if (!coded) {
1299
            s->block_last_index[n] = i - 1;
1300
            return 0;
1301
        }
1302
        rl = &rl_inter;
1303
        scan_table = zigzag_direct;
1304
    }
1305

    
1306
    for(;;) {
1307
        code = get_vlc(&s->gb, &rl->vlc);
1308
        if (code < 0)
1309
            return -1;
1310
        if (code == rl->n) {
1311
            /* escape */
1312
            if (get_bits1(&s->gb) != 0) {
1313
                if (get_bits1(&s->gb) != 0) {
1314
                    /* third escape */
1315
                    last = get_bits1(&s->gb);
1316
                    run = get_bits(&s->gb, 6);
1317
                    get_bits1(&s->gb); /* marker */
1318
                    level = get_bits(&s->gb, 12);
1319
                    level = (level << 20) >> 20; /* sign extend */
1320
                    skip_bits1(&s->gb); /* marker */
1321
                } else {
1322
                    /* second escape */
1323
                    code = get_vlc(&s->gb, &rl->vlc);
1324
                    if (code < 0 || code >= rl->n)
1325
                        return -1;
1326
                    run = rl->table_run[code];
1327
                    level = rl->table_level[code];
1328
                    last = code >= rl->last;
1329
                    run += rl->max_run[last][level] + 1;
1330
                    if (get_bits1(&s->gb))
1331
                        level = -level;
1332
                }
1333
            } else {
1334
                /* first escape */
1335
                code = get_vlc(&s->gb, &rl->vlc);
1336
                if (code < 0 || code >= rl->n)
1337
                    return -1;
1338
                run = rl->table_run[code];
1339
                level = rl->table_level[code];
1340
                last = code >= rl->last;
1341
                level += rl->max_level[last][run];
1342
                if (get_bits1(&s->gb))
1343
                    level = -level;
1344
            }
1345
        } else {
1346
            run = rl->table_run[code];
1347
            level = rl->table_level[code];
1348
            last = code >= rl->last;
1349
            if (get_bits1(&s->gb))
1350
                level = -level;
1351
        }
1352
        i += run;
1353
        if (i >= 64)
1354
            return -1;
1355
        j = scan_table[i];
1356
        block[j] = level;
1357
        i++;
1358
        if (last)
1359
            break;
1360
    }
1361
 not_coded:
1362
    if (s->mb_intra) {
1363
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
1364
        if (s->ac_pred) {
1365
            i = 64; /* XXX: not optimal */
1366
        }
1367
    }
1368
    s->block_last_index[n] = i - 1;
1369
    return 0;
1370
}
1371

    
1372
/* most is hardcoded. should extend to handle all h263 streams */
1373
int h263_decode_picture_header(MpegEncContext *s)
1374
{
1375
    int format, width, height;
1376

    
1377
    /* picture header */
1378
    if (get_bits(&s->gb, 22) != 0x20)
1379
        return -1;
1380
    s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
1381
    
1382
    if (get_bits1(&s->gb) != 1)
1383
        return -1;        /* marker */
1384
    if (get_bits1(&s->gb) != 0)
1385
        return -1;        /* h263 id */
1386
    skip_bits1(&s->gb);        /* split screen off */
1387
    skip_bits1(&s->gb);        /* camera  off */
1388
    skip_bits1(&s->gb);        /* freeze picture release off */
1389

    
1390
    /* Reset GOB number */
1391
    s->gob_number = 0;
1392
        
1393
    format = get_bits(&s->gb, 3);
1394

    
1395
    if (format != 7 && format != 6) {
1396
        s->h263_plus = 0;
1397
        /* H.263v1 */
1398
        width = h263_format[format][0];
1399
        height = h263_format[format][1];
1400
        if (!width)
1401
            return -1;
1402
        
1403
        s->width = width;
1404
        s->height = height;
1405
        s->pict_type = I_TYPE + get_bits1(&s->gb);
1406

    
1407
        s->unrestricted_mv = get_bits1(&s->gb); 
1408
        s->h263_long_vectors = s->unrestricted_mv;
1409

    
1410
        if (get_bits1(&s->gb) != 0)
1411
            return -1;        /* SAC: off */
1412
        if (get_bits1(&s->gb) != 0) {
1413
            s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
1414
        }   
1415
        
1416
        if (get_bits1(&s->gb) != 0)
1417
            return -1;        /* not PB frame */
1418

    
1419
        s->qscale = get_bits(&s->gb, 5);
1420
        skip_bits1(&s->gb);        /* Continuous Presence Multipoint mode: off */
1421
    } else {
1422
        int ufep;
1423
        
1424
        /* H.263v2 */
1425
        s->h263_plus = 1;
1426
        ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
1427
        
1428
        if (ufep == 1) {
1429
            /* OPPTYPE */       
1430
            format = get_bits(&s->gb, 3);
1431
            skip_bits(&s->gb,1); /* Custom PCF */
1432
            s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
1433
            skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
1434
            if (get_bits1(&s->gb) != 0) {
1435
                s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
1436
            }
1437
            if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */
1438
                s->h263_aic = 1;
1439
            }
1440
            skip_bits(&s->gb, 7);
1441
            skip_bits(&s->gb, 3); /* Reserved */
1442
        } else if (ufep != 0)
1443
            return -1;
1444
            
1445
        /* MPPTYPE */
1446
        s->pict_type = get_bits(&s->gb, 3) + 1;
1447
        if (s->pict_type != I_TYPE &&
1448
            s->pict_type != P_TYPE)
1449
            return -1;
1450
        skip_bits(&s->gb, 2);
1451
        s->no_rounding = get_bits1(&s->gb);
1452
        //fprintf(stderr, "\nRTYPE: %d", s->no_rounding);
1453
        skip_bits(&s->gb, 4);
1454
        
1455
        /* Get the picture dimensions */
1456
        if (ufep) {
1457
            if (format == 6) {
1458
                /* Custom Picture Format (CPFMT) */
1459
                skip_bits(&s->gb, 4); /* aspect ratio */
1460
                width = (get_bits(&s->gb, 9) + 1) * 4;
1461
                skip_bits1(&s->gb);
1462
                height = get_bits(&s->gb, 9) * 4;
1463
#ifdef DEBUG 
1464
                fprintf(stderr,"\nH.263+ Custom picture: %dx%d\n",width,height);
1465
#endif            
1466
            }
1467
            else {
1468
                width = h263_format[format][0];
1469
                height = h263_format[format][1];
1470
            }
1471
            if ((width == 0) || (height == 0))
1472
                return -1;
1473
            s->width = width;
1474
            s->height = height;
1475
            if (s->umvplus_dec) {
1476
                skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1477
            }
1478
        }
1479
            
1480
        s->qscale = get_bits(&s->gb, 5);
1481
    }
1482
    /* PEI */
1483
    while (get_bits1(&s->gb) != 0) {
1484
        skip_bits(&s->gb, 8);
1485
    }
1486
    s->f_code = 1;
1487
    return 0;
1488
}
1489

    
1490
/* decode mpeg4 VOP header */
1491
int mpeg4_decode_picture_header(MpegEncContext * s)
1492
{
1493
    int time_incr, startcode, state, v;
1494

    
1495
 redo:
1496
    /* search next start code */
1497
    align_get_bits(&s->gb);
1498
    state = 0xff;
1499
    for(;;) {
1500
        v = get_bits(&s->gb, 8);
1501
        if (state == 0x000001) {
1502
            state = ((state << 8) | v) & 0xffffff;
1503
            startcode = state;
1504
            break;
1505
        }
1506
        state = ((state << 8) | v) & 0xffffff;
1507
        /* XXX: really detect end of frame */
1508
        if (state == 0)
1509
            return -1;
1510
    }
1511

    
1512
    if (startcode == 0x120) {
1513
        int time_increment_resolution, width, height, vo_ver_id;
1514

    
1515
        /* vol header */
1516
        skip_bits(&s->gb, 1); /* random access */
1517
        skip_bits(&s->gb, 8); /* vo_type */
1518
        if (get_bits1(&s->gb) != 0) { /* is_ol_id */
1519
            vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */
1520
            skip_bits(&s->gb, 3); /* vo_priority */
1521
        } else {
1522
            vo_ver_id = 1;
1523
        }
1524
        
1525
        skip_bits(&s->gb, 4); /* aspect_ratio_info */
1526
        skip_bits1(&s->gb); /* vol control parameter */
1527
        s->shape = get_bits(&s->gb, 2); /* vol shape */
1528
        skip_bits1(&s->gb);   /* marker */
1529
        
1530
        time_increment_resolution = get_bits(&s->gb, 16);
1531
        s->time_increment_bits = av_log2(time_increment_resolution - 1) + 1;
1532
        if (s->time_increment_bits < 1)
1533
            s->time_increment_bits = 1;
1534
        skip_bits1(&s->gb);   /* marker */
1535

    
1536
        if (get_bits1(&s->gb) != 0) {   /* fixed_vop_rate  */
1537
            skip_bits(&s->gb, s->time_increment_bits);
1538
        }
1539

    
1540
        if (s->shape != 2) {
1541
            if (s->shape == 0) {
1542
                skip_bits1(&s->gb);   /* marker */
1543
                width = get_bits(&s->gb, 13);
1544
                skip_bits1(&s->gb);   /* marker */
1545
                height = get_bits(&s->gb, 13);
1546
                skip_bits1(&s->gb);   /* marker */
1547
            }
1548
            
1549
            skip_bits1(&s->gb);   /* interlaced */
1550
            skip_bits1(&s->gb);   /* OBMC */
1551
            if (vo_ver_id == 1) {
1552
                s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */
1553
            } else {
1554
                s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */
1555
            }
1556
            if (get_bits1(&s->gb) == 1) {   /* not_8_bit */
1557
                s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */
1558
                skip_bits(&s->gb, 4); /* bits_per_pixel */
1559
            } else {
1560
                s->quant_precision = 5;
1561
            }
1562
            
1563
            skip_bits1(&s->gb);   /* vol_quant_type */
1564
            skip_bits1(&s->gb);   /* vol_quarter_pixel */
1565
            skip_bits1(&s->gb);   /* complexity_estimation_disabled */
1566
            skip_bits1(&s->gb);   /* resync_marker_disabled */
1567
            skip_bits1(&s->gb);   /* data_partioning_enabled */
1568
            if (get_bits1(&s->gb) != 0) { /* scalability */
1569
                printf("bad scalability!!!\n");
1570
                return -1;
1571
            }
1572
        }
1573
        goto redo;
1574
    } else if (startcode != 0x1b6) {
1575
        goto redo;
1576
    }
1577

    
1578
    s->pict_type = get_bits(&s->gb, 2) + 1;        /* pict type: I = 0 , P = 1 */
1579
    if (s->pict_type != I_TYPE &&
1580
        s->pict_type != P_TYPE)
1581
        return -1;
1582
    
1583
    /* XXX: parse time base */
1584
    time_incr = 0;
1585
    while (get_bits1(&s->gb) != 0) 
1586
        time_incr++;
1587

    
1588
    skip_bits1(&s->gb);           /* marker */
1589
    skip_bits(&s->gb, s->time_increment_bits);
1590
    skip_bits1(&s->gb);           /* marker */
1591
    /* vop coded */
1592
    if (get_bits1(&s->gb) != 1)
1593
        goto redo;
1594
    
1595
    if (s->shape != 2 && s->pict_type == P_TYPE) {
1596
        /* rounding type for motion estimation */
1597
        s->no_rounding = get_bits1(&s->gb);
1598
    } else {
1599
        s->no_rounding = 0;
1600
    }
1601
    
1602
     if (s->shape != 0) {
1603
         if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {
1604
             int width, height, hor_spat_ref, ver_spat_ref;
1605
 
1606
             width = get_bits(&s->gb, 13);
1607
             skip_bits1(&s->gb);   /* marker */
1608
             height = get_bits(&s->gb, 13);
1609
             skip_bits1(&s->gb);   /* marker */
1610
             hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */
1611
             skip_bits1(&s->gb);   /* marker */
1612
             ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */
1613
         }
1614
         skip_bits1(&s->gb); /* change_CR_disable */
1615
 
1616
         if (get_bits1(&s->gb) != 0) {
1617
             skip_bits(&s->gb, 8); /* constant_alpha_value */
1618
         }
1619
     }
1620

    
1621
     if (s->shape != 2) {
1622
         skip_bits(&s->gb, 3); /* intra dc VLC threshold */
1623
  
1624
         /* note: we do not use quant_precision to avoid problem if no
1625
            MPEG4 vol header as it is found on some old opendivx
1626
            movies */
1627
         s->qscale = get_bits(&s->gb, 5);
1628
  
1629
         if (s->pict_type != I_TYPE) {
1630
             s->f_code = get_bits(&s->gb, 3);        /* fcode_for */
1631
         }
1632
         if (s->shape && (s->pict_type != I_TYPE)) {
1633
             skip_bits1(&s->gb); // vop shape coding type
1634
         }
1635
     }
1636
     return 0;
1637
}
1638

    
1639
/* don't understand why they choose a different header ! */
1640
int intel_h263_decode_picture_header(MpegEncContext *s)
1641
{
1642
    int format;
1643

    
1644
    /* picture header */
1645
    if (get_bits(&s->gb, 22) != 0x20)
1646
        return -1;
1647
    skip_bits(&s->gb, 8); /* picture timestamp */
1648

    
1649
    if (get_bits1(&s->gb) != 1)
1650
        return -1;        /* marker */
1651
    if (get_bits1(&s->gb) != 0)
1652
        return -1;        /* h263 id */
1653
    skip_bits1(&s->gb);        /* split screen off */
1654
    skip_bits1(&s->gb);        /* camera  off */
1655
    skip_bits1(&s->gb);        /* freeze picture release off */
1656

    
1657
    format = get_bits(&s->gb, 3);
1658
    if (format != 7)
1659
        return -1;
1660

    
1661
    s->h263_plus = 0;
1662

    
1663
    s->pict_type = I_TYPE + get_bits1(&s->gb);
1664
    
1665
    s->unrestricted_mv = get_bits1(&s->gb); 
1666
    s->h263_long_vectors = s->unrestricted_mv;
1667

    
1668
    if (get_bits1(&s->gb) != 0)
1669
        return -1;        /* SAC: off */
1670
    if (get_bits1(&s->gb) != 0)
1671
        return -1;        /* advanced prediction mode: off */
1672
    if (get_bits1(&s->gb) != 0)
1673
        return -1;        /* not PB frame */
1674

    
1675
    /* skip unknown header garbage */
1676
    skip_bits(&s->gb, 41);
1677

    
1678
    s->qscale = get_bits(&s->gb, 5);
1679
    skip_bits1(&s->gb);        /* Continuous Presence Multipoint mode: off */
1680

    
1681
    /* PEI */
1682
    while (get_bits1(&s->gb) != 0) {
1683
        skip_bits(&s->gb, 8);
1684
    }
1685
    s->f_code = 1;
1686
    return 0;
1687
}
1688