FFmpeg
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 "encode.h"
25 #include "internal.h"
26 #include "mathops.h"
27 #include "put_bits.h"
28 
29 #define AES3_HEADER_LEN 4
30 
31 typedef struct S302MEncContext {
32  uint8_t framing_index; /* Set for even channels on multiple of 192 samples */
34 
36 {
37  S302MEncContext *s = avctx->priv_data;
38 
39  if (avctx->channels & 1 || avctx->channels > 8) {
40  av_log(avctx, AV_LOG_ERROR,
41  "Encoding %d channel(s) is not allowed. Only 2, 4, 6 and 8 channels are supported.\n",
42  avctx->channels);
43  return AVERROR(EINVAL);
44  }
45 
46  switch (avctx->sample_fmt) {
47  case AV_SAMPLE_FMT_S16:
48  avctx->bits_per_raw_sample = 16;
49  break;
50  case AV_SAMPLE_FMT_S32:
51  if (avctx->bits_per_raw_sample > 20) {
52  if (avctx->bits_per_raw_sample > 24)
53  av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
54  avctx->bits_per_raw_sample = 24;
55  } else if (!avctx->bits_per_raw_sample) {
56  avctx->bits_per_raw_sample = 24;
57  } else if (avctx->bits_per_raw_sample <= 20) {
58  avctx->bits_per_raw_sample = 20;
59  }
60  }
61 
62  avctx->frame_size = 0;
63  avctx->bit_rate = 48000 * avctx->channels *
64  (avctx->bits_per_raw_sample + 4);
65  s->framing_index = 0;
66 
67  return 0;
68 }
69 
70 static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt,
71  const AVFrame *frame, int *got_packet_ptr)
72 {
73  S302MEncContext *s = avctx->priv_data;
74  const int buf_size = AES3_HEADER_LEN +
75  (frame->nb_samples *
76  avctx->channels *
77  (avctx->bits_per_raw_sample + 4)) / 8;
78  int ret, c, channels;
79  uint8_t *o;
80  PutBitContext pb;
81 
82  if (buf_size - AES3_HEADER_LEN > UINT16_MAX) {
83  av_log(avctx, AV_LOG_ERROR, "number of samples in frame too big\n");
84  return AVERROR(EINVAL);
85  }
86 
87  if ((ret = ff_get_encode_buffer(avctx, avpkt, buf_size, 0)) < 0)
88  return ret;
89 
90  o = avpkt->data;
91  init_put_bits(&pb, o, buf_size);
92  put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);
93  put_bits(&pb, 2, (avctx->channels - 2) >> 1); // number of channels
94  put_bits(&pb, 8, 0); // channel ID
95  put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit)
96  put_bits(&pb, 4, 0); // alignments
97  flush_put_bits(&pb);
98  o += AES3_HEADER_LEN;
99 
100  if (avctx->bits_per_raw_sample == 24) {
101  const uint32_t *samples = (uint32_t *)frame->data[0];
102 
103  for (c = 0; c < frame->nb_samples; c++) {
104  uint8_t vucf = s->framing_index == 0 ? 0x10: 0;
105 
106  for (channels = 0; channels < avctx->channels; channels += 2) {
107  o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];
108  o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];
109  o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];
110  o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;
111  o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];
112  o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];
113  o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];
114  o += 7;
115  samples += 2;
116  }
117 
118  s->framing_index++;
119  if (s->framing_index >= 192)
120  s->framing_index = 0;
121  }
122  } else if (avctx->bits_per_raw_sample == 20) {
123  const uint32_t *samples = (uint32_t *)frame->data[0];
124 
125  for (c = 0; c < frame->nb_samples; c++) {
126  uint8_t vucf = s->framing_index == 0 ? 0x80: 0;
127 
128  for (channels = 0; channels < avctx->channels; channels += 2) {
129  o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];
130  o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];
131  o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];
132  o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];
133  o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];
134  o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];
135  o += 6;
136  samples += 2;
137  }
138 
139  s->framing_index++;
140  if (s->framing_index >= 192)
141  s->framing_index = 0;
142  }
143  } else if (avctx->bits_per_raw_sample == 16) {
144  const uint16_t *samples = (uint16_t *)frame->data[0];
145 
146  for (c = 0; c < frame->nb_samples; c++) {
147  uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;
148 
149  for (channels = 0; channels < avctx->channels; channels += 2) {
150  o[0] = ff_reverse[ samples[0] & 0xFF];
151  o[1] = ff_reverse[(samples[0] & 0xFF00) >> 8];
152  o[2] = ff_reverse[(samples[1] & 0x0F) << 4] | vucf;
153  o[3] = ff_reverse[(samples[1] & 0x0FF0) >> 4];
154  o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];
155  o += 5;
156  samples += 2;
157 
158  }
159 
160  s->framing_index++;
161  if (s->framing_index >= 192)
162  s->framing_index = 0;
163  }
164  }
165 
166  *got_packet_ptr = 1;
167 
168  return 0;
169 }
170 
172  .name = "s302m",
173  .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
174  .type = AVMEDIA_TYPE_AUDIO,
175  .id = AV_CODEC_ID_S302M,
176  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_EXPERIMENTAL |
178  .priv_data_size = sizeof(S302MEncContext),
180  .encode2 = s302m_encode2_frame,
181  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
184  .supported_samplerates = (const int[]) { 48000, 0 },
185  /* .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO,
186  AV_CH_LAYOUT_QUAD,
187  AV_CH_LAYOUT_5POINT1_BACK,
188  AV_CH_LAYOUT_5POINT1_BACK | AV_CH_LAYOUT_STEREO_DOWNMIX,
189  0 }, */
190  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
191 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1001
AVCodec
AVCodec.
Definition: codec.h:197
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:187
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:953
ff_s302m_encoder
const AVCodec ff_s302m_encoder
Definition: s302menc.c:171
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
S302MEncContext::framing_index
uint8_t framing_index
Definition: s302menc.c:32
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
encode.h
ff_reverse
const uint8_t ff_reverse[256]
Definition: reverse.c:23
AES3_HEADER_LEN
#define AES3_HEADER_LEN
Definition: s302menc.c:29
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
AV_CODEC_CAP_EXPERIMENTAL
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: codec.h:100
av_cold
#define av_cold
Definition: attributes.h:90
AV_CODEC_ID_S302M
@ AV_CODEC_ID_S302M
Definition: codec_id.h:339
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
s302m_encode_init
static av_cold int s302m_encode_init(AVCodecContext *avctx)
Definition: s302menc.c:35
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1414
channels
channels
Definition: aptx.h:33
PutBitContext
Definition: put_bits.h:49
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:434
mathops.h
AV_CODEC_CAP_VARIABLE_FRAME_SIZE
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition: codec.h:129
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:989
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:982
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:384
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:77
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
S302MEncContext
Definition: s302menc.c:31
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:142
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:411
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
put_bits.h
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
s302m_encode2_frame
static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: s302menc.c:70