2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../StdAfx.h"
28 #include "../tbb_avcodec.h"
29 #include "../../ffmpeg_error.h"
31 #include <tbb/concurrent_unordered_map.h>
32 #include <tbb/concurrent_queue.h>
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>
40 #include <common/except.h>
41 #include <common/array.h>
42 #include <common/os/filesystem.h>
44 #include <tbb/parallel_for.h>
46 #include <common/assert.h>
47 #include <boost/filesystem.hpp>
48 #include <boost/lexical_cast.hpp>
49 #include <boost/rational.hpp>
56 #pragma warning (push)
57 #pragma warning (disable : 4244)
61 #include <libswscale/swscale.h>
62 #include <libavcodec/avcodec.h>
63 #include <libavformat/avformat.h>
69 namespace caspar { namespace ffmpeg {
71 core::field_mode get_mode(const AVFrame& frame)
73 if(!frame.interlaced_frame)
74 return core::field_mode::progressive;
76 return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;
79 core::pixel_format get_pixel_format(PixelFormat pix_fmt)
83 case PIX_FMT_GRAY8: return core::pixel_format::gray;
84 case PIX_FMT_RGB24: return core::pixel_format::rgb;
85 case PIX_FMT_BGR24: return core::pixel_format::bgr;
86 case PIX_FMT_BGRA: return core::pixel_format::bgra;
87 case PIX_FMT_ARGB: return core::pixel_format::argb;
88 case PIX_FMT_RGBA: return core::pixel_format::rgba;
89 case PIX_FMT_ABGR: return core::pixel_format::abgr;
90 case PIX_FMT_YUV444P: return core::pixel_format::ycbcr;
91 case PIX_FMT_YUV422P: return core::pixel_format::ycbcr;
92 case PIX_FMT_YUV420P: return core::pixel_format::ycbcr;
93 case PIX_FMT_YUV411P: return core::pixel_format::ycbcr;
94 case PIX_FMT_YUV410P: return core::pixel_format::ycbcr;
95 case PIX_FMT_YUVA420P: return core::pixel_format::ycbcra;
96 default: return core::pixel_format::invalid;
100 core::pixel_format_desc pixel_format_desc(PixelFormat pix_fmt, int width, int height)
103 AVPicture dummy_pict;
104 avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);
106 core::pixel_format_desc desc = get_pixel_format(pix_fmt);
110 case core::pixel_format::gray:
111 case core::pixel_format::luma:
113 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
116 case core::pixel_format::bgr:
117 case core::pixel_format::rgb:
119 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/3, height, 3));
122 case core::pixel_format::bgra:
123 case core::pixel_format::argb:
124 case core::pixel_format::rgba:
125 case core::pixel_format::abgr:
127 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
130 case core::pixel_format::ycbcr:
131 case core::pixel_format::ycbcra:
133 // Find chroma height
134 int size2 = static_cast<int>(dummy_pict.data[2] - dummy_pict.data[1]);
135 int h2 = size2/dummy_pict.linesize[1];
137 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
138 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[1], h2, 1));
139 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[2], h2, 1));
141 if(desc.format == core::pixel_format::ycbcra)
142 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
146 desc.format = core::pixel_format::invalid;
151 core::mutable_frame make_frame(const void* tag, const spl::shared_ptr<AVFrame>& decoded_frame, double fps, core::frame_factory& frame_factory, const core::audio_channel_layout& channel_layout)
153 static tbb::concurrent_unordered_map<int64_t, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contvalid_exts_;
155 if(decoded_frame->width < 1 || decoded_frame->height < 1)
156 return frame_factory.create_frame(tag, core::pixel_format_desc(core::pixel_format::invalid), core::audio_channel_layout::invalid());
158 const auto width = decoded_frame->width;
159 const auto height = decoded_frame->height;
160 auto desc = pixel_format_desc(static_cast<PixelFormat>(decoded_frame->format), width, height);
162 if(desc.format == core::pixel_format::invalid)
164 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);
165 auto target_pix_fmt = PIX_FMT_BGRA;
167 if(pix_fmt == PIX_FMT_UYVY422)
168 target_pix_fmt = PIX_FMT_YUV422P;
169 else if(pix_fmt == PIX_FMT_YUYV422)
170 target_pix_fmt = PIX_FMT_YUV422P;
171 else if(pix_fmt == PIX_FMT_UYYVYY411)
172 target_pix_fmt = PIX_FMT_YUV411P;
173 else if(pix_fmt == PIX_FMT_YUV420P10)
174 target_pix_fmt = PIX_FMT_YUV420P;
175 else if(pix_fmt == PIX_FMT_YUV422P10)
176 target_pix_fmt = PIX_FMT_YUV422P;
177 else if(pix_fmt == PIX_FMT_YUV444P10)
178 target_pix_fmt = PIX_FMT_YUV444P;
180 auto target_desc = pixel_format_desc(target_pix_fmt, width, height);
182 auto write = frame_factory.create_frame(tag, target_desc, channel_layout);
184 std::shared_ptr<SwsContext> sws_context;
186 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
188 int64_t key = ((static_cast<int64_t>(width) << 32) & 0xFFFF00000000) |
189 ((static_cast<int64_t>(height) << 16) & 0xFFFF0000) |
190 ((static_cast<int64_t>(pix_fmt) << 8) & 0xFF00) |
191 ((static_cast<int64_t>(target_pix_fmt) << 0) & 0xFF);
193 auto& pool = sws_contvalid_exts_[key];
195 if(!pool.try_pop(sws_context))
198 sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, target_pix_fmt, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
203 CASPAR_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") <<
204 boost::errinfo_api_function("sws_getContext"));
207 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
208 avcodec_get_frame_defaults(av_frame.get());
209 if(target_pix_fmt == PIX_FMT_BGRA)
211 auto size = avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), write.image_data(0).begin(), PIX_FMT_BGRA, width, height);
212 CASPAR_VERIFY(size == write.image_data(0).size());
216 av_frame->width = width;
217 av_frame->height = height;
218 for(int n = 0; n < target_desc.planes.size(); ++n)
220 av_frame->data[n] = write.image_data(n).begin();
221 av_frame->linesize[n] = target_desc.planes[n].linesize;
225 sws_scale(sws_context.get(), decoded_frame->data, decoded_frame->linesize, 0, height, av_frame->data, av_frame->linesize);
226 pool.push(sws_context);
228 return std::move(write);
232 auto write = frame_factory.create_frame(tag, desc, channel_layout);
234 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)
236 auto plane = desc.planes[n];
237 auto result = write.image_data(n).begin();
238 auto decoded = decoded_frame->data[n];
239 auto decoded_linesize = decoded_frame->linesize[n];
241 CASPAR_ASSERT(decoded);
242 CASPAR_ASSERT(write.image_data(n).begin());
244 // Copy line by line since ffmpeg sometimes pads each line.
245 tbb::affinity_partitioner ap;
246 tbb::parallel_for(tbb::blocked_range<int>(0, desc.planes[n].height), [&](const tbb::blocked_range<int>& r)
248 for(int y = r.begin(); y != r.end(); ++y)
249 A_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
253 return std::move(write);
257 spl::shared_ptr<AVFrame> make_av_frame(core::mutable_frame& frame)
259 std::array<uint8_t*, 4> data = {};
260 for(int n = 0; n < frame.pixel_format_desc().planes.size(); ++n)
261 data[n] = frame.image_data(n).begin();
263 return make_av_frame(data, frame.pixel_format_desc());
266 spl::shared_ptr<AVFrame> make_av_frame(std::array<uint8_t*, 4> data, const core::pixel_format_desc& pix_desc)
268 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
269 avcodec_get_frame_defaults(av_frame.get());
271 auto planes = pix_desc.planes;
272 auto format = pix_desc.format;
274 av_frame->width = planes[0].width;
275 av_frame->height = planes[0].height;
276 for(int n = 0; n < planes.size(); ++n)
278 av_frame->data[n] = data[n];
279 av_frame->linesize[n] = planes[n].linesize;
284 case core::pixel_format::rgb:
285 av_frame->format = PIX_FMT_RGB24;
287 case core::pixel_format::bgr:
288 av_frame->format = PIX_FMT_BGR24;
290 case core::pixel_format::rgba:
291 av_frame->format = PIX_FMT_RGBA;
293 case core::pixel_format::argb:
294 av_frame->format = PIX_FMT_ARGB;
296 case core::pixel_format::bgra:
297 av_frame->format = PIX_FMT_BGRA;
299 case core::pixel_format::abgr:
300 av_frame->format = PIX_FMT_ABGR;
302 case core::pixel_format::gray:
303 av_frame->format = PIX_FMT_GRAY8;
305 case core::pixel_format::ycbcr:
307 int y_w = planes[0].width;
308 int y_h = planes[0].height;
309 int c_w = planes[1].width;
310 int c_h = planes[1].height;
312 if(c_h == y_h && c_w == y_w)
313 av_frame->format = PIX_FMT_YUV444P;
314 else if(c_h == y_h && c_w*2 == y_w)
315 av_frame->format = PIX_FMT_YUV422P;
316 else if(c_h == y_h && c_w*4 == y_w)
317 av_frame->format = PIX_FMT_YUV411P;
318 else if(c_h*2 == y_h && c_w*2 == y_w)
319 av_frame->format = PIX_FMT_YUV420P;
320 else if(c_h*2 == y_h && c_w*4 == y_w)
321 av_frame->format = PIX_FMT_YUV410P;
325 case core::pixel_format::ycbcra:
326 av_frame->format = PIX_FMT_YUVA420P;
332 bool is_sane_fps(AVRational time_base)
334 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
335 return fps > 20.0 && fps < 65.0;
338 AVRational fix_time_base(AVRational time_base)
340 if(time_base.num == 1)
341 time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(time_base.den)))-1));
343 if(!is_sane_fps(time_base))
345 auto tmp = time_base;
354 double read_fps(AVFormatContext& context, double fail_value)
356 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
357 auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);
361 const auto video_context = context.streams[video_index]->codec;
362 const auto video_stream = context.streams[video_index];
364 auto frame_rate_time_base = video_stream->avg_frame_rate;
365 std::swap(frame_rate_time_base.num, frame_rate_time_base.den);
367 if (is_sane_fps(frame_rate_time_base))
369 return static_cast<double>(frame_rate_time_base.den) / static_cast<double>(frame_rate_time_base.num);
372 AVRational time_base = video_context->time_base;
374 if(boost::filesystem::path(context.filename).extension().string() == ".flv")
378 auto meta = read_flv_meta_info(context.filename);
379 return boost::lexical_cast<double>(meta["framerate"]);
388 time_base.num *= video_context->ticks_per_frame;
390 if(!is_sane_fps(time_base))
392 time_base = fix_time_base(time_base);
394 if(!is_sane_fps(time_base) && audio_index > -1)
396 auto& audio_context = *context.streams[audio_index]->codec;
397 auto& audio_stream = *context.streams[audio_index];
399 double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);
401 time_base.num = static_cast<int>(duration_sec*100000.0);
402 time_base.den = static_cast<int>(video_stream->nb_frames*100000);
407 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
409 double closest_fps = 0.0;
411 for (auto video_mode : enum_constants<core::video_format>())
413 auto format = core::video_format_desc(core::video_format(video_mode));
415 double diff1 = std::abs(format.fps - fps);
416 double diff2 = std::abs(closest_fps - fps);
419 closest_fps = format.fps;
428 void fix_meta_data(AVFormatContext& context)
430 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
434 auto video_stream = context.streams[video_index];
435 auto video_context = context.streams[video_index]->codec;
437 if(boost::filesystem::path(context.filename).extension().string() == ".flv")
441 auto meta = read_flv_meta_info(context.filename);
442 double fps = boost::lexical_cast<double>(meta["framerate"]);
443 video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);
449 auto stream_time = video_stream->time_base;
450 auto duration = video_stream->duration;
451 auto codec_time = video_context->time_base;
452 auto ticks = video_context->ticks_per_frame;
454 if(video_stream->nb_frames == 0)
455 video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);
460 spl::shared_ptr<AVPacket> create_packet()
462 spl::shared_ptr<AVPacket> packet(new AVPacket(), [](AVPacket* p)
468 av_init_packet(packet.get());
472 spl::shared_ptr<AVFrame> create_frame()
474 spl::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);
475 avcodec_get_frame_defaults(frame.get());
479 spl::shared_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index)
482 index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");
483 //if(strcmp(decoder->name, "prores") == 0 && decoder->next && strcmp(decoder->next->name, "prores_lgpl") == 0)
484 // decoder = decoder->next;
486 THROW_ON_ERROR2(tbb_avcodec_open(context.streams[index]->codec, decoder), "");
487 return spl::shared_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);
490 spl::shared_ptr<AVFormatContext> open_input(const std::wstring& filename)
492 AVFormatContext* weak_context = nullptr;
493 THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr), filename);
494 spl::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* p)
496 avformat_close_input(&p);
498 THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);
499 fix_meta_data(*context);
503 std::wstring print_mode(int width, int height, double fps, bool interlaced)
505 std::wostringstream fps_ss;
506 fps_ss << std::fixed << std::setprecision(2) << (!interlaced ? fps : 2.0 * fps);
508 return boost::lexical_cast<std::wstring>(width) + L"x" + boost::lexical_cast<std::wstring>(height) + (!interlaced ? L"p" : L"i") + fps_ss.str();
511 bool is_valid_file(const std::wstring& filename)
513 static const auto invalid_exts = {
529 static const auto valid_exts = {
543 auto ext = boost::to_lower_copy(boost::filesystem::path(filename).extension().wstring());
545 if(std::find(valid_exts.begin(), valid_exts.end(), ext) != valid_exts.end())
548 if(std::find(invalid_exts.begin(), invalid_exts.end(), ext) != invalid_exts.end())
551 auto u8filename = u8(filename);
555 pb.filename = u8filename.c_str();
557 if(av_probe_input_format2(&pb, false, &score) != nullptr)
560 std::ifstream file(u8filename);
562 std::vector<unsigned char> buf;
563 for(auto file_it = std::istreambuf_iterator<char>(file); file_it != std::istreambuf_iterator<char>() && buf.size() < 1024; ++file_it)
564 buf.push_back(*file_it);
570 pb.buf_size = static_cast<int>(buf.size());
572 return av_probe_input_format2(&pb, true, &score) != nullptr;
575 bool try_get_duration(const std::wstring filename, std::int64_t& duration, boost::rational<std::int64_t>& time_base)
577 AVFormatContext* weak_context = nullptr;
578 if (avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr) < 0)
581 std::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* p)
583 avformat_close_input(&p);
586 context->probesize = context->probesize / 10;
587 context->max_analyze_duration = context->probesize / 10;
589 if (avformat_find_stream_info(context.get(), nullptr) < 0)
592 const auto fps = read_fps(*context, 1.0);
594 const auto rational_fps = boost::rational<std::int64_t>(static_cast<int>(fps * AV_TIME_BASE), AV_TIME_BASE);
596 duration = boost::rational_cast<std::int64_t>(context->duration * rational_fps / AV_TIME_BASE);
598 if (rational_fps == 0)
601 time_base = 1 / rational_fps;
606 std::wstring probe_stem(const std::wstring& stem)
608 auto stem2 = boost::filesystem::path(stem);
609 auto parent = find_case_insensitive(stem2.parent_path().wstring());
614 auto dir = boost::filesystem::path(*parent);
616 for(auto it = boost::filesystem::directory_iterator(dir); it != boost::filesystem::directory_iterator(); ++it)
618 if(boost::iequals(it->path().stem().wstring(), stem2.filename().wstring()) && is_valid_file(it->path().wstring()))
619 return it->path().wstring();
624 core::audio_channel_layout get_audio_channel_layout(const AVCodecContext& codec_context, const std::wstring& channel_layout_spec)
626 auto num_channels = codec_context.channels;
628 if (!channel_layout_spec.empty())
630 if (boost::contains(channel_layout_spec, L":")) // Custom on the fly layout specified.
632 std::vector<std::wstring> type_and_channel_order;
633 boost::split(type_and_channel_order, channel_layout_spec, boost::is_any_of(L":"), boost::algorithm::token_compress_off);
634 auto& type = type_and_channel_order.at(0);
635 auto& order = type_and_channel_order.at(1);
637 return core::audio_channel_layout(num_channels, std::move(type), order);
639 else // Preconfigured named channel layout selected.
641 auto layout = core::audio_channel_layout_repository::get_default()->get_layout(channel_layout_spec);
644 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info(L"No channel layout with name " + channel_layout_spec + L" registered"));
646 layout->num_channels = num_channels;
652 if (!codec_context.channel_layout)
654 if (num_channels == 1)
655 return core::audio_channel_layout(num_channels, L"mono", L"FC");
656 else if (num_channels == 2)
657 return core::audio_channel_layout(num_channels, L"stereo", L"FL FR");
659 return core::audio_channel_layout(num_channels, L"", L""); // Passthru without named channels as is.
662 // What FFMpeg calls "channel layout" is only the "layout type" of a channel layout in
663 // CasparCG where the channel layout supports different orders as well.
664 // The user needs to provide additional mix-configs in casparcg.config to support more
665 // than the most common (5.1, mono and stereo) types.
667 // Based on information in https://ffmpeg.org/ffmpeg-utils.html#Channel-Layout
668 switch (codec_context.channel_layout)
670 case AV_CH_LAYOUT_MONO:
671 return core::audio_channel_layout(num_channels, L"mono", L"FC");
672 case AV_CH_LAYOUT_STEREO:
673 return core::audio_channel_layout(num_channels, L"stereo", L"FL FR");
674 case AV_CH_LAYOUT_2POINT1:
675 return core::audio_channel_layout(num_channels, L"2.1", L"FL FR LFE");
676 case AV_CH_LAYOUT_SURROUND:
677 return core::audio_channel_layout(num_channels, L"3.0", L"FL FR FC");
678 case AV_CH_LAYOUT_2_1:
679 return core::audio_channel_layout(num_channels, L"3.0(back)", L"FL FR BC");
680 case AV_CH_LAYOUT_4POINT0:
681 return core::audio_channel_layout(num_channels, L"4.0", L"FL FR FC BC");
682 case AV_CH_LAYOUT_QUAD:
683 return core::audio_channel_layout(num_channels, L"quad", L"FL FR BL BR");
684 case AV_CH_LAYOUT_2_2:
685 return core::audio_channel_layout(num_channels, L"quad(side)", L"FL FR SL SR");
686 case AV_CH_LAYOUT_3POINT1:
687 return core::audio_channel_layout(num_channels, L"3.1", L"FL FR FC LFE");
688 case AV_CH_LAYOUT_5POINT0_BACK:
689 return core::audio_channel_layout(num_channels, L"5.0", L"FL FR FC BL BR");
690 case AV_CH_LAYOUT_5POINT0:
691 return core::audio_channel_layout(num_channels, L"5.0(side)", L"FL FR FC SL SR");
692 case AV_CH_LAYOUT_4POINT1:
693 return core::audio_channel_layout(num_channels, L"4.1", L"FL FR FC LFE BC");
694 case AV_CH_LAYOUT_5POINT1_BACK:
695 return core::audio_channel_layout(num_channels, L"5.1", L"FL FR FC LFE BL BR");
696 case AV_CH_LAYOUT_5POINT1:
697 return core::audio_channel_layout(num_channels, L"5.1(side)", L"FL FR FC LFE SL SR");
698 case AV_CH_LAYOUT_6POINT0:
699 return core::audio_channel_layout(num_channels, L"6.0", L"FL FR FC BC SL SR");
700 case AV_CH_LAYOUT_6POINT0_FRONT:
701 return core::audio_channel_layout(num_channels, L"6.0(front)", L"FL FR FLC FRC SL SR");
702 case AV_CH_LAYOUT_HEXAGONAL:
703 return core::audio_channel_layout(num_channels, L"hexagonal", L"FL FR FC BL BR BC");
704 case AV_CH_LAYOUT_6POINT1:
705 return core::audio_channel_layout(num_channels, L"6.1", L"FL FR FC LFE BC SL SR");
706 case AV_CH_LAYOUT_6POINT1_BACK:
707 return core::audio_channel_layout(num_channels, L"6.1(back)", L"FL FR FC LFE BL BR BC");
708 case AV_CH_LAYOUT_6POINT1_FRONT:
709 return core::audio_channel_layout(num_channels, L"6.1(front)", L"FL FR LFE FLC FRC SL SR");
710 case AV_CH_LAYOUT_7POINT0:
711 return core::audio_channel_layout(num_channels, L"7.0", L"FL FR FC BL BR SL SR");
712 case AV_CH_LAYOUT_7POINT0_FRONT:
713 return core::audio_channel_layout(num_channels, L"7.0(front)", L"FL FR FC FLC FRC SL SR");
714 case AV_CH_LAYOUT_7POINT1:
715 return core::audio_channel_layout(num_channels, L"7.1", L"FL FR FC LFE BL BR SL SR");
716 case AV_CH_LAYOUT_7POINT1_WIDE_BACK:
717 return core::audio_channel_layout(num_channels, L"7.1(wide)", L"FL FR FC LFE BL BR FLC FRC");
718 case AV_CH_LAYOUT_7POINT1_WIDE:
719 return core::audio_channel_layout(num_channels, L"7.1(wide-side)", L"FL FR FC LFE FLC FRC SL SR");
720 case AV_CH_LAYOUT_STEREO_DOWNMIX:
721 return core::audio_channel_layout(num_channels, L"downmix", L"DL DR");
724 return core::audio_channel_layout(num_channels, L"", L"");
728 // av_get_default_channel_layout does not work for layouts not predefined in ffmpeg. This is needed to support > 8 channels.
729 std::int64_t create_channel_layout_bitmask(int num_channels)
731 if (num_channels > 63)
732 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info(L"FFMpeg cannot handle more than 63 audio channels"));
734 const auto ALL_63_CHANNELS = 0x7FFFFFFFFFFFFFFFULL;
736 auto to_shift = 63 - num_channels;
737 auto result = ALL_63_CHANNELS >> to_shift;
739 return static_cast<std::int64_t>(result);
743 //void av_dup_frame(AVFrame* frame)
745 // AVFrame* new_frame = avcodec_alloc_frame();
748 // const uint8_t *src_data[4] = {0};
749 // memcpy(const_cast<uint8_t**>(&src_data[0]), frame->data, 4);
750 // const int src_linesizes[4] = {0};
751 // memcpy(const_cast<int*>(&src_linesizes[0]), frame->linesize, 4);
753 // av_image_alloc(new_frame->data, new_frame->linesize, new_frame->width, new_frame->height, frame->format, 16);
755 // av_image_copy(new_frame->data, new_frame->linesize, src_data, src_linesizes, frame->format, new_frame->width, new_frame->height);