1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Crate error type.
//!
//! Every message names what failed, the input, and the next action.
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::path::PathBuf;
/// The result type of every fallible containr call.
pub type Result<T> = std::result::Result<T, Error>;
/// The container format an error comes from.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Container {
/// MP4 or QuickTime MOV.
Mp4,
/// Matroska or WebM.
Matroska,
/// WebM output, which holds a subset of Matroska. Demux errors use
/// [`Container::Matroska`].
WebM,
}
impl Container {
/// The format name, as error messages print it.
pub const fn name(self) -> &'static str {
match self {
Self::Mp4 => "MP4/MOV",
Self::Matroska => "Matroska/WebM",
Self::WebM => "WebM",
}
}
/// What the format calls one structural unit: `box` or `element`.
pub const fn unit(self) -> &'static str {
match self {
Self::Mp4 => "box",
Self::Matroska | Self::WebM => "element",
}
}
/// The output name the remux hint uses.
const fn remux_output(self) -> &'static str {
match self {
Self::Mp4 => "output.mp4",
Self::Matroska => "output.mkv",
Self::WebM => "output.webm",
}
}
}
/// A demux or mux error.
///
/// `path` fields hold the box or element path inside the container, such as
/// `moov/trak[2]/mdia/minf/stbl/stsz` or `Segment/Tracks/TrackEntry[2]/CodecID`.
/// A `trak[n]` or `TrackEntry[n]` index counts from 1 in file order.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// A read or seek on the input failed.
Io {
/// What the demuxer was reading.
what: String,
/// The byte offset of the read.
offset: u64,
/// The underlying error.
source: io::Error,
},
/// A box or element holds bytes that do not decode.
Malformed {
/// The container format.
container: Container,
/// The box or element path.
path: String,
/// The byte offset of the box or element in the input.
offset: u64,
/// What does not decode.
reason: String,
},
/// Two tables or elements disagree, or one refers past another one.
Inconsistent {
/// The container format.
container: Container,
/// The box or element path that fails the check.
path: String,
/// The values that disagree.
reason: String,
},
/// A size from the input exceeds the bound the demuxer allows.
TooLarge {
/// The container format.
container: Container,
/// The box or element path.
path: String,
/// The size the input declares, in bytes or entries.
declared: u64,
/// The largest accepted value.
limit: u64,
},
/// A required box or element is absent.
MissingBox {
/// The container format.
container: Container,
/// The path of the absent box or element.
path: String,
},
/// The input is fragmented MP4 (`moof` or `mvex`), which is out of scope.
Fragmented {
/// The box path that marks the fragmentation.
path: String,
/// The byte offset of that box.
offset: u64,
},
/// No track has the requested id.
TrackNotFound {
/// The requested track id.
track_id: u32,
/// The track ids the input holds.
available: Vec<u32>,
},
/// A packet refers to bytes outside the input.
PacketOutOfBounds {
/// The track id of the packet.
track_id: u32,
/// The 0-based sample index of the packet.
index: u64,
/// The byte offset of the packet.
offset: u64,
/// The packet size in bytes.
size: u32,
/// The input length in bytes.
input_len: u64,
},
/// An error while reading a named file.
InFile {
/// The file.
file: PathBuf,
/// The error.
source: Box<Error>,
},
/// A muxer refuses a stream, codec or item the output container cannot
/// hold.
Unsupported {
/// The output container.
container: Container,
/// What the output cannot hold, such as ``codec `S_TEXT/ASS` ``.
what: String,
/// The container that holds it.
use_instead: Container,
},
/// A packet's decode timestamp is not above the previous packet's.
DtsOutOfOrder {
/// The output track id.
track_id: u32,
/// The 0-based index of the packet in the track.
index: u64,
/// The packet's decode timestamp.
dts: i64,
/// The previous packet's decode timestamp.
previous: i64,
},
/// `pts - dts` fits no composition offset the output can store. Readers
/// treat `ctts` offsets as signed, so the range is -2^31 to 2^31 - 1.
CompositionOffset {
/// The output track id.
track_id: u32,
/// The 0-based index of the packet in the track.
index: u64,
/// The packet's presentation timestamp.
pts: i64,
/// The packet's decode timestamp.
dts: i64,
/// Why the offset does not fit.
reason: String,
},
/// A track needs more than one sample description, as after a
/// parameter-set change. The muxer writes one per track.
SampleDescriptionChange {
/// The track id.
track_id: u32,
/// The number of sample descriptions the track needs.
descriptions: u64,
},
/// The last packet of a track has no duration, and the track has no
/// default duration. A stream copy plan fills this duration from the last
/// two timestamps of an audio or video track.
MissingDuration {
/// The output track id.
track_id: u32,
/// The 0-based index of the packet in the track.
index: u64,
},
/// A packet breaks a muxer rule other than the named ones.
InvalidPacket {
/// The output container.
container: Container,
/// The output track id.
track_id: u32,
/// The 0-based index of the packet in the track.
index: u64,
/// The rule the packet breaks.
reason: String,
},
/// A muxer call is invalid: an option, a track spec, a metadata item, a
/// chapter, or the call order.
InvalidMux {
/// The output container.
container: Container,
/// What is invalid, and the accepted values.
reason: String,
},
}
// The constructors serve the container features. A build with none of them
// compiles the type alone. The `mp4_` constructors build MP4/MOV errors.
// The `mkv_` constructors build Matroska/WebM errors.
#[cfg_attr(not(any(feature = "mp4", feature = "mkv")), allow(dead_code))]
impl Error {
pub(crate) fn io(what: impl Into<String>, offset: u64, source: io::Error) -> Self {
Self::Io {
what: what.into(),
offset,
source,
}
}
#[cfg_attr(not(feature = "mp4"), allow(dead_code))]
pub(crate) fn mp4_malformed(
path: impl Into<String>,
offset: u64,
reason: impl Into<String>,
) -> Self {
Self::malformed_in(Container::Mp4, path, offset, reason)
}
#[cfg_attr(not(feature = "mp4"), allow(dead_code))]
pub(crate) fn mp4_inconsistent(path: impl Into<String>, reason: impl Into<String>) -> Self {
Self::inconsistent_in(Container::Mp4, path, reason)
}
#[cfg_attr(not(feature = "mp4"), allow(dead_code))]
pub(crate) fn mp4_missing(path: impl Into<String>) -> Self {
Self::missing_in(Container::Mp4, path)
}
#[cfg_attr(not(feature = "mp4"), allow(dead_code))]
pub(crate) fn mp4_too_large(path: impl Into<String>, declared: u64, limit: u64) -> Self {
Self::too_large_in(Container::Mp4, path, declared, limit)
}
#[cfg_attr(not(feature = "mkv"), allow(dead_code))]
pub(crate) fn mkv_malformed(
path: impl Into<String>,
offset: u64,
reason: impl Into<String>,
) -> Self {
Self::malformed_in(Container::Matroska, path, offset, reason)
}
#[cfg_attr(not(feature = "mkv"), allow(dead_code))]
pub(crate) fn mkv_inconsistent(path: impl Into<String>, reason: impl Into<String>) -> Self {
Self::inconsistent_in(Container::Matroska, path, reason)
}
#[cfg_attr(not(feature = "mkv"), allow(dead_code))]
pub(crate) fn mkv_missing(path: impl Into<String>) -> Self {
Self::missing_in(Container::Matroska, path)
}
#[cfg_attr(not(feature = "mkv"), allow(dead_code))]
pub(crate) fn mkv_too_large(path: impl Into<String>, declared: u64, limit: u64) -> Self {
Self::too_large_in(Container::Matroska, path, declared, limit)
}
fn malformed_in(
container: Container,
path: impl Into<String>,
offset: u64,
reason: impl Into<String>,
) -> Self {
Self::Malformed {
container,
path: path.into(),
offset,
reason: reason.into(),
}
}
fn inconsistent_in(
container: Container,
path: impl Into<String>,
reason: impl Into<String>,
) -> Self {
Self::Inconsistent {
container,
path: path.into(),
reason: reason.into(),
}
}
fn missing_in(container: Container, path: impl Into<String>) -> Self {
Self::MissingBox {
container,
path: path.into(),
}
}
fn too_large_in(
container: Container,
path: impl Into<String>,
declared: u64,
limit: u64,
) -> Self {
Self::TooLarge {
container,
path: path.into(),
declared,
limit,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io {
what,
offset,
source,
} => write!(
f,
"reading {what} at byte offset {offset} failed: {source}. \
Check that the input is complete and readable"
),
Self::Malformed {
container,
path,
offset,
reason,
} => write!(
f,
"{} `{path}` at byte offset {offset} is malformed: {reason}. \
The input is damaged or not {}. Remux it with \
`ffmpeg -i <input> -c copy <{}>` and open the output",
container.unit(),
container.name(),
container.remux_output()
),
Self::Inconsistent {
container,
path,
reason,
} => write!(
f,
"{} `{path}` is inconsistent: {reason}. The input is damaged. \
Remux it with `ffmpeg -i <input> -c copy <{}>` and open the output",
container.unit(),
container.remux_output()
),
Self::TooLarge {
container,
path,
declared,
limit,
} => write!(
f,
"{} `{path}` declares {declared}, above the limit of {limit}. \
The input is damaged, or it needs a larger limit than this \
demuxer accepts",
container.unit()
),
Self::MissingBox { container, path } => write!(
f,
"required {} `{path}` is absent. The input is truncated or \
not {}. Supply a complete file",
container.unit(),
container.name()
),
Self::Fragmented { path, offset } => write!(
f,
"box `{path}` at byte offset {offset} marks fragmented MP4, \
which this demuxer does not read. Defragment it with \
`ffmpeg -i <input> -c copy <output.mp4>` and open the output"
),
Self::TrackNotFound {
track_id,
available,
} => write!(
f,
"no track has id {track_id}. The input holds track ids \
{available:?}. Pass one of those ids"
),
Self::PacketOutOfBounds {
track_id,
index,
offset,
size,
input_len,
} => write!(
f,
"packet {index} of track {track_id} spans {size} bytes at \
offset {offset}, past the input end at {input_len}. The input \
is truncated. Supply the complete file"
),
Self::InFile { file, source } => write!(f, "{}: {source}", file.display()),
Self::Unsupported {
container,
what,
use_instead,
} => {
// WebM refusals name the `matroska` doc type, since the
// `Matroska/WebM` name also covers WebM.
let target = match (*container, *use_instead) {
(Container::WebM, Container::Matroska) => "Matroska (`DocType::Matroska`)",
(_, other) => other.name(),
};
write!(
f,
"{} output cannot hold {what}. Mux it into {target} instead",
container.name()
)
}
Self::DtsOutOfOrder {
track_id,
index,
dts,
previous,
} => write!(
f,
"packet {index} of track {track_id} has dts {dts}, not above the \
previous dts {previous}. Send the packets of each track in decode \
order, with increasing dts"
),
Self::CompositionOffset {
track_id,
index,
pts,
dts,
reason,
} => write!(
f,
"packet {index} of track {track_id} has pts {pts} and dts {dts}: \
{reason}"
),
Self::SampleDescriptionChange {
track_id,
descriptions,
} => write!(
f,
"track {track_id} needs {descriptions} sample descriptions, as after \
a parameter-set change. The muxer writes one per track. Split the \
stream at the change, or re-encode it with one configuration"
),
Self::MissingDuration { track_id, index } => write!(
f,
"packet {index} of track {track_id} is the last of its track and has \
no duration, and the track spec has no default duration. Set \
`PacketIn::duration` on the last packet, or the default duration \
(`TrackSpec::default_duration` for MP4, \
`MatroskaTrack::default_duration` for Matroska). `StreamCopy::plan` \
fills it from the last two timestamps of an audio or video track, so \
a stream copy gets this error only for a track with one timestamp, a \
`DiscardPadding` that covers that gap, or a subtitle track"
),
Self::InvalidPacket {
container,
track_id,
index,
reason,
} => write!(
f,
"packet {index} of track {track_id} is invalid for {} output: {reason}",
container.name()
),
Self::InvalidMux { container, reason } => {
write!(
f,
"the {} muxer refuses the call: {reason}",
container.name()
)
}
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::Io { source, .. } => Some(source),
Self::InFile { source, .. } => Some(source.as_ref()),
_ => None,
}
}
}