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.

263 lines
7.9KB

  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. av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol);
  70. return f;
  71. }
  72. int ff_a52_syncinfo( AVCodecContext * avctx, uint8_t * buf, int * flags, int * sample_rate, int * bit_rate )
  73. {
  74. AC3DecodeState *s = avctx->priv_data;
  75. return s->a52_syncinfo(buf, flags, sample_rate, bit_rate);
  76. }
  77. #endif
  78. static int a52_decode_init(AVCodecContext *avctx)
  79. {
  80. AC3DecodeState *s = avctx->priv_data;
  81. #ifdef CONFIG_A52BIN
  82. s->handle = dlopen(liba52name, RTLD_LAZY);
  83. if (!s->handle)
  84. {
  85. av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror());
  86. return -1;
  87. }
  88. s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
  89. s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
  90. s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
  91. s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
  92. s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
  93. s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
  94. if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
  95. || !s->a52_frame || !s->a52_block || !s->a52_free)
  96. {
  97. dlclose(s->handle);
  98. return -1;
  99. }
  100. #else
  101. /* static linked version */
  102. s->handle = 0;
  103. s->a52_init = a52_init;
  104. s->a52_samples = a52_samples;
  105. s->a52_syncinfo = a52_syncinfo;
  106. s->a52_frame = a52_frame;
  107. s->a52_block = a52_block;
  108. s->a52_free = a52_free;
  109. #endif
  110. s->state = s->a52_init(0); /* later use CPU flags */
  111. s->samples = s->a52_samples(s->state);
  112. s->inbuf_ptr = s->inbuf;
  113. s->frame_size = 0;
  114. return 0;
  115. }
  116. /**** the following two functions comes from a52dec */
  117. static inline int blah (int32_t i)
  118. {
  119. if (i > 0x43c07fff)
  120. return 32767;
  121. else if (i < 0x43bf8000)
  122. return -32768;
  123. return i - 0x43c00000;
  124. }
  125. static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
  126. {
  127. int i, j, c;
  128. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  129. j = 0;
  130. nchannels *= 256;
  131. for (i = 0; i < 256; i++) {
  132. for (c = 0; c < nchannels; c += 256)
  133. s16[j++] = blah (f[i + c]);
  134. }
  135. }
  136. /**** end */
  137. #define HEADER_SIZE 7
  138. static int a52_decode_frame(AVCodecContext *avctx,
  139. void *data, int *data_size,
  140. uint8_t *buf, int buf_size)
  141. {
  142. AC3DecodeState *s = avctx->priv_data;
  143. uint8_t *buf_ptr;
  144. int flags, i, len;
  145. int sample_rate, bit_rate;
  146. short *out_samples = data;
  147. float level;
  148. static const int ac3_channels[8] = {
  149. 2, 1, 2, 3, 3, 4, 4, 5
  150. };
  151. buf_ptr = buf;
  152. while (buf_size > 0) {
  153. len = s->inbuf_ptr - s->inbuf;
  154. if (s->frame_size == 0) {
  155. /* no header seen : find one. We need at least 7 bytes to parse it */
  156. len = HEADER_SIZE - len;
  157. if (len > buf_size)
  158. len = buf_size;
  159. memcpy(s->inbuf_ptr, buf_ptr, len);
  160. buf_ptr += len;
  161. s->inbuf_ptr += len;
  162. buf_size -= len;
  163. if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
  164. len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
  165. if (len == 0) {
  166. /* no sync found : move by one byte (inefficient, but simple!) */
  167. memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
  168. s->inbuf_ptr--;
  169. } else {
  170. s->frame_size = len;
  171. /* update codec info */
  172. avctx->sample_rate = sample_rate;
  173. s->channels = ac3_channels[s->flags & 7];
  174. if (s->flags & A52_LFE)
  175. s->channels++;
  176. if (avctx->channels == 0)
  177. /* No specific number of channel requested */
  178. avctx->channels = s->channels;
  179. else if (s->channels < avctx->channels) {
  180. av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
  181. avctx->channels = s->channels;
  182. }
  183. avctx->bit_rate = bit_rate;
  184. }
  185. }
  186. } else if (len < s->frame_size) {
  187. len = s->frame_size - len;
  188. if (len > buf_size)
  189. len = buf_size;
  190. memcpy(s->inbuf_ptr, buf_ptr, len);
  191. buf_ptr += len;
  192. s->inbuf_ptr += len;
  193. buf_size -= len;
  194. } else {
  195. flags = s->flags;
  196. if (avctx->channels == 1)
  197. flags = A52_MONO;
  198. else if (avctx->channels == 2)
  199. flags = A52_STEREO;
  200. else
  201. flags |= A52_ADJUST_LEVEL;
  202. level = 1;
  203. if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
  204. fail:
  205. s->inbuf_ptr = s->inbuf;
  206. s->frame_size = 0;
  207. continue;
  208. }
  209. for (i = 0; i < 6; i++) {
  210. if (s->a52_block(s->state))
  211. goto fail;
  212. float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
  213. }
  214. s->inbuf_ptr = s->inbuf;
  215. s->frame_size = 0;
  216. *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
  217. break;
  218. }
  219. }
  220. return buf_ptr - buf;
  221. }
  222. static int a52_decode_end(AVCodecContext *avctx)
  223. {
  224. AC3DecodeState *s = avctx->priv_data;
  225. s->a52_free(s->state);
  226. #ifdef CONFIG_A52BIN
  227. dlclose(s->handle);
  228. #endif
  229. return 0;
  230. }
  231. AVCodec ac3_decoder = {
  232. "ac3",
  233. CODEC_TYPE_AUDIO,
  234. CODEC_ID_AC3,
  235. sizeof(AC3DecodeState),
  236. a52_decode_init,
  237. NULL,
  238. a52_decode_end,
  239. a52_decode_frame,
  240. };