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.

176 lines
5.0KB

  1. /*
  2. * AC3 decoder
  3. * Copyright (c) 2001 Gerard Lantau.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "avcodec.h"
  20. #include "libac3/ac3.h"
  21. /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
  22. released under the GPL license. I may reimplement it someday... */
  23. typedef struct AC3DecodeState {
  24. UINT8 inbuf[4096]; /* input buffer */
  25. UINT8 *inbuf_ptr;
  26. int frame_size;
  27. int flags;
  28. ac3_state_t state;
  29. } AC3DecodeState;
  30. static int ac3_decode_init(AVCodecContext *avctx)
  31. {
  32. AC3DecodeState *s = avctx->priv_data;
  33. ac3_init ();
  34. s->inbuf_ptr = s->inbuf;
  35. s->frame_size = 0;
  36. return 0;
  37. }
  38. stream_samples_t samples;
  39. /**** the following two functions comes from ac3dec */
  40. static inline int blah (int32_t i)
  41. {
  42. if (i > 0x43c07fff)
  43. return 32767;
  44. else if (i < 0x43bf8000)
  45. return -32768;
  46. else
  47. return i - 0x43c00000;
  48. }
  49. static inline void float_to_int (float * _f, INT16 * s16)
  50. {
  51. int i;
  52. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  53. for (i = 0; i < 256; i++) {
  54. s16[2*i] = blah (f[i]);
  55. s16[2*i+1] = blah (f[i+256]);
  56. }
  57. }
  58. static inline void float_to_int_mono (float * _f, INT16 * s16)
  59. {
  60. int i;
  61. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  62. for (i = 0; i < 256; i++) {
  63. s16[i] = blah (f[i]);
  64. }
  65. }
  66. /**** end */
  67. #define HEADER_SIZE 7
  68. static int ac3_decode_frame(AVCodecContext *avctx,
  69. void *data, int *data_size,
  70. UINT8 *buf, int buf_size)
  71. {
  72. AC3DecodeState *s = avctx->priv_data;
  73. UINT8 *buf_ptr;
  74. int flags, i, len;
  75. int sample_rate, bit_rate;
  76. short *out_samples = data;
  77. float level;
  78. *data_size = 0;
  79. buf_ptr = buf;
  80. while (buf_size > 0) {
  81. len = s->inbuf_ptr - s->inbuf;
  82. if (s->frame_size == 0) {
  83. /* no header seen : find one. We need at least 7 bytes to parse it */
  84. len = HEADER_SIZE - len;
  85. if (len > buf_size)
  86. len = buf_size;
  87. memcpy(s->inbuf_ptr, buf_ptr, len);
  88. buf_ptr += len;
  89. s->inbuf_ptr += len;
  90. buf_size -= len;
  91. if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
  92. len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
  93. if (len == 0) {
  94. /* no sync found : move by one byte (inefficient, but simple!) */
  95. memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
  96. s->inbuf_ptr--;
  97. } else {
  98. s->frame_size = len;
  99. /* update codec info */
  100. avctx->sample_rate = sample_rate;
  101. if ((s->flags & AC3_CHANNEL_MASK) == AC3_MONO)
  102. avctx->channels = 1;
  103. else
  104. avctx->channels = 2;
  105. avctx->bit_rate = bit_rate;
  106. }
  107. }
  108. } else if (len < s->frame_size) {
  109. len = s->frame_size - len;
  110. if (len > buf_size)
  111. len = buf_size;
  112. memcpy(s->inbuf_ptr, buf_ptr, len);
  113. buf_ptr += len;
  114. s->inbuf_ptr += len;
  115. buf_size -= len;
  116. } else {
  117. if (avctx->channels == 1)
  118. flags = AC3_MONO;
  119. else
  120. flags = AC3_STEREO;
  121. flags |= AC3_ADJUST_LEVEL;
  122. level = 1;
  123. if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
  124. fail:
  125. s->inbuf_ptr = s->inbuf;
  126. s->frame_size = 0;
  127. continue;
  128. }
  129. for (i = 0; i < 6; i++) {
  130. if (ac3_block (&s->state))
  131. goto fail;
  132. if (avctx->channels == 1)
  133. float_to_int_mono (*samples, out_samples + i * 256);
  134. else
  135. float_to_int (*samples, out_samples + i * 512);
  136. }
  137. s->inbuf_ptr = s->inbuf;
  138. s->frame_size = 0;
  139. *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
  140. break;
  141. }
  142. }
  143. return buf_ptr - buf;
  144. }
  145. static int ac3_decode_end(AVCodecContext *s)
  146. {
  147. return 0;
  148. }
  149. AVCodec ac3_decoder = {
  150. "ac3",
  151. CODEC_TYPE_AUDIO,
  152. CODEC_ID_AC3,
  153. sizeof(AC3DecodeState),
  154. ac3_decode_init,
  155. NULL,
  156. ac3_decode_end,
  157. ac3_decode_frame,
  158. };