FFmpeg
transcode.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 demuxing, decoding, filtering, encoding and muxing API usage example
27  * @example transcode.c
28  *
29  * Convert input to output file, applying some hard-coded filter-graph on both
30  * audio and video streams.
31  */
32 
33 #include <libavcodec/avcodec.h>
34 #include <libavformat/avformat.h>
35 #include <libavfilter/buffersink.h>
36 #include <libavfilter/buffersrc.h>
38 #include <libavutil/opt.h>
39 #include <libavutil/pixdesc.h>
40 
43 typedef struct FilteringContext {
47 
52 
53 typedef struct StreamContext {
56 
60 
61 static int open_input_file(const char *filename)
62 {
63  int ret;
64  unsigned int i;
65 
66  ifmt_ctx = NULL;
67  if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
68  av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
69  return ret;
70  }
71 
73  av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
74  return ret;
75  }
76 
78  if (!stream_ctx)
79  return AVERROR(ENOMEM);
80 
81  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
82  AVStream *stream = ifmt_ctx->streams[i];
83  const AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
84  AVCodecContext *codec_ctx;
85  if (!dec) {
86  av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i);
88  }
89  codec_ctx = avcodec_alloc_context3(dec);
90  if (!codec_ctx) {
91  av_log(NULL, AV_LOG_ERROR, "Failed to allocate the decoder context for stream #%u\n", i);
92  return AVERROR(ENOMEM);
93  }
94  ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar);
95  if (ret < 0) {
96  av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context "
97  "for stream #%u\n", i);
98  return ret;
99  }
100 
101  /* Inform the decoder about the timebase for the packet timestamps.
102  * This is highly recommended, but not mandatory. */
103  codec_ctx->pkt_timebase = stream->time_base;
104 
105  /* Reencode video & audio and remux subtitles etc. */
106  if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
107  || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
108  if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
109  codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, stream, NULL);
110  /* Open decoder */
111  ret = avcodec_open2(codec_ctx, dec, NULL);
112  if (ret < 0) {
113  av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
114  return ret;
115  }
116  }
117  stream_ctx[i].dec_ctx = codec_ctx;
118 
120  if (!stream_ctx[i].dec_frame)
121  return AVERROR(ENOMEM);
122  }
123 
124  av_dump_format(ifmt_ctx, 0, filename, 0);
125  return 0;
126 }
127 
128 static int open_output_file(const char *filename)
129 {
130  AVStream *out_stream;
131  AVStream *in_stream;
132  AVCodecContext *dec_ctx, *enc_ctx;
133  const AVCodec *encoder;
134  int ret;
135  unsigned int i;
136 
137  ofmt_ctx = NULL;
139  if (!ofmt_ctx) {
140  av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
141  return AVERROR_UNKNOWN;
142  }
143 
144 
145  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
146  out_stream = avformat_new_stream(ofmt_ctx, NULL);
147  if (!out_stream) {
148  av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
149  return AVERROR_UNKNOWN;
150  }
151 
152  in_stream = ifmt_ctx->streams[i];
154 
157  /* in this example, we choose transcoding to same codec */
159  if (!encoder) {
160  av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
161  return AVERROR_INVALIDDATA;
162  }
163  enc_ctx = avcodec_alloc_context3(encoder);
164  if (!enc_ctx) {
165  av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
166  return AVERROR(ENOMEM);
167  }
168 
169  /* In this example, we transcode to same properties (picture size,
170  * sample rate etc.). These properties can be changed for output
171  * streams easily using filters */
173  enc_ctx->height = dec_ctx->height;
174  enc_ctx->width = dec_ctx->width;
176  /* take first format from list of supported formats */
177  if (encoder->pix_fmts)
178  enc_ctx->pix_fmt = encoder->pix_fmts[0];
179  else
180  enc_ctx->pix_fmt = dec_ctx->pix_fmt;
181  /* video time_base can be set to whatever is handy and supported by encoder */
182  enc_ctx->time_base = av_inv_q(dec_ctx->framerate);
183  } else {
184  enc_ctx->sample_rate = dec_ctx->sample_rate;
186  if (ret < 0)
187  return ret;
188  /* take first format from list of supported formats */
189  enc_ctx->sample_fmt = encoder->sample_fmts[0];
190  enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
191  }
192 
195 
196  /* Third parameter can be used to pass settings to encoder */
197  ret = avcodec_open2(enc_ctx, encoder, NULL);
198  if (ret < 0) {
199  av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
200  return ret;
201  }
202  ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
203  if (ret < 0) {
204  av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
205  return ret;
206  }
207 
208  out_stream->time_base = enc_ctx->time_base;
209  stream_ctx[i].enc_ctx = enc_ctx;
210  } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
211  av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
212  return AVERROR_INVALIDDATA;
213  } else {
214  /* if this stream must be remuxed */
215  ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
216  if (ret < 0) {
217  av_log(NULL, AV_LOG_ERROR, "Copying parameters for stream #%u failed\n", i);
218  return ret;
219  }
220  out_stream->time_base = in_stream->time_base;
221  }
222 
223  }
224  av_dump_format(ofmt_ctx, 0, filename, 1);
225 
226  if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
227  ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
228  if (ret < 0) {
229  av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
230  return ret;
231  }
232  }
233 
234  /* init muxer, write output file header */
236  if (ret < 0) {
237  av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
238  return ret;
239  }
240 
241  return 0;
242 }
243 
245  AVCodecContext *enc_ctx, const char *filter_spec)
246 {
247  char args[512];
248  int ret = 0;
249  const AVFilter *buffersrc = NULL;
250  const AVFilter *buffersink = NULL;
256 
257  if (!outputs || !inputs || !filter_graph) {
258  ret = AVERROR(ENOMEM);
259  goto end;
260  }
261 
263  buffersrc = avfilter_get_by_name("buffer");
264  buffersink = avfilter_get_by_name("buffersink");
265  if (!buffersrc || !buffersink) {
266  av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
268  goto end;
269  }
270 
271  snprintf(args, sizeof(args),
272  "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
277 
278  ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
279  args, NULL, filter_graph);
280  if (ret < 0) {
281  av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
282  goto end;
283  }
284 
285  ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
287  if (ret < 0) {
288  av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
289  goto end;
290  }
291 
292  ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
293  (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
295  if (ret < 0) {
296  av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
297  goto end;
298  }
299  } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
300  char buf[64];
301  buffersrc = avfilter_get_by_name("abuffer");
302  buffersink = avfilter_get_by_name("abuffersink");
303  if (!buffersrc || !buffersink) {
304  av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
306  goto end;
307  }
308 
311  av_channel_layout_describe(&dec_ctx->ch_layout, buf, sizeof(buf));
312  snprintf(args, sizeof(args),
313  "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=%s",
316  buf);
317  ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
318  args, NULL, filter_graph);
319  if (ret < 0) {
320  av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
321  goto end;
322  }
323 
324  ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
326  if (ret < 0) {
327  av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
328  goto end;
329  }
330 
331  ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
332  (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
334  if (ret < 0) {
335  av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
336  goto end;
337  }
338 
339  av_channel_layout_describe(&enc_ctx->ch_layout, buf, sizeof(buf));
340  ret = av_opt_set(buffersink_ctx, "ch_layouts",
342  if (ret < 0) {
343  av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
344  goto end;
345  }
346 
347  ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
348  (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
350  if (ret < 0) {
351  av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
352  goto end;
353  }
354  } else {
356  goto end;
357  }
358 
359  /* Endpoints for the filter graph. */
360  outputs->name = av_strdup("in");
361  outputs->filter_ctx = buffersrc_ctx;
362  outputs->pad_idx = 0;
363  outputs->next = NULL;
364 
365  inputs->name = av_strdup("out");
366  inputs->filter_ctx = buffersink_ctx;
367  inputs->pad_idx = 0;
368  inputs->next = NULL;
369 
370  if (!outputs->name || !inputs->name) {
371  ret = AVERROR(ENOMEM);
372  goto end;
373  }
374 
375  if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
376  &inputs, &outputs, NULL)) < 0)
377  goto end;
378 
380  goto end;
381 
382  /* Fill FilteringContext */
385  fctx->filter_graph = filter_graph;
386 
387 end:
390 
391  return ret;
392 }
393 
394 static int init_filters(void)
395 {
396  const char *filter_spec;
397  unsigned int i;
398  int ret;
400  if (!filter_ctx)
401  return AVERROR(ENOMEM);
402 
403  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
409  continue;
410 
411 
413  filter_spec = "null"; /* passthrough (dummy) filter for video */
414  else
415  filter_spec = "anull"; /* passthrough (dummy) filter for audio */
417  stream_ctx[i].enc_ctx, filter_spec);
418  if (ret)
419  return ret;
420 
422  if (!filter_ctx[i].enc_pkt)
423  return AVERROR(ENOMEM);
424 
426  if (!filter_ctx[i].filtered_frame)
427  return AVERROR(ENOMEM);
428  }
429  return 0;
430 }
431 
432 static int encode_write_frame(unsigned int stream_index, int flush)
433 {
434  StreamContext *stream = &stream_ctx[stream_index];
435  FilteringContext *filter = &filter_ctx[stream_index];
436  AVFrame *filt_frame = flush ? NULL : filter->filtered_frame;
437  AVPacket *enc_pkt = filter->enc_pkt;
438  int ret;
439 
440  av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
441  /* encode filtered frame */
442  av_packet_unref(enc_pkt);
443 
444  if (filt_frame && filt_frame->pts != AV_NOPTS_VALUE)
445  filt_frame->pts = av_rescale_q(filt_frame->pts, filt_frame->time_base,
446  stream->enc_ctx->time_base);
447 
448  ret = avcodec_send_frame(stream->enc_ctx, filt_frame);
449 
450  if (ret < 0)
451  return ret;
452 
453  while (ret >= 0) {
454  ret = avcodec_receive_packet(stream->enc_ctx, enc_pkt);
455 
456  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
457  return 0;
458 
459  /* prepare packet for muxing */
460  enc_pkt->stream_index = stream_index;
461  av_packet_rescale_ts(enc_pkt,
462  stream->enc_ctx->time_base,
463  ofmt_ctx->streams[stream_index]->time_base);
464 
465  av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
466  /* mux encoded frame */
468  }
469 
470  return ret;
471 }
472 
473 static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
474 {
475  FilteringContext *filter = &filter_ctx[stream_index];
476  int ret;
477 
478  av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
479  /* push the decoded frame into the filtergraph */
480  ret = av_buffersrc_add_frame_flags(filter->buffersrc_ctx,
481  frame, 0);
482  if (ret < 0) {
483  av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
484  return ret;
485  }
486 
487  /* pull filtered frames from the filtergraph */
488  while (1) {
489  av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
490  ret = av_buffersink_get_frame(filter->buffersink_ctx,
491  filter->filtered_frame);
492  if (ret < 0) {
493  /* if no more frames for output - returns AVERROR(EAGAIN)
494  * if flushed and no more frames for output - returns AVERROR_EOF
495  * rewrite retcode to 0 to show it as normal procedure completion
496  */
497  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
498  ret = 0;
499  break;
500  }
501 
502  filter->filtered_frame->time_base = av_buffersink_get_time_base(filter->buffersink_ctx);;
503  filter->filtered_frame->pict_type = AV_PICTURE_TYPE_NONE;
504  ret = encode_write_frame(stream_index, 0);
505  av_frame_unref(filter->filtered_frame);
506  if (ret < 0)
507  break;
508  }
509 
510  return ret;
511 }
512 
513 static int flush_encoder(unsigned int stream_index)
514 {
515  if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
517  return 0;
518 
519  av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
520  return encode_write_frame(stream_index, 1);
521 }
522 
523 int main(int argc, char **argv)
524 {
525  int ret;
526  AVPacket *packet = NULL;
527  unsigned int stream_index;
528  unsigned int i;
529 
530  if (argc != 3) {
531  av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
532  return 1;
533  }
534 
535  if ((ret = open_input_file(argv[1])) < 0)
536  goto end;
537  if ((ret = open_output_file(argv[2])) < 0)
538  goto end;
539  if ((ret = init_filters()) < 0)
540  goto end;
541  if (!(packet = av_packet_alloc()))
542  goto end;
543 
544  /* read all packets */
545  while (1) {
546  if ((ret = av_read_frame(ifmt_ctx, packet)) < 0)
547  break;
548  stream_index = packet->stream_index;
549  av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
550  stream_index);
551 
552  if (filter_ctx[stream_index].filter_graph) {
553  StreamContext *stream = &stream_ctx[stream_index];
554 
555  av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
556 
558  if (ret < 0) {
559  av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
560  break;
561  }
562 
563  while (ret >= 0) {
564  ret = avcodec_receive_frame(stream->dec_ctx, stream->dec_frame);
565  if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
566  break;
567  else if (ret < 0)
568  goto end;
569 
570  stream->dec_frame->pts = stream->dec_frame->best_effort_timestamp;
571  ret = filter_encode_write_frame(stream->dec_frame, stream_index);
572  if (ret < 0)
573  goto end;
574  }
575  } else {
576  /* remux this frame without reencoding */
578  ifmt_ctx->streams[stream_index]->time_base,
579  ofmt_ctx->streams[stream_index]->time_base);
580 
582  if (ret < 0)
583  goto end;
584  }
586  }
587 
588  /* flush decoders, filters and encoders */
589  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
590  StreamContext *stream;
591 
592  if (!filter_ctx[i].filter_graph)
593  continue;
594 
595  stream = &stream_ctx[i];
596 
597  av_log(NULL, AV_LOG_INFO, "Flushing stream %u decoder\n", i);
598 
599  /* flush decoder */
600  ret = avcodec_send_packet(stream->dec_ctx, NULL);
601  if (ret < 0) {
602  av_log(NULL, AV_LOG_ERROR, "Flushing decoding failed\n");
603  goto end;
604  }
605 
606  while (ret >= 0) {
607  ret = avcodec_receive_frame(stream->dec_ctx, stream->dec_frame);
608  if (ret == AVERROR_EOF)
609  break;
610  else if (ret < 0)
611  goto end;
612 
613  stream->dec_frame->pts = stream->dec_frame->best_effort_timestamp;
615  if (ret < 0)
616  goto end;
617  }
618 
619  /* flush filter */
621  if (ret < 0) {
622  av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
623  goto end;
624  }
625 
626  /* flush encoder */
627  ret = flush_encoder(i);
628  if (ret < 0) {
629  av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
630  goto end;
631  }
632  }
633 
635 end:
637  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
640  avcodec_free_context(&stream_ctx[i].enc_ctx);
643  av_packet_free(&filter_ctx[i].enc_pkt);
644  av_frame_free(&filter_ctx[i].filtered_frame);
645  }
646 
647  av_frame_free(&stream_ctx[i].dec_frame);
648  }
655 
656  if (ret < 0)
657  av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
658 
659  return ret ? 1 : 0;
660 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
AVCodec
AVCodec.
Definition: codec.h:187
dec_ctx
static AVCodecContext * dec_ctx
Definition: decode_filter_audio.c:46
avcodec_receive_packet
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Definition: encode.c:556
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1064
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FilteringContext::filtered_frame
AVFrame * filtered_frame
Definition: transcode.c:49
stream_ctx
static StreamContext * stream_ctx
Definition: transcode.c:59
AVCodec::pix_fmts
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:209
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
avcodec_find_encoder
const AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: allcodecs.c:970
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
pixdesc.h
ofmt_ctx
static AVFormatContext * ofmt_ctx
Definition: transcode.c:42
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1183
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:312
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: demux.c:1462
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
StreamContext::dec_frame
AVFrame * dec_frame
Definition: transcode.c:57
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:74
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:334
avfilter_graph_free
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
Definition: avfiltergraph.c:119
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1803
avformat_close_input
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: demux.c:374
FilteringContext
Definition: transcode.c:43
avfilter_graph_create_filter
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.
Definition: avfiltergraph.c:138
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
AVCodec::sample_fmts
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:211
avfilter_graph_alloc
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
Definition: avfiltergraph.c:82
filter_ctx
static FilteringContext * filter_ctx
Definition: transcode.c:51
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:521
av_opt_set
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:487
AVRational::num
int num
Numerator.
Definition: rational.h:59
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
avfilter_inout_free
void avfilter_inout_free(AVFilterInOut **inout)
Free the supplied list of AVFilterInOut and set *inout to NULL.
Definition: graphparser.c:75
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_dump_format
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,...
Definition: dump.c:629
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: demux.c:226
av_channel_layout_describe
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
Definition: channel_layout.c:786
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:153
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:112
av_buffersink_get_frame
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:96
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:713
av_buffersink_get_time_base
AVRational av_buffersink_get_time_base(const AVFilterContext *ctx)
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:637
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
av_rescale_q
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
av_get_sample_fmt_name
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:51
avformat_write_header
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:477
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:451
FilteringContext::buffersrc_ctx
AVFilterContext * buffersrc_ctx
Definition: transcode.c:45
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
avfilter_get_by_name
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: allfilters.c:625
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const struct AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
avfilter_graph_config
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
Definition: avfiltergraph.c:1169
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:880
NULL
#define NULL
Definition: coverity.c:32
av_opt_set_bin
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
Definition: opt.c:639
avcodec_free_context
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:168
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1157
avfilter_inout_alloc
AVFilterInOut * avfilter_inout_alloc(void)
Allocate a single AVFilterInOut entry.
Definition: graphparser.c:70
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:128
AVFilterGraph
Definition: avfilter.h:864
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
avcodec_find_decoder
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:975
AVFrame::best_effort_timestamp
int64_t best_effort_timestamp
frame timestamp estimated using various heuristics, in stream time base
Definition: frame.h:676
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1171
StreamContext
Definition: transcode.c:53
AVOutputFormat::flags
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS,...
Definition: avformat.h:530
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:563
avformat_find_stream_info
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: demux.c:2436
ifmt_ctx
static AVFormatContext * ifmt_ctx
Definition: transcode.c:41
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
AVCodecContext::pkt_timebase
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition: avcodec.h:1817
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFrame::time_base
AVRational time_base
Time base for the timestamps in this frame.
Definition: frame.h:467
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:469
AVMEDIA_TYPE_UNKNOWN
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:278
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:563
FilteringContext::enc_pkt
AVPacket * enc_pkt
Definition: transcode.c:48
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
av_packet_rescale_ts
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another.
Definition: avpacket.c:527
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
buffersink.h
buffersink_ctx
AVFilterContext * buffersink_ctx
Definition: decode_filter_audio.c:47
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:972
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:709
flush_encoder
static int flush_encoder(unsigned int stream_index)
Definition: transcode.c:513
avio_closep
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:1304
av_write_trailer
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1291
StreamContext::dec_ctx
AVCodecContext * dec_ctx
Definition: transcode.c:54
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:479
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
avfilter_graph_parse_ptr
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:918
init_filter
static int init_filter(FilteringContext *fctx, AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, const char *filter_spec)
Definition: transcode.c:244
encode_write_frame
static int encode_write_frame(unsigned int stream_index, int flush)
Definition: transcode.c:432
av_buffersrc_add_frame_flags
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:176
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
packet
enum AVPacketSideDataType packet
Definition: decode.c:1425
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AVCodecContext::height
int height
Definition: avcodec.h:621
filter_encode_write_frame
static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
Definition: transcode.c:473
avcodec_send_frame
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
Definition: encode.c:518
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
avcodec.h
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:841
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
av_guess_frame_rate
AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
Guess the frame rate, based on both the container and codec information.
Definition: avformat.c:655
AVFormatContext::oformat
const struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1134
avformat.h
StreamContext::enc_ctx
AVCodecContext * enc_ctx
Definition: transcode.c:55
AVCodecContext
main external API structure.
Definition: avcodec.h:441
channel_layout.h
init_filters
static int init_filters(void)
Definition: transcode.c:394
outputs
static const AVFilterPad outputs[]
Definition: af_afwtdn.c:1291
AVRational::den
int den
Denominator.
Definition: rational.h:60
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:102
AV_CODEC_CAP_DELAY
#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: codec.h:76
avio_open
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:1239
AVPacket::stream_index
int stream_index
Definition: packet.h:493
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:647
AVERROR_DECODER_NOT_FOUND
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:54
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:449
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:367
main
int main(int argc, char **argv)
Definition: transcode.c:523
avcodec_parameters_from_context
int avcodec_parameters_from_context(struct AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: codec_par.c:137
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
filter_graph
AVFilterGraph * filter_graph
Definition: decode_filter_audio.c:49
av_interleaved_write_frame
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:1276
FilteringContext::buffersink_ctx
AVFilterContext * buffersink_ctx
Definition: transcode.c:44
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
open_output_file
static int open_output_file(const char *filename)
Definition: transcode.c:128
open_input_file
static int open_input_file(const char *filename)
Definition: transcode.c:61
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
avformat_alloc_output_context2
int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:93
AVFilterInOut
A linked-list of the inputs/outputs of the filter chain.
Definition: avfilter.h:1024
snprintf
#define snprintf
Definition: snprintf.h:34
FilteringContext::filter_graph
AVFilterGraph * filter_graph
Definition: transcode.c:46
buffersrc.h
AVCodecContext::sample_aspect_ratio
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:822
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:106
buffersrc_ctx
AVFilterContext * buffersrc_ctx
Definition: decode_filter_audio.c:48