]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/util/util.h
1d2c647db788a2cc5890214fbf025a6158ea55fa
[casparcg] / modules / ffmpeg / producer / util / util.h
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 #pragma once
23
24 #include <common/forward.h>
25 #include <common/memory.h>
26
27 #include <core/video_format.h>
28 #include <core/frame/pixel_format.h>
29 #include <core/frame/audio_channel_layout.h>
30 #include <core/frame/frame.h>
31 #include <core/fwd.h>
32
33 #include <boost/rational.hpp>
34
35 #include <array>
36 #include <vector>
37 #include <utility>
38
39 #if defined(_MSC_VER)
40 #pragma warning (push)
41 #pragma warning (disable : 4244)
42 #endif
43 extern "C"
44 {
45 #include <libavutil/avutil.h>
46 }
47 #if defined(_MSC_VER)
48 #pragma warning (pop)
49 #endif
50
51 struct AVFrame;
52 struct AVFormatContext;
53 struct AVPacket;
54 struct AVRational;
55 struct AVCodecContext;
56
57 namespace caspar { namespace ffmpeg {
58
59 typedef std::vector<std::pair<std::string, std::string>> ffmpeg_options;
60
61 // Utils
62
63 core::field_mode                                        get_mode(const AVFrame& frame);
64 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);
65 spl::shared_ptr<AVFrame>                        make_av_frame(core::mutable_frame& frame);
66 spl::shared_ptr<AVFrame>                        make_av_frame(std::array<uint8_t*, 4> data, const core::pixel_format_desc& pix_desc);
67
68 core::pixel_format_desc                         pixel_format_desc(PixelFormat pix_fmt, int width, int height);
69
70 spl::shared_ptr<AVPacket> create_packet();
71 spl::shared_ptr<AVFrame>  create_frame();
72
73 spl::shared_ptr<AVCodecContext> open_codec(AVFormatContext& context, AVMediaType type, int& index, bool single_threaded);
74 spl::shared_ptr<AVFormatContext> open_input(const std::wstring& filename);
75
76 bool is_sane_fps(AVRational time_base);
77 AVRational fix_time_base(AVRational time_base);
78
79 std::shared_ptr<core::mutable_audio_buffer>     flush_audio();
80 std::shared_ptr<core::mutable_audio_buffer>     empty_audio();
81 std::shared_ptr<AVFrame>                                        flush_video();
82 std::shared_ptr<AVFrame>                                        empty_video();
83
84 double read_fps(AVFormatContext& context, double fail_value);
85 boost::rational<int> read_framerate(AVFormatContext& context, const boost::rational<int>& fail_value);
86
87 std::wstring print_mode(int width, int height, double fps, bool interlaced);
88
89 std::wstring probe_stem(const std::wstring& stem, bool only_video);
90 bool is_valid_file(const std::wstring& filename, bool only_video);
91 bool try_get_duration(const std::wstring filename, std::int64_t& duration, boost::rational<std::int64_t>& time_base);
92
93 core::audio_channel_layout get_audio_channel_layout(int num_channels, std::uint64_t layout, const std::wstring& channel_layout_spec);
94
95 // av_get_default_channel_layout does not work for layouts not predefined in ffmpeg. This is needed to support > 8 channels.
96 std::int64_t create_channel_layout_bitmask(int num_channels);
97
98 std::vector<int> find_audio_cadence(const boost::rational<int>& framerate);
99
100 }}