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>
33 #include <libavfilter/buffersink.h>
34 #include <libavfilter/buffersrc.h>
35 #include <libavutil/opt.h>
36 #include <libavutil/pixdesc.h>
37 
40 typedef struct FilteringContext {
46 
47 typedef struct StreamContext {
52 
53 static int open_input_file(const char *filename)
54 {
55  int ret;
56  unsigned int i;
57 
58  ifmt_ctx = NULL;
59  if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
60  av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
61  return ret;
62  }
63 
64  if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
65  av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
66  return ret;
67  }
68 
69  stream_ctx = av_mallocz_array(ifmt_ctx->nb_streams, sizeof(*stream_ctx));
70  if (!stream_ctx)
71  return AVERROR(ENOMEM);
72 
73  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
74  AVStream *stream = ifmt_ctx->streams[i];
76  AVCodecContext *codec_ctx;
77  if (!dec) {
78  av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i);
80  }
81  codec_ctx = avcodec_alloc_context3(dec);
82  if (!codec_ctx) {
83  av_log(NULL, AV_LOG_ERROR, "Failed to allocate the decoder context for stream #%u\n", i);
84  return AVERROR(ENOMEM);
85  }
86  ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar);
87  if (ret < 0) {
88  av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context "
89  "for stream #%u\n", i);
90  return ret;
91  }
92  /* Reencode video & audio and remux subtitles etc. */
93  if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
94  || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
95  if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
96  codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, stream, NULL);
97  /* Open decoder */
98  ret = avcodec_open2(codec_ctx, dec, NULL);
99  if (ret < 0) {
100  av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
101  return ret;
102  }
103  }
104  stream_ctx[i].dec_ctx = codec_ctx;
105  }
106 
107  av_dump_format(ifmt_ctx, 0, filename, 0);
108  return 0;
109 }
110 
111 static int open_output_file(const char *filename)
112 {
113  AVStream *out_stream;
114  AVStream *in_stream;
115  AVCodecContext *dec_ctx, *enc_ctx;
116  AVCodec *encoder;
117  int ret;
118  unsigned int i;
119 
120  ofmt_ctx = NULL;
121  avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
122  if (!ofmt_ctx) {
123  av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
124  return AVERROR_UNKNOWN;
125  }
126 
127 
128  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
129  out_stream = avformat_new_stream(ofmt_ctx, NULL);
130  if (!out_stream) {
131  av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
132  return AVERROR_UNKNOWN;
133  }
134 
135  in_stream = ifmt_ctx->streams[i];
136  dec_ctx = stream_ctx[i].dec_ctx;
137 
138  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
139  || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
140  /* in this example, we choose transcoding to same codec */
141  encoder = avcodec_find_encoder(dec_ctx->codec_id);
142  if (!encoder) {
143  av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
144  return AVERROR_INVALIDDATA;
145  }
146  enc_ctx = avcodec_alloc_context3(encoder);
147  if (!enc_ctx) {
148  av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
149  return AVERROR(ENOMEM);
150  }
151 
152  /* In this example, we transcode to same properties (picture size,
153  * sample rate etc.). These properties can be changed for output
154  * streams easily using filters */
155  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
156  enc_ctx->height = dec_ctx->height;
157  enc_ctx->width = dec_ctx->width;
158  enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
159  /* take first format from list of supported formats */
160  if (encoder->pix_fmts)
161  enc_ctx->pix_fmt = encoder->pix_fmts[0];
162  else
163  enc_ctx->pix_fmt = dec_ctx->pix_fmt;
164  /* video time_base can be set to whatever is handy and supported by encoder */
165  enc_ctx->time_base = av_inv_q(dec_ctx->framerate);
166  } else {
167  enc_ctx->sample_rate = dec_ctx->sample_rate;
168  enc_ctx->channel_layout = dec_ctx->channel_layout;
170  /* take first format from list of supported formats */
171  enc_ctx->sample_fmt = encoder->sample_fmts[0];
172  enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
173  }
174 
175  /* Third parameter can be used to pass settings to encoder */
176  ret = avcodec_open2(enc_ctx, encoder, NULL);
177  if (ret < 0) {
178  av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
179  return ret;
180  }
181  ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
182  if (ret < 0) {
183  av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
184  return ret;
185  }
186  if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
188 
189  out_stream->time_base = enc_ctx->time_base;
190  stream_ctx[i].enc_ctx = enc_ctx;
191  } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
192  av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
193  return AVERROR_INVALIDDATA;
194  } else {
195  /* if this stream must be remuxed */
196  ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
197  if (ret < 0) {
198  av_log(NULL, AV_LOG_ERROR, "Copying parameters for stream #%u failed\n", i);
199  return ret;
200  }
201  out_stream->time_base = in_stream->time_base;
202  }
203 
204  }
205  av_dump_format(ofmt_ctx, 0, filename, 1);
206 
207  if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
208  ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
209  if (ret < 0) {
210  av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
211  return ret;
212  }
213  }
214 
215  /* init muxer, write output file header */
216  ret = avformat_write_header(ofmt_ctx, NULL);
217  if (ret < 0) {
218  av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
219  return ret;
220  }
221 
222  return 0;
223 }
224 
226  AVCodecContext *enc_ctx, const char *filter_spec)
227 {
228  char args[512];
229  int ret = 0;
230  const AVFilter *buffersrc = NULL;
231  const AVFilter *buffersink = NULL;
237 
238  if (!outputs || !inputs || !filter_graph) {
239  ret = AVERROR(ENOMEM);
240  goto end;
241  }
242 
243  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
244  buffersrc = avfilter_get_by_name("buffer");
245  buffersink = avfilter_get_by_name("buffersink");
246  if (!buffersrc || !buffersink) {
247  av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
248  ret = AVERROR_UNKNOWN;
249  goto end;
250  }
251 
252  snprintf(args, sizeof(args),
253  "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
254  dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
255  dec_ctx->time_base.num, dec_ctx->time_base.den,
256  dec_ctx->sample_aspect_ratio.num,
257  dec_ctx->sample_aspect_ratio.den);
258 
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 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 buffer sink\n");
270  goto end;
271  }
272 
273  ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
274  (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
276  if (ret < 0) {
277  av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
278  goto end;
279  }
280  } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
281  buffersrc = avfilter_get_by_name("abuffer");
282  buffersink = avfilter_get_by_name("abuffersink");
283  if (!buffersrc || !buffersink) {
284  av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
285  ret = AVERROR_UNKNOWN;
286  goto end;
287  }
288 
289  if (!dec_ctx->channel_layout)
290  dec_ctx->channel_layout =
292  snprintf(args, sizeof(args),
293  "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
294  dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
296  dec_ctx->channel_layout);
297  ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
298  args, NULL, filter_graph);
299  if (ret < 0) {
300  av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
301  goto end;
302  }
303 
304  ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
305  NULL, NULL, filter_graph);
306  if (ret < 0) {
307  av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
308  goto end;
309  }
310 
311  ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
312  (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
314  if (ret < 0) {
315  av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
316  goto end;
317  }
318 
319  ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
320  (uint8_t*)&enc_ctx->channel_layout,
321  sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
322  if (ret < 0) {
323  av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
324  goto end;
325  }
326 
327  ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
328  (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
330  if (ret < 0) {
331  av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
332  goto end;
333  }
334  } else {
335  ret = AVERROR_UNKNOWN;
336  goto end;
337  }
338 
339  /* Endpoints for the filter graph. */
340  outputs->name = av_strdup("in");
341  outputs->filter_ctx = buffersrc_ctx;
342  outputs->pad_idx = 0;
343  outputs->next = NULL;
344 
345  inputs->name = av_strdup("out");
346  inputs->filter_ctx = buffersink_ctx;
347  inputs->pad_idx = 0;
348  inputs->next = NULL;
349 
350  if (!outputs->name || !inputs->name) {
351  ret = AVERROR(ENOMEM);
352  goto end;
353  }
354 
355  if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
356  &inputs, &outputs, NULL)) < 0)
357  goto end;
358 
359  if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
360  goto end;
361 
362  /* Fill FilteringContext */
365  fctx->filter_graph = filter_graph;
366 
367 end:
368  avfilter_inout_free(&inputs);
369  avfilter_inout_free(&outputs);
370 
371  return ret;
372 }
373 
374 static int init_filters(void)
375 {
376  const char *filter_spec;
377  unsigned int i;
378  int ret;
379  filter_ctx = av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
380  if (!filter_ctx)
381  return AVERROR(ENOMEM);
382 
383  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
384  filter_ctx[i].buffersrc_ctx = NULL;
385  filter_ctx[i].buffersink_ctx = NULL;
386  filter_ctx[i].filter_graph = NULL;
387  if (!(ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
388  || ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
389  continue;
390 
391 
392  if (ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
393  filter_spec = "null"; /* passthrough (dummy) filter for video */
394  else
395  filter_spec = "anull"; /* passthrough (dummy) filter for audio */
396  ret = init_filter(&filter_ctx[i], stream_ctx[i].dec_ctx,
397  stream_ctx[i].enc_ctx, filter_spec);
398  if (ret)
399  return ret;
400  }
401  return 0;
402 }
403 
404 static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
405  int ret;
406  int got_frame_local;
407  AVPacket enc_pkt;
408  int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
409  (ifmt_ctx->streams[stream_index]->codecpar->codec_type ==
411 
412  if (!got_frame)
413  got_frame = &got_frame_local;
414 
415  av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
416  /* encode filtered frame */
417  enc_pkt.data = NULL;
418  enc_pkt.size = 0;
419  av_init_packet(&enc_pkt);
420  ret = enc_func(stream_ctx[stream_index].enc_ctx, &enc_pkt,
421  filt_frame, got_frame);
422  av_frame_free(&filt_frame);
423  if (ret < 0)
424  return ret;
425  if (!(*got_frame))
426  return 0;
427 
428  /* prepare packet for muxing */
429  enc_pkt.stream_index = stream_index;
430  av_packet_rescale_ts(&enc_pkt,
431  stream_ctx[stream_index].enc_ctx->time_base,
432  ofmt_ctx->streams[stream_index]->time_base);
433 
434  av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
435  /* mux encoded frame */
436  ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
437  return ret;
438 }
439 
440 static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
441 {
442  int ret;
443  AVFrame *filt_frame;
444 
445  av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
446  /* push the decoded frame into the filtergraph */
447  ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
448  frame, 0);
449  if (ret < 0) {
450  av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
451  return ret;
452  }
453 
454  /* pull filtered frames from the filtergraph */
455  while (1) {
456  filt_frame = av_frame_alloc();
457  if (!filt_frame) {
458  ret = AVERROR(ENOMEM);
459  break;
460  }
461  av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
462  ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
463  filt_frame);
464  if (ret < 0) {
465  /* if no more frames for output - returns AVERROR(EAGAIN)
466  * if flushed and no more frames for output - returns AVERROR_EOF
467  * rewrite retcode to 0 to show it as normal procedure completion
468  */
469  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
470  ret = 0;
471  av_frame_free(&filt_frame);
472  break;
473  }
474 
475  filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
476  ret = encode_write_frame(filt_frame, stream_index, NULL);
477  if (ret < 0)
478  break;
479  }
480 
481  return ret;
482 }
483 
484 static int flush_encoder(unsigned int stream_index)
485 {
486  int ret;
487  int got_frame;
488 
489  if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
491  return 0;
492 
493  while (1) {
494  av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
495  ret = encode_write_frame(NULL, stream_index, &got_frame);
496  if (ret < 0)
497  break;
498  if (!got_frame)
499  return 0;
500  }
501  return ret;
502 }
503 
504 int main(int argc, char **argv)
505 {
506  int ret;
507  AVPacket packet = { .data = NULL, .size = 0 };
508  AVFrame *frame = NULL;
509  enum AVMediaType type;
510  unsigned int stream_index;
511  unsigned int i;
512  int got_frame;
513  int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
514 
515  if (argc != 3) {
516  av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
517  return 1;
518  }
519 
520  if ((ret = open_input_file(argv[1])) < 0)
521  goto end;
522  if ((ret = open_output_file(argv[2])) < 0)
523  goto end;
524  if ((ret = init_filters()) < 0)
525  goto end;
526 
527  /* read all packets */
528  while (1) {
529  if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
530  break;
531  stream_index = packet.stream_index;
532  type = ifmt_ctx->streams[packet.stream_index]->codecpar->codec_type;
533  av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
534  stream_index);
535 
536  if (filter_ctx[stream_index].filter_graph) {
537  av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
538  frame = av_frame_alloc();
539  if (!frame) {
540  ret = AVERROR(ENOMEM);
541  break;
542  }
543  av_packet_rescale_ts(&packet,
544  ifmt_ctx->streams[stream_index]->time_base,
545  stream_ctx[stream_index].dec_ctx->time_base);
546  dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
548  ret = dec_func(stream_ctx[stream_index].dec_ctx, frame,
549  &got_frame, &packet);
550  if (ret < 0) {
551  av_frame_free(&frame);
552  av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
553  break;
554  }
555 
556  if (got_frame) {
557  frame->pts = frame->best_effort_timestamp;
558  ret = filter_encode_write_frame(frame, stream_index);
559  av_frame_free(&frame);
560  if (ret < 0)
561  goto end;
562  } else {
563  av_frame_free(&frame);
564  }
565  } else {
566  /* remux this frame without reencoding */
567  av_packet_rescale_ts(&packet,
568  ifmt_ctx->streams[stream_index]->time_base,
569  ofmt_ctx->streams[stream_index]->time_base);
570 
571  ret = av_interleaved_write_frame(ofmt_ctx, &packet);
572  if (ret < 0)
573  goto end;
574  }
575  av_packet_unref(&packet);
576  }
577 
578  /* flush filters and encoders */
579  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
580  /* flush filter */
581  if (!filter_ctx[i].filter_graph)
582  continue;
584  if (ret < 0) {
585  av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
586  goto end;
587  }
588 
589  /* flush encoder */
590  ret = flush_encoder(i);
591  if (ret < 0) {
592  av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
593  goto end;
594  }
595  }
596 
597  av_write_trailer(ofmt_ctx);
598 end:
599  av_packet_unref(&packet);
600  av_frame_free(&frame);
601  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
602  avcodec_free_context(&stream_ctx[i].dec_ctx);
603  if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && stream_ctx[i].enc_ctx)
604  avcodec_free_context(&stream_ctx[i].enc_ctx);
605  if (filter_ctx && filter_ctx[i].filter_graph)
606  avfilter_graph_free(&filter_ctx[i].filter_graph);
607  }
608  av_free(filter_ctx);
609  av_free(stream_ctx);
610  avformat_close_input(&ifmt_ctx);
611  if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
612  avio_closep(&ofmt_ctx->pb);
613  avformat_free_context(ofmt_ctx);
614 
615  if (ret < 0)
616  av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
617 
618  return ret ? 1 : 0;
619 }
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:1154
AVFilterGraph * filter_graph
Definition: transcoding.c:43
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3040
AVFilterContext * buffersink_ctx
static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame)
Definition: transcoding.c:404
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: allcodecs.c:857
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:1185
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
Definition: avfiltergraph.c:83
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:203
struct AVFilterInOut * next
next input/input in the list, NULL if this is the last
Definition: avfilter.h:1014
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1431
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:655
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:1896
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
attribute_deprecated int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
Definition: encode.c:118
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
attribute_deprecated 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: decode.c:833
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
Definition: opt.c:573
AVCodec.
Definition: avcodec.h:3408
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1640
Undefined.
Definition: avutil.h:273
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:1342
static int open_input_file(const char *filename)
Definition: transcoding.c:53
AVRational av_guess_frame_rate(AVFormatContext *ctx, AVStream *stream, AVFrame *frame)
Guess the frame rate, based on both the container and codec information.
Definition: utils.c:5017
memory buffer sink API for audio and video
#define AV_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:984
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2181
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT, AVFMT_TS_NEGATIVE
Definition: avformat.h:526
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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:4450
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:2078
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.
uint8_t * data
Definition: avcodec.h:1430
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1361
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:561
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2003
#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:484
static AVFormatContext * ifmt_ctx
Definition: transcoding.c:38
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:694
#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
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1598
AVFilterContext * buffersrc_ctx
static FilteringContext * filter_ctx
Definition: transcoding.c:45
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:49
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2224
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: allfilters.c:428
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1398
AVFilterContext * buffersrc_ctx
Definition: transcoding.c:42
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3429
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
static int init_filter(FilteringContext *fctx, AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, const char *filter_spec)
Definition: transcoding.c:225
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
AVRational time_base
Definition: signature.h:103
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:156
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:555
int width
picture width / height.
Definition: avcodec.h:1690
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:468
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
AVFilterContext * filter_ctx
filter context associated to this input/output
Definition: avfilter.h:1008
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static int init_filters(void)
Definition: transcoding.c:374
Stream structure.
Definition: avformat.h:873
A linked-list of the inputs/outputs of the filter chain.
Definition: avfilter.h:1003
AVCodecContext * enc_ctx
Definition: transcoding.c:49
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
Libavcodec external API header.
enum AVMediaType codec_type
Definition: avcodec.h:1526
attribute_deprecated 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: decode.c:826
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:171
enum AVCodecID codec_id
Definition: avcodec.h:1528
int sample_rate
samples per second
Definition: avcodec.h:2173
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
main external API structure.
Definition: avcodec.h:1518
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:862
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
GLint GLenum type
Definition: opengl_enc.c:105
static StreamContext * stream_ctx
Definition: transcoding.c:51
Filter definition.
Definition: avfilter.h:144
int pad_idx
index of the filt_ctx pad to use for linking
Definition: avfilter.h:1011
Rational number (pair of numerator and denominator).
Definition: rational.h:58
attribute_deprecated int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
Definition: encode.c:260
static int open_output_file(const char *filename)
Definition: transcoding.c:111
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:2021
AVMediaType
Definition: avutil.h:199
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:538
#define snprintf
Definition: snprintf.h:34
int64_t best_effort_timestamp
frame timestamp estimated using various heuristics, in stream time base
Definition: frame.h:483
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4384
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1767
char * name
unique name for this input/output in the list
Definition: avfilter.h:1005
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:882
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
Main libavformat public API header.
int
AVFilterInOut * avfilter_inout_alloc(void)
Allocate a single AVFilterInOut entry.
Definition: graphparser.c:198
#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:3559
static AVCodecContext * dec_ctx
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int den
Denominator.
Definition: rational.h:60
static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
Definition: transcoding.c:440
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:152
#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:4422
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:52
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:2174
AVCodecContext * dec_ctx
Definition: transcoding.c:48
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:537
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
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:1247
int main(int argc, char **argv)
Definition: transcoding.c:504
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
#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:3431
AVFilterContext * buffersink_ctx
Definition: transcoding.c:41
int stream_index
Definition: avcodec.h:1432
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:902
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:67
This structure stores compressed data.
Definition: avcodec.h:1407
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
static AVFormatContext * ofmt_ctx
Definition: transcoding.c:39
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191