From: Steinar H. Gunderson Date: Fri, 14 Apr 2017 23:52:05 +0000 (+0200) Subject: Move some common FFmpeg utilities out into a shared file, instead of having them... X-Git-Tag: 1.6.0~48 X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=ad62963eb4dc6407698e8569ae175f6d43c9b018 Move some common FFmpeg utilities out into a shared file, instead of having them in ImageInput. --- diff --git a/Makefile b/Makefile index 20b5f25..9dc5526 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ AUDIO_MIXER_OBJS = audio_mixer.o alsa_input.o alsa_pool.o ebu_r128_proc.o stereo OBJS += chroma_subsampler.o v210_converter.o mixer.o pbo_frame_allocator.o context.o ref_counted_frame.o theme.o httpd.o flags.o image_input.o alsa_output.o disk_space_estimator.o print_latency.o timecode_renderer.o $(AUDIO_MIXER_OBJS) # Streaming and encoding objects -OBJS += quicksync_encoder.o x264_encoder.o x264_dynamic.o x264_speed_control.o video_encoder.o metacube2.o mux.o audio_encoder.o ffmpeg_raii.o +OBJS += quicksync_encoder.o x264_encoder.o x264_dynamic.o x264_speed_control.o video_encoder.o metacube2.o mux.o audio_encoder.o ffmpeg_raii.o ffmpeg_util.o # DeckLink OBJS += decklink_capture.o decklink_util.o decklink_output.o decklink/DeckLinkAPIDispatch.o diff --git a/ffmpeg_capture.cpp b/ffmpeg_capture.cpp index 464b4cb..6e672d9 100644 --- a/ffmpeg_capture.cpp +++ b/ffmpeg_capture.cpp @@ -28,6 +28,7 @@ extern "C" { #include "bmusb/bmusb.h" #include "ffmpeg_raii.h" +#include "ffmpeg_util.h" #include "flags.h" #include "image_input.h" @@ -179,25 +180,14 @@ bool FFmpegCapture::play_video(const string &pathname) return false; } - int video_stream_index = -1, audio_stream_index = -1; - AVRational video_timebase{ 1, 1 }; - for (unsigned i = 0; i < format_ctx->nb_streams; ++i) { - if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && - video_stream_index == -1) { - video_stream_index = i; - video_timebase = format_ctx->streams[i]->time_base; - } - if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && - audio_stream_index == -1) { - audio_stream_index = i; - } - } + int video_stream_index = find_stream_index(format_ctx.get(), AVMEDIA_TYPE_VIDEO); if (video_stream_index == -1) { fprintf(stderr, "%s: No video stream found\n", pathname.c_str()); return false; } const AVCodecParameters *codecpar = format_ctx->streams[video_stream_index]->codecpar; + AVRational video_timebase = format_ctx->streams[video_stream_index]->time_base; AVCodecContextWithDeleter codec_ctx = avcodec_alloc_context3_unique(nullptr); if (avcodec_parameters_to_context(codec_ctx.get(), codecpar) < 0) { fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str()); diff --git a/ffmpeg_util.cpp b/ffmpeg_util.cpp new file mode 100644 index 0000000..e348d0a --- /dev/null +++ b/ffmpeg_util.cpp @@ -0,0 +1,75 @@ +#include "ffmpeg_util.h" + +#include +#include +#include + +#include +#include + +#include "flags.h" + +using namespace std; + +string search_for_file(const string &filename) +{ + if (!filename.empty() && filename[0] == '/') { + // Absolute path. + return filename; + } + + // See if we match ^[a-z]:/, which is probably a URL of some sort + // (FFmpeg understands various forms of these). + for (size_t i = 0; i < filename.size() - 1; ++i) { + if (filename[i] == ':' && filename[i + 1] == '/') { + return filename; + } + if (!isalpha(filename[i])) { + break; + } + } + + // Look for the file in all theme_dirs until we find one; + // that will be the permanent resolution of this file, whether + // it is actually valid or not. + // We store errors from all the attempts, and show them + // once we know we can't find any of them. + vector errors; + for (const string &dir : global_flags.theme_dirs) { + string pathname = dir + "/" + filename; + if (access(pathname.c_str(), O_RDONLY) == 0) { + return pathname; + } else { + char buf[512]; + snprintf(buf, sizeof(buf), "%s: %s", pathname.c_str(), strerror(errno)); + errors.push_back(buf); + } + } + + for (const string &error : errors) { + fprintf(stderr, "%s\n", error.c_str()); + } + return ""; +} + +string search_for_file_or_die(const string &filename) +{ + string pathname = search_for_file(filename); + if (pathname.empty()) { + fprintf(stderr, "Couldn't find %s in any directory in --theme-dirs, exiting.\n", + filename.c_str()); + exit(1); + } + return pathname; +} + +int find_stream_index(AVFormatContext *ctx, AVMediaType media_type) +{ + for (unsigned i = 0; i < ctx->nb_streams; ++i) { + if (ctx->streams[i]->codecpar->codec_type == media_type) { + return i; + } + } + return -1; +} + diff --git a/ffmpeg_util.h b/ffmpeg_util.h new file mode 100644 index 0000000..c037a15 --- /dev/null +++ b/ffmpeg_util.h @@ -0,0 +1,23 @@ +#ifndef _FFMPEG_UTIL_H +#define _FFMPEG_UTIL_H 1 + +// Some common utilities for the two FFmpeg users (ImageInput and FFmpegCapture). + +#include + +extern "C" { +#include +} + +// Look for the file in all theme_dirs until we find one; +// that will be the permanent resolution of this file, whether +// it is actually valid or not. Returns an empty string on error. +std::string search_for_file(const std::string &filename); + +// Same, but exits on error. +std::string search_for_file_or_die(const std::string &filename); + +// Returns -1 if not found. +int find_stream_index(AVFormatContext *ctx, AVMediaType media_type); + +#endif // !defined(_FFMPEG_UTIL_H) diff --git a/image_input.cpp b/image_input.cpp index a3a110c..f6805d3 100644 --- a/image_input.cpp +++ b/image_input.cpp @@ -30,6 +30,7 @@ extern "C" { #include #include "ffmpeg_raii.h" +#include "ffmpeg_util.h" #include "flags.h" #include "flat_input.h" @@ -37,58 +38,6 @@ struct SwsContext; using namespace std; -string search_for_file(const string &filename) -{ - if (!filename.empty() && filename[0] == '/') { - // Absolute path. - return filename; - } - - // See if we match ^[a-z]:/, which is probably a URL of some sort - // (FFmpeg understands various forms of these). - for (size_t i = 0; i < filename.size() - 1; ++i) { - if (filename[i] == ':' && filename[i + 1] == '/') { - return filename; - } - if (!isalpha(filename[i])) { - break; - } - } - - // Look for the file in all theme_dirs until we find one; - // that will be the permanent resolution of this file, whether - // it is actually valid or not. - // We store errors from all the attempts, and show them - // once we know we can't find any of them. - vector errors; - for (const string &dir : global_flags.theme_dirs) { - string pathname = dir + "/" + filename; - if (access(pathname.c_str(), O_RDONLY) == 0) { - return pathname; - } else { - char buf[512]; - snprintf(buf, sizeof(buf), "%s: %s", pathname.c_str(), strerror(errno)); - errors.push_back(buf); - } - } - - for (const string &error : errors) { - fprintf(stderr, "%s\n", error.c_str()); - } - return ""; -} - -string search_for_file_or_die(const string &filename) -{ - string pathname = search_for_file(filename); - if (pathname.empty()) { - fprintf(stderr, "Couldn't find %s in any directory in --theme-dirs, exiting.\n", - filename.c_str()); - exit(1); - } - return pathname; -} - ImageInput::ImageInput(const string &filename) : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA, GL_UNSIGNED_BYTE, 1280, 720), // Resolution will be overwritten. @@ -161,13 +110,7 @@ shared_ptr ImageInput::load_image_raw(const string &pat return nullptr; } - int stream_index = -1; - for (unsigned i = 0; i < format_ctx->nb_streams; ++i) { - if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { - stream_index = i; - break; - } - } + int stream_index = find_stream_index(format_ctx.get(), AVMEDIA_TYPE_VIDEO); if (stream_index == -1) { fprintf(stderr, "%s: No video stream found\n", pathname.c_str()); return nullptr; diff --git a/image_input.h b/image_input.h index f384264..02be497 100644 --- a/image_input.h +++ b/image_input.h @@ -46,12 +46,4 @@ private: static std::condition_variable threads_should_quit_modified; // Signals when threads_should_quit is set. }; -// Look for the file in all theme_dirs until we find one; -// that will be the permanent resolution of this file, whether -// it is actually valid or not. Returns an empty string on error. -std::string search_for_file(const std::string &filename); - -// Same, but exits on error. -std::string search_for_file_or_die(const std::string &filename); - #endif // !defined(_IMAGE_INPUT_H)