Skip to content
Snippets Groups Projects
Commit 00770b03 authored by Philip-Dylan Gleonec's avatar Philip-Dylan Gleonec Committed by Pierre Lespagnol
Browse files

ffmpeg: add FEC support for opus decoder

Patch ffmpeg to add an option (AVOption) to enable opus FEC decoding
using libopus and add FEC decoding when the option is enabled.

Issue: #5155
Change-Id: I78754a9d02a333c7d73da60b87fd40a6da544835
parent b3d23010
No related branches found
No related tags found
No related merge requests found
diff --git a/libavcodec/libopusdec.c b/libavcodec/libopusdec.c
index 2a97811d18..40ee7b8fec 100644
--- a/libavcodec/libopusdec.c
+++ b/libavcodec/libopusdec.c
@@ -43,6 +43,8 @@
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
int apply_phase_inv;
#endif
+ int decode_fec;
+ int64_t expected_next_pts;
};
#define OPUS_HEAD_SIZE 19
@@ -134,6 +136,8 @@
/* Decoder delay (in samples) at 48kHz */
avc->delay = avc->internal->skip_samples = opus->pre_skip;
+ opus->expected_next_pts = AV_NOPTS_VALUE;
+
return 0;
}
@@ -155,25 +159,81 @@
{
struct libopus_context *opus = avc->priv_data;
AVFrame *frame = data;
- int ret, nb_samples;
+ uint8_t *outptr;
+ int ret, nb_samples = 0, nb_lost_samples = 0, nb_samples_left;
+
+ // If FEC is enabled, calculate number of lost samples
+ if (opus->decode_fec &&
+ opus->expected_next_pts != AV_NOPTS_VALUE &&
+ pkt->pts != AV_NOPTS_VALUE &&
+ pkt->pts != opus->expected_next_pts) {
+ // Cap at recovering 120 ms of lost audio.
+ nb_lost_samples = pkt->pts - opus->expected_next_pts;
+ nb_lost_samples = FFMIN(nb_lost_samples, MAX_FRAME_SIZE);
+ }
- frame->nb_samples = MAX_FRAME_SIZE;
+ frame->nb_samples = MAX_FRAME_SIZE + nb_lost_samples;
if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
return ret;
+ outptr = frame->data[0];
+ nb_samples_left = frame->nb_samples;
+
+ if (opus->decode_fec && nb_lost_samples) {
+ // Try to recover the lost samples with FEC data from this one.
+ // If there's no FEC data, the decoder will do loss concealment instead.
+ if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
+ nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
+ (opus_int16 *)outptr,
+ nb_lost_samples, 1);
+ else
+ nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
+ (float *)outptr,
+ nb_lost_samples, 1);
+
+ if (nb_samples < 0) {
+ av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
+ opus_strerror(nb_samples));
+ return ff_opus_error_to_averror(nb_samples);
+ }
+
+ av_log(avc, AV_LOG_WARNING, "Recovered %d samples with FEC/PLC\n",
+ nb_samples);
+
+ outptr += nb_samples * avc->channels * av_get_bytes_per_sample(avc->sample_fmt);
+ nb_samples_left -= nb_samples;
+ if (pkt->pts != AV_NOPTS_VALUE) {
+ pkt->pts -= nb_samples;
+ frame->pts = pkt->pts;
+ }
+ }
+
+ // Decode the actual, non-lost data.
if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
- nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
- (opus_int16 *)frame->data[0],
- frame->nb_samples, 0);
+ ret = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
+ (opus_int16 *)outptr,
+ nb_samples_left, 0);
else
- nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
- (float *)frame->data[0],
- frame->nb_samples, 0);
+ ret = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
+ (float *)outptr,
+ nb_samples_left, 0);
- if (nb_samples < 0) {
+ if (ret < 0) {
av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
- opus_strerror(nb_samples));
- return ff_opus_error_to_averror(nb_samples);
+ opus_strerror(ret));
+ return ff_opus_error_to_averror(ret);
+ }
+
+ nb_samples += ret;
+
+ if (opus->decode_fec)
+ {
+ // Calculate the next expected pts
+ if (pkt->pts == AV_NOPTS_VALUE) {
+ opus->expected_next_pts = AV_NOPTS_VALUE;
+ } else {
+ opus->expected_next_pts = pkt->pts + nb_samples;
+ }
}
#ifndef OPUS_SET_GAIN
@@ -214,6 +274,7 @@
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
{ "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
#endif
+ { "decode_fec", "Decode FEC data or use PLC", OFFSET(decode_fec), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
{ NULL },
};
......@@ -351,6 +351,7 @@ ffmpeg: ffmpeg-$(FFMPEG_HASH).tar.gz
$(APPLY) $(SRC)/ffmpeg/remove-mjpeg-log.patch
$(APPLY) $(SRC)/ffmpeg/change-RTCP-ratio.patch
$(APPLY) $(SRC)/ffmpeg/rtp_ext_abs_send_time.patch
$(APPLY) $(SRC)/ffmpeg/libopusdec-enable-FEC.patch
$(UPDATE_AUTOCONFIG)
$(MOVE)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment