internal.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 2012 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************/
  12. #if !defined(_opusfile_internal_h)
  13. # define _opusfile_internal_h (1)
  14. # if !defined(_REENTRANT)
  15. # define _REENTRANT
  16. # endif
  17. # if !defined(_GNU_SOURCE)
  18. # define _GNU_SOURCE
  19. # endif
  20. # if !defined(_LARGEFILE_SOURCE)
  21. # define _LARGEFILE_SOURCE
  22. # endif
  23. # if !defined(_LARGEFILE64_SOURCE)
  24. # define _LARGEFILE64_SOURCE
  25. # endif
  26. # if !defined(_FILE_OFFSET_BITS)
  27. # define _FILE_OFFSET_BITS 64
  28. # endif
  29. # include <stdlib.h>
  30. # include <opusfile.h>
  31. typedef struct OggOpusLink OggOpusLink;
  32. # if defined(OP_FIXED_POINT)
  33. typedef opus_int16 op_sample;
  34. # else
  35. typedef float op_sample;
  36. /*We're using this define to test for libopus 1.1 or later until libopus
  37. provides a better mechanism.*/
  38. # if defined(OPUS_GET_EXPERT_FRAME_DURATION_REQUEST)
  39. /*Enable soft clipping prevention in 16-bit decodes.*/
  40. # define OP_SOFT_CLIP (1)
  41. # endif
  42. # endif
  43. # if OP_GNUC_PREREQ(4,2)
  44. /*Disable excessive warnings about the order of operations.*/
  45. # pragma GCC diagnostic ignored "-Wparentheses"
  46. # elif defined(_MSC_VER)
  47. /*Disable excessive warnings about the order of operations.*/
  48. # pragma warning(disable:4554)
  49. /*Disable warnings about "deprecated" POSIX functions.*/
  50. # pragma warning(disable:4996)
  51. # endif
  52. # if OP_GNUC_PREREQ(3,0)
  53. /*Another alternative is
  54. (__builtin_constant_p(_x)?!!(_x):__builtin_expect(!!(_x),1))
  55. but that evaluates _x multiple times, which may be bad.*/
  56. # define OP_LIKELY(_x) (__builtin_expect(!!(_x),1))
  57. # define OP_UNLIKELY(_x) (__builtin_expect(!!(_x),0))
  58. # else
  59. # define OP_LIKELY(_x) (!!(_x))
  60. # define OP_UNLIKELY(_x) (!!(_x))
  61. # endif
  62. # if defined(OP_ENABLE_ASSERTIONS)
  63. # if OP_GNUC_PREREQ(2,5)||__SUNPRO_C>=0x590
  64. __attribute__((noreturn))
  65. # endif
  66. void op_fatal_impl(const char *_str,const char *_file,int _line);
  67. # define OP_FATAL(_str) (op_fatal_impl(_str,__FILE__,__LINE__))
  68. # define OP_ASSERT(_cond) \
  69. do{ \
  70. if(OP_UNLIKELY(!(_cond)))OP_FATAL("assertion failed: " #_cond); \
  71. } \
  72. while(0)
  73. # define OP_ALWAYS_TRUE(_cond) OP_ASSERT(_cond)
  74. # else
  75. # define OP_FATAL(_str) abort()
  76. # define OP_ASSERT(_cond)
  77. # define OP_ALWAYS_TRUE(_cond) ((void)(_cond))
  78. # endif
  79. # define OP_INT64_MAX (2*(((ogg_int64_t)1<<62)-1)|1)
  80. # define OP_INT64_MIN (-OP_INT64_MAX-1)
  81. # define OP_INT32_MAX (2*(((ogg_int32_t)1<<30)-1)|1)
  82. # define OP_INT32_MIN (-OP_INT32_MAX-1)
  83. # define OP_MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
  84. # define OP_MAX(_a,_b) ((_a)>(_b)?(_a):(_b))
  85. # define OP_CLAMP(_lo,_x,_hi) (OP_MAX(_lo,OP_MIN(_x,_hi)))
  86. /*Advance a file offset by the given amount, clamping against OP_INT64_MAX.
  87. This is used to advance a known offset by things like OP_CHUNK_SIZE or
  88. OP_PAGE_SIZE_MAX, while making sure to avoid signed overflow.
  89. It assumes that both _offset and _amount are non-negative.*/
  90. #define OP_ADV_OFFSET(_offset,_amount) \
  91. (OP_MIN(_offset,OP_INT64_MAX-(_amount))+(_amount))
  92. /*The maximum channel count for any mapping we'll actually decode.*/
  93. # define OP_NCHANNELS_MAX (8)
  94. /*Initial state.*/
  95. # define OP_NOTOPEN (0)
  96. /*We've found the first Opus stream in the first link.*/
  97. # define OP_PARTOPEN (1)
  98. # define OP_OPENED (2)
  99. /*We've found the first Opus stream in the current link.*/
  100. # define OP_STREAMSET (3)
  101. /*We've initialized the decoder for the chosen Opus stream in the current
  102. link.*/
  103. # define OP_INITSET (4)
  104. /*Information cached for a single link in a chained Ogg Opus file.
  105. We choose the first Opus stream encountered in each link to play back (and
  106. require at least one).*/
  107. struct OggOpusLink{
  108. /*The byte offset of the first header page in this link.*/
  109. opus_int64 offset;
  110. /*The byte offset of the first data page from the chosen Opus stream in this
  111. link (after the headers).*/
  112. opus_int64 data_offset;
  113. /*The byte offset of the last page from the chosen Opus stream in this link.
  114. This is used when seeking to ensure we find a page before the last one, so
  115. that end-trimming calculations work properly.
  116. This is only valid for seekable sources.*/
  117. opus_int64 end_offset;
  118. /*The granule position of the last sample.
  119. This is only valid for seekable sources.*/
  120. ogg_int64_t pcm_end;
  121. /*The granule position before the first sample.*/
  122. ogg_int64_t pcm_start;
  123. /*The serial number.*/
  124. ogg_uint32_t serialno;
  125. /*The contents of the info header.*/
  126. OpusHead head;
  127. /*The contents of the comment header.*/
  128. OpusTags tags;
  129. };
  130. struct OggOpusFile{
  131. /*The callbacks used to access the data source.*/
  132. OpusFileCallbacks callbacks;
  133. /*A FILE *, memory bufer, etc.*/
  134. void *source;
  135. /*Whether or not we can seek with this data source.*/
  136. int seekable;
  137. /*The number of links in this chained Ogg Opus file.*/
  138. int nlinks;
  139. /*The cached information from each link in a chained Ogg Opus file.
  140. If source isn't seekable (e.g., it's a pipe), only the current link
  141. appears.*/
  142. OggOpusLink *links;
  143. /*The number of serial numbers from a single link.*/
  144. int nserialnos;
  145. /*The capacity of the list of serial numbers from a single link.*/
  146. int cserialnos;
  147. /*Storage for the list of serial numbers from a single link.*/
  148. ogg_uint32_t *serialnos;
  149. /*This is the current offset of the data processed by the ogg_sync_state.
  150. After a seek, this should be set to the target offset so that we can track
  151. the byte offsets of subsequent pages.
  152. After a call to op_get_next_page(), this will point to the first byte after
  153. that page.*/
  154. opus_int64 offset;
  155. /*The total size of this data source, or -1 if it's unseekable.*/
  156. opus_int64 end;
  157. /*Used to locate pages in the data source.*/
  158. ogg_sync_state oy;
  159. /*One of OP_NOTOPEN, OP_PARTOPEN, OP_OPENED, OP_STREAMSET, OP_INITSET.*/
  160. int ready_state;
  161. /*The current link being played back.*/
  162. int cur_link;
  163. /*The number of decoded samples to discard from the start of decoding.*/
  164. opus_int32 cur_discard_count;
  165. /*The granule position of the previous packet (current packet start time).*/
  166. ogg_int64_t prev_packet_gp;
  167. /*The stream offset of the most recent page with completed packets, or -1.
  168. This is only needed to recover continued packet data in the seeking logic,
  169. when we use the current position as one of our bounds, only to later
  170. discover it was the correct starting point.*/
  171. opus_int64 prev_page_offset;
  172. /*The number of bytes read since the last bitrate query, including framing.*/
  173. opus_int64 bytes_tracked;
  174. /*The number of samples decoded since the last bitrate query.*/
  175. ogg_int64_t samples_tracked;
  176. /*Takes physical pages and welds them into a logical stream of packets.*/
  177. ogg_stream_state os;
  178. /*Re-timestamped packets from a single page.
  179. Buffering these relies on the undocumented libogg behavior that ogg_packet
  180. pointers remain valid until the next page is submitted to the
  181. ogg_stream_state they came from.*/
  182. ogg_packet op[255];
  183. /*The index of the next packet to return.*/
  184. int op_pos;
  185. /*The total number of packets available.*/
  186. int op_count;
  187. /*Central working state for the packet-to-PCM decoder.*/
  188. OpusMSDecoder *od;
  189. /*The application-provided packet decode callback.*/
  190. op_decode_cb_func decode_cb;
  191. /*The application-provided packet decode callback context.*/
  192. void *decode_cb_ctx;
  193. /*The stream count used to initialize the decoder.*/
  194. int od_stream_count;
  195. /*The coupled stream count used to initialize the decoder.*/
  196. int od_coupled_count;
  197. /*The channel count used to initialize the decoder.*/
  198. int od_channel_count;
  199. /*The channel mapping used to initialize the decoder.*/
  200. unsigned char od_mapping[OP_NCHANNELS_MAX];
  201. /*The buffered data for one decoded packet.*/
  202. op_sample *od_buffer;
  203. /*The current position in the decoded buffer.*/
  204. int od_buffer_pos;
  205. /*The number of valid samples in the decoded buffer.*/
  206. int od_buffer_size;
  207. /*The type of gain offset to apply.
  208. One of OP_HEADER_GAIN, OP_TRACK_GAIN, or OP_ABSOLUTE_GAIN.*/
  209. int gain_type;
  210. /*The offset to apply to the gain.*/
  211. opus_int32 gain_offset_q8;
  212. /*Internal state for soft clipping and dithering float->short output.*/
  213. #if !defined(OP_FIXED_POINT)
  214. # if defined(OP_SOFT_CLIP)
  215. float clip_state[OP_NCHANNELS_MAX];
  216. # endif
  217. float dither_a[OP_NCHANNELS_MAX*4];
  218. float dither_b[OP_NCHANNELS_MAX*4];
  219. opus_uint32 dither_seed;
  220. int dither_mute;
  221. int dither_disabled;
  222. /*The number of channels represented by the internal state.
  223. This gets set to 0 whenever anything that would prevent state propagation
  224. occurs (switching between the float/short APIs, or between the
  225. stereo/multistream APIs).*/
  226. int state_channel_count;
  227. #endif
  228. };
  229. int op_strncasecmp(const char *_a,const char *_b,int _n);
  230. #endif