idnits 2.17.1 draft-ietf-codec-opus-update-05.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** The document seems to lack a Security Considerations section. ** The document seems to lack separate sections for Informative/Normative References. All references will be assumed normative when checking for downward references. ** The abstract seems to contain references ([RFC6716]), which it shouldn't. Please replace those with straight textual mentions of the documents in question. == There are 1 instance of lines with non-RFC2606-compliant FQDNs in the document. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (December 19, 2016) is 2656 days in the past. Is this intentional? -- Found something which looks like a code comment -- if you have code sections in the document, please surround them with '' and '' lines. Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) == Missing Reference: 'L' is mentioned on line 271, but not defined Summary: 3 errors (**), 0 flaws (~~), 3 warnings (==), 2 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 Network Working Group JM. Valin 3 Internet-Draft Mozilla Corporation 4 Intended status: Standards Track K. Vos 5 Expires: June 22, 2017 vocTone 6 December 19, 2016 8 Updates to the Opus Audio Codec 9 draft-ietf-codec-opus-update-05 11 Abstract 13 This document addresses minor issues that were found in the 14 specification of the Opus audio codec in RFC 6716 [RFC6716]. 16 Status of This Memo 18 This Internet-Draft is submitted in full conformance with the 19 provisions of BCP 78 and BCP 79. 21 Internet-Drafts are working documents of the Internet Engineering 22 Task Force (IETF). Note that other groups may also distribute 23 working documents as Internet-Drafts. The list of current Internet- 24 Drafts is at http://datatracker.ietf.org/drafts/current/. 26 Internet-Drafts are draft documents valid for a maximum of six months 27 and may be updated, replaced, or obsoleted by other documents at any 28 time. It is inappropriate to use Internet-Drafts as reference 29 material or to cite them other than as "work in progress." 31 This Internet-Draft will expire on June 22, 2017. 33 Copyright Notice 35 Copyright (c) 2016 IETF Trust and the persons identified as the 36 document authors. All rights reserved. 38 This document is subject to BCP 78 and the IETF Trust's Legal 39 Provisions Relating to IETF Documents 40 (http://trustee.ietf.org/license-info) in effect on the date of 41 publication of this document. Please review these documents 42 carefully, as they describe your rights and restrictions with respect 43 to this document. Code Components extracted from this document must 44 include Simplified BSD License text as described in Section 4.e of 45 the Trust Legal Provisions and are provided without warranty as 46 described in the Simplified BSD License. 48 Table of Contents 50 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 2 51 2. Terminology . . . . . . . . . . . . . . . . . . . . . . . . . 2 52 3. Stereo State Reset in SILK . . . . . . . . . . . . . . . . . 3 53 4. Parsing of the Opus Packet Padding . . . . . . . . . . . . . 3 54 5. Resampler buffer . . . . . . . . . . . . . . . . . . . . . . 4 55 6. Integer wrap-around in inverse gain computation . . . . . . . 5 56 7. Integer wrap-around in LSF decoding . . . . . . . . . . . . . 6 57 8. Cap on Band Energy . . . . . . . . . . . . . . . . . . . . . 6 58 9. Hybrid Folding . . . . . . . . . . . . . . . . . . . . . . . 7 59 10. Downmix to Mono . . . . . . . . . . . . . . . . . . . . . . . 9 60 11. New Test Vectors . . . . . . . . . . . . . . . . . . . . . . 9 61 12. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 9 62 13. Acknowledgements . . . . . . . . . . . . . . . . . . . . . . 9 63 14. References . . . . . . . . . . . . . . . . . . . . . . . . . 10 64 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 10 66 1. Introduction 68 This document addresses minor issues that were discovered in the 69 reference implementation of the Opus codec that serves as the 70 specification in RFC 6716 [RFC6716]. Only issues affecting the 71 decoder are listed here. An up-to-date implementation of the Opus 72 encoder can be found at https://opus-codec.org/. 74 Some of the changes in this document update normative behaviour in a 75 way that requires new test vectors. The English text of the 76 specification is unaffected, only the C implementation is. The 77 updated specification remains fully compatible with the original 78 specification. 80 Note: due to RFC formatting conventions, lines exceeding the column 81 width in the patch above are split using a backslash character. The 82 backslashes at the end of a line and the white space at the beginning 83 of the following line are not part of the patch. A properly 84 formatted patch including the three changes above is available at 85 . (EDITOR: change 86 to an ietf.org link when ready) 88 2. Terminology 90 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 91 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 92 document are to be interpreted as described in RFC 2119 [RFC2119]. 94 3. Stereo State Reset in SILK 96 The reference implementation does not reinitialize the stereo state 97 during a mode switch. The old stereo memory can produce a brief 98 impulse (i.e. single sample) in the decoded audio. This can be fixed 99 by changing silk/dec_API.c at line 72: 101 for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) { 102 ret = silk_init_decoder( &channel_state[ n ] ); 103 } 104 + silk_memset(&((silk_decoder *)decState)->sStereo, 0, 105 + sizeof(((silk_decoder *)decState)->sStereo)); 106 + /* Not strictly needed, but it's cleaner that way */ 107 + ((silk_decoder *)decState)->prev_decode_only_middle = 0; 109 return ret; 110 } 112 This change affects the normative part of the decoder, although the 113 amount of change is too small to make a significant impact on 114 testvectors. 116 4. Parsing of the Opus Packet Padding 118 It was discovered that some invalid packets of very large size could 119 trigger an out-of-bounds read in the Opus packet parsing code 120 responsible for padding. This is due to an integer overflow if the 121 signaled padding exceeds 2^31-1 bytes (the actual packet may be 122 smaller). The code can be fixed by applying the following changes at 123 line 596 of src/opus_decoder.c: 125 /* Padding flag is bit 6 */ 126 if (ch&0x40) 127 { 128 - int padding=0; 129 int p; 130 do { 131 if (len<=0) 132 return OPUS_INVALID_PACKET; 133 p = *data++; 134 len--; 135 - padding += p==255 ? 254: p; 136 + len -= p==255 ? 254: p; 137 } while (p==255); 138 - len -= padding; 139 } 141 This packet parsing issue is limited to reading memory up to about 60 142 kB beyond the compressed buffer. This can only be triggered by a 143 compressed packet more than about 16 MB long, so it's not a problem 144 for RTP. In theory, it _could_ crash a file decoder (e.g. Opus in 145 Ogg) if the memory just after the incoming packet is out-of-range, 146 but our attempts to trigger such a crash in a production application 147 built using an affected version of the Opus decoder failed. 149 5. Resampler buffer 151 The SILK resampler had the following issues: 153 1. The calls to memcpy() were using sizeof(opus_int32), but the type 154 of the local buffer was opus_int16. 156 2. Because the size was wrong, this potentially allowed the source 157 and destination regions of the memcpy() to overlap. We _believe_ 158 that nSamplesIn is at least fs_in_khZ, which is at least 8. 159 Since RESAMPLER_ORDER_FIR_12 is only 8, that should not be a 160 problem once the type size is fixed. 162 3. The size of the buffer used RESAMPLER_MAX_BATCH_SIZE_IN, but the 163 data stored in it was actually _twice_ the input batch size 164 (nSamplesIn<<1). 166 The fact that the code never produced any error in testing (including 167 when run under the Valgrind memory debugger), suggests that in 168 practice the batch sizes are reasonable enough that none of the 169 issues above was ever a problem. However, proving that is non- 170 obvious. 172 The code can be fixed by applying the following changes to line 78 of 173 silk/resampler_private_IIR_FIR.c: 175 ) 176 { 177 silk_resampler_state_struct *S = \ 178 (silk_resampler_state_struct *)SS; 179 opus_int32 nSamplesIn; 180 opus_int32 max_index_Q16, index_increment_Q16; 181 - opus_int16 buf[ RESAMPLER_MAX_BATCH_SIZE_IN + \ 182 RESAMPLER_ORDER_FIR_12 ]; 183 + opus_int16 buf[ 2*RESAMPLER_MAX_BATCH_SIZE_IN + \ 184 RESAMPLER_ORDER_FIR_12 ]; 186 /* Copy buffered samples to start of buffer */ 187 - silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 \ 188 * sizeof( opus_int32 ) ); 189 + silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 \ 190 * sizeof( opus_int16 ) ); 192 /* Iterate over blocks of frameSizeIn input samples */ 193 index_increment_Q16 = S->invRatio_Q16; 194 while( 1 ) { 195 nSamplesIn = silk_min( inLen, S->batchSize ); 197 /* Upsample 2x */ 198 silk_resampler_private_up2_HQ( S->sIIR, &buf[ \ 199 RESAMPLER_ORDER_FIR_12 ], in, nSamplesIn ); 201 max_index_Q16 = silk_LSHIFT32( nSamplesIn, 16 + 1 \ 202 ); /* + 1 because 2x upsampling */ 203 out = silk_resampler_private_IIR_FIR_INTERPOL( out, \ 204 buf, max_index_Q16, index_increment_Q16 ); 205 in += nSamplesIn; 206 inLen -= nSamplesIn; 208 if( inLen > 0 ) { 209 /* More iterations to do; copy last part of \ 210 filtered signal to beginning of buffer */ 211 - silk_memcpy( buf, &buf[ nSamplesIn << 1 ], \ 212 RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) ); 213 + silk_memmove( buf, &buf[ nSamplesIn << 1 ], \ 214 RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) ); 215 } else { 216 break; 217 } 218 } 220 /* Copy last part of filtered signal to the state for \ 221 the next call */ 222 - silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], \ 223 RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) ); 224 + silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], \ 225 RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) ); 226 } 228 6. Integer wrap-around in inverse gain computation 230 It was discovered through decoder fuzzing that some bitstreams could 231 produce integer values exceeding 32-bits in 232 LPC_inverse_pred_gain_QA(), causing a wrap-around. Although the 233 error is harmless in practice, the C standard considers the behavior 234 as undefined, so the following patch to line 87 of silk/ 235 LPC_inv_pred_gain.c detects values that do not fit in a 32-bit 236 integer and considers the corresponding filters unstable: 238 /* Update AR coefficient */ 239 for( n = 0; n < k; n++ ) { 240 - tmp_QA = Aold_QA[ n ] - MUL32_FRAC_Q( \ 241 Aold_QA[ k - n - 1 ], rc_Q31, 31 ); 242 - Anew_QA[ n ] = MUL32_FRAC_Q( tmp_QA, rc_mult2 , mult2Q ); 243 + opus_int64 tmp64; 244 + tmp_QA = silk_SUB_SAT32( Aold_QA[ n ], MUL32_FRAC_Q( \ 245 Aold_QA[ k - n - 1 ], rc_Q31, 31 ) ); 246 + tmp64 = silk_RSHIFT_ROUND64( silk_SMULL( tmp_QA, \ 247 rc_mult2 ), mult2Q); 248 + if( tmp64 > silk_int32_MAX || tmp64 < silk_int32_MIN ) { 249 + return 0; 250 + } 251 + Anew_QA[ n ] = ( opus_int32 )tmp64; 252 } 254 7. Integer wrap-around in LSF decoding 256 It was discovered -- also from decoder fuzzing -- that an integer 257 wrap-around could occur when decoding line spectral frequency 258 coefficients from extreme bitstreams. The end result of the wrap- 259 around is an illegal read access on the stack, which the authors do 260 not believe is exploitable but should nonetheless be fixed. The 261 following patch to line 137 of silk/NLSF_stabilize.c prevents the 262 problem: 264 /* Keep delta_min distance between the NLSFs */ 265 for( i = 1; i < L; i++ ) 266 - NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], \ 267 NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); 268 + NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], \ 269 silk_ADD_SAT16( NLSF_Q15[i-1], NDeltaMin_Q15[i] ) ); 271 /* Last NLSF should be no higher than 1 - NDeltaMin[L] */ 273 8. Cap on Band Energy 275 On extreme bit-streams, it is possible for log-domain band energy 276 levels to exceed the maximum single-precision floating point value 277 once converted to a linear scale. This would later cause the decoded 278 values to be NaN, possibly causing problems in the software using the 279 PCM values. This can be avoided with the following patch to line 552 280 of celt/quant_bands.c: 282 { 283 opus_val16 lg = ADD16(oldEBands[i+c*m->nbEBands], 284 SHL16((opus_val16)eMeans[i],6)); 285 + lg = MIN32(QCONST32(32.f, 16), lg); 286 eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(lg),4); 287 } 288 for (;inbEBands;i++) 290 9. Hybrid Folding 292 When encoding in hybrid mode at low bitrate, we sometimes only have 293 enough bits to code a single CELT band (8 - 9.6 kHz). When that 294 happens, the second band (CELT band 18, from 9.6 to 12 kHz) cannot 295 use folding because it is wider than the amount already coded, and 296 falls back to LCG noise. Because it can also happen on transients 297 (e.g. stops), it can cause audible pre-echo. 299 To address the issue, we change the folding behavior so that it is 300 never forced to fall back to LCG due to the first band not containing 301 enough coefficients to fold onto the second band. This is achieved 302 by simply repeating part of the first band in the folding of the 303 second band. This changes the code in celt/bands.c around line 1237: 305 b = 0; 306 } 308 - if (resynth && M*eBands[i]-N >= M*eBands[start] && \ 309 (update_lowband || lowband_offset==0)) 310 + if (resynth && (M*eBands[i]-N >= M*eBands[start] || \ 311 i==start+1) && (update_lowband || lowband_offset==0)) 312 lowband_offset = i; 314 + if (i == start+1) 315 + { 316 + int n1, n2; 317 + int offset; 318 + n1 = M*(eBands[start+1]-eBands[start]); 319 + n2 = M*(eBands[start+2]-eBands[start+1]); 320 + offset = M*eBands[start]; 321 + /* Duplicate enough of the first band folding data to \ 322 be able to fold the second band. 323 + Copies no data for CELT-only mode. */ 324 + OPUS_COPY(&norm[offset+n1], &norm[offset+2*n1 - n2], n2-n1); 325 + if (C==2) 326 + OPUS_COPY(&norm2[offset+n1], &norm2[offset+2*n1 - n2], \ 327 n2-n1); 328 + } 329 + 330 tf_change = tf_res[i]; 331 if (i>=m->effEBands) 332 { 334 as well as line 1260: 336 fold_start = lowband_offset; 337 while(M*eBands[--fold_start] > effective_lowband); 338 fold_end = lowband_offset-1; 339 - while(M*eBands[++fold_end] < effective_lowband+N); 340 + while(++fold_end < i && M*eBands[fold_end] < \ 341 effective_lowband+N); 342 x_cm = y_cm = 0; 343 fold_i = fold_start; do { 344 x_cm |= collapse_masks[fold_i*C+0]; 346 The fix does not impact compatibility, because the improvement does 347 not depend on the encoder doing anything special. There is also no 348 reasonable way for an encoder to use the original behavior to improve 349 quality over the proposed change. 351 10. Downmix to Mono 353 The last issue is not strictly a bug, but it is an issue that has 354 been reported when downmixing an Opus decoded stream to mono, whether 355 this is done inside the decoder or as a post-processing step on the 356 stereo decoder output. Opus intensity stereo allows optionally 357 coding the two channels 180-degrees out of phase on a per-band basis. 358 This provides better stereo quality than forcing the two channels to 359 be in phase, but when the output is downmixed to mono, the energy in 360 the affected bands is cancelled sometimes resulting in audible 361 artefacts. 363 As a work-around for this issue, the decoder MAY choose not to apply 364 the 180-degree phase shift when the output is meant to be downmixed 365 (inside or outside of the decoder). 367 11. New Test Vectors 369 Changes in Section 9 and Section 10 have sufficient impact on the 370 testvectors to make them fail. For this reason, this document also 371 updates the Opus test vectors. The new test vectors now include two 372 decoded outputs for the same bitstream. The outputs with suffix 'm' 373 do not apply the CELT 180-degree phase shift as allowed in 374 Section 10, while the outputs without the suffix do. An 375 implementation is compliant as long as it passes either set of 376 vectors. 378 In addition, any Opus implementation that passes the original test 379 vectors from RFC 6716 [RFC6716] is still compliant with the Opus 380 specification. However, newer implementations SHOULD be based on the 381 new test vectors rather than the old ones. 383 The new test vectors are located at . (EDITOR: change to an ietf.org link when 385 ready) 387 12. IANA Considerations 389 This document makes no request of IANA. 391 Note to RFC Editor: this section may be removed on publication as an 392 RFC. 394 13. Acknowledgements 396 We would like to thank Juri Aedla for reporting the issue with the 397 parsing of the Opus padding. Also, thanks to Jonathan Lennox and 398 Mark Harris for their feedback on this document. 400 14. References 402 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 403 Requirement Levels", BCP 14, RFC 2119, 404 DOI 10.17487/RFC2119, March 1997, 405 . 407 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 408 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 409 September 2012, . 411 Authors' Addresses 413 Jean-Marc Valin 414 Mozilla Corporation 415 331 E. Evelyn Avenue 416 Mountain View, CA 94041 417 USA 419 Phone: +1 650 903-0800 420 Email: jmvalin@jmvalin.ca 422 Koen Vos 423 vocTone 425 Email: koenvos74@gmail.com