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.

226 lines
6.7KB

  1. /*
  2. * A52 decoder using liba52
  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 using liba52
  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. int flags;
  37. int channels;
  38. a52_state_t* state;
  39. sample_t* samples;
  40. /*
  41. * virtual method table
  42. *
  43. * using this function table so the liba52 doesn't
  44. * have to be really linked together with ffmpeg
  45. * and might be linked in runtime - this allows binary
  46. * distribution of ffmpeg library which doens't depend
  47. * on liba52 library - but if user has it installed
  48. * it will be used - user might install such library
  49. * separately
  50. */
  51. void* handle;
  52. a52_state_t* (*a52_init)(uint32_t mm_accel);
  53. sample_t* (*a52_samples)(a52_state_t * state);
  54. int (*a52_syncinfo)(uint8_t * buf, int * flags,
  55. int * sample_rate, int * bit_rate);
  56. int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
  57. sample_t * level, sample_t bias);
  58. void (*a52_dynrng)(a52_state_t * state,
  59. sample_t (* call) (sample_t, void *), void * data);
  60. int (*a52_block)(a52_state_t * state);
  61. void (*a52_free)(a52_state_t * state);
  62. } AC3DecodeState;
  63. #ifdef CONFIG_LIBA52BIN
  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. #endif
  72. static int a52_decode_init(AVCodecContext *avctx)
  73. {
  74. AC3DecodeState *s = avctx->priv_data;
  75. #ifdef CONFIG_LIBA52BIN
  76. s->handle = dlopen(liba52name, RTLD_LAZY);
  77. if (!s->handle)
  78. {
  79. av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror());
  80. return -1;
  81. }
  82. s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
  83. s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
  84. s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
  85. s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
  86. s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
  87. s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
  88. if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
  89. || !s->a52_frame || !s->a52_block || !s->a52_free)
  90. {
  91. dlclose(s->handle);
  92. return -1;
  93. }
  94. #else
  95. s->handle = 0;
  96. s->a52_init = a52_init;
  97. s->a52_samples = a52_samples;
  98. s->a52_syncinfo = a52_syncinfo;
  99. s->a52_frame = a52_frame;
  100. s->a52_block = a52_block;
  101. s->a52_free = a52_free;
  102. #endif
  103. s->state = s->a52_init(0); /* later use CPU flags */
  104. s->samples = s->a52_samples(s->state);
  105. /* allow downmixing to stereo or mono */
  106. if (avctx->channels > 0 && avctx->request_channels > 0 &&
  107. avctx->request_channels < avctx->channels &&
  108. avctx->request_channels <= 2) {
  109. avctx->channels = avctx->request_channels;
  110. }
  111. return 0;
  112. }
  113. /**** the following function comes from a52dec */
  114. static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
  115. {
  116. int i, j, c;
  117. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  118. j = 0;
  119. nchannels *= 256;
  120. for (i = 0; i < 256; i++) {
  121. for (c = 0; c < nchannels; c += 256)
  122. s16[j++] = av_clip_int16(f[i + c] - 0x43c00000);
  123. }
  124. }
  125. /**** end */
  126. #define HEADER_SIZE 7
  127. static int a52_decode_frame(AVCodecContext *avctx,
  128. void *data, int *data_size,
  129. uint8_t *buf, int buf_size)
  130. {
  131. AC3DecodeState *s = avctx->priv_data;
  132. int flags, i, len;
  133. int sample_rate, bit_rate;
  134. short *out_samples = data;
  135. float level;
  136. static const int ac3_channels[8] = {
  137. 2, 1, 2, 3, 3, 4, 4, 5
  138. };
  139. *data_size= 0;
  140. if (buf_size < HEADER_SIZE) {
  141. av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes for header\n");
  142. return -1;
  143. }
  144. len = s->a52_syncinfo(buf, &s->flags, &sample_rate, &bit_rate);
  145. if (len == 0) {
  146. av_log(avctx, AV_LOG_ERROR, "Error decoding frame, no sync byte at begin\n");
  147. return -1;
  148. }
  149. if (buf_size < len) {
  150. av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes\n");
  151. return -1;
  152. }
  153. /* update codec info */
  154. avctx->sample_rate = sample_rate;
  155. s->channels = ac3_channels[s->flags & 7];
  156. if (s->flags & A52_LFE)
  157. s->channels++;
  158. if (avctx->request_channels > 0 &&
  159. avctx->request_channels <= 2 &&
  160. avctx->request_channels < s->channels) {
  161. avctx->channels = avctx->request_channels;
  162. } else {
  163. avctx->channels = s->channels;
  164. }
  165. avctx->bit_rate = bit_rate;
  166. flags = s->flags;
  167. if (avctx->channels == 1)
  168. flags = A52_MONO;
  169. else if (avctx->channels == 2)
  170. flags = A52_STEREO;
  171. else
  172. flags |= A52_ADJUST_LEVEL;
  173. level = 1;
  174. if (s->a52_frame(s->state, buf, &flags, &level, 384)) {
  175. fail:
  176. av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
  177. return -1;
  178. }
  179. for (i = 0; i < 6; i++) {
  180. if (s->a52_block(s->state))
  181. goto fail;
  182. float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
  183. }
  184. *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
  185. return len;
  186. }
  187. static int a52_decode_end(AVCodecContext *avctx)
  188. {
  189. AC3DecodeState *s = avctx->priv_data;
  190. s->a52_free(s->state);
  191. #ifdef CONFIG_LIBA52BIN
  192. dlclose(s->handle);
  193. #endif
  194. return 0;
  195. }
  196. AVCodec liba52_decoder = {
  197. "liba52",
  198. CODEC_TYPE_AUDIO,
  199. CODEC_ID_AC3,
  200. sizeof(AC3DecodeState),
  201. a52_decode_init,
  202. NULL,
  203. a52_decode_end,
  204. a52_decode_frame,
  205. };