]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/util/util.cpp
0f525d6f02f5649981b17d031bc1c9c4380f98d2
[casparcg] / modules / ffmpeg / producer / util / util.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "util.h"
25
26 #include "flv.h"
27
28 #include "../tbb_avcodec.h"
29 #include "../../ffmpeg_error.h"
30
31 #include <tbb/concurrent_unordered_map.h>
32 #include <tbb/concurrent_queue.h>
33
34 #include <core/frame/frame_transform.h>
35 #include <core/frame/frame_factory.h>
36 #include <core/frame/frame.h>
37 #include <core/frame/audio_channel_layout.h>
38 #include <core/producer/frame_producer.h>
39
40 #include <common/except.h>
41 #include <common/array.h>
42 #include <common/os/filesystem.h>
43 #include <common/memcpy.h>
44
45 #include <tbb/parallel_for.h>
46
47 #include <common/assert.h>
48 #include <boost/filesystem.hpp>
49 #include <boost/lexical_cast.hpp>
50 #include <boost/rational.hpp>
51
52 #include <fstream>
53
54 #include <asmlib.h>
55
56 #if defined(_MSC_VER)
57 #pragma warning (push)
58 #pragma warning (disable : 4244)
59 #endif
60 extern "C" 
61 {
62         #include <libswscale/swscale.h>
63         #include <libavcodec/avcodec.h>
64         #include <libavformat/avformat.h>
65 }
66 #if defined(_MSC_VER)
67 #pragma warning (pop)
68 #endif
69
70 namespace caspar { namespace ffmpeg {
71                 
72 core::field_mode get_mode(const AVFrame& frame)
73 {
74         if(!frame.interlaced_frame)
75                 return core::field_mode::progressive;
76
77         return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;
78 }
79
80 core::pixel_format get_pixel_format(PixelFormat pix_fmt)
81 {
82         switch(pix_fmt)
83         {
84         case PIX_FMT_GRAY8:                     return core::pixel_format::gray;
85         case PIX_FMT_RGB24:                     return core::pixel_format::rgb;
86         case PIX_FMT_BGR24:                     return core::pixel_format::bgr;
87         case PIX_FMT_BGRA:                      return core::pixel_format::bgra;
88         case PIX_FMT_ARGB:                      return core::pixel_format::argb;
89         case PIX_FMT_RGBA:                      return core::pixel_format::rgba;
90         case PIX_FMT_ABGR:                      return core::pixel_format::abgr;
91         case PIX_FMT_YUV444P:           return core::pixel_format::ycbcr;
92         case PIX_FMT_YUV422P:           return core::pixel_format::ycbcr;
93         case PIX_FMT_YUV420P:           return core::pixel_format::ycbcr;
94         case PIX_FMT_YUV411P:           return core::pixel_format::ycbcr;
95         case PIX_FMT_YUV410P:           return core::pixel_format::ycbcr;
96         case PIX_FMT_YUVA420P:          return core::pixel_format::ycbcra;
97         default:                                        return core::pixel_format::invalid;
98         }
99 }
100
101 core::pixel_format_desc pixel_format_desc(PixelFormat pix_fmt, int width, int height)
102 {
103         // Get linesizes
104         AVPicture dummy_pict;   
105         avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);
106
107         core::pixel_format_desc desc = get_pixel_format(pix_fmt);
108                 
109         switch(desc.format)
110         {
111         case core::pixel_format::gray:
112         case core::pixel_format::luma:
113                 {
114                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));                                               
115                         return desc;
116                 }
117         case core::pixel_format::bgr:
118         case core::pixel_format::rgb:
119                 {
120                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/3, height, 3));                                             
121                         return desc;
122                 }
123         case core::pixel_format::bgra:
124         case core::pixel_format::argb:
125         case core::pixel_format::rgba:
126         case core::pixel_format::abgr:
127                 {
128                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));                                             
129                         return desc;
130                 }
131         case core::pixel_format::ycbcr:
132         case core::pixel_format::ycbcra:
133                 {               
134                         // Find chroma height
135                         int size2 = static_cast<int>(dummy_pict.data[2] - dummy_pict.data[1]);
136                         int h2 = size2/dummy_pict.linesize[1];                  
137
138                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
139                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
140                         desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
141
142                         if(desc.format == core::pixel_format::ycbcra)                                           
143                                 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));       
144                         return desc;
145                 }               
146         default:                
147                 desc.format = core::pixel_format::invalid;
148                 return desc;
149         }
150 }
151
152 core::mutable_frame make_frame(const void* tag, const spl::shared_ptr<AVFrame>& decoded_frame, core::frame_factory& frame_factory, const core::audio_channel_layout& channel_layout)
153 {                       
154         static tbb::concurrent_unordered_map<int64_t, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contvalid_exts_;
155         
156         if(decoded_frame->width < 1 || decoded_frame->height < 1)
157                 return frame_factory.create_frame(tag, core::pixel_format_desc(core::pixel_format::invalid), core::audio_channel_layout::invalid());
158
159         const auto width  = decoded_frame->width;
160         const auto height = decoded_frame->height;
161         auto desc                 = pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);
162                 
163         if(desc.format == core::pixel_format::invalid)
164         {
165                 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);
166                 auto target_pix_fmt = PIX_FMT_BGRA;
167
168                 if(pix_fmt == PIX_FMT_UYVY422)
169                         target_pix_fmt = PIX_FMT_YUV422P;
170                 else if(pix_fmt == PIX_FMT_YUYV422)
171                         target_pix_fmt = PIX_FMT_YUV422P;
172                 else if(pix_fmt == PIX_FMT_UYYVYY411)
173                         target_pix_fmt = PIX_FMT_YUV411P;
174                 else if(pix_fmt == PIX_FMT_YUV420P10)
175                         target_pix_fmt = PIX_FMT_YUV420P;
176                 else if(pix_fmt == PIX_FMT_YUV422P10)
177                         target_pix_fmt = PIX_FMT_YUV422P;
178                 else if(pix_fmt == PIX_FMT_YUV444P10)
179                         target_pix_fmt = PIX_FMT_YUV444P;
180                 
181                 auto target_desc = pixel_format_desc(target_pix_fmt, width, height);
182
183                 auto write = frame_factory.create_frame(tag, target_desc, channel_layout);
184
185                 std::shared_ptr<SwsContext> sws_context;
186
187                 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
188                 
189                 int64_t key = ((static_cast<int64_t>(width)                      << 32) & 0xFFFF00000000) | 
190                                           ((static_cast<int64_t>(height)                 << 16) & 0xFFFF0000) | 
191                                           ((static_cast<int64_t>(pix_fmt)                <<  8) & 0xFF00) | 
192                                           ((static_cast<int64_t>(target_pix_fmt) <<  0) & 0xFF);
193                         
194                 auto& pool = sws_contvalid_exts_[key];
195                                                 
196                 if(!pool.try_pop(sws_context))
197                 {
198                         double param;
199                         sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, target_pix_fmt, SWS_BILINEAR, nullptr, nullptr, &param), sws_freeContext);
200                 }
201                         
202                 if(!sws_context)
203                 {
204                         CASPAR_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") << 
205                                                                         boost::errinfo_api_function("sws_getContext"));
206                 }       
207                 
208                 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);      
209                 avcodec_get_frame_defaults(av_frame.get());                     
210                 if(target_pix_fmt == PIX_FMT_BGRA)
211                 {
212                         auto size = avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write.image_data(0).begin(), PIX_FMT_BGRA, width, height);
213                         CASPAR_VERIFY(size == write.image_data(0).size()); 
214                 }
215                 else
216                 {
217                         av_frame->width  = width;
218                         av_frame->height = height;
219                         for(int n = 0; n < target_desc.planes.size(); ++n)
220                         {
221                                 av_frame->data[n]               = write.image_data(n).begin();
222                                 av_frame->linesize[n]   = target_desc.planes[n].linesize;
223                         }
224                 }
225
226                 sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);      
227                 pool.push(sws_context); 
228
229                 return std::move(write);
230         }
231         else
232         {
233                 auto write = frame_factory.create_frame(tag, desc, channel_layout);
234                 
235                 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)
236                 {
237                         auto plane            = desc.planes[n];
238                         auto result           = write.image_data(n).begin();
239                         auto decoded          = decoded_frame->data[n];
240                         auto decoded_linesize = decoded_frame->linesize[n];
241                         
242                         CASPAR_ASSERT(decoded);
243                         CASPAR_ASSERT(write.image_data(n).begin());
244
245                         if (decoded_linesize != plane.linesize)
246                         {
247                                 // Copy line by line since ffmpeg sometimes pads each line.
248                                 tbb::affinity_partitioner ap;
249                                 tbb::parallel_for(tbb::blocked_range<int>(0, desc.planes[n].height), [&](const tbb::blocked_range<int>& r)
250                                 {
251                                         for (int y = r.begin(); y != r.end(); ++y)
252                                                 A_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
253                                 }, ap);
254                         }
255                         else
256                         {
257                                 fast_memcpy(result, decoded, plane.size);
258                         }
259                 }
260         
261                 return std::move(write);
262         }
263 }
264
265 spl::shared_ptr<AVFrame> make_av_frame(core::mutable_frame& frame)
266 {
267         std::array<uint8_t*, 4> data = {};
268         for(int n = 0; n < frame.pixel_format_desc().planes.size(); ++n)
269                 data[n] = frame.image_data(n).begin();
270
271         return make_av_frame(data, frame.pixel_format_desc());
272 }
273
274 spl::shared_ptr<AVFrame> make_av_frame(std::array<uint8_t*, 4> data, const core::pixel_format_desc& pix_desc)
275 {
276         spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);      
277         avcodec_get_frame_defaults(av_frame.get());
278         
279         auto planes              = pix_desc.planes;
280         auto format              = pix_desc.format;
281
282         av_frame->width  = planes[0].width;
283         av_frame->height = planes[0].height;
284         for(int n = 0; n < planes.size(); ++n)  
285         {
286                 av_frame->data[n]         = data[n];
287                 av_frame->linesize[n] = planes[n].linesize;     
288         }
289
290         switch(format)
291         {
292         case core::pixel_format::rgb:
293                 av_frame->format = PIX_FMT_RGB24;
294                 break;
295         case core::pixel_format::bgr:
296                 av_frame->format = PIX_FMT_BGR24;
297                 break;
298         case core::pixel_format::rgba:
299                 av_frame->format = PIX_FMT_RGBA; 
300                 break;
301         case core::pixel_format::argb:
302                 av_frame->format = PIX_FMT_ARGB; 
303                 break;
304         case core::pixel_format::bgra:
305                 av_frame->format = PIX_FMT_BGRA; 
306                 break;
307         case core::pixel_format::abgr:
308                 av_frame->format = PIX_FMT_ABGR; 
309                 break;
310         case core::pixel_format::gray:
311                 av_frame->format = PIX_FMT_GRAY8; 
312                 break;
313         case core::pixel_format::ycbcr:
314         {
315                 int y_w = planes[0].width;
316                 int y_h = planes[0].height;
317                 int c_w = planes[1].width;
318                 int c_h = planes[1].height;
319
320                 if(c_h == y_h && c_w == y_w)
321                         av_frame->format = PIX_FMT_YUV444P;
322                 else if(c_h == y_h && c_w*2 == y_w)
323                         av_frame->format = PIX_FMT_YUV422P;
324                 else if(c_h == y_h && c_w*4 == y_w)
325                         av_frame->format = PIX_FMT_YUV411P;
326                 else if(c_h*2 == y_h && c_w*2 == y_w)
327                         av_frame->format = PIX_FMT_YUV420P;
328                 else if(c_h*2 == y_h && c_w*4 == y_w)
329                         av_frame->format = PIX_FMT_YUV410P;
330
331                 break;
332         }
333         case core::pixel_format::ycbcra:
334                 av_frame->format = PIX_FMT_YUVA420P;
335                 break;
336         }
337         return av_frame;
338 }
339
340 bool is_sane_fps(AVRational time_base)
341 {
342         double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
343         return fps > 20.0 && fps < 65.0;
344 }
345
346 AVRational fix_time_base(AVRational time_base)
347 {
348         if(time_base.num == 1)
349                 time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(time_base.den)))-1));    
350                         
351         if(!is_sane_fps(time_base))
352         {
353                 auto tmp = time_base;
354                 tmp.den /= 2;
355                 if(is_sane_fps(tmp))
356                         time_base = tmp;
357         }
358
359         return time_base;
360 }
361
362 double read_fps(AVFormatContext& context, double fail_value)
363 {                                               
364         auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
365         auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);
366         
367         if(video_index > -1)
368         {
369                 const auto video_context = context.streams[video_index]->codec;
370                 const auto video_stream  = context.streams[video_index];
371
372                 auto frame_rate_time_base = video_stream->avg_frame_rate;
373                 std::swap(frame_rate_time_base.num, frame_rate_time_base.den);
374
375                 if (is_sane_fps(frame_rate_time_base))
376                 {
377                         return static_cast<double>(frame_rate_time_base.den) / static_cast<double>(frame_rate_time_base.num);
378                 }
379
380                 AVRational time_base = video_context->time_base;
381
382                 if(boost::filesystem::path(context.filename).extension().string() == ".flv")
383                 {
384                         try
385                         {
386                                 auto meta = read_flv_meta_info(context.filename);
387                                 return boost::lexical_cast<double>(meta["framerate"]);
388                         }
389                         catch(...)
390                         {
391                                 return 0.0;
392                         }
393                 }
394                 else
395                 {
396                         time_base.num *= video_context->ticks_per_frame;
397
398                         if(!is_sane_fps(time_base))
399                         {                       
400                                 time_base = fix_time_base(time_base);
401
402                                 if(!is_sane_fps(time_base) && audio_index > -1)
403                                 {
404                                         auto& audio_context = *context.streams[audio_index]->codec;
405                                         auto& audio_stream  = *context.streams[audio_index];
406
407                                         double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);
408                                                                 
409                                         time_base.num = static_cast<int>(duration_sec*100000.0);
410                                         time_base.den = static_cast<int>(video_stream->nb_frames*100000);
411                                 }
412                         }
413                 }
414                 
415                 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
416
417                 double closest_fps = 0.0;
418
419                 for (auto video_mode : enum_constants<core::video_format>())
420                 {
421                         auto format = core::video_format_desc(core::video_format(video_mode));
422
423                         double diff1 = std::abs(format.fps - fps);
424                         double diff2 = std::abs(closest_fps - fps);
425
426                         if(diff1 < diff2)
427                                 closest_fps = format.fps;
428                 }
429         
430                 return closest_fps;
431         }
432
433         return fail_value;      
434 }
435
436 void fix_meta_data(AVFormatContext& context)
437 {
438         auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
439
440         if(video_index > -1)
441         {
442                 auto video_stream   = context.streams[video_index];
443                 auto video_context  = context.streams[video_index]->codec;
444                                                 
445                 if(boost::filesystem::path(context.filename).extension().string() == ".flv")
446                 {
447                         try
448                         {
449                                 auto meta = read_flv_meta_info(context.filename);
450                                 double fps = boost::lexical_cast<double>(meta["framerate"]);
451                                 video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);
452                         }
453                         catch(...){}
454                 }
455                 else
456                 {
457                         auto stream_time = video_stream->time_base;
458                         auto duration    = video_stream->duration;
459                         auto codec_time  = video_context->time_base;
460                         auto ticks               = video_context->ticks_per_frame;
461
462                         if(video_stream->nb_frames == 0)
463                                 video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);     
464                 }
465         }
466 }
467
468 spl::shared_ptr<AVPacket> create_packet()
469 {
470         spl::shared_ptr<AVPacket> packet(new AVPacket(), [](AVPacket* p)
471         {
472                 av_free_packet(p);
473                 delete p;
474         });
475         
476         av_init_packet(packet.get());
477         return packet;
478 }
479
480 spl::shared_ptr<AVFrame> create_frame()
481 {       
482         spl::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);
483         avcodec_get_frame_defaults(frame.get());
484         return frame;
485 }
486
487 spl::shared_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index, bool single_threaded)
488 {       
489         AVCodec* decoder;
490         index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");
491         //if(strcmp(decoder->name, "prores") == 0 && decoder->next && strcmp(decoder->next->name, "prores_lgpl") == 0)
492         //      decoder = decoder->next;
493
494         THROW_ON_ERROR2(tbb_avcodec_open(context.streams[index]->codec, decoder, single_threaded), "");
495         return spl::shared_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);
496 }
497
498 spl::shared_ptr<AVFormatContext> open_input(const std::wstring& filename)
499 {
500         AVFormatContext* weak_context = nullptr;
501         THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr), filename);
502         spl::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* p)
503         {
504                 avformat_close_input(&p);
505         });
506         THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);
507         fix_meta_data(*context);
508         return context;
509 }
510
511 std::wstring print_mode(int width, int height, double fps, bool interlaced)
512 {
513         std::wostringstream fps_ss;
514         fps_ss << std::fixed << std::setprecision(2) << (!interlaced ? fps : 2.0 * fps);
515
516         return boost::lexical_cast<std::wstring>(width) + L"x" + boost::lexical_cast<std::wstring>(height) + (!interlaced ? L"p" : L"i") + fps_ss.str();
517 }
518
519 bool is_valid_file(const std::wstring& filename, bool only_video)
520 {                               
521         static const auto invalid_exts = {
522                 L".png",
523                 L".tga",
524                 L".bmp",
525                 L".jpg",
526                 L".jpeg",
527                 L".gif",
528                 L".tiff",
529                 L".tif",
530                 L".jp2",
531                 L".jpx",
532                 L".j2k",
533                 L".j2c",
534                 L".swf",
535                 L".ct"
536         };
537         static const auto only_audio = {
538                 L".mp3",
539                 L".wav",
540                 L".wma"
541         };
542         static const auto valid_exts = {
543                 L".m2t",
544                 L".mov",
545                 L".mp4",
546                 L".dv",
547                 L".flv",
548                 L".mpg",
549                 L".dnxhd",
550                 L".h264",
551                 L".prores"
552         };
553
554         auto ext = boost::to_lower_copy(boost::filesystem::path(filename).extension().wstring());
555                 
556         if(std::find(valid_exts.begin(), valid_exts.end(), ext) != valid_exts.end())
557                 return true;
558
559         if (!only_video && std::find(only_audio.begin(), only_audio.end(), ext) != only_audio.end())
560                 return true;
561         
562         if(std::find(invalid_exts.begin(), invalid_exts.end(), ext) != invalid_exts.end())
563                 return false;   
564
565         if (only_video && std::find(only_audio.begin(), only_audio.end(), ext) != only_audio.end())
566                 return false;
567
568         auto u8filename = u8(filename);
569         
570         int score = 0;
571         AVProbeData pb = {};
572         pb.filename = u8filename.c_str();
573
574         if(av_probe_input_format2(&pb, false, &score) != nullptr)
575                 return true;
576
577         std::ifstream file(u8filename);
578
579         std::vector<unsigned char> buf;
580         for(auto file_it = std::istreambuf_iterator<char>(file); file_it != std::istreambuf_iterator<char>() && buf.size() < 1024; ++file_it)
581                 buf.push_back(*file_it);
582
583         if(buf.empty())
584                 return nullptr;
585
586         pb.buf          = buf.data();
587         pb.buf_size = static_cast<int>(buf.size());
588
589         return av_probe_input_format2(&pb, true, &score) != nullptr;
590 }
591
592 bool try_get_duration(const std::wstring filename, std::int64_t& duration, boost::rational<std::int64_t>& time_base)
593 {
594         AVFormatContext* weak_context = nullptr;
595         if (avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr) < 0)
596                 return false;
597
598         std::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* p)
599         {
600                 avformat_close_input(&p);
601         });
602
603         context->probesize = context->probesize / 10;
604         context->max_analyze_duration = context->probesize / 10;
605
606         if (avformat_find_stream_info(context.get(), nullptr) < 0)
607                 return false;
608
609         const auto fps = read_fps(*context, 1.0);
610
611         const auto rational_fps = boost::rational<std::int64_t>(static_cast<int>(fps * AV_TIME_BASE), AV_TIME_BASE);
612
613         duration = boost::rational_cast<std::int64_t>(context->duration * rational_fps / AV_TIME_BASE);
614
615         if (rational_fps == 0)
616                 return false;
617
618         time_base = 1 / rational_fps;
619
620         return true;
621 }
622
623 std::wstring probe_stem(const std::wstring& stem, bool only_video)
624 {
625         auto stem2 = boost::filesystem::path(stem);
626         auto parent = find_case_insensitive(stem2.parent_path().wstring());
627
628         if (!parent)
629                 return L"";
630
631         auto dir = boost::filesystem::path(*parent);
632
633         for(auto it = boost::filesystem::directory_iterator(dir); it != boost::filesystem::directory_iterator(); ++it)
634         {
635                 if(boost::iequals(it->path().stem().wstring(), stem2.filename().wstring()) && is_valid_file(it->path().wstring(), only_video))
636                         return it->path().wstring();
637         }
638         return L"";
639 }
640
641 core::audio_channel_layout get_audio_channel_layout(const AVCodecContext& codec_context, const std::wstring& channel_layout_spec)
642 {
643         auto num_channels = codec_context.channels;
644
645         if (!channel_layout_spec.empty())
646         {
647                 if (boost::contains(channel_layout_spec, L":")) // Custom on the fly layout specified.
648                 {
649                         std::vector<std::wstring> type_and_channel_order;
650                         boost::split(type_and_channel_order, channel_layout_spec, boost::is_any_of(L":"), boost::algorithm::token_compress_off);
651                         auto& type                      = type_and_channel_order.at(0);
652                         auto& order                     = type_and_channel_order.at(1);
653
654                         return core::audio_channel_layout(num_channels, std::move(type), order);
655                 }
656                 else // Preconfigured named channel layout selected.
657                 {
658                         auto layout = core::audio_channel_layout_repository::get_default()->get_layout(channel_layout_spec);
659
660                         if (!layout)
661                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"No channel layout with name " + channel_layout_spec + L" registered"));
662
663                         layout->num_channels = num_channels;
664
665                         return *layout;
666                 }
667         }
668
669         if (!codec_context.channel_layout)
670         {
671                 if (num_channels == 1)
672                         return core::audio_channel_layout(num_channels, L"mono", L"FC");
673                 else if (num_channels == 2)
674                         return core::audio_channel_layout(num_channels, L"stereo", L"FL FR");
675                 else
676                         return core::audio_channel_layout(num_channels, L"", L""); // Passthru without named channels as is.
677         }
678
679         // What FFmpeg calls "channel layout" is only the "layout type" of a channel layout in
680         // CasparCG where the channel layout supports different orders as well.
681         // The user needs to provide additional mix-configs in casparcg.config to support more
682         // than the most common (5.1, mono and stereo) types.
683
684         // Based on information in https://ffmpeg.org/ffmpeg-utils.html#Channel-Layout
685         switch (codec_context.channel_layout)
686         {
687         case AV_CH_LAYOUT_MONO:
688                 return core::audio_channel_layout(num_channels, L"mono",                        L"FC");
689         case AV_CH_LAYOUT_STEREO:
690                 return core::audio_channel_layout(num_channels, L"stereo",                      L"FL FR");
691         case AV_CH_LAYOUT_2POINT1:
692                 return core::audio_channel_layout(num_channels, L"2.1",                         L"FL FR LFE");
693         case AV_CH_LAYOUT_SURROUND:
694                 return core::audio_channel_layout(num_channels, L"3.0",                         L"FL FR FC");
695         case AV_CH_LAYOUT_2_1:
696                 return core::audio_channel_layout(num_channels, L"3.0(back)",           L"FL FR BC");
697         case AV_CH_LAYOUT_4POINT0:
698                 return core::audio_channel_layout(num_channels, L"4.0",                         L"FL FR FC BC");
699         case AV_CH_LAYOUT_QUAD:
700                 return core::audio_channel_layout(num_channels, L"quad",                        L"FL FR BL BR");
701         case AV_CH_LAYOUT_2_2:
702                 return core::audio_channel_layout(num_channels, L"quad(side)",          L"FL FR SL SR");
703         case AV_CH_LAYOUT_3POINT1:
704                 return core::audio_channel_layout(num_channels, L"3.1",                         L"FL FR FC LFE");
705         case AV_CH_LAYOUT_5POINT0_BACK:
706                 return core::audio_channel_layout(num_channels, L"5.0",                         L"FL FR FC BL BR");
707         case AV_CH_LAYOUT_5POINT0:
708                 return core::audio_channel_layout(num_channels, L"5.0(side)",           L"FL FR FC SL SR");
709         case AV_CH_LAYOUT_4POINT1:
710                 return core::audio_channel_layout(num_channels, L"4.1",                         L"FL FR FC LFE BC");
711         case AV_CH_LAYOUT_5POINT1_BACK:
712                 return core::audio_channel_layout(num_channels, L"5.1",                         L"FL FR FC LFE BL BR");
713         case AV_CH_LAYOUT_5POINT1:
714                 return core::audio_channel_layout(num_channels, L"5.1(side)",           L"FL FR FC LFE SL SR");
715         case AV_CH_LAYOUT_6POINT0:
716                 return core::audio_channel_layout(num_channels, L"6.0",                         L"FL FR FC BC SL SR");
717         case AV_CH_LAYOUT_6POINT0_FRONT:
718                 return core::audio_channel_layout(num_channels, L"6.0(front)",          L"FL FR FLC FRC SL SR");
719         case AV_CH_LAYOUT_HEXAGONAL:
720                 return core::audio_channel_layout(num_channels, L"hexagonal",           L"FL FR FC BL BR BC");
721         case AV_CH_LAYOUT_6POINT1:
722                 return core::audio_channel_layout(num_channels, L"6.1",                         L"FL FR FC LFE BC SL SR");
723         case AV_CH_LAYOUT_6POINT1_BACK:
724                 return core::audio_channel_layout(num_channels, L"6.1(back)",           L"FL FR FC LFE BL BR BC");
725         case AV_CH_LAYOUT_6POINT1_FRONT:
726                 return core::audio_channel_layout(num_channels, L"6.1(front)",          L"FL FR LFE FLC FRC SL SR");
727         case AV_CH_LAYOUT_7POINT0:
728                 return core::audio_channel_layout(num_channels, L"7.0",                         L"FL FR FC BL BR SL SR");
729         case AV_CH_LAYOUT_7POINT0_FRONT:
730                 return core::audio_channel_layout(num_channels, L"7.0(front)",          L"FL FR FC FLC FRC SL SR");
731         case AV_CH_LAYOUT_7POINT1:
732                 return core::audio_channel_layout(num_channels, L"7.1",                         L"FL FR FC LFE BL BR SL SR");
733         case AV_CH_LAYOUT_7POINT1_WIDE_BACK:
734                 return core::audio_channel_layout(num_channels, L"7.1(wide)",           L"FL FR FC LFE BL BR FLC FRC");
735         case AV_CH_LAYOUT_7POINT1_WIDE:
736                 return core::audio_channel_layout(num_channels, L"7.1(wide-side)",      L"FL FR FC LFE FLC FRC SL SR");
737         case AV_CH_LAYOUT_STEREO_DOWNMIX:
738                 return core::audio_channel_layout(num_channels, L"downmix",                     L"DL DR");
739         default:
740                 // Passthru
741                 return core::audio_channel_layout(num_channels, L"", L"");
742         }
743 }
744
745 // av_get_default_channel_layout does not work for layouts not predefined in ffmpeg. This is needed to support > 8 channels.
746 std::int64_t create_channel_layout_bitmask(int num_channels)
747 {
748         if (num_channels > 63)
749                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info(L"FFmpeg cannot handle more than 63 audio channels"));
750
751         const auto ALL_63_CHANNELS = 0x7FFFFFFFFFFFFFFFULL;
752
753         auto to_shift = 63 - num_channels;
754         auto result = ALL_63_CHANNELS >> to_shift;
755
756         return static_cast<std::int64_t>(result);
757 }
758
759 //
760 //void av_dup_frame(AVFrame* frame)
761 //{
762 //      AVFrame* new_frame = avcodec_alloc_frame();
763 //
764 //
765 //      const uint8_t *src_data[4] = {0};
766 //      memcpy(const_cast<uint8_t**>(&src_data[0]), frame->data, 4);
767 //      const int src_linesizes[4] = {0};
768 //      memcpy(const_cast<int*>(&src_linesizes[0]), frame->linesize, 4);
769 //
770 //      av_image_alloc(new_frame->data, new_frame->linesize, new_frame->width, new_frame->height, frame->format, 16);
771 //
772 //      av_image_copy(new_frame->data, new_frame->linesize, src_data, src_linesizes, frame->format, new_frame->width, new_frame->height);
773 //
774 //      frame =
775 //}
776
777 }}