| 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-2020 * |
| 9 | * by the Xiph.Org Foundation and contributors https://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 total duration of all prior links. |
| 140 | This is always zero for non-seekable sources.*/ |
| 141 | ogg_int64_t pcm_file_offset; |
| 142 | /*The granule position of the last sample. |
| 143 | This is only valid for seekable sources.*/ |
| 144 | ogg_int64_t pcm_end; |
| 145 | /*The granule position before the first sample.*/ |
| 146 | ogg_int64_t pcm_start; |
| 147 | /*The serial number.*/ |
| 148 | ogg_uint32_t serialno; |
| 149 | /*The contents of the info header.*/ |
| 150 | OpusHead head; |
| 151 | /*The contents of the comment header.*/ |
| 152 | OpusTags tags; |
| 153 | }; |
| 154 | |
| 155 | struct OggOpusFile{ |
| 156 | /*The callbacks used to access the stream.*/ |
| 157 | OpusFileCallbacks callbacks; |
| 158 | /*A FILE *, memory buffer, etc.*/ |
| 159 | void *stream; |
| 160 | /*Whether or not we can seek with this stream.*/ |
| 161 | int seekable; |
| 162 | /*The number of links in this chained Ogg Opus file.*/ |
| 163 | int nlinks; |
| 164 | /*The cached information from each link in a chained Ogg Opus file. |
| 165 | If stream isn't seekable (e.g., it's a pipe), only the current link |
| 166 | appears.*/ |
| 167 | OggOpusLink *links; |
| 168 | /*The number of serial numbers from a single link.*/ |
| 169 | int nserialnos; |
| 170 | /*The capacity of the list of serial numbers from a single link.*/ |
| 171 | int cserialnos; |
| 172 | /*Storage for the list of serial numbers from a single link. |
| 173 | This is a scratch buffer used when scanning the BOS pages at the start of |
| 174 | each link.*/ |
| 175 | ogg_uint32_t *serialnos; |
| 176 | /*This is the current offset of the data processed by the ogg_sync_state. |
| 177 | After a seek, this should be set to the target offset so that we can track |
| 178 | the byte offsets of subsequent pages. |
| 179 | After a call to op_get_next_page(), this will point to the first byte after |
| 180 | that page.*/ |
| 181 | opus_int64 offset; |
| 182 | /*The total size of this stream, or -1 if it's unseekable.*/ |
| 183 | opus_int64 end; |
| 184 | /*Used to locate pages in the stream.*/ |
| 185 | ogg_sync_state oy; |
| 186 | /*One of OP_NOTOPEN, OP_PARTOPEN, OP_OPENED, OP_STREAMSET, OP_INITSET.*/ |
| 187 | int ready_state; |
| 188 | /*The current link being played back.*/ |
| 189 | int cur_link; |
| 190 | /*The number of decoded samples to discard from the start of decoding.*/ |
| 191 | opus_int32 cur_discard_count; |
| 192 | /*The granule position of the previous packet (current packet start time).*/ |
| 193 | ogg_int64_t prev_packet_gp; |
| 194 | /*The stream offset of the most recent page with completed packets, or -1. |
| 195 | This is only needed to recover continued packet data in the seeking logic, |
| 196 | when we use the current position as one of our bounds, only to later |
| 197 | discover it was the correct starting point.*/ |
| 198 | opus_int64 prev_page_offset; |
| 199 | /*The number of bytes read since the last bitrate query, including framing.*/ |
| 200 | opus_int64 bytes_tracked; |
| 201 | /*The number of samples decoded since the last bitrate query.*/ |
| 202 | ogg_int64_t samples_tracked; |
| 203 | /*Takes physical pages and welds them into a logical stream of packets.*/ |
| 204 | ogg_stream_state os; |
| 205 | /*Re-timestamped packets from a single page. |
| 206 | Buffering these relies on the undocumented libogg behavior that ogg_packet |
| 207 | pointers remain valid until the next page is submitted to the |
| 208 | ogg_stream_state they came from.*/ |
| 209 | ogg_packet op[255]; |
| 210 | /*The index of the next packet to return.*/ |
| 211 | int op_pos; |
| 212 | /*The total number of packets available.*/ |
| 213 | int op_count; |
| 214 | /*Central working state for the packet-to-PCM decoder.*/ |
| 215 | OpusMSDecoder *od; |
| 216 | /*The application-provided packet decode callback.*/ |
| 217 | op_decode_cb_func decode_cb; |
| 218 | /*The application-provided packet decode callback context.*/ |
| 219 | void *decode_cb_ctx; |
| 220 | /*The stream count used to initialize the decoder.*/ |
| 221 | int od_stream_count; |
| 222 | /*The coupled stream count used to initialize the decoder.*/ |
| 223 | int od_coupled_count; |
| 224 | /*The channel count used to initialize the decoder.*/ |
| 225 | int od_channel_count; |
| 226 | /*The channel mapping used to initialize the decoder.*/ |
| 227 | unsigned char od_mapping[OP_NCHANNELS_MAX]; |
| 228 | /*The buffered data for one decoded packet.*/ |
| 229 | op_sample *od_buffer; |
| 230 | /*The current position in the decoded buffer.*/ |
| 231 | int od_buffer_pos; |
| 232 | /*The number of valid samples in the decoded buffer.*/ |
| 233 | int od_buffer_size; |
| 234 | /*The type of gain offset to apply. |
| 235 | One of OP_HEADER_GAIN, OP_ALBUM_GAIN, OP_TRACK_GAIN, or OP_ABSOLUTE_GAIN.*/ |
| 236 | int gain_type; |
| 237 | /*The offset to apply to the gain.*/ |
| 238 | opus_int32 gain_offset_q8; |
| 239 | /*Internal state for soft clipping and dithering float->short output.*/ |
| 240 | #if !defined(OP_FIXED_POINT) |
| 241 | # if defined(OP_SOFT_CLIP) |
| 242 | float clip_state[OP_NCHANNELS_MAX]; |
| 243 | # endif |
| 244 | float dither_a[OP_NCHANNELS_MAX*4]; |
| 245 | float dither_b[OP_NCHANNELS_MAX*4]; |
| 246 | opus_uint32 dither_seed; |
| 247 | int dither_mute; |
| 248 | int dither_disabled; |
| 249 | /*The number of channels represented by the internal state. |
| 250 | This gets set to 0 whenever anything that would prevent state propagation |
| 251 | occurs (switching between the float/short APIs, or between the |
| 252 | stereo/multistream APIs).*/ |
| 253 | int state_channel_count; |
| 254 | #endif |
| 255 | }; |
| 256 | |
| 257 | int op_strncasecmp(const char *_a,const char *_b,int _n); |
| 258 | |
| 259 | #endif |