]> 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 <deque>\r
15 \r
16 using namespace boost::assign;\r
17 \r
18 namespace caspar { namespace core { namespace ffmpeg{\r
19         \r
20 struct ffmpeg_producer : public frame_producer\r
21 {\r
22         input                                                           input_;                 \r
23         audio_decoder                                           audio_decoder_;\r
24         video_decoder                                           video_decoder_;\r
25 \r
26         std::deque<safe_ptr<write_frame>>       video_frame_channel_;   \r
27         std::deque<std::vector<short>>          audio_chunk_channel_;\r
28 \r
29         std::queue<safe_ptr<draw_frame>>        ouput_channel_;\r
30         \r
31         const std::wstring                                      filename_;\r
32         \r
33         safe_ptr<draw_frame>                            last_frame_;\r
34 \r
35         video_format_desc                                       format_desc_;\r
36 \r
37 public:\r
38         explicit ffmpeg_producer(const std::wstring& filename, const  std::vector<std::wstring>& params) \r
39                 : filename_(filename)\r
40                 , last_frame_(draw_frame(draw_frame::empty()))\r
41                 , input_(filename)\r
42                 , video_decoder_(input_.get_video_codec_context().get())\r
43                 , audio_decoder_(input_.get_audio_codec_context().get(), input_.fps())\r
44         {                               \r
45                 input_.set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());\r
46 \r
47                 auto seek = std::find(params.begin(), params.end(), L"SEEK");\r
48                 if(seek != params.end() && ++seek != params.end())\r
49                 {\r
50                         if(!input_.seek(boost::lexical_cast<unsigned long long>(*seek)))\r
51                                 CASPAR_LOG(warning) << "Failed to seek file: " << filename_  << "to frame" << *seek;\r
52                 }\r
53         }\r
54 \r
55         virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)\r
56         {\r
57                 format_desc_ = frame_processor->get_video_format_desc();\r
58                 video_decoder_.initialize(frame_processor);\r
59         }\r
60                 \r
61         virtual safe_ptr<draw_frame> receive()\r
62         {\r
63                 while(ouput_channel_.empty() && !input_.is_eof())\r
64                 {       \r
65                         aligned_buffer video_packet;\r
66                         if(video_frame_channel_.size() < 3)     \r
67                                 video_packet = input_.get_video_packet();               \r
68                         \r
69                         aligned_buffer audio_packet;\r
70                         if(audio_chunk_channel_.size() < 3)     \r
71                                 audio_packet = input_.get_audio_packet();               \r
72 \r
73                         tbb::parallel_invoke(\r
74                         [&]\r
75                         { // Video Decoding and Scaling\r
76                                 if(!video_packet.empty())\r
77                                 {\r
78                                         auto frame = video_decoder_.execute(video_packet);\r
79                                         video_frame_channel_.push_back(std::move(frame));       \r
80                                 }\r
81                         }, \r
82                         [&] \r
83                         { // Audio Decoding\r
84                                 if(!audio_packet.empty())\r
85                                 {\r
86                                         auto chunks = audio_decoder_.execute(audio_packet);\r
87                                         audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
88                                 }\r
89                         });\r
90 \r
91                         while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || input_.get_audio_codec_context() == nullptr))\r
92                         {\r
93                                 if(input_.get_audio_codec_context() != nullptr) \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         virtual std::wstring print() const\r
122         {\r
123                 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
124         }\r
125 };\r
126 \r
127 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)\r
128 {                       \r
129         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
130         std::wstring filename = params[0];\r
131         \r
132         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
133                 {                                       \r
134                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
135                 });\r
136 \r
137         if(ext == extensions.end())\r
138                 return frame_producer::empty();\r
139 \r
140         return make_safe<ffmpeg_producer>(filename + L"." + *ext, params);\r
141 }\r
142 \r
143 }}}