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.

243 lines
7.8KB

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