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.1KB

  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. int channels;
  29. ac3_state_t state;
  30. } AC3DecodeState;
  31. static int ac3_decode_init(AVCodecContext *avctx)
  32. {
  33. AC3DecodeState *s = avctx->priv_data;
  34. ac3_init ();
  35. s->inbuf_ptr = s->inbuf;
  36. s->frame_size = 0;
  37. return 0;
  38. }
  39. stream_samples_t samples;
  40. /**** the following two functions comes from ac3dec */
  41. static inline int blah (int32_t i)
  42. {
  43. if (i > 0x43c07fff)
  44. return 32767;
  45. else if (i < 0x43bf8000)
  46. return -32768;
  47. else
  48. return i - 0x43c00000;
  49. }
  50. static inline void float_to_int (float * _f, INT16 * s16, int nchannels)
  51. {
  52. int i, j, c;
  53. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  54. j = 0;
  55. nchannels *= 256;
  56. for (i = 0; i < 256; i++) {
  57. for (c = 0; c < nchannels; c += 256)
  58. s16[j++] = blah (f[i + c]);
  59. }
  60. }
  61. /**** end */
  62. #define HEADER_SIZE 7
  63. static int ac3_decode_frame(AVCodecContext *avctx,
  64. void *data, int *data_size,
  65. UINT8 *buf, int buf_size)
  66. {
  67. AC3DecodeState *s = avctx->priv_data;
  68. UINT8 *buf_ptr;
  69. int flags, i, len;
  70. int sample_rate, bit_rate;
  71. short *out_samples = data;
  72. float level;
  73. static const int ac3_channels[8] = {
  74. 2, 1, 2, 3, 3, 4, 4, 5
  75. };
  76. *data_size = 0;
  77. buf_ptr = buf;
  78. while (buf_size > 0) {
  79. len = s->inbuf_ptr - s->inbuf;
  80. if (s->frame_size == 0) {
  81. /* no header seen : find one. We need at least 7 bytes to parse it */
  82. len = HEADER_SIZE - len;
  83. if (len > buf_size)
  84. len = buf_size;
  85. memcpy(s->inbuf_ptr, buf_ptr, len);
  86. buf_ptr += len;
  87. s->inbuf_ptr += len;
  88. buf_size -= len;
  89. if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
  90. len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
  91. if (len == 0) {
  92. /* no sync found : move by one byte (inefficient, but simple!) */
  93. memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
  94. s->inbuf_ptr--;
  95. } else {
  96. s->frame_size = len;
  97. /* update codec info */
  98. avctx->sample_rate = sample_rate;
  99. s->channels = ac3_channels[s->flags & 7];
  100. if (s->flags & AC3_LFE)
  101. s->channels++;
  102. if (avctx->channels == 0)
  103. /* No specific number of channel requested */
  104. avctx->channels = s->channels;
  105. else if (s->channels < avctx->channels) {
  106. fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
  107. avctx->channels = s->channels;
  108. }
  109. avctx->bit_rate = bit_rate;
  110. }
  111. }
  112. } else if (len < s->frame_size) {
  113. len = s->frame_size - len;
  114. if (len > buf_size)
  115. len = buf_size;
  116. memcpy(s->inbuf_ptr, buf_ptr, len);
  117. buf_ptr += len;
  118. s->inbuf_ptr += len;
  119. buf_size -= len;
  120. } else {
  121. flags = s->flags;
  122. if (avctx->channels == 1)
  123. flags = AC3_MONO;
  124. else if (avctx->channels == 2)
  125. flags = AC3_STEREO;
  126. else
  127. flags |= AC3_ADJUST_LEVEL;
  128. level = 1;
  129. if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
  130. fail:
  131. s->inbuf_ptr = s->inbuf;
  132. s->frame_size = 0;
  133. continue;
  134. }
  135. for (i = 0; i < 6; i++) {
  136. if (ac3_block (&s->state))
  137. goto fail;
  138. float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
  139. }
  140. s->inbuf_ptr = s->inbuf;
  141. s->frame_size = 0;
  142. *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
  143. break;
  144. }
  145. }
  146. return buf_ptr - buf;
  147. }
  148. static int ac3_decode_end(AVCodecContext *s)
  149. {
  150. return 0;
  151. }
  152. AVCodec ac3_decoder = {
  153. "ac3",
  154. CODEC_TYPE_AUDIO,
  155. CODEC_ID_AC3,
  156. sizeof(AC3DecodeState),
  157. ac3_decode_init,
  158. NULL,
  159. ac3_decode_end,
  160. ac3_decode_frame,
  161. };