]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/audio/audio_decoder.cpp
2.0.2: - graph: Fixed potential buffer overflow.
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "audio_decoder.h"\r
25 \r
26 #include "audio_resampler.h"\r
27 \r
28 #include "../util/util.h"\r
29 #include "../../ffmpeg_error.h"\r
30 \r
31 #include <core/video_format.h>\r
32 \r
33 #include <tbb/cache_aligned_allocator.h>\r
34 \r
35 #include <queue>\r
36 \r
37 #if defined(_MSC_VER)\r
38 #pragma warning (push)\r
39 #pragma warning (disable : 4244)\r
40 #endif\r
41 extern "C" \r
42 {\r
43         #include <libavformat/avformat.h>\r
44         #include <libavcodec/avcodec.h>\r
45 }\r
46 #if defined(_MSC_VER)\r
47 #pragma warning (pop)\r
48 #endif\r
49 \r
50 namespace caspar { namespace ffmpeg {\r
51         \r
52 struct audio_decoder::implementation : boost::noncopyable\r
53 {       \r
54         int                                                                                                                     index_;\r
55         const safe_ptr<AVCodecContext>                                                          codec_context_;         \r
56         const core::video_format_desc                                                           format_desc_;\r
57 \r
58         audio_resampler                                                                                         resampler_;\r
59 \r
60         std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>>      buffer1_;\r
61 \r
62         std::queue<safe_ptr<AVPacket>>                                                          packets_;\r
63 \r
64         const int64_t                                                                                           nb_frames_;\r
65         tbb::atomic<size_t>                                                                                     file_frame_number_;\r
66 public:\r
67         explicit implementation(const safe_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) \r
68                 : format_desc_(format_desc)     \r
69                 , codec_context_(open_codec(*context, AVMEDIA_TYPE_AUDIO, index_))\r
70                 , resampler_(format_desc.audio_channels,        codec_context_->channels,\r
71                                          format_desc.audio_sample_rate, codec_context_->sample_rate,\r
72                                          AV_SAMPLE_FMT_S32,                             codec_context_->sample_fmt)\r
73                 , buffer1_(AVCODEC_MAX_AUDIO_FRAME_SIZE*2)\r
74                 , nb_frames_(0)//context->streams[index_]->nb_frames)\r
75         {               \r
76                 file_frame_number_ = 0;\r
77                 CASPAR_LOG(debug) << "[audio_decoder] " << context->streams[index_]->codec->codec->long_name;      \r
78         }\r
79 \r
80         void push(const std::shared_ptr<AVPacket>& packet)\r
81         {                       \r
82                 if(!packet)\r
83                         return;\r
84 \r
85                 if(packet->stream_index == index_ || packet->data == nullptr)\r
86                         packets_.push(make_safe_ptr(packet));\r
87         }       \r
88         \r
89         std::shared_ptr<core::audio_buffer> poll()\r
90         {\r
91                 if(packets_.empty())\r
92                         return nullptr;\r
93                                 \r
94                 auto packet = packets_.front();\r
95 \r
96                 if(packet->data == nullptr)\r
97                 {\r
98                         packets_.pop();\r
99                         file_frame_number_ = static_cast<size_t>(packet->pos);\r
100                         avcodec_flush_buffers(codec_context_.get());\r
101                         return flush_audio();\r
102                 }\r
103 \r
104                 auto audio = decode(*packet);\r
105 \r
106                 if(packet->size == 0)                                   \r
107                         packets_.pop();\r
108 \r
109                 return audio;\r
110         }\r
111 \r
112         std::shared_ptr<core::audio_buffer> decode(AVPacket& pkt)\r
113         {               \r
114                 buffer1_.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE*2);\r
115                 int written_bytes = buffer1_.size() - FF_INPUT_BUFFER_PADDING_SIZE;\r
116                 \r
117                 int ret = THROW_ON_ERROR2(avcodec_decode_audio3(codec_context_.get(), reinterpret_cast<int16_t*>(buffer1_.data()), &written_bytes, &pkt), "[audio_decoder]");\r
118 \r
119                 // There might be several frames in one packet.\r
120                 pkt.size -= ret;\r
121                 pkt.data += ret;\r
122                         \r
123                 buffer1_.resize(written_bytes);\r
124 \r
125                 buffer1_ = resampler_.resample(std::move(buffer1_));\r
126                 \r
127                 const auto n_samples = buffer1_.size() / av_get_bytes_per_sample(AV_SAMPLE_FMT_S32);\r
128                 const auto samples = reinterpret_cast<int32_t*>(buffer1_.data());\r
129 \r
130                 ++file_frame_number_;\r
131 \r
132                 return std::make_shared<core::audio_buffer>(samples, samples + n_samples);\r
133         }\r
134 \r
135         bool ready() const\r
136         {\r
137                 return packets_.size() > 10;\r
138         }\r
139 \r
140         uint32_t nb_frames() const\r
141         {\r
142                 return 0;//std::max<int64_t>(nb_frames_, file_frame_number_);\r
143         }\r
144 };\r
145 \r
146 audio_decoder::audio_decoder(const safe_ptr<AVFormatContext>& context, const core::video_format_desc& format_desc) : impl_(new implementation(context, format_desc)){}\r
147 void audio_decoder::push(const std::shared_ptr<AVPacket>& packet){impl_->push(packet);}\r
148 bool audio_decoder::ready() const{return impl_->ready();}\r
149 std::shared_ptr<core::audio_buffer> audio_decoder::poll(){return impl_->poll();}\r
150 uint32_t audio_decoder::nb_frames() const{return impl_->nb_frames();}\r
151 uint32_t audio_decoder::file_frame_number() const{return impl_->file_frame_number_;}\r
152 \r
153 }}