PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/liblame/mpglib/common.c

https://github.com/hgmeier/xbmc
C | 344 lines | 238 code | 63 blank | 43 comment | 35 complexity | b9ead84b5924b63b56b5e2d59d84fbd2 MD5 | raw file
  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.32.8.4 2010/03/22 14:32:36 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 "common.h"
  41. #ifdef WITH_DMALLOC
  42. #include <dmalloc.h>
  43. #endif
  44. /* In C++ the array first must be prototyped, why ? */
  45. /* *INDENT-OFF* */
  46. const int tabsel_123 [2] [3] [16] = {
  47. { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
  48. {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
  49. {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
  50. { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
  51. {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
  52. {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
  53. };
  54. const long freqs[9] = { 44100, 48000, 32000,
  55. 22050, 24000, 16000,
  56. 11025, 12000, 8000 };
  57. /* *INDENT-ON* */
  58. real muls[27][64];
  59. #if 0
  60. static void
  61. get_II_stuff(struct frame *fr)
  62. {
  63. /* *INDENT-OFF* */
  64. static const int translate [3] [2] [16] = /* char ? */
  65. { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
  66. { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
  67. { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
  68. { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
  69. { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
  70. { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
  71. /* *INDENT-ON* */
  72. int table, sblim;
  73. static const struct al_table2 *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
  74. static int sblims[5] = { 27, 30, 8, 12, 30 };
  75. if (fr->lsf)
  76. table = 4;
  77. else
  78. table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
  79. sblim = sblims[table];
  80. fr->alloc = tables[table];
  81. fr->II_sblimit = sblim;
  82. }
  83. #endif
  84. #define HDRCMPMASK 0xfffffd00
  85. #define MAX_INPUT_FRAMESIZE 4096
  86. int
  87. head_check(unsigned long head, int check_layer)
  88. {
  89. /*
  90. look for a valid header.
  91. if check_layer > 0, then require that
  92. nLayer = check_layer.
  93. */
  94. /* bits 13-14 = layer 3 */
  95. int nLayer = 4 - ((head >> 17) & 3);
  96. if ((head & 0xffe00000) != 0xffe00000) {
  97. /* syncword */
  98. return FALSE;
  99. }
  100. if (nLayer == 4)
  101. return FALSE;
  102. if (check_layer > 0 && nLayer != check_layer)
  103. return FALSE;
  104. if (((head >> 12) & 0xf) == 0xf) {
  105. /* bits 16,17,18,19 = 1111 invalid bitrate */
  106. return FALSE;
  107. }
  108. if (((head >> 10) & 0x3) == 0x3) {
  109. /* bits 20,21 = 11 invalid sampling freq */
  110. return FALSE;
  111. }
  112. if ((head & 0x3) == 0x2)
  113. /* invalid emphasis */
  114. return FALSE;
  115. return TRUE;
  116. }
  117. /*
  118. * decode a header and write the information
  119. * into the frame structure
  120. */
  121. int
  122. decode_header(struct frame *fr, unsigned long newhead)
  123. {
  124. if (newhead & (1 << 20)) {
  125. fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
  126. fr->mpeg25 = 0;
  127. }
  128. else {
  129. fr->lsf = 1;
  130. fr->mpeg25 = 1;
  131. }
  132. fr->lay = 4 - ((newhead >> 17) & 3);
  133. if (((newhead >> 10) & 0x3) == 0x3) {
  134. fprintf(stderr, "Stream error\n");
  135. exit(1);
  136. }
  137. if (fr->mpeg25) {
  138. fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
  139. }
  140. else
  141. fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
  142. fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
  143. if (fr->mpeg25) /* allow Bitrate change for 2.5 ... */
  144. fr->bitrate_index = ((newhead >> 12) & 0xf);
  145. fr->bitrate_index = ((newhead >> 12) & 0xf);
  146. fr->padding = ((newhead >> 9) & 0x1);
  147. fr->extension = ((newhead >> 8) & 0x1);
  148. fr->mode = ((newhead >> 6) & 0x3);
  149. fr->mode_ext = ((newhead >> 4) & 0x3);
  150. fr->copyright = ((newhead >> 3) & 0x1);
  151. fr->original = ((newhead >> 2) & 0x1);
  152. fr->emphasis = newhead & 0x3;
  153. fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
  154. switch (fr->lay) {
  155. case 1:
  156. fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
  157. fr->framesize /= freqs[fr->sampling_frequency];
  158. fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
  159. fr->down_sample = 0;
  160. fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
  161. break;
  162. case 2:
  163. fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
  164. fr->framesize /= freqs[fr->sampling_frequency];
  165. fr->framesize += fr->padding - 4;
  166. fr->down_sample = 0;
  167. fr->down_sample_sblimit = SBLIMIT >> (fr->down_sample);
  168. break;
  169. case 3:
  170. #if 0
  171. fr->do_layer = do_layer3;
  172. if (fr->lsf)
  173. ssize = (fr->stereo == 1) ? 9 : 17;
  174. else
  175. ssize = (fr->stereo == 1) ? 17 : 32;
  176. #endif
  177. #if 0
  178. if (fr->error_protection)
  179. ssize += 2;
  180. #endif
  181. if (fr->framesize > MAX_INPUT_FRAMESIZE) {
  182. fprintf(stderr, "Frame size too big.\n");
  183. fr->framesize = MAX_INPUT_FRAMESIZE;
  184. return (0);
  185. }
  186. if (fr->bitrate_index == 0)
  187. fr->framesize = 0;
  188. else {
  189. fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
  190. fr->framesize /= freqs[fr->sampling_frequency] << (fr->lsf);
  191. fr->framesize = fr->framesize + fr->padding - 4;
  192. }
  193. break;
  194. default:
  195. fprintf(stderr, "Sorry, layer %d not supported\n", fr->lay);
  196. return (0);
  197. }
  198. /* print_header(fr); */
  199. return 1;
  200. }
  201. #if 1
  202. void
  203. print_header(struct frame *fr)
  204. {
  205. static const char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
  206. static const char *layers[4] = { "Unknown", "I", "II", "III" };
  207. fprintf(stderr, "MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
  208. fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
  209. layers[fr->lay], freqs[fr->sampling_frequency],
  210. modes[fr->mode], fr->mode_ext, fr->framesize + 4);
  211. fprintf(stderr, "Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
  212. fr->stereo, fr->copyright ? "Yes" : "No",
  213. fr->original ? "Yes" : "No", fr->error_protection ? "Yes" : "No", fr->emphasis);
  214. fprintf(stderr, "Bitrate: %d Kbits/s, Extension value: %d\n",
  215. tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index], fr->extension);
  216. }
  217. void
  218. print_header_compact(struct frame *fr)
  219. {
  220. static const char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
  221. static const char *layers[4] = { "Unknown", "I", "II", "III" };
  222. fprintf(stderr, "MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
  223. fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
  224. layers[fr->lay],
  225. tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index],
  226. freqs[fr->sampling_frequency], modes[fr->mode]);
  227. }
  228. #endif
  229. unsigned int
  230. getbits(PMPSTR mp, int number_of_bits)
  231. {
  232. unsigned long rval;
  233. if (number_of_bits <= 0 || !mp->wordpointer)
  234. return 0;
  235. {
  236. rval = mp->wordpointer[0];
  237. rval <<= 8;
  238. rval |= mp->wordpointer[1];
  239. rval <<= 8;
  240. rval |= mp->wordpointer[2];
  241. rval <<= mp->bitindex;
  242. rval &= 0xffffff;
  243. mp->bitindex += number_of_bits;
  244. rval >>= (24 - number_of_bits);
  245. mp->wordpointer += (mp->bitindex >> 3);
  246. mp->bitindex &= 7;
  247. }
  248. return rval;
  249. }
  250. unsigned int
  251. getbits_fast(PMPSTR mp, int number_of_bits)
  252. {
  253. unsigned long rval;
  254. {
  255. rval = mp->wordpointer[0];
  256. rval <<= 8;
  257. rval |= mp->wordpointer[1];
  258. rval <<= mp->bitindex;
  259. rval &= 0xffff;
  260. mp->bitindex += number_of_bits;
  261. rval >>= (16 - number_of_bits);
  262. mp->wordpointer += (mp->bitindex >> 3);
  263. mp->bitindex &= 7;
  264. }
  265. return rval;
  266. }
  267. int
  268. set_pointer(PMPSTR mp, long backstep)
  269. {
  270. unsigned char *bsbufold;
  271. if (mp->fsizeold < 0 && backstep > 0) {
  272. fprintf(stderr, "hip: Can't step back %ld bytes!\n", backstep);
  273. return MP3_ERR;
  274. }
  275. bsbufold = mp->bsspace[1 - mp->bsnum] + 512;
  276. mp->wordpointer -= backstep;
  277. if (backstep)
  278. memcpy(mp->wordpointer, bsbufold + mp->fsizeold - backstep, (size_t) backstep);
  279. mp->bitindex = 0;
  280. return MP3_OK;
  281. }