Commit d5280455 authored by Vittorio Giovara's avatar Vittorio Giovara

mpegvideo: Have ff_mpeg_unref_picture use AVCodecContext directly

This skips setting the memory to 0 but allows for reuse on different
contextes. Oracle did not report any unsual activity because of it.
parent 6f54dc43
...@@ -509,14 +509,14 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic) ...@@ -509,14 +509,14 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
s->uvlinesize != pic->f->linesize[1])) { s->uvlinesize != pic->f->linesize[1])) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"get_buffer() failed (stride changed)\n"); "get_buffer() failed (stride changed)\n");
ff_mpeg_unref_picture(s, pic); ff_mpeg_unref_picture(s->avctx, pic);
return -1; return -1;
} }
if (pic->f->linesize[1] != pic->f->linesize[2]) { if (pic->f->linesize[1] != pic->f->linesize[2]) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"get_buffer() failed (uv stride mismatch)\n"); "get_buffer() failed (uv stride mismatch)\n");
ff_mpeg_unref_picture(s, pic); ff_mpeg_unref_picture(s->avctx, pic);
return -1; return -1;
} }
...@@ -524,7 +524,7 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic) ...@@ -524,7 +524,7 @@ static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
(ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) { (ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) {
av_log(s->avctx, AV_LOG_ERROR, av_log(s->avctx, AV_LOG_ERROR,
"get_buffer() failed to allocate context scratch buffers.\n"); "get_buffer() failed to allocate context scratch buffers.\n");
ff_mpeg_unref_picture(s, pic); ff_mpeg_unref_picture(s->avctx, pic);
return ret; return ret;
} }
...@@ -659,7 +659,7 @@ int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared) ...@@ -659,7 +659,7 @@ int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
return 0; return 0;
fail: fail:
av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n"); av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
ff_mpeg_unref_picture(s, pic); ff_mpeg_unref_picture(s->avctx, pic);
ff_free_picture_tables(pic); ff_free_picture_tables(pic);
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
...@@ -667,17 +667,15 @@ fail: ...@@ -667,17 +667,15 @@ fail:
/** /**
* Deallocate a picture. * Deallocate a picture.
*/ */
void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic) void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
{ {
int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
pic->tf.f = pic->f; pic->tf.f = pic->f;
/* WM Image / Screen codecs allocate internal buffers with different /* WM Image / Screen codecs allocate internal buffers with different
* dimensions / colorspaces; ignore user-defined callbacks for these. */ * dimensions / colorspaces; ignore user-defined callbacks for these. */
if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && if (avctx->codec->id != AV_CODEC_ID_WMV3IMAGE &&
s->codec_id != AV_CODEC_ID_VC1IMAGE && avctx->codec->id != AV_CODEC_ID_VC1IMAGE &&
s->codec_id != AV_CODEC_ID_MSS2) avctx->codec->id != AV_CODEC_ID_MSS2)
ff_thread_release_buffer(s->avctx, &pic->tf); ff_thread_release_buffer(avctx, &pic->tf);
else if (pic->f) else if (pic->f)
av_frame_unref(pic->f); av_frame_unref(pic->f);
...@@ -685,8 +683,6 @@ void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic) ...@@ -685,8 +683,6 @@ void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
if (pic->needs_realloc) if (pic->needs_realloc)
ff_free_picture_tables(pic); ff_free_picture_tables(pic);
memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
} }
static int update_picture_tables(Picture *dst, Picture *src) static int update_picture_tables(Picture *dst, Picture *src)
...@@ -765,7 +761,7 @@ int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src) ...@@ -765,7 +761,7 @@ int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
return 0; return 0;
fail: fail:
ff_mpeg_unref_picture(s, dst); ff_mpeg_unref_picture(s->avctx, dst);
return ret; return ret;
} }
...@@ -939,7 +935,7 @@ int ff_mpeg_update_thread_context(AVCodecContext *dst, ...@@ -939,7 +935,7 @@ int ff_mpeg_update_thread_context(AVCodecContext *dst,
s->picture_number = s1->picture_number; s->picture_number = s1->picture_number;
for (i = 0; i < MAX_PICTURE_COUNT; i++) { for (i = 0; i < MAX_PICTURE_COUNT; i++) {
ff_mpeg_unref_picture(s, &s->picture[i]); ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
if (s1->picture[i].f->buf[0] && if (s1->picture[i].f->buf[0] &&
(ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0) (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
return ret; return ret;
...@@ -947,7 +943,7 @@ int ff_mpeg_update_thread_context(AVCodecContext *dst, ...@@ -947,7 +943,7 @@ int ff_mpeg_update_thread_context(AVCodecContext *dst,
#define UPDATE_PICTURE(pic)\ #define UPDATE_PICTURE(pic)\
do {\ do {\
ff_mpeg_unref_picture(s, &s->pic);\ ff_mpeg_unref_picture(s->avctx, &s->pic);\
if (s1->pic.f->buf[0])\ if (s1->pic.f->buf[0])\
ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\ ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
else\ else\
...@@ -1497,22 +1493,22 @@ void ff_mpv_common_end(MpegEncContext *s) ...@@ -1497,22 +1493,22 @@ void ff_mpv_common_end(MpegEncContext *s)
if (s->picture) { if (s->picture) {
for (i = 0; i < MAX_PICTURE_COUNT; i++) { for (i = 0; i < MAX_PICTURE_COUNT; i++) {
ff_free_picture_tables(&s->picture[i]); ff_free_picture_tables(&s->picture[i]);
ff_mpeg_unref_picture(s, &s->picture[i]); ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
av_frame_free(&s->picture[i].f); av_frame_free(&s->picture[i].f);
} }
} }
av_freep(&s->picture); av_freep(&s->picture);
ff_free_picture_tables(&s->last_picture); ff_free_picture_tables(&s->last_picture);
ff_mpeg_unref_picture(s, &s->last_picture); ff_mpeg_unref_picture(s->avctx, &s->last_picture);
av_frame_free(&s->last_picture.f); av_frame_free(&s->last_picture.f);
ff_free_picture_tables(&s->current_picture); ff_free_picture_tables(&s->current_picture);
ff_mpeg_unref_picture(s, &s->current_picture); ff_mpeg_unref_picture(s->avctx, &s->current_picture);
av_frame_free(&s->current_picture.f); av_frame_free(&s->current_picture.f);
ff_free_picture_tables(&s->next_picture); ff_free_picture_tables(&s->next_picture);
ff_mpeg_unref_picture(s, &s->next_picture); ff_mpeg_unref_picture(s->avctx, &s->next_picture);
av_frame_free(&s->next_picture.f); av_frame_free(&s->next_picture.f);
ff_free_picture_tables(&s->new_picture); ff_free_picture_tables(&s->new_picture);
ff_mpeg_unref_picture(s, &s->new_picture); ff_mpeg_unref_picture(s->avctx, &s->new_picture);
av_frame_free(&s->new_picture.f); av_frame_free(&s->new_picture.f);
free_context_frame(s); free_context_frame(s);
...@@ -1623,7 +1619,7 @@ static void release_unused_pictures(MpegEncContext *s) ...@@ -1623,7 +1619,7 @@ static void release_unused_pictures(MpegEncContext *s)
/* release non reference frames */ /* release non reference frames */
for (i = 0; i < MAX_PICTURE_COUNT; i++) { for (i = 0; i < MAX_PICTURE_COUNT; i++) {
if (!s->picture[i].reference) if (!s->picture[i].reference)
ff_mpeg_unref_picture(s, &s->picture[i]); ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
} }
} }
...@@ -1663,7 +1659,7 @@ int ff_find_unused_picture(MpegEncContext *s, int shared) ...@@ -1663,7 +1659,7 @@ int ff_find_unused_picture(MpegEncContext *s, int shared)
if (s->picture[ret].needs_realloc) { if (s->picture[ret].needs_realloc) {
s->picture[ret].needs_realloc = 0; s->picture[ret].needs_realloc = 0;
ff_free_picture_tables(&s->picture[ret]); ff_free_picture_tables(&s->picture[ret]);
ff_mpeg_unref_picture(s, &s->picture[ret]); ff_mpeg_unref_picture(s->avctx, &s->picture[ret]);
} }
} }
return ret; return ret;
...@@ -1683,7 +1679,7 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) ...@@ -1683,7 +1679,7 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr != s->next_picture_ptr &&
s->last_picture_ptr->f->buf[0]) { s->last_picture_ptr->f->buf[0]) {
ff_mpeg_unref_picture(s, s->last_picture_ptr); ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
} }
/* release forgotten pictures */ /* release forgotten pictures */
...@@ -1695,11 +1691,11 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) ...@@ -1695,11 +1691,11 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
if (!(avctx->active_thread_type & FF_THREAD_FRAME)) if (!(avctx->active_thread_type & FF_THREAD_FRAME))
av_log(avctx, AV_LOG_ERROR, av_log(avctx, AV_LOG_ERROR,
"releasing zombie picture\n"); "releasing zombie picture\n");
ff_mpeg_unref_picture(s, &s->picture[i]); ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
} }
} }
ff_mpeg_unref_picture(s, &s->current_picture); ff_mpeg_unref_picture(s->avctx, &s->current_picture);
release_unused_pictures(s); release_unused_pictures(s);
...@@ -1824,14 +1820,14 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) ...@@ -1824,14 +1820,14 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
} }
if (s->last_picture_ptr) { if (s->last_picture_ptr) {
ff_mpeg_unref_picture(s, &s->last_picture); ff_mpeg_unref_picture(s->avctx, &s->last_picture);
if (s->last_picture_ptr->f->buf[0] && if (s->last_picture_ptr->f->buf[0] &&
(ret = ff_mpeg_ref_picture(s, &s->last_picture, (ret = ff_mpeg_ref_picture(s, &s->last_picture,
s->last_picture_ptr)) < 0) s->last_picture_ptr)) < 0)
return ret; return ret;
} }
if (s->next_picture_ptr) { if (s->next_picture_ptr) {
ff_mpeg_unref_picture(s, &s->next_picture); ff_mpeg_unref_picture(s->avctx, &s->next_picture);
if (s->next_picture_ptr->f->buf[0] && if (s->next_picture_ptr->f->buf[0] &&
(ret = ff_mpeg_ref_picture(s, &s->next_picture, (ret = ff_mpeg_ref_picture(s, &s->next_picture,
s->next_picture_ptr)) < 0) s->next_picture_ptr)) < 0)
...@@ -2433,12 +2429,12 @@ void ff_mpeg_flush(AVCodecContext *avctx){ ...@@ -2433,12 +2429,12 @@ void ff_mpeg_flush(AVCodecContext *avctx){
return; return;
for (i = 0; i < MAX_PICTURE_COUNT; i++) for (i = 0; i < MAX_PICTURE_COUNT; i++)
ff_mpeg_unref_picture(s, &s->picture[i]); ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL; s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
ff_mpeg_unref_picture(s, &s->current_picture); ff_mpeg_unref_picture(s->avctx, &s->current_picture);
ff_mpeg_unref_picture(s, &s->last_picture); ff_mpeg_unref_picture(s->avctx, &s->last_picture);
ff_mpeg_unref_picture(s, &s->next_picture); ff_mpeg_unref_picture(s->avctx, &s->next_picture);
s->mb_x= s->mb_y= 0; s->mb_x= s->mb_y= 0;
......
...@@ -864,7 +864,7 @@ void ff_wmv2_encode_mb(MpegEncContext * s, ...@@ -864,7 +864,7 @@ void ff_wmv2_encode_mb(MpegEncContext * s,
int motion_x, int motion_y); int motion_x, int motion_y);
int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src); int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src);
void ff_mpeg_unref_picture(MpegEncContext *s, Picture *picture); void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *picture);
void ff_free_picture_tables(Picture *pic); void ff_free_picture_tables(Picture *pic);
#endif /* AVCODEC_MPEGVIDEO_H */ #endif /* AVCODEC_MPEGVIDEO_H */
...@@ -886,7 +886,7 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) ...@@ -886,7 +886,7 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
av_frame_free(&s->tmp_frames[i]); av_frame_free(&s->tmp_frames[i]);
ff_free_picture_tables(&s->new_picture); ff_free_picture_tables(&s->new_picture);
ff_mpeg_unref_picture(s, &s->new_picture); ff_mpeg_unref_picture(s->avctx, &s->new_picture);
av_freep(&s->avctx->stats_out); av_freep(&s->avctx->stats_out);
av_freep(&s->ac_stats); av_freep(&s->ac_stats);
...@@ -1370,7 +1370,7 @@ no_output_pic: ...@@ -1370,7 +1370,7 @@ no_output_pic:
s->reordered_input_picture[0]->f->pict_type != s->reordered_input_picture[0]->f->pict_type !=
AV_PICTURE_TYPE_B ? 3 : 0; AV_PICTURE_TYPE_B ? 3 : 0;
ff_mpeg_unref_picture(s, &s->new_picture); ff_mpeg_unref_picture(s->avctx, &s->new_picture);
if ((ret = ff_mpeg_ref_picture(s, &s->new_picture, s->reordered_input_picture[0]))) if ((ret = ff_mpeg_ref_picture(s, &s->new_picture, s->reordered_input_picture[0])))
return ret; return ret;
...@@ -1405,14 +1405,14 @@ no_output_pic: ...@@ -1405,14 +1405,14 @@ no_output_pic:
s->new_picture.f->data[i] += INPLACE_OFFSET; s->new_picture.f->data[i] += INPLACE_OFFSET;
} }
} }
ff_mpeg_unref_picture(s, &s->current_picture); ff_mpeg_unref_picture(s->avctx, &s->current_picture);
if ((ret = ff_mpeg_ref_picture(s, &s->current_picture, if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
s->current_picture_ptr)) < 0) s->current_picture_ptr)) < 0)
return ret; return ret;
s->picture_number = s->new_picture.f->display_picture_number; s->picture_number = s->new_picture.f->display_picture_number;
} else { } else {
ff_mpeg_unref_picture(s, &s->new_picture); ff_mpeg_unref_picture(s->avctx, &s->new_picture);
} }
return 0; return 0;
} }
...@@ -1456,7 +1456,7 @@ static void frame_end(MpegEncContext *s) ...@@ -1456,7 +1456,7 @@ static void frame_end(MpegEncContext *s)
/* release non-reference frames */ /* release non-reference frames */
for (i = 0; i < MAX_PICTURE_COUNT; i++) { for (i = 0; i < MAX_PICTURE_COUNT; i++) {
if (!s->picture[i].reference) if (!s->picture[i].reference)
ff_mpeg_unref_picture(s, &s->picture[i]); ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
} }
} }
...@@ -1493,13 +1493,13 @@ static int frame_start(MpegEncContext *s) ...@@ -1493,13 +1493,13 @@ static int frame_start(MpegEncContext *s)
if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr != s->next_picture_ptr &&
s->last_picture_ptr->f->buf[0]) { s->last_picture_ptr->f->buf[0]) {
ff_mpeg_unref_picture(s, s->last_picture_ptr); ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
} }
s->current_picture_ptr->f->pict_type = s->pict_type; s->current_picture_ptr->f->pict_type = s->pict_type;
s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
ff_mpeg_unref_picture(s, &s->current_picture); ff_mpeg_unref_picture(s->avctx, &s->current_picture);
if ((ret = ff_mpeg_ref_picture(s, &s->current_picture, if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
s->current_picture_ptr)) < 0) s->current_picture_ptr)) < 0)
return ret; return ret;
...@@ -1511,14 +1511,14 @@ static int frame_start(MpegEncContext *s) ...@@ -1511,14 +1511,14 @@ static int frame_start(MpegEncContext *s)
} }
if (s->last_picture_ptr) { if (s->last_picture_ptr) {
ff_mpeg_unref_picture(s, &s->last_picture); ff_mpeg_unref_picture(s->avctx, &s->last_picture);
if (s->last_picture_ptr->f->buf[0] && if (s->last_picture_ptr->f->buf[0] &&
(ret = ff_mpeg_ref_picture(s, &s->last_picture, (ret = ff_mpeg_ref_picture(s, &s->last_picture,
s->last_picture_ptr)) < 0) s->last_picture_ptr)) < 0)
return ret; return ret;
} }
if (s->next_picture_ptr) { if (s->next_picture_ptr) {
ff_mpeg_unref_picture(s, &s->next_picture); ff_mpeg_unref_picture(s->avctx, &s->next_picture);
if (s->next_picture_ptr->f->buf[0] && if (s->next_picture_ptr->f->buf[0] &&
(ret = ff_mpeg_ref_picture(s, &s->next_picture, (ret = ff_mpeg_ref_picture(s, &s->next_picture,
s->next_picture_ptr)) < 0) s->next_picture_ptr)) < 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment