stream_decoder.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:85k
源码类别:

Windows CE

开发平台:

C/C++

  1. /* libFLAC - Free Lossless Audio Codec library
  2.  * Copyright (C) 2000,2001,2002,2003,2004,2005  Josh Coalson
  3.  *
  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.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - Neither the name of the Xiph.org Foundation nor the names of its
  16.  * contributors may be used to endorse or promote products derived from
  17.  * this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. #include <stdio.h>
  32. #include <stdlib.h> /* for malloc() */
  33. #include <string.h> /* for memset/memcpy() */
  34. #include "FLAC/assert.h"
  35. #include "protected/stream_decoder.h"
  36. #include "private/bitbuffer.h"
  37. #include "private/bitmath.h"
  38. #include "private/cpu.h"
  39. #include "private/crc.h"
  40. #include "private/fixed.h"
  41. #include "private/format.h"
  42. #include "private/lpc.h"
  43. #include "private/memory.h"
  44. #ifdef HAVE_CONFIG_H
  45. #include <config.h>
  46. #endif
  47. #ifdef max
  48. #undef max
  49. #endif
  50. #define max(a,b) ((a)>(b)?(a):(b))
  51. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  52. #ifdef _MSC_VER
  53. #define FLAC__U64L(x) x
  54. #else
  55. #define FLAC__U64L(x) x##LLU
  56. #endif
  57. /***********************************************************************
  58.  *
  59.  * Private static data
  60.  *
  61.  ***********************************************************************/
  62. static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
  63. /***********************************************************************
  64.  *
  65.  * Private class method prototypes
  66.  *
  67.  ***********************************************************************/
  68. static void set_defaults_(FLAC__StreamDecoder *decoder);
  69. static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
  70. static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
  71. static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
  72. static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
  73. static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
  74. static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
  75. static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
  76. static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
  77. static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
  78. static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
  79. static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
  80. static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
  81. static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
  82. static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
  83. static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
  84. static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
  85. static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
  86. static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
  87. static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
  88. static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
  89. /***********************************************************************
  90.  *
  91.  * Private class data
  92.  *
  93.  ***********************************************************************/
  94. typedef struct FLAC__StreamDecoderPrivate {
  95. FLAC__StreamDecoderReadCallback read_callback;
  96. FLAC__StreamDecoderWriteCallback write_callback;
  97. FLAC__StreamDecoderMetadataCallback metadata_callback;
  98. FLAC__StreamDecoderErrorCallback error_callback;
  99. /* generic 32-bit datapath: */
  100. void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
  101. /* generic 64-bit datapath: */
  102. void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
  103. /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
  104. void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
  105. /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
  106. void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
  107. void *client_data;
  108. FLAC__BitBuffer *input;
  109. FLAC__int32 *output[FLAC__MAX_CHANNELS];
  110. FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
  111. FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
  112. unsigned output_capacity, output_channels;
  113. FLAC__uint32 last_frame_number;
  114. FLAC__uint32 last_block_size;
  115. FLAC__uint64 samples_decoded;
  116. FLAC__bool has_stream_info, has_seek_table;
  117. FLAC__StreamMetadata stream_info;
  118. FLAC__StreamMetadata seek_table;
  119. FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
  120. FLAC__byte *metadata_filter_ids;
  121. unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
  122. FLAC__Frame frame;
  123. FLAC__bool cached; /* true if there is a byte in lookahead */
  124. FLAC__CPUInfo cpuinfo;
  125. FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
  126. FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
  127. /* unaligned (original) pointers to allocated data */
  128. FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
  129. } FLAC__StreamDecoderPrivate;
  130. /***********************************************************************
  131.  *
  132.  * Public static class data
  133.  *
  134.  ***********************************************************************/
  135. FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
  136. "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
  137. "FLAC__STREAM_DECODER_READ_METADATA",
  138. "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
  139. "FLAC__STREAM_DECODER_READ_FRAME",
  140. "FLAC__STREAM_DECODER_END_OF_STREAM",
  141. "FLAC__STREAM_DECODER_ABORTED",
  142. "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
  143. "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
  144. "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
  145. "FLAC__STREAM_DECODER_INVALID_CALLBACK",
  146. "FLAC__STREAM_DECODER_UNINITIALIZED"
  147. };
  148. FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
  149. "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
  150. "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
  151. "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
  152. };
  153. FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
  154. "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
  155. "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
  156. };
  157. FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
  158. "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
  159. "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
  160. "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
  161. };
  162. /***********************************************************************
  163.  *
  164.  * Class constructor/destructor
  165.  *
  166.  ***********************************************************************/
  167. FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
  168. {
  169. FLAC__StreamDecoder *decoder;
  170. unsigned i;
  171. FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
  172. decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
  173. if(decoder == 0) {
  174. return 0;
  175. }
  176. decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
  177. if(decoder->protected_ == 0) {
  178. free(decoder);
  179. return 0;
  180. }
  181. decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
  182. if(decoder->private_ == 0) {
  183. free(decoder->protected_);
  184. free(decoder);
  185. return 0;
  186. }
  187. decoder->private_->input = FLAC__bitbuffer_new();
  188. if(decoder->private_->input == 0) {
  189. free(decoder->private_);
  190. free(decoder->protected_);
  191. free(decoder);
  192. return 0;
  193. }
  194. decoder->private_->metadata_filter_ids_capacity = 16;
  195. if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
  196. FLAC__bitbuffer_delete(decoder->private_->input);
  197. free(decoder->private_);
  198. free(decoder->protected_);
  199. free(decoder);
  200. return 0;
  201. }
  202. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  203. decoder->private_->output[i] = 0;
  204. decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
  205. }
  206. decoder->private_->output_capacity = 0;
  207. decoder->private_->output_channels = 0;
  208. decoder->private_->has_seek_table = false;
  209. for(i = 0; i < FLAC__MAX_CHANNELS; i++)
  210. FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
  211. set_defaults_(decoder);
  212. decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
  213. return decoder;
  214. }
  215. FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
  216. {
  217. unsigned i;
  218. FLAC__ASSERT(0 != decoder);
  219. FLAC__ASSERT(0 != decoder->protected_);
  220. FLAC__ASSERT(0 != decoder->private_);
  221. FLAC__ASSERT(0 != decoder->private_->input);
  222. FLAC__stream_decoder_finish(decoder);
  223. if(0 != decoder->private_->metadata_filter_ids)
  224. free(decoder->private_->metadata_filter_ids);
  225. FLAC__bitbuffer_delete(decoder->private_->input);
  226. for(i = 0; i < FLAC__MAX_CHANNELS; i++)
  227. FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
  228. free(decoder->private_);
  229. free(decoder->protected_);
  230. free(decoder);
  231. }
  232. /***********************************************************************
  233.  *
  234.  * Public class methods
  235.  *
  236.  ***********************************************************************/
  237. FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
  238. {
  239. FLAC__ASSERT(0 != decoder);
  240. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  241. return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
  242. if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
  243. return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
  244. if(!FLAC__bitbuffer_init(decoder->private_->input))
  245. return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  246. decoder->private_->last_frame_number = 0;
  247. decoder->private_->last_block_size = 0;
  248. decoder->private_->samples_decoded = 0;
  249. decoder->private_->has_stream_info = false;
  250. decoder->private_->cached = false;
  251. /*
  252.  * get the CPU info and set the function pointers
  253.  */
  254. FLAC__cpu_info(&decoder->private_->cpuinfo);
  255. /* first default to the non-asm routines */
  256. decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
  257. decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
  258. decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
  259. decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
  260. /* now override with asm where appropriate */
  261. #ifndef FLAC__NO_ASM
  262. if(decoder->private_->cpuinfo.use_asm) {
  263. #ifdef FLAC__CPU_IA32
  264. FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
  265. #ifdef FLAC__HAS_NASM
  266. if(decoder->private_->cpuinfo.data.ia32.mmx) {
  267. decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
  268. decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
  269. decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
  270. }
  271. else {
  272. decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
  273. decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
  274. decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
  275. }
  276. #endif
  277. #elif defined FLAC__CPU_PPC
  278. FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
  279. if(decoder->private_->cpuinfo.data.ppc.altivec) {
  280. decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
  281. decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
  282. }
  283. #endif
  284. }
  285. #endif
  286. if(!FLAC__stream_decoder_reset(decoder))
  287. return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  288. return decoder->protected_->state;
  289. }
  290. FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
  291. {
  292. unsigned i;
  293. FLAC__ASSERT(0 != decoder);
  294. if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
  295. return;
  296. if(0 != decoder->private_->seek_table.data.seek_table.points) {
  297. free(decoder->private_->seek_table.data.seek_table.points);
  298. decoder->private_->seek_table.data.seek_table.points = 0;
  299. decoder->private_->has_seek_table = false;
  300. }
  301. FLAC__bitbuffer_free(decoder->private_->input);
  302. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  303. /* WATCHOUT:
  304.  * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
  305.  * output arrays have a buffer of up to 3 zeroes in front
  306.  * (at negative indices) for alignment purposes; we use 4
  307.  * to keep the data well-aligned.
  308.  */
  309. if(0 != decoder->private_->output[i]) {
  310. free(decoder->private_->output[i]-4);
  311. decoder->private_->output[i] = 0;
  312. }
  313. if(0 != decoder->private_->residual_unaligned[i]) {
  314. free(decoder->private_->residual_unaligned[i]);
  315. decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
  316. }
  317. }
  318. decoder->private_->output_capacity = 0;
  319. decoder->private_->output_channels = 0;
  320. set_defaults_(decoder);
  321. decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
  322. }
  323. FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
  324. {
  325. FLAC__ASSERT(0 != decoder);
  326. FLAC__ASSERT(0 != decoder->private_);
  327. FLAC__ASSERT(0 != decoder->protected_);
  328. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  329. return false;
  330. decoder->private_->read_callback = value;
  331. return true;
  332. }
  333. FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
  334. {
  335. FLAC__ASSERT(0 != decoder);
  336. FLAC__ASSERT(0 != decoder->private_);
  337. FLAC__ASSERT(0 != decoder->protected_);
  338. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  339. return false;
  340. decoder->private_->write_callback = value;
  341. return true;
  342. }
  343. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
  344. {
  345. FLAC__ASSERT(0 != decoder);
  346. FLAC__ASSERT(0 != decoder->private_);
  347. FLAC__ASSERT(0 != decoder->protected_);
  348. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  349. return false;
  350. decoder->private_->metadata_callback = value;
  351. return true;
  352. }
  353. FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
  354. {
  355. FLAC__ASSERT(0 != decoder);
  356. FLAC__ASSERT(0 != decoder->private_);
  357. FLAC__ASSERT(0 != decoder->protected_);
  358. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  359. return false;
  360. decoder->private_->error_callback = value;
  361. return true;
  362. }
  363. FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
  364. {
  365. FLAC__ASSERT(0 != decoder);
  366. FLAC__ASSERT(0 != decoder->private_);
  367. FLAC__ASSERT(0 != decoder->protected_);
  368. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  369. return false;
  370. decoder->private_->client_data = value;
  371. return true;
  372. }
  373. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
  374. {
  375. FLAC__ASSERT(0 != decoder);
  376. FLAC__ASSERT(0 != decoder->private_);
  377. FLAC__ASSERT(0 != decoder->protected_);
  378. FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
  379. /* double protection */
  380. if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
  381. return false;
  382. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  383. return false;
  384. decoder->private_->metadata_filter[type] = true;
  385. if(type == FLAC__METADATA_TYPE_APPLICATION)
  386. decoder->private_->metadata_filter_ids_count = 0;
  387. return true;
  388. }
  389. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
  390. {
  391. FLAC__ASSERT(0 != decoder);
  392. FLAC__ASSERT(0 != decoder->private_);
  393. FLAC__ASSERT(0 != decoder->protected_);
  394. FLAC__ASSERT(0 != id);
  395. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  396. return false;
  397. if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
  398. return true;
  399. FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
  400. if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
  401. if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
  402. return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  403. decoder->private_->metadata_filter_ids_capacity *= 2;
  404. }
  405. memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
  406. decoder->private_->metadata_filter_ids_count++;
  407. return true;
  408. }
  409. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
  410. {
  411. unsigned i;
  412. FLAC__ASSERT(0 != decoder);
  413. FLAC__ASSERT(0 != decoder->private_);
  414. FLAC__ASSERT(0 != decoder->protected_);
  415. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  416. return false;
  417. for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
  418. decoder->private_->metadata_filter[i] = true;
  419. decoder->private_->metadata_filter_ids_count = 0;
  420. return true;
  421. }
  422. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
  423. {
  424. FLAC__ASSERT(0 != decoder);
  425. FLAC__ASSERT(0 != decoder->private_);
  426. FLAC__ASSERT(0 != decoder->protected_);
  427. FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
  428. /* double protection */
  429. if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
  430. return false;
  431. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  432. return false;
  433. decoder->private_->metadata_filter[type] = false;
  434. if(type == FLAC__METADATA_TYPE_APPLICATION)
  435. decoder->private_->metadata_filter_ids_count = 0;
  436. return true;
  437. }
  438. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
  439. {
  440. FLAC__ASSERT(0 != decoder);
  441. FLAC__ASSERT(0 != decoder->private_);
  442. FLAC__ASSERT(0 != decoder->protected_);
  443. FLAC__ASSERT(0 != id);
  444. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  445. return false;
  446. if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
  447. return true;
  448. FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
  449. if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
  450. if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
  451. return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  452. decoder->private_->metadata_filter_ids_capacity *= 2;
  453. }
  454. memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
  455. decoder->private_->metadata_filter_ids_count++;
  456. return true;
  457. }
  458. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
  459. {
  460. FLAC__ASSERT(0 != decoder);
  461. FLAC__ASSERT(0 != decoder->private_);
  462. FLAC__ASSERT(0 != decoder->protected_);
  463. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  464. return false;
  465. memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
  466. decoder->private_->metadata_filter_ids_count = 0;
  467. return true;
  468. }
  469. FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
  470. {
  471. FLAC__ASSERT(0 != decoder);
  472. FLAC__ASSERT(0 != decoder->protected_);
  473. return decoder->protected_->state;
  474. }
  475. FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
  476. {
  477. return FLAC__StreamDecoderStateString[decoder->protected_->state];
  478. }
  479. FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
  480. {
  481. FLAC__ASSERT(0 != decoder);
  482. FLAC__ASSERT(0 != decoder->protected_);
  483. return decoder->protected_->channels;
  484. }
  485. FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
  486. {
  487. FLAC__ASSERT(0 != decoder);
  488. FLAC__ASSERT(0 != decoder->protected_);
  489. return decoder->protected_->channel_assignment;
  490. }
  491. FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
  492. {
  493. FLAC__ASSERT(0 != decoder);
  494. FLAC__ASSERT(0 != decoder->protected_);
  495. return decoder->protected_->bits_per_sample;
  496. }
  497. FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
  498. {
  499. FLAC__ASSERT(0 != decoder);
  500. FLAC__ASSERT(0 != decoder->protected_);
  501. return decoder->protected_->sample_rate;
  502. }
  503. FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
  504. {
  505. FLAC__ASSERT(0 != decoder);
  506. FLAC__ASSERT(0 != decoder->protected_);
  507. return decoder->protected_->blocksize;
  508. }
  509. FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
  510. {
  511. FLAC__ASSERT(0 != decoder);
  512. FLAC__ASSERT(0 != decoder->private_);
  513. FLAC__ASSERT(0 != decoder->protected_);
  514. if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
  515. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  516. return false;
  517. }
  518. decoder->private_->last_frame_number = 0;
  519. decoder->private_->last_block_size = 0;
  520. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  521. return true;
  522. }
  523. FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
  524. {
  525. FLAC__ASSERT(0 != decoder);
  526. FLAC__ASSERT(0 != decoder->private_);
  527. FLAC__ASSERT(0 != decoder->protected_);
  528. if(!FLAC__stream_decoder_flush(decoder)) {
  529. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  530. return false;
  531. }
  532. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
  533. decoder->private_->samples_decoded = 0;
  534. return true;
  535. }
  536. FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
  537. {
  538. FLAC__bool got_a_frame;
  539. FLAC__ASSERT(0 != decoder);
  540. FLAC__ASSERT(0 != decoder->protected_);
  541. while(1) {
  542. switch(decoder->protected_->state) {
  543. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  544. if(!find_metadata_(decoder))
  545. return false; /* above function sets the status for us */
  546. break;
  547. case FLAC__STREAM_DECODER_READ_METADATA:
  548. if(!read_metadata_(decoder))
  549. return false; /* above function sets the status for us */
  550. else
  551. return true;
  552. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  553. if(!frame_sync_(decoder))
  554. return true; /* above function sets the status for us */
  555. break;
  556. case FLAC__STREAM_DECODER_READ_FRAME:
  557. if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
  558. return false; /* above function sets the status for us */
  559. if(got_a_frame)
  560. return true; /* above function sets the status for us */
  561. break;
  562. case FLAC__STREAM_DECODER_END_OF_STREAM:
  563. case FLAC__STREAM_DECODER_ABORTED:
  564. return true;
  565. default:
  566. FLAC__ASSERT(0);
  567. return false;
  568. }
  569. }
  570. }
  571. FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
  572. {
  573. FLAC__ASSERT(0 != decoder);
  574. FLAC__ASSERT(0 != decoder->protected_);
  575. while(1) {
  576. switch(decoder->protected_->state) {
  577. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  578. if(!find_metadata_(decoder))
  579. return false; /* above function sets the status for us */
  580. break;
  581. case FLAC__STREAM_DECODER_READ_METADATA:
  582. if(!read_metadata_(decoder))
  583. return false; /* above function sets the status for us */
  584. break;
  585. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  586. case FLAC__STREAM_DECODER_READ_FRAME:
  587. case FLAC__STREAM_DECODER_END_OF_STREAM:
  588. case FLAC__STREAM_DECODER_ABORTED:
  589. return true;
  590. default:
  591. FLAC__ASSERT(0);
  592. return false;
  593. }
  594. }
  595. }
  596. FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
  597. {
  598. FLAC__bool dummy;
  599. FLAC__ASSERT(0 != decoder);
  600. FLAC__ASSERT(0 != decoder->protected_);
  601. while(1) {
  602. switch(decoder->protected_->state) {
  603. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  604. if(!find_metadata_(decoder))
  605. return false; /* above function sets the status for us */
  606. break;
  607. case FLAC__STREAM_DECODER_READ_METADATA:
  608. if(!read_metadata_(decoder))
  609. return false; /* above function sets the status for us */
  610. break;
  611. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  612. if(!frame_sync_(decoder))
  613. return true; /* above function sets the status for us */
  614. break;
  615. case FLAC__STREAM_DECODER_READ_FRAME:
  616. if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
  617. return false; /* above function sets the status for us */
  618. break;
  619. case FLAC__STREAM_DECODER_END_OF_STREAM:
  620. case FLAC__STREAM_DECODER_ABORTED:
  621. return true;
  622. default:
  623. FLAC__ASSERT(0);
  624. return false;
  625. }
  626. }
  627. }
  628. FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
  629. {
  630. FLAC__bool got_a_frame;
  631. FLAC__ASSERT(0 != decoder);
  632. FLAC__ASSERT(0 != decoder->protected_);
  633. while(1) {
  634. switch(decoder->protected_->state) {
  635. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  636. case FLAC__STREAM_DECODER_READ_METADATA:
  637. return false; /* above function sets the status for us */
  638. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  639. if(!frame_sync_(decoder))
  640. return true; /* above function sets the status for us */
  641. break;
  642. case FLAC__STREAM_DECODER_READ_FRAME:
  643. if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
  644. return false; /* above function sets the status for us */
  645. if(got_a_frame)
  646. return true; /* above function sets the status for us */
  647. break;
  648. case FLAC__STREAM_DECODER_END_OF_STREAM:
  649. case FLAC__STREAM_DECODER_ABORTED:
  650. return true;
  651. default:
  652. FLAC__ASSERT(0);
  653. return false;
  654. }
  655. }
  656. }
  657. /***********************************************************************
  658.  *
  659.  * Protected class methods
  660.  *
  661.  ***********************************************************************/
  662. unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
  663. {
  664. FLAC__ASSERT(0 != decoder);
  665. return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
  666. }
  667. /***********************************************************************
  668.  *
  669.  * Private class methods
  670.  *
  671.  ***********************************************************************/
  672. void set_defaults_(FLAC__StreamDecoder *decoder)
  673. {
  674. decoder->private_->read_callback = 0;
  675. decoder->private_->write_callback = 0;
  676. decoder->private_->metadata_callback = 0;
  677. decoder->private_->error_callback = 0;
  678. decoder->private_->client_data = 0;
  679. memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
  680. decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
  681. decoder->private_->metadata_filter_ids_count = 0;
  682. }
  683. FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
  684. {
  685. unsigned i;
  686. FLAC__int32 *tmp;
  687. if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
  688. return true;
  689. /* simply using realloc() is not practical because the number of channels may change mid-stream */
  690. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  691. if(0 != decoder->private_->output[i]) {
  692. free(decoder->private_->output[i]-4);
  693. decoder->private_->output[i] = 0;
  694. }
  695. if(0 != decoder->private_->residual_unaligned[i]) {
  696. free(decoder->private_->residual_unaligned[i]);
  697. decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
  698. }
  699. }
  700. for(i = 0; i < channels; i++) {
  701. /* WATCHOUT:
  702.  * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
  703.  * output arrays have a buffer of up to 3 zeroes in front
  704.  * (at negative indices) for alignment purposes; we use 4
  705.  * to keep the data well-aligned.
  706.  */
  707. tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
  708. if(tmp == 0) {
  709. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  710. return false;
  711. }
  712. memset(tmp, 0, sizeof(FLAC__int32)*4);
  713. decoder->private_->output[i] = tmp + 4;
  714. /* WATCHOUT:
  715.  * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
  716.  */
  717. if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
  718. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  719. return false;
  720. }
  721. }
  722. decoder->private_->output_capacity = size;
  723. decoder->private_->output_channels = channels;
  724. return true;
  725. }
  726. FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
  727. {
  728. unsigned i;
  729. FLAC__ASSERT(0 != decoder);
  730. FLAC__ASSERT(0 != decoder->private_);
  731. for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
  732. if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
  733. return true;
  734. return false;
  735. }
  736. FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
  737. {
  738. FLAC__uint32 x;
  739. unsigned i, id;
  740. FLAC__bool first = true;
  741. FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
  742. for(i = id = 0; i < 4; ) {
  743. if(decoder->private_->cached) {
  744. x = (FLAC__uint32)decoder->private_->lookahead;
  745. decoder->private_->cached = false;
  746. }
  747. else {
  748. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  749. return false; /* the read_callback_ sets the state for us */
  750. }
  751. if(x == FLAC__STREAM_SYNC_STRING[i]) {
  752. first = true;
  753. i++;
  754. id = 0;
  755. continue;
  756. }
  757. if(x == ID3V2_TAG_[id]) {
  758. id++;
  759. i = 0;
  760. if(id == 3) {
  761. if(!skip_id3v2_tag_(decoder))
  762. return false; /* the read_callback_ sets the state for us */
  763. }
  764. continue;
  765. }
  766. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  767. decoder->private_->header_warmup[0] = (FLAC__byte)x;
  768. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  769. return false; /* the read_callback_ sets the state for us */
  770. /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
  771. /* else we have to check if the second byte is the end of a sync code */
  772. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  773. decoder->private_->lookahead = (FLAC__byte)x;
  774. decoder->private_->cached = true;
  775. }
  776. else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
  777. decoder->private_->header_warmup[1] = (FLAC__byte)x;
  778. decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
  779. return true;
  780. }
  781. }
  782. i = 0;
  783. if(first) {
  784. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
  785. first = false;
  786. }
  787. }
  788. decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
  789. return true;
  790. }
  791. FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
  792. {
  793. FLAC__bool is_last;
  794. FLAC__uint32 i, x, type, length;
  795. FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
  796. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
  797. return false; /* the read_callback_ sets the state for us */
  798. is_last = x? true : false;
  799. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
  800. return false; /* the read_callback_ sets the state for us */
  801. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
  802. return false; /* the read_callback_ sets the state for us */
  803. if(type == FLAC__METADATA_TYPE_STREAMINFO) {
  804. if(!read_metadata_streaminfo_(decoder, is_last, length))
  805. return false;
  806. decoder->private_->has_stream_info = true;
  807. if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
  808. decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
  809. }
  810. else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
  811. if(!read_metadata_seektable_(decoder, is_last, length))
  812. return false;
  813. decoder->private_->has_seek_table = true;
  814. if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
  815. decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
  816. }
  817. else {
  818. FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
  819. unsigned real_length = length;
  820. FLAC__StreamMetadata block;
  821. block.is_last = is_last;
  822. block.type = (FLAC__MetadataType)type;
  823. block.length = length;
  824. if(type == FLAC__METADATA_TYPE_APPLICATION) {
  825. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
  826. return false; /* the read_callback_ sets the state for us */
  827. real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
  828. if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
  829. skip_it = !skip_it;
  830. }
  831. if(skip_it) {
  832. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
  833. return false; /* the read_callback_ sets the state for us */
  834. }
  835. else {
  836. switch(type) {
  837. case FLAC__METADATA_TYPE_PADDING:
  838. /* skip the padding bytes */
  839. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
  840. return false; /* the read_callback_ sets the state for us */
  841. break;
  842. case FLAC__METADATA_TYPE_APPLICATION:
  843. /* remember, we read the ID already */
  844. if(real_length > 0) {
  845. if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
  846. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  847. return false;
  848. }
  849. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
  850. return false; /* the read_callback_ sets the state for us */
  851. }
  852. else
  853. block.data.application.data = 0;
  854. break;
  855. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  856. if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
  857. return false;
  858. break;
  859. case FLAC__METADATA_TYPE_CUESHEET:
  860. if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
  861. return false;
  862. break;
  863. case FLAC__METADATA_TYPE_STREAMINFO:
  864. case FLAC__METADATA_TYPE_SEEKTABLE:
  865. FLAC__ASSERT(0);
  866. break;
  867. default:
  868. if(real_length > 0) {
  869. if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
  870. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  871. return false;
  872. }
  873. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
  874. return false; /* the read_callback_ sets the state for us */
  875. }
  876. else
  877. block.data.unknown.data = 0;
  878. break;
  879. }
  880. decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
  881. /* now we have to free any malloc'ed data in the block */
  882. switch(type) {
  883. case FLAC__METADATA_TYPE_PADDING:
  884. break;
  885. case FLAC__METADATA_TYPE_APPLICATION:
  886. if(0 != block.data.application.data)
  887. free(block.data.application.data);
  888. break;
  889. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  890. if(0 != block.data.vorbis_comment.vendor_string.entry)
  891. free(block.data.vorbis_comment.vendor_string.entry);
  892. if(block.data.vorbis_comment.num_comments > 0)
  893. for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
  894. if(0 != block.data.vorbis_comment.comments[i].entry)
  895. free(block.data.vorbis_comment.comments[i].entry);
  896. if(0 != block.data.vorbis_comment.comments)
  897. free(block.data.vorbis_comment.comments);
  898. break;
  899. case FLAC__METADATA_TYPE_CUESHEET:
  900. if(block.data.cue_sheet.num_tracks > 0)
  901. for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
  902. if(0 != block.data.cue_sheet.tracks[i].indices)
  903. free(block.data.cue_sheet.tracks[i].indices);
  904. if(0 != block.data.cue_sheet.tracks)
  905. free(block.data.cue_sheet.tracks);
  906. break;
  907. case FLAC__METADATA_TYPE_STREAMINFO:
  908. case FLAC__METADATA_TYPE_SEEKTABLE:
  909. FLAC__ASSERT(0);
  910. default:
  911. if(0 != block.data.unknown.data)
  912. free(block.data.unknown.data);
  913. break;
  914. }
  915. }
  916. }
  917. if(is_last)
  918. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  919. return true;
  920. }
  921. FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
  922. {
  923. FLAC__uint32 x;
  924. unsigned bits, used_bits = 0;
  925. FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
  926. decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
  927. decoder->private_->stream_info.is_last = is_last;
  928. decoder->private_->stream_info.length = length;
  929. bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
  930. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
  931. return false; /* the read_callback_ sets the state for us */
  932. decoder->private_->stream_info.data.stream_info.min_blocksize = x;
  933. used_bits += bits;
  934. bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
  935. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
  936. return false; /* the read_callback_ sets the state for us */
  937. decoder->private_->stream_info.data.stream_info.max_blocksize = x;
  938. used_bits += bits;
  939. bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
  940. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
  941. return false; /* the read_callback_ sets the state for us */
  942. decoder->private_->stream_info.data.stream_info.min_framesize = x;
  943. used_bits += bits;
  944. bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
  945. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
  946. return false; /* the read_callback_ sets the state for us */
  947. decoder->private_->stream_info.data.stream_info.max_framesize = x;
  948. used_bits += bits;
  949. bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
  950. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
  951. return false; /* the read_callback_ sets the state for us */
  952. decoder->private_->stream_info.data.stream_info.sample_rate = x;
  953. used_bits += bits;
  954. bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
  955. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
  956. return false; /* the read_callback_ sets the state for us */
  957. decoder->private_->stream_info.data.stream_info.channels = x+1;
  958. used_bits += bits;
  959. bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
  960. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
  961. return false; /* the read_callback_ sets the state for us */
  962. decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
  963. used_bits += bits;
  964. bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
  965. if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
  966. return false; /* the read_callback_ sets the state for us */
  967. used_bits += bits;
  968. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
  969. return false; /* the read_callback_ sets the state for us */
  970. used_bits += 16*8;
  971. /* skip the rest of the block */
  972. FLAC__ASSERT(used_bits % 8 == 0);
  973. length -= (used_bits / 8);
  974. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
  975. return false; /* the read_callback_ sets the state for us */
  976. return true;
  977. }
  978. FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
  979. {
  980. FLAC__uint32 i, x;
  981. FLAC__uint64 xx;
  982. FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
  983. decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
  984. decoder->private_->seek_table.is_last = is_last;
  985. decoder->private_->seek_table.length = length;
  986. decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
  987. /* use realloc since we may pass through here several times (e.g. after seeking) */
  988. if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
  989. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  990. return false;
  991. }
  992. for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
  993. if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
  994. return false; /* the read_callback_ sets the state for us */
  995. decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
  996. if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
  997. return false; /* the read_callback_ sets the state for us */
  998. decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
  999. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
  1000. return false; /* the read_callback_ sets the state for us */
  1001. decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
  1002. }
  1003. length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
  1004. /* if there is a partial point left, skip over it */
  1005. if(length > 0) {
  1006. /*@@@ do an error_callback() here?  there's an argument for either way */
  1007. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
  1008. return false; /* the read_callback_ sets the state for us */
  1009. }
  1010. return true;
  1011. }
  1012. FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
  1013. {
  1014. FLAC__uint32 i;
  1015. FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
  1016. /* read vendor string */
  1017. FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
  1018. if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
  1019. return false; /* the read_callback_ sets the state for us */
  1020. if(obj->vendor_string.length > 0) {
  1021. if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
  1022. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1023. return false;
  1024. }
  1025. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
  1026. return false; /* the read_callback_ sets the state for us */
  1027. obj->vendor_string.entry[obj->vendor_string.length] = '';
  1028. }
  1029. else
  1030. obj->vendor_string.entry = 0;
  1031. /* read num comments */
  1032. FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
  1033. if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
  1034. return false; /* the read_callback_ sets the state for us */
  1035. /* read comments */
  1036. if(obj->num_comments > 0) {
  1037. if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
  1038. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1039. return false;
  1040. }
  1041. for(i = 0; i < obj->num_comments; i++) {
  1042. FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
  1043. if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
  1044. return false; /* the read_callback_ sets the state for us */
  1045. if(obj->comments[i].length > 0) {
  1046. if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
  1047. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1048. return false;
  1049. }
  1050. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
  1051. return false; /* the read_callback_ sets the state for us */
  1052. obj->comments[i].entry[obj->comments[i].length] = '';
  1053. }
  1054. else
  1055. obj->comments[i].entry = 0;
  1056. }
  1057. }
  1058. else {
  1059. obj->comments = 0;
  1060. }
  1061. return true;
  1062. }
  1063. FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
  1064. {
  1065. FLAC__uint32 i, j, x;
  1066. FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
  1067. memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
  1068. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
  1069. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder))
  1070. return false; /* the read_callback_ sets the state for us */
  1071. if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
  1072. return false; /* the read_callback_ sets the state for us */
  1073. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
  1074. return false; /* the read_callback_ sets the state for us */
  1075. obj->is_cd = x? true : false;
  1076. if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
  1077. return false; /* the read_callback_ sets the state for us */
  1078. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
  1079. return false; /* the read_callback_ sets the state for us */
  1080. obj->num_tracks = x;
  1081. if(obj->num_tracks > 0) {
  1082. if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
  1083. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1084. return false;
  1085. }
  1086. for(i = 0; i < obj->num_tracks; i++) {
  1087. FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
  1088. if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
  1089. return false; /* the read_callback_ sets the state for us */
  1090. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
  1091. return false; /* the read_callback_ sets the state for us */
  1092. track->number = (FLAC__byte)x;
  1093. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
  1094. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder))
  1095. return false; /* the read_callback_ sets the state for us */
  1096. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
  1097. return false; /* the read_callback_ sets the state for us */
  1098. track->type = x;
  1099. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
  1100. return false; /* the read_callback_ sets the state for us */
  1101. track->pre_emphasis = x;
  1102. if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
  1103. return false; /* the read_callback_ sets the state for us */
  1104. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
  1105. return false; /* the read_callback_ sets the state for us */
  1106. track->num_indices = (FLAC__byte)x;
  1107. if(track->num_indices > 0) {
  1108. if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
  1109. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1110. return false;
  1111. }
  1112. for(j = 0; j < track->num_indices; j++) {
  1113. FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
  1114. if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
  1115. return false; /* the read_callback_ sets the state for us */
  1116. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
  1117. return false; /* the read_callback_ sets the state for us */
  1118. index->number = (FLAC__byte)x;
  1119. if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
  1120. return false; /* the read_callback_ sets the state for us */
  1121. }
  1122. }
  1123. }
  1124. }
  1125. return true;
  1126. }
  1127. FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
  1128. {
  1129. FLAC__uint32 x;
  1130. unsigned i, skip;
  1131. /* skip the version and flags bytes */
  1132. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
  1133. return false; /* the read_callback_ sets the state for us */
  1134. /* get the size (in bytes) to skip */
  1135. skip = 0;
  1136. for(i = 0; i < 4; i++) {
  1137. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  1138. return false; /* the read_callback_ sets the state for us */
  1139. skip <<= 7;
  1140. skip |= (x & 0x7f);
  1141. }
  1142. /* skip the rest of the tag */
  1143. if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
  1144. return false; /* the read_callback_ sets the state for us */
  1145. return true;
  1146. }
  1147. FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
  1148. {
  1149. FLAC__uint32 x;
  1150. FLAC__bool first = true;
  1151. /* If we know the total number of samples in the stream, stop if we've read that many. */
  1152. /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
  1153. if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
  1154. if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
  1155. decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
  1156. return true;
  1157. }
  1158. }
  1159. /* make sure we're byte aligned */
  1160. if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
  1161. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
  1162. return false; /* the read_callback_ sets the state for us */
  1163. }
  1164. while(1) {
  1165. if(decoder->private_->cached) {
  1166. x = (FLAC__uint32)decoder->private_->lookahead;
  1167. decoder->private_->cached = false;
  1168. }
  1169. else {
  1170. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  1171. return false; /* the read_callback_ sets the state for us */
  1172. }
  1173. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  1174. decoder->private_->header_warmup[0] = (FLAC__byte)x;
  1175. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  1176. return false; /* the read_callback_ sets the state for us */
  1177. /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
  1178. /* else we have to check if the second byte is the end of a sync code */
  1179. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  1180. decoder->private_->lookahead = (FLAC__byte)x;
  1181. decoder->private_->cached = true;
  1182. }
  1183. else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
  1184. decoder->private_->header_warmup[1] = (FLAC__byte)x;
  1185. decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
  1186. return true;
  1187. }
  1188. }
  1189. if(first) {
  1190. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
  1191. first = false;
  1192. }
  1193. }
  1194. return true;
  1195. }
  1196. FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
  1197. {
  1198. unsigned channel;
  1199. unsigned i;
  1200. FLAC__int32 mid, side, left, right;
  1201. FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
  1202. FLAC__uint32 x;
  1203. *got_a_frame = false;
  1204. /* init the CRC */
  1205. frame_crc = 0;
  1206. FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
  1207. FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
  1208. FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
  1209. if(!read_frame_header_(decoder))
  1210. return false;
  1211. if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
  1212. return true;
  1213. if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
  1214. return false;
  1215. for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
  1216. /*
  1217.  * first figure the correct bits-per-sample of the subframe
  1218.  */
  1219. unsigned bps = decoder->private_->frame.header.bits_per_sample;
  1220. switch(decoder->private_->frame.header.channel_assignment) {
  1221. case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
  1222. /* no adjustment needed */
  1223. break;
  1224. case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
  1225. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1226. if(channel == 1)
  1227. bps++;
  1228. break;
  1229. case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
  1230. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1231. if(channel == 0)
  1232. bps++;
  1233. break;
  1234. case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
  1235. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1236. if(channel == 1)
  1237. bps++;
  1238. break;
  1239. default:
  1240. FLAC__ASSERT(0);
  1241. }
  1242. /*
  1243.  * now read it
  1244.  */
  1245. if(!read_subframe_(decoder, channel, bps, do_full_decode))
  1246. return false;
  1247. if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
  1248. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1249. return true;
  1250. }
  1251. }
  1252. if(!read_zero_padding_(decoder))
  1253. return false;
  1254. /*
  1255.  * Read the frame CRC-16 from the footer and check
  1256.  */
  1257. frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
  1258. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
  1259. return false; /* the read_callback_ sets the state for us */
  1260. if(frame_crc == (FLAC__uint16)x) {
  1261. if(do_full_decode) {
  1262. /* Undo any special channel coding */
  1263. switch(decoder->private_->frame.header.channel_assignment) {
  1264. case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
  1265. /* do nothing */
  1266. break;
  1267. case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
  1268. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1269. for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
  1270. decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
  1271. break;
  1272. case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
  1273. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1274. for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
  1275. decoder->private_->output[0][i] += decoder->private_->output[1][i];
  1276. break;
  1277. case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
  1278. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1279. for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
  1280. mid = decoder->private_->output[0][i];
  1281. side = decoder->private_->output[1][i];
  1282. mid <<= 1;
  1283. if(side & 1) /* i.e. if 'side' is odd... */
  1284. mid++;
  1285. left = mid + side;
  1286. right = mid - side;
  1287. decoder->private_->output[0][i] = left >> 1;
  1288. decoder->private_->output[1][i] = right >> 1;
  1289. }
  1290. break;
  1291. default:
  1292. FLAC__ASSERT(0);
  1293. break;
  1294. }
  1295. }
  1296. }
  1297. else {
  1298. /* Bad frame, emit error and zero the output signal */
  1299. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
  1300. if(do_full_decode) {
  1301. for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
  1302. memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
  1303. }
  1304. }
  1305. }
  1306. *got_a_frame = true;
  1307. /* put the latest values into the public section of the decoder instance */
  1308. decoder->protected_->channels = decoder->private_->frame.header.channels;
  1309. decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
  1310. decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
  1311. decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
  1312. decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
  1313. FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
  1314. decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
  1315. /* write it */
  1316. if(do_full_decode) {
  1317. if(decoder->private_->write_callback(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
  1318. return false;
  1319. }
  1320. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1321. return true;
  1322. }
  1323. FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
  1324. {
  1325. FLAC__uint32 x;
  1326. FLAC__uint64 xx;
  1327. unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
  1328. FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
  1329. unsigned raw_header_len;
  1330. FLAC__bool is_unparseable = false;
  1331. const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize);
  1332. const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
  1333. FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
  1334. /* init the raw header with the saved bits from synchronization */
  1335. raw_header[0] = decoder->private_->header_warmup[0];
  1336. raw_header[1] = decoder->private_->header_warmup[1];
  1337. raw_header_len = 2;
  1338. /*
  1339.  * check to make sure that the reserved bits are 0
  1340.  */
  1341. if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
  1342. is_unparseable = true;
  1343. }
  1344. /*
  1345.  * Note that along the way as we read the header, we look for a sync
  1346.  * code inside.  If we find one it would indicate that our original
  1347.  * sync was bad since there cannot be a sync code in a valid header.
  1348.  */
  1349. /*
  1350.  * read in the raw header as bytes so we can CRC it, and parse it on the way
  1351.  */
  1352. for(i = 0; i < 2; i++) {
  1353. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  1354. return false; /* the read_callback_ sets the state for us */
  1355. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  1356. /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
  1357. decoder->private_->lookahead = (FLAC__byte)x;
  1358. decoder->private_->cached = true;
  1359. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
  1360. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1361. return true;
  1362. }
  1363. raw_header[raw_header_len++] = (FLAC__byte)x;
  1364. }
  1365. switch(x = raw_header[2] >> 4) {
  1366. case 0:
  1367. if(is_known_fixed_blocksize_stream)
  1368. decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
  1369. else
  1370. is_unparseable = true;
  1371. break;
  1372. case 1:
  1373. decoder->private_->frame.header.blocksize = 192;
  1374. break;
  1375. case 2:
  1376. case 3:
  1377. case 4:
  1378. case 5:
  1379. decoder->private_->frame.header.blocksize = 576 << (x-2);
  1380. break;
  1381. case 6:
  1382. case 7:
  1383. blocksize_hint = x;
  1384. break;
  1385. case 8:
  1386. case 9:
  1387. case 10:
  1388. case 11:
  1389. case 12:
  1390. case 13:
  1391. case 14:
  1392. case 15:
  1393. decoder->private_->frame.header.blocksize = 256 << (x-8);
  1394. break;
  1395. default:
  1396. FLAC__ASSERT(0);
  1397. break;
  1398. }
  1399. switch(x = raw_header[2] & 0x0f) {
  1400. case 0:
  1401. if(decoder->private_->has_stream_info)
  1402. decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
  1403. else
  1404. is_unparseable = true;
  1405. break;
  1406. case 1:
  1407. case 2:
  1408. case 3:
  1409. is_unparseable = true;
  1410. break;
  1411. case 4:
  1412. decoder->private_->frame.header.sample_rate = 8000;
  1413. break;
  1414. case 5:
  1415. decoder->private_->frame.header.sample_rate = 16000;
  1416. break;
  1417. case 6:
  1418. decoder->private_->frame.header.sample_rate = 22050;
  1419. break;
  1420. case 7:
  1421. decoder->private_->frame.header.sample_rate = 24000;
  1422. break;
  1423. case 8:
  1424. decoder->private_->frame.header.sample_rate = 32000;
  1425. break;
  1426. case 9:
  1427. decoder->private_->frame.header.sample_rate = 44100;
  1428. break;
  1429. case 10:
  1430. decoder->private_->frame.header.sample_rate = 48000;
  1431. break;
  1432. case 11:
  1433. decoder->private_->frame.header.sample_rate = 96000;
  1434. break;
  1435. case 12:
  1436. case 13:
  1437. case 14:
  1438. sample_rate_hint = x;
  1439. break;
  1440. case 15:
  1441. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
  1442. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1443. return true;
  1444. default:
  1445. FLAC__ASSERT(0);
  1446. }
  1447. x = (unsigned)(raw_header[3] >> 4);
  1448. if(x & 8) {
  1449. decoder->private_->frame.header.channels = 2;
  1450. switch(x & 7) {
  1451. case 0:
  1452. decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
  1453. break;
  1454. case 1:
  1455. decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
  1456. break;
  1457. case 2:
  1458. decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
  1459. break;
  1460. default:
  1461. is_unparseable = true;
  1462. break;
  1463. }
  1464. }
  1465. else {
  1466. decoder->private_->frame.header.channels = (unsigned)x + 1;
  1467. decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
  1468. }
  1469. switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
  1470. case 0:
  1471. if(decoder->private_->has_stream_info)
  1472. decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
  1473. else
  1474. is_unparseable = true;
  1475. break;
  1476. case 1:
  1477. decoder->private_->frame.header.bits_per_sample = 8;
  1478. break;
  1479. case 2:
  1480. decoder->private_->frame.header.bits_per_sample = 12;
  1481. break;
  1482. case 4:
  1483. decoder->private_->frame.header.bits_per_sample = 16;
  1484. break;
  1485. case 5:
  1486. decoder->private_->frame.header.bits_per_sample = 20;
  1487. break;
  1488. case 6:
  1489. decoder->private_->frame.header.bits_per_sample = 24;
  1490. break;
  1491. case 3:
  1492. case 7:
  1493. is_unparseable = true;
  1494. break;
  1495. default:
  1496. FLAC__ASSERT(0);
  1497. break;
  1498. }
  1499. if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
  1500. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
  1501. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1502. return true;
  1503. }
  1504. /*
  1505.  * Now we get to the regrettable consequences of not knowing for sure
  1506.  * whether we got a frame number or a sample number.  There are no
  1507.  * encoders that do variable-blocksize encoding so unless we know from
  1508.  * the STREAMINFO that it is variable-blocksize we will assume it is
  1509.  * fixed-blocksize.  The trouble comes when we have no STREAMINFO; again
  1510.  * we will guess that is fixed-blocksize.  Where this can go wrong: 1) a
  1511.  * variable-blocksize stream with no STREAMINFO; 2) a fixed-blocksize
  1512.  * stream that was edited such that one or more frames before or
  1513.  * including this one do not have the same number of samples as the
  1514.  * STREAMINFO's min and max blocksize.
  1515.  */
  1516. if(is_known_variable_blocksize_stream) {
  1517. if(blocksize_hint) {
  1518. if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
  1519. return false; /* the read_callback_ sets the state for us */
  1520. if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
  1521. decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
  1522. decoder->private_->cached = true;
  1523. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
  1524. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1525. return true;
  1526. }
  1527. decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
  1528. decoder->private_->frame.header.number.sample_number = xx;
  1529. }
  1530. else
  1531. is_unparseable = true;
  1532. }
  1533. else {
  1534. if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
  1535. return false; /* the read_callback_ sets the state for us */
  1536. if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
  1537. decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
  1538. decoder->private_->cached = true;
  1539. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
  1540. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1541. return true;
  1542. }
  1543. decoder->private_->last_frame_number = x;
  1544. decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
  1545. if(decoder->private_->has_stream_info) {
  1546. FLAC__ASSERT(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
  1547. decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
  1548. decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
  1549. }
  1550. else if(blocksize_hint) {
  1551. if(decoder->private_->last_block_size)
  1552. decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->last_block_size * (FLAC__uint64)x;
  1553. else
  1554. is_unparseable = true;
  1555. }
  1556. else {
  1557. decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
  1558. decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
  1559. }
  1560. }
  1561. if(blocksize_hint) {
  1562. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  1563. return false; /* the read_callback_ sets the state for us */
  1564. raw_header[raw_header_len++] = (FLAC__byte)x;
  1565. if(blocksize_hint == 7) {
  1566. FLAC__uint32 _x;
  1567. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
  1568. return false; /* the read_callback_ sets the state for us */
  1569. raw_header[raw_header_len++] = (FLAC__byte)_x;
  1570. x = (x << 8) | _x;
  1571. }
  1572. decoder->private_->frame.header.blocksize = x+1;
  1573. }
  1574. if(sample_rate_hint) {
  1575. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  1576. return false; /* the read_callback_ sets the state for us */
  1577. raw_header[raw_header_len++] = (FLAC__byte)x;
  1578. if(sample_rate_hint != 12) {
  1579. FLAC__uint32 _x;
  1580. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
  1581. return false; /* the read_callback_ sets the state for us */
  1582. raw_header[raw_header_len++] = (FLAC__byte)_x;
  1583. x = (x << 8) | _x;
  1584. }
  1585. if(sample_rate_hint == 12)
  1586. decoder->private_->frame.header.sample_rate = x*1000;
  1587. else if(sample_rate_hint == 13)
  1588. decoder->private_->frame.header.sample_rate = x;
  1589. else
  1590. decoder->private_->frame.header.sample_rate = x*10;
  1591. }
  1592. /* read the CRC-8 byte */
  1593. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
  1594. return false; /* the read_callback_ sets the state for us */
  1595. crc8 = (FLAC__byte)x;
  1596. if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
  1597. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
  1598. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1599. return true;
  1600. }
  1601. if(is_unparseable) {
  1602. decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
  1603. return false;
  1604. }
  1605. return true;
  1606. }
  1607. FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
  1608. {
  1609. FLAC__uint32 x;
  1610. FLAC__bool wasted_bits;
  1611. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
  1612. return false; /* the read_callback_ sets the state for us */
  1613. wasted_bits = (x & 1);
  1614. x &= 0xfe;
  1615. if(wasted_bits) {
  1616. unsigned u;
  1617. if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
  1618. return false; /* the read_callback_ sets the state for us */
  1619. decoder->private_->frame.subframes[channel].wasted_bits = u+1;
  1620. bps -= decoder->private_->frame.subframes[channel].wasted_bits;
  1621. }
  1622. else
  1623. decoder->private_->frame.subframes[channel].wasted_bits = 0;
  1624. /*
  1625.  * Lots of magic numbers here
  1626.  */
  1627. if(x & 0x80) {
  1628. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
  1629. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1630. return true;
  1631. }
  1632. else if(x == 0) {
  1633. if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
  1634. return false;
  1635. }
  1636. else if(x == 2) {
  1637. if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
  1638. return false;
  1639. }
  1640. else if(x < 16) {
  1641. decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
  1642. return false;
  1643. }
  1644. else if(x <= 24) {
  1645. if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
  1646. return false;
  1647. }
  1648. else if(x < 64) {
  1649. decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
  1650. return false;
  1651. }
  1652. else {
  1653. if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
  1654. return false;
  1655. }
  1656. if(wasted_bits && do_full_decode) {
  1657. unsigned i;
  1658. x = decoder->private_->frame.subframes[channel].wasted_bits;
  1659. for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
  1660. decoder->private_->output[channel][i] <<= x;
  1661. }
  1662. return true;
  1663. }
  1664. FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
  1665. {
  1666. FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
  1667. FLAC__int32 x;
  1668. unsigned i;
  1669. FLAC__int32 *output = decoder->private_->output[channel];
  1670. decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
  1671. if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
  1672. return false; /* the read_callback_ sets the state for us */
  1673. subframe->value = x;
  1674. /* decode the subframe */
  1675. if(do_full_decode) {
  1676. for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
  1677. output[i] = x;
  1678. }
  1679. return true;
  1680. }
  1681. FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
  1682. {
  1683. FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
  1684. FLAC__int32 i32;
  1685. FLAC__uint32 u32;
  1686. unsigned u;
  1687. decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
  1688. subframe->residual = decoder->private_->residual[channel];
  1689. subframe->order = order;
  1690. /* read warm-up samples */
  1691. for(u = 0; u < order; u++) {
  1692. if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
  1693. return false; /* the read_callback_ sets the state for us */
  1694. subframe->warmup[u] = i32;
  1695. }
  1696. /* read entropy coding method info */
  1697. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
  1698. return false; /* the read_callback_ sets the state for us */
  1699. subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
  1700. switch(subframe->entropy_coding_method.type) {
  1701. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  1702. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
  1703. return false; /* the read_callback_ sets the state for us */
  1704. subframe->entropy_coding_method.data.partitioned_rice.order = u32;
  1705. subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
  1706. break;
  1707. default:
  1708. decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
  1709. return false;
  1710. }
  1711. /* read residual */
  1712. switch(subframe->entropy_coding_method.type) {
  1713. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  1714. if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel]))
  1715. return false;
  1716. break;
  1717. default:
  1718. FLAC__ASSERT(0);
  1719. }
  1720. /* decode the subframe */
  1721. if(do_full_decode) {
  1722. memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
  1723. FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
  1724. }
  1725. return true;
  1726. }
  1727. FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
  1728. {
  1729. FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
  1730. FLAC__int32 i32;
  1731. FLAC__uint32 u32;
  1732. unsigned u;
  1733. decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
  1734. subframe->residual = decoder->private_->residual[channel];
  1735. subframe->order = order;
  1736. /* read warm-up samples */
  1737. for(u = 0; u < order; u++) {
  1738. if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
  1739. return false; /* the read_callback_ sets the state for us */
  1740. subframe->warmup[u] = i32;
  1741. }
  1742. /* read qlp coeff precision */
  1743. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
  1744. return false; /* the read_callback_ sets the state for us */
  1745. if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
  1746. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
  1747. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1748. return true;
  1749. }
  1750. subframe->qlp_coeff_precision = u32+1;
  1751. /* read qlp shift */
  1752. if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
  1753. return false; /* the read_callback_ sets the state for us */
  1754. subframe->quantization_level = i32;
  1755. /* read quantized lp coefficiencts */
  1756. for(u = 0; u < order; u++) {
  1757. if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
  1758. return false; /* the read_callback_ sets the state for us */
  1759. subframe->qlp_coeff[u] = i32;
  1760. }
  1761. /* read entropy coding method info */
  1762. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
  1763. return false; /* the read_callback_ sets the state for us */
  1764. subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
  1765. switch(subframe->entropy_coding_method.type) {
  1766. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  1767. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
  1768. return false; /* the read_callback_ sets the state for us */
  1769. subframe->entropy_coding_method.data.partitioned_rice.order = u32;
  1770. subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
  1771. break;
  1772. default:
  1773. decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
  1774. return false;
  1775. }
  1776. /* read residual */
  1777. switch(subframe->entropy_coding_method.type) {
  1778. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  1779. if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel]))
  1780. return false;
  1781. break;
  1782. default:
  1783. FLAC__ASSERT(0);
  1784. }
  1785. /* decode the subframe */
  1786. if(do_full_decode) {
  1787. memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
  1788. if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
  1789. if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
  1790. if(order <= 8)
  1791. decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
  1792. else
  1793. decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
  1794. }
  1795. else
  1796. decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
  1797. else
  1798. decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
  1799. }
  1800. return true;
  1801. }
  1802. FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
  1803. {
  1804. FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
  1805. FLAC__int32 x, *residual = decoder->private_->residual[channel];
  1806. unsigned i;
  1807. decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
  1808. subframe->data = residual;
  1809. for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
  1810. if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
  1811. return false; /* the read_callback_ sets the state for us */
  1812. residual[i] = x;
  1813. }
  1814. /* decode the subframe */
  1815. if(do_full_decode)
  1816. memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
  1817. return true;
  1818. }
  1819. FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
  1820. {
  1821. FLAC__uint32 rice_parameter;
  1822. int i;
  1823. unsigned partition, sample, u;
  1824. const unsigned partitions = 1u << partition_order;
  1825. const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
  1826. /* sanity checks */
  1827. if(partition_order == 0) {
  1828. if(decoder->private_->frame.header.blocksize < predictor_order) {
  1829. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
  1830. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1831. return true;
  1832. }
  1833. }
  1834. else {
  1835. if(partition_samples < predictor_order) {
  1836. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
  1837. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1838. return true;
  1839. }
  1840. }
  1841. if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
  1842. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1843. return false;
  1844. }
  1845. sample = 0;
  1846. for(partition = 0; partition < partitions; partition++) {
  1847. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
  1848. return false; /* the read_callback_ sets the state for us */
  1849. partitioned_rice_contents->parameters[partition] = rice_parameter;
  1850. if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  1851. #ifdef FLAC__SYMMETRIC_RICE
  1852. for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
  1853. if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
  1854. return false; /* the read_callback_ sets the state for us */
  1855. residual[sample] = i;
  1856. }
  1857. #else
  1858. u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
  1859. if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
  1860. return false; /* the read_callback_ sets the state for us */
  1861. sample += u;
  1862. #endif
  1863. }
  1864. else {
  1865. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
  1866. return false; /* the read_callback_ sets the state for us */
  1867. partitioned_rice_contents->raw_bits[partition] = rice_parameter;
  1868. for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
  1869. if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
  1870. return false; /* the read_callback_ sets the state for us */
  1871. residual[sample] = i;
  1872. }
  1873. }
  1874. }
  1875. return true;
  1876. }
  1877. FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
  1878. {
  1879. if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
  1880. FLAC__uint32 zero = 0;
  1881. if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
  1882. return false; /* the read_callback_ sets the state for us */
  1883. if(zero != 0) {
  1884. decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
  1885. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1886. }
  1887. }
  1888. return true;
  1889. }
  1890. FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
  1891. {
  1892. FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
  1893. FLAC__StreamDecoderReadStatus status;
  1894. status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
  1895. if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
  1896. decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
  1897. else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
  1898. decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
  1899. return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  1900. }