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 
24 {
25  CBSBSFContext *ctx = bsf->priv_data;
26  CodedBitstreamFragment *frag = &ctx->fragment;
27  uint8_t *side_data;
28  int err;
29 
31  return 0;
32 
33  err = ff_cbs_read_packet_side_data(ctx->input, frag, pkt);
34  if (err < 0) {
35  av_log(bsf, AV_LOG_ERROR,
36  "Failed to read extradata from packet side data.\n");
37  return err;
38  }
39 
40  err = ctx->type->update_fragment(bsf, NULL, frag);
41  if (err < 0)
42  return err;
43 
44  err = ff_cbs_write_fragment_data(ctx->output, frag);
45  if (err < 0) {
46  av_log(bsf, AV_LOG_ERROR,
47  "Failed to write extradata into packet side data.\n");
48  return err;
49  }
50 
52  frag->data_size);
53  if (!side_data)
54  return AVERROR(ENOMEM);
55  memcpy(side_data, frag->data, frag->data_size);
56 
58  return 0;
59 }
60 
62 {
63  CBSBSFContext *ctx = bsf->priv_data;
64  CodedBitstreamFragment *frag = &ctx->fragment;
65  int err;
66 
67  err = ff_bsf_get_packet_ref(bsf, pkt);
68  if (err < 0)
69  return err;
70 
71  err = cbs_bsf_update_side_data(bsf, pkt);
72  if (err < 0)
73  goto fail;
74 
75  err = ff_cbs_read_packet(ctx->input, frag, pkt);
76  if (err < 0) {
77  av_log(bsf, AV_LOG_ERROR, "Failed to read %s from packet.\n",
78  ctx->type->fragment_name);
79  goto fail;
80  }
81 
82  if (frag->nb_units == 0) {
83  av_log(bsf, AV_LOG_ERROR, "No %s found in packet.\n",
84  ctx->type->unit_name);
85  err = AVERROR_INVALIDDATA;
86  goto fail;
87  }
88 
89  err = ctx->type->update_fragment(bsf, pkt, frag);
90  if (err < 0)
91  goto fail;
92 
93  err = ff_cbs_write_packet(ctx->output, pkt, frag);
94  if (err < 0) {
95  av_log(bsf, AV_LOG_ERROR, "Failed to write %s into packet.\n",
96  ctx->type->fragment_name);
97  goto fail;
98  }
99 
100  err = 0;
101 fail:
102  ff_cbs_fragment_reset(frag);
103 
104  if (err < 0)
106 
107  return err;
108 }
109 
111 {
112  CBSBSFContext *ctx = bsf->priv_data;
113  CodedBitstreamFragment *frag = &ctx->fragment;
114  int err;
115 
116  ctx->type = type;
117 
118  err = ff_cbs_init(&ctx->input, type->codec_id, bsf);
119  if (err < 0)
120  return err;
121 
122  err = ff_cbs_init(&ctx->output, type->codec_id, bsf);
123  if (err < 0)
124  return err;
125 
126  if (bsf->par_in->extradata) {
127  err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
128  if (err < 0) {
129  av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
130  goto fail;
131  }
132 
133  err = type->update_fragment(bsf, NULL, frag);
134  if (err < 0)
135  goto fail;
136 
137  err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
138  if (err < 0) {
139  av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
140  goto fail;
141  }
142  }
143 
144  err = 0;
145 fail:
146  ff_cbs_fragment_reset(frag);
147  return err;
148 }
149 
151 {
152  CBSBSFContext *ctx = bsf->priv_data;
153 
154  ff_cbs_fragment_free(&ctx->fragment);
155  ff_cbs_close(&ctx->input);
156  ff_cbs_close(&ctx->output);
157 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
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:76
ff_cbs_bsf_generic_init
int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
Initialise generic CBS BSF setup.
Definition: cbs_bsf.c:110
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:23
ff_cbs_fragment_free
av_cold void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:171
ff_cbs_read_extradata
int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
Definition: cbs.c:271
ff_cbs_read_packet_side_data
int ff_cbs_read_packet_side_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Definition: cbs.c:297
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:157
CBSBSFContext
Definition: cbs_bsf.h:53
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
ff_cbs_close
av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:127
bsf.h
cbs_bsf.h
fail
#define fail()
Definition: checkasm.h:134
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
ff_cbs_write_extradata
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
Definition: cbs.c:430
AVBSFContext::par_out
AVCodecParameters * par_out
Parameters of the output stream.
Definition: bsf.h:96
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:121
ff_cbs_write_packet
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:459
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:134
ctx
AVFormatContext * ctx
Definition: movenc.c:48
NULL
#define NULL
Definition: coverity.c:32
CBSBSFType
Definition: cbs_bsf.h:31
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:127
ff_cbs_bsf_generic_close
void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
Close a generic CBS BSF instance.
Definition: cbs_bsf.c:150
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: avpacket.c:251
ff_cbs_write_fragment_data
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:394
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:61
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: avpacket.c:230
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
ff_cbs_read_packet
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:289
ff_cbs_init
av_cold int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:76
AVPacket
This structure stores compressed data.
Definition: packet.h:351
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:257
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:152