20 #include <com/avpkit/core/BufferSink.h>
21 #include <com/avpkit/core/AudioSamples.h>
22 #include <com/avpkit/ferry/Logger.h>
26 #include "libavutil/channel_layout.h"
27 #include "VideoPicture.h"
31 VS_LOG_SETUP(VS_CPP_PACKAGE);
42 return av_buffersink_get_channels(mSinkFilterContext);
46 return av_buffersink_get_sample_rate(mSinkFilterContext);
50 return av_buffersink_get_w(mSinkFilterContext);
54 return av_buffersink_get_h(mSinkFilterContext);
58 AVRational frameRate = av_buffersink_get_frame_rate(mSinkFilterContext);
63 AVRational frameRate = av_buffersink_get_time_base(mSinkFilterContext);
68 av_buffersink_set_frame_size(mSinkFilterContext, frameSize);
71 void BufferSink::setReady() {
72 mTimeBase.reset(getTimeBase(),
true);
73 MediaFilter::setReady();
86 AVFrame* frame = av_frame_alloc();
88 retval = av_buffersink_get_frame(mSinkFilterContext, frame);
89 if (retval == AVERROR(EAGAIN)) {
91 }
else if (retval >= 0) {
93 buffer_size = av_samples_get_buffer_size(NULL, frame->channels, frame->nb_samples, (AVSampleFormat) frame->format, 0);
95 memcpy(outSamples->getRawSamples(0), frame->extended_data[0], buffer_size);
103 timeBase->
rescale(frame->pts, mTimeBase.value()));
104 VS_REF_RELEASE(timeBase);
106 av_frame_free(&frame);
123 AVFrame* frame = av_frame_alloc();
125 retval = av_buffersink_get_frame(mSinkFilterContext, frame);
126 if (retval == AVERROR(EAGAIN)) {
128 }
else if (retval >= 0) {
135 timeBase->
rescale(frame->pts, mTimeBase.value()));
136 VS_REF_RELEASE(timeBase);
138 av_frame_free(&frame);
145 BufferSink::BufferSink() {
146 mSinkFilterContext = NULL;
151 BufferSink::~BufferSink() {
153 mSinkFilterContext = NULL;
160 BufferSink* retval = NULL;
162 retval = BufferSink::make();
165 retval->mFilter = avfilter_get_by_name(
"aformat");
166 retval->mSinkFilter = avfilter_get_by_name(
"abuffersink");
167 retval->mFilterContext = avfilter_graph_alloc_filter(graph, retval->mFilter,
"aformat");
168 if (!retval->mFilterContext) {
169 VS_REF_RELEASE(retval);
171 av_opt_set(retval->mFilterContext,
"sample_fmts",
"s16", AV_OPT_SEARCH_CHILDREN);
172 if (channel_layout != IAudioSamples::ChannelLayout::CH_NONE) {
174 av_get_channel_layout_string(ch_layout,
177 (uint64_t) channel_layout);
178 av_opt_set(retval->mFilterContext,
"channel_layouts", ch_layout, AV_OPT_SEARCH_CHILDREN);
180 if (avfilter_init_str(retval->mFilterContext, NULL) < 0) {
181 VS_REF_RELEASE(retval);
183 retval->mSinkFilterContext = avfilter_graph_alloc_filter(graph, retval->mSinkFilter,
"abuffersink");
184 if (!retval->mSinkFilterContext) {
185 VS_REF_RELEASE(retval);
187 av_opt_set_int(retval->mSinkFilterContext,
"all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN);
188 if (avfilter_init_str(retval->mSinkFilterContext, NULL) < 0) {
189 VS_REF_RELEASE(retval);
191 retval->mFilterGraph = graph;
192 if (avfilter_link(retval->mFilterContext, 0, retval->mSinkFilterContext, 0) < 0) {
193 VS_REF_RELEASE(retval);
208 BufferSink* retval = NULL;
210 retval = BufferSink::make();
213 retval->mFilter = avfilter_get_by_name(
"format");
214 retval->mSinkFilter = avfilter_get_by_name(
"buffersink");
215 retval->mFilterContext = avfilter_graph_alloc_filter(graph, retval->mFilter,
"format");
216 if (!retval->mFilterContext) {
217 VS_REF_RELEASE(retval);
219 av_opt_set(retval->mFilterContext,
"pix_fmts", pixel_type == IPixelFormat::NONE ?
"yuv420p" : av_get_pix_fmt_name((AVPixelFormat) pixel_type), AV_OPT_SEARCH_CHILDREN);
220 if (avfilter_init_str(retval->mFilterContext, NULL) < 0) {
221 VS_REF_RELEASE(retval);
223 retval->mSinkFilterContext = avfilter_graph_alloc_filter(graph, retval->mSinkFilter,
"buffersink");
224 if (!retval->mSinkFilterContext) {
225 VS_REF_RELEASE(retval);
227 if (avfilter_init_str(retval->mSinkFilterContext, NULL) < 0) {
228 VS_REF_RELEASE(retval);
230 retval->mFilterGraph = graph;
231 if (avfilter_link(retval->mFilterContext, 0, retval->mSinkFilterContext, 0) < 0) {
232 VS_REF_RELEASE(retval);
virtual int32_t getSampleRate()
Find the sample rate of the samples in this audio buffer.
virtual int32_t getChannels()
Return the number of channels of the samples in this buffer.
virtual IRational * getTimeBase()
Get the time base that time stamps of this object are represented in.
virtual int32_t ensureCapacity(int32_t capacityInBytes)
Called by decoder before decoding to ensure sufficient space.
virtual void setComplete(bool complete, int32_t numSamples, int32_t sampleRate, int32_t channels, Format sampleFmt, int64_t pts)
Call this if you modify the samples and are now done.
virtual int getChannels()
Returns the channel number of output filtered samples.
virtual IRational * getTimeBase()
Returns the frame rate output filtered picture.
virtual int getWidth()
Returns the width of output filtered picture.
virtual void setNumSamples(int frameSize)
Sets the number of samples for each output filtered samples.
virtual IRational * getFrameRate()
Returns the frame rate output filtered picture.
virtual int getHeight()
Returns the height of output filtered picture.
virtual int fillVideoPicture(IVideoPicture *picture)
Fills this video picture with filtered data.
virtual int getSampleRate()
Returns the sample rate of output filtered samples.
virtual int fillAudioSamples(IAudioSamples *samples)
Fills this audio samples with filtered data.
A set of raw (decoded) samples, plus a timestamp for when to play those samples relative to other ite...
This class wraps represents a Rational number for the AVPKit.
static IRational * make()
Get a new rational that will be set to 0/0.
virtual int64_t rescale(int64_t origValue, IRational *origBase)=0
Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this...
Represents one raw (undecoded) picture in a video stream, plus a timestamp for when to display that v...
virtual void setComplete(bool aIsComplete, IPixelFormat::Type format, int width, int height, int64_t pts)
After modifying the raw data in this buffer, call this function to let the object know it is now comp...
void copyAVFrame(AVFrame *frame, IPixelFormat::Type pixel, int32_t width, int32_t height)
Called by the StreamCoder once it's done decoding.
virtual int getWidth()
What is the width of the picture.
virtual int getHeight()
What is the height of the picture.
virtual IRational * getTimeBase()
Get the time base that time stamps of this object are represented in.
This library contains routines used by AVPKit libraries for "ferry"ing Java objects to and from nativ...
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...