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.

185 lines
5.5KB

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