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>
43 #include <common/memcpy.h>
45 #include <tbb/parallel_for.h>
47 #include <common/assert.h>
48 #include <boost/filesystem.hpp>
49 #include <boost/lexical_cast.hpp>
50 #include <boost/rational.hpp>
57 #pragma warning (push)
58 #pragma warning (disable : 4244)
62 #include <libswscale/swscale.h>
63 #include <libavcodec/avcodec.h>
64 #include <libavformat/avformat.h>
70 namespace caspar { namespace ffmpeg {
72 core::field_mode get_mode(const AVFrame& frame)
74 if(!frame.interlaced_frame)
75 return core::field_mode::progressive;
77 return frame.top_field_first ? core::field_mode::upper : core::field_mode::lower;
80 core::pixel_format get_pixel_format(PixelFormat pix_fmt)
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;
101 core::pixel_format_desc pixel_format_desc(PixelFormat pix_fmt, int width, int height)
104 AVPicture dummy_pict;
105 avpicture_fill(&dummy_pict, nullptr, pix_fmt, width, height);
107 core::pixel_format_desc desc = get_pixel_format(pix_fmt);
111 case core::pixel_format::gray:
112 case core::pixel_format::luma:
114 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0], height, 1));
117 case core::pixel_format::bgr:
118 case core::pixel_format::rgb:
120 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/3, height, 3));
123 case core::pixel_format::bgra:
124 case core::pixel_format::argb:
125 case core::pixel_format::rgba:
126 case core::pixel_format::abgr:
128 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[0]/4, height, 4));
131 case core::pixel_format::ycbcr:
132 case core::pixel_format::ycbcra:
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];
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));
142 if(desc.format == core::pixel_format::ycbcra)
143 desc.planes.push_back(core::pixel_format_desc::plane(dummy_pict.linesize[3], height, 1));
147 desc.format = core::pixel_format::invalid;
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)
154 static tbb::concurrent_unordered_map<int64_t, tbb::concurrent_queue<std::shared_ptr<SwsContext>>> sws_contvalid_exts_;
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());
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);
163 if(desc.format == core::pixel_format::invalid)
165 auto pix_fmt = static_cast<PixelFormat>(decoded_frame->format);
166 auto target_pix_fmt = PIX_FMT_BGRA;
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;
181 auto target_desc = pixel_format_desc(target_pix_fmt, width, height);
183 auto write = frame_factory.create_frame(tag, target_desc, channel_layout);
185 std::shared_ptr<SwsContext> sws_context;
187 //CASPAR_LOG(warning) << "Hardware accelerated color transform not supported.";
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);
194 auto& pool = sws_contvalid_exts_[key];
196 if(!pool.try_pop(sws_context))
199 sws_context.reset(sws_getContext(width, height, pix_fmt, width, height, target_pix_fmt, SWS_BILINEAR, nullptr, nullptr, ¶m), sws_freeContext);
204 CASPAR_THROW_EXCEPTION(operation_failed() << msg_info("Could not create software scaling context.") <<
205 boost::errinfo_api_function("sws_getContext"));
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)
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());
217 av_frame->width = width;
218 av_frame->height = height;
219 for(int n = 0; n < target_desc.planes.size(); ++n)
221 av_frame->data[n] = write.image_data(n).begin();
222 av_frame->linesize[n] = target_desc.planes[n].linesize;
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);
229 return std::move(write);
233 auto write = frame_factory.create_frame(tag, desc, channel_layout);
235 for(int n = 0; n < static_cast<int>(desc.planes.size()); ++n)
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];
242 CASPAR_ASSERT(decoded);
243 CASPAR_ASSERT(write.image_data(n).begin());
245 if (decoded_linesize != plane.linesize)
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)
251 for (int y = r.begin(); y != r.end(); ++y)
252 A_memcpy(result + y*plane.linesize, decoded + y*decoded_linesize, plane.linesize);
257 fast_memcpy(result, decoded, plane.size);
261 return std::move(write);
265 spl::shared_ptr<AVFrame> make_av_frame(core::mutable_frame& frame)
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();
271 return make_av_frame(data, frame.pixel_format_desc());
274 spl::shared_ptr<AVFrame> make_av_frame(std::array<uint8_t*, 4> data, const core::pixel_format_desc& pix_desc)
276 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
277 avcodec_get_frame_defaults(av_frame.get());
279 auto planes = pix_desc.planes;
280 auto format = pix_desc.format;
282 av_frame->width = planes[0].width;
283 av_frame->height = planes[0].height;
284 for(int n = 0; n < planes.size(); ++n)
286 av_frame->data[n] = data[n];
287 av_frame->linesize[n] = planes[n].linesize;
292 case core::pixel_format::rgb:
293 av_frame->format = PIX_FMT_RGB24;
295 case core::pixel_format::bgr:
296 av_frame->format = PIX_FMT_BGR24;
298 case core::pixel_format::rgba:
299 av_frame->format = PIX_FMT_RGBA;
301 case core::pixel_format::argb:
302 av_frame->format = PIX_FMT_ARGB;
304 case core::pixel_format::bgra:
305 av_frame->format = PIX_FMT_BGRA;
307 case core::pixel_format::abgr:
308 av_frame->format = PIX_FMT_ABGR;
310 case core::pixel_format::gray:
311 av_frame->format = PIX_FMT_GRAY8;
313 case core::pixel_format::ycbcr:
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;
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;
333 case core::pixel_format::ycbcra:
334 av_frame->format = PIX_FMT_YUVA420P;
340 bool is_sane_fps(AVRational time_base)
342 double fps = static_cast<double>(time_base.den) / static_cast<double>(time_base.num);
343 return fps > 20.0 && fps < 65.0;
346 AVRational fix_time_base(AVRational time_base)
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));
351 if(!is_sane_fps(time_base))
353 auto tmp = time_base;
362 double read_fps(AVFormatContext& context, double fail_value)
364 auto framerate = read_framerate(context, boost::rational<int>(static_cast<int>(fail_value * 1000000.0), 1000000));
366 return static_cast<double>(framerate.numerator()) / static_cast<double>(framerate.denominator());
369 boost::rational<int> read_framerate(AVFormatContext& context, const boost::rational<int>& fail_value)
371 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
372 auto audio_index = av_find_best_stream(&context, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);
374 if (video_index > -1)
376 const auto video_context = context.streams[video_index]->codec;
377 const auto video_stream = context.streams[video_index];
379 auto frame_rate_time_base = video_stream->avg_frame_rate;
380 std::swap(frame_rate_time_base.num, frame_rate_time_base.den);
382 if (is_sane_fps(frame_rate_time_base))
384 return boost::rational<int>(frame_rate_time_base.den, frame_rate_time_base.num);
387 AVRational time_base = video_context->time_base;
389 if (boost::filesystem::path(context.filename).extension().string() == ".flv")
393 auto meta = read_flv_meta_info(context.filename);
394 return boost::rational<int>(static_cast<int>(boost::lexical_cast<double>(meta["framerate"]) * 1000000.0), 1000000);
403 time_base.num *= video_context->ticks_per_frame;
405 if (!is_sane_fps(time_base))
407 time_base = fix_time_base(time_base);
409 if (!is_sane_fps(time_base) && audio_index > -1)
411 auto& audio_context = *context.streams[audio_index]->codec;
412 auto& audio_stream = *context.streams[audio_index];
414 double duration_sec = audio_stream.duration / static_cast<double>(audio_context.sample_rate);
416 time_base.num = static_cast<int>(duration_sec*100000.0);
417 time_base.den = static_cast<int>(video_stream->nb_frames * 100000);
422 boost::rational<int> fps(time_base.den, time_base.num);
423 boost::rational<int> closest_fps(0);
425 for (auto video_mode : enum_constants<core::video_format>())
427 auto format = core::video_format_desc(core::video_format(video_mode));
429 auto diff1 = boost::abs(boost::rational<int>(format.time_scale, format.duration) - fps);
430 auto diff2 = boost::abs(closest_fps - fps);
433 closest_fps = boost::rational<int>(format.time_scale, format.duration);
442 void fix_meta_data(AVFormatContext& context)
444 auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
448 auto video_stream = context.streams[video_index];
449 auto video_context = context.streams[video_index]->codec;
451 if(boost::filesystem::path(context.filename).extension().string() == ".flv")
455 auto meta = read_flv_meta_info(context.filename);
456 double fps = boost::lexical_cast<double>(meta["framerate"]);
457 video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);
463 auto stream_time = video_stream->time_base;
464 auto duration = video_stream->duration;
465 auto codec_time = video_context->time_base;
466 auto ticks = video_context->ticks_per_frame;
468 if(video_stream->nb_frames == 0)
469 video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);
474 spl::shared_ptr<AVPacket> create_packet()
476 spl::shared_ptr<AVPacket> packet(new AVPacket(), [](AVPacket* p)
482 av_init_packet(packet.get());
486 spl::shared_ptr<AVFrame> create_frame()
488 spl::shared_ptr<AVFrame> frame(avcodec_alloc_frame(), av_free);
489 avcodec_get_frame_defaults(frame.get());
493 spl::shared_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index, bool single_threaded)
496 index = THROW_ON_ERROR2(av_find_best_stream(&context, type, -1, -1, &decoder, 0), "");
497 //if(strcmp(decoder->name, "prores") == 0 && decoder->next && strcmp(decoder->next->name, "prores_lgpl") == 0)
498 // decoder = decoder->next;
500 THROW_ON_ERROR2(tbb_avcodec_open(context.streams[index]->codec, decoder, single_threaded), "");
501 return spl::shared_ptr<AVCodecContext>(context.streams[index]->codec, tbb_avcodec_close);
504 spl::shared_ptr<AVFormatContext> open_input(const std::wstring& filename)
506 AVFormatContext* weak_context = nullptr;
507 THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr), filename);
508 spl::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* p)
510 avformat_close_input(&p);
512 THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), filename);
513 fix_meta_data(*context);
517 std::wstring print_mode(int width, int height, double fps, bool interlaced)
519 std::wostringstream fps_ss;
520 fps_ss << std::fixed << std::setprecision(2) << (!interlaced ? fps : 2.0 * fps);
522 return boost::lexical_cast<std::wstring>(width) + L"x" + boost::lexical_cast<std::wstring>(height) + (!interlaced ? L"p" : L"i") + fps_ss.str();
525 bool is_valid_file(const std::wstring& filename, bool only_video)
527 static const auto invalid_exts = {
543 static const auto only_audio = {
548 static const auto valid_exts = {
560 auto ext = boost::to_lower_copy(boost::filesystem::path(filename).extension().wstring());
562 if(std::find(valid_exts.begin(), valid_exts.end(), ext) != valid_exts.end())
565 if (!only_video && std::find(only_audio.begin(), only_audio.end(), ext) != only_audio.end())
568 if(std::find(invalid_exts.begin(), invalid_exts.end(), ext) != invalid_exts.end())
571 if (only_video && std::find(only_audio.begin(), only_audio.end(), ext) != only_audio.end())
574 auto u8filename = u8(filename);
578 pb.filename = u8filename.c_str();
580 if(av_probe_input_format2(&pb, false, &score) != nullptr)
583 std::ifstream file(u8filename);
585 std::vector<unsigned char> buf;
586 for(auto file_it = std::istreambuf_iterator<char>(file); file_it != std::istreambuf_iterator<char>() && buf.size() < 1024; ++file_it)
587 buf.push_back(*file_it);
593 pb.buf_size = static_cast<int>(buf.size());
595 return av_probe_input_format2(&pb, true, &score) != nullptr;
598 bool try_get_duration(const std::wstring filename, std::int64_t& duration, boost::rational<std::int64_t>& time_base)
600 AVFormatContext* weak_context = nullptr;
601 if (avformat_open_input(&weak_context, u8(filename).c_str(), nullptr, nullptr) < 0)
604 std::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* p)
606 avformat_close_input(&p);
609 context->probesize = context->probesize / 10;
610 context->max_analyze_duration = context->probesize / 10;
612 if (avformat_find_stream_info(context.get(), nullptr) < 0)
615 const auto fps = read_fps(*context, 1.0);
617 const auto rational_fps = boost::rational<std::int64_t>(static_cast<int>(fps * AV_TIME_BASE), AV_TIME_BASE);
619 duration = boost::rational_cast<std::int64_t>(context->duration * rational_fps / AV_TIME_BASE);
621 if (rational_fps == 0)
624 time_base = 1 / rational_fps;
629 std::wstring probe_stem(const std::wstring& stem, bool only_video)
631 auto stem2 = boost::filesystem::path(stem);
632 auto parent = find_case_insensitive(stem2.parent_path().wstring());
637 auto dir = boost::filesystem::path(*parent);
639 for(auto it = boost::filesystem::directory_iterator(dir); it != boost::filesystem::directory_iterator(); ++it)
641 if(boost::iequals(it->path().stem().wstring(), stem2.filename().wstring()) && is_valid_file(it->path().wstring(), only_video))
642 return it->path().wstring();
647 core::audio_channel_layout get_audio_channel_layout(const AVCodecContext& codec_context, const std::wstring& channel_layout_spec)
649 auto num_channels = codec_context.channels;
651 if (!channel_layout_spec.empty())
653 if (boost::contains(channel_layout_spec, L":")) // Custom on the fly layout specified.
655 std::vector<std::wstring> type_and_channel_order;
656 boost::split(type_and_channel_order, channel_layout_spec, boost::is_any_of(L":"), boost::algorithm::token_compress_off);
657 auto& type = type_and_channel_order.at(0);
658 auto& order = type_and_channel_order.at(1);
660 return core::audio_channel_layout(num_channels, std::move(type), order);
662 else // Preconfigured named channel layout selected.
664 auto layout = core::audio_channel_layout_repository::get_default()->get_layout(channel_layout_spec);
667 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"No channel layout with name " + channel_layout_spec + L" registered"));
669 layout->num_channels = num_channels;
675 if (!codec_context.channel_layout)
677 if (num_channels == 1)
678 return core::audio_channel_layout(num_channels, L"mono", L"FC");
679 else if (num_channels == 2)
680 return core::audio_channel_layout(num_channels, L"stereo", L"FL FR");
682 return core::audio_channel_layout(num_channels, L"", L""); // Passthru without named channels as is.
685 // What FFmpeg calls "channel layout" is only the "layout type" of a channel layout in
686 // CasparCG where the channel layout supports different orders as well.
687 // The user needs to provide additional mix-configs in casparcg.config to support more
688 // than the most common (5.1, mono and stereo) types.
690 // Based on information in https://ffmpeg.org/ffmpeg-utils.html#Channel-Layout
691 switch (codec_context.channel_layout)
693 case AV_CH_LAYOUT_MONO:
694 return core::audio_channel_layout(num_channels, L"mono", L"FC");
695 case AV_CH_LAYOUT_STEREO:
696 return core::audio_channel_layout(num_channels, L"stereo", L"FL FR");
697 case AV_CH_LAYOUT_2POINT1:
698 return core::audio_channel_layout(num_channels, L"2.1", L"FL FR LFE");
699 case AV_CH_LAYOUT_SURROUND:
700 return core::audio_channel_layout(num_channels, L"3.0", L"FL FR FC");
701 case AV_CH_LAYOUT_2_1:
702 return core::audio_channel_layout(num_channels, L"3.0(back)", L"FL FR BC");
703 case AV_CH_LAYOUT_4POINT0:
704 return core::audio_channel_layout(num_channels, L"4.0", L"FL FR FC BC");
705 case AV_CH_LAYOUT_QUAD:
706 return core::audio_channel_layout(num_channels, L"quad", L"FL FR BL BR");
707 case AV_CH_LAYOUT_2_2:
708 return core::audio_channel_layout(num_channels, L"quad(side)", L"FL FR SL SR");
709 case AV_CH_LAYOUT_3POINT1:
710 return core::audio_channel_layout(num_channels, L"3.1", L"FL FR FC LFE");
711 case AV_CH_LAYOUT_5POINT0_BACK:
712 return core::audio_channel_layout(num_channels, L"5.0", L"FL FR FC BL BR");
713 case AV_CH_LAYOUT_5POINT0:
714 return core::audio_channel_layout(num_channels, L"5.0(side)", L"FL FR FC SL SR");
715 case AV_CH_LAYOUT_4POINT1:
716 return core::audio_channel_layout(num_channels, L"4.1", L"FL FR FC LFE BC");
717 case AV_CH_LAYOUT_5POINT1_BACK:
718 return core::audio_channel_layout(num_channels, L"5.1", L"FL FR FC LFE BL BR");
719 case AV_CH_LAYOUT_5POINT1:
720 return core::audio_channel_layout(num_channels, L"5.1(side)", L"FL FR FC LFE SL SR");
721 case AV_CH_LAYOUT_6POINT0:
722 return core::audio_channel_layout(num_channels, L"6.0", L"FL FR FC BC SL SR");
723 case AV_CH_LAYOUT_6POINT0_FRONT:
724 return core::audio_channel_layout(num_channels, L"6.0(front)", L"FL FR FLC FRC SL SR");
725 case AV_CH_LAYOUT_HEXAGONAL:
726 return core::audio_channel_layout(num_channels, L"hexagonal", L"FL FR FC BL BR BC");
727 case AV_CH_LAYOUT_6POINT1:
728 return core::audio_channel_layout(num_channels, L"6.1", L"FL FR FC LFE BC SL SR");
729 case AV_CH_LAYOUT_6POINT1_BACK:
730 return core::audio_channel_layout(num_channels, L"6.1(back)", L"FL FR FC LFE BL BR BC");
731 case AV_CH_LAYOUT_6POINT1_FRONT:
732 return core::audio_channel_layout(num_channels, L"6.1(front)", L"FL FR LFE FLC FRC SL SR");
733 case AV_CH_LAYOUT_7POINT0:
734 return core::audio_channel_layout(num_channels, L"7.0", L"FL FR FC BL BR SL SR");
735 case AV_CH_LAYOUT_7POINT0_FRONT:
736 return core::audio_channel_layout(num_channels, L"7.0(front)", L"FL FR FC FLC FRC SL SR");
737 case AV_CH_LAYOUT_7POINT1:
738 return core::audio_channel_layout(num_channels, L"7.1", L"FL FR FC LFE BL BR SL SR");
739 case AV_CH_LAYOUT_7POINT1_WIDE_BACK:
740 return core::audio_channel_layout(num_channels, L"7.1(wide)", L"FL FR FC LFE BL BR FLC FRC");
741 case AV_CH_LAYOUT_7POINT1_WIDE:
742 return core::audio_channel_layout(num_channels, L"7.1(wide-side)", L"FL FR FC LFE FLC FRC SL SR");
743 case AV_CH_LAYOUT_STEREO_DOWNMIX:
744 return core::audio_channel_layout(num_channels, L"downmix", L"DL DR");
747 return core::audio_channel_layout(num_channels, L"", L"");
751 // av_get_default_channel_layout does not work for layouts not predefined in ffmpeg. This is needed to support > 8 channels.
752 std::int64_t create_channel_layout_bitmask(int num_channels)
754 if (num_channels > 63)
755 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info(L"FFmpeg cannot handle more than 63 audio channels"));
757 const auto ALL_63_CHANNELS = 0x7FFFFFFFFFFFFFFFULL;
759 auto to_shift = 63 - num_channels;
760 auto result = ALL_63_CHANNELS >> to_shift;
762 return static_cast<std::int64_t>(result);
766 //void av_dup_frame(AVFrame* frame)
768 // AVFrame* new_frame = avcodec_alloc_frame();
771 // const uint8_t *src_data[4] = {0};
772 // memcpy(const_cast<uint8_t**>(&src_data[0]), frame->data, 4);
773 // const int src_linesizes[4] = {0};
774 // memcpy(const_cast<int*>(&src_linesizes[0]), frame->linesize, 4);
776 // av_image_alloc(new_frame->data, new_frame->linesize, new_frame->width, new_frame->height, frame->format, 16);
778 // av_image_copy(new_frame->data, new_frame->linesize, src_data, src_linesizes, frame->format, new_frame->width, new_frame->height);