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.

331 lines
11KB

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