]> 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 #include "input.h"\r
6 #include "audio/audio_decoder.h"\r
7 #include "video/video_decoder.h"\r
8 \r
9 #include "../../format/video_format.h"\r
10 #include "../../processor/draw_frame.h"\r
11 \r
12 #include <tbb/parallel_invoke.h>\r
13 \r
14 #include <boost/optional.hpp>\r
15 \r
16 #include <deque>\r
17 \r
18 using namespace boost::assign;\r
19 \r
20 namespace caspar { namespace core { namespace ffmpeg{\r
21         \r
22 struct ffmpeg_producer : public frame_producer\r
23 {\r
24         input                                                           input_;                 \r
25         std::unique_ptr<audio_decoder>          audio_decoder_;\r
26         video_decoder                                           video_decoder_;\r
27 \r
28         std::deque<safe_ptr<write_frame>>       video_frame_channel_;   \r
29         std::deque<std::vector<short>>          audio_chunk_channel_;\r
30 \r
31         std::queue<safe_ptr<draw_frame>>        ouput_channel_;\r
32         \r
33         const std::wstring                                      filename_;\r
34         \r
35         safe_ptr<draw_frame>                            last_frame_;\r
36 \r
37         video_format_desc                                       format_desc_;\r
38 \r
39 public:\r
40         explicit ffmpeg_producer(const std::wstring& filename) \r
41                 : filename_(filename)\r
42                 , last_frame_(draw_frame(draw_frame::empty()))\r
43                 , input_(filename)\r
44                 , video_decoder_(input_.get_video_codec_context().get())                \r
45                 , audio_decoder_(input_.get_audio_codec_context().get() ? new audio_decoder(input_.get_audio_codec_context().get(), input_.fps()) : nullptr){}\r
46 \r
47         virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)\r
48         {\r
49                 format_desc_ = frame_processor->get_video_format_desc();\r
50                 video_decoder_.initialize(frame_processor);\r
51         }\r
52                 \r
53         virtual safe_ptr<draw_frame> receive()\r
54         {\r
55                 while(ouput_channel_.empty() && !input_.is_eof())\r
56                 {       \r
57                         aligned_buffer video_packet;\r
58                         if(video_frame_channel_.size() < 3)     \r
59                                 video_packet = input_.get_video_packet();               \r
60                         \r
61                         aligned_buffer audio_packet;\r
62                         if(audio_chunk_channel_.size() < 3)     \r
63                                 audio_packet = input_.get_audio_packet();               \r
64 \r
65                         tbb::parallel_invoke(\r
66                         [&]\r
67                         { // Video Decoding and Scaling\r
68                                 if(!video_packet.empty())\r
69                                 {\r
70                                         auto frame = video_decoder_.execute(video_packet);\r
71                                         video_frame_channel_.push_back(std::move(frame));       \r
72                                 }\r
73                         }, \r
74                         [&] \r
75                         { // Audio Decoding\r
76                                 if(!audio_packet.empty() && audio_decoder_)\r
77                                 {\r
78                                         try\r
79                                         {\r
80                                                 auto chunks = audio_decoder_->execute(audio_packet);\r
81                                                 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
82                                         }\r
83                                         catch(...)\r
84                                         {\r
85                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
86                                                 audio_decoder_.reset();\r
87                                         }\r
88                                 }\r
89                         });\r
90 \r
91                         while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || !audio_decoder_))\r
92                         {\r
93                                 if(audio_decoder_) \r
94                                 {\r
95                                         video_frame_channel_.front()->audio_data() = std::move(audio_chunk_channel_.front());\r
96                                         audio_chunk_channel_.pop_front();\r
97                                 }\r
98                                                         \r
99                                 ouput_channel_.push(video_frame_channel_.front());\r
100                                 video_frame_channel_.pop_front();\r
101                         }                               \r
102 \r
103                         if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())                      \r
104                                 return last_frame_;                     \r
105                 }\r
106 \r
107                 auto result = last_frame_;\r
108                 if(!ouput_channel_.empty())\r
109                 {\r
110                         result = std::move(ouput_channel_.front());\r
111                         last_frame_ = draw_frame(result);\r
112                         last_frame_->audio_volume(0.0); // last_frame should not have audio\r
113                         ouput_channel_.pop();\r
114                 }\r
115                 else if(input_.is_eof())\r
116                         return draw_frame::eof();\r
117 \r
118                 return result;\r
119         }\r
120 \r
121         void set_loop(bool value)\r
122         {\r
123                 input_.set_loop(value);\r
124         }\r
125 \r
126         void seek(unsigned long long value)\r
127         {\r
128                 if(!input_.seek(value))\r
129                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to seek file: )") << arg_value_info(narrow(filename_)));          \r
130         }\r
131 \r
132         virtual std::wstring print() const\r
133         {\r
134                 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
135         }\r
136 };\r
137 \r
138 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)\r
139 {                       \r
140         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
141         std::wstring filename = params[0];\r
142         \r
143         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
144                 {                                       \r
145                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
146                 });\r
147 \r
148         if(ext == extensions.end())\r
149                 return frame_producer::empty();\r
150 \r
151         auto producer = make_safe<ffmpeg_producer>(filename + L"." + *ext);\r
152 \r
153         producer->set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());\r
154         \r
155         auto seek = std::find(params.begin(), params.end(), L"SEEK");\r
156         if(seek != params.end() && ++seek != params.end())\r
157                 producer->seek(boost::lexical_cast<unsigned long long>(*seek));\r
158 \r
159         return producer;        \r
160 }\r
161 \r
162 }}}