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

Windows CE

开发平台:

C/C++

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