00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "libavutil/avassert.h"
00029 #include "dsputil.h"
00030 #include "avcodec.h"
00031 #include "mpegvideo.h"
00032 #include "h263.h"
00033 #include "h261.h"
00034 #include "h261data.h"
00035
00036 #define H261_MBA_VLC_BITS 9
00037 #define H261_MTYPE_VLC_BITS 6
00038 #define H261_MV_VLC_BITS 7
00039 #define H261_CBP_VLC_BITS 9
00040 #define TCOEFF_VLC_BITS 9
00041 #define MBA_STUFFING 33
00042 #define MBA_STARTCODE 34
00043
00044 extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
00045
00046 static VLC h261_mba_vlc;
00047 static VLC h261_mtype_vlc;
00048 static VLC h261_mv_vlc;
00049 static VLC h261_cbp_vlc;
00050
00051 static int h261_decode_block(H261Context * h, DCTELEM * block, int n, int coded);
00052
00053 static av_cold void h261_decode_init_vlc(H261Context *h){
00054 static int done = 0;
00055
00056 if(!done){
00057 done = 1;
00058 INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
00059 ff_h261_mba_bits, 1, 1,
00060 ff_h261_mba_code, 1, 1, 662);
00061 INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
00062 ff_h261_mtype_bits, 1, 1,
00063 ff_h261_mtype_code, 1, 1, 80);
00064 INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
00065 &ff_h261_mv_tab[0][1], 2, 1,
00066 &ff_h261_mv_tab[0][0], 2, 1, 144);
00067 INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
00068 &ff_h261_cbp_tab[0][1], 2, 1,
00069 &ff_h261_cbp_tab[0][0], 2, 1, 512);
00070 ff_init_rl(&ff_h261_rl_tcoeff, ff_h261_rl_table_store);
00071 INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
00072 }
00073 }
00074
00075 static av_cold int h261_decode_init(AVCodecContext *avctx){
00076 H261Context *h= avctx->priv_data;
00077 MpegEncContext * const s = &h->s;
00078
00079
00080 ff_MPV_decode_defaults(s);
00081 s->avctx = avctx;
00082
00083 s->width = s->avctx->coded_width;
00084 s->height = s->avctx->coded_height;
00085 s->codec_id = s->avctx->codec->id;
00086
00087 s->out_format = FMT_H261;
00088 s->low_delay= 1;
00089 avctx->pix_fmt= PIX_FMT_YUV420P;
00090
00091 s->codec_id= avctx->codec->id;
00092
00093 h261_decode_init_vlc(h);
00094
00095 h->gob_start_code_skipped = 0;
00096
00097 return 0;
00098 }
00099
00104 static int h261_decode_gob_header(H261Context *h){
00105 unsigned int val;
00106 MpegEncContext * const s = &h->s;
00107
00108 if ( !h->gob_start_code_skipped ){
00109
00110 val = show_bits(&s->gb, 15);
00111 if(val)
00112 return -1;
00113
00114
00115 skip_bits(&s->gb, 16);
00116 }
00117
00118 h->gob_start_code_skipped = 0;
00119
00120 h->gob_number = get_bits(&s->gb, 4);
00121 s->qscale = get_bits(&s->gb, 5);
00122
00123
00124 if (s->mb_height==18){
00125 if ((h->gob_number<=0) || (h->gob_number>12))
00126 return -1;
00127 }
00128 else{
00129 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
00130 return -1;
00131 }
00132
00133
00134 while (get_bits1(&s->gb) != 0) {
00135 skip_bits(&s->gb, 8);
00136 }
00137
00138 if(s->qscale==0) {
00139 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
00140 if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
00141 return -1;
00142 }
00143
00144
00145
00146
00147 h->current_mba = 0;
00148 h->mba_diff = 0;
00149
00150 return 0;
00151 }
00152
00157 static int ff_h261_resync(H261Context *h){
00158 MpegEncContext * const s = &h->s;
00159 int left, ret;
00160
00161 if ( h->gob_start_code_skipped ){
00162 ret= h261_decode_gob_header(h);
00163 if(ret>=0)
00164 return 0;
00165 }
00166 else{
00167 if(show_bits(&s->gb, 15)==0){
00168 ret= h261_decode_gob_header(h);
00169 if(ret>=0)
00170 return 0;
00171 }
00172
00173 s->gb= s->last_resync_gb;
00174 align_get_bits(&s->gb);
00175 left= get_bits_left(&s->gb);
00176
00177 for(;left>15+1+4+5; left-=8){
00178 if(show_bits(&s->gb, 15)==0){
00179 GetBitContext bak= s->gb;
00180
00181 ret= h261_decode_gob_header(h);
00182 if(ret>=0)
00183 return 0;
00184
00185 s->gb= bak;
00186 }
00187 skip_bits(&s->gb, 8);
00188 }
00189 }
00190
00191 return -1;
00192 }
00193
00198 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
00199 {
00200 MpegEncContext * const s = &h->s;
00201 int i;
00202
00203 s->mb_intra = 0;
00204
00205 for(i=mba1; i<mba2; i++){
00206 int j, xy;
00207
00208 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
00209 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
00210 xy = s->mb_x + s->mb_y * s->mb_stride;
00211 ff_init_block_index(s);
00212 ff_update_block_index(s);
00213
00214 for(j=0;j<6;j++)
00215 s->block_last_index[j] = -1;
00216
00217 s->mv_dir = MV_DIR_FORWARD;
00218 s->mv_type = MV_TYPE_16X16;
00219 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
00220 s->mv[0][0][0] = 0;
00221 s->mv[0][0][1] = 0;
00222 s->mb_skipped = 1;
00223 h->mtype &= ~MB_TYPE_H261_FIL;
00224
00225 ff_MPV_decode_mb(s, s->block);
00226 }
00227
00228 return 0;
00229 }
00230
00231 static int decode_mv_component(GetBitContext *gb, int v){
00232 static const int mvmap[17] = {
00233 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
00234 };
00235 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
00236
00237
00238 if ( mv_diff < 0 )
00239 return v;
00240
00241 mv_diff = mvmap[mv_diff];
00242
00243 if(mv_diff && !get_bits1(gb))
00244 mv_diff= -mv_diff;
00245
00246 v += mv_diff;
00247 if (v <=-16) v+= 32;
00248 else if(v >= 16) v-= 32;
00249
00250 return v;
00251 }
00252
00253 static int h261_decode_mb(H261Context *h){
00254 MpegEncContext * const s = &h->s;
00255 int i, cbp, xy;
00256
00257 cbp = 63;
00258
00259 do{
00260 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
00261
00262
00263
00264 if (h->mba_diff == MBA_STARTCODE){
00265 h->gob_start_code_skipped = 1;
00266 return SLICE_END;
00267 }
00268 }
00269 while( h->mba_diff == MBA_STUFFING );
00270
00271 if ( h->mba_diff < 0 ){
00272 if (get_bits_left(&s->gb) <= 7)
00273 return SLICE_END;
00274
00275 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
00276 return SLICE_ERROR;
00277 }
00278
00279 h->mba_diff += 1;
00280 h->current_mba += h->mba_diff;
00281
00282 if ( h->current_mba > MBA_STUFFING )
00283 return SLICE_ERROR;
00284
00285 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
00286 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
00287 xy = s->mb_x + s->mb_y * s->mb_stride;
00288 ff_init_block_index(s);
00289 ff_update_block_index(s);
00290
00291
00292 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
00293 if (h->mtype < 0) {
00294 av_log(s->avctx, AV_LOG_ERROR, "illegal mtype %d\n", h->mtype);
00295 return SLICE_ERROR;
00296 }
00297 h->mtype = ff_h261_mtype_map[h->mtype];
00298
00299
00300 if ( IS_QUANT ( h->mtype ) ){
00301 ff_set_qscale(s, get_bits(&s->gb, 5));
00302 }
00303
00304 s->mb_intra = IS_INTRA4x4(h->mtype);
00305
00306
00307 if ( IS_16X16 ( h->mtype ) ){
00308
00309
00310
00311
00312
00313
00314 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
00315 ( h->mba_diff != 1))
00316 {
00317 h->current_mv_x = 0;
00318 h->current_mv_y = 0;
00319 }
00320
00321 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
00322 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
00323 }else{
00324 h->current_mv_x = 0;
00325 h->current_mv_y = 0;
00326 }
00327
00328
00329 if ( HAS_CBP( h->mtype ) ){
00330 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
00331 }
00332
00333 if(s->mb_intra){
00334 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
00335 goto intra;
00336 }
00337
00338
00339 s->mv_dir = MV_DIR_FORWARD;
00340 s->mv_type = MV_TYPE_16X16;
00341 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
00342 s->mv[0][0][0] = h->current_mv_x * 2;
00343 s->mv[0][0][1] = h->current_mv_y * 2;
00344
00345 intra:
00346
00347 if(s->mb_intra || HAS_CBP(h->mtype)){
00348 s->dsp.clear_blocks(s->block[0]);
00349 for (i = 0; i < 6; i++) {
00350 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
00351 return SLICE_ERROR;
00352 }
00353 cbp+=cbp;
00354 }
00355 }else{
00356 for (i = 0; i < 6; i++)
00357 s->block_last_index[i]= -1;
00358 }
00359
00360 ff_MPV_decode_mb(s, s->block);
00361
00362 return SLICE_OK;
00363 }
00364
00369 static int h261_decode_block(H261Context * h, DCTELEM * block,
00370 int n, int coded)
00371 {
00372 MpegEncContext * const s = &h->s;
00373 int code, level, i, j, run;
00374 RLTable *rl = &ff_h261_rl_tcoeff;
00375 const uint8_t *scan_table;
00376
00377
00378
00379
00380
00381
00382 scan_table = s->intra_scantable.permutated;
00383 if (s->mb_intra){
00384
00385 level = get_bits(&s->gb, 8);
00386
00387 if((level&0x7F) == 0){
00388 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
00389 return -1;
00390 }
00391
00392 if (level == 255)
00393 level = 128;
00394 block[0] = level;
00395 i = 1;
00396 }else if(coded){
00397
00398
00399
00400
00401 int check = show_bits(&s->gb, 2);
00402 i = 0;
00403 if ( check & 0x2 ){
00404 skip_bits(&s->gb, 2);
00405 block[0] = ( check & 0x1 ) ? -1 : 1;
00406 i = 1;
00407 }
00408 }else{
00409 i = 0;
00410 }
00411 if(!coded){
00412 s->block_last_index[n] = i - 1;
00413 return 0;
00414 }
00415 for(;;){
00416 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
00417 if (code < 0){
00418 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
00419 return -1;
00420 }
00421 if (code == rl->n) {
00422
00423
00424 run = get_bits(&s->gb, 6);
00425 level = get_sbits(&s->gb, 8);
00426 }else if(code == 0){
00427 break;
00428 }else{
00429 run = rl->table_run[code];
00430 level = rl->table_level[code];
00431 if (get_bits1(&s->gb))
00432 level = -level;
00433 }
00434 i += run;
00435 if (i >= 64){
00436 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
00437 return -1;
00438 }
00439 j = scan_table[i];
00440 block[j] = level;
00441 i++;
00442 }
00443 s->block_last_index[n] = i-1;
00444 return 0;
00445 }
00446
00451 static int h261_decode_picture_header(H261Context *h){
00452 MpegEncContext * const s = &h->s;
00453 int format, i;
00454 uint32_t startcode= 0;
00455
00456 for(i= get_bits_left(&s->gb); i>24; i-=1){
00457 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
00458
00459 if(startcode == 0x10)
00460 break;
00461 }
00462
00463 if (startcode != 0x10){
00464 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
00465 return -1;
00466 }
00467
00468
00469 i= get_bits(&s->gb, 5);
00470 if(i < (s->picture_number&31))
00471 i += 32;
00472 s->picture_number = (s->picture_number&~31) + i;
00473
00474 s->avctx->time_base= (AVRational){1001, 30000};
00475 s->current_picture.f.pts = s->picture_number;
00476
00477
00478
00479 skip_bits1(&s->gb);
00480 skip_bits1(&s->gb);
00481 skip_bits1(&s->gb);
00482
00483 format = get_bits1(&s->gb);
00484
00485
00486 if (format == 0){
00487 s->width = 176;
00488 s->height = 144;
00489 s->mb_width = 11;
00490 s->mb_height = 9;
00491 }else{
00492 s->width = 352;
00493 s->height = 288;
00494 s->mb_width = 22;
00495 s->mb_height = 18;
00496 }
00497
00498 s->mb_num = s->mb_width * s->mb_height;
00499
00500 skip_bits1(&s->gb);
00501 skip_bits1(&s->gb);
00502
00503
00504 while (get_bits1(&s->gb) != 0){
00505 skip_bits(&s->gb, 8);
00506 }
00507
00508
00509
00510 s->pict_type = AV_PICTURE_TYPE_P;
00511
00512 h->gob_number = 0;
00513 return 0;
00514 }
00515
00516 static int h261_decode_gob(H261Context *h){
00517 MpegEncContext * const s = &h->s;
00518
00519 ff_set_qscale(s, s->qscale);
00520
00521
00522 while(h->current_mba <= MBA_STUFFING)
00523 {
00524 int ret;
00525
00526 ret= h261_decode_mb(h);
00527 if(ret<0){
00528 if(ret==SLICE_END){
00529 h261_decode_mb_skipped(h, h->current_mba, 33);
00530 return 0;
00531 }
00532 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
00533 return -1;
00534 }
00535
00536 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
00537 }
00538
00539 return -1;
00540 }
00541
00545 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
00546 int pos= get_bits_count(&s->gb)>>3;
00547 if(pos==0) pos=1;
00548 if(pos+10>buf_size) pos=buf_size;
00549
00550 return pos;
00551 }
00552
00553 static int h261_decode_frame(AVCodecContext *avctx,
00554 void *data, int *data_size,
00555 AVPacket *avpkt)
00556 {
00557 const uint8_t *buf = avpkt->data;
00558 int buf_size = avpkt->size;
00559 H261Context *h= avctx->priv_data;
00560 MpegEncContext *s = &h->s;
00561 int ret;
00562 AVFrame *pict = data;
00563
00564 av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
00565 av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
00566 s->flags= avctx->flags;
00567 s->flags2= avctx->flags2;
00568
00569 h->gob_start_code_skipped=0;
00570
00571 retry:
00572
00573 init_get_bits(&s->gb, buf, buf_size*8);
00574
00575 if(!s->context_initialized){
00576 if (ff_MPV_common_init(s) < 0)
00577 return -1;
00578 }
00579
00580
00581 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
00582 int i= ff_find_unused_picture(s, 0);
00583 if (i < 0)
00584 return i;
00585 s->current_picture_ptr= &s->picture[i];
00586 }
00587
00588 ret = h261_decode_picture_header(h);
00589
00590
00591 if (ret < 0){
00592 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
00593 return -1;
00594 }
00595
00596 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
00597 ParseContext pc= s->parse_context;
00598 s->parse_context.buffer=0;
00599 ff_MPV_common_end(s);
00600 s->parse_context= pc;
00601 }
00602 if (!s->context_initialized) {
00603 avcodec_set_dimensions(avctx, s->width, s->height);
00604
00605 goto retry;
00606 }
00607
00608
00609 s->current_picture.f.pict_type = s->pict_type;
00610 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
00611
00612 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
00613 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
00614 || avctx->skip_frame >= AVDISCARD_ALL)
00615 return get_consumed_bytes(s, buf_size);
00616
00617 if(ff_MPV_frame_start(s, avctx) < 0)
00618 return -1;
00619
00620 ff_er_frame_start(s);
00621
00622
00623 s->mb_x=0;
00624 s->mb_y=0;
00625
00626 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
00627 if(ff_h261_resync(h)<0)
00628 break;
00629 h261_decode_gob(h);
00630 }
00631 ff_MPV_frame_end(s);
00632
00633 av_assert0(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
00634 av_assert0(s->current_picture.f.pict_type == s->pict_type);
00635
00636 *pict = s->current_picture_ptr->f;
00637 ff_print_debug_info(s, pict);
00638
00639 *data_size = sizeof(AVFrame);
00640
00641 return get_consumed_bytes(s, buf_size);
00642 }
00643
00644 static av_cold int h261_decode_end(AVCodecContext *avctx)
00645 {
00646 H261Context *h= avctx->priv_data;
00647 MpegEncContext *s = &h->s;
00648
00649 ff_MPV_common_end(s);
00650 return 0;
00651 }
00652
00653 AVCodec ff_h261_decoder = {
00654 .name = "h261",
00655 .type = AVMEDIA_TYPE_VIDEO,
00656 .id = AV_CODEC_ID_H261,
00657 .priv_data_size = sizeof(H261Context),
00658 .init = h261_decode_init,
00659 .close = h261_decode_end,
00660 .decode = h261_decode_frame,
00661 .capabilities = CODEC_CAP_DR1,
00662 .max_lowres = 3,
00663 .long_name = NULL_IF_CONFIG_SMALL("H.261"),
00664 };