FFmpeg  4.0
uncoded_frame.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "libavutil/avassert.h"
5 #include "libavdevice/avdevice.h"
6 #include "libavfilter/avfilter.h"
8 #include "libavformat/avformat.h"
9 
10 typedef struct {
14 } Stream;
15 
16 static int create_sink(Stream *st, AVFilterGraph *graph,
17  AVFilterContext *f, int idx)
18 {
20  const char *sink_name;
21  int ret;
22 
23  switch (type) {
24  case AVMEDIA_TYPE_VIDEO: sink_name = "buffersink"; break;
25  case AVMEDIA_TYPE_AUDIO: sink_name = "abuffersink"; break;
26  default:
27  av_log(NULL, AV_LOG_ERROR, "Stream type not supported\n");
28  return AVERROR(EINVAL);
29  }
31  avfilter_get_by_name(sink_name),
32  NULL, NULL, NULL, graph);
33  if (ret < 0)
34  return ret;
35  ret = avfilter_link(f, idx, st->sink, 0);
36  if (ret < 0)
37  return ret;
38  return 0;
39 }
40 
41 int main(int argc, char **argv)
42 {
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;
49 
50  //av_log_set_level(AV_LOG_DEBUG);
51 
52  if (argc < 3) {
54  "Usage: %s filter_graph dev:out [dev2:out2...]\n\n"
55  "Examples:\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]);
59  exit(1);
60  }
61  in_graph_desc = argv[1];
62  out_dev_name = argv + 2;
63  nb_out_dev = argc - 2;
64 
66 
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",
71  av_err2str(ret));
72  goto fail;
73  }
74  ret = avfilter_graph_parse_ptr(in_graph, in_graph_desc, NULL, NULL, NULL);
75  if (ret < 0) {
76  av_log(NULL, AV_LOG_ERROR, "Unable to parse graph: %s\n",
77  av_err2str(ret));
78  goto fail;
79  }
80  nb_streams = 0;
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++) {
84  if (!f->inputs[j]) {
85  av_log(NULL, AV_LOG_ERROR, "Graph has unconnected inputs\n");
86  ret = AVERROR(EINVAL);
87  goto fail;
88  }
89  }
90  for (j = 0; j < f->nb_outputs; j++)
91  if (!f->outputs[j])
92  nb_streams++;
93  }
94  if (!nb_streams) {
95  av_log(NULL, AV_LOG_ERROR, "Graph has no output stream\n");
96  ret = AVERROR(EINVAL);
97  goto fail;
98  }
99  if (nb_out_dev != 1 && nb_out_dev != nb_streams) {
101  "Graph has %d output streams, %d devices given\n",
102  nb_streams, nb_out_dev);
103  ret = AVERROR(EINVAL);
104  goto fail;
105  }
106 
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");
110  }
111  st = streams;
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)
117  goto fail;
118  }
119  }
120  }
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");
124  goto fail;
125  }
126 
127  /* Create output devices */
128  for (i = 0; i < nb_out_dev; i++) {
129  char *fmt = NULL, *dev = out_dev_name[i];
130  st = &streams[i];
131  if ((dev = strchr(dev, ':'))) {
132  *(dev++) = 0;
133  fmt = out_dev_name[i];
134  }
135  ret = avformat_alloc_output_context2(&st->mux, NULL, fmt, dev);
136  if (ret < 0) {
137  av_log(NULL, AV_LOG_ERROR, "Failed to allocate output: %s\n",
138  av_err2str(ret));
139  goto fail;
140  }
141  if (!(st->mux->oformat->flags & AVFMT_NOFILE)) {
142  ret = avio_open2(&st->mux->pb, st->mux->url, AVIO_FLAG_WRITE,
143  NULL, NULL);
144  if (ret < 0) {
145  av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
146  av_err2str(ret));
147  goto fail;
148  }
149  }
150  }
151  for (; i < nb_streams; i++)
152  streams[i].mux = streams[0].mux;
153 
154  /* Create output device streams */
155  for (i = 0; i < nb_streams; i++) {
156  st = &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");
160  goto fail;
161  }
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);
173  break;
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);
180  break;
181  default:
182  av_assert0(!"reached");
183  }
184  }
185 
186  /* Init output devices */
187  for (i = 0; i < nb_out_dev; i++) {
188  st = &streams[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",
191  av_err2str(ret));
192  goto fail;
193  }
194  }
195 
196  /* Check output devices */
197  for (i = 0; i < nb_streams; i++) {
198  st = &streams[i];
199  ret = av_write_uncoded_frame_query(st->mux, st->stream->index);
200  if (ret < 0) {
201  av_log(st->mux, AV_LOG_ERROR,
202  "Uncoded frames not supported on stream #%d: %s\n",
203  i, av_err2str(ret));
204  goto fail;
205  }
206  }
207 
208  while (run) {
209  ret = avfilter_graph_request_oldest(in_graph);
210  if (ret < 0) {
211  if (ret == AVERROR_EOF) {
212  run = 0;
213  } else {
214  av_log(NULL, AV_LOG_ERROR, "Error filtering: %s\n",
215  av_err2str(ret));
216  break;
217  }
218  }
219  for (i = 0; i < nb_streams; i++) {
220  st = &streams[i];
221  while (1) {
222  if (!frame && !(frame = av_frame_alloc())) {
223  ret = AVERROR(ENOMEM);
224  av_log(NULL, AV_LOG_ERROR, "Could not allocate frame\n");
225  goto fail;
226  }
227  ret = av_buffersink_get_frame_flags(st->sink, frame,
229  if (ret < 0) {
230  if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
231  av_log(NULL, AV_LOG_WARNING, "Error in sink: %s\n",
232  av_err2str(ret));
233  break;
234  }
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);
240  st->stream->index,
241  frame);
242  frame = NULL;
243  if (ret < 0) {
244  av_log(st->mux, AV_LOG_ERROR,
245  "Error writing frame: %s\n", av_err2str(ret));
246  goto fail;
247  }
248  }
249  }
250  }
251  ret = 0;
252 
253  for (i = 0; i < nb_out_dev; i++) {
254  st = &streams[i];
255  av_write_trailer(st->mux);
256  }
257 
258 fail:
259  av_frame_free(&frame);
260  avfilter_graph_free(&in_graph);
261  if (streams) {
262  for (i = 0; i < nb_out_dev; i++) {
263  st = &streams[i];
264  if (st->mux) {
265  if (st->mux->pb)
266  avio_closep(&st->mux->pb);
267  avformat_free_context(st->mux);
268  }
269  }
270  }
271  av_freep(&streams);
272  return ret < 0;
273 }
AVFilterContext ** filters
Definition: avfilter.h:842
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
const char * fmt
Definition: avisynth_c.h:769
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
Definition: avfiltergraph.c:83
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
Get the type of an AVFilterPad.
Definition: avfilter.c:1039
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:655
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
uint8_t run
Definition: svq3.c:206
int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index)
Test whether a muxer supports uncoded frame.
Definition: mux.c:1379
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
Format I/O context.
Definition: avformat.h:1342
memory buffer sink API for audio and video
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int av_buffersink_get_sample_rate(const AVFilterContext *ctx)
int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)
Link two filters together.
Definition: avfilter.c:135
AVFilterPad * output_pads
array of output pads
Definition: avfilter.h:349
static int nb_streams
Definition: ffprobe.c:276
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4441
static AVFrame * frame
int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt, const char *name, const char *args, void *opaque, AVFilterGraph *graph_ctx)
Create and add a filter instance into an existing graph.
AVRational av_buffersink_get_frame_rate(const AVFilterContext *ctx)
#define AVERROR_EOF
End of file.
Definition: error.h:55
static int create_sink(Stream *st, AVFilterGraph *graph, AVFilterContext *f, int idx)
Definition: uncoded_frame.c:16
int av_buffersink_get_h(const AVFilterContext *ctx)
int av_buffersink_get_format(const AVFilterContext *ctx)
#define av_log(a,...)
#define AV_BUFFERSINK_FLAG_NO_REQUEST
Tell av_buffersink_get_buffer_ref() not to request a frame from its input.
Definition: buffersink.h:60
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:148
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
Main libavdevice API header.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
unsigned nb_outputs
number of output pads
Definition: avfilter.h:351
simple assert() macros that are a bit more flexible than ISO C assert().
int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
Get a frame with filtered data from sink and put it in frame.
Definition: buffersink.c:119
#define fail()
Definition: checkasm.h:116
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: allfilters.c:428
unsigned nb_inputs
number of input pads
Definition: avfilter.h:347
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:508
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFilterContext * sink
Definition: uncoded_frame.c:13
Stream structure.
Definition: avformat.h:873
AVRational av_buffersink_get_sample_aspect_ratio(const AVFilterContext *ctx)
enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
Return the PCM codec associated with a sample format.
Definition: utils.c:1473
uint64_t av_buffersink_get_channel_layout(const AVFilterContext *ctx)
int avfilter_graph_request_oldest(AVFilterGraph *graph)
Request a frame on the oldest sink link.
AVStream * stream
Definition: uncoded_frame.c:12
enum AVMediaType av_buffersink_get_type(const AVFilterContext *ctx)
cl_device_type type
AVMediaType
Definition: avutil.h:199
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1178
int av_buffersink_get_w(const AVFilterContext *ctx)
unsigned nb_filters
Definition: avfilter.h:843
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4375
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
void avdevice_register_all(void)
Initialize libavdevice and register all the input and output devices.
Definition: alldevices.c:67
Main libavformat public API header.
int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index, AVFrame *frame)
Write an uncoded frame to an output media file.
Definition: mux.c:1373
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
int main(int argc, char **argv)
Definition: uncoded_frame.c:41
int av_buffersink_get_channels(const AVFilterContext *ctx)
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.
Definition: graphparser.c:538
An instance of a filter.
Definition: avfilter.h:338
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1247
AVFormatContext * mux
Definition: uncoded_frame.c:11
#define av_freep(p)
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:1215
AVRational av_buffersink_get_time_base(const AVFilterContext *ctx)
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248