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