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

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. Stream::Stream():
  40. encoder_(::OggFLAC__stream_encoder_new())
  41. { }
  42. Stream::~Stream()
  43. {
  44. if(0 != encoder_) {
  45. ::OggFLAC__stream_encoder_finish(encoder_);
  46. ::OggFLAC__stream_encoder_delete(encoder_);
  47. }
  48. }
  49. bool Stream::is_valid() const
  50. {
  51. return 0 != encoder_;
  52. }
  53. bool Stream::set_serial_number(long value)
  54. {
  55. FLAC__ASSERT(is_valid());
  56. return (bool)::OggFLAC__stream_encoder_set_serial_number(encoder_, value);
  57. }
  58. bool Stream::set_verify(bool value)
  59. {
  60. FLAC__ASSERT(is_valid());
  61. return (bool)::OggFLAC__stream_encoder_set_verify(encoder_, value);
  62. }
  63. bool Stream::set_streamable_subset(bool value)
  64. {
  65. FLAC__ASSERT(is_valid());
  66. return (bool)::OggFLAC__stream_encoder_set_streamable_subset(encoder_, value);
  67. }
  68. bool Stream::set_do_mid_side_stereo(bool value)
  69. {
  70. FLAC__ASSERT(is_valid());
  71. return (bool)::OggFLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
  72. }
  73. bool Stream::set_loose_mid_side_stereo(bool value)
  74. {
  75. FLAC__ASSERT(is_valid());
  76. return (bool)::OggFLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
  77. }
  78. bool Stream::set_channels(unsigned value)
  79. {
  80. FLAC__ASSERT(is_valid());
  81. return (bool)::OggFLAC__stream_encoder_set_channels(encoder_, value);
  82. }
  83. bool Stream::set_bits_per_sample(unsigned value)
  84. {
  85. FLAC__ASSERT(is_valid());
  86. return (bool)::OggFLAC__stream_encoder_set_bits_per_sample(encoder_, value);
  87. }
  88. bool Stream::set_sample_rate(unsigned value)
  89. {
  90. FLAC__ASSERT(is_valid());
  91. return (bool)::OggFLAC__stream_encoder_set_sample_rate(encoder_, value);
  92. }
  93. bool Stream::set_blocksize(unsigned value)
  94. {
  95. FLAC__ASSERT(is_valid());
  96. return (bool)::OggFLAC__stream_encoder_set_blocksize(encoder_, value);
  97. }
  98. bool Stream::set_max_lpc_order(unsigned value)
  99. {
  100. FLAC__ASSERT(is_valid());
  101. return (bool)::OggFLAC__stream_encoder_set_max_lpc_order(encoder_, value);
  102. }
  103. bool Stream::set_qlp_coeff_precision(unsigned value)
  104. {
  105. FLAC__ASSERT(is_valid());
  106. return (bool)::OggFLAC__stream_encoder_set_qlp_coeff_precision(encoder_, value);
  107. }
  108. bool Stream::set_do_qlp_coeff_prec_search(bool value)
  109. {
  110. FLAC__ASSERT(is_valid());
  111. return (bool)::OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
  112. }
  113. bool Stream::set_do_escape_coding(bool value)
  114. {
  115. FLAC__ASSERT(is_valid());
  116. return (bool)::OggFLAC__stream_encoder_set_do_escape_coding(encoder_, value);
  117. }
  118. bool Stream::set_do_exhaustive_model_search(bool value)
  119. {
  120. FLAC__ASSERT(is_valid());
  121. return (bool)::OggFLAC__stream_encoder_set_do_exhaustive_model_search(encoder_, value);
  122. }
  123. bool Stream::set_min_residual_partition_order(unsigned value)
  124. {
  125. FLAC__ASSERT(is_valid());
  126. return (bool)::OggFLAC__stream_encoder_set_min_residual_partition_order(encoder_, value);
  127. }
  128. bool Stream::set_max_residual_partition_order(unsigned value)
  129. {
  130. FLAC__ASSERT(is_valid());
  131. return (bool)::OggFLAC__stream_encoder_set_max_residual_partition_order(encoder_, value);
  132. }
  133. bool Stream::set_rice_parameter_search_dist(unsigned value)
  134. {
  135. FLAC__ASSERT(is_valid());
  136. return (bool)::OggFLAC__stream_encoder_set_rice_parameter_search_dist(encoder_, value);
  137. }
  138. bool Stream::set_total_samples_estimate(FLAC__uint64 value)
  139. {
  140. FLAC__ASSERT(is_valid());
  141. return (bool)::OggFLAC__stream_encoder_set_total_samples_estimate(encoder_, value);
  142. }
  143. bool Stream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
  144. {
  145. FLAC__ASSERT(is_valid());
  146. return (bool)::OggFLAC__stream_encoder_set_metadata(encoder_, metadata, num_blocks);
  147. }
  148. Stream::State Stream::get_state() const
  149. {
  150. FLAC__ASSERT(is_valid());
  151. return State(::OggFLAC__stream_encoder_get_state(encoder_));
  152. }
  153. FLAC::Encoder::Stream::State Stream::get_FLAC_stream_encoder_state() const
  154. {
  155. FLAC__ASSERT(is_valid());
  156. return FLAC::Encoder::Stream::State(::OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(encoder_));
  157. }
  158. FLAC::Decoder::Stream::State Stream::get_verify_decoder_state() const
  159. {
  160. FLAC__ASSERT(is_valid());
  161. return FLAC::Decoder::Stream::State(::OggFLAC__stream_encoder_get_verify_decoder_state(encoder_));
  162. }
  163. void Stream::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__stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
  167. }
  168. bool Stream::get_verify() const
  169. {
  170. FLAC__ASSERT(is_valid());
  171. return (bool)::OggFLAC__stream_encoder_get_verify(encoder_);
  172. }
  173. bool Stream::get_streamable_subset() const
  174. {
  175. FLAC__ASSERT(is_valid());
  176. return (bool)::OggFLAC__stream_encoder_get_streamable_subset(encoder_);
  177. }
  178. bool Stream::get_do_mid_side_stereo() const
  179. {
  180. FLAC__ASSERT(is_valid());
  181. return (bool)::OggFLAC__stream_encoder_get_do_mid_side_stereo(encoder_);
  182. }
  183. bool Stream::get_loose_mid_side_stereo() const
  184. {
  185. FLAC__ASSERT(is_valid());
  186. return (bool)::OggFLAC__stream_encoder_get_loose_mid_side_stereo(encoder_);
  187. }
  188. unsigned Stream::get_channels() const
  189. {
  190. FLAC__ASSERT(is_valid());
  191. return ::OggFLAC__stream_encoder_get_channels(encoder_);
  192. }
  193. unsigned Stream::get_bits_per_sample() const
  194. {
  195. FLAC__ASSERT(is_valid());
  196. return ::OggFLAC__stream_encoder_get_bits_per_sample(encoder_);
  197. }
  198. unsigned Stream::get_sample_rate() const
  199. {
  200. FLAC__ASSERT(is_valid());
  201. return ::OggFLAC__stream_encoder_get_sample_rate(encoder_);
  202. }
  203. unsigned Stream::get_blocksize() const
  204. {
  205. FLAC__ASSERT(is_valid());
  206. return ::OggFLAC__stream_encoder_get_blocksize(encoder_);
  207. }
  208. unsigned Stream::get_max_lpc_order() const
  209. {
  210. FLAC__ASSERT(is_valid());
  211. return ::OggFLAC__stream_encoder_get_max_lpc_order(encoder_);
  212. }
  213. unsigned Stream::get_qlp_coeff_precision() const
  214. {
  215. FLAC__ASSERT(is_valid());
  216. return ::OggFLAC__stream_encoder_get_qlp_coeff_precision(encoder_);
  217. }
  218. bool Stream::get_do_qlp_coeff_prec_search() const
  219. {
  220. FLAC__ASSERT(is_valid());
  221. return (bool)::OggFLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
  222. }
  223. bool Stream::get_do_escape_coding() const
  224. {
  225. FLAC__ASSERT(is_valid());
  226. return (bool)::OggFLAC__stream_encoder_get_do_escape_coding(encoder_);
  227. }
  228. bool Stream::get_do_exhaustive_model_search() const
  229. {
  230. FLAC__ASSERT(is_valid());
  231. return (bool)::OggFLAC__stream_encoder_get_do_exhaustive_model_search(encoder_);
  232. }
  233. unsigned Stream::get_min_residual_partition_order() const
  234. {
  235. FLAC__ASSERT(is_valid());
  236. return ::OggFLAC__stream_encoder_get_min_residual_partition_order(encoder_);
  237. }
  238. unsigned Stream::get_max_residual_partition_order() const
  239. {
  240. FLAC__ASSERT(is_valid());
  241. return ::OggFLAC__stream_encoder_get_max_residual_partition_order(encoder_);
  242. }
  243. unsigned Stream::get_rice_parameter_search_dist() const
  244. {
  245. FLAC__ASSERT(is_valid());
  246. return ::OggFLAC__stream_encoder_get_rice_parameter_search_dist(encoder_);
  247. }
  248. FLAC__uint64 Stream::get_total_samples_estimate() const
  249. {
  250. FLAC__ASSERT(is_valid());
  251. return ::OggFLAC__stream_encoder_get_total_samples_estimate(encoder_);
  252. }
  253. Stream::State Stream::init()
  254. {
  255. FLAC__ASSERT(is_valid());
  256. ::OggFLAC__stream_encoder_set_write_callback(encoder_, write_callback_);
  257. ::OggFLAC__stream_encoder_set_metadata_callback(encoder_, metadata_callback_);
  258. ::OggFLAC__stream_encoder_set_client_data(encoder_, (void*)this);
  259. return State(::OggFLAC__stream_encoder_init(encoder_));
  260. }
  261. void Stream::finish()
  262. {
  263. FLAC__ASSERT(is_valid());
  264. ::OggFLAC__stream_encoder_finish(encoder_);
  265. }
  266. bool Stream::process(const FLAC__int32 * const buffer[], unsigned samples)
  267. {
  268. FLAC__ASSERT(is_valid());
  269. return (bool)::OggFLAC__stream_encoder_process(encoder_, buffer, samples);
  270. }
  271. bool Stream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
  272. {
  273. FLAC__ASSERT(is_valid());
  274. return (bool)::OggFLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
  275. }
  276. ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
  277. {
  278. (void)encoder;
  279. FLAC__ASSERT(0 != client_data);
  280. Stream *instance = reinterpret_cast<Stream *>(client_data);
  281. FLAC__ASSERT(0 != instance);
  282. return instance->write_callback(buffer, bytes, samples, current_frame);
  283. }
  284. void Stream::metadata_callback_(const ::OggFLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
  285. {
  286. (void)encoder;
  287. FLAC__ASSERT(0 != client_data);
  288. Stream *instance = reinterpret_cast<Stream *>(client_data);
  289. FLAC__ASSERT(0 != instance);
  290. instance->metadata_callback(metadata);
  291. }
  292. }
  293. }