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.

313 lines
9.2KB

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