summaryrefslogtreecommitdiff
path: root/libavcodec/mpegvideo.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/mpegvideo.c')
-rw-r--r--libavcodec/mpegvideo.c259
1 files changed, 221 insertions, 38 deletions
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index f24e11910a..dfb2289201 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]);
@@ -315,6 +316,7 @@ int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared){
s->prev_pict_types[0]= s->dropable ? AV_PICTURE_TYPE_B : s->pict_type;
if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == AV_PICTURE_TYPE_B)
pic->age= INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
+ pic->owner2 = NULL;
return 0;
fail: //for the FF_ALLOCZ_OR_GOTO macro
@@ -458,6 +460,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 +555,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 +586,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;
}
@@ -602,8 +683,9 @@ av_cold int MPV_common_init(MpegEncContext *s)
}
}
- 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]);
}
@@ -665,9 +747,10 @@ av_cold int MPV_common_init(MpegEncContext *s)
}
s->context_initialized = 1;
+ s->thread_context[0]= s;
if (s->width && s->height) {
- s->thread_context[0]= s;
+ if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) {
threads = s->avctx->thread_count;
for(i=1; i<threads; i++){
@@ -681,6 +764,11 @@ av_cold int MPV_common_init(MpegEncContext *s)
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;
@@ -694,12 +782,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;
@@ -754,8 +844,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]);
}
}
@@ -769,7 +859,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])
@@ -863,18 +954,33 @@ void init_vlc_rl(RLTable *rl)
}
}
+void ff_release_unused_pictures(MpegEncContext *s, int remove_current)
+{
+ int i;
+
+ /* release non reference frames */
+ for(i=0; i<s->picture_count; i++){
+ if(s->picture[i].data[0] && !s->picture[i].reference
+ && (!s->picture[i].owner2 || s->picture[i].owner2 == s)
+ && (remove_current || &s->picture[i] != s->current_picture_ptr)
+ /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
+ free_frame_buffer(s, &s->picture[i]);
+ }
+ }
+}
+
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;
}
}
@@ -931,7 +1037,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]);
@@ -942,12 +1048,7 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
}
if(!s->encoding){
- /* release non reference frames */
- for(i=0; i<MAX_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]);
- }
- }
+ ff_release_unused_pictures(s, 1);
if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
pic= s->current_picture_ptr; //we already have a unused image (maybe it was set before reading the header)
@@ -977,6 +1078,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;
@@ -1010,6 +1112,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==AV_PICTURE_TYPE_B){
/* Allocate a dummy frame */
@@ -1017,6 +1121,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);
}
}
@@ -1068,11 +1174,12 @@ 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);
- }else if(!s->avctx->hwaccel
+ }else if((s->error_count || s->encoding)
+ && !s->avctx->hwaccel
&& !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
&& s->unrestricted_mv
&& s->current_picture.reference
@@ -1088,6 +1195,7 @@ void MPV_frame_end(MpegEncContext *s)
s->h_edge_pos>>1, s->v_edge_pos>>1,
EDGE_WIDTH/2, EDGE_TOP | EDGE_BOTTOM);
}
+
emms_c();
s->last_pict_type = s->pict_type;
@@ -1108,7 +1216,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]);
}
@@ -1121,6 +1229,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);
+ }
}
/**
@@ -1785,6 +1897,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)
@@ -1949,6 +2098,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;
@@ -2112,19 +2271,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==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
src= (AVFrame*)s->current_picture_ptr;
else if(s->last_picture_ptr)
@@ -2190,7 +2367,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]);
@@ -2444,3 +2621,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 && !s->error_occurred)
+ ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
+}