Merge commit 'e3d0f49abb20a551bf6d885f75c354d6d0bbeb9d'
[ffmpeg.git] / libavcodec / ituh263dec.c
1 /*
2  * ITU H263 bitstream decoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * H263+ support.
5  * Copyright (c) 2001 Juan J. Sierralta P
6  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 /**
26  * @file
27  * h263 decoder.
28  */
29
30 #define UNCHECKED_BITSTREAM_READER 1
31 #include <limits.h>
32
33 #include "libavutil/attributes.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/mathematics.h"
36 #include "avcodec.h"
37 #include "mpegvideo.h"
38 #include "h263.h"
39 #include "h263data.h"
40 #include "internal.h"
41 #include "mathops.h"
42 #include "mpegutils.h"
43 #include "unary.h"
44 #include "flv.h"
45 #include "mpeg4video.h"
46 #include "mpegvideodata.h"
47
48 // The defines below define the number of bits that are read at once for
49 // reading vlc values. Changing these may improve speed and data cache needs
50 // be aware though that decreasing them may need the number of stages that is
51 // passed to get_vlc* to be increased.
52 #define MV_VLC_BITS 9
53 #define H263_MBTYPE_B_VLC_BITS 6
54 #define CBPC_B_VLC_BITS 3
55
56 static const int h263_mb_type_b_map[15]= {
57     MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
58     MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
59     MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
60                       MB_TYPE_L0                                 | MB_TYPE_16x16,
61                       MB_TYPE_L0   | MB_TYPE_CBP                 | MB_TYPE_16x16,
62                       MB_TYPE_L0   | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
63                       MB_TYPE_L1                                 | MB_TYPE_16x16,
64                       MB_TYPE_L1   | MB_TYPE_CBP                 | MB_TYPE_16x16,
65                       MB_TYPE_L1   | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
66                       MB_TYPE_L0L1                               | MB_TYPE_16x16,
67                       MB_TYPE_L0L1 | MB_TYPE_CBP                 | MB_TYPE_16x16,
68                       MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
69     0, //stuffing
70     MB_TYPE_INTRA4x4                | MB_TYPE_CBP,
71     MB_TYPE_INTRA4x4                | MB_TYPE_CBP | MB_TYPE_QUANT,
72 };
73
74 void ff_h263_show_pict_info(MpegEncContext *s){
75     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
76     av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n",
77          s->qscale, av_get_picture_type_char(s->pict_type),
78          s->gb.size_in_bits, 1-s->no_rounding,
79          s->obmc ? " AP" : "",
80          s->umvplus ? " UMV" : "",
81          s->h263_long_vectors ? " LONG" : "",
82          s->h263_plus ? " +" : "",
83          s->h263_aic ? " AIC" : "",
84          s->alt_inter_vlc ? " AIV" : "",
85          s->modified_quant ? " MQ" : "",
86          s->loop_filter ? " LOOP" : "",
87          s->h263_slice_structured ? " SS" : "",
88          s->avctx->framerate.num, s->avctx->framerate.den
89     );
90     }
91 }
92
93 /***********************************************/
94 /* decoding */
95
96 VLC ff_h263_intra_MCBPC_vlc;
97 VLC ff_h263_inter_MCBPC_vlc;
98 VLC ff_h263_cbpy_vlc;
99 static VLC mv_vlc;
100 static VLC h263_mbtype_b_vlc;
101 static VLC cbpc_b_vlc;
102
103 /* init vlcs */
104
105 /* XXX: find a better solution to handle static init */
106 av_cold void ff_h263_decode_init_vlc(void)
107 {
108     static volatile int done = 0;
109
110     if (!done) {
111         INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
112                  ff_h263_intra_MCBPC_bits, 1, 1,
113                  ff_h263_intra_MCBPC_code, 1, 1, 72);
114         INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
115                  ff_h263_inter_MCBPC_bits, 1, 1,
116                  ff_h263_inter_MCBPC_code, 1, 1, 198);
117         INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
118                  &ff_h263_cbpy_tab[0][1], 2, 1,
119                  &ff_h263_cbpy_tab[0][0], 2, 1, 64);
120         INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
121                  &ff_mvtab[0][1], 2, 1,
122                  &ff_mvtab[0][0], 2, 1, 538);
123         ff_rl_init(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
124         ff_rl_init(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
125         INIT_VLC_RL(ff_h263_rl_inter, 554);
126         INIT_VLC_RL(ff_rl_intra_aic, 554);
127         INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
128                  &ff_h263_mbtype_b_tab[0][1], 2, 1,
129                  &ff_h263_mbtype_b_tab[0][0], 2, 1, 80);
130         INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
131                  &ff_cbpc_b_tab[0][1], 2, 1,
132                  &ff_cbpc_b_tab[0][0], 2, 1, 8);
133         done = 1;
134     }
135 }
136
137 int ff_h263_decode_mba(MpegEncContext *s)
138 {
139     int i, mb_pos;
140
141     for(i=0; i<6; i++){
142         if(s->mb_num-1 <= ff_mba_max[i]) break;
143     }
144     mb_pos= get_bits(&s->gb, ff_mba_length[i]);
145     s->mb_x= mb_pos % s->mb_width;
146     s->mb_y= mb_pos / s->mb_width;
147
148     return mb_pos;
149 }
150
151 /**
152  * Decode the group of blocks header or slice header.
153  * @return <0 if an error occurred
154  */
155 static int h263_decode_gob_header(MpegEncContext *s)
156 {
157     unsigned int val, gob_number;
158     int left;
159
160     /* Check for GOB Start Code */
161     val = show_bits(&s->gb, 16);
162     if(val)
163         return -1;
164
165         /* We have a GBSC probably with GSTUFF */
166     skip_bits(&s->gb, 16); /* Drop the zeros */
167     left= get_bits_left(&s->gb);
168     //MN: we must check the bits left or we might end in a infinite loop (or segfault)
169     for(;left>13; left--){
170         if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
171     }
172     if(left<=13)
173         return -1;
174
175     if(s->h263_slice_structured){
176         if(check_marker(&s->gb, "before MBA")==0)
177             return -1;
178
179         ff_h263_decode_mba(s);
180
181         if(s->mb_num > 1583)
182             if(check_marker(&s->gb, "after MBA")==0)
183                 return -1;
184
185         s->qscale = get_bits(&s->gb, 5); /* SQUANT */
186         if(check_marker(&s->gb, "after SQUANT")==0)
187             return -1;
188         skip_bits(&s->gb, 2); /* GFID */
189     }else{
190         gob_number = get_bits(&s->gb, 5); /* GN */
191         s->mb_x= 0;
192         s->mb_y= s->gob_index* gob_number;
193         skip_bits(&s->gb, 2); /* GFID */
194         s->qscale = get_bits(&s->gb, 5); /* GQUANT */
195     }
196
197     if(s->mb_y >= s->mb_height)
198         return -1;
199
200     if(s->qscale==0)
201         return -1;
202
203     return 0;
204 }
205
206 /**
207  * Decode the group of blocks / video packet header.
208  * @return bit position of the resync_marker, or <0 if none was found
209  */
210 int ff_h263_resync(MpegEncContext *s){
211     int left, pos, ret;
212
213     if(s->codec_id==AV_CODEC_ID_MPEG4){
214         skip_bits1(&s->gb);
215         align_get_bits(&s->gb);
216     }
217
218     if(show_bits(&s->gb, 16)==0){
219         pos= get_bits_count(&s->gb);
220         if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
221             ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
222         else
223             ret= h263_decode_gob_header(s);
224         if(ret>=0)
225             return pos;
226     }
227     //OK, it's not where it is supposed to be ...
228     s->gb= s->last_resync_gb;
229     align_get_bits(&s->gb);
230     left= get_bits_left(&s->gb);
231
232     for(;left>16+1+5+5; left-=8){
233         if(show_bits(&s->gb, 16)==0){
234             GetBitContext bak= s->gb;
235
236             pos= get_bits_count(&s->gb);
237             if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
238                 ret= ff_mpeg4_decode_video_packet_header(s->avctx->priv_data);
239             else
240                 ret= h263_decode_gob_header(s);
241             if(ret>=0)
242                 return pos;
243
244             s->gb= bak;
245         }
246         skip_bits(&s->gb, 8);
247     }
248
249     return -1;
250 }
251
252 int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code)
253 {
254     int code, val, sign, shift;
255     code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
256
257     if (code == 0)
258         return pred;
259     if (code < 0)
260         return 0xffff;
261
262     sign = get_bits1(&s->gb);
263     shift = f_code - 1;
264     val = code;
265     if (shift) {
266         val = (val - 1) << shift;
267         val |= get_bits(&s->gb, shift);
268         val++;
269     }
270     if (sign)
271         val = -val;
272     val += pred;
273
274     /* modulo decoding */
275     if (!s->h263_long_vectors) {
276         val = sign_extend(val, 5 + f_code);
277     } else {
278         /* horrible h263 long vector mode */
279         if (pred < -31 && val < -63)
280             val += 64;
281         if (pred > 32 && val > 63)
282             val -= 64;
283
284     }
285     return val;
286 }
287
288
289 /* Decode RVLC of H.263+ UMV */
290 static int h263p_decode_umotion(MpegEncContext * s, int pred)
291 {
292    int code = 0, sign;
293
294    if (get_bits1(&s->gb)) /* Motion difference = 0 */
295       return pred;
296
297    code = 2 + get_bits1(&s->gb);
298
299    while (get_bits1(&s->gb))
300    {
301       code <<= 1;
302       code += get_bits1(&s->gb);
303    }
304    sign = code & 1;
305    code >>= 1;
306
307    code = (sign) ? (pred - code) : (pred + code);
308    ff_tlog(s->avctx,"H.263+ UMV Motion = %d\n", code);
309    return code;
310
311 }
312
313 /**
314  * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
315  */
316 static void preview_obmc(MpegEncContext *s){
317     GetBitContext gb= s->gb;
318
319     int cbpc, i, pred_x, pred_y, mx, my;
320     int16_t *mot_val;
321     const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
322     const int stride= s->b8_stride*2;
323
324     for(i=0; i<4; i++)
325         s->block_index[i]+= 2;
326     for(i=4; i<6; i++)
327         s->block_index[i]+= 1;
328     s->mb_x++;
329
330     av_assert2(s->pict_type == AV_PICTURE_TYPE_P);
331
332     do{
333         if (get_bits1(&s->gb)) {
334             /* skip mb */
335             mot_val = s->current_picture.motion_val[0][s->block_index[0]];
336             mot_val[0       ]= mot_val[2       ]=
337             mot_val[0+stride]= mot_val[2+stride]= 0;
338             mot_val[1       ]= mot_val[3       ]=
339             mot_val[1+stride]= mot_val[3+stride]= 0;
340
341             s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
342             goto end;
343         }
344         cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
345     }while(cbpc == 20);
346
347     if(cbpc & 4){
348         s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
349     }else{
350         get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
351         if (cbpc & 8) {
352             if(s->modified_quant){
353                 if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
354                 else                  skip_bits(&s->gb, 5);
355             }else
356                 skip_bits(&s->gb, 2);
357         }
358
359         if ((cbpc & 16) == 0) {
360                 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
361                 /* 16x16 motion prediction */
362                 mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
363                 if (s->umvplus)
364                    mx = h263p_decode_umotion(s, pred_x);
365                 else
366                    mx = ff_h263_decode_motion(s, pred_x, 1);
367
368                 if (s->umvplus)
369                    my = h263p_decode_umotion(s, pred_y);
370                 else
371                    my = ff_h263_decode_motion(s, pred_y, 1);
372
373                 mot_val[0       ]= mot_val[2       ]=
374                 mot_val[0+stride]= mot_val[2+stride]= mx;
375                 mot_val[1       ]= mot_val[3       ]=
376                 mot_val[1+stride]= mot_val[3+stride]= my;
377         } else {
378             s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
379             for(i=0;i<4;i++) {
380                 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
381                 if (s->umvplus)
382                   mx = h263p_decode_umotion(s, pred_x);
383                 else
384                   mx = ff_h263_decode_motion(s, pred_x, 1);
385
386                 if (s->umvplus)
387                   my = h263p_decode_umotion(s, pred_y);
388                 else
389                   my = ff_h263_decode_motion(s, pred_y, 1);
390                 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
391                   skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
392                 mot_val[0] = mx;
393                 mot_val[1] = my;
394             }
395         }
396     }
397 end:
398
399     for(i=0; i<4; i++)
400         s->block_index[i]-= 2;
401     for(i=4; i<6; i++)
402         s->block_index[i]-= 1;
403     s->mb_x--;
404
405     s->gb= gb;
406 }
407
408 static void h263_decode_dquant(MpegEncContext *s){
409     static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
410
411     if(s->modified_quant){
412         if(get_bits1(&s->gb))
413             s->qscale= ff_modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
414         else
415             s->qscale= get_bits(&s->gb, 5);
416     }else
417         s->qscale += quant_tab[get_bits(&s->gb, 2)];
418     ff_set_qscale(s, s->qscale);
419 }
420
421 static int h263_decode_block(MpegEncContext * s, int16_t * block,
422                              int n, int coded)
423 {
424     int level, i, j, run;
425     RLTable *rl = &ff_h263_rl_inter;
426     const uint8_t *scan_table;
427     GetBitContext gb= s->gb;
428
429     scan_table = s->intra_scantable.permutated;
430     if (s->h263_aic && s->mb_intra) {
431         rl = &ff_rl_intra_aic;
432         i = 0;
433         if (s->ac_pred) {
434             if (s->h263_aic_dir)
435                 scan_table = s->intra_v_scantable.permutated; /* left */
436             else
437                 scan_table = s->intra_h_scantable.permutated; /* top */
438         }
439     } else if (s->mb_intra) {
440         /* DC coef */
441         if (CONFIG_RV10_DECODER && s->codec_id == AV_CODEC_ID_RV10) {
442           if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
443             int component, diff;
444             component = (n <= 3 ? 0 : n - 4 + 1);
445             level = s->last_dc[component];
446             if (s->rv10_first_dc_coded[component]) {
447                 diff = ff_rv_decode_dc(s, n);
448                 if (diff == 0xffff)
449                     return -1;
450                 level += diff;
451                 level = level & 0xff; /* handle wrap round */
452                 s->last_dc[component] = level;
453             } else {
454                 s->rv10_first_dc_coded[component] = 1;
455             }
456           } else {
457                 level = get_bits(&s->gb, 8);
458                 if (level == 255)
459                     level = 128;
460           }
461         }else{
462             level = get_bits(&s->gb, 8);
463             if((level&0x7F) == 0){
464                 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
465                 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
466                     return -1;
467             }
468             if (level == 255)
469                 level = 128;
470         }
471         block[0] = level;
472         i = 1;
473     } else {
474         i = 0;
475     }
476     if (!coded) {
477         if (s->mb_intra && s->h263_aic)
478             goto not_coded;
479         s->block_last_index[n] = i - 1;
480         return 0;
481     }
482 retry:
483     {
484     OPEN_READER(re, &s->gb);
485     i--; // offset by -1 to allow direct indexing of scan_table
486     for(;;) {
487         UPDATE_CACHE(re, &s->gb);
488         GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
489         if (run == 66) {
490             if (level){
491                 CLOSE_READER(re, &s->gb);
492                 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
493                 return -1;
494             }
495             /* escape */
496             if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
497                 int is11 = SHOW_UBITS(re, &s->gb, 1);
498                 SKIP_CACHE(re, &s->gb, 1);
499                 run = SHOW_UBITS(re, &s->gb, 7) + 1;
500                 if (is11) {
501                     SKIP_COUNTER(re, &s->gb, 1 + 7);
502                     UPDATE_CACHE(re, &s->gb);
503                     level = SHOW_SBITS(re, &s->gb, 11);
504                     SKIP_COUNTER(re, &s->gb, 11);
505                 } else {
506                     SKIP_CACHE(re, &s->gb, 7);
507                     level = SHOW_SBITS(re, &s->gb, 7);
508                     SKIP_COUNTER(re, &s->gb, 1 + 7 + 7);
509                 }
510             } else {
511                 run = SHOW_UBITS(re, &s->gb, 7) + 1;
512                 SKIP_CACHE(re, &s->gb, 7);
513                 level = (int8_t)SHOW_UBITS(re, &s->gb, 8);
514                 SKIP_COUNTER(re, &s->gb, 7 + 8);
515                 if(level == -128){
516                     UPDATE_CACHE(re, &s->gb);
517                     if (s->codec_id == AV_CODEC_ID_RV10) {
518                         /* XXX: should patch encoder too */
519                         level = SHOW_SBITS(re, &s->gb, 12);
520                         SKIP_COUNTER(re, &s->gb, 12);
521                     }else{
522                         level = SHOW_UBITS(re, &s->gb, 5);
523                         SKIP_CACHE(re, &s->gb, 5);
524                         level |= SHOW_SBITS(re, &s->gb, 6)<<5;
525                         SKIP_COUNTER(re, &s->gb, 5 + 6);
526                     }
527                 }
528             }
529         } else {
530             if (SHOW_UBITS(re, &s->gb, 1))
531                 level = -level;
532             SKIP_COUNTER(re, &s->gb, 1);
533         }
534         i += run;
535         if (i >= 64){
536             CLOSE_READER(re, &s->gb);
537             // redo update without last flag, revert -1 offset
538             i = i - run + ((run-1)&63) + 1;
539             if (i < 64) {
540                 // only last marker, no overrun
541                 block[scan_table[i]] = level;
542                 break;
543             }
544             if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
545                 //Looks like a hack but no, it's the way it is supposed to work ...
546                 rl = &ff_rl_intra_aic;
547                 i = 0;
548                 s->gb= gb;
549                 s->bdsp.clear_block(block);
550                 goto retry;
551             }
552             av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
553             return -1;
554         }
555         j = scan_table[i];
556         block[j] = level;
557     }
558     }
559 not_coded:
560     if (s->mb_intra && s->h263_aic) {
561         ff_h263_pred_acdc(s, block, n);
562         i = 63;
563     }
564     s->block_last_index[n] = i;
565     return 0;
566 }
567
568 static int h263_skip_b_part(MpegEncContext *s, int cbp)
569 {
570     LOCAL_ALIGNED_16(int16_t, dblock, [64]);
571     int i, mbi;
572     int bli[6];
573
574     /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
575      * but real value should be restored in order to be used later (in OBMC condition)
576      */
577     mbi = s->mb_intra;
578     memcpy(bli, s->block_last_index, sizeof(bli));
579     s->mb_intra = 0;
580     for (i = 0; i < 6; i++) {
581         if (h263_decode_block(s, dblock, i, cbp&32) < 0)
582             return -1;
583         cbp+=cbp;
584     }
585     s->mb_intra = mbi;
586     memcpy(s->block_last_index, bli, sizeof(bli));
587     return 0;
588 }
589
590 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
591 {
592     int c, mv = 1;
593
594     if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
595         c = get_bits1(gb);
596         if (pb_frame == 2 && c)
597             mv = !get_bits1(gb);
598     } else { // h.263 Annex M improved PB-frame
599         mv = get_unary(gb, 0, 4) + 1;
600         c = mv & 1;
601         mv = !!(mv & 2);
602     }
603     if(c)
604         *cbpb = get_bits(gb, 6);
605     return mv;
606 }
607
608 int ff_h263_decode_mb(MpegEncContext *s,
609                       int16_t block[6][64])
610 {
611     int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
612     int16_t *mot_val;
613     const int xy= s->mb_x + s->mb_y * s->mb_stride;
614     int cbpb = 0, pb_mv_count = 0;
615
616     av_assert2(!s->h263_pred);
617
618     if (s->pict_type == AV_PICTURE_TYPE_P) {
619         do{
620             if (get_bits1(&s->gb)) {
621                 /* skip mb */
622                 s->mb_intra = 0;
623                 for(i=0;i<6;i++)
624                     s->block_last_index[i] = -1;
625                 s->mv_dir = MV_DIR_FORWARD;
626                 s->mv_type = MV_TYPE_16X16;
627                 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
628                 s->mv[0][0][0] = 0;
629                 s->mv[0][0][1] = 0;
630                 s->mb_skipped = !(s->obmc | s->loop_filter);
631                 goto end;
632             }
633             cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
634             if (cbpc < 0){
635                 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
636                 return -1;
637             }
638         }while(cbpc == 20);
639
640         s->bdsp.clear_blocks(s->block[0]);
641
642         dquant = cbpc & 8;
643         s->mb_intra = ((cbpc & 4) != 0);
644         if (s->mb_intra) goto intra;
645
646         if(s->pb_frame && get_bits1(&s->gb))
647             pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
648         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
649
650         if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
651             cbpy ^= 0xF;
652
653         cbp = (cbpc & 3) | (cbpy << 2);
654         if (dquant) {
655             h263_decode_dquant(s);
656         }
657
658         s->mv_dir = MV_DIR_FORWARD;
659         if ((cbpc & 16) == 0) {
660             s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
661             /* 16x16 motion prediction */
662             s->mv_type = MV_TYPE_16X16;
663             ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
664             if (s->umvplus)
665                mx = h263p_decode_umotion(s, pred_x);
666             else
667                mx = ff_h263_decode_motion(s, pred_x, 1);
668
669             if (mx >= 0xffff)
670                 return -1;
671
672             if (s->umvplus)
673                my = h263p_decode_umotion(s, pred_y);
674             else
675                my = ff_h263_decode_motion(s, pred_y, 1);
676
677             if (my >= 0xffff)
678                 return -1;
679             s->mv[0][0][0] = mx;
680             s->mv[0][0][1] = my;
681
682             if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
683                skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
684         } else {
685             s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
686             s->mv_type = MV_TYPE_8X8;
687             for(i=0;i<4;i++) {
688                 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
689                 if (s->umvplus)
690                   mx = h263p_decode_umotion(s, pred_x);
691                 else
692                   mx = ff_h263_decode_motion(s, pred_x, 1);
693                 if (mx >= 0xffff)
694                     return -1;
695
696                 if (s->umvplus)
697                   my = h263p_decode_umotion(s, pred_y);
698                 else
699                   my = ff_h263_decode_motion(s, pred_y, 1);
700                 if (my >= 0xffff)
701                     return -1;
702                 s->mv[0][i][0] = mx;
703                 s->mv[0][i][1] = my;
704                 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
705                   skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
706                 mot_val[0] = mx;
707                 mot_val[1] = my;
708             }
709         }
710     } else if(s->pict_type==AV_PICTURE_TYPE_B) {
711         int mb_type;
712         const int stride= s->b8_stride;
713         int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
714         int16_t *mot_val1 = s->current_picture.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
715 //        const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
716
717         //FIXME ugly
718         mot_val0[0       ]= mot_val0[2       ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
719         mot_val0[1       ]= mot_val0[3       ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
720         mot_val1[0       ]= mot_val1[2       ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
721         mot_val1[1       ]= mot_val1[3       ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
722
723         do{
724             mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
725             if (mb_type < 0){
726                 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
727                 return -1;
728             }
729
730             mb_type= h263_mb_type_b_map[ mb_type ];
731         }while(!mb_type);
732
733         s->mb_intra = IS_INTRA(mb_type);
734         if(HAS_CBP(mb_type)){
735             s->bdsp.clear_blocks(s->block[0]);
736             cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
737             if(s->mb_intra){
738                 dquant = IS_QUANT(mb_type);
739                 goto intra;
740             }
741
742             cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
743
744             if (cbpy < 0){
745                 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
746                 return -1;
747             }
748
749             if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
750                 cbpy ^= 0xF;
751
752             cbp = (cbpc & 3) | (cbpy << 2);
753         }else
754             cbp=0;
755
756         av_assert2(!s->mb_intra);
757
758         if(IS_QUANT(mb_type)){
759             h263_decode_dquant(s);
760         }
761
762         if(IS_DIRECT(mb_type)){
763             s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
764             mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
765         }else{
766             s->mv_dir = 0;
767             s->mv_type= MV_TYPE_16X16;
768 //FIXME UMV
769
770             if(USES_LIST(mb_type, 0)){
771                 int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &mx, &my);
772                 s->mv_dir = MV_DIR_FORWARD;
773
774                 mx = ff_h263_decode_motion(s, mx, 1);
775                 my = ff_h263_decode_motion(s, my, 1);
776
777                 s->mv[0][0][0] = mx;
778                 s->mv[0][0][1] = my;
779                 mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
780                 mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
781             }
782
783             if(USES_LIST(mb_type, 1)){
784                 int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &mx, &my);
785                 s->mv_dir |= MV_DIR_BACKWARD;
786
787                 mx = ff_h263_decode_motion(s, mx, 1);
788                 my = ff_h263_decode_motion(s, my, 1);
789
790                 s->mv[1][0][0] = mx;
791                 s->mv[1][0][1] = my;
792                 mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
793                 mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
794             }
795         }
796
797         s->current_picture.mb_type[xy] = mb_type;
798     } else { /* I-Frame */
799         do{
800             cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
801             if (cbpc < 0){
802                 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
803                 return -1;
804             }
805         }while(cbpc == 8);
806
807         s->bdsp.clear_blocks(s->block[0]);
808
809         dquant = cbpc & 4;
810         s->mb_intra = 1;
811 intra:
812         s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
813         if (s->h263_aic) {
814             s->ac_pred = get_bits1(&s->gb);
815             if(s->ac_pred){
816                 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
817
818                 s->h263_aic_dir = get_bits1(&s->gb);
819             }
820         }else
821             s->ac_pred = 0;
822
823         if(s->pb_frame && get_bits1(&s->gb))
824             pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
825         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
826         if(cbpy<0){
827             av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
828             return -1;
829         }
830         cbp = (cbpc & 3) | (cbpy << 2);
831         if (dquant) {
832             h263_decode_dquant(s);
833         }
834
835         pb_mv_count += !!s->pb_frame;
836     }
837
838     while(pb_mv_count--){
839         ff_h263_decode_motion(s, 0, 1);
840         ff_h263_decode_motion(s, 0, 1);
841     }
842
843     /* decode each block */
844     for (i = 0; i < 6; i++) {
845         if (h263_decode_block(s, block[i], i, cbp&32) < 0)
846             return -1;
847         cbp+=cbp;
848     }
849
850     if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
851         return -1;
852     if(s->obmc && !s->mb_intra){
853         if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
854             preview_obmc(s);
855     }
856 end:
857
858         /* per-MB end of slice check */
859     {
860         int v= show_bits(&s->gb, 16);
861
862         if (get_bits_left(&s->gb) < 16) {
863             v >>= 16 - get_bits_left(&s->gb);
864         }
865
866         if(v==0)
867             return SLICE_END;
868     }
869
870     return SLICE_OK;
871 }
872
873 /* most is hardcoded. should extend to handle all h263 streams */
874 int ff_h263_decode_picture_header(MpegEncContext *s)
875 {
876     int format, width, height, i;
877     uint32_t startcode;
878
879     align_get_bits(&s->gb);
880
881     if (show_bits(&s->gb, 2) == 2 && s->avctx->frame_number == 0) {
882          av_log(s->avctx, AV_LOG_WARNING, "Header looks like RTP instead of H.263\n");
883     }
884
885     startcode= get_bits(&s->gb, 22-8);
886
887     for(i= get_bits_left(&s->gb); i>24; i-=8) {
888         startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
889
890         if(startcode == 0x20)
891             break;
892     }
893
894     if (startcode != 0x20) {
895         av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
896         return -1;
897     }
898     /* temporal reference */
899     i = get_bits(&s->gb, 8); /* picture timestamp */
900     if( (s->picture_number&~0xFF)+i < s->picture_number)
901         i+= 256;
902     s->picture_number= (s->picture_number&~0xFF) + i;
903
904     /* PTYPE starts here */
905     if (check_marker(&s->gb, "in PTYPE") != 1) {
906         return -1;
907     }
908     if (get_bits1(&s->gb) != 0) {
909         av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
910         return -1;      /* h263 id */
911     }
912     skip_bits1(&s->gb);         /* split screen off */
913     skip_bits1(&s->gb);         /* camera  off */
914     skip_bits1(&s->gb);         /* freeze picture release off */
915
916     format = get_bits(&s->gb, 3);
917     /*
918         0    forbidden
919         1    sub-QCIF
920         10   QCIF
921         7       extended PTYPE (PLUSPTYPE)
922     */
923
924     if (format != 7 && format != 6) {
925         s->h263_plus = 0;
926         /* H.263v1 */
927         width = ff_h263_format[format][0];
928         height = ff_h263_format[format][1];
929         if (!width)
930             return -1;
931
932         s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
933
934         s->h263_long_vectors = get_bits1(&s->gb);
935
936         if (get_bits1(&s->gb) != 0) {
937             av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
938             return -1; /* SAC: off */
939         }
940         s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
941         s->unrestricted_mv = s->h263_long_vectors || s->obmc;
942
943         s->pb_frame = get_bits1(&s->gb);
944         s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
945         skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
946
947         s->width = width;
948         s->height = height;
949         s->avctx->sample_aspect_ratio= (AVRational){12,11};
950         s->avctx->framerate = (AVRational){ 30000, 1001 };
951     } else {
952         int ufep;
953
954         /* H.263v2 */
955         s->h263_plus = 1;
956         ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
957
958         /* ufep other than 0 and 1 are reserved */
959         if (ufep == 1) {
960             /* OPPTYPE */
961             format = get_bits(&s->gb, 3);
962             ff_dlog(s->avctx, "ufep=1, format: %d\n", format);
963             s->custom_pcf= get_bits1(&s->gb);
964             s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
965             if (get_bits1(&s->gb) != 0) {
966                 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
967             }
968             s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
969             s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
970             s->loop_filter= get_bits1(&s->gb);
971             s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
972             if(s->avctx->lowres)
973                 s->loop_filter = 0;
974
975             s->h263_slice_structured= get_bits1(&s->gb);
976             if (get_bits1(&s->gb) != 0) {
977                 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
978             }
979             if (get_bits1(&s->gb) != 0) {
980                 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
981             }
982             s->alt_inter_vlc= get_bits1(&s->gb);
983             s->modified_quant= get_bits1(&s->gb);
984             if(s->modified_quant)
985                 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
986
987             skip_bits(&s->gb, 1); /* Prevent start code emulation */
988
989             skip_bits(&s->gb, 3); /* Reserved */
990         } else if (ufep != 0) {
991             av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
992             return -1;
993         }
994
995         /* MPPTYPE */
996         s->pict_type = get_bits(&s->gb, 3);
997         switch(s->pict_type){
998         case 0: s->pict_type= AV_PICTURE_TYPE_I;break;
999         case 1: s->pict_type= AV_PICTURE_TYPE_P;break;
1000         case 2: s->pict_type= AV_PICTURE_TYPE_P;s->pb_frame = 3;break;
1001         case 3: s->pict_type= AV_PICTURE_TYPE_B;break;
1002         case 7: s->pict_type= AV_PICTURE_TYPE_I;break; //ZYGO
1003         default:
1004             return -1;
1005         }
1006         skip_bits(&s->gb, 2);
1007         s->no_rounding = get_bits1(&s->gb);
1008         skip_bits(&s->gb, 4);
1009
1010         /* Get the picture dimensions */
1011         if (ufep) {
1012             if (format == 6) {
1013                 /* Custom Picture Format (CPFMT) */
1014                 s->aspect_ratio_info = get_bits(&s->gb, 4);
1015                 ff_dlog(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
1016                 /* aspect ratios:
1017                 0 - forbidden
1018                 1 - 1:1
1019                 2 - 12:11 (CIF 4:3)
1020                 3 - 10:11 (525-type 4:3)
1021                 4 - 16:11 (CIF 16:9)
1022                 5 - 40:33 (525-type 16:9)
1023                 6-14 - reserved
1024                 */
1025                 width = (get_bits(&s->gb, 9) + 1) * 4;
1026                 check_marker(&s->gb, "in dimensions");
1027                 height = get_bits(&s->gb, 9) * 4;
1028                 ff_dlog(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
1029                 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1030                     /* aspected dimensions */
1031                     s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
1032                     s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
1033                 }else{
1034                     s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
1035                 }
1036             } else {
1037                 width = ff_h263_format[format][0];
1038                 height = ff_h263_format[format][1];
1039                 s->avctx->sample_aspect_ratio= (AVRational){12,11};
1040             }
1041             s->avctx->sample_aspect_ratio.den <<= s->ehc_mode;
1042             if ((width == 0) || (height == 0))
1043                 return -1;
1044             s->width = width;
1045             s->height = height;
1046
1047             if(s->custom_pcf){
1048                 int gcd;
1049                 s->avctx->framerate.num  = 1800000;
1050                 s->avctx->framerate.den  = 1000 + get_bits1(&s->gb);
1051                 s->avctx->framerate.den *= get_bits(&s->gb, 7);
1052                 if(s->avctx->framerate.den == 0){
1053                     av_log(s, AV_LOG_ERROR, "zero framerate\n");
1054                     return -1;
1055                 }
1056                 gcd= av_gcd(s->avctx->framerate.den, s->avctx->framerate.num);
1057                 s->avctx->framerate.den /= gcd;
1058                 s->avctx->framerate.num /= gcd;
1059             }else{
1060                 s->avctx->framerate = (AVRational){ 30000, 1001 };
1061             }
1062         }
1063
1064         if(s->custom_pcf){
1065             skip_bits(&s->gb, 2); //extended Temporal reference
1066         }
1067
1068         if (ufep) {
1069             if (s->umvplus) {
1070                 if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1071                     skip_bits1(&s->gb);
1072             }
1073             if(s->h263_slice_structured){
1074                 if (get_bits1(&s->gb) != 0) {
1075                     av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
1076                 }
1077                 if (get_bits1(&s->gb) != 0) {
1078                     av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
1079                 }
1080             }
1081         }
1082
1083         s->qscale = get_bits(&s->gb, 5);
1084     }
1085
1086     if (s->width == 0 || s->height == 0) {
1087         av_log(s->avctx, AV_LOG_ERROR, "dimensions 0\n");
1088         return -1;
1089     }
1090     s->mb_width = (s->width  + 15) / 16;
1091     s->mb_height = (s->height  + 15) / 16;
1092     s->mb_num = s->mb_width * s->mb_height;
1093
1094     if (s->pb_frame) {
1095         skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
1096         if (s->custom_pcf)
1097             skip_bits(&s->gb, 2); //extended Temporal reference
1098         skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
1099     }
1100
1101     if (s->pict_type!=AV_PICTURE_TYPE_B) {
1102         s->time            = s->picture_number;
1103         s->pp_time         = s->time - s->last_non_b_time;
1104         s->last_non_b_time = s->time;
1105     }else{
1106         s->time    = s->picture_number;
1107         s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
1108         if (s->pp_time <=s->pb_time ||
1109             s->pp_time <= s->pp_time - s->pb_time ||
1110             s->pp_time <= 0){
1111             s->pp_time = 2;
1112             s->pb_time = 1;
1113         }
1114         ff_mpeg4_init_direct_mv(s);
1115     }
1116
1117     /* PEI */
1118     if (skip_1stop_8data_bits(&s->gb) < 0)
1119         return AVERROR_INVALIDDATA;
1120
1121     if(s->h263_slice_structured){
1122         if (check_marker(&s->gb, "SEPB1") != 1) {
1123             return -1;
1124         }
1125
1126         ff_h263_decode_mba(s);
1127
1128         if (check_marker(&s->gb, "SEPB2") != 1) {
1129             return -1;
1130         }
1131     }
1132     s->f_code = 1;
1133
1134     if(s->h263_aic){
1135          s->y_dc_scale_table=
1136          s->c_dc_scale_table= ff_aic_dc_scale_table;
1137     }else{
1138         s->y_dc_scale_table=
1139         s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1140     }
1141
1142         ff_h263_show_pict_info(s);
1143     if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO") && get_bits_left(&s->gb) >= 85 + 13*3*16 + 50){
1144         int i,j;
1145         for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
1146         av_log(s->avctx, AV_LOG_DEBUG, "\n");
1147         for(i=0; i<13; i++){
1148             for(j=0; j<3; j++){
1149                 int v= get_bits(&s->gb, 8);
1150                 v |= get_sbits(&s->gb, 8)<<8;
1151                 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
1152             }
1153             av_log(s->avctx, AV_LOG_DEBUG, "\n");
1154         }
1155         for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
1156     }
1157
1158     return 0;
1159 }