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

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++/encoder.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 Encoder {
  39. SeekableStream::SeekableStream():
  40. encoder_(::FLAC__seekable_stream_encoder_new())
  41. { }
  42. SeekableStream::~SeekableStream()
  43. {
  44. if(0 != encoder_) {
  45. ::FLAC__seekable_stream_encoder_finish(encoder_);
  46. ::FLAC__seekable_stream_encoder_delete(encoder_);
  47. }
  48. }
  49. bool SeekableStream::is_valid() const
  50. {
  51. return 0 != encoder_;
  52. }
  53. bool SeekableStream::set_verify(bool value)
  54. {
  55. FLAC__ASSERT(is_valid());
  56. return (bool)::FLAC__seekable_stream_encoder_set_verify(encoder_, value);
  57. }
  58. bool SeekableStream::set_streamable_subset(bool value)
  59. {
  60. FLAC__ASSERT(is_valid());
  61. return (bool)::FLAC__seekable_stream_encoder_set_streamable_subset(encoder_, value);
  62. }
  63. bool SeekableStream::set_do_mid_side_stereo(bool value)
  64. {
  65. FLAC__ASSERT(is_valid());
  66. return (bool)::FLAC__seekable_stream_encoder_set_do_mid_side_stereo(encoder_, value);
  67. }
  68. bool SeekableStream::set_loose_mid_side_stereo(bool value)
  69. {
  70. FLAC__ASSERT(is_valid());
  71. return (bool)::FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(encoder_, value);
  72. }
  73. bool SeekableStream::set_channels(unsigned value)
  74. {
  75. FLAC__ASSERT(is_valid());
  76. return (bool)::FLAC__seekable_stream_encoder_set_channels(encoder_, value);
  77. }
  78. bool SeekableStream::set_bits_per_sample(unsigned value)
  79. {
  80. FLAC__ASSERT(is_valid());
  81. return (bool)::FLAC__seekable_stream_encoder_set_bits_per_sample(encoder_, value);
  82. }
  83. bool SeekableStream::set_sample_rate(unsigned value)
  84. {
  85. FLAC__ASSERT(is_valid());
  86. return (bool)::FLAC__seekable_stream_encoder_set_sample_rate(encoder_, value);
  87. }
  88. bool SeekableStream::set_blocksize(unsigned value)
  89. {
  90. FLAC__ASSERT(is_valid());
  91. return (bool)::FLAC__seekable_stream_encoder_set_blocksize(encoder_, value);
  92. }
  93. bool SeekableStream::set_max_lpc_order(unsigned value)
  94. {
  95. FLAC__ASSERT(is_valid());
  96. return (bool)::FLAC__seekable_stream_encoder_set_max_lpc_order(encoder_, value);
  97. }
  98. bool SeekableStream::set_qlp_coeff_precision(unsigned value)
  99. {
  100. FLAC__ASSERT(is_valid());
  101. return (bool)::FLAC__seekable_stream_encoder_set_qlp_coeff_precision(encoder_, value);
  102. }
  103. bool SeekableStream::set_do_qlp_coeff_prec_search(bool value)
  104. {
  105. FLAC__ASSERT(is_valid());
  106. return (bool)::FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
  107. }
  108. bool SeekableStream::set_do_escape_coding(bool value)
  109. {
  110. FLAC__ASSERT(is_valid());
  111. return (bool)::FLAC__seekable_stream_encoder_set_do_escape_coding(encoder_, value);
  112. }
  113. bool SeekableStream::set_do_exhaustive_model_search(bool value)
  114. {
  115. FLAC__ASSERT(is_valid());
  116. return (bool)::FLAC__seekable_stream_encoder_set_do_exhaustive_model_search(encoder_, value);
  117. }
  118. bool SeekableStream::set_min_residual_partition_order(unsigned value)
  119. {
  120. FLAC__ASSERT(is_valid());
  121. return (bool)::FLAC__seekable_stream_encoder_set_min_residual_partition_order(encoder_, value);
  122. }
  123. bool SeekableStream::set_max_residual_partition_order(unsigned value)
  124. {
  125. FLAC__ASSERT(is_valid());
  126. return (bool)::FLAC__seekable_stream_encoder_set_max_residual_partition_order(encoder_, value);
  127. }
  128. bool SeekableStream::set_rice_parameter_search_dist(unsigned value)
  129. {
  130. FLAC__ASSERT(is_valid());
  131. return (bool)::FLAC__seekable_stream_encoder_set_rice_parameter_search_dist(encoder_, value);
  132. }
  133. bool SeekableStream::set_total_samples_estimate(FLAC__uint64 value)
  134. {
  135. FLAC__ASSERT(is_valid());
  136. return (bool)::FLAC__seekable_stream_encoder_set_total_samples_estimate(encoder_, value);
  137. }
  138. bool SeekableStream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
  139. {
  140. FLAC__ASSERT(is_valid());
  141. return (bool)::FLAC__seekable_stream_encoder_set_metadata(encoder_, metadata, num_blocks);
  142. }
  143. bool SeekableStream::set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks)
  144. {
  145. FLAC__ASSERT(is_valid());
  146. #if (defined _MSC_VER) || (defined __SUNPRO_CC)
  147. // MSVC++ can't handle:
  148. // ::FLAC__StreamMetadata *m[num_blocks];
  149. // so we do this ugly workaround
  150. ::FLAC__StreamMetadata **m = new ::FLAC__StreamMetadata*[num_blocks];
  151. #else
  152. ::FLAC__StreamMetadata *m[num_blocks];
  153. #endif
  154. for(unsigned i = 0; i < num_blocks; i++) {
  155. // we can get away with this since we know the encoder will only correct the is_last flags
  156. m[i] = const_cast< ::FLAC__StreamMetadata*>((::FLAC__StreamMetadata*)metadata[i]);
  157. }
  158. #if (defined _MSC_VER) || (defined __SUNPRO_CC)
  159. // complete the hack
  160. const bool ok = (bool)::FLAC__seekable_stream_encoder_set_metadata(encoder_, m, num_blocks);
  161. delete [] m;
  162. return ok;
  163. #else
  164. return (bool)::FLAC__seekable_stream_encoder_set_metadata(encoder_, m, num_blocks);
  165. #endif
  166. }
  167. SeekableStream::State SeekableStream::get_state() const
  168. {
  169. FLAC__ASSERT(is_valid());
  170. return State(::FLAC__seekable_stream_encoder_get_state(encoder_));
  171. }
  172. Stream::State SeekableStream::get_stream_encoder_state() const
  173. {
  174. FLAC__ASSERT(is_valid());
  175. return Stream::State(::FLAC__seekable_stream_encoder_get_stream_encoder_state(encoder_));
  176. }
  177. Decoder::Stream::State SeekableStream::get_verify_decoder_state() const
  178. {
  179. FLAC__ASSERT(is_valid());
  180. return Decoder::Stream::State(::FLAC__seekable_stream_encoder_get_verify_decoder_state(encoder_));
  181. }
  182. void SeekableStream::get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
  183. {
  184. FLAC__ASSERT(is_valid());
  185. ::FLAC__seekable_stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
  186. }
  187. bool SeekableStream::get_verify() const
  188. {
  189. FLAC__ASSERT(is_valid());
  190. return (bool)::FLAC__seekable_stream_encoder_get_verify(encoder_);
  191. }
  192. bool SeekableStream::get_streamable_subset() const
  193. {
  194. FLAC__ASSERT(is_valid());
  195. return (bool)::FLAC__seekable_stream_encoder_get_streamable_subset(encoder_);
  196. }
  197. bool SeekableStream::get_do_mid_side_stereo() const
  198. {
  199. FLAC__ASSERT(is_valid());
  200. return (bool)::FLAC__seekable_stream_encoder_get_do_mid_side_stereo(encoder_);
  201. }
  202. bool SeekableStream::get_loose_mid_side_stereo() const
  203. {
  204. FLAC__ASSERT(is_valid());
  205. return (bool)::FLAC__seekable_stream_encoder_get_loose_mid_side_stereo(encoder_);
  206. }
  207. unsigned SeekableStream::get_channels() const
  208. {
  209. FLAC__ASSERT(is_valid());
  210. return ::FLAC__seekable_stream_encoder_get_channels(encoder_);
  211. }
  212. unsigned SeekableStream::get_bits_per_sample() const
  213. {
  214. FLAC__ASSERT(is_valid());
  215. return ::FLAC__seekable_stream_encoder_get_bits_per_sample(encoder_);
  216. }
  217. unsigned SeekableStream::get_sample_rate() const
  218. {
  219. FLAC__ASSERT(is_valid());
  220. return ::FLAC__seekable_stream_encoder_get_sample_rate(encoder_);
  221. }
  222. unsigned SeekableStream::get_blocksize() const
  223. {
  224. FLAC__ASSERT(is_valid());
  225. return ::FLAC__seekable_stream_encoder_get_blocksize(encoder_);
  226. }
  227. unsigned SeekableStream::get_max_lpc_order() const
  228. {
  229. FLAC__ASSERT(is_valid());
  230. return ::FLAC__seekable_stream_encoder_get_max_lpc_order(encoder_);
  231. }
  232. unsigned SeekableStream::get_qlp_coeff_precision() const
  233. {
  234. FLAC__ASSERT(is_valid());
  235. return ::FLAC__seekable_stream_encoder_get_qlp_coeff_precision(encoder_);
  236. }
  237. bool SeekableStream::get_do_qlp_coeff_prec_search() const
  238. {
  239. FLAC__ASSERT(is_valid());
  240. return (bool)::FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
  241. }
  242. bool SeekableStream::get_do_escape_coding() const
  243. {
  244. FLAC__ASSERT(is_valid());
  245. return (bool)::FLAC__seekable_stream_encoder_get_do_escape_coding(encoder_);
  246. }
  247. bool SeekableStream::get_do_exhaustive_model_search() const
  248. {
  249. FLAC__ASSERT(is_valid());
  250. return (bool)::FLAC__seekable_stream_encoder_get_do_exhaustive_model_search(encoder_);
  251. }
  252. unsigned SeekableStream::get_min_residual_partition_order() const
  253. {
  254. FLAC__ASSERT(is_valid());
  255. return ::FLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder_);
  256. }
  257. unsigned SeekableStream::get_max_residual_partition_order() const
  258. {
  259. FLAC__ASSERT(is_valid());
  260. return ::FLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder_);
  261. }
  262. unsigned SeekableStream::get_rice_parameter_search_dist() const
  263. {
  264. FLAC__ASSERT(is_valid());
  265. return ::FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder_);
  266. }
  267. FLAC__uint64 SeekableStream::get_total_samples_estimate() const
  268. {
  269. FLAC__ASSERT(is_valid());
  270. return ::FLAC__seekable_stream_encoder_get_total_samples_estimate(encoder_);
  271. }
  272. SeekableStream::State SeekableStream::init()
  273. {
  274. FLAC__ASSERT(is_valid());
  275. ::FLAC__seekable_stream_encoder_set_seek_callback(encoder_, seek_callback_);
  276. ::FLAC__seekable_stream_encoder_set_tell_callback(encoder_, tell_callback_);
  277. ::FLAC__seekable_stream_encoder_set_write_callback(encoder_, write_callback_);
  278. ::FLAC__seekable_stream_encoder_set_client_data(encoder_, (void*)this);
  279. return State(::FLAC__seekable_stream_encoder_init(encoder_));
  280. }
  281. void SeekableStream::finish()
  282. {
  283. FLAC__ASSERT(is_valid());
  284. ::FLAC__seekable_stream_encoder_finish(encoder_);
  285. }
  286. bool SeekableStream::process(const FLAC__int32 * const buffer[], unsigned samples)
  287. {
  288. FLAC__ASSERT(is_valid());
  289. return (bool)::FLAC__seekable_stream_encoder_process(encoder_, buffer, samples);
  290. }
  291. bool SeekableStream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
  292. {
  293. FLAC__ASSERT(is_valid());
  294. return (bool)::FLAC__seekable_stream_encoder_process_interleaved(encoder_, buffer, samples);
  295. }
  296. ::FLAC__SeekableStreamEncoderSeekStatus SeekableStream::seek_callback_(const ::FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  297. {
  298. (void)encoder;
  299. FLAC__ASSERT(0 != client_data);
  300. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  301. FLAC__ASSERT(0 != instance);
  302. return instance->seek_callback(absolute_byte_offset);
  303. }
  304. ::FLAC__SeekableStreamEncoderTellStatus SeekableStream::tell_callback_(const ::FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  305. {
  306. (void)encoder;
  307. FLAC__ASSERT(0 != client_data);
  308. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  309. FLAC__ASSERT(0 != instance);
  310. return instance->tell_callback(absolute_byte_offset);
  311. }
  312. ::FLAC__StreamEncoderWriteStatus SeekableStream::write_callback_(const ::FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
  313. {
  314. (void)encoder;
  315. FLAC__ASSERT(0 != client_data);
  316. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  317. FLAC__ASSERT(0 != instance);
  318. return instance->write_callback(buffer, bytes, samples, current_frame);
  319. }
  320. }
  321. }