ffmpeg_mux.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 "ffmpeg_utils.h"
  24. #include "sync_queue.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/fifo.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/log.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/time.h"
  31. #include "libavutil/timestamp.h"
  32. #include "libavcodec/packet.h"
  33. #include "libavformat/avformat.h"
  34. #include "libavformat/avio.h"
  35. typedef struct MuxThreadContext {
  36. AVPacket *pkt;
  37. AVPacket *fix_sub_duration_pkt;
  38. } MuxThreadContext;
  39. static Muxer *mux_from_of(OutputFile *of)
  40. {
  41. return (Muxer*)of;
  42. }
  43. static int64_t filesize(AVIOContext *pb)
  44. {
  45. int64_t ret = -1;
  46. if (pb) {
  47. ret = avio_size(pb);
  48. if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too
  49. ret = avio_tell(pb);
  50. }
  51. return ret;
  52. }
  53. static void mux_log_debug_ts(OutputStream *ost, const AVPacket *pkt)
  54. {
  55. static const char *desc[] = {
  56. [LATENCY_PROBE_DEMUX] = "demux",
  57. [LATENCY_PROBE_DEC_PRE] = "decode",
  58. [LATENCY_PROBE_DEC_POST] = "decode",
  59. [LATENCY_PROBE_FILTER_PRE] = "filter",
  60. [LATENCY_PROBE_FILTER_POST] = "filter",
  61. [LATENCY_PROBE_ENC_PRE] = "encode",
  62. [LATENCY_PROBE_ENC_POST] = "encode",
  63. [LATENCY_PROBE_NB] = "mux",
  64. };
  65. char latency[512];
  66. *latency = 0;
  67. if (pkt->opaque_ref) {
  68. const FrameData *fd = (FrameData*)pkt->opaque_ref->data;
  69. int64_t now = av_gettime_relative();
  70. int64_t total = INT64_MIN;
  71. int next;
  72. for (unsigned i = 0; i < FF_ARRAY_ELEMS(fd->wallclock); i = next) {
  73. int64_t val = fd->wallclock[i];
  74. next = i + 1;
  75. if (val == INT64_MIN)
  76. continue;
  77. if (total == INT64_MIN) {
  78. total = now - val;
  79. snprintf(latency, sizeof(latency), "total:%gms", total / 1e3);
  80. }
  81. // find the next valid entry
  82. for (; next <= FF_ARRAY_ELEMS(fd->wallclock); next++) {
  83. int64_t val_next = (next == FF_ARRAY_ELEMS(fd->wallclock)) ?
  84. now : fd->wallclock[next];
  85. int64_t diff;
  86. if (val_next == INT64_MIN)
  87. continue;
  88. diff = val_next - val;
  89. // print those stages that take at least 5% of total
  90. if (100. * diff > 5. * total) {
  91. av_strlcat(latency, ", ", sizeof(latency));
  92. if (!strcmp(desc[i], desc[next]))
  93. av_strlcat(latency, desc[i], sizeof(latency));
  94. else
  95. av_strlcatf(latency, sizeof(latency), "%s-%s:",
  96. desc[i], desc[next]);
  97. av_strlcatf(latency, sizeof(latency), " %gms/%d%%",
  98. diff / 1e3, (int)(100. * diff / total));
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. av_log(ost, AV_LOG_INFO, "muxer <- pts:%s pts_time:%s dts:%s dts_time:%s "
  105. "duration:%s duration_time:%s size:%d latency(%s)\n",
  106. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
  107. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
  108. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
  109. pkt->size, *latency ? latency : "N/A");
  110. }
  111. static int mux_fixup_ts(Muxer *mux, MuxStream *ms, AVPacket *pkt)
  112. {
  113. OutputStream *ost = &ms->ost;
  114. #if FFMPEG_OPT_VSYNC_DROP
  115. if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
  116. pkt->pts = pkt->dts = AV_NOPTS_VALUE;
  117. #endif
  118. // rescale timestamps to the stream timebase
  119. if (ost->type == AVMEDIA_TYPE_AUDIO && !ost->enc) {
  120. // use av_rescale_delta() for streamcopying audio, to preserve
  121. // accuracy with coarse input timebases
  122. int duration = av_get_audio_frame_duration2(ost->st->codecpar, pkt->size);
  123. if (!duration)
  124. duration = ost->st->codecpar->frame_size;
  125. pkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
  126. (AVRational){1, ost->st->codecpar->sample_rate}, duration,
  127. &ms->ts_rescale_delta_last, ost->st->time_base);
  128. pkt->pts = pkt->dts;
  129. pkt->duration = av_rescale_q(pkt->duration, pkt->time_base, ost->st->time_base);
  130. } else
  131. av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
  132. pkt->time_base = ost->st->time_base;
  133. if (!(mux->fc->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  134. if (pkt->dts != AV_NOPTS_VALUE &&
  135. pkt->pts != AV_NOPTS_VALUE &&
  136. pkt->dts > pkt->pts) {
  137. av_log(ost, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64", replacing by guess\n",
  138. pkt->dts, pkt->pts);
  139. pkt->pts =
  140. pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1
  141. - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
  142. - FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
  143. }
  144. if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) &&
  145. pkt->dts != AV_NOPTS_VALUE &&
  146. ms->last_mux_dts != AV_NOPTS_VALUE) {
  147. int64_t max = ms->last_mux_dts + !(mux->fc->oformat->flags & AVFMT_TS_NONSTRICT);
  148. if (pkt->dts < max) {
  149. int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
  150. if (exit_on_error)
  151. loglevel = AV_LOG_ERROR;
  152. av_log(ost, loglevel, "Non-monotonic DTS; "
  153. "previous: %"PRId64", current: %"PRId64"; ",
  154. ms->last_mux_dts, pkt->dts);
  155. if (exit_on_error) {
  156. return AVERROR(EINVAL);
  157. }
  158. av_log(ost, loglevel, "changing to %"PRId64". This may result "
  159. "in incorrect timestamps in the output file.\n",
  160. max);
  161. if (pkt->pts >= pkt->dts)
  162. pkt->pts = FFMAX(pkt->pts, max);
  163. pkt->dts = max;
  164. }
  165. }
  166. }
  167. ms->last_mux_dts = pkt->dts;
  168. if (debug_ts)
  169. mux_log_debug_ts(ost, pkt);
  170. return 0;
  171. }
  172. static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
  173. {
  174. MuxStream *ms = ms_from_ost(ost);
  175. AVFormatContext *s = mux->fc;
  176. int64_t fs;
  177. uint64_t frame_num;
  178. int ret;
  179. fs = filesize(s->pb);
  180. atomic_store(&mux->last_filesize, fs);
  181. if (fs >= mux->limit_filesize) {
  182. ret = AVERROR_EOF;
  183. goto fail;
  184. }
  185. ret = mux_fixup_ts(mux, ms, pkt);
  186. if (ret < 0)
  187. goto fail;
  188. ms->data_size_mux += pkt->size;
  189. frame_num = atomic_fetch_add(&ost->packets_written, 1);
  190. pkt->stream_index = ost->index;
  191. if (ms->stats.io)
  192. enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num);
  193. ret = av_interleaved_write_frame(s, pkt);
  194. if (ret < 0) {
  195. av_log(ost, AV_LOG_ERROR,
  196. "Error submitting a packet to the muxer: %s\n",
  197. av_err2str(ret));
  198. goto fail;
  199. }
  200. return 0;
  201. fail:
  202. av_packet_unref(pkt);
  203. return ret;
  204. }
  205. static int sync_queue_process(Muxer *mux, MuxStream *ms, AVPacket *pkt, int *stream_eof)
  206. {
  207. OutputFile *of = &mux->of;
  208. if (ms->sq_idx_mux >= 0) {
  209. int ret = sq_send(mux->sq_mux, ms->sq_idx_mux, SQPKT(pkt));
  210. if (ret < 0) {
  211. if (ret == AVERROR_EOF)
  212. *stream_eof = 1;
  213. return ret;
  214. }
  215. while (1) {
  216. ret = sq_receive(mux->sq_mux, -1, SQPKT(mux->sq_pkt));
  217. if (ret < 0) {
  218. /* n.b.: We forward EOF from the sync queue, terminating muxing.
  219. * This assumes that if a muxing sync queue is present, then all
  220. * the streams use it. That is true currently, but may change in
  221. * the future, then this code needs to be revisited.
  222. */
  223. return ret == AVERROR(EAGAIN) ? 0 : ret;
  224. }
  225. ret = write_packet(mux, of->streams[ret],
  226. mux->sq_pkt);
  227. if (ret < 0)
  228. return ret;
  229. }
  230. } else if (pkt)
  231. return write_packet(mux, &ms->ost, pkt);
  232. return 0;
  233. }
  234. static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt);
  235. /* apply the output bitstream filters */
  236. static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt,
  237. OutputStream *ost, AVPacket *pkt, int *stream_eof)
  238. {
  239. MuxStream *ms = ms_from_ost(ost);
  240. const char *err_msg;
  241. int ret = 0;
  242. if (pkt && !ost->enc) {
  243. ret = of_streamcopy(&mux->of, ost, pkt);
  244. if (ret == AVERROR(EAGAIN))
  245. return 0;
  246. else if (ret == AVERROR_EOF) {
  247. av_packet_unref(pkt);
  248. pkt = NULL;
  249. ret = 0;
  250. *stream_eof = 1;
  251. } else if (ret < 0)
  252. goto fail;
  253. }
  254. // emit heartbeat for -fix_sub_duration;
  255. // we are only interested in heartbeats on on random access points.
  256. if (pkt && (pkt->flags & AV_PKT_FLAG_KEY)) {
  257. mt->fix_sub_duration_pkt->opaque = (void*)(intptr_t)PKT_OPAQUE_FIX_SUB_DURATION;
  258. mt->fix_sub_duration_pkt->pts = pkt->pts;
  259. mt->fix_sub_duration_pkt->time_base = pkt->time_base;
  260. ret = sch_mux_sub_heartbeat(mux->sch, mux->sch_idx, ms->sch_idx,
  261. mt->fix_sub_duration_pkt);
  262. if (ret < 0)
  263. goto fail;
  264. }
  265. if (ms->bsf_ctx) {
  266. int bsf_eof = 0;
  267. if (pkt)
  268. av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in);
  269. ret = av_bsf_send_packet(ms->bsf_ctx, pkt);
  270. if (ret < 0) {
  271. err_msg = "submitting a packet for bitstream filtering";
  272. goto fail;
  273. }
  274. while (!bsf_eof) {
  275. ret = av_bsf_receive_packet(ms->bsf_ctx, ms->bsf_pkt);
  276. if (ret == AVERROR(EAGAIN))
  277. return 0;
  278. else if (ret == AVERROR_EOF)
  279. bsf_eof = 1;
  280. else if (ret < 0) {
  281. av_log(ost, AV_LOG_ERROR,
  282. "Error applying bitstream filters to a packet: %s",
  283. av_err2str(ret));
  284. if (exit_on_error)
  285. return ret;
  286. continue;
  287. }
  288. if (!bsf_eof)
  289. ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out;
  290. ret = sync_queue_process(mux, ms, bsf_eof ? NULL : ms->bsf_pkt, stream_eof);
  291. if (ret < 0)
  292. goto mux_fail;
  293. }
  294. *stream_eof = 1;
  295. } else {
  296. ret = sync_queue_process(mux, ms, pkt, stream_eof);
  297. if (ret < 0)
  298. goto mux_fail;
  299. }
  300. return *stream_eof ? AVERROR_EOF : 0;
  301. mux_fail:
  302. err_msg = "submitting a packet to the muxer";
  303. fail:
  304. if (ret != AVERROR_EOF)
  305. av_log(ost, AV_LOG_ERROR, "Error %s: %s\n", err_msg, av_err2str(ret));
  306. return ret;
  307. }
  308. static void thread_set_name(OutputFile *of)
  309. {
  310. char name[16];
  311. snprintf(name, sizeof(name), "mux%d:%s", of->index, of->format->name);
  312. ff_thread_setname(name);
  313. }
  314. static void mux_thread_uninit(MuxThreadContext *mt)
  315. {
  316. av_packet_free(&mt->pkt);
  317. av_packet_free(&mt->fix_sub_duration_pkt);
  318. memset(mt, 0, sizeof(*mt));
  319. }
  320. static int mux_thread_init(MuxThreadContext *mt)
  321. {
  322. memset(mt, 0, sizeof(*mt));
  323. mt->pkt = av_packet_alloc();
  324. if (!mt->pkt)
  325. goto fail;
  326. mt->fix_sub_duration_pkt = av_packet_alloc();
  327. if (!mt->fix_sub_duration_pkt)
  328. goto fail;
  329. return 0;
  330. fail:
  331. mux_thread_uninit(mt);
  332. return AVERROR(ENOMEM);
  333. }
  334. int muxer_thread(void *arg)
  335. {
  336. Muxer *mux = arg;
  337. OutputFile *of = &mux->of;
  338. MuxThreadContext mt;
  339. int ret = 0;
  340. ret = mux_thread_init(&mt);
  341. if (ret < 0)
  342. goto finish;
  343. thread_set_name(of);
  344. while (1) {
  345. OutputStream *ost;
  346. int stream_idx, stream_eof = 0;
  347. ret = sch_mux_receive(mux->sch, of->index, mt.pkt);
  348. stream_idx = mt.pkt->stream_index;
  349. if (stream_idx < 0) {
  350. av_log(mux, AV_LOG_VERBOSE, "All streams finished\n");
  351. ret = 0;
  352. break;
  353. }
  354. ost = of->streams[mux->sch_stream_idx[stream_idx]];
  355. mt.pkt->stream_index = ost->index;
  356. mt.pkt->flags &= ~AV_PKT_FLAG_TRUSTED;
  357. ret = mux_packet_filter(mux, &mt, ost, ret < 0 ? NULL : mt.pkt, &stream_eof);
  358. av_packet_unref(mt.pkt);
  359. if (ret == AVERROR_EOF) {
  360. if (stream_eof) {
  361. sch_mux_receive_finish(mux->sch, of->index, stream_idx);
  362. } else {
  363. av_log(mux, AV_LOG_VERBOSE, "Muxer returned EOF\n");
  364. ret = 0;
  365. break;
  366. }
  367. } else if (ret < 0) {
  368. av_log(mux, AV_LOG_ERROR, "Error muxing a packet\n");
  369. break;
  370. }
  371. }
  372. finish:
  373. mux_thread_uninit(&mt);
  374. return ret;
  375. }
  376. static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt)
  377. {
  378. MuxStream *ms = ms_from_ost(ost);
  379. FrameData *fd = pkt->opaque_ref ? (FrameData*)pkt->opaque_ref->data : NULL;
  380. int64_t dts = fd ? fd->dts_est : AV_NOPTS_VALUE;
  381. int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
  382. int64_t ts_offset;
  383. if (of->recording_time != INT64_MAX &&
  384. dts >= of->recording_time + start_time)
  385. return AVERROR_EOF;
  386. if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
  387. !ms->copy_initial_nonkeyframes)
  388. return AVERROR(EAGAIN);
  389. if (!ms->streamcopy_started) {
  390. if (!ms->copy_prior_start &&
  391. (pkt->pts == AV_NOPTS_VALUE ?
  392. dts < ms->ts_copy_start :
  393. pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
  394. return AVERROR(EAGAIN);
  395. if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
  396. return AVERROR(EAGAIN);
  397. }
  398. ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
  399. if (pkt->pts != AV_NOPTS_VALUE)
  400. pkt->pts -= ts_offset;
  401. if (pkt->dts == AV_NOPTS_VALUE) {
  402. pkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, pkt->time_base);
  403. } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  404. pkt->pts = pkt->dts - ts_offset;
  405. }
  406. pkt->dts -= ts_offset;
  407. ms->streamcopy_started = 1;
  408. return 0;
  409. }
  410. int print_sdp(const char *filename);
  411. int print_sdp(const char *filename)
  412. {
  413. char sdp[16384];
  414. int j = 0, ret;
  415. AVIOContext *sdp_pb;
  416. AVFormatContext **avc;
  417. avc = av_malloc_array(nb_output_files, sizeof(*avc));
  418. if (!avc)
  419. return AVERROR(ENOMEM);
  420. for (int i = 0; i < nb_output_files; i++) {
  421. if (!strcmp(output_files[i]->format->name, "rtp")) {
  422. avc[j] = mux_from_of(output_files[i])->fc;
  423. j++;
  424. }
  425. }
  426. if (!j) {
  427. av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
  428. ret = AVERROR(EINVAL);
  429. goto fail;
  430. }
  431. ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
  432. if (ret < 0)
  433. goto fail;
  434. if (!filename) {
  435. printf("SDP:\n%s\n", sdp);
  436. fflush(stdout);
  437. } else {
  438. ret = avio_open2(&sdp_pb, filename, AVIO_FLAG_WRITE, &int_cb, NULL);
  439. if (ret < 0) {
  440. av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", filename);
  441. goto fail;
  442. }
  443. avio_print(sdp_pb, sdp);
  444. avio_closep(&sdp_pb);
  445. }
  446. fail:
  447. av_freep(&avc);
  448. return ret;
  449. }
  450. int mux_check_init(void *arg)
  451. {
  452. Muxer *mux = arg;
  453. OutputFile *of = &mux->of;
  454. AVFormatContext *fc = mux->fc;
  455. int ret;
  456. ret = avformat_write_header(fc, &mux->opts);
  457. if (ret < 0) {
  458. av_log(mux, AV_LOG_ERROR, "Could not write header (incorrect codec "
  459. "parameters ?): %s\n", av_err2str(ret));
  460. return ret;
  461. }
  462. //assert_avoptions(of->opts);
  463. mux->header_written = 1;
  464. av_dump_format(fc, of->index, fc->url, 1);
  465. atomic_fetch_add(&nb_output_dumped, 1);
  466. return 0;
  467. }
  468. static int bsf_init(MuxStream *ms)
  469. {
  470. OutputStream *ost = &ms->ost;
  471. AVBSFContext *ctx = ms->bsf_ctx;
  472. int ret;
  473. if (!ctx)
  474. return avcodec_parameters_copy(ost->st->codecpar, ost->par_in);
  475. ret = avcodec_parameters_copy(ctx->par_in, ost->par_in);
  476. if (ret < 0)
  477. return ret;
  478. ctx->time_base_in = ost->st->time_base;
  479. ret = av_bsf_init(ctx);
  480. if (ret < 0) {
  481. av_log(ms, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
  482. ctx->filter->name);
  483. return ret;
  484. }
  485. ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
  486. if (ret < 0)
  487. return ret;
  488. ost->st->time_base = ctx->time_base_out;
  489. ms->bsf_pkt = av_packet_alloc();
  490. if (!ms->bsf_pkt)
  491. return AVERROR(ENOMEM);
  492. return 0;
  493. }
  494. int of_stream_init(OutputFile *of, OutputStream *ost)
  495. {
  496. Muxer *mux = mux_from_of(of);
  497. MuxStream *ms = ms_from_ost(ost);
  498. int ret;
  499. /* initialize bitstream filters for the output stream
  500. * needs to be done here, because the codec id for streamcopy is not
  501. * known until now */
  502. ret = bsf_init(ms);
  503. if (ret < 0)
  504. return ret;
  505. if (ms->stream_duration) {
  506. ost->st->duration = av_rescale_q(ms->stream_duration, ms->stream_duration_tb,
  507. ost->st->time_base);
  508. }
  509. if (ms->sch_idx >= 0)
  510. return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx);
  511. return 0;
  512. }
  513. static int check_written(OutputFile *of)
  514. {
  515. int64_t total_packets_written = 0;
  516. int pass1_used = 1;
  517. int ret = 0;
  518. for (int i = 0; i < of->nb_streams; i++) {
  519. OutputStream *ost = of->streams[i];
  520. uint64_t packets_written = atomic_load(&ost->packets_written);
  521. total_packets_written += packets_written;
  522. if (ost->enc_ctx &&
  523. (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
  524. != AV_CODEC_FLAG_PASS1)
  525. pass1_used = 0;
  526. if (!packets_written &&
  527. (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) {
  528. av_log(ost, AV_LOG_FATAL, "Empty output stream\n");
  529. ret = err_merge(ret, AVERROR(EINVAL));
  530. }
  531. }
  532. if (!total_packets_written) {
  533. int level = AV_LOG_WARNING;
  534. if (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT) {
  535. ret = err_merge(ret, AVERROR(EINVAL));
  536. level = AV_LOG_FATAL;
  537. }
  538. av_log(of, level, "Output file is empty, nothing was encoded%s\n",
  539. pass1_used ? "" : "(check -ss / -t / -frames parameters if used)");
  540. }
  541. return ret;
  542. }
  543. static void mux_final_stats(Muxer *mux)
  544. {
  545. OutputFile *of = &mux->of;
  546. uint64_t total_packets = 0, total_size = 0;
  547. uint64_t video_size = 0, audio_size = 0, subtitle_size = 0,
  548. extra_size = 0, other_size = 0;
  549. uint8_t overhead[16] = "unknown";
  550. int64_t file_size = of_filesize(of);
  551. av_log(of, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
  552. of->index, of->url);
  553. for (int j = 0; j < of->nb_streams; j++) {
  554. OutputStream *ost = of->streams[j];
  555. MuxStream *ms = ms_from_ost(ost);
  556. const AVCodecParameters *par = ost->st->codecpar;
  557. const enum AVMediaType type = par->codec_type;
  558. const uint64_t s = ms->data_size_mux;
  559. switch (type) {
  560. case AVMEDIA_TYPE_VIDEO: video_size += s; break;
  561. case AVMEDIA_TYPE_AUDIO: audio_size += s; break;
  562. case AVMEDIA_TYPE_SUBTITLE: subtitle_size += s; break;
  563. default: other_size += s; break;
  564. }
  565. extra_size += par->extradata_size;
  566. total_size += s;
  567. total_packets += atomic_load(&ost->packets_written);
  568. av_log(of, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
  569. of->index, j, av_get_media_type_string(type));
  570. if (ost->enc) {
  571. av_log(of, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
  572. ost->frames_encoded);
  573. if (type == AVMEDIA_TYPE_AUDIO)
  574. av_log(of, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
  575. av_log(of, AV_LOG_VERBOSE, "; ");
  576. }
  577. av_log(of, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
  578. atomic_load(&ost->packets_written), s);
  579. av_log(of, AV_LOG_VERBOSE, "\n");
  580. }
  581. av_log(of, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
  582. total_packets, total_size);
  583. if (total_size && file_size > 0 && file_size >= total_size) {
  584. snprintf(overhead, sizeof(overhead), "%f%%",
  585. 100.0 * (file_size - total_size) / total_size);
  586. }
  587. av_log(of, AV_LOG_INFO,
  588. "video:%1.0fKiB audio:%1.0fKiB subtitle:%1.0fKiB other streams:%1.0fKiB "
  589. "global headers:%1.0fKiB muxing overhead: %s\n",
  590. video_size / 1024.0,
  591. audio_size / 1024.0,
  592. subtitle_size / 1024.0,
  593. other_size / 1024.0,
  594. extra_size / 1024.0,
  595. overhead);
  596. }
  597. int of_write_trailer(OutputFile *of)
  598. {
  599. Muxer *mux = mux_from_of(of);
  600. AVFormatContext *fc = mux->fc;
  601. int ret, mux_result = 0;
  602. if (!mux->header_written) {
  603. av_log(mux, AV_LOG_ERROR,
  604. "Nothing was written into output file, because "
  605. "at least one of its streams received no packets.\n");
  606. return AVERROR(EINVAL);
  607. }
  608. ret = av_write_trailer(fc);
  609. if (ret < 0) {
  610. av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
  611. mux_result = err_merge(mux_result, ret);
  612. }
  613. mux->last_filesize = filesize(fc->pb);
  614. if (!(of->format->flags & AVFMT_NOFILE)) {
  615. ret = avio_closep(&fc->pb);
  616. if (ret < 0) {
  617. av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret));
  618. mux_result = err_merge(mux_result, ret);
  619. }
  620. }
  621. mux_final_stats(mux);
  622. // check whether anything was actually written
  623. ret = check_written(of);
  624. mux_result = err_merge(mux_result, ret);
  625. return mux_result;
  626. }
  627. static void enc_stats_uninit(EncStats *es)
  628. {
  629. for (int i = 0; i < es->nb_components; i++)
  630. av_freep(&es->components[i].str);
  631. av_freep(&es->components);
  632. if (es->lock_initialized)
  633. pthread_mutex_destroy(&es->lock);
  634. es->lock_initialized = 0;
  635. }
  636. static void ost_free(OutputStream **post)
  637. {
  638. OutputStream *ost = *post;
  639. MuxStream *ms;
  640. if (!ost)
  641. return;
  642. ms = ms_from_ost(ost);
  643. enc_free(&ost->enc);
  644. if (ost->logfile) {
  645. if (fclose(ost->logfile))
  646. av_log(ms, AV_LOG_ERROR,
  647. "Error closing logfile, loss of information possible: %s\n",
  648. av_err2str(AVERROR(errno)));
  649. ost->logfile = NULL;
  650. }
  651. avcodec_parameters_free(&ost->par_in);
  652. av_bsf_free(&ms->bsf_ctx);
  653. av_packet_free(&ms->bsf_pkt);
  654. av_packet_free(&ms->pkt);
  655. av_dict_free(&ost->encoder_opts);
  656. av_freep(&ost->kf.pts);
  657. av_expr_free(ost->kf.pexpr);
  658. av_freep(&ost->logfile_prefix);
  659. av_freep(&ost->apad);
  660. av_freep(&ost->attachment_filename);
  661. av_dict_free(&ost->sws_dict);
  662. av_dict_free(&ost->swr_opts);
  663. if (ost->enc_ctx)
  664. av_freep(&ost->enc_ctx->stats_in);
  665. avcodec_free_context(&ost->enc_ctx);
  666. enc_stats_uninit(&ost->enc_stats_pre);
  667. enc_stats_uninit(&ost->enc_stats_post);
  668. enc_stats_uninit(&ms->stats);
  669. av_freep(post);
  670. }
  671. static void fc_close(AVFormatContext **pfc)
  672. {
  673. AVFormatContext *fc = *pfc;
  674. if (!fc)
  675. return;
  676. if (!(fc->oformat->flags & AVFMT_NOFILE))
  677. avio_closep(&fc->pb);
  678. avformat_free_context(fc);
  679. *pfc = NULL;
  680. }
  681. void of_free(OutputFile **pof)
  682. {
  683. OutputFile *of = *pof;
  684. Muxer *mux;
  685. if (!of)
  686. return;
  687. mux = mux_from_of(of);
  688. sq_free(&mux->sq_mux);
  689. for (int i = 0; i < of->nb_streams; i++)
  690. ost_free(&of->streams[i]);
  691. av_freep(&of->streams);
  692. av_freep(&mux->sch_stream_idx);
  693. av_dict_free(&mux->opts);
  694. av_packet_free(&mux->sq_pkt);
  695. fc_close(&mux->fc);
  696. av_freep(pof);
  697. }
  698. int64_t of_filesize(OutputFile *of)
  699. {
  700. Muxer *mux = mux_from_of(of);
  701. return atomic_load(&mux->last_filesize);
  702. }