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 "demux.h"
24 #include "internal.h"
25 #include "spdif.h"
26 
27 #define MARKER_16LE 0x72F81F4E
28 #define MARKER_20LE 0x20876FF0E154
29 #define MARKER_24LE 0x72F8961F4EA5
30 
31 #define IS_16LE_MARKER(state) ((state & 0xFFFFFFFF) == MARKER_16LE)
32 #define IS_20LE_MARKER(state) ((state & 0xF0FFFFF0FFFF) == MARKER_20LE)
33 #define IS_24LE_MARKER(state) ((state & 0xFFFFFFFFFFFF) == MARKER_24LE)
34 #define IS_LE_MARKER(state) (IS_16LE_MARKER(state) || IS_20LE_MARKER(state) || IS_24LE_MARKER(state))
35 
36 static int s337m_get_offset_and_codec(void *avc,
37  uint64_t state,
38  int data_type, int data_size,
39  int *offset, enum AVCodecID *codec)
40 {
41  int word_bits;
42 
43  if (IS_16LE_MARKER(state)) {
44  word_bits = 16;
45  } else if (IS_20LE_MARKER(state)) {
46  data_type >>= 8;
47  data_size >>= 4;
48  word_bits = 20;
49  } else {
50  data_type >>= 8;
51  word_bits = 24;
52  }
53 
54  if ((data_type & 0x1F) != 0x1C) {
55  if (avc)
56  avpriv_report_missing_feature(avc, "Data type %#x in SMPTE 337M", data_type & 0x1F);
57  return AVERROR_PATCHWELCOME;
58  }
59 
60  if (codec)
61  *codec = AV_CODEC_ID_DOLBY_E;
62 
63  switch (data_size / word_bits) {
64  case 3648:
65  *offset = 1920;
66  break;
67  case 3644:
68  *offset = 2002;
69  break;
70  case 3640:
71  *offset = 2000;
72  break;
73  case 3040:
74  *offset = 1601;
75  break;
76  default:
77  if (avc)
78  avpriv_report_missing_feature(avc, "Dolby E data size %d in SMPTE 337M", data_size);
79  return AVERROR_PATCHWELCOME;
80  }
81 
82  *offset -= 4;
83  *offset *= (word_bits + 7 >> 3) * 2;
84  return 0;
85 }
86 
87 static int s337m_probe(const AVProbeData *p)
88 {
89  uint64_t state = 0;
90  int markers[3] = { 0 };
91  int i, pos, sum, max, data_type, data_size, offset;
92  uint8_t *buf;
93 
94  for (pos = 0; pos < p->buf_size; pos++) {
95  state = (state << 8) | p->buf[pos];
97  continue;
98 
99  buf = p->buf + pos + 1;
100  if (IS_16LE_MARKER(state)) {
101  data_type = AV_RL16(buf );
102  data_size = AV_RL16(buf + 2);
103  } else {
104  data_type = AV_RL24(buf );
105  data_size = AV_RL24(buf + 3);
106  }
107 
108  if (s337m_get_offset_and_codec(NULL, state, data_type, data_size, &offset, NULL))
109  continue;
110 
111  i = IS_16LE_MARKER(state) ? 0 : IS_20LE_MARKER(state) ? 1 : 2;
112  markers[i]++;
113 
114  pos += IS_16LE_MARKER(state) ? 4 : 6;
115  pos += offset;
116  state = 0;
117  }
118 
119  sum = max = 0;
120  for (i = 0; i < FF_ARRAY_ELEMS(markers); i++) {
121  sum += markers[i];
122  if (markers[max] < markers[i])
123  max = i;
124  }
125 
126  if (markers[max] > 3 && markers[max] * 4 > sum * 3)
127  return AVPROBE_SCORE_EXTENSION + 1;
128 
129  return 0;
130 }
131 
133 {
134  s->ctx_flags |= AVFMTCTX_NOHEADER;
135  return 0;
136 }
137 
138 static void bswap_buf24(uint8_t *data, int size)
139 {
140  int i;
141 
142  for (i = 0; i < size / 3; i++, data += 3)
143  FFSWAP(uint8_t, data[0], data[2]);
144 }
145 
147 {
148  AVIOContext *pb = s->pb;
149  uint64_t state = 0;
150  int ret, data_type, data_size, offset;
151  enum AVCodecID codec;
152 
153  while (!IS_LE_MARKER(state)) {
154  state = (state << 8) | avio_r8(pb);
155  if (avio_feof(pb))
156  return AVERROR_EOF;
157  }
158 
159  if (IS_16LE_MARKER(state)) {
160  data_type = avio_rl16(pb);
161  data_size = avio_rl16(pb);
162  } else {
163  data_type = avio_rl24(pb);
164  data_size = avio_rl24(pb);
165  }
166 
167  if ((ret = s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &codec)) < 0)
168  return ret;
169 
170  if ((ret = av_get_packet(pb, pkt, offset)) != offset)
171  return ret < 0 ? ret : AVERROR_EOF;
172 
173  if (IS_16LE_MARKER(state))
174  ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
175  else
177 
178  if (!s->nb_streams) {
180  if (!st) {
181  return AVERROR(ENOMEM);
182  }
184  st->codecpar->codec_id = codec;
186  }
187 
188  return 0;
189 }
190 
192  .p.name = "s337m",
193  .p.long_name = NULL_IF_CONFIG_SMALL("SMPTE 337M"),
194  .p.flags = AVFMT_GENERIC_INDEX,
195  .read_probe = s337m_probe,
196  .read_header = s337m_read_header,
197  .read_packet = s337m_read_packet,
198 };
bswap_buf24
static void bswap_buf24(uint8_t *data, int size)
Definition: s337m.c:138
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVPacket::data
uint8_t * data
Definition: packet.h:524
data
const char data[16]
Definition: mxf.c:148
s337m_read_packet
static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: s337m.c:146
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:454
IS_20LE_MARKER
#define IS_20LE_MARKER(state)
Definition: s337m.c:32
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:417
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
pkt
AVPacket * pkt
Definition: movenc.c:60
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:198
IS_16LE_MARKER
#define IS_16LE_MARKER(state)
Definition: s337m.c:31
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
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:524
if
if(ret)
Definition: filter_design.txt:179
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:386
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
ff_s337m_demuxer
const FFInputFormat ff_s337m_demuxer
Definition: s337m.c:191
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1206
s337m_read_header
static int s337m_read_header(AVFormatContext *s)
Definition: s337m.c:132
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
s337m_probe
static int s337m_probe(const AVProbeData *p)
Definition: s337m.c:87
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:525
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:94
size
int size
Definition: twinvq_data.h:10344
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
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
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:256
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:722
state
static struct @416 state
demux.h
IS_LE_MARKER
#define IS_LE_MARKER(state)
Definition: s337m.c:34
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
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:594
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:501
FFInputFormat
Definition: demux.h:37
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:36
spdif.h
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346