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.

248 lines
7.3KB

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