draft-valin-codec-opus-update.xml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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-valin-codec-opus-update-00"
  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>650 Castro Street</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="T." surname="Terriberry" fullname="Timothy B. Terriberry">
  32. <organization>Mozilla Corporation</organization>
  33. <address>
  34. <postal>
  35. <street>650 Castro Street</street>
  36. <city>Mountain View</city>
  37. <region>CA</region>
  38. <code>94041</code>
  39. <country>USA</country>
  40. </postal>
  41. <phone>+1 650 903-0800</phone>
  42. <email>tterriberry@mozilla.com</email>
  43. </address>
  44. </author>
  45. <author initials="K." surname="Vos" fullname="Koen Vos">
  46. <organization>Skype Technologies S.A.</organization>
  47. <address>
  48. <postal>
  49. <street>Soder Malarstrand 43</street>
  50. <city>Stockholm</city>
  51. <region></region>
  52. <code>11825</code>
  53. <country>SE</country>
  54. </postal>
  55. <phone>+46 73 085 7619</phone>
  56. <email>koen.vos@skype.net</email>
  57. </address>
  58. </author>
  59. <date day="12" month="July" year="2013" />
  60. <abstract>
  61. <t>This document addresses minor issues that were found in the specification
  62. of the Opus audio codec in <xref target="RFC6716">RFC 6716</xref>.</t>
  63. </abstract>
  64. </front>
  65. <middle>
  66. <section title="Introduction">
  67. <t>This document address minor issues that were discovered in the reference
  68. implementation of the Opus codec that serves as the specification in
  69. <xref target="RFC6716">RFC 6716</xref>. Only issues affecting the decoder are
  70. listed here. An up-to-date implementation of the Opus encoder can be found at
  71. http://opus-codec.org/. The updated specification remains fully compatible with
  72. the original specification and only one of the changes results in any difference
  73. in the audio output.
  74. </t>
  75. </section>
  76. <section title="Terminology">
  77. <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
  78. "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
  79. document are to be interpreted as described in <xref
  80. target="RFC2119">RFC 2119</xref>.</t>
  81. </section>
  82. <section title="Stereo State Reset in SILK">
  83. <t>The reference implementation does not reinitialize the stereo state
  84. during a mode switch. The old stereo memory can produce a brief impulse
  85. (i.e. single sample) in the decoded audio. This can be fixed by changing
  86. silk/dec_API.c at line 72:
  87. <figure>
  88. <artwork><![CDATA[
  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;
  96. return ret;
  97. }
  98. ]]></artwork>
  99. </figure>
  100. This change affects the normative part of the decoder. Fortunately,
  101. the modified decoder is still compliant with the original specification because
  102. it still easily passes the testvectors. For example, for the float decoder
  103. at 48 kHz, the opus_compare (arbitrary) "quality score" changes from
  104. from 99.9333% to 99.925%.
  105. </t>
  106. </section>
  107. <section anchor="padding" title="Parsing of the Opus Packet Padding">
  108. <t>It was discovered that some invalid packets of very large size could trigger
  109. an out-of-bounds read in the Opus packet parsing code responsible for padding.
  110. This is due to an integer overflow if the signaled padding exceeds 2^31-1 bytes
  111. (the actual packet may be smaller). The code can be fixed by applying the following
  112. changes at line 596 of src/opus_decoder.c:
  113. <figure>
  114. <artwork><![CDATA[
  115. /* Padding flag is bit 6 */
  116. if (ch&0x40)
  117. {
  118. - int padding=0;
  119. int p;
  120. do {
  121. if (len<=0)
  122. return OPUS_INVALID_PACKET;
  123. p = *data++;
  124. len--;
  125. - padding += p==255 ? 254: p;
  126. + len -= p==255 ? 254: p;
  127. } while (p==255);
  128. - len -= padding;
  129. }
  130. ]]></artwork>
  131. </figure>
  132. </t>
  133. <t>This packet parsing issue is limited to reading memory up
  134. to about 60 kB beyond the compressed buffer. This can only be triggered
  135. by a compressed packet more than about 16 MB long, so it's not a problem
  136. for RTP. In theory, it <spanx style="emph">could</spanx> crash a file
  137. decoder (e.g. Opus in Ogg) if the memory just after the incoming packet
  138. is out-of-range, but that could not be achieved when attempted in a production
  139. application built using an affected version of the Opus decoder.</t>
  140. </section>
  141. <section anchor="resampler" title="Resampler buffer">
  142. <t>The SILK resampler had the following issues:
  143. <list style="numbers">
  144. <t>The calls to memcpy() were using sizeof(opus_int32), but the type of the
  145. local buffer was opus_int16.</t>
  146. <t>Because the size was wrong, this potentially allowed the source
  147. and destination regions of the memcpy overlap.
  148. We <spanx style="emph">believe</spanx> that nSamplesIn is at least fs_in_khZ,
  149. which is at least 8.
  150. Since RESAMPLER_ORDER_FIR_12 is only 8,that should not be a problem once
  151. the type size is fixed.</t>
  152. <t>The size of the buffer used RESAMPLER_MAX_BATCH_SIZE_IN, but the
  153. data stored in it was actually _twice_ the input batch size
  154. (nSamplesIn&lt;&lt;1).</t>
  155. </list></t>
  156. <t>
  157. The fact that the code never produced any error in testing (including when run under the
  158. Valgrind memory debugger), suggests that in practice
  159. the batch sizes are reasonable enough that none of the issues above
  160. was ever a problem. However, proving that is non-obvious.
  161. </t>
  162. <t>The code can be fixed by applying the following changes to like 70 of silk/resampler_private_IIR_FIR.c:
  163. <figure>
  164. <artwork><![CDATA[
  165. opus_int16 out[], /* O Output signal */
  166. const opus_int16 in[], /* I Input signal */
  167. opus_int32 inLen /* I Number of input samples */
  168. )
  169. {
  170. silk_resampler_state_struct *S = (silk_resampler_state_struct *)SS;
  171. opus_int32 nSamplesIn;
  172. opus_int32 max_index_Q16, index_increment_Q16;
  173. - opus_int16 buf[ RESAMPLER_MAX_BATCH_SIZE_IN + RESAMPLER_ORDER_FIR_12 ];
  174. + opus_int16 buf[ 2*RESAMPLER_MAX_BATCH_SIZE_IN + RESAMPLER_ORDER_FIR_12 ];
  175. /* Copy buffered samples to start of buffer */
  176. - silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) );
  177. + silk_memcpy( buf, S->sFIR, RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) );
  178. /* Iterate over blocks of frameSizeIn input samples */
  179. index_increment_Q16 = S->invRatio_Q16;
  180. while( 1 ) {
  181. nSamplesIn = silk_min( inLen, S->batchSize );
  182. /* Upsample 2x */
  183. silk_resampler_private_up2_HQ( S->sIIR, &buf[ RESAMPLER_ORDER_FIR_12 ], in, nSamplesIn );
  184. max_index_Q16 = silk_LSHIFT32( nSamplesIn, 16 + 1 ); /* + 1 because 2x upsampling */
  185. out = silk_resampler_private_IIR_FIR_INTERPOL( out, buf, max_index_Q16, index_increment_Q16 );
  186. in += nSamplesIn;
  187. inLen -= nSamplesIn;
  188. if( inLen > 0 ) {
  189. /* More iterations to do; copy last part of filtered signal to beginning of buffer */
  190. - silk_memcpy( buf, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) );
  191. + silk_memmove( buf, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) );
  192. } else {
  193. break;
  194. }
  195. }
  196. /* Copy last part of filtered signal to the state for the next call */
  197. - silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int32 ) );
  198. + silk_memcpy( S->sFIR, &buf[ nSamplesIn << 1 ], RESAMPLER_ORDER_FIR_12 * sizeof( opus_int16 ) );
  199. }
  200. ]]></artwork>
  201. </figure>
  202. </t>
  203. </section>
  204. <section title="Downmix to Mono">
  205. <t>The last issue is not strictly a bug, but it is an issue that has been reported
  206. when downmixing Opus decoded stream to mono, whether this is done inside the decoder
  207. or as a post-processing on the stereo decoder output. Opus intensity stereo allows
  208. optionally coding the two channels 180-degrees out of phase on a per-band basis.
  209. This provides better stereo quality than forcing the two channels to be in phase,
  210. but when the output is downmixed to mono, the energy in the affected bands is cancelled
  211. sometimes resulting in audible artefacts.
  212. </t>
  213. <t>A possible work-around for this issue would be to optionally allow the decoder to
  214. not apply the 180-degree phase shift when the output is meant to be downmixed (inside or
  215. outside of the decoder).
  216. </t>
  217. </section>
  218. <section anchor="IANA" title="IANA Considerations">
  219. <t>This document makes no request of IANA.</t>
  220. <t>Note to RFC Editor: this section may be removed on publication as an
  221. RFC.</t>
  222. </section>
  223. <section anchor="Acknowledgements" title="Acknowledgements">
  224. <t>We would like to thank Juri Aedla for reporting the issue with the parsing of
  225. the Opus padding.</t>
  226. </section>
  227. </middle>
  228. <back>
  229. <references title="References">
  230. <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml"?>
  231. <?rfc include="http://xml.resource.org/public/rfc/bibxml/reference.RFC.6716.xml"?>
  232. </references>
  233. </back>
  234. </rfc>