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.

320 lines
9.4KB

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