FFmpeg
Loading...
Searching...
No Matches
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
24#include "libavutil/reverse.h"
25#include "avcodec.h"
26#include "codec_internal.h"
27#include "encode.h"
28#include "mathops.h"
29#include "put_bits.h"
30
31#define AES3_HEADER_LEN 4
32
33typedef struct S302MEncContext {
34 uint8_t framing_index; /* Set for even channels on multiple of 192 samples */
36
38{
39 S302MEncContext *s = avctx->priv_data;
40
41 if (avctx->ch_layout.nb_channels & 1 || avctx->ch_layout.nb_channels > 8) {
42 av_log(avctx, AV_LOG_ERROR,
43 "Encoding %d channel(s) is not allowed. Only 2, 4, 6 and 8 channels are supported.\n",
44 avctx->ch_layout.nb_channels);
45 return AVERROR(EINVAL);
46 }
47
48 switch (avctx->sample_fmt) {
50 avctx->bits_per_raw_sample = 16;
51 break;
53 if (avctx->bits_per_raw_sample > 20) {
54 if (avctx->bits_per_raw_sample > 24)
55 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
56 avctx->bits_per_raw_sample = 24;
57 } else if (!avctx->bits_per_raw_sample) {
58 avctx->bits_per_raw_sample = 24;
59 } else if (avctx->bits_per_raw_sample <= 20) {
60 avctx->bits_per_raw_sample = 20;
61 }
62 }
63
64 avctx->bit_rate = 48000 * avctx->ch_layout.nb_channels *
65 (avctx->bits_per_raw_sample + 4);
66 s->framing_index = 0;
67
68 return 0;
69}
70
72 const AVFrame *frame, int *got_packet_ptr)
73{
74 S302MEncContext *s = avctx->priv_data;
75 const int nb_channels = avctx->ch_layout.nb_channels;
76 const int buf_size = AES3_HEADER_LEN +
77 (frame->nb_samples * nb_channels *
78 (avctx->bits_per_raw_sample + 4)) / 8;
79 int ret, c, channels;
80 uint8_t *o;
82
83 if (buf_size - AES3_HEADER_LEN > UINT16_MAX) {
84 av_log(avctx, AV_LOG_ERROR, "number of samples in frame too big\n");
85 return AVERROR(EINVAL);
86 }
87
88 if ((ret = ff_get_encode_buffer(avctx, avpkt, buf_size, 0)) < 0)
89 return ret;
90
91 o = avpkt->data;
92 init_put_bits(&pb, o, buf_size);
93 put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);
94 put_bits(&pb, 2, (nb_channels - 2) >> 1); // number of channels
95 put_bits(&pb, 8, 0); // channel ID
96 put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit)
97 put_bits(&pb, 4, 0); // alignments
98 flush_put_bits(&pb);
99 o += AES3_HEADER_LEN;
100
101 if (avctx->bits_per_raw_sample == 24) {
102 const uint32_t *samples = (uint32_t *)frame->data[0];
103
104 for (c = 0; c < frame->nb_samples; c++) {
105 uint8_t vucf = s->framing_index == 0 ? 0x10: 0;
106
107 for (channels = 0; channels < nb_channels; channels += 2) {
108 o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];
109 o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];
110 o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];
111 o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;
112 o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];
113 o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];
114 o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];
115 o += 7;
116 samples += 2;
117 }
118
119 s->framing_index++;
120 if (s->framing_index >= 192)
121 s->framing_index = 0;
122 }
123 } else if (avctx->bits_per_raw_sample == 20) {
124 const uint32_t *samples = (uint32_t *)frame->data[0];
125
126 for (c = 0; c < frame->nb_samples; c++) {
127 uint8_t vucf = s->framing_index == 0 ? 0x80: 0;
128
129 for (channels = 0; channels < nb_channels; channels += 2) {
130 o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];
131 o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];
132 o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];
133 o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];
134 o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];
135 o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];
136 o += 6;
137 samples += 2;
138 }
139
140 s->framing_index++;
141 if (s->framing_index >= 192)
142 s->framing_index = 0;
143 }
144 } else if (avctx->bits_per_raw_sample == 16) {
145 const uint16_t *samples = (uint16_t *)frame->data[0];
146
147 for (c = 0; c < frame->nb_samples; c++) {
148 uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;
149
150 for (channels = 0; channels < nb_channels; channels += 2) {
151 o[0] = ff_reverse[ samples[0] & 0xFF];
152 o[1] = ff_reverse[(samples[0] & 0xFF00) >> 8];
153 o[2] = ff_reverse[(samples[1] & 0x0F) << 4] | vucf;
154 o[3] = ff_reverse[(samples[1] & 0x0FF0) >> 4];
155 o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];
156 o += 5;
157 samples += 2;
158
159 }
160
161 s->framing_index++;
162 if (s->framing_index >= 192)
163 s->framing_index = 0;
164 }
165 }
166
167 *got_packet_ptr = 1;
168
169 return 0;
170}
171
const FFCodec ff_s302m_encoder
Definition s302menc.c:172
channels
Definition aptx.h:31
Libavcodec external API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define CODEC_SAMPLERATES(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
Definition codec.h:116
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition codec.h:90
@ AV_CODEC_ID_S302M
Definition codec_id.h:356
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
#define av_cold
Definition attributes.h:117
const uint8_t ff_reverse[256]
Definition reverse.c:23
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
#define AES3_HEADER_LEN
Definition s302m.c:32
static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition s302menc.c:71
static av_cold int s302m_encode_init(AVCodecContext *avctx)
Definition s302menc.c:37
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1571
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
uint8_t * data
Definition packet.h:603
uint8_t framing_index
Definition s302menc.c:34
#define av_log(a,...)
static double c[64]