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.

368 lines
12KB

  1. /*
  2. * Audio Toolbox system codecs
  3. *
  4. * copyright (c) 2016 Rodger Combs
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <AudioToolbox/AudioToolbox.h>
  23. #include "config.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/log.h"
  30. typedef struct ATDecodeContext {
  31. AVClass *av_class;
  32. AudioConverterRef converter;
  33. AudioStreamPacketDescription pkt_desc;
  34. AVPacket in_pkt;
  35. AVPacket new_in_pkt;
  36. AVBitStreamFilterContext *bsf;
  37. unsigned pkt_size;
  38. int64_t last_pts;
  39. int eof;
  40. } ATDecodeContext;
  41. static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
  42. {
  43. switch (codec) {
  44. case AV_CODEC_ID_AAC:
  45. return kAudioFormatMPEG4AAC;
  46. case AV_CODEC_ID_AC3:
  47. return kAudioFormatAC3;
  48. case AV_CODEC_ID_ADPCM_IMA_QT:
  49. return kAudioFormatAppleIMA4;
  50. case AV_CODEC_ID_ALAC:
  51. return kAudioFormatAppleLossless;
  52. case AV_CODEC_ID_AMR_NB:
  53. return kAudioFormatAMR;
  54. case AV_CODEC_ID_GSM_MS:
  55. return kAudioFormatMicrosoftGSM;
  56. case AV_CODEC_ID_ILBC:
  57. return kAudioFormatiLBC;
  58. case AV_CODEC_ID_MP1:
  59. return kAudioFormatMPEGLayer1;
  60. case AV_CODEC_ID_MP2:
  61. return kAudioFormatMPEGLayer2;
  62. case AV_CODEC_ID_MP3:
  63. return kAudioFormatMPEGLayer3;
  64. case AV_CODEC_ID_PCM_ALAW:
  65. return kAudioFormatALaw;
  66. case AV_CODEC_ID_PCM_MULAW:
  67. return kAudioFormatULaw;
  68. case AV_CODEC_ID_QDMC:
  69. return kAudioFormatQDesign;
  70. case AV_CODEC_ID_QDM2:
  71. return kAudioFormatQDesign2;
  72. default:
  73. av_assert0(!"Invalid codec ID!");
  74. return 0;
  75. }
  76. }
  77. static void ffat_update_ctx(AVCodecContext *avctx)
  78. {
  79. ATDecodeContext *at = avctx->priv_data;
  80. AudioStreamBasicDescription in_format;
  81. UInt32 size = sizeof(in_format);
  82. if (!AudioConverterGetProperty(at->converter,
  83. kAudioConverterCurrentInputStreamDescription,
  84. &size, &in_format)) {
  85. avctx->channels = in_format.mChannelsPerFrame;
  86. at->pkt_size = in_format.mFramesPerPacket;
  87. }
  88. if (!at->pkt_size)
  89. at->pkt_size = 2048;
  90. }
  91. static void put_descr(PutByteContext *pb, int tag, unsigned int size)
  92. {
  93. int i = 3;
  94. bytestream2_put_byte(pb, tag);
  95. for (; i > 0; i--)
  96. bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
  97. bytestream2_put_byte(pb, size & 0x7F);
  98. }
  99. static av_cold int ffat_init_decoder(AVCodecContext *avctx)
  100. {
  101. ATDecodeContext *at = avctx->priv_data;
  102. OSStatus status;
  103. enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
  104. AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
  105. AudioStreamBasicDescription in_format = {
  106. .mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100,
  107. .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
  108. .mBytesPerPacket = avctx->block_align,
  109. .mChannelsPerFrame = avctx->channels ? avctx->channels : 1,
  110. };
  111. AudioStreamBasicDescription out_format = {
  112. .mSampleRate = in_format.mSampleRate,
  113. .mFormatID = kAudioFormatLinearPCM,
  114. .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
  115. .mFramesPerPacket = 1,
  116. .mChannelsPerFrame = in_format.mChannelsPerFrame,
  117. .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
  118. };
  119. avctx->sample_fmt = sample_fmt;
  120. if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
  121. in_format.mFramesPerPacket = 64;
  122. status = AudioConverterNew(&in_format, &out_format, &at->converter);
  123. if (status != 0) {
  124. av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
  125. return AVERROR_UNKNOWN;
  126. }
  127. if (avctx->extradata_size) {
  128. char *extradata = avctx->extradata;
  129. int extradata_size = avctx->extradata_size;
  130. if (avctx->codec_id == AV_CODEC_ID_AAC) {
  131. PutByteContext pb;
  132. extradata_size = 5 + 3 + 5+13 + 5+avctx->extradata_size;
  133. if (!(extradata = av_malloc(extradata_size)))
  134. return AVERROR(ENOMEM);
  135. bytestream2_init_writer(&pb, extradata, extradata_size);
  136. // ES descriptor
  137. put_descr(&pb, 0x03, 3 + 5+13 + 5+avctx->extradata_size);
  138. bytestream2_put_be16(&pb, 0);
  139. bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
  140. // DecoderConfig descriptor
  141. put_descr(&pb, 0x04, 13 + 5+avctx->extradata_size);
  142. // Object type indication
  143. bytestream2_put_byte(&pb, 0x40);
  144. bytestream2_put_byte(&pb, 0x15); // flags (= Audiostream)
  145. bytestream2_put_be24(&pb, 0); // Buffersize DB
  146. bytestream2_put_be32(&pb, 0); // maxbitrate
  147. bytestream2_put_be32(&pb, 0); // avgbitrate
  148. // DecoderSpecific info descriptor
  149. put_descr(&pb, 0x05, avctx->extradata_size);
  150. bytestream2_put_buffer(&pb, avctx->extradata, avctx->extradata_size);
  151. }
  152. status = AudioConverterSetProperty(at->converter,
  153. kAudioConverterDecompressionMagicCookie,
  154. extradata_size, extradata);
  155. if (status != 0)
  156. av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
  157. }
  158. ffat_update_ctx(avctx);
  159. at->last_pts = AV_NOPTS_VALUE;
  160. return 0;
  161. }
  162. static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
  163. AudioBufferList *data,
  164. AudioStreamPacketDescription **packets,
  165. void *inctx)
  166. {
  167. AVCodecContext *avctx = inctx;
  168. ATDecodeContext *at = avctx->priv_data;
  169. if (at->eof) {
  170. *nb_packets = 0;
  171. if (packets) {
  172. *packets = &at->pkt_desc;
  173. at->pkt_desc.mDataByteSize = 0;
  174. }
  175. return 0;
  176. }
  177. av_packet_move_ref(&at->in_pkt, &at->new_in_pkt);
  178. at->new_in_pkt.data = 0;
  179. at->new_in_pkt.size = 0;
  180. if (!at->in_pkt.data) {
  181. *nb_packets = 0;
  182. return 1;
  183. }
  184. data->mNumberBuffers = 1;
  185. data->mBuffers[0].mNumberChannels = 0;
  186. data->mBuffers[0].mDataByteSize = at->in_pkt.size;
  187. data->mBuffers[0].mData = at->in_pkt.data;
  188. *nb_packets = 1;
  189. if (packets) {
  190. *packets = &at->pkt_desc;
  191. at->pkt_desc.mDataByteSize = at->in_pkt.size;
  192. }
  193. return 0;
  194. }
  195. static int ffat_decode(AVCodecContext *avctx, void *data,
  196. int *got_frame_ptr, AVPacket *avpkt)
  197. {
  198. ATDecodeContext *at = avctx->priv_data;
  199. AVFrame *frame = data;
  200. int pkt_size = avpkt->size;
  201. AVPacket filtered_packet;
  202. OSStatus ret;
  203. AudioBufferList out_buffers = {
  204. .mNumberBuffers = 1,
  205. .mBuffers = {
  206. {
  207. .mNumberChannels = avctx->channels,
  208. .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * at->pkt_size * avctx->channels,
  209. }
  210. }
  211. };
  212. if (avctx->codec_id == AV_CODEC_ID_AAC && avpkt->size > 2 &&
  213. (AV_RB16(avpkt->data) & 0xfff0) == 0xfff0) {
  214. int first = 0;
  215. uint8_t *p_filtered = NULL;
  216. int n_filtered = 0;
  217. if (!at->bsf) {
  218. first = 1;
  219. if(!(at->bsf = av_bitstream_filter_init("aac_adtstoasc")))
  220. return AVERROR(ENOMEM);
  221. }
  222. ret = av_bitstream_filter_filter(at->bsf, avctx, NULL, &p_filtered, &n_filtered,
  223. avpkt->data, avpkt->size, 0);
  224. if (ret >= 0 && p_filtered != avpkt->data) {
  225. filtered_packet = *avpkt;
  226. avpkt = &filtered_packet;
  227. avpkt->data = p_filtered;
  228. avpkt->size = n_filtered;
  229. }
  230. if (first) {
  231. if ((ret = ffat_set_extradata(avctx)) < 0)
  232. return ret;
  233. ffat_update_ctx(avctx);
  234. out_buffers.mBuffers[0].mNumberChannels = avctx->channels;
  235. out_buffers.mBuffers[0].mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * at->pkt_size * avctx->channels;
  236. }
  237. }
  238. av_packet_unref(&at->new_in_pkt);
  239. if (avpkt->size) {
  240. if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0)
  241. return ret;
  242. at->new_in_pkt.data = avpkt->data;
  243. } else {
  244. at->eof = 1;
  245. }
  246. frame->sample_rate = avctx->sample_rate;
  247. frame->nb_samples = at->pkt_size;
  248. ff_get_buffer(avctx, frame, 0);
  249. out_buffers.mBuffers[0].mData = frame->data[0];
  250. ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
  251. &frame->nb_samples, &out_buffers, NULL);
  252. if ((!ret || ret == 1) && frame->nb_samples) {
  253. *got_frame_ptr = 1;
  254. if (at->last_pts != AV_NOPTS_VALUE) {
  255. frame->pts = at->last_pts;
  256. at->last_pts = avpkt->pts;
  257. }
  258. } else if (ret && ret != 1) {
  259. av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
  260. } else {
  261. at->last_pts = avpkt->pts;
  262. }
  263. return pkt_size;
  264. }
  265. static av_cold void ffat_decode_flush(AVCodecContext *avctx)
  266. {
  267. ATDecodeContext *at = avctx->priv_data;
  268. AudioConverterReset(at->converter);
  269. av_packet_unref(&at->new_in_pkt);
  270. av_packet_unref(&at->in_pkt);
  271. }
  272. static av_cold int ffat_close_decoder(AVCodecContext *avctx)
  273. {
  274. ATDecodeContext *at = avctx->priv_data;
  275. AudioConverterDispose(at->converter);
  276. av_packet_unref(&at->new_in_pkt);
  277. av_packet_unref(&at->in_pkt);
  278. return 0;
  279. }
  280. #define FFAT_DEC_CLASS(NAME) \
  281. static const AVClass ffat_##NAME##_dec_class = { \
  282. .class_name = "at_" #NAME "_dec", \
  283. .version = LIBAVUTIL_VERSION_INT, \
  284. };
  285. #define FFAT_DEC(NAME, ID) \
  286. FFAT_DEC_CLASS(NAME) \
  287. AVCodec ff_##NAME##_at_decoder = { \
  288. .name = #NAME "_at", \
  289. .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
  290. .type = AVMEDIA_TYPE_AUDIO, \
  291. .id = ID, \
  292. .priv_data_size = sizeof(ATDecodeContext), \
  293. .init = ffat_init_decoder, \
  294. .close = ffat_close_decoder, \
  295. .decode = ffat_decode, \
  296. .flush = ffat_decode_flush, \
  297. .priv_class = &ffat_##NAME##_dec_class, \
  298. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
  299. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
  300. };
  301. FFAT_DEC(aac, AV_CODEC_ID_AAC)
  302. FFAT_DEC(ac3, AV_CODEC_ID_AC3)
  303. FFAT_DEC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT)
  304. FFAT_DEC(alac, AV_CODEC_ID_ALAC)
  305. FFAT_DEC(amr_nb, AV_CODEC_ID_AMR_NB)
  306. FFAT_DEC(gsm_ms, AV_CODEC_ID_GSM_MS)
  307. FFAT_DEC(ilbc, AV_CODEC_ID_ILBC)
  308. FFAT_DEC(mp1, AV_CODEC_ID_MP1)
  309. FFAT_DEC(mp2, AV_CODEC_ID_MP2)
  310. FFAT_DEC(mp3, AV_CODEC_ID_MP3)
  311. FFAT_DEC(pcm_alaw, AV_CODEC_ID_PCM_ALAW)
  312. FFAT_DEC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW)
  313. FFAT_DEC(qdmc, AV_CODEC_ID_QDMC)
  314. FFAT_DEC(qdm2, AV_CODEC_ID_QDM2)