FFmpeg
Loading...
Searching...
No Matches
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
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
36static 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);
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);
80 }
81
82 *offset -= 4;
83 *offset *= (word_bits + 7 >> 3) * 2;
84 return 0;
85}
86
87static 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];
96 if (!IS_LE_MARKER(state))
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
138static 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
174 ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
175 else
176 bswap_buf24(pkt->data, pkt->size);
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};
const FFInputFormat ff_s337m_demuxer
Definition s337m.c:191
Main libavformat public API header.
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
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:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition avformat.h:612
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
unsigned int avio_rl24(AVIOContext *s)
Definition aviobuf.c:725
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
#define max(a, b)
static AVPacket * pkt
static struct @346255127015250356166251341105367306144006377143 state
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_DOLBY_E
Definition codec_id.h:537
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RL24(x)
#define AV_RL16(p)
unsigned offset
Definition libaomenc.c:763
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FFSWAP(type, a, b)
Definition macros.h:52
const char data[16]
Definition mxf.c:149
static int s337m_read_header(AVFormatContext *s)
Definition s337m.c:132
static int s337m_probe(const AVProbeData *p)
Definition s337m.c:87
#define IS_20LE_MARKER(state)
Definition s337m.c:32
#define IS_LE_MARKER(state)
Definition s337m.c:34
#define IS_16LE_MARKER(state)
Definition s337m.c:31
static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition s337m.c:146
static void bswap_buf24(uint8_t *data, int size)
Definition s337m.c:138
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
#define FF_ARRAY_ELEMS(a)
void ff_spdif_bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
Definition spdif.c:26
unsigned int pos
Definition spdifenc.c:431
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
enum AVStreamParseType need_parsing
Definition internal.h:321
int size