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

Windows CE

开发平台:

C/C++

  1. /* libFLAC - Free Lossless Audio Codec library
  2.  * Copyright (C) 2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - Neither the name of the Xiph.org Foundation nor the names of its
  16.  * contributors may be used to endorse or promote products derived from
  17.  * this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. #include <stdio.h>
  32. #include <stdlib.h> /* for malloc() */
  33. #include <string.h> /* for strlen(), strcpy() */
  34. #include "FLAC/assert.h"
  35. #include "protected/file_encoder.h"
  36. #ifdef max
  37. #undef max
  38. #endif
  39. #define max(x,y) ((x)>(y)?(x):(y))
  40. /***********************************************************************
  41.  *
  42.  * Private class method prototypes
  43.  *
  44.  ***********************************************************************/
  45. /* unpublished debug routines */
  46. extern FLAC__bool FLAC__seekable_stream_encoder_disable_constant_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
  47. extern FLAC__bool FLAC__seekable_stream_encoder_disable_fixed_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
  48. extern FLAC__bool FLAC__seekable_stream_encoder_disable_verbatim_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
  49. static void set_defaults_(FLAC__FileEncoder *encoder);
  50. static FLAC__SeekableStreamEncoderSeekStatus seek_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
  51. static FLAC__SeekableStreamEncoderTellStatus tell_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
  52. static FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
  53. /***********************************************************************
  54.  *
  55.  * Private class data
  56.  *
  57.  ***********************************************************************/
  58. typedef struct FLAC__FileEncoderPrivate {
  59. FLAC__FileEncoderProgressCallback progress_callback;
  60. void *client_data;
  61. char *filename;
  62. FLAC__uint64 bytes_written;
  63. FLAC__uint64 samples_written;
  64. unsigned frames_written;
  65. unsigned total_frames_estimate;
  66. FLAC__SeekableStreamEncoder *seekable_stream_encoder;
  67. FILE *file;
  68. } FLAC__FileEncoderPrivate;
  69. /***********************************************************************
  70.  *
  71.  * Public static class data
  72.  *
  73.  ***********************************************************************/
  74. FLAC_API const char * const FLAC__FileEncoderStateString[] = {
  75. "FLAC__FILE_ENCODER_OK",
  76. "FLAC__FILE_ENCODER_NO_FILENAME",
  77. "FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR",
  78. "FLAC__FILE_ENCODER_FATAL_ERROR_WHILE_WRITING",
  79. "FLAC__FILE_ENCODER_ERROR_OPENING_FILE",
  80. "FLAC__FILE_ENCODER_MEMORY_ALLOCATION_ERROR",
  81. "FLAC__FILE_ENCODER_ALREADY_INITIALIZED",
  82. "FLAC__FILE_ENCODER_UNINITIALIZED"
  83. };
  84. /***********************************************************************
  85.  *
  86.  * Class constructor/destructor
  87.  *
  88.  ***********************************************************************/
  89. FLAC_API FLAC__FileEncoder *FLAC__file_encoder_new()
  90. {
  91. FLAC__FileEncoder *encoder;
  92. FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
  93. encoder = (FLAC__FileEncoder*)calloc(1, sizeof(FLAC__FileEncoder));
  94. if(encoder == 0) {
  95. return 0;
  96. }
  97. encoder->protected_ = (FLAC__FileEncoderProtected*)calloc(1, sizeof(FLAC__FileEncoderProtected));
  98. if(encoder->protected_ == 0) {
  99. free(encoder);
  100. return 0;
  101. }
  102. encoder->private_ = (FLAC__FileEncoderPrivate*)calloc(1, sizeof(FLAC__FileEncoderPrivate));
  103. if(encoder->private_ == 0) {
  104. free(encoder->protected_);
  105. free(encoder);
  106. return 0;
  107. }
  108. encoder->private_->seekable_stream_encoder = FLAC__seekable_stream_encoder_new();
  109. if(0 == encoder->private_->seekable_stream_encoder) {
  110. free(encoder->private_);
  111. free(encoder->protected_);
  112. free(encoder);
  113. return 0;
  114. }
  115. encoder->private_->file = 0;
  116. set_defaults_(encoder);
  117. encoder->protected_->state = FLAC__FILE_ENCODER_UNINITIALIZED;
  118. return encoder;
  119. }
  120. FLAC_API void FLAC__file_encoder_delete(FLAC__FileEncoder *encoder)
  121. {
  122. FLAC__ASSERT(0 != encoder);
  123. FLAC__ASSERT(0 != encoder->protected_);
  124. FLAC__ASSERT(0 != encoder->private_);
  125. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  126. (void)FLAC__file_encoder_finish(encoder);
  127. FLAC__seekable_stream_encoder_delete(encoder->private_->seekable_stream_encoder);
  128. free(encoder->private_);
  129. free(encoder->protected_);
  130. free(encoder);
  131. }
  132. /***********************************************************************
  133.  *
  134.  * Public class methods
  135.  *
  136.  ***********************************************************************/
  137. FLAC_API FLAC__FileEncoderState FLAC__file_encoder_init(FLAC__FileEncoder *encoder)
  138. {
  139. FLAC__ASSERT(0 != encoder);
  140. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  141. return encoder->protected_->state = FLAC__FILE_ENCODER_ALREADY_INITIALIZED;
  142. if(0 == encoder->private_->filename)
  143. return encoder->protected_->state = FLAC__FILE_ENCODER_NO_FILENAME;
  144. encoder->private_->file = fopen(encoder->private_->filename, "w+b");
  145. if(encoder->private_->file == 0)
  146. return encoder->protected_->state = FLAC__FILE_ENCODER_ERROR_OPENING_FILE;
  147. encoder->private_->bytes_written = 0;
  148. encoder->private_->samples_written = 0;
  149. encoder->private_->frames_written = 0;
  150. FLAC__seekable_stream_encoder_set_seek_callback(encoder->private_->seekable_stream_encoder, seek_callback_);
  151. FLAC__seekable_stream_encoder_set_tell_callback(encoder->private_->seekable_stream_encoder, tell_callback_);
  152. FLAC__seekable_stream_encoder_set_write_callback(encoder->private_->seekable_stream_encoder, write_callback_);
  153. FLAC__seekable_stream_encoder_set_client_data(encoder->private_->seekable_stream_encoder, encoder);
  154. if(FLAC__seekable_stream_encoder_init(encoder->private_->seekable_stream_encoder) != FLAC__SEEKABLE_STREAM_ENCODER_OK)
  155. return encoder->protected_->state = FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
  156. {
  157. unsigned blocksize = FLAC__file_encoder_get_blocksize(encoder);
  158. FLAC__ASSERT(blocksize != 0);
  159. encoder->private_->total_frames_estimate = (unsigned)((FLAC__file_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
  160. }
  161. return encoder->protected_->state = FLAC__FILE_ENCODER_OK;
  162. }
  163. FLAC_API void FLAC__file_encoder_finish(FLAC__FileEncoder *encoder)
  164. {
  165. FLAC__ASSERT(0 != encoder);
  166. if(encoder->protected_->state == FLAC__FILE_ENCODER_UNINITIALIZED)
  167. return;
  168. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  169. /* FLAC__seekable_stream_encoder_finish() might write data so we must close the file after it. */
  170. FLAC__seekable_stream_encoder_finish(encoder->private_->seekable_stream_encoder);
  171. if(0 != encoder->private_->file) {
  172. fclose(encoder->private_->file);
  173. encoder->private_->file = 0;
  174. }
  175. if(0 != encoder->private_->filename) {
  176. free(encoder->private_->filename);
  177. encoder->private_->filename = 0;
  178. }
  179. set_defaults_(encoder);
  180. encoder->protected_->state = FLAC__FILE_ENCODER_UNINITIALIZED;
  181. }
  182. FLAC_API FLAC__bool FLAC__file_encoder_set_verify(FLAC__FileEncoder *encoder, FLAC__bool value)
  183. {
  184. FLAC__ASSERT(0 != encoder);
  185. FLAC__ASSERT(0 != encoder->private_);
  186. FLAC__ASSERT(0 != encoder->protected_);
  187. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  188. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  189. return false;
  190. return FLAC__seekable_stream_encoder_set_verify(encoder->private_->seekable_stream_encoder, value);
  191. }
  192. FLAC_API FLAC__bool FLAC__file_encoder_set_streamable_subset(FLAC__FileEncoder *encoder, FLAC__bool value)
  193. {
  194. FLAC__ASSERT(0 != encoder);
  195. FLAC__ASSERT(0 != encoder->private_);
  196. FLAC__ASSERT(0 != encoder->protected_);
  197. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  198. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  199. return false;
  200. return FLAC__seekable_stream_encoder_set_streamable_subset(encoder->private_->seekable_stream_encoder, value);
  201. }
  202. FLAC_API FLAC__bool FLAC__file_encoder_set_do_mid_side_stereo(FLAC__FileEncoder *encoder, FLAC__bool value)
  203. {
  204. FLAC__ASSERT(0 != encoder);
  205. FLAC__ASSERT(0 != encoder->private_);
  206. FLAC__ASSERT(0 != encoder->protected_);
  207. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  208. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  209. return false;
  210. return FLAC__seekable_stream_encoder_set_do_mid_side_stereo(encoder->private_->seekable_stream_encoder, value);
  211. }
  212. FLAC_API FLAC__bool FLAC__file_encoder_set_loose_mid_side_stereo(FLAC__FileEncoder *encoder, FLAC__bool value)
  213. {
  214. FLAC__ASSERT(0 != encoder);
  215. FLAC__ASSERT(0 != encoder->private_);
  216. FLAC__ASSERT(0 != encoder->protected_);
  217. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  218. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  219. return false;
  220. return FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(encoder->private_->seekable_stream_encoder, value);
  221. }
  222. FLAC_API FLAC__bool FLAC__file_encoder_set_channels(FLAC__FileEncoder *encoder, unsigned value)
  223. {
  224. FLAC__ASSERT(0 != encoder);
  225. FLAC__ASSERT(0 != encoder->private_);
  226. FLAC__ASSERT(0 != encoder->protected_);
  227. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  228. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  229. return false;
  230. return FLAC__seekable_stream_encoder_set_channels(encoder->private_->seekable_stream_encoder, value);
  231. }
  232. FLAC_API FLAC__bool FLAC__file_encoder_set_bits_per_sample(FLAC__FileEncoder *encoder, unsigned value)
  233. {
  234. FLAC__ASSERT(0 != encoder);
  235. FLAC__ASSERT(0 != encoder->private_);
  236. FLAC__ASSERT(0 != encoder->protected_);
  237. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  238. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  239. return false;
  240. return FLAC__seekable_stream_encoder_set_bits_per_sample(encoder->private_->seekable_stream_encoder, value);
  241. }
  242. FLAC_API FLAC__bool FLAC__file_encoder_set_sample_rate(FLAC__FileEncoder *encoder, unsigned value)
  243. {
  244. FLAC__ASSERT(0 != encoder);
  245. FLAC__ASSERT(0 != encoder->private_);
  246. FLAC__ASSERT(0 != encoder->protected_);
  247. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  248. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  249. return false;
  250. return FLAC__seekable_stream_encoder_set_sample_rate(encoder->private_->seekable_stream_encoder, value);
  251. }
  252. FLAC_API FLAC__bool FLAC__file_encoder_set_blocksize(FLAC__FileEncoder *encoder, unsigned value)
  253. {
  254. FLAC__ASSERT(0 != encoder);
  255. FLAC__ASSERT(0 != encoder->private_);
  256. FLAC__ASSERT(0 != encoder->protected_);
  257. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  258. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  259. return false;
  260. return FLAC__seekable_stream_encoder_set_blocksize(encoder->private_->seekable_stream_encoder, value);
  261. }
  262. FLAC_API FLAC__bool FLAC__file_encoder_set_max_lpc_order(FLAC__FileEncoder *encoder, unsigned value)
  263. {
  264. FLAC__ASSERT(0 != encoder);
  265. FLAC__ASSERT(0 != encoder->private_);
  266. FLAC__ASSERT(0 != encoder->protected_);
  267. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  268. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  269. return false;
  270. return FLAC__seekable_stream_encoder_set_max_lpc_order(encoder->private_->seekable_stream_encoder, value);
  271. }
  272. FLAC_API FLAC__bool FLAC__file_encoder_set_qlp_coeff_precision(FLAC__FileEncoder *encoder, unsigned value)
  273. {
  274. FLAC__ASSERT(0 != encoder);
  275. FLAC__ASSERT(0 != encoder->private_);
  276. FLAC__ASSERT(0 != encoder->protected_);
  277. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  278. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  279. return false;
  280. return FLAC__seekable_stream_encoder_set_qlp_coeff_precision(encoder->private_->seekable_stream_encoder, value);
  281. }
  282. FLAC_API FLAC__bool FLAC__file_encoder_set_do_qlp_coeff_prec_search(FLAC__FileEncoder *encoder, FLAC__bool value)
  283. {
  284. FLAC__ASSERT(0 != encoder);
  285. FLAC__ASSERT(0 != encoder->private_);
  286. FLAC__ASSERT(0 != encoder->protected_);
  287. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  288. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  289. return false;
  290. return FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(encoder->private_->seekable_stream_encoder, value);
  291. }
  292. FLAC_API FLAC__bool FLAC__file_encoder_set_do_escape_coding(FLAC__FileEncoder *encoder, FLAC__bool value)
  293. {
  294. FLAC__ASSERT(0 != encoder);
  295. FLAC__ASSERT(0 != encoder->private_);
  296. FLAC__ASSERT(0 != encoder->protected_);
  297. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  298. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  299. return false;
  300. return FLAC__seekable_stream_encoder_set_do_escape_coding(encoder->private_->seekable_stream_encoder, value);
  301. }
  302. FLAC_API FLAC__bool FLAC__file_encoder_set_do_exhaustive_model_search(FLAC__FileEncoder *encoder, FLAC__bool value)
  303. {
  304. FLAC__ASSERT(0 != encoder);
  305. FLAC__ASSERT(0 != encoder->private_);
  306. FLAC__ASSERT(0 != encoder->protected_);
  307. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  308. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  309. return false;
  310. return FLAC__seekable_stream_encoder_set_do_exhaustive_model_search(encoder->private_->seekable_stream_encoder, value);
  311. }
  312. FLAC_API FLAC__bool FLAC__file_encoder_set_min_residual_partition_order(FLAC__FileEncoder *encoder, unsigned value)
  313. {
  314. FLAC__ASSERT(0 != encoder);
  315. FLAC__ASSERT(0 != encoder->private_);
  316. FLAC__ASSERT(0 != encoder->protected_);
  317. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  318. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  319. return false;
  320. return FLAC__seekable_stream_encoder_set_min_residual_partition_order(encoder->private_->seekable_stream_encoder, value);
  321. }
  322. FLAC_API FLAC__bool FLAC__file_encoder_set_max_residual_partition_order(FLAC__FileEncoder *encoder, unsigned value)
  323. {
  324. FLAC__ASSERT(0 != encoder);
  325. FLAC__ASSERT(0 != encoder->private_);
  326. FLAC__ASSERT(0 != encoder->protected_);
  327. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  328. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  329. return false;
  330. return FLAC__seekable_stream_encoder_set_max_residual_partition_order(encoder->private_->seekable_stream_encoder, value);
  331. }
  332. FLAC_API FLAC__bool FLAC__file_encoder_set_rice_parameter_search_dist(FLAC__FileEncoder *encoder, unsigned value)
  333. {
  334. FLAC__ASSERT(0 != encoder);
  335. FLAC__ASSERT(0 != encoder->private_);
  336. FLAC__ASSERT(0 != encoder->protected_);
  337. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  338. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  339. return false;
  340. return FLAC__seekable_stream_encoder_set_rice_parameter_search_dist(encoder->private_->seekable_stream_encoder, value);
  341. }
  342. FLAC_API FLAC__bool FLAC__file_encoder_set_total_samples_estimate(FLAC__FileEncoder *encoder, FLAC__uint64 value)
  343. {
  344. FLAC__ASSERT(0 != encoder);
  345. FLAC__ASSERT(0 != encoder->private_);
  346. FLAC__ASSERT(0 != encoder->protected_);
  347. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  348. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  349. return false;
  350. return FLAC__seekable_stream_encoder_set_total_samples_estimate(encoder->private_->seekable_stream_encoder, value);
  351. }
  352. FLAC_API FLAC__bool FLAC__file_encoder_set_metadata(FLAC__FileEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
  353. {
  354. FLAC__ASSERT(0 != encoder);
  355. FLAC__ASSERT(0 != encoder->private_);
  356. FLAC__ASSERT(0 != encoder->protected_);
  357. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  358. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  359. return false;
  360. return FLAC__seekable_stream_encoder_set_metadata(encoder->private_->seekable_stream_encoder, metadata, num_blocks);
  361. }
  362. FLAC_API FLAC__bool FLAC__file_encoder_set_filename(FLAC__FileEncoder *encoder, const char *value)
  363. {
  364. FLAC__ASSERT(0 != encoder);
  365. FLAC__ASSERT(0 != encoder->private_);
  366. FLAC__ASSERT(0 != encoder->protected_);
  367. FLAC__ASSERT(0 != value);
  368. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  369. return false;
  370. if(0 != encoder->private_->filename) {
  371. free(encoder->private_->filename);
  372. encoder->private_->filename = 0;
  373. }
  374. if(0 == (encoder->private_->filename = (char*)malloc(strlen(value)+1))) {
  375. encoder->protected_->state = FLAC__FILE_ENCODER_MEMORY_ALLOCATION_ERROR;
  376. return false;
  377. }
  378. strcpy(encoder->private_->filename, value);
  379. return true;
  380. }
  381. FLAC_API FLAC__bool FLAC__file_encoder_set_progress_callback(FLAC__FileEncoder *encoder, FLAC__FileEncoderProgressCallback value)
  382. {
  383. FLAC__ASSERT(0 != encoder);
  384. FLAC__ASSERT(0 != encoder->private_);
  385. FLAC__ASSERT(0 != encoder->protected_);
  386. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  387. return false;
  388. encoder->private_->progress_callback = value;
  389. return true;
  390. }
  391. FLAC_API FLAC__bool FLAC__file_encoder_set_client_data(FLAC__FileEncoder *encoder, void *value)
  392. {
  393. FLAC__ASSERT(0 != encoder);
  394. FLAC__ASSERT(0 != encoder->private_);
  395. FLAC__ASSERT(0 != encoder->protected_);
  396. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  397. return false;
  398. encoder->private_->client_data = value;
  399. return true;
  400. }
  401. /*
  402.  * These three functions are not static, but not publically exposed in
  403.  * include/FLAC/ either.  They are used by the test suite.
  404.  */
  405. FLAC_API FLAC__bool FLAC__file_encoder_disable_constant_subframes(FLAC__FileEncoder *encoder, FLAC__bool value)
  406. {
  407. FLAC__ASSERT(0 != encoder);
  408. FLAC__ASSERT(0 != encoder->private_);
  409. FLAC__ASSERT(0 != encoder->protected_);
  410. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  411. return false;
  412. return FLAC__seekable_stream_encoder_disable_constant_subframes(encoder->private_->seekable_stream_encoder, value);
  413. }
  414. FLAC_API FLAC__bool FLAC__file_encoder_disable_fixed_subframes(FLAC__FileEncoder *encoder, FLAC__bool value)
  415. {
  416. FLAC__ASSERT(0 != encoder);
  417. FLAC__ASSERT(0 != encoder->private_);
  418. FLAC__ASSERT(0 != encoder->protected_);
  419. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  420. return false;
  421. return FLAC__seekable_stream_encoder_disable_fixed_subframes(encoder->private_->seekable_stream_encoder, value);
  422. }
  423. FLAC_API FLAC__bool FLAC__file_encoder_disable_verbatim_subframes(FLAC__FileEncoder *encoder, FLAC__bool value)
  424. {
  425. FLAC__ASSERT(0 != encoder);
  426. FLAC__ASSERT(0 != encoder->private_);
  427. FLAC__ASSERT(0 != encoder->protected_);
  428. if(encoder->protected_->state != FLAC__FILE_ENCODER_UNINITIALIZED)
  429. return false;
  430. return FLAC__seekable_stream_encoder_disable_verbatim_subframes(encoder->private_->seekable_stream_encoder, value);
  431. }
  432. FLAC_API FLAC__FileEncoderState FLAC__file_encoder_get_state(const FLAC__FileEncoder *encoder)
  433. {
  434. FLAC__ASSERT(0 != encoder);
  435. FLAC__ASSERT(0 != encoder->protected_);
  436. return encoder->protected_->state;
  437. }
  438. FLAC_API FLAC__SeekableStreamEncoderState FLAC__file_encoder_get_seekable_stream_encoder_state(const FLAC__FileEncoder *encoder)
  439. {
  440. FLAC__ASSERT(0 != encoder);
  441. FLAC__ASSERT(0 != encoder->private_);
  442. return FLAC__seekable_stream_encoder_get_state(encoder->private_->seekable_stream_encoder);
  443. }
  444. FLAC_API FLAC__StreamEncoderState FLAC__file_encoder_get_stream_encoder_state(const FLAC__FileEncoder *encoder)
  445. {
  446. FLAC__ASSERT(0 != encoder);
  447. FLAC__ASSERT(0 != encoder->private_);
  448. return FLAC__seekable_stream_encoder_get_stream_encoder_state(encoder->private_->seekable_stream_encoder);
  449. }
  450. FLAC_API FLAC__StreamDecoderState FLAC__file_encoder_get_verify_decoder_state(const FLAC__FileEncoder *encoder)
  451. {
  452. FLAC__ASSERT(0 != encoder);
  453. FLAC__ASSERT(0 != encoder->private_);
  454. return FLAC__seekable_stream_encoder_get_verify_decoder_state(encoder->private_->seekable_stream_encoder);
  455. }
  456. FLAC_API const char *FLAC__file_encoder_get_resolved_state_string(const FLAC__FileEncoder *encoder)
  457. {
  458. if(encoder->protected_->state != FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR)
  459. return FLAC__FileEncoderStateString[encoder->protected_->state];
  460. else
  461. return FLAC__seekable_stream_encoder_get_resolved_state_string(encoder->private_->seekable_stream_encoder);
  462. }
  463. FLAC_API void FLAC__file_encoder_get_verify_decoder_error_stats(const FLAC__FileEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
  464. {
  465. FLAC__ASSERT(0 != encoder);
  466. FLAC__ASSERT(0 != encoder->private_);
  467. FLAC__seekable_stream_encoder_get_verify_decoder_error_stats(encoder->private_->seekable_stream_encoder, absolute_sample, frame_number, channel, sample, expected, got);
  468. }
  469. FLAC_API FLAC__bool FLAC__file_encoder_get_verify(const FLAC__FileEncoder *encoder)
  470. {
  471. FLAC__ASSERT(0 != encoder);
  472. FLAC__ASSERT(0 != encoder->private_);
  473. return FLAC__seekable_stream_encoder_get_verify(encoder->private_->seekable_stream_encoder);
  474. }
  475. FLAC_API FLAC__bool FLAC__file_encoder_get_streamable_subset(const FLAC__FileEncoder *encoder)
  476. {
  477. FLAC__ASSERT(0 != encoder);
  478. FLAC__ASSERT(0 != encoder->private_);
  479. return FLAC__seekable_stream_encoder_get_streamable_subset(encoder->private_->seekable_stream_encoder);
  480. }
  481. FLAC_API FLAC__bool FLAC__file_encoder_get_do_mid_side_stereo(const FLAC__FileEncoder *encoder)
  482. {
  483. FLAC__ASSERT(0 != encoder);
  484. FLAC__ASSERT(0 != encoder->private_);
  485. return FLAC__seekable_stream_encoder_get_do_mid_side_stereo(encoder->private_->seekable_stream_encoder);
  486. }
  487. FLAC_API FLAC__bool FLAC__file_encoder_get_loose_mid_side_stereo(const FLAC__FileEncoder *encoder)
  488. {
  489. FLAC__ASSERT(0 != encoder);
  490. FLAC__ASSERT(0 != encoder->private_);
  491. return FLAC__seekable_stream_encoder_get_loose_mid_side_stereo(encoder->private_->seekable_stream_encoder);
  492. }
  493. FLAC_API unsigned FLAC__file_encoder_get_channels(const FLAC__FileEncoder *encoder)
  494. {
  495. FLAC__ASSERT(0 != encoder);
  496. FLAC__ASSERT(0 != encoder->private_);
  497. return FLAC__seekable_stream_encoder_get_channels(encoder->private_->seekable_stream_encoder);
  498. }
  499. FLAC_API unsigned FLAC__file_encoder_get_bits_per_sample(const FLAC__FileEncoder *encoder)
  500. {
  501. FLAC__ASSERT(0 != encoder);
  502. FLAC__ASSERT(0 != encoder->private_);
  503. return FLAC__seekable_stream_encoder_get_bits_per_sample(encoder->private_->seekable_stream_encoder);
  504. }
  505. FLAC_API unsigned FLAC__file_encoder_get_sample_rate(const FLAC__FileEncoder *encoder)
  506. {
  507. FLAC__ASSERT(0 != encoder);
  508. FLAC__ASSERT(0 != encoder->private_);
  509. return FLAC__seekable_stream_encoder_get_sample_rate(encoder->private_->seekable_stream_encoder);
  510. }
  511. FLAC_API unsigned FLAC__file_encoder_get_blocksize(const FLAC__FileEncoder *encoder)
  512. {
  513. FLAC__ASSERT(0 != encoder);
  514. FLAC__ASSERT(0 != encoder->private_);
  515. return FLAC__seekable_stream_encoder_get_blocksize(encoder->private_->seekable_stream_encoder);
  516. }
  517. FLAC_API unsigned FLAC__file_encoder_get_max_lpc_order(const FLAC__FileEncoder *encoder)
  518. {
  519. FLAC__ASSERT(0 != encoder);
  520. FLAC__ASSERT(0 != encoder->private_);
  521. return FLAC__seekable_stream_encoder_get_max_lpc_order(encoder->private_->seekable_stream_encoder);
  522. }
  523. FLAC_API unsigned FLAC__file_encoder_get_qlp_coeff_precision(const FLAC__FileEncoder *encoder)
  524. {
  525. FLAC__ASSERT(0 != encoder);
  526. FLAC__ASSERT(0 != encoder->private_);
  527. return FLAC__seekable_stream_encoder_get_qlp_coeff_precision(encoder->private_->seekable_stream_encoder);
  528. }
  529. FLAC_API FLAC__bool FLAC__file_encoder_get_do_qlp_coeff_prec_search(const FLAC__FileEncoder *encoder)
  530. {
  531. FLAC__ASSERT(0 != encoder);
  532. FLAC__ASSERT(0 != encoder->private_);
  533. return FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(encoder->private_->seekable_stream_encoder);
  534. }
  535. FLAC_API FLAC__bool FLAC__file_encoder_get_do_escape_coding(const FLAC__FileEncoder *encoder)
  536. {
  537. FLAC__ASSERT(0 != encoder);
  538. FLAC__ASSERT(0 != encoder->private_);
  539. return FLAC__seekable_stream_encoder_get_do_escape_coding(encoder->private_->seekable_stream_encoder);
  540. }
  541. FLAC_API FLAC__bool FLAC__file_encoder_get_do_exhaustive_model_search(const FLAC__FileEncoder *encoder)
  542. {
  543. FLAC__ASSERT(0 != encoder);
  544. FLAC__ASSERT(0 != encoder->private_);
  545. return FLAC__seekable_stream_encoder_get_do_exhaustive_model_search(encoder->private_->seekable_stream_encoder);
  546. }
  547. FLAC_API unsigned FLAC__file_encoder_get_min_residual_partition_order(const FLAC__FileEncoder *encoder)
  548. {
  549. FLAC__ASSERT(0 != encoder);
  550. FLAC__ASSERT(0 != encoder->private_);
  551. return FLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder->private_->seekable_stream_encoder);
  552. }
  553. FLAC_API unsigned FLAC__file_encoder_get_max_residual_partition_order(const FLAC__FileEncoder *encoder)
  554. {
  555. FLAC__ASSERT(0 != encoder);
  556. FLAC__ASSERT(0 != encoder->private_);
  557. return FLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder->private_->seekable_stream_encoder);
  558. }
  559. FLAC_API unsigned FLAC__file_encoder_get_rice_parameter_search_dist(const FLAC__FileEncoder *encoder)
  560. {
  561. FLAC__ASSERT(0 != encoder);
  562. FLAC__ASSERT(0 != encoder->private_);
  563. return FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder->private_->seekable_stream_encoder);
  564. }
  565. FLAC_API FLAC__uint64 FLAC__file_encoder_get_total_samples_estimate(const FLAC__FileEncoder *encoder)
  566. {
  567. FLAC__ASSERT(0 != encoder);
  568. FLAC__ASSERT(0 != encoder->private_);
  569. return FLAC__seekable_stream_encoder_get_total_samples_estimate(encoder->private_->seekable_stream_encoder);
  570. }
  571. FLAC_API FLAC__bool FLAC__file_encoder_process(FLAC__FileEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
  572. {
  573. FLAC__ASSERT(0 != encoder);
  574. FLAC__ASSERT(0 != encoder->private_);
  575. if(!FLAC__seekable_stream_encoder_process(encoder->private_->seekable_stream_encoder, buffer, samples)) {
  576. encoder->protected_->state = FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
  577. return false;
  578. }
  579. else
  580. return true;
  581. }
  582. /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
  583. FLAC_API FLAC__bool FLAC__file_encoder_process_interleaved(FLAC__FileEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
  584. {
  585. FLAC__ASSERT(0 != encoder);
  586. FLAC__ASSERT(0 != encoder->private_);
  587. if(!FLAC__seekable_stream_encoder_process_interleaved(encoder->private_->seekable_stream_encoder, buffer, samples)) {
  588. encoder->protected_->state = FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
  589. return false;
  590. }
  591. else
  592. return true;
  593. }
  594. /***********************************************************************
  595.  *
  596.  * Private class methods
  597.  *
  598.  ***********************************************************************/
  599. void set_defaults_(FLAC__FileEncoder *encoder)
  600. {
  601. FLAC__ASSERT(0 != encoder);
  602. FLAC__ASSERT(0 != encoder->private_);
  603. encoder->private_->progress_callback = 0;
  604. encoder->private_->client_data = 0;
  605. encoder->private_->total_frames_estimate = 0;
  606. encoder->private_->filename = 0;
  607. }
  608. FLAC__SeekableStreamEncoderSeekStatus seek_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  609. {
  610. FLAC__FileEncoder *file_encoder = (FLAC__FileEncoder*)client_data;
  611. (void)encoder;
  612. FLAC__ASSERT(0 != file_encoder);
  613. if(fseek(file_encoder->private_->file, (long)absolute_byte_offset, SEEK_SET) < 0)
  614. return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR;
  615. else
  616. return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
  617. }
  618. FLAC__SeekableStreamEncoderTellStatus tell_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  619. {
  620. FLAC__FileEncoder *file_encoder = (FLAC__FileEncoder*)client_data;
  621. long offset;
  622. (void)encoder;
  623. FLAC__ASSERT(0 != file_encoder);
  624. offset = ftell(file_encoder->private_->file);
  625. if(offset < 0) {
  626. return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_ERROR;
  627. }
  628. else {
  629. *absolute_byte_offset = (FLAC__uint64)offset;
  630. return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK;
  631. }
  632. }
  633. #ifdef FLAC__VALGRIND_TESTING
  634. static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  635. {
  636. size_t ret = fwrite(ptr, size, nmemb, stream);
  637. if(!ferror(stream))
  638. fflush(stream);
  639. return ret;
  640. }
  641. #else
  642. #define local__fwrite fwrite
  643. #endif
  644. FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
  645. {
  646. FLAC__FileEncoder *file_encoder = (FLAC__FileEncoder*)client_data;
  647. (void)encoder, (void)samples, (void)current_frame;
  648. FLAC__ASSERT(0 != file_encoder);
  649. if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, file_encoder->private_->file) == bytes) {
  650. file_encoder->private_->bytes_written += bytes;
  651. file_encoder->private_->samples_written += samples;
  652. /* we keep a high watermark on the number of frames written because
  653.  * when the encoder goes back to write metadata, 'current_frame'
  654.  * will drop back to 0.
  655.  */
  656. file_encoder->private_->frames_written = max(file_encoder->private_->frames_written, current_frame+1);
  657. if(0 != file_encoder->private_->progress_callback && samples > 0)
  658. file_encoder->private_->progress_callback(file_encoder, file_encoder->private_->bytes_written, file_encoder->private_->samples_written, file_encoder->private_->frames_written, file_encoder->private_->total_frames_estimate, file_encoder->private_->client_data);
  659. return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  660. }
  661. else
  662. return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
  663. }