FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
s302menc.c
Go to the documentation of this file.
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 
23 #include "avcodec.h"
24 #include "internal.h"
25 #include "mathops.h"
26 #include "put_bits.h"
27 
28 #define AES3_HEADER_LEN 4
29 
30 typedef struct S302MEncContext {
31  uint8_t framing_index; /* Set for even channels on multiple of 192 samples */
33 
35 {
36  S302MEncContext *s = avctx->priv_data;
37 
38  if (avctx->channels & 1 || avctx->channels > 8) {
39  av_log(avctx, AV_LOG_ERROR,
40  "Encoding %d channel(s) is not allowed. Only 2, 4, 6 and 8 channels are supported.\n",
41  avctx->channels);
42  return AVERROR(EINVAL);
43  }
44 
45  switch (avctx->sample_fmt) {
46  case AV_SAMPLE_FMT_S16:
47  avctx->bits_per_raw_sample = 16;
48  break;
49  case AV_SAMPLE_FMT_S32:
50  if (avctx->bits_per_raw_sample > 20) {
51  if (avctx->bits_per_raw_sample > 24)
52  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
53  avctx->bits_per_raw_sample = 24;
54  } else if (!avctx->bits_per_raw_sample) {
55  avctx->bits_per_raw_sample = 24;
56  } else if (avctx->bits_per_raw_sample <= 20) {
57  avctx->bits_per_raw_sample = 20;
58  }
59  }
60 
61  avctx->frame_size = 0;
62  avctx->bit_rate = 48000 * avctx->channels *
63  (avctx->bits_per_raw_sample + 4);
64  s->framing_index = 0;
65 
66  return 0;
67 }
68 
69 static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt,
70  const AVFrame *frame, int *got_packet_ptr)
71 {
72  S302MEncContext *s = avctx->priv_data;
73  const int buf_size = AES3_HEADER_LEN +
74  (frame->nb_samples *
75  avctx->channels *
76  (avctx->bits_per_raw_sample + 4)) / 8;
77  int ret, c, channels;
78  uint8_t *o;
79  PutBitContext pb;
80 
81  if (buf_size - AES3_HEADER_LEN > UINT16_MAX) {
82  av_log(avctx, AV_LOG_ERROR, "number of samples in frame too big\n");
83  return AVERROR(EINVAL);
84  }
85 
86  if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size, 0)) < 0)
87  return ret;
88 
89  o = avpkt->data;
90  init_put_bits(&pb, o, buf_size);
91  put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);
92  put_bits(&pb, 2, (avctx->channels - 2) >> 1); // number of channels
93  put_bits(&pb, 8, 0); // channel ID
94  put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit)
95  put_bits(&pb, 4, 0); // alignments
96  flush_put_bits(&pb);
97  o += AES3_HEADER_LEN;
98 
99  if (avctx->bits_per_raw_sample == 24) {
100  const uint32_t *samples = (uint32_t *)frame->data[0];
101 
102  for (c = 0; c < frame->nb_samples; c++) {
103  uint8_t vucf = s->framing_index == 0 ? 0x10: 0;
104 
105  for (channels = 0; channels < avctx->channels; channels += 2) {
106  o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];
107  o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];
108  o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];
109  o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;
110  o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];
111  o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];
112  o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];
113  o += 7;
114  samples += 2;
115  }
116 
117  s->framing_index++;
118  if (s->framing_index >= 192)
119  s->framing_index = 0;
120  }
121  } else if (avctx->bits_per_raw_sample == 20) {
122  const uint32_t *samples = (uint32_t *)frame->data[0];
123 
124  for (c = 0; c < frame->nb_samples; c++) {
125  uint8_t vucf = s->framing_index == 0 ? 0x80: 0;
126 
127  for (channels = 0; channels < avctx->channels; channels += 2) {
128  o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];
129  o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];
130  o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];
131  o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];
132  o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];
133  o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];
134  o += 6;
135  samples += 2;
136  }
137 
138  s->framing_index++;
139  if (s->framing_index >= 192)
140  s->framing_index = 0;
141  }
142  } else if (avctx->bits_per_raw_sample == 16) {
143  const uint16_t *samples = (uint16_t *)frame->data[0];
144 
145  for (c = 0; c < frame->nb_samples; c++) {
146  uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;
147 
148  for (channels = 0; channels < avctx->channels; channels += 2) {
149  o[0] = ff_reverse[ samples[0] & 0xFF];
150  o[1] = ff_reverse[(samples[0] & 0xFF00) >> 8];
151  o[2] = ff_reverse[(samples[1] & 0x0F) << 4] | vucf;
152  o[3] = ff_reverse[(samples[1] & 0x0FF0) >> 4];
153  o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];
154  o += 5;
155  samples += 2;
156 
157  }
158 
159  s->framing_index++;
160  if (s->framing_index >= 192)
161  s->framing_index = 0;
162  }
163  }
164 
165  *got_packet_ptr = 1;
166 
167  return 0;
168 }
169 
171  .name = "s302m",
172  .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
173  .type = AVMEDIA_TYPE_AUDIO,
174  .id = AV_CODEC_ID_S302M,
175  .priv_data_size = sizeof(S302MEncContext),
177  .encode2 = s302m_encode2_frame,
178  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
182  .supported_samplerates = (const int[]) { 48000, 0 },
183  /* .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO,
184  AV_CH_LAYOUT_QUAD,
185  AV_CH_LAYOUT_5POINT1_BACK,
186  AV_CH_LAYOUT_5POINT1_BACK | AV_CH_LAYOUT_STEREO_DOWNMIX,
187  0 }, */
188 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1583
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
const uint8_t ff_reverse[256]
Definition: reverse.c:23
channels
Definition: aptx.c:30
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:1016
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2757
AVCodec.
Definition: avcodec.h:3424
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2197
uint8_t
#define av_cold
Definition: attributes.h:82
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1445
signed 32 bits
Definition: samplefmt.h:62
#define av_log(a,...)
static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: s302menc.c:69
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: avcodec.h:1040
#define s(width, name)
Definition: cbs_vp9.c:257
AVCodec ff_s302m_encoder
Definition: s302menc.c:170
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2209
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
main external API structure.
Definition: avcodec.h:1533
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define AES3_HEADER_LEN
Definition: s302menc.c:28
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
void * priv_data
Definition: avcodec.h:1560
int channels
number of audio channels
Definition: avcodec.h:2190
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
uint8_t framing_index
Definition: s302menc.c:31
static av_cold int s302m_encode_init(AVCodecContext *avctx)
Definition: s302menc.c:34
This structure stores compressed data.
Definition: avcodec.h:1422
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
for(j=16;j >0;--j)
bitstream writer API