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 ff_h261_mba_bits, 1, 1,
00059 ff_h261_mba_code, 1, 1, 662);
00060 INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
00061 ff_h261_mtype_bits, 1, 1,
00062 ff_h261_mtype_code, 1, 1, 80);
00063 INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
00064 &ff_h261_mv_tab[0][1], 2, 1,
00065 &ff_h261_mv_tab[0][0], 2, 1, 144);
00066 INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
00067 &ff_h261_cbp_tab[0][1], 2, 1,
00068 &ff_h261_cbp_tab[0][0], 2, 1, 512);
00069 ff_init_rl(&ff_h261_rl_tcoeff, ff_h261_rl_table_store);
00070 INIT_VLC_RL(ff_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 ff_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 | AV_EF_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.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 ff_MPV_decode_mb(s, s->block);
00225 }
00226
00227 return 0;
00228 }
00229
00230 static int decode_mv_component(GetBitContext *gb, int v){
00231 static const int mvmap[17] = {
00232 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
00233 };
00234 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
00235
00236
00237 if ( mv_diff < 0 )
00238 return v;
00239
00240 mv_diff = mvmap[mv_diff];
00241
00242 if(mv_diff && !get_bits1(gb))
00243 mv_diff= -mv_diff;
00244
00245 v += mv_diff;
00246 if (v <=-16) v+= 32;
00247 else if(v >= 16) v-= 32;
00248
00249 return v;
00250 }
00251
00252 static int h261_decode_mb(H261Context *h){
00253 MpegEncContext * const s = &h->s;
00254 int i, cbp, xy;
00255
00256 cbp = 63;
00257
00258 do{
00259 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
00260
00261
00262
00263 if (h->mba_diff == MBA_STARTCODE){
00264 h->gob_start_code_skipped = 1;
00265 return SLICE_END;
00266 }
00267 }
00268 while( h->mba_diff == MBA_STUFFING );
00269
00270 if ( h->mba_diff < 0 ){
00271 if (get_bits_left(&s->gb) <= 7)
00272 return SLICE_END;
00273
00274 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
00275 return SLICE_ERROR;
00276 }
00277
00278 h->mba_diff += 1;
00279 h->current_mba += h->mba_diff;
00280
00281 if ( h->current_mba > MBA_STUFFING )
00282 return SLICE_ERROR;
00283
00284 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
00285 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
00286 xy = s->mb_x + s->mb_y * s->mb_stride;
00287 ff_init_block_index(s);
00288 ff_update_block_index(s);
00289
00290
00291 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
00292 if (h->mtype < 0) {
00293 av_log(s->avctx, AV_LOG_ERROR, "illegal mtype %d\n", h->mtype);
00294 return SLICE_ERROR;
00295 }
00296 h->mtype = ff_h261_mtype_map[h->mtype];
00297
00298
00299 if ( IS_QUANT ( h->mtype ) ){
00300 ff_set_qscale(s, get_bits(&s->gb, 5));
00301 }
00302
00303 s->mb_intra = IS_INTRA4x4(h->mtype);
00304
00305
00306 if ( IS_16X16 ( h->mtype ) ){
00307
00308
00309
00310
00311
00312
00313 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
00314 ( h->mba_diff != 1))
00315 {
00316 h->current_mv_x = 0;
00317 h->current_mv_y = 0;
00318 }
00319
00320 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
00321 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
00322 }else{
00323 h->current_mv_x = 0;
00324 h->current_mv_y = 0;
00325 }
00326
00327
00328 if ( HAS_CBP( h->mtype ) ){
00329 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
00330 }
00331
00332 if(s->mb_intra){
00333 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
00334 goto intra;
00335 }
00336
00337
00338 s->mv_dir = MV_DIR_FORWARD;
00339 s->mv_type = MV_TYPE_16X16;
00340 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
00341 s->mv[0][0][0] = h->current_mv_x * 2;
00342 s->mv[0][0][1] = h->current_mv_y * 2;
00343
00344 intra:
00345
00346 if(s->mb_intra || HAS_CBP(h->mtype)){
00347 s->dsp.clear_blocks(s->block[0]);
00348 for (i = 0; i < 6; i++) {
00349 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
00350 return SLICE_ERROR;
00351 }
00352 cbp+=cbp;
00353 }
00354 }else{
00355 for (i = 0; i < 6; i++)
00356 s->block_last_index[i]= -1;
00357 }
00358
00359 ff_MPV_decode_mb(s, s->block);
00360
00361 return SLICE_OK;
00362 }
00363
00368 static int h261_decode_block(H261Context * h, DCTELEM * block,
00369 int n, int coded)
00370 {
00371 MpegEncContext * const s = &h->s;
00372 int code, level, i, j, run;
00373 RLTable *rl = &ff_h261_rl_tcoeff;
00374 const uint8_t *scan_table;
00375
00376
00377
00378
00379
00380
00381 scan_table = s->intra_scantable.permutated;
00382 if (s->mb_intra){
00383
00384 level = get_bits(&s->gb, 8);
00385
00386 if((level&0x7F) == 0){
00387 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
00388 return -1;
00389 }
00390
00391 if (level == 255)
00392 level = 128;
00393 block[0] = level;
00394 i = 1;
00395 }else if(coded){
00396
00397
00398
00399
00400 int check = show_bits(&s->gb, 2);
00401 i = 0;
00402 if ( check & 0x2 ){
00403 skip_bits(&s->gb, 2);
00404 block[0] = ( check & 0x1 ) ? -1 : 1;
00405 i = 1;
00406 }
00407 }else{
00408 i = 0;
00409 }
00410 if(!coded){
00411 s->block_last_index[n] = i - 1;
00412 return 0;
00413 }
00414 for(;;){
00415 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
00416 if (code < 0){
00417 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
00418 return -1;
00419 }
00420 if (code == rl->n) {
00421
00422
00423 run = get_bits(&s->gb, 6);
00424 level = get_sbits(&s->gb, 8);
00425 }else if(code == 0){
00426 break;
00427 }else{
00428 run = rl->table_run[code];
00429 level = rl->table_level[code];
00430 if (get_bits1(&s->gb))
00431 level = -level;
00432 }
00433 i += run;
00434 if (i >= 64){
00435 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
00436 return -1;
00437 }
00438 j = scan_table[i];
00439 block[j] = level;
00440 i++;
00441 }
00442 s->block_last_index[n] = i-1;
00443 return 0;
00444 }
00445
00450 static int h261_decode_picture_header(H261Context *h){
00451 MpegEncContext * const s = &h->s;
00452 int format, i;
00453 uint32_t startcode= 0;
00454
00455 for(i= get_bits_left(&s->gb); i>24; i-=1){
00456 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
00457
00458 if(startcode == 0x10)
00459 break;
00460 }
00461
00462 if (startcode != 0x10){
00463 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
00464 return -1;
00465 }
00466
00467
00468 i= get_bits(&s->gb, 5);
00469 if(i < (s->picture_number&31))
00470 i += 32;
00471 s->picture_number = (s->picture_number&~31) + i;
00472
00473 s->avctx->time_base= (AVRational){1001, 30000};
00474 s->current_picture.f.pts = s->picture_number;
00475
00476
00477
00478 skip_bits1(&s->gb);
00479 skip_bits1(&s->gb);
00480 skip_bits1(&s->gb);
00481
00482 format = get_bits1(&s->gb);
00483
00484
00485 if (format == 0){
00486 s->width = 176;
00487 s->height = 144;
00488 s->mb_width = 11;
00489 s->mb_height = 9;
00490 }else{
00491 s->width = 352;
00492 s->height = 288;
00493 s->mb_width = 22;
00494 s->mb_height = 18;
00495 }
00496
00497 s->mb_num = s->mb_width * s->mb_height;
00498
00499 skip_bits1(&s->gb);
00500 skip_bits1(&s->gb);
00501
00502
00503 while (get_bits1(&s->gb) != 0){
00504 skip_bits(&s->gb, 8);
00505 }
00506
00507
00508
00509 s->pict_type = AV_PICTURE_TYPE_P;
00510
00511 h->gob_number = 0;
00512 return 0;
00513 }
00514
00515 static int h261_decode_gob(H261Context *h){
00516 MpegEncContext * const s = &h->s;
00517
00518 ff_set_qscale(s, s->qscale);
00519
00520
00521 while(h->current_mba <= MBA_STUFFING)
00522 {
00523 int ret;
00524
00525 ret= h261_decode_mb(h);
00526 if(ret<0){
00527 if(ret==SLICE_END){
00528 h261_decode_mb_skipped(h, h->current_mba, 33);
00529 return 0;
00530 }
00531 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
00532 return -1;
00533 }
00534
00535 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
00536 }
00537
00538 return -1;
00539 }
00540
00544 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
00545 int pos= get_bits_count(&s->gb)>>3;
00546 if(pos==0) pos=1;
00547 if(pos+10>buf_size) pos=buf_size;
00548
00549 return pos;
00550 }
00551
00552 static int h261_decode_frame(AVCodecContext *avctx,
00553 void *data, int *data_size,
00554 AVPacket *avpkt)
00555 {
00556 const uint8_t *buf = avpkt->data;
00557 int buf_size = avpkt->size;
00558 H261Context *h= avctx->priv_data;
00559 MpegEncContext *s = &h->s;
00560 int ret;
00561 AVFrame *pict = data;
00562
00563 av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
00564 av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
00565 s->flags= avctx->flags;
00566 s->flags2= avctx->flags2;
00567
00568 h->gob_start_code_skipped=0;
00569
00570 retry:
00571
00572 init_get_bits(&s->gb, buf, buf_size*8);
00573
00574 if(!s->context_initialized){
00575 if (ff_MPV_common_init(s) < 0)
00576 return -1;
00577 }
00578
00579
00580 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
00581 int i= ff_find_unused_picture(s, 0);
00582 if (i < 0)
00583 return i;
00584 s->current_picture_ptr= &s->picture[i];
00585 }
00586
00587 ret = h261_decode_picture_header(h);
00588
00589
00590 if (ret < 0){
00591 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
00592 return -1;
00593 }
00594
00595 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
00596 ParseContext pc= s->parse_context;
00597 s->parse_context.buffer=0;
00598 ff_MPV_common_end(s);
00599 s->parse_context= pc;
00600 }
00601 if (!s->context_initialized) {
00602 avcodec_set_dimensions(avctx, s->width, s->height);
00603
00604 goto retry;
00605 }
00606
00607
00608 s->current_picture.f.pict_type = s->pict_type;
00609 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
00610
00611 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
00612 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
00613 || avctx->skip_frame >= AVDISCARD_ALL)
00614 return get_consumed_bytes(s, buf_size);
00615
00616 if(ff_MPV_frame_start(s, avctx) < 0)
00617 return -1;
00618
00619 ff_er_frame_start(s);
00620
00621
00622 s->mb_x=0;
00623 s->mb_y=0;
00624
00625 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
00626 if(ff_h261_resync(h)<0)
00627 break;
00628 h261_decode_gob(h);
00629 }
00630 ff_MPV_frame_end(s);
00631
00632 assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
00633 assert(s->current_picture.f.pict_type == s->pict_type);
00634
00635 *pict = s->current_picture_ptr->f;
00636 ff_print_debug_info(s, pict);
00637
00638 *data_size = sizeof(AVFrame);
00639
00640 return get_consumed_bytes(s, buf_size);
00641 }
00642
00643 static av_cold int h261_decode_end(AVCodecContext *avctx)
00644 {
00645 H261Context *h= avctx->priv_data;
00646 MpegEncContext *s = &h->s;
00647
00648 ff_MPV_common_end(s);
00649 return 0;
00650 }
00651
00652 AVCodec ff_h261_decoder = {
00653 .name = "h261",
00654 .type = AVMEDIA_TYPE_VIDEO,
00655 .id = CODEC_ID_H261,
00656 .priv_data_size = sizeof(H261Context),
00657 .init = h261_decode_init,
00658 .close = h261_decode_end,
00659 .decode = h261_decode_frame,
00660 .capabilities = CODEC_CAP_DR1,
00661 .max_lowres = 3,
00662 .long_name = NULL_IF_CONFIG_SMALL("H.261"),
00663 };