| 1 | |
|---|
| 2 | #include "FFmpegParameters.hpp" |
|---|
| 3 | |
|---|
| 4 | #include <string> |
|---|
| 5 | #include <iostream> |
|---|
| 6 | #include <sstream> |
|---|
| 7 | |
|---|
| 8 | #if LIBAVCODEC_VERSION_MAJOR >= 53 |
|---|
| 9 | extern "C" |
|---|
| 10 | { |
|---|
| 11 | #include <parseutils.h> |
|---|
| 12 | } |
|---|
| 13 | #define av_parse_video_frame_size av_parse_video_size |
|---|
| 14 | #define av_parse_video_frame_rate av_parse_video_rate |
|---|
| 15 | #endif |
|---|
| 16 | |
|---|
| 17 | #if LIBAVCODEC_VERSION_MAJOR >= 53 || \ |
|---|
| 18 | (LIBAVCODEC_VERSION_MAJOR==52 && LIBAVCODEC_VERSION_MINOR>=49) |
|---|
| 19 | |
|---|
| 20 | extern "C" |
|---|
| 21 | { |
|---|
| 22 | #include <pixdesc.h> |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | inline PixelFormat osg_av_get_pix_fmt(const char *name) { return av_get_pix_fmt(name); } |
|---|
| 26 | |
|---|
| 27 | #else |
|---|
| 28 | inline PixelFormat osg_av_get_pix_fmt(const char *name) { return avcodec_get_pix_fmt(name); } |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | namespace osgFFmpeg { |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | FFmpegParameters::FFmpegParameters() : |
|---|
| 37 | m_format(0) |
|---|
| 38 | { |
|---|
| 39 | memset(&m_parameters, 0, sizeof(m_parameters)); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | FFmpegParameters::~FFmpegParameters() |
|---|
| 44 | {} |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | void FFmpegParameters::parse(const std::string& name, const std::string& value) |
|---|
| 48 | { |
|---|
| 49 | if (value.empty()) |
|---|
| 50 | { |
|---|
| 51 | return; |
|---|
| 52 | } |
|---|
| 53 | else if (name == "format") |
|---|
| 54 | { |
|---|
| 55 | avdevice_register_all(); |
|---|
| 56 | m_format = av_find_input_format(value.c_str()); |
|---|
| 57 | if (!m_format) |
|---|
| 58 | OSG_NOTICE<<"Failed to apply input video format: "<<value.c_str()<<std::endl; |
|---|
| 59 | } |
|---|
| 60 | else if (name == "pixel_format") |
|---|
| 61 | { |
|---|
| 62 | m_parameters.pix_fmt = osg_av_get_pix_fmt(value.c_str()); |
|---|
| 63 | } |
|---|
| 64 | else if (name == "frame_size") |
|---|
| 65 | { |
|---|
| 66 | int frame_width = 0, frame_height = 0; |
|---|
| 67 | if (av_parse_video_frame_size(&frame_width, &frame_height, value.c_str()) < 0) |
|---|
| 68 | { |
|---|
| 69 | OSG_NOTICE<<"Failed to apply frame size: "<<value.c_str()<<std::endl; |
|---|
| 70 | return; |
|---|
| 71 | } |
|---|
| 72 | if ((frame_width % 2) != 0 || (frame_height % 2) != 0) |
|---|
| 73 | { |
|---|
| 74 | OSG_NOTICE<<"Frame size must be a multiple of 2: "<<frame_width<<"x"<<frame_height<<std::endl; |
|---|
| 75 | return; |
|---|
| 76 | } |
|---|
| 77 | m_parameters.width = frame_width; |
|---|
| 78 | m_parameters.height = frame_height; |
|---|
| 79 | } |
|---|
| 80 | else if (name == "frame_rate") |
|---|
| 81 | { |
|---|
| 82 | AVRational frame_rate; |
|---|
| 83 | if (av_parse_video_frame_rate(&frame_rate, value.c_str()) < 0) |
|---|
| 84 | { |
|---|
| 85 | OSG_NOTICE<<"Failed to apply frame rate: "<<value.c_str()<<std::endl; |
|---|
| 86 | return; |
|---|
| 87 | } |
|---|
| 88 | m_parameters.time_base.den = frame_rate.num; |
|---|
| 89 | m_parameters.time_base.num = frame_rate.den; |
|---|
| 90 | } |
|---|
| 91 | else if (name == "audio_sample_rate") |
|---|
| 92 | { |
|---|
| 93 | int audio_sample_rate = 44100; |
|---|
| 94 | std::stringstream ss(value); ss >> audio_sample_rate; |
|---|
| 95 | m_parameters.sample_rate = audio_sample_rate; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | } |
|---|