draft-ietf-codec-opus-update.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?xml version="1.0" encoding="US-ASCII"?>
  2. <!DOCTYPE rfc SYSTEM "rfc2629.dtd">
  3. <?rfc toc="yes"?>
  4. <?rfc tocompact="yes"?>
  5. <?rfc tocdepth="3"?>
  6. <?rfc tocindent="yes"?>
  7. <?rfc symrefs="yes"?>
  8. <?rfc sortrefs="yes"?>
  9. <?rfc comments="yes"?>
  10. <?rfc inline="yes"?>
  11. <?rfc compact="yes"?>
  12. <?rfc subcompact="no"?>
  13. <rfc category="std" docName="draft-ietf-codec-opus-update-02"
  14. ipr="trust200902">
  15. <front>
  16. <title abbrev="Opus Update">Updates to the Opus Audio Codec</title>
  17. <author initials="JM" surname="Valin" fullname="Jean-Marc Valin">
  18. <organization>Mozilla Corporation</organization>
  19. <address>
  20. <postal>
  21. <street>331 E. Evelyn Avenue</street>
  22. <city>Mountain View</city>
  23. <region>CA</region>
  24. <code>94041</code>
  25. <country>USA</country>
  26. </postal>
  27. <phone>+1 650 903-0800</phone>
  28. <email>jmvalin@jmvalin.ca</email>
  29. </address>
  30. </author>
  31. <author initials="K." surname="Vos" fullname="Koen Vos">
  32. <organization>vocTone</organization>
  33. <address>
  34. <postal>
  35. <street></street>
  36. <city></city>
  37. <region></region>
  38. <code></code>
  39. <country></country>
  40. </postal>
  41. <phone></phone>
  42. <email>koenvos74@gmail.com</email>
  43. </address>
  44. </author>
  45. <date day="1" month="July" year="2016" />
  46. <abstract>
  47. <t>This document addresses minor issues that were found in the specification
  48. of the Opus audio codec in <xref target="RFC6716">RFC 6716</xref>.</t>
  49. </abstract>
  50. </front>
  51. <middle>
  52. <section title="Introduction">
  53. <t>This document addresses minor issues that were discovered in the reference
  54. implementation of the Opus codec that serves as the specification in
  55. <xref target="RFC6716">RFC 6716</xref>. Only issues affecting the decoder are
  56. listed here. An up-to-date implementation of the Opus encoder can be found at
  57. http://opus-codec.org/. The updated specification remains fully compatible with
  58. the original specification and only one of the changes results in any difference
  59. in the audio output.
  60. </t>
  61. </section>
  62. <section title="Terminology">
  63. <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
  64. "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
  65. document are to be interpreted as described in <xref
  66. target="RFC2119">RFC 2119</xref>.</t>
  67. </section>
  68. <section title="Stereo State Reset in SILK">
  69. <t>The reference implementation does not reinitialize the stereo state
  70. during a mode switch. The old stereo memory can produce a brief impulse
  71. (i.e. single sample) in the decoded audio. This can be fixed by changing
  72. silk/dec_API.c at line 72:
  73. </t>
  74. <figure>
  75. <artwork><![CDATA[
  76. for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
  77. ret = silk_init_decoder( &channel_state[ n ] );
  78. }
  79. + silk_memset(&((silk_decoder *)decState)->sStereo, 0,
  80. + sizeof(((silk_decoder *)decState)->sStereo));
  81. + /* Not strictly needed, but it's cleaner that way */
  82. + ((silk_decoder *)decState)->prev_decode_only_middle = 0;
  83. return ret;
  84. }
  85. ]]></artwork>
  86. </figure>
  87. <t>
  88. This change affects the normative part of the decoder, although the
  89. amount of change is too small to make a significant impact on testvectors.
  90. </t>
  91. </section>
  92. <section anchor="padding" title="Parsing of the Opus Packet Padding">
  93. <t>It was discovered that some invalid packets of very large size could trigger
  94. an out-of-bounds read in the Opus packet parsing code responsible for padding.
  95. This is due to an integer overflow if the signaled padding exceeds 2^31-1 bytes
  96. (the actual packet may be smaller). The code can be fixed by applying the following
  97. changes at line 596 of src/opus_decoder.c:
  98. </t>
  99. <figure>
  100. <artwork><![CDATA[
  101. /* Padding flag is bit 6 */
  102. if (ch&0x40)
  103. {
  104. - int padding=0;
  105. int p;
  106. do {
  107. if (len<=0)
  108. return OPUS_INVALID_PACKET;
  109. p = *data++;
  110. len--;
  111. - padding += p==255 ? 254: p;
  112. + len -= p==255 ? 254: p;
  113. } while (p==255);
  114. - len -= padding;
  115. }
  116. ]]></artwork>
  117. </figure>
  118. <t>This packet parsing issue is limited to reading memory up
  119. to about 60 kB beyond the compressed buffer. This can only be triggered
  120. by a compressed packet more than about 16 MB long, so it's not a problem
  121. for RTP. In theory, it <spanx style="emph">could</spanx> crash a file
  122. decoder (e.g. Opus in Ogg) if the memory just after the incoming packet
  123. is out-of-range, but our attempts to trigger such a crash in a production
  124. application built using an affected version of the Opus decoder failed.</t>
  125. </section>
  126. <section anchor="resampler" title="Resampler buffer">
  127. <t>The SILK resampler had the following issues:
  128. <list style="numbers">
  129. <t>The calls to memcpy() were using sizeof(opus_int32), but the type of the
  130. local buffer was opus_int16.</t>
  131. <t>Because the size was wrong, this potentially allowed the source
  132. and destination regions of the memcpy() to overlap.
  133. We <spanx style="emph">believe</spanx> that nSamplesIn is at least fs_in_khZ,
  134. which is at least 8.
  135. Since RESAMPLER_ORDER_FIR_12 is only 8, that should not be a problem once
  136. the type size is fixed.</t>
  137. <t>The size of the buffer used RESAMPLER_MAX_BATCH_SIZE_IN, but the
  138. data stored in it was actually _twice_ the input batch size
  139. (nSamplesIn&lt;&lt;1).</t>
  140. </list></t>
  141. <t>
  142. The fact that the code never produced any error in testing (including when run under the
  143. Valgrind memory debugger), suggests that in practice
  144. the batch sizes are reasonable enough that none of the issues above
  145. was ever a problem. However, proving that is non-obvious.
  146. </t>
  147. <t>The code can be fixed by applying the following changes to line 70 of silk/resampler_private_IIR_FIR.c:
  148. </t>
  149. <figure>
  150. <artwork><![CDATA[
  151. )
  152. {
  153. silk_resampler_state_struct *S = \
  154. (silk_resampler_state_struct *)SS;
  155. opus_int32 nSamplesIn;
  156. opus_int32 max_index_Q16, index_increment_Q16;
  157. - opus_int16 buf[ RESAMPLER_MAX_BATCH_SIZE_IN + \
  158. RESAMPLER_ORDER_FIR_12 ];
  159. + opus_int16 buf[ 2*RESAMPLER_MAX_BATCH_SIZE_IN + \
  160. RESAMPLER_ORDER_FIR_12 ];
  161. /* Copy buffered samples to start of buffer */
  162. - silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 \
  163. * sizeof( opus_int32 ) );
  164. + silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 \
  165. * sizeof( opus_int16 ) );
  166. /* Iterate over blocks of frameSizeIn input samples */
  167. index_increment_Q16 = S->invRatio_Q16;
  168. while( 1 ) {
  169. nSamplesIn = silk_min( inLen, S->batchSize );
  170. /* Upsample 2x */
  171. silk_resampler_private_up2_HQ( S->sIIR, &buf[ \
  172. RESAMPLER_ORDER_FIR_12 ], in, nSamplesIn );
  173. max_index_Q16 = silk_LSHIFT32( nSamplesIn, 16 + 1 \
  174. ); /* + 1 because 2x upsampling */
  175. out = silk_resampler_private_IIR_FIR_INTERPOL( out, \
  176. buf, max_index_Q16, index_increment_Q16 );
  177. in += nSamplesIn;
  178. inLen -= nSamplesIn;
  179. if( inLen > 0 ) {
  180. /* More iterations to do; copy last part of \
  181. filtered signal to beginning of buffer */
  182. - silk_memcpy( buf, &buf[ nSamplesIn << 1 ], \
  183. RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) );
  184. + silk_memmove( buf, &buf[ nSamplesIn << 1 ], \
  185. RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) );
  186. } else {
  187. break;
  188. }
  189. }
  190. /* Copy last part of filtered signal to the state for \
  191. the next call */
  192. - silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], \
  193. RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) );
  194. + silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], \
  195. RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) );
  196. }
  197. ]]></artwork>
  198. </figure>
  199. <t>
  200. Note: due to RFC formatting conventions, lines exceeding the column width
  201. in the patch above are split using a backslash character. The backslashes
  202. at the end of a line and the white space at the beginning
  203. of the following line are not part of the patch. A properly formatted patch
  204. including the three changes above is available at
  205. <eref target="https://jmvalin.ca/misc_stuff/opus_update.patch"/>. (EDITOR:
  206. change to an ietf.org link when ready)
  207. </t>
  208. </section>
  209. <section title="Downmix to Mono" anchor="stereo">
  210. <t>The last issue is not strictly a bug, but it is an issue that has been reported
  211. when downmixing an Opus decoded stream to mono, whether this is done inside the decoder
  212. or as a post-processing step on the stereo decoder output. Opus intensity stereo allows
  213. optionally coding the two channels 180-degrees out of phase on a per-band basis.
  214. This provides better stereo quality than forcing the two channels to be in phase,
  215. but when the output is downmixed to mono, the energy in the affected bands is cancelled
  216. sometimes resulting in audible artefacts.
  217. </t>
  218. <t>As a work-around for this issue, the decoder MAY choose not to apply the 180-degree
  219. phase shift when the output is meant to be downmixed (inside or
  220. outside of the decoder).
  221. </t>
  222. </section>
  223. <section title="Hybrid Folding" anchor="folding">
  224. <t>When encoding in hybrid mode at low bitrate, we sometimes only have
  225. enough bits to code a single CELT band (8 - 9.6 kHz). When that happens,
  226. the second band (CELT band 18, from 9.6 to 12 kHz) cannot use folding
  227. because it is wider than the amount already coded, and falls back to
  228. LCG noise. Because it can also happen on transients (e.g. stops), it
  229. can cause audible pre-echo.
  230. </t>
  231. <t>
  232. To address the issue, we change the folding behaviour so that it is
  233. never forced to fall back to LCG due to the first band not containing
  234. enough coefficients to fold onto the second band. This
  235. is achieved by simply repeating part of the first band in the folding
  236. of the second band. This changes the code in celt/bands.c around line 1237:
  237. </t>
  238. <figure>
  239. <artwork><![CDATA[
  240. b = 0;
  241. }
  242. - if (resynth && M*eBands[i]-N >= M*eBands[start] && \
  243. (update_lowband || lowband_offset==0))
  244. + if (resynth && (M*eBands[i]-N >= M*eBands[start] || \
  245. i==start+1) && (update_lowband || lowband_offset==0))
  246. lowband_offset = i;
  247. + if (i == start+1)
  248. + {
  249. + int n1, n2;
  250. + int offset;
  251. + n1 = M*(eBands[start+1]-eBands[start]);
  252. + n2 = M*(eBands[start+2]-eBands[start+1]);
  253. + offset = M*eBands[start];
  254. + /* Duplicate enough of the first band folding data to \
  255. be able to fold the second band.
  256. + Copies no data for CELT-only mode. */
  257. + OPUS_COPY(&norm[offset+n1], &norm[offset+2*n1 - n2], n2-n1);
  258. + if (C==2)
  259. + OPUS_COPY(&norm2[offset+n1], &norm2[offset+2*n1 - n2], \
  260. n2-n1);
  261. + }
  262. +
  263. tf_change = tf_res[i];
  264. if (i>=m->effEBands)
  265. {
  266. ]]></artwork>
  267. </figure>
  268. <t>
  269. as well as line 1260:
  270. </t>
  271. <figure>
  272. <artwork><![CDATA[
  273. fold_start = lowband_offset;
  274. while(M*eBands[--fold_start] > effective_lowband);
  275. fold_end = lowband_offset-1;
  276. - while(M*eBands[++fold_end] < effective_lowband+N);
  277. + while(++fold_end < i && M*eBands[fold_end] < \
  278. effective_lowband+N);
  279. x_cm = y_cm = 0;
  280. fold_i = fold_start; do {
  281. x_cm |= collapse_masks[fold_i*C+0];
  282. ]]></artwork>
  283. </figure>
  284. <t>
  285. The fix does not impact compatibility, because the improvement does
  286. not depend on the encoder doing anything special. There is also no
  287. reasonable way for an encoder to use the original behaviour to
  288. improve quality over the proposed change.
  289. </t>
  290. </section>
  291. <section title="New Test Vectors">
  292. <t>Changes in <xref target="stereo"/> and <xref target="folding"/> have
  293. sufficient impact on the testvectors to make them fail. For this reason,
  294. this document also updates the Opus test vectors. The new test vectors now
  295. include two decoded outputs for the same bitstream. The outputs with
  296. suffix 'm' do not apply the CELT 180-degree phase shift as allowed in
  297. <xref target="stereo"/>, while the outputs with suffix 's' do. An
  298. implementation is compliant as long as it passes either the 'm' or the
  299. 's' set of vectors.
  300. </t>
  301. <t>
  302. In addition, any Opus implementation
  303. that passes the original test vectors from <xref target="RFC6716">RFC 6716</xref>
  304. is still compliant with the Opus specification. However, newer implementations
  305. SHOULD be based on the new test vectors rather than the old ones.
  306. </t>
  307. <t>The new test vectors are located at
  308. <eref target="https://jmvalin.ca/misc_stuff/opus_newvectors.tar.gz"/>. (EDITOR:
  309. change to an ietf.org link when ready)
  310. </t>
  311. </section>
  312. <section anchor="IANA" title="IANA Considerations">
  313. <t>This document makes no request of IANA.</t>
  314. <t>Note to RFC Editor: this section may be removed on publication as an
  315. RFC.</t>
  316. </section>
  317. <section anchor="Acknowledgements" title="Acknowledgements">
  318. <t>We would like to thank Juri Aedla for reporting the issue with the parsing of
  319. the Opus padding.</t>
  320. </section>
  321. </middle>
  322. <back>
  323. <references title="References">
  324. <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml"?>
  325. <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.6716.xml"?>
  326. </references>
  327. </back>
  328. </rfc>