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.

337 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 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";
  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 void channel_setup(AVCodecContext *avctx)
  93. {
  94. #ifdef FAAD2_VERSION
  95. FAACContext *s = avctx->priv_data;
  96. if (avctx->request_channels > 0 && avctx->request_channels == 2 &&
  97. avctx->request_channels < avctx->channels) {
  98. faacDecConfigurationPtr faac_cfg;
  99. avctx->channels = 2;
  100. faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
  101. faac_cfg->downMatrix = 1;
  102. s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
  103. }
  104. #endif
  105. }
  106. static int faac_init_mp4(AVCodecContext *avctx)
  107. {
  108. FAACContext *s = avctx->priv_data;
  109. unsigned long samplerate;
  110. #ifndef FAAD2_VERSION
  111. unsigned long channels;
  112. #else
  113. unsigned char channels;
  114. #endif
  115. int r = 0;
  116. if (avctx->extradata){
  117. r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
  118. avctx->extradata_size,
  119. &samplerate, &channels);
  120. if (r < 0){
  121. av_log(avctx, AV_LOG_ERROR,
  122. "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n",
  123. r, samplerate, (long)channels, avctx->extradata_size);
  124. } else {
  125. avctx->sample_rate = samplerate;
  126. avctx->channels = channels;
  127. channel_setup(avctx);
  128. s->init = 1;
  129. }
  130. }
  131. return r;
  132. }
  133. static int faac_decode_frame(AVCodecContext *avctx,
  134. void *data, int *data_size,
  135. uint8_t *buf, int buf_size)
  136. {
  137. FAACContext *s = avctx->priv_data;
  138. #ifndef FAAD2_VERSION
  139. unsigned long bytesconsumed;
  140. short *sample_buffer = NULL;
  141. unsigned long samples;
  142. int out;
  143. #else
  144. faacDecFrameInfo frame_info;
  145. void *out;
  146. #endif
  147. if(buf_size == 0)
  148. return 0;
  149. #ifndef FAAD2_VERSION
  150. out = s->faacDecDecode(s->faac_handle,
  151. (unsigned char*)buf,
  152. &bytesconsumed,
  153. data,
  154. &samples);
  155. samples *= s->sample_size;
  156. if (data_size)
  157. *data_size = samples;
  158. return (buf_size < (int)bytesconsumed)
  159. ? buf_size : (int)bytesconsumed;
  160. #else
  161. if(!s->init){
  162. unsigned long srate;
  163. unsigned char channels;
  164. int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
  165. if(r < 0){
  166. av_log(avctx, AV_LOG_ERROR, "faac: codec init failed.\n");
  167. return -1;
  168. }
  169. avctx->sample_rate = srate;
  170. avctx->channels = channels;
  171. channel_setup(avctx);
  172. s->init = 1;
  173. }
  174. out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
  175. if (frame_info.error > 0) {
  176. av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
  177. s->faacDecGetErrorMessage(frame_info.error));
  178. return -1;
  179. }
  180. if (!avctx->frame_size)
  181. avctx->frame_size = frame_info.samples/avctx->channels;
  182. frame_info.samples *= s->sample_size;
  183. memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
  184. if (data_size)
  185. *data_size = frame_info.samples;
  186. return (buf_size < (int)frame_info.bytesconsumed)
  187. ? buf_size : (int)frame_info.bytesconsumed;
  188. #endif
  189. }
  190. static av_cold int faac_decode_end(AVCodecContext *avctx)
  191. {
  192. FAACContext *s = avctx->priv_data;
  193. s->faacDecClose(s->faac_handle);
  194. dlclose(s->handle);
  195. return 0;
  196. }
  197. static av_cold int faac_decode_init(AVCodecContext *avctx)
  198. {
  199. FAACContext *s = avctx->priv_data;
  200. faacDecConfigurationPtr faac_cfg;
  201. #ifdef CONFIG_LIBFAADBIN
  202. const char* err = 0;
  203. s->handle = dlopen(libfaadname, RTLD_LAZY);
  204. if (!s->handle)
  205. {
  206. av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
  207. libfaadname, dlerror());
  208. return -1;
  209. }
  210. #define dfaac(a) do { \
  211. const char* n = AV_STRINGIFY(faacDec ## a); \
  212. if (!err && !(s->faacDec ## a = dlsym(s->handle, n))) { \
  213. err = n; \
  214. } \
  215. } while(0)
  216. #else /* !CONFIG_LIBFAADBIN */
  217. #define dfaac(a) s->faacDec ## a = faacDec ## a
  218. #endif /* CONFIG_LIBFAADBIN */
  219. // resolve all needed function calls
  220. dfaac(Open);
  221. dfaac(Close);
  222. dfaac(GetCurrentConfiguration);
  223. dfaac(SetConfiguration);
  224. dfaac(Init);
  225. dfaac(Init2);
  226. dfaac(Decode);
  227. #ifdef FAAD2_VERSION
  228. dfaac(GetErrorMessage);
  229. #endif
  230. #undef dfaac
  231. #ifdef CONFIG_LIBFAADBIN
  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_coded_sample) {
  248. case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_coded_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. if(!s->init && avctx->channels > 0)
  275. channel_setup(avctx);
  276. avctx->sample_fmt = SAMPLE_FMT_S16;
  277. return 0;
  278. }
  279. #define AAC_CODEC(id, name, long_name_) \
  280. AVCodec name ## _decoder = { \
  281. #name, \
  282. CODEC_TYPE_AUDIO, \
  283. id, \
  284. sizeof(FAACContext), \
  285. faac_decode_init, \
  286. NULL, \
  287. faac_decode_end, \
  288. faac_decode_frame, \
  289. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  290. }
  291. // FIXME - raw AAC files - maybe just one entry will be enough
  292. AAC_CODEC(CODEC_ID_AAC, libfaad, "libfaad AAC (Advanced Audio Codec)");
  293. #undef AAC_CODEC