FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
transcoding.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Nicolas George
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2014 Andrey Utkin
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /**
26  * @file
27  * API example for demuxing, decoding, filtering, encoding and muxing
28  * @example transcoding.c
29  */
30 
31 #include <libavcodec/avcodec.h>
32 #include <libavformat/avformat.h>
34 #include <libavfilter/avcodec.h>
35 #include <libavfilter/buffersink.h>
36 #include <libavfilter/buffersrc.h>
37 #include <libavutil/opt.h>
38 #include <libavutil/pixdesc.h>
39 
42 typedef struct FilteringContext {
48 
49 static int open_input_file(const char *filename)
50 {
51  int ret;
52  unsigned int i;
53 
54  ifmt_ctx = NULL;
55  if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
56  av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
57  return ret;
58  }
59 
60  if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
61  av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
62  return ret;
63  }
64 
65  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
66  AVStream *stream;
67  AVCodecContext *codec_ctx;
68  stream = ifmt_ctx->streams[i];
69  codec_ctx = stream->codec;
70  /* Reencode video & audio and remux subtitles etc. */
71  if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
72  || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
73  /* Open decoder */
74  ret = avcodec_open2(codec_ctx,
75  avcodec_find_decoder(codec_ctx->codec_id), NULL);
76  if (ret < 0) {
77  av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
78  return ret;
79  }
80  }
81  }
82 
83  av_dump_format(ifmt_ctx, 0, filename, 0);
84  return 0;
85 }
86 
87 static int open_output_file(const char *filename)
88 {
89  AVStream *out_stream;
90  AVStream *in_stream;
91  AVCodecContext *dec_ctx, *enc_ctx;
92  AVCodec *encoder;
93  int ret;
94  unsigned int i;
95 
96  ofmt_ctx = NULL;
97  avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
98  if (!ofmt_ctx) {
99  av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
100  return AVERROR_UNKNOWN;
101  }
102 
103 
104  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
105  out_stream = avformat_new_stream(ofmt_ctx, NULL);
106  if (!out_stream) {
107  av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
108  return AVERROR_UNKNOWN;
109  }
110 
111  in_stream = ifmt_ctx->streams[i];
112  dec_ctx = in_stream->codec;
113  enc_ctx = out_stream->codec;
114 
115  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
116  || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
117  /* in this example, we choose transcoding to same codec */
118  encoder = avcodec_find_encoder(dec_ctx->codec_id);
119  if (!encoder) {
120  av_log(NULL, AV_LOG_FATAL, "Neccessary encoder not found\n");
121  return AVERROR_INVALIDDATA;
122  }
123 
124  /* In this example, we transcode to same properties (picture size,
125  * sample rate etc.). These properties can be changed for output
126  * streams easily using filters */
127  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
128  enc_ctx->height = dec_ctx->height;
129  enc_ctx->width = dec_ctx->width;
130  enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
131  /* take first format from list of supported formats */
132  enc_ctx->pix_fmt = encoder->pix_fmts[0];
133  /* video time_base can be set to whatever is handy and supported by encoder */
134  enc_ctx->time_base = dec_ctx->time_base;
135  } else {
136  enc_ctx->sample_rate = dec_ctx->sample_rate;
137  enc_ctx->channel_layout = dec_ctx->channel_layout;
139  /* take first format from list of supported formats */
140  enc_ctx->sample_fmt = encoder->sample_fmts[0];
141  enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
142  }
143 
144  /* Third parameter can be used to pass settings to encoder */
145  ret = avcodec_open2(enc_ctx, encoder, NULL);
146  if (ret < 0) {
147  av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
148  return ret;
149  }
150  } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
151  av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
152  return AVERROR_INVALIDDATA;
153  } else {
154  /* if this stream must be remuxed */
155  ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,
156  ifmt_ctx->streams[i]->codec);
157  if (ret < 0) {
158  av_log(NULL, AV_LOG_ERROR, "Copying stream context failed\n");
159  return ret;
160  }
161  }
162 
163  if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
164  enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
165 
166  }
167  av_dump_format(ofmt_ctx, 0, filename, 1);
168 
169  if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
170  ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
171  if (ret < 0) {
172  av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
173  return ret;
174  }
175  }
176 
177  /* init muxer, write output file header */
178  ret = avformat_write_header(ofmt_ctx, NULL);
179  if (ret < 0) {
180  av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
181  return ret;
182  }
183 
184  return 0;
185 }
186 
188  AVCodecContext *enc_ctx, const char *filter_spec)
189 {
190  char args[512];
191  int ret = 0;
192  AVFilter *buffersrc = NULL;
193  AVFilter *buffersink = NULL;
199 
200  if (!outputs || !inputs || !filter_graph) {
201  ret = AVERROR(ENOMEM);
202  goto end;
203  }
204 
205  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
206  buffersrc = avfilter_get_by_name("buffer");
207  buffersink = avfilter_get_by_name("buffersink");
208  if (!buffersrc || !buffersink) {
209  av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
210  ret = AVERROR_UNKNOWN;
211  goto end;
212  }
213 
214  snprintf(args, sizeof(args),
215  "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
216  dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
217  dec_ctx->time_base.num, dec_ctx->time_base.den,
218  dec_ctx->sample_aspect_ratio.num,
219  dec_ctx->sample_aspect_ratio.den);
220 
221  ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
222  args, NULL, filter_graph);
223  if (ret < 0) {
224  av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
225  goto end;
226  }
227 
228  ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
229  NULL, NULL, filter_graph);
230  if (ret < 0) {
231  av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
232  goto end;
233  }
234 
235  ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
236  (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
238  if (ret < 0) {
239  av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
240  goto end;
241  }
242  } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
243  buffersrc = avfilter_get_by_name("abuffer");
244  buffersink = avfilter_get_by_name("abuffersink");
245  if (!buffersrc || !buffersink) {
246  av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
247  ret = AVERROR_UNKNOWN;
248  goto end;
249  }
250 
251  if (!dec_ctx->channel_layout)
252  dec_ctx->channel_layout =
254  snprintf(args, sizeof(args),
255  "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
256  dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
258  dec_ctx->channel_layout);
259  ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
260  args, NULL, filter_graph);
261  if (ret < 0) {
262  av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
263  goto end;
264  }
265 
266  ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
267  NULL, NULL, filter_graph);
268  if (ret < 0) {
269  av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
270  goto end;
271  }
272 
273  ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
274  (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
276  if (ret < 0) {
277  av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
278  goto end;
279  }
280 
281  ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
282  (uint8_t*)&enc_ctx->channel_layout,
283  sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
284  if (ret < 0) {
285  av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
286  goto end;
287  }
288 
289  ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
290  (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
292  if (ret < 0) {
293  av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
294  goto end;
295  }
296  } else {
297  ret = AVERROR_UNKNOWN;
298  goto end;
299  }
300 
301  /* Endpoints for the filter graph. */
302  outputs->name = av_strdup("in");
303  outputs->filter_ctx = buffersrc_ctx;
304  outputs->pad_idx = 0;
305  outputs->next = NULL;
306 
307  inputs->name = av_strdup("out");
308  inputs->filter_ctx = buffersink_ctx;
309  inputs->pad_idx = 0;
310  inputs->next = NULL;
311 
312  if (!outputs->name || !inputs->name) {
313  ret = AVERROR(ENOMEM);
314  goto end;
315  }
316 
317  if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
318  &inputs, &outputs, NULL)) < 0)
319  goto end;
320 
321  if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
322  goto end;
323 
324  /* Fill FilteringContext */
327  fctx->filter_graph = filter_graph;
328 
329 end:
330  avfilter_inout_free(&inputs);
331  avfilter_inout_free(&outputs);
332 
333  return ret;
334 }
335 
336 static int init_filters(void)
337 {
338  const char *filter_spec;
339  unsigned int i;
340  int ret;
341  filter_ctx = av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
342  if (!filter_ctx)
343  return AVERROR(ENOMEM);
344 
345  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
346  filter_ctx[i].buffersrc_ctx = NULL;
347  filter_ctx[i].buffersink_ctx = NULL;
348  filter_ctx[i].filter_graph = NULL;
349  if (!(ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO
350  || ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO))
351  continue;
352 
353 
354  if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
355  filter_spec = "null"; /* passthrough (dummy) filter for video */
356  else
357  filter_spec = "anull"; /* passthrough (dummy) filter for audio */
358  ret = init_filter(&filter_ctx[i], ifmt_ctx->streams[i]->codec,
359  ofmt_ctx->streams[i]->codec, filter_spec);
360  if (ret)
361  return ret;
362  }
363  return 0;
364 }
365 
366 static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
367  int ret;
368  int got_frame_local;
369  AVPacket enc_pkt;
370  int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
371  (ifmt_ctx->streams[stream_index]->codec->codec_type ==
373 
374  if (!got_frame)
375  got_frame = &got_frame_local;
376 
377  av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
378  /* encode filtered frame */
379  enc_pkt.data = NULL;
380  enc_pkt.size = 0;
381  av_init_packet(&enc_pkt);
382  ret = enc_func(ofmt_ctx->streams[stream_index]->codec, &enc_pkt,
383  filt_frame, got_frame);
384  av_frame_free(&filt_frame);
385  if (ret < 0)
386  return ret;
387  if (!(*got_frame))
388  return 0;
389 
390  /* prepare packet for muxing */
391  enc_pkt.stream_index = stream_index;
392  av_packet_rescale_ts(&enc_pkt,
393  ofmt_ctx->streams[stream_index]->codec->time_base,
394  ofmt_ctx->streams[stream_index]->time_base);
395 
396  av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
397  /* mux encoded frame */
398  ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
399  return ret;
400 }
401 
402 static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
403 {
404  int ret;
405  AVFrame *filt_frame;
406 
407  av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
408  /* push the decoded frame into the filtergraph */
409  ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
410  frame, 0);
411  if (ret < 0) {
412  av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
413  return ret;
414  }
415 
416  /* pull filtered frames from the filtergraph */
417  while (1) {
418  filt_frame = av_frame_alloc();
419  if (!filt_frame) {
420  ret = AVERROR(ENOMEM);
421  break;
422  }
423  av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
424  ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
425  filt_frame);
426  if (ret < 0) {
427  /* if no more frames for output - returns AVERROR(EAGAIN)
428  * if flushed and no more frames for output - returns AVERROR_EOF
429  * rewrite retcode to 0 to show it as normal procedure completion
430  */
431  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
432  ret = 0;
433  av_frame_free(&filt_frame);
434  break;
435  }
436 
437  filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
438  ret = encode_write_frame(filt_frame, stream_index, NULL);
439  if (ret < 0)
440  break;
441  }
442 
443  return ret;
444 }
445 
446 static int flush_encoder(unsigned int stream_index)
447 {
448  int ret;
449  int got_frame;
450 
451  if (!(ofmt_ctx->streams[stream_index]->codec->codec->capabilities &
453  return 0;
454 
455  while (1) {
456  av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
457  ret = encode_write_frame(NULL, stream_index, &got_frame);
458  if (ret < 0)
459  break;
460  if (!got_frame)
461  return 0;
462  }
463  return ret;
464 }
465 
466 int main(int argc, char **argv)
467 {
468  int ret;
469  AVPacket packet = { .data = NULL, .size = 0 };
470  AVFrame *frame = NULL;
471  enum AVMediaType type;
472  unsigned int stream_index;
473  unsigned int i;
474  int got_frame;
475  int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
476 
477  if (argc != 3) {
478  av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
479  return 1;
480  }
481 
482  av_register_all();
484 
485  if ((ret = open_input_file(argv[1])) < 0)
486  goto end;
487  if ((ret = open_output_file(argv[2])) < 0)
488  goto end;
489  if ((ret = init_filters()) < 0)
490  goto end;
491 
492  /* read all packets */
493  while (1) {
494  if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
495  break;
496  stream_index = packet.stream_index;
497  type = ifmt_ctx->streams[packet.stream_index]->codec->codec_type;
498  av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
499  stream_index);
500 
501  if (filter_ctx[stream_index].filter_graph) {
502  av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
503  frame = av_frame_alloc();
504  if (!frame) {
505  ret = AVERROR(ENOMEM);
506  break;
507  }
508  av_packet_rescale_ts(&packet,
509  ifmt_ctx->streams[stream_index]->time_base,
510  ifmt_ctx->streams[stream_index]->codec->time_base);
511  dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
513  ret = dec_func(ifmt_ctx->streams[stream_index]->codec, frame,
514  &got_frame, &packet);
515  if (ret < 0) {
516  av_frame_free(&frame);
517  av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
518  break;
519  }
520 
521  if (got_frame) {
522  frame->pts = av_frame_get_best_effort_timestamp(frame);
523  ret = filter_encode_write_frame(frame, stream_index);
524  av_frame_free(&frame);
525  if (ret < 0)
526  goto end;
527  } else {
528  av_frame_free(&frame);
529  }
530  } else {
531  /* remux this frame without reencoding */
532  av_packet_rescale_ts(&packet,
533  ifmt_ctx->streams[stream_index]->time_base,
534  ofmt_ctx->streams[stream_index]->time_base);
535 
536  ret = av_interleaved_write_frame(ofmt_ctx, &packet);
537  if (ret < 0)
538  goto end;
539  }
540  av_free_packet(&packet);
541  }
542 
543  /* flush filters and encoders */
544  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
545  /* flush filter */
546  if (!filter_ctx[i].filter_graph)
547  continue;
549  if (ret < 0) {
550  av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
551  goto end;
552  }
553 
554  /* flush encoder */
555  ret = flush_encoder(i);
556  if (ret < 0) {
557  av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
558  goto end;
559  }
560  }
561 
562  av_write_trailer(ofmt_ctx);
563 end:
564  av_free_packet(&packet);
565  av_frame_free(&frame);
566  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
567  avcodec_close(ifmt_ctx->streams[i]->codec);
568  if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && ofmt_ctx->streams[i]->codec)
569  avcodec_close(ofmt_ctx->streams[i]->codec);
570  if (filter_ctx && filter_ctx[i].filter_graph)
571  avfilter_graph_free(&filter_ctx[i].filter_graph);
572  }
573  av_free(filter_ctx);
574  avformat_close_input(&ifmt_ctx);
575  if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
576  avio_closep(&ofmt_ctx->pb);
577  avformat_free_context(ofmt_ctx);
578 
579  if (ret < 0)
580  av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
581 
582  return ret ? 1 : 0;
583 }
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:909
AVFilterGraph * filter_graph
Definition: transcoding.c:45
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1250
AVFilterContext * buffersink_ctx
static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame)
Definition: transcoding.c:366
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:2932
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:913
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:428
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
Definition: avfiltergraph.c:76
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:402
Memory buffer source API.
AVFilterGraph * filter_graph
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
void avfilter_inout_free(AVFilterInOut **inout)
Free the supplied list of AVFilterInOut and set *inout to NULL.
Definition: graphparser.c:184
struct AVFilterInOut * next
next input/input in the list, NULL if this is the last
Definition: avfilter.h:1362
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1163
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:461
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1623
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
Definition: opt.c:506
int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
Definition: utils.c:1837
AVCodec.
Definition: avcodec.h:3181
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:180
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1369
Undefined.
Definition: avutil.h:266
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Format I/O context.
Definition: avformat.h:1272
static int open_input_file(const char *filename)
Definition: transcoding.c:49
memory buffer sink API for audio and video
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1993
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT
Definition: avformat.h:532
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
AVOptions.
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:758
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
libavcodec/libavfilter gluing utilities
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3672
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1340
int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
Definition: utils.c:2101
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.
int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
Add a frame to the buffer source.
Definition: buffersrc.c:94
uint8_t * data
Definition: avcodec.h:1162
#define AVERROR_EOF
End of file.
Definition: error.h:55
int64_t av_frame_get_best_effort_timestamp(const AVFrame *frame)
Accessors for some AVFrame fields.
#define av_log(a,...)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1291
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
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
Print detailed information about the input or output format, such as duration, bitrate, streams, container, programs, metadata, side data, codec and time base.
Definition: dump.c:477
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:2843
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int flush_encoder(unsigned int stream_index)
Definition: transcoding.c:446
static AVFormatContext * ifmt_ctx
Definition: transcoding.c:40
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
Definition: utils.c:2352
void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...
Definition: avpacket.c:594
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:824
#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:148
int capabilities
Codec capabilities.
Definition: avcodec.h:3200
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1335
AVFilterContext * buffersrc_ctx
static FilteringContext * filter_ctx
Definition: transcoding.c:47
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:47
Libavcodec external API header.
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2046
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: avfilter.c:487
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1328
AVFilterContext * buffersrc_ctx
Definition: transcoding.c:44
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3202
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
static int init_filter(FilteringContext *fctx, AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, const char *filter_spec)
Definition: transcoding.c:187
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:602
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:471
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, const AVPacket *avpkt)
Decode the audio frame of size avpkt->size from avpkt->data into frame.
Definition: utils.c:2498
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:193
AVFilterContext * filter_ctx
filter context associated to this input/output
Definition: avfilter.h:1356
static int init_filters(void)
Definition: transcoding.c:336
Stream structure.
Definition: avformat.h:842
A linked-list of the inputs/outputs of the filter chain.
Definition: avfilter.h:1351
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
enum AVMediaType codec_type
Definition: avcodec.h:1249
enum AVCodecID codec_id
Definition: avcodec.h:1258
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:265
int sample_rate
samples per second
Definition: avcodec.h:1985
AVIOContext * pb
I/O context.
Definition: avformat.h:1314
main external API structure.
Definition: avcodec.h:1241
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:2951
GLint GLenum type
Definition: opengl_enc.c:105
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
int pad_idx
index of the filt_ctx pad to use for linking
Definition: avfilter.h:1359
rational number numerator/denominator
Definition: rational.h:43
static int open_output_file(const char *filename)
Definition: transcoding.c:87
AVMediaType
Definition: avutil.h:192
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1330
#define snprintf
Definition: snprintf.h:34
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3609
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1475
char * name
unique name for this input/output in the list
Definition: avfilter.h:1353
Main libavformat public API header.
AVFilterInOut * avfilter_inout_alloc(void)
Allocate a single AVFilterInOut entry.
Definition: graphparser.c:179
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:465
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3024
static AVCodecContext * dec_ctx
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
int den
denominator
Definition: rational.h:45
static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
Definition: transcoding.c:402
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:3644
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:1986
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:527
An instance of a filter.
Definition: avfilter.h:633
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:967
int main(int argc, char **argv)
Definition: transcoding.c:466
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
#define av_malloc_array(a, b)
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3204
AVFilterContext * buffersink_ctx
Definition: transcoding.c:43
int stream_index
Definition: avcodec.h:1164
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:884
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:121
This structure stores compressed data.
Definition: avcodec.h:1139
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:51
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:955
static AVFormatContext * ofmt_ctx
Definition: transcoding.c:41