1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVCODEC_CBS_BSF_H
20 #define AVCODEC_CBS_BSF_H
21 
22 #include "libavutil/log.h"
23 #include "libavutil/opt.h"
24 
25 #include "bsf.h"
26 #include "codec_id.h"
27 #include "cbs.h"
28 #include "packet.h"
29 
30 
31 typedef struct CBSBSFType {
32     enum AVCodecID codec_id;
33 
34     // Name of a frame fragment in this codec (e.g. "access unit",
35     // "temporal unit").
36     const char *fragment_name;
37 
38     // Name of a unit for this BSF, for use in error messages (e.g.
39     // "NAL unit", "OBU").
40     const char *unit_name;
41 
42     // Update the content of a fragment with whatever metadata changes
43     // are desired.  The associated AVPacket is provided so that any side
44     // data associated with the fragment can be inspected or edited.  If
45     // pkt is NULL, then an extradata header fragment is being updated.
46     int (*update_fragment)(AVBSFContext *bsf, AVPacket *pkt,
47                            CodedBitstreamFragment *frag);
48 } CBSBSFType;
49 
50 // Common structure for all generic CBS BSF users.  An instance of this
51 // structure must be the first member of the BSF private context (to be
52 // pointed to by AVBSFContext.priv_data).
53 typedef struct CBSBSFContext {
54     const AVClass         *class;
55     const CBSBSFType      *type;
56 
57     CodedBitstreamContext *input;
58     CodedBitstreamContext *output;
59     CodedBitstreamFragment fragment;
60 } CBSBSFContext;
61 
62 /**
63  * Initialise generic CBS BSF setup.
64  *
65  * Creates the input and output CBS instances, and applies the filter to
66  * the extradata on the input codecpar if any is present.
67  *
68  * Since it calls the update_fragment() function immediately to deal with
69  * extradata, this should be called after any codec-specific setup is done
70  * (probably at the end of the FFBitStreamFilter.init function).
71  */
72 int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type);
73 
74 /**
75  * Close a generic CBS BSF instance.
76  *
77  * If no other deinitialisation is required then this function can be used
78  * directly as FFBitStreamFilter.close.
79  */
80 void ff_cbs_bsf_generic_close(AVBSFContext *bsf);
81 
82 /**
83  * Filter operation for CBS BSF.
84  *
85  * Reads the input packet into a CBS fragment, calls update_fragment() on
86  * it, then writes the result to an output packet.  If the input packet
87  * has AV_PKT_DATA_NEW_EXTRADATA side-data associated with it then it does
88  * the same thing to that new extradata to form the output side-data first.
89  *
90  * If the BSF does not do anything else then this function can be used
91  * directly as FFBitStreamFilter.filter.
92  */
93 int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt);
94 
95 
96 // Options for element manipulation.
97 enum {
98     // Pass this element through unchanged.
99     BSF_ELEMENT_PASS,
100     // Insert this element, replacing any existing instances of it.
101     // Associated values may be provided explicitly (as addtional options)
102     // or implicitly (either as side data or deduced from other parts of
103     // the stream).
104     BSF_ELEMENT_INSERT,
105     // Remove this element if it appears in the stream.
106     BSF_ELEMENT_REMOVE,
107     // Extract this element to side data, so that further manipulation
108     // can happen elsewhere.
109     BSF_ELEMENT_EXTRACT,
110 };
111 
112 #define BSF_ELEMENT_OPTIONS_PIR(name, help, field, opt_flags) \
113     { name, help, OFFSET(field), AV_OPT_TYPE_INT, \
114         { .i64 = BSF_ELEMENT_PASS }, \
115         BSF_ELEMENT_PASS, BSF_ELEMENT_REMOVE, opt_flags, name }, \
116     { "pass",   NULL, 0, AV_OPT_TYPE_CONST, \
117         { .i64 = BSF_ELEMENT_PASS   }, .flags = opt_flags, .unit = name }, \
118     { "insert", NULL, 0, AV_OPT_TYPE_CONST, \
119         { .i64 = BSF_ELEMENT_INSERT }, .flags = opt_flags, .unit = name }, \
120     { "remove", NULL, 0, AV_OPT_TYPE_CONST, \
121         { .i64 = BSF_ELEMENT_REMOVE }, .flags = opt_flags, .unit = name }
122 
123 #define BSF_ELEMENT_OPTIONS_PIRE(name, help, field, opt_flags) \
124     { name, help, OFFSET(field), AV_OPT_TYPE_INT, \
125         { .i64 = BSF_ELEMENT_PASS }, \
126         BSF_ELEMENT_PASS, BSF_ELEMENT_EXTRACT, opt_flags, name }, \
127     { "pass",   NULL, 0, AV_OPT_TYPE_CONST, \
128         { .i64 = BSF_ELEMENT_PASS    }, .flags = opt_flags, .unit = name }, \
129     { "insert", NULL, 0, AV_OPT_TYPE_CONST, \
130         { .i64 = BSF_ELEMENT_INSERT  }, .flags = opt_flags, .unit = name }, \
131     { "remove", NULL, 0, AV_OPT_TYPE_CONST, \
132         { .i64 = BSF_ELEMENT_REMOVE  }, .flags = opt_flags, .unit = name }, \
133     { "extract", NULL, 0, AV_OPT_TYPE_CONST, \
134         { .i64 = BSF_ELEMENT_EXTRACT }, .flags = opt_flags, .unit = name } \
135 
136 
137 #endif /* AVCODEC_CBS_BSF_H */
138