Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263.c @ 4278e7a6

History | View | Annotate | Download (78.9 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
//rounded divison & shift
29
#define RDIV(a,b) ((a) > 0 ? ((a)+((b)>>1))/(b) : ((a)-((b)>>1))/(b))
30
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
31
#define ABS(a) (((a)>=0)?(a):(-(a)))
32

    
33
static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
34
                              int n);
35
static void h263_encode_motion(MpegEncContext * s, int val);
36
static void h263p_encode_umotion(MpegEncContext * s, int val);
37
static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
38
                               int n, int dc, UINT8 *scan_table);
39
static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
40
static int h263p_decode_umotion(MpegEncContext * s, int pred);
41
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
42
                             int n, int coded);
43
static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
44
                              int n, int coded);
45
static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr);
46
static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
47
                              int dir);
48

    
49
extern UINT32 inverse[256];
50

    
51
int h263_get_picture_format(int width, int height)
52
{
53
    int format;
54

    
55
    if (width == 128 && height == 96)
56
        format = 1;
57
    else if (width == 176 && height == 144)
58
        format = 2;
59
    else if (width == 352 && height == 288)
60
        format = 3;
61
    else if (width == 704 && height == 576)
62
        format = 4;
63
    else if (width == 1408 && height == 1152)
64
        format = 5;
65
    else
66
        format = 7;
67
    return format;
68
}
69

    
70
void h263_encode_picture_header(MpegEncContext * s, int picture_number)
71
{
72
    int format;
73

    
74
    align_put_bits(&s->pb);
75

    
76
    /* Update the pointer to last GOB */
77
    s->ptr_lastgob = pbBufPtr(&s->pb);
78
    s->gob_number = 0;
79

    
80
    put_bits(&s->pb, 22, 0x20); /* PSC */
81
    put_bits(&s->pb, 8, (((INT64)s->picture_number * 30 * FRAME_RATE_BASE) / 
82
                         s->frame_rate) & 0xff);
83

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

    
155
    put_bits(&s->pb, 1, 0);        /* no PEI */
156
}
157

    
158
int h263_encode_gob_header(MpegEncContext * s, int mb_line)
159
{
160
    int pdif=0;
161
    
162
    /* Check to see if we need to put a new GBSC */
163
    /* for RTP packetization                    */
164
    if (s->rtp_mode) {
165
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
166
        if (pdif >= s->rtp_payload_size) {
167
            /* Bad luck, packet must be cut before */
168
            align_put_bits(&s->pb);
169
            flush_put_bits(&s->pb);
170
            /* Call the RTP callback to send the last GOB */
171
            if (s->rtp_callback) {
172
                pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
173
                s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
174
            }
175
            s->ptr_lastgob = pbBufPtr(&s->pb);
176
            put_bits(&s->pb, 17, 1); /* GBSC */
177
            s->gob_number = mb_line / s->gob_index;
178
            put_bits(&s->pb, 5, s->gob_number); /* GN */
179
            put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
180
            put_bits(&s->pb, 5, s->qscale); /* GQUANT */
181
            //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
182
            return pdif;
183
       } else if (pdif + s->mb_line_avgsize >= s->rtp_payload_size) {
184
           /* Cut the packet before we can't */
185
           align_put_bits(&s->pb);
186
           flush_put_bits(&s->pb);
187
           /* Call the RTP callback to send the last GOB */
188
           if (s->rtp_callback) {
189
               pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
190
               s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
191
           }
192
           s->ptr_lastgob = pbBufPtr(&s->pb);
193
           put_bits(&s->pb, 17, 1); /* GBSC */
194
           s->gob_number = mb_line / s->gob_index;
195
           put_bits(&s->pb, 5, s->gob_number); /* GN */
196
           put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
197
           put_bits(&s->pb, 5, s->qscale); /* GQUANT */
198
           //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
199
           return pdif;
200
       }
201
   }
202
   return 0;
203
}
204

    
205
static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6])
206
{
207
    int score0=0, score1=0;
208
    int i, n;
209

    
210
    for(n=0; n<6; n++){
211
        INT16 *ac_val, *ac_val1;
212

    
213
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
214
        ac_val1= ac_val;
215
        if(dir[n]){
216
            ac_val-= s->block_wrap[n]*16;
217
            for(i=1; i<8; i++){
218
                const int level= block[n][block_permute_op(i   )];
219
                score0+= ABS(level);
220
                score1+= ABS(level - ac_val[i+8]);
221
                ac_val1[i  ]=    block[n][block_permute_op(i<<3)];
222
                ac_val1[i+8]= level;
223
            }
224
        }else{
225
            ac_val-= 16;
226
            for(i=1; i<8; i++){
227
                const int level= block[n][block_permute_op(i<<3)];
228
                score0+= ABS(level);
229
                score1+= ABS(level - ac_val[i]);
230
                ac_val1[i  ]= level;
231
                ac_val1[i+8]=    block[n][block_permute_op(i   )];
232
            }
233
        }
234
    }
235

    
236
    return score0 > score1 ? 1 : 0;    
237
}
238

    
239
void mpeg4_encode_mb(MpegEncContext * s,
240
                    DCTELEM block[6][64],
241
                    int motion_x, int motion_y)
242
{
243
    int cbpc, cbpy, i, cbp, pred_x, pred_y;
244
    
245
    //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
246
    if (!s->mb_intra) {
247
        /* compute cbp */
248
        cbp = 0;
249
        for (i = 0; i < 6; i++) {
250
        if (s->block_last_index[i] >= 0)
251
            cbp |= 1 << (5 - i);
252
        }
253
        if ((cbp | motion_x | motion_y) == 0) {
254
            /* skip macroblock */
255
            put_bits(&s->pb, 1, 1);
256
            return;
257
        }
258
        put_bits(&s->pb, 1, 0);        /* mb coded */
259
        cbpc = cbp & 3;
260
        put_bits(&s->pb,
261
                inter_MCBPC_bits[cbpc],
262
                inter_MCBPC_code[cbpc]);
263
        cbpy = cbp >> 2;
264
        cbpy ^= 0xf;
265
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
266

    
267
        /* motion vectors: 16x16 mode only now */
268
        h263_pred_motion(s, 0, &pred_x, &pred_y);
269
      
270
        h263_encode_motion(s, motion_x - pred_x);
271
        h263_encode_motion(s, motion_y - pred_y);
272

    
273
        /* encode each block */
274
        for (i = 0; i < 6; i++) {
275
            mpeg4_encode_block(s, block[i], i, 0, zigzag_direct);
276
        }
277
    } else {
278
        int dc_diff[6];   //dc values with the dc prediction subtracted 
279
        int dir[6];  //prediction direction
280
        int zigzag_last_index[6];
281
        UINT8 *scan_table[6];
282

    
283
        for(i=0; i<6; i++){
284
            const int level= block[i][0];
285
            UINT16 *dc_ptr;
286

    
287
            dc_diff[i]= level - mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
288
            if (i < 4) {
289
                *dc_ptr = level * s->y_dc_scale;
290
            } else {
291
                *dc_ptr = level * s->c_dc_scale;
292
            }
293
        }
294

    
295
        s->ac_pred= decide_ac_pred(s, block, dir);
296

    
297
        if(s->ac_pred){
298
            for(i=0; i<6; i++){
299
                UINT8 *st;
300
                int last_index;
301

    
302
                mpeg4_inv_pred_ac(s, block[i], i, dir[i]);
303
                if (dir[i]==0) st = ff_alternate_vertical_scan; /* left */
304
                else           st = ff_alternate_horizontal_scan; /* top */
305

    
306
                for(last_index=63; last_index>=0; last_index--) //FIXME optimize
307
                    if(block[i][st[last_index]]) break;
308
                zigzag_last_index[i]= s->block_last_index[i];
309
                s->block_last_index[i]= last_index;
310
                scan_table[i]= st;
311
            }
312
        }else{
313
            for(i=0; i<6; i++)
314
                scan_table[i]= zigzag_direct;
315
        }
316

    
317
        /* compute cbp */
318
        cbp = 0;
319
        for (i = 0; i < 6; i++) {
320
            if (s->block_last_index[i] >= 1)
321
                cbp |= 1 << (5 - i);
322
        }
323

    
324
        cbpc = cbp & 3;
325
        if (s->pict_type == I_TYPE) {
326
            put_bits(&s->pb,
327
                intra_MCBPC_bits[cbpc],
328
                intra_MCBPC_code[cbpc]);
329
        } else {
330
            put_bits(&s->pb, 1, 0);        /* mb coded */
331
            put_bits(&s->pb,
332
                inter_MCBPC_bits[cbpc + 4],
333
                inter_MCBPC_code[cbpc + 4]);
334
        }
335
        put_bits(&s->pb, 1, s->ac_pred);
336
        cbpy = cbp >> 2;
337
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
338

    
339
        /* encode each block */
340
        for (i = 0; i < 6; i++) {
341
            mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i]);
342
        }
343

    
344
        /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
345
        if(s->ac_pred){
346
            for(i=0; i<6; i++){
347
                int j;    
348
                INT16 *ac_val;
349

    
350
                ac_val = s->ac_val[0][0] + s->block_index[i] * 16;
351

    
352
                if(dir[i]){
353
                    for(j=1; j<8; j++) 
354
                        block[i][block_permute_op(j   )]= ac_val[j+8];
355
                }else{
356
                    for(j=1; j<8; j++) 
357
                        block[i][block_permute_op(j<<3)]= ac_val[j  ];
358
                }
359
                s->block_last_index[i]= zigzag_last_index[i];
360
            }
361
        }
362
    }
363
}
364

    
365
void h263_encode_mb(MpegEncContext * s,
366
                    DCTELEM block[6][64],
367
                    int motion_x, int motion_y)
368
{
369
    int cbpc, cbpy, i, cbp, pred_x, pred_y;
370
   
371
    //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
372
   if (!s->mb_intra) {
373
           /* compute cbp */
374
           cbp = 0;
375
           for (i = 0; i < 6; i++) {
376
              if (s->block_last_index[i] >= 0)
377
                   cbp |= 1 << (5 - i);
378
           }
379
           if ((cbp | motion_x | motion_y) == 0) {
380
              /* skip macroblock */
381
              put_bits(&s->pb, 1, 1);
382
              return;
383
           }
384
           put_bits(&s->pb, 1, 0);        /* mb coded */
385
           cbpc = cbp & 3;
386
           put_bits(&s->pb,
387
                inter_MCBPC_bits[cbpc],
388
                inter_MCBPC_code[cbpc]);
389
           cbpy = cbp >> 2;
390
           cbpy ^= 0xf;
391
           put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
392

    
393
           /* motion vectors: 16x16 mode only now */
394
      h263_pred_motion(s, 0, &pred_x, &pred_y);
395
      
396
      if (!s->umvplus) {  
397
         h263_encode_motion(s, motion_x - pred_x);
398
         h263_encode_motion(s, motion_y - pred_y);
399
      }
400
      else {
401
         h263p_encode_umotion(s, motion_x - pred_x);
402
         h263p_encode_umotion(s, motion_y - pred_y);
403
         if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
404
            /* To prevent Start Code emulation */
405
            put_bits(&s->pb,1,1);
406
      }
407
   } else {
408
        /* compute cbp */
409
        cbp = 0;
410
        for (i = 0; i < 6; i++) {
411
            if (s->block_last_index[i] >= 1)
412
                cbp |= 1 << (5 - i);
413
        }
414

    
415
        cbpc = cbp & 3;
416
        if (s->pict_type == I_TYPE) {
417
            put_bits(&s->pb,
418
                     intra_MCBPC_bits[cbpc],
419
                     intra_MCBPC_code[cbpc]);
420
        } else {
421
            put_bits(&s->pb, 1, 0);        /* mb coded */
422
            put_bits(&s->pb,
423
                     inter_MCBPC_bits[cbpc + 4],
424
                     inter_MCBPC_code[cbpc + 4]);
425
        }
426
        if (s->h263_pred) {
427
            /* XXX: currently, we do not try to use ac prediction */
428
            put_bits(&s->pb, 1, 0);        /* no ac prediction */
429
        }
430
        cbpy = cbp >> 2;
431
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
432
    }
433

    
434
    /* encode each block */
435
    if (s->h263_pred) {
436
        for (i = 0; i < 6; i++) {
437
//            mpeg4_encode_block(s, block[i], i);
438
        }
439
    } else {
440
        for (i = 0; i < 6; i++) {
441
            h263_encode_block(s, block[i], i);
442
        }
443
    }
444
}
445

    
446
void h263_pred_acdc(MpegEncContext * s, INT16 *block, int n)
447
{
448
    int x, y, wrap, a, c, pred_dc, scale, i;
449
    INT16 *dc_val, *ac_val, *ac_val1;
450

    
451
    /* find prediction */
452
    if (n < 4) {
453
        x = 2 * s->mb_x + 1 + (n & 1);
454
        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
455
        wrap = s->mb_width * 2 + 2;
456
        dc_val = s->dc_val[0];
457
        ac_val = s->ac_val[0][0];
458
        scale = s->y_dc_scale;
459
    } else {
460
        x = s->mb_x + 1;
461
        y = s->mb_y + 1;
462
        wrap = s->mb_width + 2;
463
        dc_val = s->dc_val[n - 4 + 1];
464
        ac_val = s->ac_val[n - 4 + 1][0];
465
        scale = s->c_dc_scale;
466
    }
467
    
468
    ac_val += ((y) * wrap + (x)) * 16;
469
    ac_val1 = ac_val;
470
    
471
    /* B C
472
     * A X 
473
     */
474
    a = dc_val[(x - 1) + (y) * wrap];
475
    c = dc_val[(x) + (y - 1) * wrap];
476
    
477
    pred_dc = 1024;
478
    if (s->ac_pred) {
479
        if (s->h263_aic_dir) {
480
            /* left prediction */
481
            if (a != 1024) {
482
                ac_val -= 16;
483
                for(i=1;i<8;i++) {
484
                    block[block_permute_op(i*8)] += ac_val[i];
485
                }
486
                pred_dc = a;
487
            }
488
        } else {
489
            /* top prediction */
490
            if (c != 1024) {
491
                ac_val -= 16 * wrap;
492
                for(i=1;i<8;i++) {
493
                    block[block_permute_op(i)] += ac_val[i + 8];
494
                }
495
                pred_dc = c;
496
            }
497
        }
498
    } else {
499
        /* just DC prediction */
500
        if (a != 1024 && c != 1024)
501
            pred_dc = (a + c) >> 1;
502
        else if (a != 1024)
503
            pred_dc = a;
504
        else
505
            pred_dc = c;
506
    }
507
    
508
    /* we assume pred is positive */
509
    block[0]=block[0]*scale + pred_dc;
510
    
511
    if (block[0] < 0)
512
        block[0] = 0;
513
    else if (!(block[0] & 1))
514
        block[0]++;
515
    
516
    /* Update AC/DC tables */
517
    dc_val[(x) + (y) * wrap] = block[0];
518
    
519
    /* left copy */
520
    for(i=1;i<8;i++)
521
        ac_val1[i] = block[block_permute_op(i * 8)];
522
    /* top copy */
523
    for(i=1;i<8;i++)
524
        ac_val1[8 + i] = block[block_permute_op(i)];
525
}
526

    
527

    
528
static inline int mid_pred(int a, int b, int c)
529
{
530
    int vmin, vmax;
531
    vmax = vmin = a;
532
    if (b < vmin)
533
        vmin = b;
534
    else
535
        vmax = b;
536

    
537
    if (c < vmin)
538
        vmin = c;
539
    else if (c > vmax)
540
        vmax = c;
541

    
542
    return a + b + c - vmin - vmax;
543
}
544

    
545
INT16 *h263_pred_motion(MpegEncContext * s, int block, 
546
                        int *px, int *py)
547
{
548
    int xy, wrap;
549
    INT16 *A, *B, *C, *mot_val;
550
    static const int off[4]= {2, 1, 1, -1};
551

    
552
    wrap = s->block_wrap[0];
553
    xy = s->block_index[block];
554

    
555
    mot_val = s->motion_val[xy];
556

    
557
    /* special case for first line */
558
    if (s->mb_y == 0 || s->first_slice_line || s->first_gob_line) {
559
        A = s->motion_val[xy - 1];
560
        *px = A[0];
561
        *py = A[1];
562
    } else {
563
        A = s->motion_val[xy - 1];
564
        B = s->motion_val[xy - wrap];
565
        C = s->motion_val[xy + off[block] - wrap];
566
        *px = mid_pred(A[0], B[0], C[0]);
567
        *py = mid_pred(A[1], B[1], C[1]);
568
    }
569
    return mot_val;
570
}
571

    
572
static void h263_encode_motion(MpegEncContext * s, int val)
573
{
574
    int range, l, m, bit_size, sign, code, bits;
575

    
576
    if (val == 0) {
577
        /* zero vector */
578
        code = 0;
579
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
580
    } else {
581
        bit_size = s->f_code - 1;
582
        range = 1 << bit_size;
583
        /* modulo encoding */
584
        l = range * 32;
585
        m = 2 * l;
586
        if (val < -l) {
587
            val += m;
588
        } else if (val >= l) {
589
            val -= m;
590
        }
591

    
592
        if (val >= 0) {
593
            val--;
594
            code = (val >> bit_size) + 1;
595
            bits = val & (range - 1);
596
            sign = 0;
597
        } else {
598
            val = -val;
599
            val--;
600
            code = (val >> bit_size) + 1;
601
            bits = val & (range - 1);
602
            sign = 1;
603
        }
604

    
605
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); 
606
        if (bit_size > 0) {
607
            put_bits(&s->pb, bit_size, bits);
608
        }
609
    }
610
}
611

    
612
/* Encode MV differences on H.263+ with Unrestricted MV mode */
613
static void h263p_encode_umotion(MpegEncContext * s, int val)
614
{
615
    short sval = 0; 
616
    short i = 0;
617
    short n_bits = 0;
618
    short temp_val;
619
    int code = 0;
620
    int tcode;
621
    
622
    if ( val == 0)
623
        put_bits(&s->pb, 1, 1);
624
    else if (val == 1)
625
        put_bits(&s->pb, 3, 0);
626
    else if (val == -1)
627
        put_bits(&s->pb, 3, 2);
628
    else {
629
        
630
        sval = ((val < 0) ? (short)(-val):(short)val);
631
        temp_val = sval;
632
        
633
        while (temp_val != 0) {
634
            temp_val = temp_val >> 1;
635
            n_bits++;
636
        }
637
        
638
        i = n_bits - 1;
639
        while (i > 0) {
640
            tcode = (sval & (1 << (i-1))) >> (i-1);
641
            tcode = (tcode << 1) | 1;
642
            code = (code << 2) | tcode;
643
            i--;
644
        }
645
        code = ((code << 1) | (val < 0)) << 1;
646
        put_bits(&s->pb, (2*n_bits)+1, code);
647
        //printf("\nVal = %d\tCode = %d", sval, code);
648
    }
649
}
650

    
651
void h263_encode_init_vlc(MpegEncContext *s)
652
{
653
    static int done = 0;
654

    
655
    if (!done) {
656
        done = 1;
657
        init_rl(&rl_inter);
658
        init_rl(&rl_intra);
659
    }
660
}
661

    
662
static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
663
{
664
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
665
    int code;
666
    RLTable *rl = &rl_inter;
667

    
668
    if (s->mb_intra) {
669
        /* DC coef */
670
            level = block[0];
671
        /* 255 cannot be represented, so we clamp */
672
        if (level > 254) {
673
            level = 254;
674
            block[0] = 254;
675
        }
676
        /* 0 cannot be represented also */
677
        else if (!level) {
678
            level = 1;
679
            block[0] = 1;
680
        }
681
            if (level == 128)
682
                put_bits(&s->pb, 8, 0xff);
683
            else
684
                put_bits(&s->pb, 8, level & 0xff);
685
            i = 1;
686
    } else {
687
            i = 0;
688
    }
689

    
690
    /* AC coefs */
691
    last_index = s->block_last_index[n];
692
    last_non_zero = i - 1;
693
    for (; i <= last_index; i++) {
694
        j = zigzag_direct[i];
695
        level = block[j];
696
        if (level) {
697
            run = i - last_non_zero - 1;
698
            last = (i == last_index);
699
            sign = 0;
700
            slevel = level;
701
            if (level < 0) {
702
                sign = 1;
703
                level = -level;
704
            }
705
            code = get_rl_index(rl, last, run, level);
706
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
707
            if (code == rl->n) {
708
                put_bits(&s->pb, 1, last);
709
                put_bits(&s->pb, 6, run);
710
                put_bits(&s->pb, 8, slevel & 0xff);
711
            } else {
712
                put_bits(&s->pb, 1, sign);
713
            }
714
            last_non_zero = i;
715
        }
716
    }
717
}
718

    
719
/***************************************************/
720

    
721
static void mpeg4_stuffing(PutBitContext * pbc)
722
{
723
    int length;
724
    put_bits(pbc, 1, 0);
725
    length= (-get_bit_count(pbc))&7;
726
    put_bits(pbc, length, (1<<length)-1);
727
}
728

    
729
static void put_string(PutBitContext * pbc, char *s)
730
{
731
    while(*s){
732
        put_bits(pbc, 8, *s);
733
        s++;
734
    }
735
    put_bits(pbc, 8, 0);
736
}
737

    
738
static void mpeg4_encode_vol_header(MpegEncContext * s)
739
{
740
    int vo_ver_id=1; //must be 2 if we want GMC or q-pel
741

    
742
    if(get_bit_count(&s->pb)!=0) mpeg4_stuffing(&s->pb);
743
    put_bits(&s->pb, 16, 0);
744
    put_bits(&s->pb, 16, 0x100);        /* video obj */
745
    put_bits(&s->pb, 16, 0);
746
    put_bits(&s->pb, 16, 0x120);        /* video obj layer */
747

    
748
    put_bits(&s->pb, 1, 0);                /* random access vol */
749
    put_bits(&s->pb, 8, 1);                /* video obj type indication= simple obj */
750
    put_bits(&s->pb, 1, 1);                /* is obj layer id= yes */
751
      put_bits(&s->pb, 4, vo_ver_id);        /* is obj layer ver id */
752
      put_bits(&s->pb, 3, 1);                /* is obj layer priority */
753
    put_bits(&s->pb, 4, 1);                /* aspect ratio info= sqare pixel */ //FIXME real aspect
754
    put_bits(&s->pb, 1, 0);                /* vol control parameters= no */
755
    put_bits(&s->pb, 2, RECT_SHAPE);        /* vol shape= rectangle */
756
    put_bits(&s->pb, 1, 1);                /* marker bit */
757
    put_bits(&s->pb, 16, s->time_increment_resolution=30000);
758
    s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
759
    if (s->time_increment_bits < 1)
760
        s->time_increment_bits = 1;
761
    put_bits(&s->pb, 1, 1);                /* marker bit */
762
    put_bits(&s->pb, 1, 0);                /* fixed vop rate=no */
763
    put_bits(&s->pb, 1, 1);                /* marker bit */
764
    put_bits(&s->pb, 13, s->width);        /* vol width */
765
    put_bits(&s->pb, 1, 1);                /* marker bit */
766
    put_bits(&s->pb, 13, s->height);        /* vol height */
767
    put_bits(&s->pb, 1, 1);                /* marker bit */
768
    put_bits(&s->pb, 1, 0);                /* interlace */
769
    put_bits(&s->pb, 1, 1);                /* obmc disable */
770
    if (vo_ver_id == 1) {
771
        put_bits(&s->pb, 1, s->vol_sprite_usage=0);                /* sprite enable */
772
    }else{ /* vo_ver_id == 2 */
773
        put_bits(&s->pb, 2, s->vol_sprite_usage=0);                /* sprite enable */
774
    }
775
    put_bits(&s->pb, 1, 0);                /* not 8 bit */
776
    put_bits(&s->pb, 1, 0);                /* quant type= h263 style*/
777
    if (vo_ver_id != 1)
778
        put_bits(&s->pb, 1, s->quarter_sample=0);
779
    put_bits(&s->pb, 1, 1);                /* complexity estimation disable */
780
    put_bits(&s->pb, 1, 1);                /* resync marker disable */
781
    put_bits(&s->pb, 1, 0);                /* data partitioned */
782
    if (vo_ver_id != 1){
783
        put_bits(&s->pb, 1, 0);                /* newpred */
784
        put_bits(&s->pb, 1, 0);                /* reduced res vop */
785
    }
786
    put_bits(&s->pb, 1, 0);                /* scalability */
787

    
788
    mpeg4_stuffing(&s->pb);
789
    put_bits(&s->pb, 16, 0);
790
    put_bits(&s->pb, 16, 0x1B2);        /* user_data */
791
    put_string(&s->pb, "ffmpeg"); //FIXME append some version ...
792

    
793
    s->no_rounding = 0;
794
}
795

    
796
/* write mpeg4 VOP header */
797
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
798
{
799
    if(s->pict_type==I_TYPE) mpeg4_encode_vol_header(s);
800

    
801
    if(get_bit_count(&s->pb)!=0) mpeg4_stuffing(&s->pb);
802
    put_bits(&s->pb, 16, 0);                /* vop header */
803
    put_bits(&s->pb, 16, 0x1B6);        /* vop header */
804
    put_bits(&s->pb, 2, s->pict_type - 1);        /* pict type: I = 0 , P = 1 */
805
    /* XXX: time base + 1 not always correct */
806
    put_bits(&s->pb, 1, 1);
807
    put_bits(&s->pb, 1, 0);
808

    
809
    put_bits(&s->pb, 1, 1);        /* marker */
810
    put_bits(&s->pb, s->time_increment_bits, 1);        /* XXX: correct time increment */
811
    put_bits(&s->pb, 1, 1);        /* marker */
812
    put_bits(&s->pb, 1, 1);        /* vop coded */
813
    if (    s->pict_type == P_TYPE 
814
        || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
815
        s->no_rounding ^= 1;
816
        put_bits(&s->pb, 1, s->no_rounding);        /* rounding type */
817
    }
818
    put_bits(&s->pb, 3, 0);        /* intra dc VLC threshold */
819
    //FIXME sprite stuff
820

    
821
    put_bits(&s->pb, 5, s->qscale);
822

    
823
    if (s->pict_type != I_TYPE)
824
        put_bits(&s->pb, 3, s->f_code);        /* fcode_for */
825
    if (s->pict_type == B_TYPE)
826
        put_bits(&s->pb, 3, s->b_code);        /* fcode_back */
827
    //    printf("****frame %d\n", picture_number);
828
}
829

    
830
void h263_dc_scale(MpegEncContext * s)
831
{
832
#if 1
833
    const static UINT8 y_tab[32]={
834
    //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
835
        0, 8, 8, 8, 8,10,12,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,34,36,38,40,42,44,46
836
    };
837
    const static UINT8 c_tab[32]={
838
    //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
839
        0, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,20,21,22,23,24,25
840
    };
841
    s->y_dc_scale = y_tab[s->qscale];
842
    s->c_dc_scale = c_tab[s->qscale];
843
#else
844
    int quant;
845
    quant = s->qscale;
846
    /* luminance */
847
    if (quant < 5)
848
        s->y_dc_scale = 8;
849
    else if (quant > 4 && quant < 9)
850
        s->y_dc_scale = (2 * quant);
851
    else if (quant > 8 && quant < 25)
852
        s->y_dc_scale = (quant + 8);
853
    else
854
        s->y_dc_scale = (2 * quant - 16);
855
    /* chrominance */
856
    if (quant < 5)
857
        s->c_dc_scale = 8;
858
    else if (quant > 4 && quant < 25)
859
        s->c_dc_scale = ((quant + 13) / 2);
860
    else
861
        s->c_dc_scale = (quant - 6);
862
#endif
863
}
864

    
865
static inline int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
866
{
867
    int a, b, c, wrap, pred, scale;
868
    UINT16 *dc_val;
869
    int dummy;
870

    
871
    /* find prediction */
872
    if (n < 4) {
873
        scale = s->y_dc_scale;
874
    } else {
875
        scale = s->c_dc_scale;
876
    }
877
    wrap= s->block_wrap[n];
878
    dc_val = s->dc_val[0] + s->block_index[n];
879

    
880
    /* B C
881
     * A X 
882
     */
883
    a = dc_val[ - 1];
884
    b = dc_val[ - 1 - wrap];
885
    c = dc_val[ - wrap];
886

    
887
    if (abs(a - b) < abs(b - c)) {
888
        pred = c;
889
        *dir_ptr = 1; /* top */
890
    } else {
891
        pred = a;
892
        *dir_ptr = 0; /* left */
893
    }
894
    /* we assume pred is positive */
895
#ifdef ARCH_X86
896
        asm volatile (
897
                "xorl %%edx, %%edx        \n\t"
898
                "mul %%ecx                \n\t"
899
                : "=d" (pred), "=a"(dummy)
900
                : "a" (pred + (scale >> 1)), "c" (inverse[scale])
901
        );
902
#else
903
    pred = (pred + (scale >> 1)) / scale;
904
#endif
905

    
906
    /* prepare address for prediction update */
907
    *dc_val_ptr = &dc_val[0];
908

    
909
    return pred;
910
}
911

    
912
void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
913
                   int dir)
914
{
915
    int i;
916
    INT16 *ac_val, *ac_val1;
917

    
918
    /* find prediction */
919
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
920
    ac_val1 = ac_val;
921
    if (s->ac_pred) {
922
        if (dir == 0) {
923
            /* left prediction */
924
            ac_val -= 16;
925
            for(i=1;i<8;i++) {
926
                block[block_permute_op(i*8)] += ac_val[i];
927
            }
928
        } else {
929
            /* top prediction */
930
            ac_val -= 16 * s->block_wrap[n];
931
            for(i=1;i<8;i++) {
932
                block[block_permute_op(i)] += ac_val[i + 8];
933
            }
934
        }
935
    }
936
    /* left copy */
937
    for(i=1;i<8;i++)
938
        ac_val1[i] = block[block_permute_op(i * 8)];
939
    /* top copy */
940
    for(i=1;i<8;i++)
941
        ac_val1[8 + i] = block[block_permute_op(i)];
942
}
943

    
944
static void mpeg4_inv_pred_ac(MpegEncContext * s, INT16 *block, int n,
945
                              int dir)
946
{
947
    int i;
948
    INT16 *ac_val;
949

    
950
    /* find prediction */
951
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
952
 
953
    if (dir == 0) {
954
        /* left prediction */
955
        ac_val -= 16;
956
        for(i=1;i<8;i++) {
957
            block[block_permute_op(i*8)] -= ac_val[i];
958
        }
959
    } else {
960
        /* top prediction */
961
        ac_val -= 16 * s->block_wrap[n];
962
        for(i=1;i<8;i++) {
963
            block[block_permute_op(i)] -= ac_val[i + 8];
964
        }
965
    }
966
}
967

    
968

    
969
static inline void mpeg4_encode_dc(MpegEncContext * s, int level, int n)
970
{
971
    int size, v;
972
    /* find number of bits */
973
    size = 0;
974
    v = abs(level);
975
    while (v) {
976
        v >>= 1;
977
        size++;
978
    }
979

    
980
    if (n < 4) {
981
        /* luminance */
982
        put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
983
    } else {
984
        /* chrominance */
985
        put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
986
    }
987

    
988
    /* encode remaining bits */
989
    if (size > 0) {
990
        if (level < 0)
991
            level = (-level) ^ ((1 << size) - 1);
992
        put_bits(&s->pb, size, level);
993
        if (size > 8)
994
            put_bits(&s->pb, 1, 1);
995
    }
996
}
997

    
998
static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc, UINT8 *scan_table)
999
{
1000
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
1001
    int code;
1002
    const RLTable *rl;
1003

    
1004
    if (s->mb_intra) {
1005
        /* mpeg4 based DC predictor */
1006
        mpeg4_encode_dc(s, intra_dc, n);
1007
        i = 1;
1008
        rl = &rl_intra;
1009
    } else {
1010
        i = 0;
1011
        rl = &rl_inter;
1012
    }
1013

    
1014
    /* AC coefs */
1015
    last_index = s->block_last_index[n];
1016
    last_non_zero = i - 1;
1017
    for (; i <= last_index; i++) {
1018
        j = scan_table[i];
1019
        level = block[j];
1020
        if (level) {
1021
            run = i - last_non_zero - 1;
1022
            last = (i == last_index);
1023
            sign = 0;
1024
            slevel = level;
1025
            if (level < 0) {
1026
                sign = 1;
1027
                level = -level;
1028
            }
1029
            code = get_rl_index(rl, last, run, level);
1030
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1031
            if (code == rl->n) {
1032
                int level1, run1;
1033
                level1 = level - rl->max_level[last][run];
1034
                if (level1 < 1) 
1035
                    goto esc2;
1036
                code = get_rl_index(rl, last, run, level1);
1037
                if (code == rl->n) {
1038
                esc2:
1039
                    put_bits(&s->pb, 1, 1);
1040
                    if (level > MAX_LEVEL)
1041
                        goto esc3;
1042
                    run1 = run - rl->max_run[last][level] - 1;
1043
                    if (run1 < 0)
1044
                        goto esc3;
1045
                    code = get_rl_index(rl, last, run1, level);
1046
                    if (code == rl->n) {
1047
                    esc3:
1048
                        /* third escape */
1049
                        put_bits(&s->pb, 1, 1);
1050
                        put_bits(&s->pb, 1, last);
1051
                        put_bits(&s->pb, 6, run);
1052
                        put_bits(&s->pb, 1, 1);
1053
                        put_bits(&s->pb, 12, slevel & 0xfff);
1054
                        put_bits(&s->pb, 1, 1);
1055
                    } else {
1056
                        /* second escape */
1057
                        put_bits(&s->pb, 1, 0);
1058
                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1059
                        put_bits(&s->pb, 1, sign);
1060
                    }
1061
                } else {
1062
                    /* first escape */
1063
                    put_bits(&s->pb, 1, 0);
1064
                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
1065
                    put_bits(&s->pb, 1, sign);
1066
                }
1067
            } else {
1068
                put_bits(&s->pb, 1, sign);
1069
            }
1070
            last_non_zero = i;
1071
        }
1072
    }
1073
}
1074

    
1075

    
1076

    
1077
/***********************************************/
1078
/* decoding */
1079

    
1080
static VLC intra_MCBPC_vlc;
1081
static VLC inter_MCBPC_vlc;
1082
static VLC cbpy_vlc;
1083
static VLC mv_vlc;
1084
static VLC dc_lum, dc_chrom;
1085
static VLC sprite_trajectory;
1086
static VLC mb_type_b_vlc;
1087

    
1088
void init_rl(RLTable *rl)
1089
{
1090
    INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
1091
    UINT8 index_run[MAX_RUN+1];
1092
    int last, run, level, start, end, i;
1093

    
1094
    /* compute max_level[], max_run[] and index_run[] */
1095
    for(last=0;last<2;last++) {
1096
        if (last == 0) {
1097
            start = 0;
1098
            end = rl->last;
1099
        } else {
1100
            start = rl->last;
1101
            end = rl->n;
1102
        }
1103

    
1104
        memset(max_level, 0, MAX_RUN + 1);
1105
        memset(max_run, 0, MAX_LEVEL + 1);
1106
        memset(index_run, rl->n, MAX_RUN + 1);
1107
        for(i=start;i<end;i++) {
1108
            run = rl->table_run[i];
1109
            level = rl->table_level[i];
1110
            if (index_run[run] == rl->n)
1111
                index_run[run] = i;
1112
            if (level > max_level[run])
1113
                max_level[run] = level;
1114
            if (run > max_run[level])
1115
                max_run[level] = run;
1116
        }
1117
        rl->max_level[last] = malloc(MAX_RUN + 1);
1118
        memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1119
        rl->max_run[last] = malloc(MAX_LEVEL + 1);
1120
        memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1121
        rl->index_run[last] = malloc(MAX_RUN + 1);
1122
        memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1123
    }
1124
}
1125

    
1126
void init_vlc_rl(RLTable *rl)
1127
{
1128
    init_vlc(&rl->vlc, 9, rl->n + 1, 
1129
             &rl->table_vlc[0][1], 4, 2,
1130
             &rl->table_vlc[0][0], 4, 2);
1131
}
1132

    
1133
/* init vlcs */
1134

    
1135
/* XXX: find a better solution to handle static init */
1136
void h263_decode_init_vlc(MpegEncContext *s)
1137
{
1138
    static int done = 0;
1139

    
1140
    if (!done) {
1141
        done = 1;
1142

    
1143
        init_vlc(&intra_MCBPC_vlc, 6, 8, 
1144
                 intra_MCBPC_bits, 1, 1,
1145
                 intra_MCBPC_code, 1, 1);
1146
        init_vlc(&inter_MCBPC_vlc, 9, 25, 
1147
                 inter_MCBPC_bits, 1, 1,
1148
                 inter_MCBPC_code, 1, 1);
1149
        init_vlc(&cbpy_vlc, 6, 16,
1150
                 &cbpy_tab[0][1], 2, 1,
1151
                 &cbpy_tab[0][0], 2, 1);
1152
        init_vlc(&mv_vlc, 9, 33,
1153
                 &mvtab[0][1], 2, 1,
1154
                 &mvtab[0][0], 2, 1);
1155
        init_rl(&rl_inter);
1156
        init_rl(&rl_intra);
1157
        init_rl(&rl_intra_aic);
1158
        init_vlc_rl(&rl_inter);
1159
        init_vlc_rl(&rl_intra);
1160
        init_vlc_rl(&rl_intra_aic);
1161
        init_vlc(&dc_lum, 9, 13,
1162
                 &DCtab_lum[0][1], 2, 1,
1163
                 &DCtab_lum[0][0], 2, 1);
1164
        init_vlc(&dc_chrom, 9, 13,
1165
                 &DCtab_chrom[0][1], 2, 1,
1166
                 &DCtab_chrom[0][0], 2, 1);
1167
        init_vlc(&sprite_trajectory, 9, 15,
1168
                 &sprite_trajectory_tab[0][1], 4, 2,
1169
                 &sprite_trajectory_tab[0][0], 4, 2);
1170
        init_vlc(&mb_type_b_vlc, 4, 4,
1171
                 &mb_type_b_tab[0][1], 2, 1,
1172
                 &mb_type_b_tab[0][0], 2, 1);
1173
    }
1174
}
1175

    
1176
int h263_decode_gob_header(MpegEncContext *s)
1177
{
1178
    unsigned int val, gfid;
1179
    
1180
    /* Check for GOB Start Code */
1181
    val = show_bits(&s->gb, 16);
1182
    if (val == 0) {
1183
        /* We have a GBSC probably with GSTUFF */
1184
        skip_bits(&s->gb, 16); /* Drop the zeros */
1185
        while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */
1186
#ifdef DEBUG
1187
        fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x);
1188
#endif
1189
        s->gob_number = get_bits(&s->gb, 5); /* GN */
1190
        gfid = get_bits(&s->gb, 2); /* GFID */
1191
        s->qscale = get_bits(&s->gb, 5); /* GQUANT */
1192
#ifdef DEBUG
1193
        fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale);
1194
#endif
1195
        return 1;
1196
    }
1197
    return 0;
1198
            
1199
}
1200

    
1201
int h263_decode_mb(MpegEncContext *s,
1202
                   DCTELEM block[6][64])
1203
{
1204
    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1205
    INT16 *mot_val;
1206
    static INT8 quant_tab[4] = { -1, -2, 1, 2 };
1207

    
1208
    if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
1209
        if (get_bits1(&s->gb)) {
1210
            /* skip mb */
1211
            s->mb_intra = 0;
1212
            for(i=0;i<6;i++)
1213
                s->block_last_index[i] = -1;
1214
            s->mv_dir = MV_DIR_FORWARD;
1215
            s->mv_type = MV_TYPE_16X16;
1216
            if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
1217
                const int a= s->sprite_warping_accuracy;
1218
//                int l = (1 << (s->f_code - 1)) * 32;
1219

    
1220
                s->mcsel=1;
1221
                s->mv[0][0][0] = RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
1222
                s->mv[0][0][1] = RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
1223
/*                if (s->mv[0][0][0] < -l) s->mv[0][0][0]= -l;
1224
                else if (s->mv[0][0][0] >= l) s->mv[0][0][0]= l-1;
1225
                if (s->mv[0][0][1] < -l) s->mv[0][0][1]= -l;
1226
                else if (s->mv[0][0][1] >= l) s->mv[0][0][1]= l-1;*/
1227

    
1228
                s->mb_skiped = 0;
1229
            }else{
1230
                s->mcsel=0;
1231
                s->mv[0][0][0] = 0;
1232
                s->mv[0][0][1] = 0;
1233
                s->mb_skiped = 1;
1234
            }
1235
            return 0;
1236
        }
1237
        cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc);
1238
        //fprintf(stderr, "\tCBPC: %d", cbpc);
1239
        if (cbpc < 0)
1240
            return -1;
1241
        if (cbpc > 20)
1242
            cbpc+=3;
1243
        else if (cbpc == 20)
1244
            fprintf(stderr, "Stuffing !");
1245
        
1246
        dquant = cbpc & 8;
1247
        s->mb_intra = ((cbpc & 4) != 0);
1248
        if (s->mb_intra) goto intra;
1249
        
1250
        if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
1251
            s->mcsel= get_bits1(&s->gb);
1252
        else s->mcsel= 0;
1253
        cbpy = get_vlc(&s->gb, &cbpy_vlc);
1254
        cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
1255
        if (dquant) {
1256
            s->qscale += quant_tab[get_bits(&s->gb, 2)];
1257
            if (s->qscale < 1)
1258
                s->qscale = 1;
1259
            else if (s->qscale > 31)
1260
                s->qscale = 31;
1261
        }
1262
        s->mv_dir = MV_DIR_FORWARD;
1263
        if ((cbpc & 16) == 0) {
1264
            /* 16x16 motion prediction */
1265
            s->mv_type = MV_TYPE_16X16;
1266
            h263_pred_motion(s, 0, &pred_x, &pred_y);
1267
            if (s->umvplus_dec)
1268
               mx = h263p_decode_umotion(s, pred_x);
1269
            else if(!s->mcsel)
1270
               mx = h263_decode_motion(s, pred_x, s->f_code);
1271
            else {
1272
               const int a= s->sprite_warping_accuracy;
1273
//        int l = (1 << (s->f_code - 1)) * 32;
1274
               mx= RSHIFT(s->sprite_offset[0][0], a-s->quarter_sample);
1275
//        if (mx < -l) mx= -l;
1276
//        else if (mx >= l) mx= l-1;
1277
            }
1278
            if (mx >= 0xffff)
1279
                return -1;
1280
            
1281
            if (s->umvplus_dec)
1282
               my = h263p_decode_umotion(s, pred_y);
1283
            else if(!s->mcsel)
1284
               my = h263_decode_motion(s, pred_y, s->f_code);
1285
            else{
1286
               const int a= s->sprite_warping_accuracy;
1287
//       int l = (1 << (s->f_code - 1)) * 32;
1288
               my= RSHIFT(s->sprite_offset[0][1], a-s->quarter_sample);
1289
//       if (my < -l) my= -l;
1290
//       else if (my >= l) my= l-1;
1291
            }
1292
            if (my >= 0xffff)
1293
                return -1;
1294
            s->mv[0][0][0] = mx;
1295
            s->mv[0][0][1] = my;
1296
            /*fprintf(stderr, "\n MB %d", (s->mb_y * s->mb_width) + s->mb_x);
1297
            fprintf(stderr, "\n\tmvx: %d\t\tpredx: %d", mx, pred_x);
1298
            fprintf(stderr, "\n\tmvy: %d\t\tpredy: %d", my, pred_y);*/
1299
            if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
1300
               skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
1301
                           
1302
        } else {
1303
            s->mv_type = MV_TYPE_8X8;
1304
            for(i=0;i<4;i++) {
1305
                mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
1306
                if (s->umvplus_dec)
1307
                  mx = h263p_decode_umotion(s, pred_x);
1308
                else
1309
                  mx = h263_decode_motion(s, pred_x, s->f_code);
1310
                if (mx >= 0xffff)
1311
                    return -1;
1312
                
1313
                if (s->umvplus_dec)
1314
                  my = h263p_decode_umotion(s, pred_y);
1315
                else    
1316
                  my = h263_decode_motion(s, pred_y, s->f_code);
1317
                if (my >= 0xffff)
1318
                    return -1;
1319
                s->mv[0][i][0] = mx;
1320
                s->mv[0][i][1] = my;
1321
                if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
1322
                  skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
1323
                mot_val[0] = mx;
1324
                mot_val[1] = my;
1325
            }
1326
        }
1327
    } else if(s->pict_type==B_TYPE) {
1328
        int modb1; // first bit of modb
1329
        int modb2; // second bit of modb
1330
        int mb_type;
1331
        int time_pp;
1332
        int time_pb;
1333
        int xy;
1334

    
1335
        s->mb_intra = 0; //B-frames never contain intra blocks
1336
        s->mcsel=0;      //     ...               true gmc blocks
1337

    
1338
        if(s->mb_x==0){
1339
            s->last_mv[0][0][0]= 
1340
            s->last_mv[0][0][1]= 
1341
            s->last_mv[1][0][0]= 
1342
            s->last_mv[1][0][1]= 0;
1343
        }
1344

    
1345
        /* if we skipped it in the future P Frame than skip it now too */
1346
        s->mb_skiped= s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; // Note, skiptab=0 if last was GMC
1347

    
1348
        if(s->mb_skiped){
1349
                /* skip mb */
1350
            for(i=0;i<6;i++)
1351
                s->block_last_index[i] = -1;
1352

    
1353
            s->mv_dir = MV_DIR_FORWARD;
1354
            s->mv_type = MV_TYPE_16X16;
1355
            s->mv[0][0][0] = 0;
1356
            s->mv[0][0][1] = 0;
1357
            s->mv[1][0][0] = 0;
1358
            s->mv[1][0][1] = 0;
1359
            s->last_mv[0][0][0]=
1360
            s->last_mv[0][0][1]= 
1361
            s->last_mv[1][0][0]= 
1362
            s->last_mv[1][0][1]= 0;
1363
            s->mb_skiped = 1;
1364
            return 0;
1365
        }
1366

    
1367
        modb1= get_bits1(&s->gb);
1368
        if(modb1==0){
1369
            modb2= get_bits1(&s->gb);
1370
            mb_type= get_vlc(&s->gb, &mb_type_b_vlc);
1371
            if(modb2==0) cbp= get_bits(&s->gb, 6);
1372
            else cbp=0;
1373
            if (mb_type && cbp) {
1374
                if(get_bits1(&s->gb)){
1375
                    s->qscale +=get_bits1(&s->gb)*4 - 2;
1376
                    if (s->qscale < 1)
1377
                        s->qscale = 1;
1378
                    else if (s->qscale > 31)
1379
                        s->qscale = 31;
1380
                }
1381
            }
1382
        }else{
1383
            mb_type=4; //like 0 but no vectors coded
1384
            cbp=0;
1385
        }
1386
        s->mv_type = MV_TYPE_16X16; // we'll switch to 8x8 only if the last P frame had 8x8 for this MB and mb_type=0 here
1387
        mx=my=0; //for case 4, we could put this to the mb_type=4 but than gcc compains about uninitalized mx/my
1388
        switch(mb_type)
1389
        {
1390
        case 0: 
1391
            mx = h263_decode_motion(s, 0, 1);
1392
            my = h263_decode_motion(s, 0, 1);
1393
        case 4: 
1394
            s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1395
            xy= s->block_index[0];
1396
            time_pp= s->last_non_b_time[0] - s->last_non_b_time[1];
1397
            time_pb= s->time - s->last_non_b_time[1];
1398
//if(time_pp>3000 )printf("%d %d  ", time_pp, time_pb);
1399
            //FIXME 4MV
1400
            //FIXME avoid divides
1401
            s->mv[0][0][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
1402
            s->mv[0][0][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
1403
            s->mv[1][0][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0]
1404
                                : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp + mx;
1405
            s->mv[1][0][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1] 
1406
                                : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my;
1407
/*            s->mv[0][0][0] = 
1408
            s->mv[0][0][1] = 
1409
            s->mv[1][0][0] = 
1410
            s->mv[1][0][1] = 1000;*/
1411
            break;
1412
        case 1: 
1413
            s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1414
            mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1415
            my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1416
            s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
1417
            s->last_mv[0][0][1]= s->mv[0][0][1] = my;
1418

    
1419
            mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1420
            my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1421
            s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
1422
            s->last_mv[1][0][1]= s->mv[1][0][1] = my;
1423
            break;
1424
        case 2: 
1425
            s->mv_dir = MV_DIR_BACKWARD;
1426
            mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1427
            my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1428
            s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
1429
            s->last_mv[1][0][1]= s->mv[1][0][1] = my;
1430
            break;
1431
        case 3:
1432
            s->mv_dir = MV_DIR_FORWARD;
1433
            mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1434
            my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1435
            s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
1436
            s->last_mv[0][0][1]= s->mv[0][0][1] = my;
1437
            break;
1438
        default: return -1;
1439
        }
1440
    } else { /* I-Frame */
1441
        cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
1442
        if (cbpc < 0)
1443
            return -1;
1444
        dquant = cbpc & 4;
1445
        s->mb_intra = 1;
1446
intra:
1447
        s->ac_pred = 0;
1448
        if (s->h263_pred || s->h263_aic) {
1449
            s->ac_pred = get_bits1(&s->gb);
1450
            if (s->ac_pred && s->h263_aic)
1451
                s->h263_aic_dir = get_bits1(&s->gb);
1452
        }
1453
        if (s->h263_aic) {
1454
            s->y_dc_scale = 2 * s->qscale;
1455
            s->c_dc_scale = 2 * s->qscale;
1456
        }
1457
        cbpy = get_vlc(&s->gb, &cbpy_vlc);
1458
        cbp = (cbpc & 3) | (cbpy << 2);
1459
        if (dquant) {
1460
            s->qscale += quant_tab[get_bits(&s->gb, 2)];
1461
            if (s->qscale < 1)
1462
                s->qscale = 1;
1463
            else if (s->qscale > 31)
1464
                s->qscale = 31;
1465
        }
1466
    }
1467

    
1468
    /* decode each block */
1469
    if (s->h263_pred) {
1470
        for (i = 0; i < 6; i++) {
1471
            if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1472
                return -1;
1473
        }
1474
    } else {
1475
        for (i = 0; i < 6; i++) {
1476
            if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
1477
                return -1;
1478
        }
1479
    }
1480
    return 0;
1481
}
1482

    
1483
static int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
1484
{
1485
    int code, val, sign, shift, l, m;
1486

    
1487
    code = get_vlc(&s->gb, &mv_vlc);
1488
    if (code < 0)
1489
        return 0xffff;
1490

    
1491
    if (code == 0)
1492
        return pred;
1493
    sign = get_bits1(&s->gb);
1494
    shift = f_code - 1;
1495
    val = (code - 1) << shift;
1496
    if (shift > 0)
1497
        val |= get_bits(&s->gb, shift);
1498
    val++;
1499
    if (sign)
1500
        val = -val;
1501
    val += pred;
1502
    
1503
    /* modulo decoding */
1504
    if (!s->h263_long_vectors) {
1505
        l = (1 << (f_code - 1)) * 32;
1506
        m = 2 * l;
1507
        if (val < -l) {
1508
            val += m;
1509
        } else if (val >= l) {
1510
            val -= m;
1511
        }
1512
    } else {
1513
        /* horrible h263 long vector mode */
1514
        if (pred < -31 && val < -63)
1515
            val += 64;
1516
        if (pred > 32 && val > 63)
1517
            val -= 64;
1518
        
1519
    }
1520
    return val;
1521
}
1522

    
1523
/* Decodes RVLC of H.263+ UMV */
1524
static int h263p_decode_umotion(MpegEncContext * s, int pred)
1525
{
1526
   int code = 0, sign;
1527
   
1528
   if (get_bits1(&s->gb)) /* Motion difference = 0 */
1529
      return pred;
1530
   
1531
   code = 2 + get_bits1(&s->gb);
1532
   
1533
   while (get_bits1(&s->gb))
1534
   {
1535
      code <<= 1;
1536
      code += get_bits1(&s->gb);
1537
   }
1538
   sign = code & 1;
1539
   code >>= 1;
1540
   
1541
   code = (sign) ? (pred - code) : (pred + code);
1542
#ifdef DEBUG
1543
   fprintf(stderr,"H.263+ UMV Motion = %d\n", code);
1544
#endif
1545
   return code;   
1546

    
1547
}
1548

    
1549
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
1550
                             int n, int coded)
1551
{
1552
    int code, level, i, j, last, run;
1553
    RLTable *rl = &rl_inter;
1554
    const UINT8 *scan_table;
1555

    
1556
    scan_table = zigzag_direct;
1557
    if (s->h263_aic && s->mb_intra) {
1558
        rl = &rl_intra_aic;
1559
        i = 0;
1560
        if (s->ac_pred) {
1561
            if (s->h263_aic_dir) 
1562
                scan_table = ff_alternate_vertical_scan; /* left */
1563
            else
1564
                scan_table = ff_alternate_horizontal_scan; /* top */
1565
        }
1566
    } else if (s->mb_intra) {
1567
        /* DC coef */
1568
        if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
1569
            int component, diff;
1570
            component = (n <= 3 ? 0 : n - 4 + 1);
1571
            level = s->last_dc[component];
1572
            if (s->rv10_first_dc_coded[component]) {
1573
                diff = rv_decode_dc(s, n);
1574
                if (diff == 0xffff)
1575
                    return -1;
1576
                level += diff;
1577
                level = level & 0xff; /* handle wrap round */
1578
                s->last_dc[component] = level;
1579
            } else {
1580
                s->rv10_first_dc_coded[component] = 1;
1581
            }
1582
        } else {
1583
            level = get_bits(&s->gb, 8);
1584
            if (level == 255)
1585
                level = 128;
1586
        }
1587
        block[0] = level;
1588
        i = 1;
1589
    } else {
1590
        i = 0;
1591
    }
1592
    if (!coded) {
1593
        if (s->mb_intra && s->h263_aic)
1594
            goto not_coded;
1595
        s->block_last_index[n] = i - 1;
1596
        return 0;
1597
    }
1598

    
1599
    for(;;) {
1600
        code = get_vlc(&s->gb, &rl->vlc);
1601
        if (code < 0)
1602
            return -1;
1603
        if (code == rl->n) {
1604
            /* escape */
1605
            last = get_bits1(&s->gb);
1606
            run = get_bits(&s->gb, 6);
1607
            level = (INT8)get_bits(&s->gb, 8);
1608
            if (s->h263_rv10 && level == -128) {
1609
                /* XXX: should patch encoder too */
1610
                level = get_bits(&s->gb, 12);
1611
                level = (level << 20) >> 20;
1612
            }
1613
        } else {
1614
            run = rl->table_run[code];
1615
            level = rl->table_level[code];
1616
            last = code >= rl->last;
1617
            if (get_bits1(&s->gb))
1618
                level = -level;
1619
        }
1620
        i += run;
1621
        if (i >= 64)
1622
            return -1;
1623
        j = scan_table[i];
1624
        block[j] = level;
1625
        if (last)
1626
            break;
1627
        i++;
1628
    }
1629
not_coded:    
1630
    if (s->mb_intra && s->h263_aic) {
1631
        h263_pred_acdc(s, block, n);
1632
        i = 63;
1633
    }
1634
    s->block_last_index[n] = i;
1635
    return 0;
1636
}
1637

    
1638
static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
1639
{
1640
    int level, pred, code;
1641
    UINT16 *dc_val;
1642

    
1643
    if (n < 4) 
1644
        code = get_vlc(&s->gb, &dc_lum);
1645
    else 
1646
        code = get_vlc(&s->gb, &dc_chrom);
1647
    if (code < 0)
1648
        return -1;
1649
    if (code == 0) {
1650
        level = 0;
1651
    } else {
1652
        level = get_bits(&s->gb, code);
1653
        if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
1654
            level = - (level ^ ((1 << code) - 1));
1655
        if (code > 8)
1656
            skip_bits1(&s->gb); /* marker */
1657
    }
1658

    
1659
    pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
1660
    level += pred;
1661
    if (level < 0)
1662
        level = 0;
1663
    if (n < 4) {
1664
        *dc_val = level * s->y_dc_scale;
1665
    } else {
1666
        *dc_val = level * s->c_dc_scale;
1667
    }
1668
    return level;
1669
}
1670

    
1671
static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
1672
                              int n, int coded)
1673
{
1674
    int code, level, i, j, last, run;
1675
    int dc_pred_dir;
1676
    RLTable *rl;
1677
    const UINT8 *scan_table;
1678

    
1679
    if (s->mb_intra) {
1680
        /* DC coef */
1681
        level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1682
        if (level < 0)
1683
            return -1;
1684
        block[0] = level;
1685
        i = 1;
1686
        if (!coded) 
1687
            goto not_coded;
1688
        rl = &rl_intra;
1689
        if (s->ac_pred) {
1690
            if (dc_pred_dir == 0) 
1691
                scan_table = ff_alternate_vertical_scan; /* left */
1692
            else
1693
                scan_table = ff_alternate_horizontal_scan; /* top */
1694
        } else {
1695
            scan_table = zigzag_direct;
1696
        }
1697
    } else {
1698
        i = 0;
1699
        if (!coded) {
1700
            s->block_last_index[n] = i - 1;
1701
            return 0;
1702
        }
1703
        rl = &rl_inter;
1704
        scan_table = zigzag_direct;
1705
    }
1706

    
1707
    for(;;) {
1708
        code = get_vlc(&s->gb, &rl->vlc);
1709
        if (code < 0)
1710
            return -1;
1711
        if (code == rl->n) {
1712
            /* escape */
1713
            if (get_bits1(&s->gb) != 0) {
1714
                if (get_bits1(&s->gb) != 0) {
1715
                    /* third escape */
1716
                    last = get_bits1(&s->gb);
1717
                    run = get_bits(&s->gb, 6);
1718
                    get_bits1(&s->gb); /* marker */
1719
                    level = get_bits(&s->gb, 12);
1720
                    level = (level << 20) >> 20; /* sign extend */
1721
                    skip_bits1(&s->gb); /* marker */
1722
                } else {
1723
                    /* second escape */
1724
                    code = get_vlc(&s->gb, &rl->vlc);
1725
                    if (code < 0 || code >= rl->n)
1726
                        return -1;
1727
                    run = rl->table_run[code];
1728
                    level = rl->table_level[code];
1729
                    last = code >= rl->last;
1730
                    run += rl->max_run[last][level] + 1;
1731
                    if (get_bits1(&s->gb))
1732
                        level = -level;
1733
                }
1734
            } else {
1735
                /* first escape */
1736
                code = get_vlc(&s->gb, &rl->vlc);
1737
                if (code < 0 || code >= rl->n)
1738
                    return -1;
1739
                run = rl->table_run[code];
1740
                level = rl->table_level[code];
1741
                last = code >= rl->last;
1742
                level += rl->max_level[last][run];
1743
                if (get_bits1(&s->gb))
1744
                    level = -level;
1745
            }
1746
        } else {
1747
            run = rl->table_run[code];
1748
            level = rl->table_level[code];
1749
            last = code >= rl->last;
1750
            if (get_bits1(&s->gb))
1751
                level = -level;
1752
        }
1753
        i += run;
1754
        if (i >= 64)
1755
            return -1;
1756
        j = scan_table[i];
1757
        block[j] = level;
1758
        i++;
1759
        if (last)
1760
            break;
1761
    }
1762
 not_coded:
1763
    if (s->mb_intra) {
1764
        mpeg4_pred_ac(s, block, n, dc_pred_dir);
1765
        if (s->ac_pred) {
1766
            i = 64; /* XXX: not optimal */
1767
        }
1768
    }
1769
    s->block_last_index[n] = i - 1;
1770
    return 0;
1771
}
1772

    
1773
/* most is hardcoded. should extend to handle all h263 streams */
1774
int h263_decode_picture_header(MpegEncContext *s)
1775
{
1776
    int format, width, height;
1777

    
1778
    /* picture header */
1779
    if (get_bits(&s->gb, 22) != 0x20)
1780
        return -1;
1781
    s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
1782
    
1783
    if (get_bits1(&s->gb) != 1)
1784
        return -1;        /* marker */
1785
    if (get_bits1(&s->gb) != 0)
1786
        return -1;        /* h263 id */
1787
    skip_bits1(&s->gb);        /* split screen off */
1788
    skip_bits1(&s->gb);        /* camera  off */
1789
    skip_bits1(&s->gb);        /* freeze picture release off */
1790

    
1791
    /* Reset GOB number */
1792
    s->gob_number = 0;
1793
        
1794
    format = get_bits(&s->gb, 3);
1795

    
1796
    if (format != 7 && format != 6) {
1797
        s->h263_plus = 0;
1798
        /* H.263v1 */
1799
        width = h263_format[format][0];
1800
        height = h263_format[format][1];
1801
        if (!width)
1802
            return -1;
1803
        
1804
        s->width = width;
1805
        s->height = height;
1806
        s->pict_type = I_TYPE + get_bits1(&s->gb);
1807

    
1808
        s->unrestricted_mv = get_bits1(&s->gb); 
1809
        s->h263_long_vectors = s->unrestricted_mv;
1810

    
1811
        if (get_bits1(&s->gb) != 0)
1812
            return -1;        /* SAC: off */
1813
        if (get_bits1(&s->gb) != 0) {
1814
            s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
1815
        }   
1816
        
1817
        if (get_bits1(&s->gb) != 0)
1818
            return -1;        /* not PB frame */
1819

    
1820
        s->qscale = get_bits(&s->gb, 5);
1821
        skip_bits1(&s->gb);        /* Continuous Presence Multipoint mode: off */
1822
    } else {
1823
        int ufep;
1824
        
1825
        /* H.263v2 */
1826
        s->h263_plus = 1;
1827
        ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
1828
        
1829
        if (ufep == 1) {
1830
            /* OPPTYPE */       
1831
            format = get_bits(&s->gb, 3);
1832
            skip_bits(&s->gb,1); /* Custom PCF */
1833
            s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
1834
            skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
1835
            if (get_bits1(&s->gb) != 0) {
1836
                s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
1837
            }
1838
            if (get_bits1(&s->gb) != 0) { /* Advanced Intra Coding (AIC) */
1839
                s->h263_aic = 1;
1840
            }
1841
            skip_bits(&s->gb, 7);
1842
            skip_bits(&s->gb, 3); /* Reserved */
1843
        } else if (ufep != 0)
1844
            return -1;
1845
            
1846
        /* MPPTYPE */
1847
        s->pict_type = get_bits(&s->gb, 3) + 1;
1848
        if (s->pict_type != I_TYPE &&
1849
            s->pict_type != P_TYPE)
1850
            return -1;
1851
        skip_bits(&s->gb, 2);
1852
        s->no_rounding = get_bits1(&s->gb);
1853
        //fprintf(stderr, "\nRTYPE: %d", s->no_rounding);
1854
        skip_bits(&s->gb, 4);
1855
        
1856
        /* Get the picture dimensions */
1857
        if (ufep) {
1858
            if (format == 6) {
1859
                /* Custom Picture Format (CPFMT) */
1860
                skip_bits(&s->gb, 4); /* aspect ratio */
1861
                width = (get_bits(&s->gb, 9) + 1) * 4;
1862
                skip_bits1(&s->gb);
1863
                height = get_bits(&s->gb, 9) * 4;
1864
#ifdef DEBUG 
1865
                fprintf(stderr,"\nH.263+ Custom picture: %dx%d\n",width,height);
1866
#endif            
1867
            }
1868
            else {
1869
                width = h263_format[format][0];
1870
                height = h263_format[format][1];
1871
            }
1872
            if ((width == 0) || (height == 0))
1873
                return -1;
1874
            s->width = width;
1875
            s->height = height;
1876
            if (s->umvplus_dec) {
1877
                skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1878
            }
1879
        }
1880
            
1881
        s->qscale = get_bits(&s->gb, 5);
1882
    }
1883
    /* PEI */
1884
    while (get_bits1(&s->gb) != 0) {
1885
        skip_bits(&s->gb, 8);
1886
    }
1887
    s->f_code = 1;
1888
    return 0;
1889
}
1890

    
1891
static void mpeg4_decode_sprite_trajectory(MpegEncContext * s)
1892
{
1893
    int i;
1894
    int a= 2<<s->sprite_warping_accuracy;
1895
    int rho= 3-s->sprite_warping_accuracy;
1896
    int r=16/a;
1897
    const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
1898
    int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
1899
    int sprite_ref[4][2];
1900
    int virtual_ref[2][2];
1901
    int w2, h2;
1902
    int alpha=0, beta=0;
1903
    int w= s->width;
1904
    int h= s->height;
1905
//printf("SP %d\n", s->sprite_warping_accuracy);
1906
    for(i=0; i<s->num_sprite_warping_points; i++){
1907
        int length;
1908
        int x=0, y=0;
1909

    
1910
        length= get_vlc(&s->gb, &sprite_trajectory);
1911
        if(length){
1912
            x= get_bits(&s->gb, length);
1913
//printf("lx %d %d\n", length, x);
1914
            if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/
1915
                x = - (x ^ ((1 << length) - 1));
1916
        }
1917
        if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(&s->gb); /* marker bit */
1918
        
1919
        length= get_vlc(&s->gb, &sprite_trajectory);
1920
        if(length){
1921
            y=get_bits(&s->gb, length);
1922
//printf("ly %d %d\n", length, y);
1923
            if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/
1924
                y = - (y ^ ((1 << length) - 1));
1925
        }
1926
        skip_bits1(&s->gb); /* marker bit */
1927
//printf("%d %d %d %d\n", x, y, i, s->sprite_warping_accuracy);
1928
//if(i>0 && (x!=0 || y!=0)) printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
1929
//x=y=0;
1930
        d[i][0]= x;
1931
        d[i][1]= y;
1932
    }
1933

    
1934
    while((1<<alpha)<w) alpha++;
1935
    while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
1936
    w2= 1<<alpha;
1937
    h2= 1<<beta;
1938

    
1939
// Note, the 4th point isnt used for GMC
1940
    if(s->divx_version==500 && s->divx_build==413){
1941
        sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
1942
        sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
1943
        sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
1944
        sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
1945
        sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
1946
        sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
1947
    } else {
1948
        sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
1949
        sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
1950
        sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
1951
        sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
1952
        sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
1953
        sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
1954
    }
1955
/*    sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
1956
    sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
1957
    
1958
// this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
1959
// perhaps it should be reordered to be more readable ...
1960
// the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
1961
// so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
1962
    virtual_ref[0][0]= 16*(vop_ref[0][0] + w2) 
1963
        + RDIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w);
1964
    virtual_ref[0][1]= 16*vop_ref[0][1] 
1965
        + RDIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w);
1966
    virtual_ref[1][0]= 16*vop_ref[0][0] 
1967
        + RDIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h);
1968
    virtual_ref[1][1]= 16*(vop_ref[0][1] + h2) 
1969
        + RDIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h);
1970

    
1971
    switch(s->num_sprite_warping_points)
1972
    {
1973
        case 0:
1974
            s->sprite_offset[0][0]= 0;
1975
            s->sprite_offset[0][1]= 0;
1976
            s->sprite_offset[1][0]= 0;
1977
            s->sprite_offset[1][1]= 0;
1978
            s->sprite_delta[0][0][0]= a;
1979
            s->sprite_delta[0][0][1]= 0;
1980
            s->sprite_delta[0][1][0]= 0;
1981
            s->sprite_delta[0][1][1]= a;
1982
            s->sprite_delta[1][0][0]= a;
1983
            s->sprite_delta[1][0][1]= 0;
1984
            s->sprite_delta[1][1][0]= 0;
1985
            s->sprite_delta[1][1][1]= a;
1986
            s->sprite_shift[0][0]= 0;
1987
            s->sprite_shift[0][1]= 0;
1988
            s->sprite_shift[1][0]= 0;
1989
            s->sprite_shift[1][1]= 0;
1990
            break;
1991
        case 1: //GMC only
1992
            s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
1993
            s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
1994
            s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
1995
            s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
1996
            s->sprite_delta[0][0][0]= a;
1997
            s->sprite_delta[0][0][1]= 0;
1998
            s->sprite_delta[0][1][0]= 0;
1999
            s->sprite_delta[0][1][1]= a;
2000
            s->sprite_delta[1][0][0]= a;
2001
            s->sprite_delta[1][0][1]= 0;
2002
            s->sprite_delta[1][1][0]= 0;
2003
            s->sprite_delta[1][1][1]= a;
2004
            s->sprite_shift[0][0]= 0;
2005
            s->sprite_shift[0][1]= 0;
2006
            s->sprite_shift[1][0]= 0;
2007
            s->sprite_shift[1][1]= 0;
2008
            break;
2009
        case 2:
2010
        case 3: //FIXME
2011
            s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
2012
                                                  + ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
2013
                                                    +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1]));
2014
            s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
2015
                                                  + ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
2016
                                                    +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1]));
2017
            s->sprite_offset[1][0]= ((-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
2018
                                 +( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
2019
                                 +2*w2*r*sprite_ref[0][0] - 16*w2);
2020
            s->sprite_offset[1][1]= ((-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1) 
2021
                                 +(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
2022
                                 +2*w2*r*sprite_ref[0][1] - 16*w2);
2023
            s->sprite_delta[0][0][0]=   (-r*sprite_ref[0][0] + virtual_ref[0][0]);
2024
            s->sprite_delta[0][0][1]=   ( r*sprite_ref[0][1] - virtual_ref[0][1]);
2025
            s->sprite_delta[0][1][0]=   (-r*sprite_ref[0][1] + virtual_ref[0][1]);
2026
            s->sprite_delta[0][1][1]=   (-r*sprite_ref[0][0] + virtual_ref[0][0]);
2027
            s->sprite_delta[1][0][0]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]);
2028
            s->sprite_delta[1][0][1]= 4*( r*sprite_ref[0][1] - virtual_ref[0][1]);
2029
            s->sprite_delta[1][1][0]= 4*(-r*sprite_ref[0][1] + virtual_ref[0][1]);
2030
            s->sprite_delta[1][1][1]= 4*(-r*sprite_ref[0][0] + virtual_ref[0][0]);
2031
            s->sprite_shift[0][0]= alpha+rho;
2032
            s->sprite_shift[0][1]= alpha+rho;
2033
            s->sprite_shift[1][0]= alpha+rho+2;
2034
            s->sprite_shift[1][1]= alpha+rho+2;
2035
            break;
2036
//        case 3:
2037
            break;
2038
    }
2039
/*printf("%d %d\n", s->sprite_delta[0][0][0], a<<s->sprite_shift[0][0]);
2040
printf("%d %d\n", s->sprite_delta[0][0][1], 0);
2041
printf("%d %d\n", s->sprite_delta[0][1][0], 0);
2042
printf("%d %d\n", s->sprite_delta[0][1][1], a<<s->sprite_shift[0][1]);
2043
printf("%d %d\n", s->sprite_delta[1][0][0], a<<s->sprite_shift[1][0]);
2044
printf("%d %d\n", s->sprite_delta[1][0][1], 0);
2045
printf("%d %d\n", s->sprite_delta[1][1][0], 0);
2046
printf("%d %d\n", s->sprite_delta[1][1][1], a<<s->sprite_shift[1][1]);*/
2047
    /* try to simplify the situation */ 
2048
    if(   s->sprite_delta[0][0][0] == a<<s->sprite_shift[0][0]
2049
       && s->sprite_delta[0][0][1] == 0
2050
       && s->sprite_delta[0][1][0] == 0
2051
       && s->sprite_delta[0][1][1] == a<<s->sprite_shift[0][1]
2052
       && s->sprite_delta[1][0][0] == a<<s->sprite_shift[1][0]
2053
       && s->sprite_delta[1][0][1] == 0
2054
       && s->sprite_delta[1][1][0] == 0
2055
       && s->sprite_delta[1][1][1] == a<<s->sprite_shift[1][1])
2056
    {
2057
        s->sprite_offset[0][0]>>=s->sprite_shift[0][0];
2058
        s->sprite_offset[0][1]>>=s->sprite_shift[0][1];
2059
        s->sprite_offset[1][0]>>=s->sprite_shift[1][0];
2060
        s->sprite_offset[1][1]>>=s->sprite_shift[1][1];
2061
        s->sprite_delta[0][0][0]= a;
2062
        s->sprite_delta[0][0][1]= 0;
2063
        s->sprite_delta[0][1][0]= 0;
2064
        s->sprite_delta[0][1][1]= a;
2065
        s->sprite_delta[1][0][0]= a;
2066
        s->sprite_delta[1][0][1]= 0;
2067
        s->sprite_delta[1][1][0]= 0;
2068
        s->sprite_delta[1][1][1]= a;
2069
        s->sprite_shift[0][0]= 0;
2070
        s->sprite_shift[0][1]= 0;
2071
        s->sprite_shift[1][0]= 0;
2072
        s->sprite_shift[1][1]= 0;
2073
        s->real_sprite_warping_points=1;
2074
    }
2075
    else
2076
        s->real_sprite_warping_points= s->num_sprite_warping_points;
2077

    
2078
}
2079

    
2080
/* decode mpeg4 VOP header */
2081
int mpeg4_decode_picture_header(MpegEncContext * s)
2082
{
2083
    int time_incr, startcode, state, v;
2084

    
2085
 redo:
2086
    /* search next start code */
2087
    align_get_bits(&s->gb);
2088
    state = 0xff;
2089
    for(;;) {
2090
        v = get_bits(&s->gb, 8);
2091
        if (state == 0x000001) {
2092
            state = ((state << 8) | v) & 0xffffff;
2093
            startcode = state;
2094
            break;
2095
        }
2096
        state = ((state << 8) | v) & 0xffffff;
2097
        /* XXX: really detect end of frame */
2098
        if (state == 0){
2099
            printf("illegal zero code found\n");
2100
            return -1;
2101
        }
2102
    }
2103
//printf("startcode %X %d\n", startcode, get_bits_count(&s->gb));
2104
    if (startcode == 0x120) { // Video Object Layer
2105
        int width, height, vo_ver_id;
2106

    
2107
        /* vol header */
2108
        skip_bits(&s->gb, 1); /* random access */
2109
        skip_bits(&s->gb, 8); /* vo_type */
2110
        if (get_bits1(&s->gb) != 0) { /* is_ol_id */
2111
            vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */
2112
            skip_bits(&s->gb, 3); /* vo_priority */
2113
        } else {
2114
            vo_ver_id = 1;
2115
        }
2116
        
2117
        s->aspect_ratio_info= get_bits(&s->gb, 4);
2118
        if(s->aspect_ratio_info == EXTENDET_PAR){
2119
            skip_bits(&s->gb, 8); //par_width
2120
            skip_bits(&s->gb, 8); // par_height
2121
        }
2122
        if(get_bits1(&s->gb)){ /* vol control parameter */
2123
            printf("vol control parameter not supported\n");
2124
            return -1;   
2125
        }
2126
        s->shape = get_bits(&s->gb, 2); /* vol shape */
2127
        if(s->shape != RECT_SHAPE) printf("only rectangular vol supported\n");
2128
        if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
2129
            printf("Gray shape not supported\n");
2130
            skip_bits(&s->gb, 4);  //video_object_layer_shape_extension
2131
        }
2132

    
2133
        skip_bits1(&s->gb);   /* marker */
2134
        
2135
        s->time_increment_resolution = get_bits(&s->gb, 16);
2136
        s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
2137
        if (s->time_increment_bits < 1)
2138
            s->time_increment_bits = 1;
2139
        skip_bits1(&s->gb);   /* marker */
2140

    
2141
        if (get_bits1(&s->gb) != 0) {   /* fixed_vop_rate  */
2142
            skip_bits(&s->gb, s->time_increment_bits);
2143
        }
2144

    
2145
        if (s->shape != BIN_ONLY_SHAPE) {
2146
            if (s->shape == RECT_SHAPE) {
2147
                skip_bits1(&s->gb);   /* marker */
2148
                width = get_bits(&s->gb, 13);
2149
                skip_bits1(&s->gb);   /* marker */
2150
                height = get_bits(&s->gb, 13);
2151
                skip_bits1(&s->gb);   /* marker */
2152
            }
2153
            
2154
            if(get_bits1(&s->gb)) printf("interlaced not supported\n");   /* interlaced */
2155
            if(!get_bits1(&s->gb)) printf("OBMC not supported\n");   /* OBMC Disable */
2156
            if (vo_ver_id == 1) {
2157
                s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */
2158
            } else {
2159
                s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */
2160
            }
2161
            if(s->vol_sprite_usage==STATIC_SPRITE) printf("Static Sprites not supported\n");
2162
            if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
2163
                if(s->vol_sprite_usage==STATIC_SPRITE){
2164
                    s->sprite_width = get_bits(&s->gb, 13);
2165
                    skip_bits1(&s->gb); /* marker */
2166
                    s->sprite_height= get_bits(&s->gb, 13);
2167
                    skip_bits1(&s->gb); /* marker */
2168
                    s->sprite_left  = get_bits(&s->gb, 13);
2169
                    skip_bits1(&s->gb); /* marker */
2170
                    s->sprite_top   = get_bits(&s->gb, 13);
2171
                    skip_bits1(&s->gb); /* marker */
2172
                }
2173
                s->num_sprite_warping_points= get_bits(&s->gb, 6);
2174
                s->sprite_warping_accuracy = get_bits(&s->gb, 2);
2175
                s->sprite_brightness_change= get_bits1(&s->gb);
2176
                if(s->vol_sprite_usage==STATIC_SPRITE)
2177
                    s->low_latency_sprite= get_bits1(&s->gb);            
2178
            }
2179
            // FIXME sadct disable bit if verid!=1 && shape not rect
2180
            
2181
            if (get_bits1(&s->gb) == 1) {   /* not_8_bit */
2182
                s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */
2183
                if(get_bits(&s->gb, 4)!=8) printf("N-bit not supported\n"); /* bits_per_pixel */
2184
            } else {
2185
                s->quant_precision = 5;
2186
            }
2187
            
2188
            // FIXME a bunch of grayscale shape things
2189
            if(get_bits1(&s->gb)) printf("Quant-Type not supported\n");  /* vol_quant_type */ //FIXME
2190
            if(vo_ver_id != 1)
2191
                 s->quarter_sample= get_bits1(&s->gb);
2192
            else s->quarter_sample=0;
2193

    
2194
            if(!get_bits1(&s->gb)) printf("Complexity estimation not supported\n");
2195
#if 0
2196
            if(get_bits1(&s->gb)) printf("resync disable\n");
2197
#else
2198
            skip_bits1(&s->gb);   /* resync_marker_disabled */
2199
#endif
2200
            s->data_partioning= get_bits1(&s->gb);
2201
            if(s->data_partioning){
2202
                printf("data partitioning not supported\n");
2203
                skip_bits1(&s->gb); // reversible vlc
2204
            }
2205
            
2206
            if(vo_ver_id != 1) {
2207
                s->new_pred= get_bits1(&s->gb);
2208
                if(s->new_pred){
2209
                    printf("new pred not supported\n");
2210
                    skip_bits(&s->gb, 2); /* requested upstream message type */
2211
                    skip_bits1(&s->gb); /* newpred segment type */
2212
                }
2213
                s->reduced_res_vop= get_bits1(&s->gb);
2214
                if(s->reduced_res_vop) printf("reduced resolution VOP not supported\n");
2215
            }
2216
            else{
2217
                s->new_pred=0;
2218
                s->reduced_res_vop= 0;
2219
            }
2220

    
2221
            s->scalability= get_bits1(&s->gb);
2222
            if (s->scalability) {
2223
                printf("bad scalability!!!\n");
2224
                return -1;
2225
            }
2226
        }
2227
//printf("end Data %X %d\n", show_bits(&s->gb, 32), get_bits_count(&s->gb)&0x7);
2228
        goto redo;
2229
    } else if (startcode == 0x1b2) { //userdata
2230
        char buf[256];
2231
        int i;
2232
        int e;
2233
        int ver, build;
2234

    
2235
//printf("user Data %X\n", show_bits(&s->gb, 32));
2236
        buf[0]= show_bits(&s->gb, 8);
2237
        for(i=1; i<256; i++){
2238
            buf[i]= show_bits(&s->gb, 16)&0xFF;
2239
            if(buf[i]==0) break;
2240
            skip_bits(&s->gb, 8);
2241
        }
2242
        buf[255]=0;
2243
        e=sscanf(buf, "DivX%dBuild%d", &ver, &build);
2244
        if(e==2){
2245
            s->divx_version= ver;
2246
            s->divx_build= build;
2247
            if(s->picture_number==0){
2248
                printf("This file was encoded with DivX%d Build%d\n", ver, build);
2249
                if(ver==500 && build==413){ //most likely all version are indeed totally buggy but i dunno for sure ...
2250
                    printf("WARNING: this version of DivX is not MPEG4 compatible, trying to workaround these bugs...\n");
2251
                }else{
2252
                    printf("hmm, i havnt seen that version of divx yet, lets assume they fixed these bugs ...\n"
2253
                           "using mpeg4 decoder, if it fails contact the developers (of ffmpeg)\n");
2254
                }
2255
            }
2256
        }
2257
//printf("User Data: %s\n", buf);
2258
        goto redo;
2259
    } else if (startcode != 0x1b6) { //VOP
2260
        goto redo;
2261
    }
2262

    
2263
    s->pict_type = get_bits(&s->gb, 2) + 1;        /* pict type: I = 0 , P = 1 */
2264
//printf("pic: %d\n", s->pict_type); 
2265
    time_incr=0;
2266
    while (get_bits1(&s->gb) != 0) 
2267
        time_incr++;
2268

    
2269
    check_marker(&s->gb, "before time_increment");
2270
    s->time_increment= get_bits(&s->gb, s->time_increment_bits);
2271
    if(s->pict_type!=B_TYPE){
2272
        s->time_base+= time_incr;
2273
        s->last_non_b_time[1]= s->last_non_b_time[0];
2274
        s->last_non_b_time[0]= s->time_base*s->time_increment_resolution + s->time_increment;
2275
    }else{
2276
        s->time= (s->last_non_b_time[1]/s->time_increment_resolution + time_incr)*s->time_increment_resolution;
2277
        s->time+= s->time_increment;
2278
    }
2279

    
2280
    if(check_marker(&s->gb, "before vop_coded")==0 && s->picture_number==0){
2281
        printf("hmm, seems the headers arnt complete, trying to guess time_increment_bits\n");
2282
        for(s->time_increment_bits++ ;s->time_increment_bits<16; s->time_increment_bits++){
2283
            if(get_bits1(&s->gb)) break;
2284
        }
2285
        printf("my guess is %d bits ;)\n",s->time_increment_bits);
2286
    }
2287
    /* vop coded */
2288
    if (get_bits1(&s->gb) != 1)
2289
        goto redo;
2290
//printf("time %d %d %d || %d %d %d\n", s->time_increment_bits, s->time_increment, s->time_base,
2291
//s->time, s->last_non_b_time[0], s->last_non_b_time[1]);  
2292
    if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == P_TYPE
2293
                          || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) {
2294
        /* rounding type for motion estimation */
2295
        s->no_rounding = get_bits1(&s->gb);
2296
    } else {
2297
        s->no_rounding = 0;
2298
    }
2299
//FIXME reduced res stuff
2300

    
2301
     if (s->shape != RECT_SHAPE) {
2302
         if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) {
2303
             int width, height, hor_spat_ref, ver_spat_ref;
2304
 
2305
             width = get_bits(&s->gb, 13);
2306
             skip_bits1(&s->gb);   /* marker */
2307
             height = get_bits(&s->gb, 13);
2308
             skip_bits1(&s->gb);   /* marker */
2309
             hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */
2310
             skip_bits1(&s->gb);   /* marker */
2311
             ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */
2312
         }
2313
         skip_bits1(&s->gb); /* change_CR_disable */
2314
 
2315
         if (get_bits1(&s->gb) != 0) {
2316
             skip_bits(&s->gb, 8); /* constant_alpha_value */
2317
         }
2318
     }
2319
//FIXME complexity estimation stuff
2320
     
2321
     if (s->shape != BIN_ONLY_SHAPE) {
2322
         skip_bits(&s->gb, 3); /* intra dc VLC threshold */
2323
         //FIXME interlaced specific bits
2324
     }
2325

    
2326
     if(s->pict_type == S_TYPE && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
2327
         if(s->num_sprite_warping_points){
2328
             mpeg4_decode_sprite_trajectory(s);
2329
         }
2330
         if(s->sprite_brightness_change) printf("sprite_brightness_change not supported\n");
2331
         if(s->vol_sprite_usage==STATIC_SPRITE) printf("static sprite not supported\n");
2332
     }
2333

    
2334
     if (s->shape != BIN_ONLY_SHAPE) {
2335
         /* note: we do not use quant_precision to avoid problem if no
2336
            MPEG4 vol header as it is found on some old opendivx
2337
            movies */
2338
         s->qscale = get_bits(&s->gb, 5);
2339
         if(s->qscale==0){
2340
             printf("Error, header damaged or not MPEG4 header (qscale=0)\n");
2341
             return -1; // makes no sense to continue, as there is nothing left from the image then
2342
         }
2343
  
2344
         if (s->pict_type != I_TYPE) {
2345
             s->f_code = get_bits(&s->gb, 3);        /* fcode_for */
2346
             if(s->f_code==0){
2347
                 printf("Error, header damaged or not MPEG4 header (f_code=0)\n");
2348
                 return -1; // makes no sense to continue, as the MV decoding will break very quickly
2349
             }
2350
         }
2351
         if (s->pict_type == B_TYPE) {
2352
             s->b_code = get_bits(&s->gb, 3);
2353
//printf("b-code %d\n", s->b_code);
2354
         }
2355
//printf("quant:%d fcode:%d\n", s->qscale, s->f_code);
2356
         if(!s->scalability){
2357
             if (s->shape!=RECT_SHAPE && s->pict_type!=I_TYPE) {
2358
                 skip_bits1(&s->gb); // vop shape coding type
2359
             }
2360
         }
2361
     }
2362
     s->picture_number++; // better than pic number==0 allways ;)
2363
     return 0;
2364
}
2365

    
2366
/* don't understand why they choose a different header ! */
2367
int intel_h263_decode_picture_header(MpegEncContext *s)
2368
{
2369
    int format;
2370

    
2371
    /* picture header */
2372
    if (get_bits(&s->gb, 22) != 0x20)
2373
        return -1;
2374
    skip_bits(&s->gb, 8); /* picture timestamp */
2375

    
2376
    if (get_bits1(&s->gb) != 1)
2377
        return -1;        /* marker */
2378
    if (get_bits1(&s->gb) != 0)
2379
        return -1;        /* h263 id */
2380
    skip_bits1(&s->gb);        /* split screen off */
2381
    skip_bits1(&s->gb);        /* camera  off */
2382
    skip_bits1(&s->gb);        /* freeze picture release off */
2383

    
2384
    format = get_bits(&s->gb, 3);
2385
    if (format != 7)
2386
        return -1;
2387

    
2388
    s->h263_plus = 0;
2389

    
2390
    s->pict_type = I_TYPE + get_bits1(&s->gb);
2391
    
2392
    s->unrestricted_mv = get_bits1(&s->gb); 
2393
    s->h263_long_vectors = s->unrestricted_mv;
2394

    
2395
    if (get_bits1(&s->gb) != 0)
2396
        return -1;        /* SAC: off */
2397
    if (get_bits1(&s->gb) != 0)
2398
        return -1;        /* advanced prediction mode: off */
2399
    if (get_bits1(&s->gb) != 0)
2400
        return -1;        /* not PB frame */
2401

    
2402
    /* skip unknown header garbage */
2403
    skip_bits(&s->gb, 41);
2404

    
2405
    s->qscale = get_bits(&s->gb, 5);
2406
    skip_bits1(&s->gb);        /* Continuous Presence Multipoint mode: off */
2407

    
2408
    /* PEI */
2409
    while (get_bits1(&s->gb) != 0) {
2410
        skip_bits(&s->gb, 8);
2411
    }
2412
    s->f_code = 1;
2413
    return 0;
2414
}
2415