FFmpeg
sink.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 sink. Heavily based on libavfilter/buffersink.c
22  */
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/opt.h"
26 
27 #include "libavcodec/bsf.h"
29 #include "libavcodec/packet.h"
31 
32 typedef struct BufferSinkContext {
33  const AVClass *class;
34  unsigned warning_limit;
35 
38 
40 {
41  if ((flags & AV_BSF_SINK_FLAG_PEEK)) {
42  buf->peeked_pkt = in;
43  return av_packet_ref(out, in);
44  } else {
45  buf->peeked_pkt = NULL;
47  av_packet_free(&in);
48  return 0;
49  }
50 }
51 
53 {
55  AVBitStreamFilterLink *inlink = ctx->inputs[0];
57  int status, ret;
58  AVPacket *cur_pkt;
59  int64_t pts;
60  int buffersrc_empty = 0;
61 
62  if (buf->peeked_pkt)
63  return return_or_keep_packet(buf, pkt, buf->peeked_pkt, flags);
64 
65  while (1) {
67  if (ret < 0) {
68  return ret;
69  } else if (ret) {
70  return return_or_keep_packet(buf, pkt, cur_pkt, flags);
72  return status;
73  } else if ((flags & AV_BSF_SINK_FLAG_NO_REQUEST)) {
74  return AVERROR(EAGAIN);
75  } else if (li->packet_wanted_out) {
76  ret = ff_bsf_graph_run_once(ctx->graph);
77  if (ret == FFERROR_SOURCE_EMPTY) {
78  buffersrc_empty = 1;
79  } else if (ret == AVERROR(EAGAIN)) {
80  if (buffersrc_empty)
81  return ret;
83  } else if (ret < 0) {
84  return ret;
85  }
86  } else {
88  }
89  }
90 }
91 
93 {
95 
96  s->warning_limit = 100;
97 
98  return 0;
99 }
100 
102 {
104 
105  av_packet_free(&buf->peeked_pkt);
106 }
107 
109 {
111  BitStreamFilterLinkInternal * const li = ff_link_internal(ctx->inputs[0]);
112 
113  if (buf->warning_limit &&
116  "%d buffers queued in %s, something may be wrong.\n",
117  buf->warning_limit,
118  (char *)av_x_if_null(ctx->name, ctx->filter->name));
119  buf->warning_limit *= 10;
120  }
121 
122  /* The packet is queued, the rest is up to av_bsf_sink_get_packet */
123  return 0;
124 }
125 
127 {
128  av_assert0(ff_bsf(ctx->filter)->activate == activate);
129  return ctx->inputs[0]->time_base;
130 }
131 
133 {
134  av_assert0(ff_bsf(ctx->filter)->activate == activate);
135  return ctx->inputs[0]->par;
136 }
137 
138 BSF_DEFINE_CLASS_EXT(sink, "sink", NULL);
139 
141  .p.name = "sink",
142  .p.priv_class = &sink_class,
143  .priv_data_size = sizeof(BufferSinkContext),
144  .init2 = init,
145  .uninit = uninit,
146  .activate = activate,
148 };
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
ff_default_bsf_pad
const AVBitStreamFilterPad ff_default_bsf_pad[1]
Definition: bitstreamfilter.c:907
ff_bsf
static const av_always_inline FFBitStreamFilter * ff_bsf(const AVBitStreamFilter *bsf)
Definition: bsf_internal.h:82
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
ff_bsf_inlink_acknowledge_status
int ff_bsf_inlink_acknowledge_status(AVBitStreamFilterLink *link, int *rstatus, int64_t *rpts)
Definition: bitstreamfilter.c:810
out
static FILE * out
Definition: movenc.c:55
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:49
AVBitStreamFilter::name
const char * name
Definition: bsf.h:112
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_bsf_sink_get_parameters
const AVCodecParameters * av_bsf_sink_get_parameters(const AVBitStreamFilterContext *ctx)
Definition: sink.c:132
ff_bsf_inlink_consume_packet
int ff_bsf_inlink_consume_packet(AVBitStreamFilterLink *link, AVPacket **rpkt)
Definition: bitstreamfilter.c:844
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
FFERROR_SOURCE_EMPTY
#define FFERROR_SOURCE_EMPTY
Definition: filters.h:33
ff_bsf_inlink_request_packet
void ff_bsf_inlink_request_packet(AVBitStreamFilterLink *link)
Definition: bitstreamfilter.c:866
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
BitStreamFilterLinkInternal::packet_wanted_out
int packet_wanted_out
True if a packet is currently wanted on the output of this filter.
Definition: bsf_internal.h:149
bsf.h
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
init
static int init(AVBitStreamFilterContext *ctx)
Definition: sink.c:92
avassert.h
AV_BSF_SINK_FLAG_NO_REQUEST
@ AV_BSF_SINK_FLAG_NO_REQUEST
Tell av_bsf_sink_get_packet() not to request a packet from its input.
Definition: bsf.h:677
FFBitStreamFilter::activate
int(* activate)(AVBitStreamFilterContext *ctx)
Filter activation function.
Definition: bsf_internal.h:79
BSFILTER_INPUTS
#define BSFILTER_INPUTS(array)
Definition: filters.h:145
BufferSinkContext::peeked_pkt
AVPacket * peeked_pkt
Definition: sink.c:36
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
av_bsf_sink_get_time_base
AVRational av_bsf_sink_get_time_base(const AVBitStreamFilterContext *ctx)
Definition: sink.c:126
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
FFBitStreamFilter
Definition: bsf_internal.h:29
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
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
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
uninit
static void uninit(AVBitStreamFilterContext *ctx)
Definition: sink.c:101
ff_link_internal
static BitStreamFilterLinkInternal * ff_link_internal(AVBitStreamFilterLink *link)
Definition: bsf_internal.h:164
BSF_DEFINE_CLASS_EXT
BSF_DEFINE_CLASS_EXT(sink, "sink", NULL)
return_or_keep_packet
static int return_or_keep_packet(BufferSinkContext *buf, AVPacket *out, AVPacket *in, int flags)
Definition: sink.c:39
packet.h
s
uint8_t s
Definition: llvidencdsp.c:39
BufferSinkContext
Definition: sink.c:32
ret
ret
Definition: filter_design.txt:187
BufferSinkContext::warning_limit
unsigned warning_limit
Definition: sink.c:34
av_bsf_sink_get_packet
int attribute_align_arg av_bsf_sink_get_packet(AVBitStreamFilterContext *ctx, AVPacket *pkt, int flags)
Get a packet with filtered data from sink and put it in packet.
Definition: sink.c:52
ff_sink_bsf
const FFBitStreamFilter ff_sink_bsf
Definition: sink.c:140
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
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
AV_BSF_SINK_FLAG_PEEK
@ AV_BSF_SINK_FLAG_PEEK
Tell av_buffersink_get_buffer_ref() to read video/samples buffer reference, but not remove it from th...
Definition: bsf.h:670
activate
static int activate(AVBitStreamFilterContext *ctx)
Definition: sink.c:108
AVPacket
This structure stores compressed data.
Definition: packet.h:580
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
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1342
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