FFmpeg 6.0
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>
38#include <libavutil/opt.h>
39#include <libavutil/pixdesc.h>
40
43typedef struct FilteringContext {
47
52
53typedef struct StreamContext {
56
60
61static 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
72 if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
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 /* Reencode video & audio and remux subtitles etc. */
101 if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
102 || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
103 if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
104 codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, stream, NULL);
105 /* Open decoder */
106 ret = avcodec_open2(codec_ctx, dec, NULL);
107 if (ret < 0) {
108 av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
109 return ret;
110 }
111 }
112 stream_ctx[i].dec_ctx = codec_ctx;
113
115 if (!stream_ctx[i].dec_frame)
116 return AVERROR(ENOMEM);
117 }
118
119 av_dump_format(ifmt_ctx, 0, filename, 0);
120 return 0;
121}
122
123static int open_output_file(const char *filename)
124{
125 AVStream *out_stream;
126 AVStream *in_stream;
127 AVCodecContext *dec_ctx, *enc_ctx;
128 const AVCodec *encoder;
129 int ret;
130 unsigned int i;
131
132 ofmt_ctx = NULL;
133 avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
134 if (!ofmt_ctx) {
135 av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
136 return AVERROR_UNKNOWN;
137 }
138
139
140 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
141 out_stream = avformat_new_stream(ofmt_ctx, NULL);
142 if (!out_stream) {
143 av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
144 return AVERROR_UNKNOWN;
145 }
146
147 in_stream = ifmt_ctx->streams[i];
149
152 /* in this example, we choose transcoding to same codec */
154 if (!encoder) {
155 av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
156 return AVERROR_INVALIDDATA;
157 }
158 enc_ctx = avcodec_alloc_context3(encoder);
159 if (!enc_ctx) {
160 av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
161 return AVERROR(ENOMEM);
162 }
163
164 /* In this example, we transcode to same properties (picture size,
165 * sample rate etc.). These properties can be changed for output
166 * streams easily using filters */
168 enc_ctx->height = dec_ctx->height;
169 enc_ctx->width = dec_ctx->width;
171 /* take first format from list of supported formats */
172 if (encoder->pix_fmts)
173 enc_ctx->pix_fmt = encoder->pix_fmts[0];
174 else
175 enc_ctx->pix_fmt = dec_ctx->pix_fmt;
176 /* video time_base can be set to whatever is handy and supported by encoder */
177 enc_ctx->time_base = av_inv_q(dec_ctx->framerate);
178 } else {
179 enc_ctx->sample_rate = dec_ctx->sample_rate;
181 if (ret < 0)
182 return ret;
183 /* take first format from list of supported formats */
184 enc_ctx->sample_fmt = encoder->sample_fmts[0];
185 enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
186 }
187
190
191 /* Third parameter can be used to pass settings to encoder */
192 ret = avcodec_open2(enc_ctx, encoder, NULL);
193 if (ret < 0) {
194 av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
195 return ret;
196 }
197 ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
198 if (ret < 0) {
199 av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
200 return ret;
201 }
202
203 out_stream->time_base = enc_ctx->time_base;
204 stream_ctx[i].enc_ctx = enc_ctx;
205 } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
206 av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
207 return AVERROR_INVALIDDATA;
208 } else {
209 /* if this stream must be remuxed */
210 ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
211 if (ret < 0) {
212 av_log(NULL, AV_LOG_ERROR, "Copying parameters for stream #%u failed\n", i);
213 return ret;
214 }
215 out_stream->time_base = in_stream->time_base;
216 }
217
218 }
219 av_dump_format(ofmt_ctx, 0, filename, 1);
220
221 if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
222 ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
223 if (ret < 0) {
224 av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
225 return ret;
226 }
227 }
228
229 /* init muxer, write output file header */
230 ret = avformat_write_header(ofmt_ctx, NULL);
231 if (ret < 0) {
232 av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
233 return ret;
234 }
235
236 return 0;
237}
238
240 AVCodecContext *enc_ctx, const char *filter_spec)
241{
242 char args[512];
243 int ret = 0;
244 const AVFilter *buffersrc = NULL;
245 const AVFilter *buffersink = NULL;
251
252 if (!outputs || !inputs || !filter_graph) {
253 ret = AVERROR(ENOMEM);
254 goto end;
255 }
256
258 buffersrc = avfilter_get_by_name("buffer");
259 buffersink = avfilter_get_by_name("buffersink");
260 if (!buffersrc || !buffersink) {
261 av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
262 ret = AVERROR_UNKNOWN;
263 goto end;
264 }
265
266 snprintf(args, sizeof(args),
267 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
272
273 ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
274 args, NULL, filter_graph);
275 if (ret < 0) {
276 av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
277 goto end;
278 }
279
280 ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
281 NULL, NULL, filter_graph);
282 if (ret < 0) {
283 av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
284 goto end;
285 }
286
287 ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
288 (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
290 if (ret < 0) {
291 av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
292 goto end;
293 }
294 } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
295 char buf[64];
296 buffersrc = avfilter_get_by_name("abuffer");
297 buffersink = avfilter_get_by_name("abuffersink");
298 if (!buffersrc || !buffersink) {
299 av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
300 ret = AVERROR_UNKNOWN;
301 goto end;
302 }
303
306 av_channel_layout_describe(&dec_ctx->ch_layout, buf, sizeof(buf));
307 snprintf(args, sizeof(args),
308 "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=%s",
311 buf);
312 ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
313 args, NULL, filter_graph);
314 if (ret < 0) {
315 av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
316 goto end;
317 }
318
319 ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
320 NULL, NULL, filter_graph);
321 if (ret < 0) {
322 av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
323 goto end;
324 }
325
326 ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
327 (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
329 if (ret < 0) {
330 av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
331 goto end;
332 }
333
334 av_channel_layout_describe(&enc_ctx->ch_layout, buf, sizeof(buf));
335 ret = av_opt_set(buffersink_ctx, "ch_layouts",
337 if (ret < 0) {
338 av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
339 goto end;
340 }
341
342 ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
343 (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
345 if (ret < 0) {
346 av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
347 goto end;
348 }
349 } else {
350 ret = AVERROR_UNKNOWN;
351 goto end;
352 }
353
354 /* Endpoints for the filter graph. */
355 outputs->name = av_strdup("in");
356 outputs->filter_ctx = buffersrc_ctx;
357 outputs->pad_idx = 0;
358 outputs->next = NULL;
359
360 inputs->name = av_strdup("out");
361 inputs->filter_ctx = buffersink_ctx;
362 inputs->pad_idx = 0;
363 inputs->next = NULL;
364
365 if (!outputs->name || !inputs->name) {
366 ret = AVERROR(ENOMEM);
367 goto end;
368 }
369
370 if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
371 &inputs, &outputs, NULL)) < 0)
372 goto end;
373
374 if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
375 goto end;
376
377 /* Fill FilteringContext */
381
382end:
383 avfilter_inout_free(&inputs);
384 avfilter_inout_free(&outputs);
385
386 return ret;
387}
388
389static int init_filters(void)
390{
391 const char *filter_spec;
392 unsigned int i;
393 int ret;
395 if (!filter_ctx)
396 return AVERROR(ENOMEM);
397
398 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
399 filter_ctx[i].buffersrc_ctx = NULL;
400 filter_ctx[i].buffersink_ctx = NULL;
401 filter_ctx[i].filter_graph = NULL;
404 continue;
405
406
408 filter_spec = "null"; /* passthrough (dummy) filter for video */
409 else
410 filter_spec = "anull"; /* passthrough (dummy) filter for audio */
412 stream_ctx[i].enc_ctx, filter_spec);
413 if (ret)
414 return ret;
415
417 if (!filter_ctx[i].enc_pkt)
418 return AVERROR(ENOMEM);
419
421 if (!filter_ctx[i].filtered_frame)
422 return AVERROR(ENOMEM);
423 }
424 return 0;
425}
426
427static int encode_write_frame(unsigned int stream_index, int flush)
428{
429 StreamContext *stream = &stream_ctx[stream_index];
430 FilteringContext *filter = &filter_ctx[stream_index];
431 AVFrame *filt_frame = flush ? NULL : filter->filtered_frame;
432 AVPacket *enc_pkt = filter->enc_pkt;
433 int ret;
434
435 av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
436 /* encode filtered frame */
437 av_packet_unref(enc_pkt);
438
439 ret = avcodec_send_frame(stream->enc_ctx, filt_frame);
440
441 if (ret < 0)
442 return ret;
443
444 while (ret >= 0) {
445 ret = avcodec_receive_packet(stream->enc_ctx, enc_pkt);
446
447 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
448 return 0;
449
450 /* prepare packet for muxing */
451 enc_pkt->stream_index = stream_index;
452 av_packet_rescale_ts(enc_pkt,
453 stream->enc_ctx->time_base,
454 ofmt_ctx->streams[stream_index]->time_base);
455
456 av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
457 /* mux encoded frame */
458 ret = av_interleaved_write_frame(ofmt_ctx, enc_pkt);
459 }
460
461 return ret;
462}
463
464static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
465{
466 FilteringContext *filter = &filter_ctx[stream_index];
467 int ret;
468
469 av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
470 /* push the decoded frame into the filtergraph */
472 frame, 0);
473 if (ret < 0) {
474 av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
475 return ret;
476 }
477
478 /* pull filtered frames from the filtergraph */
479 while (1) {
480 av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
482 filter->filtered_frame);
483 if (ret < 0) {
484 /* if no more frames for output - returns AVERROR(EAGAIN)
485 * if flushed and no more frames for output - returns AVERROR_EOF
486 * rewrite retcode to 0 to show it as normal procedure completion
487 */
488 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
489 ret = 0;
490 break;
491 }
492
494 ret = encode_write_frame(stream_index, 0);
496 if (ret < 0)
497 break;
498 }
499
500 return ret;
501}
502
503static int flush_encoder(unsigned int stream_index)
504{
505 if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
507 return 0;
508
509 av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
510 return encode_write_frame(stream_index, 1);
511}
512
513int main(int argc, char **argv)
514{
515 int ret;
516 AVPacket *packet = NULL;
517 unsigned int stream_index;
518 unsigned int i;
519
520 if (argc != 3) {
521 av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
522 return 1;
523 }
524
525 if ((ret = open_input_file(argv[1])) < 0)
526 goto end;
527 if ((ret = open_output_file(argv[2])) < 0)
528 goto end;
529 if ((ret = init_filters()) < 0)
530 goto end;
531 if (!(packet = av_packet_alloc()))
532 goto end;
533
534 /* read all packets */
535 while (1) {
536 if ((ret = av_read_frame(ifmt_ctx, packet)) < 0)
537 break;
538 stream_index = packet->stream_index;
539 av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
540 stream_index);
541
542 if (filter_ctx[stream_index].filter_graph) {
543 StreamContext *stream = &stream_ctx[stream_index];
544
545 av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
546
548 ifmt_ctx->streams[stream_index]->time_base,
549 stream->dec_ctx->time_base);
550 ret = avcodec_send_packet(stream->dec_ctx, packet);
551 if (ret < 0) {
552 av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
553 break;
554 }
555
556 while (ret >= 0) {
557 ret = avcodec_receive_frame(stream->dec_ctx, stream->dec_frame);
558 if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
559 break;
560 else if (ret < 0)
561 goto end;
562
563 stream->dec_frame->pts = stream->dec_frame->best_effort_timestamp;
564 ret = filter_encode_write_frame(stream->dec_frame, stream_index);
565 if (ret < 0)
566 goto end;
567 }
568 } else {
569 /* remux this frame without reencoding */
571 ifmt_ctx->streams[stream_index]->time_base,
572 ofmt_ctx->streams[stream_index]->time_base);
573
575 if (ret < 0)
576 goto end;
577 }
578 av_packet_unref(packet);
579 }
580
581 /* flush filters and encoders */
582 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
583 /* flush filter */
584 if (!filter_ctx[i].filter_graph)
585 continue;
586 ret = filter_encode_write_frame(NULL, i);
587 if (ret < 0) {
588 av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
589 goto end;
590 }
591
592 /* flush encoder */
593 ret = flush_encoder(i);
594 if (ret < 0) {
595 av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
596 goto end;
597 }
598 }
599
601end:
602 av_packet_free(&packet);
603 for (i = 0; i < ifmt_ctx->nb_streams; i++) {
606 avcodec_free_context(&stream_ctx[i].enc_ctx);
609 av_packet_free(&filter_ctx[i].enc_pkt);
610 av_frame_free(&filter_ctx[i].filtered_frame);
611 }
612
613 av_frame_free(&stream_ctx[i].dec_frame);
614 }
621
622 if (ret < 0)
623 av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
624
625 return ret ? 1 : 0;
626}
Libavcodec external API header.
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:468
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:478
int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:624
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
memory buffer sink API for audio and video
Memory buffer source API.
Public libavutil channel layout APIs header.
AVFilterGraph * filter_graph
AVFilterContext * buffersink_ctx
AVFilterContext * buffersrc_ctx
static AVCodecContext * dec_ctx
static AVFrame * frame
Definition: demux_decode.c:54
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:563
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
#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
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
const AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:317
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
int 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...
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
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.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
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.
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
AVRational av_guess_frame_rate(AVFormatContext *ctx, AVStream *stream, AVFrame *frame)
Guess the frame rate, based on both the container and codec information.
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,...
int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
Get a frame with filtered data from sink and put it in frame.
av_warn_unused_result int av_buffersrc_add_frame_flags(AVFilterContext *buffer_src, AVFrame *frame, int flags)
Add a frame to the buffer source.
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
void avfilter_inout_free(AVFilterInOut **inout)
Free the supplied list of AVFilterInOut and set *inout to NULL.
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.
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
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.
AVFilterInOut * avfilter_inout_alloc(void)
Allocate a single AVFilterInOut entry.
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
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.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:54
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
#define AVERROR_EOF
End of file.
Definition: error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
#define AVERROR(e)
Definition: error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
void av_free(void *ptr)
Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family.
char * av_strdup(const char *s) av_malloc_attrib
Duplicate a string.
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Allocate a memory block for an array with av_mallocz().
void * av_malloc_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_malloc().
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:273
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.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int size, int search_flags)
AVOptions.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition: avcodec.h:426
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:635
int width
picture width / height.
Definition: avcodec.h:598
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2054
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1050
enum AVMediaType codec_type
Definition: avcodec.h:434
AVRational framerate
Definition: avcodec.h:1750
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:795
int sample_rate
samples per second
Definition: avcodec.h:1034
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:548
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:506
enum AVCodecID codec_id
Definition: avcodec.h:436
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:58
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVCodec.
Definition: codec.h:184
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:208
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:206
An instance of a filter.
Definition: avfilter.h:392
A linked-list of the inputs/outputs of the filter chain.
Definition: avfilter.h:1015
AVFilterContext * filter_ctx
filter context associated to this input/output
Definition: avfilter.h:1020
int pad_idx
index of the filt_ctx pad to use for linking
Definition: avfilter.h:1023
char * name
unique name for this input/output in the list
Definition: avfilter.h:1017
struct AVFilterInOut * next
next input/input in the list, NULL if this is the last
Definition: avfilter.h:1026
Filter definition.
Definition: avfilter.h:161
Format I/O context.
Definition: avformat.h:1104
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1160
AVIOContext * pb
I/O context.
Definition: avformat.h:1146
const struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1123
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1172
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:437
int64_t best_effort_timestamp
frame timestamp estimated using various heuristics, in stream time base
Definition: frame.h:612
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:427
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS,...
Definition: avformat.h:527
This structure stores compressed data.
Definition: packet.h:351
int stream_index
Definition: packet.h:376
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
Stream structure.
Definition: avformat.h:838
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:877
AVPacket * enc_pkt
Definition: transcode.c:48
AVFilterContext * buffersrc_ctx
Definition: transcode.c:45
AVFilterContext * buffersink_ctx
Definition: transcode.c:44
AVFilterGraph * filter_graph
Definition: transcode.c:46
AVFrame * filtered_frame
Definition: transcode.c:49
AVFrame * dec_frame
Definition: transcode.c:57
AVCodecContext * dec_ctx
Definition: transcode.c:54
AVCodecContext * enc_ctx
Definition: transcode.c:55
static int init_filters(void)
Definition: transcode.c:389
static StreamContext * stream_ctx
Definition: transcode.c:59
static int encode_write_frame(unsigned int stream_index, int flush)
Definition: transcode.c:427
static AVFormatContext * ifmt_ctx
Definition: transcode.c:41
static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
Definition: transcode.c:464
int main(int argc, char **argv)
Definition: transcode.c:513
static int init_filter(FilteringContext *fctx, AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, const char *filter_spec)
Definition: transcode.c:239
static FilteringContext * filter_ctx
Definition: transcode.c:51
static AVFormatContext * ofmt_ctx
Definition: transcode.c:42
static int open_output_file(const char *filename)
Definition: transcode.c:123
static int open_input_file(const char *filename)
Definition: transcode.c:61
static int flush_encoder(unsigned int stream_index)
Definition: transcode.c:503