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 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->error_recognition >= FF_ER_COMPLIANT)
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.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_count(&s->gb) + 7 >= s->gb.size_in_bits )
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 h->mtype = h261_mtype_map[h->mtype];
00290
00291
00292 if ( IS_QUANT ( h->mtype ) ){
00293 ff_set_qscale(s, get_bits(&s->gb, 5));
00294 }
00295
00296 s->mb_intra = IS_INTRA4x4(h->mtype);
00297
00298
00299 if ( IS_16X16 ( h->mtype ) ){
00300
00301
00302
00303
00304
00305
00306 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
00307 ( h->mba_diff != 1))
00308 {
00309 h->current_mv_x = 0;
00310 h->current_mv_y = 0;
00311 }
00312
00313 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
00314 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
00315 }else{
00316 h->current_mv_x = 0;
00317 h->current_mv_y = 0;
00318 }
00319
00320
00321 if ( HAS_CBP( h->mtype ) ){
00322 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
00323 }
00324
00325 if(s->mb_intra){
00326 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
00327 goto intra;
00328 }
00329
00330
00331 s->mv_dir = MV_DIR_FORWARD;
00332 s->mv_type = MV_TYPE_16X16;
00333 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
00334 s->mv[0][0][0] = h->current_mv_x * 2;
00335 s->mv[0][0][1] = h->current_mv_y * 2;
00336
00337 intra:
00338
00339 if(s->mb_intra || HAS_CBP(h->mtype)){
00340 s->dsp.clear_blocks(s->block[0]);
00341 for (i = 0; i < 6; i++) {
00342 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
00343 return SLICE_ERROR;
00344 }
00345 cbp+=cbp;
00346 }
00347 }else{
00348 for (i = 0; i < 6; i++)
00349 s->block_last_index[i]= -1;
00350 }
00351
00352 MPV_decode_mb(s, s->block);
00353
00354 return SLICE_OK;
00355 }
00356
00361 static int h261_decode_block(H261Context * h, DCTELEM * block,
00362 int n, int coded)
00363 {
00364 MpegEncContext * const s = &h->s;
00365 int code, level, i, j, run;
00366 RLTable *rl = &h261_rl_tcoeff;
00367 const uint8_t *scan_table;
00368
00369
00370
00371
00372
00373
00374 scan_table = s->intra_scantable.permutated;
00375 if (s->mb_intra){
00376
00377 level = get_bits(&s->gb, 8);
00378
00379 if((level&0x7F) == 0){
00380 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
00381 return -1;
00382 }
00383
00384 if (level == 255)
00385 level = 128;
00386 block[0] = level;
00387 i = 1;
00388 }else if(coded){
00389
00390
00391
00392
00393 int check = show_bits(&s->gb, 2);
00394 i = 0;
00395 if ( check & 0x2 ){
00396 skip_bits(&s->gb, 2);
00397 block[0] = ( check & 0x1 ) ? -1 : 1;
00398 i = 1;
00399 }
00400 }else{
00401 i = 0;
00402 }
00403 if(!coded){
00404 s->block_last_index[n] = i - 1;
00405 return 0;
00406 }
00407 for(;;){
00408 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
00409 if (code < 0){
00410 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
00411 return -1;
00412 }
00413 if (code == rl->n) {
00414
00415
00416 run = get_bits(&s->gb, 6);
00417 level = get_sbits(&s->gb, 8);
00418 }else if(code == 0){
00419 break;
00420 }else{
00421 run = rl->table_run[code];
00422 level = rl->table_level[code];
00423 if (get_bits1(&s->gb))
00424 level = -level;
00425 }
00426 i += run;
00427 if (i >= 64){
00428 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
00429 return -1;
00430 }
00431 j = scan_table[i];
00432 block[j] = level;
00433 i++;
00434 }
00435 s->block_last_index[n] = i-1;
00436 return 0;
00437 }
00438
00443 static int h261_decode_picture_header(H261Context *h){
00444 MpegEncContext * const s = &h->s;
00445 int format, i;
00446 uint32_t startcode= 0;
00447
00448 for(i= get_bits_left(&s->gb); i>24; i-=1){
00449 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
00450
00451 if(startcode == 0x10)
00452 break;
00453 }
00454
00455 if (startcode != 0x10){
00456 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
00457 return -1;
00458 }
00459
00460
00461 i= get_bits(&s->gb, 5);
00462 if(i < (s->picture_number&31))
00463 i += 32;
00464 s->picture_number = (s->picture_number&~31) + i;
00465
00466 s->avctx->time_base= (AVRational){1001, 30000};
00467 s->current_picture.pts= s->picture_number;
00468
00469
00470
00471 skip_bits1(&s->gb);
00472 skip_bits1(&s->gb);
00473 skip_bits1(&s->gb);
00474
00475 format = get_bits1(&s->gb);
00476
00477
00478 if (format == 0){
00479 s->width = 176;
00480 s->height = 144;
00481 s->mb_width = 11;
00482 s->mb_height = 9;
00483 }else{
00484 s->width = 352;
00485 s->height = 288;
00486 s->mb_width = 22;
00487 s->mb_height = 18;
00488 }
00489
00490 s->mb_num = s->mb_width * s->mb_height;
00491
00492 skip_bits1(&s->gb);
00493 skip_bits1(&s->gb);
00494
00495
00496 while (get_bits1(&s->gb) != 0){
00497 skip_bits(&s->gb, 8);
00498 }
00499
00500
00501
00502 s->pict_type = FF_P_TYPE;
00503
00504 h->gob_number = 0;
00505 return 0;
00506 }
00507
00508 static int h261_decode_gob(H261Context *h){
00509 MpegEncContext * const s = &h->s;
00510
00511 ff_set_qscale(s, s->qscale);
00512
00513
00514 while(h->current_mba <= MBA_STUFFING)
00515 {
00516 int ret;
00517
00518 ret= h261_decode_mb(h);
00519 if(ret<0){
00520 if(ret==SLICE_END){
00521 h261_decode_mb_skipped(h, h->current_mba, 33);
00522 return 0;
00523 }
00524 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
00525 return -1;
00526 }
00527
00528 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
00529 }
00530
00531 return -1;
00532 }
00533
00537 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
00538 int pos= get_bits_count(&s->gb)>>3;
00539 if(pos==0) pos=1;
00540 if(pos+10>buf_size) pos=buf_size;
00541
00542 return pos;
00543 }
00544
00545 static int h261_decode_frame(AVCodecContext *avctx,
00546 void *data, int *data_size,
00547 AVPacket *avpkt)
00548 {
00549 const uint8_t *buf = avpkt->data;
00550 int buf_size = avpkt->size;
00551 H261Context *h= avctx->priv_data;
00552 MpegEncContext *s = &h->s;
00553 int ret;
00554 AVFrame *pict = data;
00555
00556 dprintf(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
00557 dprintf(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
00558 s->flags= avctx->flags;
00559 s->flags2= avctx->flags2;
00560
00561 h->gob_start_code_skipped=0;
00562
00563 retry:
00564
00565 init_get_bits(&s->gb, buf, buf_size*8);
00566
00567 if(!s->context_initialized){
00568 if (MPV_common_init(s) < 0)
00569 return -1;
00570 }
00571
00572
00573 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
00574 int i= ff_find_unused_picture(s, 0);
00575 s->current_picture_ptr= &s->picture[i];
00576 }
00577
00578 ret = h261_decode_picture_header(h);
00579
00580
00581 if (ret < 0){
00582 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
00583 return -1;
00584 }
00585
00586 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
00587 ParseContext pc= s->parse_context;
00588 s->parse_context.buffer=0;
00589 MPV_common_end(s);
00590 s->parse_context= pc;
00591 }
00592 if (!s->context_initialized) {
00593 avcodec_set_dimensions(avctx, s->width, s->height);
00594
00595 goto retry;
00596 }
00597
00598
00599 s->current_picture.pict_type= s->pict_type;
00600 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
00601
00602
00603 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
00604 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
00605 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
00606 || avctx->skip_frame >= AVDISCARD_ALL)
00607 return get_consumed_bytes(s, buf_size);
00608
00609 if(MPV_frame_start(s, avctx) < 0)
00610 return -1;
00611
00612 ff_er_frame_start(s);
00613
00614
00615 s->mb_x=0;
00616 s->mb_y=0;
00617
00618 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
00619 if(ff_h261_resync(h)<0)
00620 break;
00621 h261_decode_gob(h);
00622 }
00623 MPV_frame_end(s);
00624
00625 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
00626 assert(s->current_picture.pict_type == s->pict_type);
00627 *pict= *(AVFrame*)s->current_picture_ptr;
00628 ff_print_debug_info(s, pict);
00629
00630 *data_size = sizeof(AVFrame);
00631
00632 return get_consumed_bytes(s, buf_size);
00633 }
00634
00635 static av_cold int h261_decode_end(AVCodecContext *avctx)
00636 {
00637 H261Context *h= avctx->priv_data;
00638 MpegEncContext *s = &h->s;
00639
00640 MPV_common_end(s);
00641 return 0;
00642 }
00643
00644 AVCodec h261_decoder = {
00645 "h261",
00646 AVMEDIA_TYPE_VIDEO,
00647 CODEC_ID_H261,
00648 sizeof(H261Context),
00649 h261_decode_init,
00650 NULL,
00651 h261_decode_end,
00652 h261_decode_frame,
00653 CODEC_CAP_DR1,
00654 .long_name = NULL_IF_CONFIG_SMALL("H.261"),
00655 };