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