FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <string.h>
20 
21 #include "libavutil/log.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 
25 #include "avcodec.h"
26 #include "bsf.h"
27 
28 struct AVBSFInternal {
30  int eof;
31 };
32 
34 {
36 
37  if (!pctx || !*pctx)
38  return;
39  ctx = *pctx;
40 
41  if (ctx->filter->close)
42  ctx->filter->close(ctx);
43  if (ctx->filter->priv_class && ctx->priv_data)
44  av_opt_free(ctx->priv_data);
45 
46  av_opt_free(ctx);
47 
49  av_freep(&ctx->internal);
50  av_freep(&ctx->priv_data);
51 
54 
55  av_freep(pctx);
56 }
57 
58 static void *bsf_child_next(void *obj, void *prev)
59 {
60  AVBSFContext *ctx = obj;
61  if (!prev && ctx->filter->priv_class)
62  return ctx->priv_data;
63  return NULL;
64 }
65 
66 static const AVClass bsf_class = {
67  .class_name = "AVBSFContext",
68  .item_name = av_default_item_name,
69  .version = LIBAVUTIL_VERSION_INT,
70  .child_next = bsf_child_next,
71  .child_class_next = ff_bsf_child_class_next,
72 };
73 
75 {
76  return &bsf_class;
77 }
78 
80 {
82  int ret;
83 
84  ctx = av_mallocz(sizeof(*ctx));
85  if (!ctx)
86  return AVERROR(ENOMEM);
87 
88  ctx->av_class = &bsf_class;
89  ctx->filter = filter;
90 
93  if (!ctx->par_in || !ctx->par_out) {
94  ret = AVERROR(ENOMEM);
95  goto fail;
96  }
97 
98  ctx->internal = av_mallocz(sizeof(*ctx->internal));
99  if (!ctx->internal) {
100  ret = AVERROR(ENOMEM);
101  goto fail;
102  }
103 
105  if (!ctx->internal->buffer_pkt) {
106  ret = AVERROR(ENOMEM);
107  goto fail;
108  }
109 
110  av_opt_set_defaults(ctx);
111 
112  /* allocate priv data and init private options */
113  if (filter->priv_data_size) {
114  ctx->priv_data = av_mallocz(filter->priv_data_size);
115  if (!ctx->priv_data) {
116  ret = AVERROR(ENOMEM);
117  goto fail;
118  }
119  if (filter->priv_class) {
120  *(const AVClass **)ctx->priv_data = filter->priv_class;
122  }
123  }
124 
125  *pctx = ctx;
126  return 0;
127 fail:
128  av_bsf_free(&ctx);
129  return ret;
130 }
131 
133 {
134  int ret, i;
135 
136  /* check that the codec is supported */
137  if (ctx->filter->codec_ids) {
138  for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
139  if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
140  break;
141  if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
143  av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
144  "bitstream filter '%s'. Supported codecs are: ",
145  desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
146  for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
147  desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
148  av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
149  desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
150  }
151  av_log(ctx, AV_LOG_ERROR, "\n");
152  return AVERROR(EINVAL);
153  }
154  }
155 
156  /* initialize output parameters to be the same as input
157  * init below might overwrite that */
158  ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
159  if (ret < 0)
160  return ret;
161 
162  ctx->time_base_out = ctx->time_base_in;
163 
164  if (ctx->filter->init) {
165  ret = ctx->filter->init(ctx);
166  if (ret < 0)
167  return ret;
168  }
169 
170  return 0;
171 }
172 
174 {
175  if (!pkt || !pkt->data) {
176  ctx->internal->eof = 1;
177  return 0;
178  }
179 
180  if (ctx->internal->eof) {
181  av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
182  return AVERROR(EINVAL);
183  }
184 
185  if (ctx->internal->buffer_pkt->data ||
187  return AVERROR(EAGAIN);
188 
190 
191  return 0;
192 }
193 
195 {
196  return ctx->filter->filter(ctx, pkt);
197 }
198 
200 {
201  AVBSFInternal *in = ctx->internal;
202  AVPacket *tmp_pkt;
203 
204  if (in->eof)
205  return AVERROR_EOF;
206 
207  if (!ctx->internal->buffer_pkt->data &&
209  return AVERROR(EAGAIN);
210 
211  tmp_pkt = av_packet_alloc();
212  if (!tmp_pkt)
213  return AVERROR(ENOMEM);
214 
215  *pkt = ctx->internal->buffer_pkt;
216  ctx->internal->buffer_pkt = tmp_pkt;
217 
218  return 0;
219 }
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:33
#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:5742
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5708
static void * bsf_child_next(void *obj, void *prev)
Definition: bsf.c:58
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
memory handling functions
const char * desc
Definition: nvenc.c:89
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1264
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3922
const struct AVBitStreamFilter * filter
The bitstream filter this context is an instance of.
Definition: avcodec.h:5686
The bitstream filter state.
Definition: avcodec.h:5677
static AVPacket pkt
static const AVClass bsf_class
Definition: bsf.c:66
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5698
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:132
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:79
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:194
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
AVOptions.
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: utils.c:4038
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
const char * name
Definition: avcodec.h:5724
uint8_t * data
Definition: avcodec.h:1580
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:617
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:4059
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
enum AVCodecID * codec_ids
A list of codec ids supported by the filter, terminated by AV_CODEC_ID_NONE.
Definition: avcodec.h:5731
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition: avcodec.h:5714
int side_data_elems
Definition: avcodec.h:1592
int eof
Definition: bsf.c:30
int(* init)(AVBSFContext *ctx)
Definition: avcodec.h:5753
void avcodec_parameters_free(AVCodecParameters **par)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: utils.c:4048
#define fail()
Definition: checkasm.h:81
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2956
const AVClass * ff_bsf_child_class_next(const AVClass *prev)
const AVClass * av_bsf_get_class(void)
Get the AVClass for AVBSFContext.
Definition: bsf.c:74
AVRational time_base_out
The timebase used for the timestamps of the output packets.
Definition: avcodec.h:5720
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:173
AVFormatContext * ctx
Definition: movenc.c:48
const AVClass * av_class
A class for logging and AVOptions.
Definition: avcodec.h:5681
Libavcodec external API header.
void(* close)(AVBSFContext *ctx)
Definition: avcodec.h:5755
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
AVPacket * buffer_pkt
Definition: bsf.c:29
int(* filter)(AVBSFContext *ctx, AVPacket *pkt)
Definition: avcodec.h:5754
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:665
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:657
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1516
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:199
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:51
#define av_freep(p)
AVBSFInternal * internal
Opaque libavcodec internal data.
Definition: avcodec.h:5692
This structure stores compressed data.
Definition: avcodec.h:1557
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5703
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:252