FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 {
15 } Stream;
16 
17 static int create_sink(Stream *st, AVFilterGraph *graph,
18  AVFilterContext *f, int idx)
19 {
21  const char *sink_name;
22  int ret;
23 
24  switch (type) {
25  case AVMEDIA_TYPE_VIDEO: sink_name = "buffersink"; break;
26  case AVMEDIA_TYPE_AUDIO: sink_name = "abuffersink"; break;
27  default:
28  av_log(NULL, AV_LOG_ERROR, "Stream type not supported\n");
29  return AVERROR(EINVAL);
30  }
32  avfilter_get_by_name(sink_name),
33  NULL, NULL, NULL, graph);
34  if (ret < 0)
35  return ret;
36  ret = avfilter_link(f, idx, st->sink, 0);
37  if (ret < 0)
38  return ret;
39  st->link = st->sink->inputs[0];
40  return 0;
41 }
42 
43 int main(int argc, char **argv)
44 {
45  char *in_graph_desc, **out_dev_name;
46  int nb_out_dev = 0, nb_streams = 0;
47  AVFilterGraph *in_graph = NULL;
48  Stream *streams = NULL, *st;
49  AVFrame *frame = NULL;
50  int i, j, run = 1, ret;
51 
52  //av_log_set_level(AV_LOG_DEBUG);
53 
54  if (argc < 3) {
56  "Usage: %s filter_graph dev:out [dev2:out2...]\n\n"
57  "Examples:\n"
58  "%s movie=file.nut:s=v+a xv:- alsa:default\n"
59  "%s movie=file.nut:s=v+a uncodedframecrc:pipe:0\n",
60  argv[0], argv[0], argv[0]);
61  exit(1);
62  }
63  in_graph_desc = argv[1];
64  out_dev_name = argv + 2;
65  nb_out_dev = argc - 2;
66 
70 
71  /* Create input graph */
72  if (!(in_graph = avfilter_graph_alloc())) {
73  ret = AVERROR(ENOMEM);
74  av_log(NULL, AV_LOG_ERROR, "Unable to alloc graph graph: %s\n",
75  av_err2str(ret));
76  goto fail;
77  }
78  ret = avfilter_graph_parse_ptr(in_graph, in_graph_desc, NULL, NULL, NULL);
79  if (ret < 0) {
80  av_log(NULL, AV_LOG_ERROR, "Unable to parse graph: %s\n",
81  av_err2str(ret));
82  goto fail;
83  }
84  nb_streams = 0;
85  for (i = 0; i < in_graph->nb_filters; i++) {
86  AVFilterContext *f = in_graph->filters[i];
87  for (j = 0; j < f->nb_inputs; j++) {
88  if (!f->inputs[j]) {
89  av_log(NULL, AV_LOG_ERROR, "Graph has unconnected inputs\n");
90  ret = AVERROR(EINVAL);
91  goto fail;
92  }
93  }
94  for (j = 0; j < f->nb_outputs; j++)
95  if (!f->outputs[j])
96  nb_streams++;
97  }
98  if (!nb_streams) {
99  av_log(NULL, AV_LOG_ERROR, "Graph has no output stream\n");
100  ret = AVERROR(EINVAL);
101  goto fail;
102  }
103  if (nb_out_dev != 1 && nb_out_dev != nb_streams) {
105  "Graph has %d output streams, %d devices given\n",
106  nb_streams, nb_out_dev);
107  ret = AVERROR(EINVAL);
108  goto fail;
109  }
110 
111  if (!(streams = av_calloc(nb_streams, sizeof(*streams)))) {
112  ret = AVERROR(ENOMEM);
113  av_log(NULL, AV_LOG_ERROR, "Could not allocate streams\n");
114  }
115  st = streams;
116  for (i = 0; i < in_graph->nb_filters; i++) {
117  AVFilterContext *f = in_graph->filters[i];
118  for (j = 0; j < f->nb_outputs; j++) {
119  if (!f->outputs[j]) {
120  if ((ret = create_sink(st++, in_graph, f, j)) < 0)
121  goto fail;
122  }
123  }
124  }
125  av_assert0(st - streams == nb_streams);
126  if ((ret = avfilter_graph_config(in_graph, NULL)) < 0) {
127  av_log(NULL, AV_LOG_ERROR, "Failed to configure graph\n");
128  goto fail;
129  }
130 
131  /* Create output devices */
132  for (i = 0; i < nb_out_dev; i++) {
133  char *fmt = NULL, *dev = out_dev_name[i];
134  st = &streams[i];
135  if ((dev = strchr(dev, ':'))) {
136  *(dev++) = 0;
137  fmt = out_dev_name[i];
138  }
139  ret = avformat_alloc_output_context2(&st->mux, NULL, fmt, dev);
140  if (ret < 0) {
141  av_log(NULL, AV_LOG_ERROR, "Failed to allocate output: %s\n",
142  av_err2str(ret));
143  goto fail;
144  }
145  if (!(st->mux->oformat->flags & AVFMT_NOFILE)) {
146  ret = avio_open2(&st->mux->pb, st->mux->filename, AVIO_FLAG_WRITE,
147  NULL, NULL);
148  if (ret < 0) {
149  av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
150  av_err2str(ret));
151  goto fail;
152  }
153  }
154  }
155  for (; i < nb_streams; i++)
156  streams[i].mux = streams[0].mux;
157 
158  /* Create output device streams */
159  for (i = 0; i < nb_streams; i++) {
160  st = &streams[i];
161  if (!(st->stream = avformat_new_stream(st->mux, NULL))) {
162  ret = AVERROR(ENOMEM);
163  av_log(NULL, AV_LOG_ERROR, "Failed to create output stream\n");
164  goto fail;
165  }
166  st->stream->codec->codec_type = st->link->type;
167  st->stream->time_base = st->stream->codec->time_base =
168  st->link->time_base;
169  switch (st->link->type) {
170  case AVMEDIA_TYPE_VIDEO:
171  st->stream->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
172  st->stream->avg_frame_rate =
173  st->stream-> r_frame_rate = av_buffersink_get_frame_rate(st->sink);
174  st->stream->codec->width = st->link->w;
175  st->stream->codec->height = st->link->h;
176  st->stream->codec->sample_aspect_ratio = st->link->sample_aspect_ratio;
177  st->stream->codec->pix_fmt = st->link->format;
178  break;
179  case AVMEDIA_TYPE_AUDIO:
180  st->stream->codec->channel_layout = st->link->channel_layout;
181  st->stream->codec->channels = avfilter_link_get_channels(st->link);
182  st->stream->codec->sample_rate = st->link->sample_rate;
183  st->stream->codec->sample_fmt = st->link->format;
184  st->stream->codec->codec_id =
185  av_get_pcm_codec(st->stream->codec->sample_fmt, -1);
186  break;
187  default:
188  av_assert0(!"reached");
189  }
190  }
191 
192  /* Init output devices */
193  for (i = 0; i < nb_out_dev; i++) {
194  st = &streams[i];
195  if ((ret = avformat_write_header(st->mux, NULL)) < 0) {
196  av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n",
197  av_err2str(ret));
198  goto fail;
199  }
200  }
201 
202  /* Check output devices */
203  for (i = 0; i < nb_streams; i++) {
204  st = &streams[i];
205  ret = av_write_uncoded_frame_query(st->mux, st->stream->index);
206  if (ret < 0) {
207  av_log(st->mux, AV_LOG_ERROR,
208  "Uncoded frames not supported on stream #%d: %s\n",
209  i, av_err2str(ret));
210  goto fail;
211  }
212  }
213 
214  while (run) {
215  ret = avfilter_graph_request_oldest(in_graph);
216  if (ret < 0) {
217  if (ret == AVERROR_EOF) {
218  run = 0;
219  } else {
220  av_log(NULL, AV_LOG_ERROR, "Error filtering: %s\n",
221  av_err2str(ret));
222  break;
223  }
224  }
225  for (i = 0; i < nb_streams; i++) {
226  st = &streams[i];
227  while (1) {
228  if (!frame && !(frame = av_frame_alloc())) {
229  ret = AVERROR(ENOMEM);
230  av_log(NULL, AV_LOG_ERROR, "Could not allocate frame\n");
231  goto fail;
232  }
233  ret = av_buffersink_get_frame_flags(st->sink, frame,
235  if (ret < 0) {
236  if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
237  av_log(NULL, AV_LOG_WARNING, "Error in sink: %s\n",
238  av_err2str(ret));
239  break;
240  }
241  if (frame->pts != AV_NOPTS_VALUE)
242  frame->pts = av_rescale_q(frame->pts,
243  st->link ->time_base,
244  st->stream->time_base);
246  st->stream->index,
247  frame);
248  frame = NULL;
249  if (ret < 0) {
250  av_log(st->stream->codec, AV_LOG_ERROR,
251  "Error writing frame: %s\n", av_err2str(ret));
252  goto fail;
253  }
254  }
255  }
256  }
257  ret = 0;
258 
259  for (i = 0; i < nb_out_dev; i++) {
260  st = &streams[i];
261  av_write_trailer(st->mux);
262  }
263 
264 fail:
265  av_frame_free(&frame);
266  avfilter_graph_free(&in_graph);
267  if (streams) {
268  for (i = 0; i < nb_out_dev; i++) {
269  st = &streams[i];
270  if (st->mux) {
271  if (st->mux->pb)
272  avio_closep(&st->mux->pb);
273  avformat_free_context(st->mux);
274  }
275  }
276  }
277  av_freep(&streams);
278  return ret < 0;
279 }
AVFilterContext ** filters
Definition: avfilter.h:779
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
const char * fmt
Definition: avisynth_c.h:632
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
Definition: avfiltergraph.c:76
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
AVFilterLink * link
Definition: uncoded_frame.c:14
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:1030
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:607
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
void avdevice_register_all(void)
Initialize libavdevice and register all the input and output devices.
Definition: alldevices.c:40
uint8_t run
Definition: svq3.c:192
int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index)
Test whether a muxer supports uncoded frame.
Definition: mux.c:1319
Format I/O context.
Definition: avformat.h:1325
memory buffer sink API for audio and video
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:313
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)
Link two filters together.
Definition: avfilter.c:133
AVFilterPad * output_pads
array of output pads
Definition: avfilter.h:316
static int nb_streams
Definition: ffprobe.c:254
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4065
void avfilter_register_all(void)
Initialize the filter system.
Definition: allfilters.c:40
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.
#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:17
#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
int avfilter_link_get_channels(AVFilterLink *link)
Get the number of channels of a link.
Definition: avfilter.c:178
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
unsigned nb_outputs
number of output pads
Definition: avfilter.h:318
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:128
#define fail()
Definition: checkasm.h:81
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: avfilter.c:519
unsigned nb_inputs
number of input pads
Definition: avfilter.h:314
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:496
#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:876
enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
Return the PCM codec associated with a sample format.
Definition: utils.c:3425
int avfilter_graph_request_oldest(AVFilterGraph *graph)
Request a frame on the oldest sink link.
GLint GLenum type
Definition: opengl_enc.c:105
AVStream * stream
Definition: uncoded_frame.c:12
AVMediaType
Definition: avutil.h:191
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:1051
unsigned nb_filters
Definition: avfilter.h:780
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4001
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
Get the frame rate of the input.
Definition: buffersink.c:271
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
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:1313
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:476
int main(int argc, char **argv)
Definition: uncoded_frame.c:43
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:522
An instance of a filter.
Definition: avfilter.h:305
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1184
AVFormatContext * mux
Definition: uncoded_frame.c:11
#define av_freep(p)
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:44
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:1086
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240