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.

258 lines
8.1KB

  1. /*
  2. * A52 decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file a52dec.c
  23. * A52 decoder.
  24. */
  25. #include "avcodec.h"
  26. #include <a52dec/a52.h>
  27. #ifdef CONFIG_LIBA52BIN
  28. #include <dlfcn.h>
  29. static const char* liba52name = "liba52.so.0";
  30. #endif
  31. /**
  32. * liba52 - Copyright (C) Aaron Holtzman
  33. * released under the GPL license.
  34. */
  35. typedef struct AC3DecodeState {
  36. uint8_t inbuf[4096]; /* input buffer */
  37. uint8_t *inbuf_ptr;
  38. int frame_size;
  39. int flags;
  40. int channels;
  41. a52_state_t* state;
  42. sample_t* samples;
  43. /*
  44. * virtual method table
  45. *
  46. * using this function table so the liba52 doesn't
  47. * have to be really linked together with ffmpeg
  48. * and might be linked in runtime - this allows binary
  49. * distribution of ffmpeg library which doens't depend
  50. * on liba52 library - but if user has it installed
  51. * it will be used - user might install such library
  52. * separately
  53. */
  54. void* handle;
  55. a52_state_t* (*a52_init)(uint32_t mm_accel);
  56. sample_t* (*a52_samples)(a52_state_t * state);
  57. int (*a52_syncinfo)(uint8_t * buf, int * flags,
  58. int * sample_rate, int * bit_rate);
  59. int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
  60. sample_t * level, sample_t bias);
  61. void (*a52_dynrng)(a52_state_t * state,
  62. sample_t (* call) (sample_t, void *), void * data);
  63. int (*a52_block)(a52_state_t * state);
  64. void (*a52_free)(a52_state_t * state);
  65. } AC3DecodeState;
  66. static void* dlsymm(void* handle, const char* symbol)
  67. {
  68. void* f = dlsym(handle, symbol);
  69. if (!f)
  70. av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol);
  71. return f;
  72. }
  73. static int a52_decode_init(AVCodecContext *avctx)
  74. {
  75. AC3DecodeState *s = avctx->priv_data;
  76. #ifdef CONFIG_LIBA52BIN
  77. s->handle = dlopen(liba52name, RTLD_LAZY);
  78. if (!s->handle)
  79. {
  80. av_log( avctx, AV_LOG_ERROR, "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. s->handle = 0;
  97. s->a52_init = a52_init;
  98. s->a52_samples = a52_samples;
  99. s->a52_syncinfo = a52_syncinfo;
  100. s->a52_frame = a52_frame;
  101. s->a52_block = a52_block;
  102. s->a52_free = a52_free;
  103. #endif
  104. s->state = s->a52_init(0); /* later use CPU flags */
  105. s->samples = s->a52_samples(s->state);
  106. s->inbuf_ptr = s->inbuf;
  107. s->frame_size = 0;
  108. return 0;
  109. }
  110. /**** the following two functions comes from a52dec */
  111. static inline int blah (int32_t i)
  112. {
  113. if (i > 0x43c07fff)
  114. return 32767;
  115. else if (i < 0x43bf8000)
  116. return -32768;
  117. return i - 0x43c00000;
  118. }
  119. static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
  120. {
  121. int i, j, c;
  122. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  123. j = 0;
  124. nchannels *= 256;
  125. for (i = 0; i < 256; i++) {
  126. for (c = 0; c < nchannels; c += 256)
  127. s16[j++] = blah (f[i + c]);
  128. }
  129. }
  130. /**** end */
  131. #define HEADER_SIZE 7
  132. static int a52_decode_frame(AVCodecContext *avctx,
  133. void *data, int *data_size,
  134. uint8_t *buf, int buf_size)
  135. {
  136. AC3DecodeState *s = avctx->priv_data;
  137. uint8_t *buf_ptr;
  138. int flags, i, len;
  139. int sample_rate, bit_rate;
  140. short *out_samples = data;
  141. float level;
  142. static const int ac3_channels[8] = {
  143. 2, 1, 2, 3, 3, 4, 4, 5
  144. };
  145. *data_size= 0;
  146. buf_ptr = buf;
  147. while (buf_size > 0) {
  148. len = s->inbuf_ptr - s->inbuf;
  149. if (s->frame_size == 0) {
  150. /* no header seen : find one. We need at least 7 bytes to parse it */
  151. len = HEADER_SIZE - len;
  152. if (len > buf_size)
  153. len = buf_size;
  154. memcpy(s->inbuf_ptr, buf_ptr, len);
  155. buf_ptr += len;
  156. s->inbuf_ptr += len;
  157. buf_size -= len;
  158. if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
  159. len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
  160. if (len == 0) {
  161. /* no sync found : move by one byte (inefficient, but simple!) */
  162. memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
  163. s->inbuf_ptr--;
  164. } else {
  165. s->frame_size = len;
  166. /* update codec info */
  167. avctx->sample_rate = sample_rate;
  168. s->channels = ac3_channels[s->flags & 7];
  169. if (s->flags & A52_LFE)
  170. s->channels++;
  171. if (avctx->channels == 0)
  172. /* No specific number of channel requested */
  173. avctx->channels = s->channels;
  174. else if (s->channels < avctx->channels) {
  175. av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
  176. avctx->channels = s->channels;
  177. }
  178. avctx->bit_rate = bit_rate;
  179. }
  180. }
  181. } else if (len < s->frame_size) {
  182. len = s->frame_size - len;
  183. if (len > buf_size)
  184. len = buf_size;
  185. memcpy(s->inbuf_ptr, buf_ptr, len);
  186. buf_ptr += len;
  187. s->inbuf_ptr += len;
  188. buf_size -= len;
  189. } else {
  190. flags = s->flags;
  191. if (avctx->channels == 1)
  192. flags = A52_MONO;
  193. else if (avctx->channels == 2)
  194. flags = A52_STEREO;
  195. else
  196. flags |= A52_ADJUST_LEVEL;
  197. level = 1;
  198. if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
  199. fail:
  200. av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
  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_LIBA52BIN
  223. dlclose(s->handle);
  224. #endif
  225. return 0;
  226. }
  227. AVCodec liba52_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. };