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 
89  return frame_size;
90 }
91 
92 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
93  int *got_frame_ptr, AVPacket *avpkt)
94 {
95  S302Context *s = avctx->priv_data;
96  AVFrame *frame = data;
97  const uint8_t *buf = avpkt->data;
98  int buf_size = avpkt->size;
99  int block_size, ret;
100  int i;
101  int non_pcm_data_type = -1;
102 
103  int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
104  if (frame_size < 0)
105  return frame_size;
106 
107  buf_size -= AES3_HEADER_LEN;
108  buf += AES3_HEADER_LEN;
109 
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)
114  return ret;
115 
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;
119 
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++ = (ff_reverse[buf[2]] << 24) |
124  (ff_reverse[buf[1]] << 16) |
125  (ff_reverse[buf[0]] << 8);
126  *o++ = (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);
130  buf += 7;
131  }
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])
136  break;
137  if (o[i+4] == 0x96F87200U && o[i+5] == 0xA54E1F00) {
138  non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
139  break;
140  }
141  }
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++ = (ff_reverse[buf[2] & 0xf0] << 28) |
146  (ff_reverse[buf[1]] << 20) |
147  (ff_reverse[buf[0]] << 12);
148  *o++ = (ff_reverse[buf[5] & 0xf0] << 28) |
149  (ff_reverse[buf[4]] << 20) |
150  (ff_reverse[buf[3]] << 12);
151  buf += 6;
152  }
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])
157  break;
158  if (o[i+4] == 0x6F872000U && o[i+5] == 0x54E1F000) {
159  non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
160  break;
161  }
162  }
163  } else {
164  uint16_t *o = (uint16_t *)frame->data[0];
165  for (; buf_size > 4; buf_size -= 5) {
166  *o++ = (ff_reverse[buf[1]] << 8) |
167  ff_reverse[buf[0]];
168  *o++ = (ff_reverse[buf[4] & 0xf0] << 12) |
169  (ff_reverse[buf[3]] << 4) |
170  (ff_reverse[buf[2]] >> 4);
171  buf += 5;
172  }
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])
177  break;
178  if (o[i+4] == 0xF872U && o[i+5] == 0x4E1F) {
179  non_pcm_data_type = (o[i+6] & 0x1F);
180  break;
181  }
182  }
183  }
184 
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",
189  non_pcm_data_type);
190  return AVERROR_PATCHWELCOME;
191  }
192  if (s->non_pcm_mode & 1) {
193  return avpkt->size;
194  }
195  }
196 
197  avctx->sample_rate = 48000;
198 
199  *got_frame_ptr = 1;
200 
201  return avpkt->size;
202 }
203 
204 #define FLAGS AV_OPT_FLAG_VIDEO_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"},
211  {NULL}
212 };
213 
214 static const AVClass s302m_class = {
215  "SMPTE 302M Decoder",
219 };
220 
222  .name = "s302m",
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),
228  .capabilities = AV_CODEC_CAP_DR1,
229  .priv_class = &s302m_class,
230 };
#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:1424
static const AVOption s302m_options[]
Definition: s302m.c:205
#define AV_CH_LAYOUT_STEREO
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3003
AVCodec.
Definition: avcodec.h:3472
uint8_t bits
Definition: crc.c:295
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2270
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:87
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1423
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:221
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
const uint8_t ff_reverse[256]
Definition: reverse.c:23
#define AV_CH_LAYOUT_QUAD
const char * name
Name of the codec implementation.
Definition: avcodec.h:3479
Libavcodec external API header.
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2323
#define FLAGS
Definition: s302m.c:204
static const AVClass s302m_class
Definition: s302m.c:214
int bit_rate
the average bitrate
Definition: avcodec.h:1567
#define AV_CH_LAYOUT_STEREO_DOWNMIX
#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:1805
int sample_rate
samples per second
Definition: avcodec.h:2262
main external API structure.
Definition: avcodec.h:1502
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1040
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:523
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:280
signed 16 bits
Definition: samplefmt.h:62
void * priv_data
Definition: avcodec.h:1544
int channels
number of audio channels
Definition: avcodec.h:2263
static int s302m_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: s302m.c:92
This structure stores compressed data.
Definition: avcodec.h:1400
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:857
for(j=16;j >0;--j)