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 
38 static AVFormatContext *ifmt_ctx;
39 static AVFormatContext *ofmt_ctx;
40 typedef struct FilteringContext {
41     AVFilterContext *buffersink_ctx;
42     AVFilterContext *buffersrc_ctx;
43     AVFilterGraph *filter_graph;
44 } FilteringContext;
45 static FilteringContext *filter_ctx;
46 
47 typedef struct StreamContext {
48     AVCodecContext *dec_ctx;
49     AVCodecContext *enc_ctx;
50 } StreamContext;
51 static StreamContext *stream_ctx;
52 
open_input_file(const char * filename)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];
75         AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
76         AVCodecContext *codec_ctx;
77         if (!dec) {
78             av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i);
79             return AVERROR_DECODER_NOT_FOUND;
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 
open_output_file(const char * filename)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;
169                 enc_ctx->channels = av_get_channel_layout_nb_channels(enc_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             if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
176                 enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
177 
178             /* Third parameter can be used to pass settings to encoder */
179             ret = avcodec_open2(enc_ctx, encoder, NULL);
180             if (ret < 0) {
181                 av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
182                 return ret;
183             }
184             ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
185             if (ret < 0) {
186                 av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
187                 return ret;
188             }
189 
190             out_stream->time_base = enc_ctx->time_base;
191             stream_ctx[i].enc_ctx = enc_ctx;
192         } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
193             av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
194             return AVERROR_INVALIDDATA;
195         } else {
196             /* if this stream must be remuxed */
197             ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
198             if (ret < 0) {
199                 av_log(NULL, AV_LOG_ERROR, "Copying parameters for stream #%u failed\n", i);
200                 return ret;
201             }
202             out_stream->time_base = in_stream->time_base;
203         }
204 
205     }
206     av_dump_format(ofmt_ctx, 0, filename, 1);
207 
208     if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
209         ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
210         if (ret < 0) {
211             av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
212             return ret;
213         }
214     }
215 
216     /* init muxer, write output file header */
217     ret = avformat_write_header(ofmt_ctx, NULL);
218     if (ret < 0) {
219         av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
220         return ret;
221     }
222 
223     return 0;
224 }
225 
init_filter(FilteringContext * fctx,AVCodecContext * dec_ctx,AVCodecContext * enc_ctx,const char * filter_spec)226 static int init_filter(FilteringContext* fctx, AVCodecContext *dec_ctx,
227         AVCodecContext *enc_ctx, const char *filter_spec)
228 {
229     char args[512];
230     int ret = 0;
231     const AVFilter *buffersrc = NULL;
232     const AVFilter *buffersink = NULL;
233     AVFilterContext *buffersrc_ctx = NULL;
234     AVFilterContext *buffersink_ctx = NULL;
235     AVFilterInOut *outputs = avfilter_inout_alloc();
236     AVFilterInOut *inputs  = avfilter_inout_alloc();
237     AVFilterGraph *filter_graph = avfilter_graph_alloc();
238 
239     if (!outputs || !inputs || !filter_graph) {
240         ret = AVERROR(ENOMEM);
241         goto end;
242     }
243 
244     if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
245         buffersrc = avfilter_get_by_name("buffer");
246         buffersink = avfilter_get_by_name("buffersink");
247         if (!buffersrc || !buffersink) {
248             av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
249             ret = AVERROR_UNKNOWN;
250             goto end;
251         }
252 
253         snprintf(args, sizeof(args),
254                 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
255                 dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
256                 dec_ctx->time_base.num, dec_ctx->time_base.den,
257                 dec_ctx->sample_aspect_ratio.num,
258                 dec_ctx->sample_aspect_ratio.den);
259 
260         ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
261                 args, NULL, filter_graph);
262         if (ret < 0) {
263             av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
264             goto end;
265         }
266 
267         ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
268                 NULL, NULL, filter_graph);
269         if (ret < 0) {
270             av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
271             goto end;
272         }
273 
274         ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
275                 (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
276                 AV_OPT_SEARCH_CHILDREN);
277         if (ret < 0) {
278             av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
279             goto end;
280         }
281     } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
282         buffersrc = avfilter_get_by_name("abuffer");
283         buffersink = avfilter_get_by_name("abuffersink");
284         if (!buffersrc || !buffersink) {
285             av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
286             ret = AVERROR_UNKNOWN;
287             goto end;
288         }
289 
290         if (!dec_ctx->channel_layout)
291             dec_ctx->channel_layout =
292                 av_get_default_channel_layout(dec_ctx->channels);
293         snprintf(args, sizeof(args),
294                 "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
295                 dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
296                 av_get_sample_fmt_name(dec_ctx->sample_fmt),
297                 dec_ctx->channel_layout);
298         ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
299                 args, NULL, filter_graph);
300         if (ret < 0) {
301             av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
302             goto end;
303         }
304 
305         ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
306                 NULL, NULL, filter_graph);
307         if (ret < 0) {
308             av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
309             goto end;
310         }
311 
312         ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
313                 (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
314                 AV_OPT_SEARCH_CHILDREN);
315         if (ret < 0) {
316             av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
317             goto end;
318         }
319 
320         ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
321                 (uint8_t*)&enc_ctx->channel_layout,
322                 sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
323         if (ret < 0) {
324             av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
325             goto end;
326         }
327 
328         ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
329                 (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
330                 AV_OPT_SEARCH_CHILDREN);
331         if (ret < 0) {
332             av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
333             goto end;
334         }
335     } else {
336         ret = AVERROR_UNKNOWN;
337         goto end;
338     }
339 
340     /* Endpoints for the filter graph. */
341     outputs->name       = av_strdup("in");
342     outputs->filter_ctx = buffersrc_ctx;
343     outputs->pad_idx    = 0;
344     outputs->next       = NULL;
345 
346     inputs->name       = av_strdup("out");
347     inputs->filter_ctx = buffersink_ctx;
348     inputs->pad_idx    = 0;
349     inputs->next       = NULL;
350 
351     if (!outputs->name || !inputs->name) {
352         ret = AVERROR(ENOMEM);
353         goto end;
354     }
355 
356     if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
357                     &inputs, &outputs, NULL)) < 0)
358         goto end;
359 
360     if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
361         goto end;
362 
363     /* Fill FilteringContext */
364     fctx->buffersrc_ctx = buffersrc_ctx;
365     fctx->buffersink_ctx = buffersink_ctx;
366     fctx->filter_graph = filter_graph;
367 
368 end:
369     avfilter_inout_free(&inputs);
370     avfilter_inout_free(&outputs);
371 
372     return ret;
373 }
374 
init_filters(void)375 static int init_filters(void)
376 {
377     const char *filter_spec;
378     unsigned int i;
379     int ret;
380     filter_ctx = av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
381     if (!filter_ctx)
382         return AVERROR(ENOMEM);
383 
384     for (i = 0; i < ifmt_ctx->nb_streams; i++) {
385         filter_ctx[i].buffersrc_ctx  = NULL;
386         filter_ctx[i].buffersink_ctx = NULL;
387         filter_ctx[i].filter_graph   = NULL;
388         if (!(ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
389                 || ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
390             continue;
391 
392 
393         if (ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
394             filter_spec = "null"; /* passthrough (dummy) filter for video */
395         else
396             filter_spec = "anull"; /* passthrough (dummy) filter for audio */
397         ret = init_filter(&filter_ctx[i], stream_ctx[i].dec_ctx,
398                 stream_ctx[i].enc_ctx, filter_spec);
399         if (ret)
400             return ret;
401     }
402     return 0;
403 }
404 
encode_write_frame(AVFrame * filt_frame,unsigned int stream_index,int * got_frame)405 static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
406     int ret;
407     int got_frame_local;
408     AVPacket enc_pkt;
409     int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
410         (ifmt_ctx->streams[stream_index]->codecpar->codec_type ==
411          AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2;
412 
413     if (!got_frame)
414         got_frame = &got_frame_local;
415 
416     av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
417     /* encode filtered frame */
418     enc_pkt.data = NULL;
419     enc_pkt.size = 0;
420     av_init_packet(&enc_pkt);
421     ret = enc_func(stream_ctx[stream_index].enc_ctx, &enc_pkt,
422             filt_frame, got_frame);
423     av_frame_free(&filt_frame);
424     if (ret < 0)
425         return ret;
426     if (!(*got_frame))
427         return 0;
428 
429     /* prepare packet for muxing */
430     enc_pkt.stream_index = stream_index;
431     av_packet_rescale_ts(&enc_pkt,
432                          stream_ctx[stream_index].enc_ctx->time_base,
433                          ofmt_ctx->streams[stream_index]->time_base);
434 
435     av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
436     /* mux encoded frame */
437     ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
438     return ret;
439 }
440 
filter_encode_write_frame(AVFrame * frame,unsigned int stream_index)441 static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
442 {
443     int ret;
444     AVFrame *filt_frame;
445 
446     av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
447     /* push the decoded frame into the filtergraph */
448     ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
449             frame, 0);
450     if (ret < 0) {
451         av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
452         return ret;
453     }
454 
455     /* pull filtered frames from the filtergraph */
456     while (1) {
457         filt_frame = av_frame_alloc();
458         if (!filt_frame) {
459             ret = AVERROR(ENOMEM);
460             break;
461         }
462         av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
463         ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
464                 filt_frame);
465         if (ret < 0) {
466             /* if no more frames for output - returns AVERROR(EAGAIN)
467              * if flushed and no more frames for output - returns AVERROR_EOF
468              * rewrite retcode to 0 to show it as normal procedure completion
469              */
470             if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
471                 ret = 0;
472             av_frame_free(&filt_frame);
473             break;
474         }
475 
476         filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
477         ret = encode_write_frame(filt_frame, stream_index, NULL);
478         if (ret < 0)
479             break;
480     }
481 
482     return ret;
483 }
484 
flush_encoder(unsigned int stream_index)485 static int flush_encoder(unsigned int stream_index)
486 {
487     int ret;
488     int got_frame;
489 
490     if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
491                 AV_CODEC_CAP_DELAY))
492         return 0;
493 
494     while (1) {
495         av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
496         ret = encode_write_frame(NULL, stream_index, &got_frame);
497         if (ret < 0)
498             break;
499         if (!got_frame)
500             return 0;
501     }
502     return ret;
503 }
504 
main(int argc,char ** argv)505 int main(int argc, char **argv)
506 {
507     int ret;
508     AVPacket packet = { .data = NULL, .size = 0 };
509     AVFrame *frame = NULL;
510     enum AVMediaType type;
511     unsigned int stream_index;
512     unsigned int i;
513     int got_frame;
514     int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
515 
516     if (argc != 3) {
517         av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
518         return 1;
519     }
520 
521     if ((ret = open_input_file(argv[1])) < 0)
522         goto end;
523     if ((ret = open_output_file(argv[2])) < 0)
524         goto end;
525     if ((ret = init_filters()) < 0)
526         goto end;
527 
528     /* read all packets */
529     while (1) {
530         if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
531             break;
532         stream_index = packet.stream_index;
533         type = ifmt_ctx->streams[packet.stream_index]->codecpar->codec_type;
534         av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
535                 stream_index);
536 
537         if (filter_ctx[stream_index].filter_graph) {
538             av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
539             frame = av_frame_alloc();
540             if (!frame) {
541                 ret = AVERROR(ENOMEM);
542                 break;
543             }
544             av_packet_rescale_ts(&packet,
545                                  ifmt_ctx->streams[stream_index]->time_base,
546                                  stream_ctx[stream_index].dec_ctx->time_base);
547             dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
548                 avcodec_decode_audio4;
549             ret = dec_func(stream_ctx[stream_index].dec_ctx, frame,
550                     &got_frame, &packet);
551             if (ret < 0) {
552                 av_frame_free(&frame);
553                 av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
554                 break;
555             }
556 
557             if (got_frame) {
558                 frame->pts = frame->best_effort_timestamp;
559                 ret = filter_encode_write_frame(frame, stream_index);
560                 av_frame_free(&frame);
561                 if (ret < 0)
562                     goto end;
563             } else {
564                 av_frame_free(&frame);
565             }
566         } else {
567             /* remux this frame without reencoding */
568             av_packet_rescale_ts(&packet,
569                                  ifmt_ctx->streams[stream_index]->time_base,
570                                  ofmt_ctx->streams[stream_index]->time_base);
571 
572             ret = av_interleaved_write_frame(ofmt_ctx, &packet);
573             if (ret < 0)
574                 goto end;
575         }
576         av_packet_unref(&packet);
577     }
578 
579     /* flush filters and encoders */
580     for (i = 0; i < ifmt_ctx->nb_streams; i++) {
581         /* flush filter */
582         if (!filter_ctx[i].filter_graph)
583             continue;
584         ret = filter_encode_write_frame(NULL, i);
585         if (ret < 0) {
586             av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
587             goto end;
588         }
589 
590         /* flush encoder */
591         ret = flush_encoder(i);
592         if (ret < 0) {
593             av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
594             goto end;
595         }
596     }
597 
598     av_write_trailer(ofmt_ctx);
599 end:
600     av_packet_unref(&packet);
601     av_frame_free(&frame);
602     for (i = 0; i < ifmt_ctx->nb_streams; i++) {
603         avcodec_free_context(&stream_ctx[i].dec_ctx);
604         if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && stream_ctx[i].enc_ctx)
605             avcodec_free_context(&stream_ctx[i].enc_ctx);
606         if (filter_ctx && filter_ctx[i].filter_graph)
607             avfilter_graph_free(&filter_ctx[i].filter_graph);
608     }
609     av_free(filter_ctx);
610     av_free(stream_ctx);
611     avformat_close_input(&ifmt_ctx);
612     if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
613         avio_closep(&ofmt_ctx->pb);
614     avformat_free_context(ofmt_ctx);
615 
616     if (ret < 0)
617         av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
618 
619     return ret ? 1 : 0;
620 }
621