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