• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h"
12 
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #include "opus.h"
17 
18 #include "webrtc/common_audio/signal_processing/resample_by_2_internal.h"
19 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
20 
21 enum {
22   /* Maximum supported frame size in WebRTC is 60 ms. */
23   kWebRtcOpusMaxEncodeFrameSizeMs = 60,
24 
25   /* The format allows up to 120 ms frames. Since we don't control the other
26    * side, we must allow for packets of that size. NetEq is currently limited
27    * to 60 ms on the receive side. */
28   kWebRtcOpusMaxDecodeFrameSizeMs = 120,
29 
30   /* Maximum sample count per channel is 48 kHz * maximum frame size in
31    * milliseconds. */
32   kWebRtcOpusMaxFrameSizePerChannel = 48 * kWebRtcOpusMaxDecodeFrameSizeMs,
33 
34   /* Maximum sample count per frame is 48 kHz * maximum frame size in
35    * milliseconds * maximum number of channels. */
36   kWebRtcOpusMaxFrameSize = kWebRtcOpusMaxFrameSizePerChannel * 2,
37 
38   /* Maximum sample count per channel for output resampled to 32 kHz,
39    * 32 kHz * maximum frame size in milliseconds. */
40   kWebRtcOpusMaxFrameSizePerChannel32kHz = 32 * kWebRtcOpusMaxDecodeFrameSizeMs,
41 
42   /* Number of samples in resampler state. */
43   kWebRtcOpusStateSize = 7,
44 
45   /* Default frame size, 20 ms @ 48 kHz, in samples (for one channel). */
46   kWebRtcOpusDefaultFrameSize = 960,
47 };
48 
49 struct WebRtcOpusEncInst {
50   OpusEncoder* encoder;
51 };
52 
WebRtcOpus_EncoderCreate(OpusEncInst ** inst,int32_t channels)53 int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst, int32_t channels) {
54   OpusEncInst* state;
55   if (inst != NULL) {
56     state = (OpusEncInst*) calloc(1, sizeof(OpusEncInst));
57     if (state) {
58       int error;
59       /* Default to VoIP application for mono, and AUDIO for stereo. */
60       int application = (channels == 1) ? OPUS_APPLICATION_VOIP :
61           OPUS_APPLICATION_AUDIO;
62 
63       state->encoder = opus_encoder_create(48000, channels, application,
64                                            &error);
65       if (error == OPUS_OK && state->encoder != NULL) {
66         *inst = state;
67         return 0;
68       }
69       free(state);
70     }
71   }
72   return -1;
73 }
74 
WebRtcOpus_EncoderFree(OpusEncInst * inst)75 int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst) {
76   if (inst) {
77     opus_encoder_destroy(inst->encoder);
78     free(inst);
79     return 0;
80   } else {
81     return -1;
82   }
83 }
84 
WebRtcOpus_Encode(OpusEncInst * inst,int16_t * audio_in,int16_t samples,int16_t length_encoded_buffer,uint8_t * encoded)85 int16_t WebRtcOpus_Encode(OpusEncInst* inst, int16_t* audio_in, int16_t samples,
86                           int16_t length_encoded_buffer, uint8_t* encoded) {
87   opus_int16* audio = (opus_int16*) audio_in;
88   unsigned char* coded = encoded;
89   int res;
90 
91   if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) {
92     return -1;
93   }
94 
95   res = opus_encode(inst->encoder, audio, samples, coded,
96                     length_encoded_buffer);
97 
98   if (res > 0) {
99     return res;
100   }
101   return -1;
102 }
103 
WebRtcOpus_SetBitRate(OpusEncInst * inst,int32_t rate)104 int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) {
105   if (inst) {
106     return opus_encoder_ctl(inst->encoder, OPUS_SET_BITRATE(rate));
107   } else {
108     return -1;
109   }
110 }
111 
WebRtcOpus_SetPacketLossRate(OpusEncInst * inst,int32_t loss_rate)112 int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) {
113   if (inst) {
114     return opus_encoder_ctl(inst->encoder,
115                             OPUS_SET_PACKET_LOSS_PERC(loss_rate));
116   } else {
117     return -1;
118   }
119 }
120 
WebRtcOpus_EnableFec(OpusEncInst * inst)121 int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) {
122   if (inst) {
123     return opus_encoder_ctl(inst->encoder, OPUS_SET_INBAND_FEC(1));
124   } else {
125     return -1;
126   }
127 }
128 
WebRtcOpus_DisableFec(OpusEncInst * inst)129 int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) {
130   if (inst) {
131     return opus_encoder_ctl(inst->encoder, OPUS_SET_INBAND_FEC(0));
132   } else {
133     return -1;
134   }
135 }
136 
WebRtcOpus_SetComplexity(OpusEncInst * inst,int32_t complexity)137 int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) {
138   if (inst) {
139     return opus_encoder_ctl(inst->encoder, OPUS_SET_COMPLEXITY(complexity));
140   } else {
141     return -1;
142   }
143 }
144 
145 struct WebRtcOpusDecInst {
146   int16_t state_48_32_left[8];
147   int16_t state_48_32_right[8];
148   OpusDecoder* decoder_left;
149   OpusDecoder* decoder_right;
150   int prev_decoded_samples;
151   int channels;
152 };
153 
WebRtcOpus_DecoderCreate(OpusDecInst ** inst,int channels)154 int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, int channels) {
155   int error_l;
156   int error_r;
157   OpusDecInst* state;
158 
159   if (inst != NULL) {
160     /* Create Opus decoder state. */
161     state = (OpusDecInst*) calloc(1, sizeof(OpusDecInst));
162     if (state == NULL) {
163       return -1;
164     }
165 
166     /* Create new memory for left and right channel, always at 48000 Hz. */
167     state->decoder_left = opus_decoder_create(48000, channels, &error_l);
168     state->decoder_right = opus_decoder_create(48000, channels, &error_r);
169     if (error_l == OPUS_OK && error_r == OPUS_OK && state->decoder_left != NULL
170         && state->decoder_right != NULL) {
171       /* Creation of memory all ok. */
172       state->channels = channels;
173       state->prev_decoded_samples = kWebRtcOpusDefaultFrameSize;
174       *inst = state;
175       return 0;
176     }
177 
178     /* If memory allocation was unsuccessful, free the entire state. */
179     if (state->decoder_left) {
180       opus_decoder_destroy(state->decoder_left);
181     }
182     if (state->decoder_right) {
183       opus_decoder_destroy(state->decoder_right);
184     }
185     free(state);
186   }
187   return -1;
188 }
189 
WebRtcOpus_DecoderFree(OpusDecInst * inst)190 int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) {
191   if (inst) {
192     opus_decoder_destroy(inst->decoder_left);
193     opus_decoder_destroy(inst->decoder_right);
194     free(inst);
195     return 0;
196   } else {
197     return -1;
198   }
199 }
200 
WebRtcOpus_DecoderChannels(OpusDecInst * inst)201 int WebRtcOpus_DecoderChannels(OpusDecInst* inst) {
202   return inst->channels;
203 }
204 
WebRtcOpus_DecoderInitNew(OpusDecInst * inst)205 int16_t WebRtcOpus_DecoderInitNew(OpusDecInst* inst) {
206   int error = opus_decoder_ctl(inst->decoder_left, OPUS_RESET_STATE);
207   if (error == OPUS_OK) {
208     memset(inst->state_48_32_left, 0, sizeof(inst->state_48_32_left));
209     memset(inst->state_48_32_right, 0, sizeof(inst->state_48_32_right));
210     return 0;
211   }
212   return -1;
213 }
214 
WebRtcOpus_DecoderInit(OpusDecInst * inst)215 int16_t WebRtcOpus_DecoderInit(OpusDecInst* inst) {
216   int error = opus_decoder_ctl(inst->decoder_left, OPUS_RESET_STATE);
217   if (error == OPUS_OK) {
218     memset(inst->state_48_32_left, 0, sizeof(inst->state_48_32_left));
219     return 0;
220   }
221   return -1;
222 }
223 
WebRtcOpus_DecoderInitSlave(OpusDecInst * inst)224 int16_t WebRtcOpus_DecoderInitSlave(OpusDecInst* inst) {
225   int error = opus_decoder_ctl(inst->decoder_right, OPUS_RESET_STATE);
226   if (error == OPUS_OK) {
227     memset(inst->state_48_32_right, 0, sizeof(inst->state_48_32_right));
228     return 0;
229   }
230   return -1;
231 }
232 
233 /* |frame_size| is set to maximum Opus frame size in the normal case, and
234  * is set to the number of samples needed for PLC in case of losses.
235  * It is up to the caller to make sure the value is correct. */
DecodeNative(OpusDecoder * inst,const int16_t * encoded,int16_t encoded_bytes,int frame_size,int16_t * decoded,int16_t * audio_type)236 static int DecodeNative(OpusDecoder* inst, const int16_t* encoded,
237                         int16_t encoded_bytes, int frame_size,
238                         int16_t* decoded, int16_t* audio_type) {
239   unsigned char* coded = (unsigned char*) encoded;
240   opus_int16* audio = (opus_int16*) decoded;
241 
242   int res = opus_decode(inst, coded, encoded_bytes, audio, frame_size, 0);
243 
244   /* TODO(tlegrand): set to DTX for zero-length packets? */
245   *audio_type = 0;
246 
247   if (res > 0) {
248     return res;
249   }
250   return -1;
251 }
252 
DecodeFec(OpusDecoder * inst,const int16_t * encoded,int16_t encoded_bytes,int frame_size,int16_t * decoded,int16_t * audio_type)253 static int DecodeFec(OpusDecoder* inst, const int16_t* encoded,
254                      int16_t encoded_bytes, int frame_size,
255                      int16_t* decoded, int16_t* audio_type) {
256   unsigned char* coded = (unsigned char*) encoded;
257   opus_int16* audio = (opus_int16*) decoded;
258 
259   int res = opus_decode(inst, coded, encoded_bytes, audio, frame_size, 1);
260 
261   /* TODO(tlegrand): set to DTX for zero-length packets? */
262   *audio_type = 0;
263 
264   if (res > 0) {
265     return res;
266   }
267   return -1;
268 }
269 
270 /* Resample from 48 to 32 kHz. Length of state is assumed to be
271  * kWebRtcOpusStateSize (7).
272  */
WebRtcOpus_Resample48to32(const int16_t * samples_in,int length,int16_t * state,int16_t * samples_out)273 static int WebRtcOpus_Resample48to32(const int16_t* samples_in, int length,
274                                      int16_t* state, int16_t* samples_out) {
275   int i;
276   int blocks;
277   int16_t output_samples;
278   int32_t buffer32[kWebRtcOpusMaxFrameSizePerChannel + kWebRtcOpusStateSize];
279 
280   /* Resample from 48 kHz to 32 kHz. */
281   for (i = 0; i < kWebRtcOpusStateSize; i++) {
282     buffer32[i] = state[i];
283     state[i] = samples_in[length - kWebRtcOpusStateSize + i];
284   }
285   for (i = 0; i < length; i++) {
286     buffer32[kWebRtcOpusStateSize + i] = samples_in[i];
287   }
288   /* Resampling 3 samples to 2. Function divides the input in |blocks| number
289    * of 3-sample groups, and output is |blocks| number of 2-sample groups.
290    * When this is removed, the compensation in WebRtcOpus_DurationEst should be
291    * removed too. */
292   blocks = length / 3;
293   WebRtcSpl_Resample48khzTo32khz(buffer32, buffer32, blocks);
294   output_samples = (int16_t) (blocks * 2);
295   WebRtcSpl_VectorBitShiftW32ToW16(samples_out, output_samples, buffer32, 15);
296 
297   return output_samples;
298 }
299 
WebRtcOpus_DeInterleaveResample(OpusDecInst * inst,int16_t * input,int sample_pairs,int16_t * output)300 static int WebRtcOpus_DeInterleaveResample(OpusDecInst* inst, int16_t* input,
301                                            int sample_pairs, int16_t* output) {
302   int i;
303   int16_t buffer_left[kWebRtcOpusMaxFrameSizePerChannel];
304   int16_t buffer_right[kWebRtcOpusMaxFrameSizePerChannel];
305   int16_t buffer_out[kWebRtcOpusMaxFrameSizePerChannel32kHz];
306   int resampled_samples;
307 
308   /* De-interleave the signal in left and right channel. */
309   for (i = 0; i < sample_pairs; i++) {
310     /* Take every second sample, starting at the first sample. */
311     buffer_left[i] = input[i * 2];
312     buffer_right[i] = input[i * 2 + 1];
313   }
314 
315   /* Resample from 48 kHz to 32 kHz for left channel. */
316   resampled_samples = WebRtcOpus_Resample48to32(
317       buffer_left, sample_pairs, inst->state_48_32_left, buffer_out);
318 
319   /* Add samples interleaved to output vector. */
320   for (i = 0; i < resampled_samples; i++) {
321     output[i * 2] = buffer_out[i];
322   }
323 
324   /* Resample from 48 kHz to 32 kHz for right channel. */
325   resampled_samples = WebRtcOpus_Resample48to32(
326       buffer_right, sample_pairs, inst->state_48_32_right, buffer_out);
327 
328   /* Add samples interleaved to output vector. */
329   for (i = 0; i < resampled_samples; i++) {
330     output[i * 2 + 1] = buffer_out[i];
331   }
332 
333   return resampled_samples;
334 }
335 
WebRtcOpus_DecodeNew(OpusDecInst * inst,const uint8_t * encoded,int16_t encoded_bytes,int16_t * decoded,int16_t * audio_type)336 int16_t WebRtcOpus_DecodeNew(OpusDecInst* inst, const uint8_t* encoded,
337                              int16_t encoded_bytes, int16_t* decoded,
338                              int16_t* audio_type) {
339   /* |buffer| is big enough for 120 ms (the largest Opus packet size) of stereo
340    * audio at 48 kHz. */
341   int16_t buffer[kWebRtcOpusMaxFrameSize];
342   int16_t* coded = (int16_t*)encoded;
343   int decoded_samples;
344   int resampled_samples;
345 
346   /* If mono case, just do a regular call to the decoder.
347    * If stereo, we need to de-interleave the stereo output into blocks with
348    * left and right channel. Each block is resampled to 32 kHz, and then
349    * interleaved again. */
350 
351   /* Decode to a temporary buffer. */
352   decoded_samples = DecodeNative(inst->decoder_left, coded, encoded_bytes,
353                                  kWebRtcOpusMaxFrameSizePerChannel,
354                                  buffer, audio_type);
355   if (decoded_samples < 0) {
356     return -1;
357   }
358 
359   if (inst->channels == 2) {
360     /* De-interleave and resample. */
361     resampled_samples = WebRtcOpus_DeInterleaveResample(inst,
362                                                         buffer,
363                                                         decoded_samples,
364                                                         decoded);
365   } else {
366     /* Resample from 48 kHz to 32 kHz. Filter state memory for left channel is
367      * used for mono signals. */
368     resampled_samples = WebRtcOpus_Resample48to32(buffer,
369                                                   decoded_samples,
370                                                   inst->state_48_32_left,
371                                                   decoded);
372   }
373 
374   /* Update decoded sample memory, to be used by the PLC in case of losses. */
375   inst->prev_decoded_samples = decoded_samples;
376 
377   return resampled_samples;
378 }
379 
WebRtcOpus_Decode(OpusDecInst * inst,const int16_t * encoded,int16_t encoded_bytes,int16_t * decoded,int16_t * audio_type)380 int16_t WebRtcOpus_Decode(OpusDecInst* inst, const int16_t* encoded,
381                           int16_t encoded_bytes, int16_t* decoded,
382                           int16_t* audio_type) {
383   /* |buffer16| is big enough for 120 ms (the largestOpus packet size) of
384    * stereo audio at 48 kHz. */
385   int16_t buffer16[kWebRtcOpusMaxFrameSize];
386   int decoded_samples;
387   int16_t output_samples;
388   int i;
389 
390   /* If mono case, just do a regular call to the decoder.
391    * If stereo, call to WebRtcOpus_Decode() gives left channel as output, and
392    * calls to WebRtcOpus_Decode_slave() give right channel as output.
393    * This is to make stereo work with the current setup of NetEQ, which
394    * requires two calls to the decoder to produce stereo. */
395 
396   /* Decode to a temporary buffer. */
397   decoded_samples = DecodeNative(inst->decoder_left, encoded, encoded_bytes,
398                                  kWebRtcOpusMaxFrameSizePerChannel, buffer16,
399                                  audio_type);
400   if (decoded_samples < 0) {
401     return -1;
402   }
403   if (inst->channels == 2) {
404     /* The parameter |decoded_samples| holds the number of samples pairs, in
405      * case of stereo. Number of samples in |buffer16| equals |decoded_samples|
406      * times 2. */
407     for (i = 0; i < decoded_samples; i++) {
408       /* Take every second sample, starting at the first sample. This gives
409        * the left channel. */
410       buffer16[i] = buffer16[i * 2];
411     }
412   }
413 
414   /* Resample from 48 kHz to 32 kHz. */
415   output_samples = WebRtcOpus_Resample48to32(buffer16, decoded_samples,
416                                              inst->state_48_32_left, decoded);
417 
418   /* Update decoded sample memory, to be used by the PLC in case of losses. */
419   inst->prev_decoded_samples = decoded_samples;
420 
421   return output_samples;
422 }
423 
WebRtcOpus_DecodeSlave(OpusDecInst * inst,const int16_t * encoded,int16_t encoded_bytes,int16_t * decoded,int16_t * audio_type)424 int16_t WebRtcOpus_DecodeSlave(OpusDecInst* inst, const int16_t* encoded,
425                                int16_t encoded_bytes, int16_t* decoded,
426                                int16_t* audio_type) {
427   /* |buffer16| is big enough for 120 ms (the largestOpus packet size) of
428    * stereo audio at 48 kHz. */
429   int16_t buffer16[kWebRtcOpusMaxFrameSize];
430   int decoded_samples;
431   int16_t output_samples;
432   int i;
433 
434   /* Decode to a temporary buffer. */
435   decoded_samples = DecodeNative(inst->decoder_right, encoded, encoded_bytes,
436                                  kWebRtcOpusMaxFrameSizePerChannel, buffer16,
437                                  audio_type);
438   if (decoded_samples < 0) {
439     return -1;
440   }
441   if (inst->channels == 2) {
442     /* The parameter |decoded_samples| holds the number of samples pairs, in
443      * case of stereo. Number of samples in |buffer16| equals |decoded_samples|
444      * times 2. */
445     for (i = 0; i < decoded_samples; i++) {
446       /* Take every second sample, starting at the second sample. This gives
447        * the right channel. */
448       buffer16[i] = buffer16[i * 2 + 1];
449     }
450   } else {
451     /* Decode slave should never be called for mono packets. */
452     return -1;
453   }
454   /* Resample from 48 kHz to 32 kHz. */
455   output_samples = WebRtcOpus_Resample48to32(buffer16, decoded_samples,
456                                              inst->state_48_32_right, decoded);
457 
458   return output_samples;
459 }
460 
WebRtcOpus_DecodePlc(OpusDecInst * inst,int16_t * decoded,int16_t number_of_lost_frames)461 int16_t WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded,
462                              int16_t number_of_lost_frames) {
463   int16_t buffer[kWebRtcOpusMaxFrameSize];
464   int16_t audio_type = 0;
465   int decoded_samples;
466   int resampled_samples;
467   int plc_samples;
468 
469   /* If mono case, just do a regular call to the plc function, before
470    * resampling.
471    * If stereo, we need to de-interleave the stereo output into blocks with
472    * left and right channel. Each block is resampled to 32 kHz, and then
473    * interleaved again. */
474 
475   /* Decode to a temporary buffer. The number of samples we ask for is
476    * |number_of_lost_frames| times |prev_decoded_samples_|. Limit the number
477    * of samples to maximum |kWebRtcOpusMaxFrameSizePerChannel|. */
478   plc_samples = number_of_lost_frames * inst->prev_decoded_samples;
479   plc_samples = (plc_samples <= kWebRtcOpusMaxFrameSizePerChannel) ?
480       plc_samples : kWebRtcOpusMaxFrameSizePerChannel;
481   decoded_samples = DecodeNative(inst->decoder_left, NULL, 0, plc_samples,
482                                  buffer, &audio_type);
483   if (decoded_samples < 0) {
484     return -1;
485   }
486 
487   if (inst->channels == 2) {
488      /* De-interleave and resample. */
489      resampled_samples = WebRtcOpus_DeInterleaveResample(inst,
490                                                          buffer,
491                                                          decoded_samples,
492                                                          decoded);
493    } else {
494      /* Resample from 48 kHz to 32 kHz. Filter state memory for left channel is
495       * used for mono signals. */
496      resampled_samples = WebRtcOpus_Resample48to32(buffer,
497                                                    decoded_samples,
498                                                    inst->state_48_32_left,
499                                                    decoded);
500    }
501 
502   return resampled_samples;
503 }
504 
WebRtcOpus_DecodePlcMaster(OpusDecInst * inst,int16_t * decoded,int16_t number_of_lost_frames)505 int16_t WebRtcOpus_DecodePlcMaster(OpusDecInst* inst, int16_t* decoded,
506                                    int16_t number_of_lost_frames) {
507   int16_t buffer[kWebRtcOpusMaxFrameSize];
508   int decoded_samples;
509   int resampled_samples;
510   int16_t audio_type = 0;
511   int plc_samples;
512   int i;
513 
514   /* If mono case, just do a regular call to the decoder.
515    * If stereo, call to WebRtcOpus_DecodePlcMaster() gives left channel as
516    * output, and calls to WebRtcOpus_DecodePlcSlave() give right channel as
517    * output. This is to make stereo work with the current setup of NetEQ, which
518    * requires two calls to the decoder to produce stereo. */
519 
520   /* Decode to a temporary buffer. The number of samples we ask for is
521    * |number_of_lost_frames| times |prev_decoded_samples_|. Limit the number
522    * of samples to maximum |kWebRtcOpusMaxFrameSizePerChannel|. */
523   plc_samples = number_of_lost_frames * inst->prev_decoded_samples;
524   plc_samples = (plc_samples <= kWebRtcOpusMaxFrameSizePerChannel) ?
525       plc_samples : kWebRtcOpusMaxFrameSizePerChannel;
526   decoded_samples = DecodeNative(inst->decoder_left, NULL, 0, plc_samples,
527                                  buffer, &audio_type);
528   if (decoded_samples < 0) {
529     return -1;
530   }
531 
532   if (inst->channels == 2) {
533     /* The parameter |decoded_samples| holds the number of sample pairs, in
534      * case of stereo. The original number of samples in |buffer| equals
535      * |decoded_samples| times 2. */
536     for (i = 0; i < decoded_samples; i++) {
537       /* Take every second sample, starting at the first sample. This gives
538        * the left channel. */
539       buffer[i] = buffer[i * 2];
540     }
541   }
542 
543   /* Resample from 48 kHz to 32 kHz for left channel. */
544   resampled_samples = WebRtcOpus_Resample48to32(buffer,
545                                                 decoded_samples,
546                                                 inst->state_48_32_left,
547                                                 decoded);
548   return resampled_samples;
549 }
550 
WebRtcOpus_DecodePlcSlave(OpusDecInst * inst,int16_t * decoded,int16_t number_of_lost_frames)551 int16_t WebRtcOpus_DecodePlcSlave(OpusDecInst* inst, int16_t* decoded,
552                                   int16_t number_of_lost_frames) {
553   int16_t buffer[kWebRtcOpusMaxFrameSize];
554   int decoded_samples;
555   int resampled_samples;
556   int16_t audio_type = 0;
557   int plc_samples;
558   int i;
559 
560   /* Calls to WebRtcOpus_DecodePlcSlave() give right channel as output.
561    * The function should never be called in the mono case. */
562   if (inst->channels != 2) {
563     return -1;
564   }
565 
566   /* Decode to a temporary buffer. The number of samples we ask for is
567    * |number_of_lost_frames| times |prev_decoded_samples_|. Limit the number
568    * of samples to maximum |kWebRtcOpusMaxFrameSizePerChannel|. */
569   plc_samples = number_of_lost_frames * inst->prev_decoded_samples;
570   plc_samples = (plc_samples <= kWebRtcOpusMaxFrameSizePerChannel)
571       ? plc_samples : kWebRtcOpusMaxFrameSizePerChannel;
572   decoded_samples = DecodeNative(inst->decoder_right, NULL, 0, plc_samples,
573                                  buffer, &audio_type);
574   if (decoded_samples < 0) {
575     return -1;
576   }
577 
578   /* The parameter |decoded_samples| holds the number of sample pairs,
579    * The original number of samples in |buffer| equals |decoded_samples|
580    * times 2. */
581   for (i = 0; i < decoded_samples; i++) {
582     /* Take every second sample, starting at the second sample. This gives
583      * the right channel. */
584     buffer[i] = buffer[i * 2 + 1];
585   }
586 
587   /* Resample from 48 kHz to 32 kHz for left channel. */
588   resampled_samples = WebRtcOpus_Resample48to32(buffer,
589                                                 decoded_samples,
590                                                 inst->state_48_32_right,
591                                                 decoded);
592   return resampled_samples;
593 }
594 
WebRtcOpus_DecodeFec(OpusDecInst * inst,const uint8_t * encoded,int16_t encoded_bytes,int16_t * decoded,int16_t * audio_type)595 int16_t WebRtcOpus_DecodeFec(OpusDecInst* inst, const uint8_t* encoded,
596                              int16_t encoded_bytes, int16_t* decoded,
597                              int16_t* audio_type) {
598   /* |buffer| is big enough for 120 ms (the largest Opus packet size) of stereo
599    * audio at 48 kHz. */
600   int16_t buffer[kWebRtcOpusMaxFrameSize];
601   int16_t* coded = (int16_t*)encoded;
602   int decoded_samples;
603   int resampled_samples;
604   int fec_samples;
605 
606   if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) {
607     return 0;
608   }
609 
610   fec_samples = opus_packet_get_samples_per_frame(encoded, 48000);
611 
612   /* Decode to a temporary buffer. */
613   decoded_samples = DecodeFec(inst->decoder_left, coded, encoded_bytes,
614                               fec_samples, buffer, audio_type);
615   if (decoded_samples < 0) {
616     return -1;
617   }
618 
619   /* If mono case, just do a regular call to the decoder.
620    * If stereo, we need to de-interleave the stereo output into blocks with
621    * left and right channel. Each block is resampled to 32 kHz, and then
622    * interleaved again. */
623   if (inst->channels == 2) {
624     /* De-interleave and resample. */
625     resampled_samples = WebRtcOpus_DeInterleaveResample(inst,
626                                                         buffer,
627                                                         decoded_samples,
628                                                         decoded);
629   } else {
630     /* Resample from 48 kHz to 32 kHz. Filter state memory for left channel is
631      * used for mono signals. */
632     resampled_samples = WebRtcOpus_Resample48to32(buffer,
633                                                   decoded_samples,
634                                                   inst->state_48_32_left,
635                                                   decoded);
636   }
637 
638   return resampled_samples;
639 }
640 
WebRtcOpus_DurationEst(OpusDecInst * inst,const uint8_t * payload,int payload_length_bytes)641 int WebRtcOpus_DurationEst(OpusDecInst* inst,
642                            const uint8_t* payload,
643                            int payload_length_bytes) {
644   int frames, samples;
645   frames = opus_packet_get_nb_frames(payload, payload_length_bytes);
646   if (frames < 0) {
647     /* Invalid payload data. */
648     return 0;
649   }
650   samples = frames * opus_packet_get_samples_per_frame(payload, 48000);
651   if (samples < 120 || samples > 5760) {
652     /* Invalid payload duration. */
653     return 0;
654   }
655   /* Compensate for the down-sampling from 48 kHz to 32 kHz.
656    * This should be removed when the resampling in WebRtcOpus_Decode is
657    * removed. */
658   samples = samples * 2 / 3;
659   return samples;
660 }
661 
WebRtcOpus_FecDurationEst(const uint8_t * payload,int payload_length_bytes)662 int WebRtcOpus_FecDurationEst(const uint8_t* payload,
663                               int payload_length_bytes) {
664   int samples;
665   if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) {
666     return 0;
667   }
668 
669   samples = opus_packet_get_samples_per_frame(payload, 48000);
670   if (samples < 480 || samples > 5760) {
671     /* Invalid payload duration. */
672     return 0;
673   }
674   /* Compensate for the down-sampling from 48 kHz to 32 kHz.
675    * This should be removed when the resampling in WebRtcOpus_Decode is
676    * removed. */
677   samples = samples * 2 / 3;
678   return samples;
679 }
680 
WebRtcOpus_PacketHasFec(const uint8_t * payload,int payload_length_bytes)681 int WebRtcOpus_PacketHasFec(const uint8_t* payload,
682                             int payload_length_bytes) {
683   int frames, channels, payload_length_ms;
684   int n;
685   opus_int16 frame_sizes[48];
686   const unsigned char *frame_data[48];
687 
688   if (payload == NULL || payload_length_bytes <= 0)
689     return 0;
690 
691   /* In CELT_ONLY mode, packets should not have FEC. */
692   if (payload[0] & 0x80)
693     return 0;
694 
695   payload_length_ms = opus_packet_get_samples_per_frame(payload, 48000) / 48;
696   if (10 > payload_length_ms)
697     payload_length_ms = 10;
698 
699   channels = opus_packet_get_nb_channels(payload);
700 
701   switch (payload_length_ms) {
702     case 10:
703     case 20: {
704       frames = 1;
705       break;
706     }
707     case 40: {
708       frames = 2;
709       break;
710     }
711     case 60: {
712       frames = 3;
713       break;
714     }
715     default: {
716       return 0; // It is actually even an invalid packet.
717     }
718   }
719 
720   /* The following is to parse the LBRR flags. */
721   if (opus_packet_parse(payload, payload_length_bytes, NULL, frame_data,
722                         frame_sizes, NULL) < 0) {
723     return 0;
724   }
725 
726   if (frame_sizes[0] <= 1) {
727     return 0;
728   }
729 
730   for (n = 0; n < channels; n++) {
731     if (frame_data[0][0] & (0x80 >> ((n + 1) * (frames + 1) - 1)))
732       return 1;
733   }
734 
735   return 0;
736 }
737