FFmpeg
Loading...
Searching...
No Matches
amix.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 <stdio.h>
20
22#include "libavutil/error.h"
23#include "libavutil/frame.h"
24#include "libavutil/samplefmt.h"
25#include "libavutil/timestamp.h"
29
30#define SRC "=sample_fmt=flt:sample_rate=48000:channel_layout=mono:time_base=1/48000"
31
33{
35 int ret;
36
37 if (!frame)
38 return AVERROR(ENOMEM);
39 frame->format = AV_SAMPLE_FMT_FLT;
40 frame->sample_rate = 48000;
42 frame->nb_samples = 1024;
43 frame->pts = pts;
45 if (ret >= 0) {
46 av_samples_set_silence(frame->extended_data, 0, frame->nb_samples,
47 1, frame->format);
49 }
51 return ret;
52}
53
55{
56 int ret;
57
58 while ((ret = av_buffersink_get_frame(sink, frame)) >= 0) {
59 printf("pts:%s samples:%d\n", av_ts2str(frame->pts), frame->nb_samples);
61 }
62 return ret == AVERROR(EAGAIN) || ret == AVERROR_EOF ? 0 : ret;
63}
64
65int main(void)
66{
69 AVFilterContext *in0, *in1, *sink;
70 int ret = AVERROR(ENOMEM);
71
72 if (!graph || !frame)
73 goto end;
74 graph->nb_threads = 1;
75 ret = avfilter_graph_parse_ptr(graph, "abuffer@in0" SRC "[a];abuffer@in1" SRC "[b];"
76 "[a][b]amix=duration=longest,abuffersink@out",
77 NULL, NULL, NULL);
78 if (ret < 0 || (ret = avfilter_graph_config(graph, NULL)) < 0)
79 goto end;
80 in0 = avfilter_graph_get_filter(graph, "abuffer@in0");
81 in1 = avfilter_graph_get_filter(graph, "abuffer@in1");
82 sink = avfilter_graph_get_filter(graph, "abuffersink@out");
83
84 /* Input 1's second frame is consumed while input 0's frame list is
85 * empty, before input 0's EOF has been signalled. */
86 if ((ret = send_frame(in0, 48000)) < 0 ||
87 (ret = send_frame(in1, 0)) < 0 ||
88 (ret = drain(sink, frame)) < 0 ||
89 (ret = send_frame(in1, 1024)) < 0 ||
90 (ret = drain(sink, frame)) < 0 ||
91 (ret = av_buffersrc_add_frame(in0, NULL)) < 0 ||
92 (ret = drain(sink, frame)) < 0 ||
93 (ret = send_frame(in1, 2048)) < 0 ||
94 (ret = av_buffersrc_add_frame(in1, NULL)) < 0 ||
95 (ret = drain(sink, frame)) < 0)
96 goto end;
97
98end:
100 avfilter_graph_free(&graph);
101 if (ret < 0) {
102 fprintf(stderr, "%s\n", av_err2str(ret));
103 return 1;
104 }
105 return 0;
106}
int main(void)
Definition amix.c:65
static int drain(AVFilterContext *sink, AVFrame *frame)
Definition amix.c:54
static int send_frame(AVFilterContext *src, int64_t pts)
Definition amix.c:32
Main libavfilter public API header.
memory buffer sink API for audio and video
Memory buffer source API.
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
__device__ int printf(const char *,...)
static AVFrame * frame
error code definitions
int ret
Definition ffplay.c:391
reference-counted frame API
int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
Get a frame with filtered data from sink and put it in frame.
Definition buffersink.c:77
int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
Add a frame to the buffer source.
Definition buffersrc.c:202
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)
Add a graph described by a string to a graph.
AVFilterContext * avfilter_graph_get_filter(AVFilterGraph *graph, const char *name)
Get a filter instance identified by instance name from graph.
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:499
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:209
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition samplefmt.c:247
#define SRC(x, y)
An AVChannelLayout holds information about the channel layout of audio data.
An instance of a filter.
Definition avfilter.h:279
int nb_threads
Maximum number of threads used by filters in this graph.
Definition avfilter.h:598
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
#define src
Definition vp8dsp.c:248
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition timestamp.h:54
static int64_t pts