00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #define UNCHECKED_BITSTREAM_READER 1
00031
00032
00033 #include <limits.h>
00034
00035 #include "libavutil/mathematics.h"
00036 #include "dsputil.h"
00037 #include "avcodec.h"
00038 #include "mpegvideo.h"
00039 #include "h263.h"
00040 #include "mathops.h"
00041 #include "unary.h"
00042 #include "flv.h"
00043 #include "mpeg4video.h"
00044
00045
00046
00047
00048
00049 #define MV_VLC_BITS 9
00050 #define H263_MBTYPE_B_VLC_BITS 6
00051 #define CBPC_B_VLC_BITS 3
00052
00053 static const int h263_mb_type_b_map[15]= {
00054 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
00055 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
00056 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
00057 MB_TYPE_L0 | MB_TYPE_16x16,
00058 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_16x16,
00059 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00060 MB_TYPE_L1 | MB_TYPE_16x16,
00061 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_16x16,
00062 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00063 MB_TYPE_L0L1 | MB_TYPE_16x16,
00064 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_16x16,
00065 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00066 0,
00067 MB_TYPE_INTRA4x4 | MB_TYPE_CBP,
00068 MB_TYPE_INTRA4x4 | MB_TYPE_CBP | MB_TYPE_QUANT,
00069 };
00070
00071 void ff_h263_show_pict_info(MpegEncContext *s){
00072 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00073 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",
00074 s->qscale, av_get_picture_type_char(s->pict_type),
00075 s->gb.size_in_bits, 1-s->no_rounding,
00076 s->obmc ? " AP" : "",
00077 s->umvplus ? " UMV" : "",
00078 s->h263_long_vectors ? " LONG" : "",
00079 s->h263_plus ? " +" : "",
00080 s->h263_aic ? " AIC" : "",
00081 s->alt_inter_vlc ? " AIV" : "",
00082 s->modified_quant ? " MQ" : "",
00083 s->loop_filter ? " LOOP" : "",
00084 s->h263_slice_structured ? " SS" : "",
00085 s->avctx->time_base.den, s->avctx->time_base.num
00086 );
00087 }
00088 }
00089
00090
00091
00092
00093 VLC ff_h263_intra_MCBPC_vlc;
00094 VLC ff_h263_inter_MCBPC_vlc;
00095 VLC ff_h263_cbpy_vlc;
00096 static VLC mv_vlc;
00097 static VLC h263_mbtype_b_vlc;
00098 static VLC cbpc_b_vlc;
00099
00100
00101
00102
00103 void ff_h263_decode_init_vlc(MpegEncContext *s)
00104 {
00105 static int done = 0;
00106
00107 if (!done) {
00108 done = 1;
00109
00110 INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
00111 ff_h263_intra_MCBPC_bits, 1, 1,
00112 ff_h263_intra_MCBPC_code, 1, 1, 72);
00113 INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
00114 ff_h263_inter_MCBPC_bits, 1, 1,
00115 ff_h263_inter_MCBPC_code, 1, 1, 198);
00116 INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
00117 &ff_h263_cbpy_tab[0][1], 2, 1,
00118 &ff_h263_cbpy_tab[0][0], 2, 1, 64);
00119 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
00120 &ff_mvtab[0][1], 2, 1,
00121 &ff_mvtab[0][0], 2, 1, 538);
00122 ff_init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
00123 ff_init_rl(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
00124 INIT_VLC_RL(ff_h263_rl_inter, 554);
00125 INIT_VLC_RL(ff_rl_intra_aic, 554);
00126 INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
00127 &ff_h263_mbtype_b_tab[0][1], 2, 1,
00128 &ff_h263_mbtype_b_tab[0][0], 2, 1, 80);
00129 INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
00130 &ff_cbpc_b_tab[0][1], 2, 1,
00131 &ff_cbpc_b_tab[0][0], 2, 1, 8);
00132 }
00133 }
00134
00135 int ff_h263_decode_mba(MpegEncContext *s)
00136 {
00137 int i, mb_pos;
00138
00139 for(i=0; i<6; i++){
00140 if(s->mb_num-1 <= ff_mba_max[i]) break;
00141 }
00142 mb_pos= get_bits(&s->gb, ff_mba_length[i]);
00143 s->mb_x= mb_pos % s->mb_width;
00144 s->mb_y= mb_pos / s->mb_width;
00145
00146 return mb_pos;
00147 }
00148
00153 static int h263_decode_gob_header(MpegEncContext *s)
00154 {
00155 unsigned int val, gob_number;
00156 int left;
00157
00158
00159 val = show_bits(&s->gb, 16);
00160 if(val)
00161 return -1;
00162
00163
00164 skip_bits(&s->gb, 16);
00165 left= get_bits_left(&s->gb);
00166
00167 for(;left>13; left--){
00168 if(get_bits1(&s->gb)) break;
00169 }
00170 if(left<=13)
00171 return -1;
00172
00173 if(s->h263_slice_structured){
00174 if(get_bits1(&s->gb)==0)
00175 return -1;
00176
00177 ff_h263_decode_mba(s);
00178
00179 if(s->mb_num > 1583)
00180 if(get_bits1(&s->gb)==0)
00181 return -1;
00182
00183 s->qscale = get_bits(&s->gb, 5);
00184 if(get_bits1(&s->gb)==0)
00185 return -1;
00186 skip_bits(&s->gb, 2);
00187 }else{
00188 gob_number = get_bits(&s->gb, 5);
00189 s->mb_x= 0;
00190 s->mb_y= s->gob_index* gob_number;
00191 skip_bits(&s->gb, 2);
00192 s->qscale = get_bits(&s->gb, 5);
00193 }
00194
00195 if(s->mb_y >= s->mb_height)
00196 return -1;
00197
00198 if(s->qscale==0)
00199 return -1;
00200
00201 return 0;
00202 }
00203
00210 const uint8_t *ff_h263_find_resync_marker(const uint8_t *av_restrict p, const uint8_t *av_restrict end)
00211 {
00212 av_assert2(p < end);
00213
00214 end-=2;
00215 p++;
00216 for(;p<end; p+=2){
00217 if(!*p){
00218 if (!p[-1] && p[1]) return p - 1;
00219 else if(!p[ 1] && p[2]) return p;
00220 }
00221 }
00222 return end+2;
00223 }
00224
00229 int ff_h263_resync(MpegEncContext *s){
00230 int left, pos, ret;
00231
00232 if(s->codec_id==AV_CODEC_ID_MPEG4){
00233 skip_bits1(&s->gb);
00234 align_get_bits(&s->gb);
00235 }
00236
00237 if(show_bits(&s->gb, 16)==0){
00238 pos= get_bits_count(&s->gb);
00239 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
00240 ret= ff_mpeg4_decode_video_packet_header(s);
00241 else
00242 ret= h263_decode_gob_header(s);
00243 if(ret>=0)
00244 return pos;
00245 }
00246
00247 s->gb= s->last_resync_gb;
00248 align_get_bits(&s->gb);
00249 left= get_bits_left(&s->gb);
00250
00251 for(;left>16+1+5+5; left-=8){
00252 if(show_bits(&s->gb, 16)==0){
00253 GetBitContext bak= s->gb;
00254
00255 pos= get_bits_count(&s->gb);
00256 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4)
00257 ret= ff_mpeg4_decode_video_packet_header(s);
00258 else
00259 ret= h263_decode_gob_header(s);
00260 if(ret>=0)
00261 return pos;
00262
00263 s->gb= bak;
00264 }
00265 skip_bits(&s->gb, 8);
00266 }
00267
00268 return -1;
00269 }
00270
00271 int ff_h263_decode_motion(MpegEncContext * s, int pred, int f_code)
00272 {
00273 int code, val, sign, shift;
00274 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
00275
00276 if (code == 0)
00277 return pred;
00278 if (code < 0)
00279 return 0xffff;
00280
00281 sign = get_bits1(&s->gb);
00282 shift = f_code - 1;
00283 val = code;
00284 if (shift) {
00285 val = (val - 1) << shift;
00286 val |= get_bits(&s->gb, shift);
00287 val++;
00288 }
00289 if (sign)
00290 val = -val;
00291 val += pred;
00292
00293
00294 if (!s->h263_long_vectors) {
00295 val = sign_extend(val, 5 + f_code);
00296 } else {
00297
00298 if (pred < -31 && val < -63)
00299 val += 64;
00300 if (pred > 32 && val > 63)
00301 val -= 64;
00302
00303 }
00304 return val;
00305 }
00306
00307
00308
00309 static int h263p_decode_umotion(MpegEncContext * s, int pred)
00310 {
00311 int code = 0, sign;
00312
00313 if (get_bits1(&s->gb))
00314 return pred;
00315
00316 code = 2 + get_bits1(&s->gb);
00317
00318 while (get_bits1(&s->gb))
00319 {
00320 code <<= 1;
00321 code += get_bits1(&s->gb);
00322 }
00323 sign = code & 1;
00324 code >>= 1;
00325
00326 code = (sign) ? (pred - code) : (pred + code);
00327 av_dlog(s->avctx,"H.263+ UMV Motion = %d\n", code);
00328 return code;
00329
00330 }
00331
00335 static void preview_obmc(MpegEncContext *s){
00336 GetBitContext gb= s->gb;
00337
00338 int cbpc, i, pred_x, pred_y, mx, my;
00339 int16_t *mot_val;
00340 const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
00341 const int stride= s->b8_stride*2;
00342
00343 for(i=0; i<4; i++)
00344 s->block_index[i]+= 2;
00345 for(i=4; i<6; i++)
00346 s->block_index[i]+= 1;
00347 s->mb_x++;
00348
00349 av_assert2(s->pict_type == AV_PICTURE_TYPE_P);
00350
00351 do{
00352 if (get_bits1(&s->gb)) {
00353
00354 mot_val = s->current_picture.f.motion_val[0][s->block_index[0]];
00355 mot_val[0 ]= mot_val[2 ]=
00356 mot_val[0+stride]= mot_val[2+stride]= 0;
00357 mot_val[1 ]= mot_val[3 ]=
00358 mot_val[1+stride]= mot_val[3+stride]= 0;
00359
00360 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00361 goto end;
00362 }
00363 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
00364 }while(cbpc == 20);
00365
00366 if(cbpc & 4){
00367 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
00368 }else{
00369 get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00370 if (cbpc & 8) {
00371 if(s->modified_quant){
00372 if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
00373 else skip_bits(&s->gb, 5);
00374 }else
00375 skip_bits(&s->gb, 2);
00376 }
00377
00378 if ((cbpc & 16) == 0) {
00379 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
00380
00381 mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00382 if (s->umvplus)
00383 mx = h263p_decode_umotion(s, pred_x);
00384 else
00385 mx = ff_h263_decode_motion(s, pred_x, 1);
00386
00387 if (s->umvplus)
00388 my = h263p_decode_umotion(s, pred_y);
00389 else
00390 my = ff_h263_decode_motion(s, pred_y, 1);
00391
00392 mot_val[0 ]= mot_val[2 ]=
00393 mot_val[0+stride]= mot_val[2+stride]= mx;
00394 mot_val[1 ]= mot_val[3 ]=
00395 mot_val[1+stride]= mot_val[3+stride]= my;
00396 } else {
00397 s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
00398 for(i=0;i<4;i++) {
00399 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
00400 if (s->umvplus)
00401 mx = h263p_decode_umotion(s, pred_x);
00402 else
00403 mx = ff_h263_decode_motion(s, pred_x, 1);
00404
00405 if (s->umvplus)
00406 my = h263p_decode_umotion(s, pred_y);
00407 else
00408 my = ff_h263_decode_motion(s, pred_y, 1);
00409 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
00410 skip_bits1(&s->gb);
00411 mot_val[0] = mx;
00412 mot_val[1] = my;
00413 }
00414 }
00415 }
00416 end:
00417
00418 for(i=0; i<4; i++)
00419 s->block_index[i]-= 2;
00420 for(i=4; i<6; i++)
00421 s->block_index[i]-= 1;
00422 s->mb_x--;
00423
00424 s->gb= gb;
00425 }
00426
00427 static void h263_decode_dquant(MpegEncContext *s){
00428 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
00429
00430 if(s->modified_quant){
00431 if(get_bits1(&s->gb))
00432 s->qscale= ff_modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
00433 else
00434 s->qscale= get_bits(&s->gb, 5);
00435 }else
00436 s->qscale += quant_tab[get_bits(&s->gb, 2)];
00437 ff_set_qscale(s, s->qscale);
00438 }
00439
00440 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
00441 int n, int coded)
00442 {
00443 int code, level, i, j, last, run;
00444 RLTable *rl = &ff_h263_rl_inter;
00445 const uint8_t *scan_table;
00446 GetBitContext gb= s->gb;
00447
00448 scan_table = s->intra_scantable.permutated;
00449 if (s->h263_aic && s->mb_intra) {
00450 rl = &ff_rl_intra_aic;
00451 i = 0;
00452 if (s->ac_pred) {
00453 if (s->h263_aic_dir)
00454 scan_table = s->intra_v_scantable.permutated;
00455 else
00456 scan_table = s->intra_h_scantable.permutated;
00457 }
00458 } else if (s->mb_intra) {
00459
00460 if(s->codec_id == AV_CODEC_ID_RV10){
00461 #if CONFIG_RV10_DECODER
00462 if (s->rv10_version == 3 && s->pict_type == AV_PICTURE_TYPE_I) {
00463 int component, diff;
00464 component = (n <= 3 ? 0 : n - 4 + 1);
00465 level = s->last_dc[component];
00466 if (s->rv10_first_dc_coded[component]) {
00467 diff = ff_rv_decode_dc(s, n);
00468 if (diff == 0xffff)
00469 return -1;
00470 level += diff;
00471 level = level & 0xff;
00472 s->last_dc[component] = level;
00473 } else {
00474 s->rv10_first_dc_coded[component] = 1;
00475 }
00476 } else {
00477 level = get_bits(&s->gb, 8);
00478 if (level == 255)
00479 level = 128;
00480 }
00481 #endif
00482 }else{
00483 level = get_bits(&s->gb, 8);
00484 if((level&0x7F) == 0){
00485 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
00486 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
00487 return -1;
00488 }
00489 if (level == 255)
00490 level = 128;
00491 }
00492 block[0] = level;
00493 i = 1;
00494 } else {
00495 i = 0;
00496 }
00497 if (!coded) {
00498 if (s->mb_intra && s->h263_aic)
00499 goto not_coded;
00500 s->block_last_index[n] = i - 1;
00501 return 0;
00502 }
00503 retry:
00504 for(;;) {
00505 code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
00506 if (code < 0){
00507 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
00508 return -1;
00509 }
00510 if (code == rl->n) {
00511
00512 if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
00513 ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
00514 } else {
00515 last = get_bits1(&s->gb);
00516 run = get_bits(&s->gb, 6);
00517 level = (int8_t)get_bits(&s->gb, 8);
00518 if(level == -128){
00519 if (s->codec_id == AV_CODEC_ID_RV10) {
00520
00521 level = get_sbits(&s->gb, 12);
00522 }else{
00523 level = get_bits(&s->gb, 5);
00524 level |= get_sbits(&s->gb, 6)<<5;
00525 }
00526 }
00527 }
00528 } else {
00529 run = rl->table_run[code];
00530 level = rl->table_level[code];
00531 last = code >= rl->last;
00532 if (get_bits1(&s->gb))
00533 level = -level;
00534 }
00535 i += run;
00536 if (i >= 64){
00537 if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
00538
00539 rl = &ff_rl_intra_aic;
00540 i = 0;
00541 s->gb= gb;
00542 s->dsp.clear_block(block);
00543 goto retry;
00544 }
00545 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
00546 return -1;
00547 }
00548 j = scan_table[i];
00549 block[j] = level;
00550 if (last)
00551 break;
00552 i++;
00553 }
00554 not_coded:
00555 if (s->mb_intra && s->h263_aic) {
00556 ff_h263_pred_acdc(s, block, n);
00557 i = 63;
00558 }
00559 s->block_last_index[n] = i;
00560 return 0;
00561 }
00562
00563 static int h263_skip_b_part(MpegEncContext *s, int cbp)
00564 {
00565 LOCAL_ALIGNED_16(DCTELEM, dblock, [64]);
00566 int i, mbi;
00567
00568
00569
00570
00571 mbi = s->mb_intra;
00572 s->mb_intra = 0;
00573 for (i = 0; i < 6; i++) {
00574 if (h263_decode_block(s, dblock, i, cbp&32) < 0)
00575 return -1;
00576 cbp+=cbp;
00577 }
00578 s->mb_intra = mbi;
00579 return 0;
00580 }
00581
00582 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
00583 {
00584 int c, mv = 1;
00585
00586 if (pb_frame < 3) {
00587 c = get_bits1(gb);
00588 if (pb_frame == 2 && c)
00589 mv = !get_bits1(gb);
00590 } else {
00591 mv = get_unary(gb, 0, 4) + 1;
00592 c = mv & 1;
00593 mv = !!(mv & 2);
00594 }
00595 if(c)
00596 *cbpb = get_bits(gb, 6);
00597 return mv;
00598 }
00599
00600 int ff_h263_decode_mb(MpegEncContext *s,
00601 DCTELEM block[6][64])
00602 {
00603 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
00604 int16_t *mot_val;
00605 const int xy= s->mb_x + s->mb_y * s->mb_stride;
00606 int cbpb = 0, pb_mv_count = 0;
00607
00608 av_assert2(!s->h263_pred);
00609
00610 if (s->pict_type == AV_PICTURE_TYPE_P) {
00611 do{
00612 if (get_bits1(&s->gb)) {
00613
00614 s->mb_intra = 0;
00615 for(i=0;i<6;i++)
00616 s->block_last_index[i] = -1;
00617 s->mv_dir = MV_DIR_FORWARD;
00618 s->mv_type = MV_TYPE_16X16;
00619 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00620 s->mv[0][0][0] = 0;
00621 s->mv[0][0][1] = 0;
00622 s->mb_skipped = !(s->obmc | s->loop_filter);
00623 goto end;
00624 }
00625 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
00626 if (cbpc < 0){
00627 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
00628 return -1;
00629 }
00630 }while(cbpc == 20);
00631
00632 s->dsp.clear_blocks(s->block[0]);
00633
00634 dquant = cbpc & 8;
00635 s->mb_intra = ((cbpc & 4) != 0);
00636 if (s->mb_intra) goto intra;
00637
00638 if(s->pb_frame && get_bits1(&s->gb))
00639 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
00640 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00641
00642 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
00643 cbpy ^= 0xF;
00644
00645 cbp = (cbpc & 3) | (cbpy << 2);
00646 if (dquant) {
00647 h263_decode_dquant(s);
00648 }
00649
00650 s->mv_dir = MV_DIR_FORWARD;
00651 if ((cbpc & 16) == 0) {
00652 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
00653
00654 s->mv_type = MV_TYPE_16X16;
00655 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00656 if (s->umvplus)
00657 mx = h263p_decode_umotion(s, pred_x);
00658 else
00659 mx = ff_h263_decode_motion(s, pred_x, 1);
00660
00661 if (mx >= 0xffff)
00662 return -1;
00663
00664 if (s->umvplus)
00665 my = h263p_decode_umotion(s, pred_y);
00666 else
00667 my = ff_h263_decode_motion(s, pred_y, 1);
00668
00669 if (my >= 0xffff)
00670 return -1;
00671 s->mv[0][0][0] = mx;
00672 s->mv[0][0][1] = my;
00673
00674 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
00675 skip_bits1(&s->gb);
00676 } else {
00677 s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
00678 s->mv_type = MV_TYPE_8X8;
00679 for(i=0;i<4;i++) {
00680 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
00681 if (s->umvplus)
00682 mx = h263p_decode_umotion(s, pred_x);
00683 else
00684 mx = ff_h263_decode_motion(s, pred_x, 1);
00685 if (mx >= 0xffff)
00686 return -1;
00687
00688 if (s->umvplus)
00689 my = h263p_decode_umotion(s, pred_y);
00690 else
00691 my = ff_h263_decode_motion(s, pred_y, 1);
00692 if (my >= 0xffff)
00693 return -1;
00694 s->mv[0][i][0] = mx;
00695 s->mv[0][i][1] = my;
00696 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
00697 skip_bits1(&s->gb);
00698 mot_val[0] = mx;
00699 mot_val[1] = my;
00700 }
00701 }
00702 } else if(s->pict_type==AV_PICTURE_TYPE_B) {
00703 int mb_type;
00704 const int stride= s->b8_stride;
00705 int16_t *mot_val0 = s->current_picture.f.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
00706 int16_t *mot_val1 = s->current_picture.f.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
00707
00708
00709
00710 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
00711 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
00712 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
00713 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
00714
00715 do{
00716 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
00717 if (mb_type < 0){
00718 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
00719 return -1;
00720 }
00721
00722 mb_type= h263_mb_type_b_map[ mb_type ];
00723 }while(!mb_type);
00724
00725 s->mb_intra = IS_INTRA(mb_type);
00726 if(HAS_CBP(mb_type)){
00727 s->dsp.clear_blocks(s->block[0]);
00728 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
00729 if(s->mb_intra){
00730 dquant = IS_QUANT(mb_type);
00731 goto intra;
00732 }
00733
00734 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00735
00736 if (cbpy < 0){
00737 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
00738 return -1;
00739 }
00740
00741 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
00742 cbpy ^= 0xF;
00743
00744 cbp = (cbpc & 3) | (cbpy << 2);
00745 }else
00746 cbp=0;
00747
00748 av_assert2(!s->mb_intra);
00749
00750 if(IS_QUANT(mb_type)){
00751 h263_decode_dquant(s);
00752 }
00753
00754 if(IS_DIRECT(mb_type)){
00755 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
00756 mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
00757 }else{
00758 s->mv_dir = 0;
00759 s->mv_type= MV_TYPE_16X16;
00760
00761
00762 if(USES_LIST(mb_type, 0)){
00763 int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &mx, &my);
00764 s->mv_dir = MV_DIR_FORWARD;
00765
00766 mx = ff_h263_decode_motion(s, mx, 1);
00767 my = ff_h263_decode_motion(s, my, 1);
00768
00769 s->mv[0][0][0] = mx;
00770 s->mv[0][0][1] = my;
00771 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
00772 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
00773 }
00774
00775 if(USES_LIST(mb_type, 1)){
00776 int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &mx, &my);
00777 s->mv_dir |= MV_DIR_BACKWARD;
00778
00779 mx = ff_h263_decode_motion(s, mx, 1);
00780 my = ff_h263_decode_motion(s, my, 1);
00781
00782 s->mv[1][0][0] = mx;
00783 s->mv[1][0][1] = my;
00784 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
00785 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
00786 }
00787 }
00788
00789 s->current_picture.f.mb_type[xy] = mb_type;
00790 } else {
00791 do{
00792 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
00793 if (cbpc < 0){
00794 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
00795 return -1;
00796 }
00797 }while(cbpc == 8);
00798
00799 s->dsp.clear_blocks(s->block[0]);
00800
00801 dquant = cbpc & 4;
00802 s->mb_intra = 1;
00803 intra:
00804 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
00805 if (s->h263_aic) {
00806 s->ac_pred = get_bits1(&s->gb);
00807 if(s->ac_pred){
00808 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
00809
00810 s->h263_aic_dir = get_bits1(&s->gb);
00811 }
00812 }else
00813 s->ac_pred = 0;
00814
00815 if(s->pb_frame && get_bits1(&s->gb))
00816 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
00817 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00818 if(cbpy<0){
00819 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
00820 return -1;
00821 }
00822 cbp = (cbpc & 3) | (cbpy << 2);
00823 if (dquant) {
00824 h263_decode_dquant(s);
00825 }
00826
00827 pb_mv_count += !!s->pb_frame;
00828 }
00829
00830 while(pb_mv_count--){
00831 ff_h263_decode_motion(s, 0, 1);
00832 ff_h263_decode_motion(s, 0, 1);
00833 }
00834
00835
00836 for (i = 0; i < 6; i++) {
00837 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
00838 return -1;
00839 cbp+=cbp;
00840 }
00841
00842 if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
00843 return -1;
00844 if(s->obmc && !s->mb_intra){
00845 if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
00846 preview_obmc(s);
00847 }
00848 end:
00849
00850
00851 {
00852 int v= show_bits(&s->gb, 16);
00853
00854 if (get_bits_left(&s->gb) < 16) {
00855 v >>= 16 - get_bits_left(&s->gb);
00856 }
00857
00858 if(v==0)
00859 return SLICE_END;
00860 }
00861
00862 return SLICE_OK;
00863 }
00864
00865
00866 int ff_h263_decode_picture_header(MpegEncContext *s)
00867 {
00868 int format, width, height, i;
00869 uint32_t startcode;
00870
00871 align_get_bits(&s->gb);
00872
00873 startcode= get_bits(&s->gb, 22-8);
00874
00875 for(i= get_bits_left(&s->gb); i>24; i-=8) {
00876 startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
00877
00878 if(startcode == 0x20)
00879 break;
00880 }
00881
00882 if (startcode != 0x20) {
00883 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
00884 return -1;
00885 }
00886
00887 i = get_bits(&s->gb, 8);
00888 if( (s->picture_number&~0xFF)+i < s->picture_number)
00889 i+= 256;
00890 s->current_picture_ptr->f.pts =
00891 s->picture_number= (s->picture_number&~0xFF) + i;
00892
00893
00894 if (get_bits1(&s->gb) != 1) {
00895
00896 av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
00897 return -1;
00898 }
00899 if (get_bits1(&s->gb) != 0) {
00900 av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
00901 return -1;
00902 }
00903 skip_bits1(&s->gb);
00904 skip_bits1(&s->gb);
00905 skip_bits1(&s->gb);
00906
00907 format = get_bits(&s->gb, 3);
00908
00909
00910
00911
00912
00913
00914
00915 if (format != 7 && format != 6) {
00916 s->h263_plus = 0;
00917
00918 width = ff_h263_format[format][0];
00919 height = ff_h263_format[format][1];
00920 if (!width)
00921 return -1;
00922
00923 s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
00924
00925 s->h263_long_vectors = get_bits1(&s->gb);
00926
00927 if (get_bits1(&s->gb) != 0) {
00928 av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
00929 return -1;
00930 }
00931 s->obmc= get_bits1(&s->gb);
00932 s->unrestricted_mv = s->h263_long_vectors || s->obmc;
00933
00934 s->pb_frame = get_bits1(&s->gb);
00935 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
00936 skip_bits1(&s->gb);
00937
00938 s->width = width;
00939 s->height = height;
00940 s->avctx->sample_aspect_ratio= (AVRational){12,11};
00941 s->avctx->time_base= (AVRational){1001, 30000};
00942 } else {
00943 int ufep;
00944
00945
00946 s->h263_plus = 1;
00947 ufep = get_bits(&s->gb, 3);
00948
00949
00950 if (ufep == 1) {
00951
00952 format = get_bits(&s->gb, 3);
00953 av_dlog(s->avctx, "ufep=1, format: %d\n", format);
00954 s->custom_pcf= get_bits1(&s->gb);
00955 s->umvplus = get_bits1(&s->gb);
00956 if (get_bits1(&s->gb) != 0) {
00957 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
00958 }
00959 s->obmc= get_bits1(&s->gb);
00960 s->h263_aic = get_bits1(&s->gb);
00961 s->loop_filter= get_bits1(&s->gb);
00962 s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
00963 if(s->avctx->lowres)
00964 s->loop_filter = 0;
00965
00966 s->h263_slice_structured= get_bits1(&s->gb);
00967 if (get_bits1(&s->gb) != 0) {
00968 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
00969 }
00970 if (get_bits1(&s->gb) != 0) {
00971 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
00972 }
00973 s->alt_inter_vlc= get_bits1(&s->gb);
00974 s->modified_quant= get_bits1(&s->gb);
00975 if(s->modified_quant)
00976 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
00977
00978 skip_bits(&s->gb, 1);
00979
00980 skip_bits(&s->gb, 3);
00981 } else if (ufep != 0) {
00982 av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
00983 return -1;
00984 }
00985
00986
00987 s->pict_type = get_bits(&s->gb, 3);
00988 switch(s->pict_type){
00989 case 0: s->pict_type= AV_PICTURE_TYPE_I;break;
00990 case 1: s->pict_type= AV_PICTURE_TYPE_P;break;
00991 case 2: s->pict_type= AV_PICTURE_TYPE_P;s->pb_frame = 3;break;
00992 case 3: s->pict_type= AV_PICTURE_TYPE_B;break;
00993 case 7: s->pict_type= AV_PICTURE_TYPE_I;break;
00994 default:
00995 return -1;
00996 }
00997 skip_bits(&s->gb, 2);
00998 s->no_rounding = get_bits1(&s->gb);
00999 skip_bits(&s->gb, 4);
01000
01001
01002 if (ufep) {
01003 if (format == 6) {
01004
01005 s->aspect_ratio_info = get_bits(&s->gb, 4);
01006 av_dlog(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016 width = (get_bits(&s->gb, 9) + 1) * 4;
01017 skip_bits1(&s->gb);
01018 height = get_bits(&s->gb, 9) * 4;
01019 av_dlog(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
01020 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
01021
01022 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
01023 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
01024 }else{
01025 s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
01026 }
01027 } else {
01028 width = ff_h263_format[format][0];
01029 height = ff_h263_format[format][1];
01030 s->avctx->sample_aspect_ratio= (AVRational){12,11};
01031 }
01032 if ((width == 0) || (height == 0))
01033 return -1;
01034 s->width = width;
01035 s->height = height;
01036
01037 if(s->custom_pcf){
01038 int gcd;
01039 s->avctx->time_base.den= 1800000;
01040 s->avctx->time_base.num= 1000 + get_bits1(&s->gb);
01041 s->avctx->time_base.num*= get_bits(&s->gb, 7);
01042 if(s->avctx->time_base.num == 0){
01043 av_log(s, AV_LOG_ERROR, "zero framerate\n");
01044 return -1;
01045 }
01046 gcd= av_gcd(s->avctx->time_base.den, s->avctx->time_base.num);
01047 s->avctx->time_base.den /= gcd;
01048 s->avctx->time_base.num /= gcd;
01049 }else{
01050 s->avctx->time_base= (AVRational){1001, 30000};
01051 }
01052 }
01053
01054 if(s->custom_pcf){
01055 skip_bits(&s->gb, 2);
01056 }
01057
01058 if (ufep) {
01059 if (s->umvplus) {
01060 if(get_bits1(&s->gb)==0)
01061 skip_bits1(&s->gb);
01062 }
01063 if(s->h263_slice_structured){
01064 if (get_bits1(&s->gb) != 0) {
01065 av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
01066 }
01067 if (get_bits1(&s->gb) != 0) {
01068 av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
01069 }
01070 }
01071 }
01072
01073 s->qscale = get_bits(&s->gb, 5);
01074 }
01075
01076 s->mb_width = (s->width + 15) / 16;
01077 s->mb_height = (s->height + 15) / 16;
01078 s->mb_num = s->mb_width * s->mb_height;
01079
01080 if (s->pb_frame) {
01081 skip_bits(&s->gb, 3);
01082 if (s->custom_pcf)
01083 skip_bits(&s->gb, 2);
01084 skip_bits(&s->gb, 2);
01085 }
01086
01087 if (s->pict_type!=AV_PICTURE_TYPE_B) {
01088 s->time = s->picture_number;
01089 s->pp_time = s->time - s->last_non_b_time;
01090 s->last_non_b_time = s->time;
01091 }else{
01092 s->time = s->picture_number;
01093 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
01094 if (s->pp_time <=s->pb_time ||
01095 s->pp_time <= s->pp_time - s->pb_time ||
01096 s->pp_time <= 0){
01097 s->pp_time = 2;
01098 s->pb_time = 1;
01099 }
01100 ff_mpeg4_init_direct_mv(s);
01101 }
01102
01103
01104 while (get_bits1(&s->gb) != 0) {
01105 skip_bits(&s->gb, 8);
01106 }
01107
01108 if(s->h263_slice_structured){
01109 if (get_bits1(&s->gb) != 1) {
01110 av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
01111 return -1;
01112 }
01113
01114 ff_h263_decode_mba(s);
01115
01116 if (get_bits1(&s->gb) != 1) {
01117 av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
01118 return -1;
01119 }
01120 }
01121 s->f_code = 1;
01122
01123 if(s->h263_aic){
01124 s->y_dc_scale_table=
01125 s->c_dc_scale_table= ff_aic_dc_scale_table;
01126 }else{
01127 s->y_dc_scale_table=
01128 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
01129 }
01130
01131 ff_h263_show_pict_info(s);
01132 if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO") && get_bits_left(&s->gb) >= 85 + 13*3*16 + 50){
01133 int i,j;
01134 for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
01135 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01136 for(i=0; i<13; i++){
01137 for(j=0; j<3; j++){
01138 int v= get_bits(&s->gb, 8);
01139 v |= get_sbits(&s->gb, 8)<<8;
01140 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
01141 }
01142 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01143 }
01144 for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
01145 }
01146
01147 return 0;
01148 }