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.

674 lines
22KB

  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 "libavutil/opt.h"
  22. #include "avcodec.h"
  23. #include "put_bits.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. const AVClass *class;
  55. AVCodecContext *avctx;
  56. int frame_size; /**< current frame size */
  57. int verbatim; /**< current frame verbatim mode flag */
  58. int compression_level;
  59. int min_prediction_order;
  60. int max_prediction_order;
  61. int max_coded_frame_size;
  62. int write_sample_size;
  63. int extra_bits;
  64. int32_t sample_buf[2][DEFAULT_FRAME_SIZE];
  65. int32_t predictor_buf[DEFAULT_FRAME_SIZE];
  66. int interlacing_shift;
  67. int interlacing_leftweight;
  68. PutBitContext pbctx;
  69. RiceContext rc;
  70. AlacLPCContext lpc[2];
  71. LPCContext lpc_ctx;
  72. } AlacEncodeContext;
  73. static void init_sample_buffers(AlacEncodeContext *s, int channels,
  74. const uint8_t *samples[2])
  75. {
  76. int ch, i;
  77. int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
  78. s->avctx->bits_per_raw_sample;
  79. #define COPY_SAMPLES(type) do { \
  80. for (ch = 0; ch < channels; ch++) { \
  81. int32_t *bptr = s->sample_buf[ch]; \
  82. const type *sptr = (const type *)samples[ch]; \
  83. for (i = 0; i < s->frame_size; i++) \
  84. bptr[i] = sptr[i] >> shift; \
  85. } \
  86. } while (0)
  87. if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P)
  88. COPY_SAMPLES(int32_t);
  89. else
  90. COPY_SAMPLES(int16_t);
  91. }
  92. static void encode_scalar(AlacEncodeContext *s, int x,
  93. int k, int write_sample_size)
  94. {
  95. int divisor, q, r;
  96. k = FFMIN(k, s->rc.k_modifier);
  97. divisor = (1<<k) - 1;
  98. q = x / divisor;
  99. r = x % divisor;
  100. if (q > 8) {
  101. // write escape code and sample value directly
  102. put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
  103. put_bits(&s->pbctx, write_sample_size, x);
  104. } else {
  105. if (q)
  106. put_bits(&s->pbctx, q, (1<<q) - 1);
  107. put_bits(&s->pbctx, 1, 0);
  108. if (k != 1) {
  109. if (r > 0)
  110. put_bits(&s->pbctx, k, r+1);
  111. else
  112. put_bits(&s->pbctx, k-1, 0);
  113. }
  114. }
  115. }
  116. static void write_element_header(AlacEncodeContext *s,
  117. enum AlacRawDataBlockType element,
  118. int instance)
  119. {
  120. int encode_fs = 0;
  121. if (s->frame_size < DEFAULT_FRAME_SIZE)
  122. encode_fs = 1;
  123. put_bits(&s->pbctx, 3, element); // element type
  124. put_bits(&s->pbctx, 4, instance); // element instance
  125. put_bits(&s->pbctx, 12, 0); // unused header bits
  126. put_bits(&s->pbctx, 1, encode_fs); // Sample count is in the header
  127. put_bits(&s->pbctx, 2, s->extra_bits >> 3); // Extra bytes (for 24-bit)
  128. put_bits(&s->pbctx, 1, s->verbatim); // Audio block is verbatim
  129. if (encode_fs)
  130. put_bits32(&s->pbctx, s->frame_size); // No. of samples in the frame
  131. }
  132. static void calc_predictor_params(AlacEncodeContext *s, int ch)
  133. {
  134. int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
  135. int shift[MAX_LPC_ORDER];
  136. int opt_order;
  137. if (s->compression_level == 1) {
  138. s->lpc[ch].lpc_order = 6;
  139. s->lpc[ch].lpc_quant = 6;
  140. s->lpc[ch].lpc_coeff[0] = 160;
  141. s->lpc[ch].lpc_coeff[1] = -190;
  142. s->lpc[ch].lpc_coeff[2] = 170;
  143. s->lpc[ch].lpc_coeff[3] = -130;
  144. s->lpc[ch].lpc_coeff[4] = 80;
  145. s->lpc[ch].lpc_coeff[5] = -25;
  146. } else {
  147. opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
  148. s->frame_size,
  149. s->min_prediction_order,
  150. s->max_prediction_order,
  151. ALAC_MAX_LPC_PRECISION, coefs, shift,
  152. FF_LPC_TYPE_LEVINSON, 0,
  153. ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1);
  154. s->lpc[ch].lpc_order = opt_order;
  155. s->lpc[ch].lpc_quant = shift[opt_order-1];
  156. memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
  157. }
  158. }
  159. static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
  160. {
  161. int i, best;
  162. int32_t lt, rt;
  163. uint64_t sum[4];
  164. uint64_t score[4];
  165. /* calculate sum of 2nd order residual for each channel */
  166. sum[0] = sum[1] = sum[2] = sum[3] = 0;
  167. for (i = 2; i < n; i++) {
  168. lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
  169. rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
  170. sum[2] += FFABS((lt + rt) >> 1);
  171. sum[3] += FFABS(lt - rt);
  172. sum[0] += FFABS(lt);
  173. sum[1] += FFABS(rt);
  174. }
  175. /* calculate score for each mode */
  176. score[0] = sum[0] + sum[1];
  177. score[1] = sum[0] + sum[3];
  178. score[2] = sum[1] + sum[3];
  179. score[3] = sum[2] + sum[3];
  180. /* return mode with lowest score */
  181. best = 0;
  182. for (i = 1; i < 4; i++) {
  183. if (score[i] < score[best])
  184. best = i;
  185. }
  186. return best;
  187. }
  188. static void alac_stereo_decorrelation(AlacEncodeContext *s)
  189. {
  190. int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
  191. int i, mode, n = s->frame_size;
  192. int32_t tmp;
  193. mode = estimate_stereo_mode(left, right, n);
  194. switch (mode) {
  195. case ALAC_CHMODE_LEFT_RIGHT:
  196. s->interlacing_leftweight = 0;
  197. s->interlacing_shift = 0;
  198. break;
  199. case ALAC_CHMODE_LEFT_SIDE:
  200. for (i = 0; i < n; i++)
  201. right[i] = left[i] - right[i];
  202. s->interlacing_leftweight = 1;
  203. s->interlacing_shift = 0;
  204. break;
  205. case ALAC_CHMODE_RIGHT_SIDE:
  206. for (i = 0; i < n; i++) {
  207. tmp = right[i];
  208. right[i] = left[i] - right[i];
  209. left[i] = tmp + (right[i] >> 31);
  210. }
  211. s->interlacing_leftweight = 1;
  212. s->interlacing_shift = 31;
  213. break;
  214. default:
  215. for (i = 0; i < n; i++) {
  216. tmp = left[i];
  217. left[i] = (tmp + right[i]) >> 1;
  218. right[i] = tmp - right[i];
  219. }
  220. s->interlacing_leftweight = 1;
  221. s->interlacing_shift = 1;
  222. break;
  223. }
  224. }
  225. static void alac_linear_predictor(AlacEncodeContext *s, int ch)
  226. {
  227. int i;
  228. AlacLPCContext lpc = s->lpc[ch];
  229. if (lpc.lpc_order == 31) {
  230. s->predictor_buf[0] = s->sample_buf[ch][0];
  231. for (i = 1; i < s->frame_size; i++) {
  232. s->predictor_buf[i] = s->sample_buf[ch][i ] -
  233. s->sample_buf[ch][i - 1];
  234. }
  235. return;
  236. }
  237. // generalised linear predictor
  238. if (lpc.lpc_order > 0) {
  239. int32_t *samples = s->sample_buf[ch];
  240. int32_t *residual = s->predictor_buf;
  241. // generate warm-up samples
  242. residual[0] = samples[0];
  243. for (i = 1; i <= lpc.lpc_order; i++)
  244. residual[i] = samples[i] - samples[i-1];
  245. // perform lpc on remaining samples
  246. for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
  247. int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
  248. for (j = 0; j < lpc.lpc_order; j++) {
  249. sum += (samples[lpc.lpc_order-j] - samples[0]) *
  250. lpc.lpc_coeff[j];
  251. }
  252. sum >>= lpc.lpc_quant;
  253. sum += samples[0];
  254. residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
  255. s->write_sample_size);
  256. res_val = residual[i];
  257. if (res_val) {
  258. int index = lpc.lpc_order - 1;
  259. int neg = (res_val < 0);
  260. while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
  261. int val = samples[0] - samples[lpc.lpc_order - index];
  262. int sign = (val ? FFSIGN(val) : 0);
  263. if (neg)
  264. sign *= -1;
  265. lpc.lpc_coeff[index] -= sign;
  266. val *= sign;
  267. res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
  268. index--;
  269. }
  270. }
  271. samples++;
  272. }
  273. }
  274. }
  275. static void alac_entropy_coder(AlacEncodeContext *s)
  276. {
  277. unsigned int history = s->rc.initial_history;
  278. int sign_modifier = 0, i, k;
  279. int32_t *samples = s->predictor_buf;
  280. for (i = 0; i < s->frame_size;) {
  281. int x;
  282. k = av_log2((history >> 9) + 3);
  283. x = -2 * (*samples) -1;
  284. x ^= x >> 31;
  285. samples++;
  286. i++;
  287. encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
  288. history += x * s->rc.history_mult -
  289. ((history * s->rc.history_mult) >> 9);
  290. sign_modifier = 0;
  291. if (x > 0xFFFF)
  292. history = 0xFFFF;
  293. if (history < 128 && i < s->frame_size) {
  294. unsigned int block_size = 0;
  295. k = 7 - av_log2(history) + ((history + 16) >> 6);
  296. while (*samples == 0 && i < s->frame_size) {
  297. samples++;
  298. i++;
  299. block_size++;
  300. }
  301. encode_scalar(s, block_size, k, 16);
  302. sign_modifier = (block_size <= 0xFFFF);
  303. history = 0;
  304. }
  305. }
  306. }
  307. static void write_element(AlacEncodeContext *s,
  308. enum AlacRawDataBlockType element, int instance,
  309. const uint8_t *samples0, const uint8_t *samples1)
  310. {
  311. const uint8_t *samples[2] = { samples0, samples1 };
  312. int i, j, channels;
  313. int prediction_type = 0;
  314. PutBitContext *pb = &s->pbctx;
  315. channels = element == TYPE_CPE ? 2 : 1;
  316. if (s->verbatim) {
  317. write_element_header(s, element, instance);
  318. /* samples are channel-interleaved in verbatim mode */
  319. if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
  320. int shift = 32 - s->avctx->bits_per_raw_sample;
  321. const int32_t *samples_s32[2] = { (const int32_t *)samples0,
  322. (const int32_t *)samples1 };
  323. for (i = 0; i < s->frame_size; i++)
  324. for (j = 0; j < channels; j++)
  325. put_sbits(pb, s->avctx->bits_per_raw_sample,
  326. samples_s32[j][i] >> shift);
  327. } else {
  328. const int16_t *samples_s16[2] = { (const int16_t *)samples0,
  329. (const int16_t *)samples1 };
  330. for (i = 0; i < s->frame_size; i++)
  331. for (j = 0; j < channels; j++)
  332. put_sbits(pb, s->avctx->bits_per_raw_sample,
  333. samples_s16[j][i]);
  334. }
  335. } else {
  336. s->write_sample_size = s->avctx->bits_per_raw_sample - s->extra_bits +
  337. channels - 1;
  338. init_sample_buffers(s, channels, samples);
  339. write_element_header(s, element, instance);
  340. if (channels == 2)
  341. alac_stereo_decorrelation(s);
  342. else
  343. s->interlacing_shift = s->interlacing_leftweight = 0;
  344. put_bits(pb, 8, s->interlacing_shift);
  345. put_bits(pb, 8, s->interlacing_leftweight);
  346. for (i = 0; i < channels; i++) {
  347. calc_predictor_params(s, i);
  348. put_bits(pb, 4, prediction_type);
  349. put_bits(pb, 4, s->lpc[i].lpc_quant);
  350. put_bits(pb, 3, s->rc.rice_modifier);
  351. put_bits(pb, 5, s->lpc[i].lpc_order);
  352. // predictor coeff. table
  353. for (j = 0; j < s->lpc[i].lpc_order; j++)
  354. put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
  355. }
  356. // write extra bits if needed
  357. if (s->extra_bits) {
  358. uint32_t mask = (1 << s->extra_bits) - 1;
  359. for (i = 0; i < s->frame_size; i++) {
  360. for (j = 0; j < channels; j++) {
  361. put_bits(pb, s->extra_bits, s->sample_buf[j][i] & mask);
  362. s->sample_buf[j][i] >>= s->extra_bits;
  363. }
  364. }
  365. }
  366. // apply lpc and entropy coding to audio samples
  367. for (i = 0; i < channels; i++) {
  368. alac_linear_predictor(s, i);
  369. // TODO: determine when this will actually help. for now it's not used.
  370. if (prediction_type == 15) {
  371. // 2nd pass 1st order filter
  372. for (j = s->frame_size - 1; j > 0; j--)
  373. s->predictor_buf[j] -= s->predictor_buf[j - 1];
  374. }
  375. alac_entropy_coder(s);
  376. }
  377. }
  378. }
  379. static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
  380. uint8_t * const *samples)
  381. {
  382. PutBitContext *pb = &s->pbctx;
  383. const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
  384. const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
  385. int ch, element, sce, cpe;
  386. init_put_bits(pb, avpkt->data, avpkt->size);
  387. ch = element = sce = cpe = 0;
  388. while (ch < s->avctx->channels) {
  389. if (ch_elements[element] == TYPE_CPE) {
  390. write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
  391. samples[ch_map[ch + 1]]);
  392. cpe++;
  393. ch += 2;
  394. } else {
  395. write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
  396. sce++;
  397. ch++;
  398. }
  399. element++;
  400. }
  401. put_bits(pb, 3, TYPE_END);
  402. flush_put_bits(pb);
  403. return put_bits_count(pb) >> 3;
  404. }
  405. static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
  406. {
  407. int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
  408. return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
  409. }
  410. static av_cold int alac_encode_close(AVCodecContext *avctx)
  411. {
  412. AlacEncodeContext *s = avctx->priv_data;
  413. ff_lpc_end(&s->lpc_ctx);
  414. av_freep(&avctx->extradata);
  415. avctx->extradata_size = 0;
  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 + AV_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. #if FF_API_PRIVATE_OPT
  468. FF_DISABLE_DEPRECATION_WARNINGS
  469. if (avctx->min_prediction_order >= 0) {
  470. if (avctx->min_prediction_order < MIN_LPC_ORDER ||
  471. avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
  472. av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
  473. avctx->min_prediction_order);
  474. ret = AVERROR(EINVAL);
  475. goto error;
  476. }
  477. s->min_prediction_order = avctx->min_prediction_order;
  478. }
  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. FF_ENABLE_DEPRECATION_WARNINGS
  490. #endif
  491. if (s->max_prediction_order < s->min_prediction_order) {
  492. av_log(avctx, AV_LOG_ERROR,
  493. "invalid prediction orders: min=%d max=%d\n",
  494. s->min_prediction_order, s->max_prediction_order);
  495. ret = AVERROR(EINVAL);
  496. goto error;
  497. }
  498. s->avctx = avctx;
  499. if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
  500. s->max_prediction_order,
  501. FF_LPC_TYPE_LEVINSON)) < 0) {
  502. goto error;
  503. }
  504. return 0;
  505. error:
  506. alac_encode_close(avctx);
  507. return ret;
  508. }
  509. static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  510. const AVFrame *frame, int *got_packet_ptr)
  511. {
  512. AlacEncodeContext *s = avctx->priv_data;
  513. int out_bytes, max_frame_size, ret;
  514. s->frame_size = frame->nb_samples;
  515. if (frame->nb_samples < DEFAULT_FRAME_SIZE)
  516. max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
  517. avctx->bits_per_raw_sample);
  518. else
  519. max_frame_size = s->max_coded_frame_size;
  520. if ((ret = ff_alloc_packet(avpkt, 2 * max_frame_size))) {
  521. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  522. return ret;
  523. }
  524. /* use verbatim mode for compression_level 0 */
  525. if (s->compression_level) {
  526. s->verbatim = 0;
  527. s->extra_bits = avctx->bits_per_raw_sample - 16;
  528. } else {
  529. s->verbatim = 1;
  530. s->extra_bits = 0;
  531. }
  532. out_bytes = write_frame(s, avpkt, frame->extended_data);
  533. if (out_bytes > max_frame_size) {
  534. /* frame too large. use verbatim mode */
  535. s->verbatim = 1;
  536. s->extra_bits = 0;
  537. out_bytes = write_frame(s, avpkt, frame->extended_data);
  538. }
  539. avpkt->size = out_bytes;
  540. *got_packet_ptr = 1;
  541. return 0;
  542. }
  543. #define OFFSET(x) offsetof(AlacEncodeContext, x)
  544. #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  545. static const AVOption options[] = {
  546. { "min_prediction_order", NULL, OFFSET(min_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MIN_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
  547. { "max_prediction_order", NULL, OFFSET(max_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MAX_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
  548. { NULL },
  549. };
  550. static const AVClass alacenc_class = {
  551. .class_name = "alacenc",
  552. .item_name = av_default_item_name,
  553. .option = options,
  554. .version = LIBAVUTIL_VERSION_INT,
  555. };
  556. AVCodec ff_alac_encoder = {
  557. .name = "alac",
  558. .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
  559. .type = AVMEDIA_TYPE_AUDIO,
  560. .id = AV_CODEC_ID_ALAC,
  561. .priv_data_size = sizeof(AlacEncodeContext),
  562. .priv_class = &alacenc_class,
  563. .init = alac_encode_init,
  564. .encode2 = alac_encode_frame,
  565. .close = alac_encode_close,
  566. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
  567. .channel_layouts = ff_alac_channel_layouts,
  568. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
  569. AV_SAMPLE_FMT_S16P,
  570. AV_SAMPLE_FMT_NONE },
  571. };