flac_stream_encoder_framing.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 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. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <stdio.h>
  35. #include <string.h> /* for strlen() */
  36. #include "flac_private_stream_encoder_framing.h"
  37. #include "flac_private_crc.h"
  38. #include "flac_FLAC_assert.h"
  39. #ifdef max
  40. #undef max
  41. #endif
  42. #define max(x,y) ((x)>(y)?(x):(y))
  43. static FLAC__bool add_entropy_coding_method_(FLAC__BitWriter *bw, const FLAC__EntropyCodingMethod *method);
  44. static FLAC__bool add_residual_partitioned_rice_(FLAC__BitWriter *bw, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order, const FLAC__bool is_extended);
  45. FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitWriter *bw)
  46. {
  47. unsigned i, j;
  48. const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);
  49. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
  50. return false;
  51. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
  52. return false;
  53. /*
  54. * First, for VORBIS_COMMENTs, adjust the length to reflect our vendor string
  55. */
  56. i = metadata->length;
  57. if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
  58. FLAC__ASSERT(metadata->data.vorbis_comment.vendor_string.length == 0 || 0 != metadata->data.vorbis_comment.vendor_string.entry);
  59. i -= metadata->data.vorbis_comment.vendor_string.length;
  60. i += vendor_string_length;
  61. }
  62. FLAC__ASSERT(i < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
  63. if(!FLAC__bitwriter_write_raw_uint32(bw, i, FLAC__STREAM_METADATA_LENGTH_LEN))
  64. return false;
  65. switch(metadata->type) {
  66. case FLAC__METADATA_TYPE_STREAMINFO:
  67. FLAC__ASSERT(metadata->data.stream_info.min_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN));
  68. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN))
  69. return false;
  70. FLAC__ASSERT(metadata->data.stream_info.max_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN));
  71. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
  72. return false;
  73. FLAC__ASSERT(metadata->data.stream_info.min_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
  74. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_framesize, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
  75. return false;
  76. FLAC__ASSERT(metadata->data.stream_info.max_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
  77. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_framesize, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
  78. return false;
  79. FLAC__ASSERT(FLAC__format_sample_rate_is_valid(metadata->data.stream_info.sample_rate));
  80. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.sample_rate, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
  81. return false;
  82. FLAC__ASSERT(metadata->data.stream_info.channels > 0);
  83. FLAC__ASSERT(metadata->data.stream_info.channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN));
  84. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.channels-1, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
  85. return false;
  86. FLAC__ASSERT(metadata->data.stream_info.bits_per_sample > 0);
  87. FLAC__ASSERT(metadata->data.stream_info.bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
  88. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.bits_per_sample-1, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
  89. return false;
  90. if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
  91. return false;
  92. if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.stream_info.md5sum, 16))
  93. return false;
  94. break;
  95. case FLAC__METADATA_TYPE_PADDING:
  96. if(!FLAC__bitwriter_write_zeroes(bw, metadata->length * 8))
  97. return false;
  98. break;
  99. case FLAC__METADATA_TYPE_APPLICATION:
  100. if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8))
  101. return false;
  102. if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.data, metadata->length - (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)))
  103. return false;
  104. break;
  105. case FLAC__METADATA_TYPE_SEEKTABLE:
  106. for(i = 0; i < metadata->data.seek_table.num_points; i++) {
  107. if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].sample_number, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
  108. return false;
  109. if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
  110. return false;
  111. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
  112. return false;
  113. }
  114. break;
  115. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  116. if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, vendor_string_length))
  117. return false;
  118. if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)FLAC__VENDOR_STRING, vendor_string_length))
  119. return false;
  120. if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.num_comments))
  121. return false;
  122. for(i = 0; i < metadata->data.vorbis_comment.num_comments; i++) {
  123. if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.comments[i].length))
  124. return false;
  125. if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.vorbis_comment.comments[i].entry, metadata->data.vorbis_comment.comments[i].length))
  126. return false;
  127. }
  128. break;
  129. case FLAC__METADATA_TYPE_CUESHEET:
  130. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
  131. if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.cue_sheet.media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
  132. return false;
  133. if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.cue_sheet.lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
  134. return false;
  135. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.is_cd? 1 : 0, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
  136. return false;
  137. if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
  138. return false;
  139. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.num_tracks, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
  140. return false;
  141. for(i = 0; i < metadata->data.cue_sheet.num_tracks; i++) {
  142. const FLAC__StreamMetadata_CueSheet_Track *track = metadata->data.cue_sheet.tracks + i;
  143. if(!FLAC__bitwriter_write_raw_uint64(bw, track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
  144. return false;
  145. if(!FLAC__bitwriter_write_raw_uint32(bw, track->number, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
  146. return false;
  147. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
  148. if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
  149. return false;
  150. if(!FLAC__bitwriter_write_raw_uint32(bw, track->type, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
  151. return false;
  152. if(!FLAC__bitwriter_write_raw_uint32(bw, track->pre_emphasis, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
  153. return false;
  154. if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
  155. return false;
  156. if(!FLAC__bitwriter_write_raw_uint32(bw, track->num_indices, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
  157. return false;
  158. for(j = 0; j < track->num_indices; j++) {
  159. const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j;
  160. if(!FLAC__bitwriter_write_raw_uint64(bw, index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
  161. return false;
  162. if(!FLAC__bitwriter_write_raw_uint32(bw, index->number, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
  163. return false;
  164. if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
  165. return false;
  166. }
  167. }
  168. break;
  169. case FLAC__METADATA_TYPE_PICTURE:
  170. {
  171. size_t len;
  172. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
  173. return false;
  174. len = strlen(metadata->data.picture.mime_type);
  175. if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
  176. return false;
  177. if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.picture.mime_type, len))
  178. return false;
  179. len = strlen((const char *)metadata->data.picture.description);
  180. if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
  181. return false;
  182. if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.description, len))
  183. return false;
  184. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
  185. return false;
  186. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
  187. return false;
  188. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
  189. return false;
  190. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
  191. return false;
  192. if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.data_length, FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
  193. return false;
  194. if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.data, metadata->data.picture.data_length))
  195. return false;
  196. }
  197. break;
  198. default:
  199. if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.unknown.data, metadata->length))
  200. return false;
  201. break;
  202. }
  203. FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));
  204. return true;
  205. }
  206. FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitWriter *bw)
  207. {
  208. unsigned u, blocksize_hint, sample_rate_hint;
  209. FLAC__byte crc;
  210. FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));
  211. if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
  212. return false;
  213. if(!FLAC__bitwriter_write_raw_uint32(bw, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
  214. return false;
  215. if(!FLAC__bitwriter_write_raw_uint32(bw, (header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER)? 0 : 1, FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN))
  216. return false;
  217. FLAC__ASSERT(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
  218. /* when this assertion holds true, any legal blocksize can be expressed in the frame header */
  219. FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535u);
  220. blocksize_hint = 0;
  221. switch(header->blocksize) {
  222. case 192: u = 1; break;
  223. case 576: u = 2; break;
  224. case 1152: u = 3; break;
  225. case 2304: u = 4; break;
  226. case 4608: u = 5; break;
  227. case 256: u = 8; break;
  228. case 512: u = 9; break;
  229. case 1024: u = 10; break;
  230. case 2048: u = 11; break;
  231. case 4096: u = 12; break;
  232. case 8192: u = 13; break;
  233. case 16384: u = 14; break;
  234. case 32768: u = 15; break;
  235. default:
  236. if(header->blocksize <= 0x100)
  237. blocksize_hint = u = 6;
  238. else
  239. blocksize_hint = u = 7;
  240. break;
  241. }
  242. if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
  243. return false;
  244. FLAC__ASSERT(FLAC__format_sample_rate_is_valid(header->sample_rate));
  245. sample_rate_hint = 0;
  246. switch(header->sample_rate) {
  247. case 88200: u = 1; break;
  248. case 176400: u = 2; break;
  249. case 192000: u = 3; break;
  250. case 8000: u = 4; break;
  251. case 16000: u = 5; break;
  252. case 22050: u = 6; break;
  253. case 24000: u = 7; break;
  254. case 32000: u = 8; break;
  255. case 44100: u = 9; break;
  256. case 48000: u = 10; break;
  257. case 96000: u = 11; break;
  258. default:
  259. if(header->sample_rate <= 255000 && header->sample_rate % 1000 == 0)
  260. sample_rate_hint = u = 12;
  261. else if(header->sample_rate % 10 == 0)
  262. sample_rate_hint = u = 14;
  263. else if(header->sample_rate <= 0xffff)
  264. sample_rate_hint = u = 13;
  265. else
  266. u = 0;
  267. break;
  268. }
  269. if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
  270. return false;
  271. FLAC__ASSERT(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
  272. switch(header->channel_assignment) {
  273. case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
  274. u = header->channels - 1;
  275. break;
  276. case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
  277. FLAC__ASSERT(header->channels == 2);
  278. u = 8;
  279. break;
  280. case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
  281. FLAC__ASSERT(header->channels == 2);
  282. u = 9;
  283. break;
  284. case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
  285. FLAC__ASSERT(header->channels == 2);
  286. u = 10;
  287. break;
  288. default:
  289. FLAC__ASSERT(0);
  290. }
  291. if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
  292. return false;
  293. FLAC__ASSERT(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
  294. switch(header->bits_per_sample) {
  295. case 8 : u = 1; break;
  296. case 12: u = 2; break;
  297. case 16: u = 4; break;
  298. case 20: u = 5; break;
  299. case 24: u = 6; break;
  300. default: u = 0; break;
  301. }
  302. if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
  303. return false;
  304. if(!FLAC__bitwriter_write_raw_uint32(bw, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
  305. return false;
  306. if(header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
  307. if(!FLAC__bitwriter_write_utf8_uint32(bw, header->number.frame_number))
  308. return false;
  309. }
  310. else {
  311. if(!FLAC__bitwriter_write_utf8_uint64(bw, header->number.sample_number))
  312. return false;
  313. }
  314. if(blocksize_hint)
  315. if(!FLAC__bitwriter_write_raw_uint32(bw, header->blocksize-1, (blocksize_hint==6)? 8:16))
  316. return false;
  317. switch(sample_rate_hint) {
  318. case 12:
  319. if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate / 1000, 8))
  320. return false;
  321. break;
  322. case 13:
  323. if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate, 16))
  324. return false;
  325. break;
  326. case 14:
  327. if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate / 10, 16))
  328. return false;
  329. break;
  330. }
  331. /* write the CRC */
  332. if(!FLAC__bitwriter_get_write_crc8(bw, &crc))
  333. return false;
  334. if(!FLAC__bitwriter_write_raw_uint32(bw, crc, FLAC__FRAME_HEADER_CRC_LEN))
  335. return false;
  336. return true;
  337. }
  338. FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
  339. {
  340. FLAC__bool ok;
  341. ok =
  342. FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN) &&
  343. (wasted_bits? FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1) : true) &&
  344. FLAC__bitwriter_write_raw_int32(bw, subframe->value, subframe_bps)
  345. ;
  346. return ok;
  347. }
  348. FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
  349. {
  350. unsigned i;
  351. if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK | (subframe->order<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
  352. return false;
  353. if(wasted_bits)
  354. if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
  355. return false;
  356. for(i = 0; i < subframe->order; i++)
  357. if(!FLAC__bitwriter_write_raw_int32(bw, subframe->warmup[i], subframe_bps))
  358. return false;
  359. if(!add_entropy_coding_method_(bw, &subframe->entropy_coding_method))
  360. return false;
  361. switch(subframe->entropy_coding_method.type) {
  362. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  363. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
  364. if(!add_residual_partitioned_rice_(
  365. bw,
  366. subframe->residual,
  367. residual_samples,
  368. subframe->order,
  369. subframe->entropy_coding_method.data.partitioned_rice.contents->parameters,
  370. subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits,
  371. subframe->entropy_coding_method.data.partitioned_rice.order,
  372. /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2
  373. ))
  374. return false;
  375. break;
  376. default:
  377. FLAC__ASSERT(0);
  378. }
  379. return true;
  380. }
  381. FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
  382. {
  383. unsigned i;
  384. if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK | ((subframe->order-1)<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
  385. return false;
  386. if(wasted_bits)
  387. if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
  388. return false;
  389. for(i = 0; i < subframe->order; i++)
  390. if(!FLAC__bitwriter_write_raw_int32(bw, subframe->warmup[i], subframe_bps))
  391. return false;
  392. if(!FLAC__bitwriter_write_raw_uint32(bw, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
  393. return false;
  394. if(!FLAC__bitwriter_write_raw_int32(bw, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
  395. return false;
  396. for(i = 0; i < subframe->order; i++)
  397. if(!FLAC__bitwriter_write_raw_int32(bw, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
  398. return false;
  399. if(!add_entropy_coding_method_(bw, &subframe->entropy_coding_method))
  400. return false;
  401. switch(subframe->entropy_coding_method.type) {
  402. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  403. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
  404. if(!add_residual_partitioned_rice_(
  405. bw,
  406. subframe->residual,
  407. residual_samples,
  408. subframe->order,
  409. subframe->entropy_coding_method.data.partitioned_rice.contents->parameters,
  410. subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits,
  411. subframe->entropy_coding_method.data.partitioned_rice.order,
  412. /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2
  413. ))
  414. return false;
  415. break;
  416. default:
  417. FLAC__ASSERT(0);
  418. }
  419. return true;
  420. }
  421. FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
  422. {
  423. unsigned i;
  424. const FLAC__int32 *signal = subframe->data;
  425. if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
  426. return false;
  427. if(wasted_bits)
  428. if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
  429. return false;
  430. for(i = 0; i < samples; i++)
  431. if(!FLAC__bitwriter_write_raw_int32(bw, signal[i], subframe_bps))
  432. return false;
  433. return true;
  434. }
  435. FLAC__bool add_entropy_coding_method_(FLAC__BitWriter *bw, const FLAC__EntropyCodingMethod *method)
  436. {
  437. if(!FLAC__bitwriter_write_raw_uint32(bw, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
  438. return false;
  439. switch(method->type) {
  440. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  441. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
  442. if(!FLAC__bitwriter_write_raw_uint32(bw, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
  443. return false;
  444. break;
  445. default:
  446. FLAC__ASSERT(0);
  447. }
  448. return true;
  449. }
  450. FLAC__bool add_residual_partitioned_rice_(FLAC__BitWriter *bw, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order, const FLAC__bool is_extended)
  451. {
  452. const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
  453. const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
  454. if(partition_order == 0) {
  455. unsigned i;
  456. if(raw_bits[0] == 0) {
  457. if(!FLAC__bitwriter_write_raw_uint32(bw, rice_parameters[0], plen))
  458. return false;
  459. if(!FLAC__bitwriter_write_rice_signed_block(bw, residual, residual_samples, rice_parameters[0]))
  460. return false;
  461. }
  462. else {
  463. FLAC__ASSERT(rice_parameters[0] == 0);
  464. if(!FLAC__bitwriter_write_raw_uint32(bw, pesc, plen))
  465. return false;
  466. if(!FLAC__bitwriter_write_raw_uint32(bw, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
  467. return false;
  468. for(i = 0; i < residual_samples; i++) {
  469. if(!FLAC__bitwriter_write_raw_int32(bw, residual[i], raw_bits[0]))
  470. return false;
  471. }
  472. }
  473. return true;
  474. }
  475. else {
  476. unsigned i, j, k = 0, k_last = 0;
  477. unsigned partition_samples;
  478. const unsigned default_partition_samples = (residual_samples+predictor_order) >> partition_order;
  479. for(i = 0; i < (1u<<partition_order); i++) {
  480. partition_samples = default_partition_samples;
  481. if(i == 0)
  482. partition_samples -= predictor_order;
  483. k += partition_samples;
  484. if(raw_bits[i] == 0) {
  485. if(!FLAC__bitwriter_write_raw_uint32(bw, rice_parameters[i], plen))
  486. return false;
  487. if(!FLAC__bitwriter_write_rice_signed_block(bw, residual+k_last, k-k_last, rice_parameters[i]))
  488. return false;
  489. }
  490. else {
  491. if(!FLAC__bitwriter_write_raw_uint32(bw, pesc, plen))
  492. return false;
  493. if(!FLAC__bitwriter_write_raw_uint32(bw, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
  494. return false;
  495. for(j = k_last; j < k; j++) {
  496. if(!FLAC__bitwriter_write_raw_int32(bw, residual[j], raw_bits[i]))
  497. return false;
  498. }
  499. }
  500. k_last = k;
  501. }
  502. return true;
  503. }
  504. }