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.

335 lines
11KB

  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 faad.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 defined the libfaad will be opened at runtime
  35. */
  36. //#undef CONFIG_LIBFAADBIN
  37. //#define CONFIG_LIBFAADBIN
  38. #ifdef CONFIG_LIBFAADBIN
  39. #include <dlfcn.h>
  40. static const char* libfaadname = "libfaad.so.0";
  41. #else
  42. #define dlopen(a)
  43. #define dlclose(a)
  44. #endif
  45. typedef struct {
  46. void* handle; /* dlopen handle */
  47. void* faac_handle; /* FAAD library handle */
  48. int sample_size;
  49. int init;
  50. /* faad calls */
  51. faacDecHandle FAADAPI (*faacDecOpen)(void);
  52. faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
  53. #ifndef FAAD2_VERSION
  54. int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
  55. faacDecConfigurationPtr config);
  56. int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
  57. unsigned char *buffer,
  58. unsigned long *samplerate,
  59. unsigned long *channels);
  60. int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
  61. unsigned long SizeOfDecoderSpecificInfo,
  62. unsigned long *samplerate, unsigned long *channels);
  63. int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
  64. unsigned char *buffer,
  65. unsigned long *bytesconsumed,
  66. short *sample_buffer,
  67. unsigned long *samples);
  68. #else
  69. unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
  70. faacDecConfigurationPtr config);
  71. long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
  72. unsigned char *buffer,
  73. unsigned long buffer_size,
  74. unsigned long *samplerate,
  75. unsigned char *channels);
  76. char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
  77. unsigned long SizeOfDecoderSpecificInfo,
  78. unsigned long *samplerate, unsigned char *channels);
  79. void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
  80. faacDecFrameInfo *hInfo,
  81. unsigned char *buffer,
  82. unsigned long buffer_size);
  83. char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
  84. #endif
  85. void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
  86. } FAACContext;
  87. static const unsigned long faac_srates[] =
  88. {
  89. 96000, 88200, 64000, 48000, 44100, 32000,
  90. 24000, 22050, 16000, 12000, 11025, 8000
  91. };
  92. static int faac_init_mp4(AVCodecContext *avctx)
  93. {
  94. FAACContext *s = (FAACContext *) avctx->priv_data;
  95. unsigned long samplerate;
  96. #ifndef FAAD2_VERSION
  97. unsigned long channels;
  98. #else
  99. unsigned char channels;
  100. #endif
  101. int r = 0;
  102. if (avctx->extradata){
  103. r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
  104. avctx->extradata_size,
  105. &samplerate, &channels);
  106. if (r < 0){
  107. av_log(avctx, AV_LOG_ERROR,
  108. "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
  109. r, samplerate, (long)channels, avctx->extradata_size);
  110. } else {
  111. avctx->sample_rate = samplerate;
  112. avctx->channels = channels;
  113. s->init = 1;
  114. }
  115. }
  116. return r;
  117. }
  118. static int faac_decode_frame(AVCodecContext *avctx,
  119. void *data, int *data_size,
  120. uint8_t *buf, int buf_size)
  121. {
  122. FAACContext *s = (FAACContext *) avctx->priv_data;
  123. #ifndef FAAD2_VERSION
  124. unsigned long bytesconsumed;
  125. short *sample_buffer = NULL;
  126. unsigned long samples;
  127. int out;
  128. #else
  129. faacDecFrameInfo frame_info;
  130. void *out;
  131. #endif
  132. if(buf_size == 0)
  133. return 0;
  134. #ifndef FAAD2_VERSION
  135. out = s->faacDecDecode(s->faac_handle,
  136. (unsigned char*)buf,
  137. &bytesconsumed,
  138. data,
  139. &samples);
  140. samples *= s->sample_size;
  141. if (data_size)
  142. *data_size = samples;
  143. return (buf_size < (int)bytesconsumed)
  144. ? buf_size : (int)bytesconsumed;
  145. #else
  146. if(!s->init){
  147. unsigned long srate;
  148. unsigned char channels;
  149. int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
  150. if(r < 0){
  151. av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
  152. s->faacDecGetErrorMessage(frame_info.error));
  153. return -1;
  154. }
  155. avctx->sample_rate = srate;
  156. avctx->channels = channels;
  157. s->init = 1;
  158. }
  159. out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
  160. if (frame_info.error > 0) {
  161. av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
  162. s->faacDecGetErrorMessage(frame_info.error));
  163. return -1;
  164. }
  165. frame_info.samples *= s->sample_size;
  166. memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
  167. if (data_size)
  168. *data_size = frame_info.samples;
  169. return (buf_size < (int)frame_info.bytesconsumed)
  170. ? buf_size : (int)frame_info.bytesconsumed;
  171. #endif
  172. }
  173. static int faac_decode_end(AVCodecContext *avctx)
  174. {
  175. FAACContext *s = (FAACContext *) avctx->priv_data;
  176. if (s->faacDecClose)
  177. s->faacDecClose(s->faac_handle);
  178. dlclose(s->handle);
  179. return 0;
  180. }
  181. static int faac_decode_init(AVCodecContext *avctx)
  182. {
  183. FAACContext *s = (FAACContext *) avctx->priv_data;
  184. faacDecConfigurationPtr faac_cfg;
  185. #ifdef CONFIG_LIBFAADBIN
  186. const char* err = 0;
  187. s->handle = dlopen(libfaadname, RTLD_LAZY);
  188. if (!s->handle)
  189. {
  190. av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
  191. libfaadname, dlerror());
  192. return -1;
  193. }
  194. #define dfaac(a, b) \
  195. do { static const char* n = "faacDec" #a; \
  196. if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
  197. for(;;) {
  198. #else /* !CONFIG_LIBFAADBIN */
  199. #define dfaac(a, b) s->faacDec ## a = faacDec ## a
  200. #endif /* CONFIG_LIBFAADBIN */
  201. // resolve all needed function calls
  202. dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
  203. dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
  204. FAADAPI (*)(faacDecHandle)));
  205. #ifndef FAAD2_VERSION
  206. dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
  207. faacDecConfigurationPtr)));
  208. dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
  209. unsigned long*, unsigned long*)));
  210. dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
  211. unsigned long, unsigned long*,
  212. unsigned long*)));
  213. dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
  214. dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
  215. unsigned long*, short*, unsigned long*)));
  216. #else
  217. dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
  218. faacDecConfigurationPtr)));
  219. dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
  220. unsigned long, unsigned long*, unsigned char*)));
  221. dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
  222. unsigned long, unsigned long*,
  223. unsigned char*)));
  224. dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
  225. unsigned char*, unsigned long)));
  226. dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
  227. #endif
  228. #undef dfacc
  229. #ifdef CONFIG_LIBFAADBIN
  230. break;
  231. }
  232. if (err) {
  233. dlclose(s->handle);
  234. av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
  235. err, libfaadname);
  236. return -1;
  237. }
  238. #endif
  239. s->faac_handle = s->faacDecOpen();
  240. if (!s->faac_handle) {
  241. av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
  242. faac_decode_end(avctx);
  243. return -1;
  244. }
  245. faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
  246. if (faac_cfg) {
  247. switch (avctx->bits_per_sample) {
  248. case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
  249. default:
  250. case 16:
  251. #ifdef FAAD2_VERSION
  252. faac_cfg->outputFormat = FAAD_FMT_16BIT;
  253. #endif
  254. s->sample_size = 2;
  255. break;
  256. case 24:
  257. #ifdef FAAD2_VERSION
  258. faac_cfg->outputFormat = FAAD_FMT_24BIT;
  259. #endif
  260. s->sample_size = 3;
  261. break;
  262. case 32:
  263. #ifdef FAAD2_VERSION
  264. faac_cfg->outputFormat = FAAD_FMT_32BIT;
  265. #endif
  266. s->sample_size = 4;
  267. break;
  268. }
  269. faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
  270. faac_cfg->defObjectType = LC;
  271. }
  272. s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
  273. faac_init_mp4(avctx);
  274. return 0;
  275. }
  276. #define AAC_CODEC(id, name) \
  277. AVCodec name ## _decoder = { \
  278. #name, \
  279. CODEC_TYPE_AUDIO, \
  280. id, \
  281. sizeof(FAACContext), \
  282. faac_decode_init, \
  283. NULL, \
  284. faac_decode_end, \
  285. faac_decode_frame, \
  286. }
  287. // FIXME - raw AAC files - maybe just one entry will be enough
  288. AAC_CODEC(CODEC_ID_AAC, aac);
  289. #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
  290. // If it's mp4 file - usually embeded into Qt Mov
  291. AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
  292. #endif
  293. #undef AAC_CODEC