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 "cbs.h"
23 
24 
25 typedef struct CBSBSFType {
26     enum AVCodecID codec_id;
27 
28     // Name of a frame fragment in this codec (e.g. "access unit",
29     // "temporal unit").
30     const char *fragment_name;
31 
32     // Name of a unit for this BSF, for use in error messages (e.g.
33     // "NAL unit", "OBU").
34     const char *unit_name;
35 
36     // Update the content of a fragment with whatever metadata changes
37     // are desired.  The associated AVPacket is provided so that any side
38     // data associated with the fragment can be inspected or edited.  If
39     // pkt is NULL, then an extradata header fragment is being updated.
40     int (*update_fragment)(AVBSFContext *bsf, AVPacket *pkt,
41                            CodedBitstreamFragment *frag);
42 } CBSBSFType;
43 
44 // Common structure for all generic CBS BSF users.  An instance of this
45 // structure must be the first member of the BSF private context (to be
46 // pointed to by AVBSFContext.priv_data).
47 typedef struct CBSBSFContext {
48     const AVClass         *class;
49     const CBSBSFType      *type;
50 
51     CodedBitstreamContext *input;
52     CodedBitstreamContext *output;
53     CodedBitstreamFragment fragment;
54 } CBSBSFContext;
55 
56 /**
57  * Initialise generic CBS BSF setup.
58  *
59  * Creates the input and output CBS instances, and applies the filter to
60  * the extradata on the input codecpar if any is present.
61  *
62  * Since it calls the update_fragment() function immediately to deal with
63  * extradata, this should be called after any codec-specific setup is done
64  * (probably at the end of the AVBitStreamFilter.init function).
65  */
66 int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type);
67 
68 /**
69  * Close a generic CBS BSF instance.
70  *
71  * If no other deinitialisation is required then this function can be used
72  * directly as AVBitStreamFilter.close.
73  */
74 void ff_cbs_bsf_generic_close(AVBSFContext *bsf);
75 
76 /**
77  * Filter operation for CBS BSF.
78  *
79  * Reads the input packet into a CBS fragment, calls update_fragment() on
80  * it, then writes the result to an output packet.  If the input packet
81  * has AV_PKT_DATA_NEW_EXTRADATA side-data associated with it then it does
82  * the same thing to that new extradata to form the output side-data first.
83  *
84  * If the BSF does not do anything else then this function can be used
85  * directly as AVBitStreamFilter.filter.
86  */
87 int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt);
88 
89 
90 // Options for element manipulation.
91 enum {
92     // Pass this element through unchanged.
93     BSF_ELEMENT_PASS,
94     // Insert this element, replacing any existing instances of it.
95     // Associated values may be provided explicitly (as addtional options)
96     // or implicitly (either as side data or deduced from other parts of
97     // the stream).
98     BSF_ELEMENT_INSERT,
99     // Remove this element if it appears in the stream.
100     BSF_ELEMENT_REMOVE,
101     // Extract this element to side data, so that further manipulation
102     // can happen elsewhere.
103     BSF_ELEMENT_EXTRACT,
104 };
105 
106 #define BSF_ELEMENT_OPTIONS_PIR(name, help, field, opt_flags) \
107     { name, help, OFFSET(field), AV_OPT_TYPE_INT, \
108         { .i64 = BSF_ELEMENT_PASS }, \
109         BSF_ELEMENT_PASS, BSF_ELEMENT_REMOVE, opt_flags, name }, \
110     { "pass",   NULL, 0, AV_OPT_TYPE_CONST, \
111         { .i64 = BSF_ELEMENT_PASS   }, .flags = opt_flags, .unit = name }, \
112     { "insert", NULL, 0, AV_OPT_TYPE_CONST, \
113         { .i64 = BSF_ELEMENT_INSERT }, .flags = opt_flags, .unit = name }, \
114     { "remove", NULL, 0, AV_OPT_TYPE_CONST, \
115         { .i64 = BSF_ELEMENT_REMOVE }, .flags = opt_flags, .unit = name }
116 
117 #define BSF_ELEMENT_OPTIONS_PIRE(name, help, field, opt_flags) \
118     { name, help, OFFSET(field), AV_OPT_TYPE_INT, \
119         { .i64 = BSF_ELEMENT_PASS }, \
120         BSF_ELEMENT_PASS, BSF_ELEMENT_EXTRACT, opt_flags, name }, \
121     { "pass",   NULL, 0, AV_OPT_TYPE_CONST, \
122         { .i64 = BSF_ELEMENT_PASS    }, .flags = opt_flags, .unit = name }, \
123     { "insert", NULL, 0, AV_OPT_TYPE_CONST, \
124         { .i64 = BSF_ELEMENT_INSERT  }, .flags = opt_flags, .unit = name }, \
125     { "remove", NULL, 0, AV_OPT_TYPE_CONST, \
126         { .i64 = BSF_ELEMENT_REMOVE  }, .flags = opt_flags, .unit = name }, \
127     { "extract", NULL, 0, AV_OPT_TYPE_CONST, \
128         { .i64 = BSF_ELEMENT_EXTRACT }, .flags = opt_flags, .unit = name } \
129 
130 
131 #endif /* AVCODEC_CBS_BSF_H */
132