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.

659 lines
21KB

  1. /*
  2. * ALAC audio encoder
  3. * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "put_bits.h"
  23. #include "internal.h"
  24. #include "lpc.h"
  25. #include "mathops.h"
  26. #include "alac_data.h"
  27. #define DEFAULT_FRAME_SIZE 4096
  28. #define ALAC_EXTRADATA_SIZE 36
  29. #define ALAC_FRAME_HEADER_SIZE 55
  30. #define ALAC_FRAME_FOOTER_SIZE 3
  31. #define ALAC_ESCAPE_CODE 0x1FF
  32. #define ALAC_MAX_LPC_ORDER 30
  33. #define DEFAULT_MAX_PRED_ORDER 6
  34. #define DEFAULT_MIN_PRED_ORDER 4
  35. #define ALAC_MAX_LPC_PRECISION 9
  36. #define ALAC_MAX_LPC_SHIFT 9
  37. #define ALAC_CHMODE_LEFT_RIGHT 0
  38. #define ALAC_CHMODE_LEFT_SIDE 1
  39. #define ALAC_CHMODE_RIGHT_SIDE 2
  40. #define ALAC_CHMODE_MID_SIDE 3
  41. typedef struct RiceContext {
  42. int history_mult;
  43. int initial_history;
  44. int k_modifier;
  45. int rice_modifier;
  46. } RiceContext;
  47. typedef struct AlacLPCContext {
  48. int lpc_order;
  49. int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
  50. int lpc_quant;
  51. } AlacLPCContext;
  52. typedef struct AlacEncodeContext {
  53. int frame_size; /**< current frame size */
  54. int verbatim; /**< current frame verbatim mode flag */
  55. int compression_level;
  56. int min_prediction_order;
  57. int max_prediction_order;
  58. int max_coded_frame_size;
  59. int write_sample_size;
  60. int extra_bits;
  61. int32_t sample_buf[2][DEFAULT_FRAME_SIZE];
  62. int32_t predictor_buf[DEFAULT_FRAME_SIZE];
  63. int interlacing_shift;
  64. int interlacing_leftweight;
  65. PutBitContext pbctx;
  66. RiceContext rc;
  67. AlacLPCContext lpc[2];
  68. LPCContext lpc_ctx;
  69. AVCodecContext *avctx;
  70. } AlacEncodeContext;
  71. static void init_sample_buffers(AlacEncodeContext *s, int channels,
  72. uint8_t const *samples[2])
  73. {
  74. int ch, i;
  75. int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
  76. s->avctx->bits_per_raw_sample;
  77. #define COPY_SAMPLES(type) do { \
  78. for (ch = 0; ch < channels; ch++) { \
  79. int32_t *bptr = s->sample_buf[ch]; \
  80. const type *sptr = (const type *)samples[ch]; \
  81. for (i = 0; i < s->frame_size; i++) \
  82. bptr[i] = sptr[i] >> shift; \
  83. } \
  84. } while (0)
  85. if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P)
  86. COPY_SAMPLES(int32_t);
  87. else
  88. COPY_SAMPLES(int16_t);
  89. }
  90. static void encode_scalar(AlacEncodeContext *s, int x,
  91. int k, int write_sample_size)
  92. {
  93. int divisor, q, r;
  94. k = FFMIN(k, s->rc.k_modifier);
  95. divisor = (1<<k) - 1;
  96. q = x / divisor;
  97. r = x % divisor;
  98. if (q > 8) {
  99. // write escape code and sample value directly
  100. put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
  101. put_bits(&s->pbctx, write_sample_size, x);
  102. } else {
  103. if (q)
  104. put_bits(&s->pbctx, q, (1<<q) - 1);
  105. put_bits(&s->pbctx, 1, 0);
  106. if (k != 1) {
  107. if (r > 0)
  108. put_bits(&s->pbctx, k, r+1);
  109. else
  110. put_bits(&s->pbctx, k-1, 0);
  111. }
  112. }
  113. }
  114. static void write_element_header(AlacEncodeContext *s,
  115. enum AlacRawDataBlockType element,
  116. int instance)
  117. {
  118. int encode_fs = 0;
  119. if (s->frame_size < DEFAULT_FRAME_SIZE)
  120. encode_fs = 1;
  121. put_bits(&s->pbctx, 3, element); // element type
  122. put_bits(&s->pbctx, 4, instance); // element instance
  123. put_bits(&s->pbctx, 12, 0); // unused header bits
  124. put_bits(&s->pbctx, 1, encode_fs); // Sample count is in the header
  125. put_bits(&s->pbctx, 2, s->extra_bits >> 3); // Extra bytes (for 24-bit)
  126. put_bits(&s->pbctx, 1, s->verbatim); // Audio block is verbatim
  127. if (encode_fs)
  128. put_bits32(&s->pbctx, s->frame_size); // No. of samples in the frame
  129. }
  130. static void calc_predictor_params(AlacEncodeContext *s, int ch)
  131. {
  132. int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
  133. int shift[MAX_LPC_ORDER];
  134. int opt_order;
  135. if (s->compression_level == 1) {
  136. s->lpc[ch].lpc_order = 6;
  137. s->lpc[ch].lpc_quant = 6;
  138. s->lpc[ch].lpc_coeff[0] = 160;
  139. s->lpc[ch].lpc_coeff[1] = -190;
  140. s->lpc[ch].lpc_coeff[2] = 170;
  141. s->lpc[ch].lpc_coeff[3] = -130;
  142. s->lpc[ch].lpc_coeff[4] = 80;
  143. s->lpc[ch].lpc_coeff[5] = -25;
  144. } else {
  145. opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
  146. s->frame_size,
  147. s->min_prediction_order,
  148. s->max_prediction_order,
  149. ALAC_MAX_LPC_PRECISION, coefs, shift,
  150. FF_LPC_TYPE_LEVINSON, 0,
  151. ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1);
  152. s->lpc[ch].lpc_order = opt_order;
  153. s->lpc[ch].lpc_quant = shift[opt_order-1];
  154. memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
  155. }
  156. }
  157. static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
  158. {
  159. int i, best;
  160. int32_t lt, rt;
  161. uint64_t sum[4];
  162. uint64_t score[4];
  163. /* calculate sum of 2nd order residual for each channel */
  164. sum[0] = sum[1] = sum[2] = sum[3] = 0;
  165. for (i = 2; i < n; i++) {
  166. lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
  167. rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
  168. sum[2] += FFABS((lt + rt) >> 1);
  169. sum[3] += FFABS(lt - rt);
  170. sum[0] += FFABS(lt);
  171. sum[1] += FFABS(rt);
  172. }
  173. /* calculate score for each mode */
  174. score[0] = sum[0] + sum[1];
  175. score[1] = sum[0] + sum[3];
  176. score[2] = sum[1] + sum[3];
  177. score[3] = sum[2] + sum[3];
  178. /* return mode with lowest score */
  179. best = 0;
  180. for (i = 1; i < 4; i++) {
  181. if (score[i] < score[best])
  182. best = i;
  183. }
  184. return best;
  185. }
  186. static void alac_stereo_decorrelation(AlacEncodeContext *s)
  187. {
  188. int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
  189. int i, mode, n = s->frame_size;
  190. int32_t tmp;
  191. mode = estimate_stereo_mode(left, right, n);
  192. switch (mode) {
  193. case ALAC_CHMODE_LEFT_RIGHT:
  194. s->interlacing_leftweight = 0;
  195. s->interlacing_shift = 0;
  196. break;
  197. case ALAC_CHMODE_LEFT_SIDE:
  198. for (i = 0; i < n; i++)
  199. right[i] = left[i] - right[i];
  200. s->interlacing_leftweight = 1;
  201. s->interlacing_shift = 0;
  202. break;
  203. case ALAC_CHMODE_RIGHT_SIDE:
  204. for (i = 0; i < n; i++) {
  205. tmp = right[i];
  206. right[i] = left[i] - right[i];
  207. left[i] = tmp + (right[i] >> 31);
  208. }
  209. s->interlacing_leftweight = 1;
  210. s->interlacing_shift = 31;
  211. break;
  212. default:
  213. for (i = 0; i < n; i++) {
  214. tmp = left[i];
  215. left[i] = (tmp + right[i]) >> 1;
  216. right[i] = tmp - right[i];
  217. }
  218. s->interlacing_leftweight = 1;
  219. s->interlacing_shift = 1;
  220. break;
  221. }
  222. }
  223. static void alac_linear_predictor(AlacEncodeContext *s, int ch)
  224. {
  225. int i;
  226. AlacLPCContext lpc = s->lpc[ch];
  227. if (lpc.lpc_order == 31) {
  228. s->predictor_buf[0] = s->sample_buf[ch][0];
  229. for (i = 1; i < s->frame_size; i++) {
  230. s->predictor_buf[i] = s->sample_buf[ch][i ] -
  231. s->sample_buf[ch][i - 1];
  232. }
  233. return;
  234. }
  235. // generalised linear predictor
  236. if (lpc.lpc_order > 0) {
  237. int32_t *samples = s->sample_buf[ch];
  238. int32_t *residual = s->predictor_buf;
  239. // generate warm-up samples
  240. residual[0] = samples[0];
  241. for (i = 1; i <= lpc.lpc_order; i++)
  242. residual[i] = samples[i] - samples[i-1];
  243. // perform lpc on remaining samples
  244. for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
  245. int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
  246. for (j = 0; j < lpc.lpc_order; j++) {
  247. sum += (samples[lpc.lpc_order-j] - samples[0]) *
  248. lpc.lpc_coeff[j];
  249. }
  250. sum >>= lpc.lpc_quant;
  251. sum += samples[0];
  252. residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
  253. s->write_sample_size);
  254. res_val = residual[i];
  255. if (res_val) {
  256. int index = lpc.lpc_order - 1;
  257. int neg = (res_val < 0);
  258. while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
  259. int val = samples[0] - samples[lpc.lpc_order - index];
  260. int sign = (val ? FFSIGN(val) : 0);
  261. if (neg)
  262. sign *= -1;
  263. lpc.lpc_coeff[index] -= sign;
  264. val *= sign;
  265. res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
  266. index--;
  267. }
  268. }
  269. samples++;
  270. }
  271. }
  272. }
  273. static void alac_entropy_coder(AlacEncodeContext *s)
  274. {
  275. unsigned int history = s->rc.initial_history;
  276. int sign_modifier = 0, i, k;
  277. int32_t *samples = s->predictor_buf;
  278. for (i = 0; i < s->frame_size;) {
  279. int x;
  280. k = av_log2((history >> 9) + 3);
  281. x = -2 * (*samples) -1;
  282. x ^= x >> 31;
  283. samples++;
  284. i++;
  285. encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
  286. history += x * s->rc.history_mult -
  287. ((history * s->rc.history_mult) >> 9);
  288. sign_modifier = 0;
  289. if (x > 0xFFFF)
  290. history = 0xFFFF;
  291. if (history < 128 && i < s->frame_size) {
  292. unsigned int block_size = 0;
  293. k = 7 - av_log2(history) + ((history + 16) >> 6);
  294. while (*samples == 0 && i < s->frame_size) {
  295. samples++;
  296. i++;
  297. block_size++;
  298. }
  299. encode_scalar(s, block_size, k, 16);
  300. sign_modifier = (block_size <= 0xFFFF);
  301. history = 0;
  302. }
  303. }
  304. }
  305. static void write_element(AlacEncodeContext *s,
  306. enum AlacRawDataBlockType element, int instance,
  307. const uint8_t *samples0, const uint8_t *samples1)
  308. {
  309. uint8_t const *samples[2] = { samples0, samples1 };
  310. int i, j, channels;
  311. int prediction_type = 0;
  312. PutBitContext *pb = &s->pbctx;
  313. channels = element == TYPE_CPE ? 2 : 1;
  314. if (s->verbatim) {
  315. write_element_header(s, element, instance);
  316. /* samples are channel-interleaved in verbatim mode */
  317. if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
  318. int shift = 32 - s->avctx->bits_per_raw_sample;
  319. int32_t const *samples_s32[2] = { (const int32_t *)samples0,
  320. (const int32_t *)samples1 };
  321. for (i = 0; i < s->frame_size; i++)
  322. for (j = 0; j < channels; j++)
  323. put_sbits(pb, s->avctx->bits_per_raw_sample,
  324. samples_s32[j][i] >> shift);
  325. } else {
  326. int16_t const *samples_s16[2] = { (const int16_t *)samples0,
  327. (const int16_t *)samples1 };
  328. for (i = 0; i < s->frame_size; i++)
  329. for (j = 0; j < channels; j++)
  330. put_sbits(pb, s->avctx->bits_per_raw_sample,
  331. samples_s16[j][i]);
  332. }
  333. } else {
  334. s->write_sample_size = s->avctx->bits_per_raw_sample - s->extra_bits +
  335. channels - 1;
  336. init_sample_buffers(s, channels, samples);
  337. write_element_header(s, element, instance);
  338. if (channels == 2)
  339. alac_stereo_decorrelation(s);
  340. else
  341. s->interlacing_shift = s->interlacing_leftweight = 0;
  342. put_bits(pb, 8, s->interlacing_shift);
  343. put_bits(pb, 8, s->interlacing_leftweight);
  344. for (i = 0; i < channels; i++) {
  345. calc_predictor_params(s, i);
  346. put_bits(pb, 4, prediction_type);
  347. put_bits(pb, 4, s->lpc[i].lpc_quant);
  348. put_bits(pb, 3, s->rc.rice_modifier);
  349. put_bits(pb, 5, s->lpc[i].lpc_order);
  350. // predictor coeff. table
  351. for (j = 0; j < s->lpc[i].lpc_order; j++)
  352. put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
  353. }
  354. // write extra bits if needed
  355. if (s->extra_bits) {
  356. uint32_t mask = (1 << s->extra_bits) - 1;
  357. for (i = 0; i < s->frame_size; i++) {
  358. for (j = 0; j < channels; j++) {
  359. put_bits(pb, s->extra_bits, s->sample_buf[j][i] & mask);
  360. s->sample_buf[j][i] >>= s->extra_bits;
  361. }
  362. }
  363. }
  364. // apply lpc and entropy coding to audio samples
  365. for (i = 0; i < channels; i++) {
  366. alac_linear_predictor(s, i);
  367. // TODO: determine when this will actually help. for now it's not used.
  368. if (prediction_type == 15) {
  369. // 2nd pass 1st order filter
  370. for (j = s->frame_size - 1; j > 0; j--)
  371. s->predictor_buf[j] -= s->predictor_buf[j - 1];
  372. }
  373. alac_entropy_coder(s);
  374. }
  375. }
  376. }
  377. static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
  378. uint8_t * const *samples)
  379. {
  380. PutBitContext *pb = &s->pbctx;
  381. const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
  382. const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
  383. int ch, element, sce, cpe;
  384. init_put_bits(pb, avpkt->data, avpkt->size);
  385. ch = element = sce = cpe = 0;
  386. while (ch < s->avctx->channels) {
  387. if (ch_elements[element] == TYPE_CPE) {
  388. write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
  389. samples[ch_map[ch + 1]]);
  390. cpe++;
  391. ch += 2;
  392. } else {
  393. write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
  394. sce++;
  395. ch++;
  396. }
  397. element++;
  398. }
  399. put_bits(pb, 3, TYPE_END);
  400. flush_put_bits(pb);
  401. return put_bits_count(pb) >> 3;
  402. }
  403. static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
  404. {
  405. int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
  406. return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
  407. }
  408. static av_cold int alac_encode_close(AVCodecContext *avctx)
  409. {
  410. AlacEncodeContext *s = avctx->priv_data;
  411. ff_lpc_end(&s->lpc_ctx);
  412. av_freep(&avctx->extradata);
  413. avctx->extradata_size = 0;
  414. av_freep(&avctx->coded_frame);
  415. return 0;
  416. }
  417. static av_cold int alac_encode_init(AVCodecContext *avctx)
  418. {
  419. AlacEncodeContext *s = avctx->priv_data;
  420. int ret;
  421. uint8_t *alac_extradata;
  422. avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
  423. if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
  424. if (avctx->bits_per_raw_sample != 24)
  425. av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
  426. avctx->bits_per_raw_sample = 24;
  427. } else {
  428. avctx->bits_per_raw_sample = 16;
  429. s->extra_bits = 0;
  430. }
  431. // Set default compression level
  432. if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
  433. s->compression_level = 2;
  434. else
  435. s->compression_level = av_clip(avctx->compression_level, 0, 2);
  436. // Initialize default Rice parameters
  437. s->rc.history_mult = 40;
  438. s->rc.initial_history = 10;
  439. s->rc.k_modifier = 14;
  440. s->rc.rice_modifier = 4;
  441. s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
  442. avctx->channels,
  443. avctx->bits_per_raw_sample);
  444. avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  445. if (!avctx->extradata) {
  446. ret = AVERROR(ENOMEM);
  447. goto error;
  448. }
  449. avctx->extradata_size = ALAC_EXTRADATA_SIZE;
  450. alac_extradata = avctx->extradata;
  451. AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
  452. AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
  453. AV_WB32(alac_extradata+12, avctx->frame_size);
  454. AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
  455. AV_WB8 (alac_extradata+21, avctx->channels);
  456. AV_WB32(alac_extradata+24, s->max_coded_frame_size);
  457. AV_WB32(alac_extradata+28,
  458. avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
  459. AV_WB32(alac_extradata+32, avctx->sample_rate);
  460. // Set relevant extradata fields
  461. if (s->compression_level > 0) {
  462. AV_WB8(alac_extradata+18, s->rc.history_mult);
  463. AV_WB8(alac_extradata+19, s->rc.initial_history);
  464. AV_WB8(alac_extradata+20, s->rc.k_modifier);
  465. }
  466. s->min_prediction_order = DEFAULT_MIN_PRED_ORDER;
  467. if (avctx->min_prediction_order >= 0) {
  468. if (avctx->min_prediction_order < MIN_LPC_ORDER ||
  469. avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
  470. av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
  471. avctx->min_prediction_order);
  472. ret = AVERROR(EINVAL);
  473. goto error;
  474. }
  475. s->min_prediction_order = avctx->min_prediction_order;
  476. }
  477. s->max_prediction_order = DEFAULT_MAX_PRED_ORDER;
  478. if (avctx->max_prediction_order >= 0) {
  479. if (avctx->max_prediction_order < MIN_LPC_ORDER ||
  480. avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
  481. av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
  482. avctx->max_prediction_order);
  483. ret = AVERROR(EINVAL);
  484. goto error;
  485. }
  486. s->max_prediction_order = avctx->max_prediction_order;
  487. }
  488. if (s->max_prediction_order < s->min_prediction_order) {
  489. av_log(avctx, AV_LOG_ERROR,
  490. "invalid prediction orders: min=%d max=%d\n",
  491. s->min_prediction_order, s->max_prediction_order);
  492. ret = AVERROR(EINVAL);
  493. goto error;
  494. }
  495. avctx->coded_frame = av_frame_alloc();
  496. if (!avctx->coded_frame) {
  497. ret = AVERROR(ENOMEM);
  498. goto error;
  499. }
  500. s->avctx = avctx;
  501. if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
  502. s->max_prediction_order,
  503. FF_LPC_TYPE_LEVINSON)) < 0) {
  504. goto error;
  505. }
  506. return 0;
  507. error:
  508. alac_encode_close(avctx);
  509. return ret;
  510. }
  511. static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  512. const AVFrame *frame, int *got_packet_ptr)
  513. {
  514. AlacEncodeContext *s = avctx->priv_data;
  515. int out_bytes, max_frame_size, ret;
  516. s->frame_size = frame->nb_samples;
  517. if (frame->nb_samples < DEFAULT_FRAME_SIZE)
  518. max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
  519. avctx->bits_per_raw_sample);
  520. else
  521. max_frame_size = s->max_coded_frame_size;
  522. if ((ret = ff_alloc_packet(avpkt, 2 * max_frame_size))) {
  523. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  524. return ret;
  525. }
  526. /* use verbatim mode for compression_level 0 */
  527. if (s->compression_level) {
  528. s->verbatim = 0;
  529. s->extra_bits = avctx->bits_per_raw_sample - 16;
  530. } else {
  531. s->verbatim = 1;
  532. s->extra_bits = 0;
  533. }
  534. out_bytes = write_frame(s, avpkt, frame->extended_data);
  535. if (out_bytes > max_frame_size) {
  536. /* frame too large. use verbatim mode */
  537. s->verbatim = 1;
  538. s->extra_bits = 0;
  539. out_bytes = write_frame(s, avpkt, frame->extended_data);
  540. }
  541. avpkt->size = out_bytes;
  542. *got_packet_ptr = 1;
  543. return 0;
  544. }
  545. AVCodec ff_alac_encoder = {
  546. .name = "alac",
  547. .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
  548. .type = AVMEDIA_TYPE_AUDIO,
  549. .id = AV_CODEC_ID_ALAC,
  550. .priv_data_size = sizeof(AlacEncodeContext),
  551. .init = alac_encode_init,
  552. .encode2 = alac_encode_frame,
  553. .close = alac_encode_close,
  554. .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
  555. .channel_layouts = ff_alac_channel_layouts,
  556. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
  557. AV_SAMPLE_FMT_S16P,
  558. AV_SAMPLE_FMT_NONE },
  559. };