FFmpeg
bitstream_filter.c
Go to the documentation of this file.
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <string.h>
22 
23 #include "avcodec.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 
28 #if FF_API_OLD_BSF
30 
32 {
33  const AVBitStreamFilter *filter = NULL;
34  void *opaque = NULL;
35 
36  while (filter != f)
37  filter = av_bsf_iterate(&opaque);
38 
39  return av_bsf_iterate(&opaque);
40 }
41 
43 {
44 }
45 
46 typedef struct BSFCompatContext {
50 
52 {
54  BSFCompatContext *priv = NULL;
55  const AVBitStreamFilter *bsf;
56 
57  bsf = av_bsf_get_by_name(name);
58  if (!bsf)
59  return NULL;
60 
61  ctx = av_mallocz(sizeof(*ctx));
62  if (!ctx)
63  return NULL;
64 
65  priv = av_mallocz(sizeof(*priv));
66  if (!priv)
67  goto fail;
68 
69 
70  ctx->filter = bsf;
71  ctx->priv_data = priv;
72 
73  return ctx;
74 
75 fail:
76  if (priv)
77  av_bsf_free(&priv->ctx);
78  av_freep(&priv);
79  av_freep(&ctx);
80  return NULL;
81 }
82 
84 {
85  BSFCompatContext *priv;
86 
87  if (!bsfc)
88  return;
89 
90  priv = bsfc->priv_data;
91 
92  av_bsf_free(&priv->ctx);
93  av_freep(&bsfc->priv_data);
94  av_free(bsfc);
95 }
96 
98  AVCodecContext *avctx, const char *args,
99  uint8_t **poutbuf, int *poutbuf_size,
100  const uint8_t *buf, int buf_size, int keyframe)
101 {
102  BSFCompatContext *priv = bsfc->priv_data;
103  AVPacket pkt = { 0 };
104  int ret;
105 
106  if (!priv->ctx) {
107  ret = av_bsf_alloc(bsfc->filter, &priv->ctx);
108  if (ret < 0)
109  return ret;
110 
112  if (ret < 0)
113  return ret;
114 
115  priv->ctx->time_base_in = avctx->time_base;
116 
117  if (bsfc->args && bsfc->filter->priv_class) {
118  const AVOption *opt = av_opt_next(priv->ctx->priv_data, NULL);
119  const char * shorthand[2] = {NULL};
120 
121  if (opt)
122  shorthand[0] = opt->name;
123 
124  ret = av_opt_set_from_string(priv->ctx->priv_data, bsfc->args, shorthand, "=", ":");
125  if (ret < 0)
126  return ret;
127  }
128 
129  ret = av_bsf_init(priv->ctx);
130  if (ret < 0)
131  return ret;
132  }
133 
134  pkt.data = (uint8_t *)buf;
135  pkt.size = buf_size;
136 
137  ret = av_bsf_send_packet(priv->ctx, &pkt);
138  if (ret < 0)
139  return ret;
140 
141  *poutbuf = NULL;
142  *poutbuf_size = 0;
143 
144  ret = av_bsf_receive_packet(priv->ctx, &pkt);
145  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
146  return 0;
147  else if (ret < 0)
148  return ret;
149 
151  if (!*poutbuf) {
153  return AVERROR(ENOMEM);
154  }
155 
156  *poutbuf_size = pkt.size;
157  memcpy(*poutbuf, pkt.data, pkt.size);
158 
160 
161  /* drain all the remaining packets we cannot return */
162  while (ret >= 0) {
163  ret = av_bsf_receive_packet(priv->ctx, &pkt);
165  }
166 
167  if (!priv->extradata_updated) {
168  /* update extradata in avctx from the output codec parameters */
169  if (priv->ctx->par_out->extradata_size && (!args || !strstr(args, "private_spspps_buf"))) {
170  av_freep(&avctx->extradata);
171  avctx->extradata_size = 0;
173  if (!avctx->extradata)
174  return AVERROR(ENOMEM);
175  memcpy(avctx->extradata, priv->ctx->par_out->extradata, priv->ctx->par_out->extradata_size);
176  avctx->extradata_size = priv->ctx->par_out->extradata_size;
177  }
178 
179  priv->extradata_updated = 1;
180  }
181 
182  return 1;
183 }
185 #endif
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
AVBSFContext::par_in
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:77
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
name
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 default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
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
opt.h
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
avcodec_parameters_from_context
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:2127
av_bitstream_filter_filter
int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Definition: bitstream_filter.c:97
av_bsf_init
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:144
AVPacket::data
uint8_t * data
Definition: packet.h:355
AVOption
AVOption.
Definition: opt.h:246
AVBitStreamFilterContext::filter
const struct AVBitStreamFilter * filter
Definition: avcodec.h:3977
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
av_bsf_iterate
const AVBitStreamFilter * av_bsf_iterate(void **opaque)
Iterate over all registered bitstream filters.
Definition: bitstream_filters.c:66
AVBSFContext
The bitstream filter state.
Definition: bsf.h:49
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
av_bsf_get_by_name
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
Definition: bitstream_filters.c:83
fail
#define fail()
Definition: checkasm.h:123
AVBSFContext::par_out
AVCodecParameters * par_out
Parameters of the output stream.
Definition: bsf.h:83
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:628
BSFCompatContext::ctx
AVBSFContext * ctx
Definition: bitstream_filter.c:47
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVBSFContext::time_base_in
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition: bsf.h:89
f
#define f(width, name)
Definition: cbs_vp9.c:255
av_bitstream_filter_next
const FF_DISABLE_DEPRECATION_WARNINGS AVBitStreamFilter * av_bitstream_filter_next(const AVBitStreamFilter *f)
Definition: bitstream_filter.c:31
NULL
#define NULL
Definition: coverity.c:32
av_opt_set_from_string
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
Definition: opt.c:1558
AVBitStreamFilter::priv_class
const AVClass * priv_class
A class for the private data, used to declare bitstream filter private AVOptions.
Definition: bsf.h:117
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
av_register_bitstream_filter
void av_register_bitstream_filter(AVBitStreamFilter *bsf)
Definition: bitstream_filter.c:42
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:649
AVPacket::size
int size
Definition: packet.h:356
AVOption::name
const char * name
Definition: opt.h:247
av_bsf_receive_packet
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:223
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
internal.h
BSFCompatContext::extradata_updated
int extradata_updated
Definition: bitstream_filter.c:48
BSFCompatContext
Definition: bitstream_filter.c:46
uint8_t
uint8_t
Definition: audio_convert.c:194
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
av_opt_next
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:45
av_bitstream_filter_close
void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc)
Definition: bitstream_filter.c:83
avcodec.h
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:70
ret
ret
Definition: filter_design.txt:187
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:215
av_bsf_send_packet
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:197
AVCodecContext
main external API structure.
Definition: avcodec.h:526
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVBitStreamFilter
Definition: bsf.h:98
AVBitStreamFilterContext::priv_data
void * priv_data
Definition: avcodec.h:3976
AVBitStreamFilterContext::args
char * args
Internal default arguments, used if NULL is passed to av_bitstream_filter_filter().
Definition: avcodec.h:3984
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_bsf_free
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:40
AVBitStreamFilterContext
Definition: avcodec.h:3975
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1363
av_bsf_alloc
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:91
av_bitstream_filter_init
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Definition: bitstream_filter.c:51