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.

252 lines
7.6KB

  1. /*
  2. * A52 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 "liba52/a52.h"
  21. #ifdef LIBAVCODEC_A52BIN
  22. #include <dlfcn.h>
  23. static const char* liba52name = "liba52.so.0";
  24. #endif
  25. /**
  26. * liba52 - Copyright (C) Aaron Holtzman
  27. * released under the GPL license.
  28. */
  29. typedef struct AC3DecodeState {
  30. UINT8 inbuf[4096]; /* input buffer */
  31. UINT8 *inbuf_ptr;
  32. int frame_size;
  33. int flags;
  34. int channels;
  35. a52_state_t* state;
  36. sample_t* samples;
  37. /*
  38. * virtual method table
  39. *
  40. * using this function table so the liba52 doesn't
  41. * have to be really linked together with ffmpeg
  42. * and might be linked in runtime - this allows binary
  43. * distribution of ffmpeg library which doens't depend
  44. * on liba52 library - but if user has it installed
  45. * it will be used - user might install such library
  46. * separately
  47. */
  48. void* handle;
  49. a52_state_t* (*a52_init)(uint32_t mm_accel);
  50. sample_t* (*a52_samples)(a52_state_t * state);
  51. int (*a52_syncinfo)(uint8_t * buf, int * flags,
  52. int * sample_rate, int * bit_rate);
  53. int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
  54. sample_t * level, sample_t bias);
  55. void (*a52_dynrng)(a52_state_t * state,
  56. sample_t (* call) (sample_t, void *), void * data);
  57. int (*a52_block)(a52_state_t * state);
  58. void (*a52_free)(a52_state_t * state);
  59. } AC3DecodeState;
  60. #ifdef LIBAVCODEC_A52BIN
  61. static void* dlsymm(void* handle, const char* symbol)
  62. {
  63. void* f = dlsym(handle, symbol);
  64. if (!f)
  65. fprintf(stderr, "A52 Decoder - function '%s' can't be resolved\n", symbol);
  66. return f;
  67. }
  68. #endif
  69. static int a52_decode_init(AVCodecContext *avctx)
  70. {
  71. AC3DecodeState *s = avctx->priv_data;
  72. #ifdef LIBAVCODEC_A52BIN
  73. s->handle = dlopen(liba52name, RTLD_LAZY);
  74. if (!s->handle)
  75. {
  76. fprintf(stderr, "A52 library %s could not be opened: %s\n", liba52name, dlerror());
  77. return -1;
  78. }
  79. s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
  80. s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
  81. s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
  82. s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
  83. s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
  84. s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
  85. if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
  86. || !s->a52_frame || !s->a52_block || !s->a52_free)
  87. {
  88. dlclose(s->handle);
  89. return -1;
  90. }
  91. printf("INITIALIZED\n");
  92. #else
  93. /* static linked version */
  94. s->handle = 0;
  95. s->a52_init = a52_init;
  96. s->a52_samples = a52_samples;
  97. s->a52_syncinfo = a52_syncinfo;
  98. s->a52_frame = a52_frame;
  99. s->a52_block = a52_block;
  100. s->a52_free = a52_free;
  101. #endif
  102. s->state = s->a52_init(0); /* later use CPU flags */
  103. s->samples = s->a52_samples(s->state);
  104. s->inbuf_ptr = s->inbuf;
  105. s->frame_size = 0;
  106. return 0;
  107. }
  108. /**** the following two functions comes from a52dec */
  109. static inline int blah (int32_t i)
  110. {
  111. if (i > 0x43c07fff)
  112. return 32767;
  113. else if (i < 0x43bf8000)
  114. return -32768;
  115. return i - 0x43c00000;
  116. }
  117. static inline void float_to_int (float * _f, INT16 * s16, int nchannels)
  118. {
  119. int i, j, c;
  120. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  121. j = 0;
  122. nchannels *= 256;
  123. for (i = 0; i < 256; i++) {
  124. for (c = 0; c < nchannels; c += 256)
  125. s16[j++] = blah (f[i + c]);
  126. }
  127. }
  128. /**** end */
  129. #define HEADER_SIZE 7
  130. static int a52_decode_frame(AVCodecContext *avctx,
  131. void *data, int *data_size,
  132. UINT8 *buf, int buf_size)
  133. {
  134. AC3DecodeState *s = avctx->priv_data;
  135. UINT8 *buf_ptr;
  136. int flags, i, len;
  137. int sample_rate, bit_rate;
  138. short *out_samples = data;
  139. float level;
  140. static const int ac3_channels[8] = {
  141. 2, 1, 2, 3, 3, 4, 4, 5
  142. };
  143. *data_size = 0;
  144. buf_ptr = buf;
  145. while (buf_size > 0) {
  146. len = s->inbuf_ptr - s->inbuf;
  147. if (s->frame_size == 0) {
  148. /* no header seen : find one. We need at least 7 bytes to parse it */
  149. len = HEADER_SIZE - len;
  150. if (len > buf_size)
  151. len = buf_size;
  152. memcpy(s->inbuf_ptr, buf_ptr, len);
  153. buf_ptr += len;
  154. s->inbuf_ptr += len;
  155. buf_size -= len;
  156. if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
  157. len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
  158. if (len == 0) {
  159. /* no sync found : move by one byte (inefficient, but simple!) */
  160. memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
  161. s->inbuf_ptr--;
  162. } else {
  163. s->frame_size = len;
  164. /* update codec info */
  165. avctx->sample_rate = sample_rate;
  166. s->channels = ac3_channels[s->flags & 7];
  167. if (s->flags & A52_LFE)
  168. s->channels++;
  169. if (avctx->channels == 0)
  170. /* No specific number of channel requested */
  171. avctx->channels = s->channels;
  172. else if (s->channels < avctx->channels) {
  173. fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
  174. avctx->channels = s->channels;
  175. }
  176. avctx->bit_rate = bit_rate;
  177. }
  178. }
  179. } else if (len < s->frame_size) {
  180. len = s->frame_size - len;
  181. if (len > buf_size)
  182. len = buf_size;
  183. memcpy(s->inbuf_ptr, buf_ptr, len);
  184. buf_ptr += len;
  185. s->inbuf_ptr += len;
  186. buf_size -= len;
  187. } else {
  188. flags = s->flags;
  189. if (avctx->channels == 1)
  190. flags = A52_MONO;
  191. else if (avctx->channels == 2)
  192. flags = A52_STEREO;
  193. else
  194. flags |= A52_ADJUST_LEVEL;
  195. level = 1;
  196. if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
  197. fail:
  198. s->inbuf_ptr = s->inbuf;
  199. s->frame_size = 0;
  200. continue;
  201. }
  202. for (i = 0; i < 6; i++) {
  203. if (s->a52_block(s->state))
  204. goto fail;
  205. float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
  206. }
  207. s->inbuf_ptr = s->inbuf;
  208. s->frame_size = 0;
  209. *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
  210. break;
  211. }
  212. }
  213. return buf_ptr - buf;
  214. }
  215. static int a52_decode_end(AVCodecContext *avctx)
  216. {
  217. AC3DecodeState *s = avctx->priv_data;
  218. s->a52_free(s->state);
  219. #ifdef LIBAVCODEC_A52BIN
  220. dlclose(s->handle);
  221. #endif
  222. return 0;
  223. }
  224. AVCodec ac3_decoder = {
  225. "ac3",
  226. CODEC_TYPE_AUDIO,
  227. CODEC_ID_AC3,
  228. sizeof(AC3DecodeState),
  229. a52_decode_init,
  230. NULL,
  231. a52_decode_end,
  232. a52_decode_frame,
  233. };