FFmpeg
cbs_bsf.c
Go to the documentation of this file.
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 #include "bsf.h"
20 #include "bsf_internal.h"
21 #include "cbs_bsf.h"
22 
23 #include "libavutil/attributes.h"
24 
26 #if CONFIG_CBS_APV
28 #endif
29 #if CONFIG_CBS_AV1
31 #endif
32 #if CONFIG_CBS_H264
34 #endif
35 #if CONFIG_CBS_H265
37 #endif
38 #if CONFIG_CBS_H266
40 #endif
41 #if CONFIG_CBS_LCEVC
43 #endif
44 #if CONFIG_CBS_JPEG
46 #endif
47 #if CONFIG_CBS_MPEG2
49 #endif
50 #if CONFIG_CBS_VP8
52 #endif
53 #if CONFIG_CBS_VP9
55 #endif
57 };
58 
60 {
61  CBSBSFContext *ctx = bsf->priv_data;
62  CodedBitstreamFragment *frag = &ctx->fragment;
63  uint8_t *side_data;
64  int err;
65 
67  return 0;
68 
69  err = ff_cbs_read_packet_side_data(ctx->input, frag, pkt);
70  if (err < 0) {
71  av_log(bsf, AV_LOG_ERROR,
72  "Failed to read extradata from packet side data.\n");
73  return err;
74  }
75 
76  err = ctx->type->update_fragment(bsf, NULL, frag);
77  if (err < 0)
78  return err;
79 
80  err = ff_cbs_write_fragment_data(ctx->output, frag);
81  if (err < 0) {
82  av_log(bsf, AV_LOG_ERROR,
83  "Failed to write extradata into packet side data.\n");
84  return err;
85  }
86 
88  frag->data_size);
89  if (!side_data)
90  return AVERROR(ENOMEM);
91  memcpy(side_data, frag->data, frag->data_size);
92 
93  ff_cbs_fragment_reset(frag);
94  return 0;
95 }
96 
98 {
99  CBSBSFContext *ctx = bsf->priv_data;
100  CodedBitstreamFragment *frag = &ctx->fragment;
101  int err;
102 
103  err = ff_bsf_get_packet_ref(bsf, pkt);
104  if (err < 0)
105  return err;
106 
107  err = cbs_bsf_update_side_data(bsf, pkt);
108  if (err < 0)
109  goto fail;
110 
111  err = ff_cbs_read_packet(ctx->input, frag, pkt);
112  if (err < 0) {
113  av_log(bsf, AV_LOG_ERROR, "Failed to read %s from packet.\n",
114  ctx->type->fragment_name);
115  goto fail;
116  }
117 
118  if (frag->nb_units == 0) {
119  av_log(bsf, AV_LOG_ERROR, "No %s found in packet.\n",
120  ctx->type->unit_name);
121  err = AVERROR_INVALIDDATA;
122  goto fail;
123  }
124 
125  err = ctx->type->update_fragment(bsf, pkt, frag);
126  if (err < 0)
127  goto fail;
128 
129  err = ff_cbs_write_packet(ctx->output, pkt, frag);
130  if (err < 0) {
131  av_log(bsf, AV_LOG_ERROR, "Failed to write %s into packet.\n",
132  ctx->type->fragment_name);
133  goto fail;
134  }
135 
136  err = 0;
137 fail:
138  ff_cbs_fragment_reset(frag);
139 
140  if (err < 0)
142 
143  return err;
144 }
145 
147 {
148  CBSBSFContext *ctx = bsf->priv_data;
149  CodedBitstreamFragment *frag = &ctx->fragment;
150  int err;
151 
152  ctx->type = type;
153 
154  err = ff_cbs_init(&ctx->input, type->codec_id, bsf);
155  if (err < 0)
156  return err;
157 
158  err = ff_cbs_init(&ctx->output, type->codec_id, bsf);
159  if (err < 0)
160  return err;
161 
162  ctx->output->trace_enable = 1;
163  ctx->output->trace_level = AV_LOG_TRACE;
164  ctx->output->trace_context = ctx->output;
165  ctx->output->trace_write_callback = ff_cbs_trace_write_log;
166 
167  if (bsf->par_in->extradata) {
168  err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
169  if (err < 0) {
170  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
171  goto fail;
172  }
173 
174  err = type->update_fragment(bsf, NULL, frag);
175  if (err < 0)
176  goto fail;
177 
178  err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
179  if (err < 0) {
180  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
181  goto fail;
182  }
183  }
184 
185  err = 0;
186 fail:
187  ff_cbs_fragment_reset(frag);
188  return err;
189 }
190 
192 {
193  CBSBSFContext *ctx = bsf->priv_data;
194 
195  ff_cbs_fragment_free(&ctx->fragment);
196  ff_cbs_close(&ctx->input);
197  ff_cbs_close(&ctx->output);
198 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:433
AVBSFContext::par_in
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:90
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
bsf_internal.h
cbs_bsf_update_side_data
static int cbs_bsf_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
Definition: cbs_bsf.c:59
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:56
CBSBSFContext
Definition: cbs_bsf.h:53
ff_cbs_bsf_generic_close
av_cold void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
Close a generic CBS BSF instance.
Definition: cbs_bsf.c:191
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
bsf.h
cbs_bsf.h
fail
#define fail()
Definition: checkasm.h:224
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AVBSFContext::par_out
AVCodecParameters * par_out
Parameters of the output stream.
Definition: bsf.h:96
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:236
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:119
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:129
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:142
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:222
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:284
AV_CODEC_ID_H266
#define AV_CODEC_ID_H266
Definition: codec_id.h:253
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
CBSBSFType
Definition: cbs_bsf.h:31
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:135
attributes.h
AV_CODEC_ID_LCEVC
@ AV_CODEC_ID_LCEVC
Definition: codec_id.h:615
ff_cbs_bsf_generic_init
av_cold int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
Initialise generic CBS BSF setup.
Definition: cbs_bsf.c:146
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: packet.c:252
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:83
ff_cbs_bsf_generic_filter
int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
Filter operation for CBS BSF.
Definition: cbs_bsf.c:97
AV_CODEC_ID_APV
@ AV_CODEC_ID_APV
Definition: codec_id.h:332
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: packet.c:231
AV_CODEC_ID_H265
#define AV_CODEC_ID_H265
Definition: codec_id.h:229
ff_cbs_all_codec_ids
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
Definition: cbs_bsf.c:25
AVPacket
This structure stores compressed data.
Definition: packet.h:572
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:256
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:160