mpeg4audio.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef NULLSOFT_MPEG4AUDIOH
  2. #define NULLSOFT_MPEG4AUDIOH
  3. #include "../external_dependencies/libmp4v2/mp4.h"
  4. #include <bfc/dispatch.h>
  5. #include <bfc/platform/types.h>
  6. #include <api/service/services.h>
  7. enum
  8. {
  9. MP4_SUCCESS = 0,
  10. MP4_FAILURE = 1,
  11. MP4_GETOUTPUTPROPERTIES_NEED_MORE_INPUT = 2,
  12. MP4_NEED_MORE_INPUT = 2,
  13. MP4_GETCURRENTBITRATE_UNKNOWN = 2, // unable to calculate (e.g. VBR for CT's decoder)
  14. MP4_OUTPUTFRAMESIZE_VARIABLE = 2, // don't know if any codecs do this
  15. MP4_TYPE_MPEG1_AUDIO = 0x6B,
  16. MP4_TYPE_MPEG2_AUDIO = 0x69,
  17. MP4_TYPE_MPEG2_AAC_MAIN_AUDIO = 0x66,
  18. MP4_TYPE_MPEG2_AAC_LC_AUDIO = 0x67,
  19. MP4_TYPE_MPEG2_AAC_SSR_AUDIO = 0x68,
  20. MP4_TYPE_MPEG4_AUDIO = 0x40,
  21. MP4_TYPE_PRIVATE_AUDIO = 0xC0,
  22. MP4_TYPE_PCM16_LITTLE_ENDIAN_AUDIO = 0xE0,
  23. MP4_TYPE_VORBIS_AUDIO = 0xE1,
  24. MP4_TYPE_AC3_AUDIO = 0xE2,
  25. MP4_TYPE_ALAW_AUDIO = 0xE3,
  26. MP4_TYPE_ULAW_AUDIO = 0xE4,
  27. MP4_TYPE_G723_AUDIO = 0xE5,
  28. MP4_TYPE_PCM16_BIG_ENDIAN_AUDIO = 0xE6,
  29. /* MP4 MPEG-4 Audio types from 14496-3 Table 1.5.1 */
  30. MP4_MPEG4_TYPE_AAC_MAIN_AUDIO = 1,
  31. MP4_MPEG4_TYPE_AAC_LC_AUDIO = 2,
  32. MP4_MPEG4_TYPE_AAC_SSR_AUDIO = 3,
  33. MP4_MPEG4_TYPE_AAC_LTP_AUDIO = 4,
  34. MP4_MPEG4_TYPE_AAC_HE_AUDIO = 5,
  35. MP4_MPEG4_TYPE_AAC_SCALABLE_AUDIO = 6,
  36. MP4_MPEG4_TYPE_CELP_AUDIO = 8,
  37. MP4_MPEG4_TYPE_HVXC_AUDIO = 9,
  38. MP4_MPEG4_TYPE_TTSI_AUDIO = 12,
  39. MP4_MPEG4_TYPE_MAIN_SYNTHETIC_AUDIO = 13,
  40. MP4_MPEG4_TYPE_WAVETABLE_AUDIO = 14,
  41. MP4_MPEG4_TYPE_MIDI_AUDIO = 15,
  42. MP4_MPEG4_TYPE_ALGORITHMIC_FX_AUDIO = 16,
  43. MP4_MPEG4_TYPE_PARAMETRIC_STEREO=29,
  44. MP4_MPEG4_ALS_AUDIO=31,
  45. MP4_MPEG4_LAYER1_AUDIO = 32,
  46. MP4_MPEG4_LAYER2_AUDIO= 33,
  47. MP4_MPEG4_LAYER3_AUDIO= 34,
  48. MP4_MPEG4_SLS_AUDIO=37,
  49. };
  50. class MP4AudioDecoder : public Dispatchable
  51. {
  52. protected:
  53. MP4AudioDecoder() {}
  54. ~MP4AudioDecoder() {}
  55. public:
  56. static FOURCC getServiceType() { return WaSvc::MP4AUDIODECODER; }
  57. int Open();
  58. int OpenEx(size_t bits, size_t maxChannels, bool useFloat);
  59. int OpenMP4(MP4FileHandle mp4_file, MP4TrackId mp4_track, size_t output_bits, size_t maxChannels, bool useFloat);
  60. int AudioSpecificConfiguration(void *buffer, size_t buffer_size); // reads ASC block from mp4 file
  61. int GetCurrentBitrate(unsigned int *bitrate);
  62. int OutputFrameSize(size_t *frameSize); // in Frames
  63. int GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample); // can return an error code for "havn't locked to stream yet"
  64. int GetOutputPropertiesEx(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat);
  65. int DecodeSample(void *inputBuffer, size_t inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes);
  66. void Flush();
  67. void Close();
  68. const char *GetCodecInfoString();
  69. int CanHandleCodec(const char *codecName);
  70. int CanHandleType(uint8_t type);
  71. int CanHandleMPEG4Type(uint8_t type);
  72. int SetGain(float gain);
  73. int RequireChunks(); // return 1 if your decoder wants to read whole chunks rather than samples
  74. void EndOfStream();
  75. public:
  76. DISPATCH_CODES
  77. {
  78. MPEG4_AUDIO_OPEN = 10,
  79. MPEG4_AUDIO_OPEN_EX = 11,
  80. MPEG4_AUDIO_OPENMP4 = 12,
  81. MPEG4_AUDIO_ASC = 20,
  82. MPEG4_AUDIO_BITRATE = 30,
  83. MPEG4_AUDIO_FRAMESIZE = 40,
  84. MPEG4_AUDIO_OUTPUTINFO = 50,
  85. MPEG4_AUDIO_OUTPUTINFO_EX = 51,
  86. MPEG4_AUDIO_DECODE = 60,
  87. MPEG4_AUDIO_FLUSH = 70,
  88. MPEG4_AUDIO_CLOSE = 80,
  89. MPEG4_AUDIO_CODEC_INFO_STRING = 90,
  90. MPEG4_AUDIO_HANDLES_CODEC = 100,
  91. MPEG4_AUDIO_HANDLES_TYPE = 110,
  92. MPEG4_AUDIO_HANDLES_MPEG4_TYPE = 120,
  93. MPEG4_AUDIO_SET_GAIN=130,
  94. MPEG4_AUDIO_REQUIRE_CHUNKS = 140,
  95. MPEG4_END_OF_STREAM = 150,
  96. };
  97. };
  98. inline int MP4AudioDecoder::Open()
  99. {
  100. return _call(MPEG4_AUDIO_OPEN, (int)MP4_FAILURE);
  101. }
  102. inline int MP4AudioDecoder::OpenEx(size_t bits, size_t maxChannels, bool useFloat)
  103. {
  104. void *params[3] = { &bits, &maxChannels, &useFloat};
  105. int retval;
  106. if (_dispatch(MPEG4_AUDIO_OPEN_EX, &retval, params, 3))
  107. return retval;
  108. else
  109. return Open();
  110. }
  111. inline int MP4AudioDecoder::OpenMP4(MP4FileHandle mp4_file, MP4TrackId mp4_track, size_t output_bits, size_t maxChannels, bool useFloat)
  112. {
  113. void *params[5] = { &mp4_file, &mp4_track, &output_bits, &maxChannels, &useFloat};
  114. int retval;
  115. if (_dispatch(MPEG4_AUDIO_OPENMP4, &retval, params, 5))
  116. return retval;
  117. else
  118. return OpenEx(output_bits, maxChannels, useFloat);
  119. }
  120. inline int MP4AudioDecoder::AudioSpecificConfiguration(void *buffer, size_t buffer_size)
  121. {
  122. return _call(MPEG4_AUDIO_ASC, (int)MP4_FAILURE, buffer, buffer_size);
  123. }
  124. inline int MP4AudioDecoder::GetCurrentBitrate(unsigned int *bitrate)
  125. {
  126. return _call(MPEG4_AUDIO_BITRATE, (int)MP4_FAILURE, bitrate);
  127. }
  128. inline int MP4AudioDecoder::OutputFrameSize(size_t *frameSize)
  129. {
  130. return _call(MPEG4_AUDIO_FRAMESIZE, (int)MP4_FAILURE, frameSize);
  131. }
  132. inline int MP4AudioDecoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample)
  133. {
  134. return _call(MPEG4_AUDIO_OUTPUTINFO, (int)MP4_FAILURE, sampleRate, channels, bitsPerSample);
  135. }
  136. inline int MP4AudioDecoder::GetOutputPropertiesEx(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *useFloat)
  137. {
  138. void *params[4] = { &sampleRate, &channels, &bitsPerSample, &useFloat};
  139. int retval;
  140. if (_dispatch(MPEG4_AUDIO_OUTPUTINFO_EX, &retval, params, 4))
  141. return retval;
  142. else
  143. {
  144. *useFloat=false;
  145. return GetOutputProperties(sampleRate, channels, bitsPerSample);
  146. }
  147. // return _call(MPEG4_AUDIO_OUTPUTINFO_EX, (int)MP4_FAILURE, sampleRate, channels, bitsPerSample);
  148. }
  149. inline int MP4AudioDecoder::DecodeSample(void *inputBuffer, size_t inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  150. {
  151. return _call(MPEG4_AUDIO_DECODE, (int)MP4_FAILURE, inputBuffer, inputBufferBytes, outputBuffer, outputBufferBytes);
  152. }
  153. inline void MP4AudioDecoder::Flush()
  154. {
  155. _voidcall(MPEG4_AUDIO_FLUSH);
  156. }
  157. inline void MP4AudioDecoder::Close()
  158. {
  159. _voidcall(MPEG4_AUDIO_CLOSE);
  160. }
  161. inline const char *MP4AudioDecoder::GetCodecInfoString()
  162. {
  163. return _call(MPEG4_AUDIO_CODEC_INFO_STRING, (const char *)0);
  164. }
  165. inline int MP4AudioDecoder::CanHandleCodec(const char *codecName)
  166. {
  167. return _call(MPEG4_AUDIO_HANDLES_CODEC, (int)0, codecName);
  168. }
  169. inline int MP4AudioDecoder::CanHandleType(uint8_t type)
  170. {
  171. return _call(MPEG4_AUDIO_HANDLES_TYPE, (int)0, type);
  172. }
  173. inline int MP4AudioDecoder::CanHandleMPEG4Type(uint8_t type)
  174. {
  175. return _call(MPEG4_AUDIO_HANDLES_MPEG4_TYPE, (int)0, type);
  176. }
  177. inline int MP4AudioDecoder::SetGain(float gain)
  178. {
  179. return _call(MPEG4_AUDIO_SET_GAIN, (int)MP4_FAILURE, gain);
  180. }
  181. inline int MP4AudioDecoder::RequireChunks()
  182. {
  183. return _call(MPEG4_AUDIO_REQUIRE_CHUNKS, (int)0);
  184. }
  185. inline void MP4AudioDecoder::EndOfStream()
  186. {
  187. _voidcall(MPEG4_END_OF_STREAM);
  188. }
  189. #endif