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.

257 lines
7.7KB

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