FFmpeg
s337m.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 foo86
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/intreadwrite.h"
22 #include "avformat.h"
23 #include "internal.h"
24 #include "spdif.h"
25 
26 #define MARKER_16LE 0x72F81F4E
27 #define MARKER_20LE 0x20876FF0E154
28 #define MARKER_24LE 0x72F8961F4EA5
29 
30 #define IS_16LE_MARKER(state) ((state & 0xFFFFFFFF) == MARKER_16LE)
31 #define IS_20LE_MARKER(state) ((state & 0xF0FFFFF0FFFF) == MARKER_20LE)
32 #define IS_24LE_MARKER(state) ((state & 0xFFFFFFFFFFFF) == MARKER_24LE)
33 #define IS_LE_MARKER(state) (IS_16LE_MARKER(state) || IS_20LE_MARKER(state) || IS_24LE_MARKER(state))
34 
35 static int s337m_get_offset_and_codec(void *avc,
36  uint64_t state,
37  int data_type, int data_size,
38  int *offset, enum AVCodecID *codec)
39 {
40  int word_bits;
41 
42  if (IS_16LE_MARKER(state)) {
43  word_bits = 16;
44  } else if (IS_20LE_MARKER(state)) {
45  data_type >>= 8;
46  data_size >>= 4;
47  word_bits = 20;
48  } else {
49  data_type >>= 8;
50  word_bits = 24;
51  }
52 
53  if ((data_type & 0x1F) != 0x1C) {
54  if (avc)
55  avpriv_report_missing_feature(avc, "Data type %#x in SMPTE 337M", data_type & 0x1F);
56  return AVERROR_PATCHWELCOME;
57  }
58 
59  if (codec)
60  *codec = AV_CODEC_ID_DOLBY_E;
61 
62  switch (data_size / word_bits) {
63  case 3648:
64  *offset = 1920;
65  break;
66  case 3644:
67  *offset = 2002;
68  break;
69  case 3640:
70  *offset = 2000;
71  break;
72  case 3040:
73  *offset = 1601;
74  break;
75  default:
76  if (avc)
77  avpriv_report_missing_feature(avc, "Dolby E data size %d in SMPTE 337M", data_size);
78  return AVERROR_PATCHWELCOME;
79  }
80 
81  *offset -= 4;
82  *offset *= (word_bits + 7 >> 3) * 2;
83  return 0;
84 }
85 
86 static int s337m_probe(const AVProbeData *p)
87 {
88  uint64_t state = 0;
89  int markers[3] = { 0 };
90  int i, pos, sum, max, data_type, data_size, offset;
91  uint8_t *buf;
92 
93  for (pos = 0; pos < p->buf_size; pos++) {
94  state = (state << 8) | p->buf[pos];
96  continue;
97 
98  buf = p->buf + pos + 1;
99  if (IS_16LE_MARKER(state)) {
100  data_type = AV_RL16(buf );
101  data_size = AV_RL16(buf + 2);
102  } else {
103  data_type = AV_RL24(buf );
104  data_size = AV_RL24(buf + 3);
105  }
106 
107  if (s337m_get_offset_and_codec(NULL, state, data_type, data_size, &offset, NULL))
108  continue;
109 
110  i = IS_16LE_MARKER(state) ? 0 : IS_20LE_MARKER(state) ? 1 : 2;
111  markers[i]++;
112 
113  pos += IS_16LE_MARKER(state) ? 4 : 6;
114  pos += offset;
115  state = 0;
116  }
117 
118  sum = max = 0;
119  for (i = 0; i < FF_ARRAY_ELEMS(markers); i++) {
120  sum += markers[i];
121  if (markers[max] < markers[i])
122  max = i;
123  }
124 
125  if (markers[max] > 3 && markers[max] * 4 > sum * 3)
126  return AVPROBE_SCORE_EXTENSION + 1;
127 
128  return 0;
129 }
130 
132 {
133  s->ctx_flags |= AVFMTCTX_NOHEADER;
134  return 0;
135 }
136 
137 static void bswap_buf24(uint8_t *data, int size)
138 {
139  int i;
140 
141  for (i = 0; i < size / 3; i++, data += 3)
142  FFSWAP(uint8_t, data[0], data[2]);
143 }
144 
146 {
147  AVIOContext *pb = s->pb;
148  uint64_t state = 0;
149  int ret, data_type, data_size, offset;
150  enum AVCodecID codec;
151 
152  while (!IS_LE_MARKER(state)) {
153  state = (state << 8) | avio_r8(pb);
154  if (avio_feof(pb))
155  return AVERROR_EOF;
156  }
157 
158  if (IS_16LE_MARKER(state)) {
159  data_type = avio_rl16(pb);
160  data_size = avio_rl16(pb);
161  } else {
162  data_type = avio_rl24(pb);
163  data_size = avio_rl24(pb);
164  }
165 
166  if ((ret = s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &codec)) < 0)
167  return ret;
168 
169  if ((ret = av_get_packet(pb, pkt, offset)) != offset)
170  return ret < 0 ? ret : AVERROR_EOF;
171 
172  if (IS_16LE_MARKER(state))
173  ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
174  else
176 
177  if (!s->nb_streams) {
179  if (!st) {
180  return AVERROR(ENOMEM);
181  }
183  st->codecpar->codec_id = codec;
185  }
186 
187  return 0;
188 }
189 
191  .name = "s337m",
192  .long_name = NULL_IF_CONFIG_SMALL("SMPTE 337M"),
193  .read_probe = s337m_probe,
194  .read_header = s337m_read_header,
195  .read_packet = s337m_read_packet,
196  .flags = AVFMT_GENERIC_INDEX,
197 };
ff_s337m_demuxer
const AVInputFormat ff_s337m_demuxer
Definition: s337m.c:190
bswap_buf24
static void bswap_buf24(uint8_t *data, int size)
Definition: s337m.c:137
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:237
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:57
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVPacket::data
uint8_t * data
Definition: packet.h:374
data
const char data[16]
Definition: mxf.c:143
s337m_read_packet
static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: s337m.c:145
max
#define max(a, b)
Definition: cuda_runtime.h:33
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:456
IS_20LE_MARKER
#define IS_20LE_MARKER(state)
Definition: s337m.c:31
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:407
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:482
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:656
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ff_spdif_bswap_buf16
void ff_spdif_bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
Definition: spdif.c:26
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
IS_16LE_MARKER
#define IS_16LE_MARKER(state)
Definition: s337m.c:30
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:455
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AV_CODEC_ID_DOLBY_E
@ AV_CODEC_ID_DOLBY_E
Definition: codec_id.h:511
if
if(ret)
Definition: filter_design.txt:179
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:380
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1164
s337m_read_header
static int s337m_read_header(AVFormatContext *s)
Definition: s337m.c:131
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:453
s337m_probe
static int s337m_probe(const AVProbeData *p)
Definition: s337m.c:86
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:463
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
AVPacket::size
int size
Definition: packet.h:375
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
size
int size
Definition: twinvq_data.h:10344
state
static struct @327 state
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:751
IS_LE_MARKER
#define IS_LE_MARKER(state)
Definition: s337m.c:33
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:102
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:948
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:799
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:351
s337m_get_offset_and_codec
static int s337m_get_offset_and_codec(void *avc, uint64_t state, int data_type, int data_size, int *offset, enum AVCodecID *codec)
Definition: s337m.c:35
spdif.h
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:375