Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.91 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3. #pragma once
  4. using namespace std;
  5.  
  6. #include "stdafx.h"
  7. #include <string>
  8. #include <stdio.h>
  9. #include <cstdio>
  10. #include <Windows.h>
  11. #include <stdlib.h>
  12. #define inline __inline
  13. extern "C"
  14. {
  15. #include <libavcodec/avcodec.h>
  16. #include <libavformat/avformat.h>
  17. #include <libavfilter/avfiltergraph.h>
  18. #include <libavfilter/avcodec.h>
  19. #include <libavfilter/buffersink.h>
  20. #include <libavfilter/buffersrc.h>
  21. #include <libavutil/opt.h>
  22. #include <libavutil/pixdesc.h>
  23. }
  24. #pragma comment(lib, "dev\\lib\\avcodec.lib")
  25. #pragma comment(lib, "dev\\lib\\avformat.lib")
  26. #pragma comment(lib, "dev\\lib\\avfilter.lib")
  27. #pragma comment(lib, "dev\\lib\\avutil.lib")
  28.  
  29.  
  30. static AVFormatContext *ifmt_ctx;
  31. static AVFormatContext *ofmt_ctx;
  32. typedef struct FilteringContext {
  33. AVFilterContext *buffersink_ctx;
  34. AVFilterContext *buffersrc_ctx;
  35. AVFilterGraph *filter_graph;
  36. } FilteringContext;
  37. static FilteringContext *filter_ctx;
  38.  
  39. static int open_input_file(const char *filename)
  40. {
  41. int ret;
  42. unsigned int i;
  43. ifmt_ctx = NULL;
  44. if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0)
  45. {
  46. av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
  47. return ret;
  48.  
  49. }
  50.  
  51. if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0)
  52. {
  53. av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
  54. return ret;
  55.  
  56. }
  57.  
  58. for (i = 0; i < ifmt_ctx->nb_streams; i++)
  59. {
  60. AVStream *stream;
  61. AVCodecContext *codec_ctx;
  62. stream = ifmt_ctx->streams[i];
  63. codec_ctx = stream->codec;
  64. /* Reencode video & audio and remux subtitles etc. */
  65. if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  66. /* Open decoder */
  67. ret = avcodec_open2(codec_ctx, avcodec_find_decoder(codec_ctx->codec_id), NULL);
  68. if (ret < 0) {
  69. av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
  70. return ret;
  71.  
  72. }
  73.  
  74. }
  75.  
  76. }
  77.  
  78. av_dump_format(ifmt_ctx, 0, filename, 0);
  79. return 0;
  80. }
  81.  
  82. static int open_output_file(const char *filename)
  83. {
  84. AVStream *out_stream;
  85. AVStream *in_stream;
  86. AVCodecContext *dec_ctx, *enc_ctx;
  87. AVCodec *encoder;
  88. int ret;
  89. unsigned int i;
  90.  
  91. ofmt_ctx = NULL;
  92. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
  93. if (!ofmt_ctx) {
  94. av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
  95. return AVERROR_UNKNOWN;
  96. }
  97.  
  98.  
  99. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  100. out_stream = avformat_new_stream(ofmt_ctx, NULL);
  101. if (!out_stream) {
  102. av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
  103. return AVERROR_UNKNOWN;
  104. }
  105.  
  106. in_stream = ifmt_ctx->streams[i];
  107. dec_ctx = in_stream->codec;
  108. enc_ctx = out_stream->codec;
  109.  
  110. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
  111. || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  112. /* in this example, we choose transcoding to same codec */
  113. encoder = avcodec_find_encoder(dec_ctx->codec_id);
  114. if (!encoder) {
  115. av_log(NULL, AV_LOG_FATAL, "Neccessary encoder not found\n");
  116. return AVERROR_INVALIDDATA;
  117. }
  118.  
  119. /* In this example, we transcode to same properties (picture size,
  120. * sample rate etc.). These properties can be changed for output
  121. * streams easily using filters */
  122. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  123. enc_ctx->height = dec_ctx->height;
  124. enc_ctx->width = dec_ctx->width;
  125. enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
  126. /* take first format from list of supported formats */
  127. enc_ctx->pix_fmt = encoder->pix_fmts[0];
  128. /* video time_base can be set to whatever is handy and supported by encoder */
  129. enc_ctx->time_base = dec_ctx->time_base;
  130. }
  131. else {
  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 = { 1, enc_ctx->sample_rate };
  138. }
  139.  
  140. /* Third parameter can be used to pass settings to encoder */
  141. ret = avcodec_open2(enc_ctx, encoder, NULL);
  142. if (ret < 0) {
  143. av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
  144. return ret;
  145. }
  146. }
  147. else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
  148. av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
  149. return AVERROR_INVALIDDATA;
  150. }
  151. else {
  152. /* if this stream must be remuxed */
  153. ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,
  154. ifmt_ctx->streams[i]->codec);
  155. if (ret < 0) {
  156. av_log(NULL, AV_LOG_ERROR, "Copying stream context failed\n");
  157. return ret;
  158. }
  159. }
  160.  
  161. if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  162. enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
  163.  
  164. }
  165. av_dump_format(ofmt_ctx, 0, filename, 1);
  166.  
  167. if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
  168. ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
  169. if (ret < 0) {
  170. av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
  171. return ret;
  172. }
  173. }
  174.  
  175. /* init muxer, write output file header */
  176. ret = avformat_write_header(ofmt_ctx, NULL);
  177. if (ret < 0) {
  178. av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
  179. return ret;
  180. }
  181.  
  182. return 0;
  183. }
  184.  
  185. static int init_filter(FilteringContext* fctx, AVCodecContext *dec_ctx,
  186. AVCodecContext *enc_ctx, const char *filter_spec)
  187. {
  188. char args[512];
  189. int ret = 0;
  190. AVFilter *buffersrc = NULL;
  191. AVFilter *buffersink = NULL;
  192. AVFilterContext *buffersrc_ctx = NULL;
  193. AVFilterContext *buffersink_ctx = NULL;
  194. AVFilterInOut *outputs = avfilter_inout_alloc();
  195. AVFilterInOut *inputs = avfilter_inout_alloc();
  196. AVFilterGraph *filter_graph = avfilter_graph_alloc();
  197.  
  198. if (!outputs || !inputs || !filter_graph) {
  199. ret = AVERROR(ENOMEM);
  200. goto end;
  201.  
  202. }
  203.  
  204. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  205. buffersrc = avfilter_get_by_name("buffer");
  206. buffersink = avfilter_get_by_name("buffersink");
  207. if (!buffersrc || !buffersink) {
  208. av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
  209. ret = AVERROR_UNKNOWN;
  210. goto end;
  211.  
  212. }
  213.  
  214. _snprintf_s(args, sizeof(args),
  215. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
  216. dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
  217. dec_ctx->time_base.num, dec_ctx->time_base.den,
  218. dec_ctx->sample_aspect_ratio.num,
  219. dec_ctx->sample_aspect_ratio.den);
  220.  
  221. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  222. args, NULL, filter_graph);
  223. if (ret < 0) {
  224. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
  225. goto end;
  226.  
  227. }
  228.  
  229. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  230. NULL, NULL, filter_graph);
  231. if (ret < 0) {
  232. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
  233. goto end;
  234.  
  235. }
  236.  
  237. ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
  238. (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
  239. AV_OPT_SEARCH_CHILDREN);
  240. if (ret < 0) {
  241. av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
  242. goto end;
  243.  
  244. }
  245.  
  246. }
  247. else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  248. buffersrc = avfilter_get_by_name("abuffer");
  249. buffersink = avfilter_get_by_name("abuffersink");
  250. if (!buffersrc || !buffersink) {
  251. av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
  252. ret = AVERROR_UNKNOWN;
  253. goto end;
  254.  
  255. }
  256.  
  257. if (!dec_ctx->channel_layout)
  258. dec_ctx->channel_layout =
  259. av_get_default_channel_layout(dec_ctx->channels);
  260. _snprintf_s(args, sizeof(args),
  261. "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
  262. dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
  263. av_get_sample_fmt_name(dec_ctx->sample_fmt),
  264. dec_ctx->channel_layout);
  265. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  266. args, NULL, filter_graph);
  267. if (ret < 0) {
  268. av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
  269. goto end;
  270.  
  271. }
  272.  
  273. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  274. NULL, NULL, filter_graph);
  275. if (ret < 0) {
  276. av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
  277. goto end;
  278.  
  279. }
  280.  
  281. ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
  282. (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
  283. AV_OPT_SEARCH_CHILDREN);
  284. if (ret < 0) {
  285. av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
  286. goto end;
  287.  
  288. }
  289.  
  290. ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
  291. (uint8_t*)&enc_ctx->channel_layout,
  292. sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
  293. if (ret < 0) {
  294. av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
  295. goto end;
  296.  
  297. }
  298.  
  299. ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
  300. (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
  301. AV_OPT_SEARCH_CHILDREN);
  302. if (ret < 0) {
  303. av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
  304. goto end;
  305.  
  306. }
  307.  
  308. }
  309. else {
  310. ret = AVERROR_UNKNOWN;
  311. goto end;
  312.  
  313. }
  314.  
  315. /* Endpoints for the filter graph. */
  316. outputs->name = av_strdup("in");
  317. outputs->filter_ctx = buffersrc_ctx;
  318. outputs->pad_idx = 0;
  319. outputs->next = NULL;
  320.  
  321. inputs->name = av_strdup("out");
  322. inputs->filter_ctx = buffersink_ctx;
  323. inputs->pad_idx = 0;
  324. inputs->next = NULL;
  325.  
  326. if (!outputs->name || !inputs->name) {
  327. ret = AVERROR(ENOMEM);
  328. goto end;
  329.  
  330. }
  331.  
  332. if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
  333. &inputs, &outputs, NULL)) < 0)
  334. goto end;
  335.  
  336. if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
  337. goto end;
  338.  
  339. /* Fill FilteringContext */
  340. fctx->buffersrc_ctx = buffersrc_ctx;
  341. fctx->buffersink_ctx = buffersink_ctx;
  342. fctx->filter_graph = filter_graph;
  343.  
  344. end:
  345. avfilter_inout_free(&inputs);
  346. avfilter_inout_free(&outputs);
  347.  
  348. return ret;
  349. }
  350.  
  351. static int init_filters(void)
  352. {
  353. const char *filter_spec;
  354. unsigned int i;
  355. int ret;
  356. filter_ctx = (FilteringContext*)av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
  357. if (!filter_ctx)
  358. return AVERROR(ENOMEM);
  359.  
  360. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  361. filter_ctx[i].buffersrc_ctx = NULL;
  362. filter_ctx[i].buffersink_ctx = NULL;
  363. filter_ctx[i].filter_graph = NULL;
  364. if (!(ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO
  365. || ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO))
  366. continue;
  367.  
  368.  
  369. if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  370. filter_spec = "null"; /* passthrough (dummy) filter for video */
  371. else
  372. filter_spec = "anull"; /* passthrough (dummy) filter for audio */
  373. ret = init_filter(&filter_ctx[i], ifmt_ctx->streams[i]->codec,
  374. ofmt_ctx->streams[i]->codec, filter_spec);
  375. if (ret)
  376. return ret;
  377.  
  378. }
  379. return 0;
  380. }
  381.  
  382. static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
  383. int ret;
  384. int got_frame_local;
  385. AVPacket enc_pkt;
  386. int(*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
  387. (ifmt_ctx->streams[stream_index]->codec->codec_type ==
  388. AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2;
  389.  
  390. if (!got_frame)
  391. got_frame = &got_frame_local;
  392.  
  393. av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
  394. /* encode filtered frame */
  395. enc_pkt.data = NULL;
  396. enc_pkt.size = 0;
  397. av_init_packet(&enc_pkt);
  398. ret = enc_func(ofmt_ctx->streams[stream_index]->codec, &enc_pkt,
  399. filt_frame, got_frame);
  400. av_frame_free(&filt_frame);
  401. if (ret < 0)
  402. return ret;
  403. if (!(*got_frame))
  404. return 0;
  405.  
  406. /* prepare packet for muxing */
  407. enc_pkt.stream_index = stream_index;
  408. enc_pkt.dts = av_rescale_q_rnd(enc_pkt.dts,
  409. ofmt_ctx->streams[stream_index]->codec->time_base,
  410. ofmt_ctx->streams[stream_index]->time_base,
  411. (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  412. enc_pkt.pts = av_rescale_q_rnd(enc_pkt.pts,
  413. ofmt_ctx->streams[stream_index]->codec->time_base,
  414. ofmt_ctx->streams[stream_index]->time_base,
  415. (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  416. enc_pkt.duration = av_rescale_q(enc_pkt.duration,
  417. ofmt_ctx->streams[stream_index]->codec->time_base,
  418. ofmt_ctx->streams[stream_index]->time_base);
  419.  
  420. av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
  421. /* mux encoded frame */
  422. ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
  423. return ret;
  424.  
  425. }
  426.  
  427. static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
  428. {
  429. int ret;
  430. AVFrame *filt_frame;
  431.  
  432. av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
  433. /* push the decoded frame into the filtergraph */
  434. ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
  435. frame, 0);
  436. if (ret < 0) {
  437. av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
  438. return ret;
  439.  
  440. }
  441.  
  442. /* pull filtered frames from the filtergraph */
  443. while (1) {
  444. filt_frame = av_frame_alloc();
  445. if (!filt_frame) {
  446. ret = AVERROR(ENOMEM);
  447. break;
  448.  
  449. }
  450. av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
  451. ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
  452. filt_frame);
  453. if (ret < 0) {
  454. /* if no more frames for output - returns AVERROR(EAGAIN)
  455. * if flushed and no more frames for output - returns AVERROR_EOF
  456. * rewrite retcode to 0 to show it as normal procedure completion
  457. */
  458. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  459. ret = 0;
  460. av_frame_free(&filt_frame);
  461. break;
  462.  
  463. }
  464.  
  465. filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
  466. ret = encode_write_frame(filt_frame, stream_index, NULL);
  467. if (ret < 0)
  468. break;
  469.  
  470. }
  471.  
  472. return ret;
  473. }
  474.  
  475. static int flush_encoder(unsigned int stream_index)
  476. {
  477. int ret;
  478. int got_frame;
  479.  
  480. if (!(ofmt_ctx->streams[stream_index]->codec->codec->capabilities &
  481. CODEC_CAP_DELAY))
  482. return 0;
  483.  
  484. while (1) {
  485. av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
  486. ret = encode_write_frame(NULL, stream_index, &got_frame);
  487. if (ret < 0)
  488. break;
  489. if (!got_frame)
  490. return 0;
  491.  
  492. }
  493. return ret;
  494. }
  495.  
  496.  
  497. int main(int argc, _TCHAR* argv[])
  498. {
  499. printf("Hello");
  500. DWORD time = 5000;
  501. Sleep(time);
  502. int ret;
  503. AVPacket packet;
  504. packet.data = NULL;
  505. packet.size = 0;
  506. AVFrame *frame = NULL;
  507. enum AVMediaType type;
  508. unsigned int stream_index;
  509. unsigned int i;
  510. int got_frame;
  511. int(*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
  512. if (argc != 3) {
  513. av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
  514. return 1;
  515.  
  516. }
  517.  
  518. av_register_all();
  519. avfilter_register_all();
  520.  
  521. if ((ret = open_input_file((char *)argv[1])) < 0)
  522. goto end;
  523. if ((ret = open_output_file((char *)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]->codec->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. }
  545. packet.dts = av_rescale_q_rnd(packet.dts,
  546. ifmt_ctx->streams[stream_index]->time_base,
  547. ifmt_ctx->streams[stream_index]->codec->time_base,
  548. (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  549. packet.pts = av_rescale_q_rnd(packet.pts,
  550. ifmt_ctx->streams[stream_index]->time_base,
  551. ifmt_ctx->streams[stream_index]->codec->time_base,
  552. (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  553. dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
  554. avcodec_decode_audio4;
  555. ret = dec_func(ifmt_ctx->streams[stream_index]->codec, frame,
  556. &got_frame, &packet);
  557. if (ret < 0) {
  558. av_frame_free(&frame);
  559. av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
  560. break;
  561.  
  562. }
  563.  
  564. if (got_frame) {
  565. frame->pts = av_frame_get_best_effort_timestamp(frame);
  566. ret = filter_encode_write_frame(frame, stream_index);
  567. av_frame_free(&frame);
  568. if (ret < 0)
  569. goto end;
  570.  
  571. }
  572. else {
  573. av_frame_free(&frame);
  574.  
  575. }
  576.  
  577. }
  578. else {
  579. /* remux this frame without reencoding */
  580. packet.dts = av_rescale_q_rnd(packet.dts,
  581. ifmt_ctx->streams[stream_index]->time_base,
  582. ofmt_ctx->streams[stream_index]->time_base,
  583. (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  584. packet.pts = av_rescale_q_rnd(packet.pts,
  585. ifmt_ctx->streams[stream_index]->time_base,
  586. ofmt_ctx->streams[stream_index]->time_base,
  587. (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
  588.  
  589. ret = av_interleaved_write_frame(ofmt_ctx, &packet);
  590. if (ret < 0)
  591. goto end;
  592.  
  593. }
  594. av_free_packet(&packet);
  595.  
  596. }
  597.  
  598. /* flush filters and encoders */
  599. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  600. /* flush filter */
  601. if (!filter_ctx[i].filter_graph)
  602. continue;
  603. ret = filter_encode_write_frame(NULL, i);
  604. if (ret < 0) {
  605. av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
  606. goto end;
  607.  
  608. }
  609.  
  610. /* flush encoder */
  611. ret = flush_encoder(i);
  612. if (ret < 0) {
  613. av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
  614. goto end;
  615.  
  616. }
  617.  
  618. }
  619.  
  620. av_write_trailer(ofmt_ctx);
  621. end:
  622. av_free_packet(&packet);
  623. av_frame_free(&frame);
  624. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  625. avcodec_close(ifmt_ctx->streams[i]->codec);
  626. if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && ofmt_ctx->streams[i]->codec)
  627. avcodec_close(ofmt_ctx->streams[i]->codec);
  628. if (filter_ctx && filter_ctx[i].filter_graph)
  629. avfilter_graph_free(&filter_ctx[i].filter_graph);
  630.  
  631. }
  632. av_free(filter_ctx);
  633. avformat_close_input(&ifmt_ctx);
  634. if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
  635. avio_close(ofmt_ctx->pb);
  636. avformat_free_context(ofmt_ctx);
  637. if (ret < 0)
  638. {
  639. //av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret)); //some problemin this shit
  640. printf("error");
  641. }
  642.  
  643. return ret ? 1 : 0;
  644. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement