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

Windows CE

开发平台:

C/C++

  1. /* libOggFLAC - Free Lossless Audio Codec + Ogg library
  2.  * Copyright (C) 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 <stdlib.h> /* for calloc() */
  32. #include "FLAC/assert.h"
  33. #include "protected/stream_decoder.h"
  34. /***********************************************************************
  35.  *
  36.  * Private class method prototypes
  37.  *
  38.  ***********************************************************************/
  39. static void set_defaults_(OggFLAC__StreamDecoder *decoder);
  40. static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
  41. static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
  42. static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
  43. static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
  44. static OggFLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
  45. /***********************************************************************
  46.  *
  47.  * Private class data
  48.  *
  49.  ***********************************************************************/
  50. typedef struct OggFLAC__StreamDecoderPrivate {
  51. OggFLAC__StreamDecoderReadCallback read_callback;
  52. OggFLAC__StreamDecoderWriteCallback write_callback;
  53. OggFLAC__StreamDecoderMetadataCallback metadata_callback;
  54. OggFLAC__StreamDecoderErrorCallback error_callback;
  55. void *client_data;
  56. FLAC__StreamDecoder *FLAC_stream_decoder;
  57. } OggFLAC__StreamDecoderPrivate;
  58. /***********************************************************************
  59.  *
  60.  * Public static class data
  61.  *
  62.  ***********************************************************************/
  63. OggFLAC_API const char * const OggFLAC__StreamDecoderStateString[] = {
  64. "OggFLAC__STREAM_DECODER_OK",
  65. "OggFLAC__STREAM_DECODER_END_OF_STREAM",
  66. "OggFLAC__STREAM_DECODER_OGG_ERROR",
  67. "OggFLAC__STREAM_DECODER_READ_ERROR",
  68. "OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR",
  69. "OggFLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
  70. "OggFLAC__STREAM_DECODER_ALREADY_INITIALIZED",
  71. "OggFLAC__STREAM_DECODER_INVALID_CALLBACK",
  72. "OggFLAC__STREAM_DECODER_UNINITIALIZED"
  73. };
  74. /***********************************************************************
  75.  *
  76.  * Class constructor/destructor
  77.  *
  78.  ***********************************************************************/
  79. OggFLAC_API OggFLAC__StreamDecoder *OggFLAC__stream_decoder_new()
  80. {
  81. OggFLAC__StreamDecoder *decoder;
  82. decoder = (OggFLAC__StreamDecoder*)calloc(1, sizeof(OggFLAC__StreamDecoder));
  83. if(decoder == 0) {
  84. return 0;
  85. }
  86. decoder->protected_ = (OggFLAC__StreamDecoderProtected*)calloc(1, sizeof(OggFLAC__StreamDecoderProtected));
  87. if(decoder->protected_ == 0) {
  88. free(decoder);
  89. return 0;
  90. }
  91. decoder->private_ = (OggFLAC__StreamDecoderPrivate*)calloc(1, sizeof(OggFLAC__StreamDecoderPrivate));
  92. if(decoder->private_ == 0) {
  93. free(decoder->protected_);
  94. free(decoder);
  95. return 0;
  96. }
  97. decoder->private_->FLAC_stream_decoder = FLAC__stream_decoder_new();
  98. if(0 == decoder->private_->FLAC_stream_decoder) {
  99. free(decoder->private_);
  100. free(decoder->protected_);
  101. free(decoder);
  102. return 0;
  103. }
  104. set_defaults_(decoder);
  105. decoder->protected_->state = OggFLAC__STREAM_DECODER_UNINITIALIZED;
  106. return decoder;
  107. }
  108. OggFLAC_API void OggFLAC__stream_decoder_delete(OggFLAC__StreamDecoder *decoder)
  109. {
  110. FLAC__ASSERT(0 != decoder);
  111. FLAC__ASSERT(0 != decoder->protected_);
  112. FLAC__ASSERT(0 != decoder->private_);
  113. FLAC__ASSERT(0 != decoder->private_->FLAC_stream_decoder);
  114. OggFLAC__stream_decoder_finish(decoder);
  115. FLAC__stream_decoder_delete(decoder->private_->FLAC_stream_decoder);
  116. free(decoder->private_);
  117. free(decoder->protected_);
  118. free(decoder);
  119. }
  120. /***********************************************************************
  121.  *
  122.  * Public class methods
  123.  *
  124.  ***********************************************************************/
  125. OggFLAC_API OggFLAC__StreamDecoderState OggFLAC__stream_decoder_init(OggFLAC__StreamDecoder *decoder)
  126. {
  127. FLAC__ASSERT(0 != decoder);
  128. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  129. return decoder->protected_->state = OggFLAC__STREAM_DECODER_ALREADY_INITIALIZED;
  130. if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
  131. return decoder->protected_->state = OggFLAC__STREAM_DECODER_INVALID_CALLBACK;
  132. if(!OggFLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
  133. return decoder->protected_->state = OggFLAC__STREAM_DECODER_OGG_ERROR;
  134. FLAC__stream_decoder_set_read_callback(decoder->private_->FLAC_stream_decoder, read_callback_);
  135. FLAC__stream_decoder_set_write_callback(decoder->private_->FLAC_stream_decoder, write_callback_);
  136. FLAC__stream_decoder_set_metadata_callback(decoder->private_->FLAC_stream_decoder, metadata_callback_);
  137. FLAC__stream_decoder_set_error_callback(decoder->private_->FLAC_stream_decoder, error_callback_);
  138. FLAC__stream_decoder_set_client_data(decoder->private_->FLAC_stream_decoder, decoder);
  139. if(FLAC__stream_decoder_init(decoder->private_->FLAC_stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
  140. return decoder->protected_->state = OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR;
  141. return decoder->protected_->state = OggFLAC__STREAM_DECODER_OK;
  142. }
  143. OggFLAC_API void OggFLAC__stream_decoder_finish(OggFLAC__StreamDecoder *decoder)
  144. {
  145. FLAC__ASSERT(0 != decoder);
  146. FLAC__ASSERT(0 != decoder->private_);
  147. FLAC__ASSERT(0 != decoder->protected_);
  148. if(decoder->protected_->state == OggFLAC__STREAM_DECODER_UNINITIALIZED)
  149. return;
  150. FLAC__ASSERT(0 != decoder->private_->FLAC_stream_decoder);
  151. FLAC__stream_decoder_finish(decoder->private_->FLAC_stream_decoder);
  152. OggFLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
  153. set_defaults_(decoder);
  154. decoder->protected_->state = OggFLAC__STREAM_DECODER_UNINITIALIZED;
  155. }
  156. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_read_callback(OggFLAC__StreamDecoder *decoder, OggFLAC__StreamDecoderReadCallback value)
  157. {
  158. FLAC__ASSERT(0 != decoder);
  159. FLAC__ASSERT(0 != decoder->private_);
  160. FLAC__ASSERT(0 != decoder->protected_);
  161. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  162. return false;
  163. decoder->private_->read_callback = value;
  164. return true;
  165. }
  166. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_write_callback(OggFLAC__StreamDecoder *decoder, OggFLAC__StreamDecoderWriteCallback value)
  167. {
  168. FLAC__ASSERT(0 != decoder);
  169. FLAC__ASSERT(0 != decoder->private_);
  170. FLAC__ASSERT(0 != decoder->protected_);
  171. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  172. return false;
  173. decoder->private_->write_callback = value;
  174. return true;
  175. }
  176. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_callback(OggFLAC__StreamDecoder *decoder, OggFLAC__StreamDecoderMetadataCallback value)
  177. {
  178. FLAC__ASSERT(0 != decoder);
  179. FLAC__ASSERT(0 != decoder->private_);
  180. FLAC__ASSERT(0 != decoder->protected_);
  181. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  182. return false;
  183. decoder->private_->metadata_callback = value;
  184. return true;
  185. }
  186. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_error_callback(OggFLAC__StreamDecoder *decoder, OggFLAC__StreamDecoderErrorCallback value)
  187. {
  188. FLAC__ASSERT(0 != decoder);
  189. FLAC__ASSERT(0 != decoder->private_);
  190. FLAC__ASSERT(0 != decoder->protected_);
  191. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  192. return false;
  193. decoder->private_->error_callback = value;
  194. return true;
  195. }
  196. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_client_data(OggFLAC__StreamDecoder *decoder, void *value)
  197. {
  198. FLAC__ASSERT(0 != decoder);
  199. FLAC__ASSERT(0 != decoder->private_);
  200. FLAC__ASSERT(0 != decoder->protected_);
  201. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  202. return false;
  203. decoder->private_->client_data = value;
  204. return true;
  205. }
  206. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_serial_number(OggFLAC__StreamDecoder *decoder, long value)
  207. {
  208. FLAC__ASSERT(0 != decoder);
  209. FLAC__ASSERT(0 != decoder->private_);
  210. FLAC__ASSERT(0 != decoder->protected_);
  211. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  212. return false;
  213. OggFLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
  214. return true;
  215. }
  216. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_respond(OggFLAC__StreamDecoder *decoder, FLAC__MetadataType type)
  217. {
  218. FLAC__ASSERT(0 != decoder);
  219. FLAC__ASSERT(0 != decoder->private_);
  220. FLAC__ASSERT(0 != decoder->protected_);
  221. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  222. return false;
  223. return FLAC__stream_decoder_set_metadata_respond(decoder->private_->FLAC_stream_decoder, type);
  224. }
  225. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_respond_application(OggFLAC__StreamDecoder *decoder, const FLAC__byte id[4])
  226. {
  227. FLAC__ASSERT(0 != decoder);
  228. FLAC__ASSERT(0 != decoder->private_);
  229. FLAC__ASSERT(0 != decoder->protected_);
  230. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  231. return false;
  232. return FLAC__stream_decoder_set_metadata_respond_application(decoder->private_->FLAC_stream_decoder, id);
  233. }
  234. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_respond_all(OggFLAC__StreamDecoder *decoder)
  235. {
  236. FLAC__ASSERT(0 != decoder);
  237. FLAC__ASSERT(0 != decoder->private_);
  238. FLAC__ASSERT(0 != decoder->protected_);
  239. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  240. return false;
  241. return FLAC__stream_decoder_set_metadata_respond_all(decoder->private_->FLAC_stream_decoder);
  242. }
  243. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_ignore(OggFLAC__StreamDecoder *decoder, FLAC__MetadataType type)
  244. {
  245. FLAC__ASSERT(0 != decoder);
  246. FLAC__ASSERT(0 != decoder->private_);
  247. FLAC__ASSERT(0 != decoder->protected_);
  248. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  249. return false;
  250. return FLAC__stream_decoder_set_metadata_ignore(decoder->private_->FLAC_stream_decoder, type);
  251. }
  252. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_ignore_application(OggFLAC__StreamDecoder *decoder, const FLAC__byte id[4])
  253. {
  254. FLAC__ASSERT(0 != decoder);
  255. FLAC__ASSERT(0 != decoder->private_);
  256. FLAC__ASSERT(0 != decoder->protected_);
  257. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  258. return false;
  259. return FLAC__stream_decoder_set_metadata_ignore_application(decoder->private_->FLAC_stream_decoder, id);
  260. }
  261. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_ignore_all(OggFLAC__StreamDecoder *decoder)
  262. {
  263. FLAC__ASSERT(0 != decoder);
  264. FLAC__ASSERT(0 != decoder->private_);
  265. FLAC__ASSERT(0 != decoder->protected_);
  266. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
  267. return false;
  268. return FLAC__stream_decoder_set_metadata_ignore_all(decoder->private_->FLAC_stream_decoder);
  269. }
  270. OggFLAC_API OggFLAC__StreamDecoderState OggFLAC__stream_decoder_get_state(const OggFLAC__StreamDecoder *decoder)
  271. {
  272. FLAC__ASSERT(0 != decoder);
  273. FLAC__ASSERT(0 != decoder->protected_);
  274. return decoder->protected_->state;
  275. }
  276. OggFLAC_API FLAC__StreamDecoderState OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(const OggFLAC__StreamDecoder *decoder)
  277. {
  278. FLAC__ASSERT(0 != decoder);
  279. FLAC__ASSERT(0 != decoder->private_);
  280. return FLAC__stream_decoder_get_state(decoder->private_->FLAC_stream_decoder);
  281. }
  282. OggFLAC_API const char *OggFLAC__stream_decoder_get_resolved_state_string(const OggFLAC__StreamDecoder *decoder)
  283. {
  284. if(decoder->protected_->state != OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR)
  285. return OggFLAC__StreamDecoderStateString[decoder->protected_->state];
  286. else
  287. return FLAC__stream_decoder_get_resolved_state_string(decoder->private_->FLAC_stream_decoder);
  288. }
  289. OggFLAC_API unsigned OggFLAC__stream_decoder_get_channels(const OggFLAC__StreamDecoder *decoder)
  290. {
  291. FLAC__ASSERT(0 != decoder);
  292. FLAC__ASSERT(0 != decoder->private_);
  293. return FLAC__stream_decoder_get_channels(decoder->private_->FLAC_stream_decoder);
  294. }
  295. OggFLAC_API FLAC__ChannelAssignment OggFLAC__stream_decoder_get_channel_assignment(const OggFLAC__StreamDecoder *decoder)
  296. {
  297. FLAC__ASSERT(0 != decoder);
  298. FLAC__ASSERT(0 != decoder->private_);
  299. return FLAC__stream_decoder_get_channel_assignment(decoder->private_->FLAC_stream_decoder);
  300. }
  301. OggFLAC_API unsigned OggFLAC__stream_decoder_get_bits_per_sample(const OggFLAC__StreamDecoder *decoder)
  302. {
  303. FLAC__ASSERT(0 != decoder);
  304. FLAC__ASSERT(0 != decoder->private_);
  305. return FLAC__stream_decoder_get_bits_per_sample(decoder->private_->FLAC_stream_decoder);
  306. }
  307. OggFLAC_API unsigned OggFLAC__stream_decoder_get_sample_rate(const OggFLAC__StreamDecoder *decoder)
  308. {
  309. FLAC__ASSERT(0 != decoder);
  310. FLAC__ASSERT(0 != decoder->private_);
  311. return FLAC__stream_decoder_get_sample_rate(decoder->private_->FLAC_stream_decoder);
  312. }
  313. OggFLAC_API unsigned OggFLAC__stream_decoder_get_blocksize(const OggFLAC__StreamDecoder *decoder)
  314. {
  315. FLAC__ASSERT(0 != decoder);
  316. FLAC__ASSERT(0 != decoder->private_);
  317. return FLAC__stream_decoder_get_blocksize(decoder->private_->FLAC_stream_decoder);
  318. }
  319. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_flush(OggFLAC__StreamDecoder *decoder)
  320. {
  321. FLAC__ASSERT(0 != decoder);
  322. FLAC__ASSERT(0 != decoder->private_);
  323. FLAC__ASSERT(0 != decoder->protected_);
  324. OggFLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
  325. if(!FLAC__stream_decoder_flush(decoder->private_->FLAC_stream_decoder)) {
  326. decoder->protected_->state = OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR;
  327. return false;
  328. }
  329. decoder->protected_->state = OggFLAC__STREAM_DECODER_OK;
  330. return true;
  331. }
  332. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_reset(OggFLAC__StreamDecoder *decoder)
  333. {
  334. FLAC__ASSERT(0 != decoder);
  335. FLAC__ASSERT(0 != decoder->private_);
  336. FLAC__ASSERT(0 != decoder->protected_);
  337. if(!OggFLAC__stream_decoder_flush(decoder)) {
  338. decoder->protected_->state = OggFLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  339. return false;
  340. }
  341. OggFLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
  342. if(!FLAC__stream_decoder_reset(decoder->private_->FLAC_stream_decoder)) {
  343. decoder->protected_->state = OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR;
  344. return false;
  345. }
  346. decoder->protected_->state = OggFLAC__STREAM_DECODER_OK;
  347. return true;
  348. }
  349. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_process_single(OggFLAC__StreamDecoder *decoder)
  350. {
  351. FLAC__bool ret;
  352. FLAC__ASSERT(0 != decoder);
  353. if(FLAC__stream_decoder_get_state(decoder->private_->FLAC_stream_decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
  354. decoder->protected_->state = OggFLAC__STREAM_DECODER_END_OF_STREAM;
  355. if(decoder->protected_->state == OggFLAC__STREAM_DECODER_END_OF_STREAM)
  356. return true;
  357. FLAC__ASSERT(decoder->protected_->state == OggFLAC__STREAM_DECODER_OK);
  358. ret = FLAC__stream_decoder_process_single(decoder->private_->FLAC_stream_decoder);
  359. if(!ret)
  360. decoder->protected_->state = OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR;
  361. return ret;
  362. }
  363. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_process_until_end_of_metadata(OggFLAC__StreamDecoder *decoder)
  364. {
  365. FLAC__bool ret;
  366. FLAC__ASSERT(0 != decoder);
  367. if(FLAC__stream_decoder_get_state(decoder->private_->FLAC_stream_decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
  368. decoder->protected_->state = OggFLAC__STREAM_DECODER_END_OF_STREAM;
  369. if(decoder->protected_->state == OggFLAC__STREAM_DECODER_END_OF_STREAM)
  370. return true;
  371. FLAC__ASSERT(decoder->protected_->state == OggFLAC__STREAM_DECODER_OK);
  372. ret = FLAC__stream_decoder_process_until_end_of_metadata(decoder->private_->FLAC_stream_decoder);
  373. if(!ret)
  374. decoder->protected_->state = OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR;
  375. return ret;
  376. }
  377. OggFLAC_API FLAC__bool OggFLAC__stream_decoder_process_until_end_of_stream(OggFLAC__StreamDecoder *decoder)
  378. {
  379. FLAC__bool ret;
  380. FLAC__ASSERT(0 != decoder);
  381. if(FLAC__stream_decoder_get_state(decoder->private_->FLAC_stream_decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
  382. decoder->protected_->state = OggFLAC__STREAM_DECODER_END_OF_STREAM;
  383. if(decoder->protected_->state == OggFLAC__STREAM_DECODER_END_OF_STREAM)
  384. return true;
  385. FLAC__ASSERT(decoder->protected_->state == OggFLAC__STREAM_DECODER_OK);
  386. ret = FLAC__stream_decoder_process_until_end_of_stream(decoder->private_->FLAC_stream_decoder);
  387. if(!ret)
  388. decoder->protected_->state = OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR;
  389. return ret;
  390. }
  391. /***********************************************************************
  392.  *
  393.  * Private class methods
  394.  *
  395.  ***********************************************************************/
  396. void set_defaults_(OggFLAC__StreamDecoder *decoder)
  397. {
  398. decoder->private_->read_callback = 0;
  399. decoder->private_->write_callback = 0;
  400. decoder->private_->metadata_callback = 0;
  401. decoder->private_->error_callback = 0;
  402. decoder->private_->client_data = 0;
  403. OggFLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
  404. }
  405. FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *unused, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  406. {
  407. OggFLAC__StreamDecoder *decoder = (OggFLAC__StreamDecoder*)client_data;
  408. (void)unused;
  409. switch(OggFLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
  410. case OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
  411. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  412. /* we don't really have a way to handle lost sync via read
  413.  * callback so we'll let it pass and let the underlying
  414.  * FLAC decoder catch the error
  415.  */
  416. case OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
  417. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  418. case OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
  419. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  420. case OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
  421. case OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
  422. case OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
  423. case OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
  424. decoder->protected_->state = OggFLAC__STREAM_DECODER_READ_ERROR;
  425. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  426. case OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
  427. decoder->protected_->state = OggFLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  428. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  429. default:
  430. FLAC__ASSERT(0);
  431. /* double protection */
  432. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  433. }
  434. }
  435. FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *unused, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  436. {
  437. OggFLAC__StreamDecoder *decoder = (OggFLAC__StreamDecoder*)client_data;
  438. (void)unused;
  439. return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
  440. }
  441. void metadata_callback_(const FLAC__StreamDecoder *unused, const FLAC__StreamMetadata *metadata, void *client_data)
  442. {
  443. OggFLAC__StreamDecoder *decoder = (OggFLAC__StreamDecoder*)client_data;
  444. (void)unused;
  445. decoder->private_->metadata_callback(decoder, metadata, decoder->private_->client_data);
  446. }
  447. void error_callback_(const FLAC__StreamDecoder *unused, FLAC__StreamDecoderErrorStatus status, void *client_data)
  448. {
  449. OggFLAC__StreamDecoder *decoder = (OggFLAC__StreamDecoder*)client_data;
  450. (void)unused;
  451. decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
  452. }
  453. OggFLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  454. {
  455. OggFLAC__StreamDecoder *decoder = (OggFLAC__StreamDecoder*)void_decoder;
  456. switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
  457. case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
  458. return OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
  459. case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
  460. return OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
  461. case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
  462. return OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
  463. default:
  464. /* double protection: */
  465. FLAC__ASSERT(0);
  466. return OggFLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
  467. }
  468. }