comparison mpegvideo.c @ 745:25d7fb7c89be libavcodec

better/cleaner error resilience (done in a 2nd pass after decoding) h263/mpeg4 out of order slice decoding
author michaelni
date Sun, 13 Oct 2002 13:16:04 +0000
parents f011a0df269f
children 8e1f0939d15d
comparison
equal deleted inserted replaced
744:2f7da29ede37 745:25d7fb7c89be
132 if(p==NULL){\ 132 if(p==NULL){\
133 perror("malloc");\ 133 perror("malloc");\
134 goto fail;\ 134 goto fail;\
135 }\ 135 }\
136 } 136 }
137 /* 137
138 static void build_end(void)
139 {
140 int lastIndex;
141 int lastIndexAfterPerm=0;
142 for(lastIndex=0; lastIndex<64; lastIndex++)
143 {
144 if(ff_zigzag_direct[lastIndex] > lastIndexAfterPerm)
145 lastIndexAfterPerm= ff_zigzag_direct[lastIndex];
146 zigzag_end[lastIndex]= lastIndexAfterPerm + 1;
147 }
148 }
149 */
150 void ff_init_scantable(MpegEncContext *s, ScanTable *st, const UINT8 *src_scantable){ 138 void ff_init_scantable(MpegEncContext *s, ScanTable *st, const UINT8 *src_scantable){
151 int i; 139 int i;
152 int end; 140 int end;
153 141
154 for(i=0; i<64; i++){ 142 for(i=0; i<64; i++){
341 if(s->msmpeg4_version){ 329 if(s->msmpeg4_version){
342 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int)); 330 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
343 } 331 }
344 CHECKED_ALLOCZ(s->avctx->stats_out, 256); 332 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
345 } 333 }
334
335 CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(UINT8))
346 336
347 if (s->out_format == FMT_H263 || s->encoding) { 337 if (s->out_format == FMT_H263 || s->encoding) {
348 int size; 338 int size;
349 /* Allocate MB type table */ 339 /* Allocate MB type table */
350 CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8)) 340 CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8))
353 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); 343 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
354 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16)); 344 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
355 } 345 }
356 346
357 if(s->codec_id==CODEC_ID_MPEG4){ 347 if(s->codec_id==CODEC_ID_MPEG4){
358 /* 4mv and interlaced direct mode decoding tables */ 348 /* interlaced direct mode decoding tables */
359 CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))
360 CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16)) 349 CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16))
361 CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8)) 350 CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8))
362 } 351 }
352 /* 4mv b frame decoding table */
353 //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
354 CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))
363 355
364 if (s->h263_pred || s->h263_plus) { 356 if (s->h263_pred || s->h263_plus) {
365 int y_size, c_size, i, size; 357 int y_size, c_size, i, size;
366 358
367 /* dc values */ 359 /* dc values */
398 390
399 /* default structure is frame */ 391 /* default structure is frame */
400 s->picture_structure = PICT_FRAME; 392 s->picture_structure = PICT_FRAME;
401 393
402 /* init macroblock skip table */ 394 /* init macroblock skip table */
403 CHECKED_ALLOCZ(s->mbskip_table, s->mb_num); 395 CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
396 //Note the +1 is for a quicker mpeg4 slice_end detection
404 397
405 s->block= s->blocks[0]; 398 s->block= s->blocks[0];
406 399
407 s->context_initialized = 1; 400 s->context_initialized = 1;
408 return 0; 401 return 0;
451 av_freep(&s->co_located_type_table); 444 av_freep(&s->co_located_type_table);
452 av_freep(&s->field_mv_table); 445 av_freep(&s->field_mv_table);
453 av_freep(&s->field_select_table); 446 av_freep(&s->field_select_table);
454 av_freep(&s->avctx->stats_out); 447 av_freep(&s->avctx->stats_out);
455 av_freep(&s->ac_stats); 448 av_freep(&s->ac_stats);
449 av_freep(&s->error_status_table);
456 450
457 for(i=0;i<3;i++) { 451 for(i=0;i<3;i++) {
458 int j; 452 int j;
459 if(!(s->flags&CODEC_FLAG_DR1)){ 453 if(!(s->flags&CODEC_FLAG_DR1)){
460 av_freep(&s->last_picture_base[i]); 454 av_freep(&s->last_picture_base[i]);
595 s->out_format = FMT_H263; 589 s->out_format = FMT_H263;
596 s->h263_pred = 1; 590 s->h263_pred = 1;
597 s->unrestricted_mv = 1; 591 s->unrestricted_mv = 1;
598 s->has_b_frames= s->max_b_frames ? 1 : 0; 592 s->has_b_frames= s->max_b_frames ? 1 : 0;
599 s->low_delay= !s->has_b_frames; 593 s->low_delay= !s->has_b_frames;
600 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); 594 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
601 break; 595 break;
602 case CODEC_ID_MSMPEG4V1: 596 case CODEC_ID_MSMPEG4V1:
603 s->out_format = FMT_H263; 597 s->out_format = FMT_H263;
604 s->h263_msmpeg4 = 1; 598 s->h263_msmpeg4 = 1;
605 s->h263_pred = 1; 599 s->h263_pred = 1;
765 { 759 {
766 int i; 760 int i;
767 UINT8 *tmp; 761 UINT8 *tmp;
768 762
769 s->mb_skiped = 0; 763 s->mb_skiped = 0;
770 s->decoding_error=0;
771 avctx->mbskip_table= s->mbskip_table; 764 avctx->mbskip_table= s->mbskip_table;
772 765
773 if(avctx->flags&CODEC_FLAG_DR1){ 766 if(avctx->flags&CODEC_FLAG_DR1){
774 avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type); 767 avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type);
775 768
1995 1988
1996 return score; 1989 return score;
1997 } 1990 }
1998 1991
1999 #endif 1992 #endif
1993
1994 void ff_draw_horiz_band(MpegEncContext *s){
1995 if ( s->avctx->draw_horiz_band
1996 && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
1997 UINT8 *src_ptr[3];
1998 int y, h, offset;
1999 y = s->mb_y * 16;
2000 h = s->height - y;
2001 if (h > 16)
2002 h = 16;
2003
2004 if(s->pict_type==B_TYPE)
2005 offset = 0;
2006 else
2007 offset = y * s->linesize;
2008
2009 if(s->pict_type==B_TYPE || (!s->has_b_frames)){
2010 src_ptr[0] = s->current_picture[0] + offset;
2011 src_ptr[1] = s->current_picture[1] + (offset >> 2);
2012 src_ptr[2] = s->current_picture[2] + (offset >> 2);
2013 } else {
2014 src_ptr[0] = s->last_picture[0] + offset;
2015 src_ptr[1] = s->last_picture[1] + (offset >> 2);
2016 src_ptr[2] = s->last_picture[2] + (offset >> 2);
2017 }
2018 s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
2019 y, s->width, h);
2020 }
2021 }
2022
2000 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) 2023 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2001 { 2024 {
2002 const int mb_x= s->mb_x; 2025 const int mb_x= s->mb_x;
2003 const int mb_y= s->mb_y; 2026 const int mb_y= s->mb_y;
2004 int i; 2027 int i;
2537 s->last_dc[2] = 128; 2560 s->last_dc[2] = 128;
2538 s->mb_incr = 1; 2561 s->mb_incr = 1;
2539 s->last_mv[0][0][0] = 0; 2562 s->last_mv[0][0][0] = 0;
2540 s->last_mv[0][0][1] = 0; 2563 s->last_mv[0][0][1] = 0;
2541 2564
2542 /* Get the GOB height based on picture height */ 2565 if (s->codec_id==CODEC_ID_H263 && s->codec_id==CODEC_ID_H263)
2543 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) { 2566 s->gob_index = ff_h263_get_gob_height(s);
2544 if (s->height <= 400) 2567 else
2545 s->gob_index = 1; 2568 s->gob_index = 1; //FIXME remove
2546 else if (s->height <= 800) 2569
2547 s->gob_index = 2; 2570 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2548 else
2549 s->gob_index = 4;
2550 }else if(s->codec_id==CODEC_ID_MPEG4){
2551 s->gob_index = 1;
2552 }
2553
2554 if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
2555 ff_mpeg4_init_partitions(s); 2571 ff_mpeg4_init_partitions(s);
2556 2572
2557 s->resync_mb_x=0; 2573 s->resync_mb_x=0;
2558 s->resync_mb_y=0; 2574 s->resync_mb_y=0;
2575 s->first_slice_line = 1;
2559 for(mb_y=0; mb_y < s->mb_height; mb_y++) { 2576 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2560 /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/ 2577 /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
2561 /* TODO: Put all this stuff in a separate generic function */ 2578 /* TODO: Put all this stuff in a separate generic function */
2562 if (s->rtp_mode) { 2579 if (s->rtp_mode) {
2563 if (!mb_y) { 2580 if (!mb_y) {
2597 s->block_index[1]+=2; 2614 s->block_index[1]+=2;
2598 s->block_index[2]+=2; 2615 s->block_index[2]+=2;
2599 s->block_index[3]+=2; 2616 s->block_index[3]+=2;
2600 s->block_index[4]++; 2617 s->block_index[4]++;
2601 s->block_index[5]++; 2618 s->block_index[5]++;
2602 2619 //printf("%d %d %d %d %d\n", s->mb_x, s->mb_y, s->resync_mb_x, s->resync_mb_y, s->first_slice_line);
2603 /* write gob / video packet header for formats which support it at any MB (MPEG4) */ 2620 /* write gob / video packet header for formats which support it at any MB (MPEG4) */
2604 if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){ 2621 if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
2605 int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob; 2622 int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
2606 2623
2607 //the *2 is there so we stay below the requested size 2624 //the *2 is there so we stay below the requested size
2608 if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){ 2625 if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){
2609 if(s->codec_id==CODEC_ID_MPEG4){ 2626 if(s->codec_id==CODEC_ID_MPEG4){
2610 if(s->data_partitioning && s->pict_type!=B_TYPE){ 2627 if(s->partitioned_frame){
2611 ff_mpeg4_merge_partitions(s); 2628 ff_mpeg4_merge_partitions(s);
2612 ff_mpeg4_init_partitions(s); 2629 ff_mpeg4_init_partitions(s);
2613 } 2630 }
2614 ff_mpeg4_encode_video_packet_header(s); 2631 ff_mpeg4_encode_video_packet_header(s);
2615 2632
2623 s->ptr_lastgob = pbBufPtr(&s->pb); 2640 s->ptr_lastgob = pbBufPtr(&s->pb);
2624 s->first_slice_line=1; 2641 s->first_slice_line=1;
2625 s->resync_mb_x=mb_x; 2642 s->resync_mb_x=mb_x;
2626 s->resync_mb_y=mb_y; 2643 s->resync_mb_y=mb_y;
2627 } 2644 }
2628 2645 }
2629 if( (s->resync_mb_x == s->mb_x) 2646
2630 && s->resync_mb_y+1 == s->mb_y){ 2647 if( (s->resync_mb_x == s->mb_x)
2631 s->first_slice_line=0; 2648 && s->resync_mb_y+1 == s->mb_y){
2632 } 2649 s->first_slice_line=0;
2633 } 2650 }
2634 2651
2635 if(mb_type & (mb_type-1)){ // more than 1 MB type possible 2652 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
2636 int next_block=0; 2653 int next_block=0;
2637 int pb_bits_count, pb2_bits_count, tex_pb_bits_count; 2654 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2638 2655
2639 copy_context_before_encode(&backup_s, s, -1); 2656 copy_context_before_encode(&backup_s, s, -1);
2640 backup_s.pb= s->pb; 2657 backup_s.pb= s->pb;
2641 best_s.data_partitioning= s->data_partitioning; 2658 best_s.data_partitioning= s->data_partitioning;
2659 best_s.partitioned_frame= s->partitioned_frame;
2642 if(s->data_partitioning){ 2660 if(s->data_partitioning){
2643 backup_s.pb2= s->pb2; 2661 backup_s.pb2= s->pb2;
2644 backup_s.tex_pb= s->tex_pb; 2662 backup_s.tex_pb= s->tex_pb;
2645 } 2663 }
2646 2664
2825 if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean 2843 if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
2826 } 2844 }
2827 } 2845 }
2828 emms_c(); 2846 emms_c();
2829 2847
2830 if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE) 2848 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
2831 ff_mpeg4_merge_partitions(s); 2849 ff_mpeg4_merge_partitions(s);
2832 2850
2833 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE) 2851 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
2834 msmpeg4_encode_ext_header(s); 2852 msmpeg4_encode_ext_header(s);
2835 2853
3088 block[i] = level; 3106 block[i] = level;
3089 } 3107 }
3090 } 3108 }
3091 } 3109 }
3092 3110
3093 static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
3094 {
3095 int dc, dcb, dcr, y, i;
3096 for(i=0; i<4; i++){
3097 dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
3098 for(y=0; y<8; y++){
3099 int x;
3100 for(x=0; x<8; x++){
3101 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
3102 }
3103 }
3104 }
3105 dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
3106 dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
3107 for(y=0; y<8; y++){
3108 int x;
3109 for(x=0; x<8; x++){
3110 dest_cb[x + y*(s->uvlinesize)]= dcb/8;
3111 dest_cr[x + y*(s->uvlinesize)]= dcr/8;
3112 }
3113 }
3114 }
3115
3116 /**
3117 * will conceal past errors, and allso drop b frames if needed
3118 *
3119 */
3120 void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
3121 {
3122 int mb_x= s->mb_x;
3123 int mb_y= s->mb_y;
3124 int mb_dist=0;
3125 int i, intra_count=0, inter_count=0;
3126 int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
3127 int inter_conceal= s->msmpeg4_version ? 50 : 50;
3128
3129 // for last block
3130 if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
3131 if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
3132
3133 if(s->decoding_error==0 && unknown_pos){
3134 if(s->data_partitioning && s->pict_type!=B_TYPE)
3135 s->decoding_error= DECODING_AC_LOST;
3136 else
3137 s->decoding_error= DECODING_DESYNC;
3138 }
3139
3140 if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
3141
3142 for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
3143 if(s->mbintra_table[i]) intra_count++;
3144 else inter_count++;
3145 }
3146
3147 if(s->decoding_error==DECODING_AC_LOST){
3148 intra_conceal*=2;
3149 inter_conceal*=2;
3150 }else if(s->decoding_error==DECODING_ACDC_LOST){
3151 intra_conceal*=2;
3152 inter_conceal*=2;
3153 }
3154
3155 if(unknown_pos && (intra_count<inter_count)){
3156 intra_conceal= inter_conceal= s->mb_num;
3157 // printf("%d %d\n",intra_count, inter_count);
3158 }
3159
3160 fprintf(stderr, "concealing errors\n");
3161
3162 /* for all MBs from the current one back until the last resync marker */
3163 for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
3164 for(; mb_x>=0; mb_x--){
3165 uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
3166 uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
3167 uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
3168 int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
3169 int mb_y_backup= s->mb_y;
3170 s->mb_x=mb_x;
3171 s->mb_y=mb_y;
3172 if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
3173 if(s->decoding_error==DECODING_AC_LOST){
3174 remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
3175 // printf("remove ac to %d %d\n", mb_x, mb_y);
3176 }else{
3177 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
3178 s->last_picture, 0, 0, put_pixels_tab,
3179 0/*mx*/, 0/*my*/, 16);
3180 }
3181 }
3182 else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
3183 int mx=0;
3184 int my=0;
3185
3186 if(s->decoding_error!=DECODING_DESYNC){
3187 int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
3188 mx= s->motion_val[ xy ][0];
3189 my= s->motion_val[ xy ][1];
3190 }
3191
3192 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
3193 s->last_picture, 0, 0, put_pixels_tab,
3194 mx, my, 16);
3195 }
3196 s->mb_x= mb_x_backup;
3197 s->mb_y= mb_y_backup;
3198
3199 if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
3200 if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
3201 }
3202 mb_x=s->mb_width-1;
3203 }
3204 }
3205
3206 AVCodec mpeg1video_encoder = { 3111 AVCodec mpeg1video_encoder = {
3207 "mpeg1video", 3112 "mpeg1video",
3208 CODEC_TYPE_VIDEO, 3113 CODEC_TYPE_VIDEO,
3209 CODEC_ID_MPEG1VIDEO, 3114 CODEC_ID_MPEG1VIDEO,
3210 sizeof(MpegEncContext), 3115 sizeof(MpegEncContext),