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.

181 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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "avcodec.h"
  23. #include <inttypes.h>
  24. #include "libac3/ac3.h"
  25. /* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
  26. released under the GPL license. I may reimplement it someday... */
  27. typedef struct AC3DecodeState {
  28. UINT8 inbuf[4096]; /* input buffer */
  29. UINT8 *inbuf_ptr;
  30. int frame_size;
  31. int flags;
  32. ac3_state_t state;
  33. } AC3DecodeState;
  34. static int ac3_decode_init(AVCodecContext *avctx)
  35. {
  36. AC3DecodeState *s = avctx->priv_data;
  37. ac3_init ();
  38. s->inbuf_ptr = s->inbuf;
  39. s->frame_size = 0;
  40. return 0;
  41. }
  42. stream_samples_t samples;
  43. /**** the following two functions comes from ac3dec */
  44. static inline int blah (int32_t i)
  45. {
  46. if (i > 0x43c07fff)
  47. return 32767;
  48. else if (i < 0x43bf8000)
  49. return -32768;
  50. else
  51. return i - 0x43c00000;
  52. }
  53. static inline void float_to_int (float * _f, INT16 * s16)
  54. {
  55. int i;
  56. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  57. for (i = 0; i < 256; i++) {
  58. s16[2*i] = blah (f[i]);
  59. s16[2*i+1] = blah (f[i+256]);
  60. }
  61. }
  62. static inline void float_to_int_mono (float * _f, INT16 * s16)
  63. {
  64. int i;
  65. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  66. for (i = 0; i < 256; i++) {
  67. s16[i] = blah (f[i]);
  68. }
  69. }
  70. /**** end */
  71. #define HEADER_SIZE 7
  72. static int ac3_decode_frame(AVCodecContext *avctx,
  73. void *data, int *data_size,
  74. UINT8 *buf, int buf_size)
  75. {
  76. AC3DecodeState *s = avctx->priv_data;
  77. UINT8 *buf_ptr;
  78. int flags, i, len;
  79. int sample_rate, bit_rate;
  80. short *out_samples = data;
  81. float level;
  82. *data_size = 0;
  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. if ((s->flags & AC3_CHANNEL_MASK) == AC3_MONO)
  106. avctx->channels = 1;
  107. else
  108. avctx->channels = 2;
  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. if (avctx->channels == 1)
  122. flags = AC3_MONO;
  123. else
  124. flags = AC3_STEREO;
  125. flags |= AC3_ADJUST_LEVEL;
  126. level = 1;
  127. if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
  128. fail:
  129. s->inbuf_ptr = s->inbuf;
  130. s->frame_size = 0;
  131. continue;
  132. }
  133. for (i = 0; i < 6; i++) {
  134. if (ac3_block (&s->state))
  135. goto fail;
  136. if (avctx->channels == 1)
  137. float_to_int_mono (*samples, out_samples + i * 256);
  138. else
  139. float_to_int (*samples, out_samples + i * 512);
  140. }
  141. s->inbuf_ptr = s->inbuf;
  142. s->frame_size = 0;
  143. *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
  144. break;
  145. }
  146. }
  147. return buf_ptr - buf;
  148. }
  149. static int ac3_decode_end(AVCodecContext *s)
  150. {
  151. return 0;
  152. }
  153. AVCodec ac3_decoder = {
  154. "ac3",
  155. CODEC_TYPE_AUDIO,
  156. CODEC_ID_AC3,
  157. sizeof(AC3DecodeState),
  158. ac3_decode_init,
  159. NULL,
  160. ac3_decode_end,
  161. ac3_decode_frame,
  162. };