2021-11-10 13:51:48 +00:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2022-03-31 15:33:48 +00:00
|
|
|
#include <stdatomic.h>
|
2021-11-10 13:51:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ffmpeg.h"
|
2022-10-13 10:50:19 +00:00
|
|
|
#include "ffmpeg_mux.h"
|
2023-10-25 11:48:50 +00:00
|
|
|
#include "ffmpeg_utils.h"
|
2022-06-10 12:21:42 +00:00
|
|
|
#include "sync_queue.h"
|
2021-11-10 13:51:48 +00:00
|
|
|
|
2023-11-24 19:12:52 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2021-11-10 13:51:48 +00:00
|
|
|
#include "libavutil/fifo.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/mem.h"
|
2023-11-24 19:12:52 +00:00
|
|
|
#include "libavutil/time.h"
|
2021-11-10 13:51:48 +00:00
|
|
|
#include "libavutil/timestamp.h"
|
|
|
|
|
|
|
|
#include "libavcodec/packet.h"
|
|
|
|
|
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
#include "libavformat/avio.h"
|
|
|
|
|
2023-07-07 15:09:37 +00:00
|
|
|
typedef struct MuxThreadContext {
|
|
|
|
AVPacket *pkt;
|
2023-07-18 14:37:52 +00:00
|
|
|
AVPacket *fix_sub_duration_pkt;
|
2023-07-07 15:09:37 +00:00
|
|
|
} MuxThreadContext;
|
|
|
|
|
2022-10-13 11:14:34 +00:00
|
|
|
static Muxer *mux_from_of(OutputFile *of)
|
|
|
|
{
|
|
|
|
return (Muxer*)of;
|
|
|
|
}
|
|
|
|
|
2022-03-31 15:33:48 +00:00
|
|
|
static int64_t filesize(AVIOContext *pb)
|
|
|
|
{
|
|
|
|
int64_t ret = -1;
|
|
|
|
|
|
|
|
if (pb) {
|
|
|
|
ret = avio_size(pb);
|
|
|
|
if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too
|
|
|
|
ret = avio_tell(pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-11-24 19:12:52 +00:00
|
|
|
static void mux_log_debug_ts(OutputStream *ost, const AVPacket *pkt)
|
|
|
|
{
|
|
|
|
static const char *desc[] = {
|
|
|
|
[LATENCY_PROBE_DEMUX] = "demux",
|
|
|
|
[LATENCY_PROBE_DEC_PRE] = "decode",
|
|
|
|
[LATENCY_PROBE_DEC_POST] = "decode",
|
|
|
|
[LATENCY_PROBE_FILTER_PRE] = "filter",
|
|
|
|
[LATENCY_PROBE_FILTER_POST] = "filter",
|
|
|
|
[LATENCY_PROBE_ENC_PRE] = "encode",
|
|
|
|
[LATENCY_PROBE_ENC_POST] = "encode",
|
|
|
|
[LATENCY_PROBE_NB] = "mux",
|
|
|
|
};
|
|
|
|
|
|
|
|
char latency[512];
|
|
|
|
|
|
|
|
*latency = 0;
|
|
|
|
if (pkt->opaque_ref) {
|
|
|
|
const FrameData *fd = (FrameData*)pkt->opaque_ref->data;
|
|
|
|
int64_t now = av_gettime_relative();
|
|
|
|
int64_t total = INT64_MIN;
|
|
|
|
|
|
|
|
int next;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < FF_ARRAY_ELEMS(fd->wallclock); i = next) {
|
|
|
|
int64_t val = fd->wallclock[i];
|
|
|
|
|
|
|
|
next = i + 1;
|
|
|
|
|
|
|
|
if (val == INT64_MIN)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (total == INT64_MIN) {
|
|
|
|
total = now - val;
|
|
|
|
snprintf(latency, sizeof(latency), "total:%gms", total / 1e3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the next valid entry
|
|
|
|
for (; next <= FF_ARRAY_ELEMS(fd->wallclock); next++) {
|
|
|
|
int64_t val_next = (next == FF_ARRAY_ELEMS(fd->wallclock)) ?
|
|
|
|
now : fd->wallclock[next];
|
|
|
|
int64_t diff;
|
|
|
|
|
|
|
|
if (val_next == INT64_MIN)
|
|
|
|
continue;
|
|
|
|
diff = val_next - val;
|
|
|
|
|
|
|
|
// print those stages that take at least 5% of total
|
|
|
|
if (100. * diff > 5. * total) {
|
|
|
|
av_strlcat(latency, ", ", sizeof(latency));
|
|
|
|
|
|
|
|
if (!strcmp(desc[i], desc[next]))
|
|
|
|
av_strlcat(latency, desc[i], sizeof(latency));
|
|
|
|
else
|
|
|
|
av_strlcatf(latency, sizeof(latency), "%s-%s:",
|
|
|
|
desc[i], desc[next]);
|
|
|
|
|
|
|
|
av_strlcatf(latency, sizeof(latency), " %gms/%d%%",
|
|
|
|
diff / 1e3, (int)(100. * diff / total));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(ost, AV_LOG_INFO, "muxer <- pts:%s pts_time:%s dts:%s dts_time:%s "
|
|
|
|
"duration:%s duration_time:%s size:%d latency(%s)\n",
|
|
|
|
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
|
|
|
|
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
|
|
|
|
av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
|
|
|
|
pkt->size, *latency ? latency : "N/A");
|
|
|
|
}
|
|
|
|
|
2023-12-14 12:51:27 +00:00
|
|
|
static int mux_fixup_ts(Muxer *mux, MuxStream *ms, AVPacket *pkt)
|
2021-11-10 13:51:48 +00:00
|
|
|
{
|
2023-12-14 12:51:27 +00:00
|
|
|
OutputStream *ost = &ms->ost;
|
2022-03-31 15:33:48 +00:00
|
|
|
|
2023-12-14 12:42:17 +00:00
|
|
|
#if FFMPEG_OPT_VSYNC_DROP
|
2023-04-11 08:15:55 +00:00
|
|
|
if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
|
2021-11-10 13:51:48 +00:00
|
|
|
pkt->pts = pkt->dts = AV_NOPTS_VALUE;
|
2023-12-14 12:42:17 +00:00
|
|
|
#endif
|
2021-11-10 13:51:48 +00:00
|
|
|
|
2023-07-25 14:28:07 +00:00
|
|
|
// rescale timestamps to the stream timebase
|
|
|
|
if (ost->type == AVMEDIA_TYPE_AUDIO && !ost->enc) {
|
|
|
|
// use av_rescale_delta() for streamcopying audio, to preserve
|
|
|
|
// accuracy with coarse input timebases
|
|
|
|
int duration = av_get_audio_frame_duration2(ost->st->codecpar, pkt->size);
|
|
|
|
|
|
|
|
if (!duration)
|
|
|
|
duration = ost->st->codecpar->frame_size;
|
|
|
|
|
|
|
|
pkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
|
|
|
|
(AVRational){1, ost->st->codecpar->sample_rate}, duration,
|
|
|
|
&ms->ts_rescale_delta_last, ost->st->time_base);
|
|
|
|
pkt->pts = pkt->dts;
|
|
|
|
|
|
|
|
pkt->duration = av_rescale_q(pkt->duration, pkt->time_base, ost->st->time_base);
|
|
|
|
} else
|
|
|
|
av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
|
2023-02-06 12:12:05 +00:00
|
|
|
pkt->time_base = ost->st->time_base;
|
2021-11-10 13:51:48 +00:00
|
|
|
|
2023-12-14 12:51:27 +00:00
|
|
|
if (!(mux->fc->oformat->flags & AVFMT_NOTIMESTAMPS)) {
|
2021-11-10 13:51:48 +00:00
|
|
|
if (pkt->dts != AV_NOPTS_VALUE &&
|
|
|
|
pkt->pts != AV_NOPTS_VALUE &&
|
|
|
|
pkt->dts > pkt->pts) {
|
2023-12-14 07:20:13 +00:00
|
|
|
av_log(ost, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64", replacing by guess\n",
|
|
|
|
pkt->dts, pkt->pts);
|
2021-11-10 13:51:48 +00:00
|
|
|
pkt->pts =
|
2022-06-10 12:45:48 +00:00
|
|
|
pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1
|
|
|
|
- FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
|
|
|
|
- FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
|
2021-11-10 13:51:48 +00:00
|
|
|
}
|
2023-04-11 08:15:55 +00:00
|
|
|
if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) &&
|
2021-11-10 13:51:48 +00:00
|
|
|
pkt->dts != AV_NOPTS_VALUE &&
|
2022-06-10 12:45:48 +00:00
|
|
|
ms->last_mux_dts != AV_NOPTS_VALUE) {
|
2023-12-14 12:51:27 +00:00
|
|
|
int64_t max = ms->last_mux_dts + !(mux->fc->oformat->flags & AVFMT_TS_NONSTRICT);
|
2021-11-10 13:51:48 +00:00
|
|
|
if (pkt->dts < max) {
|
2023-04-11 08:15:55 +00:00
|
|
|
int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
|
2021-11-10 13:51:48 +00:00
|
|
|
if (exit_on_error)
|
|
|
|
loglevel = AV_LOG_ERROR;
|
2023-12-14 07:20:13 +00:00
|
|
|
av_log(ost, loglevel, "Non-monotonic DTS; "
|
|
|
|
"previous: %"PRId64", current: %"PRId64"; ",
|
|
|
|
ms->last_mux_dts, pkt->dts);
|
2022-08-09 13:19:41 +00:00
|
|
|
if (exit_on_error) {
|
2023-12-14 12:51:27 +00:00
|
|
|
return AVERROR(EINVAL);
|
2022-08-09 13:19:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-14 07:20:13 +00:00
|
|
|
av_log(ost, loglevel, "changing to %"PRId64". This may result "
|
2021-11-10 13:51:48 +00:00
|
|
|
"in incorrect timestamps in the output file.\n",
|
|
|
|
max);
|
|
|
|
if (pkt->pts >= pkt->dts)
|
|
|
|
pkt->pts = FFMAX(pkt->pts, max);
|
|
|
|
pkt->dts = max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 12:45:48 +00:00
|
|
|
ms->last_mux_dts = pkt->dts;
|
2021-11-10 13:51:48 +00:00
|
|
|
|
2023-12-14 12:51:27 +00:00
|
|
|
if (debug_ts)
|
|
|
|
mux_log_debug_ts(ost, pkt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
MuxStream *ms = ms_from_ost(ost);
|
|
|
|
AVFormatContext *s = mux->fc;
|
|
|
|
int64_t fs;
|
|
|
|
uint64_t frame_num;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
fs = filesize(s->pb);
|
|
|
|
atomic_store(&mux->last_filesize, fs);
|
|
|
|
if (fs >= mux->limit_filesize) {
|
|
|
|
ret = AVERROR_EOF;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mux_fixup_ts(mux, ms, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2023-03-28 09:24:42 +00:00
|
|
|
ms->data_size_mux += pkt->size;
|
2023-02-06 12:14:53 +00:00
|
|
|
frame_num = atomic_fetch_add(&ost->packets_written, 1);
|
2021-11-10 13:51:48 +00:00
|
|
|
|
|
|
|
pkt->stream_index = ost->index;
|
|
|
|
|
2023-02-06 12:14:53 +00:00
|
|
|
if (ms->stats.io)
|
|
|
|
enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num);
|
|
|
|
|
2021-11-10 13:51:48 +00:00
|
|
|
ret = av_interleaved_write_frame(s, pkt);
|
|
|
|
if (ret < 0) {
|
2023-05-07 16:29:09 +00:00
|
|
|
av_log(ost, AV_LOG_ERROR,
|
|
|
|
"Error submitting a packet to the muxer: %s\n",
|
|
|
|
av_err2str(ret));
|
2022-08-09 13:19:41 +00:00
|
|
|
goto fail;
|
2021-11-10 13:51:48 +00:00
|
|
|
}
|
2022-04-01 08:03:46 +00:00
|
|
|
|
|
|
|
return 0;
|
2022-08-09 13:19:41 +00:00
|
|
|
fail:
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
return ret;
|
2021-11-10 13:51:48 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 18:33:27 +00:00
|
|
|
static int sync_queue_process(Muxer *mux, MuxStream *ms, AVPacket *pkt, int *stream_eof)
|
2022-06-10 12:21:42 +00:00
|
|
|
{
|
2022-10-13 11:14:34 +00:00
|
|
|
OutputFile *of = &mux->of;
|
|
|
|
|
2023-12-13 18:33:27 +00:00
|
|
|
if (ms->sq_idx_mux >= 0) {
|
|
|
|
int ret = sq_send(mux->sq_mux, ms->sq_idx_mux, SQPKT(pkt));
|
2023-02-09 15:51:30 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == AVERROR_EOF)
|
|
|
|
*stream_eof = 1;
|
|
|
|
|
2022-03-31 15:33:48 +00:00
|
|
|
return ret;
|
2023-02-09 15:51:30 +00:00
|
|
|
}
|
2022-06-10 12:21:42 +00:00
|
|
|
|
|
|
|
while (1) {
|
2022-10-13 13:29:29 +00:00
|
|
|
ret = sq_receive(mux->sq_mux, -1, SQPKT(mux->sq_pkt));
|
2023-07-10 08:48:38 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
/* n.b.: We forward EOF from the sync queue, terminating muxing.
|
|
|
|
* This assumes that if a muxing sync queue is present, then all
|
|
|
|
* the streams use it. That is true currently, but may change in
|
|
|
|
* the future, then this code needs to be revisited.
|
|
|
|
*/
|
|
|
|
return ret == AVERROR(EAGAIN) ? 0 : ret;
|
|
|
|
}
|
2022-06-10 12:21:42 +00:00
|
|
|
|
2022-10-14 08:57:19 +00:00
|
|
|
ret = write_packet(mux, of->streams[ret],
|
2022-10-13 11:14:34 +00:00
|
|
|
mux->sq_pkt);
|
2022-04-01 08:03:46 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2022-06-10 12:21:42 +00:00
|
|
|
}
|
2022-03-31 15:33:48 +00:00
|
|
|
} else if (pkt)
|
2023-12-13 18:33:27 +00:00
|
|
|
return write_packet(mux, &ms->ost, pkt);
|
2022-04-01 08:03:46 +00:00
|
|
|
|
|
|
|
return 0;
|
2022-06-10 12:21:42 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 17:39:02 +00:00
|
|
|
static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt);
|
2023-07-18 14:37:52 +00:00
|
|
|
|
2023-10-17 18:42:17 +00:00
|
|
|
/* apply the output bitstream filters */
|
2023-07-18 14:37:52 +00:00
|
|
|
static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt,
|
|
|
|
OutputStream *ost, AVPacket *pkt, int *stream_eof)
|
2023-10-17 18:42:17 +00:00
|
|
|
{
|
|
|
|
MuxStream *ms = ms_from_ost(ost);
|
|
|
|
const char *err_msg;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
if (pkt && !ost->enc) {
|
2023-12-13 17:39:02 +00:00
|
|
|
ret = of_streamcopy(&mux->of, ost, pkt);
|
2023-07-18 14:37:52 +00:00
|
|
|
if (ret == AVERROR(EAGAIN))
|
|
|
|
return 0;
|
|
|
|
else if (ret == AVERROR_EOF) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
pkt = NULL;
|
|
|
|
ret = 0;
|
2024-02-03 19:59:07 +00:00
|
|
|
*stream_eof = 1;
|
2023-07-18 14:37:52 +00:00
|
|
|
} else if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// emit heartbeat for -fix_sub_duration;
|
|
|
|
// we are only interested in heartbeats on on random access points.
|
|
|
|
if (pkt && (pkt->flags & AV_PKT_FLAG_KEY)) {
|
|
|
|
mt->fix_sub_duration_pkt->opaque = (void*)(intptr_t)PKT_OPAQUE_FIX_SUB_DURATION;
|
|
|
|
mt->fix_sub_duration_pkt->pts = pkt->pts;
|
|
|
|
mt->fix_sub_duration_pkt->time_base = pkt->time_base;
|
|
|
|
|
|
|
|
ret = sch_mux_sub_heartbeat(mux->sch, mux->sch_idx, ms->sch_idx,
|
|
|
|
mt->fix_sub_duration_pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-10-17 18:42:17 +00:00
|
|
|
if (ms->bsf_ctx) {
|
|
|
|
int bsf_eof = 0;
|
|
|
|
|
|
|
|
if (pkt)
|
|
|
|
av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in);
|
|
|
|
|
|
|
|
ret = av_bsf_send_packet(ms->bsf_ctx, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
err_msg = "submitting a packet for bitstream filtering";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!bsf_eof) {
|
|
|
|
ret = av_bsf_receive_packet(ms->bsf_ctx, ms->bsf_pkt);
|
|
|
|
if (ret == AVERROR(EAGAIN))
|
|
|
|
return 0;
|
|
|
|
else if (ret == AVERROR_EOF)
|
|
|
|
bsf_eof = 1;
|
|
|
|
else if (ret < 0) {
|
|
|
|
av_log(ost, AV_LOG_ERROR,
|
|
|
|
"Error applying bitstream filters to a packet: %s",
|
|
|
|
av_err2str(ret));
|
|
|
|
if (exit_on_error)
|
|
|
|
return ret;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bsf_eof)
|
|
|
|
ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out;
|
|
|
|
|
2023-12-13 18:33:27 +00:00
|
|
|
ret = sync_queue_process(mux, ms, bsf_eof ? NULL : ms->bsf_pkt, stream_eof);
|
2023-10-17 18:42:17 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto mux_fail;
|
|
|
|
}
|
|
|
|
*stream_eof = 1;
|
|
|
|
} else {
|
2023-12-13 18:33:27 +00:00
|
|
|
ret = sync_queue_process(mux, ms, pkt, stream_eof);
|
2023-10-17 18:42:17 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto mux_fail;
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:59:07 +00:00
|
|
|
return *stream_eof ? AVERROR_EOF : 0;
|
2023-10-17 18:42:17 +00:00
|
|
|
|
|
|
|
mux_fail:
|
|
|
|
err_msg = "submitting a packet to the muxer";
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (ret != AVERROR_EOF)
|
|
|
|
av_log(ost, AV_LOG_ERROR, "Error %s: %s\n", err_msg, av_err2str(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-18 16:50:30 +00:00
|
|
|
static void thread_set_name(OutputFile *of)
|
|
|
|
{
|
|
|
|
char name[16];
|
|
|
|
snprintf(name, sizeof(name), "mux%d:%s", of->index, of->format->name);
|
|
|
|
ff_thread_setname(name);
|
|
|
|
}
|
|
|
|
|
2023-07-07 15:09:37 +00:00
|
|
|
static void mux_thread_uninit(MuxThreadContext *mt)
|
|
|
|
{
|
|
|
|
av_packet_free(&mt->pkt);
|
2023-07-18 14:37:52 +00:00
|
|
|
av_packet_free(&mt->fix_sub_duration_pkt);
|
2023-07-07 15:09:37 +00:00
|
|
|
|
|
|
|
memset(mt, 0, sizeof(*mt));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mux_thread_init(MuxThreadContext *mt)
|
|
|
|
{
|
|
|
|
memset(mt, 0, sizeof(*mt));
|
|
|
|
|
|
|
|
mt->pkt = av_packet_alloc();
|
|
|
|
if (!mt->pkt)
|
|
|
|
goto fail;
|
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
mt->fix_sub_duration_pkt = av_packet_alloc();
|
|
|
|
if (!mt->fix_sub_duration_pkt)
|
|
|
|
goto fail;
|
|
|
|
|
2023-07-07 15:09:37 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
mux_thread_uninit(mt);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2024-02-22 15:02:21 +00:00
|
|
|
int muxer_thread(void *arg)
|
2021-12-12 12:37:37 +00:00
|
|
|
{
|
2022-10-13 11:14:34 +00:00
|
|
|
Muxer *mux = arg;
|
|
|
|
OutputFile *of = &mux->of;
|
2023-07-07 15:09:37 +00:00
|
|
|
|
|
|
|
MuxThreadContext mt;
|
|
|
|
|
2022-03-31 15:33:48 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2023-07-07 15:09:37 +00:00
|
|
|
ret = mux_thread_init(&mt);
|
|
|
|
if (ret < 0)
|
2022-03-31 15:33:48 +00:00
|
|
|
goto finish;
|
2021-12-12 12:37:37 +00:00
|
|
|
|
2022-10-18 16:50:30 +00:00
|
|
|
thread_set_name(of);
|
|
|
|
|
2022-03-31 15:33:48 +00:00
|
|
|
while (1) {
|
|
|
|
OutputStream *ost;
|
2023-02-09 15:51:30 +00:00
|
|
|
int stream_idx, stream_eof = 0;
|
2022-03-31 15:33:48 +00:00
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
ret = sch_mux_receive(mux->sch, of->index, mt.pkt);
|
|
|
|
stream_idx = mt.pkt->stream_index;
|
2022-03-31 15:33:48 +00:00
|
|
|
if (stream_idx < 0) {
|
2023-01-28 09:06:20 +00:00
|
|
|
av_log(mux, AV_LOG_VERBOSE, "All streams finished\n");
|
2022-03-31 15:33:48 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
ost = of->streams[mux->sch_stream_idx[stream_idx]];
|
|
|
|
mt.pkt->stream_index = ost->index;
|
2024-03-01 13:57:04 +00:00
|
|
|
mt.pkt->flags &= ~AV_PKT_FLAG_TRUSTED;
|
2023-07-18 14:37:52 +00:00
|
|
|
|
|
|
|
ret = mux_packet_filter(mux, &mt, ost, ret < 0 ? NULL : mt.pkt, &stream_eof);
|
2023-07-07 15:09:37 +00:00
|
|
|
av_packet_unref(mt.pkt);
|
2023-04-22 12:56:34 +00:00
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
if (stream_eof) {
|
2023-07-18 14:37:52 +00:00
|
|
|
sch_mux_receive_finish(mux->sch, of->index, stream_idx);
|
2023-04-22 12:56:34 +00:00
|
|
|
} else {
|
|
|
|
av_log(mux, AV_LOG_VERBOSE, "Muxer returned EOF\n");
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (ret < 0) {
|
2023-01-28 09:06:20 +00:00
|
|
|
av_log(mux, AV_LOG_ERROR, "Error muxing a packet\n");
|
2022-03-31 15:33:48 +00:00
|
|
|
break;
|
2021-12-12 12:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-01 08:03:46 +00:00
|
|
|
|
2022-03-31 15:33:48 +00:00
|
|
|
finish:
|
2023-07-07 15:09:37 +00:00
|
|
|
mux_thread_uninit(&mt);
|
2022-03-31 15:33:48 +00:00
|
|
|
|
2024-02-22 15:02:21 +00:00
|
|
|
return ret;
|
2021-12-12 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 17:39:02 +00:00
|
|
|
static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt)
|
2023-04-04 07:44:42 +00:00
|
|
|
{
|
2023-04-13 12:32:57 +00:00
|
|
|
MuxStream *ms = ms_from_ost(ost);
|
2023-11-24 08:49:37 +00:00
|
|
|
FrameData *fd = pkt->opaque_ref ? (FrameData*)pkt->opaque_ref->data : NULL;
|
|
|
|
int64_t dts = fd ? fd->dts_est : AV_NOPTS_VALUE;
|
2023-04-04 07:44:42 +00:00
|
|
|
int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
|
2023-07-25 14:28:07 +00:00
|
|
|
int64_t ts_offset;
|
2023-05-25 06:50:19 +00:00
|
|
|
|
|
|
|
if (of->recording_time != INT64_MAX &&
|
|
|
|
dts >= of->recording_time + start_time)
|
2023-07-18 14:37:52 +00:00
|
|
|
return AVERROR_EOF;
|
2023-04-04 07:44:42 +00:00
|
|
|
|
2023-04-13 12:45:58 +00:00
|
|
|
if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
|
2023-04-13 12:45:58 +00:00
|
|
|
!ms->copy_initial_nonkeyframes)
|
2023-07-18 14:37:52 +00:00
|
|
|
return AVERROR(EAGAIN);
|
2023-04-04 07:44:42 +00:00
|
|
|
|
2023-04-13 12:45:58 +00:00
|
|
|
if (!ms->streamcopy_started) {
|
2023-04-13 12:45:58 +00:00
|
|
|
if (!ms->copy_prior_start &&
|
2023-04-04 07:44:42 +00:00
|
|
|
(pkt->pts == AV_NOPTS_VALUE ?
|
2023-04-13 12:45:58 +00:00
|
|
|
dts < ms->ts_copy_start :
|
|
|
|
pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
|
2023-07-18 14:37:52 +00:00
|
|
|
return AVERROR(EAGAIN);
|
2023-04-04 07:44:42 +00:00
|
|
|
|
|
|
|
if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
|
2023-07-18 14:37:52 +00:00
|
|
|
return AVERROR(EAGAIN);
|
2023-04-04 07:44:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
|
2023-04-04 07:44:42 +00:00
|
|
|
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
2023-07-18 14:37:52 +00:00
|
|
|
pkt->pts -= ts_offset;
|
2023-04-04 07:44:42 +00:00
|
|
|
|
|
|
|
if (pkt->dts == AV_NOPTS_VALUE) {
|
2023-07-18 14:37:52 +00:00
|
|
|
pkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, pkt->time_base);
|
2023-04-04 07:44:42 +00:00
|
|
|
} else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2023-07-18 14:37:52 +00:00
|
|
|
pkt->pts = pkt->dts - ts_offset;
|
2023-07-25 14:28:07 +00:00
|
|
|
}
|
2023-04-04 07:44:42 +00:00
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
pkt->dts -= ts_offset;
|
2023-04-04 07:44:42 +00:00
|
|
|
|
2023-04-13 12:45:58 +00:00
|
|
|
ms->streamcopy_started = 1;
|
2023-07-12 17:52:19 +00:00
|
|
|
|
|
|
|
return 0;
|
2023-04-04 07:44:42 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 14:56:15 +00:00
|
|
|
int print_sdp(const char *filename);
|
|
|
|
|
|
|
|
int print_sdp(const char *filename)
|
2022-06-17 13:51:12 +00:00
|
|
|
{
|
|
|
|
char sdp[16384];
|
2024-02-24 23:30:42 +00:00
|
|
|
int j = 0, ret;
|
2022-06-17 13:51:12 +00:00
|
|
|
AVIOContext *sdp_pb;
|
|
|
|
AVFormatContext **avc;
|
|
|
|
|
|
|
|
avc = av_malloc_array(nb_output_files, sizeof(*avc));
|
|
|
|
if (!avc)
|
|
|
|
return AVERROR(ENOMEM);
|
2024-02-24 23:30:42 +00:00
|
|
|
for (int i = 0; i < nb_output_files; i++) {
|
2022-06-17 13:51:12 +00:00
|
|
|
if (!strcmp(output_files[i]->format->name, "rtp")) {
|
2022-10-13 11:14:34 +00:00
|
|
|
avc[j] = mux_from_of(output_files[i])->fc;
|
2022-06-17 13:51:12 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!j) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2023-05-18 14:56:15 +00:00
|
|
|
if (!filename) {
|
2022-06-17 13:51:12 +00:00
|
|
|
printf("SDP:\n%s\n", sdp);
|
|
|
|
fflush(stdout);
|
|
|
|
} else {
|
2023-05-18 14:56:15 +00:00
|
|
|
ret = avio_open2(&sdp_pb, filename, AVIO_FLAG_WRITE, &int_cb, NULL);
|
2022-06-17 13:51:12 +00:00
|
|
|
if (ret < 0) {
|
2023-05-18 14:56:15 +00:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", filename);
|
2022-06-17 13:51:12 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_print(sdp_pb, sdp);
|
|
|
|
avio_closep(&sdp_pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_freep(&avc);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
int mux_check_init(void *arg)
|
2021-11-10 13:51:48 +00:00
|
|
|
{
|
2023-07-18 14:37:52 +00:00
|
|
|
Muxer *mux = arg;
|
2022-10-13 13:24:33 +00:00
|
|
|
OutputFile *of = &mux->of;
|
2022-10-13 11:14:34 +00:00
|
|
|
AVFormatContext *fc = mux->fc;
|
2023-07-18 14:37:52 +00:00
|
|
|
int ret;
|
2021-11-10 13:51:48 +00:00
|
|
|
|
2022-10-13 11:14:34 +00:00
|
|
|
ret = avformat_write_header(fc, &mux->opts);
|
2021-11-10 13:51:48 +00:00
|
|
|
if (ret < 0) {
|
2023-01-28 09:06:20 +00:00
|
|
|
av_log(mux, AV_LOG_ERROR, "Could not write header (incorrect codec "
|
|
|
|
"parameters ?): %s\n", av_err2str(ret));
|
2021-11-10 13:51:48 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
//assert_avoptions(of->opts);
|
2022-10-13 11:14:34 +00:00
|
|
|
mux->header_written = 1;
|
2021-11-10 13:51:48 +00:00
|
|
|
|
2022-03-30 08:50:37 +00:00
|
|
|
av_dump_format(fc, of->index, fc->url, 1);
|
2023-07-18 14:37:52 +00:00
|
|
|
atomic_fetch_add(&nb_output_dumped, 1);
|
2021-11-10 13:51:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-11-10 14:21:47 +00:00
|
|
|
|
2022-10-14 09:34:26 +00:00
|
|
|
static int bsf_init(MuxStream *ms)
|
2022-10-13 13:39:47 +00:00
|
|
|
{
|
2022-10-14 09:34:26 +00:00
|
|
|
OutputStream *ost = &ms->ost;
|
|
|
|
AVBSFContext *ctx = ms->bsf_ctx;
|
2022-10-13 13:39:47 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ctx)
|
2023-04-13 12:22:28 +00:00
|
|
|
return avcodec_parameters_copy(ost->st->codecpar, ost->par_in);
|
2022-10-13 13:39:47 +00:00
|
|
|
|
2023-04-13 12:22:28 +00:00
|
|
|
ret = avcodec_parameters_copy(ctx->par_in, ost->par_in);
|
2022-10-13 13:39:47 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ctx->time_base_in = ost->st->time_base;
|
|
|
|
|
|
|
|
ret = av_bsf_init(ctx);
|
|
|
|
if (ret < 0) {
|
2023-01-28 09:06:20 +00:00
|
|
|
av_log(ms, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
|
2022-10-13 13:39:47 +00:00
|
|
|
ctx->filter->name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ost->st->time_base = ctx->time_base_out;
|
|
|
|
|
2023-05-31 09:45:13 +00:00
|
|
|
ms->bsf_pkt = av_packet_alloc();
|
|
|
|
if (!ms->bsf_pkt)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2022-10-13 13:39:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-11 11:38:17 +00:00
|
|
|
int of_stream_init(OutputFile *of, OutputStream *ost)
|
|
|
|
{
|
2022-10-13 13:24:33 +00:00
|
|
|
Muxer *mux = mux_from_of(of);
|
2022-10-14 09:34:26 +00:00
|
|
|
MuxStream *ms = ms_from_ost(ost);
|
2022-10-13 13:39:47 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* initialize bitstream filters for the output stream
|
|
|
|
* needs to be done here, because the codec id for streamcopy is not
|
|
|
|
* known until now */
|
2022-10-14 09:34:26 +00:00
|
|
|
ret = bsf_init(ms);
|
2022-10-13 13:39:47 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2023-05-28 08:43:36 +00:00
|
|
|
if (ms->stream_duration) {
|
|
|
|
ost->st->duration = av_rescale_q(ms->stream_duration, ms->stream_duration_tb,
|
|
|
|
ost->st->time_base);
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
if (ms->sch_idx >= 0)
|
|
|
|
return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx);
|
2022-10-11 11:38:17 +00:00
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
return 0;
|
2022-10-11 11:38:17 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:26:15 +00:00
|
|
|
static int check_written(OutputFile *of)
|
|
|
|
{
|
|
|
|
int64_t total_packets_written = 0;
|
|
|
|
int pass1_used = 1;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < of->nb_streams; i++) {
|
|
|
|
OutputStream *ost = of->streams[i];
|
|
|
|
uint64_t packets_written = atomic_load(&ost->packets_written);
|
|
|
|
|
|
|
|
total_packets_written += packets_written;
|
|
|
|
|
|
|
|
if (ost->enc_ctx &&
|
|
|
|
(ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
|
|
|
|
!= AV_CODEC_FLAG_PASS1)
|
|
|
|
pass1_used = 0;
|
|
|
|
|
|
|
|
if (!packets_written &&
|
|
|
|
(abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) {
|
|
|
|
av_log(ost, AV_LOG_FATAL, "Empty output stream\n");
|
|
|
|
ret = err_merge(ret, AVERROR(EINVAL));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!total_packets_written) {
|
|
|
|
int level = AV_LOG_WARNING;
|
|
|
|
|
|
|
|
if (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT) {
|
|
|
|
ret = err_merge(ret, AVERROR(EINVAL));
|
|
|
|
level = AV_LOG_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(of, level, "Output file is empty, nothing was encoded%s\n",
|
|
|
|
pass1_used ? "" : "(check -ss / -t / -frames parameters if used)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-28 08:43:49 +00:00
|
|
|
static void mux_final_stats(Muxer *mux)
|
|
|
|
{
|
2023-03-28 08:44:34 +00:00
|
|
|
OutputFile *of = &mux->of;
|
|
|
|
uint64_t total_packets = 0, total_size = 0;
|
2023-03-28 09:13:32 +00:00
|
|
|
uint64_t video_size = 0, audio_size = 0, subtitle_size = 0,
|
|
|
|
extra_size = 0, other_size = 0;
|
|
|
|
|
|
|
|
uint8_t overhead[16] = "unknown";
|
|
|
|
int64_t file_size = of_filesize(of);
|
2023-03-28 08:44:34 +00:00
|
|
|
|
2023-03-28 08:46:47 +00:00
|
|
|
av_log(of, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
|
2023-03-28 08:44:34 +00:00
|
|
|
of->index, of->url);
|
|
|
|
|
|
|
|
for (int j = 0; j < of->nb_streams; j++) {
|
|
|
|
OutputStream *ost = of->streams[j];
|
2023-03-28 09:24:42 +00:00
|
|
|
MuxStream *ms = ms_from_ost(ost);
|
2023-03-28 09:13:32 +00:00
|
|
|
const AVCodecParameters *par = ost->st->codecpar;
|
|
|
|
const enum AVMediaType type = par->codec_type;
|
2023-03-28 09:24:42 +00:00
|
|
|
const uint64_t s = ms->data_size_mux;
|
2023-03-28 09:13:32 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO: video_size += s; break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO: audio_size += s; break;
|
|
|
|
case AVMEDIA_TYPE_SUBTITLE: subtitle_size += s; break;
|
|
|
|
default: other_size += s; break;
|
|
|
|
}
|
2023-03-28 08:44:34 +00:00
|
|
|
|
2023-03-28 09:13:32 +00:00
|
|
|
extra_size += par->extradata_size;
|
|
|
|
total_size += s;
|
2023-03-28 08:44:34 +00:00
|
|
|
total_packets += atomic_load(&ost->packets_written);
|
|
|
|
|
2023-03-28 08:46:47 +00:00
|
|
|
av_log(of, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
|
2023-03-28 08:44:34 +00:00
|
|
|
of->index, j, av_get_media_type_string(type));
|
2023-04-20 07:06:25 +00:00
|
|
|
if (ost->enc) {
|
2023-03-28 08:46:47 +00:00
|
|
|
av_log(of, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
|
2023-03-28 08:44:34 +00:00
|
|
|
ost->frames_encoded);
|
|
|
|
if (type == AVMEDIA_TYPE_AUDIO)
|
2023-03-28 08:46:47 +00:00
|
|
|
av_log(of, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
|
|
|
|
av_log(of, AV_LOG_VERBOSE, "; ");
|
2023-03-28 08:44:34 +00:00
|
|
|
}
|
2023-03-28 08:43:49 +00:00
|
|
|
|
2023-03-28 08:46:47 +00:00
|
|
|
av_log(of, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
|
2023-03-28 09:13:32 +00:00
|
|
|
atomic_load(&ost->packets_written), s);
|
2023-03-28 08:43:49 +00:00
|
|
|
|
2023-03-28 08:46:47 +00:00
|
|
|
av_log(of, AV_LOG_VERBOSE, "\n");
|
2023-03-28 08:44:34 +00:00
|
|
|
}
|
2023-03-28 08:43:49 +00:00
|
|
|
|
2023-03-28 08:46:47 +00:00
|
|
|
av_log(of, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
|
2023-03-28 08:44:34 +00:00
|
|
|
total_packets, total_size);
|
2023-03-28 09:13:32 +00:00
|
|
|
|
|
|
|
if (total_size && file_size > 0 && file_size >= total_size) {
|
|
|
|
snprintf(overhead, sizeof(overhead), "%f%%",
|
|
|
|
100.0 * (file_size - total_size) / total_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(of, AV_LOG_INFO,
|
2024-01-29 07:54:27 +00:00
|
|
|
"video:%1.0fKiB audio:%1.0fKiB subtitle:%1.0fKiB other streams:%1.0fKiB "
|
|
|
|
"global headers:%1.0fKiB muxing overhead: %s\n",
|
2023-03-28 09:13:32 +00:00
|
|
|
video_size / 1024.0,
|
|
|
|
audio_size / 1024.0,
|
|
|
|
subtitle_size / 1024.0,
|
|
|
|
other_size / 1024.0,
|
|
|
|
extra_size / 1024.0,
|
|
|
|
overhead);
|
2023-03-28 08:43:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 14:21:47 +00:00
|
|
|
int of_write_trailer(OutputFile *of)
|
|
|
|
{
|
2022-10-13 11:14:34 +00:00
|
|
|
Muxer *mux = mux_from_of(of);
|
|
|
|
AVFormatContext *fc = mux->fc;
|
2023-03-28 07:59:24 +00:00
|
|
|
int ret, mux_result = 0;
|
2021-11-10 14:21:47 +00:00
|
|
|
|
2023-07-18 14:37:52 +00:00
|
|
|
if (!mux->header_written) {
|
2023-01-28 09:06:20 +00:00
|
|
|
av_log(mux, AV_LOG_ERROR,
|
|
|
|
"Nothing was written into output file, because "
|
|
|
|
"at least one of its streams received no packets.\n");
|
2021-11-10 14:21:47 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:50:37 +00:00
|
|
|
ret = av_write_trailer(fc);
|
2021-11-10 14:21:47 +00:00
|
|
|
if (ret < 0) {
|
2023-01-28 09:06:20 +00:00
|
|
|
av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
|
2023-03-28 07:59:24 +00:00
|
|
|
mux_result = err_merge(mux_result, ret);
|
2021-11-10 14:21:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 11:14:34 +00:00
|
|
|
mux->last_filesize = filesize(fc->pb);
|
2021-12-13 14:13:37 +00:00
|
|
|
|
|
|
|
if (!(of->format->flags & AVFMT_NOFILE)) {
|
2022-03-30 08:50:37 +00:00
|
|
|
ret = avio_closep(&fc->pb);
|
2021-12-13 14:13:37 +00:00
|
|
|
if (ret < 0) {
|
2023-01-28 09:06:20 +00:00
|
|
|
av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret));
|
2023-03-28 07:59:24 +00:00
|
|
|
mux_result = err_merge(mux_result, ret);
|
2021-12-13 14:13:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 08:43:49 +00:00
|
|
|
mux_final_stats(mux);
|
|
|
|
|
2023-03-28 08:26:15 +00:00
|
|
|
// check whether anything was actually written
|
|
|
|
ret = check_written(of);
|
|
|
|
mux_result = err_merge(mux_result, ret);
|
|
|
|
|
2023-03-28 07:59:24 +00:00
|
|
|
return mux_result;
|
2021-11-10 14:21:47 +00:00
|
|
|
}
|
2021-11-10 16:33:48 +00:00
|
|
|
|
2023-12-14 19:05:54 +00:00
|
|
|
static void enc_stats_uninit(EncStats *es)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < es->nb_components; i++)
|
|
|
|
av_freep(&es->components[i].str);
|
|
|
|
av_freep(&es->components);
|
2023-12-14 19:15:02 +00:00
|
|
|
|
|
|
|
if (es->lock_initialized)
|
|
|
|
pthread_mutex_destroy(&es->lock);
|
|
|
|
es->lock_initialized = 0;
|
2023-12-14 19:05:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 09:18:00 +00:00
|
|
|
static void ost_free(OutputStream **post)
|
|
|
|
{
|
|
|
|
OutputStream *ost = *post;
|
2022-10-13 11:14:34 +00:00
|
|
|
MuxStream *ms;
|
2022-10-14 09:18:00 +00:00
|
|
|
|
|
|
|
if (!ost)
|
|
|
|
return;
|
2022-10-13 11:14:34 +00:00
|
|
|
ms = ms_from_ost(ost);
|
2022-10-14 09:18:00 +00:00
|
|
|
|
2023-03-25 18:46:28 +00:00
|
|
|
enc_free(&ost->enc);
|
|
|
|
|
2022-10-14 09:18:00 +00:00
|
|
|
if (ost->logfile) {
|
|
|
|
if (fclose(ost->logfile))
|
2023-01-28 09:06:20 +00:00
|
|
|
av_log(ms, AV_LOG_ERROR,
|
2022-10-14 09:18:00 +00:00
|
|
|
"Error closing logfile, loss of information possible: %s\n",
|
|
|
|
av_err2str(AVERROR(errno)));
|
|
|
|
ost->logfile = NULL;
|
|
|
|
}
|
|
|
|
|
2023-04-13 12:22:28 +00:00
|
|
|
avcodec_parameters_free(&ost->par_in);
|
|
|
|
|
2022-10-14 09:34:26 +00:00
|
|
|
av_bsf_free(&ms->bsf_ctx);
|
2023-05-31 09:45:13 +00:00
|
|
|
av_packet_free(&ms->bsf_pkt);
|
2022-10-14 09:18:00 +00:00
|
|
|
|
2023-05-31 10:14:05 +00:00
|
|
|
av_packet_free(&ms->pkt);
|
2022-10-14 09:18:00 +00:00
|
|
|
av_dict_free(&ost->encoder_opts);
|
|
|
|
|
2022-11-17 11:42:01 +00:00
|
|
|
av_freep(&ost->kf.pts);
|
|
|
|
av_expr_free(ost->kf.pexpr);
|
|
|
|
|
2022-10-14 09:18:00 +00:00
|
|
|
av_freep(&ost->logfile_prefix);
|
|
|
|
av_freep(&ost->apad);
|
|
|
|
|
2024-02-18 14:19:49 +00:00
|
|
|
av_freep(&ost->attachment_filename);
|
|
|
|
|
2022-10-14 09:18:00 +00:00
|
|
|
av_dict_free(&ost->sws_dict);
|
|
|
|
av_dict_free(&ost->swr_opts);
|
|
|
|
|
|
|
|
if (ost->enc_ctx)
|
|
|
|
av_freep(&ost->enc_ctx->stats_in);
|
|
|
|
avcodec_free_context(&ost->enc_ctx);
|
|
|
|
|
2023-12-14 19:05:54 +00:00
|
|
|
enc_stats_uninit(&ost->enc_stats_pre);
|
|
|
|
enc_stats_uninit(&ost->enc_stats_post);
|
|
|
|
enc_stats_uninit(&ms->stats);
|
2023-02-06 12:14:53 +00:00
|
|
|
|
2022-10-14 09:18:00 +00:00
|
|
|
av_freep(post);
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:50:37 +00:00
|
|
|
static void fc_close(AVFormatContext **pfc)
|
|
|
|
{
|
|
|
|
AVFormatContext *fc = *pfc;
|
|
|
|
|
|
|
|
if (!fc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(fc->oformat->flags & AVFMT_NOFILE))
|
|
|
|
avio_closep(&fc->pb);
|
|
|
|
avformat_free_context(fc);
|
|
|
|
|
|
|
|
*pfc = NULL;
|
|
|
|
}
|
|
|
|
|
2023-07-08 08:22:13 +00:00
|
|
|
void of_free(OutputFile **pof)
|
2021-12-12 11:16:42 +00:00
|
|
|
{
|
2022-10-13 13:26:52 +00:00
|
|
|
OutputFile *of = *pof;
|
|
|
|
Muxer *mux;
|
|
|
|
|
|
|
|
if (!of)
|
|
|
|
return;
|
|
|
|
mux = mux_from_of(of);
|
|
|
|
|
2022-10-13 13:29:29 +00:00
|
|
|
sq_free(&mux->sq_mux);
|
2022-10-13 13:26:52 +00:00
|
|
|
|
2022-10-14 09:18:00 +00:00
|
|
|
for (int i = 0; i < of->nb_streams; i++)
|
|
|
|
ost_free(&of->streams[i]);
|
|
|
|
av_freep(&of->streams);
|
|
|
|
|
2023-05-18 14:56:15 +00:00
|
|
|
av_freep(&mux->sch_stream_idx);
|
|
|
|
|
2021-12-13 14:50:36 +00:00
|
|
|
av_dict_free(&mux->opts);
|
2021-12-12 11:16:42 +00:00
|
|
|
|
2022-06-10 12:21:42 +00:00
|
|
|
av_packet_free(&mux->sq_pkt);
|
|
|
|
|
2022-03-30 08:50:37 +00:00
|
|
|
fc_close(&mux->fc);
|
2021-11-10 16:33:48 +00:00
|
|
|
|
|
|
|
av_freep(pof);
|
|
|
|
}
|
2021-12-11 13:03:18 +00:00
|
|
|
|
2021-12-11 13:44:38 +00:00
|
|
|
int64_t of_filesize(OutputFile *of)
|
|
|
|
{
|
2022-10-13 11:14:34 +00:00
|
|
|
Muxer *mux = mux_from_of(of);
|
|
|
|
return atomic_load(&mux->last_filesize);
|
2021-12-11 13:44:38 +00:00
|
|
|
}
|