FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/log.h"
26 #include "avcodec.h"
27 #include "internal.h"
28 #include "mathops.h"
29 
30 #define AES3_HEADER_LEN 4
31 
32 typedef struct S302Context {
33  AVClass *class;
35 } S302Context;
36 
38  int buf_size)
39 {
40  uint32_t h;
41  int frame_size, channels, bits;
42 
43  if (buf_size <= AES3_HEADER_LEN) {
44  av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
45  return AVERROR_INVALIDDATA;
46  }
47 
48  /*
49  * AES3 header :
50  * size: 16
51  * number channels 2
52  * channel_id 8
53  * bits per samples 2
54  * alignments 4
55  */
56 
57  h = AV_RB32(buf);
58  frame_size = (h >> 16) & 0xffff;
59  channels = ((h >> 14) & 0x0003) * 2 + 2;
60  bits = ((h >> 4) & 0x0003) * 4 + 16;
61 
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;
65  }
66 
67  /* Set output properties */
68  avctx->bits_per_raw_sample = bits;
69  if (bits > 16)
71  else
73 
74  avctx->channels = channels;
75  switch(channels) {
76  case 2:
78  break;
79  case 4:
81  break;
82  case 6:
84  break;
85  case 8:
87  }
88  avctx->bit_rate = 48000 * avctx->channels * (avctx->bits_per_raw_sample + 4) +
89  32 * (48000 / (buf_size * 8 /
90  (avctx->channels *
91  (avctx->bits_per_raw_sample + 4))));
92 
93  return frame_size;
94 }
95 
96 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
97  int *got_frame_ptr, AVPacket *avpkt)
98 {
99  S302Context *s = avctx->priv_data;
100  AVFrame *frame = data;
101  const uint8_t *buf = avpkt->data;
102  int buf_size = avpkt->size;
103  int block_size, ret;
104  int i;
105  int non_pcm_data_type = -1;
106 
107  int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
108  if (frame_size < 0)
109  return frame_size;
110 
111  buf_size -= AES3_HEADER_LEN;
112  buf += AES3_HEADER_LEN;
113 
114  /* get output buffer */
115  block_size = (avctx->bits_per_raw_sample + 4) / 4;
116  frame->nb_samples = 2 * (buf_size / block_size) / avctx->channels;
117  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
118  return ret;
119 
120  buf_size = (frame->nb_samples * avctx->channels / 2) * block_size;
121 
122  if (avctx->bits_per_raw_sample == 24) {
123  uint32_t *o = (uint32_t *)frame->data[0];
124  for (; buf_size > 6; buf_size -= 7) {
125  *o++ = (ff_reverse[buf[2]] << 24) |
126  (ff_reverse[buf[1]] << 16) |
127  (ff_reverse[buf[0]] << 8);
128  *o++ = (ff_reverse[buf[6] & 0xf0] << 28) |
129  (ff_reverse[buf[5]] << 20) |
130  (ff_reverse[buf[4]] << 12) |
131  (ff_reverse[buf[3] & 0x0f] << 4);
132  buf += 7;
133  }
134  o = (uint32_t *)frame->data[0];
135  if (avctx->channels == 2)
136  for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
137  if (o[i] || o[i+1] || o[i+2] || o[i+3])
138  break;
139  if (o[i+4] == 0x96F87200U && o[i+5] == 0xA54E1F00) {
140  non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
141  break;
142  }
143  }
144  } else if (avctx->bits_per_raw_sample == 20) {
145  uint32_t *o = (uint32_t *)frame->data[0];
146  for (; buf_size > 5; buf_size -= 6) {
147  *o++ = (ff_reverse[buf[2] & 0xf0] << 28) |
148  (ff_reverse[buf[1]] << 20) |
149  (ff_reverse[buf[0]] << 12);
150  *o++ = (ff_reverse[buf[5] & 0xf0] << 28) |
151  (ff_reverse[buf[4]] << 20) |
152  (ff_reverse[buf[3]] << 12);
153  buf += 6;
154  }
155  o = (uint32_t *)frame->data[0];
156  if (avctx->channels == 2)
157  for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
158  if (o[i] || o[i+1] || o[i+2] || o[i+3])
159  break;
160  if (o[i+4] == 0x6F872000U && o[i+5] == 0x54E1F000) {
161  non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
162  break;
163  }
164  }
165  } else {
166  uint16_t *o = (uint16_t *)frame->data[0];
167  for (; buf_size > 4; buf_size -= 5) {
168  *o++ = (ff_reverse[buf[1]] << 8) |
169  ff_reverse[buf[0]];
170  *o++ = (ff_reverse[buf[4] & 0xf0] << 12) |
171  (ff_reverse[buf[3]] << 4) |
172  (ff_reverse[buf[2]] >> 4);
173  buf += 5;
174  }
175  o = (uint16_t *)frame->data[0];
176  if (avctx->channels == 2)
177  for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
178  if (o[i] || o[i+1] || o[i+2] || o[i+3])
179  break;
180  if (o[i+4] == 0xF872U && o[i+5] == 0x4E1F) {
181  non_pcm_data_type = (o[i+6] & 0x1F);
182  break;
183  }
184  }
185  }
186 
187  if (non_pcm_data_type != -1) {
188  if (s->non_pcm_mode == 3) {
189  av_log(avctx, AV_LOG_ERROR,
190  "S302 non PCM mode with data type %d not supported\n",
191  non_pcm_data_type);
192  return AVERROR_PATCHWELCOME;
193  }
194  if (s->non_pcm_mode & 1) {
195  return avpkt->size;
196  }
197  }
198 
199  avctx->sample_rate = 48000;
200 
201  *got_frame_ptr = 1;
202 
203  return avpkt->size;
204 }
205 
206 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM
207 static const AVOption s302m_options[] = {
208  {"non_pcm_mode", "Chooses what to do with NON-PCM", offsetof(S302Context, non_pcm_mode), FF_OPT_TYPE_INT, {.i64 = 3}, 0, 3, FLAGS, "non_pcm_mode"},
209  {"copy" , "Pass NON-PCM through unchanged" , 0, FF_OPT_TYPE_CONST, {.i64 = 0}, 0, 3, FLAGS, "non_pcm_mode"},
210  {"drop" , "Drop NON-PCM" , 0, FF_OPT_TYPE_CONST, {.i64 = 1}, 0, 3, FLAGS, "non_pcm_mode"},
211  {"decode_copy" , "Decode if possible else passthrough", 0, FF_OPT_TYPE_CONST, {.i64 = 2}, 0, 3, FLAGS, "non_pcm_mode"},
212  {"decode_drop" , "Decode if possible else drop" , 0, FF_OPT_TYPE_CONST, {.i64 = 3}, 0, 3, FLAGS, "non_pcm_mode"},
213  {NULL}
214 };
215 
216 static const AVClass s302m_class = {
217  "SMPTE 302M Decoder",
221 };
222 
224  .name = "s302m",
225  .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
226  .type = AVMEDIA_TYPE_AUDIO,
227  .id = AV_CODEC_ID_S302M,
228  .priv_data_size = sizeof(S302Context),
230  .capabilities = CODEC_CAP_DR1,
231  .priv_class = &s302m_class,
232 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: s302m.c:37
int size
Definition: avcodec.h:1163
static const AVOption s302m_options[]
Definition: s302m.c:207
#define AV_CH_LAYOUT_STEREO
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2727
AVCodec.
Definition: avcodec.h:3181
if()
Definition: avfilter.c:975
uint8_t bits
Definition: crc.c:295
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1993
uint8_t
AVOptions.
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:85
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:789
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
signed 32 bits
Definition: samplefmt.h:63
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVCodec ff_s302m_decoder
Definition: s302m.c:223
av_default_item_name
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
#define AV_CH_LAYOUT_QUAD
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
Libavcodec external API header.
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2046
#define FLAGS
Definition: s302m.c:206
static const AVClass s302m_class
Definition: s302m.c:216
int bit_rate
the average bitrate
Definition: avcodec.h:1305
#define AV_CH_LAYOUT_STEREO_DOWNMIX
ret
Definition: avfilter.c:974
#define AV_CH_LAYOUT_5POINT1_BACK
int non_pcm_mode
Definition: s302m.c:34
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int frame_size
Definition: mxfenc.c:1803
int sample_rate
samples per second
Definition: avcodec.h:1985
main external API structure.
Definition: avcodec.h:1241
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1035
void * buf
Definition: avisynth_c.h:553
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AES3_HEADER_LEN
Definition: s302m.c:30
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
common internal api header.
signed 16 bits
Definition: samplefmt.h:62
void * priv_data
Definition: avcodec.h:1283
int channels
number of audio channels
Definition: avcodec.h:1986
static int s302m_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: s302m.c:96
const uint8_t ff_reverse[256]
Definition: mathtables.c:74
This structure stores compressed data.
Definition: avcodec.h:1139
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
for(j=16;j >0;--j)