mirror of
https://github.com/datarhei/ffmpeg
synced 2025-12-24 13:08:07 +08:00
122 lines
4.1 KiB
Diff
122 lines
4.1 KiB
Diff
From 194da7adf9477a49b30145ba342fcf6debd31af9 Mon Sep 17 00:00:00 2001
|
|
From: Ingo Oppermann <ingo@datarhei.com>
|
|
Date: Wed, 24 May 2023 12:16:52 +0200
|
|
Subject: [PATCH v3] Calculate bandwidth estimate (ffmpeg 5.1)
|
|
|
|
---
|
|
libavformat/hlsenc.c | 54 ++++++++++++++++++++++++++++++++++++++++----
|
|
1 file changed, 49 insertions(+), 5 deletions(-)
|
|
|
|
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
|
|
index 6f49ae1a..4d4805da 100644
|
|
--- a/libavformat/hlsenc.c
|
|
+++ b/libavformat/hlsenc.c
|
|
@@ -124,6 +124,7 @@ typedef struct VariantStream {
|
|
AVIOContext *out;
|
|
AVIOContext *out_single_file;
|
|
int packets_written;
|
|
+ uint64_t bytes_written;
|
|
int init_range_length;
|
|
uint8_t *temp_buffer;
|
|
uint8_t *init_buffer;
|
|
@@ -138,6 +139,8 @@ typedef struct VariantStream {
|
|
double dpp; // duration per packet
|
|
int64_t start_pts;
|
|
int64_t end_pts;
|
|
+ int64_t scaled_start_pts;
|
|
+ int64_t scaled_cur_pts;
|
|
int64_t video_lastpos;
|
|
int64_t video_keyframe_pos;
|
|
int64_t video_keyframe_size;
|
|
@@ -1372,7 +1375,7 @@ static int create_master_playlist(AVFormatContext *s,
|
|
AVStream *vid_st, *aud_st;
|
|
AVDictionary *options = NULL;
|
|
unsigned int i, j;
|
|
- int ret, bandwidth;
|
|
+ int ret, bandwidth, st_bandwidth, est_bandwidth;
|
|
const char *m3u8_rel_name = NULL;
|
|
const char *vtt_m3u8_rel_name = NULL;
|
|
const char *ccgroup;
|
|
@@ -1478,10 +1481,35 @@ static int create_master_playlist(AVFormatContext *s,
|
|
}
|
|
|
|
bandwidth = 0;
|
|
- if (vid_st)
|
|
- bandwidth += get_stream_bit_rate(vid_st);
|
|
- if (aud_st)
|
|
- bandwidth += get_stream_bit_rate(aud_st);
|
|
+ est_bandwidth = 0;
|
|
+
|
|
+ if (vid_st) {
|
|
+ st_bandwidth = get_stream_bit_rate(vid_st);
|
|
+ if (st_bandwidth == 0) {
|
|
+ est_bandwidth = 1;
|
|
+ } else {
|
|
+ bandwidth += st_bandwidth;
|
|
+ }
|
|
+ }
|
|
+ if (aud_st) {
|
|
+ st_bandwidth = get_stream_bit_rate(aud_st);
|
|
+ if (st_bandwidth == 0) {
|
|
+ est_bandwidth = 1;
|
|
+ } else {
|
|
+ bandwidth += st_bandwidth;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (est_bandwidth != 0) {
|
|
+ // Estimate bandwidth
|
|
+ bandwidth = (int)round((double)vs->bytes_written / (av_q2d(AV_TIME_BASE_Q) * (vs->scaled_cur_pts - vs->scaled_start_pts)) * 8);
|
|
+
|
|
+ // Reset counters
|
|
+ vs->bytes_written = 0;
|
|
+ vs->scaled_start_pts = vs->scaled_cur_pts;
|
|
+ }
|
|
+
|
|
+ // Add 10% of the bandwidth to itself
|
|
bandwidth += bandwidth / 10;
|
|
|
|
ccgroup = NULL;
|
|
@@ -2443,6 +2471,19 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
return AVERROR(ENOMEM);
|
|
}
|
|
|
|
+ if (vs->scaled_start_pts == AV_NOPTS_VALUE) {
|
|
+ vs->scaled_start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
|
|
+ }
|
|
+
|
|
+ if (vs->scaled_cur_pts == AV_NOPTS_VALUE) {
|
|
+ vs->scaled_cur_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
|
|
+ } else {
|
|
+ int64_t pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
|
|
+ if (pts > vs->scaled_cur_pts) {
|
|
+ vs->scaled_cur_pts = pts;
|
|
+ }
|
|
+ }
|
|
+
|
|
end_pts = hls->recording_time * vs->number;
|
|
|
|
if (vs->sequence - vs->nb_entries > hls->start_sequence && hls->init_time > 0) {
|
|
@@ -2663,6 +2704,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
}
|
|
|
|
vs->packets_written++;
|
|
+ vs->bytes_written += (uint64_t)pkt->size;
|
|
if (oc->pb) {
|
|
ret = ff_write_chained(oc, stream_index, pkt, s, 0);
|
|
vs->video_keyframe_size += pkt->size;
|
|
@@ -2956,6 +2998,8 @@ static int hls_init(AVFormatContext *s)
|
|
vs->sequence = hls->start_sequence;
|
|
vs->start_pts = AV_NOPTS_VALUE;
|
|
vs->end_pts = AV_NOPTS_VALUE;
|
|
+ vs->scaled_start_pts = AV_NOPTS_VALUE;
|
|
+ vs->scaled_cur_pts = AV_NOPTS_VALUE;
|
|
vs->current_segment_final_filename_fmt[0] = '\0';
|
|
vs->initial_prog_date_time = initial_program_date_time;
|
|
|
|
|
|
base-commit: 2bca71f4986725d7cf0d441e2f82a790d0a0c717
|
|
--
|
|
2.39.2 (Apple Git-143)
|
|
|