FFmpeg
source.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 /**
20  * @file
21  * Packet source. Heavily based on libavfilter/buffersrc.c
22  */
23 
24 #include <float.h>
25 
27 #include "libavutil/opt.h"
28 
29 #include "libavcodec/bsf.h"
31 #include "libavcodec/packet.h"
33 
34 typedef struct SourceContext {
35  const AVClass *class;
37  AVRational time_base; ///< time_base to set in the output link
38 
40  unsigned warning_limit;
41 
42  int eof;
45 
47 {
49 
50  return avcodec_parameters_copy(s->par, par);
51 }
52 
54 {
55  int ret;
56 
57  while (1) {
58  ret = ff_bsf_graph_run_once(graph);
59  if (ret == AVERROR(EAGAIN))
60  break;
61  if (ret < 0 && ret != FFERROR_SOURCE_EMPTY)
62  return ret;
63  }
64  return 0;
65 }
66 
68 {
70  AVPacket *copy;
71  int ret;
72 
73  s->nb_failed_requests = 0;
74 
75  if (!pkt || AVPACKET_IS_EMPTY(pkt))
76  return av_bsf_source_close(ctx, s->last_pts, flags);
77  if (s->eof)
78  return AVERROR_EOF;
79 
80  s->last_pts = pkt->pts + pkt->duration;
81 
83  if (!copy)
84  return AVERROR(ENOMEM);
85 
88  if (ret < 0) {
90  return ret;
91  }
92  } else
94 
95  ret = ff_bsf_filter_packet(ctx->outputs[0], copy);
96  if (ret < 0)
97  return ret;
98 
100  ret = push_packet(ctx->graph);
101  if (ret < 0)
102  return ret;
103  }
104 
105  BitStreamFilterLinkInternal *const li = ff_link_internal(ctx->outputs[0]);
106  if (s->warning_limit &&
107  av_container_fifo_can_read(li->fifo) >= s->warning_limit) {
109  "%d buffers queued in %s, something may be wrong.\n",
110  s->warning_limit,
111  (char *)av_x_if_null(ctx->name, ctx->filter->name));
112  s->warning_limit *= 10;
113  }
114 
115  return 0;
116 }
117 
119 {
121 
122  s->eof = 1;
124  return 0;
125 }
126 
128 {
130 
131  if (!s->eof && ff_bsf_outlink_get_status(ctx->outputs[0]))
132  s->eof = 1;
133 
134  return s->eof ? AVERROR(EOF) : 0;
135 }
136 
138 {
140 
141  c->par = avcodec_parameters_alloc();
142  if (!c->par)
143  return AVERROR(ENOMEM);
144 
145  return 0;
146 }
147 
149 {
151 
152  if (av_q2d(c->time_base) <= 0) {
153  av_log(ctx, AV_LOG_ERROR, "Invalid time base %d/%d\n", c->time_base.num, c->time_base.den);
154  return AVERROR(EINVAL);
155  }
156 
157  c->warning_limit = 100;
158  return 0;
159 }
160 
162 {
163  return ((SourceContext *)buffer_src->priv_data)->nb_failed_requests;
164 }
165 
166 #define OFFSET(x) offsetof(SourceContext, x)
167 #define FLAGS (AV_OPT_FLAG_BSF_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
168 static const AVOption buffer_options[] = {
169  { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, FLAGS },
170  { NULL },
171 };
172 
174 
176 {
178  avcodec_parameters_free(&s->par);
179 }
180 
182 {
183  SourceContext *c = link->src->priv_data;
184 
185  link->time_base = c->time_base;
186  return avcodec_parameters_copy(link->par, c->par);
187 }
188 
190 {
191  AVBitStreamFilterLink *outlink = ctx->outputs[0];
193 
194  if (!c->eof && ff_bsf_outlink_get_status(outlink)) {
195  c->eof = 1;
196  return 0;
197  }
198 
199  if (c->eof) {
200  ff_bsf_link_set_in_status(outlink, AVERROR_EOF, c->last_pts);
201  return 0;
202  }
203  c->nb_failed_requests++;
204  return FFERROR_SOURCE_EMPTY;
205 }
206 
208  {
209  .name = "default",
210  .config_props = config_props,
211  },
212 };
213 
215  .p.name = "source",
216  .p.priv_class = &buffer_class,
217  .priv_data_size = sizeof(SourceContext),
218  .activate = activate,
219  .preinit = preinit,
220  .init2 = init,
221  .uninit = uninit,
222 
224 };
flags
const SwsFlags flags[]
Definition: swscale.c:85
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
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
opt.h
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:49
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVBitStreamFilter::name
const char * name
Definition: bsf.h:112
av_cold
#define av_cold
Definition: attributes.h:119
int64_t
long long int64_t
Definition: coverity.c:34
AVBitStreamFilterPad
A filter pad used for either input or output.
Definition: filters.h:35
ff_bsf_link_set_in_status
void ff_bsf_link_set_in_status(AVBitStreamFilterLink *link, int status, int64_t pts)
Definition: bitstreamfilter.c:420
container_fifo.h
ff_bsf_source_get_nb_failed_requests
unsigned ff_bsf_source_get_nb_failed_requests(const AVBitStreamFilterContext *buffer_src)
Get the number of failed requests.
Definition: source.c:161
AVOption
AVOption.
Definition: opt.h:428
ff_bsf_graph_run_once
int ff_bsf_graph_run_once(AVBitStreamFilterGraph *graph)
Run one round of processing on a filter graph.
Definition: bsfgraph.c:361
float.h
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:621
FFERROR_SOURCE_EMPTY
#define FFERROR_SOURCE_EMPTY
Definition: filters.h:33
preinit
static av_cold int preinit(AVBitStreamFilterContext *ctx)
Definition: source.c:137
AV_OPT_TYPE_RATIONAL
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
Definition: opt.h:279
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:74
init
static av_cold int init(AVBitStreamFilterContext *ctx)
Definition: source.c:148
bsf.h
av_bsf_source_close
int av_bsf_source_close(AVBitStreamFilterContext *ctx, int64_t pts, unsigned flags)
Close the source after EOF.
Definition: source.c:118
pts
static int64_t pts
Definition: transcode_aac.c:649
BitStreamFilterLinkInternal::fifo
AVContainerFifo * fifo
Queue of packets waiting to be filtered.
Definition: bsf_internal.h:116
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
source_outputs
static const AVBitStreamFilterPad source_outputs[]
Definition: source.c:207
AV_BSF_SOURCE_FLAG_PUSH
@ AV_BSF_SOURCE_FLAG_PUSH
Immediately push the packet to the output.
Definition: bsf.h:594
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
ff_bsf_filter_packet
int ff_bsf_filter_packet(AVBitStreamFilterLink *link, AVPacket *pkt)
Send a packet of data to the next filter.
Definition: bitstreamfilter.c:666
link
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 link
Definition: filter_design.txt:23
av_bsf_source_parameters_set
int av_bsf_source_parameters_set(AVBitStreamFilterContext *ctx, const AVCodecParameters *par)
Initialize the source filter with the provided parameters.
Definition: source.c:46
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
push_packet
static int push_packet(AVBitStreamFilterGraph *graph)
Definition: source.c:53
NULL
#define NULL
Definition: coverity.c:32
FFBitStreamFilter
Definition: bsf_internal.h:29
AVPACKET_IS_EMPTY
#define AVPACKET_IS_EMPTY(pkt)
Definition: packet_internal.h:26
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
activate
static int activate(AVBitStreamFilterContext *ctx)
Definition: source.c:189
SourceContext::last_pts
int64_t last_pts
Definition: source.c:43
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: packet.c:442
av_packet_move_ref
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: packet.c:491
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_BSF_SOURCE_FLAG_KEEP_REF
@ AV_BSF_SOURCE_FLAG_KEEP_REF
Keep a reference to the packet.
Definition: bsf.h:599
packet_internal.h
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition: bsf_internal.h:33
attribute_align_arg
#define attribute_align_arg
Definition: internal.h:50
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:186
SourceContext::eof
int eof
Definition: source.c:42
ff_link_internal
static BitStreamFilterLinkInternal * ff_link_internal(AVBitStreamFilterLink *link)
Definition: bsf_internal.h:164
av_bsf_source_get_status
int av_bsf_source_get_status(AVBitStreamFilterContext *ctx)
Returns 0 or a negative AVERROR code.
Definition: source.c:127
AVFrame::time_base
AVRational time_base
Time base for the timestamps in this frame.
Definition: frame.h:583
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
AVCodecParameters::avcodec_parameters_alloc
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: codec_par.c:57
buffer_options
static const AVOption buffer_options[]
Definition: source.c:168
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:596
packet.h
FLAGS
#define FLAGS
Definition: source.c:167
s
uint8_t s
Definition: llvidencdsp.c:39
BSFILTER_OUTPUTS
#define BSFILTER_OUTPUTS(array)
Definition: filters.h:146
AVCodecParameters::avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:107
ff_source_bsf
const FFBitStreamFilter ff_source_bsf
Definition: source.c:214
ret
ret
Definition: filter_design.txt:187
uninit
static av_cold void uninit(AVBitStreamFilterContext *ctx)
Definition: source.c:175
OFFSET
#define OFFSET(x)
Definition: source.c:166
AVCodecParameters::avcodec_parameters_free
void avcodec_parameters_free(AVCodecParameters **par)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: codec_par.c:67
ff_bsf_outlink_get_status
int ff_bsf_outlink_get_status(AVBitStreamFilterLink *link)
Definition: bitstreamfilter.c:895
BSF_DEFINE_CLASS
BSF_DEFINE_CLASS(buffer)
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AVBitStreamFilterPad::name
const char * name
Pad name.
Definition: filters.h:41
AVBitStreamFilterContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:375
SourceContext::par
AVCodecParameters * par
Definition: source.c:36
SourceContext::warning_limit
unsigned warning_limit
Definition: source.c:40
av_container_fifo_can_read
size_t av_container_fifo_can_read(const AVContainerFifo *cf)
Definition: container_fifo.c:185
BitStreamFilterLinkInternal
Definition: bsf_internal.h:110
config_props
static int config_props(AVBitStreamFilterLink *link)
Definition: source.c:181
SourceContext::time_base
AVRational time_base
time_base to set in the output link
Definition: source.c:37
AVPacket
This structure stores compressed data.
Definition: packet.h:580
SourceContext::nb_failed_requests
unsigned nb_failed_requests
Definition: source.c:39
AVFormatContext::name
char * name
Name of this format context, only used for logging purposes.
Definition: avformat.h:1944
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVBitStreamFilterContext
An instance of a filter.
Definition: bsf.h:347
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
AVBitStreamFilterGraph
Definition: bsf.h:477
SourceContext
Definition: source.c:34
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1342
av_bsf_source_add_packet
int attribute_align_arg av_bsf_source_add_packet(AVBitStreamFilterContext *ctx, AVPacket *pkt, int flags)
Add a packet to the buffer source.
Definition: source.c:67
av_x_if_null
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:311