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

Windows CE

开发平台:

C/C++

  1. /* libFLAC++ - Free Lossless Audio Codec 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 "FLAC++/decoder.h"
  32. #include "FLAC/assert.h"
  33. #ifdef _MSC_VER
  34. // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
  35. #pragma warning ( disable : 4800 )
  36. #endif
  37. namespace FLAC {
  38. namespace Decoder {
  39. SeekableStream::SeekableStream():
  40. decoder_(::FLAC__seekable_stream_decoder_new())
  41. { }
  42. SeekableStream::~SeekableStream()
  43. {
  44. if(0 != decoder_) {
  45. (void) ::FLAC__seekable_stream_decoder_finish(decoder_);
  46. ::FLAC__seekable_stream_decoder_delete(decoder_);
  47. }
  48. }
  49. bool SeekableStream::is_valid() const
  50. {
  51. return 0 != decoder_;
  52. }
  53. bool SeekableStream::set_md5_checking(bool value)
  54. {
  55. FLAC__ASSERT(is_valid());
  56. return (bool)::FLAC__seekable_stream_decoder_set_md5_checking(decoder_, value);
  57. }
  58. bool SeekableStream::set_metadata_respond(::FLAC__MetadataType type)
  59. {
  60. FLAC__ASSERT(is_valid());
  61. return (bool)::FLAC__seekable_stream_decoder_set_metadata_respond(decoder_, type);
  62. }
  63. bool SeekableStream::set_metadata_respond_application(const FLAC__byte id[4])
  64. {
  65. FLAC__ASSERT(is_valid());
  66. return (bool)::FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder_, id);
  67. }
  68. bool SeekableStream::set_metadata_respond_all()
  69. {
  70. FLAC__ASSERT(is_valid());
  71. return (bool)::FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder_);
  72. }
  73. bool SeekableStream::set_metadata_ignore(::FLAC__MetadataType type)
  74. {
  75. FLAC__ASSERT(is_valid());
  76. return (bool)::FLAC__seekable_stream_decoder_set_metadata_ignore(decoder_, type);
  77. }
  78. bool SeekableStream::set_metadata_ignore_application(const FLAC__byte id[4])
  79. {
  80. FLAC__ASSERT(is_valid());
  81. return (bool)::FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder_, id);
  82. }
  83. bool SeekableStream::set_metadata_ignore_all()
  84. {
  85. FLAC__ASSERT(is_valid());
  86. return (bool)::FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder_);
  87. }
  88. SeekableStream::State SeekableStream::get_state() const
  89. {
  90. FLAC__ASSERT(is_valid());
  91. return State(::FLAC__seekable_stream_decoder_get_state(decoder_));
  92. }
  93. Stream::State SeekableStream::get_stream_decoder_state() const
  94. {
  95. FLAC__ASSERT(is_valid());
  96. return Stream::State(::FLAC__seekable_stream_decoder_get_stream_decoder_state(decoder_));
  97. }
  98. bool SeekableStream::get_md5_checking() const
  99. {
  100. FLAC__ASSERT(is_valid());
  101. return (bool)::FLAC__seekable_stream_decoder_get_md5_checking(decoder_);
  102. }
  103. unsigned SeekableStream::get_channels() const
  104. {
  105. FLAC__ASSERT(is_valid());
  106. return ::FLAC__seekable_stream_decoder_get_channels(decoder_);
  107. }
  108. ::FLAC__ChannelAssignment SeekableStream::get_channel_assignment() const
  109. {
  110. FLAC__ASSERT(is_valid());
  111. return ::FLAC__seekable_stream_decoder_get_channel_assignment(decoder_);
  112. }
  113. unsigned SeekableStream::get_bits_per_sample() const
  114. {
  115. FLAC__ASSERT(is_valid());
  116. return ::FLAC__seekable_stream_decoder_get_bits_per_sample(decoder_);
  117. }
  118. unsigned SeekableStream::get_sample_rate() const
  119. {
  120. FLAC__ASSERT(is_valid());
  121. return ::FLAC__seekable_stream_decoder_get_sample_rate(decoder_);
  122. }
  123. unsigned SeekableStream::get_blocksize() const
  124. {
  125. FLAC__ASSERT(is_valid());
  126. return ::FLAC__seekable_stream_decoder_get_blocksize(decoder_);
  127. }
  128. SeekableStream::State SeekableStream::init()
  129. {
  130. FLAC__ASSERT(is_valid());
  131. ::FLAC__seekable_stream_decoder_set_read_callback(decoder_, read_callback_);
  132. ::FLAC__seekable_stream_decoder_set_seek_callback(decoder_, seek_callback_);
  133. ::FLAC__seekable_stream_decoder_set_tell_callback(decoder_, tell_callback_);
  134. ::FLAC__seekable_stream_decoder_set_length_callback(decoder_, length_callback_);
  135. ::FLAC__seekable_stream_decoder_set_eof_callback(decoder_, eof_callback_);
  136. ::FLAC__seekable_stream_decoder_set_write_callback(decoder_, write_callback_);
  137. ::FLAC__seekable_stream_decoder_set_metadata_callback(decoder_, metadata_callback_);
  138. ::FLAC__seekable_stream_decoder_set_error_callback(decoder_, error_callback_);
  139. ::FLAC__seekable_stream_decoder_set_client_data(decoder_, (void*)this);
  140. return State(::FLAC__seekable_stream_decoder_init(decoder_));
  141. }
  142. bool SeekableStream::finish()
  143. {
  144. FLAC__ASSERT(is_valid());
  145. return (bool)::FLAC__seekable_stream_decoder_finish(decoder_);
  146. }
  147. bool SeekableStream::flush()
  148. {
  149. FLAC__ASSERT(is_valid());
  150. return (bool)::FLAC__seekable_stream_decoder_flush(decoder_);
  151. }
  152. bool SeekableStream::reset()
  153. {
  154. FLAC__ASSERT(is_valid());
  155. return (bool)::FLAC__seekable_stream_decoder_reset(decoder_);
  156. }
  157. bool SeekableStream::process_single()
  158. {
  159. FLAC__ASSERT(is_valid());
  160. return (bool)::FLAC__seekable_stream_decoder_process_single(decoder_);
  161. }
  162. bool SeekableStream::process_until_end_of_metadata()
  163. {
  164. FLAC__ASSERT(is_valid());
  165. return (bool)::FLAC__seekable_stream_decoder_process_until_end_of_metadata(decoder_);
  166. }
  167. bool SeekableStream::process_until_end_of_stream()
  168. {
  169. FLAC__ASSERT(is_valid());
  170. return (bool)::FLAC__seekable_stream_decoder_process_until_end_of_stream(decoder_);
  171. }
  172. bool SeekableStream::skip_single_frame()
  173. {
  174. FLAC__ASSERT(is_valid());
  175. return (bool)::FLAC__seekable_stream_decoder_skip_single_frame(decoder_);
  176. }
  177. bool SeekableStream::seek_absolute(FLAC__uint64 sample)
  178. {
  179. FLAC__ASSERT(is_valid());
  180. return (bool)::FLAC__seekable_stream_decoder_seek_absolute(decoder_, sample);
  181. }
  182. ::FLAC__SeekableStreamDecoderReadStatus SeekableStream::read_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  183. {
  184. (void) decoder;
  185. FLAC__ASSERT(0 != client_data);
  186. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  187. FLAC__ASSERT(0 != instance);
  188. return instance->read_callback(buffer, bytes);
  189. }
  190. ::FLAC__SeekableStreamDecoderSeekStatus SeekableStream::seek_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  191. {
  192. (void) decoder;
  193. FLAC__ASSERT(0 != client_data);
  194. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  195. FLAC__ASSERT(0 != instance);
  196. return instance->seek_callback(absolute_byte_offset);
  197. }
  198. ::FLAC__SeekableStreamDecoderTellStatus SeekableStream::tell_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  199. {
  200. (void) decoder;
  201. FLAC__ASSERT(0 != client_data);
  202. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  203. FLAC__ASSERT(0 != instance);
  204. return instance->tell_callback(absolute_byte_offset);
  205. }
  206. ::FLAC__SeekableStreamDecoderLengthStatus SeekableStream::length_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
  207. {
  208. (void) decoder;
  209. FLAC__ASSERT(0 != client_data);
  210. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  211. FLAC__ASSERT(0 != instance);
  212. return instance->length_callback(stream_length);
  213. }
  214. FLAC__bool SeekableStream::eof_callback_(const ::FLAC__SeekableStreamDecoder *decoder, void *client_data)
  215. {
  216. (void) decoder;
  217. FLAC__ASSERT(0 != client_data);
  218. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  219. FLAC__ASSERT(0 != instance);
  220. return instance->eof_callback();
  221. }
  222. ::FLAC__StreamDecoderWriteStatus SeekableStream::write_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  223. {
  224. (void) decoder;
  225. FLAC__ASSERT(0 != client_data);
  226. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  227. FLAC__ASSERT(0 != instance);
  228. return instance->write_callback(frame, buffer);
  229. }
  230. void SeekableStream::metadata_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
  231. {
  232. (void) decoder;
  233. FLAC__ASSERT(0 != client_data);
  234. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  235. FLAC__ASSERT(0 != instance);
  236. instance->metadata_callback(metadata);
  237. }
  238. void SeekableStream::error_callback_(const ::FLAC__SeekableStreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
  239. {
  240. (void) decoder;
  241. FLAC__ASSERT(0 != client_data);
  242. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  243. FLAC__ASSERT(0 != instance);
  244. instance->error_callback(status);
  245. }
  246. }
  247. }