idnits 2.17.1 draft-ietf-codec-opus-update-04.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 (October 21, 2016) is 2742 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 266, 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: April 24, 2017 vocTone 6 October 21, 2016 8 Updates to the Opus Audio Codec 9 draft-ietf-codec-opus-update-04 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 April 24, 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 . . . . . . . . . . . . . . . . . 2 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 http://opus-codec.org/. The updated 73 specification remains fully compatible with the original 74 specification. 76 2. Terminology 78 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 79 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 80 document are to be interpreted as described in RFC 2119 [RFC2119]. 82 3. Stereo State Reset in SILK 84 The reference implementation does not reinitialize the stereo state 85 during a mode switch. The old stereo memory can produce a brief 86 impulse (i.e. single sample) in the decoded audio. This can be fixed 87 by changing silk/dec_API.c at line 72: 89 for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) { 90 ret = silk_init_decoder( &channel_state[ n ] ); 91 } 92 + silk_memset(&((silk_decoder *)decState)->sStereo, 0, 93 + sizeof(((silk_decoder *)decState)->sStereo)); 94 + /* Not strictly needed, but it's cleaner that way */ 95 + ((silk_decoder *)decState)->prev_decode_only_middle = 0; 97 return ret; 98 } 100 This change affects the normative part of the decoder, although the 101 amount of change is too small to make a significant impact on 102 testvectors. 104 4. Parsing of the Opus Packet Padding 106 It was discovered that some invalid packets of very large size could 107 trigger an out-of-bounds read in the Opus packet parsing code 108 responsible for padding. This is due to an integer overflow if the 109 signaled padding exceeds 2^31-1 bytes (the actual packet may be 110 smaller). The code can be fixed by applying the following changes at 111 line 596 of src/opus_decoder.c: 113 /* Padding flag is bit 6 */ 114 if (ch&0x40) 115 { 116 - int padding=0; 117 int p; 118 do { 119 if (len<=0) 120 return OPUS_INVALID_PACKET; 121 p = *data++; 122 len--; 123 - padding += p==255 ? 254: p; 124 + len -= p==255 ? 254: p; 125 } while (p==255); 126 - len -= padding; 127 } 129 This packet parsing issue is limited to reading memory up to about 60 130 kB beyond the compressed buffer. This can only be triggered by a 131 compressed packet more than about 16 MB long, so it's not a problem 132 for RTP. In theory, it _could_ crash a file decoder (e.g. Opus in 133 Ogg) if the memory just after the incoming packet is out-of-range, 134 but our attempts to trigger such a crash in a production application 135 built using an affected version of the Opus decoder failed. 137 5. Resampler buffer 139 The SILK resampler had the following issues: 141 1. The calls to memcpy() were using sizeof(opus_int32), but the type 142 of the local buffer was opus_int16. 144 2. Because the size was wrong, this potentially allowed the source 145 and destination regions of the memcpy() to overlap. We _believe_ 146 that nSamplesIn is at least fs_in_khZ, which is at least 8. 147 Since RESAMPLER_ORDER_FIR_12 is only 8, that should not be a 148 problem once the type size is fixed. 150 3. The size of the buffer used RESAMPLER_MAX_BATCH_SIZE_IN, but the 151 data stored in it was actually _twice_ the input batch size 152 (nSamplesIn<<1). 154 The fact that the code never produced any error in testing (including 155 when run under the Valgrind memory debugger), suggests that in 156 practice the batch sizes are reasonable enough that none of the 157 issues above was ever a problem. However, proving that is non- 158 obvious. 160 The code can be fixed by applying the following changes to line 70 of 161 silk/resampler_private_IIR_FIR.c: 163 ) 164 { 165 silk_resampler_state_struct *S = \ 166 (silk_resampler_state_struct *)SS; 167 opus_int32 nSamplesIn; 168 opus_int32 max_index_Q16, index_increment_Q16; 169 - opus_int16 buf[ RESAMPLER_MAX_BATCH_SIZE_IN + \ 170 RESAMPLER_ORDER_FIR_12 ]; 171 + opus_int16 buf[ 2*RESAMPLER_MAX_BATCH_SIZE_IN + \ 172 RESAMPLER_ORDER_FIR_12 ]; 174 /* Copy buffered samples to start of buffer */ 175 - silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 \ 176 * sizeof( opus_int32 ) ); 177 + silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 \ 178 * sizeof( opus_int16 ) ); 180 /* Iterate over blocks of frameSizeIn input samples */ 181 index_increment_Q16 = S->invRatio_Q16; 182 while( 1 ) { 183 nSamplesIn = silk_min( inLen, S->batchSize ); 184 /* Upsample 2x */ 185 silk_resampler_private_up2_HQ( S->sIIR, &buf[ \ 186 RESAMPLER_ORDER_FIR_12 ], in, nSamplesIn ); 188 max_index_Q16 = silk_LSHIFT32( nSamplesIn, 16 + 1 \ 189 ); /* + 1 because 2x upsampling */ 190 out = silk_resampler_private_IIR_FIR_INTERPOL( out, \ 191 buf, max_index_Q16, index_increment_Q16 ); 192 in += nSamplesIn; 193 inLen -= nSamplesIn; 195 if( inLen > 0 ) { 196 /* More iterations to do; copy last part of \ 197 filtered signal to beginning of buffer */ 198 - silk_memcpy( buf, &buf[ nSamplesIn << 1 ], \ 199 RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) ); 200 + silk_memmove( buf, &buf[ nSamplesIn << 1 ], \ 201 RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) ); 202 } else { 203 break; 204 } 205 } 207 /* Copy last part of filtered signal to the state for \ 208 the next call */ 209 - silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], \ 210 RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) ); 211 + silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], \ 212 RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) ); 213 } 215 Note: due to RFC formatting conventions, lines exceeding the column 216 width in the patch above are split using a backslash character. The 217 backslashes at the end of a line and the white space at the beginning 218 of the following line are not part of the patch. A properly 219 formatted patch including the three changes above is available at 220 . (EDITOR: change 221 to an ietf.org link when ready) 223 6. Integer wrap-around in inverse gain computation 225 It was discovered through decoder fuzzing that some bitstreams could 226 produce integer values exceeding 32-bits in 227 LPC_inverse_pred_gain_QA(), causing a wrap-around. Although the 228 error is harmless in practice, the C standard considers the behavior 229 as undefined, so the following patch to line 87 of silk/ 230 LPC_inv_pred_gain.c detects values that do not fit in a 32-bit 231 integer and considers the corresponding filters unstable: 233 /* Update AR coefficient */ 234 for( n = 0; n < k; n++ ) { 235 - tmp_QA = Aold_QA[ n ] - MUL32_FRAC_Q( \ 236 Aold_QA[ k - n - 1 ], rc_Q31, 31 ); 237 - Anew_QA[ n ] = MUL32_FRAC_Q( tmp_QA, rc_mult2 , mult2Q ); 238 + opus_int64 tmp64; 239 + tmp_QA = silk_SUB_SAT32( Aold_QA[ n ], MUL32_FRAC_Q( \ 240 Aold_QA[ k - n - 1 ], rc_Q31, 31 ) ); 241 + tmp64 = silk_RSHIFT_ROUND64( silk_SMULL( tmp_QA, \ 242 rc_mult2 ), mult2Q); 243 + if( tmp64 > silk_int32_MAX || tmp64 < silk_int32_MIN ) { 244 + return 0; 245 + } 246 + Anew_QA[ n ] = ( opus_int32 )tmp64; 247 } 249 7. Integer wrap-around in LSF decoding 251 It was discovered -- also from decoder fuzzing -- that an integer 252 wrap-around could occur when decoding line spectral frequency 253 coefficients from extreme bitstreams. The end result of the wrap- 254 around is an illegal read access on the stack, which the authors do 255 not believe is exploitable but should nonetheless be fixed. The 256 following patch to line 137 of silk/NLSF_stabilize.c prevents the 257 problem: 259 /* Keep delta_min distance between the NLSFs */ 260 for( i = 1; i < L; i++ ) 261 - NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], \ 262 NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); 263 + NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], \ 264 silk_ADD_SAT16( NLSF_Q15[i-1], NDeltaMin_Q15[i] ) ); 266 /* Last NLSF should be no higher than 1 - NDeltaMin[L] */ 268 8. Cap on Band Energy 270 On extreme bit-streams, it is possible for log-domain band energy 271 levels to exceed the maximum single-precision floating point value 272 once converted to a linear scale. This would later cause the decoded 273 values to be NaN, possibly causing problems in the software using the 274 PCM values. This can be avoided with the following patch to line 552 275 of celt/quant_bands.c: 277 { 278 opus_val16 lg = ADD16(oldEBands[i+c*m->nbEBands], 279 SHL16((opus_val16)eMeans[i],6)); 280 + lg = MIN32(QCONST32(32.f, 16), lg); 281 eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(lg),4); 282 } 283 for (;inbEBands;i++) 285 9. Hybrid Folding 287 When encoding in hybrid mode at low bitrate, we sometimes only have 288 enough bits to code a single CELT band (8 - 9.6 kHz). When that 289 happens, the second band (CELT band 18, from 9.6 to 12 kHz) cannot 290 use folding because it is wider than the amount already coded, and 291 falls back to LCG noise. Because it can also happen on transients 292 (e.g. stops), it can cause audible pre-echo. 294 To address the issue, we change the folding behavior so that it is 295 never forced to fall back to LCG due to the first band not containing 296 enough coefficients to fold onto the second band. This is achieved 297 by simply repeating part of the first band in the folding of the 298 second band. This changes the code in celt/bands.c around line 1237: 300 b = 0; 301 } 303 - if (resynth && M*eBands[i]-N >= M*eBands[start] && \ 304 (update_lowband || lowband_offset==0)) 305 + if (resynth && (M*eBands[i]-N >= M*eBands[start] || \ 306 i==start+1) && (update_lowband || lowband_offset==0)) 307 lowband_offset = i; 309 + if (i == start+1) 310 + { 311 + int n1, n2; 312 + int offset; 313 + n1 = M*(eBands[start+1]-eBands[start]); 314 + n2 = M*(eBands[start+2]-eBands[start+1]); 315 + offset = M*eBands[start]; 316 + /* Duplicate enough of the first band folding data to \ 317 be able to fold the second band. 318 + Copies no data for CELT-only mode. */ 319 + OPUS_COPY(&norm[offset+n1], &norm[offset+2*n1 - n2], n2-n1); 320 + if (C==2) 321 + OPUS_COPY(&norm2[offset+n1], &norm2[offset+2*n1 - n2], \ 322 n2-n1); 323 + } 324 + 325 tf_change = tf_res[i]; 326 if (i>=m->effEBands) 327 { 329 as well as line 1260: 331 fold_start = lowband_offset; 332 while(M*eBands[--fold_start] > effective_lowband); 333 fold_end = lowband_offset-1; 334 - while(M*eBands[++fold_end] < effective_lowband+N); 335 + while(++fold_end < i && M*eBands[fold_end] < \ 336 effective_lowband+N); 337 x_cm = y_cm = 0; 338 fold_i = fold_start; do { 339 x_cm |= collapse_masks[fold_i*C+0]; 341 The fix does not impact compatibility, because the improvement does 342 not depend on the encoder doing anything special. There is also no 343 reasonable way for an encoder to use the original behavior to improve 344 quality over the proposed change. 346 10. Downmix to Mono 348 The last issue is not strictly a bug, but it is an issue that has 349 been reported when downmixing an Opus decoded stream to mono, whether 350 this is done inside the decoder or as a post-processing step on the 351 stereo decoder output. Opus intensity stereo allows optionally 352 coding the two channels 180-degrees out of phase on a per-band basis. 353 This provides better stereo quality than forcing the two channels to 354 be in phase, but when the output is downmixed to mono, the energy in 355 the affected bands is cancelled sometimes resulting in audible 356 artefacts. 358 As a work-around for this issue, the decoder MAY choose not to apply 359 the 180-degree phase shift when the output is meant to be downmixed 360 (inside or outside of the decoder). 362 11. New Test Vectors 364 Changes in Section 9 and Section 10 have sufficient impact on the 365 testvectors to make them fail. For this reason, this document also 366 updates the Opus test vectors. The new test vectors now include two 367 decoded outputs for the same bitstream. The outputs with suffix 'm' 368 do not apply the CELT 180-degree phase shift as allowed in 369 Section 10, while the outputs without the suffix do. An 370 implementation is compliant as long as it passes either set of 371 vectors. 373 In addition, any Opus implementation that passes the original test 374 vectors from RFC 6716 [RFC6716] is still compliant with the Opus 375 specification. However, newer implementations SHOULD be based on the 376 new test vectors rather than the old ones. 378 The new test vectors are located at . (EDITOR: change to an ietf.org link when 380 ready) 382 12. IANA Considerations 384 This document makes no request of IANA. 386 Note to RFC Editor: this section may be removed on publication as an 387 RFC. 389 13. Acknowledgements 391 We would like to thank Juri Aedla for reporting the issue with the 392 parsing of the Opus padding. 394 14. References 396 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 397 Requirement Levels", BCP 14, RFC 2119, 398 DOI 10.17487/RFC2119, March 1997, 399 . 401 [RFC6716] Valin, JM., Vos, K., and T. Terriberry, "Definition of the 402 Opus Audio Codec", RFC 6716, DOI 10.17487/RFC6716, 403 September 2012, . 405 Authors' Addresses 407 Jean-Marc Valin 408 Mozilla Corporation 409 331 E. Evelyn Avenue 410 Mountain View, CA 94041 411 USA 413 Phone: +1 650 903-0800 414 Email: jmvalin@jmvalin.ca 416 Koen Vos 417 vocTone 419 Email: koenvos74@gmail.com