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.

334 lines
10KB

  1. /*
  2. * Faad decoder
  3. * Copyright (c) 2003 Zdenek Kabelac
  4. * Copyright (c) 2004 Thomas Raivio
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/libfaad.c
  24. * AAC decoder.
  25. *
  26. * still a bit unfinished - but it plays something
  27. */
  28. #include "avcodec.h"
  29. #include "faad.h"
  30. #ifndef FAADAPI
  31. #define FAADAPI
  32. #endif
  33. /*
  34. * when CONFIG_LIBFAADBIN is true libfaad will be opened at runtime
  35. */
  36. //#undef CONFIG_LIBFAADBIN
  37. //#define CONFIG_LIBFAADBIN 0
  38. //#define CONFIG_LIBFAADBIN 1
  39. #if CONFIG_LIBFAADBIN
  40. #include <dlfcn.h>
  41. static const char* const libfaadname = "libfaad.so";
  42. #else
  43. #define dlopen(a)
  44. #define dlclose(a)
  45. #endif
  46. typedef struct {
  47. void* handle; /* dlopen handle */
  48. void* faac_handle; /* FAAD library handle */
  49. int sample_size;
  50. int init;
  51. /* faad calls */
  52. faacDecHandle FAADAPI (*faacDecOpen)(void);
  53. faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
  54. #ifndef FAAD2_VERSION
  55. int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
  56. faacDecConfigurationPtr config);
  57. int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
  58. unsigned char *buffer,
  59. unsigned long *samplerate,
  60. unsigned long *channels);
  61. int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
  62. unsigned long SizeOfDecoderSpecificInfo,
  63. unsigned long *samplerate, unsigned long *channels);
  64. int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
  65. unsigned char *buffer,
  66. unsigned long *bytesconsumed,
  67. short *sample_buffer,
  68. unsigned long *samples);
  69. #else
  70. unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
  71. faacDecConfigurationPtr config);
  72. long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
  73. unsigned char *buffer,
  74. unsigned long buffer_size,
  75. unsigned long *samplerate,
  76. unsigned char *channels);
  77. char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
  78. unsigned long SizeOfDecoderSpecificInfo,
  79. unsigned long *samplerate, unsigned char *channels);
  80. void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
  81. faacDecFrameInfo *hInfo,
  82. unsigned char *buffer,
  83. unsigned long buffer_size);
  84. char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
  85. #endif
  86. void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
  87. } FAACContext;
  88. static const unsigned long faac_srates[] =
  89. {
  90. 96000, 88200, 64000, 48000, 44100, 32000,
  91. 24000, 22050, 16000, 12000, 11025, 8000
  92. };
  93. static void channel_setup(AVCodecContext *avctx)
  94. {
  95. #ifdef FAAD2_VERSION
  96. FAACContext *s = avctx->priv_data;
  97. if (avctx->request_channels > 0 && avctx->request_channels == 2 &&
  98. avctx->request_channels < avctx->channels) {
  99. faacDecConfigurationPtr faac_cfg;
  100. avctx->channels = 2;
  101. faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
  102. faac_cfg->downMatrix = 1;
  103. s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
  104. }
  105. #endif
  106. }
  107. static av_cold int faac_init_mp4(AVCodecContext *avctx)
  108. {
  109. FAACContext *s = avctx->priv_data;
  110. unsigned long samplerate;
  111. #ifndef FAAD2_VERSION
  112. unsigned long channels;
  113. #else
  114. unsigned char channels;
  115. #endif
  116. int r = 0;
  117. if (avctx->extradata){
  118. r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
  119. avctx->extradata_size,
  120. &samplerate, &channels);
  121. if (r < 0){
  122. av_log(avctx, AV_LOG_ERROR,
  123. "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
  124. r, samplerate, (long)channels, avctx->extradata_size);
  125. } else {
  126. avctx->sample_rate = samplerate;
  127. avctx->channels = channels;
  128. channel_setup(avctx);
  129. s->init = 1;
  130. }
  131. }
  132. return r;
  133. }
  134. static int faac_decode_frame(AVCodecContext *avctx,
  135. void *data, int *data_size,
  136. AVPacket *avpkt)
  137. {
  138. const uint8_t *buf = avpkt->data;
  139. int buf_size = avpkt->size;
  140. FAACContext *s = avctx->priv_data;
  141. #ifndef FAAD2_VERSION
  142. unsigned long bytesconsumed;
  143. short *sample_buffer = NULL;
  144. unsigned long samples;
  145. int out;
  146. #else
  147. faacDecFrameInfo frame_info;
  148. void *out;
  149. #endif
  150. if(buf_size == 0)
  151. return 0;
  152. #ifndef FAAD2_VERSION
  153. out = s->faacDecDecode(s->faac_handle,
  154. (unsigned char*)buf,
  155. &bytesconsumed,
  156. data,
  157. &samples);
  158. samples *= s->sample_size;
  159. if (data_size)
  160. *data_size = samples;
  161. return (buf_size < (int)bytesconsumed)
  162. ? buf_size : (int)bytesconsumed;
  163. #else
  164. if(!s->init){
  165. unsigned long srate;
  166. unsigned char channels;
  167. int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
  168. if(r < 0){
  169. av_log(avctx, AV_LOG_ERROR, "faac: codec init failed.\n");
  170. return -1;
  171. }
  172. avctx->sample_rate = srate;
  173. avctx->channels = channels;
  174. channel_setup(avctx);
  175. s->init = 1;
  176. }
  177. out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
  178. if (frame_info.error > 0) {
  179. av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
  180. s->faacDecGetErrorMessage(frame_info.error));
  181. return -1;
  182. }
  183. if (!avctx->frame_size)
  184. avctx->frame_size = frame_info.samples/avctx->channels;
  185. frame_info.samples *= s->sample_size;
  186. memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
  187. if (data_size)
  188. *data_size = frame_info.samples;
  189. return (buf_size < (int)frame_info.bytesconsumed)
  190. ? buf_size : (int)frame_info.bytesconsumed;
  191. #endif
  192. }
  193. static av_cold int faac_decode_end(AVCodecContext *avctx)
  194. {
  195. FAACContext *s = avctx->priv_data;
  196. s->faacDecClose(s->faac_handle);
  197. dlclose(s->handle);
  198. return 0;
  199. }
  200. static av_cold int faac_decode_init(AVCodecContext *avctx)
  201. {
  202. FAACContext *s = avctx->priv_data;
  203. faacDecConfigurationPtr faac_cfg;
  204. #if CONFIG_LIBFAADBIN
  205. const char* err = 0;
  206. s->handle = dlopen(libfaadname, RTLD_LAZY);
  207. if (!s->handle)
  208. {
  209. av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
  210. libfaadname, dlerror());
  211. return -1;
  212. }
  213. #define dfaac(a) do { \
  214. const char* n = AV_STRINGIFY(faacDec ## a); \
  215. if (!err && !(s->faacDec ## a = dlsym(s->handle, n))) { \
  216. err = n; \
  217. } \
  218. } while(0)
  219. #else /* !CONFIG_LIBFAADBIN */
  220. #define dfaac(a) s->faacDec ## a = faacDec ## a
  221. #endif /* CONFIG_LIBFAADBIN */
  222. // resolve all needed function calls
  223. dfaac(Open);
  224. dfaac(Close);
  225. dfaac(GetCurrentConfiguration);
  226. dfaac(SetConfiguration);
  227. dfaac(Init);
  228. dfaac(Init2);
  229. dfaac(Decode);
  230. #ifdef FAAD2_VERSION
  231. dfaac(GetErrorMessage);
  232. #endif
  233. #undef dfaac
  234. #if CONFIG_LIBFAADBIN
  235. if (err) {
  236. dlclose(s->handle);
  237. av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
  238. err, libfaadname);
  239. return -1;
  240. }
  241. #endif
  242. s->faac_handle = s->faacDecOpen();
  243. if (!s->faac_handle) {
  244. av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
  245. faac_decode_end(avctx);
  246. return -1;
  247. }
  248. faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
  249. if (faac_cfg) {
  250. switch (avctx->bits_per_coded_sample) {
  251. case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_coded_sample); break;
  252. default:
  253. case 16:
  254. #ifdef FAAD2_VERSION
  255. faac_cfg->outputFormat = FAAD_FMT_16BIT;
  256. #endif
  257. s->sample_size = 2;
  258. break;
  259. case 24:
  260. #ifdef FAAD2_VERSION
  261. faac_cfg->outputFormat = FAAD_FMT_24BIT;
  262. #endif
  263. s->sample_size = 3;
  264. break;
  265. case 32:
  266. #ifdef FAAD2_VERSION
  267. faac_cfg->outputFormat = FAAD_FMT_32BIT;
  268. #endif
  269. s->sample_size = 4;
  270. break;
  271. }
  272. faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
  273. faac_cfg->defObjectType = LC;
  274. }
  275. s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
  276. faac_init_mp4(avctx);
  277. if(!s->init && avctx->channels > 0)
  278. channel_setup(avctx);
  279. avctx->sample_fmt = SAMPLE_FMT_S16;
  280. return 0;
  281. }
  282. AVCodec libfaad_decoder = {
  283. "libfaad",
  284. CODEC_TYPE_AUDIO,
  285. CODEC_ID_AAC,
  286. sizeof(FAACContext),
  287. faac_decode_init,
  288. NULL,
  289. faac_decode_end,
  290. faac_decode_frame,
  291. .long_name = NULL_IF_CONFIG_SMALL("libfaad AAC (Advanced Audio Codec)"),
  292. };