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