4 #include "libavutil/avassert.h"
5 #include "libavdevice/avdevice.h"
6 #include "libavfilter/avfilter.h"
7 #include "libavfilter/buffersink.h"
8 #include "libavformat/avformat.h"
13 AVFilterContext *sink;
16 static int create_sink(Stream *st, AVFilterGraph *graph,
17 AVFilterContext *f, int idx)
19 enum AVMediaType type = avfilter_pad_get_type(f->output_pads, idx);
20 const char *sink_name;
24 case AVMEDIA_TYPE_VIDEO: sink_name = "buffersink"; break;
25 case AVMEDIA_TYPE_AUDIO: sink_name = "abuffersink"; break;
27 av_log(NULL, AV_LOG_ERROR, "Stream type not supported\n");
28 return AVERROR(EINVAL);
30 ret = avfilter_graph_create_filter(&st->sink,
31 avfilter_get_by_name(sink_name),
32 NULL, NULL, NULL, graph);
35 ret = avfilter_link(f, idx, st->sink, 0);
41 int main(int argc, char **argv)
43 char *in_graph_desc, **out_dev_name;
44 int nb_out_dev = 0, nb_streams = 0;
45 AVFilterGraph *in_graph = NULL;
46 Stream *streams = NULL, *st;
47 AVFrame *frame = NULL;
48 int i, j, run = 1, ret;
50 //av_log_set_level(AV_LOG_DEBUG);
53 av_log(NULL, AV_LOG_ERROR,
54 "Usage: %s filter_graph dev:out [dev2:out2...]\n\n"
56 "%s movie=file.nut:s=v+a xv:- alsa:default\n"
57 "%s movie=file.nut:s=v+a uncodedframecrc:pipe:0\n",
58 argv[0], argv[0], argv[0]);
61 in_graph_desc = argv[1];
62 out_dev_name = argv + 2;
63 nb_out_dev = argc - 2;
65 avdevice_register_all();
67 /* Create input graph */
68 if (!(in_graph = avfilter_graph_alloc())) {
69 ret = AVERROR(ENOMEM);
70 av_log(NULL, AV_LOG_ERROR, "Unable to alloc graph graph: %s\n",
74 ret = avfilter_graph_parse_ptr(in_graph, in_graph_desc, NULL, NULL, NULL);
76 av_log(NULL, AV_LOG_ERROR, "Unable to parse graph: %s\n",
81 for (i = 0; i < in_graph->nb_filters; i++) {
82 AVFilterContext *f = in_graph->filters[i];
83 for (j = 0; j < f->nb_inputs; j++) {
85 av_log(NULL, AV_LOG_ERROR, "Graph has unconnected inputs\n");
86 ret = AVERROR(EINVAL);
90 for (j = 0; j < f->nb_outputs; j++)
95 av_log(NULL, AV_LOG_ERROR, "Graph has no output stream\n");
96 ret = AVERROR(EINVAL);
99 if (nb_out_dev != 1 && nb_out_dev != nb_streams) {
100 av_log(NULL, AV_LOG_ERROR,
101 "Graph has %d output streams, %d devices given\n",
102 nb_streams, nb_out_dev);
103 ret = AVERROR(EINVAL);
107 if (!(streams = av_calloc(nb_streams, sizeof(*streams)))) {
108 ret = AVERROR(ENOMEM);
109 av_log(NULL, AV_LOG_ERROR, "Could not allocate streams\n");
112 for (i = 0; i < in_graph->nb_filters; i++) {
113 AVFilterContext *f = in_graph->filters[i];
114 for (j = 0; j < f->nb_outputs; j++) {
115 if (!f->outputs[j]) {
116 if ((ret = create_sink(st++, in_graph, f, j)) < 0)
121 av_assert0(st - streams == nb_streams);
122 if ((ret = avfilter_graph_config(in_graph, NULL)) < 0) {
123 av_log(NULL, AV_LOG_ERROR, "Failed to configure graph\n");
127 /* Create output devices */
128 for (i = 0; i < nb_out_dev; i++) {
129 char *fmt = NULL, *dev = out_dev_name[i];
131 if ((dev = strchr(dev, ':'))) {
133 fmt = out_dev_name[i];
135 ret = avformat_alloc_output_context2(&st->mux, NULL, fmt, dev);
137 av_log(NULL, AV_LOG_ERROR, "Failed to allocate output: %s\n",
141 if (!(st->mux->oformat->flags & AVFMT_NOFILE)) {
142 ret = avio_open2(&st->mux->pb, st->mux->url, AVIO_FLAG_WRITE,
145 av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
151 for (; i < nb_streams; i++)
152 streams[i].mux = streams[0].mux;
154 /* Create output device streams */
155 for (i = 0; i < nb_streams; i++) {
157 if (!(st->stream = avformat_new_stream(st->mux, NULL))) {
158 ret = AVERROR(ENOMEM);
159 av_log(NULL, AV_LOG_ERROR, "Failed to create output stream\n");
162 st->stream->codecpar->codec_type = av_buffersink_get_type(st->sink);
163 st->stream->time_base = av_buffersink_get_time_base(st->sink);
164 switch (av_buffersink_get_type(st->sink)) {
165 case AVMEDIA_TYPE_VIDEO:
166 st->stream->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
167 st->stream->avg_frame_rate =
168 st->stream-> r_frame_rate = av_buffersink_get_frame_rate(st->sink);
169 st->stream->codecpar->width = av_buffersink_get_w(st->sink);
170 st->stream->codecpar->height = av_buffersink_get_h(st->sink);
171 st->stream->codecpar->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(st->sink);
172 st->stream->codecpar->format = av_buffersink_get_format(st->sink);
174 case AVMEDIA_TYPE_AUDIO:
175 st->stream->codecpar->channel_layout = av_buffersink_get_channel_layout(st->sink);
176 st->stream->codecpar->channels = av_buffersink_get_channels(st->sink);
177 st->stream->codecpar->sample_rate = av_buffersink_get_sample_rate(st->sink);
178 st->stream->codecpar->format = av_buffersink_get_format(st->sink);
179 st->stream->codecpar->codec_id = av_get_pcm_codec(st->stream->codecpar->format, -1);
182 av_assert0(!"reached");
186 /* Init output devices */
187 for (i = 0; i < nb_out_dev; i++) {
189 if ((ret = avformat_write_header(st->mux, NULL)) < 0) {
190 av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
196 /* Check output devices */
197 for (i = 0; i < nb_streams; i++) {
199 ret = av_write_uncoded_frame_query(st->mux, st->stream->index);
201 av_log(st->mux, AV_LOG_ERROR,
202 "Uncoded frames not supported on stream #%d: %s\n",
209 ret = avfilter_graph_request_oldest(in_graph);
211 if (ret == AVERROR_EOF) {
214 av_log(NULL, AV_LOG_ERROR, "Error filtering: %s\n",
219 for (i = 0; i < nb_streams; i++) {
222 if (!frame && !(frame = av_frame_alloc())) {
223 ret = AVERROR(ENOMEM);
224 av_log(NULL, AV_LOG_ERROR, "Could not allocate frame\n");
227 ret = av_buffersink_get_frame_flags(st->sink, frame,
228 AV_BUFFERSINK_FLAG_NO_REQUEST);
230 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
231 av_log(NULL, AV_LOG_WARNING, "Error in sink: %s\n",
235 if (frame->pts != AV_NOPTS_VALUE)
236 frame->pts = av_rescale_q(frame->pts,
237 av_buffersink_get_time_base(st->sink),
238 st->stream->time_base);
239 ret = av_interleaved_write_uncoded_frame(st->mux,
244 av_log(st->mux, AV_LOG_ERROR,
245 "Error writing frame: %s\n", av_err2str(ret));
253 for (i = 0; i < nb_out_dev; i++) {
255 av_write_trailer(st->mux);
259 av_frame_free(&frame);
260 avfilter_graph_free(&in_graph);
262 for (i = 0; i < nb_out_dev; i++) {
266 avio_closep(&st->mux->pb);
267 avformat_free_context(st->mux);