libfaad.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. uint8_t *buf, int buf_size)
  137. {
  138. FAACContext *s = avctx->priv_data;
  139. #ifndef FAAD2_VERSION
  140. unsigned long bytesconsumed;
  141. short *sample_buffer = NULL;
  142. unsigned long samples;
  143. int out;
  144. #else
  145. faacDecFrameInfo frame_info;
  146. void *out;
  147. #endif
  148. if(buf_size == 0)
  149. return 0;
  150. #ifndef FAAD2_VERSION
  151. out = s->faacDecDecode(s->faac_handle,
  152. (unsigned char*)buf,
  153. &bytesconsumed,
  154. data,
  155. &samples);
  156. samples *= s->sample_size;
  157. if (data_size)
  158. *data_size = samples;
  159. return (buf_size < (int)bytesconsumed)
  160. ? buf_size : (int)bytesconsumed;
  161. #else
  162. if(!s->init){
  163. unsigned long srate;
  164. unsigned char channels;
  165. int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
  166. if(r < 0){
  167. av_log(avctx, AV_LOG_ERROR, "faac: codec init failed.\n");
  168. return -1;
  169. }
  170. avctx->sample_rate = srate;
  171. avctx->channels = channels;
  172. channel_setup(avctx);
  173. s->init = 1;
  174. }
  175. out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
  176. if (frame_info.error > 0) {
  177. av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
  178. s->faacDecGetErrorMessage(frame_info.error));
  179. return -1;
  180. }
  181. if (!avctx->frame_size)
  182. avctx->frame_size = frame_info.samples/avctx->channels;
  183. frame_info.samples *= s->sample_size;
  184. memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
  185. if (data_size)
  186. *data_size = frame_info.samples;
  187. return (buf_size < (int)frame_info.bytesconsumed)
  188. ? buf_size : (int)frame_info.bytesconsumed;
  189. #endif
  190. }
  191. static av_cold int faac_decode_end(AVCodecContext *avctx)
  192. {
  193. FAACContext *s = avctx->priv_data;
  194. s->faacDecClose(s->faac_handle);
  195. dlclose(s->handle);
  196. return 0;
  197. }
  198. static av_cold int faac_decode_init(AVCodecContext *avctx)
  199. {
  200. FAACContext *s = avctx->priv_data;
  201. faacDecConfigurationPtr faac_cfg;
  202. #if CONFIG_LIBFAADBIN
  203. const char* err = 0;
  204. s->handle = dlopen(libfaadname, RTLD_LAZY);
  205. if (!s->handle)
  206. {
  207. av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
  208. libfaadname, dlerror());
  209. return -1;
  210. }
  211. #define dfaac(a) do { \
  212. const char* n = AV_STRINGIFY(faacDec ## a); \
  213. if (!err && !(s->faacDec ## a = dlsym(s->handle, n))) { \
  214. err = n; \
  215. } \
  216. } while(0)
  217. #else /* !CONFIG_LIBFAADBIN */
  218. #define dfaac(a) s->faacDec ## a = faacDec ## a
  219. #endif /* CONFIG_LIBFAADBIN */
  220. // resolve all needed function calls
  221. dfaac(Open);
  222. dfaac(Close);
  223. dfaac(GetCurrentConfiguration);
  224. dfaac(SetConfiguration);
  225. dfaac(Init);
  226. dfaac(Init2);
  227. dfaac(Decode);
  228. #ifdef FAAD2_VERSION
  229. dfaac(GetErrorMessage);
  230. #endif
  231. #undef dfaac
  232. #if CONFIG_LIBFAADBIN
  233. if (err) {
  234. dlclose(s->handle);
  235. av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
  236. err, libfaadname);
  237. return -1;
  238. }
  239. #endif
  240. s->faac_handle = s->faacDecOpen();
  241. if (!s->faac_handle) {
  242. av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
  243. faac_decode_end(avctx);
  244. return -1;
  245. }
  246. faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
  247. if (faac_cfg) {
  248. switch (avctx->bits_per_coded_sample) {
  249. case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_coded_sample); break;
  250. default:
  251. case 16:
  252. #ifdef FAAD2_VERSION
  253. faac_cfg->outputFormat = FAAD_FMT_16BIT;
  254. #endif
  255. s->sample_size = 2;
  256. break;
  257. case 24:
  258. #ifdef FAAD2_VERSION
  259. faac_cfg->outputFormat = FAAD_FMT_24BIT;
  260. #endif
  261. s->sample_size = 3;
  262. break;
  263. case 32:
  264. #ifdef FAAD2_VERSION
  265. faac_cfg->outputFormat = FAAD_FMT_32BIT;
  266. #endif
  267. s->sample_size = 4;
  268. break;
  269. }
  270. faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
  271. faac_cfg->defObjectType = LC;
  272. }
  273. s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
  274. faac_init_mp4(avctx);
  275. if(!s->init && avctx->channels > 0)
  276. channel_setup(avctx);
  277. avctx->sample_fmt = SAMPLE_FMT_S16;
  278. return 0;
  279. }
  280. #define AAC_CODEC(id, name, long_name_) \
  281. AVCodec name ## _decoder = { \
  282. #name, \
  283. CODEC_TYPE_AUDIO, \
  284. id, \
  285. sizeof(FAACContext), \
  286. faac_decode_init, \
  287. NULL, \
  288. faac_decode_end, \
  289. faac_decode_frame, \
  290. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  291. }
  292. // FIXME - raw AAC files - maybe just one entry will be enough
  293. AAC_CODEC(CODEC_ID_AAC, libfaad, "libfaad AAC (Advanced Audio Codec)");
  294. #undef AAC_CODEC