FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_next(&opaque);
38 
39  return av_bsf_next(&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 
111  ret = avcodec_parameters_from_context(priv->ctx->par_in, avctx);
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  }
126 
127  ret = av_bsf_init(priv->ctx);
128  if (ret < 0)
129  return ret;
130  }
131 
132  pkt.data = buf;
133  pkt.size = buf_size;
134 
135  ret = av_bsf_send_packet(priv->ctx, &pkt);
136  if (ret < 0)
137  return ret;
138 
139  *poutbuf = NULL;
140  *poutbuf_size = 0;
141 
142  ret = av_bsf_receive_packet(priv->ctx, &pkt);
143  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
144  return 0;
145  else if (ret < 0)
146  return ret;
147 
148  *poutbuf = av_malloc(pkt.size + AV_INPUT_BUFFER_PADDING_SIZE);
149  if (!*poutbuf) {
150  av_packet_unref(&pkt);
151  return AVERROR(ENOMEM);
152  }
153 
154  *poutbuf_size = pkt.size;
155  memcpy(*poutbuf, pkt.data, pkt.size);
156 
157  av_packet_unref(&pkt);
158 
159  /* drain all the remaining packets we cannot return */
160  while (ret >= 0) {
161  ret = av_bsf_receive_packet(priv->ctx, &pkt);
162  av_packet_unref(&pkt);
163  }
164 
165  if (!priv->extradata_updated) {
166  /* update extradata in avctx from the output codec parameters */
167  if (priv->ctx->par_out->extradata_size && (!args || !strstr(args, "private_spspps_buf"))) {
168  av_freep(&avctx->extradata);
169  avctx->extradata_size = 0;
171  if (!avctx->extradata)
172  return AVERROR(ENOMEM);
173  memcpy(avctx->extradata, priv->ctx->par_out->extradata, priv->ctx->par_out->extradata_size);
174  avctx->extradata_size = priv->ctx->par_out->extradata_size;
175  }
176 
177  priv->extradata_updated = 1;
178  }
179 
180  return 1;
181 }
183 #endif
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:36
#define NULL
Definition: coverity.c:32
const AVClass * priv_class
A class for the private data, used to declare bitstream filter private AVOptions. ...
Definition: avcodec.h:5898
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5864
AVOption.
Definition: opt.h:246
Memory handling functions.
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)
Filter bitstream.
The bitstream filter state.
Definition: avcodec.h:5830
int size
Definition: avcodec.h:1658
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static AVPacket pkt
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5851
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:135
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1869
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:82
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:1492
const char * name
Definition: opt.h:247
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:199
uint8_t
#define av_malloc(s)
AVOptions.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1847
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:80
uint8_t * data
Definition: avcodec.h:1657
#define AVERROR_EOF
End of file.
Definition: error.h:55
const AVBitStreamFilter * av_bsf_next(void **opaque)
Iterate over all registered bitstream filters.
#define AVERROR(e)
Definition: error.h:43
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition: avcodec.h:5870
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:45
#define fail()
Definition: checkasm.h:89
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4084
common internal API header
void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc)
Release bitstream filter context.
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:176
AVFormatContext * ctx
Definition: movenc.c:48
AVBSFContext * ctx
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1732
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:589
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1848
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:4207
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
FF_DISABLE_DEPRECATION_WARNINGS AVBitStreamFilter * av_bitstream_filter_next(const AVBitStreamFilter *f)
If f is NULL, return the first registered bitstream filter, if f is non-NULL, return the next registe...
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
#define av_free(p)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
char * args
Internal default arguments, used if NULL is passed to av_bitstream_filter_filter().
Definition: avcodec.h:5814
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4080
void av_register_bitstream_filter(AVBitStreamFilter *bsf)
Register a bitstream filter.
const struct AVBitStreamFilter * filter
Definition: avcodec.h:5807
#define av_freep(p)
This structure stores compressed data.
Definition: avcodec.h:1634
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5858
const char * name
Definition: opengl_enc.c:103