diff mbox series

[FFmpeg-devel,1/2] avcodec/libopusenc: reload packet loss at encode

Message ID 20220316140045.369016-2-philip-dylan.gleonec@savoirfairelinux.com
State New
Headers show
Series [FFmpeg-devel,1/2] avcodec/libopusenc: reload packet loss at encode | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Philip-Dylan Gleonec March 16, 2022, 2 p.m. UTC
An estimation of packet loss is required by libopus to compute its FEC
data. Currently, this estimation is constant, and can not be changed
after configuration. This means an application using libopus through
ffmpeg can not adapt the packet loss estimation when the network
quality degrades.

This patch makes the encoder reload the packet_loss AVOption before
encoding samples, if fec is enabled and the packet loss estimation set
is different than the current one. This way an application can modify
the packet loss estimation by changing the AVOption. Typical use-case
is a RTP stream, where packet loss can be estimated from RTCP packets.

Signed-off-by: Philip-Dylan Gleonec <philip-dylan.gleonec@savoirfairelinux.com>
---
 libavcodec/libopusenc.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
diff mbox series

Patch

diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c
index 45b23fcbb5..b9e2fc45e3 100644
--- a/libavcodec/libopusenc.c
+++ b/libavcodec/libopusenc.c
@@ -460,6 +460,23 @@  static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
     uint8_t *audio;
     int ret;
     int discard_padding;
+    int32_t opus_packet_loss = 0;
+
+    ret = opus_multistream_encoder_ctl(opus->enc,
+        OPUS_GET_PACKET_LOSS_PERC(&opus_packet_loss));
+    if (ret != OPUS_OK)
+        av_log(avctx, AV_LOG_WARNING,
+            "Unable to get expected packet loss percentage: %s\n",
+            opus_strerror(ret));
+
+    if (opus->opts.fec && (opus_packet_loss != opus->opts.packet_loss)) {
+        ret = opus_multistream_encoder_ctl(opus->enc,
+                                        OPUS_SET_PACKET_LOSS_PERC(opus->opts.packet_loss));
+        if (ret != OPUS_OK)
+            av_log(avctx, AV_LOG_WARNING,
+                "Unable to set expected packet loss percentage: %s\n",
+                opus_strerror(ret));
+    }
 
     if (frame) {
         ret = ff_af_queue_add(&opus->afq, frame);