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