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
00031 #include <limits.h>
00032
00033 #include "libavutil/mathematics.h"
00034 #include "libavutil/imgutils.h"
00035 #include "dsputil.h"
00036 #include "avcodec.h"
00037 #include "mpegvideo.h"
00038 #include "h263.h"
00039 #include "mathops.h"
00040 #include "unary.h"
00041 #include "flv.h"
00042 #include "mpeg4video.h"
00043
00044
00045
00046
00047
00048
00049
00050
00051 #define MV_VLC_BITS 9
00052 #define H263_MBTYPE_B_VLC_BITS 6
00053 #define CBPC_B_VLC_BITS 3
00054
00055 static const int h263_mb_type_b_map[15]= {
00056 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
00057 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
00058 MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
00059 MB_TYPE_L0 | MB_TYPE_16x16,
00060 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_16x16,
00061 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00062 MB_TYPE_L1 | MB_TYPE_16x16,
00063 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_16x16,
00064 MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00065 MB_TYPE_L0L1 | MB_TYPE_16x16,
00066 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_16x16,
00067 MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
00068 0,
00069 MB_TYPE_INTRA4x4 | MB_TYPE_CBP,
00070 MB_TYPE_INTRA4x4 | MB_TYPE_CBP | MB_TYPE_QUANT,
00071 };
00072
00073 void ff_h263_show_pict_info(MpegEncContext *s){
00074 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
00075 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",
00076 s->qscale, av_get_picture_type_char(s->pict_type),
00077 s->gb.size_in_bits, 1-s->no_rounding,
00078 s->obmc ? " AP" : "",
00079 s->umvplus ? " UMV" : "",
00080 s->h263_long_vectors ? " LONG" : "",
00081 s->h263_plus ? " +" : "",
00082 s->h263_aic ? " AIC" : "",
00083 s->alt_inter_vlc ? " AIV" : "",
00084 s->modified_quant ? " MQ" : "",
00085 s->loop_filter ? " LOOP" : "",
00086 s->h263_slice_structured ? " SS" : "",
00087 s->avctx->time_base.den, s->avctx->time_base.num
00088 );
00089 }
00090 }
00091
00092
00093
00094
00095 VLC ff_h263_intra_MCBPC_vlc;
00096 VLC ff_h263_inter_MCBPC_vlc;
00097 VLC ff_h263_cbpy_vlc;
00098 static VLC mv_vlc;
00099 static VLC h263_mbtype_b_vlc;
00100 static VLC cbpc_b_vlc;
00101
00102
00103
00104
00105 void ff_h263_decode_init_vlc(MpegEncContext *s)
00106 {
00107 static int done = 0;
00108
00109 if (!done) {
00110 done = 1;
00111
00112 INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
00113 ff_h263_intra_MCBPC_bits, 1, 1,
00114 ff_h263_intra_MCBPC_code, 1, 1, 72);
00115 INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
00116 ff_h263_inter_MCBPC_bits, 1, 1,
00117 ff_h263_inter_MCBPC_code, 1, 1, 198);
00118 INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
00119 &ff_h263_cbpy_tab[0][1], 2, 1,
00120 &ff_h263_cbpy_tab[0][0], 2, 1, 64);
00121 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
00122 &ff_mvtab[0][1], 2, 1,
00123 &ff_mvtab[0][0], 2, 1, 538);
00124 ff_init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
00125 ff_init_rl(&ff_rl_intra_aic, ff_h263_static_rl_table_store[1]);
00126 INIT_VLC_RL(ff_h263_rl_inter, 554);
00127 INIT_VLC_RL(ff_rl_intra_aic, 554);
00128 INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
00129 &ff_h263_mbtype_b_tab[0][1], 2, 1,
00130 &ff_h263_mbtype_b_tab[0][0], 2, 1, 80);
00131 INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
00132 &ff_cbpc_b_tab[0][1], 2, 1,
00133 &ff_cbpc_b_tab[0][0], 2, 1, 8);
00134 }
00135 }
00136
00137 int ff_h263_decode_mba(MpegEncContext *s)
00138 {
00139 int i, mb_pos;
00140
00141 for(i=0; i<6; i++){
00142 if(s->mb_num-1 <= ff_mba_max[i]) break;
00143 }
00144 mb_pos= get_bits(&s->gb, ff_mba_length[i]);
00145 s->mb_x= mb_pos % s->mb_width;
00146 s->mb_y= mb_pos / s->mb_width;
00147
00148 return mb_pos;
00149 }
00150
00155 static int h263_decode_gob_header(MpegEncContext *s)
00156 {
00157 unsigned int val, gob_number;
00158 int left;
00159
00160
00161 val = show_bits(&s->gb, 16);
00162 if(val)
00163 return -1;
00164
00165
00166 skip_bits(&s->gb, 16);
00167 left= get_bits_left(&s->gb);
00168
00169 for(;left>13; left--){
00170 if(get_bits1(&s->gb)) break;
00171 }
00172 if(left<=13)
00173 return -1;
00174
00175 if(s->h263_slice_structured){
00176 if(get_bits1(&s->gb)==0)
00177 return -1;
00178
00179 ff_h263_decode_mba(s);
00180
00181 if(s->mb_num > 1583)
00182 if(get_bits1(&s->gb)==0)
00183 return -1;
00184
00185 s->qscale = get_bits(&s->gb, 5);
00186 if(get_bits1(&s->gb)==0)
00187 return -1;
00188 skip_bits(&s->gb, 2);
00189 }else{
00190 gob_number = get_bits(&s->gb, 5);
00191 s->mb_x= 0;
00192 s->mb_y= s->gob_index* gob_number;
00193 skip_bits(&s->gb, 2);
00194 s->qscale = get_bits(&s->gb, 5);
00195 }
00196
00197 if(s->mb_y >= s->mb_height)
00198 return -1;
00199
00200 if(s->qscale==0)
00201 return -1;
00202
00203 return 0;
00204 }
00205
00212 const uint8_t *ff_h263_find_resync_marker(const uint8_t *restrict p, const uint8_t * restrict end)
00213 {
00214 assert(p < end);
00215
00216 end-=2;
00217 p++;
00218 for(;p<end; p+=2){
00219 if(!*p){
00220 if (!p[-1] && p[1]) return p - 1;
00221 else if(!p[ 1] && p[2]) return p;
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==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==CODEC_ID_MPEG4)
00242 ret= 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==CODEC_ID_MPEG4)
00259 ret= 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 assert(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 == 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 = 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)
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 == 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
00570
00571
00572
00573 mbi = s->mb_intra;
00574 s->mb_intra = 0;
00575 for (i = 0; i < 6; i++) {
00576 if (h263_decode_block(s, dblock, i, cbp&32) < 0)
00577 return -1;
00578 cbp+=cbp;
00579 }
00580 s->mb_intra = mbi;
00581 return 0;
00582 }
00583
00584 static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
00585 {
00586 int c, mv = 1;
00587
00588 if (pb_frame < 3) {
00589 c = get_bits1(gb);
00590 if (pb_frame == 2 && c)
00591 mv = !get_bits1(gb);
00592 } else {
00593 mv = get_unary(gb, 0, 4) + 1;
00594 c = mv & 1;
00595 mv = !!(mv & 2);
00596 }
00597 if(c)
00598 *cbpb = get_bits(gb, 6);
00599 return mv;
00600 }
00601
00602 int ff_h263_decode_mb(MpegEncContext *s,
00603 DCTELEM block[6][64])
00604 {
00605 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
00606 int16_t *mot_val;
00607 const int xy= s->mb_x + s->mb_y * s->mb_stride;
00608 int cbpb = 0, pb_mv_count = 0;
00609
00610 assert(!s->h263_pred);
00611
00612 if (s->pict_type == AV_PICTURE_TYPE_P) {
00613 do{
00614 if (get_bits1(&s->gb)) {
00615
00616 s->mb_intra = 0;
00617 for(i=0;i<6;i++)
00618 s->block_last_index[i] = -1;
00619 s->mv_dir = MV_DIR_FORWARD;
00620 s->mv_type = MV_TYPE_16X16;
00621 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00622 s->mv[0][0][0] = 0;
00623 s->mv[0][0][1] = 0;
00624 s->mb_skipped = !(s->obmc | s->loop_filter);
00625 goto end;
00626 }
00627 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
00628 if (cbpc < 0){
00629 av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
00630 return -1;
00631 }
00632 }while(cbpc == 20);
00633
00634 s->dsp.clear_blocks(s->block[0]);
00635
00636 dquant = cbpc & 8;
00637 s->mb_intra = ((cbpc & 4) != 0);
00638 if (s->mb_intra) goto intra;
00639
00640 if(s->pb_frame && get_bits1(&s->gb))
00641 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
00642 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00643
00644 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
00645 cbpy ^= 0xF;
00646
00647 cbp = (cbpc & 3) | (cbpy << 2);
00648 if (dquant) {
00649 h263_decode_dquant(s);
00650 }
00651
00652 s->mv_dir = MV_DIR_FORWARD;
00653 if ((cbpc & 16) == 0) {
00654 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
00655
00656 s->mv_type = MV_TYPE_16X16;
00657 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
00658 if (s->umvplus)
00659 mx = h263p_decode_umotion(s, pred_x);
00660 else
00661 mx = ff_h263_decode_motion(s, pred_x, 1);
00662
00663 if (mx >= 0xffff)
00664 return -1;
00665
00666 if (s->umvplus)
00667 my = h263p_decode_umotion(s, pred_y);
00668 else
00669 my = ff_h263_decode_motion(s, pred_y, 1);
00670
00671 if (my >= 0xffff)
00672 return -1;
00673 s->mv[0][0][0] = mx;
00674 s->mv[0][0][1] = my;
00675
00676 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
00677 skip_bits1(&s->gb);
00678 } else {
00679 s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
00680 s->mv_type = MV_TYPE_8X8;
00681 for(i=0;i<4;i++) {
00682 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
00683 if (s->umvplus)
00684 mx = h263p_decode_umotion(s, pred_x);
00685 else
00686 mx = ff_h263_decode_motion(s, pred_x, 1);
00687 if (mx >= 0xffff)
00688 return -1;
00689
00690 if (s->umvplus)
00691 my = h263p_decode_umotion(s, pred_y);
00692 else
00693 my = ff_h263_decode_motion(s, pred_y, 1);
00694 if (my >= 0xffff)
00695 return -1;
00696 s->mv[0][i][0] = mx;
00697 s->mv[0][i][1] = my;
00698 if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
00699 skip_bits1(&s->gb);
00700 mot_val[0] = mx;
00701 mot_val[1] = my;
00702 }
00703 }
00704 } else if(s->pict_type==AV_PICTURE_TYPE_B) {
00705 int mb_type;
00706 const int stride= s->b8_stride;
00707 int16_t *mot_val0 = s->current_picture.f.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
00708 int16_t *mot_val1 = s->current_picture.f.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
00709
00710
00711
00712 mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
00713 mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
00714 mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
00715 mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
00716
00717 do{
00718 mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
00719 if (mb_type < 0){
00720 av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
00721 return -1;
00722 }
00723
00724 mb_type= h263_mb_type_b_map[ mb_type ];
00725 }while(!mb_type);
00726
00727 s->mb_intra = IS_INTRA(mb_type);
00728 if(HAS_CBP(mb_type)){
00729 s->dsp.clear_blocks(s->block[0]);
00730 cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
00731 if(s->mb_intra){
00732 dquant = IS_QUANT(mb_type);
00733 goto intra;
00734 }
00735
00736 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00737
00738 if (cbpy < 0){
00739 av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
00740 return -1;
00741 }
00742
00743 if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
00744 cbpy ^= 0xF;
00745
00746 cbp = (cbpc & 3) | (cbpy << 2);
00747 }else
00748 cbp=0;
00749
00750 assert(!s->mb_intra);
00751
00752 if(IS_QUANT(mb_type)){
00753 h263_decode_dquant(s);
00754 }
00755
00756 if(IS_DIRECT(mb_type)){
00757 if (!s->pp_time)
00758 return AVERROR_INVALIDDATA;
00759 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
00760 mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
00761 }else{
00762 s->mv_dir = 0;
00763 s->mv_type= MV_TYPE_16X16;
00764
00765
00766 if(USES_LIST(mb_type, 0)){
00767 int16_t *mot_val= ff_h263_pred_motion(s, 0, 0, &mx, &my);
00768 s->mv_dir = MV_DIR_FORWARD;
00769
00770 mx = ff_h263_decode_motion(s, mx, 1);
00771 my = ff_h263_decode_motion(s, my, 1);
00772
00773 s->mv[0][0][0] = mx;
00774 s->mv[0][0][1] = my;
00775 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
00776 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
00777 }
00778
00779 if(USES_LIST(mb_type, 1)){
00780 int16_t *mot_val= ff_h263_pred_motion(s, 0, 1, &mx, &my);
00781 s->mv_dir |= MV_DIR_BACKWARD;
00782
00783 mx = ff_h263_decode_motion(s, mx, 1);
00784 my = ff_h263_decode_motion(s, my, 1);
00785
00786 s->mv[1][0][0] = mx;
00787 s->mv[1][0][1] = my;
00788 mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
00789 mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
00790 }
00791 }
00792
00793 s->current_picture.f.mb_type[xy] = mb_type;
00794 } else {
00795 do{
00796 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
00797 if (cbpc < 0){
00798 av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
00799 return -1;
00800 }
00801 }while(cbpc == 8);
00802
00803 s->dsp.clear_blocks(s->block[0]);
00804
00805 dquant = cbpc & 4;
00806 s->mb_intra = 1;
00807 intra:
00808 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
00809 if (s->h263_aic) {
00810 s->ac_pred = get_bits1(&s->gb);
00811 if(s->ac_pred){
00812 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
00813
00814 s->h263_aic_dir = get_bits1(&s->gb);
00815 }
00816 }else
00817 s->ac_pred = 0;
00818
00819 if(s->pb_frame && get_bits1(&s->gb))
00820 pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
00821 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
00822 if(cbpy<0){
00823 av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
00824 return -1;
00825 }
00826 cbp = (cbpc & 3) | (cbpy << 2);
00827 if (dquant) {
00828 h263_decode_dquant(s);
00829 }
00830
00831 pb_mv_count += !!s->pb_frame;
00832 }
00833
00834 while(pb_mv_count--){
00835 ff_h263_decode_motion(s, 0, 1);
00836 ff_h263_decode_motion(s, 0, 1);
00837 }
00838
00839
00840 for (i = 0; i < 6; i++) {
00841 if (h263_decode_block(s, block[i], i, cbp&32) < 0)
00842 return -1;
00843 cbp+=cbp;
00844 }
00845
00846 if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
00847 return -1;
00848 if(s->obmc && !s->mb_intra){
00849 if(s->pict_type == AV_PICTURE_TYPE_P && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
00850 preview_obmc(s);
00851 }
00852 end:
00853
00854
00855 {
00856 int v= show_bits(&s->gb, 16);
00857
00858 if (get_bits_left(&s->gb) < 16) {
00859 v >>= 16 - get_bits_left(&s->gb);
00860 }
00861
00862 if(v==0)
00863 return SLICE_END;
00864 }
00865
00866 return SLICE_OK;
00867 }
00868
00869
00870 int ff_h263_decode_picture_header(MpegEncContext *s)
00871 {
00872 int format, width, height, i, ret;
00873 uint32_t startcode;
00874
00875 align_get_bits(&s->gb);
00876
00877 startcode= get_bits(&s->gb, 22-8);
00878
00879 for(i= get_bits_left(&s->gb); i>24; i-=8) {
00880 startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
00881
00882 if(startcode == 0x20)
00883 break;
00884 }
00885
00886 if (startcode != 0x20) {
00887 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
00888 return -1;
00889 }
00890
00891 i = get_bits(&s->gb, 8);
00892 if( (s->picture_number&~0xFF)+i < s->picture_number)
00893 i+= 256;
00894 s->current_picture_ptr->f.pts =
00895 s->picture_number= (s->picture_number&~0xFF) + i;
00896
00897
00898 if (get_bits1(&s->gb) != 1) {
00899
00900 av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
00901 return -1;
00902 }
00903 if (get_bits1(&s->gb) != 0) {
00904 av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
00905 return -1;
00906 }
00907 skip_bits1(&s->gb);
00908 skip_bits1(&s->gb);
00909 skip_bits1(&s->gb);
00910
00911 format = get_bits(&s->gb, 3);
00912
00913
00914
00915
00916
00917
00918
00919 if (format != 7 && format != 6) {
00920 s->h263_plus = 0;
00921
00922 width = ff_h263_format[format][0];
00923 height = ff_h263_format[format][1];
00924
00925 s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
00926
00927 s->h263_long_vectors = get_bits1(&s->gb);
00928
00929 if (get_bits1(&s->gb) != 0) {
00930 av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
00931 return -1;
00932 }
00933 s->obmc= get_bits1(&s->gb);
00934 s->unrestricted_mv = s->h263_long_vectors || s->obmc;
00935
00936 s->pb_frame = get_bits1(&s->gb);
00937 s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
00938 skip_bits1(&s->gb);
00939
00940 s->width = width;
00941 s->height = height;
00942 s->avctx->sample_aspect_ratio= (AVRational){12,11};
00943 s->avctx->time_base= (AVRational){1001, 30000};
00944 } else {
00945 int ufep;
00946
00947
00948 s->h263_plus = 1;
00949 ufep = get_bits(&s->gb, 3);
00950
00951
00952 if (ufep == 1) {
00953
00954 format = get_bits(&s->gb, 3);
00955 av_dlog(s->avctx, "ufep=1, format: %d\n", format);
00956 s->custom_pcf= get_bits1(&s->gb);
00957 s->umvplus = get_bits1(&s->gb);
00958 if (get_bits1(&s->gb) != 0) {
00959 av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
00960 }
00961 s->obmc= get_bits1(&s->gb);
00962 s->h263_aic = get_bits1(&s->gb);
00963 s->loop_filter= get_bits1(&s->gb);
00964 s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
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 if ((ret = av_image_check_size(s->width, s->height, 0, s)) < 0)
01077 return ret;
01078
01079 s->mb_width = (s->width + 15) / 16;
01080 s->mb_height = (s->height + 15) / 16;
01081 s->mb_num = s->mb_width * s->mb_height;
01082
01083 if (s->pb_frame) {
01084 skip_bits(&s->gb, 3);
01085 if (s->custom_pcf)
01086 skip_bits(&s->gb, 2);
01087 skip_bits(&s->gb, 2);
01088 }
01089
01090
01091 while (get_bits1(&s->gb) != 0) {
01092 skip_bits(&s->gb, 8);
01093 }
01094
01095 if(s->h263_slice_structured){
01096 if (get_bits1(&s->gb) != 1) {
01097 av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
01098 return -1;
01099 }
01100
01101 ff_h263_decode_mba(s);
01102
01103 if (get_bits1(&s->gb) != 1) {
01104 av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
01105 return -1;
01106 }
01107 }
01108 s->f_code = 1;
01109
01110 if(s->h263_aic){
01111 s->y_dc_scale_table=
01112 s->c_dc_scale_table= ff_aic_dc_scale_table;
01113 }else{
01114 s->y_dc_scale_table=
01115 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
01116 }
01117
01118 ff_h263_show_pict_info(s);
01119 if (s->pict_type == AV_PICTURE_TYPE_I && s->codec_tag == AV_RL32("ZYGO")){
01120 int i,j;
01121 for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
01122 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01123 for(i=0; i<13; i++){
01124 for(j=0; j<3; j++){
01125 int v= get_bits(&s->gb, 8);
01126 v |= get_sbits(&s->gb, 8)<<8;
01127 av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
01128 }
01129 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01130 }
01131 for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
01132 }
01133
01134 return 0;
01135 }