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

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 calloc() */
  33. #include <string.h> /* for memcpy()/memcmp() */
  34. #include "FLAC/assert.h"
  35. #include "protected/seekable_stream_decoder.h"
  36. #include "protected/stream_decoder.h"
  37. #include "private/float.h" /* for FLAC__double */
  38. #include "private/md5.h"
  39. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  40. #ifdef _MSC_VER
  41. #define FLAC__U64L(x) x
  42. #else
  43. #define FLAC__U64L(x) x##LLU
  44. #endif
  45. /***********************************************************************
  46.  *
  47.  * Private class method prototypes
  48.  *
  49.  ***********************************************************************/
  50. static void set_defaults_(FLAC__SeekableStreamDecoder *decoder);
  51. static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
  52. static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
  53. static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
  54. static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
  55. static FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
  56. /***********************************************************************
  57.  *
  58.  * Private class data
  59.  *
  60.  ***********************************************************************/
  61. typedef struct FLAC__SeekableStreamDecoderPrivate {
  62. FLAC__SeekableStreamDecoderReadCallback read_callback;
  63. FLAC__SeekableStreamDecoderSeekCallback seek_callback;
  64. FLAC__SeekableStreamDecoderTellCallback tell_callback;
  65. FLAC__SeekableStreamDecoderLengthCallback length_callback;
  66. FLAC__SeekableStreamDecoderEofCallback eof_callback;
  67. FLAC__SeekableStreamDecoderWriteCallback write_callback;
  68. FLAC__SeekableStreamDecoderMetadataCallback metadata_callback;
  69. FLAC__SeekableStreamDecoderErrorCallback error_callback;
  70. void *client_data;
  71. FLAC__StreamDecoder *stream_decoder;
  72. FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek */
  73. struct FLAC__MD5Context md5context;
  74. FLAC__byte stored_md5sum[16]; /* this is what is stored in the metadata */
  75. FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
  76. /* the rest of these are only used for seeking: */
  77. FLAC__StreamMetadata_StreamInfo stream_info; /* we keep this around so we can figure out how to seek quickly */
  78. const FLAC__StreamMetadata_SeekTable *seek_table; /* we hold a pointer to the stream decoder's seek table for the same reason */
  79. /* Since we always want to see the STREAMINFO and SEEK_TABLE blocks at this level, we need some extra flags to keep track of whether they should be passed on up through the metadata_callback */
  80. FLAC__bool ignore_stream_info_block;
  81. FLAC__bool ignore_seek_table_block;
  82. FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
  83. FLAC__uint64 target_sample;
  84. } FLAC__SeekableStreamDecoderPrivate;
  85. /***********************************************************************
  86.  *
  87.  * Public static class data
  88.  *
  89.  ***********************************************************************/
  90. FLAC_API const char * const FLAC__SeekableStreamDecoderStateString[] = {
  91. "FLAC__SEEKABLE_STREAM_DECODER_OK",
  92. "FLAC__SEEKABLE_STREAM_DECODER_SEEKING",
  93. "FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM",
  94. "FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
  95. "FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR",
  96. "FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR",
  97. "FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR",
  98. "FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED",
  99. "FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK",
  100. "FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED"
  101. };
  102. FLAC_API const char * const FLAC__SeekableStreamDecoderReadStatusString[] = {
  103. "FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK",
  104. "FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR"
  105. };
  106. FLAC_API const char * const FLAC__SeekableStreamDecoderSeekStatusString[] = {
  107. "FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK",
  108. "FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR"
  109. };
  110. FLAC_API const char * const FLAC__SeekableStreamDecoderTellStatusString[] = {
  111. "FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK",
  112. "FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR"
  113. };
  114. FLAC_API const char * const FLAC__SeekableStreamDecoderLengthStatusString[] = {
  115. "FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK",
  116. "FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR"
  117. };
  118. /***********************************************************************
  119.  *
  120.  * Class constructor/destructor
  121.  *
  122.  ***********************************************************************/
  123. FLAC_API FLAC__SeekableStreamDecoder *FLAC__seekable_stream_decoder_new()
  124. {
  125. FLAC__SeekableStreamDecoder *decoder;
  126. FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
  127. decoder = (FLAC__SeekableStreamDecoder*)calloc(1, sizeof(FLAC__SeekableStreamDecoder));
  128. if(decoder == 0) {
  129. return 0;
  130. }
  131. decoder->protected_ = (FLAC__SeekableStreamDecoderProtected*)calloc(1, sizeof(FLAC__SeekableStreamDecoderProtected));
  132. if(decoder->protected_ == 0) {
  133. free(decoder);
  134. return 0;
  135. }
  136. decoder->private_ = (FLAC__SeekableStreamDecoderPrivate*)calloc(1, sizeof(FLAC__SeekableStreamDecoderPrivate));
  137. if(decoder->private_ == 0) {
  138. free(decoder->protected_);
  139. free(decoder);
  140. return 0;
  141. }
  142. decoder->private_->stream_decoder = FLAC__stream_decoder_new();
  143. if(0 == decoder->private_->stream_decoder) {
  144. free(decoder->private_);
  145. free(decoder->protected_);
  146. free(decoder);
  147. return 0;
  148. }
  149. set_defaults_(decoder);
  150. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
  151. return decoder;
  152. }
  153. FLAC_API void FLAC__seekable_stream_decoder_delete(FLAC__SeekableStreamDecoder *decoder)
  154. {
  155. FLAC__ASSERT(0 != decoder);
  156. FLAC__ASSERT(0 != decoder->protected_);
  157. FLAC__ASSERT(0 != decoder->private_);
  158. FLAC__ASSERT(0 != decoder->private_->stream_decoder);
  159. (void)FLAC__seekable_stream_decoder_finish(decoder);
  160. FLAC__stream_decoder_delete(decoder->private_->stream_decoder);
  161. free(decoder->private_);
  162. free(decoder->protected_);
  163. free(decoder);
  164. }
  165. /***********************************************************************
  166.  *
  167.  * Public class methods
  168.  *
  169.  ***********************************************************************/
  170. FLAC_API FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_init(FLAC__SeekableStreamDecoder *decoder)
  171. {
  172. FLAC__ASSERT(0 != decoder);
  173. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  174. return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED;
  175. if(0 == decoder->private_->read_callback || 0 == decoder->private_->seek_callback || 0 == decoder->private_->tell_callback || 0 == decoder->private_->length_callback || 0 == decoder->private_->eof_callback)
  176. return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
  177. if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
  178. return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
  179. decoder->private_->seek_table = 0;
  180. decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
  181. /* We initialize the FLAC__MD5Context even though we may never use it.  This
  182.  * is because md5 checking may be turned on to start and then turned off if
  183.  * a seek occurs.  So we always init the context here and finalize it in
  184.  * FLAC__seekable_stream_decoder_finish() to make sure things are always
  185.  * cleaned up properly.
  186.  */
  187. FLAC__MD5Init(&decoder->private_->md5context);
  188. FLAC__stream_decoder_set_read_callback(decoder->private_->stream_decoder, read_callback_);
  189. FLAC__stream_decoder_set_write_callback(decoder->private_->stream_decoder, write_callback_);
  190. FLAC__stream_decoder_set_metadata_callback(decoder->private_->stream_decoder, metadata_callback_);
  191. FLAC__stream_decoder_set_error_callback(decoder->private_->stream_decoder, error_callback_);
  192. FLAC__stream_decoder_set_client_data(decoder->private_->stream_decoder, decoder);
  193. /* We always want to see these blocks.  Whether or not we pass them up
  194.  * through the metadata callback will be determined by flags set in our
  195.  * implementation of ..._set_metadata_respond/ignore...()
  196.  */
  197. FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_STREAMINFO);
  198. FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_SEEKTABLE);
  199. if(FLAC__stream_decoder_init(decoder->private_->stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
  200. return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  201. return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
  202. }
  203. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_finish(FLAC__SeekableStreamDecoder *decoder)
  204. {
  205. FLAC__bool md5_failed = false;
  206. FLAC__ASSERT(0 != decoder);
  207. FLAC__ASSERT(0 != decoder->private_);
  208. FLAC__ASSERT(0 != decoder->protected_);
  209. if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  210. return true;
  211. FLAC__ASSERT(0 != decoder->private_->stream_decoder);
  212. /* see the comment in FLAC__seekable_stream_decoder_init() as to why we
  213.  * always call FLAC__MD5Final()
  214.  */
  215. FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
  216. FLAC__stream_decoder_finish(decoder->private_->stream_decoder);
  217. if(decoder->private_->do_md5_checking) {
  218. if(memcmp(decoder->private_->stored_md5sum, decoder->private_->computed_md5sum, 16))
  219. md5_failed = true;
  220. }
  221. set_defaults_(decoder);
  222. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
  223. return !md5_failed;
  224. }
  225. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_md5_checking(FLAC__SeekableStreamDecoder *decoder, FLAC__bool value)
  226. {
  227. FLAC__ASSERT(0 != decoder);
  228. FLAC__ASSERT(0 != decoder->protected_);
  229. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  230. return false;
  231. decoder->protected_->md5_checking = value;
  232. return true;
  233. }
  234. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_read_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderReadCallback value)
  235. {
  236. FLAC__ASSERT(0 != decoder);
  237. FLAC__ASSERT(0 != decoder->private_);
  238. FLAC__ASSERT(0 != decoder->protected_);
  239. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  240. return false;
  241. decoder->private_->read_callback = value;
  242. return true;
  243. }
  244. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_seek_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderSeekCallback value)
  245. {
  246. FLAC__ASSERT(0 != decoder);
  247. FLAC__ASSERT(0 != decoder->private_);
  248. FLAC__ASSERT(0 != decoder->protected_);
  249. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  250. return false;
  251. decoder->private_->seek_callback = value;
  252. return true;
  253. }
  254. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_tell_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderTellCallback value)
  255. {
  256. FLAC__ASSERT(0 != decoder);
  257. FLAC__ASSERT(0 != decoder->private_);
  258. FLAC__ASSERT(0 != decoder->protected_);
  259. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  260. return false;
  261. decoder->private_->tell_callback = value;
  262. return true;
  263. }
  264. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_length_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderLengthCallback value)
  265. {
  266. FLAC__ASSERT(0 != decoder);
  267. FLAC__ASSERT(0 != decoder->private_);
  268. FLAC__ASSERT(0 != decoder->protected_);
  269. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  270. return false;
  271. decoder->private_->length_callback = value;
  272. return true;
  273. }
  274. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_eof_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderEofCallback value)
  275. {
  276. FLAC__ASSERT(0 != decoder);
  277. FLAC__ASSERT(0 != decoder->private_);
  278. FLAC__ASSERT(0 != decoder->protected_);
  279. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  280. return false;
  281. decoder->private_->eof_callback = value;
  282. return true;
  283. }
  284. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_write_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderWriteCallback value)
  285. {
  286. FLAC__ASSERT(0 != decoder);
  287. FLAC__ASSERT(0 != decoder->private_);
  288. FLAC__ASSERT(0 != decoder->protected_);
  289. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  290. return false;
  291. decoder->private_->write_callback = value;
  292. return true;
  293. }
  294. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderMetadataCallback value)
  295. {
  296. FLAC__ASSERT(0 != decoder);
  297. FLAC__ASSERT(0 != decoder->private_);
  298. FLAC__ASSERT(0 != decoder->protected_);
  299. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  300. return false;
  301. decoder->private_->metadata_callback = value;
  302. return true;
  303. }
  304. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_error_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderErrorCallback value)
  305. {
  306. FLAC__ASSERT(0 != decoder);
  307. FLAC__ASSERT(0 != decoder->private_);
  308. FLAC__ASSERT(0 != decoder->protected_);
  309. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  310. return false;
  311. decoder->private_->error_callback = value;
  312. return true;
  313. }
  314. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_client_data(FLAC__SeekableStreamDecoder *decoder, void *value)
  315. {
  316. FLAC__ASSERT(0 != decoder);
  317. FLAC__ASSERT(0 != decoder->private_);
  318. FLAC__ASSERT(0 != decoder->protected_);
  319. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  320. return false;
  321. decoder->private_->client_data = value;
  322. return true;
  323. }
  324. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond(FLAC__SeekableStreamDecoder *decoder, FLAC__MetadataType type)
  325. {
  326. FLAC__ASSERT(0 != decoder);
  327. FLAC__ASSERT(0 != decoder->private_);
  328. FLAC__ASSERT(0 != decoder->protected_);
  329. FLAC__ASSERT(0 != decoder->private_->stream_decoder);
  330. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  331. return false;
  332. if(type == FLAC__METADATA_TYPE_STREAMINFO)
  333. decoder->private_->ignore_stream_info_block = false;
  334. else if(type == FLAC__METADATA_TYPE_SEEKTABLE)
  335. decoder->private_->ignore_seek_table_block = false;
  336. return FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, type);
  337. }
  338. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond_application(FLAC__SeekableStreamDecoder *decoder, const FLAC__byte id[4])
  339. {
  340. FLAC__ASSERT(0 != decoder);
  341. FLAC__ASSERT(0 != decoder->private_);
  342. FLAC__ASSERT(0 != decoder->protected_);
  343. FLAC__ASSERT(0 != decoder->private_->stream_decoder);
  344. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  345. return false;
  346. return FLAC__stream_decoder_set_metadata_respond_application(decoder->private_->stream_decoder, id);
  347. }
  348. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond_all(FLAC__SeekableStreamDecoder *decoder)
  349. {
  350. FLAC__ASSERT(0 != decoder);
  351. FLAC__ASSERT(0 != decoder->private_);
  352. FLAC__ASSERT(0 != decoder->protected_);
  353. FLAC__ASSERT(0 != decoder->private_->stream_decoder);
  354. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  355. return false;
  356. decoder->private_->ignore_stream_info_block = false;
  357. decoder->private_->ignore_seek_table_block = false;
  358. return FLAC__stream_decoder_set_metadata_respond_all(decoder->private_->stream_decoder);
  359. }
  360. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore(FLAC__SeekableStreamDecoder *decoder, FLAC__MetadataType type)
  361. {
  362. FLAC__ASSERT(0 != decoder);
  363. FLAC__ASSERT(0 != decoder->private_);
  364. FLAC__ASSERT(0 != decoder->protected_);
  365. FLAC__ASSERT(0 != decoder->private_->stream_decoder);
  366. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  367. return false;
  368. if(type == FLAC__METADATA_TYPE_STREAMINFO) {
  369. decoder->private_->ignore_stream_info_block = true;
  370. return true;
  371. }
  372. else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
  373. decoder->private_->ignore_seek_table_block = true;
  374. return true;
  375. }
  376. else
  377. return FLAC__stream_decoder_set_metadata_ignore(decoder->private_->stream_decoder, type);
  378. }
  379. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore_application(FLAC__SeekableStreamDecoder *decoder, const FLAC__byte id[4])
  380. {
  381. FLAC__ASSERT(0 != decoder);
  382. FLAC__ASSERT(0 != decoder->private_);
  383. FLAC__ASSERT(0 != decoder->protected_);
  384. FLAC__ASSERT(0 != decoder->private_->stream_decoder);
  385. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  386. return false;
  387. return FLAC__stream_decoder_set_metadata_ignore_application(decoder->private_->stream_decoder, id);
  388. }
  389. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore_all(FLAC__SeekableStreamDecoder *decoder)
  390. {
  391. FLAC__ASSERT(0 != decoder);
  392. FLAC__ASSERT(0 != decoder->private_);
  393. FLAC__ASSERT(0 != decoder->protected_);
  394. FLAC__ASSERT(0 != decoder->private_->stream_decoder);
  395. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
  396. return false;
  397. decoder->private_->ignore_stream_info_block = true;
  398. decoder->private_->ignore_seek_table_block = true;
  399. return
  400. FLAC__stream_decoder_set_metadata_ignore_all(decoder->private_->stream_decoder) &&
  401. FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_STREAMINFO) &&
  402. FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_SEEKTABLE);
  403. }
  404. FLAC_API FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_get_state(const FLAC__SeekableStreamDecoder *decoder)
  405. {
  406. FLAC__ASSERT(0 != decoder);
  407. FLAC__ASSERT(0 != decoder->protected_);
  408. return decoder->protected_->state;
  409. }
  410. FLAC_API FLAC__StreamDecoderState FLAC__seekable_stream_decoder_get_stream_decoder_state(const FLAC__SeekableStreamDecoder *decoder)
  411. {
  412. FLAC__ASSERT(0 != decoder);
  413. FLAC__ASSERT(0 != decoder->private_);
  414. return FLAC__stream_decoder_get_state(decoder->private_->stream_decoder);
  415. }
  416. FLAC_API const char *FLAC__seekable_stream_decoder_get_resolved_state_string(const FLAC__SeekableStreamDecoder *decoder)
  417. {
  418. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR)
  419. return FLAC__SeekableStreamDecoderStateString[decoder->protected_->state];
  420. else
  421. return FLAC__stream_decoder_get_resolved_state_string(decoder->private_->stream_decoder);
  422. }
  423. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_get_md5_checking(const FLAC__SeekableStreamDecoder *decoder)
  424. {
  425. FLAC__ASSERT(0 != decoder);
  426. FLAC__ASSERT(0 != decoder->protected_);
  427. return decoder->protected_->md5_checking;
  428. }
  429. FLAC_API unsigned FLAC__seekable_stream_decoder_get_channels(const FLAC__SeekableStreamDecoder *decoder)
  430. {
  431. FLAC__ASSERT(0 != decoder);
  432. FLAC__ASSERT(0 != decoder->private_);
  433. return FLAC__stream_decoder_get_channels(decoder->private_->stream_decoder);
  434. }
  435. FLAC_API FLAC__ChannelAssignment FLAC__seekable_stream_decoder_get_channel_assignment(const FLAC__SeekableStreamDecoder *decoder)
  436. {
  437. FLAC__ASSERT(0 != decoder);
  438. FLAC__ASSERT(0 != decoder->private_);
  439. return FLAC__stream_decoder_get_channel_assignment(decoder->private_->stream_decoder);
  440. }
  441. FLAC_API unsigned FLAC__seekable_stream_decoder_get_bits_per_sample(const FLAC__SeekableStreamDecoder *decoder)
  442. {
  443. FLAC__ASSERT(0 != decoder);
  444. FLAC__ASSERT(0 != decoder->private_);
  445. return FLAC__stream_decoder_get_bits_per_sample(decoder->private_->stream_decoder);
  446. }
  447. FLAC_API unsigned FLAC__seekable_stream_decoder_get_sample_rate(const FLAC__SeekableStreamDecoder *decoder)
  448. {
  449. FLAC__ASSERT(0 != decoder);
  450. FLAC__ASSERT(0 != decoder->private_);
  451. return FLAC__stream_decoder_get_sample_rate(decoder->private_->stream_decoder);
  452. }
  453. FLAC_API unsigned FLAC__seekable_stream_decoder_get_blocksize(const FLAC__SeekableStreamDecoder *decoder)
  454. {
  455. FLAC__ASSERT(0 != decoder);
  456. FLAC__ASSERT(0 != decoder->private_);
  457. return FLAC__stream_decoder_get_blocksize(decoder->private_->stream_decoder);
  458. }
  459. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_get_decode_position(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *position)
  460. {
  461. FLAC__ASSERT(0 != decoder);
  462. FLAC__ASSERT(0 != decoder->private_);
  463. FLAC__ASSERT(0 != position);
  464. if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK)
  465. return false;
  466. FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder));
  467. *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
  468. return true;
  469. }
  470. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_flush(FLAC__SeekableStreamDecoder *decoder)
  471. {
  472. FLAC__ASSERT(0 != decoder);
  473. FLAC__ASSERT(0 != decoder->private_);
  474. FLAC__ASSERT(0 != decoder->protected_);
  475. decoder->private_->do_md5_checking = false;
  476. if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
  477. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  478. return false;
  479. }
  480. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
  481. return true;
  482. }
  483. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_reset(FLAC__SeekableStreamDecoder *decoder)
  484. {
  485. FLAC__ASSERT(0 != decoder);
  486. FLAC__ASSERT(0 != decoder->private_);
  487. FLAC__ASSERT(0 != decoder->protected_);
  488. if(!FLAC__seekable_stream_decoder_flush(decoder)) {
  489. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  490. return false;
  491. }
  492. if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
  493. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  494. return false;
  495. }
  496. decoder->private_->seek_table = 0;
  497. decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
  498. /* We initialize the FLAC__MD5Context even though we may never use it.  This
  499.  * is because md5 checking may be turned on to start and then turned off if
  500.  * a seek occurs.  So we always init the context here and finalize it in
  501.  * FLAC__seekable_stream_decoder_finish() to make sure things are always
  502.  * cleaned up properly.
  503.  */
  504. FLAC__MD5Init(&decoder->private_->md5context);
  505. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
  506. return true;
  507. }
  508. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_process_single(FLAC__SeekableStreamDecoder *decoder)
  509. {
  510. FLAC__bool ret;
  511. FLAC__ASSERT(0 != decoder);
  512. if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
  513. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
  514. if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
  515. return true;
  516. FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
  517. ret = FLAC__stream_decoder_process_single(decoder->private_->stream_decoder);
  518. if(!ret)
  519. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  520. return ret;
  521. }
  522. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_process_until_end_of_metadata(FLAC__SeekableStreamDecoder *decoder)
  523. {
  524. FLAC__bool ret;
  525. FLAC__ASSERT(0 != decoder);
  526. if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
  527. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
  528. if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
  529. return true;
  530. FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
  531. ret = FLAC__stream_decoder_process_until_end_of_metadata(decoder->private_->stream_decoder);
  532. if(!ret)
  533. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  534. return ret;
  535. }
  536. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_process_until_end_of_stream(FLAC__SeekableStreamDecoder *decoder)
  537. {
  538. FLAC__bool ret;
  539. FLAC__ASSERT(0 != decoder);
  540. if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
  541. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
  542. if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
  543. return true;
  544. FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
  545. ret = FLAC__stream_decoder_process_until_end_of_stream(decoder->private_->stream_decoder);
  546. if(!ret)
  547. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  548. return ret;
  549. }
  550. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_skip_single_frame(FLAC__SeekableStreamDecoder *decoder)
  551. {
  552. FLAC__bool ret;
  553. FLAC__ASSERT(0 != decoder);
  554. if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
  555. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
  556. if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
  557. return true;
  558. FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
  559. ret = FLAC__stream_decoder_skip_single_frame(decoder->private_->stream_decoder);
  560. if(!ret)
  561. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  562. return ret;
  563. }
  564. FLAC_API FLAC__bool FLAC__seekable_stream_decoder_seek_absolute(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 sample)
  565. {
  566. FLAC__uint64 length;
  567. FLAC__ASSERT(0 != decoder);
  568. FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK || decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM);
  569. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEKING;
  570. /* turn off md5 checking if a seek is attempted */
  571. decoder->private_->do_md5_checking = false;
  572. if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
  573. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  574. return false;
  575. }
  576. /* get the file length */
  577. if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK) {
  578. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  579. return false;
  580. }
  581. /* rewind */
  582. if(decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
  583. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  584. return false;
  585. }
  586. if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder->private_->stream_decoder)) {
  587. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  588. return false;
  589. }
  590. if(decoder->private_->stream_info.total_samples > 0 && sample >= decoder->private_->stream_info.total_samples) {
  591. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  592. return false;
  593. }
  594. return seek_to_absolute_sample_(decoder, length, sample);
  595. }
  596. /***********************************************************************
  597.  *
  598.  * Private class methods
  599.  *
  600.  ***********************************************************************/
  601. void set_defaults_(FLAC__SeekableStreamDecoder *decoder)
  602. {
  603. decoder->private_->read_callback = 0;
  604. decoder->private_->seek_callback = 0;
  605. decoder->private_->tell_callback = 0;
  606. decoder->private_->length_callback = 0;
  607. decoder->private_->eof_callback = 0;
  608. decoder->private_->write_callback = 0;
  609. decoder->private_->metadata_callback = 0;
  610. decoder->private_->error_callback = 0;
  611. decoder->private_->client_data = 0;
  612. /* WATCHOUT: these should match the default behavior of FLAC__StreamDecoder */
  613. decoder->private_->ignore_stream_info_block = false;
  614. decoder->private_->ignore_seek_table_block = true;
  615. decoder->protected_->md5_checking = false;
  616. }
  617. FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  618. {
  619. FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
  620. (void)decoder;
  621. if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
  622. *bytes = 0;
  623. #if 0
  624. /*@@@@@@ verify that this is not needed */
  625. seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
  626. #endif
  627. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  628. }
  629. else if(*bytes > 0) {
  630. if(seekable_stream_decoder->private_->read_callback(seekable_stream_decoder, buffer, bytes, seekable_stream_decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK) {
  631. seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR;
  632. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  633. }
  634. if(*bytes == 0) {
  635. if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
  636. #if 0
  637. /*@@@@@@ verify that this is not needed */
  638. seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
  639. #endif
  640. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  641. }
  642. else
  643. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  644. }
  645. else {
  646. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  647. }
  648. }
  649. else
  650. return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
  651. }
  652. FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  653. {
  654. FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
  655. (void)decoder;
  656. if(seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
  657. FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
  658. FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
  659. FLAC__uint64 target_sample = seekable_stream_decoder->private_->target_sample;
  660. FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
  661. seekable_stream_decoder->private_->last_frame = *frame; /* save the frame */
  662. if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
  663. unsigned delta = (unsigned)(target_sample - this_frame_sample);
  664. /* kick out of seek mode */
  665. seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
  666. /* shift out the samples before target_sample */
  667. if(delta > 0) {
  668. unsigned channel;
  669. const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
  670. for(channel = 0; channel < frame->header.channels; channel++)
  671. newbuffer[channel] = buffer[channel] + delta;
  672. seekable_stream_decoder->private_->last_frame.header.blocksize -= delta;
  673. seekable_stream_decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
  674. /* write the relevant samples */
  675. return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, &seekable_stream_decoder->private_->last_frame, newbuffer, seekable_stream_decoder->private_->client_data);
  676. }
  677. else {
  678. /* write the relevant samples */
  679. return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
  680. }
  681. }
  682. else {
  683. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  684. }
  685. }
  686. else {
  687. if(seekable_stream_decoder->private_->do_md5_checking) {
  688. if(!FLAC__MD5Accumulate(&seekable_stream_decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
  689. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  690. }
  691. return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
  692. }
  693. }
  694. void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  695. {
  696. FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
  697. (void)decoder;
  698. if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
  699. seekable_stream_decoder->private_->stream_info = metadata->data.stream_info;
  700. /* save the MD5 signature for comparison later */
  701. memcpy(seekable_stream_decoder->private_->stored_md5sum, metadata->data.stream_info.md5sum, 16);
  702. if(0 == memcmp(seekable_stream_decoder->private_->stored_md5sum, "", 16))
  703. seekable_stream_decoder->private_->do_md5_checking = false;
  704. }
  705. else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE) {
  706. seekable_stream_decoder->private_->seek_table = &metadata->data.seek_table;
  707. }
  708. if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
  709. FLAC__bool ignore_block = false;
  710. if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO && seekable_stream_decoder->private_->ignore_stream_info_block)
  711. ignore_block = true;
  712. else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE && seekable_stream_decoder->private_->ignore_seek_table_block)
  713. ignore_block = true;
  714. if(!ignore_block)
  715. seekable_stream_decoder->private_->metadata_callback(seekable_stream_decoder, metadata, seekable_stream_decoder->private_->client_data);
  716. }
  717. }
  718. void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  719. {
  720. FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
  721. (void)decoder;
  722. if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
  723. seekable_stream_decoder->private_->error_callback(seekable_stream_decoder, status, seekable_stream_decoder->private_->client_data);
  724. }
  725. FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
  726. {
  727. FLAC__uint64 first_frame_offset, lower_bound, upper_bound;
  728. FLAC__int64 pos = -1, last_pos = -1;
  729. int i, lower_seek_point = -1, upper_seek_point = -1;
  730. unsigned approx_bytes_per_frame;
  731. FLAC__uint64 last_frame_sample = FLAC__U64L(0xffffffffffffffff);
  732. FLAC__bool needs_seek;
  733. const FLAC__uint64 total_samples = decoder->private_->stream_info.total_samples;
  734. const unsigned min_blocksize = decoder->private_->stream_info.min_blocksize;
  735. const unsigned max_blocksize = decoder->private_->stream_info.max_blocksize;
  736. const unsigned max_framesize = decoder->private_->stream_info.max_framesize;
  737. const unsigned channels = FLAC__seekable_stream_decoder_get_channels(decoder);
  738. const unsigned bps = FLAC__seekable_stream_decoder_get_bits_per_sample(decoder);
  739. /* we are just guessing here, but we want to guess high, not low */
  740. if(max_framesize > 0) {
  741. approx_bytes_per_frame = max_framesize;
  742. }
  743. /*
  744.  * Check if it's a known fixed-blocksize stream.  Note that though
  745.  * the spec doesn't allow zeroes in the STREAMINFO block, we may
  746.  * never get a STREAMINFO block when decoding so the value of
  747.  * min_blocksize might be zero.
  748.  */
  749. else if(min_blocksize == max_blocksize && min_blocksize > 0) {
  750. /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
  751. approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
  752. }
  753. else
  754. approx_bytes_per_frame = 4608 * channels * bps/8 + 64;
  755. /*
  756.  * The decode position is currently at the first frame since we
  757.  * rewound and processed metadata.
  758.  */
  759. if(!FLAC__seekable_stream_decoder_get_decode_position(decoder, &first_frame_offset)) {
  760. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  761. return false;
  762. }
  763. /*
  764.  * First, we set an upper and lower bound on where in the
  765.  * stream we will search.  For now we assume the worst case
  766.  * scenario, which is our best guess at the beginning of
  767.  * the first and last frames.
  768.  */
  769. lower_bound = first_frame_offset;
  770. /* calc the upper_bound, beyond which we never want to seek */
  771. if(max_framesize > 0)
  772. upper_bound = stream_length - (max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
  773. else
  774. upper_bound = stream_length - ((channels * bps * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
  775. /*
  776.  * Now we refine the bounds if we have a seektable with
  777.  * suitable points.  Note that according to the spec they
  778.  * must be ordered by ascending sample number.
  779.  */
  780. if(0 != decoder->private_->seek_table) {
  781. /* find the closest seek point <= target_sample, if it exists */
  782. for(i = (int)decoder->private_->seek_table->num_points - 1; i >= 0; i--) {
  783. if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number <= target_sample)
  784. break;
  785. }
  786. if(i >= 0) { /* i.e. we found a suitable seek point... */
  787. lower_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
  788. lower_seek_point = i;
  789. }
  790. /* find the closest seek point > target_sample, if it exists */
  791. for(i = 0; i < (int)decoder->private_->seek_table->num_points; i++) {
  792. if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number > target_sample)
  793. break;
  794. }
  795. if(i < (int)decoder->private_->seek_table->num_points) { /* i.e. we found a suitable seek point... */
  796. upper_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
  797. upper_seek_point = i;
  798. }
  799. }
  800. /*
  801.  * Now guess at where within those bounds our target
  802.  * sample will be.
  803.  */
  804. if(lower_seek_point >= 0) {
  805. /* first see if our sample is within a few frames of the lower seekpoint */
  806. if(decoder->private_->seek_table->points[lower_seek_point].sample_number <= target_sample && target_sample < decoder->private_->seek_table->points[lower_seek_point].sample_number + (decoder->private_->seek_table->points[lower_seek_point].frame_samples * 4)) {
  807. pos = (FLAC__int64)lower_bound;
  808. }
  809. else if(upper_seek_point >= 0) {
  810. const FLAC__uint64 target_offset = target_sample - decoder->private_->seek_table->points[lower_seek_point].sample_number;
  811. const FLAC__uint64 range_samples = decoder->private_->seek_table->points[upper_seek_point].sample_number - decoder->private_->seek_table->points[lower_seek_point].sample_number;
  812. const FLAC__uint64 range_bytes = (upper_bound>lower_bound? upper_bound - lower_bound - 1 : 0);
  813. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  814. #if defined _MSC_VER || defined __MINGW32__
  815. /* with MSVC you have to spoon feed it the casting */
  816. pos = (FLAC__int64)lower_bound + (FLAC__int64)(((FLAC__double)(FLAC__int64)target_offset / (FLAC__double)(FLAC__int64)range_samples) * (FLAC__double)(FLAC__int64)(range_bytes-1)) - approx_bytes_per_frame;
  817. #else
  818. pos = (FLAC__int64)lower_bound + (FLAC__int64)(((FLAC__double)target_offset / (FLAC__double)range_samples) * (FLAC__double)range_bytes) - approx_bytes_per_frame;
  819. #endif
  820. #else
  821. /* a little less accurate: */
  822. if (range_bytes <= 0xffffffff)
  823. pos = (FLAC__int64)lower_bound + (FLAC__int64)((target_offset * range_bytes) / range_samples) - approx_bytes_per_frame;
  824. else /* @@@ WATCHOUT, ~2TB limit */
  825. pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_offset>>8) * (range_bytes>>8)) / (range_samples>>16)) - approx_bytes_per_frame;
  826. #endif
  827. }
  828. }
  829. /*
  830.  * If there's no seek table, we need to use the metadata (if we
  831.  * have it) and the filelength to estimate the position of the
  832.  * frame with the correct sample.
  833.  */
  834. if(pos < 0 && total_samples > 0) {
  835. /*
  836.  * For max accuracy we should be using
  837.  * (stream_length-first_frame_offset-1) in the divisor, but the
  838.  * difference is trivial and (stream_length-first_frame_offset)
  839.  * has no chance of underflow.
  840.  */
  841. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  842. #if defined _MSC_VER || defined __MINGW32__
  843. /* with VC++ you have to spoon feed it the casting */
  844. pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((FLAC__double)(FLAC__int64)target_sample / (FLAC__double)(FLAC__int64)total_samples) * (FLAC__double)(FLAC__int64)(stream_length-first_frame_offset)) - approx_bytes_per_frame;
  845. #else
  846. pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((FLAC__double)target_sample / (FLAC__double)total_samples) * (FLAC__double)(stream_length-first_frame_offset)) - approx_bytes_per_frame;
  847. #endif
  848. #else
  849. /* a little less accurate: */
  850. if (stream_length < 0xffffffff)
  851. pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((target_sample * (stream_length-first_frame_offset)) / total_samples) - approx_bytes_per_frame;
  852. else /* @@@ WATCHOUT, ~2TB limit */
  853. pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((target_sample>>8) * ((stream_length-first_frame_offset)>>8)) / (total_samples>>16)) - approx_bytes_per_frame;
  854. #endif
  855. }
  856. /*
  857.  * If there's no seek table and total_samples is unknown, we
  858.  * don't even bother trying to figure out a target, we just use
  859.  * our current position.
  860.  */
  861. if(pos < 0) {
  862. FLAC__uint64 upos;
  863. if(decoder->private_->tell_callback(decoder, &upos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
  864. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  865. return false;
  866. }
  867. pos = (FLAC__int64)upos;
  868. needs_seek = false;
  869. }
  870. else
  871. needs_seek = true;
  872. /* clip the position to the bounds, lower bound takes precedence */
  873. if(pos >= (FLAC__int64)upper_bound) {
  874. pos = (FLAC__int64)upper_bound-1;
  875. needs_seek = true;
  876. }
  877. if(pos < (FLAC__int64)lower_bound) {
  878. pos = (FLAC__int64)lower_bound;
  879. needs_seek = true;
  880. }
  881. decoder->private_->target_sample = target_sample;
  882. while(1) {
  883. if(needs_seek) {
  884. if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
  885. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  886. return false;
  887. }
  888. if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
  889. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
  890. return false;
  891. }
  892. }
  893. /* Now we need to get a frame.  It is possible for our seek
  894.  * to land in the middle of audio data that looks exactly like
  895.  * a frame header from a future version of an encoder.  When
  896.  * that happens, FLAC__stream_decoder_process_single() will
  897.  * return false and the state will be
  898.  * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM.  But there is a
  899.  * remote possibility that it is properly synced at such a
  900.  * "future-codec frame", so to make sure, we wait to see
  901.  * several "unparseable" errors in a row before bailing out.
  902.  */
  903. {
  904. unsigned unparseable_count;
  905. FLAC__bool got_a_frame = false;
  906. for (unparseable_count = 0; !got_a_frame && unparseable_count < 10; unparseable_count++) {
  907. if(FLAC__stream_decoder_process_single(decoder->private_->stream_decoder))
  908. got_a_frame = true;
  909. else if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_UNPARSEABLE_STREAM)
  910. /* try again.  we don't want to flush the decoder since that clears the bitbuffer */
  911. decoder->private_->stream_decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  912. else /* it's a real error */
  913. break;
  914. }
  915. if (!got_a_frame) {
  916. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  917. return false;
  918. }
  919. }
  920. /* our write callback will change the state when it gets to the target frame */
  921. /* actually, we could have got_a_frame if our decoder is at FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM so we need to check for that also */
  922. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM) {
  923. break;
  924. }
  925. else { /* we need to narrow the search */
  926. const FLAC__uint64 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
  927. FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
  928. if(this_frame_sample == last_frame_sample && pos < last_pos) {
  929. /* our last move backwards wasn't big enough, double it */
  930. pos -= (last_pos - pos);
  931. needs_seek = true;
  932. }
  933. else {
  934. if(target_sample < this_frame_sample) {
  935. last_pos = pos;
  936. approx_bytes_per_frame = decoder->private_->last_frame.header.blocksize * channels * bps/8 + 64;
  937. pos -= approx_bytes_per_frame;
  938. needs_seek = true;
  939. }
  940. else { /* target_sample >= this_frame_sample + this frame's blocksize */
  941. FLAC__uint64 upos;
  942. if(decoder->private_->tell_callback(decoder, &upos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
  943. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  944. return false;
  945. }
  946. last_pos = pos;
  947. pos = (FLAC__int64)upos;
  948. pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
  949. needs_seek = false;
  950. /*
  951.  * if we haven't hit the target frame yet and our position hasn't changed,
  952.  * it means we're at the end of the stream and the seek target does not exist.
  953.  */
  954. if(last_pos == pos) {
  955. decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
  956. return false;
  957. }
  958. }
  959. }
  960. if(pos < (FLAC__int64)lower_bound)
  961. pos = (FLAC__int64)lower_bound;
  962. last_frame_sample = this_frame_sample;
  963. }
  964. }
  965. return true;
  966. }