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.

315 lines
9.3KB

  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. 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_decode_frame(AVCodecContext *avctx,
  114. void *data, int *data_size,
  115. uint8_t *buf, int buf_size)
  116. {
  117. FAACContext *s = (FAACContext *) avctx->priv_data;
  118. #ifndef FAAD2_VERSION
  119. unsigned long bytesconsumed;
  120. short *sample_buffer = NULL;
  121. unsigned long samples;
  122. int out;
  123. #else
  124. faacDecFrameInfo frame_info;
  125. void *out;
  126. #endif
  127. if(buf_size == 0)
  128. return 0;
  129. #ifndef FAAD2_VERSION
  130. out = s->faacDecDecode(s->faac_handle,
  131. (unsigned char*)buf,
  132. &bytesconsumed,
  133. data,
  134. &samples);
  135. samples *= s->sample_size;
  136. if (data_size)
  137. *data_size = samples;
  138. return (buf_size < (int)bytesconsumed)
  139. ? buf_size : (int)bytesconsumed;
  140. #else
  141. out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
  142. if (frame_info.error > 0) {
  143. av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
  144. s->faacDecGetErrorMessage(frame_info.error));
  145. return 0;
  146. }
  147. frame_info.samples *= s->sample_size;
  148. memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
  149. if (data_size)
  150. *data_size = frame_info.samples;
  151. return (buf_size < (int)frame_info.bytesconsumed)
  152. ? buf_size : (int)frame_info.bytesconsumed;
  153. #endif
  154. }
  155. static int faac_decode_end(AVCodecContext *avctx)
  156. {
  157. FAACContext *s = (FAACContext *) avctx->priv_data;
  158. if (s->faacDecClose)
  159. s->faacDecClose(s->faac_handle);
  160. dlclose(s->handle);
  161. return 0;
  162. }
  163. static int faac_decode_init(AVCodecContext *avctx)
  164. {
  165. FAACContext *s = (FAACContext *) avctx->priv_data;
  166. faacDecConfigurationPtr faac_cfg;
  167. #ifdef CONFIG_FAADBIN
  168. const char* err = 0;
  169. s->handle = dlopen(libfaadname, RTLD_LAZY);
  170. if (!s->handle)
  171. {
  172. av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
  173. libfaadname, dlerror());
  174. return -1;
  175. }
  176. #define dfaac(a, b) \
  177. do { static const char* n = "faacDec" #a; \
  178. if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
  179. for(;;) {
  180. #else /* !CONFIG_FAADBIN */
  181. #define dfaac(a, b) s->faacDec ## a = faacDec ## a
  182. #endif /* CONFIG_FAADBIN */
  183. // resolve all needed function calls
  184. dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
  185. dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
  186. FAADAPI (*)(faacDecHandle)));
  187. #ifndef FAAD2_VERSION
  188. dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
  189. faacDecConfigurationPtr)));
  190. dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
  191. unsigned long*, unsigned long*)));
  192. dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
  193. unsigned long, unsigned long*,
  194. unsigned long*)));
  195. dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
  196. dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
  197. unsigned long*, short*, unsigned long*)));
  198. #else
  199. dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
  200. faacDecConfigurationPtr)));
  201. dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
  202. unsigned long, unsigned long*, unsigned char*)));
  203. dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
  204. unsigned long, unsigned long*,
  205. unsigned char*)));
  206. dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
  207. unsigned char*, unsigned long)));
  208. dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
  209. #endif
  210. #undef dfacc
  211. #ifdef CONFIG_FAADBIN
  212. break;
  213. }
  214. if (err) {
  215. dlclose(s->handle);
  216. av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
  217. err, libfaadname);
  218. return -1;
  219. }
  220. #endif
  221. s->faac_handle = s->faacDecOpen();
  222. if (!s->faac_handle) {
  223. av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
  224. faac_decode_end(avctx);
  225. return -1;
  226. }
  227. faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
  228. if (faac_cfg) {
  229. switch (avctx->bits_per_sample) {
  230. case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
  231. default:
  232. case 16:
  233. #ifdef FAAD2_VERSION
  234. faac_cfg->outputFormat = FAAD_FMT_16BIT;
  235. #endif
  236. s->sample_size = 2;
  237. break;
  238. case 24:
  239. #ifdef FAAD2_VERSION
  240. faac_cfg->outputFormat = FAAD_FMT_24BIT;
  241. #endif
  242. s->sample_size = 3;
  243. break;
  244. case 32:
  245. #ifdef FAAD2_VERSION
  246. faac_cfg->outputFormat = FAAD_FMT_32BIT;
  247. #endif
  248. s->sample_size = 4;
  249. break;
  250. }
  251. faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
  252. faac_cfg->defObjectType = LC;
  253. }
  254. s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
  255. faac_init_mp4(avctx);
  256. return 0;
  257. }
  258. #define AAC_CODEC(id, name) \
  259. AVCodec name ## _decoder = { \
  260. #name, \
  261. CODEC_TYPE_AUDIO, \
  262. id, \
  263. sizeof(FAACContext), \
  264. faac_decode_init, \
  265. NULL, \
  266. faac_decode_end, \
  267. faac_decode_frame, \
  268. }
  269. // FIXME - raw AAC files - maybe just one entry will be enough
  270. AAC_CODEC(CODEC_ID_AAC, aac);
  271. // If it's mp4 file - usually embeded into Qt Mov
  272. AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
  273. #undef AAC_CODEC