From ebf94f439e8c0b7d61b7b6c71b80193baef76531 Mon Sep 17 00:00:00 2001 From: Alex Converse Date: Tue, 30 Aug 2011 04:51:52 +0200 Subject: [PATCH] ffmpeg: Factorize video resampling. --- ffmpeg.c | 111 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 51 deletions(-) diff --git a/ffmpeg.c b/ffmpeg.c index c61eed688e..43528f926f 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -1073,14 +1073,71 @@ static void do_subtitle_out(AVFormatContext *s, static int bit_buffer_size= 1024*256; static uint8_t *bit_buffer= NULL; +static void do_video_resample(OutputStream *ost, + InputStream *ist, + AVFrame *in_picture, + AVFrame **out_picture) +{ + int resample_changed = 0; + AVCodecContext *dec = ist->st->codec; + *out_picture = in_picture; +#if !CONFIG_AVFILTER + resample_changed = ost->resample_width != dec->width || + ost->resample_height != dec->height || + ost->resample_pix_fmt != dec->pix_fmt; + + if (resample_changed) { + av_log(NULL, AV_LOG_INFO, + "Input stream #%d.%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n", + ist->file_index, ist->st->index, + ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt), + dec->width , dec->height , av_get_pix_fmt_name(dec->pix_fmt)); + ost->resample_width = dec->width; + ost->resample_height = dec->height; + ost->resample_pix_fmt = dec->pix_fmt; + } + + ost->video_resample = dec->width != enc->width || + dec->height != enc->height || + dec->pix_fmt != enc->pix_fmt; + + if (ost->video_resample) { + *out_picture = &ost->resample_frame; + if (!ost->img_resample_ctx || resample_changed) { + /* initialize the destination picture */ + if (!ost->resample_frame.data[0]) { + avcodec_get_frame_defaults(&ost->resample_frame); + if (avpicture_alloc((AVPicture *)&ost->resample_frame, enc->pix_fmt, + enc->width, enc->height)) { + fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n"); + exit_program(1); + } + } + /* initialize a new scaler context */ + sws_freeContext(ost->img_resample_ctx); + ost->img_resample_ctx = sws_getContext(dec->width, dec->height, dec->pix_fmt, + enc->width, enc->height, enc->pix_fmt, + ost->sws_flags, NULL, NULL, NULL); + if (ost->img_resample_ctx == NULL) { + fprintf(stderr, "Cannot get resampling context\n"); + exit_program(1); + } + } + sws_scale(ost->img_resample_ctx, in_picture->data, in_picture->linesize, + 0, ost->resample_height, (*out_picture)->data, (*out_picture)->linesize); + } +#endif +} + + static void do_video_out(AVFormatContext *s, OutputStream *ost, InputStream *ist, AVFrame *in_picture, int *frame_size, float quality) { - int nb_frames, i, ret, av_unused resample_changed; - AVFrame *final_picture, *formatted_picture; + int nb_frames, i, ret; + AVFrame *final_picture; AVCodecContext *enc, *dec; double sync_ipts; @@ -1123,55 +1180,7 @@ static void do_video_out(AVFormatContext *s, if (nb_frames <= 0) return; - formatted_picture = in_picture; - final_picture = formatted_picture; - -#if !CONFIG_AVFILTER - resample_changed = ost->resample_width != dec->width || - ost->resample_height != dec->height || - ost->resample_pix_fmt != dec->pix_fmt; - - if (resample_changed) { - av_log(NULL, AV_LOG_INFO, - "Input stream #%d.%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n", - ist->file_index, ist->st->index, - ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt), - dec->width , dec->height , av_get_pix_fmt_name(dec->pix_fmt)); - ost->resample_width = dec->width; - ost->resample_height = dec->height; - ost->resample_pix_fmt = dec->pix_fmt; - } - - ost->video_resample = dec->width != enc->width || - dec->height != enc->height || - dec->pix_fmt != enc->pix_fmt; - - if (ost->video_resample) { - final_picture = &ost->resample_frame; - if (!ost->img_resample_ctx || resample_changed) { - /* initialize the destination picture */ - if (!ost->resample_frame.data[0]) { - avcodec_get_frame_defaults(&ost->resample_frame); - if (avpicture_alloc((AVPicture *)&ost->resample_frame, enc->pix_fmt, - enc->width, enc->height)) { - fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n"); - exit_program(1); - } - } - /* initialize a new scaler context */ - sws_freeContext(ost->img_resample_ctx); - ost->img_resample_ctx = sws_getContext(dec->width, dec->height, dec->pix_fmt, - enc->width, enc->height, enc->pix_fmt, - ost->sws_flags, NULL, NULL, NULL); - if (ost->img_resample_ctx == NULL) { - fprintf(stderr, "Cannot get resampling context\n"); - exit_program(1); - } - } - sws_scale(ost->img_resample_ctx, formatted_picture->data, formatted_picture->linesize, - 0, ost->resample_height, final_picture->data, final_picture->linesize); - } -#endif + do_video_resample(ost, ist, in_picture, &final_picture); /* duplicates frame if needed */ for(i=0;i