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