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