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

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 "decoders.h"
  19. #include "file_utils.h"
  20. #include "metadata_utils.h"
  21. #include "FLAC/assert.h"
  22. #include "FLAC/file_decoder.h"
  23. #include "FLAC/seekable_stream_decoder.h"
  24. #include "FLAC/stream_decoder.h"
  25. #include "share/grabbag.h"
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. typedef struct {
  31. FILE *file;
  32. unsigned current_metadata_number;
  33. FLAC__bool ignore_errors;
  34. FLAC__bool error_occurred;
  35. } stream_decoder_client_data_struct;
  36. typedef stream_decoder_client_data_struct seekable_stream_decoder_client_data_struct;
  37. typedef stream_decoder_client_data_struct file_decoder_client_data_struct;
  38. static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, unknown_;
  39. static FLAC__StreamMetadata *expected_metadata_sequence_[8];
  40. static unsigned num_expected_;
  41. static const char *flacfilename_ = "metadata.flac";
  42. static unsigned flacfilesize_;
  43. static FLAC__bool die_(const char *msg)
  44. {
  45. printf("ERROR: %sn", msg);
  46. return false;
  47. }
  48. static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder)
  49. {
  50. FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder);
  51. if(msg)
  52. printf("FAILED, %s", msg);
  53. else
  54. printf("FAILED");
  55. printf(", state = %u (%s)n", (unsigned)state, FLAC__StreamDecoderStateString[state]);
  56. return false;
  57. }
  58. static FLAC__bool die_ss_(const char *msg, const FLAC__SeekableStreamDecoder *decoder)
  59. {
  60. FLAC__SeekableStreamDecoderState state = FLAC__seekable_stream_decoder_get_state(decoder);
  61. if(msg)
  62. printf("FAILED, %s", msg);
  63. else
  64. printf("FAILED");
  65. printf(", state = %u (%s)n", (unsigned)state, FLAC__SeekableStreamDecoderStateString[state]);
  66. if(state == FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
  67. FLAC__StreamDecoderState state_ = FLAC__seekable_stream_decoder_get_stream_decoder_state(decoder);
  68. printf("      stream decoder state = %u (%s)n", (unsigned)state_, FLAC__StreamDecoderStateString[state_]);
  69. }
  70. return false;
  71. }
  72. static FLAC__bool die_f_(const char *msg, const FLAC__FileDecoder *decoder)
  73. {
  74. FLAC__FileDecoderState state = FLAC__file_decoder_get_state(decoder);
  75. if(msg)
  76. printf("FAILED, %s", msg);
  77. else
  78. printf("FAILED");
  79. printf(", state = %u (%s)n", (unsigned)state, FLAC__FileDecoderStateString[state]);
  80. if(state == FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
  81. FLAC__SeekableStreamDecoderState state_ = FLAC__file_decoder_get_seekable_stream_decoder_state(decoder);
  82. printf("      seekable stream decoder state = %u (%s)n", (unsigned)state, FLAC__SeekableStreamDecoderStateString[state_]);
  83. if(state_ == FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
  84. FLAC__StreamDecoderState state__ = FLAC__file_decoder_get_stream_decoder_state(decoder);
  85. printf("      stream decoder state = %u (%s)n", (unsigned)state__, FLAC__StreamDecoderStateString[state__]);
  86. }
  87. }
  88. return false;
  89. }
  90. static void init_metadata_blocks_()
  91. {
  92. mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
  93. }
  94. static void free_metadata_blocks_()
  95. {
  96. mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
  97. }
  98. static FLAC__bool generate_file_()
  99. {
  100. printf("nngenerating FLAC file for decoder tests...n");
  101. num_expected_ = 0;
  102. expected_metadata_sequence_[num_expected_++] = &padding_;
  103. expected_metadata_sequence_[num_expected_++] = &seektable_;
  104. expected_metadata_sequence_[num_expected_++] = &application1_;
  105. expected_metadata_sequence_[num_expected_++] = &application2_;
  106. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  107. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  108. expected_metadata_sequence_[num_expected_++] = &unknown_;
  109. if(!file_utils__generate_flacfile(flacfilename_, &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_))
  110. return die_("creating the encoded file");
  111. return true;
  112. }
  113. static FLAC__StreamDecoderReadStatus stream_decoder_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  114. {
  115. stream_decoder_client_data_struct *dcd = (stream_decoder_client_data_struct*)client_data;
  116. const unsigned requested_bytes = *bytes;
  117. (void)decoder;
  118. if(0 == dcd) {
  119. printf("ERROR: client_data in read callback is NULLn");
  120. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  121. }
  122. if(dcd->error_occurred)
  123. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  124. if(feof(dcd->file)) {
  125. *bytes = 0;
  126. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  127. }
  128. else if(requested_bytes > 0) {
  129. *bytes = fread(buffer, 1, requested_bytes, dcd->file);
  130. if(*bytes == 0) {
  131. if(feof(dcd->file))
  132. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  133. else
  134. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  135. }
  136. else {
  137. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  138. }
  139. }
  140. else
  141. return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
  142. }
  143. static FLAC__StreamDecoderWriteStatus stream_decoder_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  144. {
  145. stream_decoder_client_data_struct *dcd = (stream_decoder_client_data_struct*)client_data;
  146. (void)decoder, (void)buffer;
  147. if(0 == dcd) {
  148. printf("ERROR: client_data in write callback is NULLn");
  149. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  150. }
  151. if(dcd->error_occurred)
  152. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  153. if(
  154. (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) ||
  155. (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0)
  156. ) {
  157. printf("content... ");
  158. fflush(stdout);
  159. }
  160. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  161. }
  162. static void stream_decoder_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  163. {
  164. stream_decoder_client_data_struct *dcd = (stream_decoder_client_data_struct*)client_data;
  165. (void)decoder;
  166. if(0 == dcd) {
  167. printf("ERROR: client_data in metadata callback is NULLn");
  168. return;
  169. }
  170. if(dcd->error_occurred)
  171. return;
  172. printf("%d... ", dcd->current_metadata_number);
  173. fflush(stdout);
  174. if(dcd->current_metadata_number >= num_expected_) {
  175. (void)die_("got more metadata blocks than expected");
  176. dcd->error_occurred = true;
  177. }
  178. else {
  179. if(!mutils__compare_block(expected_metadata_sequence_[dcd->current_metadata_number], metadata)) {
  180. (void)die_("metadata block mismatch");
  181. dcd->error_occurred = true;
  182. }
  183. }
  184. dcd->current_metadata_number++;
  185. }
  186. static void stream_decoder_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  187. {
  188. stream_decoder_client_data_struct *dcd = (stream_decoder_client_data_struct*)client_data;
  189. (void)decoder;
  190. if(0 == dcd) {
  191. printf("ERROR: client_data in error callback is NULLn");
  192. return;
  193. }
  194. if(!dcd->ignore_errors) {
  195. printf("ERROR: got error callback: err = %u (%s)n", (unsigned)status, FLAC__StreamDecoderErrorStatusString[status]);
  196. dcd->error_occurred = true;
  197. }
  198. }
  199. static FLAC__bool stream_decoder_test_respond_(FLAC__StreamDecoder *decoder, stream_decoder_client_data_struct *dcd)
  200. {
  201. if(!FLAC__stream_decoder_set_read_callback(decoder, stream_decoder_read_callback_))
  202. return die_s_("at FLAC__stream_decoder_set_read_callback(), returned false", decoder);
  203. if(!FLAC__stream_decoder_set_write_callback(decoder, stream_decoder_write_callback_))
  204. return die_s_("at FLAC__stream_decoder_set_write_callback(), returned false", decoder);
  205. if(!FLAC__stream_decoder_set_metadata_callback(decoder, stream_decoder_metadata_callback_))
  206. return die_s_("at FLAC__stream_decoder_set_metadata_callback(), returned false", decoder);
  207. if(!FLAC__stream_decoder_set_error_callback(decoder, stream_decoder_error_callback_))
  208. return die_s_("at FLAC__stream_decoder_set_error_callback(), returned false", decoder);
  209. if(!FLAC__stream_decoder_set_client_data(decoder, dcd))
  210. return die_s_("at FLAC__stream_decoder_set_client_data(), returned false", decoder);
  211. printf("testing FLAC__stream_decoder_init()... ");
  212. if(FLAC__stream_decoder_init(decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
  213. return die_s_(0, decoder);
  214. printf("OKn");
  215. dcd->current_metadata_number = 0;
  216. if(fseek(dcd->file, 0, SEEK_SET) < 0) {
  217. printf("FAILED rewinding input, errno = %dn", errno);
  218. return false;
  219. }
  220. printf("testing FLAC__stream_decoder_process_until_end_of_stream()... ");
  221. if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
  222. return die_s_("returned false", decoder);
  223. printf("OKn");
  224. printf("testing FLAC__stream_decoder_finish()... ");
  225. FLAC__stream_decoder_finish(decoder);
  226. printf("OKn");
  227. return true;
  228. }
  229. static FLAC__bool test_stream_decoder()
  230. {
  231. FLAC__StreamDecoder *decoder;
  232. FLAC__StreamDecoderState state;
  233. stream_decoder_client_data_struct decoder_client_data;
  234. printf("n+++ libFLAC unit test: FLAC__StreamDecodernn");
  235. printf("testing FLAC__stream_decoder_new()... ");
  236. decoder = FLAC__stream_decoder_new();
  237. if(0 == decoder) {
  238. printf("FAILED, returned NULLn");
  239. return false;
  240. }
  241. printf("OKn");
  242. printf("testing FLAC__stream_decoder_delete()... ");
  243. FLAC__stream_decoder_delete(decoder);
  244. printf("OKn");
  245. printf("testing FLAC__stream_decoder_new()... ");
  246. decoder = FLAC__stream_decoder_new();
  247. if(0 == decoder) {
  248. printf("FAILED, returned NULLn");
  249. return false;
  250. }
  251. printf("OKn");
  252. printf("testing FLAC__stream_decoder_init()... ");
  253. if(FLAC__stream_decoder_init(decoder) == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
  254. return die_s_(0, decoder);
  255. printf("OKn");
  256. printf("testing FLAC__stream_decoder_delete()... ");
  257. FLAC__stream_decoder_delete(decoder);
  258. printf("OKn");
  259. num_expected_ = 0;
  260. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  261. printf("testing FLAC__stream_decoder_new()... ");
  262. decoder = FLAC__stream_decoder_new();
  263. if(0 == decoder) {
  264. printf("FAILED, returned NULLn");
  265. return false;
  266. }
  267. printf("OKn");
  268. printf("testing FLAC__stream_decoder_set_read_callback()... ");
  269. if(!FLAC__stream_decoder_set_read_callback(decoder, stream_decoder_read_callback_))
  270. return die_s_("returned false", decoder);
  271. printf("OKn");
  272. printf("testing FLAC__stream_decoder_set_write_callback()... ");
  273. if(!FLAC__stream_decoder_set_write_callback(decoder, stream_decoder_write_callback_))
  274. return die_s_("returned false", decoder);
  275. printf("OKn");
  276. printf("testing FLAC__stream_decoder_set_metadata_callback()... ");
  277. if(!FLAC__stream_decoder_set_metadata_callback(decoder, stream_decoder_metadata_callback_))
  278. return die_s_("returned false", decoder);
  279. printf("OKn");
  280. printf("testing FLAC__stream_decoder_set_error_callback()... ");
  281. if(!FLAC__stream_decoder_set_error_callback(decoder, stream_decoder_error_callback_))
  282. return die_s_("returned false", decoder);
  283. printf("OKn");
  284. printf("testing FLAC__stream_decoder_set_client_data()... ");
  285. if(!FLAC__stream_decoder_set_client_data(decoder, &decoder_client_data))
  286. return die_s_("returned false", decoder);
  287. printf("OKn");
  288. printf("testing FLAC__stream_decoder_init()... ");
  289. if(FLAC__stream_decoder_init(decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
  290. return die_s_(0, decoder);
  291. printf("OKn");
  292. printf("testing FLAC__stream_decoder_get_state()... ");
  293. state = FLAC__stream_decoder_get_state(decoder);
  294. printf("returned state = %u (%s)... OKn", state, FLAC__StreamDecoderStateString[state]);
  295. decoder_client_data.current_metadata_number = 0;
  296. decoder_client_data.ignore_errors = false;
  297. decoder_client_data.error_occurred = false;
  298. printf("opening FLAC file... ");
  299. decoder_client_data.file = fopen(flacfilename_, "rb");
  300. if(0 == decoder_client_data.file) {
  301. printf("ERRORn");
  302. return false;
  303. }
  304. printf("OKn");
  305. printf("testing FLAC__stream_decoder_process_until_end_of_metadata()... ");
  306. if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
  307. return die_s_("returned false", decoder);
  308. printf("OKn");
  309. printf("testing FLAC__stream_decoder_process_single()... ");
  310. if(!FLAC__stream_decoder_process_single(decoder))
  311. return die_s_("returned false", decoder);
  312. printf("OKn");
  313. printf("testing FLAC__stream_decoder_skip_single_frame()... ");
  314. if(!FLAC__stream_decoder_skip_single_frame(decoder))
  315. return die_s_("returned false", decoder);
  316. printf("OKn");
  317. printf("testing FLAC__stream_decoder_flush()... ");
  318. if(!FLAC__stream_decoder_flush(decoder))
  319. return die_s_("returned false", decoder);
  320. printf("OKn");
  321. decoder_client_data.ignore_errors = true;
  322. printf("testing FLAC__stream_decoder_process_single()... ");
  323. if(!FLAC__stream_decoder_process_single(decoder))
  324. return die_s_("returned false", decoder);
  325. printf("OKn");
  326. decoder_client_data.ignore_errors = false;
  327. printf("testing FLAC__stream_decoder_process_until_end_of_stream()... ");
  328. if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
  329. return die_s_("returned false", decoder);
  330. printf("OKn");
  331. printf("testing FLAC__stream_decoder_get_channels()... ");
  332. {
  333. unsigned channels = FLAC__stream_decoder_get_channels(decoder);
  334. if(channels != streaminfo_.data.stream_info.channels) {
  335. printf("FAILED, returned %u, expected %un", channels, streaminfo_.data.stream_info.channels);
  336. return false;
  337. }
  338. }
  339. printf("OKn");
  340. printf("testing FLAC__stream_decoder_get_bits_per_sample()... ");
  341. {
  342. unsigned bits_per_sample = FLAC__stream_decoder_get_bits_per_sample(decoder);
  343. if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
  344. printf("FAILED, returned %u, expected %un", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
  345. return false;
  346. }
  347. }
  348. printf("OKn");
  349. printf("testing FLAC__stream_decoder_get_sample_rate()... ");
  350. {
  351. unsigned sample_rate = FLAC__stream_decoder_get_sample_rate(decoder);
  352. if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
  353. printf("FAILED, returned %u, expected %un", sample_rate, streaminfo_.data.stream_info.sample_rate);
  354. return false;
  355. }
  356. }
  357. printf("OKn");
  358. printf("testing FLAC__stream_decoder_get_blocksize()... ");
  359. {
  360. unsigned blocksize = FLAC__stream_decoder_get_blocksize(decoder);
  361. /* value could be anything since we're at the last block, so accept any answer */
  362. printf("returned %u... OKn", blocksize);
  363. }
  364. printf("testing FLAC__stream_decoder_get_channel_assignment()... ");
  365. {
  366. FLAC__ChannelAssignment ca = FLAC__stream_decoder_get_channel_assignment(decoder);
  367. printf("returned %u (%s)... OKn", (unsigned)ca, FLAC__ChannelAssignmentString[ca]);
  368. }
  369. printf("testing FLAC__stream_decoder_reset()... ");
  370. if(!FLAC__stream_decoder_reset(decoder)) {
  371. state = FLAC__stream_decoder_get_state(decoder);
  372. printf("FAILED, returned false, state = %u (%s)n", state, FLAC__StreamDecoderStateString[state]);
  373. return false;
  374. }
  375. printf("OKn");
  376. decoder_client_data.current_metadata_number = 0;
  377. printf("rewinding input... ");
  378. if(fseek(decoder_client_data.file, 0, SEEK_SET) < 0) {
  379. printf("FAILED, errno = %dn", errno);
  380. return false;
  381. }
  382. printf("OKn");
  383. printf("testing FLAC__stream_decoder_process_until_end_of_stream()... ");
  384. if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
  385. return die_s_("returned false", decoder);
  386. printf("OKn");
  387. printf("testing FLAC__stream_decoder_finish()... ");
  388. FLAC__stream_decoder_finish(decoder);
  389. printf("OKn");
  390. /*
  391.  * respond all
  392.  */
  393. printf("testing FLAC__stream_decoder_set_metadata_respond_all()... ");
  394. if(!FLAC__stream_decoder_set_metadata_respond_all(decoder))
  395. return die_s_("returned false", decoder);
  396. printf("OKn");
  397. num_expected_ = 0;
  398. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  399. expected_metadata_sequence_[num_expected_++] = &padding_;
  400. expected_metadata_sequence_[num_expected_++] = &seektable_;
  401. expected_metadata_sequence_[num_expected_++] = &application1_;
  402. expected_metadata_sequence_[num_expected_++] = &application2_;
  403. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  404. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  405. expected_metadata_sequence_[num_expected_++] = &unknown_;
  406. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  407. return false;
  408. /*
  409.  * ignore all
  410.  */
  411. printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... ");
  412. if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder))
  413. return die_s_("returned false", decoder);
  414. printf("OKn");
  415. num_expected_ = 0;
  416. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  417. return false;
  418. /*
  419.  * respond all, ignore VORBIS_COMMENT
  420.  */
  421. printf("testing FLAC__stream_decoder_set_metadata_respond_all()... ");
  422. if(!FLAC__stream_decoder_set_metadata_respond_all(decoder))
  423. return die_s_("returned false", decoder);
  424. printf("OKn");
  425. printf("testing FLAC__stream_decoder_set_metadata_ignore(VORBIS_COMMENT)... ");
  426. if(!FLAC__stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
  427. return die_s_("returned false", decoder);
  428. printf("OKn");
  429. num_expected_ = 0;
  430. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  431. expected_metadata_sequence_[num_expected_++] = &padding_;
  432. expected_metadata_sequence_[num_expected_++] = &seektable_;
  433. expected_metadata_sequence_[num_expected_++] = &application1_;
  434. expected_metadata_sequence_[num_expected_++] = &application2_;
  435. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  436. expected_metadata_sequence_[num_expected_++] = &unknown_;
  437. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  438. return false;
  439. /*
  440.  * respond all, ignore APPLICATION
  441.  */
  442. printf("testing FLAC__stream_decoder_set_metadata_respond_all()... ");
  443. if(!FLAC__stream_decoder_set_metadata_respond_all(decoder))
  444. return die_s_("returned false", decoder);
  445. printf("OKn");
  446. printf("testing FLAC__stream_decoder_set_metadata_ignore(APPLICATION)... ");
  447. if(!FLAC__stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
  448. return die_s_("returned false", decoder);
  449. printf("OKn");
  450. num_expected_ = 0;
  451. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  452. expected_metadata_sequence_[num_expected_++] = &padding_;
  453. expected_metadata_sequence_[num_expected_++] = &seektable_;
  454. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  455. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  456. expected_metadata_sequence_[num_expected_++] = &unknown_;
  457. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  458. return false;
  459. /*
  460.  * respond all, ignore APPLICATION id of app#1
  461.  */
  462. printf("testing FLAC__stream_decoder_set_metadata_respond_all()... ");
  463. if(!FLAC__stream_decoder_set_metadata_respond_all(decoder))
  464. return die_s_("returned false", decoder);
  465. printf("OKn");
  466. printf("testing FLAC__stream_decoder_set_metadata_ignore_application(of app block #1)... ");
  467. if(!FLAC__stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  468. return die_s_("returned false", decoder);
  469. printf("OKn");
  470. num_expected_ = 0;
  471. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  472. expected_metadata_sequence_[num_expected_++] = &padding_;
  473. expected_metadata_sequence_[num_expected_++] = &seektable_;
  474. expected_metadata_sequence_[num_expected_++] = &application2_;
  475. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  476. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  477. expected_metadata_sequence_[num_expected_++] = &unknown_;
  478. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  479. return false;
  480. /*
  481.  * respond all, ignore APPLICATION id of app#1 & app#2
  482.  */
  483. printf("testing FLAC__stream_decoder_set_metadata_respond_all()... ");
  484. if(!FLAC__stream_decoder_set_metadata_respond_all(decoder))
  485. return die_s_("returned false", decoder);
  486. printf("OKn");
  487. printf("testing FLAC__stream_decoder_set_metadata_ignore_application(of app block #1)... ");
  488. if(!FLAC__stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  489. return die_s_("returned false", decoder);
  490. printf("OKn");
  491. printf("testing FLAC__stream_decoder_set_metadata_ignore_application(of app block #2)... ");
  492. if(!FLAC__stream_decoder_set_metadata_ignore_application(decoder, application2_.data.application.id))
  493. return die_s_("returned false", decoder);
  494. printf("OKn");
  495. num_expected_ = 0;
  496. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  497. expected_metadata_sequence_[num_expected_++] = &padding_;
  498. expected_metadata_sequence_[num_expected_++] = &seektable_;
  499. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  500. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  501. expected_metadata_sequence_[num_expected_++] = &unknown_;
  502. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  503. return false;
  504. /*
  505.  * ignore all, respond VORBIS_COMMENT
  506.  */
  507. printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... ");
  508. if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder))
  509. return die_s_("returned false", decoder);
  510. printf("OKn");
  511. printf("testing FLAC__stream_decoder_set_metadata_respond(VORBIS_COMMENT)... ");
  512. if(!FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
  513. return die_s_("returned false", decoder);
  514. printf("OKn");
  515. num_expected_ = 0;
  516. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  517. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  518. return false;
  519. /*
  520.  * ignore all, respond APPLICATION
  521.  */
  522. printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... ");
  523. if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder))
  524. return die_s_("returned false", decoder);
  525. printf("OKn");
  526. printf("testing FLAC__stream_decoder_set_metadata_respond(APPLICATION)... ");
  527. if(!FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
  528. return die_s_("returned false", decoder);
  529. printf("OKn");
  530. num_expected_ = 0;
  531. expected_metadata_sequence_[num_expected_++] = &application1_;
  532. expected_metadata_sequence_[num_expected_++] = &application2_;
  533. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  534. return false;
  535. /*
  536.  * ignore all, respond APPLICATION id of app#1
  537.  */
  538. printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... ");
  539. if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder))
  540. return die_s_("returned false", decoder);
  541. printf("OKn");
  542. printf("testing FLAC__stream_decoder_set_metadata_respond_application(of app block #1)... ");
  543. if(!FLAC__stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  544. return die_s_("returned false", decoder);
  545. printf("OKn");
  546. num_expected_ = 0;
  547. expected_metadata_sequence_[num_expected_++] = &application1_;
  548. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  549. return false;
  550. /*
  551.  * ignore all, respond APPLICATION id of app#1 & app#2
  552.  */
  553. printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... ");
  554. if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder))
  555. return die_s_("returned false", decoder);
  556. printf("OKn");
  557. printf("testing FLAC__stream_decoder_set_metadata_respond_application(of app block #1)... ");
  558. if(!FLAC__stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  559. return die_s_("returned false", decoder);
  560. printf("OKn");
  561. printf("testing FLAC__stream_decoder_set_metadata_respond_application(of app block #2)... ");
  562. if(!FLAC__stream_decoder_set_metadata_respond_application(decoder, application2_.data.application.id))
  563. return die_s_("returned false", decoder);
  564. printf("OKn");
  565. num_expected_ = 0;
  566. expected_metadata_sequence_[num_expected_++] = &application1_;
  567. expected_metadata_sequence_[num_expected_++] = &application2_;
  568. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  569. return false;
  570. /*
  571.  * respond all, ignore APPLICATION, respond APPLICATION id of app#1
  572.  */
  573. printf("testing FLAC__stream_decoder_set_metadata_respond_all()... ");
  574. if(!FLAC__stream_decoder_set_metadata_respond_all(decoder))
  575. return die_s_("returned false", decoder);
  576. printf("OKn");
  577. printf("testing FLAC__stream_decoder_set_metadata_ignore(APPLICATION)... ");
  578. if(!FLAC__stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
  579. return die_s_("returned false", decoder);
  580. printf("OKn");
  581. printf("testing FLAC__stream_decoder_set_metadata_respond_application(of app block #1)... ");
  582. if(!FLAC__stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  583. return die_s_("returned false", decoder);
  584. printf("OKn");
  585. num_expected_ = 0;
  586. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  587. expected_metadata_sequence_[num_expected_++] = &padding_;
  588. expected_metadata_sequence_[num_expected_++] = &seektable_;
  589. expected_metadata_sequence_[num_expected_++] = &application1_;
  590. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  591. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  592. expected_metadata_sequence_[num_expected_++] = &unknown_;
  593. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  594. return false;
  595. /*
  596.  * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
  597.  */
  598. printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... ");
  599. if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder))
  600. return die_s_("returned false", decoder);
  601. printf("OKn");
  602. printf("testing FLAC__stream_decoder_set_metadata_respond(APPLICATION)... ");
  603. if(!FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
  604. return die_s_("returned false", decoder);
  605. printf("OKn");
  606. printf("testing FLAC__stream_decoder_set_metadata_ignore_application(of app block #1)... ");
  607. if(!FLAC__stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  608. return die_s_("returned false", decoder);
  609. printf("OKn");
  610. num_expected_ = 0;
  611. expected_metadata_sequence_[num_expected_++] = &application2_;
  612. if(!stream_decoder_test_respond_(decoder, &decoder_client_data))
  613. return false;
  614. /* done, now leave the sequence the way we found it... */
  615. num_expected_ = 0;
  616. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  617. expected_metadata_sequence_[num_expected_++] = &padding_;
  618. expected_metadata_sequence_[num_expected_++] = &seektable_;
  619. expected_metadata_sequence_[num_expected_++] = &application1_;
  620. expected_metadata_sequence_[num_expected_++] = &application2_;
  621. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  622. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  623. expected_metadata_sequence_[num_expected_++] = &unknown_;
  624. printf("testing FLAC__stream_decoder_delete()... ");
  625. FLAC__stream_decoder_delete(decoder);
  626. printf("OKn");
  627. fclose(decoder_client_data.file);
  628. printf("nPASSED!n");
  629. return true;
  630. }
  631. static FLAC__SeekableStreamDecoderReadStatus seekable_stream_decoder_read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  632. {
  633. (void)decoder;
  634. switch(stream_decoder_read_callback_(0, buffer, bytes, client_data)) {
  635. case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
  636. return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
  637. case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
  638. case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
  639. return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
  640. default:
  641. FLAC__ASSERT(0);
  642. return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
  643. }
  644. }
  645. static FLAC__SeekableStreamDecoderSeekStatus seekable_stream_decoder_seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  646. {
  647. seekable_stream_decoder_client_data_struct *dcd = (seekable_stream_decoder_client_data_struct*)client_data;
  648. (void)decoder;
  649. if(0 == dcd) {
  650. printf("ERROR: client_data in seek callback is NULLn");
  651. return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
  652. }
  653. if(dcd->error_occurred)
  654. return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
  655. if(fseek(dcd->file, (long)absolute_byte_offset, SEEK_SET) < 0) {
  656. dcd->error_occurred = true;
  657. return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
  658. }
  659. return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
  660. }
  661. static FLAC__SeekableStreamDecoderTellStatus seekable_stream_decoder_tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  662. {
  663. seekable_stream_decoder_client_data_struct *dcd = (seekable_stream_decoder_client_data_struct*)client_data;
  664. long offset;
  665. (void)decoder;
  666. if(0 == dcd) {
  667. printf("ERROR: client_data in tell callback is NULLn");
  668. return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
  669. }
  670. if(dcd->error_occurred)
  671. return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
  672. offset = ftell(dcd->file);
  673. *absolute_byte_offset = (FLAC__uint64)offset;
  674. if(offset < 0) {
  675. dcd->error_occurred = true;
  676. return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
  677. }
  678. return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
  679. }
  680. static FLAC__SeekableStreamDecoderLengthStatus seekable_stream_decoder_length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
  681. {
  682. seekable_stream_decoder_client_data_struct *dcd = (seekable_stream_decoder_client_data_struct*)client_data;
  683. (void)decoder;
  684. if(0 == dcd) {
  685. printf("ERROR: client_data in length callback is NULLn");
  686. return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
  687. }
  688. if(dcd->error_occurred)
  689. return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
  690. *stream_length = (FLAC__uint64)flacfilesize_;
  691. return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
  692. }
  693. static FLAC__bool seekable_stream_decoder_eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data)
  694. {
  695. seekable_stream_decoder_client_data_struct *dcd = (seekable_stream_decoder_client_data_struct*)client_data;
  696. (void)decoder;
  697. if(0 == dcd) {
  698. printf("ERROR: client_data in eof callback is NULLn");
  699. return true;
  700. }
  701. if(dcd->error_occurred)
  702. return true;
  703. return feof(dcd->file);
  704. }
  705. static FLAC__StreamDecoderWriteStatus seekable_stream_decoder_write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  706. {
  707. (void)decoder;
  708. return stream_decoder_write_callback_(0, frame, buffer, client_data);
  709. }
  710. static void seekable_stream_decoder_metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  711. {
  712. (void)decoder;
  713. stream_decoder_metadata_callback_(0, metadata, client_data);
  714. }
  715. static void seekable_stream_decoder_error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  716. {
  717. (void)decoder;
  718. stream_decoder_error_callback_(0, status, client_data);
  719. }
  720. static FLAC__bool seekable_stream_decoder_test_respond_(FLAC__SeekableStreamDecoder *decoder, seekable_stream_decoder_client_data_struct *dcd)
  721. {
  722. if(!FLAC__seekable_stream_decoder_set_read_callback(decoder, seekable_stream_decoder_read_callback_))
  723. return die_ss_("at FLAC__seekable_stream_decoder_set_read_callback(), returned false", decoder);
  724. if(!FLAC__seekable_stream_decoder_set_seek_callback(decoder, seekable_stream_decoder_seek_callback_))
  725. return die_ss_("at FLAC__seekable_stream_decoder_set_seek_callback(), returned false", decoder);
  726. if(!FLAC__seekable_stream_decoder_set_tell_callback(decoder, seekable_stream_decoder_tell_callback_))
  727. return die_ss_("at FLAC__seekable_stream_decoder_set_tell_callback(), returned false", decoder);
  728. if(!FLAC__seekable_stream_decoder_set_length_callback(decoder, seekable_stream_decoder_length_callback_))
  729. return die_ss_("at FLAC__seekable_stream_decoder_set_length_callback(), returned false", decoder);
  730. if(!FLAC__seekable_stream_decoder_set_eof_callback(decoder, seekable_stream_decoder_eof_callback_))
  731. return die_ss_("at FLAC__seekable_stream_decoder_set_eof_callback(), returned false", decoder);
  732. if(!FLAC__seekable_stream_decoder_set_write_callback(decoder, seekable_stream_decoder_write_callback_))
  733. return die_ss_("at FLAC__seekable_stream_decoder_set_write_callback(), returned false", decoder);
  734. if(!FLAC__seekable_stream_decoder_set_metadata_callback(decoder, seekable_stream_decoder_metadata_callback_))
  735. return die_ss_("at FLAC__seekable_stream_decoder_set_metadata_callback(), returned false", decoder);
  736. if(!FLAC__seekable_stream_decoder_set_error_callback(decoder, seekable_stream_decoder_error_callback_))
  737. return die_ss_("at FLAC__seekable_stream_decoder_set_error_callback(), returned false", decoder);
  738. if(!FLAC__seekable_stream_decoder_set_client_data(decoder, dcd))
  739. return die_ss_("at FLAC__seekable_stream_decoder_set_client_data(), returned false", decoder);
  740. if(!FLAC__seekable_stream_decoder_set_md5_checking(decoder, true))
  741. return die_ss_("at FLAC__seekable_stream_decoder_set_md5_checking(), returned false", decoder);
  742. printf("testing FLAC__seekable_stream_decoder_init()... ");
  743. if(FLAC__seekable_stream_decoder_init(decoder) != FLAC__SEEKABLE_STREAM_DECODER_OK)
  744. return die_ss_(0, decoder);
  745. printf("OKn");
  746. dcd->current_metadata_number = 0;
  747. if(fseek(dcd->file, 0, SEEK_SET) < 0) {
  748. printf("FAILED rewinding input, errno = %dn", errno);
  749. return false;
  750. }
  751. printf("testing FLAC__seekable_stream_decoder_process_until_end_of_stream()... ");
  752. if(!FLAC__seekable_stream_decoder_process_until_end_of_stream(decoder))
  753. return die_ss_("returned false", decoder);
  754. printf("OKn");
  755. printf("testing FLAC__seekable_stream_decoder_finish()... ");
  756. (void) FLAC__seekable_stream_decoder_finish(decoder);
  757. printf("OKn");
  758. return true;
  759. }
  760. static FLAC__bool test_seekable_stream_decoder()
  761. {
  762. FLAC__SeekableStreamDecoder *decoder;
  763. FLAC__SeekableStreamDecoderState state;
  764. FLAC__StreamDecoderState sstate;
  765. seekable_stream_decoder_client_data_struct decoder_client_data;
  766. printf("n+++ libFLAC unit test: FLAC__SeekableStreamDecodernn");
  767. printf("testing FLAC__seekable_stream_decoder_new()... ");
  768. decoder = FLAC__seekable_stream_decoder_new();
  769. if(0 == decoder) {
  770. printf("FAILED, returned NULLn");
  771. return false;
  772. }
  773. printf("OKn");
  774. printf("testing FLAC__seekable_stream_decoder_delete()... ");
  775. FLAC__seekable_stream_decoder_delete(decoder);
  776. printf("OKn");
  777. printf("testing FLAC__seekable_stream_decoder_new()... ");
  778. decoder = FLAC__seekable_stream_decoder_new();
  779. if(0 == decoder) {
  780. printf("FAILED, returned NULLn");
  781. return false;
  782. }
  783. printf("OKn");
  784. printf("testing FLAC__seekable_stream_decoder_init()... ");
  785. if(FLAC__seekable_stream_decoder_init(decoder) == FLAC__SEEKABLE_STREAM_DECODER_OK)
  786. return die_ss_(0, decoder);
  787. printf("OKn");
  788. printf("testing FLAC__seekable_stream_decoder_delete()... ");
  789. FLAC__seekable_stream_decoder_delete(decoder);
  790. printf("OKn");
  791. num_expected_ = 0;
  792. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  793. printf("testing FLAC__seekable_stream_decoder_new()... ");
  794. decoder = FLAC__seekable_stream_decoder_new();
  795. if(0 == decoder) {
  796. printf("FAILED, returned NULLn");
  797. return false;
  798. }
  799. printf("OKn");
  800. printf("testing FLAC__seekable_stream_decoder_set_read_callback()... ");
  801. if(!FLAC__seekable_stream_decoder_set_read_callback(decoder, seekable_stream_decoder_read_callback_))
  802. return die_ss_("returned false", decoder);
  803. printf("OKn");
  804. printf("testing FLAC__seekable_stream_decoder_set_seek_callback()... ");
  805. if(!FLAC__seekable_stream_decoder_set_seek_callback(decoder, seekable_stream_decoder_seek_callback_))
  806. return die_ss_("returned false", decoder);
  807. printf("OKn");
  808. printf("testing FLAC__seekable_stream_decoder_set_tell_callback()... ");
  809. if(!FLAC__seekable_stream_decoder_set_tell_callback(decoder, seekable_stream_decoder_tell_callback_))
  810. return die_ss_("returned false", decoder);
  811. printf("OKn");
  812. printf("testing FLAC__seekable_stream_decoder_set_length_callback()... ");
  813. if(!FLAC__seekable_stream_decoder_set_length_callback(decoder, seekable_stream_decoder_length_callback_))
  814. return die_ss_("returned false", decoder);
  815. printf("OKn");
  816. printf("testing FLAC__seekable_stream_decoder_set_eof_callback()... ");
  817. if(!FLAC__seekable_stream_decoder_set_eof_callback(decoder, seekable_stream_decoder_eof_callback_))
  818. return die_ss_("returned false", decoder);
  819. printf("OKn");
  820. printf("testing FLAC__seekable_stream_decoder_set_write_callback()... ");
  821. if(!FLAC__seekable_stream_decoder_set_write_callback(decoder, seekable_stream_decoder_write_callback_))
  822. return die_ss_("returned false", decoder);
  823. printf("OKn");
  824. printf("testing FLAC__seekable_stream_decoder_set_metadata_callback()... ");
  825. if(!FLAC__seekable_stream_decoder_set_metadata_callback(decoder, seekable_stream_decoder_metadata_callback_))
  826. return die_ss_("returned false", decoder);
  827. printf("OKn");
  828. printf("testing FLAC__seekable_stream_decoder_set_error_callback()... ");
  829. if(!FLAC__seekable_stream_decoder_set_error_callback(decoder, seekable_stream_decoder_error_callback_))
  830. return die_ss_("returned false", decoder);
  831. printf("OKn");
  832. printf("testing FLAC__seekable_stream_decoder_set_client_data()... ");
  833. if(!FLAC__seekable_stream_decoder_set_client_data(decoder, &decoder_client_data))
  834. return die_ss_("returned false", decoder);
  835. printf("OKn");
  836. printf("testing FLAC__seekable_stream_decoder_set_md5_checking()... ");
  837. if(!FLAC__seekable_stream_decoder_set_md5_checking(decoder, true))
  838. return die_ss_("returned false", decoder);
  839. printf("OKn");
  840. printf("testing FLAC__seekable_stream_decoder_init()... ");
  841. if(FLAC__seekable_stream_decoder_init(decoder) != FLAC__SEEKABLE_STREAM_DECODER_OK)
  842. return die_ss_(0, decoder);
  843. printf("OKn");
  844. printf("testing FLAC__seekable_stream_decoder_get_state()... ");
  845. state = FLAC__seekable_stream_decoder_get_state(decoder);
  846. printf("returned state = %u (%s)... OKn", state, FLAC__SeekableStreamDecoderStateString[state]);
  847. printf("testing FLAC__seekable_stream_decoder_get_stream_decoder_state()... ");
  848. sstate = FLAC__seekable_stream_decoder_get_stream_decoder_state(decoder);
  849. printf("returned state = %u (%s)... OKn", sstate, FLAC__StreamDecoderStateString[sstate]);
  850. decoder_client_data.current_metadata_number = 0;
  851. decoder_client_data.ignore_errors = false;
  852. decoder_client_data.error_occurred = false;
  853. printf("opening FLAC file... ");
  854. decoder_client_data.file = fopen(flacfilename_, "rb");
  855. if(0 == decoder_client_data.file) {
  856. printf("ERRORn");
  857. return false;
  858. }
  859. printf("OKn");
  860. printf("testing FLAC__seekable_stream_decoder_get_md5_checking()... ");
  861. if(!FLAC__seekable_stream_decoder_get_md5_checking(decoder)) {
  862. printf("FAILED, returned false, expected truen");
  863. return false;
  864. }
  865. printf("OKn");
  866. printf("testing FLAC__seekable_stream_decoder_process_until_end_of_metadata()... ");
  867. if(!FLAC__seekable_stream_decoder_process_until_end_of_metadata(decoder))
  868. return die_ss_("returned false", decoder);
  869. printf("OKn");
  870. printf("testing FLAC__seekable_stream_decoder_process_single()... ");
  871. if(!FLAC__seekable_stream_decoder_process_single(decoder))
  872. return die_ss_("returned false", decoder);
  873. printf("OKn");
  874. printf("testing FLAC__seekable_stream_decoder_skip_single_frame()... ");
  875. if(!FLAC__seekable_stream_decoder_skip_single_frame(decoder))
  876. return die_ss_("returned false", decoder);
  877. printf("OKn");
  878. printf("testing FLAC__seekable_stream_decoder_flush()... ");
  879. if(!FLAC__seekable_stream_decoder_flush(decoder))
  880. return die_ss_("returned false", decoder);
  881. printf("OKn");
  882. decoder_client_data.ignore_errors = true;
  883. printf("testing FLAC__seekable_stream_decoder_process_single()... ");
  884. if(!FLAC__seekable_stream_decoder_process_single(decoder))
  885. return die_ss_("returned false", decoder);
  886. printf("OKn");
  887. decoder_client_data.ignore_errors = false;
  888. printf("testing FLAC__seekable_stream_decoder_seek_absolute()... ");
  889. if(!FLAC__seekable_stream_decoder_seek_absolute(decoder, 0))
  890. return die_ss_("returned false", decoder);
  891. printf("OKn");
  892. printf("testing FLAC__seekable_stream_decoder_process_until_end_of_stream()... ");
  893. if(!FLAC__seekable_stream_decoder_process_until_end_of_stream(decoder))
  894. return die_ss_("returned false", decoder);
  895. printf("OKn");
  896. printf("testing FLAC__seekable_stream_decoder_get_channels()... ");
  897. {
  898. unsigned channels = FLAC__seekable_stream_decoder_get_channels(decoder);
  899. if(channels != streaminfo_.data.stream_info.channels) {
  900. printf("FAILED, returned %u, expected %un", channels, streaminfo_.data.stream_info.channels);
  901. return false;
  902. }
  903. }
  904. printf("OKn");
  905. printf("testing FLAC__seekable_stream_decoder_get_bits_per_sample()... ");
  906. {
  907. unsigned bits_per_sample = FLAC__seekable_stream_decoder_get_bits_per_sample(decoder);
  908. if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
  909. printf("FAILED, returned %u, expected %un", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
  910. return false;
  911. }
  912. }
  913. printf("OKn");
  914. printf("testing FLAC__seekable_stream_decoder_get_sample_rate()... ");
  915. {
  916. unsigned sample_rate = FLAC__seekable_stream_decoder_get_sample_rate(decoder);
  917. if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
  918. printf("FAILED, returned %u, expected %un", sample_rate, streaminfo_.data.stream_info.sample_rate);
  919. return false;
  920. }
  921. }
  922. printf("OKn");
  923. printf("testing FLAC__seekable_stream_decoder_get_blocksize()... ");
  924. {
  925. unsigned blocksize = FLAC__seekable_stream_decoder_get_blocksize(decoder);
  926. /* value could be anything since we're at the last block, so accept any answer */
  927. printf("returned %u... OKn", blocksize);
  928. }
  929. printf("testing FLAC__seekable_stream_decoder_get_channel_assignment()... ");
  930. {
  931. FLAC__ChannelAssignment ca = FLAC__seekable_stream_decoder_get_channel_assignment(decoder);
  932. printf("returned %u (%s)... OKn", (unsigned)ca, FLAC__ChannelAssignmentString[ca]);
  933. }
  934. printf("testing FLAC__seekable_stream_decoder_reset()... ");
  935. if(!FLAC__seekable_stream_decoder_reset(decoder))
  936. return die_ss_("returned false", decoder);
  937. printf("OKn");
  938. decoder_client_data.current_metadata_number = 0;
  939. printf("rewinding input... ");
  940. if(fseek(decoder_client_data.file, 0, SEEK_SET) < 0) {
  941. printf("FAILED, errno = %dn", errno);
  942. return false;
  943. }
  944. printf("OKn");
  945. printf("testing FLAC__seekable_stream_decoder_process_until_end_of_stream()... ");
  946. if(!FLAC__seekable_stream_decoder_process_until_end_of_stream(decoder))
  947. return die_ss_("returned false", decoder);
  948. printf("OKn");
  949. printf("testing FLAC__seekable_stream_decoder_finish()... ");
  950. (void) FLAC__seekable_stream_decoder_finish(decoder);
  951. printf("OKn");
  952. /*
  953.  * respond all
  954.  */
  955. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
  956. if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
  957. return die_ss_("returned false", decoder);
  958. printf("OKn");
  959. num_expected_ = 0;
  960. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  961. expected_metadata_sequence_[num_expected_++] = &padding_;
  962. expected_metadata_sequence_[num_expected_++] = &seektable_;
  963. expected_metadata_sequence_[num_expected_++] = &application1_;
  964. expected_metadata_sequence_[num_expected_++] = &application2_;
  965. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  966. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  967. expected_metadata_sequence_[num_expected_++] = &unknown_;
  968. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  969. return false;
  970. /*
  971.  * ignore all
  972.  */
  973. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
  974. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
  975. return die_ss_("returned false", decoder);
  976. printf("OKn");
  977. num_expected_ = 0;
  978. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  979. return false;
  980. /*
  981.  * respond all, ignore VORBIS_COMMENT
  982.  */
  983. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
  984. if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
  985. return die_ss_("returned false", decoder);
  986. printf("OKn");
  987. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore(VORBIS_COMMENT)... ");
  988. if(!FLAC__seekable_stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
  989. return die_ss_("returned false", decoder);
  990. printf("OKn");
  991. num_expected_ = 0;
  992. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  993. expected_metadata_sequence_[num_expected_++] = &padding_;
  994. expected_metadata_sequence_[num_expected_++] = &seektable_;
  995. expected_metadata_sequence_[num_expected_++] = &application1_;
  996. expected_metadata_sequence_[num_expected_++] = &application2_;
  997. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  998. expected_metadata_sequence_[num_expected_++] = &unknown_;
  999. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1000. return false;
  1001. /*
  1002.  * respond all, ignore APPLICATION
  1003.  */
  1004. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
  1005. if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
  1006. return die_ss_("returned false", decoder);
  1007. printf("OKn");
  1008. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore(APPLICATION)... ");
  1009. if(!FLAC__seekable_stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
  1010. return die_ss_("returned false", decoder);
  1011. printf("OKn");
  1012. num_expected_ = 0;
  1013. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1014. expected_metadata_sequence_[num_expected_++] = &padding_;
  1015. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1016. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1017. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1018. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1019. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1020. return false;
  1021. /*
  1022.  * respond all, ignore APPLICATION id of app#1
  1023.  */
  1024. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
  1025. if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
  1026. return die_ss_("returned false", decoder);
  1027. printf("OKn");
  1028. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_application(of app block #1)... ");
  1029. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  1030. return die_ss_("returned false", decoder);
  1031. printf("OKn");
  1032. num_expected_ = 0;
  1033. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1034. expected_metadata_sequence_[num_expected_++] = &padding_;
  1035. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1036. expected_metadata_sequence_[num_expected_++] = &application2_;
  1037. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1038. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1039. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1040. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1041. return false;
  1042. /*
  1043.  * respond all, ignore APPLICATION id of app#1 & app#2
  1044.  */
  1045. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
  1046. if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
  1047. return die_ss_("returned false", decoder);
  1048. printf("OKn");
  1049. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_application(of app block #1)... ");
  1050. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  1051. return die_ss_("returned false", decoder);
  1052. printf("OKn");
  1053. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_application(of app block #2)... ");
  1054. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder, application2_.data.application.id))
  1055. return die_ss_("returned false", decoder);
  1056. printf("OKn");
  1057. num_expected_ = 0;
  1058. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1059. expected_metadata_sequence_[num_expected_++] = &padding_;
  1060. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1061. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1062. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1063. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1064. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1065. return false;
  1066. /*
  1067.  * ignore all, respond VORBIS_COMMENT
  1068.  */
  1069. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
  1070. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
  1071. return die_ss_("returned false", decoder);
  1072. printf("OKn");
  1073. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond(VORBIS_COMMENT)... ");
  1074. if(!FLAC__seekable_stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
  1075. return die_ss_("returned false", decoder);
  1076. printf("OKn");
  1077. num_expected_ = 0;
  1078. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1079. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1080. return false;
  1081. /*
  1082.  * ignore all, respond APPLICATION
  1083.  */
  1084. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
  1085. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
  1086. return die_ss_("returned false", decoder);
  1087. printf("OKn");
  1088. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond(APPLICATION)... ");
  1089. if(!FLAC__seekable_stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
  1090. return die_ss_("returned false", decoder);
  1091. printf("OKn");
  1092. num_expected_ = 0;
  1093. expected_metadata_sequence_[num_expected_++] = &application1_;
  1094. expected_metadata_sequence_[num_expected_++] = &application2_;
  1095. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1096. return false;
  1097. /*
  1098.  * ignore all, respond APPLICATION id of app#1
  1099.  */
  1100. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
  1101. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
  1102. return die_ss_("returned false", decoder);
  1103. printf("OKn");
  1104. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_application(of app block #1)... ");
  1105. if(!FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  1106. return die_ss_("returned false", decoder);
  1107. printf("OKn");
  1108. num_expected_ = 0;
  1109. expected_metadata_sequence_[num_expected_++] = &application1_;
  1110. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1111. return false;
  1112. /*
  1113.  * ignore all, respond APPLICATION id of app#1 & app#2
  1114.  */
  1115. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
  1116. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
  1117. return die_ss_("returned false", decoder);
  1118. printf("OKn");
  1119. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_application(of app block #1)... ");
  1120. if(!FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  1121. return die_ss_("returned false", decoder);
  1122. printf("OKn");
  1123. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_application(of app block #2)... ");
  1124. if(!FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder, application2_.data.application.id))
  1125. return die_ss_("returned false", decoder);
  1126. printf("OKn");
  1127. num_expected_ = 0;
  1128. expected_metadata_sequence_[num_expected_++] = &application1_;
  1129. expected_metadata_sequence_[num_expected_++] = &application2_;
  1130. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1131. return false;
  1132. /*
  1133.  * respond all, ignore APPLICATION, respond APPLICATION id of app#1
  1134.  */
  1135. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
  1136. if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
  1137. return die_ss_("returned false", decoder);
  1138. printf("OKn");
  1139. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore(APPLICATION)... ");
  1140. if(!FLAC__seekable_stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
  1141. return die_ss_("returned false", decoder);
  1142. printf("OKn");
  1143. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_application(of app block #1)... ");
  1144. if(!FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  1145. return die_ss_("returned false", decoder);
  1146. printf("OKn");
  1147. num_expected_ = 0;
  1148. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1149. expected_metadata_sequence_[num_expected_++] = &padding_;
  1150. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1151. expected_metadata_sequence_[num_expected_++] = &application1_;
  1152. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1153. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1154. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1155. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1156. return false;
  1157. /*
  1158.  * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
  1159.  */
  1160. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
  1161. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
  1162. return die_ss_("returned false", decoder);
  1163. printf("OKn");
  1164. printf("testing FLAC__seekable_stream_decoder_set_metadata_respond(APPLICATION)... ");
  1165. if(!FLAC__seekable_stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
  1166. return die_ss_("returned false", decoder);
  1167. printf("OKn");
  1168. printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_application(of app block #1)... ");
  1169. if(!FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  1170. return die_ss_("returned false", decoder);
  1171. printf("OKn");
  1172. num_expected_ = 0;
  1173. expected_metadata_sequence_[num_expected_++] = &application2_;
  1174. if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
  1175. return false;
  1176. /* done, now leave the sequence the way we found it... */
  1177. num_expected_ = 0;
  1178. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1179. expected_metadata_sequence_[num_expected_++] = &padding_;
  1180. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1181. expected_metadata_sequence_[num_expected_++] = &application1_;
  1182. expected_metadata_sequence_[num_expected_++] = &application2_;
  1183. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1184. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1185. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1186. printf("testing FLAC__seekable_stream_decoder_delete()... ");
  1187. FLAC__seekable_stream_decoder_delete(decoder);
  1188. printf("OKn");
  1189. fclose(decoder_client_data.file);
  1190. printf("nPASSED!n");
  1191. return true;
  1192. }
  1193. static FLAC__StreamDecoderWriteStatus file_decoder_write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  1194. {
  1195. (void)decoder;
  1196. return stream_decoder_write_callback_(0, frame, buffer, client_data);
  1197. }
  1198. static void file_decoder_metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  1199. {
  1200. (void)decoder;
  1201. stream_decoder_metadata_callback_(0, metadata, client_data);
  1202. }
  1203. static void file_decoder_error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  1204. {
  1205. (void)decoder;
  1206. stream_decoder_error_callback_(0, status, client_data);
  1207. }
  1208. static FLAC__bool file_decoder_test_respond_(FLAC__FileDecoder *decoder, file_decoder_client_data_struct *dcd)
  1209. {
  1210. if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
  1211. return die_f_("at FLAC__file_decoder_set_write_callback(), returned false", decoder);
  1212. if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
  1213. return die_f_("at FLAC__file_decoder_set_metadata_callback(), returned false", decoder);
  1214. if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
  1215. return die_f_("at FLAC__file_decoder_set_error_callback(), returned false", decoder);
  1216. if(!FLAC__file_decoder_set_client_data(decoder, dcd))
  1217. return die_f_("at FLAC__file_decoder_set_client_data(), returned false", decoder);
  1218. if(!FLAC__file_decoder_set_filename(decoder, flacfilename_))
  1219. return die_f_("at FLAC__file_decoder_set_filename(), returned false", decoder);
  1220. if(!FLAC__file_decoder_set_md5_checking(decoder, true))
  1221. return die_f_("at FLAC__file_decoder_set_md5_checking(), returned false", decoder);
  1222. printf("testing FLAC__file_decoder_init()... ");
  1223. if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
  1224. return die_f_(0, decoder);
  1225. printf("OKn");
  1226. dcd->current_metadata_number = 0;
  1227. printf("testing FLAC__file_decoder_process_until_end_of_file()... ");
  1228. if(!FLAC__file_decoder_process_until_end_of_file(decoder))
  1229. return die_f_("returned false", decoder);
  1230. printf("OKn");
  1231. printf("testing FLAC__file_decoder_finish()... ");
  1232. (void) FLAC__file_decoder_finish(decoder);
  1233. printf("OKn");
  1234. return true;
  1235. }
  1236. static FLAC__bool test_file_decoder()
  1237. {
  1238. FLAC__FileDecoder *decoder;
  1239. FLAC__FileDecoderState state;
  1240. FLAC__SeekableStreamDecoderState ssstate;
  1241. FLAC__StreamDecoderState sstate;
  1242. seekable_stream_decoder_client_data_struct decoder_client_data;
  1243. printf("n+++ libFLAC unit test: FLAC__FileDecodernn");
  1244. printf("testing FLAC__file_decoder_new()... ");
  1245. decoder = FLAC__file_decoder_new();
  1246. if(0 == decoder) {
  1247. printf("FAILED, returned NULLn");
  1248. return false;
  1249. }
  1250. printf("OKn");
  1251. printf("testing FLAC__file_decoder_delete()... ");
  1252. FLAC__file_decoder_delete(decoder);
  1253. printf("OKn");
  1254. printf("testing FLAC__file_decoder_new()... ");
  1255. decoder = FLAC__file_decoder_new();
  1256. if(0 == decoder) {
  1257. printf("FAILED, returned NULLn");
  1258. return false;
  1259. }
  1260. printf("OKn");
  1261. printf("testing FLAC__file_decoder_init()... ");
  1262. if(FLAC__file_decoder_init(decoder) == FLAC__FILE_DECODER_OK)
  1263. return die_f_(0, decoder);
  1264. printf("OKn");
  1265. printf("testing FLAC__file_decoder_delete()... ");
  1266. FLAC__file_decoder_delete(decoder);
  1267. printf("OKn");
  1268. num_expected_ = 0;
  1269. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1270. printf("testing FLAC__file_decoder_new()... ");
  1271. decoder = FLAC__file_decoder_new();
  1272. if(0 == decoder) {
  1273. printf("FAILED, returned NULLn");
  1274. return false;
  1275. }
  1276. printf("OKn");
  1277. printf("testing FLAC__file_decoder_set_write_callback()... ");
  1278. if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
  1279. return die_f_("returned false", decoder);
  1280. printf("OKn");
  1281. printf("testing FLAC__file_decoder_set_metadata_callback()... ");
  1282. if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
  1283. return die_f_("returned false", decoder);
  1284. printf("OKn");
  1285. printf("testing FLAC__file_decoder_set_error_callback()... ");
  1286. if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
  1287. return die_f_("returned false", decoder);
  1288. printf("OKn");
  1289. printf("testing FLAC__file_decoder_set_client_data()... ");
  1290. if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
  1291. return die_f_("returned false", decoder);
  1292. printf("OKn");
  1293. printf("testing FLAC__file_decoder_set_filename()... ");
  1294. if(!FLAC__file_decoder_set_filename(decoder, flacfilename_))
  1295. return die_f_("returned false", decoder);
  1296. printf("OKn");
  1297. printf("testing FLAC__file_decoder_set_md5_checking()... ");
  1298. if(!FLAC__file_decoder_set_md5_checking(decoder, true))
  1299. return die_f_("returned false", decoder);
  1300. printf("OKn");
  1301. printf("testing FLAC__file_decoder_init()... ");
  1302. if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
  1303. return die_f_(0, decoder);
  1304. printf("OKn");
  1305. printf("testing FLAC__file_decoder_get_state()... ");
  1306. state = FLAC__file_decoder_get_state(decoder);
  1307. printf("returned state = %u (%s)... OKn", state, FLAC__FileDecoderStateString[state]);
  1308. printf("testing FLAC__file_decoder_get_seekable_stream_decoder_state()... ");
  1309. ssstate = FLAC__file_decoder_get_seekable_stream_decoder_state(decoder);
  1310. printf("returned state = %u (%s)... OKn", ssstate, FLAC__SeekableStreamDecoderStateString[ssstate]);
  1311. printf("testing FLAC__file_decoder_get_stream_decoder_state()... ");
  1312. sstate = FLAC__file_decoder_get_stream_decoder_state(decoder);
  1313. printf("returned state = %u (%s)... OKn", sstate, FLAC__StreamDecoderStateString[sstate]);
  1314. decoder_client_data.current_metadata_number = 0;
  1315. decoder_client_data.ignore_errors = false;
  1316. decoder_client_data.error_occurred = false;
  1317. printf("testing FLAC__file_decoder_get_md5_checking()... ");
  1318. if(!FLAC__file_decoder_get_md5_checking(decoder)) {
  1319. printf("FAILED, returned false, expected truen");
  1320. return false;
  1321. }
  1322. printf("OKn");
  1323. printf("testing FLAC__file_decoder_process_until_end_of_metadata()... ");
  1324. if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
  1325. return die_f_("returned false", decoder);
  1326. printf("OKn");
  1327. printf("testing FLAC__file_decoder_process_single()... ");
  1328. if(!FLAC__file_decoder_process_single(decoder))
  1329. return die_f_("returned false", decoder);
  1330. printf("OKn");
  1331. printf("testing FLAC__file_decoder_skip_single_frame()... ");
  1332. if(!FLAC__file_decoder_skip_single_frame(decoder))
  1333. return die_f_("returned false", decoder);
  1334. printf("OKn");
  1335. printf("testing FLAC__file_decoder_seek_absolute()... ");
  1336. if(!FLAC__file_decoder_seek_absolute(decoder, 0))
  1337. return die_f_("returned false", decoder);
  1338. printf("OKn");
  1339. printf("testing FLAC__file_decoder_process_until_end_of_file()... ");
  1340. if(!FLAC__file_decoder_process_until_end_of_file(decoder))
  1341. return die_f_("returned false", decoder);
  1342. printf("OKn");
  1343. printf("testing FLAC__file_decoder_get_channels()... ");
  1344. {
  1345. unsigned channels = FLAC__file_decoder_get_channels(decoder);
  1346. if(channels != streaminfo_.data.stream_info.channels) {
  1347. printf("FAILED, returned %u, expected %un", channels, streaminfo_.data.stream_info.channels);
  1348. return false;
  1349. }
  1350. }
  1351. printf("OKn");
  1352. printf("testing FLAC__file_decoder_get_bits_per_sample()... ");
  1353. {
  1354. unsigned bits_per_sample = FLAC__file_decoder_get_bits_per_sample(decoder);
  1355. if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
  1356. printf("FAILED, returned %u, expected %un", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
  1357. return false;
  1358. }
  1359. }
  1360. printf("OKn");
  1361. printf("testing FLAC__file_decoder_get_sample_rate()... ");
  1362. {
  1363. unsigned sample_rate = FLAC__file_decoder_get_sample_rate(decoder);
  1364. if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
  1365. printf("FAILED, returned %u, expected %un", sample_rate, streaminfo_.data.stream_info.sample_rate);
  1366. return false;
  1367. }
  1368. }
  1369. printf("OKn");
  1370. printf("testing FLAC__file_decoder_get_blocksize()... ");
  1371. {
  1372. unsigned blocksize = FLAC__file_decoder_get_blocksize(decoder);
  1373. /* value could be anything since we're at the last block, so accept any answer */
  1374. printf("returned %u... OKn", blocksize);
  1375. }
  1376. printf("testing FLAC__file_decoder_get_channel_assignment()... ");
  1377. {
  1378. FLAC__ChannelAssignment ca = FLAC__file_decoder_get_channel_assignment(decoder);
  1379. printf("returned %u (%s)... OKn", (unsigned)ca, FLAC__ChannelAssignmentString[ca]);
  1380. }
  1381. printf("testing FLAC__file_decoder_finish()... ");
  1382. (void) FLAC__file_decoder_finish(decoder);
  1383. printf("OKn");
  1384. /*
  1385.  * respond all
  1386.  */
  1387. printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
  1388. if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
  1389. return die_f_("returned false", decoder);
  1390. printf("OKn");
  1391. num_expected_ = 0;
  1392. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1393. expected_metadata_sequence_[num_expected_++] = &padding_;
  1394. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1395. expected_metadata_sequence_[num_expected_++] = &application1_;
  1396. expected_metadata_sequence_[num_expected_++] = &application2_;
  1397. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1398. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1399. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1400. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1401. return false;
  1402. /*
  1403.  * ignore all
  1404.  */
  1405. printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
  1406. if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
  1407. return die_f_("returned false", decoder);
  1408. printf("OKn");
  1409. num_expected_ = 0;
  1410. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1411. return false;
  1412. /*
  1413.  * respond all, ignore VORBIS_COMMENT
  1414.  */
  1415. printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
  1416. if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
  1417. return die_f_("returned false", decoder);
  1418. printf("OKn");
  1419. printf("testing FLAC__file_decoder_set_metadata_ignore(VORBIS_COMMENT)... ");
  1420. if(!FLAC__file_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
  1421. return die_f_("returned false", decoder);
  1422. printf("OKn");
  1423. num_expected_ = 0;
  1424. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1425. expected_metadata_sequence_[num_expected_++] = &padding_;
  1426. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1427. expected_metadata_sequence_[num_expected_++] = &application1_;
  1428. expected_metadata_sequence_[num_expected_++] = &application2_;
  1429. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1430. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1431. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1432. return false;
  1433. /*
  1434.  * respond all, ignore APPLICATION
  1435.  */
  1436. printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
  1437. if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
  1438. return die_f_("returned false", decoder);
  1439. printf("OKn");
  1440. printf("testing FLAC__file_decoder_set_metadata_ignore(APPLICATION)... ");
  1441. if(!FLAC__file_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
  1442. return die_f_("returned false", decoder);
  1443. printf("OKn");
  1444. num_expected_ = 0;
  1445. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1446. expected_metadata_sequence_[num_expected_++] = &padding_;
  1447. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1448. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1449. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1450. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1451. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1452. return false;
  1453. /*
  1454.  * respond all, ignore APPLICATION id of app#1
  1455.  */
  1456. printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
  1457. if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
  1458. return die_f_("returned false", decoder);
  1459. printf("OKn");
  1460. printf("testing FLAC__file_decoder_set_metadata_ignore_application(of app block #1)... ");
  1461. if(!FLAC__file_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  1462. return die_f_("returned false", decoder);
  1463. printf("OKn");
  1464. num_expected_ = 0;
  1465. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1466. expected_metadata_sequence_[num_expected_++] = &padding_;
  1467. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1468. expected_metadata_sequence_[num_expected_++] = &application2_;
  1469. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1470. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1471. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1472. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1473. return false;
  1474. /*
  1475.  * respond all, ignore APPLICATION id of app#1 & app#2
  1476.  */
  1477. printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
  1478. if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
  1479. return die_f_("returned false", decoder);
  1480. printf("OKn");
  1481. printf("testing FLAC__file_decoder_set_metadata_ignore_application(of app block #1)... ");
  1482. if(!FLAC__file_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  1483. return die_f_("returned false", decoder);
  1484. printf("OKn");
  1485. printf("testing FLAC__file_decoder_set_metadata_ignore_application(of app block #2)... ");
  1486. if(!FLAC__file_decoder_set_metadata_ignore_application(decoder, application2_.data.application.id))
  1487. return die_f_("returned false", decoder);
  1488. printf("OKn");
  1489. num_expected_ = 0;
  1490. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1491. expected_metadata_sequence_[num_expected_++] = &padding_;
  1492. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1493. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1494. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1495. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1496. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1497. return false;
  1498. /*
  1499.  * ignore all, respond VORBIS_COMMENT
  1500.  */
  1501. printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
  1502. if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
  1503. return die_f_("returned false", decoder);
  1504. printf("OKn");
  1505. printf("testing FLAC__file_decoder_set_metadata_respond(VORBIS_COMMENT)... ");
  1506. if(!FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
  1507. return die_f_("returned false", decoder);
  1508. printf("OKn");
  1509. num_expected_ = 0;
  1510. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1511. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1512. return false;
  1513. /*
  1514.  * ignore all, respond APPLICATION
  1515.  */
  1516. printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
  1517. if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
  1518. return die_f_("returned false", decoder);
  1519. printf("OKn");
  1520. printf("testing FLAC__file_decoder_set_metadata_respond(APPLICATION)... ");
  1521. if(!FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
  1522. return die_f_("returned false", decoder);
  1523. printf("OKn");
  1524. num_expected_ = 0;
  1525. expected_metadata_sequence_[num_expected_++] = &application1_;
  1526. expected_metadata_sequence_[num_expected_++] = &application2_;
  1527. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1528. return false;
  1529. /*
  1530.  * ignore all, respond APPLICATION id of app#1
  1531.  */
  1532. printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
  1533. if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
  1534. return die_f_("returned false", decoder);
  1535. printf("OKn");
  1536. printf("testing FLAC__file_decoder_set_metadata_respond_application(of app block #1)... ");
  1537. if(!FLAC__file_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  1538. return die_f_("returned false", decoder);
  1539. printf("OKn");
  1540. num_expected_ = 0;
  1541. expected_metadata_sequence_[num_expected_++] = &application1_;
  1542. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1543. return false;
  1544. /*
  1545.  * ignore all, respond APPLICATION id of app#1 & app#2
  1546.  */
  1547. printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
  1548. if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
  1549. return die_f_("returned false", decoder);
  1550. printf("OKn");
  1551. printf("testing FLAC__file_decoder_set_metadata_respond_application(of app block #1)... ");
  1552. if(!FLAC__file_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  1553. return die_f_("returned false", decoder);
  1554. printf("OKn");
  1555. printf("testing FLAC__file_decoder_set_metadata_respond_application(of app block #2)... ");
  1556. if(!FLAC__file_decoder_set_metadata_respond_application(decoder, application2_.data.application.id))
  1557. return die_f_("returned false", decoder);
  1558. printf("OKn");
  1559. num_expected_ = 0;
  1560. expected_metadata_sequence_[num_expected_++] = &application1_;
  1561. expected_metadata_sequence_[num_expected_++] = &application2_;
  1562. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1563. return false;
  1564. /*
  1565.  * respond all, ignore APPLICATION, respond APPLICATION id of app#1
  1566.  */
  1567. printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
  1568. if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
  1569. return die_f_("returned false", decoder);
  1570. printf("OKn");
  1571. printf("testing FLAC__file_decoder_set_metadata_ignore(APPLICATION)... ");
  1572. if(!FLAC__file_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
  1573. return die_f_("returned false", decoder);
  1574. printf("OKn");
  1575. printf("testing FLAC__file_decoder_set_metadata_respond_application(of app block #1)... ");
  1576. if(!FLAC__file_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
  1577. return die_f_("returned false", decoder);
  1578. printf("OKn");
  1579. num_expected_ = 0;
  1580. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1581. expected_metadata_sequence_[num_expected_++] = &padding_;
  1582. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1583. expected_metadata_sequence_[num_expected_++] = &application1_;
  1584. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1585. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1586. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1587. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1588. return false;
  1589. /*
  1590.  * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
  1591.  */
  1592. printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
  1593. if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
  1594. return die_f_("returned false", decoder);
  1595. printf("OKn");
  1596. printf("testing FLAC__file_decoder_set_metadata_respond(APPLICATION)... ");
  1597. if(!FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
  1598. return die_f_("returned false", decoder);
  1599. printf("OKn");
  1600. printf("testing FLAC__file_decoder_set_metadata_ignore_application(of app block #1)... ");
  1601. if(!FLAC__file_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
  1602. return die_f_("returned false", decoder);
  1603. printf("OKn");
  1604. num_expected_ = 0;
  1605. expected_metadata_sequence_[num_expected_++] = &application2_;
  1606. if(!file_decoder_test_respond_(decoder, &decoder_client_data))
  1607. return false;
  1608. /* done, now leave the sequence the way we found it... */
  1609. num_expected_ = 0;
  1610. expected_metadata_sequence_[num_expected_++] = &streaminfo_;
  1611. expected_metadata_sequence_[num_expected_++] = &padding_;
  1612. expected_metadata_sequence_[num_expected_++] = &seektable_;
  1613. expected_metadata_sequence_[num_expected_++] = &application1_;
  1614. expected_metadata_sequence_[num_expected_++] = &application2_;
  1615. expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
  1616. expected_metadata_sequence_[num_expected_++] = &cuesheet_;
  1617. expected_metadata_sequence_[num_expected_++] = &unknown_;
  1618. printf("testing FLAC__file_decoder_delete()... ");
  1619. FLAC__file_decoder_delete(decoder);
  1620. printf("OKn");
  1621. printf("nPASSED!n");
  1622. return true;
  1623. }
  1624. FLAC__bool test_decoders()
  1625. {
  1626. init_metadata_blocks_();
  1627. if(!generate_file_())
  1628. return false;
  1629. if(!test_stream_decoder())
  1630. return false;
  1631. if(!test_seekable_stream_decoder())
  1632. return false;
  1633. if(!test_file_decoder())
  1634. return false;
  1635. (void) grabbag__file_remove_file(flacfilename_);
  1636. free_metadata_blocks_();
  1637. return true;
  1638. }