diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2011-03-22 22:36:57 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2011-03-22 22:36:57 +0100 |
commit | d375c1040032ed42f84b7d4ea53baad4a661b628 (patch) | |
tree | f5bb63830cbc49106376a418c292c452f3b0728d /libavcodec/mpegvideo.c | |
parent | 038566a5edc73205120f30d41233a9911a42da44 (diff) | |
download | ffmpeg-d375c1040032ed42f84b7d4ea53baad4a661b628.tar.gz |
Fake-Merge remote-tracking branch 'ffmpeg-mt/master'
Diffstat (limited to 'libavcodec/mpegvideo.c')
-rw-r--r-- | libavcodec/mpegvideo.c | 264 |
1 files changed, 219 insertions, 45 deletions
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index 6ba7142763..10a488bb27 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -38,6 +38,7 @@ #include "msmpeg4.h" #include "faandct.h" #include "xvmc_internal.h" +#include "thread.h" #include <limits.h> //#undef NDEBUG @@ -205,7 +206,7 @@ void ff_copy_picture(Picture *dst, Picture *src){ */ static void free_frame_buffer(MpegEncContext *s, Picture *pic) { - s->avctx->release_buffer(s->avctx, (AVFrame*)pic); + ff_thread_release_buffer(s->avctx, (AVFrame*)pic); av_freep(&pic->hwaccel_picture_private); } @@ -227,7 +228,7 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic) } } - r = s->avctx->get_buffer(s->avctx, (AVFrame*)pic); + r = ff_thread_get_buffer(s->avctx, (AVFrame*)pic); if (r<0 || !pic->age || !pic->type || !pic->data[0]) { av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]); @@ -458,6 +459,81 @@ void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){ //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads } +int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src) +{ + MpegEncContext *s = dst->priv_data, *s1 = src->priv_data; + + if(dst == src || !s1->context_initialized) return 0; + + //FIXME can parameters change on I-frames? in that case dst may need a reinit + if(!s->context_initialized){ + memcpy(s, s1, sizeof(MpegEncContext)); + + s->avctx = dst; + s->picture_range_start += MAX_PICTURE_COUNT; + s->picture_range_end += MAX_PICTURE_COUNT; + s->bitstream_buffer = NULL; + s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0; + + MPV_common_init(s); + } + + s->avctx->coded_height = s1->avctx->coded_height; + s->avctx->coded_width = s1->avctx->coded_width; + s->avctx->width = s1->avctx->width; + s->avctx->height = s1->avctx->height; + + s->coded_picture_number = s1->coded_picture_number; + s->picture_number = s1->picture_number; + s->input_picture_number = s1->input_picture_number; + + memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture)); + memcpy(&s->last_picture, &s1->last_picture, (char*)&s1->last_picture_ptr - (char*)&s1->last_picture); + + s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1); + s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1); + s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1); + + memcpy(s->prev_pict_types, s1->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE); + + //Error/bug resilience + s->next_p_frame_damaged = s1->next_p_frame_damaged; + s->workaround_bugs = s1->workaround_bugs; + + //MPEG4 timing info + memcpy(&s->time_increment_bits, &s1->time_increment_bits, (char*)&s1->shape - (char*)&s1->time_increment_bits); + + //B-frame info + s->max_b_frames = s1->max_b_frames; + s->low_delay = s1->low_delay; + s->dropable = s1->dropable; + + //DivX handling (doesn't work) + s->divx_packed = s1->divx_packed; + + if(s1->bitstream_buffer){ + if (s1->bitstream_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) + av_fast_malloc(&s->bitstream_buffer, &s->allocated_bitstream_buffer_size, s1->allocated_bitstream_buffer_size); + s->bitstream_buffer_size = s1->bitstream_buffer_size; + memcpy(s->bitstream_buffer, s1->bitstream_buffer, s1->bitstream_buffer_size); + memset(s->bitstream_buffer+s->bitstream_buffer_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); + } + + //MPEG2/interlacing info + memcpy(&s->progressive_sequence, &s1->progressive_sequence, (char*)&s1->rtp_mode - (char*)&s1->progressive_sequence); + + if(!s1->first_field){ + s->last_pict_type= s1->pict_type; + if (s1->current_picture_ptr) s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->quality; + + if(s1->pict_type!=FF_B_TYPE){ + s->last_non_b_pict_type= s1->pict_type; + } + } + + return 0; +} + /** * sets the given MpegEncContext to common defaults (same for encoding and decoding). * the changed fields will not depend upon the prior state of the MpegEncContext. @@ -478,6 +554,9 @@ void MPV_common_defaults(MpegEncContext *s){ s->f_code = 1; s->b_code = 1; + + s->picture_range_start = 0; + s->picture_range_end = MAX_PICTURE_COUNT; } /** @@ -506,7 +585,8 @@ av_cold int MPV_common_init(MpegEncContext *s) return -1; } - if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){ + if(s->avctx->active_thread_type&FF_THREAD_SLICE && + (s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height))){ av_log(s->avctx, AV_LOG_ERROR, "too many threads\n"); return -1; } @@ -599,8 +679,9 @@ av_cold int MPV_common_init(MpegEncContext *s) FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail) } } - FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, MAX_PICTURE_COUNT * sizeof(Picture), fail) - for(i = 0; i < MAX_PICTURE_COUNT; i++) { + s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count); + FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, s->picture_count * sizeof(Picture), fail) + for(i = 0; i < s->picture_count; i++) { avcodec_get_frame_defaults((AVFrame *)&s->picture[i]); } @@ -660,20 +741,26 @@ av_cold int MPV_common_init(MpegEncContext *s) } s->context_initialized = 1; - s->thread_context[0]= s; - threads = s->avctx->thread_count; - for(i=1; i<threads; i++){ - s->thread_context[i]= av_malloc(sizeof(MpegEncContext)); - memcpy(s->thread_context[i], s, sizeof(MpegEncContext)); - } + if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) { + threads = s->avctx->thread_count; - for(i=0; i<threads; i++){ - if(init_duplicate_context(s->thread_context[i], s) < 0) - goto fail; - s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count; - s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count; + for(i=1; i<threads; i++){ + s->thread_context[i]= av_malloc(sizeof(MpegEncContext)); + memcpy(s->thread_context[i], s, sizeof(MpegEncContext)); + } + + for(i=0; i<threads; i++){ + if(init_duplicate_context(s->thread_context[i], s) < 0) + goto fail; + s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count; + s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count; + } + } else { + if(init_duplicate_context(s, s) < 0) goto fail; + s->start_mb_y = 0; + s->end_mb_y = s->mb_height; } return 0; @@ -687,12 +774,14 @@ void MPV_common_end(MpegEncContext *s) { int i, j, k; - for(i=0; i<s->avctx->thread_count; i++){ - free_duplicate_context(s->thread_context[i]); - } - for(i=1; i<s->avctx->thread_count; i++){ - av_freep(&s->thread_context[i]); - } + if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) { + for(i=0; i<s->avctx->thread_count; i++){ + free_duplicate_context(s->thread_context[i]); + } + for(i=1; i<s->avctx->thread_count; i++){ + av_freep(&s->thread_context[i]); + } + } else free_duplicate_context(s); av_freep(&s->parse_context.buffer); s->parse_context.buffer_size=0; @@ -747,8 +836,8 @@ void MPV_common_end(MpegEncContext *s) av_freep(&s->reordered_input_picture); av_freep(&s->dct_offset); - if(s->picture){ - for(i=0; i<MAX_PICTURE_COUNT; i++){ + if(s->picture && !s->avctx->is_copy){ + for(i=0; i<s->picture_count; i++){ free_picture(s, &s->picture[i]); } } @@ -762,7 +851,8 @@ void MPV_common_end(MpegEncContext *s) for(i=0; i<3; i++) av_freep(&s->visualization_buffer[i]); - avcodec_default_free_buffers(s->avctx); + if(!(s->avctx->active_thread_type&FF_THREAD_FRAME)) + avcodec_default_free_buffers(s->avctx); } void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3]) @@ -860,14 +950,14 @@ int ff_find_unused_picture(MpegEncContext *s, int shared){ int i; if(shared){ - for(i=0; i<MAX_PICTURE_COUNT; i++){ + for(i=s->picture_range_start; i<s->picture_range_end; i++){ if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i; } }else{ - for(i=0; i<MAX_PICTURE_COUNT; i++){ + for(i=s->picture_range_start; i<s->picture_range_end; i++){ if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME } - for(i=0; i<MAX_PICTURE_COUNT; i++){ + for(i=s->picture_range_start; i<s->picture_range_end; i++){ if(s->picture[i].data[0]==NULL) return i; } } @@ -924,7 +1014,7 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) /* release forgotten pictures */ /* if(mpeg124/h263) */ if(!s->encoding){ - for(i=0; i<MAX_PICTURE_COUNT; i++){ + for(i=0; i<s->picture_count; i++){ if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){ av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n"); free_frame_buffer(s, &s->picture[i]); @@ -936,7 +1026,7 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) if(!s->encoding){ /* release non reference frames */ - for(i=0; i<MAX_PICTURE_COUNT; i++){ + for(i=0; i<s->picture_count; i++){ if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){ free_frame_buffer(s, &s->picture[i]); } @@ -970,6 +1060,7 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) s->current_picture_ptr->top_field_first= (s->picture_structure == PICT_TOP_FIELD) == s->first_field; } s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence; + s->current_picture_ptr->field_picture= s->picture_structure != PICT_FRAME; } s->current_picture_ptr->pict_type= s->pict_type; @@ -998,6 +1089,8 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) s->last_picture_ptr= &s->picture[i]; if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) return -1; + ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 0); + ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 1); } if((s->next_picture_ptr==NULL || s->next_picture_ptr->data[0]==NULL) && s->pict_type==FF_B_TYPE){ /* Allocate a dummy frame */ @@ -1005,6 +1098,8 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) s->next_picture_ptr= &s->picture[i]; if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) return -1; + ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 0); + ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 1); } } @@ -1057,7 +1152,7 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) void MPV_frame_end(MpegEncContext *s) { int i; - /* draw edge for correct motion prediction if outside */ + /* redraw edges for the frame if decoding didn't complete */ //just to make sure that all data is rendered. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){ ff_xvmc_field_end(s); @@ -1067,10 +1162,14 @@ void MPV_frame_end(MpegEncContext *s) && s->current_picture.reference && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) { - s->dsp.draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH ); - s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); - s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); + int edges = EDGE_BOTTOM | EDGE_TOP, h = s->v_edge_pos; + + s->dsp.draw_edges(s->current_picture_ptr->data[0], s->linesize , s->h_edge_pos , h , EDGE_WIDTH , edges); + s->dsp.draw_edges(s->current_picture_ptr->data[1], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges); + s->dsp.draw_edges(s->current_picture_ptr->data[2], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges); + } + emms_c(); s->last_pict_type = s->pict_type; @@ -1091,7 +1190,7 @@ void MPV_frame_end(MpegEncContext *s) if(s->encoding){ /* release non-reference frames */ - for(i=0; i<MAX_PICTURE_COUNT; i++){ + for(i=0; i<s->picture_count; i++){ if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){ free_frame_buffer(s, &s->picture[i]); } @@ -1104,6 +1203,10 @@ void MPV_frame_end(MpegEncContext *s) memset(&s->current_picture, 0, sizeof(Picture)); #endif s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr; + + if (s->codec_id != CODEC_ID_H264 && s->current_picture.reference) { + ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_height-1, 0); + } } /** @@ -1768,6 +1871,43 @@ static inline void MPV_motion_lowres(MpegEncContext *s, } } +/** + * find the lowest MB row referenced in the MVs + */ +int MPV_lowest_referenced_row(MpegEncContext *s, int dir) +{ + int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample; + int my, off, i, mvs; + + if (s->picture_structure != PICT_FRAME) goto unhandled; + + switch (s->mv_type) { + case MV_TYPE_16X16: + mvs = 1; + break; + case MV_TYPE_16X8: + mvs = 2; + break; + case MV_TYPE_8X8: + mvs = 4; + break; + default: + goto unhandled; + } + + for (i = 0; i < mvs; i++) { + my = s->mv[dir][i][1]<<qpel_shift; + my_max = FFMAX(my_max, my); + my_min = FFMIN(my_min, my); + } + + off = (FFMAX(-my_min, my_max) + 63) >> 6; + + return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1); +unhandled: + return s->mb_height-1; +} + /* put block[] to dest[] */ static inline void put_dct(MpegEncContext *s, DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale) @@ -1932,6 +2072,16 @@ void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64], /* motion handling */ /* decoding or more than one mb_type (MC was already done otherwise) */ if(!s->encoding){ + + if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) { + if (s->mv_dir & MV_DIR_FORWARD) { + ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0); + } + if (s->mv_dir & MV_DIR_BACKWARD) { + ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0); + } + } + if(lowres_flag){ h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab; @@ -2096,19 +2246,37 @@ void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){ * @param h is the normal height, this will be reduced automatically if needed for the last row */ void ff_draw_horiz_band(MpegEncContext *s, int y, int h){ + const int field_pic= s->picture_structure != PICT_FRAME; + if(field_pic){ + h <<= 1; + y <<= 1; + } + + if (!s->avctx->hwaccel + && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) + && s->unrestricted_mv + && s->current_picture.reference + && !s->intra_only + && !(s->flags&CODEC_FLAG_EMU_EDGE)) { + int sides = 0, edge_h; + if (y==0) sides |= EDGE_TOP; + if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM; + + edge_h= FFMIN(h, s->v_edge_pos - y); + + s->dsp.draw_edges(s->current_picture_ptr->data[0] + y *s->linesize , s->linesize , s->h_edge_pos , edge_h , EDGE_WIDTH , sides); + s->dsp.draw_edges(s->current_picture_ptr->data[1] + (y>>1)*s->uvlinesize, s->uvlinesize, s->h_edge_pos>>1, edge_h>>1, EDGE_WIDTH/2, sides); + s->dsp.draw_edges(s->current_picture_ptr->data[2] + (y>>1)*s->uvlinesize, s->uvlinesize, s->h_edge_pos>>1, edge_h>>1, EDGE_WIDTH/2, sides); + } + + h= FFMIN(h, s->avctx->height - y); + + if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return; + if (s->avctx->draw_horiz_band) { AVFrame *src; - const int field_pic= s->picture_structure != PICT_FRAME; int offset[4]; - h= FFMIN(h, (s->avctx->height>>field_pic) - y); - - if(field_pic && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)){ - h <<= 1; - y <<= 1; - if(s->first_field) return; - } - if(s->pict_type==FF_B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER)) src= (AVFrame*)s->current_picture_ptr; else if(s->last_picture_ptr) @@ -2174,7 +2342,7 @@ void ff_mpeg_flush(AVCodecContext *avctx){ if(s==NULL || s->picture==NULL) return; - for(i=0; i<MAX_PICTURE_COUNT; i++){ + for(i=0; i<s->picture_count; i++){ if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL || s->picture[i].type == FF_BUFFER_TYPE_USER)) free_frame_buffer(s, &s->picture[i]); @@ -2428,3 +2596,9 @@ void ff_set_qscale(MpegEncContext * s, int qscale) s->y_dc_scale= s->y_dc_scale_table[ qscale ]; s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ]; } + +void MPV_report_decode_progress(MpegEncContext *s) +{ + if (s->pict_type != FF_B_TYPE && !s->partitioned_frame) + ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0); +} |