transcoding.c 21 KB

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