common.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * common.c: some common bitstream operations
  3. *
  4. * Copyright (C) 1999-2010 The L.A.M.E. project
  5. *
  6. * Initially written by Michael Hipp, see also AUTHORS and README.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. /* $Id: common.c,v 1.42 2017/08/19 14:20:27 robert Exp $ */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <ctype.h>
  28. #include <stdlib.h>
  29. #include <signal.h>
  30. #ifdef HAVE_FCNTL_H
  31. #include <fcntl.h>
  32. #endif
  33. #ifdef macintosh
  34. #include <types.h>
  35. #include <stat.h>
  36. #else
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #endif
  40. #include <assert.h>
  41. #include "common.h"
  42. #ifdef WITH_DMALLOC
  43. #include <dmalloc.h>
  44. #endif
  45. /* In C++ the array first must be prototyped, why ? */
  46. /* *INDENT-OFF* */
  47. const int tabsel_123 [2] [3] [16] = {
  48. { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
  49. {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
  50. {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
  51. { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
  52. {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
  53. {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
  54. };
  55. const long freqs[9] = { 44100, 48000, 32000,
  56. 22050, 24000, 16000,
  57. 11025, 12000, 8000 };
  58. /* *INDENT-ON* */
  59. real muls[27][64];
  60. #if 0
  61. static void
  62. get_II_stuff(struct frame *fr)
  63. {
  64. /* *INDENT-OFF* */
  65. static const int translate [3] [2] [16] = /* char ? */
  66. { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
  67. { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
  68. { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
  69. { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
  70. { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
  71. { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
  72. /* *INDENT-ON* */
  73. int table, sblim;
  74. static const struct al_table2 *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
  75. static int sblims[5] = { 27, 30, 8, 12, 30 };
  76. if (fr->lsf)
  77. table = 4;
  78. else
  79. table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
  80. sblim = sblims[table];
  81. fr->alloc = tables[table];
  82. fr->II_sblimit = sblim;
  83. }
  84. #endif
  85. #define HDRCMPMASK 0xfffffd00
  86. #define MAX_INPUT_FRAMESIZE 4096
  87. int
  88. head_check(unsigned long head, int check_layer)
  89. {
  90. /*
  91. look for a valid header.
  92. if check_layer > 0, then require that
  93. nLayer = check_layer.
  94. */
  95. /* bits 13-14 = layer 3 */
  96. int nLayer = 4 - ((head >> 17) & 3);
  97. if ((head & 0xffe00000) != 0xffe00000) {
  98. /* syncword */
  99. return FALSE;
  100. }
  101. if (nLayer == 4)
  102. return FALSE;
  103. if (check_layer > 0 && nLayer != check_layer)
  104. return FALSE;
  105. if (((head >> 12) & 0xf) == 0xf) {
  106. /* bits 16,17,18,19 = 1111 invalid bitrate */
  107. return FALSE;
  108. }
  109. if (((head >> 10) & 0x3) == 0x3) {
  110. /* bits 20,21 = 11 invalid sampling freq */
  111. return FALSE;
  112. }
  113. if ((head & 0x3) == 0x2)
  114. /* invalid emphasis */
  115. return FALSE;
  116. return TRUE;
  117. }
  118. #if 0
  119. static void
  120. print_header(PMPSTR mp, struct frame *fr)
  121. {
  122. static const char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
  123. static const char *layers[4] = { "Unknown", "I", "II", "III" };
  124. lame_report_fnc(mp->report_msg, "MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
  125. fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
  126. layers[fr->lay], freqs[fr->sampling_frequency],
  127. modes[fr->mode], fr->mode_ext, fr->framesize + 4);
  128. lame_report_fnc(mp->report_msg, "Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
  129. fr->stereo, fr->copyright ? "Yes" : "No",
  130. fr->original ? "Yes" : "No", fr->error_protection ? "Yes" : "No", fr->emphasis);
  131. lame_report_fnc(mp->report_msg, "Bitrate: %d Kbits/s, Extension value: %d\n",
  132. tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index], fr->extension);
  133. }
  134. static void
  135. print_header_compact(PMPSTR mp, struct frame *fr)
  136. {
  137. static const char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
  138. static const char *layers[4] = { "Unknown", "I", "II", "III" };
  139. lame_report_fnc(mp->report_err, "MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
  140. fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
  141. layers[fr->lay],
  142. tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index],
  143. freqs[fr->sampling_frequency], modes[fr->mode]);
  144. }
  145. #endif
  146. /*
  147. * decode a header and write the information
  148. * into the frame structure
  149. */
  150. int
  151. decode_header(PMPSTR mp, struct frame *fr, unsigned long newhead)
  152. {
  153. if (newhead & (1 << 20)) {
  154. fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
  155. fr->mpeg25 = 0;
  156. }
  157. else {
  158. fr->lsf = 1;
  159. fr->mpeg25 = 1;
  160. }
  161. fr->lay = 4 - ((newhead >> 17) & 3);
  162. if (fr->lay != 3 && fr->mpeg25) {
  163. lame_report_fnc(mp->report_err, "MPEG-2.5 is supported by Layer3 only\n");
  164. return 0;
  165. }
  166. if (((newhead >> 10) & 0x3) == 0x3) {
  167. lame_report_fnc(mp->report_err, "Stream error\n");
  168. return 0;
  169. }
  170. if (fr->mpeg25) {
  171. fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
  172. }
  173. else
  174. fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
  175. fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
  176. if (fr->mpeg25) /* allow Bitrate change for 2.5 ... */
  177. fr->bitrate_index = ((newhead >> 12) & 0xf);
  178. fr->bitrate_index = ((newhead >> 12) & 0xf);
  179. fr->padding = ((newhead >> 9) & 0x1);
  180. fr->extension = ((newhead >> 8) & 0x1);
  181. fr->mode = ((newhead >> 6) & 0x3);
  182. fr->mode_ext = ((newhead >> 4) & 0x3);
  183. fr->copyright = ((newhead >> 3) & 0x1);
  184. fr->original = ((newhead >> 2) & 0x1);
  185. fr->emphasis = newhead & 0x3;
  186. fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
  187. switch (fr->lay) {
  188. case 1:
  189. fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
  190. fr->framesize /= freqs[fr->sampling_frequency];
  191. fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
  192. fr->down_sample = 0;
  193. fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
  194. break;
  195. case 2:
  196. fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
  197. fr->framesize /= freqs[fr->sampling_frequency];
  198. fr->framesize += fr->padding - 4;
  199. fr->down_sample = 0;
  200. fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
  201. break;
  202. case 3:
  203. #if 0
  204. fr->do_layer = do_layer3;
  205. if (fr->lsf)
  206. ssize = (fr->stereo == 1) ? 9 : 17;
  207. else
  208. ssize = (fr->stereo == 1) ? 17 : 32;
  209. #endif
  210. #if 0
  211. if (fr->error_protection)
  212. ssize += 2;
  213. #endif
  214. if (fr->framesize > MAX_INPUT_FRAMESIZE) {
  215. lame_report_fnc(mp->report_err, "Frame size too big.\n");
  216. fr->framesize = MAX_INPUT_FRAMESIZE;
  217. return (0);
  218. }
  219. if (fr->bitrate_index == 0)
  220. fr->framesize = 0;
  221. else {
  222. fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
  223. fr->framesize /= freqs[fr->sampling_frequency] << (fr->lsf);
  224. fr->framesize = fr->framesize + fr->padding - 4;
  225. }
  226. break;
  227. default:
  228. lame_report_fnc(mp->report_err, "Sorry, layer %d not supported\n", fr->lay);
  229. return (0);
  230. }
  231. /* print_header(mp, fr); */
  232. return 1;
  233. }
  234. unsigned int
  235. getbits(PMPSTR mp, int number_of_bits)
  236. {
  237. unsigned long rval;
  238. if (number_of_bits <= 0 || !mp->wordpointer)
  239. return 0;
  240. {
  241. rval = mp->wordpointer[0];
  242. rval <<= 8;
  243. rval |= mp->wordpointer[1];
  244. rval <<= 8;
  245. rval |= mp->wordpointer[2];
  246. rval <<= mp->bitindex;
  247. rval &= 0xffffff;
  248. mp->bitindex += number_of_bits;
  249. rval >>= (24 - number_of_bits);
  250. mp->wordpointer += (mp->bitindex >> 3);
  251. mp->bitindex &= 7;
  252. }
  253. return rval;
  254. }
  255. unsigned int
  256. getbits_fast(PMPSTR mp, int number_of_bits)
  257. {
  258. unsigned long rval;
  259. {
  260. rval = mp->wordpointer[0];
  261. rval <<= 8;
  262. rval |= mp->wordpointer[1];
  263. rval <<= mp->bitindex;
  264. rval &= 0xffff;
  265. mp->bitindex += number_of_bits;
  266. rval >>= (16 - number_of_bits);
  267. mp->wordpointer += (mp->bitindex >> 3);
  268. mp->bitindex &= 7;
  269. }
  270. return rval;
  271. }
  272. unsigned char
  273. get_leq_8_bits(PMPSTR mp, unsigned int number_of_bits)
  274. {
  275. assert(number_of_bits <= 8);
  276. return (unsigned char) getbits_fast(mp, number_of_bits);
  277. }
  278. unsigned short
  279. get_leq_16_bits(PMPSTR mp, unsigned int number_of_bits)
  280. {
  281. assert(number_of_bits <= 16);
  282. return (unsigned short) getbits_fast(mp, number_of_bits);
  283. }
  284. int
  285. set_pointer(PMPSTR mp, long backstep)
  286. {
  287. unsigned char *bsbufold;
  288. if (mp->fsizeold < 0 && backstep > 0) {
  289. lame_report_fnc(mp->report_err, "hip: Can't step back %ld bytes!\n", backstep);
  290. return MP3_ERR;
  291. }
  292. bsbufold = mp->bsspace[1 - mp->bsnum] + 512;
  293. mp->wordpointer -= backstep;
  294. if (backstep)
  295. memcpy(mp->wordpointer, bsbufold + mp->fsizeold - backstep, (size_t) backstep);
  296. mp->bitindex = 0;
  297. return MP3_OK;
  298. }