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.

662 lines
21KB

  1. /*
  2. * ALAC audio encoder
  3. * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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[2][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. int32_t *residual = s->predictor_buf[ch];
  228. if (lpc.lpc_order == 31) {
  229. residual[0] = s->sample_buf[ch][0];
  230. for (i = 1; i < s->frame_size; i++) {
  231. residual[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. // generate warm-up samples
  240. residual[0] = samples[0];
  241. for (i = 1; i <= lpc.lpc_order; i++)
  242. residual[i] = sign_extend(samples[i] - samples[i-1], s->write_sample_size);
  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, int ch)
  274. {
  275. unsigned int history = s->rc.initial_history;
  276. int sign_modifier = 0, i, k;
  277. int32_t *samples = s->predictor_buf[ch];
  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. // extract extra bits if needed
  339. if (s->extra_bits) {
  340. uint32_t mask = (1 << s->extra_bits) - 1;
  341. for (j = 0; j < channels; j++) {
  342. int32_t *extra = s->predictor_buf[j];
  343. int32_t *smp = s->sample_buf[j];
  344. for (i = 0; i < s->frame_size; i++) {
  345. extra[i] = smp[i] & mask;
  346. smp[i] >>= s->extra_bits;
  347. }
  348. }
  349. }
  350. if (channels == 2)
  351. alac_stereo_decorrelation(s);
  352. else
  353. s->interlacing_shift = s->interlacing_leftweight = 0;
  354. put_bits(pb, 8, s->interlacing_shift);
  355. put_bits(pb, 8, s->interlacing_leftweight);
  356. for (i = 0; i < channels; i++) {
  357. calc_predictor_params(s, i);
  358. put_bits(pb, 4, prediction_type);
  359. put_bits(pb, 4, s->lpc[i].lpc_quant);
  360. put_bits(pb, 3, s->rc.rice_modifier);
  361. put_bits(pb, 5, s->lpc[i].lpc_order);
  362. // predictor coeff. table
  363. for (j = 0; j < s->lpc[i].lpc_order; j++)
  364. put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
  365. }
  366. // write extra bits if needed
  367. if (s->extra_bits) {
  368. for (i = 0; i < s->frame_size; i++) {
  369. for (j = 0; j < channels; j++) {
  370. put_bits(pb, s->extra_bits, s->predictor_buf[j][i]);
  371. }
  372. }
  373. }
  374. // apply lpc and entropy coding to audio samples
  375. for (i = 0; i < channels; i++) {
  376. alac_linear_predictor(s, i);
  377. // TODO: determine when this will actually help. for now it's not used.
  378. if (prediction_type == 15) {
  379. // 2nd pass 1st order filter
  380. int32_t *residual = s->predictor_buf[i];
  381. for (j = s->frame_size - 1; j > 0; j--)
  382. residual[j] -= residual[j - 1];
  383. }
  384. alac_entropy_coder(s, i);
  385. }
  386. }
  387. }
  388. static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
  389. uint8_t * const *samples)
  390. {
  391. PutBitContext *pb = &s->pbctx;
  392. const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
  393. const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
  394. int ch, element, sce, cpe;
  395. init_put_bits(pb, avpkt->data, avpkt->size);
  396. ch = element = sce = cpe = 0;
  397. while (ch < s->avctx->channels) {
  398. if (ch_elements[element] == TYPE_CPE) {
  399. write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
  400. samples[ch_map[ch + 1]]);
  401. cpe++;
  402. ch += 2;
  403. } else {
  404. write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
  405. sce++;
  406. ch++;
  407. }
  408. element++;
  409. }
  410. put_bits(pb, 3, TYPE_END);
  411. flush_put_bits(pb);
  412. return put_bits_count(pb) >> 3;
  413. }
  414. static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
  415. {
  416. int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
  417. return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
  418. }
  419. static av_cold int alac_encode_close(AVCodecContext *avctx)
  420. {
  421. AlacEncodeContext *s = avctx->priv_data;
  422. ff_lpc_end(&s->lpc_ctx);
  423. av_freep(&avctx->extradata);
  424. avctx->extradata_size = 0;
  425. return 0;
  426. }
  427. static av_cold int alac_encode_init(AVCodecContext *avctx)
  428. {
  429. AlacEncodeContext *s = avctx->priv_data;
  430. int ret;
  431. uint8_t *alac_extradata;
  432. avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
  433. if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
  434. if (avctx->bits_per_raw_sample != 24)
  435. av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
  436. avctx->bits_per_raw_sample = 24;
  437. } else {
  438. avctx->bits_per_raw_sample = 16;
  439. s->extra_bits = 0;
  440. }
  441. // Set default compression level
  442. if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
  443. s->compression_level = 2;
  444. else
  445. s->compression_level = av_clip(avctx->compression_level, 0, 2);
  446. // Initialize default Rice parameters
  447. s->rc.history_mult = 40;
  448. s->rc.initial_history = 10;
  449. s->rc.k_modifier = 14;
  450. s->rc.rice_modifier = 4;
  451. s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
  452. avctx->channels,
  453. avctx->bits_per_raw_sample);
  454. avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
  455. if (!avctx->extradata) {
  456. ret = AVERROR(ENOMEM);
  457. goto error;
  458. }
  459. avctx->extradata_size = ALAC_EXTRADATA_SIZE;
  460. alac_extradata = avctx->extradata;
  461. AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
  462. AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
  463. AV_WB32(alac_extradata+12, avctx->frame_size);
  464. AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
  465. AV_WB8 (alac_extradata+21, avctx->channels);
  466. AV_WB32(alac_extradata+24, s->max_coded_frame_size);
  467. AV_WB32(alac_extradata+28,
  468. avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
  469. AV_WB32(alac_extradata+32, avctx->sample_rate);
  470. // Set relevant extradata fields
  471. if (s->compression_level > 0) {
  472. AV_WB8(alac_extradata+18, s->rc.history_mult);
  473. AV_WB8(alac_extradata+19, s->rc.initial_history);
  474. AV_WB8(alac_extradata+20, s->rc.k_modifier);
  475. }
  476. s->min_prediction_order = DEFAULT_MIN_PRED_ORDER;
  477. if (avctx->min_prediction_order >= 0) {
  478. if (avctx->min_prediction_order < MIN_LPC_ORDER ||
  479. avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
  480. av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
  481. avctx->min_prediction_order);
  482. ret = AVERROR(EINVAL);
  483. goto error;
  484. }
  485. s->min_prediction_order = avctx->min_prediction_order;
  486. }
  487. s->max_prediction_order = DEFAULT_MAX_PRED_ORDER;
  488. if (avctx->max_prediction_order >= 0) {
  489. if (avctx->max_prediction_order < MIN_LPC_ORDER ||
  490. avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
  491. av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
  492. avctx->max_prediction_order);
  493. ret = AVERROR(EINVAL);
  494. goto error;
  495. }
  496. s->max_prediction_order = avctx->max_prediction_order;
  497. }
  498. if (s->max_prediction_order < s->min_prediction_order) {
  499. av_log(avctx, AV_LOG_ERROR,
  500. "invalid prediction orders: min=%d max=%d\n",
  501. s->min_prediction_order, s->max_prediction_order);
  502. ret = AVERROR(EINVAL);
  503. goto error;
  504. }
  505. s->avctx = avctx;
  506. if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
  507. s->max_prediction_order,
  508. FF_LPC_TYPE_LEVINSON)) < 0) {
  509. goto error;
  510. }
  511. return 0;
  512. error:
  513. alac_encode_close(avctx);
  514. return ret;
  515. }
  516. static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  517. const AVFrame *frame, int *got_packet_ptr)
  518. {
  519. AlacEncodeContext *s = avctx->priv_data;
  520. int out_bytes, max_frame_size, ret;
  521. s->frame_size = frame->nb_samples;
  522. if (frame->nb_samples < DEFAULT_FRAME_SIZE)
  523. max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
  524. avctx->bits_per_raw_sample);
  525. else
  526. max_frame_size = s->max_coded_frame_size;
  527. if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * max_frame_size, 0)) < 0)
  528. return ret;
  529. /* use verbatim mode for compression_level 0 */
  530. if (s->compression_level) {
  531. s->verbatim = 0;
  532. s->extra_bits = avctx->bits_per_raw_sample - 16;
  533. } else {
  534. s->verbatim = 1;
  535. s->extra_bits = 0;
  536. }
  537. out_bytes = write_frame(s, avpkt, frame->extended_data);
  538. if (out_bytes > max_frame_size) {
  539. /* frame too large. use verbatim mode */
  540. s->verbatim = 1;
  541. s->extra_bits = 0;
  542. out_bytes = write_frame(s, avpkt, frame->extended_data);
  543. }
  544. avpkt->size = out_bytes;
  545. *got_packet_ptr = 1;
  546. return 0;
  547. }
  548. AVCodec ff_alac_encoder = {
  549. .name = "alac",
  550. .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
  551. .type = AVMEDIA_TYPE_AUDIO,
  552. .id = AV_CODEC_ID_ALAC,
  553. .priv_data_size = sizeof(AlacEncodeContext),
  554. .init = alac_encode_init,
  555. .encode2 = alac_encode_frame,
  556. .close = alac_encode_close,
  557. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
  558. .channel_layouts = ff_alac_channel_layouts,
  559. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
  560. AV_SAMPLE_FMT_S16P,
  561. AV_SAMPLE_FMT_NONE },
  562. };