3 * Copyright (c) 2008 Laurent Aimar <fenrir@videolan.org>
4 * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/log.h"
30 #define AES3_HEADER_LEN 4
32 typedef struct S302Context {
37 static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
41 int frame_size, channels, bits;
43 if (buf_size <= AES3_HEADER_LEN) {
44 av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
45 return AVERROR_INVALIDDATA;
58 frame_size = (h >> 16) & 0xffff;
59 channels = ((h >> 14) & 0x0003) * 2 + 2;
60 bits = ((h >> 4) & 0x0003) * 4 + 16;
62 if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
63 av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
64 return AVERROR_INVALIDDATA;
67 /* Set output properties */
68 avctx->bits_per_raw_sample = bits;
70 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
72 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
74 avctx->channels = channels;
77 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
80 avctx->channel_layout = AV_CH_LAYOUT_QUAD;
83 avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
86 avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK | AV_CH_LAYOUT_STEREO_DOWNMIX;
92 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
93 int *got_frame_ptr, AVPacket *avpkt)
95 S302Context *s = avctx->priv_data;
96 AVFrame *frame = data;
97 const uint8_t *buf = avpkt->data;
98 int buf_size = avpkt->size;
101 int non_pcm_data_type = -1;
103 int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
107 buf_size -= AES3_HEADER_LEN;
108 buf += AES3_HEADER_LEN;
110 /* get output buffer */
111 block_size = (avctx->bits_per_raw_sample + 4) / 4;
112 frame->nb_samples = 2 * (buf_size / block_size) / avctx->channels;
113 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
116 avctx->bit_rate = 48000 * avctx->channels * (avctx->bits_per_raw_sample + 4) +
117 32 * 48000 / frame->nb_samples;
118 buf_size = (frame->nb_samples * avctx->channels / 2) * block_size;
120 if (avctx->bits_per_raw_sample == 24) {
121 uint32_t *o = (uint32_t *)frame->data[0];
122 for (; buf_size > 6; buf_size -= 7) {
123 *o++ = ((unsigned)ff_reverse[buf[2]] << 24) |
124 (ff_reverse[buf[1]] << 16) |
125 (ff_reverse[buf[0]] << 8);
126 *o++ = ((unsigned)ff_reverse[buf[6] & 0xf0] << 28) |
127 (ff_reverse[buf[5]] << 20) |
128 (ff_reverse[buf[4]] << 12) |
129 (ff_reverse[buf[3] & 0x0f] << 4);
132 o = (uint32_t *)frame->data[0];
133 if (avctx->channels == 2)
134 for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
135 if (o[i] || o[i+1] || o[i+2] || o[i+3])
137 if (o[i+4] == 0x96F87200U && o[i+5] == 0xA54E1F00) {
138 non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
142 } else if (avctx->bits_per_raw_sample == 20) {
143 uint32_t *o = (uint32_t *)frame->data[0];
144 for (; buf_size > 5; buf_size -= 6) {
145 *o++ = ((unsigned)ff_reverse[buf[2] & 0xf0] << 28) |
146 (ff_reverse[buf[1]] << 20) |
147 (ff_reverse[buf[0]] << 12);
148 *o++ = ((unsigned)ff_reverse[buf[5] & 0xf0] << 28) |
149 (ff_reverse[buf[4]] << 20) |
150 (ff_reverse[buf[3]] << 12);
153 o = (uint32_t *)frame->data[0];
154 if (avctx->channels == 2)
155 for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
156 if (o[i] || o[i+1] || o[i+2] || o[i+3])
158 if (o[i+4] == 0x6F872000U && o[i+5] == 0x54E1F000) {
159 non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
164 uint16_t *o = (uint16_t *)frame->data[0];
165 for (; buf_size > 4; buf_size -= 5) {
166 *o++ = (ff_reverse[buf[1]] << 8) |
168 *o++ = (ff_reverse[buf[4] & 0xf0] << 12) |
169 (ff_reverse[buf[3]] << 4) |
170 (ff_reverse[buf[2]] >> 4);
173 o = (uint16_t *)frame->data[0];
174 if (avctx->channels == 2)
175 for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
176 if (o[i] || o[i+1] || o[i+2] || o[i+3])
178 if (o[i+4] == 0xF872U && o[i+5] == 0x4E1F) {
179 non_pcm_data_type = (o[i+6] & 0x1F);
185 if (non_pcm_data_type != -1) {
186 if (s->non_pcm_mode == 3) {
187 av_log(avctx, AV_LOG_ERROR,
188 "S302 non PCM mode with data type %d not supported\n",
190 return AVERROR_PATCHWELCOME;
192 if (s->non_pcm_mode & 1) {
197 avctx->sample_rate = 48000;
204 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_DECODING_PARAM
205 static const AVOption s302m_options[] = {
206 {"non_pcm_mode", "Chooses what to do with NON-PCM", offsetof(S302Context, non_pcm_mode), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 3, FLAGS, "non_pcm_mode"},
207 {"copy" , "Pass NON-PCM through unchanged" , 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 3, FLAGS, "non_pcm_mode"},
208 {"drop" , "Drop NON-PCM" , 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 3, FLAGS, "non_pcm_mode"},
209 {"decode_copy" , "Decode if possible else passthrough", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 3, FLAGS, "non_pcm_mode"},
210 {"decode_drop" , "Decode if possible else drop" , 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 3, FLAGS, "non_pcm_mode"},
214 static const AVClass s302m_class = {
215 .class_name = "SMPTE 302M Decoder",
216 .item_name = av_default_item_name,
217 .option = s302m_options,
218 .version = LIBAVUTIL_VERSION_INT,
221 AVCodec ff_s302m_decoder = {
223 .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
224 .type = AVMEDIA_TYPE_AUDIO,
225 .id = AV_CODEC_ID_S302M,
226 .priv_data_size = sizeof(S302Context),
227 .decode = s302m_decode_frame,
228 .capabilities = AV_CODEC_CAP_DR1,
229 .priv_class = &s302m_class,