759c628e93713e3193e61c894b44af623d5afdd3
2 * Copyright (c) 2010 Nicolas George
3 * Copyright (c) 2011 Stefano Sabatini
4 * Copyright (c) 2014 Andrey Utkin
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
27 * API example for demuxing, decoding, filtering, encoding and muxing
28 * @example transcoding.c
31 #include <libavcodec/avcodec.h>
32 #include <libavformat/avformat.h>
33 #include <libavfilter/avfiltergraph.h>
34 #include <libavfilter/avcodec.h>
35 #include <libavfilter/buffersink.h>
36 #include <libavfilter/buffersrc.h>
37 #include <libavutil/opt.h>
38 #include <libavutil/pixdesc.h>
40 static AVFormatContext
*ifmt_ctx
;
41 static AVFormatContext
*ofmt_ctx
;
42 typedef struct FilteringContext
{
43 AVFilterContext
*buffersink_ctx
;
44 AVFilterContext
*buffersrc_ctx
;
45 AVFilterGraph
*filter_graph
;
47 static FilteringContext
*filter_ctx
;
49 static int open_input_file(const char *filename
)
55 if ((ret
= avformat_open_input(&ifmt_ctx
, filename
, NULL
, NULL
)) < 0) {
56 av_log(NULL
, AV_LOG_ERROR
, "Cannot open input file\n");
60 if ((ret
= avformat_find_stream_info(ifmt_ctx
, NULL
)) < 0) {
61 av_log(NULL
, AV_LOG_ERROR
, "Cannot find stream information\n");
65 for (i
= 0; i
< ifmt_ctx
->nb_streams
; i
++) {
67 AVCodecContext
*codec_ctx
;
68 stream
= ifmt_ctx
->streams
[i
];
69 codec_ctx
= stream
->codec
;
70 /* Reencode video & audio and remux subtitles etc. */
71 if (codec_ctx
->codec_type
== AVMEDIA_TYPE_VIDEO
72 || codec_ctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
74 ret
= avcodec_open2(codec_ctx
,
75 avcodec_find_decoder(codec_ctx
->codec_id
), NULL
);
77 av_log(NULL
, AV_LOG_ERROR
, "Failed to open decoder for stream #%u\n", i
);
83 av_dump_format(ifmt_ctx
, 0, filename
, 0);
87 static int open_output_file(const char *filename
)
91 AVCodecContext
*dec_ctx
, *enc_ctx
;
97 avformat_alloc_output_context2(&ofmt_ctx
, NULL
, NULL
, filename
);
99 av_log(NULL
, AV_LOG_ERROR
, "Could not create output context\n");
100 return AVERROR_UNKNOWN
;
104 for (i
= 0; i
< ifmt_ctx
->nb_streams
; i
++) {
105 out_stream
= avformat_new_stream(ofmt_ctx
, NULL
);
107 av_log(NULL
, AV_LOG_ERROR
, "Failed allocating output stream\n");
108 return AVERROR_UNKNOWN
;
111 in_stream
= ifmt_ctx
->streams
[i
];
112 dec_ctx
= in_stream
->codec
;
113 enc_ctx
= out_stream
->codec
;
115 if (dec_ctx
->codec_type
== AVMEDIA_TYPE_VIDEO
116 || dec_ctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
117 /* in this example, we choose transcoding to same codec */
118 encoder
= avcodec_find_encoder(dec_ctx
->codec_id
);
120 /* In this example, we transcode to same properties (picture size,
121 * sample rate etc.). These properties can be changed for output
122 * streams easily using filters */
123 if (dec_ctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
124 enc_ctx
->height
= dec_ctx
->height
;
125 enc_ctx
->width
= dec_ctx
->width
;
126 enc_ctx
->sample_aspect_ratio
= dec_ctx
->sample_aspect_ratio
;
127 /* take first format from list of supported formats */
128 enc_ctx
->pix_fmt
= encoder
->pix_fmts
[0];
129 /* video time_base can be set to whatever is handy and supported by encoder */
130 enc_ctx
->time_base
= dec_ctx
->time_base
;
132 enc_ctx
->sample_rate
= dec_ctx
->sample_rate
;
133 enc_ctx
->channel_layout
= dec_ctx
->channel_layout
;
134 enc_ctx
->channels
= av_get_channel_layout_nb_channels(enc_ctx
->channel_layout
);
135 /* take first format from list of supported formats */
136 enc_ctx
->sample_fmt
= encoder
->sample_fmts
[0];
137 enc_ctx
->time_base
= (AVRational
){1, enc_ctx
->sample_rate
};
140 /* Third parameter can be used to pass settings to encoder */
141 ret
= avcodec_open2(enc_ctx
, encoder
, NULL
);
143 av_log(NULL
, AV_LOG_ERROR
, "Cannot open video encoder for stream #%u\n", i
);
146 } else if (dec_ctx
->codec_type
== AVMEDIA_TYPE_UNKNOWN
) {
147 av_log(NULL
, AV_LOG_FATAL
, "Elementary stream #%d is of unknown type, cannot proceed\n", i
);
148 return AVERROR_INVALIDDATA
;
150 /* if this stream must be remuxed */
151 ret
= avcodec_copy_context(ofmt_ctx
->streams
[i
]->codec
,
152 ifmt_ctx
->streams
[i
]->codec
);
154 av_log(NULL
, AV_LOG_ERROR
, "Copying stream context failed\n");
159 if (ofmt_ctx
->oformat
->flags
& AVFMT_GLOBALHEADER
)
160 enc_ctx
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
163 av_dump_format(ofmt_ctx
, 0, filename
, 1);
165 if (!(ofmt_ctx
->oformat
->flags
& AVFMT_NOFILE
)) {
166 ret
= avio_open(&ofmt_ctx
->pb
, filename
, AVIO_FLAG_WRITE
);
168 av_log(NULL
, AV_LOG_ERROR
, "Could not open output file '%s'", filename
);
173 /* init muxer, write output file header */
174 ret
= avformat_write_header(ofmt_ctx
, NULL
);
176 av_log(NULL
, AV_LOG_ERROR
, "Error occurred when opening output file\n");
183 static int init_filter(FilteringContext
* fctx
, AVCodecContext
*dec_ctx
,
184 AVCodecContext
*enc_ctx
, const char *filter_spec
)
188 AVFilter
*buffersrc
= NULL
;
189 AVFilter
*buffersink
= NULL
;
190 AVFilterContext
*buffersrc_ctx
= NULL
;
191 AVFilterContext
*buffersink_ctx
= NULL
;
192 AVFilterInOut
*outputs
= avfilter_inout_alloc();
193 AVFilterInOut
*inputs
= avfilter_inout_alloc();
194 AVFilterGraph
*filter_graph
= avfilter_graph_alloc();
196 if (!outputs
|| !inputs
|| !filter_graph
) {
197 ret
= AVERROR(ENOMEM
);
201 if (dec_ctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
202 buffersrc
= avfilter_get_by_name("buffer");
203 buffersink
= avfilter_get_by_name("buffersink");
204 if (!buffersrc
|| !buffersink
) {
205 av_log(NULL
, AV_LOG_ERROR
, "filtering source or sink element not found\n");
206 ret
= AVERROR_UNKNOWN
;
210 snprintf(args
, sizeof(args
),
211 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
212 dec_ctx
->width
, dec_ctx
->height
, dec_ctx
->pix_fmt
,
213 dec_ctx
->time_base
.num
, dec_ctx
->time_base
.den
,
214 dec_ctx
->sample_aspect_ratio
.num
,
215 dec_ctx
->sample_aspect_ratio
.den
);
217 ret
= avfilter_graph_create_filter(&buffersrc_ctx
, buffersrc
, "in",
218 args
, NULL
, filter_graph
);
220 av_log(NULL
, AV_LOG_ERROR
, "Cannot create buffer source\n");
224 ret
= avfilter_graph_create_filter(&buffersink_ctx
, buffersink
, "out",
225 NULL
, NULL
, filter_graph
);
227 av_log(NULL
, AV_LOG_ERROR
, "Cannot create buffer sink\n");
231 ret
= av_opt_set_bin(buffersink_ctx
, "pix_fmts",
232 (uint8_t*)&enc_ctx
->pix_fmt
, sizeof(enc_ctx
->pix_fmt
),
233 AV_OPT_SEARCH_CHILDREN
);
235 av_log(NULL
, AV_LOG_ERROR
, "Cannot set output pixel format\n");
238 } else if (dec_ctx
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
239 buffersrc
= avfilter_get_by_name("abuffer");
240 buffersink
= avfilter_get_by_name("abuffersink");
241 if (!buffersrc
|| !buffersink
) {
242 av_log(NULL
, AV_LOG_ERROR
, "filtering source or sink element not found\n");
243 ret
= AVERROR_UNKNOWN
;
247 if (!dec_ctx
->channel_layout
)
248 dec_ctx
->channel_layout
=
249 av_get_default_channel_layout(dec_ctx
->channels
);
250 snprintf(args
, sizeof(args
),
251 "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64
,
252 dec_ctx
->time_base
.num
, dec_ctx
->time_base
.den
, dec_ctx
->sample_rate
,
253 av_get_sample_fmt_name(dec_ctx
->sample_fmt
),
254 dec_ctx
->channel_layout
);
255 ret
= avfilter_graph_create_filter(&buffersrc_ctx
, buffersrc
, "in",
256 args
, NULL
, filter_graph
);
258 av_log(NULL
, AV_LOG_ERROR
, "Cannot create audio buffer source\n");
262 ret
= avfilter_graph_create_filter(&buffersink_ctx
, buffersink
, "out",
263 NULL
, NULL
, filter_graph
);
265 av_log(NULL
, AV_LOG_ERROR
, "Cannot create audio buffer sink\n");
269 ret
= av_opt_set_bin(buffersink_ctx
, "sample_fmts",
270 (uint8_t*)&enc_ctx
->sample_fmt
, sizeof(enc_ctx
->sample_fmt
),
271 AV_OPT_SEARCH_CHILDREN
);
273 av_log(NULL
, AV_LOG_ERROR
, "Cannot set output sample format\n");
277 ret
= av_opt_set_bin(buffersink_ctx
, "channel_layouts",
278 (uint8_t*)&enc_ctx
->channel_layout
,
279 sizeof(enc_ctx
->channel_layout
), AV_OPT_SEARCH_CHILDREN
);
281 av_log(NULL
, AV_LOG_ERROR
, "Cannot set output channel layout\n");
285 ret
= av_opt_set_bin(buffersink_ctx
, "sample_rates",
286 (uint8_t*)&enc_ctx
->sample_rate
, sizeof(enc_ctx
->sample_rate
),
287 AV_OPT_SEARCH_CHILDREN
);
289 av_log(NULL
, AV_LOG_ERROR
, "Cannot set output sample rate\n");
293 ret
= AVERROR_UNKNOWN
;
297 /* Endpoints for the filter graph. */
298 outputs
->name
= av_strdup("in");
299 outputs
->filter_ctx
= buffersrc_ctx
;
300 outputs
->pad_idx
= 0;
301 outputs
->next
= NULL
;
303 inputs
->name
= av_strdup("out");
304 inputs
->filter_ctx
= buffersink_ctx
;
308 if (!outputs
->name
|| !inputs
->name
) {
309 ret
= AVERROR(ENOMEM
);
313 if ((ret
= avfilter_graph_parse_ptr(filter_graph
, filter_spec
,
314 &inputs
, &outputs
, NULL
)) < 0)
317 if ((ret
= avfilter_graph_config(filter_graph
, NULL
)) < 0)
320 /* Fill FilteringContext */
321 fctx
->buffersrc_ctx
= buffersrc_ctx
;
322 fctx
->buffersink_ctx
= buffersink_ctx
;
323 fctx
->filter_graph
= filter_graph
;
326 avfilter_inout_free(&inputs
);
327 avfilter_inout_free(&outputs
);
332 static int init_filters(void)
334 const char *filter_spec
;
337 filter_ctx
= av_malloc_array(ifmt_ctx
->nb_streams
, sizeof(*filter_ctx
));
339 return AVERROR(ENOMEM
);
341 for (i
= 0; i
< ifmt_ctx
->nb_streams
; i
++) {
342 filter_ctx
[i
].buffersrc_ctx
= NULL
;
343 filter_ctx
[i
].buffersink_ctx
= NULL
;
344 filter_ctx
[i
].filter_graph
= NULL
;
345 if (!(ifmt_ctx
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
346 || ifmt_ctx
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
))
350 if (ifmt_ctx
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
)
351 filter_spec
= "null"; /* passthrough (dummy) filter for video */
353 filter_spec
= "anull"; /* passthrough (dummy) filter for audio */
354 ret
= init_filter(&filter_ctx
[i
], ifmt_ctx
->streams
[i
]->codec
,
355 ofmt_ctx
->streams
[i
]->codec
, filter_spec
);
362 static int encode_write_frame(AVFrame
*filt_frame
, unsigned int stream_index
, int *got_frame
) {
366 int (*enc_func
)(AVCodecContext
*, AVPacket
*, const AVFrame
*, int *) =
367 (ifmt_ctx
->streams
[stream_index
]->codec
->codec_type
==
368 AVMEDIA_TYPE_VIDEO
) ? avcodec_encode_video2
: avcodec_encode_audio2
;
371 got_frame
= &got_frame_local
;
373 av_log(NULL
, AV_LOG_INFO
, "Encoding frame\n");
374 /* encode filtered frame */
377 av_init_packet(&enc_pkt
);
378 ret
= enc_func(ofmt_ctx
->streams
[stream_index
]->codec
, &enc_pkt
,
379 filt_frame
, got_frame
);
380 av_frame_free(&filt_frame
);
386 /* prepare packet for muxing */
387 enc_pkt
.stream_index
= stream_index
;
388 av_packet_rescale_ts(&enc_pkt
,
389 ofmt_ctx
->streams
[stream_index
]->codec
->time_base
,
390 ofmt_ctx
->streams
[stream_index
]->time_base
);
392 av_log(NULL
, AV_LOG_DEBUG
, "Muxing frame\n");
393 /* mux encoded frame */
394 ret
= av_interleaved_write_frame(ofmt_ctx
, &enc_pkt
);
398 static int filter_encode_write_frame(AVFrame
*frame
, unsigned int stream_index
)
403 av_log(NULL
, AV_LOG_INFO
, "Pushing decoded frame to filters\n");
404 /* push the decoded frame into the filtergraph */
405 ret
= av_buffersrc_add_frame_flags(filter_ctx
[stream_index
].buffersrc_ctx
,
408 av_log(NULL
, AV_LOG_ERROR
, "Error while feeding the filtergraph\n");
412 /* pull filtered frames from the filtergraph */
414 filt_frame
= av_frame_alloc();
416 ret
= AVERROR(ENOMEM
);
419 av_log(NULL
, AV_LOG_INFO
, "Pulling filtered frame from filters\n");
420 ret
= av_buffersink_get_frame(filter_ctx
[stream_index
].buffersink_ctx
,
423 /* if no more frames for output - returns AVERROR(EAGAIN)
424 * if flushed and no more frames for output - returns AVERROR_EOF
425 * rewrite retcode to 0 to show it as normal procedure completion
427 if (ret
== AVERROR(EAGAIN
) || ret
== AVERROR_EOF
)
429 av_frame_free(&filt_frame
);
433 filt_frame
->pict_type
= AV_PICTURE_TYPE_NONE
;
434 ret
= encode_write_frame(filt_frame
, stream_index
, NULL
);
442 static int flush_encoder(unsigned int stream_index
)
447 if (!(ofmt_ctx
->streams
[stream_index
]->codec
->codec
->capabilities
&
452 av_log(NULL
, AV_LOG_INFO
, "Flushing stream #%u encoder\n", stream_index
);
453 ret
= encode_write_frame(NULL
, stream_index
, &got_frame
);
462 int main(int argc
, char **argv
)
465 AVPacket packet
= { .data
= NULL
, .size
= 0 };
466 AVFrame
*frame
= NULL
;
467 enum AVMediaType type
;
468 unsigned int stream_index
;
471 int (*dec_func
)(AVCodecContext
*, AVFrame
*, int *, const AVPacket
*);
474 av_log(NULL
, AV_LOG_ERROR
, "Usage: %s <input file> <output file>\n", argv
[0]);
479 avfilter_register_all();
481 if ((ret
= open_input_file(argv
[1])) < 0)
483 if ((ret
= open_output_file(argv
[2])) < 0)
485 if ((ret
= init_filters()) < 0)
488 /* read all packets */
490 if ((ret
= av_read_frame(ifmt_ctx
, &packet
)) < 0)
492 stream_index
= packet
.stream_index
;
493 type
= ifmt_ctx
->streams
[packet
.stream_index
]->codec
->codec_type
;
494 av_log(NULL
, AV_LOG_DEBUG
, "Demuxer gave frame of stream_index %u\n",
497 if (filter_ctx
[stream_index
].filter_graph
) {
498 av_log(NULL
, AV_LOG_DEBUG
, "Going to reencode&filter the frame\n");
499 frame
= av_frame_alloc();
501 ret
= AVERROR(ENOMEM
);
504 av_packet_rescale_ts(&packet
,
505 ifmt_ctx
->streams
[stream_index
]->time_base
,
506 ifmt_ctx
->streams
[stream_index
]->codec
->time_base
);
507 dec_func
= (type
== AVMEDIA_TYPE_VIDEO
) ? avcodec_decode_video2
:
508 avcodec_decode_audio4
;
509 ret
= dec_func(ifmt_ctx
->streams
[stream_index
]->codec
, frame
,
510 &got_frame
, &packet
);
512 av_frame_free(&frame
);
513 av_log(NULL
, AV_LOG_ERROR
, "Decoding failed\n");
518 frame
->pts
= av_frame_get_best_effort_timestamp(frame
);
519 ret
= filter_encode_write_frame(frame
, stream_index
);
520 av_frame_free(&frame
);
524 av_frame_free(&frame
);
527 /* remux this frame without reencoding */
528 av_packet_rescale_ts(&packet
,
529 ifmt_ctx
->streams
[stream_index
]->time_base
,
530 ofmt_ctx
->streams
[stream_index
]->time_base
);
532 ret
= av_interleaved_write_frame(ofmt_ctx
, &packet
);
536 av_free_packet(&packet
);
539 /* flush filters and encoders */
540 for (i
= 0; i
< ifmt_ctx
->nb_streams
; i
++) {
542 if (!filter_ctx
[i
].filter_graph
)
544 ret
= filter_encode_write_frame(NULL
, i
);
546 av_log(NULL
, AV_LOG_ERROR
, "Flushing filter failed\n");
551 ret
= flush_encoder(i
);
553 av_log(NULL
, AV_LOG_ERROR
, "Flushing encoder failed\n");
558 av_write_trailer(ofmt_ctx
);
560 av_free_packet(&packet
);
561 av_frame_free(&frame
);
562 for (i
= 0; i
< ifmt_ctx
->nb_streams
; i
++) {
563 avcodec_close(ifmt_ctx
->streams
[i
]->codec
);
564 if (ofmt_ctx
&& ofmt_ctx
->nb_streams
> i
&& ofmt_ctx
->streams
[i
] && ofmt_ctx
->streams
[i
]->codec
)
565 avcodec_close(ofmt_ctx
->streams
[i
]->codec
);
566 if (filter_ctx
&& filter_ctx
[i
].filter_graph
)
567 avfilter_graph_free(&filter_ctx
[i
].filter_graph
);
570 avformat_close_input(&ifmt_ctx
);
571 if (ofmt_ctx
&& !(ofmt_ctx
->oformat
->flags
& AVFMT_NOFILE
))
572 avio_close(ofmt_ctx
->pb
);
573 avformat_free_context(ofmt_ctx
);
576 av_log(NULL
, AV_LOG_ERROR
, "Error occurred: %s\n", av_err2str(ret
));