FFmpeg
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 
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/log.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "mathops.h"
30 
31 #define AES3_HEADER_LEN 4
32 
33 typedef struct S302Context {
34  AVClass *class;
36 } S302Context;
37 
38 static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
39  int buf_size)
40 {
41  uint32_t h;
42  int frame_size, channels, bits;
43 
44  if (buf_size <= AES3_HEADER_LEN) {
45  av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
46  return AVERROR_INVALIDDATA;
47  }
48 
49  /*
50  * AES3 header :
51  * size: 16
52  * number channels 2
53  * channel_id 8
54  * bits per samples 2
55  * alignments 4
56  */
57 
58  h = AV_RB32(buf);
59  frame_size = (h >> 16) & 0xffff;
60  channels = ((h >> 14) & 0x0003) * 2 + 2;
61  bits = ((h >> 4) & 0x0003) * 4 + 16;
62 
63  if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
64  av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
65  return AVERROR_INVALIDDATA;
66  }
67 
68  /* Set output properties */
69  avctx->bits_per_raw_sample = bits;
70  if (bits > 16)
72  else
74 
75  avctx->channels = channels;
76  switch(channels) {
77  case 2:
79  break;
80  case 4:
82  break;
83  case 6:
85  break;
86  case 8:
88  }
89 
90  return frame_size;
91 }
92 
93 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
94  int *got_frame_ptr, AVPacket *avpkt)
95 {
96  S302Context *s = avctx->priv_data;
97  AVFrame *frame = data;
98  const uint8_t *buf = avpkt->data;
99  int buf_size = avpkt->size;
100  int block_size, ret;
101  int i;
102  int non_pcm_data_type = -1;
103 
104  int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
105  if (frame_size < 0)
106  return frame_size;
107 
108  buf_size -= AES3_HEADER_LEN;
109  buf += AES3_HEADER_LEN;
110 
111  /* get output buffer */
112  block_size = (avctx->bits_per_raw_sample + 4) / 4;
113  frame->nb_samples = 2 * (buf_size / block_size) / avctx->channels;
114  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
115  return ret;
116 
117  avctx->bit_rate = 48000 * avctx->channels * (avctx->bits_per_raw_sample + 4) +
118  32 * 48000 / frame->nb_samples;
119  buf_size = (frame->nb_samples * avctx->channels / 2) * block_size;
120 
121  if (avctx->bits_per_raw_sample == 24) {
122  uint32_t *o = (uint32_t *)frame->data[0];
123  for (; buf_size > 6; buf_size -= 7) {
124  *o++ = ((unsigned)ff_reverse[buf[2]] << 24) |
125  (ff_reverse[buf[1]] << 16) |
126  (ff_reverse[buf[0]] << 8);
127  *o++ = ((unsigned)ff_reverse[buf[6] & 0xf0] << 28) |
128  (ff_reverse[buf[5]] << 20) |
129  (ff_reverse[buf[4]] << 12) |
130  (ff_reverse[buf[3] & 0x0f] << 4);
131  buf += 7;
132  }
133  o = (uint32_t *)frame->data[0];
134  if (avctx->channels == 2)
135  for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
136  if (o[i] || o[i+1] || o[i+2] || o[i+3])
137  break;
138  if (o[i+4] == 0x96F87200U && o[i+5] == 0xA54E1F00) {
139  non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
140  break;
141  }
142  }
143  } else if (avctx->bits_per_raw_sample == 20) {
144  uint32_t *o = (uint32_t *)frame->data[0];
145  for (; buf_size > 5; buf_size -= 6) {
146  *o++ = ((unsigned)ff_reverse[buf[2] & 0xf0] << 28) |
147  (ff_reverse[buf[1]] << 20) |
148  (ff_reverse[buf[0]] << 12);
149  *o++ = ((unsigned)ff_reverse[buf[5] & 0xf0] << 28) |
150  (ff_reverse[buf[4]] << 20) |
151  (ff_reverse[buf[3]] << 12);
152  buf += 6;
153  }
154  o = (uint32_t *)frame->data[0];
155  if (avctx->channels == 2)
156  for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
157  if (o[i] || o[i+1] || o[i+2] || o[i+3])
158  break;
159  if (o[i+4] == 0x6F872000U && o[i+5] == 0x54E1F000) {
160  non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
161  break;
162  }
163  }
164  } else {
165  uint16_t *o = (uint16_t *)frame->data[0];
166  for (; buf_size > 4; buf_size -= 5) {
167  *o++ = (ff_reverse[buf[1]] << 8) |
168  ff_reverse[buf[0]];
169  *o++ = (ff_reverse[buf[4] & 0xf0] << 12) |
170  (ff_reverse[buf[3]] << 4) |
171  (ff_reverse[buf[2]] >> 4);
172  buf += 5;
173  }
174  o = (uint16_t *)frame->data[0];
175  if (avctx->channels == 2)
176  for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
177  if (o[i] || o[i+1] || o[i+2] || o[i+3])
178  break;
179  if (o[i+4] == 0xF872U && o[i+5] == 0x4E1F) {
180  non_pcm_data_type = (o[i+6] & 0x1F);
181  break;
182  }
183  }
184  }
185 
186  if (non_pcm_data_type != -1) {
187  if (s->non_pcm_mode == 3) {
188  av_log(avctx, AV_LOG_ERROR,
189  "S302 non PCM mode with data type %d not supported\n",
190  non_pcm_data_type);
191  return AVERROR_PATCHWELCOME;
192  }
193  if (s->non_pcm_mode & 1) {
194  return avpkt->size;
195  }
196  }
197 
198  avctx->sample_rate = 48000;
199 
200  *got_frame_ptr = 1;
201 
202  return avpkt->size;
203 }
204 
205 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_DECODING_PARAM
206 static const AVOption s302m_options[] = {
207  {"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"},
208  {"copy" , "Pass NON-PCM through unchanged" , 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 3, FLAGS, "non_pcm_mode"},
209  {"drop" , "Drop NON-PCM" , 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 3, FLAGS, "non_pcm_mode"},
210  {"decode_copy" , "Decode if possible else passthrough", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 3, FLAGS, "non_pcm_mode"},
211  {"decode_drop" , "Decode if possible else drop" , 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 3, FLAGS, "non_pcm_mode"},
212  {NULL}
213 };
214 
215 static const AVClass s302m_class = {
216  .class_name = "SMPTE 302M Decoder",
217  .item_name = av_default_item_name,
218  .option = s302m_options,
219  .version = LIBAVUTIL_VERSION_INT,
220 };
221 
223  .name = "s302m",
224  .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
225  .type = AVMEDIA_TYPE_AUDIO,
226  .id = AV_CODEC_ID_S302M,
227  .priv_data_size = sizeof(S302Context),
229  .capabilities = AV_CODEC_CAP_CHANNEL_CONF |
231  .priv_class = &s302m_class,
232 };
AVCodec
AVCodec.
Definition: codec.h:202
opt.h
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1043
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:992
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
ff_reverse
const uint8_t ff_reverse[256]
Definition: reverse.c:23
data
const char data[16]
Definition: mxf.c:143
AES3_HEADER_LEN
#define AES3_HEADER_LEN
Definition: s302m.c:31
s302m_class
static const AVClass s302m_class
Definition: s302m.c:215
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_QUAD
Definition: channel_layout.h:99
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_CODEC_ID_S302M
@ AV_CODEC_ID_S302M
Definition: codec_id.h:339
FLAGS
#define FLAGS
Definition: s302m.c:205
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
ff_s302m_decoder
const AVCodec ff_s302m_decoder
Definition: s302m.c:222
frame_size
int frame_size
Definition: mxfenc.c:2207
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CH_LAYOUT_STEREO_DOWNMIX
#define AV_CH_LAYOUT_STEREO_DOWNMIX
Definition: channel_layout.h:117
bits
uint8_t bits
Definition: vp3data.h:141
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1425
channels
channels
Definition: aptx.h:33
if
if(ret)
Definition: filter_design.txt:179
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
S302Context
Definition: s302m.c:33
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:433
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
mathops.h
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:109
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1652
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
AVPacket::size
int size
Definition: packet.h:374
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:1000
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
S302Context::non_pcm_mode
int non_pcm_mode
Definition: s302m.c:35
AV_CH_LAYOUT_5POINT1_BACK
#define AV_CH_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:103
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
i
int i
Definition: input.c:406
log.h
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:209
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
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:383
channel_layout.h
s302m_decode_frame
static int s302m_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: s302m.c:93
s302m_options
static const AVOption s302m_options[]
Definition: s302m.c:206
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
s302m_parse_frame_header
static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: s302m.c:38