explicit implementation(const std::shared_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) \r
: format_desc_(format_desc) \r
, nb_frames_(0)\r
- , buffer1_(AVCODEC_MAX_AUDIO_FRAME_SIZE*2)\r
- { \r
+ { \r
try\r
{\r
AVCodec* dec;\r
index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0), "[audio_decoder]");\r
\r
THROW_ON_ERROR2(avcodec_open(context->streams[index_]->codec, dec), "[audio_decoder]");\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- CASPAR_LOG(warning) << "[audio_decoder] Failed to open audio. Running without audio.";\r
- return;\r
- }\r
+ \r
+ codec_context_.reset(context->streams[index_]->codec, avcodec_close);\r
\r
- codec_context_.reset(context->streams[index_]->codec, avcodec_close);\r
+ buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
\r
- //nb_frames_ = context->streams[index_]->nb_frames;\r
- //if(nb_frames_ == 0)\r
- // nb_frames_ = context->streams[index_]->duration * context->streams[index_]->time_base.den;\r
+ //nb_frames_ = context->streams[index_]->nb_frames;\r
+ //if(nb_frames_ == 0)\r
+ // nb_frames_ = context->streams[index_]->duration * context->streams[index_]->time_base.den;\r
\r
- if(codec_context_ &&\r
- (codec_context_->sample_rate != static_cast<int>(format_desc_.audio_sample_rate) || \r
- codec_context_->channels != static_cast<int>(format_desc_.audio_channels)) ||\r
- codec_context_->sample_fmt != AV_SAMPLE_FMT_S16)\r
- { \r
- auto resampler = av_audio_resample_init(format_desc_.audio_channels, codec_context_->channels,\r
- format_desc_.audio_sample_rate, codec_context_->sample_rate,\r
- AV_SAMPLE_FMT_S16, codec_context_->sample_fmt,\r
- 16, 10, 0, 0.8);\r
+ if(codec_context_->sample_rate != static_cast<int>(format_desc_.audio_sample_rate) || \r
+ codec_context_->channels != static_cast<int>(format_desc_.audio_channels) ||\r
+ codec_context_->sample_fmt != AV_SAMPLE_FMT_S16)\r
+ { \r
+ auto resampler = av_audio_resample_init(format_desc_.audio_channels, codec_context_->channels,\r
+ format_desc_.audio_sample_rate, codec_context_->sample_rate,\r
+ AV_SAMPLE_FMT_S16, codec_context_->sample_fmt,\r
+ 16, 10, 0, 0.8);\r
\r
- buffer2_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
+ buffer2_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
\r
- CASPAR_LOG(warning) << L" Invalid audio format. Resampling.";\r
+ CASPAR_LOG(warning) << L" Invalid audio format. Resampling.";\r
\r
- if(resampler)\r
- resampler_.reset(resampler, audio_resample_close);\r
- else\r
- codec_context_ = nullptr;\r
- } \r
+ if(resampler)\r
+ resampler_.reset(resampler, audio_resample_close);\r
+ else\r
+ codec_context_ = nullptr;\r
+ } \r
+ }\r
+ catch(...)\r
+ {\r
+ index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0), "[audio_decoder]");\r
+\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ CASPAR_LOG(warning) << "[audio_decoder] Failed to open audio-stream. Running without audio."; \r
+ }\r
}\r
\r
void push(const std::shared_ptr<AVPacket>& packet)\r
{ \r
- if(!codec_context_)\r
- return;\r
-\r
if(packet && packet->stream_index != index_)\r
return;\r
\r
\r
std::vector<std::shared_ptr<std::vector<int16_t>>> poll()\r
{\r
+ if(!codec_context_)\r
+ return empty_poll();\r
+\r
std::vector<std::shared_ptr<std::vector<int16_t>>> result;\r
\r
- if(!codec_context_)\r
- result.push_back(std::make_shared<std::vector<int16_t>>(format_desc_.audio_samples_per_frame, 0));\r
- else if(!packets_.empty())\r
- { \r
- auto packet = packets_.front();\r
-\r
- if(packet) \r
- {\r
- result.push_back(decode(*packet));\r
- if(packet->size == 0) \r
- packets_.pop();\r
- }\r
- else \r
- { \r
- avcodec_flush_buffers(codec_context_.get());\r
- result.push_back(nullptr);\r
+ if(packets_.empty())\r
+ return result;\r
+ \r
+ auto packet = packets_.front();\r
+\r
+ if(packet) \r
+ {\r
+ result.push_back(decode(*packet));\r
+ if(packet->size == 0) \r
packets_.pop();\r
- }\r
}\r
+ else \r
+ { \r
+ avcodec_flush_buffers(codec_context_.get());\r
+ result.push_back(nullptr);\r
+ packets_.pop();\r
+ } \r
\r
return result;\r
}\r
\r
+ std::vector<std::shared_ptr<std::vector<int16_t>>> empty_poll()\r
+ {\r
+ if(packets_.empty())\r
+ return std::vector<std::shared_ptr<std::vector<int16_t>>>();\r
+ \r
+ auto packet = packets_.front();\r
+ packets_.pop();\r
+\r
+ if(!packet) \r
+ return boost::assign::list_of(nullptr);\r
+ \r
+ return boost::assign::list_of(std::make_shared<std::vector<int16_t>>(format_desc_.audio_samples_per_frame, 0)); \r
+ }\r
+\r
std::shared_ptr<std::vector<int16_t>> decode(AVPacket& pkt)\r
{ \r
int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;\r
std::shared_ptr<AVCodecContext> codec_context_;\r
int index_;\r
\r
- std::queue<std::shared_ptr<AVPacket>> packet_buffer_;\r
+ std::queue<std::shared_ptr<AVPacket>> packets_;\r
\r
filter filter_;\r
\r
index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0), "[video_decoder]");\r
\r
THROW_ON_ERROR2(tbb_avcodec_open(context->streams[index_]->codec, dec), "[video_decoder]");\r
+ \r
+ codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\r
+ \r
+ // Some files give an invalid time_base numerator, try to fix it.\r
+ if(codec_context_ && codec_context_->time_base.num == 1)\r
+ codec_context_->time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(codec_context_->time_base.den)))-1)); \r
+ \r
+ nb_frames_ = context->streams[index_]->nb_frames;\r
+ if(nb_frames_ == 0)\r
+ nb_frames_ = context->streams[index_]->duration;// * context->streams[index_]->time_base.den;\r
+\r
+ fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);\r
+ if(double_rate(filter))\r
+ fps_ *= 2;\r
}\r
catch(...)\r
{\r
+ index_ = THROW_ON_ERROR2(av_find_best_stream(context.get(), AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0), "[video_decoder]");\r
+\r
CASPAR_LOG_CURRENT_EXCEPTION();\r
- CASPAR_LOG(warning) << "[video_decoder] Failed to open video. Running without audio.";\r
- return;\r
+ CASPAR_LOG(warning) << "[video_decoder] Failed to open video-stream. Running without video."; \r
}\r
- \r
- codec_context_.reset(context->streams[index_]->codec, tbb_avcodec_close);\r
- \r
- // Some files give an invalid time_base numerator, try to fix it.\r
- if(codec_context_ && codec_context_->time_base.num == 1)\r
- codec_context_->time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(codec_context_->time_base.den)))-1)); \r
- \r
- nb_frames_ = context->streams[index_]->nb_frames;\r
- if(nb_frames_ == 0)\r
- nb_frames_ = context->streams[index_]->duration;// * context->streams[index_]->time_base.den;\r
-\r
- fps_ = static_cast<double>(codec_context_->time_base.den) / static_cast<double>(codec_context_->time_base.num);\r
- if(double_rate(filter))\r
- fps_ *= 2;\r
}\r
\r
void push(const std::shared_ptr<AVPacket>& packet)\r
{\r
- if(!codec_context_)\r
- return;\r
-\r
if(packet && packet->stream_index != index_)\r
return;\r
\r
- packet_buffer_.push(packet);\r
+ packets_.push(packet);\r
}\r
\r
std::vector<std::shared_ptr<AVFrame>> poll()\r
{ \r
+ if(!codec_context_)\r
+ return empty_poll();\r
+\r
std::vector<std::shared_ptr<AVFrame>> result;\r
\r
- if(!codec_context_)\r
+ if(packets_.empty())\r
+ return result;\r
+\r
+ auto packet = packets_.front();\r
+ \r
+ if(packet)\r
{\r
- std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);\r
- frame->data[0] = nullptr;\r
- result.push_back(frame);\r
+ auto frame = decode(*packet);\r
+ boost::range::push_back(result, filter_.execute(frame));\r
+ if(packet->size == 0)\r
+ packets_.pop();\r
}\r
- else if(!packet_buffer_.empty())\r
+ else\r
{\r
- auto packet = packet_buffer_.front();\r
- \r
- if(packet)\r
+ if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
{\r
- auto frame = decode(*packet);\r
- boost::range::push_back(result, filter_.execute(frame));\r
- if(packet->size == 0)\r
- packet_buffer_.pop();\r
+ AVPacket pkt;\r
+ av_init_packet(&pkt);\r
+ pkt.data = nullptr;\r
+ pkt.size = 0;\r
+ auto frame = decode(pkt);\r
+ boost::range::push_back(result, filter_.execute(frame)); \r
}\r
- else\r
- {\r
- if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)\r
- {\r
- AVPacket pkt;\r
- av_init_packet(&pkt);\r
- pkt.data = nullptr;\r
- pkt.size = 0;\r
- auto frame = decode(pkt);\r
- boost::range::push_back(result, filter_.execute(frame)); \r
- }\r
-\r
- if(result.empty())\r
- { \r
- packet_buffer_.pop();\r
- avcodec_flush_buffers(codec_context_.get());\r
- result.push_back(nullptr);\r
- }\r
+\r
+ if(result.empty())\r
+ { \r
+ packets_.pop();\r
+ avcodec_flush_buffers(codec_context_.get());\r
+ result.push_back(nullptr);\r
}\r
}\r
\r
return result;\r
}\r
\r
+ std::vector<std::shared_ptr<AVFrame>> empty_poll()\r
+ { \r
+ if(packets_.empty())\r
+ return std::vector<std::shared_ptr<AVFrame>>();\r
+ \r
+ auto packet = packets_.front();\r
+ packets_.pop();\r
+\r
+ if(!packet) \r
+ return boost::assign::list_of(nullptr);\r
+\r
+ std::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);\r
+ frame->data[0] = nullptr;\r
+\r
+ return boost::assign::list_of(frame); \r
+ }\r
+\r
std::shared_ptr<AVFrame> decode(AVPacket& pkt)\r
{\r
std::shared_ptr<AVFrame> decoded_frame(avcodec_alloc_frame(), av_free);\r
\r
bool ready() const\r
{\r
- return !codec_context_ || !packet_buffer_.empty();\r
+ return !codec_context_ || !packets_.empty();\r
}\r
\r
double fps() const\r