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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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-01"
  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="4" month="September" year="2014" />
  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. <figure>
  74. <artwork><![CDATA[
  75. for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
  76. ret = silk_init_decoder( &channel_state[ n ] );
  77. }
  78. + silk_memset(&((silk_decoder *)decState)->sStereo, 0,
  79. + sizeof(((silk_decoder *)decState)->sStereo));
  80. + /* Not strictly needed, but it's cleaner that way */
  81. + ((silk_decoder *)decState)->prev_decode_only_middle = 0;
  82. return ret;
  83. }
  84. ]]></artwork>
  85. </figure>
  86. This change affects the normative part of the decoder. Fortunately,
  87. the modified decoder is still compliant with the original specification because
  88. it still easily passes the testvectors. For example, for the float decoder
  89. at 48 kHz, the opus_compare (arbitrary) "quality score" changes from
  90. from 99.9333% to 99.925%.
  91. </t>
  92. </section>
  93. <section anchor="padding" title="Parsing of the Opus Packet Padding">
  94. <t>It was discovered that some invalid packets of very large size could trigger
  95. an out-of-bounds read in the Opus packet parsing code responsible for padding.
  96. This is due to an integer overflow if the signaled padding exceeds 2^31-1 bytes
  97. (the actual packet may be smaller). The code can be fixed by applying the following
  98. changes at line 596 of src/opus_decoder.c:
  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>
  119. <t>This packet parsing issue is limited to reading memory up
  120. to about 60 kB beyond the compressed buffer. This can only be triggered
  121. by a compressed packet more than about 16 MB long, so it's not a problem
  122. for RTP. In theory, it <spanx style="emph">could</spanx> crash a file
  123. decoder (e.g. Opus in Ogg) if the memory just after the incoming packet
  124. is out-of-range, but our attempts to trigger such a crash in a production
  125. application built using an affected version of the Opus decoder failed.</t>
  126. </section>
  127. <section anchor="resampler" title="Resampler buffer">
  128. <t>The SILK resampler had the following issues:
  129. <list style="numbers">
  130. <t>The calls to memcpy() were using sizeof(opus_int32), but the type of the
  131. local buffer was opus_int16.</t>
  132. <t>Because the size was wrong, this potentially allowed the source
  133. and destination regions of the memcpy() to overlap.
  134. We <spanx style="emph">believe</spanx> that nSamplesIn is at least fs_in_khZ,
  135. which is at least 8.
  136. Since RESAMPLER_ORDER_FIR_12 is only 8, that should not be a problem once
  137. the type size is fixed.</t>
  138. <t>The size of the buffer used RESAMPLER_MAX_BATCH_SIZE_IN, but the
  139. data stored in it was actually _twice_ the input batch size
  140. (nSamplesIn&lt;&lt;1).</t>
  141. </list></t>
  142. <t>
  143. The fact that the code never produced any error in testing (including when run under the
  144. Valgrind memory debugger), suggests that in practice
  145. the batch sizes are reasonable enough that none of the issues above
  146. was ever a problem. However, proving that is non-obvious.
  147. </t>
  148. <t>The code can be fixed by applying the following changes to line 70 of silk/resampler_private_IIR_FIR.c:
  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. Note: due to RFC formatting conventions, lines exceeding the column width
  200. in the patch above are split using a backslash character. The backslashes
  201. at the end of a line and the white space at the beginning
  202. of the following line are not part of the patch. A properly formatted patch
  203. including the three changes above is available at
  204. <eref target="http://jmvalin.ca/misc_stuff/opus_update.patch"/>.
  205. </t>
  206. </section>
  207. <section title="Downmix to Mono">
  208. <t>The last issue is not strictly a bug, but it is an issue that has been reported
  209. when downmixing an Opus decoded stream to mono, whether this is done inside the decoder
  210. or as a post-processing step on the stereo decoder output. Opus intensity stereo allows
  211. optionally coding the two channels 180-degrees out of phase on a per-band basis.
  212. This provides better stereo quality than forcing the two channels to be in phase,
  213. but when the output is downmixed to mono, the energy in the affected bands is cancelled
  214. sometimes resulting in audible artefacts.
  215. </t>
  216. <t>As a work-around for this issue, the decoder MAY choose not to apply the 180-degree
  217. phase shift when the output is meant to be downmixed (inside or
  218. outside of the decoder).
  219. </t>
  220. </section>
  221. <section anchor="IANA" title="IANA Considerations">
  222. <t>This document makes no request of IANA.</t>
  223. <t>Note to RFC Editor: this section may be removed on publication as an
  224. RFC.</t>
  225. </section>
  226. <section anchor="Acknowledgements" title="Acknowledgements">
  227. <t>We would like to thank Juri Aedla for reporting the issue with the parsing of
  228. the Opus padding.</t>
  229. </section>
  230. </middle>
  231. <back>
  232. <references title="References">
  233. <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml"?>
  234. <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.6716.xml"?>
  235. </references>
  236. </back>
  237. </rfc>