FFmpeg
Loading...
Searching...
No Matches
s302m.c
Go to the documentation of this file.
1/*
2 * SMPTE 302M decoder
3 * Copyright (c) 2008 Laurent Aimar <fenrir@videolan.org>
4 * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@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
25#include "libavutil/opt.h"
26#include "libavutil/log.h"
27#include "libavutil/reverse.h"
28#include "avcodec.h"
29#include "codec_internal.h"
30#include "decode.h"
31
32#define AES3_HEADER_LEN 4
33
34typedef struct S302Context {
35 AVClass *class;
38
39static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
40 int buf_size)
41{
42 uint32_t h;
44
45 if (buf_size <= AES3_HEADER_LEN) {
46 av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
48 }
49
50 /*
51 * AES3 header :
52 * size: 16
53 * number channels 2
54 * channel_id 8
55 * bits per samples 2
56 * alignments 4
57 */
58
59 h = AV_RB32(buf);
60 frame_size = (h >> 16) & 0xffff;
61 channels = ((h >> 14) & 0x0003) * 2 + 2;
62 bits = ((h >> 4) & 0x0003) * 4 + 16;
63
64 if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
65 av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
67 }
68
69 /* Set output properties */
71 if (bits > 16)
73 else
75
77 switch(channels) {
78 case 2:
80 break;
81 case 4:
83 break;
84 case 6:
86 break;
87 case 8:
90 break;
91 default:
94 break;
95 }
96
97 return frame_size;
98}
99
101 int *got_frame_ptr, AVPacket *avpkt)
102{
103 S302Context *s = avctx->priv_data;
104 const uint8_t *buf = avpkt->data;
105 int buf_size = avpkt->size;
106 int block_size, ret, channels;
107 int i;
108 int non_pcm_data_type = -1;
109
110 int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
111 if (frame_size < 0)
112 return frame_size;
113
114 buf_size -= AES3_HEADER_LEN;
115 buf += AES3_HEADER_LEN;
116
117 /* get output buffer */
118 block_size = (avctx->bits_per_raw_sample + 4) / 4;
120 frame->nb_samples = 2 * (buf_size / block_size) / channels;
121 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
122 return ret;
123
124 avctx->bit_rate = 48000 * channels * (avctx->bits_per_raw_sample + 4) +
125 32 * 48000 / frame->nb_samples;
126 buf_size = (frame->nb_samples * channels / 2) * block_size;
127
128 if (avctx->bits_per_raw_sample == 24) {
129 uint32_t *o = (uint32_t *)frame->data[0];
130 for (; buf_size > 6; buf_size -= 7) {
131 *o++ = ((unsigned)ff_reverse[buf[2]] << 24) |
132 (ff_reverse[buf[1]] << 16) |
133 (ff_reverse[buf[0]] << 8);
134 *o++ = ((unsigned)ff_reverse[buf[6] & 0xf0] << 28) |
135 (ff_reverse[buf[5]] << 20) |
136 (ff_reverse[buf[4]] << 12) |
137 (ff_reverse[buf[3] & 0x0f] << 4);
138 buf += 7;
139 }
140 o = (uint32_t *)frame->data[0];
141 if (channels == 2)
142 for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
143 if (o[i] || o[i+1] || o[i+2] || o[i+3])
144 break;
145 if (o[i+4] == 0x96F87200U && o[i+5] == 0xA54E1F00) {
146 non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
147 break;
148 }
149 }
150 } else if (avctx->bits_per_raw_sample == 20) {
151 uint32_t *o = (uint32_t *)frame->data[0];
152 for (; buf_size > 5; buf_size -= 6) {
153 *o++ = ((unsigned)ff_reverse[buf[2] & 0xf0] << 28) |
154 (ff_reverse[buf[1]] << 20) |
155 (ff_reverse[buf[0]] << 12);
156 *o++ = ((unsigned)ff_reverse[buf[5] & 0xf0] << 28) |
157 (ff_reverse[buf[4]] << 20) |
158 (ff_reverse[buf[3]] << 12);
159 buf += 6;
160 }
161 o = (uint32_t *)frame->data[0];
162 if (channels == 2)
163 for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
164 if (o[i] || o[i+1] || o[i+2] || o[i+3])
165 break;
166 if (o[i+4] == 0x6F872000U && o[i+5] == 0x54E1F000) {
167 non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
168 break;
169 }
170 }
171 } else {
172 uint16_t *o = (uint16_t *)frame->data[0];
173 for (; buf_size > 4; buf_size -= 5) {
174 *o++ = (ff_reverse[buf[1]] << 8) |
175 ff_reverse[buf[0]];
176 *o++ = (ff_reverse[buf[4] & 0xf0] << 12) |
177 (ff_reverse[buf[3]] << 4) |
178 (ff_reverse[buf[2]] >> 4);
179 buf += 5;
180 }
181 o = (uint16_t *)frame->data[0];
182 if (channels == 2)
183 for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
184 if (o[i] || o[i+1] || o[i+2] || o[i+3])
185 break;
186 if (o[i+4] == 0xF872U && o[i+5] == 0x4E1F) {
187 non_pcm_data_type = (o[i+6] & 0x1F);
188 break;
189 }
190 }
191 }
192
193 if (non_pcm_data_type != -1) {
194 if (s->non_pcm_mode == 3) {
195 av_log(avctx, AV_LOG_ERROR,
196 "S302 non PCM mode with data type %d not supported\n",
197 non_pcm_data_type);
199 }
200 if (s->non_pcm_mode & 1) {
201 return avpkt->size;
202 }
203 }
204
205 avctx->sample_rate = 48000;
206
207 *got_frame_ptr = 1;
208
209 return avpkt->size;
210}
211
212#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_DECODING_PARAM
213static const AVOption s302m_options[] = {
214 {"non_pcm_mode", "Chooses what to do with NON-PCM", offsetof(S302Context, non_pcm_mode), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
215 {"copy" , "Pass NON-PCM through unchanged" , 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
216 {"drop" , "Drop NON-PCM" , 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
217 {"decode_copy" , "Decode if possible else passthrough", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
218 {"decode_drop" , "Decode if possible else drop" , 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
219 {NULL}
220};
221
222static const AVClass s302m_class = {
223 .class_name = "SMPTE 302M Decoder",
224 .item_name = av_default_item_name,
225 .option = s302m_options,
226 .version = LIBAVUTIL_VERSION_INT,
227};
228
230 .p.name = "s302m",
231 CODEC_LONG_NAME("SMPTE 302M"),
232 .p.type = AVMEDIA_TYPE_AUDIO,
233 .p.id = AV_CODEC_ID_S302M,
234 .p.priv_class = &s302m_class,
235 .priv_data_size = sizeof(S302Context),
237 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
239};
const FFCodec ff_s302m_decoder
Definition s302m.c:229
channels
Definition aptx.h:31
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
static const uint8_t bits[8]
Definition fastaudio.c:100
static const uint8_t frame_size[4]
Definition g723_1.h:222
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CH_LAYOUT_STEREO_DOWNMIX
#define AV_CH_LAYOUT_5POINT1_BACK
#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_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_S302M
Definition codec_id.h:356
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_STEREO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
#define AV_CHANNEL_LAYOUT_QUAD
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ 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
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_RB32(p)
const uint8_t ff_reverse[256]
Definition reverse.c:23
AVOptions.
static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition s302m.c:39
static const AVOption s302m_options[]
Definition s302m.c:213
static int s302m_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition s302m.c:100
static const AVClass s302m_class
Definition s302m.c:222
#define AES3_HEADER_LEN
Definition s302m.c:32
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
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
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int non_pcm_mode
Definition s302m.c:36
#define av_log(a,...)