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.

228 lines
6.8KB

  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 av_cold 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. avctx->sample_fmt = SAMPLE_FMT_S16;
  112. return 0;
  113. }
  114. /**** the following function comes from a52dec */
  115. static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
  116. {
  117. int i, j, c;
  118. int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
  119. j = 0;
  120. nchannels *= 256;
  121. for (i = 0; i < 256; i++) {
  122. for (c = 0; c < nchannels; c += 256)
  123. s16[j++] = av_clip_int16(f[i + c] - 0x43c00000);
  124. }
  125. }
  126. /**** end */
  127. #define HEADER_SIZE 7
  128. static int a52_decode_frame(AVCodecContext *avctx,
  129. void *data, int *data_size,
  130. uint8_t *buf, int buf_size)
  131. {
  132. AC3DecodeState *s = avctx->priv_data;
  133. int flags, i, len;
  134. int sample_rate, bit_rate;
  135. short *out_samples = data;
  136. float level;
  137. static const int ac3_channels[8] = {
  138. 2, 1, 2, 3, 3, 4, 4, 5
  139. };
  140. *data_size= 0;
  141. if (buf_size < HEADER_SIZE) {
  142. av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes for header\n");
  143. return -1;
  144. }
  145. len = s->a52_syncinfo(buf, &s->flags, &sample_rate, &bit_rate);
  146. if (len == 0) {
  147. av_log(avctx, AV_LOG_ERROR, "Error decoding frame, no sync byte at begin\n");
  148. return -1;
  149. }
  150. if (buf_size < len) {
  151. av_log(avctx, AV_LOG_ERROR, "Error decoding frame, not enough bytes\n");
  152. return -1;
  153. }
  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->request_channels > 0 &&
  160. avctx->request_channels <= 2 &&
  161. avctx->request_channels < s->channels) {
  162. avctx->channels = avctx->request_channels;
  163. } else {
  164. avctx->channels = s->channels;
  165. }
  166. avctx->bit_rate = bit_rate;
  167. flags = s->flags;
  168. if (avctx->channels == 1)
  169. flags = A52_MONO;
  170. else if (avctx->channels == 2)
  171. flags = A52_STEREO;
  172. else
  173. flags |= A52_ADJUST_LEVEL;
  174. level = 1;
  175. if (s->a52_frame(s->state, buf, &flags, &level, 384)) {
  176. fail:
  177. av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
  178. return -1;
  179. }
  180. for (i = 0; i < 6; i++) {
  181. if (s->a52_block(s->state))
  182. goto fail;
  183. float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
  184. }
  185. *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
  186. return len;
  187. }
  188. static av_cold int a52_decode_end(AVCodecContext *avctx)
  189. {
  190. AC3DecodeState *s = avctx->priv_data;
  191. s->a52_free(s->state);
  192. #ifdef CONFIG_LIBA52BIN
  193. dlclose(s->handle);
  194. #endif
  195. return 0;
  196. }
  197. AVCodec liba52_decoder = {
  198. "liba52",
  199. CODEC_TYPE_AUDIO,
  200. CODEC_ID_AC3,
  201. sizeof(AC3DecodeState),
  202. a52_decode_init,
  203. NULL,
  204. a52_decode_end,
  205. a52_decode_frame,
  206. .long_name = NULL_IF_CONFIG_SMALL("liba52 ATSC A/52 / AC-3"),
  207. };