]> git.sesse.net Git - casparcg/blob - core/producer/ffmpeg/ffmpeg_producer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / ffmpeg / ffmpeg_producer.cpp
1 #include "../../stdafx.h"\r
2 \r
3 #include "ffmpeg_producer.h"\r
4 \r
5 #if defined(_MSC_VER)\r
6 #pragma warning (push)\r
7 #pragma warning (disable : 4244)\r
8 #endif\r
9 \r
10 extern "C" \r
11 {\r
12         #define __STDC_CONSTANT_MACROS\r
13         #define __STDC_LIMIT_MACROS\r
14         #include <libavcodec/avcodec.h>\r
15         #include <libavformat/avformat.h>\r
16         #include <libavutil/avutil.h>\r
17         #include <libswscale/swscale.h>\r
18 }\r
19 \r
20 #if defined(_MSC_VER)\r
21 #pragma warning (pop)\r
22 #endif\r
23 \r
24 #include "input.h"\r
25 \r
26 #include "audio/audio_decoder.h"\r
27 #include "video/video_decoder.h"\r
28 #include "video/video_decoder.h"\r
29 \r
30 #include "../../format/video_format.h"\r
31 #include "../../processor/draw_frame.h"\r
32 #include "../../processor/draw_frame.h"\r
33 \r
34 #include <tbb/mutex.h>\r
35 #include <tbb/parallel_invoke.h>\r
36 #include <tbb/task_group.h>\r
37 \r
38 #include <boost/algorithm/string/case_conv.hpp>\r
39 #include <boost/lexical_cast.hpp>\r
40 #include <boost/thread.hpp>\r
41 #include <boost/thread/once.hpp>\r
42 \r
43 #include <deque>\r
44 \r
45 using namespace boost::assign;\r
46 \r
47 namespace caspar { namespace core { namespace ffmpeg{\r
48         \r
49 struct ffmpeg_producer_impl\r
50 {\r
51 public:\r
52         ffmpeg_producer_impl(const std::wstring& filename, const  std::vector<std::wstring>& params) : filename_(filename), last_frame_(draw_frame(draw_frame::empty())),\r
53                 input_(filename), video_decoder_(input_.get_video_codec_context().get()), audio_decoder_(input_.get_audio_codec_context().get())\r
54         {                               \r
55                 input_.set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());\r
56 \r
57                 auto seek = std::find(params.begin(), params.end(), L"SEEK");\r
58                 if(seek != params.end() && ++seek != params.end())\r
59                 {\r
60                         if(!input_.seek(boost::lexical_cast<unsigned long long>(*seek)))\r
61                                 CASPAR_LOG(warning) << "Failed to seek file: " << filename_  << "to frame" << *seek;\r
62                 }\r
63         }\r
64 \r
65         void initialize(const safe_ptr<frame_processor_device>& frame_processor)\r
66         {\r
67                 format_desc_ = frame_processor->get_video_format_desc();\r
68                 video_decoder_.initialize(frame_processor);\r
69         }\r
70                 \r
71         safe_ptr<draw_frame> receive()\r
72         {\r
73                 while(ouput_channel_.empty() && !input_.is_eof())\r
74                 {       \r
75                         aligned_buffer video_packet;\r
76                         if(video_frame_channel_.size() < 3)     \r
77                                 video_packet = input_.get_video_packet();               \r
78                         \r
79                         aligned_buffer audio_packet;\r
80                         if(audio_chunk_channel_.size() < 3)     \r
81                                 audio_packet = input_.get_audio_packet();               \r
82 \r
83                         tbb::parallel_invoke(\r
84                         [&]\r
85                         { // Video Decoding and Scaling\r
86                                 if(!video_packet.empty())\r
87                                 {\r
88                                         auto frame = video_decoder_.execute(video_packet);\r
89                                         video_frame_channel_.push_back(std::move(frame));       \r
90                                 }\r
91                         }, \r
92                         [&] \r
93                         { // Audio Decoding\r
94                                 if(!audio_packet.empty())\r
95                                 {\r
96                                         auto chunks = audio_decoder_.execute(audio_packet);\r
97                                         audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
98                                 }\r
99                         });\r
100 \r
101                         while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || input_.get_audio_codec_context() == nullptr))\r
102                         {\r
103                                 std::vector<short> audio_data;\r
104                                 if(input_.get_audio_codec_context() != nullptr) \r
105                                 {\r
106                                         audio_data = std::move(audio_chunk_channel_.front());\r
107                                         audio_chunk_channel_.pop_front();\r
108                                 }\r
109                                                         \r
110                                 auto write = std::move(video_frame_channel_.front());\r
111                                 write->audio_data() = std::move(audio_data);\r
112                                 auto transform = draw_frame(write);\r
113                                 video_frame_channel_.pop_front();\r
114                 \r
115                                 // TODO: Make generic for all formats and modes.\r
116                                 if(input_.get_video_codec_context()->codec_id == CODEC_ID_DVVIDEO) // Move up one field         \r
117                                         transform.translate(0.0f, 1.0/static_cast<double>(format_desc_.height));        \r
118                                 \r
119                                 ouput_channel_.push(std::move(transform));\r
120                         }                               \r
121 \r
122                         if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())                      \r
123                                 return last_frame_;                     \r
124                 }\r
125 \r
126                 auto result = last_frame_;\r
127                 if(!ouput_channel_.empty())\r
128                 {\r
129                         result = std::move(ouput_channel_.front());\r
130                         last_frame_ = draw_frame(result);\r
131                         last_frame_->audio_volume(0.0); // last_frame should not have audio\r
132                         ouput_channel_.pop();\r
133                 }\r
134                 else if(input_.is_eof())\r
135                         return draw_frame::eof();\r
136 \r
137                 return result;\r
138         }\r
139 \r
140         std::wstring print() const\r
141         {\r
142                 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
143         }\r
144 \r
145         input                                                           input_;                 \r
146         audio_decoder                                           audio_decoder_;\r
147         video_decoder                                           video_decoder_;\r
148 \r
149         std::deque<safe_ptr<write_frame>>       video_frame_channel_;   \r
150         std::deque<std::vector<short>>          audio_chunk_channel_;\r
151 \r
152         std::queue<safe_ptr<draw_frame>>        ouput_channel_;\r
153         \r
154         const std::wstring                                      filename_;\r
155         \r
156         safe_ptr<draw_frame>                            last_frame_;\r
157 \r
158         video_format_desc                                       format_desc_;\r
159 };\r
160 \r
161 class ffmpeg_producer : public frame_producer\r
162 {\r
163 public:\r
164         ffmpeg_producer(const std::wstring& filename, const  std::vector<std::wstring>& params) : impl_(new ffmpeg_producer_impl(filename, params)){}\r
165         ffmpeg_producer(ffmpeg_producer&& other) : impl_(std::move(other.impl_)){}\r
166         virtual safe_ptr<draw_frame> receive(){return impl_->receive();}\r
167         virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor){impl_->initialize(frame_processor);}\r
168         virtual std::wstring print() const{return impl_->print();}\r
169 private:\r
170         std::shared_ptr<ffmpeg_producer_impl> impl_;\r
171 };\r
172 \r
173 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)\r
174 {                       \r
175         static const std::vector<std::wstring> extensions = list_of(L"mpg")(L"avi")(L"mov")(L"dv")(L"wav")(L"mp3")(L"mp4")(L"f4v")(L"flv");\r
176         std::wstring filename = params[0];\r
177         \r
178         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
179                 {                                       \r
180                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
181                 });\r
182 \r
183         if(ext == extensions.end())\r
184                 return frame_producer::empty();\r
185                 \r
186         static boost::once_flag av_register_all_flag = BOOST_ONCE_INIT;\r
187         boost::call_once(av_register_all, av_register_all_flag);        \r
188                 \r
189         static boost::once_flag avcodec_init_flag = BOOST_ONCE_INIT;\r
190         boost::call_once(avcodec_init, avcodec_init_flag);      \r
191 \r
192         return make_safe<ffmpeg_producer>(filename + L"." + *ext, params);\r
193 }\r
194 \r
195 }}}