From: Helge Norberg Date: Wed, 5 Oct 2016 16:26:17 +0000 (+0200) Subject: [ffmpeg] Remove redundant av_frame_alloc()/av_frame_free() RAII pairs all over the... X-Git-Tag: 2.1.0_Beta1~34 X-Git-Url: https://git.sesse.net/?p=casparcg;a=commitdiff_plain;h=b8c8660af595b9897af41183fe8b12786d45af98 [ffmpeg] Remove redundant av_frame_alloc()/av_frame_free() RAII pairs all over the code and use ffmpeg::create_frame() instead which encapsulates that creation. --- diff --git a/modules/ffmpeg/audio_channel_remapper.cpp b/modules/ffmpeg/audio_channel_remapper.cpp index a49395a87..920aa7203 100644 --- a/modules/ffmpeg/audio_channel_remapper.cpp +++ b/modules/ffmpeg/audio_channel_remapper.cpp @@ -166,11 +166,7 @@ struct audio_channel_remapper::impl auto num_samples = input.size() / input_layout_.num_channels; auto expected_output_size = num_samples * output_layout_.num_channels; - auto input_frame = std::shared_ptr(av_frame_alloc(), [](AVFrame* p) - { - if (p) - av_frame_free(&p); - }); + auto input_frame = ffmpeg::create_frame(); input_frame->channels = input_layout_.num_channels; input_frame->channel_layout = ffmpeg::create_channel_layout_bitmask(input_layout_.num_channels); diff --git a/modules/ffmpeg/consumer/ffmpeg_consumer.cpp b/modules/ffmpeg/consumer/ffmpeg_consumer.cpp index fb2045b6f..b3006ab0a 100644 --- a/modules/ffmpeg/consumer/ffmpeg_consumer.cpp +++ b/modules/ffmpeg/consumer/ffmpeg_consumer.cpp @@ -18,7 +18,7 @@ * * Author: Robert Nagy, ronag89@gmail.com */ - + #include "../StdAfx.h" #include "../ffmpeg_error.h" @@ -26,6 +26,7 @@ #include "ffmpeg_consumer.h" #include "../producer/tbb_avcodec.h" +#include "../producer/util/util.h" #include #include @@ -66,7 +67,7 @@ #pragma warning (push) #pragma warning (disable : 4244) #endif -extern "C" +extern "C" { #define __STDC_CONSTANT_MACROS #define __STDC_LIMIT_MACROS @@ -83,23 +84,23 @@ extern "C" #endif namespace caspar { namespace ffmpeg { - + int av_opt_set(void *obj, const char *name, const char *val, int search_flags) { AVClass* av_class = *(AVClass**)obj; if((strcmp(name, "pix_fmt") == 0 || strcmp(name, "pixel_format") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0) { - AVCodecContext* c = (AVCodecContext*)obj; + AVCodecContext* c = (AVCodecContext*)obj; auto pix_fmt = av_get_pix_fmt(val); if(pix_fmt == PIX_FMT_NONE) - return -1; + return -1; c->pix_fmt = pix_fmt; return 0; } //if((strcmp(name, "r") == 0 || strcmp(name, "frame_rate") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0) //{ - // AVCodecContext* c = (AVCodecContext*)obj; + // AVCodecContext* c = (AVCodecContext*)obj; // if(c->codec_type != AVMEDIA_TYPE_VIDEO) // return -1; @@ -128,7 +129,7 @@ struct option { } }; - + struct output_format { AVOutputFormat* format; @@ -155,26 +156,26 @@ struct output_format { return set_opt(o.name, o.value); }); - + if(vcodec == CODEC_ID_NONE && format) vcodec = format->video_codec; if(acodec == CODEC_ID_NONE && format) acodec = format->audio_codec; - + if(vcodec == CODEC_ID_NONE) vcodec = CODEC_ID_H264; - + if(acodec == CODEC_ID_NONE) acodec = CODEC_ID_PCM_S16LE; } - + bool set_opt(const std::string& name, const std::string& value) { //if(name == "target") - //{ + //{ // enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN; - // + // // if(name.find("pal-") != std::string::npos) // norm = PAL; // else if(name.find("ntsc-") != std::string::npos) @@ -182,8 +183,8 @@ struct output_format // if(norm == UNKNOWN) // CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("target")); - // - // if (name.find("-dv") != std::string::npos) + // + // if (name.find("-dv") != std::string::npos) // { // set_opt("f", "dv"); // if(norm == PAL) @@ -200,11 +201,11 @@ struct output_format // } // } // set_opt("s", norm == PAL ? "720x576" : "720x480"); - // } + // } // return true; //} - //else + //else if(name == "f") { format = av_guess_format(value.c_str(), nullptr, nullptr); @@ -238,7 +239,7 @@ struct output_format { if(av_parse_video_size(&width, &height, value.c_str()) < 0) CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown video size " + value)); - + return true; } else if(name == "croptop") @@ -253,7 +254,7 @@ struct output_format return true; } - + return false; } }; @@ -261,7 +262,7 @@ struct output_format typedef cache_aligned_vector byte_vector; struct ffmpeg_consumer : boost::noncopyable -{ +{ const spl::shared_ptr graph_; const std::string filename_; const std::string full_filename_ = u8(env::media_folder()) + filename_; @@ -270,13 +271,13 @@ struct ffmpeg_consumer : boost::noncopyable const core::audio_channel_layout channel_layout_; core::monitor::subject monitor_subject_; - + tbb::spin_mutex exception_mutex_; std::exception_ptr exception_; - + std::shared_ptr audio_st_; std::shared_ptr video_st_; - + byte_vector picture_buffer_; byte_vector key_picture_buf_; byte_vector audio_buffer_; @@ -318,21 +319,21 @@ public: executor_.set_capacity(8); oc_->oformat = output_format_.format; - + std::strcpy(oc_->filename, full_filename_.c_str()); - + // Add the audio and video streams using the default format codecs and initialize the codecs. video_st_ = add_video_stream(options); if (!key_only) audio_st_ = add_audio_stream(options); - + av_dump_format(oc_.get(), 0, full_filename_.c_str(), 1); - + // Open the output ffmpeg, if needed. - if (!(oc_->oformat->flags & AVFMT_NOFILE)) + if (!(oc_->oformat->flags & AVFMT_NOFILE)) THROW_ON_ERROR2(avio_open(&oc_->pb, full_filename_.c_str(), AVIO_FLAG_WRITE), "[ffmpeg_consumer]"); - + THROW_ON_ERROR2(avformat_write_header(oc_.get(), nullptr), "[ffmpeg_consumer]"); if(options.size() > 0) @@ -343,7 +344,7 @@ public: } ~ffmpeg_consumer() - { + { try { executor_.wait(); @@ -354,16 +355,16 @@ public: } LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]"); - + if (!key_only_) audio_st_.reset(); video_st_.reset(); - - if (!(oc_->oformat->flags & AVFMT_NOFILE)) + + if (!(oc_->oformat->flags & AVFMT_NOFILE)) LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]"); } - + // frame_consumer void send(core::const_frame& frame) @@ -377,7 +378,7 @@ public: std::rethrow_exception(exception); executor_.begin_invoke([=] - { + { encode(frame); current_encoding_delay_ = frame.get_age_millis(); }); @@ -397,7 +398,7 @@ public: { return L"ffmpeg[" + u16(filename_) + L"]"; } - + core::monitor::subject& monitor_output() { return monitor_subject_; @@ -405,13 +406,13 @@ public: private: std::shared_ptr add_video_stream(std::vector