Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

503 lines
12 KiB

  1. /*
  2. * libmad - MPEG audio decoder library
  3. * Copyright (C) 2000-2004 Underbit Technologies, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * $Id: frame.c,v 1.29 2004/02/04 22:59:19 rob Exp $
  20. */
  21. # ifdef HAVE_CONFIG_H
  22. # include "config.h"
  23. # endif
  24. # include "global.h"
  25. # include <stdlib.h>
  26. # include "bit.h"
  27. # include "stream.h"
  28. # include "frame.h"
  29. # include "timer.h"
  30. # include "layer12.h"
  31. # include "layer3.h"
  32. static
  33. unsigned long const bitrate_table[5][15] = {
  34. /* MPEG-1 */
  35. { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, /* Layer I */
  36. 256000, 288000, 320000, 352000, 384000, 416000, 448000 },
  37. { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer II */
  38. 128000, 160000, 192000, 224000, 256000, 320000, 384000 },
  39. { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, /* Layer III */
  40. 112000, 128000, 160000, 192000, 224000, 256000, 320000 },
  41. /* MPEG-2 LSF */
  42. { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, /* Layer I */
  43. 128000, 144000, 160000, 176000, 192000, 224000, 256000 },
  44. { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, /* Layers */
  45. 64000, 80000, 96000, 112000, 128000, 144000, 160000 } /* II & III */
  46. };
  47. static
  48. unsigned int const samplerate_table[3] = { 44100, 48000, 32000 };
  49. static
  50. int (*const decoder_table[3])(struct mad_stream *, struct mad_frame *) = {
  51. mad_layer_I,
  52. mad_layer_II,
  53. mad_layer_III
  54. };
  55. /*
  56. * NAME: header->init()
  57. * DESCRIPTION: initialize header struct
  58. */
  59. void mad_header_init(struct mad_header *header)
  60. {
  61. header->layer = 0;
  62. header->mode = 0;
  63. header->mode_extension = 0;
  64. header->emphasis = 0;
  65. header->bitrate = 0;
  66. header->samplerate = 0;
  67. header->crc_check = 0;
  68. header->crc_target = 0;
  69. header->flags = 0;
  70. header->private_bits = 0;
  71. header->duration = mad_timer_zero;
  72. }
  73. /*
  74. * NAME: frame->init()
  75. * DESCRIPTION: initialize frame struct
  76. */
  77. void mad_frame_init(struct mad_frame *frame)
  78. {
  79. mad_header_init(&frame->header);
  80. frame->options = 0;
  81. frame->overlap = 0;
  82. mad_frame_mute(frame);
  83. }
  84. /*
  85. * NAME: frame->finish()
  86. * DESCRIPTION: deallocate any dynamic memory associated with frame
  87. */
  88. void mad_frame_finish(struct mad_frame *frame)
  89. {
  90. mad_header_finish(&frame->header);
  91. if (frame->overlap) {
  92. free(frame->overlap);
  93. frame->overlap = 0;
  94. }
  95. }
  96. /*
  97. * NAME: decode_header()
  98. * DESCRIPTION: read header data and following CRC word
  99. */
  100. static
  101. int decode_header(struct mad_header *header, struct mad_stream *stream)
  102. {
  103. unsigned int index;
  104. header->flags = 0;
  105. header->private_bits = 0;
  106. /* header() */
  107. /* syncword */
  108. mad_bit_skip(&stream->ptr, 11);
  109. /* MPEG 2.5 indicator (really part of syncword) */
  110. if (mad_bit_read(&stream->ptr, 1) == 0)
  111. header->flags |= MAD_FLAG_MPEG_2_5_EXT;
  112. /* ID */
  113. if (mad_bit_read(&stream->ptr, 1) == 0)
  114. header->flags |= MAD_FLAG_LSF_EXT;
  115. else if (header->flags & MAD_FLAG_MPEG_2_5_EXT) {
  116. stream->error = MAD_ERROR_LOSTSYNC;
  117. return -1;
  118. }
  119. /* layer */
  120. header->layer = 4 - mad_bit_read(&stream->ptr, 2);
  121. if (header->layer == 4) {
  122. stream->error = MAD_ERROR_BADLAYER;
  123. return -1;
  124. }
  125. /* protection_bit */
  126. if (mad_bit_read(&stream->ptr, 1) == 0) {
  127. header->flags |= MAD_FLAG_PROTECTION;
  128. header->crc_check = mad_bit_crc(stream->ptr, 16, 0xffff);
  129. }
  130. /* bitrate_index */
  131. index = mad_bit_read(&stream->ptr, 4);
  132. if (index == 15) {
  133. stream->error = MAD_ERROR_BADBITRATE;
  134. return -1;
  135. }
  136. if (header->flags & MAD_FLAG_LSF_EXT)
  137. header->bitrate = bitrate_table[3 + (header->layer >> 1)][index];
  138. else
  139. header->bitrate = bitrate_table[header->layer - 1][index];
  140. /* sampling_frequency */
  141. index = mad_bit_read(&stream->ptr, 2);
  142. if (index == 3) {
  143. stream->error = MAD_ERROR_BADSAMPLERATE;
  144. return -1;
  145. }
  146. header->samplerate = samplerate_table[index];
  147. if (header->flags & MAD_FLAG_LSF_EXT) {
  148. header->samplerate /= 2;
  149. if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
  150. header->samplerate /= 2;
  151. }
  152. /* padding_bit */
  153. if (mad_bit_read(&stream->ptr, 1))
  154. header->flags |= MAD_FLAG_PADDING;
  155. /* private_bit */
  156. if (mad_bit_read(&stream->ptr, 1))
  157. header->private_bits |= MAD_PRIVATE_HEADER;
  158. /* mode */
  159. header->mode = 3 - mad_bit_read(&stream->ptr, 2);
  160. /* mode_extension */
  161. header->mode_extension = mad_bit_read(&stream->ptr, 2);
  162. /* copyright */
  163. if (mad_bit_read(&stream->ptr, 1))
  164. header->flags |= MAD_FLAG_COPYRIGHT;
  165. /* original/copy */
  166. if (mad_bit_read(&stream->ptr, 1))
  167. header->flags |= MAD_FLAG_ORIGINAL;
  168. /* emphasis */
  169. header->emphasis = mad_bit_read(&stream->ptr, 2);
  170. # if defined(OPT_STRICT)
  171. /*
  172. * ISO/IEC 11172-3 says this is a reserved emphasis value, but
  173. * streams exist which use it anyway. Since the value is not important
  174. * to the decoder proper, we allow it unless OPT_STRICT is defined.
  175. */
  176. if (header->emphasis == MAD_EMPHASIS_RESERVED) {
  177. stream->error = MAD_ERROR_BADEMPHASIS;
  178. return -1;
  179. }
  180. # endif
  181. /* error_check() */
  182. /* crc_check */
  183. if (header->flags & MAD_FLAG_PROTECTION)
  184. header->crc_target = mad_bit_read(&stream->ptr, 16);
  185. return 0;
  186. }
  187. /*
  188. * NAME: free_bitrate()
  189. * DESCRIPTION: attempt to discover the bitstream's free bitrate
  190. */
  191. static
  192. int free_bitrate(struct mad_stream *stream, struct mad_header const *header)
  193. {
  194. struct mad_bitptr keep_ptr;
  195. unsigned long rate = 0;
  196. unsigned int pad_slot, slots_per_frame;
  197. unsigned char const *ptr = 0;
  198. keep_ptr = stream->ptr;
  199. pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
  200. slots_per_frame = (header->layer == MAD_LAYER_III &&
  201. (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
  202. while (mad_stream_sync(stream) == 0) {
  203. struct mad_stream peek_stream;
  204. struct mad_header peek_header;
  205. peek_stream = *stream;
  206. peek_header = *header;
  207. if (decode_header(&peek_header, &peek_stream) == 0 &&
  208. peek_header.layer == header->layer &&
  209. peek_header.samplerate == header->samplerate) {
  210. unsigned int N;
  211. ptr = mad_bit_nextbyte(&stream->ptr);
  212. N = ptr - stream->this_frame;
  213. if (header->layer == MAD_LAYER_I) {
  214. rate = (unsigned long) header->samplerate *
  215. (N - 4 * pad_slot + 4) / 48 / 1000;
  216. }
  217. else {
  218. rate = (unsigned long) header->samplerate *
  219. (N - pad_slot + 1) / slots_per_frame / 1000;
  220. }
  221. if (rate >= 8)
  222. break;
  223. }
  224. mad_bit_skip(&stream->ptr, 8);
  225. }
  226. stream->ptr = keep_ptr;
  227. if (rate < 8 || (header->layer == MAD_LAYER_III && rate > 640)) {
  228. stream->error = MAD_ERROR_LOSTSYNC;
  229. return -1;
  230. }
  231. stream->freerate = rate * 1000;
  232. return 0;
  233. }
  234. /*
  235. * NAME: header->decode()
  236. * DESCRIPTION: read the next frame header from the stream
  237. */
  238. int mad_header_decode(struct mad_header *header, struct mad_stream *stream)
  239. {
  240. register unsigned char const *ptr, *end;
  241. unsigned int pad_slot, N;
  242. ptr = stream->next_frame;
  243. end = stream->bufend;
  244. if (ptr == 0) {
  245. stream->error = MAD_ERROR_BUFPTR;
  246. goto fail;
  247. }
  248. /* stream skip */
  249. if (stream->skiplen) {
  250. if (!stream->sync)
  251. ptr = stream->this_frame;
  252. if (end - ptr < stream->skiplen) {
  253. stream->skiplen -= end - ptr;
  254. stream->next_frame = end;
  255. stream->error = MAD_ERROR_BUFLEN;
  256. goto fail;
  257. }
  258. ptr += stream->skiplen;
  259. stream->skiplen = 0;
  260. stream->sync = 1;
  261. }
  262. sync:
  263. /* synchronize */
  264. if (stream->sync) {
  265. if (end - ptr < MAD_BUFFER_GUARD) {
  266. stream->next_frame = ptr;
  267. stream->error = MAD_ERROR_BUFLEN;
  268. goto fail;
  269. }
  270. else if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
  271. /* mark point where frame sync word was expected */
  272. stream->this_frame = ptr;
  273. stream->next_frame = ptr + 1;
  274. stream->error = MAD_ERROR_LOSTSYNC;
  275. goto fail;
  276. }
  277. }
  278. else {
  279. mad_bit_init(&stream->ptr, ptr);
  280. if (mad_stream_sync(stream) == -1) {
  281. if (end - stream->next_frame >= MAD_BUFFER_GUARD)
  282. stream->next_frame = end - MAD_BUFFER_GUARD;
  283. stream->error = MAD_ERROR_BUFLEN;
  284. goto fail;
  285. }
  286. ptr = mad_bit_nextbyte(&stream->ptr);
  287. }
  288. /* begin processing */
  289. stream->this_frame = ptr;
  290. stream->next_frame = ptr + 1; /* possibly bogus sync word */
  291. mad_bit_init(&stream->ptr, stream->this_frame);
  292. if (decode_header(header, stream) == -1)
  293. goto fail;
  294. /* calculate frame duration */
  295. mad_timer_set(&header->duration, 0,
  296. 32 * MAD_NSBSAMPLES(header), header->samplerate);
  297. /* calculate free bit rate */
  298. if (header->bitrate == 0) {
  299. if ((stream->freerate == 0 || !stream->sync ||
  300. (header->layer == MAD_LAYER_III && stream->freerate > 640000)) &&
  301. free_bitrate(stream, header) == -1)
  302. goto fail;
  303. header->bitrate = stream->freerate;
  304. header->flags |= MAD_FLAG_FREEFORMAT;
  305. }
  306. /* calculate beginning of next frame */
  307. pad_slot = (header->flags & MAD_FLAG_PADDING) ? 1 : 0;
  308. if (header->layer == MAD_LAYER_I)
  309. N = ((12 * header->bitrate / header->samplerate) + pad_slot) * 4;
  310. else {
  311. unsigned int slots_per_frame;
  312. slots_per_frame = (header->layer == MAD_LAYER_III &&
  313. (header->flags & MAD_FLAG_LSF_EXT)) ? 72 : 144;
  314. N = (slots_per_frame * header->bitrate / header->samplerate) + pad_slot;
  315. }
  316. /* verify there is enough data left in buffer to decode this frame */
  317. if (N + MAD_BUFFER_GUARD > end - stream->this_frame) {
  318. stream->next_frame = stream->this_frame;
  319. stream->error = MAD_ERROR_BUFLEN;
  320. goto fail;
  321. }
  322. stream->next_frame = stream->this_frame + N;
  323. if (!stream->sync) {
  324. /* check that a valid frame header follows this frame */
  325. ptr = stream->next_frame;
  326. if (!(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0)) {
  327. ptr = stream->next_frame = stream->this_frame + 1;
  328. goto sync;
  329. }
  330. stream->sync = 1;
  331. }
  332. header->flags |= MAD_FLAG_INCOMPLETE;
  333. return 0;
  334. fail:
  335. stream->sync = 0;
  336. return -1;
  337. }
  338. /*
  339. * NAME: frame->decode()
  340. * DESCRIPTION: decode a single frame from a bitstream
  341. */
  342. int mad_frame_decode(struct mad_frame *frame, struct mad_stream *stream)
  343. {
  344. frame->options = stream->options;
  345. /* header() */
  346. /* error_check() */
  347. if (!(frame->header.flags & MAD_FLAG_INCOMPLETE) &&
  348. mad_header_decode(&frame->header, stream) == -1)
  349. goto fail;
  350. /* audio_data() */
  351. frame->header.flags &= ~MAD_FLAG_INCOMPLETE;
  352. if (decoder_table[frame->header.layer - 1](stream, frame) == -1) {
  353. if (!MAD_RECOVERABLE(stream->error))
  354. stream->next_frame = stream->this_frame;
  355. goto fail;
  356. }
  357. /* ancillary_data() */
  358. if (frame->header.layer != MAD_LAYER_III) {
  359. struct mad_bitptr next_frame;
  360. mad_bit_init(&next_frame, stream->next_frame);
  361. stream->anc_ptr = stream->ptr;
  362. stream->anc_bitlen = mad_bit_length(&stream->ptr, &next_frame);
  363. mad_bit_finish(&next_frame);
  364. }
  365. return 0;
  366. fail:
  367. stream->anc_bitlen = 0;
  368. return -1;
  369. }
  370. /*
  371. * NAME: frame->mute()
  372. * DESCRIPTION: zero all subband values so the frame becomes silent
  373. */
  374. void mad_frame_mute(struct mad_frame *frame)
  375. {
  376. unsigned int s, sb;
  377. for (s = 0; s < 36; ++s) {
  378. for (sb = 0; sb < 32; ++sb) {
  379. frame->sbsample[0][s][sb] =
  380. frame->sbsample[1][s][sb] = 0;
  381. }
  382. }
  383. if (frame->overlap) {
  384. for (s = 0; s < 18; ++s) {
  385. for (sb = 0; sb < 32; ++sb) {
  386. (*frame->overlap)[0][sb][s] =
  387. (*frame->overlap)[1][sb][s] = 0;
  388. }
  389. }
  390. }
  391. }