a52dec.c 8.1 KB

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