xref: /aosp_15_r20/external/openscreen/cast/standalone_receiver/avcodec_glue.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 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_RECEIVER_AVCODEC_GLUE_H_
6 #define CAST_STANDALONE_RECEIVER_AVCODEC_GLUE_H_
7 
8 #include <memory>
9 
10 extern "C" {
11 #include <libavcodec/avcodec.h>
12 #include <libavutil/common.h>
13 #include <libavutil/imgutils.h>
14 #include <libavutil/samplefmt.h>
15 }
16 
17 namespace openscreen {
18 namespace cast {
19 
20 // Macro that, for an AVFoo, generates code for:
21 //
22 //  using FooUniquePtr = std::unique_ptr<Foo, FooFreer>;
23 //  FooUniquePtr MakeUniqueFoo(...args...);
24 #define DEFINE_AV_UNIQUE_PTR(name, create_func, free_statement)         \
25   namespace internal {                                                  \
26   struct name##Freer {                                                  \
27     void operator()(name* obj) const {                                  \
28       if (obj) {                                                        \
29         free_statement;                                                 \
30       }                                                                 \
31     }                                                                   \
32   };                                                                    \
33   }                                                                     \
34                                                                         \
35   using name##UniquePtr = std::unique_ptr<name, internal::name##Freer>; \
36                                                                         \
37   template <typename... Args>                                           \
38   name##UniquePtr MakeUnique##name(Args&&... args) {                    \
39     return name##UniquePtr(create_func(std::forward<Args>(args)...));   \
40   }
41 
42 DEFINE_AV_UNIQUE_PTR(AVCodecParserContext,
43                      av_parser_init,
44                      av_parser_close(obj));
45 DEFINE_AV_UNIQUE_PTR(AVCodecContext,
46                      avcodec_alloc_context3,
47                      avcodec_free_context(&obj));
48 DEFINE_AV_UNIQUE_PTR(AVPacket, av_packet_alloc, av_packet_free(&obj));
49 DEFINE_AV_UNIQUE_PTR(AVFrame, av_frame_alloc, av_frame_free(&obj));
50 
51 #undef DEFINE_AV_UNIQUE_PTR
52 
53 }  // namespace cast
54 }  // namespace openscreen
55 
56 #endif  // CAST_STANDALONE_RECEIVER_AVCODEC_GLUE_H_
57