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

Windows CE

开发平台:

C/C++

  1. /* test_libOggFLAC++ - Unit tester for libOggFLAC++
  2.  * Copyright (C) 2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. #include "encoders.h"
  19. extern "C" {
  20. #include "file_utils.h"
  21. #include "metadata_utils.h"
  22. }
  23. #include "FLAC/assert.h"
  24. #include "OggFLAC++/encoder.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, unknown_;
  29. static ::FLAC__StreamMetadata *metadata_sequence_[] = { &vorbiscomment_, &padding_, &seektable_, &application1_, &application2_, &cuesheet_, &unknown_ };
  30. static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
  31. static const char *oggflacfilename_ = "metadata.ogg";
  32. static void init_metadata_blocks_()
  33. {
  34. mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
  35. }
  36. static void free_metadata_blocks_()
  37. {
  38. mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
  39. }
  40. class StreamEncoder : public OggFLAC::Encoder::Stream {
  41. public:
  42. StreamEncoder(): OggFLAC::Encoder::Stream() { }
  43. ~StreamEncoder() { }
  44. // from OggFLAC::Encoder::Stream
  45. ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame);
  46. void metadata_callback(const ::FLAC__StreamMetadata *metadata);
  47. bool die(const char *msg = 0) const;
  48. };
  49. ::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
  50. {
  51. (void)buffer, (void)bytes, (void)samples, (void)current_frame;
  52. return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  53. }
  54. void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
  55. {
  56. (void)metadata;
  57. }
  58. bool StreamEncoder::die(const char *msg) const
  59. {
  60. State state = get_state();
  61. if(msg)
  62. printf("FAILED, %s", msg);
  63. else
  64. printf("FAILED");
  65. printf(", state = %u (%s)n", (unsigned)((::OggFLAC__StreamEncoderState)state), state.as_cstring());
  66. if(state == ::OggFLAC__STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR) {
  67. FLAC::Encoder::Stream::State state_ = get_FLAC_stream_encoder_state();
  68. printf("      FLAC stream encoder state = %u (%s)n", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
  69. if(state_ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
  70. FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
  71. printf("      verify decoder state = %u (%s)n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
  72. }
  73. }
  74. return false;
  75. }
  76. static bool test_stream_encoder()
  77. {
  78. StreamEncoder *encoder;
  79. FLAC__int32 samples[1024];
  80. FLAC__int32 *samples_array[1] = { samples };
  81. unsigned i;
  82. printf("n+++ libOggFLAC++ unit test: OggFLAC::Encoder::Streamnn");
  83. printf("allocating encoder instance... ");
  84. encoder = new StreamEncoder();
  85. if(0 == encoder) {
  86. printf("FAILED, new returned NULLn");
  87. return false;
  88. }
  89. printf("OKn");
  90. printf("testing is_valid()... ");
  91. if(!encoder->is_valid()) {
  92. printf("FAILED, returned falsen");
  93. return false;
  94. }
  95. printf("OKn");
  96. printf("testing set_serial_number()... ");
  97. if(!encoder->set_serial_number(file_utils__serial_number))
  98. return encoder->die("returned false");
  99. printf("OKn");
  100. printf("testing set_verify()... ");
  101. if(!encoder->set_verify(true))
  102. return encoder->die("returned false");
  103. printf("OKn");
  104. printf("testing set_streamable_subset()... ");
  105. if(!encoder->set_streamable_subset(true))
  106. return encoder->die("returned false");
  107. printf("OKn");
  108. printf("testing set_do_mid_side_stereo()... ");
  109. if(!encoder->set_do_mid_side_stereo(false))
  110. return encoder->die("returned false");
  111. printf("OKn");
  112. printf("testing set_loose_mid_side_stereo()... ");
  113. if(!encoder->set_loose_mid_side_stereo(false))
  114. return encoder->die("returned false");
  115. printf("OKn");
  116. printf("testing set_channels()... ");
  117. if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
  118. return encoder->die("returned false");
  119. printf("OKn");
  120. printf("testing set_bits_per_sample()... ");
  121. if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
  122. return encoder->die("returned false");
  123. printf("OKn");
  124. printf("testing set_sample_rate()... ");
  125. if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
  126. return encoder->die("returned false");
  127. printf("OKn");
  128. printf("testing set_blocksize()... ");
  129. if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
  130. return encoder->die("returned false");
  131. printf("OKn");
  132. printf("testing set_max_lpc_order()... ");
  133. if(!encoder->set_max_lpc_order(0))
  134. return encoder->die("returned false");
  135. printf("OKn");
  136. printf("testing set_qlp_coeff_precision()... ");
  137. if(!encoder->set_qlp_coeff_precision(0))
  138. return encoder->die("returned false");
  139. printf("OKn");
  140. printf("testing set_do_qlp_coeff_prec_search()... ");
  141. if(!encoder->set_do_qlp_coeff_prec_search(false))
  142. return encoder->die("returned false");
  143. printf("OKn");
  144. printf("testing set_do_escape_coding()... ");
  145. if(!encoder->set_do_escape_coding(false))
  146. return encoder->die("returned false");
  147. printf("OKn");
  148. printf("testing set_do_exhaustive_model_search()... ");
  149. if(!encoder->set_do_exhaustive_model_search(false))
  150. return encoder->die("returned false");
  151. printf("OKn");
  152. printf("testing set_min_residual_partition_order()... ");
  153. if(!encoder->set_min_residual_partition_order(0))
  154. return encoder->die("returned false");
  155. printf("OKn");
  156. printf("testing set_max_residual_partition_order()... ");
  157. if(!encoder->set_max_residual_partition_order(0))
  158. return encoder->die("returned false");
  159. printf("OKn");
  160. printf("testing set_rice_parameter_search_dist()... ");
  161. if(!encoder->set_rice_parameter_search_dist(0))
  162. return encoder->die("returned false");
  163. printf("OKn");
  164. printf("testing set_total_samples_estimate()... ");
  165. if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
  166. return encoder->die("returned false");
  167. printf("OKn");
  168. printf("testing set_metadata()... ");
  169. if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
  170. return encoder->die("returned false");
  171. printf("OKn");
  172. printf("testing init()... ");
  173. if(encoder->init() != ::OggFLAC__STREAM_ENCODER_OK)
  174. return encoder->die();
  175. printf("OKn");
  176. printf("testing get_state()... ");
  177. OggFLAC::Encoder::Stream::State state = encoder->get_state();
  178. printf("returned state = %u (%s)... OKn", (unsigned)((::OggFLAC__StreamEncoderState)state), state.as_cstring());
  179. printf("testing get_FLAC_stream_encoder_state()... ");
  180. FLAC::Encoder::Stream::State state_ = encoder->get_FLAC_stream_encoder_state();
  181. printf("returned state = %u (%s)... OKn", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
  182. printf("testing get_verify_decoder_state()... ");
  183. FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
  184. printf("returned state = %u (%s)... OKn", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
  185. {
  186. FLAC__uint64 absolute_sample;
  187. unsigned frame_number;
  188. unsigned channel;
  189. unsigned sample;
  190. FLAC__int32 expected;
  191. FLAC__int32 got;
  192. printf("testing get_verify_decoder_error_stats()... ");
  193. encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
  194. printf("OKn");
  195. }
  196. printf("testing get_verify()... ");
  197. if(encoder->get_verify() != true) {
  198. printf("FAILED, expected true, got falsen");
  199. return false;
  200. }
  201. printf("OKn");
  202. printf("testing get_streamable_subset()... ");
  203. if(encoder->get_streamable_subset() != true) {
  204. printf("FAILED, expected true, got falsen");
  205. return false;
  206. }
  207. printf("OKn");
  208. printf("testing get_do_mid_side_stereo()... ");
  209. if(encoder->get_do_mid_side_stereo() != false) {
  210. printf("FAILED, expected false, got truen");
  211. return false;
  212. }
  213. printf("OKn");
  214. printf("testing get_loose_mid_side_stereo()... ");
  215. if(encoder->get_loose_mid_side_stereo() != false) {
  216. printf("FAILED, expected false, got truen");
  217. return false;
  218. }
  219. printf("OKn");
  220. printf("testing get_channels()... ");
  221. if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
  222. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.channels, encoder->get_channels());
  223. return false;
  224. }
  225. printf("OKn");
  226. printf("testing get_bits_per_sample()... ");
  227. if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
  228. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
  229. return false;
  230. }
  231. printf("OKn");
  232. printf("testing get_sample_rate()... ");
  233. if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
  234. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
  235. return false;
  236. }
  237. printf("OKn");
  238. printf("testing get_blocksize()... ");
  239. if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
  240. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
  241. return false;
  242. }
  243. printf("OKn");
  244. printf("testing get_max_lpc_order()... ");
  245. if(encoder->get_max_lpc_order() != 0) {
  246. printf("FAILED, expected %u, got %un", 0, encoder->get_max_lpc_order());
  247. return false;
  248. }
  249. printf("OKn");
  250. printf("testing get_qlp_coeff_precision()... ");
  251. (void)encoder->get_qlp_coeff_precision();
  252. /* we asked the encoder to auto select this so we accept anything */
  253. printf("OKn");
  254. printf("testing get_do_qlp_coeff_prec_search()... ");
  255. if(encoder->get_do_qlp_coeff_prec_search() != false) {
  256. printf("FAILED, expected false, got truen");
  257. return false;
  258. }
  259. printf("OKn");
  260. printf("testing get_do_escape_coding()... ");
  261. if(encoder->get_do_escape_coding() != false) {
  262. printf("FAILED, expected false, got truen");
  263. return false;
  264. }
  265. printf("OKn");
  266. printf("testing get_do_exhaustive_model_search()... ");
  267. if(encoder->get_do_exhaustive_model_search() != false) {
  268. printf("FAILED, expected false, got truen");
  269. return false;
  270. }
  271. printf("OKn");
  272. printf("testing get_min_residual_partition_order()... ");
  273. if(encoder->get_min_residual_partition_order() != 0) {
  274. printf("FAILED, expected %u, got %un", 0, encoder->get_min_residual_partition_order());
  275. return false;
  276. }
  277. printf("OKn");
  278. printf("testing get_max_residual_partition_order()... ");
  279. if(encoder->get_max_residual_partition_order() != 0) {
  280. printf("FAILED, expected %u, got %un", 0, encoder->get_max_residual_partition_order());
  281. return false;
  282. }
  283. printf("OKn");
  284. printf("testing get_rice_parameter_search_dist()... ");
  285. if(encoder->get_rice_parameter_search_dist() != 0) {
  286. printf("FAILED, expected %u, got %un", 0, encoder->get_rice_parameter_search_dist());
  287. return false;
  288. }
  289. printf("OKn");
  290. printf("testing get_total_samples_estimate()... ");
  291. if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
  292. printf("FAILED, expected %llu, got %llun", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
  293. return false;
  294. }
  295. printf("OKn");
  296. /* init the dummy sample buffer */
  297. for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
  298. samples[i] = i & 7;
  299. printf("testing process()... ");
  300. if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
  301. return encoder->die("returned false");
  302. printf("OKn");
  303. printf("testing process_interleaved()... ");
  304. if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
  305. return encoder->die("returned false");
  306. printf("OKn");
  307. printf("testing finish()... ");
  308. encoder->finish();
  309. printf("OKn");
  310. printf("freeing encoder instance... ");
  311. delete encoder;
  312. printf("OKn");
  313. printf("nPASSED!n");
  314. return true;
  315. }
  316. class SeekableStreamEncoder : public OggFLAC::Encoder::SeekableStream {
  317. public:
  318. SeekableStreamEncoder(): OggFLAC::Encoder::SeekableStream() { }
  319. ~SeekableStreamEncoder() { }
  320. // from OggFLAC::Encoder::SeekableStream
  321. ::OggFLAC__SeekableStreamEncoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes);
  322. ::FLAC__SeekableStreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
  323. ::FLAC__SeekableStreamEncoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
  324. ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame);
  325. bool die(const char *msg = 0) const;
  326. };
  327. ::OggFLAC__SeekableStreamEncoderReadStatus SeekableStreamEncoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
  328. {
  329. (void)buffer, (void)bytes;
  330. ::memset(buffer, 0, *bytes); /* init buffer to avoid valgrind errors */
  331. return ::OggFLAC__SEEKABLE_STREAM_ENCODER_READ_STATUS_CONTINUE;
  332. }
  333. ::FLAC__SeekableStreamEncoderSeekStatus SeekableStreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset)
  334. {
  335. (void)absolute_byte_offset;
  336. return ::FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
  337. }
  338. ::FLAC__SeekableStreamEncoderTellStatus SeekableStreamEncoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
  339. {
  340. (void)absolute_byte_offset;
  341. return ::FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK;
  342. }
  343. ::FLAC__StreamEncoderWriteStatus SeekableStreamEncoder::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
  344. {
  345. (void)buffer, (void)bytes, (void)samples, (void)current_frame;
  346. return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  347. }
  348. bool SeekableStreamEncoder::die(const char *msg) const
  349. {
  350. State state = get_state();
  351. if(msg)
  352. printf("FAILED, %s", msg);
  353. else
  354. printf("FAILED");
  355. printf(", state = %u (%s)n", (unsigned)((::OggFLAC__SeekableStreamEncoderState)state), state.as_cstring());
  356. if(state == ::OggFLAC__SEEKABLE_STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR) {
  357. FLAC::Encoder::Stream::State state_ = get_FLAC_stream_encoder_state();
  358. printf("      FLAC stream encoder state = %u (%s)n", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
  359. if(state_ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
  360. FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
  361. printf("      verify decoder state = %u (%s)n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
  362. }
  363. }
  364. return false;
  365. }
  366. static bool test_seekable_stream_encoder()
  367. {
  368. SeekableStreamEncoder *encoder;
  369. FLAC__int32 samples[1024];
  370. FLAC__int32 *samples_array[1] = { samples };
  371. unsigned i;
  372. printf("n+++ libOggFLAC++ unit test: OggFLAC::Encoder::SeekableStreamnn");
  373. printf("allocating encoder instance... ");
  374. encoder = new SeekableStreamEncoder();
  375. if(0 == encoder) {
  376. printf("FAILED, new returned NULLn");
  377. return false;
  378. }
  379. printf("OKn");
  380. printf("testing is_valid()... ");
  381. if(!encoder->is_valid()) {
  382. printf("FAILED, returned falsen");
  383. return false;
  384. }
  385. printf("OKn");
  386. printf("testing set_serial_number()... ");
  387. if(!encoder->set_serial_number(file_utils__serial_number))
  388. return encoder->die("returned false");
  389. printf("OKn");
  390. printf("testing set_verify()... ");
  391. if(!encoder->set_verify(true))
  392. return encoder->die("returned false");
  393. printf("OKn");
  394. printf("testing set_streamable_subset()... ");
  395. if(!encoder->set_streamable_subset(true))
  396. return encoder->die("returned false");
  397. printf("OKn");
  398. printf("testing set_do_mid_side_stereo()... ");
  399. if(!encoder->set_do_mid_side_stereo(false))
  400. return encoder->die("returned false");
  401. printf("OKn");
  402. printf("testing set_loose_mid_side_stereo()... ");
  403. if(!encoder->set_loose_mid_side_stereo(false))
  404. return encoder->die("returned false");
  405. printf("OKn");
  406. printf("testing set_channels()... ");
  407. if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
  408. return encoder->die("returned false");
  409. printf("OKn");
  410. printf("testing set_bits_per_sample()... ");
  411. if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
  412. return encoder->die("returned false");
  413. printf("OKn");
  414. printf("testing set_sample_rate()... ");
  415. if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
  416. return encoder->die("returned false");
  417. printf("OKn");
  418. printf("testing set_blocksize()... ");
  419. if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
  420. return encoder->die("returned false");
  421. printf("OKn");
  422. printf("testing set_max_lpc_order()... ");
  423. if(!encoder->set_max_lpc_order(0))
  424. return encoder->die("returned false");
  425. printf("OKn");
  426. printf("testing set_qlp_coeff_precision()... ");
  427. if(!encoder->set_qlp_coeff_precision(0))
  428. return encoder->die("returned false");
  429. printf("OKn");
  430. printf("testing set_do_qlp_coeff_prec_search()... ");
  431. if(!encoder->set_do_qlp_coeff_prec_search(false))
  432. return encoder->die("returned false");
  433. printf("OKn");
  434. printf("testing set_do_escape_coding()... ");
  435. if(!encoder->set_do_escape_coding(false))
  436. return encoder->die("returned false");
  437. printf("OKn");
  438. printf("testing set_do_exhaustive_model_search()... ");
  439. if(!encoder->set_do_exhaustive_model_search(false))
  440. return encoder->die("returned false");
  441. printf("OKn");
  442. printf("testing set_min_residual_partition_order()... ");
  443. if(!encoder->set_min_residual_partition_order(0))
  444. return encoder->die("returned false");
  445. printf("OKn");
  446. printf("testing set_max_residual_partition_order()... ");
  447. if(!encoder->set_max_residual_partition_order(0))
  448. return encoder->die("returned false");
  449. printf("OKn");
  450. printf("testing set_rice_parameter_search_dist()... ");
  451. if(!encoder->set_rice_parameter_search_dist(0))
  452. return encoder->die("returned false");
  453. printf("OKn");
  454. printf("testing set_total_samples_estimate()... ");
  455. if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
  456. return encoder->die("returned false");
  457. printf("OKn");
  458. printf("testing set_metadata()... ");
  459. if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
  460. return encoder->die("returned false");
  461. printf("OKn");
  462. printf("testing init()... ");
  463. if(encoder->init() != ::OggFLAC__SEEKABLE_STREAM_ENCODER_OK)
  464. return encoder->die();
  465. printf("OKn");
  466. printf("testing get_state()... ");
  467. OggFLAC::Encoder::SeekableStream::State state = encoder->get_state();
  468. printf("returned state = %u (%s)... OKn", (unsigned)((::OggFLAC__SeekableStreamEncoderState)state), state.as_cstring());
  469. printf("testing get_FLAC_stream_encoder_state()... ");
  470. FLAC::Encoder::Stream::State state_ = encoder->get_FLAC_stream_encoder_state();
  471. printf("returned state = %u (%s)... OKn", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
  472. printf("testing get_verify_decoder_state()... ");
  473. FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
  474. printf("returned state = %u (%s)... OKn", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
  475. {
  476. FLAC__uint64 absolute_sample;
  477. unsigned frame_number;
  478. unsigned channel;
  479. unsigned sample;
  480. FLAC__int32 expected;
  481. FLAC__int32 got;
  482. printf("testing get_verify_decoder_error_stats()... ");
  483. encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
  484. printf("OKn");
  485. }
  486. printf("testing get_verify()... ");
  487. if(encoder->get_verify() != true) {
  488. printf("FAILED, expected true, got falsen");
  489. return false;
  490. }
  491. printf("OKn");
  492. printf("testing get_streamable_subset()... ");
  493. if(encoder->get_streamable_subset() != true) {
  494. printf("FAILED, expected true, got falsen");
  495. return false;
  496. }
  497. printf("OKn");
  498. printf("testing get_do_mid_side_stereo()... ");
  499. if(encoder->get_do_mid_side_stereo() != false) {
  500. printf("FAILED, expected false, got truen");
  501. return false;
  502. }
  503. printf("OKn");
  504. printf("testing get_loose_mid_side_stereo()... ");
  505. if(encoder->get_loose_mid_side_stereo() != false) {
  506. printf("FAILED, expected false, got truen");
  507. return false;
  508. }
  509. printf("OKn");
  510. printf("testing get_channels()... ");
  511. if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
  512. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.channels, encoder->get_channels());
  513. return false;
  514. }
  515. printf("OKn");
  516. printf("testing get_bits_per_sample()... ");
  517. if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
  518. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
  519. return false;
  520. }
  521. printf("OKn");
  522. printf("testing get_sample_rate()... ");
  523. if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
  524. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
  525. return false;
  526. }
  527. printf("OKn");
  528. printf("testing get_blocksize()... ");
  529. if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
  530. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
  531. return false;
  532. }
  533. printf("OKn");
  534. printf("testing get_max_lpc_order()... ");
  535. if(encoder->get_max_lpc_order() != 0) {
  536. printf("FAILED, expected %u, got %un", 0, encoder->get_max_lpc_order());
  537. return false;
  538. }
  539. printf("OKn");
  540. printf("testing get_qlp_coeff_precision()... ");
  541. (void)encoder->get_qlp_coeff_precision();
  542. /* we asked the encoder to auto select this so we accept anything */
  543. printf("OKn");
  544. printf("testing get_do_qlp_coeff_prec_search()... ");
  545. if(encoder->get_do_qlp_coeff_prec_search() != false) {
  546. printf("FAILED, expected false, got truen");
  547. return false;
  548. }
  549. printf("OKn");
  550. printf("testing get_do_escape_coding()... ");
  551. if(encoder->get_do_escape_coding() != false) {
  552. printf("FAILED, expected false, got truen");
  553. return false;
  554. }
  555. printf("OKn");
  556. printf("testing get_do_exhaustive_model_search()... ");
  557. if(encoder->get_do_exhaustive_model_search() != false) {
  558. printf("FAILED, expected false, got truen");
  559. return false;
  560. }
  561. printf("OKn");
  562. printf("testing get_min_residual_partition_order()... ");
  563. if(encoder->get_min_residual_partition_order() != 0) {
  564. printf("FAILED, expected %u, got %un", 0, encoder->get_min_residual_partition_order());
  565. return false;
  566. }
  567. printf("OKn");
  568. printf("testing get_max_residual_partition_order()... ");
  569. if(encoder->get_max_residual_partition_order() != 0) {
  570. printf("FAILED, expected %u, got %un", 0, encoder->get_max_residual_partition_order());
  571. return false;
  572. }
  573. printf("OKn");
  574. printf("testing get_rice_parameter_search_dist()... ");
  575. if(encoder->get_rice_parameter_search_dist() != 0) {
  576. printf("FAILED, expected %u, got %un", 0, encoder->get_rice_parameter_search_dist());
  577. return false;
  578. }
  579. printf("OKn");
  580. printf("testing get_total_samples_estimate()... ");
  581. if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
  582. printf("FAILED, expected %llu, got %llun", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
  583. return false;
  584. }
  585. printf("OKn");
  586. /* init the dummy sample buffer */
  587. for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
  588. samples[i] = i & 7;
  589. printf("testing process()... ");
  590. if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
  591. return encoder->die("returned false");
  592. printf("OKn");
  593. printf("testing process_interleaved()... ");
  594. if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
  595. return encoder->die("returned false");
  596. printf("OKn");
  597. printf("testing finish()... ");
  598. encoder->finish();
  599. printf("OKn");
  600. printf("freeing encoder instance... ");
  601. delete encoder;
  602. printf("OKn");
  603. printf("nPASSED!n");
  604. return true;
  605. }
  606. class FileEncoder : public OggFLAC::Encoder::File {
  607. public:
  608. FileEncoder(): OggFLAC::Encoder::File() { }
  609. ~FileEncoder() { }
  610. // from OggFLAC::Encoder::File
  611. void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
  612. bool die(const char *msg = 0) const;
  613. };
  614. void FileEncoder::progress_callback(FLAC__uint64, FLAC__uint64, unsigned, unsigned)
  615. {
  616. }
  617. bool FileEncoder::die(const char *msg) const
  618. {
  619. State state = get_state();
  620. if(msg)
  621. printf("FAILED, %s", msg);
  622. else
  623. printf("FAILED");
  624. printf(", state = %u (%s)n", (unsigned)((::OggFLAC__FileEncoderState)state), state.as_cstring());
  625. if(state == ::OggFLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR) {
  626. OggFLAC::Encoder::SeekableStream::State state_ = get_seekable_stream_encoder_state();
  627. printf("      seekable stream encoder state = %u (%s)n", (unsigned)((::OggFLAC__SeekableStreamEncoderState)state_), state_.as_cstring());
  628. if(state_ == ::OggFLAC__SEEKABLE_STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR) {
  629. FLAC::Encoder::Stream::State state__ = get_FLAC_stream_encoder_state();
  630. printf("      FLAC stream encoder state = %u (%s)n", (unsigned)((::FLAC__StreamEncoderState)state__), state__.as_cstring());
  631. if(state__ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
  632. FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
  633. printf("      verify decoder state = %u (%s)n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
  634. }
  635. }
  636. }
  637. return false;
  638. }
  639. static bool test_file_encoder()
  640. {
  641. FileEncoder *encoder;
  642. FLAC__int32 samples[1024];
  643. FLAC__int32 *samples_array[1] = { samples };
  644. unsigned i;
  645. printf("n+++ libOggFLAC++ unit test: OggFLAC::Encoder::Filenn");
  646. printf("allocating encoder instance... ");
  647. encoder = new FileEncoder();
  648. if(0 == encoder) {
  649. printf("FAILED, new returned NULLn");
  650. return false;
  651. }
  652. printf("OKn");
  653. printf("testing is_valid()... ");
  654. if(!encoder->is_valid()) {
  655. printf("FAILED, returned falsen");
  656. return false;
  657. }
  658. printf("OKn");
  659. printf("testing set_serial_number()... ");
  660. if(!encoder->set_serial_number(file_utils__serial_number))
  661. return encoder->die("returned false");
  662. printf("OKn");
  663. printf("testing set_verify()... ");
  664. if(!encoder->set_verify(true))
  665. return encoder->die("returned false");
  666. printf("OKn");
  667. printf("testing set_streamable_subset()... ");
  668. if(!encoder->set_streamable_subset(true))
  669. return encoder->die("returned false");
  670. printf("OKn");
  671. printf("testing set_do_mid_side_stereo()... ");
  672. if(!encoder->set_do_mid_side_stereo(false))
  673. return encoder->die("returned false");
  674. printf("OKn");
  675. printf("testing set_loose_mid_side_stereo()... ");
  676. if(!encoder->set_loose_mid_side_stereo(false))
  677. return encoder->die("returned false");
  678. printf("OKn");
  679. printf("testing set_channels()... ");
  680. if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
  681. return encoder->die("returned false");
  682. printf("OKn");
  683. printf("testing set_bits_per_sample()... ");
  684. if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
  685. return encoder->die("returned false");
  686. printf("OKn");
  687. printf("testing set_sample_rate()... ");
  688. if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
  689. return encoder->die("returned false");
  690. printf("OKn");
  691. printf("testing set_blocksize()... ");
  692. if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
  693. return encoder->die("returned false");
  694. printf("OKn");
  695. printf("testing set_max_lpc_order()... ");
  696. if(!encoder->set_max_lpc_order(0))
  697. return encoder->die("returned false");
  698. printf("OKn");
  699. printf("testing set_qlp_coeff_precision()... ");
  700. if(!encoder->set_qlp_coeff_precision(0))
  701. return encoder->die("returned false");
  702. printf("OKn");
  703. printf("testing set_do_qlp_coeff_prec_search()... ");
  704. if(!encoder->set_do_qlp_coeff_prec_search(false))
  705. return encoder->die("returned false");
  706. printf("OKn");
  707. printf("testing set_do_escape_coding()... ");
  708. if(!encoder->set_do_escape_coding(false))
  709. return encoder->die("returned false");
  710. printf("OKn");
  711. printf("testing set_do_exhaustive_model_search()... ");
  712. if(!encoder->set_do_exhaustive_model_search(false))
  713. return encoder->die("returned false");
  714. printf("OKn");
  715. printf("testing set_min_residual_partition_order()... ");
  716. if(!encoder->set_min_residual_partition_order(0))
  717. return encoder->die("returned false");
  718. printf("OKn");
  719. printf("testing set_max_residual_partition_order()... ");
  720. if(!encoder->set_max_residual_partition_order(0))
  721. return encoder->die("returned false");
  722. printf("OKn");
  723. printf("testing set_rice_parameter_search_dist()... ");
  724. if(!encoder->set_rice_parameter_search_dist(0))
  725. return encoder->die("returned false");
  726. printf("OKn");
  727. printf("testing set_total_samples_estimate()... ");
  728. if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
  729. return encoder->die("returned false");
  730. printf("OKn");
  731. printf("testing set_metadata()... ");
  732. if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
  733. return encoder->die("returned false");
  734. printf("OKn");
  735. printf("testing set_filename()... ");
  736. if(!encoder->set_filename(oggflacfilename_))
  737. return encoder->die("returned false");
  738. printf("OKn");
  739. printf("testing init()... ");
  740. if(encoder->init() != ::OggFLAC__FILE_ENCODER_OK)
  741. return encoder->die();
  742. printf("OKn");
  743. printf("testing get_state()... ");
  744. OggFLAC::Encoder::File::State state = encoder->get_state();
  745. printf("returned state = %u (%s)... OKn", (unsigned)((::OggFLAC__FileEncoderState)state), state.as_cstring());
  746. printf("testing get_seekable_stream_encoder_state()... ");
  747. OggFLAC::Encoder::SeekableStream::State state_ = encoder->get_seekable_stream_encoder_state();
  748. printf("returned state = %u (%s)... OKn", (unsigned)((::OggFLAC__SeekableStreamEncoderState)state_), state_.as_cstring());
  749. printf("testing get_FLAC_stream_encoder_state()... ");
  750. FLAC::Encoder::Stream::State state__ = encoder->get_FLAC_stream_encoder_state();
  751. printf("returned state = %u (%s)... OKn", (unsigned)((::FLAC__StreamEncoderState)state__), state__.as_cstring());
  752. printf("testing get_verify_decoder_state()... ");
  753. FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
  754. printf("returned state = %u (%s)... OKn", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
  755. {
  756. FLAC__uint64 absolute_sample;
  757. unsigned frame_number;
  758. unsigned channel;
  759. unsigned sample;
  760. FLAC__int32 expected;
  761. FLAC__int32 got;
  762. printf("testing get_verify_decoder_error_stats()... ");
  763. encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
  764. printf("OKn");
  765. }
  766. printf("testing get_verify()... ");
  767. if(encoder->get_verify() != true) {
  768. printf("FAILED, expected true, got falsen");
  769. return false;
  770. }
  771. printf("OKn");
  772. printf("testing get_streamable_subset()... ");
  773. if(encoder->get_streamable_subset() != true) {
  774. printf("FAILED, expected true, got falsen");
  775. return false;
  776. }
  777. printf("OKn");
  778. printf("testing get_do_mid_side_stereo()... ");
  779. if(encoder->get_do_mid_side_stereo() != false) {
  780. printf("FAILED, expected false, got truen");
  781. return false;
  782. }
  783. printf("OKn");
  784. printf("testing get_loose_mid_side_stereo()... ");
  785. if(encoder->get_loose_mid_side_stereo() != false) {
  786. printf("FAILED, expected false, got truen");
  787. return false;
  788. }
  789. printf("OKn");
  790. printf("testing get_channels()... ");
  791. if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
  792. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.channels, encoder->get_channels());
  793. return false;
  794. }
  795. printf("OKn");
  796. printf("testing get_bits_per_sample()... ");
  797. if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
  798. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
  799. return false;
  800. }
  801. printf("OKn");
  802. printf("testing get_sample_rate()... ");
  803. if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
  804. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
  805. return false;
  806. }
  807. printf("OKn");
  808. printf("testing get_blocksize()... ");
  809. if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
  810. printf("FAILED, expected %u, got %un", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
  811. return false;
  812. }
  813. printf("OKn");
  814. printf("testing get_max_lpc_order()... ");
  815. if(encoder->get_max_lpc_order() != 0) {
  816. printf("FAILED, expected %u, got %un", 0, encoder->get_max_lpc_order());
  817. return false;
  818. }
  819. printf("OKn");
  820. printf("testing get_qlp_coeff_precision()... ");
  821. (void)encoder->get_qlp_coeff_precision();
  822. /* we asked the encoder to auto select this so we accept anything */
  823. printf("OKn");
  824. printf("testing get_do_qlp_coeff_prec_search()... ");
  825. if(encoder->get_do_qlp_coeff_prec_search() != false) {
  826. printf("FAILED, expected false, got truen");
  827. return false;
  828. }
  829. printf("OKn");
  830. printf("testing get_do_escape_coding()... ");
  831. if(encoder->get_do_escape_coding() != false) {
  832. printf("FAILED, expected false, got truen");
  833. return false;
  834. }
  835. printf("OKn");
  836. printf("testing get_do_exhaustive_model_search()... ");
  837. if(encoder->get_do_exhaustive_model_search() != false) {
  838. printf("FAILED, expected false, got truen");
  839. return false;
  840. }
  841. printf("OKn");
  842. printf("testing get_min_residual_partition_order()... ");
  843. if(encoder->get_min_residual_partition_order() != 0) {
  844. printf("FAILED, expected %u, got %un", 0, encoder->get_min_residual_partition_order());
  845. return false;
  846. }
  847. printf("OKn");
  848. printf("testing get_max_residual_partition_order()... ");
  849. if(encoder->get_max_residual_partition_order() != 0) {
  850. printf("FAILED, expected %u, got %un", 0, encoder->get_max_residual_partition_order());
  851. return false;
  852. }
  853. printf("OKn");
  854. printf("testing get_rice_parameter_search_dist()... ");
  855. if(encoder->get_rice_parameter_search_dist() != 0) {
  856. printf("FAILED, expected %u, got %un", 0, encoder->get_rice_parameter_search_dist());
  857. return false;
  858. }
  859. printf("OKn");
  860. printf("testing get_total_samples_estimate()... ");
  861. if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
  862. printf("FAILED, expected %llu, got %llun", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
  863. return false;
  864. }
  865. printf("OKn");
  866. /* init the dummy sample buffer */
  867. for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
  868. samples[i] = i & 7;
  869. printf("testing process()... ");
  870. if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
  871. return encoder->die("returned false");
  872. printf("OKn");
  873. printf("testing process_interleaved()... ");
  874. if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
  875. return encoder->die("returned false");
  876. printf("OKn");
  877. printf("testing finish()... ");
  878. encoder->finish();
  879. printf("OKn");
  880. printf("freeing encoder instance... ");
  881. delete encoder;
  882. printf("OKn");
  883. printf("nPASSED!n");
  884. return true;
  885. }
  886. bool test_encoders()
  887. {
  888. init_metadata_blocks_();
  889. if(!test_stream_encoder())
  890. return false;
  891. if(!test_seekable_stream_encoder())
  892. return false;
  893. if(!test_file_encoder())
  894. return false;
  895. free_metadata_blocks_();
  896. return true;
  897. }