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.

308 lines
9.2KB

  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 "bitstream.h"
  23. #include "dsputil.h"
  24. #include "lpc.h"
  25. #define DEFAULT_FRAME_SIZE 4096
  26. #define DEFAULT_SAMPLE_SIZE 16
  27. #define MAX_CHANNELS 8
  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. typedef struct RiceContext {
  38. int history_mult;
  39. int initial_history;
  40. int k_modifier;
  41. int rice_modifier;
  42. } RiceContext;
  43. typedef struct LPCContext {
  44. int lpc_order;
  45. int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
  46. int lpc_quant;
  47. } LPCContext;
  48. typedef struct AlacEncodeContext {
  49. int compression_level;
  50. int max_coded_frame_size;
  51. int write_sample_size;
  52. int32_t sample_buf[MAX_CHANNELS][DEFAULT_FRAME_SIZE];
  53. int interlacing_shift;
  54. int interlacing_leftweight;
  55. PutBitContext pbctx;
  56. RiceContext rc;
  57. LPCContext lpc[MAX_CHANNELS];
  58. DSPContext dspctx;
  59. AVCodecContext *avctx;
  60. } AlacEncodeContext;
  61. static void init_sample_buffers(AlacEncodeContext *s, int16_t *input_samples)
  62. {
  63. int ch, i;
  64. for(ch=0;ch<s->avctx->channels;ch++) {
  65. int16_t *sptr = input_samples + ch;
  66. for(i=0;i<s->avctx->frame_size;i++) {
  67. s->sample_buf[ch][i] = *sptr;
  68. sptr += s->avctx->channels;
  69. }
  70. }
  71. }
  72. static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
  73. {
  74. int divisor, q, r;
  75. k = FFMIN(k, s->rc.k_modifier);
  76. divisor = (1<<k) - 1;
  77. q = x / divisor;
  78. r = x % divisor;
  79. if(q > 8) {
  80. // write escape code and sample value directly
  81. put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
  82. put_bits(&s->pbctx, write_sample_size, x);
  83. } else {
  84. if(q)
  85. put_bits(&s->pbctx, q, (1<<q) - 1);
  86. put_bits(&s->pbctx, 1, 0);
  87. if(k != 1) {
  88. if(r > 0)
  89. put_bits(&s->pbctx, k, r+1);
  90. else
  91. put_bits(&s->pbctx, k-1, 0);
  92. }
  93. }
  94. }
  95. static void write_frame_header(AlacEncodeContext *s, int is_verbatim)
  96. {
  97. put_bits(&s->pbctx, 3, s->avctx->channels-1); // No. of channels -1
  98. put_bits(&s->pbctx, 16, 0); // Seems to be zero
  99. put_bits(&s->pbctx, 1, 1); // Sample count is in the header
  100. put_bits(&s->pbctx, 2, 0); // FIXME: Wasted bytes field
  101. put_bits(&s->pbctx, 1, is_verbatim); // Audio block is verbatim
  102. put_bits(&s->pbctx, 32, s->avctx->frame_size); // No. of samples in the frame
  103. }
  104. static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
  105. {
  106. int i, best;
  107. int32_t lt, rt;
  108. uint64_t sum[4];
  109. uint64_t score[4];
  110. /* calculate sum of 2nd order residual for each channel */
  111. sum[0] = sum[1] = sum[2] = sum[3] = 0;
  112. for(i=2; i<n; i++) {
  113. lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
  114. rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
  115. sum[2] += FFABS((lt + rt) >> 1);
  116. sum[3] += FFABS(lt - rt);
  117. sum[0] += FFABS(lt);
  118. sum[1] += FFABS(rt);
  119. }
  120. /* calculate score for each mode */
  121. score[0] = sum[0] + sum[1];
  122. score[1] = sum[0] + sum[3];
  123. score[2] = sum[1] + sum[3];
  124. score[3] = sum[2] + sum[3];
  125. /* return mode with lowest score */
  126. best = 0;
  127. for(i=1; i<4; i++) {
  128. if(score[i] < score[best]) {
  129. best = i;
  130. }
  131. }
  132. static void write_compressed_frame(AlacEncodeContext *s)
  133. {
  134. int i, j;
  135. /* only simple mid/side decorrelation supported as of now */
  136. alac_stereo_decorrelation(s);
  137. put_bits(&s->pbctx, 8, s->interlacing_shift);
  138. put_bits(&s->pbctx, 8, s->interlacing_leftweight);
  139. for(i=0;i<s->avctx->channels;i++) {
  140. calc_predictor_params(s, i);
  141. put_bits(&s->pbctx, 4, 0); // prediction type : currently only type 0 has been RE'd
  142. put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant);
  143. put_bits(&s->pbctx, 3, s->rc.rice_modifier);
  144. put_bits(&s->pbctx, 5, s->lpc[i].lpc_order);
  145. // predictor coeff. table
  146. for(j=0;j<s->lpc[i].lpc_order;j++) {
  147. put_sbits(&s->pbctx, 16, s->lpc[i].lpc_coeff[j]);
  148. }
  149. }
  150. // apply lpc and entropy coding to audio samples
  151. for(i=0;i<s->avctx->channels;i++) {
  152. alac_linear_predictor(s, i);
  153. alac_entropy_coder(s);
  154. }
  155. }
  156. static av_cold int alac_encode_init(AVCodecContext *avctx)
  157. {
  158. AlacEncodeContext *s = avctx->priv_data;
  159. uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1);
  160. avctx->frame_size = DEFAULT_FRAME_SIZE;
  161. avctx->bits_per_sample = DEFAULT_SAMPLE_SIZE;
  162. if(avctx->sample_fmt != SAMPLE_FMT_S16) {
  163. av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
  164. return -1;
  165. }
  166. // Set default compression level
  167. if(avctx->compression_level == FF_COMPRESSION_DEFAULT)
  168. s->compression_level = 1;
  169. else
  170. s->compression_level = av_clip(avctx->compression_level, 0, 1);
  171. // Initialize default Rice parameters
  172. s->rc.history_mult = 40;
  173. s->rc.initial_history = 10;
  174. s->rc.k_modifier = 14;
  175. s->rc.rice_modifier = 4;
  176. s->max_coded_frame_size = (ALAC_FRAME_HEADER_SIZE + ALAC_FRAME_FOOTER_SIZE +
  177. avctx->frame_size*avctx->channels*avctx->bits_per_sample)>>3;
  178. s->write_sample_size = avctx->bits_per_sample + avctx->channels - 1; // FIXME: consider wasted_bytes
  179. AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
  180. AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
  181. AV_WB32(alac_extradata+12, avctx->frame_size);
  182. AV_WB8 (alac_extradata+17, avctx->bits_per_sample);
  183. AV_WB8 (alac_extradata+21, avctx->channels);
  184. AV_WB32(alac_extradata+24, s->max_coded_frame_size);
  185. AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_sample); // average bitrate
  186. AV_WB32(alac_extradata+32, avctx->sample_rate);
  187. // Set relevant extradata fields
  188. if(s->compression_level > 0) {
  189. AV_WB8(alac_extradata+18, s->rc.history_mult);
  190. AV_WB8(alac_extradata+19, s->rc.initial_history);
  191. AV_WB8(alac_extradata+20, s->rc.k_modifier);
  192. }
  193. avctx->extradata = alac_extradata;
  194. avctx->extradata_size = ALAC_EXTRADATA_SIZE;
  195. avctx->coded_frame = avcodec_alloc_frame();
  196. avctx->coded_frame->key_frame = 1;
  197. s->avctx = avctx;
  198. dsputil_init(&s->dspctx, avctx);
  199. return 0;
  200. }
  201. static int alac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
  202. int buf_size, void *data)
  203. {
  204. AlacEncodeContext *s = avctx->priv_data;
  205. PutBitContext *pb = &s->pbctx;
  206. int i, out_bytes, verbatim_flag = 0;
  207. if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
  208. av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n");
  209. return -1;
  210. }
  211. if(buf_size < 2*s->max_coded_frame_size) {
  212. av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n");
  213. return -1;
  214. }
  215. if((s->compression_level == 0) || verbatim_flag) {
  216. // Verbatim mode
  217. int16_t *samples = data;
  218. write_frame_header(s, 1);
  219. for(i=0; i<avctx->frame_size*avctx->channels; i++) {
  220. put_sbits(pb, 16, *samples++);
  221. }
  222. } else {
  223. init_sample_buffers(s, data);
  224. write_frame_header(s, 0);
  225. write_compressed_frame(s);
  226. }
  227. put_bits(pb, 3, 7);
  228. flush_put_bits(pb);
  229. out_bytes = put_bits_count(pb) >> 3;
  230. if(out_bytes > s->max_coded_frame_size) {
  231. /* frame too large. use verbatim mode */
  232. if(verbatim_flag || (s->compression_level == 0)) {
  233. /* still too large. must be an error. */
  234. av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
  235. return -1;
  236. }
  237. verbatim_flag = 1;
  238. goto verbatim;
  239. }
  240. return out_bytes;
  241. }
  242. static av_cold int alac_encode_close(AVCodecContext *avctx)
  243. {
  244. av_freep(&avctx->extradata);
  245. avctx->extradata_size = 0;
  246. av_freep(&avctx->coded_frame);
  247. return 0;
  248. }
  249. AVCodec alac_encoder = {
  250. "alac",
  251. CODEC_TYPE_AUDIO,
  252. CODEC_ID_ALAC,
  253. sizeof(AlacEncodeContext),
  254. alac_encode_init,
  255. alac_encode_frame,
  256. alac_encode_close,
  257. .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
  258. .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
  259. };