FFmpeg  4.4.5
3dostr.c
Go to the documentation of this file.
1 /*
2  * 3DO STR demuxer
3  * Copyright (c) 2015 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 
26 static int threedostr_probe(const AVProbeData *p)
27 {
28  for (int i = 0; i < p->buf_size;) {
29  unsigned chunk = AV_RL32(p->buf + i);
30  unsigned size = AV_RB32(p->buf + i + 4);
31 
32  if (size < 8 || p->buf_size - i < size)
33  return 0;
34  i += 8;
35  size -= 8;
36  switch (chunk) {
37  case MKTAG('C','T','R','L'):
38  break;
39  case MKTAG('S','N','D','S'):
40  if (size < 56)
41  return 0;
42  i += 8;
43  if (AV_RL32(p->buf + i) != MKTAG('S','H','D','R'))
44  return 0;
45  i += 28;
46 
47  if (AV_RB32(p->buf + i) <= 0)
48  return 0;
49  i += 4;
50  if (AV_RB32(p->buf + i) <= 0)
51  return 0;
52  i += 4;
53  if (AV_RL32(p->buf + i) == MKTAG('S','D','X','2'))
54  return AVPROBE_SCORE_MAX;
55  else
56  return 0;
57  break;
58  case MKTAG('S','H','D','R'):
59  if (size > 0x78) {
60  i += 0x78;
61  size -= 0x78;
62  }
63  break;
64  default:
65  break;
66  }
67 
68  i += size;
69  }
70 
71  return 0;
72 }
73 
75 {
76  unsigned chunk, codec = 0, size, ctrl_size = -1, found_shdr = 0;
77  AVStream *st;
78 
79  while (!avio_feof(s->pb) && !found_shdr) {
80  chunk = avio_rl32(s->pb);
81  size = avio_rb32(s->pb);
82 
83  if (size < 8)
84  return AVERROR_INVALIDDATA;
85  size -= 8;
86 
87  switch (chunk) {
88  case MKTAG('C','T','R','L'):
89  ctrl_size = size;
90  break;
91  case MKTAG('S','N','D','S'):
92  if (size < 56)
93  return AVERROR_INVALIDDATA;
94  avio_skip(s->pb, 8);
95  if (avio_rl32(s->pb) != MKTAG('S','H','D','R'))
96  return AVERROR_INVALIDDATA;
97  avio_skip(s->pb, 24);
98 
99  st = avformat_new_stream(s, NULL);
100  if (!st)
101  return AVERROR(ENOMEM);
102 
104  st->codecpar->sample_rate = avio_rb32(s->pb);
105  st->codecpar->channels = avio_rb32(s->pb);
106  if (st->codecpar->channels <= 0 || st->codecpar->sample_rate <= 0)
107  return AVERROR_INVALIDDATA;
108  codec = avio_rl32(s->pb);
109  avio_skip(s->pb, 4);
110  if (ctrl_size == 20 || ctrl_size == 3 || ctrl_size == -1)
111  st->duration = (avio_rb32(s->pb) - 1) / st->codecpar->channels;
112  else
113  st->duration = avio_rb32(s->pb) * 16 / st->codecpar->channels;
114  size -= 56;
115  found_shdr = 1;
116  break;
117  case MKTAG('S','H','D','R'):
118  if (size > 0x78) {
119  avio_skip(s->pb, 0x74);
120  size -= 0x78;
121  if (avio_rl32(s->pb) == MKTAG('C','T','R','L') && size > 4) {
122  ctrl_size = avio_rb32(s->pb);
123  size -= 4;
124  }
125  }
126  break;
127  default:
128  av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
129  break;
130  }
131 
132  avio_skip(s->pb, size);
133  }
134 
135  switch (codec) {
136  case MKTAG('S','D','X','2'):
138  st->codecpar->block_align = 1 * st->codecpar->channels;
139  break;
140  default:
141  avpriv_request_sample(s, "codec %X", codec);
142  return AVERROR_PATCHWELCOME;
143  }
144 
145  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
146 
147  return 0;
148 }
149 
151 {
152  unsigned chunk, size;
153  AVStream *st = s->streams[0];
154  int64_t pos;
155  int ret = 0;
156 
157  while (!avio_feof(s->pb)) {
158  pos = avio_tell(s->pb);
159  chunk = avio_rl32(s->pb);
160  size = avio_rb32(s->pb);
161 
162  if (!size)
163  continue;
164 
165  if (size < 8)
166  return AVERROR_INVALIDDATA;
167  size -= 8;
168 
169  switch (chunk) {
170  case MKTAG('S','N','D','S'):
171  if (size <= 16)
172  return AVERROR_INVALIDDATA;
173  avio_skip(s->pb, 8);
174  if (avio_rl32(s->pb) != MKTAG('S','S','M','P'))
175  return AVERROR_INVALIDDATA;
176  avio_skip(s->pb, 4);
177  size -= 16;
178  ret = av_get_packet(s->pb, pkt, size);
179  pkt->pos = pos;
180  pkt->stream_index = 0;
181  pkt->duration = size / st->codecpar->channels;
182  return ret;
183  default:
184  av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
185  break;
186  }
187 
188  avio_skip(s->pb, size);
189  }
190 
191  return AVERROR_EOF;
192 }
193 
195  .name = "3dostr",
196  .long_name = NULL_IF_CONFIG_SMALL("3DO STR"),
197  .read_probe = threedostr_probe,
198  .read_header = threedostr_read_header,
199  .read_packet = threedostr_read_packet,
200  .extensions = "str",
201  .flags = AVFMT_GENERIC_INDEX,
202 };
static int threedostr_probe(const AVProbeData *p)
Definition: 3dostr.c:26
AVInputFormat ff_threedostr_demuxer
Definition: 3dostr.c:194
static int threedostr_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: 3dostr.c:150
static int threedostr_read_header(AVFormatContext *s)
Definition: 3dostr.c:74
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
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:310
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:364
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:750
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:781
#define AV_RB32
Definition: intreadwrite.h:130
#define AV_RL32
Definition: intreadwrite.h:146
#define s(width, name)
Definition: cbs_vp9.c:257
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
@ AV_CODEC_ID_SDX2_DPCM
Definition: codec_id.h:419
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
int i
Definition: input.c:407
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4945
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
unsigned int pos
Definition: spdifenc.c:412
int channels
Audio only.
Definition: codec_par.h:166
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int block_align
Audio only.
Definition: codec_par.h:177
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
#define avpriv_request_sample(...)
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
int size