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