bitstream.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * MP3 bitstream Output interface for LAME
  3. *
  4. * Copyright (c) 1999 Takehiro TOMINAGA
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <stdlib.h>
  25. #include <assert.h>
  26. #include <stdio.h>
  27. #include "tables.h"
  28. #include "bitstream.h"
  29. #include "quantize.h"
  30. #include "quantize_pvt.h"
  31. #include "version.h"
  32. #ifdef WITH_DMALLOC
  33. #include <dmalloc.h>
  34. #endif
  35. /* This is the scfsi_band table from 2.4.2.7 of the IS */
  36. const int scfsi_band[5] = { 0, 6, 11, 16, 21 };
  37. /* unsigned int is at least this large: */
  38. /* we work with ints, so when doing bit manipulation, we limit
  39. * ourselves to MAX_LENGTH-2 just to be on the safe side */
  40. #define MAX_LENGTH 32
  41. #ifdef DEBUG
  42. static int hoge, hogege;
  43. #endif
  44. void putheader_bits(lame_internal_flags *gfc,int w_ptr)
  45. {
  46. Bit_stream_struc *bs;
  47. bs = &gfc->bs;
  48. #ifdef DEBUG
  49. hoge += gfc->sideinfo_len * 8;
  50. hogege += gfc->sideinfo_len * 8;
  51. #endif
  52. memcpy(&bs->buf[bs->buf_byte_idx], gfc->header[gfc->w_ptr].buf,
  53. gfc->sideinfo_len);
  54. bs->buf_byte_idx += gfc->sideinfo_len;
  55. bs->totbit += gfc->sideinfo_len * 8;
  56. gfc->w_ptr = (gfc->w_ptr + 1) & (MAX_HEADER_BUF - 1);
  57. }
  58. /*write j bits into the bit stream */
  59. static void
  60. putbits2(lame_global_flags *gfp, int val, int j)
  61. {
  62. lame_internal_flags *gfc=gfp->internal_flags;
  63. Bit_stream_struc *bs;
  64. bs = &gfc->bs;
  65. // assert(j < MAX_LENGTH-2);
  66. while (j > 0) {
  67. int k;
  68. if (bs->buf_bit_idx == 0) {
  69. bs->buf_bit_idx = 8;
  70. bs->buf_byte_idx++;
  71. // assert(bs->buf_byte_idx < BUFFER_SIZE);
  72. // assert(gfc->header[gfc->w_ptr].write_timing >= bs->totbit);
  73. if (gfc->header[gfc->w_ptr].write_timing == bs->totbit)
  74. putheader_bits(gfc, gfc->w_ptr);
  75. bs->buf[bs->buf_byte_idx] = 0;
  76. }
  77. k = Min(j, bs->buf_bit_idx);
  78. j -= k;
  79. bs->buf_bit_idx -= k;
  80. // assert (j < MAX_LENGTH); /* 32 too large on 32 bit machines */
  81. // assert (bs->buf_bit_idx < MAX_LENGTH);
  82. bs->buf[bs->buf_byte_idx] |= (val >> j) << bs->buf_bit_idx;
  83. bs->totbit += k;
  84. }
  85. }
  86. /*write j bits into the bit stream, ignoring frame headers */
  87. static void
  88. putbits_noheaders(lame_global_flags *gfp, int val, int j)
  89. {
  90. lame_internal_flags *gfc=gfp->internal_flags;
  91. Bit_stream_struc *bs;
  92. bs = &gfc->bs;
  93. // assert(j < MAX_LENGTH-2);
  94. while (j > 0) {
  95. int k;
  96. if (bs->buf_bit_idx == 0) {
  97. bs->buf_bit_idx = 8;
  98. bs->buf_byte_idx++;
  99. // assert(bs->buf_byte_idx < BUFFER_SIZE);
  100. bs->buf[bs->buf_byte_idx] = 0;
  101. }
  102. k = Min(j, bs->buf_bit_idx);
  103. j -= k;
  104. bs->buf_bit_idx -= k;
  105. // assert (j < MAX_LENGTH); /* 32 too large on 32 bit machines */
  106. // assert (bs->buf_bit_idx < MAX_LENGTH);
  107. bs->buf[bs->buf_byte_idx] |= (val >> j) << bs->buf_bit_idx;
  108. bs->totbit += k;
  109. }
  110. }
  111. /*
  112. Some combinations of bitrate, Fs, and stereo make it impossible to stuff
  113. out a frame using just main_data, due to the limited number of bits to
  114. indicate main_data_length. In these situations, we put stuffing bits into
  115. the ancillary data...
  116. */
  117. static void
  118. drain_into_ancillary(lame_global_flags *gfp,int remainingBits)
  119. {
  120. lame_internal_flags *gfc=gfp->internal_flags;
  121. int i;
  122. assert(remainingBits >= 0);
  123. if (remainingBits >= 8) {
  124. putbits2(gfp,0x4c,8);
  125. remainingBits -= 8;
  126. }
  127. if (remainingBits >= 8) {
  128. putbits2(gfp,0x41,8);
  129. remainingBits -= 8;
  130. }
  131. if (remainingBits >= 8) {
  132. putbits2(gfp,0x4d,8);
  133. remainingBits -= 8;
  134. }
  135. if (remainingBits >= 8) {
  136. putbits2(gfp,0x45,8);
  137. remainingBits -= 8;
  138. }
  139. if (remainingBits >= 32) {
  140. const char *version = get_lame_short_version ();
  141. if (remainingBits >= 32)
  142. for (i=0; i<(int)strlen(version) && remainingBits >=8 ; ++i) {
  143. remainingBits -= 8;
  144. putbits2(gfp,version[i],8);
  145. }
  146. }
  147. for (; remainingBits >= 1; remainingBits -= 1 ) {
  148. putbits2 ( gfp, gfc->ancillary_flag, 1 );
  149. gfc->ancillary_flag ^= 1;
  150. }
  151. assert (remainingBits == 0);
  152. }
  153. /*write N bits into the header */
  154. inline static void
  155. writeheader(lame_internal_flags *gfc,int val, int j)
  156. {
  157. int ptr = gfc->header[gfc->h_ptr].ptr;
  158. while (j > 0) {
  159. int k = Min(j, 8 - (ptr & 7));
  160. j -= k;
  161. assert (j < MAX_LENGTH); /* >> 32 too large for 32 bit machines */
  162. gfc->header[gfc->h_ptr].buf[ptr >> 3]
  163. |= ((val >> j)) << (8 - (ptr & 7) - k);
  164. ptr += k;
  165. }
  166. gfc->header[gfc->h_ptr].ptr = ptr;
  167. }
  168. /* (jo) this wrapper function for BF_addEntry() updates also the crc */
  169. static void
  170. CRC_writeheader(lame_internal_flags *gfc, int value, int length,int *crc)
  171. {
  172. int bit = 1 << length;
  173. assert(length < MAX_LENGTH-2);
  174. while((bit >>= 1)){
  175. *crc <<= 1;
  176. if (!(*crc & 0x10000) ^ !(value & bit))
  177. *crc ^= CRC16_POLYNOMIAL;
  178. }
  179. *crc &= 0xffff;
  180. writeheader(gfc,value, length);
  181. }
  182. void main_CRC_init (void) {}
  183. inline static void
  184. encodeSideInfo2(lame_global_flags *gfp,int bitsPerFrame)
  185. {
  186. lame_internal_flags *gfc=gfp->internal_flags;
  187. III_side_info_t *l3_side;
  188. int gr, ch;
  189. int crc;
  190. l3_side = &gfc->l3_side;
  191. gfc->header[gfc->h_ptr].ptr = 0;
  192. memset(gfc->header[gfc->h_ptr].buf, 0, gfc->sideinfo_len);
  193. crc = 0xffff; /* (jo) init crc16 for error_protection */
  194. if (gfp->out_samplerate < 16000)
  195. writeheader(gfc,0xffe, 12);
  196. else
  197. writeheader(gfc,0xfff, 12);
  198. writeheader(gfc,(gfp->version), 1);
  199. writeheader(gfc,4 - 3, 2);
  200. writeheader(gfc,(!gfp->error_protection), 1);
  201. /* (jo) from now on call the CRC_writeheader() wrapper to update crc */
  202. CRC_writeheader(gfc,(gfc->bitrate_index), 4,&crc);
  203. CRC_writeheader(gfc,(gfc->samplerate_index), 2,&crc);
  204. CRC_writeheader(gfc,(gfc->padding), 1,&crc);
  205. CRC_writeheader(gfc,(gfp->extension), 1,&crc);
  206. CRC_writeheader(gfc,(gfp->mode), 2,&crc);
  207. CRC_writeheader(gfc,(gfc->mode_ext), 2,&crc);
  208. CRC_writeheader(gfc,(gfp->copyright), 1,&crc);
  209. CRC_writeheader(gfc,(gfp->original), 1,&crc);
  210. CRC_writeheader(gfc,(gfp->emphasis), 2,&crc);
  211. if (gfp->error_protection) {
  212. writeheader(gfc,0, 16); /* dummy */
  213. }
  214. if (gfp->version == 1) {
  215. /* MPEG1 */
  216. assert(l3_side->main_data_begin >= 0);
  217. CRC_writeheader(gfc,(l3_side->main_data_begin), 9,&crc);
  218. if (gfc->channels_out == 2)
  219. CRC_writeheader(gfc,l3_side->private_bits, 3,&crc);
  220. else
  221. CRC_writeheader(gfc,l3_side->private_bits, 5,&crc);
  222. for (ch = 0; ch < gfc->channels_out; ch++) {
  223. int band;
  224. for (band = 0; band < 4; band++) {
  225. CRC_writeheader(gfc,l3_side->scfsi[ch][band], 1,&crc);
  226. }
  227. }
  228. for (gr = 0; gr < 2; gr++) {
  229. for (ch = 0; ch < gfc->channels_out; ch++) {
  230. gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
  231. CRC_writeheader(gfc,gi->part2_3_length, 12,&crc);
  232. CRC_writeheader(gfc,gi->big_values / 2, 9,&crc);
  233. CRC_writeheader(gfc,gi->global_gain, 8,&crc);
  234. CRC_writeheader(gfc,gi->scalefac_compress, 4,&crc);
  235. CRC_writeheader(gfc,gi->window_switching_flag, 1,&crc);
  236. if (gi->window_switching_flag) {
  237. CRC_writeheader(gfc,gi->block_type, 2,&crc);
  238. CRC_writeheader(gfc,gi->mixed_block_flag, 1,&crc);
  239. if (gi->table_select[0] == 14)
  240. gi->table_select[0] = 16;
  241. CRC_writeheader(gfc,gi->table_select[0], 5,&crc);
  242. if (gi->table_select[1] == 14)
  243. gi->table_select[1] = 16;
  244. CRC_writeheader(gfc,gi->table_select[1], 5,&crc);
  245. CRC_writeheader(gfc,gi->subblock_gain[0], 3,&crc);
  246. CRC_writeheader(gfc,gi->subblock_gain[1], 3,&crc);
  247. CRC_writeheader(gfc,gi->subblock_gain[2], 3,&crc);
  248. } else {
  249. assert(gi->block_type == NORM_TYPE);
  250. if (gi->table_select[0] == 14)
  251. gi->table_select[0] = 16;
  252. CRC_writeheader(gfc,gi->table_select[0], 5,&crc);
  253. if (gi->table_select[1] == 14)
  254. gi->table_select[1] = 16;
  255. CRC_writeheader(gfc,gi->table_select[1], 5,&crc);
  256. if (gi->table_select[2] == 14)
  257. gi->table_select[2] = 16;
  258. CRC_writeheader(gfc,gi->table_select[2], 5,&crc);
  259. assert(gi->region0_count < 16U);
  260. assert(gi->region1_count < 8U);
  261. CRC_writeheader(gfc,gi->region0_count, 4,&crc);
  262. CRC_writeheader(gfc,gi->region1_count, 3,&crc);
  263. }
  264. CRC_writeheader(gfc,gi->preflag, 1,&crc);
  265. CRC_writeheader(gfc,gi->scalefac_scale, 1,&crc);
  266. CRC_writeheader(gfc,gi->count1table_select, 1,&crc);
  267. }
  268. }
  269. } else {
  270. /* MPEG2 */
  271. assert(l3_side->main_data_begin >= 0);
  272. CRC_writeheader(gfc,(l3_side->main_data_begin), 8,&crc);
  273. CRC_writeheader(gfc,l3_side->private_bits, gfc->channels_out,&crc);
  274. gr = 0;
  275. for (ch = 0; ch < gfc->channels_out; ch++) {
  276. gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
  277. CRC_writeheader(gfc,gi->part2_3_length, 12,&crc);
  278. CRC_writeheader(gfc,gi->big_values / 2, 9,&crc);
  279. CRC_writeheader(gfc,gi->global_gain, 8,&crc);
  280. CRC_writeheader(gfc,gi->scalefac_compress, 9,&crc);
  281. CRC_writeheader(gfc,gi->window_switching_flag, 1,&crc);
  282. if (gi->window_switching_flag) {
  283. CRC_writeheader(gfc,gi->block_type, 2,&crc);
  284. CRC_writeheader(gfc,gi->mixed_block_flag, 1,&crc);
  285. if (gi->table_select[0] == 14)
  286. gi->table_select[0] = 16;
  287. CRC_writeheader(gfc,gi->table_select[0], 5,&crc);
  288. if (gi->table_select[1] == 14)
  289. gi->table_select[1] = 16;
  290. CRC_writeheader(gfc,gi->table_select[1], 5,&crc);
  291. CRC_writeheader(gfc,gi->subblock_gain[0], 3,&crc);
  292. CRC_writeheader(gfc,gi->subblock_gain[1], 3,&crc);
  293. CRC_writeheader(gfc,gi->subblock_gain[2], 3,&crc);
  294. } else {
  295. if (gi->table_select[0] == 14)
  296. gi->table_select[0] = 16;
  297. CRC_writeheader(gfc,gi->table_select[0], 5,&crc);
  298. if (gi->table_select[1] == 14)
  299. gi->table_select[1] = 16;
  300. CRC_writeheader(gfc,gi->table_select[1], 5,&crc);
  301. if (gi->table_select[2] == 14)
  302. gi->table_select[2] = 16;
  303. CRC_writeheader(gfc,gi->table_select[2], 5,&crc);
  304. assert(gi->region0_count < 16U);
  305. assert(gi->region1_count < 8U);
  306. CRC_writeheader(gfc,gi->region0_count, 4,&crc);
  307. CRC_writeheader(gfc,gi->region1_count, 3,&crc);
  308. }
  309. CRC_writeheader(gfc,gi->scalefac_scale, 1,&crc);
  310. CRC_writeheader(gfc,gi->count1table_select, 1,&crc);
  311. }
  312. }
  313. if (gfp->error_protection) {
  314. /* (jo) error_protection: add crc16 information to header */
  315. gfc->header[gfc->h_ptr].buf[4] = crc >> 8;
  316. gfc->header[gfc->h_ptr].buf[5] = crc & 255;
  317. }
  318. {
  319. int old = gfc->h_ptr;
  320. assert(gfc->header[old].ptr == gfc->sideinfo_len * 8);
  321. gfc->h_ptr = (old + 1) & (MAX_HEADER_BUF - 1);
  322. gfc->header[gfc->h_ptr].write_timing =
  323. gfc->header[old].write_timing + bitsPerFrame;
  324. if (gfc->h_ptr == gfc->w_ptr) {
  325. /* yikes! we are out of header buffer space */
  326. ERRORF(gfc,"Error: MAX_HEADER_BUF too small in bitstream.c \n");
  327. }
  328. }
  329. }
  330. inline static int
  331. huffman_coder_count1(lame_global_flags *gfp,int *ix, gr_info *gi)
  332. {
  333. #ifdef DEBUG
  334. lame_internal_flags *gfc = gfp->internal_flags;
  335. #endif
  336. /* Write count1 area */
  337. const struct huffcodetab *h = &ht[gi->count1table_select + 32];
  338. int i,bits=0;
  339. #ifdef DEBUG
  340. int gegebo = gfc->bs.totbit;
  341. #endif
  342. ix += gi->big_values;
  343. assert(gi->count1table_select < 2);
  344. for (i = (gi->count1 - gi->big_values) / 4; i > 0; --i) {
  345. int huffbits = 0;
  346. int p = 0, v;
  347. v = ix[0];
  348. if (v) {
  349. p += 8;
  350. if (v < 0)
  351. huffbits++;
  352. assert(-1 <= v && v <= 1);
  353. }
  354. v = ix[1];
  355. if (v) {
  356. p += 4;
  357. huffbits *= 2;
  358. if (v < 0)
  359. huffbits++;
  360. assert(-1 <= v && v <= 1);
  361. }
  362. v = ix[2];
  363. if (v) {
  364. p += 2;
  365. huffbits *= 2;
  366. if (v < 0)
  367. huffbits++;
  368. assert(-1 <= v && v <= 1);
  369. }
  370. v = ix[3];
  371. if (v) {
  372. p++;
  373. huffbits *= 2;
  374. if (v < 0)
  375. huffbits++;
  376. assert(-1 <= v && v <= 1);
  377. }
  378. ix += 4;
  379. putbits2(gfp,huffbits + h->table[p], h->hlen[p]);
  380. bits += h->hlen[p];
  381. }
  382. #ifdef DEBUG
  383. DEBUGF("%ld %d %d %d\n",gfc->bs.totbit -gegebo, gi->count1bits, gi->big_values, gi->count1);
  384. #endif
  385. return bits;
  386. }
  387. /*
  388. * Implements the pseudocode of page 98 of the IS
  389. */
  390. static int
  391. HuffmanCode(lame_global_flags* const gfp, int table_select, int x1, int x2)
  392. {
  393. struct huffcodetab* h = ht + table_select;
  394. int code = 0;
  395. int cbits = 0;
  396. int xbits = 0;
  397. int sgn_x1 = 0;
  398. int sgn_x2 = 0;
  399. int linbits = h->xlen;
  400. int xlen = h->xlen;
  401. int ext;
  402. // assert ( table_select > 0 );
  403. if (x1 < 0) {
  404. sgn_x1++;
  405. x1 = -x1;
  406. }
  407. if (x2 < 0) {
  408. sgn_x2++;
  409. x2 = -x2;
  410. }
  411. ext = sgn_x1;
  412. if (table_select > 15) {
  413. /* use ESC-words */
  414. if (x1 > 14) {
  415. int linbits_x1 = x1 - 15;
  416. // assert ( linbits_x1 <= h->linmax );
  417. ext |= linbits_x1 << 1;
  418. xbits = linbits;
  419. x1 = 15;
  420. }
  421. if (x2 > 14) {
  422. int linbits_x2 = x2 - 15;
  423. // assert ( linbits_x2 <= h->linmax );
  424. ext <<= linbits;
  425. ext |= linbits_x2;
  426. xbits += linbits;
  427. x2 = 15;
  428. }
  429. xlen = 16;
  430. }
  431. if (x1 != 0) {
  432. cbits--;
  433. }
  434. if (x2 != 0) {
  435. ext <<= 1;
  436. ext |= sgn_x2;
  437. cbits--;
  438. }
  439. xbits -= cbits;
  440. // assert ( (x1|x2) < 16u );
  441. x1 = x1 * xlen + x2;
  442. code = h->table [x1];
  443. cbits += h->hlen [x1];
  444. // assert ( cbits <= MAX_LENGTH );
  445. // assert ( xbits <= MAX_LENGTH );
  446. putbits2 ( gfp, code, cbits );
  447. putbits2 ( gfp, ext, xbits );
  448. return cbits + xbits;
  449. }
  450. static int
  451. Huffmancodebits(lame_global_flags *gfp, int tableindex, int start, int end, int *ix)
  452. {
  453. int i,bits;
  454. // assert(tableindex < 32);
  455. if (!tableindex) return 0;
  456. bits=0;
  457. for (i = start; i < end; i += 2) {
  458. bits += HuffmanCode(gfp,tableindex, ix[i], ix[i + 1]);
  459. }
  460. return bits;
  461. }
  462. /*
  463. Note the discussion of huffmancodebits() on pages 28
  464. and 29 of the IS, as well as the definitions of the side
  465. information on pages 26 and 27.
  466. */
  467. static int
  468. ShortHuffmancodebits(lame_global_flags *gfp,int *ix, gr_info *gi)
  469. {
  470. lame_internal_flags *gfc=gfp->internal_flags;
  471. int bits;
  472. int region1Start;
  473. region1Start = 3*gfc->scalefac_band.s[3];
  474. if (region1Start > gi->big_values)
  475. region1Start = gi->big_values;
  476. /* short blocks do not have a region2 */
  477. bits = Huffmancodebits(gfp,gi->table_select[0], 0, region1Start, ix);
  478. bits += Huffmancodebits(gfp,gi->table_select[1], region1Start, gi->big_values, ix);
  479. return bits;
  480. }
  481. static int
  482. LongHuffmancodebits(lame_global_flags *gfp,int *ix, gr_info *gi)
  483. {
  484. lame_internal_flags *gfc=gfp->internal_flags;
  485. int i, bigvalues,bits=0;
  486. int region1Start, region2Start;
  487. bigvalues = gi->big_values;
  488. assert(0 <= bigvalues && bigvalues <= 576);
  489. i = gi->region0_count + 1;
  490. assert(i < 23);
  491. region1Start = gfc->scalefac_band.l[i];
  492. i += gi->region1_count + 1;
  493. assert(i < 23);
  494. region2Start = gfc->scalefac_band.l[i];
  495. if (region1Start > bigvalues)
  496. region1Start = bigvalues;
  497. if (region2Start > bigvalues)
  498. region2Start = bigvalues;
  499. bits +=Huffmancodebits(gfp,gi->table_select[0], 0, region1Start, ix);
  500. bits +=Huffmancodebits(gfp,gi->table_select[1], region1Start, region2Start, ix);
  501. bits +=Huffmancodebits(gfp,gi->table_select[2], region2Start, bigvalues, ix);
  502. return bits;
  503. }
  504. inline static int
  505. writeMainData ( lame_global_flags * const gfp,
  506. int l3_enc [2] [2] [576],
  507. III_scalefac_t scalefac [2] [2] )
  508. {
  509. int gr, ch, sfb,data_bits,scale_bits,tot_bits=0;
  510. lame_internal_flags *gfc=gfp->internal_flags;
  511. III_side_info_t *l3_side;
  512. l3_side = &gfc->l3_side;
  513. if (gfp->version == 1) {
  514. /* MPEG 1 */
  515. for (gr = 0; gr < 2; gr++) {
  516. for (ch = 0; ch < gfc->channels_out; ch++) {
  517. gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
  518. int slen1 = slen1_tab[gi->scalefac_compress];
  519. int slen2 = slen2_tab[gi->scalefac_compress];
  520. data_bits=0;
  521. scale_bits=0;
  522. #ifdef DEBUG
  523. hogege = gfc->bs.totbit;
  524. #endif
  525. if (gi->block_type == SHORT_TYPE) {
  526. for (sfb = 0; sfb < SBPSY_s; sfb++) {
  527. int slen = sfb < 6 ? slen1 : slen2;
  528. assert(scalefac[gr][ch].s[sfb][0]>=0);
  529. assert(scalefac[gr][ch].s[sfb][1]>=0);
  530. assert(scalefac[gr][ch].s[sfb][2]>=0);
  531. putbits2(gfp,scalefac[gr][ch].s[sfb][0], slen);
  532. putbits2(gfp,scalefac[gr][ch].s[sfb][1], slen);
  533. putbits2(gfp,scalefac[gr][ch].s[sfb][2], slen);
  534. scale_bits += 3*slen;
  535. }
  536. data_bits += ShortHuffmancodebits(gfp,l3_enc[gr][ch], gi);
  537. } else {
  538. int i;
  539. for (i = 0; i < sizeof(scfsi_band) / sizeof(int) - 1;
  540. i++) {
  541. if (gr != 0 && l3_side->scfsi[ch][i])
  542. continue;
  543. for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1];
  544. sfb++) {
  545. assert(scalefac[gr][ch].l[sfb]>=0);
  546. putbits2(gfp,scalefac[gr][ch].l[sfb],
  547. sfb < 11 ? slen1 : slen2);
  548. scale_bits += sfb < 11 ? slen1 : slen2;
  549. }
  550. }
  551. data_bits +=LongHuffmancodebits(gfp,l3_enc[gr][ch], gi);
  552. }
  553. data_bits +=huffman_coder_count1(gfp,l3_enc[gr][ch], gi);
  554. #ifdef DEBUG
  555. DEBUGF("<%ld> ", gfc->bs.totbit-hogege);
  556. #endif
  557. /* does bitcount in quantize.c agree with actual bit count?*/
  558. assert(data_bits==gi->part2_3_length-gi->part2_length);
  559. assert(scale_bits==gi->part2_length);
  560. tot_bits += scale_bits + data_bits;
  561. } /* for ch */
  562. } /* for gr */
  563. } else {
  564. /* MPEG 2 */
  565. gr = 0;
  566. for (ch = 0; ch < gfc->channels_out; ch++) {
  567. gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
  568. int i, sfb_partition;
  569. assert(gi->sfb_partition_table);
  570. data_bits = 0;
  571. scale_bits=0;
  572. sfb = 0;
  573. sfb_partition = 0;
  574. if (gi->block_type == SHORT_TYPE) {
  575. for (; sfb_partition < 4; sfb_partition++) {
  576. int sfbs = gi->sfb_partition_table[sfb_partition] / 3;
  577. int slen = gi->slen[sfb_partition];
  578. for (i = 0; i < sfbs; i++, sfb++) {
  579. putbits2(gfp,Max(scalefac[gr][ch].s[sfb][0], 0U), slen);
  580. putbits2(gfp,Max(scalefac[gr][ch].s[sfb][1], 0U), slen);
  581. putbits2(gfp,Max(scalefac[gr][ch].s[sfb][2], 0U), slen);
  582. scale_bits += 3*slen;
  583. }
  584. }
  585. data_bits += ShortHuffmancodebits(gfp,l3_enc[gr][ch], gi);
  586. } else {
  587. for (; sfb_partition < 4; sfb_partition++) {
  588. int sfbs = gi->sfb_partition_table[sfb_partition];
  589. int slen = gi->slen[sfb_partition];
  590. for (i = 0; i < sfbs; i++, sfb++) {
  591. putbits2(gfp,Max(scalefac[gr][ch].l[sfb], 0U), slen);
  592. scale_bits += slen;
  593. }
  594. }
  595. data_bits +=LongHuffmancodebits(gfp,l3_enc[gr][ch], gi);
  596. }
  597. data_bits +=huffman_coder_count1(gfp,l3_enc[gr][ch], gi);
  598. /* does bitcount in quantize.c agree with actual bit count?*/
  599. assert(data_bits==gi->part2_3_length-gi->part2_length);
  600. assert(scale_bits==gi->part2_length);
  601. tot_bits += scale_bits + data_bits;
  602. } /* for ch */
  603. } /* for gf */
  604. return tot_bits;
  605. } /* main_data */
  606. void
  607. flush_bitstream(lame_global_flags *gfp)
  608. {
  609. lame_internal_flags *gfc=gfp->internal_flags;
  610. int flushbits,remaining_headers;
  611. int bitsPerFrame, mean_bits;
  612. int last_ptr,first_ptr;
  613. first_ptr=gfc->w_ptr; /* first header to add to bitstream */
  614. last_ptr = gfc->h_ptr - 1; /* last header to add to bitstream */
  615. if (last_ptr==-1) last_ptr=MAX_HEADER_BUF-1;
  616. /* add this many bits to bitstream so we can flush all headers */
  617. flushbits = gfc->header[last_ptr].write_timing - gfc->bs.totbit;
  618. if (flushbits >= 0) {
  619. /* if flushbits >= 0, some headers have not yet been written */
  620. /* reduce flushbits by the size of the headers */
  621. remaining_headers= 1+last_ptr - first_ptr;
  622. if (last_ptr < first_ptr)
  623. remaining_headers= 1+last_ptr - first_ptr + MAX_HEADER_BUF;
  624. flushbits -= remaining_headers*8*gfc->sideinfo_len;
  625. }
  626. /* finally, add some bits so that the last frame is complete
  627. * these bits are not necessary to decode the last frame, but
  628. * some decoders will ignore last frame if these bits are missing
  629. */
  630. getframebits(gfp,&bitsPerFrame,&mean_bits);
  631. flushbits += bitsPerFrame;
  632. if (flushbits<0) {
  633. #if 0
  634. /* if flushbits < 0, this would mean that the buffer looks like:
  635. * (data...) last_header (data...) (extra data that should not be here...)
  636. */
  637. DEBUGF("last header write_timing = %i \n",gfc->header[last_ptr].write_timing);
  638. DEBUGF("first header write_timing = %i \n",gfc->header[first_ptr].write_timing);
  639. DEBUGF("bs.totbit: %i \n",gfc->bs.totbit);
  640. DEBUGF("first_ptr, last_ptr %i %i \n",first_ptr,last_ptr);
  641. DEBUGF("remaining_headers = %i \n",remaining_headers);
  642. DEBUGF("bitsperframe: %i \n",bitsPerFrame);
  643. DEBUGF("sidelen: %i \n",gfc->sideinfo_len);
  644. #endif
  645. ERRORF(gfc,"strange error flushing buffer ... \n");
  646. } else {
  647. drain_into_ancillary(gfp,flushbits);
  648. }
  649. assert (gfc->header[last_ptr].write_timing + bitsPerFrame == gfc->bs.totbit);
  650. }
  651. void add_dummy_byte ( lame_global_flags* const gfp, unsigned char val )
  652. {
  653. lame_internal_flags *gfc = gfp->internal_flags;
  654. int i;
  655. putbits_noheaders(gfp,val,8);
  656. for (i=0 ; i< MAX_HEADER_BUF ; ++i)
  657. gfc->header[i].write_timing += 8;
  658. }
  659. /*
  660. format_bitstream()
  661. This is called after a frame of audio has been quantized and coded.
  662. It will write the encoded audio to the bitstream. Note that
  663. from a layer3 encoder's perspective the bit stream is primarily
  664. a series of main_data() blocks, with header and side information
  665. inserted at the proper locations to maintain framing. (See Figure A.7
  666. in the IS).
  667. */
  668. int
  669. format_bitstream(lame_global_flags *gfp, int bitsPerFrame,
  670. int l3_enc[2][2][576],
  671. III_scalefac_t scalefac[2][2] )
  672. {
  673. lame_internal_flags *gfc=gfp->internal_flags;
  674. int bits;
  675. III_side_info_t *l3_side;
  676. l3_side = &gfc->l3_side;
  677. drain_into_ancillary(gfp,l3_side->resvDrain_pre);
  678. encodeSideInfo2(gfp,bitsPerFrame);
  679. bits = 8*gfc->sideinfo_len;
  680. bits+=writeMainData(gfp,l3_enc,scalefac);
  681. drain_into_ancillary(gfp,l3_side->resvDrain_post);
  682. bits += l3_side->resvDrain_post;
  683. l3_side->main_data_begin += (bitsPerFrame-bits)/8;
  684. if ((l3_side->main_data_begin * 8) != gfc->ResvSize ) {
  685. ERRORF(gfc,"bit reservoir error: \n"
  686. "l3_side->main_data_begin: %i \n"
  687. "Resvoir size: %i \n"
  688. "resv drain (post) %i \n"
  689. "resv drain (pre) %i \n"
  690. "header and sideinfo: %i \n"
  691. "data bits: %i \n"
  692. "total bits: %i (remainder: %i) \n"
  693. "bitsperframe: %i \n",
  694. 8*l3_side->main_data_begin,
  695. gfc->ResvSize,
  696. l3_side->resvDrain_post,
  697. l3_side->resvDrain_pre,
  698. 8*gfc->sideinfo_len,
  699. bits-l3_side->resvDrain_post-8*gfc->sideinfo_len,
  700. bits, bits % 8,
  701. bitsPerFrame
  702. );
  703. gfc->ResvSize = l3_side->main_data_begin*8;
  704. };
  705. assert(gfc->bs.totbit % 8 == 0);
  706. if (gfc->bs.totbit > 1000000000 ) {
  707. /* to avoid totbit overflow, (at 8h encoding at 128kbs) lets reset bit counter*/
  708. int i;
  709. for (i=0 ; i< MAX_HEADER_BUF ; ++i)
  710. gfc->header[i].write_timing -= gfc->bs.totbit;
  711. gfc->bs.totbit=0;
  712. }
  713. return 0;
  714. }
  715. int copy_buffer(unsigned char *buffer,int size,Bit_stream_struc *bs)
  716. {
  717. int minimum = bs->buf_byte_idx + 1;
  718. if (minimum <= 0) return 0;
  719. if (size!=0 && minimum>size) return -1; /* buffer is too small */
  720. memcpy(buffer,bs->buf,minimum);
  721. bs->buf_byte_idx = -1;
  722. bs->buf_bit_idx = 0;
  723. return minimum;
  724. }
  725. void init_bit_stream_w(lame_internal_flags *gfc)
  726. {
  727. gfc->bs.buf = (unsigned char *) malloc(BUFFER_SIZE);
  728. gfc->bs.buf_size = BUFFER_SIZE;
  729. gfc->h_ptr = gfc->w_ptr = 0;
  730. gfc->header[gfc->h_ptr].write_timing = 0;
  731. gfc->bs.buf_byte_idx = -1;
  732. gfc->bs.buf_bit_idx = 0;
  733. gfc->bs.totbit = 0;
  734. }
  735. /* end of bitstream.c */