opus_header.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* Copyright (C)2012 Xiph.Org Foundation
  2. File: opus_header.c
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  12. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  13. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  15. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. # include "config.h"
  25. #endif
  26. #include "opus_header.h"
  27. #include <string.h>
  28. #include <stdio.h>
  29. /* Header contents:
  30. - "OpusHead" (64 bits)
  31. - version number (8 bits)
  32. - Channels C (8 bits)
  33. - Pre-skip (16 bits)
  34. - Sampling rate (32 bits)
  35. - Gain in dB (16 bits, S7.8)
  36. - Mapping (8 bits, 0=single stream (mono/stereo) 1=Vorbis mapping,
  37. 2=ambisonics, 3=projection ambisonics, 4..239: reserved,
  38. 240..254: experiments, 255: multistream with no mapping)
  39. - if (mapping != 0)
  40. - N = total number of streams (8 bits)
  41. - M = number of paired streams (8 bits)
  42. - if (mapping != a projection family)
  43. - C times channel origin
  44. - if (C<2*M)
  45. - stream = byte/2
  46. - if (byte&0x1 == 0)
  47. - left
  48. else
  49. - right
  50. - else
  51. - stream = byte-M
  52. - else
  53. - D demixing matrix (C*(N+M)*16 bits)
  54. */
  55. typedef struct {
  56. unsigned char *data;
  57. int maxlen;
  58. int pos;
  59. } Packet;
  60. static int write_uint32(Packet *p, opus_uint32 val)
  61. {
  62. if (p->pos>p->maxlen-4)
  63. return 0;
  64. p->data[p->pos ] = (val ) & 0xFF;
  65. p->data[p->pos+1] = (val>> 8) & 0xFF;
  66. p->data[p->pos+2] = (val>>16) & 0xFF;
  67. p->data[p->pos+3] = (val>>24) & 0xFF;
  68. p->pos += 4;
  69. return 1;
  70. }
  71. static int write_uint16(Packet *p, opus_uint16 val)
  72. {
  73. if (p->pos>p->maxlen-2)
  74. return 0;
  75. p->data[p->pos ] = (val ) & 0xFF;
  76. p->data[p->pos+1] = (val>> 8) & 0xFF;
  77. p->pos += 2;
  78. return 1;
  79. }
  80. static int write_chars(Packet *p, const unsigned char *str, int nb_chars)
  81. {
  82. int i;
  83. if (p->pos>p->maxlen-nb_chars)
  84. return 0;
  85. for (i=0;i<nb_chars;i++)
  86. p->data[p->pos++] = str[i];
  87. return 1;
  88. }
  89. static int write_matrix_chars(Packet *p, const OpusGenericEncoder *st)
  90. {
  91. #ifdef OPUS_HAVE_OPUS_PROJECTION_H
  92. opus_int32 size;
  93. int ret;
  94. ret=opeint_encoder_ctl(st, OPUS_PROJECTION_GET_DEMIXING_MATRIX_SIZE(&size));
  95. if (ret != OPUS_OK) return 0;
  96. if (size>p->maxlen-p->pos) return 0;
  97. ret=opeint_encoder_ctl(st, OPUS_PROJECTION_GET_DEMIXING_MATRIX(&p->data[p->pos], size));
  98. if (ret != OPUS_OK) return 0;
  99. p->pos += size;
  100. return 1;
  101. #else
  102. (void)p;
  103. (void)st;
  104. return 0;
  105. #endif
  106. }
  107. int opeint_opus_header_get_size(const OpusHeader *h)
  108. {
  109. int len=0;
  110. if (opeint_use_projection(h->channel_mapping))
  111. {
  112. /* 19 bytes from fixed header,
  113. * 2 bytes for nb_streams & nb_coupled,
  114. * 2 bytes per cell of demixing matrix, where:
  115. * rows=channels, cols=nb_streams+nb_coupled
  116. */
  117. len=21+(h->channels*(h->nb_streams+h->nb_coupled)*2);
  118. }
  119. else
  120. {
  121. /* 19 bytes from fixed header,
  122. * 2 bytes for nb_streams & nb_coupled,
  123. * 1 byte per channel
  124. */
  125. len=21+h->channels;
  126. }
  127. return len;
  128. }
  129. int opeint_opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len, const OpusGenericEncoder *st)
  130. {
  131. int i;
  132. Packet p;
  133. unsigned char ch;
  134. p.data = packet;
  135. p.maxlen = len;
  136. p.pos = 0;
  137. if (len<19)return 0;
  138. if (!write_chars(&p, (const unsigned char*)"OpusHead", 8))
  139. return 0;
  140. /* Version is 1 */
  141. ch = 1;
  142. if (!write_chars(&p, &ch, 1))
  143. return 0;
  144. ch = h->channels;
  145. if (!write_chars(&p, &ch, 1))
  146. return 0;
  147. if (!write_uint16(&p, h->preskip))
  148. return 0;
  149. if (!write_uint32(&p, h->input_sample_rate))
  150. return 0;
  151. if (opeint_use_projection(h->channel_mapping))
  152. {
  153. #ifdef OPUS_HAVE_OPUS_PROJECTION_H
  154. opus_int32 matrix_gain;
  155. int ret;
  156. ret=opeint_encoder_ctl(st, OPUS_PROJECTION_GET_DEMIXING_MATRIX_GAIN(&matrix_gain));
  157. if (ret != OPUS_OK) return 0;
  158. if (!write_uint16(&p, h->gain + matrix_gain))
  159. return 0;
  160. #else
  161. return 0;
  162. #endif
  163. }
  164. else
  165. {
  166. if (!write_uint16(&p, h->gain))
  167. return 0;
  168. }
  169. ch = h->channel_mapping;
  170. if (!write_chars(&p, &ch, 1))
  171. return 0;
  172. if (h->channel_mapping != 0)
  173. {
  174. ch = h->nb_streams;
  175. if (!write_chars(&p, &ch, 1))
  176. return 0;
  177. ch = h->nb_coupled;
  178. if (!write_chars(&p, &ch, 1))
  179. return 0;
  180. /* Multi-stream support */
  181. if (opeint_use_projection(h->channel_mapping))
  182. {
  183. if (!write_matrix_chars(&p, st))
  184. return 0;
  185. }
  186. else
  187. {
  188. for (i=0;i<h->channels;i++)
  189. {
  190. if (!write_chars(&p, &h->stream_map[i], 1))
  191. return 0;
  192. }
  193. }
  194. }
  195. return p.pos;
  196. }
  197. /*
  198. Comments will be stored in the Vorbis style.
  199. It is described in the "Structure" section of
  200. http://www.xiph.org/ogg/vorbis/doc/v-comment.html
  201. However, Opus and other non-vorbis formats omit the "framing_bit".
  202. The comment header is decoded as follows:
  203. 1) [vendor_length] = read an unsigned integer of 32 bits
  204. 2) [vendor_string] = read a UTF-8 vector as [vendor_length] octets
  205. 3) [user_comment_list_length] = read an unsigned integer of 32 bits
  206. 4) iterate [user_comment_list_length] times {
  207. 5) [length] = read an unsigned integer of 32 bits
  208. 6) this iteration's user comment = read a UTF-8 vector as [length] octets
  209. }
  210. 7) done.
  211. */
  212. #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
  213. ((buf[base+2]<<16)&0xff0000)| \
  214. ((buf[base+1]<<8)&0xff00)| \
  215. (buf[base]&0xff))
  216. #define writeint(buf, base, val) do{ buf[base+3]=((val)>>24)&0xff; \
  217. buf[base+2]=((val)>>16)&0xff; \
  218. buf[base+1]=((val)>>8)&0xff; \
  219. buf[base]=(val)&0xff; \
  220. }while(0)
  221. void opeint_comment_init(char **comments, int* length, const char *vendor_string)
  222. {
  223. /*The 'vendor' field should be the actual encoding library used.*/
  224. int vendor_length=strlen(vendor_string);
  225. int user_comment_list_length=0;
  226. int len=8+4+vendor_length+4;
  227. char *p=(char*)malloc(len);
  228. if (p == NULL) {
  229. len=0;
  230. } else {
  231. memcpy(p, "OpusTags", 8);
  232. writeint(p, 8, vendor_length);
  233. memcpy(p+12, vendor_string, vendor_length);
  234. writeint(p, 12+vendor_length, user_comment_list_length);
  235. }
  236. *length=len;
  237. *comments=p;
  238. }
  239. int opeint_comment_add(char **comments, int* length, const char *tag, const char *val)
  240. {
  241. char* p=*comments;
  242. int vendor_length=readint(p, 8);
  243. int user_comment_list_length=readint(p, 8+4+vendor_length);
  244. int tag_len=(tag?strlen(tag)+1:0);
  245. int val_len=strlen(val);
  246. int len=(*length)+4+tag_len+val_len;
  247. p=(char*)realloc(p, len);
  248. if (p == NULL) return 1;
  249. writeint(p, *length, tag_len+val_len); /* length of comment */
  250. if(tag){
  251. memcpy(p+*length+4, tag, tag_len); /* comment tag */
  252. (p+*length+4)[tag_len-1] = '='; /* separator */
  253. }
  254. memcpy(p+*length+4+tag_len, val, val_len); /* comment */
  255. writeint(p, 8+4+vendor_length, user_comment_list_length+1);
  256. *comments=p;
  257. *length=len;
  258. return 0;
  259. }
  260. void opeint_comment_pad(char **comments, int* length, int amount)
  261. {
  262. if(amount>0){
  263. int i;
  264. int newlen;
  265. char* p=*comments;
  266. /*Make sure there is at least amount worth of padding free, and
  267. round up to the maximum that fits in the current ogg segments.*/
  268. newlen=(*length+amount+255)/255*255-1;
  269. p=realloc(p,newlen);
  270. if (p == NULL) return;
  271. for(i=*length;i<newlen;i++)p[i]=0;
  272. *comments=p;
  273. *length=newlen;
  274. }
  275. }
  276. #undef readint
  277. #undef writeint