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