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

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 "OggFLAC++/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 OggFLAC {
  38. namespace Encoder {
  39. SeekableStream::SeekableStream():
  40. encoder_(::OggFLAC__seekable_stream_encoder_new())
  41. { }
  42. SeekableStream::~SeekableStream()
  43. {
  44. if(0 != encoder_) {
  45. ::OggFLAC__seekable_stream_encoder_finish(encoder_);
  46. ::OggFLAC__seekable_stream_encoder_delete(encoder_);
  47. }
  48. }
  49. bool SeekableStream::set_serial_number(long value)
  50. {
  51. FLAC__ASSERT(is_valid());
  52. return (bool)::OggFLAC__seekable_stream_encoder_set_serial_number(encoder_, value);
  53. }
  54. bool SeekableStream::is_valid() const
  55. {
  56. return 0 != encoder_;
  57. }
  58. bool SeekableStream::set_verify(bool value)
  59. {
  60. FLAC__ASSERT(is_valid());
  61. return (bool)::OggFLAC__seekable_stream_encoder_set_verify(encoder_, value);
  62. }
  63. bool SeekableStream::set_streamable_subset(bool value)
  64. {
  65. FLAC__ASSERT(is_valid());
  66. return (bool)::OggFLAC__seekable_stream_encoder_set_streamable_subset(encoder_, value);
  67. }
  68. bool SeekableStream::set_do_mid_side_stereo(bool value)
  69. {
  70. FLAC__ASSERT(is_valid());
  71. return (bool)::OggFLAC__seekable_stream_encoder_set_do_mid_side_stereo(encoder_, value);
  72. }
  73. bool SeekableStream::set_loose_mid_side_stereo(bool value)
  74. {
  75. FLAC__ASSERT(is_valid());
  76. return (bool)::OggFLAC__seekable_stream_encoder_set_loose_mid_side_stereo(encoder_, value);
  77. }
  78. bool SeekableStream::set_channels(unsigned value)
  79. {
  80. FLAC__ASSERT(is_valid());
  81. return (bool)::OggFLAC__seekable_stream_encoder_set_channels(encoder_, value);
  82. }
  83. bool SeekableStream::set_bits_per_sample(unsigned value)
  84. {
  85. FLAC__ASSERT(is_valid());
  86. return (bool)::OggFLAC__seekable_stream_encoder_set_bits_per_sample(encoder_, value);
  87. }
  88. bool SeekableStream::set_sample_rate(unsigned value)
  89. {
  90. FLAC__ASSERT(is_valid());
  91. return (bool)::OggFLAC__seekable_stream_encoder_set_sample_rate(encoder_, value);
  92. }
  93. bool SeekableStream::set_blocksize(unsigned value)
  94. {
  95. FLAC__ASSERT(is_valid());
  96. return (bool)::OggFLAC__seekable_stream_encoder_set_blocksize(encoder_, value);
  97. }
  98. bool SeekableStream::set_max_lpc_order(unsigned value)
  99. {
  100. FLAC__ASSERT(is_valid());
  101. return (bool)::OggFLAC__seekable_stream_encoder_set_max_lpc_order(encoder_, value);
  102. }
  103. bool SeekableStream::set_qlp_coeff_precision(unsigned value)
  104. {
  105. FLAC__ASSERT(is_valid());
  106. return (bool)::OggFLAC__seekable_stream_encoder_set_qlp_coeff_precision(encoder_, value);
  107. }
  108. bool SeekableStream::set_do_qlp_coeff_prec_search(bool value)
  109. {
  110. FLAC__ASSERT(is_valid());
  111. return (bool)::OggFLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
  112. }
  113. bool SeekableStream::set_do_escape_coding(bool value)
  114. {
  115. FLAC__ASSERT(is_valid());
  116. return (bool)::OggFLAC__seekable_stream_encoder_set_do_escape_coding(encoder_, value);
  117. }
  118. bool SeekableStream::set_do_exhaustive_model_search(bool value)
  119. {
  120. FLAC__ASSERT(is_valid());
  121. return (bool)::OggFLAC__seekable_stream_encoder_set_do_exhaustive_model_search(encoder_, value);
  122. }
  123. bool SeekableStream::set_min_residual_partition_order(unsigned value)
  124. {
  125. FLAC__ASSERT(is_valid());
  126. return (bool)::OggFLAC__seekable_stream_encoder_set_min_residual_partition_order(encoder_, value);
  127. }
  128. bool SeekableStream::set_max_residual_partition_order(unsigned value)
  129. {
  130. FLAC__ASSERT(is_valid());
  131. return (bool)::OggFLAC__seekable_stream_encoder_set_max_residual_partition_order(encoder_, value);
  132. }
  133. bool SeekableStream::set_rice_parameter_search_dist(unsigned value)
  134. {
  135. FLAC__ASSERT(is_valid());
  136. return (bool)::OggFLAC__seekable_stream_encoder_set_rice_parameter_search_dist(encoder_, value);
  137. }
  138. bool SeekableStream::set_total_samples_estimate(FLAC__uint64 value)
  139. {
  140. FLAC__ASSERT(is_valid());
  141. return (bool)::OggFLAC__seekable_stream_encoder_set_total_samples_estimate(encoder_, value);
  142. }
  143. bool SeekableStream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
  144. {
  145. FLAC__ASSERT(is_valid());
  146. return (bool)::OggFLAC__seekable_stream_encoder_set_metadata(encoder_, metadata, num_blocks);
  147. }
  148. SeekableStream::State SeekableStream::get_state() const
  149. {
  150. FLAC__ASSERT(is_valid());
  151. return State(::OggFLAC__seekable_stream_encoder_get_state(encoder_));
  152. }
  153. FLAC::Encoder::Stream::State SeekableStream::get_FLAC_stream_encoder_state() const
  154. {
  155. FLAC__ASSERT(is_valid());
  156. return FLAC::Encoder::Stream::State(::OggFLAC__seekable_stream_encoder_get_FLAC_stream_encoder_state(encoder_));
  157. }
  158. FLAC::Decoder::Stream::State SeekableStream::get_verify_decoder_state() const
  159. {
  160. FLAC__ASSERT(is_valid());
  161. return FLAC::Decoder::Stream::State(::OggFLAC__seekable_stream_encoder_get_verify_decoder_state(encoder_));
  162. }
  163. void SeekableStream::get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
  164. {
  165. FLAC__ASSERT(is_valid());
  166. ::OggFLAC__seekable_stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
  167. }
  168. bool SeekableStream::get_verify() const
  169. {
  170. FLAC__ASSERT(is_valid());
  171. return (bool)::OggFLAC__seekable_stream_encoder_get_verify(encoder_);
  172. }
  173. bool SeekableStream::get_streamable_subset() const
  174. {
  175. FLAC__ASSERT(is_valid());
  176. return (bool)::OggFLAC__seekable_stream_encoder_get_streamable_subset(encoder_);
  177. }
  178. bool SeekableStream::get_do_mid_side_stereo() const
  179. {
  180. FLAC__ASSERT(is_valid());
  181. return (bool)::OggFLAC__seekable_stream_encoder_get_do_mid_side_stereo(encoder_);
  182. }
  183. bool SeekableStream::get_loose_mid_side_stereo() const
  184. {
  185. FLAC__ASSERT(is_valid());
  186. return (bool)::OggFLAC__seekable_stream_encoder_get_loose_mid_side_stereo(encoder_);
  187. }
  188. unsigned SeekableStream::get_channels() const
  189. {
  190. FLAC__ASSERT(is_valid());
  191. return ::OggFLAC__seekable_stream_encoder_get_channels(encoder_);
  192. }
  193. unsigned SeekableStream::get_bits_per_sample() const
  194. {
  195. FLAC__ASSERT(is_valid());
  196. return ::OggFLAC__seekable_stream_encoder_get_bits_per_sample(encoder_);
  197. }
  198. unsigned SeekableStream::get_sample_rate() const
  199. {
  200. FLAC__ASSERT(is_valid());
  201. return ::OggFLAC__seekable_stream_encoder_get_sample_rate(encoder_);
  202. }
  203. unsigned SeekableStream::get_blocksize() const
  204. {
  205. FLAC__ASSERT(is_valid());
  206. return ::OggFLAC__seekable_stream_encoder_get_blocksize(encoder_);
  207. }
  208. unsigned SeekableStream::get_max_lpc_order() const
  209. {
  210. FLAC__ASSERT(is_valid());
  211. return ::OggFLAC__seekable_stream_encoder_get_max_lpc_order(encoder_);
  212. }
  213. unsigned SeekableStream::get_qlp_coeff_precision() const
  214. {
  215. FLAC__ASSERT(is_valid());
  216. return ::OggFLAC__seekable_stream_encoder_get_qlp_coeff_precision(encoder_);
  217. }
  218. bool SeekableStream::get_do_qlp_coeff_prec_search() const
  219. {
  220. FLAC__ASSERT(is_valid());
  221. return (bool)::OggFLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
  222. }
  223. bool SeekableStream::get_do_escape_coding() const
  224. {
  225. FLAC__ASSERT(is_valid());
  226. return (bool)::OggFLAC__seekable_stream_encoder_get_do_escape_coding(encoder_);
  227. }
  228. bool SeekableStream::get_do_exhaustive_model_search() const
  229. {
  230. FLAC__ASSERT(is_valid());
  231. return (bool)::OggFLAC__seekable_stream_encoder_get_do_exhaustive_model_search(encoder_);
  232. }
  233. unsigned SeekableStream::get_min_residual_partition_order() const
  234. {
  235. FLAC__ASSERT(is_valid());
  236. return ::OggFLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder_);
  237. }
  238. unsigned SeekableStream::get_max_residual_partition_order() const
  239. {
  240. FLAC__ASSERT(is_valid());
  241. return ::OggFLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder_);
  242. }
  243. unsigned SeekableStream::get_rice_parameter_search_dist() const
  244. {
  245. FLAC__ASSERT(is_valid());
  246. return ::OggFLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder_);
  247. }
  248. FLAC__uint64 SeekableStream::get_total_samples_estimate() const
  249. {
  250. FLAC__ASSERT(is_valid());
  251. return ::OggFLAC__seekable_stream_encoder_get_total_samples_estimate(encoder_);
  252. }
  253. SeekableStream::State SeekableStream::init()
  254. {
  255. FLAC__ASSERT(is_valid());
  256. ::OggFLAC__seekable_stream_encoder_set_read_callback(encoder_, read_callback_);
  257. ::OggFLAC__seekable_stream_encoder_set_seek_callback(encoder_, seek_callback_);
  258. ::OggFLAC__seekable_stream_encoder_set_tell_callback(encoder_, tell_callback_);
  259. ::OggFLAC__seekable_stream_encoder_set_write_callback(encoder_, write_callback_);
  260. ::OggFLAC__seekable_stream_encoder_set_client_data(encoder_, (void*)this);
  261. return State(::OggFLAC__seekable_stream_encoder_init(encoder_));
  262. }
  263. void SeekableStream::finish()
  264. {
  265. FLAC__ASSERT(is_valid());
  266. ::OggFLAC__seekable_stream_encoder_finish(encoder_);
  267. }
  268. bool SeekableStream::process(const FLAC__int32 * const buffer[], unsigned samples)
  269. {
  270. FLAC__ASSERT(is_valid());
  271. return (bool)::OggFLAC__seekable_stream_encoder_process(encoder_, buffer, samples);
  272. }
  273. bool SeekableStream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
  274. {
  275. FLAC__ASSERT(is_valid());
  276. return (bool)::OggFLAC__seekable_stream_encoder_process_interleaved(encoder_, buffer, samples);
  277. }
  278. ::OggFLAC__SeekableStreamEncoderReadStatus SeekableStream::read_callback_(const ::OggFLAC__SeekableStreamEncoder *encoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  279. {
  280. (void)encoder;
  281. FLAC__ASSERT(0 != client_data);
  282. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  283. FLAC__ASSERT(0 != instance);
  284. return instance->read_callback(buffer, bytes);
  285. }
  286. ::FLAC__SeekableStreamEncoderSeekStatus SeekableStream::seek_callback_(const ::OggFLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  287. {
  288. (void)encoder;
  289. FLAC__ASSERT(0 != client_data);
  290. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  291. FLAC__ASSERT(0 != instance);
  292. return instance->seek_callback(absolute_byte_offset);
  293. }
  294. ::FLAC__SeekableStreamEncoderTellStatus SeekableStream::tell_callback_(const ::OggFLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  295. {
  296. (void)encoder;
  297. FLAC__ASSERT(0 != client_data);
  298. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  299. FLAC__ASSERT(0 != instance);
  300. return instance->tell_callback(absolute_byte_offset);
  301. }
  302. ::FLAC__StreamEncoderWriteStatus SeekableStream::write_callback_(const ::OggFLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
  303. {
  304. (void)encoder;
  305. FLAC__ASSERT(0 != client_data);
  306. SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
  307. FLAC__ASSERT(0 != instance);
  308. return instance->write_callback(buffer, bytes, samples, current_frame);
  309. }
  310. }
  311. }