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.

626 lines
21KB

  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 "ac3_parser.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. #include "mpegaudiodecheader.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/log.h"
  32. #ifndef __MAC_10_11
  33. #define kAudioFormatEnhancedAC3 'ec-3'
  34. #endif
  35. typedef struct ATDecodeContext {
  36. AVClass *av_class;
  37. AudioConverterRef converter;
  38. AudioStreamPacketDescription pkt_desc;
  39. AVPacket in_pkt;
  40. AVPacket new_in_pkt;
  41. AVBSFContext *bsf;
  42. char *decoded_data;
  43. int channel_map[64];
  44. uint8_t *extradata;
  45. int extradata_size;
  46. int64_t last_pts;
  47. int eof;
  48. } ATDecodeContext;
  49. static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
  50. {
  51. switch (codec) {
  52. case AV_CODEC_ID_AAC:
  53. return kAudioFormatMPEG4AAC;
  54. case AV_CODEC_ID_AC3:
  55. return kAudioFormatAC3;
  56. case AV_CODEC_ID_ADPCM_IMA_QT:
  57. return kAudioFormatAppleIMA4;
  58. case AV_CODEC_ID_ALAC:
  59. return kAudioFormatAppleLossless;
  60. case AV_CODEC_ID_AMR_NB:
  61. return kAudioFormatAMR;
  62. case AV_CODEC_ID_EAC3:
  63. return kAudioFormatEnhancedAC3;
  64. case AV_CODEC_ID_GSM_MS:
  65. return kAudioFormatMicrosoftGSM;
  66. case AV_CODEC_ID_ILBC:
  67. return kAudioFormatiLBC;
  68. case AV_CODEC_ID_MP1:
  69. return kAudioFormatMPEGLayer1;
  70. case AV_CODEC_ID_MP2:
  71. return kAudioFormatMPEGLayer2;
  72. case AV_CODEC_ID_MP3:
  73. return kAudioFormatMPEGLayer3;
  74. case AV_CODEC_ID_PCM_ALAW:
  75. return kAudioFormatALaw;
  76. case AV_CODEC_ID_PCM_MULAW:
  77. return kAudioFormatULaw;
  78. case AV_CODEC_ID_QDMC:
  79. return kAudioFormatQDesign;
  80. case AV_CODEC_ID_QDM2:
  81. return kAudioFormatQDesign2;
  82. default:
  83. av_assert0(!"Invalid codec ID!");
  84. return 0;
  85. }
  86. }
  87. static int ffat_get_channel_id(AudioChannelLabel label)
  88. {
  89. if (label == 0)
  90. return -1;
  91. else if (label <= kAudioChannelLabel_LFEScreen)
  92. return label - 1;
  93. else if (label <= kAudioChannelLabel_RightSurround)
  94. return label + 4;
  95. else if (label <= kAudioChannelLabel_CenterSurround)
  96. return label + 1;
  97. else if (label <= kAudioChannelLabel_RightSurroundDirect)
  98. return label + 23;
  99. else if (label <= kAudioChannelLabel_TopBackRight)
  100. return label - 1;
  101. else if (label < kAudioChannelLabel_RearSurroundLeft)
  102. return -1;
  103. else if (label <= kAudioChannelLabel_RearSurroundRight)
  104. return label - 29;
  105. else if (label <= kAudioChannelLabel_RightWide)
  106. return label - 4;
  107. else if (label == kAudioChannelLabel_LFE2)
  108. return ff_ctzll(AV_CH_LOW_FREQUENCY_2);
  109. else if (label == kAudioChannelLabel_Mono)
  110. return ff_ctzll(AV_CH_FRONT_CENTER);
  111. else
  112. return -1;
  113. }
  114. static int ffat_compare_channel_descriptions(const void* a, const void* b)
  115. {
  116. const AudioChannelDescription* da = a;
  117. const AudioChannelDescription* db = b;
  118. return ffat_get_channel_id(da->mChannelLabel) - ffat_get_channel_id(db->mChannelLabel);
  119. }
  120. static AudioChannelLayout *ffat_convert_layout(AudioChannelLayout *layout, UInt32* size)
  121. {
  122. AudioChannelLayoutTag tag = layout->mChannelLayoutTag;
  123. AudioChannelLayout *new_layout;
  124. if (tag == kAudioChannelLayoutTag_UseChannelDescriptions)
  125. return layout;
  126. else if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
  127. AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForBitmap,
  128. sizeof(UInt32), &layout->mChannelBitmap, size);
  129. else
  130. AudioFormatGetPropertyInfo(kAudioFormatProperty_ChannelLayoutForTag,
  131. sizeof(AudioChannelLayoutTag), &tag, size);
  132. new_layout = av_malloc(*size);
  133. if (!new_layout) {
  134. av_free(layout);
  135. return NULL;
  136. }
  137. if (tag == kAudioChannelLayoutTag_UseChannelBitmap)
  138. AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForBitmap,
  139. sizeof(UInt32), &layout->mChannelBitmap, size, new_layout);
  140. else
  141. AudioFormatGetProperty(kAudioFormatProperty_ChannelLayoutForTag,
  142. sizeof(AudioChannelLayoutTag), &tag, size, new_layout);
  143. new_layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
  144. av_free(layout);
  145. return new_layout;
  146. }
  147. static int ffat_update_ctx(AVCodecContext *avctx)
  148. {
  149. ATDecodeContext *at = avctx->priv_data;
  150. AudioStreamBasicDescription format;
  151. UInt32 size = sizeof(format);
  152. if (!AudioConverterGetProperty(at->converter,
  153. kAudioConverterCurrentInputStreamDescription,
  154. &size, &format)) {
  155. if (format.mSampleRate)
  156. avctx->sample_rate = format.mSampleRate;
  157. avctx->channels = format.mChannelsPerFrame;
  158. avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
  159. avctx->frame_size = format.mFramesPerPacket;
  160. }
  161. if (!AudioConverterGetProperty(at->converter,
  162. kAudioConverterCurrentOutputStreamDescription,
  163. &size, &format)) {
  164. format.mSampleRate = avctx->sample_rate;
  165. format.mChannelsPerFrame = avctx->channels;
  166. AudioConverterSetProperty(at->converter,
  167. kAudioConverterCurrentOutputStreamDescription,
  168. size, &format);
  169. }
  170. if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterOutputChannelLayout,
  171. &size, NULL) && size) {
  172. AudioChannelLayout *layout = av_malloc(size);
  173. uint64_t layout_mask = 0;
  174. int i;
  175. if (!layout)
  176. return AVERROR(ENOMEM);
  177. AudioConverterGetProperty(at->converter, kAudioConverterOutputChannelLayout,
  178. &size, layout);
  179. if (!(layout = ffat_convert_layout(layout, &size)))
  180. return AVERROR(ENOMEM);
  181. for (i = 0; i < layout->mNumberChannelDescriptions; i++) {
  182. int id = ffat_get_channel_id(layout->mChannelDescriptions[i].mChannelLabel);
  183. if (id < 0)
  184. goto done;
  185. if (layout_mask & (1 << id))
  186. goto done;
  187. layout_mask |= 1 << id;
  188. layout->mChannelDescriptions[i].mChannelFlags = i; // Abusing flags as index
  189. }
  190. avctx->channel_layout = layout_mask;
  191. qsort(layout->mChannelDescriptions, layout->mNumberChannelDescriptions,
  192. sizeof(AudioChannelDescription), &ffat_compare_channel_descriptions);
  193. for (i = 0; i < layout->mNumberChannelDescriptions; i++)
  194. at->channel_map[i] = layout->mChannelDescriptions[i].mChannelFlags;
  195. done:
  196. av_free(layout);
  197. }
  198. if (!avctx->frame_size)
  199. avctx->frame_size = 2048;
  200. return 0;
  201. }
  202. static void put_descr(PutByteContext *pb, int tag, unsigned int size)
  203. {
  204. int i = 3;
  205. bytestream2_put_byte(pb, tag);
  206. for (; i > 0; i--)
  207. bytestream2_put_byte(pb, (size >> (7 * i)) | 0x80);
  208. bytestream2_put_byte(pb, size & 0x7F);
  209. }
  210. static uint8_t* ffat_get_magic_cookie(AVCodecContext *avctx, UInt32 *cookie_size)
  211. {
  212. ATDecodeContext *at = avctx->priv_data;
  213. if (avctx->codec_id == AV_CODEC_ID_AAC) {
  214. char *extradata;
  215. PutByteContext pb;
  216. *cookie_size = 5 + 3 + 5+13 + 5+at->extradata_size;
  217. if (!(extradata = av_malloc(*cookie_size)))
  218. return NULL;
  219. bytestream2_init_writer(&pb, extradata, *cookie_size);
  220. // ES descriptor
  221. put_descr(&pb, 0x03, 3 + 5+13 + 5+at->extradata_size);
  222. bytestream2_put_be16(&pb, 0);
  223. bytestream2_put_byte(&pb, 0x00); // flags (= no flags)
  224. // DecoderConfig descriptor
  225. put_descr(&pb, 0x04, 13 + 5+at->extradata_size);
  226. // Object type indication
  227. bytestream2_put_byte(&pb, 0x40);
  228. bytestream2_put_byte(&pb, 0x15); // flags (= Audiostream)
  229. bytestream2_put_be24(&pb, 0); // Buffersize DB
  230. bytestream2_put_be32(&pb, 0); // maxbitrate
  231. bytestream2_put_be32(&pb, 0); // avgbitrate
  232. // DecoderSpecific info descriptor
  233. put_descr(&pb, 0x05, at->extradata_size);
  234. bytestream2_put_buffer(&pb, at->extradata, at->extradata_size);
  235. return extradata;
  236. } else {
  237. *cookie_size = at->extradata_size;
  238. return at->extradata;
  239. }
  240. }
  241. static av_cold int ffat_usable_extradata(AVCodecContext *avctx)
  242. {
  243. ATDecodeContext *at = avctx->priv_data;
  244. return at->extradata_size &&
  245. (avctx->codec_id == AV_CODEC_ID_ALAC ||
  246. avctx->codec_id == AV_CODEC_ID_QDM2 ||
  247. avctx->codec_id == AV_CODEC_ID_QDMC ||
  248. avctx->codec_id == AV_CODEC_ID_AAC);
  249. }
  250. static int ffat_set_extradata(AVCodecContext *avctx)
  251. {
  252. ATDecodeContext *at = avctx->priv_data;
  253. if (ffat_usable_extradata(avctx)) {
  254. OSStatus status;
  255. UInt32 cookie_size;
  256. uint8_t *cookie = ffat_get_magic_cookie(avctx, &cookie_size);
  257. if (!cookie)
  258. return AVERROR(ENOMEM);
  259. status = AudioConverterSetProperty(at->converter,
  260. kAudioConverterDecompressionMagicCookie,
  261. cookie_size, cookie);
  262. if (status != 0)
  263. av_log(avctx, AV_LOG_WARNING, "AudioToolbox cookie error: %i\n", (int)status);
  264. if (cookie != at->extradata)
  265. av_free(cookie);
  266. }
  267. return 0;
  268. }
  269. static av_cold int ffat_create_decoder(AVCodecContext *avctx, AVPacket *pkt)
  270. {
  271. ATDecodeContext *at = avctx->priv_data;
  272. OSStatus status;
  273. int i;
  274. enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
  275. AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
  276. AudioStreamBasicDescription in_format = {
  277. .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
  278. .mBytesPerPacket = (avctx->codec_id == AV_CODEC_ID_ILBC) ? avctx->block_align : 0,
  279. };
  280. AudioStreamBasicDescription out_format = {
  281. .mFormatID = kAudioFormatLinearPCM,
  282. .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
  283. .mFramesPerPacket = 1,
  284. .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
  285. };
  286. avctx->sample_fmt = sample_fmt;
  287. if (ffat_usable_extradata(avctx)) {
  288. UInt32 format_size = sizeof(in_format);
  289. UInt32 cookie_size;
  290. uint8_t *cookie = ffat_get_magic_cookie(avctx, &cookie_size);
  291. if (!cookie)
  292. return AVERROR(ENOMEM);
  293. status = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
  294. cookie_size, cookie, &format_size, &in_format);
  295. if (cookie != at->extradata)
  296. av_free(cookie);
  297. if (status != 0) {
  298. av_log(avctx, AV_LOG_ERROR, "AudioToolbox header-parse error: %i\n", (int)status);
  299. return AVERROR_UNKNOWN;
  300. }
  301. #if CONFIG_MP1_AT_DECODER || CONFIG_MP2_AT_DECODER || CONFIG_MP3_AT_DECODER
  302. } else if (pkt && pkt->size >= 4 &&
  303. (avctx->codec_id == AV_CODEC_ID_MP1 ||
  304. avctx->codec_id == AV_CODEC_ID_MP2 ||
  305. avctx->codec_id == AV_CODEC_ID_MP3)) {
  306. enum AVCodecID codec_id;
  307. int bit_rate;
  308. if (ff_mpa_decode_header(AV_RB32(pkt->data), &avctx->sample_rate,
  309. &in_format.mChannelsPerFrame, &avctx->frame_size,
  310. &bit_rate, &codec_id) < 0)
  311. return AVERROR_INVALIDDATA;
  312. avctx->bit_rate = bit_rate;
  313. in_format.mSampleRate = avctx->sample_rate;
  314. #endif
  315. #if CONFIG_AC3_AT_DECODER || CONFIG_EAC3_AT_DECODER
  316. } else if (pkt && pkt->size >= 7 &&
  317. (avctx->codec_id == AV_CODEC_ID_AC3 ||
  318. avctx->codec_id == AV_CODEC_ID_EAC3)) {
  319. AC3HeaderInfo hdr, *phdr = &hdr;
  320. GetBitContext gbc;
  321. init_get_bits(&gbc, pkt->data, pkt->size);
  322. if (avpriv_ac3_parse_header(&gbc, &phdr) < 0)
  323. return AVERROR_INVALIDDATA;
  324. in_format.mSampleRate = hdr.sample_rate;
  325. in_format.mChannelsPerFrame = hdr.channels;
  326. avctx->frame_size = hdr.num_blocks * 256;
  327. avctx->bit_rate = hdr.bit_rate;
  328. #endif
  329. } else {
  330. in_format.mSampleRate = avctx->sample_rate ? avctx->sample_rate : 44100;
  331. in_format.mChannelsPerFrame = avctx->channels ? avctx->channels : 1;
  332. }
  333. avctx->sample_rate = out_format.mSampleRate = in_format.mSampleRate;
  334. avctx->channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame;
  335. if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
  336. in_format.mFramesPerPacket = 64;
  337. status = AudioConverterNew(&in_format, &out_format, &at->converter);
  338. if (status != 0) {
  339. av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
  340. return AVERROR_UNKNOWN;
  341. }
  342. if ((status = ffat_set_extradata(avctx)) < 0)
  343. return status;
  344. for (i = 0; i < (sizeof(at->channel_map) / sizeof(at->channel_map[0])); i++)
  345. at->channel_map[i] = i;
  346. ffat_update_ctx(avctx);
  347. if(!(at->decoded_data = av_malloc(av_get_bytes_per_sample(avctx->sample_fmt)
  348. * avctx->frame_size * avctx->channels)))
  349. return AVERROR(ENOMEM);
  350. at->last_pts = AV_NOPTS_VALUE;
  351. return 0;
  352. }
  353. static av_cold int ffat_init_decoder(AVCodecContext *avctx)
  354. {
  355. ATDecodeContext *at = avctx->priv_data;
  356. at->extradata = avctx->extradata;
  357. at->extradata_size = avctx->extradata_size;
  358. if ((avctx->channels && avctx->sample_rate) || ffat_usable_extradata(avctx))
  359. return ffat_create_decoder(avctx, NULL);
  360. else
  361. return 0;
  362. }
  363. static OSStatus ffat_decode_callback(AudioConverterRef converter, UInt32 *nb_packets,
  364. AudioBufferList *data,
  365. AudioStreamPacketDescription **packets,
  366. void *inctx)
  367. {
  368. AVCodecContext *avctx = inctx;
  369. ATDecodeContext *at = avctx->priv_data;
  370. if (at->eof) {
  371. *nb_packets = 0;
  372. if (packets) {
  373. *packets = &at->pkt_desc;
  374. at->pkt_desc.mDataByteSize = 0;
  375. }
  376. return 0;
  377. }
  378. av_packet_unref(&at->in_pkt);
  379. av_packet_move_ref(&at->in_pkt, &at->new_in_pkt);
  380. if (!at->in_pkt.data) {
  381. *nb_packets = 0;
  382. return 1;
  383. }
  384. data->mNumberBuffers = 1;
  385. data->mBuffers[0].mNumberChannels = 0;
  386. data->mBuffers[0].mDataByteSize = at->in_pkt.size;
  387. data->mBuffers[0].mData = at->in_pkt.data;
  388. *nb_packets = 1;
  389. if (packets) {
  390. *packets = &at->pkt_desc;
  391. at->pkt_desc.mDataByteSize = at->in_pkt.size;
  392. }
  393. return 0;
  394. }
  395. #define COPY_SAMPLES(type) \
  396. type *in_ptr = (type*)at->decoded_data; \
  397. type *end_ptr = in_ptr + frame->nb_samples * avctx->channels; \
  398. type *out_ptr = (type*)frame->data[0]; \
  399. for (; in_ptr < end_ptr; in_ptr += avctx->channels, out_ptr += avctx->channels) { \
  400. int c; \
  401. for (c = 0; c < avctx->channels; c++) \
  402. out_ptr[c] = in_ptr[at->channel_map[c]]; \
  403. }
  404. static void ffat_copy_samples(AVCodecContext *avctx, AVFrame *frame)
  405. {
  406. ATDecodeContext *at = avctx->priv_data;
  407. if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
  408. COPY_SAMPLES(int32_t);
  409. } else {
  410. COPY_SAMPLES(int16_t);
  411. }
  412. }
  413. static int ffat_decode(AVCodecContext *avctx, void *data,
  414. int *got_frame_ptr, AVPacket *avpkt)
  415. {
  416. ATDecodeContext *at = avctx->priv_data;
  417. AVFrame *frame = data;
  418. int pkt_size = avpkt->size;
  419. AVPacket filtered_packet = {0};
  420. OSStatus ret;
  421. AudioBufferList out_buffers;
  422. if (avctx->codec_id == AV_CODEC_ID_AAC && avpkt->size > 2 &&
  423. (AV_RB16(avpkt->data) & 0xfff0) == 0xfff0) {
  424. AVPacket filter_pkt = {0};
  425. if (!at->bsf) {
  426. const AVBitStreamFilter *bsf = av_bsf_get_by_name("aac_adtstoasc");
  427. if(!bsf)
  428. return AVERROR_BSF_NOT_FOUND;
  429. if ((ret = av_bsf_alloc(bsf, &at->bsf)))
  430. return ret;
  431. if (((ret = avcodec_parameters_from_context(at->bsf->par_in, avctx)) < 0) ||
  432. ((ret = av_bsf_init(at->bsf)) < 0)) {
  433. av_bsf_free(&at->bsf);
  434. return ret;
  435. }
  436. }
  437. if ((ret = av_packet_ref(&filter_pkt, avpkt)) < 0)
  438. return ret;
  439. if ((ret = av_bsf_send_packet(at->bsf, &filter_pkt)) < 0) {
  440. av_packet_unref(&filter_pkt);
  441. return ret;
  442. }
  443. if ((ret = av_bsf_receive_packet(at->bsf, &filtered_packet)) < 0)
  444. return ret;
  445. at->extradata = at->bsf->par_out->extradata;
  446. at->extradata_size = at->bsf->par_out->extradata_size;
  447. avpkt = &filtered_packet;
  448. }
  449. if (!at->converter) {
  450. if ((ret = ffat_create_decoder(avctx, avpkt)) < 0) {
  451. av_packet_unref(&filtered_packet);
  452. return ret;
  453. }
  454. }
  455. out_buffers = (AudioBufferList){
  456. .mNumberBuffers = 1,
  457. .mBuffers = {
  458. {
  459. .mNumberChannels = avctx->channels,
  460. .mDataByteSize = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->frame_size
  461. * avctx->channels,
  462. }
  463. }
  464. };
  465. av_packet_unref(&at->new_in_pkt);
  466. if (avpkt->size) {
  467. if (filtered_packet.data) {
  468. at->new_in_pkt = filtered_packet;
  469. } else if ((ret = av_packet_ref(&at->new_in_pkt, avpkt)) < 0) {
  470. return ret;
  471. }
  472. } else {
  473. at->eof = 1;
  474. }
  475. frame->sample_rate = avctx->sample_rate;
  476. frame->nb_samples = avctx->frame_size;
  477. out_buffers.mBuffers[0].mData = at->decoded_data;
  478. ret = AudioConverterFillComplexBuffer(at->converter, ffat_decode_callback, avctx,
  479. &frame->nb_samples, &out_buffers, NULL);
  480. if ((!ret || ret == 1) && frame->nb_samples) {
  481. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  482. return ret;
  483. ffat_copy_samples(avctx, frame);
  484. *got_frame_ptr = 1;
  485. if (at->last_pts != AV_NOPTS_VALUE) {
  486. frame->pkt_pts = at->last_pts;
  487. at->last_pts = avpkt->pts;
  488. }
  489. } else if (ret && ret != 1) {
  490. av_log(avctx, AV_LOG_WARNING, "Decode error: %i\n", ret);
  491. } else {
  492. at->last_pts = avpkt->pts;
  493. }
  494. return pkt_size;
  495. }
  496. static av_cold void ffat_decode_flush(AVCodecContext *avctx)
  497. {
  498. ATDecodeContext *at = avctx->priv_data;
  499. AudioConverterReset(at->converter);
  500. av_packet_unref(&at->new_in_pkt);
  501. av_packet_unref(&at->in_pkt);
  502. }
  503. static av_cold int ffat_close_decoder(AVCodecContext *avctx)
  504. {
  505. ATDecodeContext *at = avctx->priv_data;
  506. AudioConverterDispose(at->converter);
  507. av_bsf_free(&at->bsf);
  508. av_packet_unref(&at->new_in_pkt);
  509. av_packet_unref(&at->in_pkt);
  510. av_free(at->decoded_data);
  511. return 0;
  512. }
  513. #define FFAT_DEC_CLASS(NAME) \
  514. static const AVClass ffat_##NAME##_dec_class = { \
  515. .class_name = "at_" #NAME "_dec", \
  516. .version = LIBAVUTIL_VERSION_INT, \
  517. };
  518. #define FFAT_DEC(NAME, ID) \
  519. FFAT_DEC_CLASS(NAME) \
  520. AVCodec ff_##NAME##_at_decoder = { \
  521. .name = #NAME "_at", \
  522. .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
  523. .type = AVMEDIA_TYPE_AUDIO, \
  524. .id = ID, \
  525. .priv_data_size = sizeof(ATDecodeContext), \
  526. .init = ffat_init_decoder, \
  527. .close = ffat_close_decoder, \
  528. .decode = ffat_decode, \
  529. .flush = ffat_decode_flush, \
  530. .priv_class = &ffat_##NAME##_dec_class, \
  531. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, \
  532. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
  533. };
  534. FFAT_DEC(aac, AV_CODEC_ID_AAC)
  535. FFAT_DEC(ac3, AV_CODEC_ID_AC3)
  536. FFAT_DEC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT)
  537. FFAT_DEC(alac, AV_CODEC_ID_ALAC)
  538. FFAT_DEC(amr_nb, AV_CODEC_ID_AMR_NB)
  539. FFAT_DEC(eac3, AV_CODEC_ID_EAC3)
  540. FFAT_DEC(gsm_ms, AV_CODEC_ID_GSM_MS)
  541. FFAT_DEC(ilbc, AV_CODEC_ID_ILBC)
  542. FFAT_DEC(mp1, AV_CODEC_ID_MP1)
  543. FFAT_DEC(mp2, AV_CODEC_ID_MP2)
  544. FFAT_DEC(mp3, AV_CODEC_ID_MP3)
  545. FFAT_DEC(pcm_alaw, AV_CODEC_ID_PCM_ALAW)
  546. FFAT_DEC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW)
  547. FFAT_DEC(qdmc, AV_CODEC_ID_QDMC)
  548. FFAT_DEC(qdm2, AV_CODEC_ID_QDM2)