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.

183 lines
5.2KB

  1. /*
  2. * AC3 decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file ac3dec.c
  21. * AC3 decoder.
  22. */
  23. //#define DEBUG
  24. #include "avcodec.h"
  25. #include "libac3/ac3.h"
  26. /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
  27. released under the GPL license. I may reimplement it someday... */
  28. typedef struct AC3DecodeState {
  29. uint8_t inbuf[4096]; /* input buffer */
  30. uint8_t *inbuf_ptr;
  31. int frame_size;
  32. int flags;
  33. int channels;
  34. ac3_state_t state;
  35. } AC3DecodeState;
  36. static int ac3_decode_init(AVCodecContext *avctx)
  37. {
  38. AC3DecodeState *s = avctx->priv_data;
  39. ac3_init ();
  40. s->inbuf_ptr = s->inbuf;
  41. s->frame_size = 0;
  42. return 0;
  43. }
  44. stream_samples_t samples;
  45. /**** the following two functions comes from ac3dec */
  46. static inline int blah (int32_t i)
  47. {
  48. if (i > 0x43c07fff)
  49. return 32767;
  50. else if (i < 0x43bf8000)
  51. return -32768;
  52. else
  53. return i - 0x43c00000;
  54. }
  55. static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
  56. {
  57. int i, j, c;
  58. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  59. j = 0;
  60. nchannels *= 256;
  61. for (i = 0; i < 256; i++) {
  62. for (c = 0; c < nchannels; c += 256)
  63. s16[j++] = blah (f[i + c]);
  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_t *buf, int buf_size)
  71. {
  72. AC3DecodeState *s = avctx->priv_data;
  73. uint8_t *buf_ptr;
  74. int flags, i, len;
  75. int sample_rate, bit_rate;
  76. short *out_samples = data;
  77. float level;
  78. static const int ac3_channels[8] = {
  79. 2, 1, 2, 3, 3, 4, 4, 5
  80. };
  81. buf_ptr = buf;
  82. while (buf_size > 0) {
  83. len = s->inbuf_ptr - s->inbuf;
  84. if (s->frame_size == 0) {
  85. /* no header seen : find one. We need at least 7 bytes to parse it */
  86. len = HEADER_SIZE - len;
  87. if (len > buf_size)
  88. len = buf_size;
  89. memcpy(s->inbuf_ptr, buf_ptr, len);
  90. buf_ptr += len;
  91. s->inbuf_ptr += len;
  92. buf_size -= len;
  93. if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
  94. len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
  95. if (len == 0) {
  96. /* no sync found : move by one byte (inefficient, but simple!) */
  97. memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
  98. s->inbuf_ptr--;
  99. } else {
  100. s->frame_size = len;
  101. /* update codec info */
  102. avctx->sample_rate = sample_rate;
  103. s->channels = ac3_channels[s->flags & 7];
  104. if (s->flags & AC3_LFE)
  105. s->channels++;
  106. if (avctx->channels == 0)
  107. /* No specific number of channel requested */
  108. avctx->channels = s->channels;
  109. else if (s->channels < avctx->channels) {
  110. fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
  111. avctx->channels = s->channels;
  112. }
  113. avctx->bit_rate = bit_rate;
  114. }
  115. }
  116. } else if (len < s->frame_size) {
  117. len = s->frame_size - len;
  118. if (len > buf_size)
  119. len = buf_size;
  120. memcpy(s->inbuf_ptr, buf_ptr, len);
  121. buf_ptr += len;
  122. s->inbuf_ptr += len;
  123. buf_size -= len;
  124. } else {
  125. flags = s->flags;
  126. if (avctx->channels == 1)
  127. flags = AC3_MONO;
  128. else if (avctx->channels == 2)
  129. flags = AC3_STEREO;
  130. else
  131. flags |= AC3_ADJUST_LEVEL;
  132. level = 1;
  133. if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
  134. fail:
  135. s->inbuf_ptr = s->inbuf;
  136. s->frame_size = 0;
  137. continue;
  138. }
  139. for (i = 0; i < 6; i++) {
  140. if (ac3_block (&s->state))
  141. goto fail;
  142. float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
  143. }
  144. s->inbuf_ptr = s->inbuf;
  145. s->frame_size = 0;
  146. *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
  147. break;
  148. }
  149. }
  150. return buf_ptr - buf;
  151. }
  152. static int ac3_decode_end(AVCodecContext *s)
  153. {
  154. return 0;
  155. }
  156. AVCodec ac3_decoder = {
  157. "ac3",
  158. CODEC_TYPE_AUDIO,
  159. CODEC_ID_AC3,
  160. sizeof(AC3DecodeState),
  161. ac3_decode_init,
  162. NULL,
  163. ac3_decode_end,
  164. ac3_decode_frame,
  165. };