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.

179 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 "mathops.h"
  25. #include "put_bits.h"
  26. #define AES3_HEADER_LEN 4
  27. typedef struct S302MEncContext {
  28. uint8_t framing_index; /* Set for even channels on multiple of 192 samples */
  29. } S302MEncContext;
  30. static av_cold int s302m_encode_init(AVCodecContext *avctx)
  31. {
  32. S302MEncContext *s = avctx->priv_data;
  33. if (avctx->channels & 1 || avctx->channels > 8) {
  34. av_log(avctx, AV_LOG_ERROR,
  35. "Encoding %d channel(s) is not allowed. Only 2, 4, 6 and 8 channels are supported.\n",
  36. avctx->channels);
  37. return AVERROR(EINVAL);
  38. }
  39. switch (avctx->sample_fmt) {
  40. case AV_SAMPLE_FMT_S16:
  41. avctx->bits_per_raw_sample = 16;
  42. break;
  43. case AV_SAMPLE_FMT_S32:
  44. if (avctx->bits_per_raw_sample > 20) {
  45. if (avctx->bits_per_raw_sample > 24)
  46. av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
  47. avctx->bits_per_raw_sample = 24;
  48. } else if (!avctx->bits_per_raw_sample) {
  49. avctx->bits_per_raw_sample = 24;
  50. } else if (avctx->bits_per_raw_sample <= 20) {
  51. avctx->bits_per_raw_sample = 20;
  52. }
  53. }
  54. avctx->frame_size = 0;
  55. avctx->bit_rate = 48000 * avctx->channels *
  56. (avctx->bits_per_raw_sample + 4);
  57. s->framing_index = 0;
  58. return 0;
  59. }
  60. static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt,
  61. const AVFrame *frame, int *got_packet_ptr)
  62. {
  63. S302MEncContext *s = avctx->priv_data;
  64. const int buf_size = AES3_HEADER_LEN +
  65. (frame->nb_samples *
  66. avctx->channels *
  67. (avctx->bits_per_raw_sample + 4)) / 8;
  68. int ret, c, channels;
  69. uint8_t *o;
  70. PutBitContext pb;
  71. if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size, 0)) < 0)
  72. return ret;
  73. o = avpkt->data;
  74. init_put_bits(&pb, o, buf_size);
  75. put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);
  76. put_bits(&pb, 2, (avctx->channels - 2) >> 1); // number of channels
  77. put_bits(&pb, 8, 0); // channel ID
  78. put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit)
  79. put_bits(&pb, 4, 0); // alignments
  80. flush_put_bits(&pb);
  81. o += AES3_HEADER_LEN;
  82. if (avctx->bits_per_raw_sample == 24) {
  83. const uint32_t *samples = (uint32_t *)frame->data[0];
  84. for (c = 0; c < frame->nb_samples; c++) {
  85. uint8_t vucf = s->framing_index == 0 ? 0x10: 0;
  86. for (channels = 0; channels < avctx->channels; channels += 2) {
  87. o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];
  88. o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];
  89. o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];
  90. o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;
  91. o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];
  92. o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];
  93. o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];
  94. o += 7;
  95. samples += 2;
  96. }
  97. s->framing_index++;
  98. if (s->framing_index >= 192)
  99. s->framing_index = 0;
  100. }
  101. } else if (avctx->bits_per_raw_sample == 20) {
  102. const uint32_t *samples = (uint32_t *)frame->data[0];
  103. for (c = 0; c < frame->nb_samples; c++) {
  104. uint8_t vucf = s->framing_index == 0 ? 0x80: 0;
  105. for (channels = 0; channels < avctx->channels; channels += 2) {
  106. o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];
  107. o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];
  108. o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];
  109. o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];
  110. o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];
  111. o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];
  112. o += 6;
  113. samples += 2;
  114. }
  115. s->framing_index++;
  116. if (s->framing_index >= 192)
  117. s->framing_index = 0;
  118. }
  119. } else if (avctx->bits_per_raw_sample == 16) {
  120. const uint16_t *samples = (uint16_t *)frame->data[0];
  121. for (c = 0; c < frame->nb_samples; c++) {
  122. uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;
  123. for (channels = 0; channels < avctx->channels; channels += 2) {
  124. o[0] = ff_reverse[ samples[0] & 0xFF];
  125. o[1] = ff_reverse[(samples[0] & 0xFF00) >> 8];
  126. o[2] = ff_reverse[(samples[1] & 0x0F) << 4] | vucf;
  127. o[3] = ff_reverse[(samples[1] & 0x0FF0) >> 4];
  128. o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];
  129. o += 5;
  130. samples += 2;
  131. }
  132. s->framing_index++;
  133. if (s->framing_index >= 192)
  134. s->framing_index = 0;
  135. }
  136. }
  137. *got_packet_ptr = 1;
  138. return 0;
  139. }
  140. AVCodec ff_s302m_encoder = {
  141. .name = "s302m",
  142. .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
  143. .type = AVMEDIA_TYPE_AUDIO,
  144. .id = AV_CODEC_ID_S302M,
  145. .priv_data_size = sizeof(S302MEncContext),
  146. .init = s302m_encode_init,
  147. .encode2 = s302m_encode2_frame,
  148. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
  149. AV_SAMPLE_FMT_S16,
  150. AV_SAMPLE_FMT_NONE },
  151. .capabilities = AV_CODEC_CAP_VARIABLE_FRAME_SIZE | AV_CODEC_CAP_EXPERIMENTAL,
  152. .supported_samplerates = (const int[]) { 48000, 0 },
  153. };