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.

178 lines
6.6KB

  1. /*
  2. * SMPTE 302M encoder
  3. * Copyright (c) 2010 Google, Inc.
  4. * Copyright (c) 2013 Darryl Wallace <wallacdj@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. #include "put_bits.h"
  25. #define AES3_HEADER_LEN 4
  26. typedef struct S302MEncContext {
  27. uint8_t framing_index; /* Set for even channels on multiple of 192 samples */
  28. } S302MEncContext;
  29. static av_cold int s302m_encode_init(AVCodecContext *avctx)
  30. {
  31. S302MEncContext *s = avctx->priv_data;
  32. if (avctx->channels & 1 || avctx->channels > 8) {
  33. av_log(avctx, AV_LOG_ERROR,
  34. "Encoding %d channel(s) is not allowed. Only 2, 4, 6 and 8 channels are supported.\n",
  35. avctx->channels);
  36. return AVERROR(EINVAL);
  37. }
  38. switch (avctx->sample_fmt) {
  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 > 20) {
  44. if (avctx->bits_per_raw_sample > 24) {
  45. av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
  46. avctx->bits_per_raw_sample = 24;
  47. } else if (!avctx->bits_per_raw_sample) {
  48. avctx->bits_per_raw_sample = 24;
  49. } else if (avctx->bits_per_raw_sample <= 20) {
  50. avctx->bits_per_raw_sample = 20;
  51. }
  52. }
  53. avctx->frame_size = 0;
  54. avctx->bit_rate = 48000 * avctx->channels *
  55. (avctx->bits_per_raw_sample + 4);
  56. s->framing_index = 0;
  57. return 0;
  58. }
  59. static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt,
  60. const AVFrame *frame, int *got_packet_ptr)
  61. {
  62. S302MEncContext *s = avctx->priv_data;
  63. const int buf_size = AES3_HEADER_LEN +
  64. (frame->nb_samples *
  65. avctx->channels *
  66. (avctx->bits_per_raw_sample + 4)) / 8;
  67. int ret, c, channels;
  68. uint8_t *o;
  69. PutBitContext pb;
  70. if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)) < 0)
  71. return ret;
  72. o = avpkt->data;
  73. init_put_bits(&pb, o, buf_size * 8);
  74. put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);
  75. put_bits(&pb, 2, (avctx->channels - 2) >> 1); // number of channels
  76. put_bits(&pb, 8, 0); // channel ID
  77. put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit)
  78. put_bits(&pb, 4, 0); // alignments
  79. flush_put_bits(&pb);
  80. o += AES3_HEADER_LEN;
  81. if (avctx->bits_per_raw_sample == 24) {
  82. const uint32_t *samples = (uint32_t *)frame->data[0];
  83. for (c = 0; c < frame->nb_samples; c++) {
  84. uint8_t vucf = s->framing_index == 0 ? 0x10: 0;
  85. for (channels = 0; channels < avctx->channels; channels += 2) {
  86. o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];
  87. o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];
  88. o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];
  89. o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;
  90. o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];
  91. o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];
  92. o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];
  93. o += 7;
  94. samples += 2;
  95. }
  96. s->framing_index++;
  97. if (s->framing_index >= 192)
  98. s->framing_index = 0;
  99. }
  100. } else if (avctx->bits_per_raw_sample == 20) {
  101. const uint32_t *samples = (uint32_t *)frame->data[0];
  102. for (c = 0; c < frame->nb_samples; c++) {
  103. uint8_t vucf = s->framing_index == 0 ? 0x80: 0;
  104. for (channels = 0; channels < avctx->channels; channels += 2) {
  105. o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];
  106. o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];
  107. o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];
  108. o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];
  109. o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];
  110. o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];
  111. o += 6;
  112. samples += 2;
  113. }
  114. s->framing_index++;
  115. if (s->framing_index >= 192)
  116. s->framing_index = 0;
  117. }
  118. } else if (avctx->bits_per_raw_sample == 16) {
  119. const uint16_t *samples = (uint16_t *)frame->data[0];
  120. for (c = 0; c < frame->nb_samples; c++) {
  121. uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;
  122. for (channels = 0; channels < avctx->channels; channels += 2) {
  123. o[0] = ff_reverse[ samples[0] & 0xFF];
  124. o[1] = ff_reverse[(samples[0] & 0xFF00) >> 8];
  125. o[2] = ff_reverse[(samples[1] & 0x0F) << 4] | vucf;
  126. o[3] = ff_reverse[(samples[1] & 0x0FF0) >> 4];
  127. o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];
  128. o += 5;
  129. samples += 2;
  130. }
  131. s->framing_index++;
  132. if (s->framing_index >= 192)
  133. s->framing_index = 0;
  134. }
  135. }
  136. *got_packet_ptr = 1;
  137. return 0;
  138. }
  139. AVCodec ff_s302m_encoder = {
  140. .name = "s302m",
  141. .type = AVMEDIA_TYPE_AUDIO,
  142. .id = CODEC_ID_S302M,
  143. .priv_data_size = sizeof(S302MEncContext),
  144. .init = s302m_encode_init,
  145. .encode2 = s302m_encode2_frame,
  146. .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
  147. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
  148. AV_SAMPLE_FMT_S16,
  149. AV_SAMPLE_FMT_NONE },
  150. .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE | CODEC_CAP_EXPERIMENTAL,
  151. .supported_samplerates = (const int[]) { 48000, 0 },
  152. };