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.

233 lines
7.3KB

  1. /*
  2. * TTA (The Lossless True Audio) encoder
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #define BITSTREAM_WRITER_LE
  21. #include "ttadata.h"
  22. #include "avcodec.h"
  23. #include "put_bits.h"
  24. #include "internal.h"
  25. #include "libavutil/crc.h"
  26. typedef struct TTAEncContext {
  27. const AVCRC *crc_table;
  28. int bps;
  29. TTAChannel *ch_ctx;
  30. } TTAEncContext;
  31. static av_cold int tta_encode_init(AVCodecContext *avctx)
  32. {
  33. TTAEncContext *s = avctx->priv_data;
  34. s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  35. switch (avctx->sample_fmt) {
  36. case AV_SAMPLE_FMT_U8:
  37. avctx->bits_per_raw_sample = 8;
  38. break;
  39. case AV_SAMPLE_FMT_S16:
  40. avctx->bits_per_raw_sample = 16;
  41. break;
  42. case AV_SAMPLE_FMT_S32:
  43. if (avctx->bits_per_raw_sample > 24)
  44. av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
  45. avctx->bits_per_raw_sample = 24;
  46. }
  47. s->bps = avctx->bits_per_raw_sample >> 3;
  48. avctx->frame_size = 256 * avctx->sample_rate / 245;
  49. s->ch_ctx = av_malloc_array(avctx->channels, sizeof(*s->ch_ctx));
  50. if (!s->ch_ctx)
  51. return AVERROR(ENOMEM);
  52. return 0;
  53. }
  54. static inline void ttafilter_process(TTAFilter *c, int32_t *in)
  55. {
  56. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  57. if (c->error < 0) {
  58. qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
  59. qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
  60. } else if (c->error > 0) {
  61. qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
  62. qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
  63. }
  64. sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
  65. dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
  66. dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
  67. dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
  68. dx[4] = ((dl[4] >> 30) | 1);
  69. dx[5] = ((dl[5] >> 30) | 2) & ~1;
  70. dx[6] = ((dl[6] >> 30) | 2) & ~1;
  71. dx[7] = ((dl[7] >> 30) | 4) & ~3;
  72. dl[4] = -dl[5]; dl[5] = -dl[6];
  73. dl[6] = *in - dl[7]; dl[7] = *in;
  74. dl[5] += dl[6]; dl[4] += dl[5];
  75. *in -= (sum >> c->shift);
  76. c->error = *in;
  77. }
  78. static int32_t get_sample(const AVFrame *frame, int sample,
  79. enum AVSampleFormat format)
  80. {
  81. int32_t ret;
  82. if (format == AV_SAMPLE_FMT_U8) {
  83. ret = frame->data[0][sample] - 0x80;
  84. } else if (format == AV_SAMPLE_FMT_S16) {
  85. const int16_t *ptr = (const int16_t *)frame->data[0];
  86. ret = ptr[sample];
  87. } else {
  88. const int32_t *ptr = (const int32_t *)frame->data[0];
  89. ret = ptr[sample] >> 8;
  90. }
  91. return ret;
  92. }
  93. static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  94. const AVFrame *frame, int *got_packet_ptr)
  95. {
  96. TTAEncContext *s = avctx->priv_data;
  97. PutBitContext pb;
  98. int ret, i, out_bytes, cur_chan = 0, res = 0, samples = 0;
  99. if ((ret = ff_alloc_packet2(avctx, avpkt, frame->nb_samples * 2 * avctx->channels * s->bps, 0)) < 0)
  100. return ret;
  101. init_put_bits(&pb, avpkt->data, avpkt->size);
  102. // init per channel states
  103. for (i = 0; i < avctx->channels; i++) {
  104. s->ch_ctx[i].predictor = 0;
  105. ff_tta_filter_init(&s->ch_ctx[i].filter, ff_tta_filter_configs[s->bps - 1]);
  106. ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
  107. }
  108. for (i = 0; i < frame->nb_samples * avctx->channels; i++) {
  109. TTAChannel *c = &s->ch_ctx[cur_chan];
  110. TTAFilter *filter = &c->filter;
  111. TTARice *rice = &c->rice;
  112. uint32_t k, unary, outval;
  113. int32_t value, temp;
  114. value = get_sample(frame, samples++, avctx->sample_fmt);
  115. if (avctx->channels > 1) {
  116. if (cur_chan < avctx->channels - 1)
  117. value = res = get_sample(frame, samples, avctx->sample_fmt) - value;
  118. else
  119. value -= res / 2;
  120. }
  121. temp = value;
  122. #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
  123. switch (s->bps) {
  124. case 1: value -= PRED(c->predictor, 4); break;
  125. case 2:
  126. case 3: value -= PRED(c->predictor, 5); break;
  127. }
  128. c->predictor = temp;
  129. ttafilter_process(filter, &value);
  130. outval = (value > 0) ? (value << 1) - 1: -value << 1;
  131. k = rice->k0;
  132. rice->sum0 += outval - (rice->sum0 >> 4);
  133. if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
  134. rice->k0--;
  135. else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
  136. rice->k0++;
  137. if (outval >= ff_tta_shift_1[k]) {
  138. outval -= ff_tta_shift_1[k];
  139. k = rice->k1;
  140. rice->sum1 += outval - (rice->sum1 >> 4);
  141. if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
  142. rice->k1--;
  143. else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
  144. rice->k1++;
  145. unary = 1 + (outval >> k);
  146. do {
  147. if (unary > 31) {
  148. put_bits(&pb, 31, 0x7FFFFFFF);
  149. unary -= 31;
  150. } else {
  151. put_bits(&pb, unary, (1 << unary) - 1);
  152. unary = 0;
  153. }
  154. } while (unary);
  155. }
  156. put_bits(&pb, 1, 0);
  157. if (k)
  158. put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
  159. if (cur_chan < avctx->channels - 1)
  160. cur_chan++;
  161. else
  162. cur_chan = 0;
  163. }
  164. flush_put_bits(&pb);
  165. out_bytes = put_bits_count(&pb) >> 3;
  166. put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
  167. flush_put_bits(&pb);
  168. avpkt->pts = frame->pts;
  169. avpkt->size = out_bytes + 4;
  170. avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
  171. *got_packet_ptr = 1;
  172. return 0;
  173. }
  174. static av_cold int tta_encode_close(AVCodecContext *avctx)
  175. {
  176. TTAEncContext *s = avctx->priv_data;
  177. av_freep(&s->ch_ctx);
  178. return 0;
  179. }
  180. AVCodec ff_tta_encoder = {
  181. .name = "tta",
  182. .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
  183. .type = AVMEDIA_TYPE_AUDIO,
  184. .id = AV_CODEC_ID_TTA,
  185. .priv_data_size = sizeof(TTAEncContext),
  186. .init = tta_encode_init,
  187. .close = tta_encode_close,
  188. .encode2 = tta_encode_frame,
  189. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_LOSSLESS,
  190. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8,
  191. AV_SAMPLE_FMT_S16,
  192. AV_SAMPLE_FMT_S32,
  193. AV_SAMPLE_FMT_NONE },
  194. };