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