1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CAST_STANDALONE_SENDER_FFMPEG_GLUE_H_
6 #define CAST_STANDALONE_SENDER_FFMPEG_GLUE_H_
7 
8 extern "C" {
9 #include <libavcodec/avcodec.h>
10 #include <libavformat/avformat.h>
11 #include <libavutil/channel_layout.h>
12 #include <libavutil/common.h>
13 #include <libavutil/imgutils.h>
14 #include <libavutil/mathematics.h>
15 #include <libavutil/pixfmt.h>
16 #include <libavutil/samplefmt.h>
17 #include <libswresample/swresample.h>
18 }
19 
20 #include <memory>
21 #include <utility>
22 
23 namespace openscreen {
24 namespace cast {
25 
26 namespace internal {
27 
28 // Convenience allocator for a new AVFormatContext, given a file |path|. Returns
29 // nullptr on error. Note: MakeUniqueAVFormatContext() is the public API.
30 AVFormatContext* CreateAVFormatContextForFile(const char* path);
31 
32 }  // namespace internal
33 
34 // Macro that, for an AVFoo, generates code for:
35 //
36 //  using FooUniquePtr = std::unique_ptr<Foo, FooFreer>;
37 //  FooUniquePtr MakeUniqueFoo(...args...);
38 #define DEFINE_AV_UNIQUE_PTR(name, create_func, free_func)              \
39   namespace internal {                                                  \
40   struct name##Freer {                                                  \
41     void operator()(name* obj) const {                                  \
42       if (obj) {                                                        \
43         free_func(&obj);                                                \
44       }                                                                 \
45     }                                                                   \
46   };                                                                    \
47   }                                                                     \
48                                                                         \
49   using name##UniquePtr = std::unique_ptr<name, internal::name##Freer>; \
50                                                                         \
51   template <typename... Args>                                           \
52   name##UniquePtr MakeUnique##name(Args&&... args) {                    \
53     return name##UniquePtr(create_func(std::forward<Args>(args)...));   \
54   }
55 
56 DEFINE_AV_UNIQUE_PTR(AVFormatContext,
57                      ::openscreen::cast::internal::CreateAVFormatContextForFile,
58                      avformat_close_input);
59 DEFINE_AV_UNIQUE_PTR(AVCodecContext,
60                      avcodec_alloc_context3,
61                      avcodec_free_context);
62 DEFINE_AV_UNIQUE_PTR(AVPacket, av_packet_alloc, av_packet_free);
63 DEFINE_AV_UNIQUE_PTR(AVFrame, av_frame_alloc, av_frame_free);
64 DEFINE_AV_UNIQUE_PTR(SwrContext, swr_alloc, swr_free);
65 
66 #undef DEFINE_AV_UNIQUE_PTR
67 
68 }  // namespace cast
69 }  // namespace openscreen
70 
71 #endif  // CAST_STANDALONE_SENDER_FFMPEG_GLUE_H_
72