Libav
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 Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 
25 #include "libavutil/internal.h"
26 #include "libavutil/mem.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 {
49 
51 {
53  BSFCompatContext *priv = NULL;
54  const AVBitStreamFilter *bsf;
55 
56  bsf = av_bsf_get_by_name(name);
57  if (!bsf)
58  return NULL;
59 
60  ctx = av_mallocz(sizeof(*ctx));
61  if (!ctx)
62  return NULL;
63 
64  priv = av_mallocz(sizeof(*priv));
65  if (!priv)
66  goto fail;
67 
68 
69  ctx->filter = bsf;
70  ctx->priv_data = priv;
71 
72  return ctx;
73 
74 fail:
75  if (priv)
76  av_bsf_free(&priv->ctx);
77  av_freep(&priv);
78  av_freep(&ctx);
79  return NULL;
80 }
81 
83 {
84  BSFCompatContext *priv = bsfc->priv_data;
85 
86  av_bsf_free(&priv->ctx);
87  av_freep(&bsfc->priv_data);
88  av_free(bsfc);
89 }
90 
92  AVCodecContext *avctx, const char *args,
93  uint8_t **poutbuf, int *poutbuf_size,
94  const uint8_t *buf, int buf_size, int keyframe)
95 {
96  BSFCompatContext *priv = bsfc->priv_data;
97  AVPacket pkt = { 0 };
98  int ret;
99 
100  if (!priv->ctx) {
101  ret = av_bsf_alloc(bsfc->filter, &priv->ctx);
102  if (ret < 0)
103  return ret;
104 
105  ret = avcodec_parameters_from_context(priv->ctx->par_in, avctx);
106  if (ret < 0)
107  return ret;
108 
109  priv->ctx->time_base_in = avctx->time_base;
110 
111  ret = av_bsf_init(priv->ctx);
112  if (ret < 0)
113  return ret;
114 
115  if (priv->ctx->par_out->extradata_size) {
116  av_freep(&avctx->extradata);
117  avctx->extradata_size = 0;
119  if (!avctx->extradata)
120  return AVERROR(ENOMEM);
121  memcpy(avctx->extradata, priv->ctx->par_out->extradata,
122  priv->ctx->par_out->extradata_size);
123  avctx->extradata_size = priv->ctx->par_out->extradata_size;
124  }
125  }
126 
127  pkt.data = buf;
128  pkt.size = buf_size;
129 
130  ret = av_bsf_send_packet(priv->ctx, &pkt);
131  if (ret < 0)
132  return ret;
133 
134  *poutbuf = NULL;
135  *poutbuf_size = 0;
136 
137  ret = av_bsf_receive_packet(priv->ctx, &pkt);
138  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
139  return 0;
140  else if (ret < 0)
141  return ret;
142 
143  *poutbuf = av_malloc(pkt.size + AV_INPUT_BUFFER_PADDING_SIZE);
144  if (!*poutbuf) {
145  av_packet_unref(&pkt);
146  return AVERROR(ENOMEM);
147  }
148 
149  *poutbuf_size = pkt.size;
150  memcpy(*poutbuf, pkt.data, pkt.size);
151 
152  av_packet_unref(&pkt);
153 
154  /* drain all the remaining packets we cannot return */
155  while (ret >= 0) {
156  ret = av_bsf_receive_packet(priv->ctx, &pkt);
157  av_packet_unref(&pkt);
158  }
159 
160  return 1;
161 }
163 #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:33
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5021
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)
The bitstream filter state.
Definition: avcodec.h:4990
int size
Definition: avcodec.h:1347
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:132
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1535
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:79
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:194
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
uint8_t * data
Definition: avcodec.h:1346
#define AVERROR_EOF
End of file.
Definition: error.h:51
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
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)
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition: avcodec.h:5027
#define fail()
Definition: checkasm.h:80
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
common internal API header
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
const char * name
Definition: qsvenc.c:44
void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc)
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:173
AVBSFContext * ctx
NULL
Definition: eval.c:55
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1409
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
int extradata_size
Definition: avcodec.h:1524
struct AVBitStreamFilter * filter
Definition: avcodec.h:4972
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:2837
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
FF_DISABLE_DEPRECATION_WARNINGS AVBitStreamFilter * av_bitstream_filter_next(const AVBitStreamFilter *f)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
void av_register_bitstream_filter(AVBitStreamFilter *bsf)
This structure stores compressed data.
Definition: avcodec.h:1323
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5016
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211