projects/guix·blob javascript-team · refs

127 lines · 4.9 KiB

1diff --git a/libavcodec/libopusdec.c b/libavcodec/libopusdec.c
2index 9b9a610343..8ec5bfc1ad 100644
3--- a/libavcodec/libopusdec.c
4+++ b/libavcodec/libopusdec.c
5@@ -45,6 +45,8 @@ struct libopus_context {
6 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
7 int apply_phase_inv;
8 #endif
9+ int decode_fec;
10+ int64_t expected_next_pts;
11 };
12
13 #define OPUS_HEAD_SIZE 19
14@@ -141,6 +143,8 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
15 /* Decoder delay (in samples) at 48kHz */
16 avc->delay = avc->internal->skip_samples = opus->pre_skip;
17
18+ opus->expected_next_pts = AV_NOPTS_VALUE;
19+
20 return 0;
21 }
22
23@@ -161,27 +165,82 @@ static int libopus_decode(AVCodecContext *avc, AVFrame *frame,
24 int *got_frame_ptr, AVPacket *pkt)
25 {
26 struct libopus_context *opus = avc->priv_data;
27- int ret, nb_samples;
28+ uint8_t *outptr;
29+ int ret, nb_samples = 0, nb_lost_samples = 0, nb_samples_left;
30+
31+ // If FEC is enabled, calculate number of lost samples
32+ if (opus->decode_fec &&
33+ opus->expected_next_pts != AV_NOPTS_VALUE &&
34+ pkt->pts != AV_NOPTS_VALUE &&
35+ pkt->pts != opus->expected_next_pts) {
36+ // Cap at recovering 120 ms of lost audio.
37+ nb_lost_samples = pkt->pts - opus->expected_next_pts;
38+ nb_lost_samples = FFMIN(nb_lost_samples, MAX_FRAME_SIZE);
39+ }
40
41- frame->nb_samples = MAX_FRAME_SIZE;
42+ frame->nb_samples = MAX_FRAME_SIZE + nb_lost_samples;
43 if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
44 return ret;
45
46+ outptr = frame->data[0];
47+ nb_samples_left = frame->nb_samples;
48+
49+ if (opus->decode_fec && nb_lost_samples) {
50+ // Try to recover the lost samples with FEC data from this one.
51+ // If there's no FEC data, the decoder will do loss concealment instead.
52+ if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
53+ nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
54+ (opus_int16 *)outptr,
55+ nb_lost_samples, 1);
56+ else
57+ nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
58+ (float *)outptr,
59+ nb_lost_samples, 1);
60+
61+ if (nb_samples < 0) {
62+ av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
63+ opus_strerror(nb_samples));
64+ return ff_opus_error_to_averror(nb_samples);
65+ }
66+
67+ av_log(avc, AV_LOG_WARNING, "Recovered %d samples with FEC/PLC\n",
68+ nb_samples);
69+
70+ outptr += nb_samples * avc->channels * av_get_bytes_per_sample(avc->sample_fmt);
71+ nb_samples_left -= nb_samples;
72+ if (pkt->pts != AV_NOPTS_VALUE) {
73+ pkt->pts -= nb_samples;
74+ frame->pts = pkt->pts;
75+ }
76+ }
77+
78+ // Decode the actual, non-lost data.
79 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
80- nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
81- (opus_int16 *)frame->data[0],
82- frame->nb_samples, 0);
83+ ret = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
84+ (opus_int16 *)outptr,
85+ nb_samples_left, 0);
86 else
87- nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
88- (float *)frame->data[0],
89- frame->nb_samples, 0);
90+ ret = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
91+ (float *)outptr,
92+ nb_samples_left, 0);
93
94- if (nb_samples < 0) {
95+ if (ret < 0) {
96 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
97- opus_strerror(nb_samples));
98- return ff_opus_error_to_averror(nb_samples);
99+ opus_strerror(ret));
100+ return ff_opus_error_to_averror(ret);
101 }
102
103+ nb_samples += ret;
104+
105+ if (opus->decode_fec)
106+ {
107+ // Calculate the next expected pts
108+ if (pkt->pts == AV_NOPTS_VALUE) {
109+ opus->expected_next_pts = AV_NOPTS_VALUE;
110+ } else {
111+ opus->expected_next_pts = pkt->pts + nb_samples;
112+ }
113+ }
114 #ifndef OPUS_SET_GAIN
115 {
116 int i = avc->ch_layout.nb_channels * nb_samples;
117@@ -220,6 +279,7 @@ static const AVOption libopusdec_options[] = {
118 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
119 { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
120 #endif
121+ { "decode_fec", "Decode FEC data or use PLC", OFFSET(decode_fec), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
122 { NULL },
123 };
124
125--
1262.34.1
127