]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.0.0.2: ffmpeg_producer: Fixed problem with audio and video out of sync which was...
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "audio_decoder.h"\r
23 \r
24 #if defined(_MSC_VER)\r
25 #pragma warning (push)\r
26 #pragma warning (disable : 4244)\r
27 #endif\r
28 extern "C" \r
29 {\r
30         #define __STDC_CONSTANT_MACROS\r
31         #define __STDC_LIMIT_MACROS\r
32         #include <libavformat/avformat.h>\r
33         #include <libavcodec/avcodec.h>\r
34 }\r
35 #if defined(_MSC_VER)\r
36 #pragma warning (pop)\r
37 #endif\r
38 \r
39 namespace caspar {\r
40 \r
41 struct audio_decoder::implementation : boost::noncopyable\r
42 {       \r
43         AVCodecContext&                                 codec_context_;         \r
44         const core::video_format_desc   format_desc_;\r
45         std::vector<short>                              current_chunk_;\r
46         std::vector<std::vector<short>> chunks_;                \r
47         size_t                                                  frame_number_;\r
48 public:\r
49         explicit implementation(AVCodecContext& codec_context, const core::video_format_desc& format_desc) \r
50                 : codec_context_(codec_context)\r
51                 , format_desc_(format_desc)     \r
52                 , frame_number_(0)\r
53         {\r
54                 if(codec_context_.sample_rate != static_cast<int>(format_desc_.audio_sample_rate) || \r
55                    codec_context_.channels != static_cast<int>(format_desc_.audio_channels))\r
56                 {       \r
57                         BOOST_THROW_EXCEPTION(\r
58                                 file_read_error()  <<\r
59                                 msg_info("Invalid sample-rate or number of channels.") <<\r
60                                 arg_value_info(boost::lexical_cast<std::string>(codec_context_.sample_rate)) << \r
61                                 arg_name_info("codec_context"));\r
62                 }\r
63         }\r
64                         \r
65         void push(const std::shared_ptr<AVPacket>& audio_packet)\r
66         {                       \r
67                 if(!audio_packet)\r
68                 {       \r
69                         avcodec_flush_buffers(&codec_context_);\r
70                         current_chunk_.clear();\r
71                         frame_number_ = 0;\r
72                         return;\r
73                 }\r
74                                 \r
75                 auto s = current_chunk_.size();\r
76                 current_chunk_.resize(s + 4*format_desc_.audio_sample_rate*2+FF_INPUT_BUFFER_PADDING_SIZE/2, 0);\r
77                 \r
78                 int written_bytes = (current_chunk_.size() - s)*2 - FF_INPUT_BUFFER_PADDING_SIZE;\r
79                 const int errn = avcodec_decode_audio3(&codec_context_, &current_chunk_[s], &written_bytes, audio_packet.get());\r
80                 if(errn < 0)\r
81                 {       \r
82                         BOOST_THROW_EXCEPTION(\r
83                                 invalid_operation() <<\r
84                                 boost::errinfo_api_function("avcodec_decode_audio2") <<\r
85                                 boost::errinfo_errno(AVUNERROR(errn)));\r
86                 }\r
87 \r
88                 current_chunk_.resize(s + written_bytes/2);\r
89 \r
90                 const auto last = current_chunk_.end() - current_chunk_.size() % format_desc_.audio_samples_per_frame;\r
91                 \r
92                 for(auto it = current_chunk_.begin(); it != last; it += format_desc_.audio_samples_per_frame)           \r
93                         chunks_.push_back(std::vector<short>(it, it + format_desc_.audio_samples_per_frame));           \r
94 \r
95                 current_chunk_.erase(current_chunk_.begin(), last);\r
96         }\r
97 \r
98         bool empty() const\r
99         {\r
100                 return chunks_.empty();\r
101         }\r
102 \r
103         std::vector<short> front()\r
104         {\r
105                 return chunks_.front();\r
106         }\r
107 \r
108         void pop()\r
109         {\r
110                 ++frame_number_;\r
111                 chunks_.pop_back();\r
112         }\r
113 };\r
114 \r
115 audio_decoder::audio_decoder(AVCodecContext& codec_context, const core::video_format_desc& format_desc) : impl_(new implementation(codec_context, format_desc)){}\r
116 void audio_decoder::push(const std::shared_ptr<AVPacket>& audio_packet){impl_->push(std::move(audio_packet));}\r
117 bool audio_decoder::empty() const {return impl_->empty();}\r
118 std::vector<short> audio_decoder::front() {return impl_->front();}\r
119 void audio_decoder::pop(){impl_->pop();}\r
120 size_t audio_decoder::frame_number() const{return impl_->frame_number_;}\r
121 }