speex_header.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (C) 2002 Jean-Marc Valin
  2. File: speex_header.c
  3. Describes the Speex header
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "arch.h"
  31. #include <speex/speex_header.h>
  32. #include <speex/speex.h>
  33. #include "os_support.h"
  34. #ifndef NULL
  35. #define NULL 0
  36. #endif
  37. /** Convert little endian */
  38. static inline spx_int32_t le_int(spx_int32_t i)
  39. {
  40. #if !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) )
  41. spx_uint32_t ui, ret;
  42. ui = i;
  43. ret = ui>>24;
  44. ret |= (ui>>8)&0x0000ff00;
  45. ret |= (ui<<8)&0x00ff0000;
  46. ret |= (ui<<24);
  47. return ret;
  48. #else
  49. return i;
  50. #endif
  51. }
  52. #define ENDIAN_SWITCH(x) {x=le_int(x);}
  53. /*
  54. typedef struct SpeexHeader {
  55. char speex_string[8];
  56. char speex_version[SPEEX_HEADER_VERSION_LENGTH];
  57. int speex_version_id;
  58. int header_size;
  59. int rate;
  60. int mode;
  61. int mode_bitstream_version;
  62. int nb_channels;
  63. int bitrate;
  64. int frame_size;
  65. int vbr;
  66. int frames_per_packet;
  67. int extra_headers;
  68. int reserved1;
  69. int reserved2;
  70. } SpeexHeader;
  71. */
  72. EXPORT void speex_init_header(SpeexHeader *header, int rate, int nb_channels, const SpeexMode *m)
  73. {
  74. int i;
  75. const char *h="Speex ";
  76. /*
  77. strncpy(header->speex_string, "Speex ", 8);
  78. strncpy(header->speex_version, SPEEX_VERSION, SPEEX_HEADER_VERSION_LENGTH-1);
  79. header->speex_version[SPEEX_HEADER_VERSION_LENGTH-1]=0;
  80. */
  81. for (i=0;i<8;i++)
  82. header->speex_string[i]=h[i];
  83. for (i=0;i<SPEEX_HEADER_VERSION_LENGTH-1 && SPEEX_VERSION[i];i++)
  84. header->speex_version[i]=SPEEX_VERSION[i];
  85. for (;i<SPEEX_HEADER_VERSION_LENGTH;i++)
  86. header->speex_version[i]=0;
  87. header->speex_version_id = 1;
  88. header->header_size = sizeof(SpeexHeader);
  89. header->rate = rate;
  90. header->mode = m->modeID;
  91. header->mode_bitstream_version = m->bitstream_version;
  92. if (m->modeID<0)
  93. speex_warning("This mode is meant to be used alone");
  94. header->nb_channels = nb_channels;
  95. header->bitrate = -1;
  96. speex_mode_query(m, SPEEX_MODE_FRAME_SIZE, &header->frame_size);
  97. header->vbr = 0;
  98. header->frames_per_packet = 0;
  99. header->extra_headers = 0;
  100. header->reserved1 = 0;
  101. header->reserved2 = 0;
  102. }
  103. EXPORT char *speex_header_to_packet(SpeexHeader *header, int *size)
  104. {
  105. SpeexHeader *le_header;
  106. le_header = (SpeexHeader*)speex_alloc(sizeof(SpeexHeader));
  107. SPEEX_COPY(le_header, header, 1);
  108. /*Make sure everything is now little-endian*/
  109. ENDIAN_SWITCH(le_header->speex_version_id);
  110. ENDIAN_SWITCH(le_header->header_size);
  111. ENDIAN_SWITCH(le_header->rate);
  112. ENDIAN_SWITCH(le_header->mode);
  113. ENDIAN_SWITCH(le_header->mode_bitstream_version);
  114. ENDIAN_SWITCH(le_header->nb_channels);
  115. ENDIAN_SWITCH(le_header->bitrate);
  116. ENDIAN_SWITCH(le_header->frame_size);
  117. ENDIAN_SWITCH(le_header->vbr);
  118. ENDIAN_SWITCH(le_header->frames_per_packet);
  119. ENDIAN_SWITCH(le_header->extra_headers);
  120. *size = sizeof(SpeexHeader);
  121. return (char *)le_header;
  122. }
  123. EXPORT SpeexHeader *speex_packet_to_header(char *packet, int size)
  124. {
  125. int i;
  126. SpeexHeader *le_header;
  127. const char *h = "Speex ";
  128. for (i=0;i<8;i++)
  129. if (packet[i]!=h[i])
  130. {
  131. speex_notify("This doesn't look like a Speex file");
  132. return NULL;
  133. }
  134. /*FIXME: Do we allow larger headers?*/
  135. if (size < (int)sizeof(SpeexHeader))
  136. {
  137. speex_notify("Speex header too small");
  138. return NULL;
  139. }
  140. le_header = (SpeexHeader*)speex_alloc(sizeof(SpeexHeader));
  141. SPEEX_COPY(le_header, (SpeexHeader*)packet, 1);
  142. /*Make sure everything is converted correctly from little-endian*/
  143. ENDIAN_SWITCH(le_header->speex_version_id);
  144. ENDIAN_SWITCH(le_header->header_size);
  145. ENDIAN_SWITCH(le_header->rate);
  146. ENDIAN_SWITCH(le_header->mode);
  147. ENDIAN_SWITCH(le_header->mode_bitstream_version);
  148. ENDIAN_SWITCH(le_header->nb_channels);
  149. ENDIAN_SWITCH(le_header->bitrate);
  150. ENDIAN_SWITCH(le_header->frame_size);
  151. ENDIAN_SWITCH(le_header->vbr);
  152. ENDIAN_SWITCH(le_header->frames_per_packet);
  153. ENDIAN_SWITCH(le_header->extra_headers);
  154. if (le_header->mode >= SPEEX_NB_MODES || le_header->mode < 0)
  155. {
  156. speex_notify("Invalid mode specified in Speex header");
  157. speex_free (le_header);
  158. return NULL;
  159. }
  160. if (le_header->nb_channels>2)
  161. le_header->nb_channels = 2;
  162. if (le_header->nb_channels<1)
  163. le_header->nb_channels = 1;
  164. return le_header;
  165. }
  166. EXPORT void speex_header_free(void *ptr)
  167. {
  168. speex_free(ptr);
  169. }