From d375c1040032ed42f84b7d4ea53baad4a661b628 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Tue, 22 Mar 2011 22:36:57 +0100 Subject: Fake-Merge remote-tracking branch 'ffmpeg-mt/master' --- libavcodec/avcodec.h | 4 + libavcodec/dsputil.c | 19 +- libavcodec/dsputil.h | 4 +- libavcodec/h263dec.c | 5 + libavcodec/h264.c | 686 ++++++++++++++++++++++++++++++++----------- libavcodec/h264.h | 4 +- libavcodec/h264_direct.c | 41 ++- libavcodec/mdec.c | 20 +- libavcodec/mimic.c | 60 +++- libavcodec/mpeg12.c | 36 ++- libavcodec/mpeg4videodec.c | 20 +- libavcodec/mpegvideo.c | 264 ++++++++++++++--- libavcodec/mpegvideo.h | 7 + libavcodec/mpegvideo_enc.c | 24 +- libavcodec/snow.c | 6 +- libavcodec/utils.c | 9 +- libavcodec/vp8.c | 10 +- libavcodec/x86/dsputil_mmx.c | 63 ++-- 18 files changed, 986 insertions(+), 296 deletions(-) (limited to 'libavcodec') diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 3ea1e76989..0f9201d8e4 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -725,10 +725,12 @@ typedef struct RcOverride{ * Codec should fill in channel configuration and samplerate instead of container */ #define CODEC_CAP_CHANNEL_CONF 0x0400 + /** * Codec is able to deal with negative linesizes */ #define CODEC_CAP_NEG_LINESIZES 0x0800 + /** * Codec supports frame-level multithreading. */ @@ -2983,7 +2985,9 @@ typedef struct AVCodec { const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1 const int64_t *channel_layouts; ///< array of support channel layouts, or NULL if unknown. array is terminated by 0 uint8_t max_lowres; ///< maximum value for lowres supported by the decoder + AVClass *priv_class; ///< AVClass for the private context + const AVProfile *profiles; ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN} /** diff --git a/libavcodec/dsputil.c b/libavcodec/dsputil.c index b293642b1d..d4e538cd07 100644 --- a/libavcodec/dsputil.c +++ b/libavcodec/dsputil.c @@ -298,7 +298,7 @@ static int sse16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h) /* draw the edges of width 'w' of an image of size width, height */ //FIXME check that this is ok for mpeg4 interlaced -static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w) +static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w, int sides) { uint8_t *ptr, *last_line; int i; @@ -306,8 +306,8 @@ static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w) last_line = buf + (height - 1) * wrap; for(i=0;imb_x >= s->mb_width){ s->mb_x=0; ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size); + MPV_report_decode_progress(s); s->mb_y++; } return 0; @@ -255,6 +257,7 @@ static int decode_slice(MpegEncContext *s){ } ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size); + MPV_report_decode_progress(s); s->mb_x= 0; } @@ -639,6 +642,8 @@ retry: if(MPV_frame_start(s, avctx) < 0) return -1; + if (!s->divx_packed) ff_thread_finish_setup(avctx); + if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) { ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer); goto frame_end; diff --git a/libavcodec/h264.c b/libavcodec/h264.c index 5ebf929ee9..d25b310e52 100644 --- a/libavcodec/h264.c +++ b/libavcodec/h264.c @@ -36,6 +36,7 @@ #include "golomb.h" #include "mathops.h" #include "rectangle.h" +#include "thread.h" #include "vdpau_internal.h" #include "libavutil/avassert.h" @@ -249,6 +250,141 @@ static int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){ return 0; } +static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n, int height, + int y_offset, int list){ + int raw_my= h->mv_cache[list][ scan8[n] ][1]; + int filter_height= (raw_my&3) ? 2 : 0; + int full_my= (raw_my>>2) + y_offset; + int top = full_my - filter_height, bottom = full_my + height + filter_height; + + return FFMAX(abs(top), bottom); +} + +static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n, int height, + int y_offset, int list0, int list1, int *nrefs){ + MpegEncContext * const s = &h->s; + int my; + + y_offset += 16*(s->mb_y >> MB_FIELD); + + if(list0){ + int ref_n = h->ref_cache[0][ scan8[n] ]; + Picture *ref= &h->ref_list[0][ref_n]; + + // Error resilience puts the current picture in the ref list. + // Don't try to wait on these as it will cause a deadlock. + // Fields can wait on each other, though. + if(ref->thread_opaque != s->current_picture.thread_opaque || + (ref->reference&3) != s->picture_structure) { + my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0); + if (refs[0][ref_n] < 0) nrefs[0] += 1; + refs[0][ref_n] = FFMAX(refs[0][ref_n], my); + } + } + + if(list1){ + int ref_n = h->ref_cache[1][ scan8[n] ]; + Picture *ref= &h->ref_list[1][ref_n]; + + if(ref->thread_opaque != s->current_picture.thread_opaque || + (ref->reference&3) != s->picture_structure) { + my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1); + if (refs[1][ref_n] < 0) nrefs[1] += 1; + refs[1][ref_n] = FFMAX(refs[1][ref_n], my); + } + } +} + +/** + * Wait until all reference frames are available for MC operations. + * + * @param h the H264 context + */ +static void await_references(H264Context *h){ + MpegEncContext * const s = &h->s; + const int mb_xy= h->mb_xy; + const int mb_type= s->current_picture.mb_type[mb_xy]; + int refs[2][48]; + int nrefs[2] = {0}; + int ref, list; + + memset(refs, -1, sizeof(refs)); + + if(IS_16X16(mb_type)){ + get_lowest_part_y(h, refs, 0, 16, 0, + IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); + }else if(IS_16X8(mb_type)){ + get_lowest_part_y(h, refs, 0, 8, 0, + IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); + get_lowest_part_y(h, refs, 8, 8, 8, + IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs); + }else if(IS_8X16(mb_type)){ + get_lowest_part_y(h, refs, 0, 16, 0, + IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); + get_lowest_part_y(h, refs, 4, 16, 0, + IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs); + }else{ + int i; + + assert(IS_8X8(mb_type)); + + for(i=0; i<4; i++){ + const int sub_mb_type= h->sub_mb_type[i]; + const int n= 4*i; + int y_offset= (i&2)<<2; + + if(IS_SUB_8X8(sub_mb_type)){ + get_lowest_part_y(h, refs, n , 8, y_offset, + IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); + }else if(IS_SUB_8X4(sub_mb_type)){ + get_lowest_part_y(h, refs, n , 4, y_offset, + IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); + get_lowest_part_y(h, refs, n+2, 4, y_offset+4, + IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); + }else if(IS_SUB_4X8(sub_mb_type)){ + get_lowest_part_y(h, refs, n , 8, y_offset, + IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); + get_lowest_part_y(h, refs, n+1, 8, y_offset, + IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); + }else{ + int j; + assert(IS_SUB_4X4(sub_mb_type)); + for(j=0; j<4; j++){ + int sub_y_offset= y_offset + 2*(j&2); + get_lowest_part_y(h, refs, n+j, 4, sub_y_offset, + IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); + } + } + } + } + + for(list=h->list_count-1; list>=0; list--){ + for(ref=0; ref<48 && nrefs[list]; ref++){ + int row = refs[list][ref]; + if(row >= 0){ + Picture *ref_pic = &h->ref_list[list][ref]; + int ref_field = ref_pic->reference - 1; + int ref_field_picture = ref_pic->field_picture; + int pic_height = 16*s->mb_height >> ref_field_picture; + + row <<= MB_MBAFF; + nrefs[list]--; + + if(!FIELD_PICTURE && ref_field_picture){ // frame referencing two fields + ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1); + ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) , pic_height-1), 0); + }else if(FIELD_PICTURE && !ref_field_picture){ // field referencing one field of a frame + ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field , pic_height-1), 0); + }else if(FIELD_PICTURE){ + ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field); + }else{ + ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0); + } + } + } + } +} + #if 0 /** * DCT transforms the 16 dc values. @@ -539,6 +675,8 @@ static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t assert(IS_INTER(mb_type)); + if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) + await_references(h); prefetch_motion(h, 0); if(IS_16X16(mb_type)){ @@ -887,7 +1025,7 @@ av_cold int ff_h264_decode_init(AVCodecContext *avctx){ ff_h264_decode_init_vlc(); h->thread_context[0] = h; - h->outputed_poc = INT_MIN; + h->outputed_poc = h->next_outputed_poc = INT_MIN; h->prev_poc_msb= 1<<16; h->x264_build = -1; ff_h264_reset_sei(h); @@ -910,6 +1048,125 @@ av_cold int ff_h264_decode_init(AVCodecContext *avctx){ return 0; } +static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base) +{ + int i; + + for (i=0; ipriv_data; + + if (!avctx->is_copy) return 0; + memset(h->sps_buffers, 0, sizeof(h->sps_buffers)); + memset(h->pps_buffers, 0, sizeof(h->pps_buffers)); + + return 0; +} + +#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field) +static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){ + H264Context *h= dst->priv_data, *h1= src->priv_data; + MpegEncContext * const s = &h->s, * const s1 = &h1->s; + int inited = s->context_initialized, err; + int i; + + if(dst == src || !s1->context_initialized) return 0; + + err = ff_mpeg_update_thread_context(dst, src); + if(err) return err; + + //FIXME handle width/height changing + if(!inited){ + for(i = 0; i < MAX_SPS_COUNT; i++) + av_freep(h->sps_buffers + i); + + for(i = 0; i < MAX_PPS_COUNT; i++) + av_freep(h->pps_buffers + i); + + memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc + memset(h->sps_buffers, 0, sizeof(h->sps_buffers)); + memset(h->pps_buffers, 0, sizeof(h->pps_buffers)); + ff_h264_alloc_tables(h); + context_init(h); + + for(i=0; i<2; i++){ + h->rbsp_buffer[i] = NULL; + h->rbsp_buffer_size[i] = 0; + } + + h->thread_context[0] = h; + + // frame_start may not be called for the next thread (if it's decoding a bottom field) + // so this has to be allocated here + h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize); + + s->dsp.clear_blocks(h->mb); + } + + //extradata/NAL handling + h->is_avc = h1->is_avc; + + //SPS/PPS + copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS)); + h->sps = h1->sps; + copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS)); + h->pps = h1->pps; + + //Dequantization matrices + //FIXME these are big - can they be only copied when PPS changes? + copy_fields(h, h1, dequant4_buffer, dequant4_coeff); + + for(i=0; i<6; i++) + h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]); + + for(i=0; i<2; i++) + h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]); + + h->dequant_coeff_pps = h1->dequant_coeff_pps; + + //POC timing + copy_fields(h, h1, poc_lsb, redundant_pic_count); + + //reference lists + copy_fields(h, h1, ref_count, intra_gb); + copy_fields(h, h1, short_ref, cabac_init_idc); + + copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1); + copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1); + copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1); + + h->last_slice_type = h1->last_slice_type; + + if(!s->current_picture_ptr) return 0; + + if(!s->dropable) { + ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index); + h->prev_poc_msb = h->poc_msb; + h->prev_poc_lsb = h->poc_lsb; + } + h->prev_frame_num_offset= h->frame_num_offset; + h->prev_frame_num = h->frame_num; + h->outputed_poc = h->next_outputed_poc; + + return 0; +} + int ff_h264_frame_start(H264Context *h){ MpegEncContext * const s = &h->s; int i; @@ -961,11 +1218,167 @@ int ff_h264_frame_start(H264Context *h){ s->current_picture_ptr->field_poc[0]= s->current_picture_ptr->field_poc[1]= INT_MAX; + + h->next_output_pic = NULL; + assert(s->current_picture_ptr->long_ref==0); return 0; } +/** + * Run setup operations that must be run after slice header decoding. + * This includes finding the next displayed frame. + * + * @param h h264 master context + */ +static void decode_postinit(H264Context *h){ + MpegEncContext * const s = &h->s; + Picture *out = s->current_picture_ptr; + Picture *cur = s->current_picture_ptr; + int i, pics, out_of_order, out_idx; + + s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264; + s->current_picture_ptr->pict_type= s->pict_type; + + if (h->next_output_pic) return; + + if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) { + //FIXME this allows the next thread to start once we encounter the first field of a PAFF packet + //This works if the next packet contains the second field. It does not work if both fields are + //in the same packet. + //ff_thread_finish_setup(s->avctx); + return; + } + + cur->interlaced_frame = 0; + cur->repeat_pict = 0; + + /* Signal interlacing information externally. */ + /* Prioritize picture timing SEI information over used decoding process if it exists. */ + + if(h->sps.pic_struct_present_flag){ + switch (h->sei_pic_struct) + { + case SEI_PIC_STRUCT_FRAME: + break; + case SEI_PIC_STRUCT_TOP_FIELD: + case SEI_PIC_STRUCT_BOTTOM_FIELD: + cur->interlaced_frame = 1; + break; + case SEI_PIC_STRUCT_TOP_BOTTOM: + case SEI_PIC_STRUCT_BOTTOM_TOP: + if (FIELD_OR_MBAFF_PICTURE) + cur->interlaced_frame = 1; + else + // try to flag soft telecine progressive + cur->interlaced_frame = h->prev_interlaced_frame; + break; + case SEI_PIC_STRUCT_TOP_BOTTOM_TOP: + case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM: + // Signal the possibility of telecined film externally (pic_struct 5,6) + // From these hints, let the applications decide if they apply deinterlacing. + cur->repeat_pict = 1; + break; + case SEI_PIC_STRUCT_FRAME_DOUBLING: + // Force progressive here, as doubling interlaced frame is a bad idea. + cur->repeat_pict = 2; + break; + case SEI_PIC_STRUCT_FRAME_TRIPLING: + cur->repeat_pict = 4; + break; + } + + if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP) + cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0; + }else{ + /* Derive interlacing flag from used decoding process. */ + cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE; + } + h->prev_interlaced_frame = cur->interlaced_frame; + + if (cur->field_poc[0] != cur->field_poc[1]){ + /* Derive top_field_first from field pocs. */ + cur->top_field_first = cur->field_poc[0] < cur->field_poc[1]; + }else{ + if(cur->interlaced_frame || h->sps.pic_struct_present_flag){ + /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */ + if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM + || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP) + cur->top_field_first = 1; + else + cur->top_field_first = 0; + }else{ + /* Most likely progressive */ + cur->top_field_first = 0; + } + } + + //FIXME do something with unavailable reference frames + + /* Sort B-frames into display order */ + + if(h->sps.bitstream_restriction_flag + && s->avctx->has_b_frames < h->sps.num_reorder_frames){ + s->avctx->has_b_frames = h->sps.num_reorder_frames; + s->low_delay = 0; + } + + if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT + && !h->sps.bitstream_restriction_flag){ + s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT; + s->low_delay= 0; + } + + pics = 0; + while(h->delayed_pic[pics]) pics++; + + assert(pics <= MAX_DELAYED_PIC_COUNT); + + h->delayed_pic[pics++] = cur; + if(cur->reference == 0) + cur->reference = DELAYED_PIC_REF; + + out = h->delayed_pic[0]; + out_idx = 0; + for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++) + if(h->delayed_pic[i]->poc < out->poc){ + out = h->delayed_pic[i]; + out_idx = i; + } + if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) + h->next_outputed_poc= INT_MIN; + out_of_order = out->poc < h->next_outputed_poc; + + if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames) + { } + else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) + || (s->low_delay && + ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2) + || cur->pict_type == FF_B_TYPE))) + { + s->low_delay = 0; + s->avctx->has_b_frames++; + } + + if(out_of_order || pics > s->avctx->has_b_frames){ + out->reference &= ~DELAYED_PIC_REF; + for(i=out_idx; h->delayed_pic[i]; i++) + h->delayed_pic[i] = h->delayed_pic[i+1]; + } + if(!out_of_order && pics > s->avctx->has_b_frames){ + h->next_output_pic = out; + if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) { + h->next_outputed_poc = INT_MIN; + } else + h->next_outputed_poc = out->poc; + }else{ + av_log(s->avctx, AV_LOG_DEBUG, "no picture\n"); + } + + ff_thread_finish_setup(s->avctx); +} + static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){ MpegEncContext * const s = &h->s; uint8_t *top_border; @@ -1479,7 +1892,7 @@ static void flush_dpb(AVCodecContext *avctx){ h->delayed_pic[i]->reference= 0; h->delayed_pic[i]= NULL; } - h->outputed_poc= INT_MIN; + h->outputed_poc=h->next_outputed_poc= INT_MIN; h->prev_interlaced_frame = 1; idr(h); if(h->s.current_picture_ptr) @@ -1603,24 +2016,28 @@ static void init_scan_tables(H264Context *h){ } } -static void field_end(H264Context *h){ +static void field_end(H264Context *h, int in_setup){ MpegEncContext * const s = &h->s; AVCodecContext * const avctx= s->avctx; s->mb_y= 0; - s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264; - s->current_picture_ptr->pict_type= s->pict_type; + if (!in_setup && !s->dropable) + ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1, + s->picture_structure==PICT_BOTTOM_FIELD); if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) ff_vdpau_h264_set_reference_frames(s); - if(!s->dropable) { - ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index); - h->prev_poc_msb= h->poc_msb; - h->prev_poc_lsb= h->poc_lsb; + if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){ + if(!s->dropable) { + ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index); + h->prev_poc_msb= h->poc_msb; + h->prev_poc_lsb= h->poc_lsb; + } + h->prev_frame_num_offset= h->frame_num_offset; + h->prev_frame_num= h->frame_num; + h->outputed_poc = h->next_outputed_poc; } - h->prev_frame_num_offset= h->frame_num_offset; - h->prev_frame_num= h->frame_num; if (avctx->hwaccel) { if (avctx->hwaccel->end_frame(avctx) < 0) @@ -1737,7 +2154,7 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ if(first_mb_in_slice == 0){ //FIXME better field boundary detection if(h0->current_slice && FIELD_PICTURE){ - field_end(h); + field_end(h, 1); } h0->current_slice = 0; @@ -1806,8 +2223,10 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ if (s->context_initialized && ( s->width != s->avctx->width || s->height != s->avctx->height || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) { - if(h != h0) + if(h != h0) { + av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0); return -1; // width / height changed during parallelized decoding + } free_tables(h, 0); flush_dpb(s->avctx); MPV_common_end(s); @@ -1852,21 +2271,26 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ init_scan_tables(h); ff_h264_alloc_tables(h); - for(i = 1; i < s->avctx->thread_count; i++) { - H264Context *c; - c = h->thread_context[i] = av_malloc(sizeof(H264Context)); - memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext)); - memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext)); - c->h264dsp = h->h264dsp; - c->sps = h->sps; - c->pps = h->pps; - init_scan_tables(c); - clone_tables(c, h, i); - } - - for(i = 0; i < s->avctx->thread_count; i++) - if(context_init(h->thread_context[i]) < 0) + if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) { + if (context_init(h) < 0) return -1; + } else { + for(i = 1; i < s->avctx->thread_count; i++) { + H264Context *c; + c = h->thread_context[i] = av_malloc(sizeof(H264Context)); + memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext)); + memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext)); + c->h264dsp = h->h264dsp; + c->sps = h->sps; + c->pps = h->pps; + init_scan_tables(c); + clone_tables(c, h, i); + } + + for(i = 0; i < s->avctx->thread_count; i++) + if(context_init(h->thread_context[i]) < 0) + return -1; + } } h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num); @@ -1887,6 +2311,10 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME; if(h0->current_slice == 0){ + if(h->frame_num != h->prev_frame_num && + (h->prev_frame_num+1)%(1<sps.log2_max_frame_num) < (h->frame_num - h->sps.ref_frame_count)) + h->prev_frame_num = h->frame_num - h->sps.ref_frame_count - 1; + while(h->frame_num != h->prev_frame_num && h->frame_num != (h->prev_frame_num+1)%(1<sps.log2_max_frame_num)){ Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL; @@ -1896,6 +2324,8 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ h->prev_frame_num++; h->prev_frame_num %= 1<sps.log2_max_frame_num; s->current_picture_ptr->frame_num= h->prev_frame_num; + ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0); + ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1); ff_generate_sliding_window_mmcos(h); ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index); /* Error concealment: if a ref is missing, copy the previous ref in its place. @@ -2045,6 +2475,9 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0) return -1; + //FIXME mt gives valgrind warnings and crashes if this is uncommented + /* + if(h->slice_type_nos!=FF_I_TYPE){ s->last_picture_ptr= &h->ref_list[0][0]; ff_copy_picture(&s->last_picture, s->last_picture_ptr); @@ -2054,6 +2487,8 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ ff_copy_picture(&s->next_picture, s->next_picture_ptr); } + */ + if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE ) || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) ) pred_weight_table(h); @@ -2200,7 +2635,8 @@ static int decode_slice_header(H264Context *h, H264Context *h0){ +(h->ref_list[j][i].reference&3); } - h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; + //FIXME: fix draw_edges+PAFF+frame threads + h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type&FF_THREAD_FRAME)) ? 0 : 16; h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width; if(s->avctx->debug&FF_DEBUG_PICT_INFO){ @@ -2521,6 +2957,40 @@ static void predict_field_decoding_flag(H264Context *h){ h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0; } +/** + * Draw edges and report progress for the last MB row. + */ +static void decode_finish_row(H264Context *h){ + MpegEncContext * const s = &h->s; + int top = 16*(s->mb_y >> FIELD_PICTURE); + int height = 16 << FRAME_MBAFF; + int deblock_border = (16 + 4) << FRAME_MBAFF; + int pic_height = 16*s->mb_height >> FIELD_PICTURE; + + if (h->deblocking_filter) { + if((top + height) >= pic_height) + height += deblock_border; + + top -= deblock_border; + } + + if (top >= pic_height || (top + height) < h->emu_edge_height) + return; + + height = FFMIN(height, pic_height - top); + if (top < h->emu_edge_height) { + height = top+height; + top = 0; + } + + ff_draw_horiz_band(s, top, height); + + if (s->dropable) return; + + ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1, + s->picture_structure==PICT_BOTTOM_FIELD); +} + static int decode_slice(struct AVCodecContext *avctx, void *arg){ H264Context *h = *(void**)arg; MpegEncContext * const s = &h->s; @@ -2574,7 +3044,7 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg){ if( ++s->mb_x >= s->mb_width ) { s->mb_x = 0; loop_filter(h); - ff_draw_horiz_band(s, 16*s->mb_y, 16); + decode_finish_row(h); ++s->mb_y; if(FIELD_OR_MBAFF_PICTURE) { ++s->mb_y; @@ -2614,7 +3084,7 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg){ if(++s->mb_x >= s->mb_width){ s->mb_x=0; loop_filter(h); - ff_draw_horiz_band(s, 16*s->mb_y, 16); + decode_finish_row(h); ++s->mb_y; if(FIELD_OR_MBAFF_PICTURE) { ++s->mb_y; @@ -2747,7 +3217,7 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){ int context_count = 0; int next_avc= h->is_avc ? 0 : buf_size; - h->max_contexts = avctx->thread_count; + h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1; #if 0 int i; for(i=0; i<50; i++){ @@ -2842,16 +3312,21 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){ if((err = decode_slice_header(hx, h))) break; + s->current_picture_ptr->key_frame |= + (hx->nal_unit_type == NAL_IDR_SLICE) || + (h->sei_recovery_frame_cnt >= 0); + if (h->current_slice == 1) { + if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) { + decode_postinit(h); + } + if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0) return -1; if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) ff_vdpau_h264_picture_start(s); } - s->current_picture_ptr->key_frame |= - (hx->nal_unit_type == NAL_IDR_SLICE) || - (h->sei_recovery_frame_cnt >= 0); if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5 && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE) @@ -2981,6 +3456,8 @@ static int decode_frame(AVCodecContext *avctx, Picture *out; int i, out_idx; + s->current_picture_ptr = NULL; + //FIXME factorize this with the output code below out = h->delayed_pic[0]; out_idx = 0; @@ -3017,143 +3494,18 @@ static int decode_frame(AVCodecContext *avctx, } if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){ - Picture *out = s->current_picture_ptr; - Picture *cur = s->current_picture_ptr; - int i, pics, out_of_order, out_idx; - field_end(h); + if(s->flags2 & CODEC_FLAG2_CHUNKS) decode_postinit(h); - if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) { + field_end(h, 0); + + if (!h->next_output_pic) { /* Wait for second field. */ *data_size = 0; } else { - cur->interlaced_frame = 0; - cur->repeat_pict = 0; - - /* Signal interlacing information externally. */ - /* Prioritize picture timing SEI information over used decoding process if it exists. */ - - if(h->sps.pic_struct_present_flag){ - switch (h->sei_pic_struct) - { - case SEI_PIC_STRUCT_FRAME: - break; - case SEI_PIC_STRUCT_TOP_FIELD: - case SEI_PIC_STRUCT_BOTTOM_FIELD: - cur->interlaced_frame = 1; - break; - case SEI_PIC_STRUCT_TOP_BOTTOM: - case SEI_PIC_STRUCT_BOTTOM_TOP: - if (FIELD_OR_MBAFF_PICTURE) - cur->interlaced_frame = 1; - else - // try to flag soft telecine progressive - cur->interlaced_frame = h->prev_interlaced_frame; - break; - case SEI_PIC_STRUCT_TOP_BOTTOM_TOP: - case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM: - // Signal the possibility of telecined film externally (pic_struct 5,6) - // From these hints, let the applications decide if they apply deinterlacing. - cur->repeat_pict = 1; - break; - case SEI_PIC_STRUCT_FRAME_DOUBLING: - // Force progressive here, as doubling interlaced frame is a bad idea. - cur->repeat_pict = 2; - break; - case SEI_PIC_STRUCT_FRAME_TRIPLING: - cur->repeat_pict = 4; - break; - } - - if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP) - cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0; - }else{ - /* Derive interlacing flag from used decoding process. */ - cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE; - } - h->prev_interlaced_frame = cur->interlaced_frame; - - if (cur->field_poc[0] != cur->field_poc[1]){ - /* Derive top_field_first from field pocs. */ - cur->top_field_first = cur->field_poc[0] < cur->field_poc[1]; - }else{ - if(cur->interlaced_frame || h->sps.pic_struct_present_flag){ - /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */ - if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM - || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP) - cur->top_field_first = 1; - else - cur->top_field_first = 0; - }else{ - /* Most likely progressive */ - cur->top_field_first = 0; - } - } - - //FIXME do something with unavailable reference frames - - /* Sort B-frames into display order */ - - if(h->sps.bitstream_restriction_flag - && s->avctx->has_b_frames < h->sps.num_reorder_frames){ - s->avctx->has_b_frames = h->sps.num_reorder_frames; - s->low_delay = 0; - } - - if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT - && !h->sps.bitstream_restriction_flag){ - s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT; - s->low_delay= 0; - } - - pics = 0; - while(h->delayed_pic[pics]) pics++; - - assert(pics <= MAX_DELAYED_PIC_COUNT); - - h->delayed_pic[pics++] = cur; - if(cur->reference == 0) - cur->reference = DELAYED_PIC_REF; - - out = h->delayed_pic[0]; - out_idx = 0; - for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++) - if(h->delayed_pic[i]->poc < out->poc){ - out = h->delayed_pic[i]; - out_idx = i; - } - if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) - h->outputed_poc= INT_MIN; - out_of_order = out->poc < h->outputed_poc; - - if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames) - { } - else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) - || (s->low_delay && - ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2) - || cur->pict_type == FF_B_TYPE))) - { - s->low_delay = 0; - s->avctx->has_b_frames++; - } - - if(out_of_order || pics > s->avctx->has_b_frames){ - out->reference &= ~DELAYED_PIC_REF; - for(i=out_idx; h->delayed_pic[i]; i++) - h->delayed_pic[i] = h->delayed_pic[i+1]; - } - if(!out_of_order && pics > s->avctx->has_b_frames){ - *data_size = sizeof(AVFrame); - - if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) { - h->outputed_poc = INT_MIN; - } else - h->outputed_poc = out->poc; - *pict= *(AVFrame*)out; - }else{ - av_log(avctx, AV_LOG_DEBUG, "no picture\n"); - } + *data_size = sizeof(AVFrame); + *pict = *(AVFrame*)h->next_output_pic; } } @@ -3412,9 +3764,11 @@ AVCodec ff_h264_decoder = { NULL, ff_h264_decode_end, decode_frame, - /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY, + /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS, .flush= flush_dpb, .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"), + .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy), + .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context), .profiles = NULL_IF_CONFIG_SMALL(profiles), }; diff --git a/libavcodec/h264.h b/libavcodec/h264.h index b403968485..770a7ecb59 100644 --- a/libavcodec/h264.h +++ b/libavcodec/h264.h @@ -392,9 +392,9 @@ typedef struct H264Context{ /** * num_ref_idx_l0/1_active_minus1 + 1 */ + uint8_t *list_counts; ///< Array of list_count per MB specifying the slice type unsigned int ref_count[2]; ///< counts frames or fields, depending on current mb mode unsigned int list_count; - uint8_t *list_counts; ///< Array of list_count per MB specifying the slice type Picture ref_list[2][48]; /**< 0..15: frame refs, 16..47: mbaff field refs. Reordered version of default_ref_list according to picture reordering in slice header */ @@ -504,7 +504,9 @@ typedef struct H264Context{ Picture *long_ref[32]; Picture default_ref_list[2][32]; ///< base reference list for all slices of a coded picture Picture *delayed_pic[MAX_DELAYED_PIC_COUNT+2]; //FIXME size? + Picture *next_output_pic; int outputed_poc; + int next_outputed_poc; /** * memory management control operations buffer. diff --git a/libavcodec/h264_direct.c b/libavcodec/h264_direct.c index d22780d925..a26a51a330 100644 --- a/libavcodec/h264_direct.c +++ b/libavcodec/h264_direct.c @@ -31,6 +31,7 @@ #include "mpegvideo.h" #include "h264.h" #include "rectangle.h" +#include "thread.h" //#undef NDEBUG #include @@ -126,7 +127,7 @@ void ff_h264_direct_ref_list_init(H264Context * const h){ h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc)); ref1sidx=sidx= h->col_parity; }else if(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){ // FL -> FL & differ parity - h->col_fieldoff= s->mb_stride*(2*(h->ref_list[1][0].reference) - 3); + h->col_fieldoff= 2*(h->ref_list[1][0].reference) - 3; } if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred) @@ -140,11 +141,27 @@ void ff_h264_direct_ref_list_init(H264Context * const h){ } } +static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y) +{ + int ref_field = ref->reference - 1; + int ref_field_picture = ref->field_picture; + int ref_height = 16*h->s.mb_height >> ref_field_picture; + + if(!HAVE_PTHREADS || !(h->s.avctx->active_thread_type&FF_THREAD_FRAME)) + return; + + //FIXME it can be safe to access mb stuff + //even if pixels aren't deblocked yet + + ff_thread_await_progress((AVFrame*)ref, FFMIN(16*mb_y >> ref_field_picture, ref_height-1), + ref_field_picture && ref_field); +} + static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){ MpegEncContext * const s = &h->s; int b8_stride = 2; int b4_stride = h->b_stride; - int mb_xy = h->mb_xy; + int mb_xy = h->mb_xy, mb_y = s->mb_y; int mb_type_col[2]; const int16_t (*l1mv0)[2], (*l1mv1)[2]; const int8_t *l1ref0, *l1ref1; @@ -157,6 +174,8 @@ static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){ assert(h->ref_list[1][0].reference&3); + await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type)); + #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM) @@ -217,14 +236,17 @@ static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){ if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL + mb_y = (s->mb_y&~1) + h->col_parity; mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride; b8_stride = 0; }else{ - mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity + mb_y += h->col_fieldoff; + mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity } goto single_col; }else{ // AFL/AFR/FR/FL -> AFR/FR if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR + mb_y = s->mb_y&~1; mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride; mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy]; mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride]; @@ -260,6 +282,8 @@ single_col: } } + await_reference_mb_row(h, &h->ref_list[1][0], mb_y); + l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]]; l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]]; l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy]; @@ -384,7 +408,7 @@ static void pred_temp_direct_motion(H264Context * const h, int *mb_type){ MpegEncContext * const s = &h->s; int b8_stride = 2; int b4_stride = h->b_stride; - int mb_xy = h->mb_xy; + int mb_xy = h->mb_xy, mb_y = s->mb_y; int mb_type_col[2]; const int16_t (*l1mv0)[2], (*l1mv1)[2]; const int8_t *l1ref0, *l1ref1; @@ -394,16 +418,21 @@ static void pred_temp_direct_motion(H264Context * const h, int *mb_type){ assert(h->ref_list[1][0].reference&3); + await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type)); + if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL + mb_y = (s->mb_y&~1) + h->col_parity; mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride; b8_stride = 0; }else{ - mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity + mb_y += h->col_fieldoff; + mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity } goto single_col; }else{ // AFL/AFR/FR/FL -> AFR/FR if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR + mb_y = s->mb_y&~1; mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride; mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy]; mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride]; @@ -440,6 +469,8 @@ single_col: } } + await_reference_mb_row(h, &h->ref_list[1][0], mb_y); + l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]]; l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]]; l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy]; diff --git a/libavcodec/mdec.c b/libavcodec/mdec.c index 12d1751172..b28ac26913 100644 --- a/libavcodec/mdec.c +++ b/libavcodec/mdec.c @@ -31,6 +31,7 @@ #include "dsputil.h" #include "mpegvideo.h" #include "mpeg12.h" +#include "thread.h" typedef struct MDECContext{ AVCodecContext *avctx; @@ -162,10 +163,10 @@ static int decode_frame(AVCodecContext *avctx, int i; if(p->data[0]) - avctx->release_buffer(avctx, p); + ff_thread_release_buffer(avctx, p); p->reference= 0; - if(avctx->get_buffer(avctx, p) < 0){ + if(ff_thread_get_buffer(avctx, p) < 0){ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return -1; } @@ -238,6 +239,18 @@ static av_cold int decode_init(AVCodecContext *avctx){ return 0; } +static av_cold int decode_init_thread_copy(AVCodecContext *avctx){ + MDECContext * const a = avctx->priv_data; + AVFrame *p = (AVFrame*)&a->picture; + + avctx->coded_frame= p; + a->avctx= avctx; + + p->qscale_table= av_mallocz( p->qstride * a->mb_height); + + return 0; +} + static av_cold int decode_end(AVCodecContext *avctx){ MDECContext * const a = avctx->priv_data; @@ -259,7 +272,8 @@ AVCodec ff_mdec_decoder = { NULL, decode_end, decode_frame, - CODEC_CAP_DR1, + CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS, .long_name= NULL_IF_CONFIG_SMALL("Sony PlayStation MDEC (Motion DECoder)"), + .init_thread_copy= ONLY_IF_THREADS_ENABLED(decode_init_thread_copy) }; diff --git a/libavcodec/mimic.c b/libavcodec/mimic.c index 450dd65784..2f3f9d9930 100644 --- a/libavcodec/mimic.c +++ b/libavcodec/mimic.c @@ -27,6 +27,7 @@ #include "get_bits.h" #include "bytestream.h" #include "dsputil.h" +#include "thread.h" #define MIMIC_HEADER_SIZE 20 @@ -51,6 +52,10 @@ typedef struct { ScanTable scantable; DSPContext dsp; VLC vlc; + + /* Kept in the context so multithreading can have a constant to read from */ + int next_cur_index; + int next_prev_index; } MimicContext; static const uint32_t huffcodes[] = { @@ -121,6 +126,23 @@ static av_cold int mimic_decode_init(AVCodecContext *avctx) return 0; } +static int mimic_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from) +{ + MimicContext *dst = avctx->priv_data, *src = avctx_from->priv_data; + + if (avctx == avctx_from) return 0; + + dst->cur_index = src->next_cur_index; + dst->prev_index = src->next_prev_index; + + memcpy(dst->buf_ptrs, src->buf_ptrs, sizeof(src->buf_ptrs)); + memcpy(dst->flipped_ptrs, src->flipped_ptrs, sizeof(src->flipped_ptrs)); + + memset(&dst->buf_ptrs[dst->cur_index], 0, sizeof(AVFrame)); + + return 0; +} + static const int8_t vlcdec_lookup[9][64] = { { 0, }, { -1, 1, }, @@ -205,7 +227,7 @@ static int vlc_decode_block(MimicContext *ctx, int num_coeffs, int qscale) static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe) { - int y, x, plane; + int y, x, plane, cur_row = 0; for(plane = 0; plane < 3; plane++) { const int is_chroma = !!plane; @@ -236,6 +258,7 @@ static int decode(MimicContext *ctx, int quality, int num_coeffs, int index = (ctx->cur_index+backref)&15; uint8_t *p = ctx->flipped_ptrs[index].data[0]; + ff_thread_await_progress(&ctx->buf_ptrs[index], cur_row, 0); if(p) { p += src - ctx->flipped_ptrs[ctx->prev_index].data[plane]; @@ -246,6 +269,7 @@ static int decode(MimicContext *ctx, int quality, int num_coeffs, } } } else { + ff_thread_await_progress(&ctx->buf_ptrs[ctx->prev_index], cur_row, 0); ctx->dsp.put_pixels_tab[1][0](dst, src, stride, 8); } src += 8; @@ -253,6 +277,8 @@ static int decode(MimicContext *ctx, int quality, int num_coeffs, } src += (stride - ctx->num_hblocks[plane])<<3; dst += (stride - ctx->num_hblocks[plane])<<3; + + ff_thread_report_progress(&ctx->buf_ptrs[ctx->cur_index], cur_row++, 0); } } @@ -326,14 +352,20 @@ static int mimic_decode_frame(AVCodecContext *avctx, void *data, } ctx->buf_ptrs[ctx->cur_index].reference = 1; - if(avctx->get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) { + ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? FF_P_TYPE:FF_I_TYPE; + if(ff_thread_get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return -1; } + ctx->next_prev_index = ctx->cur_index; + ctx->next_cur_index = (ctx->cur_index - 1) & 15; + prepare_avpic(ctx, &ctx->flipped_ptrs[ctx->cur_index], (AVPicture*) &ctx->buf_ptrs[ctx->cur_index]); + ff_thread_finish_setup(avctx); + av_fast_malloc(&ctx->swap_buf, &ctx->swap_buf_size, swap_buf_size + FF_INPUT_BUFFER_PADDING_SIZE); if(!ctx->swap_buf) @@ -345,21 +377,23 @@ static int mimic_decode_frame(AVCodecContext *avctx, void *data, init_get_bits(&ctx->gb, ctx->swap_buf, swap_buf_size << 3); if(!decode(ctx, quality, num_coeffs, !is_pframe)) { - avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]); - return -1; + if (avctx->active_thread_type&FF_THREAD_FRAME) + ff_thread_report_progress(&ctx->buf_ptrs[ctx->cur_index], INT_MAX, 0); + else { + ff_thread_release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]); + return -1; + } } - ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? FF_P_TYPE:FF_I_TYPE; *(AVFrame*)data = ctx->buf_ptrs[ctx->cur_index]; *data_size = sizeof(AVFrame); - ctx->prev_index = ctx->cur_index; - ctx->cur_index--; - ctx->cur_index &= 15; + ctx->prev_index = ctx->next_prev_index; + ctx->cur_index = ctx->next_cur_index; /* Only release frames that aren't used for backreferences anymore */ if(ctx->buf_ptrs[ctx->cur_index].data[0]) - avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]); + ff_thread_release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]); return buf_size; } @@ -370,9 +404,12 @@ static av_cold int mimic_decode_end(AVCodecContext *avctx) int i; av_free(ctx->swap_buf); + + if(avctx->is_copy) return 0; + for(i = 0; i < 16; i++) if(ctx->buf_ptrs[i].data[0]) - avctx->release_buffer(avctx, &ctx->buf_ptrs[i]); + ff_thread_release_buffer(avctx, &ctx->buf_ptrs[i]); free_vlc(&ctx->vlc); return 0; @@ -387,6 +424,7 @@ AVCodec ff_mimic_decoder = { NULL, mimic_decode_end, mimic_decode_frame, - CODEC_CAP_DR1, + CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS, .long_name = NULL_IF_CONFIG_SMALL("Mimic"), + .update_thread_context = ONLY_IF_THREADS_ENABLED(mimic_decode_update_thread_context) }; diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c index e33f597ce7..76995ad45c 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -37,6 +37,7 @@ #include "bytestream.h" #include "vdpau_internal.h" #include "xvmc_internal.h" +#include "thread.h" //#undef NDEBUG //#include @@ -1179,6 +1180,27 @@ static av_cold int mpeg_decode_init(AVCodecContext *avctx) return 0; } +static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from) +{ + Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data; + MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx; + int err; + + if(avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized) + return 0; + + err = ff_mpeg_update_thread_context(avctx, avctx_from); + if(err) return err; + + if(!ctx->mpeg_enc_ctx_allocated) + memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext)); + + if(!(s->pict_type == FF_B_TYPE || s->low_delay)) + s->picture_number++; + + return 0; +} + static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm, const uint8_t *new_perm){ uint16_t temp_matrix[64]; @@ -1595,6 +1617,9 @@ static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size) } *s->current_picture_ptr->pan_scan= s1->pan_scan; + + if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME) + ff_thread_finish_setup(avctx); }else{ //second field int i; @@ -1769,6 +1794,7 @@ static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y, const int mb_size= 16>>s->avctx->lowres; ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size); + MPV_report_decode_progress(s); s->mb_x = 0; s->mb_y += 1<current_picture_ptr; ff_print_debug_info(s, pict); } else { - s->picture_number++; + if (avctx->active_thread_type&FF_THREAD_FRAME) + s->picture_number++; /* latency of 1 frame for I- and P-frames */ /* XXX: use another variable than picture_number */ if (s->last_picture_ptr != NULL) { @@ -2260,7 +2287,7 @@ static int decode_chunks(AVCodecContext *avctx, buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code); if (start_code > 0x1ff){ if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){ - if(avctx->thread_count > 1){ + if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_SLICE){ int i; avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*)); @@ -2422,7 +2449,7 @@ static int decode_chunks(AVCodecContext *avctx, break; } - if(avctx->thread_count > 1){ + if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_SLICE){ int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count; if(threshold <= mb_y){ MpegEncContext *thread_context= s2->thread_context[s->slice_count]; @@ -2496,6 +2523,7 @@ AVCodec ff_mpeg1video_decoder = { .flush= flush, .max_lowres= 3, .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"), + .update_thread_context= ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context) }; AVCodec ff_mpeg2video_decoder = { @@ -2532,7 +2560,7 @@ AVCodec ff_mpegvideo_decoder = { #if CONFIG_MPEG_XVMC_DECODER static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){ - if( avctx->thread_count > 1) + if( avctx->active_thread_type & FF_THREAD_SLICE ) return -1; if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) ) return -1; diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c index c152b3c2f2..b293bada32 100644 --- a/libavcodec/mpeg4videodec.c +++ b/libavcodec/mpeg4videodec.c @@ -23,6 +23,7 @@ #include "mpegvideo.h" #include "mpeg4video.h" #include "h263.h" +#include "thread.h" // The defines below define the number of bits that are read at once for // reading vlc values. Changing these may improve speed and data cache needs @@ -373,7 +374,13 @@ int mpeg4_decode_video_packet_header(MpegEncContext *s) return -1; } if(s->pict_type == FF_B_TYPE){ - while(s->next_picture.mbskip_table[ s->mb_index2xy[ mb_num ] ]) mb_num++; + int mb_x = 0, mb_y = 0; + + while(s->next_picture.mbskip_table[ s->mb_index2xy[ mb_num ] ]) { + if (!mb_x) ff_thread_await_progress((AVFrame*)s->next_picture_ptr, mb_y++, 0); + mb_num++; + if (++mb_x == s->mb_width) mb_x = 0; + } if(mb_num >= s->mb_num) return -1; // slice contains just skipped MBs which where already decoded } @@ -1303,6 +1310,8 @@ static int mpeg4_decode_mb(MpegEncContext *s, s->last_mv[i][1][0]= s->last_mv[i][1][1]= 0; } + + ff_thread_await_progress((AVFrame*)s->next_picture_ptr, s->mb_y, 0); } /* if we skipped it in the future P Frame than skip it now too */ @@ -1482,6 +1491,12 @@ end: if(s->codec_id==CODEC_ID_MPEG4){ if(mpeg4_is_resync(s)){ const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1; + + if(s->pict_type==FF_B_TYPE){ + ff_thread_await_progress((AVFrame*)s->next_picture_ptr, + (s->mb_x + delta >= s->mb_width) ? FFMIN(s->mb_y+1, s->mb_height-1) : s->mb_y, 0); + } + if(s->pict_type==FF_B_TYPE && s->next_picture.mbskip_table[xy + delta]) return SLICE_OK; return SLICE_END; @@ -2239,11 +2254,12 @@ AVCodec ff_mpeg4_decoder = { NULL, ff_h263_decode_end, ff_h263_decode_frame, - CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, + CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS, .flush= ff_mpeg_flush, .max_lowres= 3, .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"), .pix_fmts= ff_hwaccel_pixfmt_list_420, + .update_thread_context= ONLY_IF_THREADS_ENABLED(ff_mpeg_update_thread_context) }; 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 //#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; ithread_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; ithread_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; ithread_context[i]= av_malloc(sizeof(MpegEncContext)); + memcpy(s->thread_context[i], s, sizeof(MpegEncContext)); + } + + for(i=0; ithread_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; iavctx->thread_count; i++){ - free_duplicate_context(s->thread_context[i]); - } - for(i=1; iavctx->thread_count; i++){ - av_freep(&s->thread_context[i]); - } + if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) { + for(i=0; iavctx->thread_count; i++){ + free_duplicate_context(s->thread_context[i]); + } + for(i=1; iavctx->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; ipicture && !s->avctx->is_copy){ + for(i=0; ipicture_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; ipicture_range_start; ipicture_range_end; i++){ if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i; } }else{ - for(i=0; ipicture_range_start; ipicture_range_end; i++){ if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME } - for(i=0; ipicture_range_start; ipicture_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; ipicture_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; ipicture_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; ipicture_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]<> 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; ipicture_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); +} diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h index 53ab0f4a7f..3e447abd23 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -123,6 +123,7 @@ typedef struct Picture{ int ref_poc[2][2][16]; ///< h264 POCs of the frames used as reference (FIXME need per slice) int ref_count[2][2]; ///< number of entries in ref_poc (FIXME need per slice) int mbaff; ///< h264 1 -> MBAFF frame 0-> not MBAFF + int field_picture; ///< whether or not the picture was encoded in seperate fields int mb_var_sum; ///< sum of MB variance for current frame int mc_mb_var_sum; ///< motion compensated MB variance for current frame @@ -292,6 +293,8 @@ typedef struct MpegEncContext { Picture *last_picture_ptr; ///< pointer to the previous picture. Picture *next_picture_ptr; ///< pointer to the next picture (for bidir pred) Picture *current_picture_ptr; ///< pointer to the current picture + int picture_count; ///< number of allocated pictures (MAX_PICTURE_COUNT * avctx->thread_count) + int picture_range_start, picture_range_end; ///< the part of picture that this context can allocate in uint8_t *visualization_buffer[3]; //< temporary buffer vor MV visualization int last_dc[3]; ///< last DC values for MPEG1 int16_t *dc_val_base; @@ -681,6 +684,7 @@ typedef struct MpegEncContext { void (*denoise_dct)(struct MpegEncContext *s, DCTELEM *block); } MpegEncContext; +#define REBASE_PICTURE(pic, new_ctx, old_ctx) (pic ? &new_ctx->picture[pic - old_ctx->picture] : NULL) void MPV_decode_defaults(MpegEncContext *s); int MPV_common_init(MpegEncContext *s); @@ -706,6 +710,9 @@ void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix); int ff_find_unused_picture(MpegEncContext *s, int shared); void ff_denoise_dct(MpegEncContext *s, DCTELEM *block); void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src); +int MPV_lowest_referenced_row(MpegEncContext *s, int dir); +void MPV_report_decode_progress(MpegEncContext *s); +int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src); const uint8_t *ff_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state); void ff_set_qscale(MpegEncContext * s, int qscale); diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index a7f16080f4..9255fa872a 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -36,6 +36,7 @@ #include "mjpegenc.h" #include "msmpeg4.h" #include "faandct.h" +#include "thread.h" #include "aandcttab.h" #include "flv.h" #include "mpeg4video.h" @@ -1238,9 +1239,9 @@ int MPV_encode_picture(AVCodecContext *avctx, { MpegEncContext *s = avctx->priv_data; AVFrame *pic_arg = data; - int i, stuffing_count; + int i, stuffing_count, context_count = avctx->active_thread_type&FF_THREAD_SLICE ? avctx->thread_count : 1; - for(i=0; ithread_count; i++){ + for(i=0; ithread_context[i]->start_mb_y; int end_y= s->thread_context[i]-> end_mb_y; int h= s->mb_height; @@ -1304,7 +1305,7 @@ vbv_retry: s->last_non_b_time= s->time - s->pp_time; } // av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda); - for(i=0; ithread_count; i++){ + for(i=0; ithread_context[i]->pb; init_put_bits(pb, pb->buf, pb->buf_end - pb->buf); } @@ -2771,6 +2772,7 @@ static int encode_picture(MpegEncContext *s, int picture_number) { int i; int bits; + int context_count = s->avctx->active_thread_type&FF_THREAD_SLICE ? s->avctx->thread_count : 1; s->picture_number = picture_number; @@ -2810,7 +2812,7 @@ static int encode_picture(MpegEncContext *s, int picture_number) } s->mb_intra=0; //for the rate distortion & bit compare functions - for(i=1; iavctx->thread_count; i++){ + for(i=1; ithread_context[i], s); } @@ -2823,11 +2825,11 @@ static int encode_picture(MpegEncContext *s, int picture_number) s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8; if(s->pict_type != FF_B_TYPE && s->avctx->me_threshold==0){ if((s->avctx->pre_me && s->last_non_b_pict_type==FF_I_TYPE) || s->avctx->pre_me==2){ - s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*)); + s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*)); } } - s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*)); + s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*)); }else /* if(s->pict_type == FF_I_TYPE) */{ /* I-Frame */ for(i=0; imb_stride*s->mb_height; i++) @@ -2835,10 +2837,10 @@ static int encode_picture(MpegEncContext *s, int picture_number) if(!s->fixed_qscale){ /* finding spatial complexity for I-frame rate control */ - s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*)); + s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*)); } } - for(i=1; iavctx->thread_count; i++){ + for(i=1; ithread_context[i]); } s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp; @@ -2974,11 +2976,11 @@ static int encode_picture(MpegEncContext *s, int picture_number) bits= put_bits_count(&s->pb); s->header_bits= bits - s->last_bits; - for(i=1; iavctx->thread_count; i++){ + for(i=1; ithread_context[i], s); } - s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*)); - for(i=1; iavctx->thread_count; i++){ + s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*)); + for(i=1; ithread_context[i]); } emms_c(); diff --git a/libavcodec/snow.c b/libavcodec/snow.c index 9ab9fed71f..f330fe32e6 100644 --- a/libavcodec/snow.c +++ b/libavcodec/snow.c @@ -1976,9 +1976,9 @@ static int frame_start(SnowContext *s){ int h= s->avctx->height; if(s->current_picture.data[0]){ - s->dsp.draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH ); - s->dsp.draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2); - s->dsp.draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2); + s->dsp.draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH , EDGE_TOP|EDGE_BOTTOM); + s->dsp.draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2, EDGE_TOP|EDGE_BOTTOM); + s->dsp.draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2, EDGE_TOP|EDGE_BOTTOM); } release_buffer(s->avctx); diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 40d689e1f6..2c737d9625 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -366,6 +366,7 @@ void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){ assert(pic->type==FF_BUFFER_TYPE_INTERNAL); assert(s->internal_buffer_count); + if(s->internal_buffer){ buf = NULL; /* avoids warning */ for(i=0; iinternal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize buf= &((InternalBuffer*)s->internal_buffer)[i]; @@ -377,6 +378,7 @@ void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){ last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count]; FFSWAP(InternalBuffer, *buf, *last); + } for(i=0; i<4; i++){ pic->data[i]=NULL; @@ -572,6 +574,7 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec) goto free_and_end; } } + ret=0; end: entangled_thread_counter--; @@ -1298,9 +1301,9 @@ int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op)) unsigned int ff_toupper4(unsigned int x) { return toupper( x &0xFF) - + (toupper((x>>8 )&0xFF)<<8 ) - + (toupper((x>>16)&0xFF)<<16) - + (toupper((x>>24)&0xFF)<<24); + + (toupper((x>>8 )&0xFF)<<8 ) + + (toupper((x>>16)&0xFF)<<16) + + (toupper((x>>24)&0xFF)<<24); } #if !HAVE_PTHREADS diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c index c067f4cf2f..073de8e25a 100644 --- a/libavcodec/vp8.c +++ b/libavcodec/vp8.c @@ -471,7 +471,7 @@ void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y) mb - 1 /* left */, mb + 1 /* top-left */ }; enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV }; - enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT }; + enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT }; int idx = CNT_ZERO; int cur_sign_bias = s->sign_bias[mb->ref_frame]; int8_t *sign_bias = s->sign_bias; @@ -512,7 +512,7 @@ void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y) mb->mode = VP8_MVMODE_MV; /* If we have three distinct MVs, merge first and last if they're the same */ - if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1+EDGE_TOP]) == AV_RN32A(&near_mv[1+EDGE_TOPLEFT])) + if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1+VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1+VP8_EDGE_TOPLEFT])) cnt[CNT_NEAREST] += 1; /* Swap near and nearest if necessary */ @@ -526,9 +526,9 @@ void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y) /* Choose the best mv out of 0,0 and the nearest mv */ clamp_mv(s, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])]); - cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) + - (mb_edge[EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 + - (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT); + cnt[CNT_SPLITMV] = ((mb_edge[VP8_EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) + + (mb_edge[VP8_EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 + + (mb_edge[VP8_EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT); if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) { mb->mode = VP8_MVMODE_SPLIT; diff --git a/libavcodec/x86/dsputil_mmx.c b/libavcodec/x86/dsputil_mmx.c index a5b0b3ae61..3272556a74 100644 --- a/libavcodec/x86/dsputil_mmx.c +++ b/libavcodec/x86/dsputil_mmx.c @@ -783,7 +783,7 @@ static void h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale){ /* draw the edges of width 'w' of an image of size width, height this mmx version can only handle w==8 || w==16 */ -static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w) +static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w, int sides) { uint8_t *ptr, *last_line; int i; @@ -838,34 +838,39 @@ static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height, int w) for(i=0;i