You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

622 lines
22KB

  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. * @file
  26. * API example for demuxing, decoding, filtering, encoding and muxing
  27. * @example transcoding.c
  28. */
  29. #include <libavcodec/avcodec.h>
  30. #include <libavformat/avformat.h>
  31. #include <libavfilter/buffersink.h>
  32. #include <libavfilter/buffersrc.h>
  33. #include <libavutil/opt.h>
  34. #include <libavutil/pixdesc.h>
  35. static AVFormatContext *ifmt_ctx;
  36. static AVFormatContext *ofmt_ctx;
  37. typedef struct FilteringContext {
  38. AVFilterContext *buffersink_ctx;
  39. AVFilterContext *buffersrc_ctx;
  40. AVFilterGraph *filter_graph;
  41. AVPacket *enc_pkt;
  42. AVFrame *filtered_frame;
  43. } FilteringContext;
  44. static FilteringContext *filter_ctx;
  45. typedef struct StreamContext {
  46. AVCodecContext *dec_ctx;
  47. AVCodecContext *enc_ctx;
  48. AVFrame *dec_frame;
  49. } StreamContext;
  50. static StreamContext *stream_ctx;
  51. static int open_input_file(const char *filename)
  52. {
  53. int ret;
  54. unsigned int i;
  55. ifmt_ctx = NULL;
  56. if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
  57. av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
  58. return ret;
  59. }
  60. if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
  61. av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
  62. return ret;
  63. }
  64. stream_ctx = av_mallocz_array(ifmt_ctx->nb_streams, sizeof(*stream_ctx));
  65. if (!stream_ctx)
  66. return AVERROR(ENOMEM);
  67. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  68. AVStream *stream = ifmt_ctx->streams[i];
  69. AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
  70. AVCodecContext *codec_ctx;
  71. if (!dec) {
  72. av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i);
  73. return AVERROR_DECODER_NOT_FOUND;
  74. }
  75. codec_ctx = avcodec_alloc_context3(dec);
  76. if (!codec_ctx) {
  77. av_log(NULL, AV_LOG_ERROR, "Failed to allocate the decoder context for stream #%u\n", i);
  78. return AVERROR(ENOMEM);
  79. }
  80. ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar);
  81. if (ret < 0) {
  82. av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context "
  83. "for stream #%u\n", i);
  84. return ret;
  85. }
  86. /* Reencode video & audio and remux subtitles etc. */
  87. if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
  88. || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  89. if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
  90. codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, stream, NULL);
  91. /* Open decoder */
  92. ret = avcodec_open2(codec_ctx, dec, NULL);
  93. if (ret < 0) {
  94. av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
  95. return ret;
  96. }
  97. }
  98. stream_ctx[i].dec_ctx = codec_ctx;
  99. stream_ctx[i].dec_frame = av_frame_alloc();
  100. if (!stream_ctx[i].dec_frame)
  101. return AVERROR(ENOMEM);
  102. }
  103. av_dump_format(ifmt_ctx, 0, filename, 0);
  104. return 0;
  105. }
  106. static int open_output_file(const char *filename)
  107. {
  108. AVStream *out_stream;
  109. AVStream *in_stream;
  110. AVCodecContext *dec_ctx, *enc_ctx;
  111. AVCodec *encoder;
  112. int ret;
  113. unsigned int i;
  114. ofmt_ctx = NULL;
  115. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
  116. if (!ofmt_ctx) {
  117. av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
  118. return AVERROR_UNKNOWN;
  119. }
  120. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  121. out_stream = avformat_new_stream(ofmt_ctx, NULL);
  122. if (!out_stream) {
  123. av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
  124. return AVERROR_UNKNOWN;
  125. }
  126. in_stream = ifmt_ctx->streams[i];
  127. dec_ctx = stream_ctx[i].dec_ctx;
  128. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
  129. || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  130. /* in this example, we choose transcoding to same codec */
  131. encoder = avcodec_find_encoder(dec_ctx->codec_id);
  132. if (!encoder) {
  133. av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. enc_ctx = avcodec_alloc_context3(encoder);
  137. if (!enc_ctx) {
  138. av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
  139. return AVERROR(ENOMEM);
  140. }
  141. /* In this example, we transcode to same properties (picture size,
  142. * sample rate etc.). These properties can be changed for output
  143. * streams easily using filters */
  144. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  145. enc_ctx->height = dec_ctx->height;
  146. enc_ctx->width = dec_ctx->width;
  147. enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
  148. /* take first format from list of supported formats */
  149. if (encoder->pix_fmts)
  150. enc_ctx->pix_fmt = encoder->pix_fmts[0];
  151. else
  152. enc_ctx->pix_fmt = dec_ctx->pix_fmt;
  153. /* video time_base can be set to whatever is handy and supported by encoder */
  154. enc_ctx->time_base = av_inv_q(dec_ctx->framerate);
  155. } else {
  156. enc_ctx->sample_rate = dec_ctx->sample_rate;
  157. enc_ctx->channel_layout = dec_ctx->channel_layout;
  158. enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
  159. /* take first format from list of supported formats */
  160. enc_ctx->sample_fmt = encoder->sample_fmts[0];
  161. enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
  162. }
  163. if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  164. enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  165. /* Third parameter can be used to pass settings to encoder */
  166. ret = avcodec_open2(enc_ctx, encoder, NULL);
  167. if (ret < 0) {
  168. av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
  169. return ret;
  170. }
  171. ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
  172. if (ret < 0) {
  173. av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
  174. return ret;
  175. }
  176. out_stream->time_base = enc_ctx->time_base;
  177. stream_ctx[i].enc_ctx = enc_ctx;
  178. } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
  179. av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
  180. return AVERROR_INVALIDDATA;
  181. } else {
  182. /* if this stream must be remuxed */
  183. ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
  184. if (ret < 0) {
  185. av_log(NULL, AV_LOG_ERROR, "Copying parameters for stream #%u failed\n", i);
  186. return ret;
  187. }
  188. out_stream->time_base = in_stream->time_base;
  189. }
  190. }
  191. av_dump_format(ofmt_ctx, 0, filename, 1);
  192. if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
  193. ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
  194. if (ret < 0) {
  195. av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
  196. return ret;
  197. }
  198. }
  199. /* init muxer, write output file header */
  200. ret = avformat_write_header(ofmt_ctx, NULL);
  201. if (ret < 0) {
  202. av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
  203. return ret;
  204. }
  205. return 0;
  206. }
  207. static int init_filter(FilteringContext* fctx, AVCodecContext *dec_ctx,
  208. AVCodecContext *enc_ctx, const char *filter_spec)
  209. {
  210. char args[512];
  211. int ret = 0;
  212. const AVFilter *buffersrc = NULL;
  213. const AVFilter *buffersink = NULL;
  214. AVFilterContext *buffersrc_ctx = NULL;
  215. AVFilterContext *buffersink_ctx = NULL;
  216. AVFilterInOut *outputs = avfilter_inout_alloc();
  217. AVFilterInOut *inputs = avfilter_inout_alloc();
  218. AVFilterGraph *filter_graph = avfilter_graph_alloc();
  219. if (!outputs || !inputs || !filter_graph) {
  220. ret = AVERROR(ENOMEM);
  221. goto end;
  222. }
  223. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  224. buffersrc = avfilter_get_by_name("buffer");
  225. buffersink = avfilter_get_by_name("buffersink");
  226. if (!buffersrc || !buffersink) {
  227. av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
  228. ret = AVERROR_UNKNOWN;
  229. goto end;
  230. }
  231. snprintf(args, sizeof(args),
  232. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
  233. dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
  234. dec_ctx->time_base.num, dec_ctx->time_base.den,
  235. dec_ctx->sample_aspect_ratio.num,
  236. dec_ctx->sample_aspect_ratio.den);
  237. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  238. args, NULL, filter_graph);
  239. if (ret < 0) {
  240. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
  241. goto end;
  242. }
  243. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  244. NULL, NULL, filter_graph);
  245. if (ret < 0) {
  246. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
  247. goto end;
  248. }
  249. ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
  250. (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
  251. AV_OPT_SEARCH_CHILDREN);
  252. if (ret < 0) {
  253. av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
  254. goto end;
  255. }
  256. } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  257. buffersrc = avfilter_get_by_name("abuffer");
  258. buffersink = avfilter_get_by_name("abuffersink");
  259. if (!buffersrc || !buffersink) {
  260. av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
  261. ret = AVERROR_UNKNOWN;
  262. goto end;
  263. }
  264. if (!dec_ctx->channel_layout)
  265. dec_ctx->channel_layout =
  266. av_get_default_channel_layout(dec_ctx->channels);
  267. snprintf(args, sizeof(args),
  268. "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
  269. dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
  270. av_get_sample_fmt_name(dec_ctx->sample_fmt),
  271. dec_ctx->channel_layout);
  272. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  273. args, NULL, filter_graph);
  274. if (ret < 0) {
  275. av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
  276. goto end;
  277. }
  278. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  279. NULL, NULL, filter_graph);
  280. if (ret < 0) {
  281. av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
  282. goto end;
  283. }
  284. ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
  285. (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
  286. AV_OPT_SEARCH_CHILDREN);
  287. if (ret < 0) {
  288. av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
  289. goto end;
  290. }
  291. ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
  292. (uint8_t*)&enc_ctx->channel_layout,
  293. sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
  294. if (ret < 0) {
  295. av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
  296. goto end;
  297. }
  298. ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
  299. (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
  300. AV_OPT_SEARCH_CHILDREN);
  301. if (ret < 0) {
  302. av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
  303. goto end;
  304. }
  305. } else {
  306. ret = AVERROR_UNKNOWN;
  307. goto end;
  308. }
  309. /* Endpoints for the filter graph. */
  310. outputs->name = av_strdup("in");
  311. outputs->filter_ctx = buffersrc_ctx;
  312. outputs->pad_idx = 0;
  313. outputs->next = NULL;
  314. inputs->name = av_strdup("out");
  315. inputs->filter_ctx = buffersink_ctx;
  316. inputs->pad_idx = 0;
  317. inputs->next = NULL;
  318. if (!outputs->name || !inputs->name) {
  319. ret = AVERROR(ENOMEM);
  320. goto end;
  321. }
  322. if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
  323. &inputs, &outputs, NULL)) < 0)
  324. goto end;
  325. if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
  326. goto end;
  327. /* Fill FilteringContext */
  328. fctx->buffersrc_ctx = buffersrc_ctx;
  329. fctx->buffersink_ctx = buffersink_ctx;
  330. fctx->filter_graph = filter_graph;
  331. end:
  332. avfilter_inout_free(&inputs);
  333. avfilter_inout_free(&outputs);
  334. return ret;
  335. }
  336. static int init_filters(void)
  337. {
  338. const char *filter_spec;
  339. unsigned int i;
  340. int ret;
  341. filter_ctx = av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
  342. if (!filter_ctx)
  343. return AVERROR(ENOMEM);
  344. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  345. filter_ctx[i].buffersrc_ctx = NULL;
  346. filter_ctx[i].buffersink_ctx = NULL;
  347. filter_ctx[i].filter_graph = NULL;
  348. if (!(ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
  349. || ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
  350. continue;
  351. if (ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  352. filter_spec = "null"; /* passthrough (dummy) filter for video */
  353. else
  354. filter_spec = "anull"; /* passthrough (dummy) filter for audio */
  355. ret = init_filter(&filter_ctx[i], stream_ctx[i].dec_ctx,
  356. stream_ctx[i].enc_ctx, filter_spec);
  357. if (ret)
  358. return ret;
  359. filter_ctx[i].enc_pkt = av_packet_alloc();
  360. if (!filter_ctx[i].enc_pkt)
  361. return AVERROR(ENOMEM);
  362. filter_ctx[i].filtered_frame = av_frame_alloc();
  363. if (!filter_ctx[i].filtered_frame)
  364. return AVERROR(ENOMEM);
  365. }
  366. return 0;
  367. }
  368. static int encode_write_frame(unsigned int stream_index, int flush)
  369. {
  370. StreamContext *stream = &stream_ctx[stream_index];
  371. FilteringContext *filter = &filter_ctx[stream_index];
  372. AVFrame *filt_frame = flush ? NULL : filter->filtered_frame;
  373. AVPacket *enc_pkt = filter->enc_pkt;
  374. int ret;
  375. av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
  376. /* encode filtered frame */
  377. av_packet_unref(enc_pkt);
  378. ret = avcodec_send_frame(stream->enc_ctx, filt_frame);
  379. if (ret < 0)
  380. return ret;
  381. while (ret >= 0) {
  382. ret = avcodec_receive_packet(stream->enc_ctx, enc_pkt);
  383. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  384. return 0;
  385. /* prepare packet for muxing */
  386. enc_pkt->stream_index = stream_index;
  387. av_packet_rescale_ts(enc_pkt,
  388. stream->enc_ctx->time_base,
  389. ofmt_ctx->streams[stream_index]->time_base);
  390. av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
  391. /* mux encoded frame */
  392. ret = av_interleaved_write_frame(ofmt_ctx, enc_pkt);
  393. }
  394. return ret;
  395. }
  396. static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
  397. {
  398. FilteringContext *filter = &filter_ctx[stream_index];
  399. int ret;
  400. av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
  401. /* push the decoded frame into the filtergraph */
  402. ret = av_buffersrc_add_frame_flags(filter->buffersrc_ctx,
  403. frame, 0);
  404. if (ret < 0) {
  405. av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
  406. return ret;
  407. }
  408. /* pull filtered frames from the filtergraph */
  409. while (1) {
  410. av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
  411. ret = av_buffersink_get_frame(filter->buffersink_ctx,
  412. filter->filtered_frame);
  413. if (ret < 0) {
  414. /* if no more frames for output - returns AVERROR(EAGAIN)
  415. * if flushed and no more frames for output - returns AVERROR_EOF
  416. * rewrite retcode to 0 to show it as normal procedure completion
  417. */
  418. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  419. ret = 0;
  420. break;
  421. }
  422. filter->filtered_frame->pict_type = AV_PICTURE_TYPE_NONE;
  423. ret = encode_write_frame(stream_index, 0);
  424. av_frame_unref(filter->filtered_frame);
  425. if (ret < 0)
  426. break;
  427. }
  428. return ret;
  429. }
  430. static int flush_encoder(unsigned int stream_index)
  431. {
  432. if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
  433. AV_CODEC_CAP_DELAY))
  434. return 0;
  435. av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
  436. return encode_write_frame(stream_index, 1);
  437. }
  438. int main(int argc, char **argv)
  439. {
  440. int ret;
  441. AVPacket *packet = NULL;
  442. unsigned int stream_index;
  443. unsigned int i;
  444. if (argc != 3) {
  445. av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
  446. return 1;
  447. }
  448. if ((ret = open_input_file(argv[1])) < 0)
  449. goto end;
  450. if ((ret = open_output_file(argv[2])) < 0)
  451. goto end;
  452. if ((ret = init_filters()) < 0)
  453. goto end;
  454. if (!(packet = av_packet_alloc()))
  455. goto end;
  456. /* read all packets */
  457. while (1) {
  458. if ((ret = av_read_frame(ifmt_ctx, packet)) < 0)
  459. break;
  460. stream_index = packet->stream_index;
  461. av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
  462. stream_index);
  463. if (filter_ctx[stream_index].filter_graph) {
  464. StreamContext *stream = &stream_ctx[stream_index];
  465. av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
  466. av_packet_rescale_ts(packet,
  467. ifmt_ctx->streams[stream_index]->time_base,
  468. stream->dec_ctx->time_base);
  469. ret = avcodec_send_packet(stream->dec_ctx, packet);
  470. if (ret < 0) {
  471. av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
  472. break;
  473. }
  474. while (ret >= 0) {
  475. ret = avcodec_receive_frame(stream->dec_ctx, stream->dec_frame);
  476. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  477. break;
  478. else if (ret < 0)
  479. goto end;
  480. stream->dec_frame->pts = stream->dec_frame->best_effort_timestamp;
  481. ret = filter_encode_write_frame(stream->dec_frame, stream_index);
  482. if (ret < 0)
  483. goto end;
  484. }
  485. } else {
  486. /* remux this frame without reencoding */
  487. av_packet_rescale_ts(packet,
  488. ifmt_ctx->streams[stream_index]->time_base,
  489. ofmt_ctx->streams[stream_index]->time_base);
  490. ret = av_interleaved_write_frame(ofmt_ctx, packet);
  491. if (ret < 0)
  492. goto end;
  493. }
  494. av_packet_unref(packet);
  495. }
  496. /* flush filters and encoders */
  497. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  498. /* flush filter */
  499. if (!filter_ctx[i].filter_graph)
  500. continue;
  501. ret = filter_encode_write_frame(NULL, i);
  502. if (ret < 0) {
  503. av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
  504. goto end;
  505. }
  506. /* flush encoder */
  507. ret = flush_encoder(i);
  508. if (ret < 0) {
  509. av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
  510. goto end;
  511. }
  512. }
  513. av_write_trailer(ofmt_ctx);
  514. end:
  515. av_packet_free(&packet);
  516. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  517. avcodec_free_context(&stream_ctx[i].dec_ctx);
  518. if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && stream_ctx[i].enc_ctx)
  519. avcodec_free_context(&stream_ctx[i].enc_ctx);
  520. if (filter_ctx && filter_ctx[i].filter_graph) {
  521. avfilter_graph_free(&filter_ctx[i].filter_graph);
  522. av_packet_free(&filter_ctx[i].enc_pkt);
  523. av_frame_free(&filter_ctx[i].filtered_frame);
  524. }
  525. av_frame_free(&stream_ctx[i].dec_frame);
  526. }
  527. av_free(filter_ctx);
  528. av_free(stream_ctx);
  529. avformat_close_input(&ifmt_ctx);
  530. if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
  531. avio_closep(&ofmt_ctx->pb);
  532. avformat_free_context(ofmt_ctx);
  533. if (ret < 0)
  534. av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
  535. return ret ? 1 : 0;
  536. }