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.

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