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

Windows CE

开发平台:

C/C++

  1. /* libOggFLAC - Free Lossless Audio Codec + Ogg library
  2.  * Copyright (C) 2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - Neither the name of the Xiph.org Foundation nor the names of its
  16.  * contributors may be used to endorse or promote products derived from
  17.  * this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. #include <stdio.h>
  32. #include <stdlib.h> /* for malloc() */
  33. #include <string.h> /* for strlen(), strcpy() */
  34. #include "FLAC/assert.h"
  35. #include "OggFLAC/seekable_stream_encoder.h"
  36. #include "protected/file_encoder.h"
  37. #ifdef max
  38. #undef max
  39. #endif
  40. #define max(x,y) ((x)>(y)?(x):(y))
  41. /***********************************************************************
  42.  *
  43.  * Private class method prototypes
  44.  *
  45.  ***********************************************************************/
  46. /* unpublished debug routines */
  47. extern FLAC__bool OggFLAC__seekable_stream_encoder_disable_constant_subframes(OggFLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
  48. extern FLAC__bool OggFLAC__seekable_stream_encoder_disable_fixed_subframes(OggFLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
  49. extern FLAC__bool OggFLAC__seekable_stream_encoder_disable_verbatim_subframes(OggFLAC__SeekableStreamEncoder *encoder, FLAC__bool value);
  50. static void set_defaults_(OggFLAC__FileEncoder *encoder);
  51. static OggFLAC__SeekableStreamEncoderReadStatus read_callback_(const OggFLAC__SeekableStreamEncoder *encoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
  52. static FLAC__SeekableStreamEncoderSeekStatus seek_callback_(const OggFLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
  53. static FLAC__SeekableStreamEncoderTellStatus tell_callback_(const OggFLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
  54. static FLAC__StreamEncoderWriteStatus write_callback_(const OggFLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
  55. /***********************************************************************
  56.  *
  57.  * Private class data
  58.  *
  59.  ***********************************************************************/
  60. typedef struct OggFLAC__FileEncoderPrivate {
  61. OggFLAC__FileEncoderProgressCallback progress_callback;
  62. void *client_data;
  63. char *filename;
  64. FLAC__uint64 bytes_written;
  65. FLAC__uint64 samples_written;
  66. unsigned frames_written;
  67. unsigned total_frames_estimate;
  68. OggFLAC__SeekableStreamEncoder *seekable_stream_encoder;
  69. FILE *file;
  70. } OggFLAC__FileEncoderPrivate;
  71. /***********************************************************************
  72.  *
  73.  * Public static class data
  74.  *
  75.  ***********************************************************************/
  76. OggFLAC_API const char * const OggFLAC__FileEncoderStateString[] = {
  77. "OggFLAC__FILE_ENCODER_OK",
  78. "OggFLAC__FILE_ENCODER_NO_FILENAME",
  79. "OggFLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR",
  80. "OggFLAC__FILE_ENCODER_FATAL_ERROR_WHILE_WRITING",
  81. "OggFLAC__FILE_ENCODER_ERROR_OPENING_FILE",
  82. "OggFLAC__FILE_ENCODER_MEMORY_ALLOCATION_ERROR",
  83. "OggFLAC__FILE_ENCODER_ALREADY_INITIALIZED",
  84. "OggFLAC__FILE_ENCODER_UNINITIALIZED"
  85. };
  86. /***********************************************************************
  87.  *
  88.  * Class constructor/destructor
  89.  *
  90.  ***********************************************************************/
  91. OggFLAC_API OggFLAC__FileEncoder *OggFLAC__file_encoder_new()
  92. {
  93. OggFLAC__FileEncoder *encoder;
  94. FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
  95. encoder = (OggFLAC__FileEncoder*)calloc(1, sizeof(OggFLAC__FileEncoder));
  96. if(encoder == 0) {
  97. return 0;
  98. }
  99. encoder->protected_ = (OggFLAC__FileEncoderProtected*)calloc(1, sizeof(OggFLAC__FileEncoderProtected));
  100. if(encoder->protected_ == 0) {
  101. free(encoder);
  102. return 0;
  103. }
  104. encoder->private_ = (OggFLAC__FileEncoderPrivate*)calloc(1, sizeof(OggFLAC__FileEncoderPrivate));
  105. if(encoder->private_ == 0) {
  106. free(encoder->protected_);
  107. free(encoder);
  108. return 0;
  109. }
  110. encoder->private_->seekable_stream_encoder = OggFLAC__seekable_stream_encoder_new();
  111. if(0 == encoder->private_->seekable_stream_encoder) {
  112. free(encoder->private_);
  113. free(encoder->protected_);
  114. free(encoder);
  115. return 0;
  116. }
  117. encoder->private_->file = 0;
  118. set_defaults_(encoder);
  119. encoder->protected_->state = OggFLAC__FILE_ENCODER_UNINITIALIZED;
  120. return encoder;
  121. }
  122. OggFLAC_API void OggFLAC__file_encoder_delete(OggFLAC__FileEncoder *encoder)
  123. {
  124. FLAC__ASSERT(0 != encoder);
  125. FLAC__ASSERT(0 != encoder->protected_);
  126. FLAC__ASSERT(0 != encoder->private_);
  127. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  128. (void)OggFLAC__file_encoder_finish(encoder);
  129. OggFLAC__seekable_stream_encoder_delete(encoder->private_->seekable_stream_encoder);
  130. free(encoder->private_);
  131. free(encoder->protected_);
  132. free(encoder);
  133. }
  134. /***********************************************************************
  135.  *
  136.  * Public class methods
  137.  *
  138.  ***********************************************************************/
  139. OggFLAC_API OggFLAC__FileEncoderState OggFLAC__file_encoder_init(OggFLAC__FileEncoder *encoder)
  140. {
  141. FLAC__ASSERT(0 != encoder);
  142. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  143. return encoder->protected_->state = OggFLAC__FILE_ENCODER_ALREADY_INITIALIZED;
  144. if(0 == encoder->private_->filename)
  145. return encoder->protected_->state = OggFLAC__FILE_ENCODER_NO_FILENAME;
  146. encoder->private_->file = fopen(encoder->private_->filename, "w+b");
  147. if(encoder->private_->file == 0)
  148. return encoder->protected_->state = OggFLAC__FILE_ENCODER_ERROR_OPENING_FILE;
  149. encoder->private_->bytes_written = 0;
  150. encoder->private_->samples_written = 0;
  151. encoder->private_->frames_written = 0;
  152. OggFLAC__seekable_stream_encoder_set_read_callback(encoder->private_->seekable_stream_encoder, read_callback_);
  153. OggFLAC__seekable_stream_encoder_set_seek_callback(encoder->private_->seekable_stream_encoder, seek_callback_);
  154. OggFLAC__seekable_stream_encoder_set_tell_callback(encoder->private_->seekable_stream_encoder, tell_callback_);
  155. OggFLAC__seekable_stream_encoder_set_write_callback(encoder->private_->seekable_stream_encoder, write_callback_);
  156. OggFLAC__seekable_stream_encoder_set_client_data(encoder->private_->seekable_stream_encoder, encoder);
  157. if(OggFLAC__seekable_stream_encoder_init(encoder->private_->seekable_stream_encoder) != OggFLAC__SEEKABLE_STREAM_ENCODER_OK)
  158. return encoder->protected_->state = OggFLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
  159. {
  160. unsigned blocksize = OggFLAC__file_encoder_get_blocksize(encoder);
  161. FLAC__ASSERT(blocksize != 0);
  162. encoder->private_->total_frames_estimate = (unsigned)((OggFLAC__file_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
  163. }
  164. return encoder->protected_->state = OggFLAC__FILE_ENCODER_OK;
  165. }
  166. OggFLAC_API void OggFLAC__file_encoder_finish(OggFLAC__FileEncoder *encoder)
  167. {
  168. FLAC__ASSERT(0 != encoder);
  169. if(encoder->protected_->state == OggFLAC__FILE_ENCODER_UNINITIALIZED)
  170. return;
  171. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  172. /* OggFLAC__seekable_stream_encoder_finish() might write data so we must close the file after it. */
  173. OggFLAC__seekable_stream_encoder_finish(encoder->private_->seekable_stream_encoder);
  174. if(0 != encoder->private_->file) {
  175. fclose(encoder->private_->file);
  176. encoder->private_->file = 0;
  177. }
  178. if(0 != encoder->private_->filename) {
  179. free(encoder->private_->filename);
  180. encoder->private_->filename = 0;
  181. }
  182. set_defaults_(encoder);
  183. encoder->protected_->state = OggFLAC__FILE_ENCODER_UNINITIALIZED;
  184. }
  185. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_serial_number(OggFLAC__FileEncoder *encoder, long serial_number)
  186. {
  187. FLAC__ASSERT(0 != encoder);
  188. FLAC__ASSERT(0 != encoder->private_);
  189. FLAC__ASSERT(0 != encoder->protected_);
  190. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  191. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  192. return false;
  193. return OggFLAC__seekable_stream_encoder_set_serial_number(encoder->private_->seekable_stream_encoder, serial_number);
  194. }
  195. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_verify(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  196. {
  197. FLAC__ASSERT(0 != encoder);
  198. FLAC__ASSERT(0 != encoder->private_);
  199. FLAC__ASSERT(0 != encoder->protected_);
  200. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  201. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  202. return false;
  203. return OggFLAC__seekable_stream_encoder_set_verify(encoder->private_->seekable_stream_encoder, value);
  204. }
  205. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_streamable_subset(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  206. {
  207. FLAC__ASSERT(0 != encoder);
  208. FLAC__ASSERT(0 != encoder->private_);
  209. FLAC__ASSERT(0 != encoder->protected_);
  210. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  211. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  212. return false;
  213. return OggFLAC__seekable_stream_encoder_set_streamable_subset(encoder->private_->seekable_stream_encoder, value);
  214. }
  215. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_do_mid_side_stereo(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  216. {
  217. FLAC__ASSERT(0 != encoder);
  218. FLAC__ASSERT(0 != encoder->private_);
  219. FLAC__ASSERT(0 != encoder->protected_);
  220. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  221. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  222. return false;
  223. return OggFLAC__seekable_stream_encoder_set_do_mid_side_stereo(encoder->private_->seekable_stream_encoder, value);
  224. }
  225. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_loose_mid_side_stereo(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  226. {
  227. FLAC__ASSERT(0 != encoder);
  228. FLAC__ASSERT(0 != encoder->private_);
  229. FLAC__ASSERT(0 != encoder->protected_);
  230. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  231. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  232. return false;
  233. return OggFLAC__seekable_stream_encoder_set_loose_mid_side_stereo(encoder->private_->seekable_stream_encoder, value);
  234. }
  235. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_channels(OggFLAC__FileEncoder *encoder, unsigned value)
  236. {
  237. FLAC__ASSERT(0 != encoder);
  238. FLAC__ASSERT(0 != encoder->private_);
  239. FLAC__ASSERT(0 != encoder->protected_);
  240. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  241. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  242. return false;
  243. return OggFLAC__seekable_stream_encoder_set_channels(encoder->private_->seekable_stream_encoder, value);
  244. }
  245. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_bits_per_sample(OggFLAC__FileEncoder *encoder, unsigned value)
  246. {
  247. FLAC__ASSERT(0 != encoder);
  248. FLAC__ASSERT(0 != encoder->private_);
  249. FLAC__ASSERT(0 != encoder->protected_);
  250. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  251. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  252. return false;
  253. return OggFLAC__seekable_stream_encoder_set_bits_per_sample(encoder->private_->seekable_stream_encoder, value);
  254. }
  255. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_sample_rate(OggFLAC__FileEncoder *encoder, unsigned value)
  256. {
  257. FLAC__ASSERT(0 != encoder);
  258. FLAC__ASSERT(0 != encoder->private_);
  259. FLAC__ASSERT(0 != encoder->protected_);
  260. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  261. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  262. return false;
  263. return OggFLAC__seekable_stream_encoder_set_sample_rate(encoder->private_->seekable_stream_encoder, value);
  264. }
  265. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_blocksize(OggFLAC__FileEncoder *encoder, unsigned value)
  266. {
  267. FLAC__ASSERT(0 != encoder);
  268. FLAC__ASSERT(0 != encoder->private_);
  269. FLAC__ASSERT(0 != encoder->protected_);
  270. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  271. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  272. return false;
  273. return OggFLAC__seekable_stream_encoder_set_blocksize(encoder->private_->seekable_stream_encoder, value);
  274. }
  275. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_max_lpc_order(OggFLAC__FileEncoder *encoder, unsigned value)
  276. {
  277. FLAC__ASSERT(0 != encoder);
  278. FLAC__ASSERT(0 != encoder->private_);
  279. FLAC__ASSERT(0 != encoder->protected_);
  280. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  281. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  282. return false;
  283. return OggFLAC__seekable_stream_encoder_set_max_lpc_order(encoder->private_->seekable_stream_encoder, value);
  284. }
  285. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_qlp_coeff_precision(OggFLAC__FileEncoder *encoder, unsigned value)
  286. {
  287. FLAC__ASSERT(0 != encoder);
  288. FLAC__ASSERT(0 != encoder->private_);
  289. FLAC__ASSERT(0 != encoder->protected_);
  290. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  291. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  292. return false;
  293. return OggFLAC__seekable_stream_encoder_set_qlp_coeff_precision(encoder->private_->seekable_stream_encoder, value);
  294. }
  295. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_do_qlp_coeff_prec_search(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  296. {
  297. FLAC__ASSERT(0 != encoder);
  298. FLAC__ASSERT(0 != encoder->private_);
  299. FLAC__ASSERT(0 != encoder->protected_);
  300. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  301. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  302. return false;
  303. return OggFLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(encoder->private_->seekable_stream_encoder, value);
  304. }
  305. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_do_escape_coding(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  306. {
  307. FLAC__ASSERT(0 != encoder);
  308. FLAC__ASSERT(0 != encoder->private_);
  309. FLAC__ASSERT(0 != encoder->protected_);
  310. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  311. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  312. return false;
  313. return OggFLAC__seekable_stream_encoder_set_do_escape_coding(encoder->private_->seekable_stream_encoder, value);
  314. }
  315. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_do_exhaustive_model_search(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  316. {
  317. FLAC__ASSERT(0 != encoder);
  318. FLAC__ASSERT(0 != encoder->private_);
  319. FLAC__ASSERT(0 != encoder->protected_);
  320. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  321. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  322. return false;
  323. return OggFLAC__seekable_stream_encoder_set_do_exhaustive_model_search(encoder->private_->seekable_stream_encoder, value);
  324. }
  325. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_min_residual_partition_order(OggFLAC__FileEncoder *encoder, unsigned value)
  326. {
  327. FLAC__ASSERT(0 != encoder);
  328. FLAC__ASSERT(0 != encoder->private_);
  329. FLAC__ASSERT(0 != encoder->protected_);
  330. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  331. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  332. return false;
  333. return OggFLAC__seekable_stream_encoder_set_min_residual_partition_order(encoder->private_->seekable_stream_encoder, value);
  334. }
  335. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_max_residual_partition_order(OggFLAC__FileEncoder *encoder, unsigned value)
  336. {
  337. FLAC__ASSERT(0 != encoder);
  338. FLAC__ASSERT(0 != encoder->private_);
  339. FLAC__ASSERT(0 != encoder->protected_);
  340. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  341. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  342. return false;
  343. return OggFLAC__seekable_stream_encoder_set_max_residual_partition_order(encoder->private_->seekable_stream_encoder, value);
  344. }
  345. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_rice_parameter_search_dist(OggFLAC__FileEncoder *encoder, unsigned value)
  346. {
  347. FLAC__ASSERT(0 != encoder);
  348. FLAC__ASSERT(0 != encoder->private_);
  349. FLAC__ASSERT(0 != encoder->protected_);
  350. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  351. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  352. return false;
  353. return OggFLAC__seekable_stream_encoder_set_rice_parameter_search_dist(encoder->private_->seekable_stream_encoder, value);
  354. }
  355. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_total_samples_estimate(OggFLAC__FileEncoder *encoder, FLAC__uint64 value)
  356. {
  357. FLAC__ASSERT(0 != encoder);
  358. FLAC__ASSERT(0 != encoder->private_);
  359. FLAC__ASSERT(0 != encoder->protected_);
  360. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  361. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  362. return false;
  363. return OggFLAC__seekable_stream_encoder_set_total_samples_estimate(encoder->private_->seekable_stream_encoder, value);
  364. }
  365. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_metadata(OggFLAC__FileEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
  366. {
  367. FLAC__ASSERT(0 != encoder);
  368. FLAC__ASSERT(0 != encoder->private_);
  369. FLAC__ASSERT(0 != encoder->protected_);
  370. FLAC__ASSERT(0 != encoder->private_->seekable_stream_encoder);
  371. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  372. return false;
  373. return OggFLAC__seekable_stream_encoder_set_metadata(encoder->private_->seekable_stream_encoder, metadata, num_blocks);
  374. }
  375. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_filename(OggFLAC__FileEncoder *encoder, const char *value)
  376. {
  377. FLAC__ASSERT(0 != encoder);
  378. FLAC__ASSERT(0 != encoder->private_);
  379. FLAC__ASSERT(0 != encoder->protected_);
  380. FLAC__ASSERT(0 != value);
  381. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  382. return false;
  383. if(0 != encoder->private_->filename) {
  384. free(encoder->private_->filename);
  385. encoder->private_->filename = 0;
  386. }
  387. if(0 == (encoder->private_->filename = (char*)malloc(strlen(value)+1))) {
  388. encoder->protected_->state = OggFLAC__FILE_ENCODER_MEMORY_ALLOCATION_ERROR;
  389. return false;
  390. }
  391. strcpy(encoder->private_->filename, value);
  392. return true;
  393. }
  394. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_progress_callback(OggFLAC__FileEncoder *encoder, OggFLAC__FileEncoderProgressCallback value)
  395. {
  396. FLAC__ASSERT(0 != encoder);
  397. FLAC__ASSERT(0 != encoder->private_);
  398. FLAC__ASSERT(0 != encoder->protected_);
  399. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  400. return false;
  401. encoder->private_->progress_callback = value;
  402. return true;
  403. }
  404. OggFLAC_API FLAC__bool OggFLAC__file_encoder_set_client_data(OggFLAC__FileEncoder *encoder, void *value)
  405. {
  406. FLAC__ASSERT(0 != encoder);
  407. FLAC__ASSERT(0 != encoder->private_);
  408. FLAC__ASSERT(0 != encoder->protected_);
  409. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  410. return false;
  411. encoder->private_->client_data = value;
  412. return true;
  413. }
  414. /*
  415.  * These three functions are not static, but not publically exposed in
  416.  * include/OggFLAC/ either.  They are used by the test suite.
  417.  */
  418. OggFLAC_API FLAC__bool OggFLAC__file_encoder_disable_constant_subframes(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  419. {
  420. FLAC__ASSERT(0 != encoder);
  421. FLAC__ASSERT(0 != encoder->private_);
  422. FLAC__ASSERT(0 != encoder->protected_);
  423. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  424. return false;
  425. return OggFLAC__seekable_stream_encoder_disable_constant_subframes(encoder->private_->seekable_stream_encoder, value);
  426. }
  427. OggFLAC_API FLAC__bool OggFLAC__file_encoder_disable_fixed_subframes(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  428. {
  429. FLAC__ASSERT(0 != encoder);
  430. FLAC__ASSERT(0 != encoder->private_);
  431. FLAC__ASSERT(0 != encoder->protected_);
  432. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  433. return false;
  434. return OggFLAC__seekable_stream_encoder_disable_fixed_subframes(encoder->private_->seekable_stream_encoder, value);
  435. }
  436. OggFLAC_API FLAC__bool OggFLAC__file_encoder_disable_verbatim_subframes(OggFLAC__FileEncoder *encoder, FLAC__bool value)
  437. {
  438. FLAC__ASSERT(0 != encoder);
  439. FLAC__ASSERT(0 != encoder->private_);
  440. FLAC__ASSERT(0 != encoder->protected_);
  441. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_UNINITIALIZED)
  442. return false;
  443. return OggFLAC__seekable_stream_encoder_disable_verbatim_subframes(encoder->private_->seekable_stream_encoder, value);
  444. }
  445. OggFLAC_API OggFLAC__FileEncoderState OggFLAC__file_encoder_get_state(const OggFLAC__FileEncoder *encoder)
  446. {
  447. FLAC__ASSERT(0 != encoder);
  448. FLAC__ASSERT(0 != encoder->protected_);
  449. return encoder->protected_->state;
  450. }
  451. OggFLAC_API OggFLAC__SeekableStreamEncoderState OggFLAC__file_encoder_get_seekable_stream_encoder_state(const OggFLAC__FileEncoder *encoder)
  452. {
  453. FLAC__ASSERT(0 != encoder);
  454. FLAC__ASSERT(0 != encoder->private_);
  455. return OggFLAC__seekable_stream_encoder_get_state(encoder->private_->seekable_stream_encoder);
  456. }
  457. OggFLAC_API FLAC__StreamEncoderState OggFLAC__file_encoder_get_FLAC_stream_encoder_state(const OggFLAC__FileEncoder *encoder)
  458. {
  459. FLAC__ASSERT(0 != encoder);
  460. FLAC__ASSERT(0 != encoder->private_);
  461. return OggFLAC__seekable_stream_encoder_get_FLAC_stream_encoder_state(encoder->private_->seekable_stream_encoder);
  462. }
  463. OggFLAC_API FLAC__StreamDecoderState OggFLAC__file_encoder_get_verify_decoder_state(const OggFLAC__FileEncoder *encoder)
  464. {
  465. FLAC__ASSERT(0 != encoder);
  466. FLAC__ASSERT(0 != encoder->private_);
  467. return OggFLAC__seekable_stream_encoder_get_verify_decoder_state(encoder->private_->seekable_stream_encoder);
  468. }
  469. OggFLAC_API const char *OggFLAC__file_encoder_get_resolved_state_string(const OggFLAC__FileEncoder *encoder)
  470. {
  471. if(encoder->protected_->state != OggFLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR)
  472. return OggFLAC__FileEncoderStateString[encoder->protected_->state];
  473. else
  474. return OggFLAC__seekable_stream_encoder_get_resolved_state_string(encoder->private_->seekable_stream_encoder);
  475. }
  476. OggFLAC_API void OggFLAC__file_encoder_get_verify_decoder_error_stats(const OggFLAC__FileEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
  477. {
  478. FLAC__ASSERT(0 != encoder);
  479. FLAC__ASSERT(0 != encoder->private_);
  480. OggFLAC__seekable_stream_encoder_get_verify_decoder_error_stats(encoder->private_->seekable_stream_encoder, absolute_sample, frame_number, channel, sample, expected, got);
  481. }
  482. OggFLAC_API FLAC__bool OggFLAC__file_encoder_get_verify(const OggFLAC__FileEncoder *encoder)
  483. {
  484. FLAC__ASSERT(0 != encoder);
  485. FLAC__ASSERT(0 != encoder->private_);
  486. return OggFLAC__seekable_stream_encoder_get_verify(encoder->private_->seekable_stream_encoder);
  487. }
  488. OggFLAC_API FLAC__bool OggFLAC__file_encoder_get_streamable_subset(const OggFLAC__FileEncoder *encoder)
  489. {
  490. FLAC__ASSERT(0 != encoder);
  491. FLAC__ASSERT(0 != encoder->private_);
  492. return OggFLAC__seekable_stream_encoder_get_streamable_subset(encoder->private_->seekable_stream_encoder);
  493. }
  494. OggFLAC_API FLAC__bool OggFLAC__file_encoder_get_do_mid_side_stereo(const OggFLAC__FileEncoder *encoder)
  495. {
  496. FLAC__ASSERT(0 != encoder);
  497. FLAC__ASSERT(0 != encoder->private_);
  498. return OggFLAC__seekable_stream_encoder_get_do_mid_side_stereo(encoder->private_->seekable_stream_encoder);
  499. }
  500. OggFLAC_API FLAC__bool OggFLAC__file_encoder_get_loose_mid_side_stereo(const OggFLAC__FileEncoder *encoder)
  501. {
  502. FLAC__ASSERT(0 != encoder);
  503. FLAC__ASSERT(0 != encoder->private_);
  504. return OggFLAC__seekable_stream_encoder_get_loose_mid_side_stereo(encoder->private_->seekable_stream_encoder);
  505. }
  506. OggFLAC_API unsigned OggFLAC__file_encoder_get_channels(const OggFLAC__FileEncoder *encoder)
  507. {
  508. FLAC__ASSERT(0 != encoder);
  509. FLAC__ASSERT(0 != encoder->private_);
  510. return OggFLAC__seekable_stream_encoder_get_channels(encoder->private_->seekable_stream_encoder);
  511. }
  512. OggFLAC_API unsigned OggFLAC__file_encoder_get_bits_per_sample(const OggFLAC__FileEncoder *encoder)
  513. {
  514. FLAC__ASSERT(0 != encoder);
  515. FLAC__ASSERT(0 != encoder->private_);
  516. return OggFLAC__seekable_stream_encoder_get_bits_per_sample(encoder->private_->seekable_stream_encoder);
  517. }
  518. OggFLAC_API unsigned OggFLAC__file_encoder_get_sample_rate(const OggFLAC__FileEncoder *encoder)
  519. {
  520. FLAC__ASSERT(0 != encoder);
  521. FLAC__ASSERT(0 != encoder->private_);
  522. return OggFLAC__seekable_stream_encoder_get_sample_rate(encoder->private_->seekable_stream_encoder);
  523. }
  524. OggFLAC_API unsigned OggFLAC__file_encoder_get_blocksize(const OggFLAC__FileEncoder *encoder)
  525. {
  526. FLAC__ASSERT(0 != encoder);
  527. FLAC__ASSERT(0 != encoder->private_);
  528. return OggFLAC__seekable_stream_encoder_get_blocksize(encoder->private_->seekable_stream_encoder);
  529. }
  530. OggFLAC_API unsigned OggFLAC__file_encoder_get_max_lpc_order(const OggFLAC__FileEncoder *encoder)
  531. {
  532. FLAC__ASSERT(0 != encoder);
  533. FLAC__ASSERT(0 != encoder->private_);
  534. return OggFLAC__seekable_stream_encoder_get_max_lpc_order(encoder->private_->seekable_stream_encoder);
  535. }
  536. OggFLAC_API unsigned OggFLAC__file_encoder_get_qlp_coeff_precision(const OggFLAC__FileEncoder *encoder)
  537. {
  538. FLAC__ASSERT(0 != encoder);
  539. FLAC__ASSERT(0 != encoder->private_);
  540. return OggFLAC__seekable_stream_encoder_get_qlp_coeff_precision(encoder->private_->seekable_stream_encoder);
  541. }
  542. OggFLAC_API FLAC__bool OggFLAC__file_encoder_get_do_qlp_coeff_prec_search(const OggFLAC__FileEncoder *encoder)
  543. {
  544. FLAC__ASSERT(0 != encoder);
  545. FLAC__ASSERT(0 != encoder->private_);
  546. return OggFLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(encoder->private_->seekable_stream_encoder);
  547. }
  548. OggFLAC_API FLAC__bool OggFLAC__file_encoder_get_do_escape_coding(const OggFLAC__FileEncoder *encoder)
  549. {
  550. FLAC__ASSERT(0 != encoder);
  551. FLAC__ASSERT(0 != encoder->private_);
  552. return OggFLAC__seekable_stream_encoder_get_do_escape_coding(encoder->private_->seekable_stream_encoder);
  553. }
  554. OggFLAC_API FLAC__bool OggFLAC__file_encoder_get_do_exhaustive_model_search(const OggFLAC__FileEncoder *encoder)
  555. {
  556. FLAC__ASSERT(0 != encoder);
  557. FLAC__ASSERT(0 != encoder->private_);
  558. return OggFLAC__seekable_stream_encoder_get_do_exhaustive_model_search(encoder->private_->seekable_stream_encoder);
  559. }
  560. OggFLAC_API unsigned OggFLAC__file_encoder_get_min_residual_partition_order(const OggFLAC__FileEncoder *encoder)
  561. {
  562. FLAC__ASSERT(0 != encoder);
  563. FLAC__ASSERT(0 != encoder->private_);
  564. return OggFLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder->private_->seekable_stream_encoder);
  565. }
  566. OggFLAC_API unsigned OggFLAC__file_encoder_get_max_residual_partition_order(const OggFLAC__FileEncoder *encoder)
  567. {
  568. FLAC__ASSERT(0 != encoder);
  569. FLAC__ASSERT(0 != encoder->private_);
  570. return OggFLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder->private_->seekable_stream_encoder);
  571. }
  572. OggFLAC_API unsigned OggFLAC__file_encoder_get_rice_parameter_search_dist(const OggFLAC__FileEncoder *encoder)
  573. {
  574. FLAC__ASSERT(0 != encoder);
  575. FLAC__ASSERT(0 != encoder->private_);
  576. return OggFLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder->private_->seekable_stream_encoder);
  577. }
  578. OggFLAC_API FLAC__uint64 OggFLAC__file_encoder_get_total_samples_estimate(const OggFLAC__FileEncoder *encoder)
  579. {
  580. FLAC__ASSERT(0 != encoder);
  581. FLAC__ASSERT(0 != encoder->private_);
  582. return OggFLAC__seekable_stream_encoder_get_total_samples_estimate(encoder->private_->seekable_stream_encoder);
  583. }
  584. OggFLAC_API FLAC__bool OggFLAC__file_encoder_process(OggFLAC__FileEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
  585. {
  586. FLAC__ASSERT(0 != encoder);
  587. FLAC__ASSERT(0 != encoder->private_);
  588. if(!OggFLAC__seekable_stream_encoder_process(encoder->private_->seekable_stream_encoder, buffer, samples)) {
  589. encoder->protected_->state = OggFLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
  590. return false;
  591. }
  592. else
  593. return true;
  594. }
  595. /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
  596. OggFLAC_API FLAC__bool OggFLAC__file_encoder_process_interleaved(OggFLAC__FileEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
  597. {
  598. FLAC__ASSERT(0 != encoder);
  599. FLAC__ASSERT(0 != encoder->private_);
  600. if(!OggFLAC__seekable_stream_encoder_process_interleaved(encoder->private_->seekable_stream_encoder, buffer, samples)) {
  601. encoder->protected_->state = OggFLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR;
  602. return false;
  603. }
  604. else
  605. return true;
  606. }
  607. /***********************************************************************
  608.  *
  609.  * Private class methods
  610.  *
  611.  ***********************************************************************/
  612. void set_defaults_(OggFLAC__FileEncoder *encoder)
  613. {
  614. FLAC__ASSERT(0 != encoder);
  615. FLAC__ASSERT(0 != encoder->private_);
  616. encoder->private_->progress_callback = 0;
  617. encoder->private_->client_data = 0;
  618. encoder->private_->total_frames_estimate = 0;
  619. encoder->private_->filename = 0;
  620. }
  621. OggFLAC__SeekableStreamEncoderReadStatus read_callback_(const OggFLAC__SeekableStreamEncoder *encoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  622. {
  623. OggFLAC__FileEncoder *file_encoder = (OggFLAC__FileEncoder*)client_data;
  624. (void)encoder;
  625. FLAC__ASSERT(0 != file_encoder);
  626. *bytes = (unsigned)fread(buffer, 1, *bytes, file_encoder->private_->file);
  627. if (*bytes == 0) {
  628. if (feof(file_encoder->private_->file))
  629. return OggFLAC__SEEKABLE_STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
  630. else if (ferror(file_encoder->private_->file))
  631. return OggFLAC__SEEKABLE_STREAM_ENCODER_READ_STATUS_ABORT;
  632. }
  633. return OggFLAC__SEEKABLE_STREAM_ENCODER_READ_STATUS_CONTINUE;
  634. }
  635. FLAC__SeekableStreamEncoderSeekStatus seek_callback_(const OggFLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  636. {
  637. OggFLAC__FileEncoder *file_encoder = (OggFLAC__FileEncoder*)client_data;
  638. (void)encoder;
  639. FLAC__ASSERT(0 != file_encoder);
  640. if(fseek(file_encoder->private_->file, (long)absolute_byte_offset, SEEK_SET) < 0)
  641. return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR;
  642. else
  643. return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
  644. }
  645. FLAC__SeekableStreamEncoderTellStatus tell_callback_(const OggFLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  646. {
  647. OggFLAC__FileEncoder *file_encoder = (OggFLAC__FileEncoder*)client_data;
  648. long offset;
  649. (void)encoder;
  650. FLAC__ASSERT(0 != file_encoder);
  651. offset = ftell(file_encoder->private_->file);
  652. if(offset < 0) {
  653. return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_ERROR;
  654. }
  655. else {
  656. *absolute_byte_offset = (FLAC__uint64)offset;
  657. return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK;
  658. }
  659. }
  660. #ifdef FLAC__VALGRIND_TESTING
  661. static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  662. {
  663. size_t ret = fwrite(ptr, size, nmemb, stream);
  664. if(!ferror(stream))
  665. fflush(stream);
  666. return ret;
  667. }
  668. #else
  669. #define local__fwrite fwrite
  670. #endif
  671. FLAC__StreamEncoderWriteStatus write_callback_(const OggFLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
  672. {
  673. OggFLAC__FileEncoder *file_encoder = (OggFLAC__FileEncoder*)client_data;
  674. (void)encoder;
  675. FLAC__ASSERT(0 != file_encoder);
  676. if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, file_encoder->private_->file) == bytes) {
  677. file_encoder->private_->bytes_written += bytes;
  678. file_encoder->private_->samples_written += samples;
  679. /* we keep a high watermark on the number of frames written
  680.  * because when the encoder goes back to write metadata,
  681.  * 'current_frame' will drop back to 0.
  682.  */
  683. file_encoder->private_->frames_written = max(file_encoder->private_->frames_written, current_frame+1);
  684. /*@@@ We would like to add an '&& samples > 0' to the if
  685.  * clause here but currently because of the nature of our Ogg
  686.  * writing implementation, 'samples' is always 0 (see
  687.  * ogg_encoder_aspect.c).  The downside is extra progress
  688.  * callbacks.
  689.  */
  690. if(0 != file_encoder->private_->progress_callback)
  691. 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);
  692. return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  693. }
  694. else
  695. return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
  696. }