ffmpeg_mux.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdatomic.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "ffmpeg.h"
  22. #include "ffmpeg_mux.h"
  23. #include "objpool.h"
  24. #include "sync_queue.h"
  25. #include "thread_queue.h"
  26. #include "libavutil/fifo.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/log.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/timestamp.h"
  31. #include "libavutil/thread.h"
  32. #include "libavcodec/packet.h"
  33. #include "libavformat/avformat.h"
  34. #include "libavformat/avio.h"
  35. int want_sdp = 1;
  36. static Muxer *mux_from_of(OutputFile *of)
  37. {
  38. return (Muxer*)of;
  39. }
  40. static int64_t filesize(AVIOContext *pb)
  41. {
  42. int64_t ret = -1;
  43. if (pb) {
  44. ret = avio_size(pb);
  45. if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too
  46. ret = avio_tell(pb);
  47. }
  48. return ret;
  49. }
  50. static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
  51. {
  52. MuxStream *ms = ms_from_ost(ost);
  53. AVFormatContext *s = mux->fc;
  54. AVStream *st = ost->st;
  55. int64_t fs;
  56. uint64_t frame_num;
  57. int ret;
  58. fs = filesize(s->pb);
  59. atomic_store(&mux->last_filesize, fs);
  60. if (fs >= mux->limit_filesize) {
  61. ret = AVERROR_EOF;
  62. goto fail;
  63. }
  64. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
  65. pkt->pts = pkt->dts = AV_NOPTS_VALUE;
  66. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  67. if (ost->frame_rate.num && ost->is_cfr) {
  68. if (pkt->duration > 0)
  69. av_log(ost, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
  70. pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
  71. pkt->time_base);
  72. }
  73. }
  74. av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
  75. pkt->time_base = ost->st->time_base;
  76. if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  77. if (pkt->dts != AV_NOPTS_VALUE &&
  78. pkt->pts != AV_NOPTS_VALUE &&
  79. pkt->dts > pkt->pts) {
  80. av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n",
  81. pkt->dts, pkt->pts,
  82. ost->file_index, ost->st->index);
  83. pkt->pts =
  84. pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1
  85. - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
  86. - FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
  87. }
  88. if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
  89. pkt->dts != AV_NOPTS_VALUE &&
  90. ms->last_mux_dts != AV_NOPTS_VALUE) {
  91. int64_t max = ms->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
  92. if (pkt->dts < max) {
  93. int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
  94. if (exit_on_error)
  95. loglevel = AV_LOG_ERROR;
  96. av_log(s, loglevel, "Non-monotonous DTS in output stream "
  97. "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
  98. ost->file_index, ost->st->index, ms->last_mux_dts, pkt->dts);
  99. if (exit_on_error) {
  100. ret = AVERROR(EINVAL);
  101. goto fail;
  102. }
  103. av_log(s, loglevel, "changing to %"PRId64". This may result "
  104. "in incorrect timestamps in the output file.\n",
  105. max);
  106. if (pkt->pts >= pkt->dts)
  107. pkt->pts = FFMAX(pkt->pts, max);
  108. pkt->dts = max;
  109. }
  110. }
  111. }
  112. ms->last_mux_dts = pkt->dts;
  113. ost->data_size_mux += pkt->size;
  114. frame_num = atomic_fetch_add(&ost->packets_written, 1);
  115. pkt->stream_index = ost->index;
  116. if (debug_ts) {
  117. av_log(ost, AV_LOG_INFO, "muxer <- type:%s "
  118. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
  119. av_get_media_type_string(st->codecpar->codec_type),
  120. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
  121. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
  122. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
  123. pkt->size
  124. );
  125. }
  126. if (ms->stats.io)
  127. enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num);
  128. ret = av_interleaved_write_frame(s, pkt);
  129. if (ret < 0) {
  130. print_error("av_interleaved_write_frame()", ret);
  131. goto fail;
  132. }
  133. return 0;
  134. fail:
  135. av_packet_unref(pkt);
  136. return ret;
  137. }
  138. static int sync_queue_process(Muxer *mux, OutputStream *ost, AVPacket *pkt, int *stream_eof)
  139. {
  140. OutputFile *of = &mux->of;
  141. if (ost->sq_idx_mux >= 0) {
  142. int ret = sq_send(mux->sq_mux, ost->sq_idx_mux, SQPKT(pkt));
  143. if (ret < 0) {
  144. if (ret == AVERROR_EOF)
  145. *stream_eof = 1;
  146. return ret;
  147. }
  148. while (1) {
  149. ret = sq_receive(mux->sq_mux, -1, SQPKT(mux->sq_pkt));
  150. if (ret < 0)
  151. return (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) ? 0 : ret;
  152. ret = write_packet(mux, of->streams[ret],
  153. mux->sq_pkt);
  154. if (ret < 0)
  155. return ret;
  156. }
  157. } else if (pkt)
  158. return write_packet(mux, ost, pkt);
  159. return 0;
  160. }
  161. static void thread_set_name(OutputFile *of)
  162. {
  163. char name[16];
  164. snprintf(name, sizeof(name), "mux%d:%s", of->index, of->format->name);
  165. ff_thread_setname(name);
  166. }
  167. static void *muxer_thread(void *arg)
  168. {
  169. Muxer *mux = arg;
  170. OutputFile *of = &mux->of;
  171. AVPacket *pkt = NULL;
  172. int ret = 0;
  173. pkt = av_packet_alloc();
  174. if (!pkt) {
  175. ret = AVERROR(ENOMEM);
  176. goto finish;
  177. }
  178. thread_set_name(of);
  179. while (1) {
  180. OutputStream *ost;
  181. int stream_idx, stream_eof = 0;
  182. ret = tq_receive(mux->tq, &stream_idx, pkt);
  183. if (stream_idx < 0) {
  184. av_log(mux, AV_LOG_VERBOSE, "All streams finished\n");
  185. ret = 0;
  186. break;
  187. }
  188. ost = of->streams[stream_idx];
  189. ret = sync_queue_process(mux, ost, ret < 0 ? NULL : pkt, &stream_eof);
  190. av_packet_unref(pkt);
  191. if (ret == AVERROR_EOF && stream_eof)
  192. tq_receive_finish(mux->tq, stream_idx);
  193. else if (ret < 0) {
  194. av_log(mux, AV_LOG_ERROR, "Error muxing a packet\n");
  195. break;
  196. }
  197. }
  198. finish:
  199. av_packet_free(&pkt);
  200. for (unsigned int i = 0; i < mux->fc->nb_streams; i++)
  201. tq_receive_finish(mux->tq, i);
  202. av_log(mux, AV_LOG_VERBOSE, "Terminating muxer thread\n");
  203. return (void*)(intptr_t)ret;
  204. }
  205. static int thread_submit_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
  206. {
  207. int ret = 0;
  208. if (!pkt || ost->finished & MUXER_FINISHED)
  209. goto finish;
  210. ret = tq_send(mux->tq, ost->index, pkt);
  211. if (ret < 0)
  212. goto finish;
  213. return 0;
  214. finish:
  215. if (pkt)
  216. av_packet_unref(pkt);
  217. ost->finished |= MUXER_FINISHED;
  218. tq_send_finish(mux->tq, ost->index);
  219. return ret == AVERROR_EOF ? 0 : ret;
  220. }
  221. static int queue_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
  222. {
  223. MuxStream *ms = ms_from_ost(ost);
  224. AVPacket *tmp_pkt = NULL;
  225. int ret;
  226. if (!av_fifo_can_write(ms->muxing_queue)) {
  227. size_t cur_size = av_fifo_can_read(ms->muxing_queue);
  228. size_t pkt_size = pkt ? pkt->size : 0;
  229. unsigned int are_we_over_size =
  230. (ms->muxing_queue_data_size + pkt_size) > ms->muxing_queue_data_threshold;
  231. size_t limit = are_we_over_size ? ms->max_muxing_queue_size : SIZE_MAX;
  232. size_t new_size = FFMIN(2 * cur_size, limit);
  233. if (new_size <= cur_size) {
  234. av_log(ost, AV_LOG_ERROR,
  235. "Too many packets buffered for output stream %d:%d.\n",
  236. ost->file_index, ost->st->index);
  237. return AVERROR(ENOSPC);
  238. }
  239. ret = av_fifo_grow2(ms->muxing_queue, new_size - cur_size);
  240. if (ret < 0)
  241. return ret;
  242. }
  243. if (pkt) {
  244. ret = av_packet_make_refcounted(pkt);
  245. if (ret < 0)
  246. return ret;
  247. tmp_pkt = av_packet_alloc();
  248. if (!tmp_pkt)
  249. return AVERROR(ENOMEM);
  250. av_packet_move_ref(tmp_pkt, pkt);
  251. ms->muxing_queue_data_size += tmp_pkt->size;
  252. }
  253. av_fifo_write(ms->muxing_queue, &tmp_pkt, 1);
  254. return 0;
  255. }
  256. static int submit_packet(Muxer *mux, AVPacket *pkt, OutputStream *ost)
  257. {
  258. int ret;
  259. if (mux->tq) {
  260. return thread_submit_packet(mux, ost, pkt);
  261. } else {
  262. /* the muxer is not initialized yet, buffer the packet */
  263. ret = queue_packet(mux, ost, pkt);
  264. if (ret < 0) {
  265. if (pkt)
  266. av_packet_unref(pkt);
  267. return ret;
  268. }
  269. }
  270. return 0;
  271. }
  272. void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof)
  273. {
  274. Muxer *mux = mux_from_of(of);
  275. MuxStream *ms = ms_from_ost(ost);
  276. const char *err_msg;
  277. int ret = 0;
  278. if (!eof && pkt->dts != AV_NOPTS_VALUE)
  279. ost->last_mux_dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
  280. /* apply the output bitstream filters */
  281. if (ms->bsf_ctx) {
  282. int bsf_eof = 0;
  283. ret = av_bsf_send_packet(ms->bsf_ctx, eof ? NULL : pkt);
  284. if (ret < 0) {
  285. err_msg = "submitting a packet for bitstream filtering";
  286. goto fail;
  287. }
  288. while (!bsf_eof) {
  289. ret = av_bsf_receive_packet(ms->bsf_ctx, pkt);
  290. if (ret == AVERROR(EAGAIN))
  291. return;
  292. else if (ret == AVERROR_EOF)
  293. bsf_eof = 1;
  294. else if (ret < 0) {
  295. err_msg = "applying bitstream filters to a packet";
  296. goto fail;
  297. }
  298. ret = submit_packet(mux, bsf_eof ? NULL : pkt, ost);
  299. if (ret < 0)
  300. goto mux_fail;
  301. }
  302. } else {
  303. ret = submit_packet(mux, eof ? NULL : pkt, ost);
  304. if (ret < 0)
  305. goto mux_fail;
  306. }
  307. return;
  308. mux_fail:
  309. err_msg = "submitting a packet to the muxer";
  310. fail:
  311. av_log(ost, AV_LOG_ERROR, "Error %s\n", err_msg);
  312. if (exit_on_error)
  313. exit_program(1);
  314. }
  315. static int thread_stop(Muxer *mux)
  316. {
  317. void *ret;
  318. if (!mux || !mux->tq)
  319. return 0;
  320. for (unsigned int i = 0; i < mux->fc->nb_streams; i++)
  321. tq_send_finish(mux->tq, i);
  322. pthread_join(mux->thread, &ret);
  323. tq_free(&mux->tq);
  324. return (int)(intptr_t)ret;
  325. }
  326. static void pkt_move(void *dst, void *src)
  327. {
  328. av_packet_move_ref(dst, src);
  329. }
  330. static int thread_start(Muxer *mux)
  331. {
  332. AVFormatContext *fc = mux->fc;
  333. ObjPool *op;
  334. int ret;
  335. op = objpool_alloc_packets();
  336. if (!op)
  337. return AVERROR(ENOMEM);
  338. mux->tq = tq_alloc(fc->nb_streams, mux->thread_queue_size, op, pkt_move);
  339. if (!mux->tq) {
  340. objpool_free(&op);
  341. return AVERROR(ENOMEM);
  342. }
  343. ret = pthread_create(&mux->thread, NULL, muxer_thread, (void*)mux);
  344. if (ret) {
  345. tq_free(&mux->tq);
  346. return AVERROR(ret);
  347. }
  348. /* flush the muxing queues */
  349. for (int i = 0; i < fc->nb_streams; i++) {
  350. OutputStream *ost = mux->of.streams[i];
  351. MuxStream *ms = ms_from_ost(ost);
  352. AVPacket *pkt;
  353. /* try to improve muxing time_base (only possible if nothing has been written yet) */
  354. if (!av_fifo_can_read(ms->muxing_queue))
  355. ost->mux_timebase = ost->st->time_base;
  356. while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) {
  357. ret = thread_submit_packet(mux, ost, pkt);
  358. if (pkt) {
  359. ms->muxing_queue_data_size -= pkt->size;
  360. av_packet_free(&pkt);
  361. }
  362. if (ret < 0)
  363. return ret;
  364. }
  365. }
  366. return 0;
  367. }
  368. static int print_sdp(void)
  369. {
  370. char sdp[16384];
  371. int i;
  372. int j, ret;
  373. AVIOContext *sdp_pb;
  374. AVFormatContext **avc;
  375. for (i = 0; i < nb_output_files; i++) {
  376. if (!mux_from_of(output_files[i])->header_written)
  377. return 0;
  378. }
  379. avc = av_malloc_array(nb_output_files, sizeof(*avc));
  380. if (!avc)
  381. return AVERROR(ENOMEM);
  382. for (i = 0, j = 0; i < nb_output_files; i++) {
  383. if (!strcmp(output_files[i]->format->name, "rtp")) {
  384. avc[j] = mux_from_of(output_files[i])->fc;
  385. j++;
  386. }
  387. }
  388. if (!j) {
  389. av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
  390. ret = AVERROR(EINVAL);
  391. goto fail;
  392. }
  393. ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
  394. if (ret < 0)
  395. goto fail;
  396. if (!sdp_filename) {
  397. printf("SDP:\n%s\n", sdp);
  398. fflush(stdout);
  399. } else {
  400. ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL);
  401. if (ret < 0) {
  402. av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
  403. goto fail;
  404. }
  405. avio_print(sdp_pb, sdp);
  406. avio_closep(&sdp_pb);
  407. av_freep(&sdp_filename);
  408. }
  409. // SDP successfully written, allow muxer threads to start
  410. ret = 1;
  411. fail:
  412. av_freep(&avc);
  413. return ret;
  414. }
  415. int mux_check_init(Muxer *mux)
  416. {
  417. OutputFile *of = &mux->of;
  418. AVFormatContext *fc = mux->fc;
  419. int ret, i;
  420. for (i = 0; i < fc->nb_streams; i++) {
  421. OutputStream *ost = of->streams[i];
  422. if (!ost->initialized)
  423. return 0;
  424. }
  425. ret = avformat_write_header(fc, &mux->opts);
  426. if (ret < 0) {
  427. av_log(mux, AV_LOG_ERROR, "Could not write header (incorrect codec "
  428. "parameters ?): %s\n", av_err2str(ret));
  429. return ret;
  430. }
  431. //assert_avoptions(of->opts);
  432. mux->header_written = 1;
  433. av_dump_format(fc, of->index, fc->url, 1);
  434. nb_output_dumped++;
  435. if (sdp_filename || want_sdp) {
  436. ret = print_sdp();
  437. if (ret < 0) {
  438. av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n");
  439. return ret;
  440. } else if (ret == 1) {
  441. /* SDP is written only after all the muxers are ready, so now we
  442. * start ALL the threads */
  443. for (i = 0; i < nb_output_files; i++) {
  444. ret = thread_start(mux_from_of(output_files[i]));
  445. if (ret < 0)
  446. return ret;
  447. }
  448. }
  449. } else {
  450. ret = thread_start(mux_from_of(of));
  451. if (ret < 0)
  452. return ret;
  453. }
  454. return 0;
  455. }
  456. static int bsf_init(MuxStream *ms)
  457. {
  458. OutputStream *ost = &ms->ost;
  459. AVBSFContext *ctx = ms->bsf_ctx;
  460. int ret;
  461. if (!ctx)
  462. return 0;
  463. ret = avcodec_parameters_copy(ctx->par_in, ost->st->codecpar);
  464. if (ret < 0)
  465. return ret;
  466. ctx->time_base_in = ost->st->time_base;
  467. ret = av_bsf_init(ctx);
  468. if (ret < 0) {
  469. av_log(ms, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
  470. ctx->filter->name);
  471. return ret;
  472. }
  473. ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
  474. if (ret < 0)
  475. return ret;
  476. ost->st->time_base = ctx->time_base_out;
  477. return 0;
  478. }
  479. int of_stream_init(OutputFile *of, OutputStream *ost)
  480. {
  481. Muxer *mux = mux_from_of(of);
  482. MuxStream *ms = ms_from_ost(ost);
  483. int ret;
  484. if (ost->sq_idx_mux >= 0)
  485. sq_set_tb(mux->sq_mux, ost->sq_idx_mux, ost->mux_timebase);
  486. /* initialize bitstream filters for the output stream
  487. * needs to be done here, because the codec id for streamcopy is not
  488. * known until now */
  489. ret = bsf_init(ms);
  490. if (ret < 0)
  491. return ret;
  492. ost->initialized = 1;
  493. return mux_check_init(mux);
  494. }
  495. int of_write_trailer(OutputFile *of)
  496. {
  497. Muxer *mux = mux_from_of(of);
  498. AVFormatContext *fc = mux->fc;
  499. int ret;
  500. if (!mux->tq) {
  501. av_log(mux, AV_LOG_ERROR,
  502. "Nothing was written into output file, because "
  503. "at least one of its streams received no packets.\n");
  504. return AVERROR(EINVAL);
  505. }
  506. ret = thread_stop(mux);
  507. if (ret < 0)
  508. main_return_code = ret;
  509. ret = av_write_trailer(fc);
  510. if (ret < 0) {
  511. av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
  512. return ret;
  513. }
  514. mux->last_filesize = filesize(fc->pb);
  515. if (!(of->format->flags & AVFMT_NOFILE)) {
  516. ret = avio_closep(&fc->pb);
  517. if (ret < 0) {
  518. av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret));
  519. return ret;
  520. }
  521. }
  522. return 0;
  523. }
  524. static void ost_free(OutputStream **post)
  525. {
  526. OutputStream *ost = *post;
  527. MuxStream *ms;
  528. if (!ost)
  529. return;
  530. ms = ms_from_ost(ost);
  531. if (ost->logfile) {
  532. if (fclose(ost->logfile))
  533. av_log(ms, AV_LOG_ERROR,
  534. "Error closing logfile, loss of information possible: %s\n",
  535. av_err2str(AVERROR(errno)));
  536. ost->logfile = NULL;
  537. }
  538. if (ms->muxing_queue) {
  539. AVPacket *pkt;
  540. while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0)
  541. av_packet_free(&pkt);
  542. av_fifo_freep2(&ms->muxing_queue);
  543. }
  544. av_bsf_free(&ms->bsf_ctx);
  545. av_frame_free(&ost->filtered_frame);
  546. av_frame_free(&ost->sq_frame);
  547. av_frame_free(&ost->last_frame);
  548. av_packet_free(&ost->pkt);
  549. av_dict_free(&ost->encoder_opts);
  550. av_freep(&ost->kf.pts);
  551. av_expr_free(ost->kf.pexpr);
  552. av_freep(&ost->avfilter);
  553. av_freep(&ost->logfile_prefix);
  554. av_freep(&ost->apad);
  555. #if FFMPEG_OPT_MAP_CHANNEL
  556. av_freep(&ost->audio_channels_map);
  557. ost->audio_channels_mapped = 0;
  558. #endif
  559. av_dict_free(&ost->sws_dict);
  560. av_dict_free(&ost->swr_opts);
  561. if (ost->enc_ctx)
  562. av_freep(&ost->enc_ctx->stats_in);
  563. avcodec_free_context(&ost->enc_ctx);
  564. for (int i = 0; i < ost->enc_stats_pre.nb_components; i++)
  565. av_freep(&ost->enc_stats_pre.components[i].str);
  566. av_freep(&ost->enc_stats_pre.components);
  567. for (int i = 0; i < ost->enc_stats_post.nb_components; i++)
  568. av_freep(&ost->enc_stats_post.components[i].str);
  569. av_freep(&ost->enc_stats_post.components);
  570. for (int i = 0; i < ms->stats.nb_components; i++)
  571. av_freep(&ms->stats.components[i].str);
  572. av_freep(&ms->stats.components);
  573. av_freep(post);
  574. }
  575. static void fc_close(AVFormatContext **pfc)
  576. {
  577. AVFormatContext *fc = *pfc;
  578. if (!fc)
  579. return;
  580. if (!(fc->oformat->flags & AVFMT_NOFILE))
  581. avio_closep(&fc->pb);
  582. avformat_free_context(fc);
  583. *pfc = NULL;
  584. }
  585. void of_close(OutputFile **pof)
  586. {
  587. OutputFile *of = *pof;
  588. Muxer *mux;
  589. if (!of)
  590. return;
  591. mux = mux_from_of(of);
  592. thread_stop(mux);
  593. sq_free(&of->sq_encode);
  594. sq_free(&mux->sq_mux);
  595. for (int i = 0; i < of->nb_streams; i++)
  596. ost_free(&of->streams[i]);
  597. av_freep(&of->streams);
  598. av_dict_free(&mux->opts);
  599. av_packet_free(&mux->sq_pkt);
  600. fc_close(&mux->fc);
  601. av_freep(pof);
  602. }
  603. int64_t of_filesize(OutputFile *of)
  604. {
  605. Muxer *mux = mux_from_of(of);
  606. return atomic_load(&mux->last_filesize);
  607. }