ffmpeg_mux.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 && ms->ts_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;
  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. *stream_eof = 1;
  250. } else if (ret < 0)
  251. goto fail;
  252. }
  253. // emit heartbeat for -fix_sub_duration;
  254. // we are only interested in heartbeats on on random access points.
  255. if (pkt && (pkt->flags & AV_PKT_FLAG_KEY)) {
  256. mt->fix_sub_duration_pkt->opaque = (void*)(intptr_t)PKT_OPAQUE_FIX_SUB_DURATION;
  257. mt->fix_sub_duration_pkt->pts = pkt->pts;
  258. mt->fix_sub_duration_pkt->time_base = pkt->time_base;
  259. ret = sch_mux_sub_heartbeat(mux->sch, mux->sch_idx, ms->sch_idx,
  260. mt->fix_sub_duration_pkt);
  261. if (ret < 0)
  262. goto fail;
  263. }
  264. if (ms->bsf_ctx) {
  265. int bsf_eof = 0;
  266. if (pkt)
  267. av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in);
  268. ret = av_bsf_send_packet(ms->bsf_ctx, pkt);
  269. if (ret < 0) {
  270. err_msg = "submitting a packet for bitstream filtering";
  271. goto fail;
  272. }
  273. while (!bsf_eof) {
  274. ret = av_bsf_receive_packet(ms->bsf_ctx, ms->bsf_pkt);
  275. if (ret == AVERROR(EAGAIN))
  276. return 0;
  277. else if (ret == AVERROR_EOF)
  278. bsf_eof = 1;
  279. else if (ret < 0) {
  280. av_log(ost, AV_LOG_ERROR,
  281. "Error applying bitstream filters to a packet: %s",
  282. av_err2str(ret));
  283. if (exit_on_error)
  284. return ret;
  285. continue;
  286. }
  287. if (!bsf_eof)
  288. ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out;
  289. ret = sync_queue_process(mux, ms, bsf_eof ? NULL : ms->bsf_pkt, stream_eof);
  290. if (ret < 0)
  291. goto mux_fail;
  292. }
  293. *stream_eof = 1;
  294. } else {
  295. ret = sync_queue_process(mux, ms, pkt, stream_eof);
  296. if (ret < 0)
  297. goto mux_fail;
  298. }
  299. return *stream_eof ? AVERROR_EOF : 0;
  300. mux_fail:
  301. err_msg = "submitting a packet to the muxer";
  302. fail:
  303. if (ret != AVERROR_EOF)
  304. av_log(ost, AV_LOG_ERROR, "Error %s: %s\n", err_msg, av_err2str(ret));
  305. return ret;
  306. }
  307. static void thread_set_name(Muxer *mux)
  308. {
  309. char name[16];
  310. snprintf(name, sizeof(name), "mux%d:%s",
  311. mux->of.index, mux->fc->oformat->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(mux);
  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. Muxer *mux = mux_from_of(output_files[i]);
  422. if (!strcmp(mux->fc->oformat->name, "rtp")) {
  423. avc[j] = mux->fc;
  424. j++;
  425. }
  426. }
  427. if (!j) {
  428. av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
  429. ret = AVERROR(EINVAL);
  430. goto fail;
  431. }
  432. ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
  433. if (ret < 0)
  434. goto fail;
  435. if (!filename) {
  436. printf("SDP:\n%s\n", sdp);
  437. fflush(stdout);
  438. } else {
  439. ret = avio_open2(&sdp_pb, filename, AVIO_FLAG_WRITE, &int_cb, NULL);
  440. if (ret < 0) {
  441. av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", filename);
  442. goto fail;
  443. }
  444. avio_print(sdp_pb, sdp);
  445. avio_closep(&sdp_pb);
  446. }
  447. fail:
  448. av_freep(&avc);
  449. return ret;
  450. }
  451. int mux_check_init(void *arg)
  452. {
  453. Muxer *mux = arg;
  454. OutputFile *of = &mux->of;
  455. AVFormatContext *fc = mux->fc;
  456. int ret;
  457. ret = avformat_write_header(fc, &mux->opts);
  458. if (ret < 0) {
  459. av_log(mux, AV_LOG_ERROR, "Could not write header (incorrect codec "
  460. "parameters ?): %s\n", av_err2str(ret));
  461. return ret;
  462. }
  463. //assert_avoptions(of->opts);
  464. mux->header_written = 1;
  465. av_dump_format(fc, of->index, fc->url, 1);
  466. atomic_fetch_add(&nb_output_dumped, 1);
  467. return 0;
  468. }
  469. static int bsf_init(MuxStream *ms)
  470. {
  471. OutputStream *ost = &ms->ost;
  472. AVBSFContext *ctx = ms->bsf_ctx;
  473. int ret;
  474. if (!ctx)
  475. return avcodec_parameters_copy(ost->st->codecpar, ost->par_in);
  476. ret = avcodec_parameters_copy(ctx->par_in, ost->par_in);
  477. if (ret < 0)
  478. return ret;
  479. ctx->time_base_in = ost->st->time_base;
  480. ret = av_bsf_init(ctx);
  481. if (ret < 0) {
  482. av_log(ms, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
  483. ctx->filter->name);
  484. return ret;
  485. }
  486. ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
  487. if (ret < 0)
  488. return ret;
  489. ost->st->time_base = ctx->time_base_out;
  490. ms->bsf_pkt = av_packet_alloc();
  491. if (!ms->bsf_pkt)
  492. return AVERROR(ENOMEM);
  493. return 0;
  494. }
  495. int of_stream_init(OutputFile *of, OutputStream *ost)
  496. {
  497. Muxer *mux = mux_from_of(of);
  498. MuxStream *ms = ms_from_ost(ost);
  499. int ret;
  500. /* initialize bitstream filters for the output stream
  501. * needs to be done here, because the codec id for streamcopy is not
  502. * known until now */
  503. ret = bsf_init(ms);
  504. if (ret < 0)
  505. return ret;
  506. if (ms->stream_duration) {
  507. ost->st->duration = av_rescale_q(ms->stream_duration, ms->stream_duration_tb,
  508. ost->st->time_base);
  509. }
  510. if (ms->sch_idx >= 0)
  511. return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx);
  512. return 0;
  513. }
  514. static int check_written(OutputFile *of)
  515. {
  516. int64_t total_packets_written = 0;
  517. int pass1_used = 1;
  518. int ret = 0;
  519. for (int i = 0; i < of->nb_streams; i++) {
  520. OutputStream *ost = of->streams[i];
  521. uint64_t packets_written = atomic_load(&ost->packets_written);
  522. total_packets_written += packets_written;
  523. if (ost->enc_ctx &&
  524. (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
  525. != AV_CODEC_FLAG_PASS1)
  526. pass1_used = 0;
  527. if (!packets_written &&
  528. (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) {
  529. av_log(ost, AV_LOG_FATAL, "Empty output stream\n");
  530. ret = err_merge(ret, AVERROR(EINVAL));
  531. }
  532. }
  533. if (!total_packets_written) {
  534. int level = AV_LOG_WARNING;
  535. if (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT) {
  536. ret = err_merge(ret, AVERROR(EINVAL));
  537. level = AV_LOG_FATAL;
  538. }
  539. av_log(of, level, "Output file is empty, nothing was encoded%s\n",
  540. pass1_used ? "" : "(check -ss / -t / -frames parameters if used)");
  541. }
  542. return ret;
  543. }
  544. static void mux_final_stats(Muxer *mux)
  545. {
  546. OutputFile *of = &mux->of;
  547. uint64_t total_packets = 0, total_size = 0;
  548. uint64_t video_size = 0, audio_size = 0, subtitle_size = 0,
  549. extra_size = 0, other_size = 0;
  550. uint8_t overhead[16] = "unknown";
  551. int64_t file_size = of_filesize(of);
  552. av_log(of, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
  553. of->index, of->url);
  554. for (int j = 0; j < of->nb_streams; j++) {
  555. OutputStream *ost = of->streams[j];
  556. MuxStream *ms = ms_from_ost(ost);
  557. const AVCodecParameters *par = ost->st->codecpar;
  558. const enum AVMediaType type = par->codec_type;
  559. const uint64_t s = ms->data_size_mux;
  560. switch (type) {
  561. case AVMEDIA_TYPE_VIDEO: video_size += s; break;
  562. case AVMEDIA_TYPE_AUDIO: audio_size += s; break;
  563. case AVMEDIA_TYPE_SUBTITLE: subtitle_size += s; break;
  564. default: other_size += s; break;
  565. }
  566. extra_size += par->extradata_size;
  567. total_size += s;
  568. total_packets += atomic_load(&ost->packets_written);
  569. av_log(of, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
  570. of->index, j, av_get_media_type_string(type));
  571. if (ost->enc) {
  572. av_log(of, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
  573. ost->frames_encoded);
  574. if (type == AVMEDIA_TYPE_AUDIO)
  575. av_log(of, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
  576. av_log(of, AV_LOG_VERBOSE, "; ");
  577. }
  578. av_log(of, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
  579. atomic_load(&ost->packets_written), s);
  580. av_log(of, AV_LOG_VERBOSE, "\n");
  581. }
  582. av_log(of, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
  583. total_packets, total_size);
  584. if (total_size && file_size > 0 && file_size >= total_size) {
  585. snprintf(overhead, sizeof(overhead), "%f%%",
  586. 100.0 * (file_size - total_size) / total_size);
  587. }
  588. av_log(of, AV_LOG_INFO,
  589. "video:%1.0fKiB audio:%1.0fKiB subtitle:%1.0fKiB other streams:%1.0fKiB "
  590. "global headers:%1.0fKiB muxing overhead: %s\n",
  591. video_size / 1024.0,
  592. audio_size / 1024.0,
  593. subtitle_size / 1024.0,
  594. other_size / 1024.0,
  595. extra_size / 1024.0,
  596. overhead);
  597. }
  598. int of_write_trailer(OutputFile *of)
  599. {
  600. Muxer *mux = mux_from_of(of);
  601. AVFormatContext *fc = mux->fc;
  602. int ret, mux_result = 0;
  603. if (!mux->header_written) {
  604. av_log(mux, AV_LOG_ERROR,
  605. "Nothing was written into output file, because "
  606. "at least one of its streams received no packets.\n");
  607. return AVERROR(EINVAL);
  608. }
  609. ret = av_write_trailer(fc);
  610. if (ret < 0) {
  611. av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
  612. mux_result = err_merge(mux_result, ret);
  613. }
  614. mux->last_filesize = filesize(fc->pb);
  615. if (!(fc->oformat->flags & AVFMT_NOFILE)) {
  616. ret = avio_closep(&fc->pb);
  617. if (ret < 0) {
  618. av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret));
  619. mux_result = err_merge(mux_result, ret);
  620. }
  621. }
  622. mux_final_stats(mux);
  623. // check whether anything was actually written
  624. ret = check_written(of);
  625. mux_result = err_merge(mux_result, ret);
  626. return mux_result;
  627. }
  628. static void enc_stats_uninit(EncStats *es)
  629. {
  630. for (int i = 0; i < es->nb_components; i++)
  631. av_freep(&es->components[i].str);
  632. av_freep(&es->components);
  633. if (es->lock_initialized)
  634. pthread_mutex_destroy(&es->lock);
  635. es->lock_initialized = 0;
  636. }
  637. static void ost_free(OutputStream **post)
  638. {
  639. OutputStream *ost = *post;
  640. MuxStream *ms;
  641. if (!ost)
  642. return;
  643. ms = ms_from_ost(ost);
  644. enc_free(&ost->enc);
  645. fg_free(&ost->fg_simple);
  646. if (ost->logfile) {
  647. if (fclose(ost->logfile))
  648. av_log(ms, AV_LOG_ERROR,
  649. "Error closing logfile, loss of information possible: %s\n",
  650. av_err2str(AVERROR(errno)));
  651. ost->logfile = NULL;
  652. }
  653. avcodec_parameters_free(&ost->par_in);
  654. av_bsf_free(&ms->bsf_ctx);
  655. av_packet_free(&ms->bsf_pkt);
  656. av_packet_free(&ms->pkt);
  657. av_dict_free(&ost->encoder_opts);
  658. av_freep(&ost->kf.pts);
  659. av_expr_free(ost->kf.pexpr);
  660. av_freep(&ost->logfile_prefix);
  661. av_freep(&ost->attachment_filename);
  662. if (ost->enc_ctx)
  663. av_freep(&ost->enc_ctx->stats_in);
  664. avcodec_free_context(&ost->enc_ctx);
  665. enc_stats_uninit(&ost->enc_stats_pre);
  666. enc_stats_uninit(&ost->enc_stats_post);
  667. enc_stats_uninit(&ms->stats);
  668. av_freep(post);
  669. }
  670. static void fc_close(AVFormatContext **pfc)
  671. {
  672. AVFormatContext *fc = *pfc;
  673. if (!fc)
  674. return;
  675. if (!(fc->oformat->flags & AVFMT_NOFILE))
  676. avio_closep(&fc->pb);
  677. avformat_free_context(fc);
  678. *pfc = NULL;
  679. }
  680. void of_free(OutputFile **pof)
  681. {
  682. OutputFile *of = *pof;
  683. Muxer *mux;
  684. if (!of)
  685. return;
  686. mux = mux_from_of(of);
  687. sq_free(&mux->sq_mux);
  688. for (int i = 0; i < of->nb_streams; i++)
  689. ost_free(&of->streams[i]);
  690. av_freep(&of->streams);
  691. av_freep(&mux->sch_stream_idx);
  692. av_dict_free(&mux->opts);
  693. av_packet_free(&mux->sq_pkt);
  694. fc_close(&mux->fc);
  695. av_freep(pof);
  696. }
  697. int64_t of_filesize(OutputFile *of)
  698. {
  699. Muxer *mux = mux_from_of(of);
  700. return atomic_load(&mux->last_filesize);
  701. }