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.

663 lines
23KB

  1. /*
  2. * Audio Toolbox system codecs
  3. *
  4. * copyright (c) 2016 rcombs
  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. #define FF_BUFQUEUE_SIZE 256
  24. #include "libavfilter/bufferqueue.h"
  25. #include "config.h"
  26. #include "audio_frame_queue.h"
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "internal.h"
  30. #include "libavformat/isom.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/log.h"
  34. typedef struct ATDecodeContext {
  35. AVClass *av_class;
  36. int mode;
  37. int quality;
  38. AudioConverterRef converter;
  39. struct FFBufQueue frame_queue;
  40. struct FFBufQueue used_frame_queue;
  41. unsigned pkt_size;
  42. AudioFrameQueue afq;
  43. int eof;
  44. int frame_size;
  45. AVFrame* encoding_frame;
  46. } ATDecodeContext;
  47. static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile)
  48. {
  49. switch (codec) {
  50. case AV_CODEC_ID_AAC:
  51. switch (profile) {
  52. case FF_PROFILE_AAC_LOW:
  53. default:
  54. return kAudioFormatMPEG4AAC;
  55. case FF_PROFILE_AAC_HE:
  56. return kAudioFormatMPEG4AAC_HE;
  57. case FF_PROFILE_AAC_HE_V2:
  58. return kAudioFormatMPEG4AAC_HE_V2;
  59. case FF_PROFILE_AAC_LD:
  60. return kAudioFormatMPEG4AAC_LD;
  61. case FF_PROFILE_AAC_ELD:
  62. return kAudioFormatMPEG4AAC_ELD;
  63. }
  64. case AV_CODEC_ID_ADPCM_IMA_QT:
  65. return kAudioFormatAppleIMA4;
  66. case AV_CODEC_ID_ALAC:
  67. return kAudioFormatAppleLossless;
  68. case AV_CODEC_ID_ILBC:
  69. return kAudioFormatiLBC;
  70. case AV_CODEC_ID_PCM_ALAW:
  71. return kAudioFormatALaw;
  72. case AV_CODEC_ID_PCM_MULAW:
  73. return kAudioFormatULaw;
  74. default:
  75. av_assert0(!"Invalid codec ID!");
  76. return 0;
  77. }
  78. }
  79. static void ffat_update_ctx(AVCodecContext *avctx)
  80. {
  81. ATDecodeContext *at = avctx->priv_data;
  82. UInt32 size = sizeof(unsigned);
  83. AudioConverterPrimeInfo prime_info;
  84. AudioStreamBasicDescription out_format;
  85. AudioConverterGetProperty(at->converter,
  86. kAudioConverterPropertyMaximumOutputPacketSize,
  87. &size, &at->pkt_size);
  88. if (at->pkt_size <= 0)
  89. at->pkt_size = 1024 * 50;
  90. size = sizeof(prime_info);
  91. if (!AudioConverterGetProperty(at->converter,
  92. kAudioConverterPrimeInfo,
  93. &size, &prime_info)) {
  94. avctx->initial_padding = prime_info.leadingFrames;
  95. }
  96. size = sizeof(out_format);
  97. if (!AudioConverterGetProperty(at->converter,
  98. kAudioConverterCurrentOutputStreamDescription,
  99. &size, &out_format)) {
  100. if (out_format.mFramesPerPacket)
  101. avctx->frame_size = out_format.mFramesPerPacket;
  102. if (out_format.mBytesPerPacket && avctx->codec_id == AV_CODEC_ID_ILBC)
  103. avctx->block_align = out_format.mBytesPerPacket;
  104. }
  105. at->frame_size = avctx->frame_size;
  106. if (avctx->codec_id == AV_CODEC_ID_PCM_MULAW ||
  107. avctx->codec_id == AV_CODEC_ID_PCM_ALAW) {
  108. at->pkt_size *= 1024;
  109. avctx->frame_size *= 1024;
  110. }
  111. }
  112. static int read_descr(GetByteContext *gb, int *tag)
  113. {
  114. int len = 0;
  115. int count = 4;
  116. *tag = bytestream2_get_byte(gb);
  117. while (count--) {
  118. int c = bytestream2_get_byte(gb);
  119. len = (len << 7) | (c & 0x7f);
  120. if (!(c & 0x80))
  121. break;
  122. }
  123. return len;
  124. }
  125. static int get_ilbc_mode(AVCodecContext *avctx)
  126. {
  127. if (avctx->block_align == 38)
  128. return 20;
  129. else if (avctx->block_align == 50)
  130. return 30;
  131. else if (avctx->bit_rate > 0)
  132. return avctx->bit_rate <= 14000 ? 30 : 20;
  133. else
  134. return 30;
  135. }
  136. static av_cold int get_channel_label(int channel)
  137. {
  138. uint64_t map = 1 << channel;
  139. if (map <= AV_CH_LOW_FREQUENCY)
  140. return channel + 1;
  141. else if (map <= AV_CH_BACK_RIGHT)
  142. return channel + 29;
  143. else if (map <= AV_CH_BACK_CENTER)
  144. return channel - 1;
  145. else if (map <= AV_CH_SIDE_RIGHT)
  146. return channel - 4;
  147. else if (map <= AV_CH_TOP_BACK_RIGHT)
  148. return channel + 1;
  149. else if (map <= AV_CH_STEREO_RIGHT)
  150. return -1;
  151. else if (map <= AV_CH_WIDE_RIGHT)
  152. return channel + 4;
  153. else if (map <= AV_CH_SURROUND_DIRECT_RIGHT)
  154. return channel - 23;
  155. else if (map == AV_CH_LOW_FREQUENCY_2)
  156. return kAudioChannelLabel_LFE2;
  157. else
  158. return -1;
  159. }
  160. static int remap_layout(AudioChannelLayout *layout, uint64_t in_layout, int count)
  161. {
  162. int i;
  163. int c = 0;
  164. layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
  165. layout->mNumberChannelDescriptions = count;
  166. for (i = 0; i < count; i++) {
  167. int label;
  168. while (!(in_layout & (1 << c)) && c < 64)
  169. c++;
  170. if (c == 64)
  171. return AVERROR(EINVAL); // This should never happen
  172. label = get_channel_label(c);
  173. layout->mChannelDescriptions[i].mChannelLabel = label;
  174. if (label < 0)
  175. return AVERROR(EINVAL);
  176. c++;
  177. }
  178. return 0;
  179. }
  180. static int get_aac_tag(uint64_t in_layout)
  181. {
  182. switch (in_layout) {
  183. case AV_CH_LAYOUT_MONO:
  184. return kAudioChannelLayoutTag_Mono;
  185. case AV_CH_LAYOUT_STEREO:
  186. return kAudioChannelLayoutTag_Stereo;
  187. case AV_CH_LAYOUT_QUAD:
  188. return kAudioChannelLayoutTag_AAC_Quadraphonic;
  189. case AV_CH_LAYOUT_OCTAGONAL:
  190. return kAudioChannelLayoutTag_AAC_Octagonal;
  191. case AV_CH_LAYOUT_SURROUND:
  192. return kAudioChannelLayoutTag_AAC_3_0;
  193. case AV_CH_LAYOUT_4POINT0:
  194. return kAudioChannelLayoutTag_AAC_4_0;
  195. case AV_CH_LAYOUT_5POINT0:
  196. return kAudioChannelLayoutTag_AAC_5_0;
  197. case AV_CH_LAYOUT_5POINT1:
  198. return kAudioChannelLayoutTag_AAC_5_1;
  199. case AV_CH_LAYOUT_6POINT0:
  200. return kAudioChannelLayoutTag_AAC_6_0;
  201. case AV_CH_LAYOUT_6POINT1:
  202. return kAudioChannelLayoutTag_AAC_6_1;
  203. case AV_CH_LAYOUT_7POINT0:
  204. return kAudioChannelLayoutTag_AAC_7_0;
  205. case AV_CH_LAYOUT_7POINT1_WIDE_BACK:
  206. return kAudioChannelLayoutTag_AAC_7_1;
  207. case AV_CH_LAYOUT_7POINT1:
  208. return kAudioChannelLayoutTag_MPEG_7_1_C;
  209. default:
  210. return 0;
  211. }
  212. }
  213. static av_cold int ffat_init_encoder(AVCodecContext *avctx)
  214. {
  215. ATDecodeContext *at = avctx->priv_data;
  216. OSStatus status;
  217. AudioStreamBasicDescription in_format = {
  218. .mSampleRate = avctx->sample_rate,
  219. .mFormatID = kAudioFormatLinearPCM,
  220. .mFormatFlags = ((avctx->sample_fmt == AV_SAMPLE_FMT_FLT ||
  221. avctx->sample_fmt == AV_SAMPLE_FMT_DBL) ? kAudioFormatFlagIsFloat
  222. : avctx->sample_fmt == AV_SAMPLE_FMT_U8 ? 0
  223. : kAudioFormatFlagIsSignedInteger)
  224. | kAudioFormatFlagIsPacked,
  225. .mBytesPerPacket = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
  226. .mFramesPerPacket = 1,
  227. .mBytesPerFrame = av_get_bytes_per_sample(avctx->sample_fmt) * avctx->channels,
  228. .mChannelsPerFrame = avctx->channels,
  229. .mBitsPerChannel = av_get_bytes_per_sample(avctx->sample_fmt) * 8,
  230. };
  231. AudioStreamBasicDescription out_format = {
  232. .mSampleRate = avctx->sample_rate,
  233. .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
  234. .mChannelsPerFrame = in_format.mChannelsPerFrame,
  235. };
  236. UInt32 layout_size = sizeof(AudioChannelLayout) +
  237. sizeof(AudioChannelDescription) * avctx->channels;
  238. AudioChannelLayout *channel_layout = av_malloc(layout_size);
  239. if (!channel_layout)
  240. return AVERROR(ENOMEM);
  241. if (avctx->codec_id == AV_CODEC_ID_ILBC) {
  242. int mode = get_ilbc_mode(avctx);
  243. out_format.mFramesPerPacket = 8000 * mode / 1000;
  244. out_format.mBytesPerPacket = (mode == 20 ? 38 : 50);
  245. }
  246. status = AudioConverterNew(&in_format, &out_format, &at->converter);
  247. if (status != 0) {
  248. av_log(avctx, AV_LOG_ERROR, "AudioToolbox init error: %i\n", (int)status);
  249. av_free(channel_layout);
  250. return AVERROR_UNKNOWN;
  251. }
  252. if (!avctx->channel_layout)
  253. avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
  254. if ((status = remap_layout(channel_layout, avctx->channel_layout, avctx->channels)) < 0) {
  255. av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
  256. av_free(channel_layout);
  257. return status;
  258. }
  259. if (AudioConverterSetProperty(at->converter, kAudioConverterInputChannelLayout,
  260. layout_size, channel_layout)) {
  261. av_log(avctx, AV_LOG_ERROR, "Unsupported input channel layout\n");
  262. av_free(channel_layout);
  263. return AVERROR(EINVAL);
  264. }
  265. if (avctx->codec_id == AV_CODEC_ID_AAC) {
  266. int tag = get_aac_tag(avctx->channel_layout);
  267. if (tag) {
  268. channel_layout->mChannelLayoutTag = tag;
  269. channel_layout->mNumberChannelDescriptions = 0;
  270. }
  271. }
  272. if (AudioConverterSetProperty(at->converter, kAudioConverterOutputChannelLayout,
  273. layout_size, channel_layout)) {
  274. av_log(avctx, AV_LOG_ERROR, "Unsupported output channel layout\n");
  275. av_free(channel_layout);
  276. return AVERROR(EINVAL);
  277. }
  278. av_free(channel_layout);
  279. if (avctx->bits_per_raw_sample)
  280. AudioConverterSetProperty(at->converter,
  281. kAudioConverterPropertyBitDepthHint,
  282. sizeof(avctx->bits_per_raw_sample),
  283. &avctx->bits_per_raw_sample);
  284. #if !TARGET_OS_IPHONE
  285. if (at->mode == -1)
  286. at->mode = (avctx->flags & AV_CODEC_FLAG_QSCALE) ?
  287. kAudioCodecBitRateControlMode_Variable :
  288. kAudioCodecBitRateControlMode_Constant;
  289. AudioConverterSetProperty(at->converter, kAudioCodecPropertyBitRateControlMode,
  290. sizeof(at->mode), &at->mode);
  291. if (at->mode == kAudioCodecBitRateControlMode_Variable) {
  292. int q = avctx->global_quality / FF_QP2LAMBDA;
  293. if (q < 0 || q > 14) {
  294. av_log(avctx, AV_LOG_WARNING,
  295. "VBR quality %d out of range, should be 0-14\n", q);
  296. q = av_clip(q, 0, 14);
  297. }
  298. q = 127 - q * 9;
  299. AudioConverterSetProperty(at->converter, kAudioCodecPropertySoundQualityForVBR,
  300. sizeof(q), &q);
  301. } else
  302. #endif
  303. if (avctx->bit_rate > 0) {
  304. UInt32 rate = avctx->bit_rate;
  305. UInt32 size;
  306. status = AudioConverterGetPropertyInfo(at->converter,
  307. kAudioConverterApplicableEncodeBitRates,
  308. &size, NULL);
  309. if (!status && size) {
  310. UInt32 new_rate = rate;
  311. int count;
  312. int i;
  313. AudioValueRange *ranges = av_malloc(size);
  314. if (!ranges)
  315. return AVERROR(ENOMEM);
  316. AudioConverterGetProperty(at->converter,
  317. kAudioConverterApplicableEncodeBitRates,
  318. &size, ranges);
  319. count = size / sizeof(AudioValueRange);
  320. for (i = 0; i < count; i++) {
  321. AudioValueRange *range = &ranges[i];
  322. if (rate >= range->mMinimum && rate <= range->mMaximum) {
  323. new_rate = rate;
  324. break;
  325. } else if (rate > range->mMaximum) {
  326. new_rate = range->mMaximum;
  327. } else {
  328. new_rate = range->mMinimum;
  329. break;
  330. }
  331. }
  332. if (new_rate != rate) {
  333. av_log(avctx, AV_LOG_WARNING,
  334. "Bitrate %u not allowed; changing to %u\n", rate, new_rate);
  335. rate = new_rate;
  336. }
  337. av_free(ranges);
  338. }
  339. AudioConverterSetProperty(at->converter, kAudioConverterEncodeBitRate,
  340. sizeof(rate), &rate);
  341. }
  342. at->quality = 96 - at->quality * 32;
  343. AudioConverterSetProperty(at->converter, kAudioConverterCodecQuality,
  344. sizeof(at->quality), &at->quality);
  345. if (!AudioConverterGetPropertyInfo(at->converter, kAudioConverterCompressionMagicCookie,
  346. &avctx->extradata_size, NULL) &&
  347. avctx->extradata_size) {
  348. int extradata_size = avctx->extradata_size;
  349. uint8_t *extradata;
  350. if (!(avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE)))
  351. return AVERROR(ENOMEM);
  352. if (avctx->codec_id == AV_CODEC_ID_ALAC) {
  353. avctx->extradata_size = 0x24;
  354. AV_WB32(avctx->extradata, 0x24);
  355. AV_WB32(avctx->extradata + 4, MKBETAG('a','l','a','c'));
  356. extradata = avctx->extradata + 12;
  357. avctx->extradata_size = 0x24;
  358. } else {
  359. extradata = avctx->extradata;
  360. }
  361. status = AudioConverterGetProperty(at->converter,
  362. kAudioConverterCompressionMagicCookie,
  363. &extradata_size, extradata);
  364. if (status != 0) {
  365. av_log(avctx, AV_LOG_ERROR, "AudioToolbox cookie error: %i\n", (int)status);
  366. return AVERROR_UNKNOWN;
  367. } else if (avctx->codec_id == AV_CODEC_ID_AAC) {
  368. GetByteContext gb;
  369. int tag, len;
  370. bytestream2_init(&gb, extradata, extradata_size);
  371. do {
  372. len = read_descr(&gb, &tag);
  373. if (tag == MP4DecConfigDescrTag) {
  374. bytestream2_skip(&gb, 13);
  375. len = read_descr(&gb, &tag);
  376. if (tag == MP4DecSpecificDescrTag) {
  377. len = FFMIN(gb.buffer_end - gb.buffer, len);
  378. memmove(extradata, gb.buffer, len);
  379. avctx->extradata_size = len;
  380. break;
  381. }
  382. } else if (tag == MP4ESDescrTag) {
  383. int flags;
  384. bytestream2_skip(&gb, 2);
  385. flags = bytestream2_get_byte(&gb);
  386. if (flags & 0x80) //streamDependenceFlag
  387. bytestream2_skip(&gb, 2);
  388. if (flags & 0x40) //URL_Flag
  389. bytestream2_skip(&gb, bytestream2_get_byte(&gb));
  390. if (flags & 0x20) //OCRstreamFlag
  391. bytestream2_skip(&gb, 2);
  392. }
  393. } while (bytestream2_get_bytes_left(&gb));
  394. } else if (avctx->codec_id != AV_CODEC_ID_ALAC) {
  395. avctx->extradata_size = extradata_size;
  396. }
  397. }
  398. ffat_update_ctx(avctx);
  399. #if !TARGET_OS_IPHONE && defined(__MAC_10_9)
  400. if (at->mode == kAudioCodecBitRateControlMode_Variable && avctx->rc_max_rate) {
  401. UInt32 max_size = avctx->rc_max_rate * avctx->frame_size / avctx->sample_rate;
  402. if (max_size)
  403. AudioConverterSetProperty(at->converter, kAudioCodecPropertyPacketSizeLimitForVBR,
  404. sizeof(max_size), &max_size);
  405. }
  406. #endif
  407. ff_af_queue_init(avctx, &at->afq);
  408. at->encoding_frame = av_frame_alloc();
  409. if (!at->encoding_frame)
  410. return AVERROR(ENOMEM);
  411. return 0;
  412. }
  413. static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_packets,
  414. AudioBufferList *data,
  415. AudioStreamPacketDescription **packets,
  416. void *inctx)
  417. {
  418. AVCodecContext *avctx = inctx;
  419. ATDecodeContext *at = avctx->priv_data;
  420. AVFrame *frame;
  421. int ret;
  422. if (!at->frame_queue.available) {
  423. if (at->eof) {
  424. *nb_packets = 0;
  425. return 0;
  426. } else {
  427. *nb_packets = 0;
  428. return 1;
  429. }
  430. }
  431. frame = ff_bufqueue_get(&at->frame_queue);
  432. data->mNumberBuffers = 1;
  433. data->mBuffers[0].mNumberChannels = avctx->channels;
  434. data->mBuffers[0].mDataByteSize = frame->nb_samples *
  435. av_get_bytes_per_sample(avctx->sample_fmt) *
  436. avctx->channels;
  437. data->mBuffers[0].mData = frame->data[0];
  438. if (*nb_packets > frame->nb_samples)
  439. *nb_packets = frame->nb_samples;
  440. av_frame_unref(at->encoding_frame);
  441. ret = av_frame_ref(at->encoding_frame, frame);
  442. if (ret < 0) {
  443. *nb_packets = 0;
  444. return ret;
  445. }
  446. ff_bufqueue_add(avctx, &at->used_frame_queue, frame);
  447. return 0;
  448. }
  449. static int ffat_encode(AVCodecContext *avctx, AVPacket *avpkt,
  450. const AVFrame *frame, int *got_packet_ptr)
  451. {
  452. ATDecodeContext *at = avctx->priv_data;
  453. OSStatus ret;
  454. AudioBufferList out_buffers = {
  455. .mNumberBuffers = 1,
  456. .mBuffers = {
  457. {
  458. .mNumberChannels = avctx->channels,
  459. .mDataByteSize = at->pkt_size,
  460. }
  461. }
  462. };
  463. AudioStreamPacketDescription out_pkt_desc = {0};
  464. if (frame) {
  465. AVFrame *in_frame;
  466. if (ff_bufqueue_is_full(&at->frame_queue)) {
  467. /*
  468. * The frame queue is significantly larger than needed in practice,
  469. * but no clear way to determine the minimum number of samples to
  470. * get output from AudioConverterFillComplexBuffer().
  471. */
  472. av_log(avctx, AV_LOG_ERROR, "Bug: frame queue is too small.\n");
  473. return AVERROR_BUG;
  474. }
  475. if ((ret = ff_af_queue_add(&at->afq, frame)) < 0)
  476. return ret;
  477. in_frame = av_frame_clone(frame);
  478. if (!in_frame)
  479. return AVERROR(ENOMEM);
  480. ff_bufqueue_add(avctx, &at->frame_queue, in_frame);
  481. } else {
  482. at->eof = 1;
  483. }
  484. if ((ret = ff_alloc_packet2(avctx, avpkt, at->pkt_size, 0)) < 0)
  485. return ret;
  486. out_buffers.mBuffers[0].mData = avpkt->data;
  487. *got_packet_ptr = avctx->frame_size / at->frame_size;
  488. ret = AudioConverterFillComplexBuffer(at->converter, ffat_encode_callback, avctx,
  489. got_packet_ptr, &out_buffers,
  490. (avctx->frame_size > at->frame_size) ? NULL : &out_pkt_desc);
  491. ff_bufqueue_discard_all(&at->used_frame_queue);
  492. if ((!ret || ret == 1) && *got_packet_ptr) {
  493. avpkt->size = out_buffers.mBuffers[0].mDataByteSize;
  494. ff_af_queue_remove(&at->afq, out_pkt_desc.mVariableFramesInPacket ?
  495. out_pkt_desc.mVariableFramesInPacket :
  496. avctx->frame_size,
  497. &avpkt->pts,
  498. &avpkt->duration);
  499. } else if (ret && ret != 1) {
  500. av_log(avctx, AV_LOG_WARNING, "Encode error: %i\n", ret);
  501. }
  502. return 0;
  503. }
  504. static av_cold void ffat_encode_flush(AVCodecContext *avctx)
  505. {
  506. ATDecodeContext *at = avctx->priv_data;
  507. AudioConverterReset(at->converter);
  508. ff_bufqueue_discard_all(&at->frame_queue);
  509. ff_bufqueue_discard_all(&at->used_frame_queue);
  510. }
  511. static av_cold int ffat_close_encoder(AVCodecContext *avctx)
  512. {
  513. ATDecodeContext *at = avctx->priv_data;
  514. AudioConverterDispose(at->converter);
  515. ff_bufqueue_discard_all(&at->frame_queue);
  516. ff_bufqueue_discard_all(&at->used_frame_queue);
  517. ff_af_queue_close(&at->afq);
  518. av_frame_free(&at->encoding_frame);
  519. return 0;
  520. }
  521. static const AVProfile aac_profiles[] = {
  522. { FF_PROFILE_AAC_LOW, "LC" },
  523. { FF_PROFILE_AAC_HE, "HE-AAC" },
  524. { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
  525. { FF_PROFILE_AAC_LD, "LD" },
  526. { FF_PROFILE_AAC_ELD, "ELD" },
  527. { FF_PROFILE_UNKNOWN },
  528. };
  529. #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  530. static const AVOption options[] = {
  531. #if !TARGET_OS_IPHONE
  532. {"aac_at_mode", "ratecontrol mode", offsetof(ATDecodeContext, mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, kAudioCodecBitRateControlMode_Variable, AE, "mode"},
  533. {"auto", "VBR if global quality is given; CBR otherwise", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, AE, "mode"},
  534. {"cbr", "constant bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Constant}, INT_MIN, INT_MAX, AE, "mode"},
  535. {"abr", "long-term average bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_LongTermAverage}, INT_MIN, INT_MAX, AE, "mode"},
  536. {"cvbr", "constrained variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_VariableConstrained}, INT_MIN, INT_MAX, AE, "mode"},
  537. {"vbr" , "variable bitrate", 0, AV_OPT_TYPE_CONST, {.i64 = kAudioCodecBitRateControlMode_Variable}, INT_MIN, INT_MAX, AE, "mode"},
  538. #endif
  539. {"aac_at_quality", "quality vs speed control", offsetof(ATDecodeContext, quality), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 2, AE},
  540. { NULL },
  541. };
  542. #define FFAT_ENC_CLASS(NAME) \
  543. static const AVClass ffat_##NAME##_enc_class = { \
  544. .class_name = "at_" #NAME "_enc", \
  545. .item_name = av_default_item_name, \
  546. .option = options, \
  547. .version = LIBAVUTIL_VERSION_INT, \
  548. };
  549. #define FFAT_ENC(NAME, ID, PROFILES, ...) \
  550. FFAT_ENC_CLASS(NAME) \
  551. AVCodec ff_##NAME##_at_encoder = { \
  552. .name = #NAME "_at", \
  553. .long_name = NULL_IF_CONFIG_SMALL(#NAME " (AudioToolbox)"), \
  554. .type = AVMEDIA_TYPE_AUDIO, \
  555. .id = ID, \
  556. .priv_data_size = sizeof(ATDecodeContext), \
  557. .init = ffat_init_encoder, \
  558. .close = ffat_close_encoder, \
  559. .encode2 = ffat_encode, \
  560. .flush = ffat_encode_flush, \
  561. .priv_class = &ffat_##NAME##_enc_class, \
  562. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
  563. AV_CODEC_CAP_ENCODER_FLUSH __VA_ARGS__, \
  564. .sample_fmts = (const enum AVSampleFormat[]) { \
  565. AV_SAMPLE_FMT_S16, \
  566. AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_NONE \
  567. }, \
  568. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \
  569. .profiles = PROFILES, \
  570. .wrapper_name = "at", \
  571. };
  572. static const uint64_t aac_at_channel_layouts[] = {
  573. AV_CH_LAYOUT_MONO,
  574. AV_CH_LAYOUT_STEREO,
  575. AV_CH_LAYOUT_SURROUND,
  576. AV_CH_LAYOUT_4POINT0,
  577. AV_CH_LAYOUT_5POINT0,
  578. AV_CH_LAYOUT_5POINT1,
  579. AV_CH_LAYOUT_6POINT0,
  580. AV_CH_LAYOUT_6POINT1,
  581. AV_CH_LAYOUT_7POINT0,
  582. AV_CH_LAYOUT_7POINT1_WIDE_BACK,
  583. AV_CH_LAYOUT_QUAD,
  584. AV_CH_LAYOUT_OCTAGONAL,
  585. 0,
  586. };
  587. FFAT_ENC(aac, AV_CODEC_ID_AAC, aac_profiles, , .channel_layouts = aac_at_channel_layouts)
  588. //FFAT_ENC(adpcm_ima_qt, AV_CODEC_ID_ADPCM_IMA_QT, NULL)
  589. FFAT_ENC(alac, AV_CODEC_ID_ALAC, NULL, | AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  590. FFAT_ENC(ilbc, AV_CODEC_ID_ILBC, NULL)
  591. FFAT_ENC(pcm_alaw, AV_CODEC_ID_PCM_ALAW, NULL)
  592. FFAT_ENC(pcm_mulaw, AV_CODEC_ID_PCM_MULAW, NULL)