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.

184 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. *data_size = 0;
  82. buf_ptr = buf;
  83. while (buf_size > 0) {
  84. len = s->inbuf_ptr - s->inbuf;
  85. if (s->frame_size == 0) {
  86. /* no header seen : find one. We need at least 7 bytes to parse it */
  87. len = HEADER_SIZE - len;
  88. if (len > buf_size)
  89. len = buf_size;
  90. memcpy(s->inbuf_ptr, buf_ptr, len);
  91. buf_ptr += len;
  92. s->inbuf_ptr += len;
  93. buf_size -= len;
  94. if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
  95. len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
  96. if (len == 0) {
  97. /* no sync found : move by one byte (inefficient, but simple!) */
  98. memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
  99. s->inbuf_ptr--;
  100. } else {
  101. s->frame_size = len;
  102. /* update codec info */
  103. avctx->sample_rate = sample_rate;
  104. s->channels = ac3_channels[s->flags & 7];
  105. if (s->flags & AC3_LFE)
  106. s->channels++;
  107. if (avctx->channels == 0)
  108. /* No specific number of channel requested */
  109. avctx->channels = s->channels;
  110. else if (s->channels < avctx->channels) {
  111. fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
  112. avctx->channels = s->channels;
  113. }
  114. avctx->bit_rate = bit_rate;
  115. }
  116. }
  117. } else if (len < s->frame_size) {
  118. len = s->frame_size - len;
  119. if (len > buf_size)
  120. len = buf_size;
  121. memcpy(s->inbuf_ptr, buf_ptr, len);
  122. buf_ptr += len;
  123. s->inbuf_ptr += len;
  124. buf_size -= len;
  125. } else {
  126. flags = s->flags;
  127. if (avctx->channels == 1)
  128. flags = AC3_MONO;
  129. else if (avctx->channels == 2)
  130. flags = AC3_STEREO;
  131. else
  132. flags |= AC3_ADJUST_LEVEL;
  133. level = 1;
  134. if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
  135. fail:
  136. s->inbuf_ptr = s->inbuf;
  137. s->frame_size = 0;
  138. continue;
  139. }
  140. for (i = 0; i < 6; i++) {
  141. if (ac3_block (&s->state))
  142. goto fail;
  143. float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
  144. }
  145. s->inbuf_ptr = s->inbuf;
  146. s->frame_size = 0;
  147. *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
  148. break;
  149. }
  150. }
  151. return buf_ptr - buf;
  152. }
  153. static int ac3_decode_end(AVCodecContext *s)
  154. {
  155. return 0;
  156. }
  157. AVCodec ac3_decoder = {
  158. "ac3",
  159. CODEC_TYPE_AUDIO,
  160. CODEC_ID_AC3,
  161. sizeof(AC3DecodeState),
  162. ac3_decode_init,
  163. NULL,
  164. ac3_decode_end,
  165. ac3_decode_frame,
  166. };