frame.c 12 KB

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