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