ff-decode 0.14.1

Video and audio decoding - the Rust way
Documentation
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
//! Error types for decoding operations.
//!
//! This module provides the [`DecodeError`] enum which represents all
//! possible errors that can occur during video/audio decoding.

use std::path::PathBuf;
use std::time::Duration;

use thiserror::Error;

use crate::HardwareAccel;

/// Errors that can occur during decoding operations.
///
/// This enum covers all error conditions that may arise when opening,
/// configuring, or decoding media files.
///
/// # Error Categories
///
/// - **File errors**: [`FileNotFound`](Self::FileNotFound)
/// - **Stream errors**: [`NoVideoStream`](Self::NoVideoStream), [`NoAudioStream`](Self::NoAudioStream)
/// - **Codec errors**: [`UnsupportedCodec`](Self::UnsupportedCodec)
/// - **Runtime errors**: [`DecodingFailed`](Self::DecodingFailed), [`SeekFailed`](Self::SeekFailed)
/// - **Hardware errors**: [`HwAccelUnavailable`](Self::HwAccelUnavailable)
/// - **Configuration errors**: [`InvalidOutputDimensions`](Self::InvalidOutputDimensions)
/// - **Internal errors**: [`Ffmpeg`](Self::Ffmpeg), [`Io`](Self::Io)
#[derive(Error, Debug)]
pub enum DecodeError {
    /// File was not found at the specified path.
    ///
    /// This error occurs when attempting to open a file that doesn't exist.
    #[error("File not found: {path}")]
    FileNotFound {
        /// Path that was not found.
        path: PathBuf,
    },

    /// No video stream exists in the media file.
    ///
    /// This error occurs when trying to decode video from a file that
    /// only contains audio or other non-video streams.
    #[error("No video stream found in: {path}")]
    NoVideoStream {
        /// Path to the media file.
        path: PathBuf,
    },

    /// No audio stream exists in the media file.
    ///
    /// This error occurs when trying to decode audio from a file that
    /// only contains video or other non-audio streams.
    #[error("No audio stream found in: {path}")]
    NoAudioStream {
        /// Path to the media file.
        path: PathBuf,
    },

    /// The codec is not supported by this decoder.
    ///
    /// This may occur for uncommon or proprietary codecs that are not
    /// included in the `FFmpeg` build.
    #[error("Codec not supported: {codec}")]
    UnsupportedCodec {
        /// Name of the unsupported codec.
        codec: String,
    },

    /// The decoder for a known codec is absent from this `FFmpeg` build.
    ///
    /// Unlike [`UnsupportedCodec`](Self::UnsupportedCodec), the codec ID is
    /// recognised by `FFmpeg` but the decoder was not compiled in (e.g.
    /// `--enable-decoder=exr` was omitted from the build).
    #[error("Decoder unavailable: {codec} — {hint}")]
    DecoderUnavailable {
        /// Short name of the codec (e.g. `"exr"`).
        codec: String,
        /// Human-readable suggestion for the caller.
        hint: String,
    },

    /// Decoding operation failed at a specific point.
    ///
    /// This can occur due to corrupted data, unexpected stream format,
    /// or internal decoder errors.
    #[error("Decoding failed at {timestamp:?}: {reason}")]
    DecodingFailed {
        /// Timestamp where decoding failed (if known).
        timestamp: Option<Duration>,
        /// Reason for the failure.
        reason: String,
    },

    /// Seek operation failed.
    ///
    /// Seeking may fail for various reasons including corrupted index,
    /// seeking beyond file bounds, or stream format limitations.
    #[error("Seek failed to {target:?}: {reason}")]
    SeekFailed {
        /// Target position of the seek.
        target: Duration,
        /// Reason for the failure.
        reason: String,
    },

    /// Requested hardware acceleration is not available.
    ///
    /// This error occurs when a specific hardware accelerator is requested
    /// but the system doesn't support it. Consider using [`HardwareAccel::Auto`]
    /// for automatic fallback.
    #[error("Hardware acceleration unavailable: {accel:?}")]
    HwAccelUnavailable {
        /// The unavailable hardware acceleration type.
        accel: HardwareAccel,
    },

    /// Output dimensions are invalid.
    ///
    /// Width and height passed to [`output_size`](crate::video::builder::VideoDecoderBuilder::output_size),
    /// [`output_width`](crate::video::builder::VideoDecoderBuilder::output_width), or
    /// [`output_height`](crate::video::builder::VideoDecoderBuilder::output_height) must be
    /// greater than zero and even (required by most pixel formats).
    #[error("Invalid output dimensions: {width}x{height} (must be > 0 and even)")]
    InvalidOutputDimensions {
        /// Requested output width.
        width: u32,
        /// Requested output height.
        height: u32,
    },

    /// `FFmpeg` internal error.
    ///
    /// This wraps errors from the underlying `FFmpeg` library that don't
    /// fit into other categories.
    #[error("ffmpeg error: {message} (code={code})")]
    Ffmpeg {
        /// Raw `FFmpeg` error code (negative integer). `0` when no numeric code is available.
        code: i32,
        /// Human-readable error message from `av_strerror` or an internal description.
        message: String,
    },

    /// I/O error during file operations.
    ///
    /// This wraps standard I/O errors such as permission denied,
    /// disk full, or network errors for remote files.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// The connection timed out before a response was received.
    ///
    /// Maps from `FFmpeg` error code `AVERROR(ETIMEDOUT)`.
    /// `endpoint` is the sanitized URL (password replaced with `***`,
    /// query string removed).
    #[error("network timeout: endpoint={endpoint} — {message} (code={code})")]
    NetworkTimeout {
        /// Raw `FFmpeg` error code.
        code: i32,
        /// Sanitized endpoint URL (no credentials, no query string).
        endpoint: String,
        /// Human-readable error message from `av_strerror`.
        message: String,
    },

    /// The connection was refused or the host could not be reached.
    ///
    /// Maps from `FFmpeg` error codes `AVERROR(ECONNREFUSED)`,
    /// `AVERROR(EHOSTUNREACH)`, `AVERROR(ENETUNREACH)`, and DNS failures.
    /// `endpoint` is the sanitized URL (password replaced with `***`,
    /// query string removed).
    #[error("connection failed: endpoint={endpoint} — {message} (code={code})")]
    ConnectionFailed {
        /// Raw `FFmpeg` error code.
        code: i32,
        /// Sanitized endpoint URL (no credentials, no query string).
        endpoint: String,
        /// Human-readable error message from `av_strerror`.
        message: String,
    },

    /// The stream was interrupted after a connection was established.
    ///
    /// Maps from `AVERROR(EIO)` and `AVERROR_EOF` in a network context.
    /// `endpoint` is the sanitized URL (password replaced with `***`,
    /// query string removed).
    #[error("stream interrupted: endpoint={endpoint} — {message} (code={code})")]
    StreamInterrupted {
        /// Raw `FFmpeg` error code.
        code: i32,
        /// Sanitized endpoint URL (no credentials, no query string).
        endpoint: String,
        /// Human-readable error message from `av_strerror`.
        message: String,
    },

    /// Seeking was requested on a live stream where seeking is not supported.
    ///
    /// Returned by `VideoDecoder::seek()` and `AudioDecoder::seek()` when
    /// `is_live()` returns `true`.
    #[error("seek is not supported on live streams")]
    SeekNotSupported,

    /// A decoded frame exceeds the supported resolution limit.
    #[error("unsupported resolution {width}x{height}: exceeds 32768 in one or both axes")]
    UnsupportedResolution {
        /// Frame width.
        width: u32,
        /// Frame height.
        height: u32,
    },

    /// Too many consecutive corrupt packets — the stream is unrecoverable.
    #[error(
        "stream corrupted: {consecutive_invalid_packets} consecutive invalid packets without recovery"
    )]
    StreamCorrupted {
        /// Number of consecutive invalid packets that triggered the error.
        consecutive_invalid_packets: u32,
    },

    /// No frame was found at or after the requested timestamp.
    ///
    /// Returned by `VideoDecoder::extract_frame()` when EOF is reached before
    /// a frame at or after the target position is found.
    #[error("no frame found at timestamp: {timestamp:?}")]
    NoFrameAtTimestamp {
        /// The timestamp that was requested.
        timestamp: Duration,
    },

    /// An analysis operation failed for a structural reason.
    ///
    /// Returned by tools in [`crate::analysis`] when the operation cannot
    /// proceed (e.g. zero interval, missing audio stream, unsupported format).
    #[error("analysis failed: {reason}")]
    AnalysisFailed {
        /// Human-readable description of why the analysis failed.
        reason: String,
    },
}

impl DecodeError {
    /// Creates a new [`DecodeError::DecodingFailed`] with the given reason.
    ///
    /// # Arguments
    ///
    /// * `reason` - Description of why decoding failed.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_decode::DecodeError;
    ///
    /// let error = DecodeError::decoding_failed("Corrupted frame data");
    /// assert!(error.to_string().contains("Corrupted frame data"));
    /// assert!(error.is_recoverable());
    /// ```
    #[must_use]
    pub fn decoding_failed(reason: impl Into<String>) -> Self {
        Self::DecodingFailed {
            timestamp: None,
            reason: reason.into(),
        }
    }

    /// Creates a new [`DecodeError::DecodingFailed`] with timestamp and reason.
    ///
    /// # Arguments
    ///
    /// * `timestamp` - The timestamp where decoding failed.
    /// * `reason` - Description of why decoding failed.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_decode::DecodeError;
    /// use std::time::Duration;
    ///
    /// let error = DecodeError::decoding_failed_at(
    ///     Duration::from_secs(30),
    ///     "Invalid packet size"
    /// );
    /// assert!(error.to_string().contains("30s"));
    /// assert!(error.is_recoverable());
    /// ```
    #[must_use]
    pub fn decoding_failed_at(timestamp: Duration, reason: impl Into<String>) -> Self {
        Self::DecodingFailed {
            timestamp: Some(timestamp),
            reason: reason.into(),
        }
    }

    /// Creates a new [`DecodeError::SeekFailed`].
    ///
    /// # Arguments
    ///
    /// * `target` - The target position of the failed seek.
    /// * `reason` - Description of why the seek failed.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_decode::DecodeError;
    /// use std::time::Duration;
    ///
    /// let error = DecodeError::seek_failed(
    ///     Duration::from_secs(60),
    ///     "Index not found"
    /// );
    /// assert!(error.to_string().contains("60s"));
    /// assert!(error.is_recoverable());
    /// ```
    #[must_use]
    pub fn seek_failed(target: Duration, reason: impl Into<String>) -> Self {
        Self::SeekFailed {
            target,
            reason: reason.into(),
        }
    }

    /// Creates a new [`DecodeError::DecoderUnavailable`].
    ///
    /// # Arguments
    ///
    /// * `codec` — Short codec name (e.g. `"exr"`).
    /// * `hint` — Human-readable suggestion for the user.
    #[must_use]
    pub fn decoder_unavailable(codec: impl Into<String>, hint: impl Into<String>) -> Self {
        Self::DecoderUnavailable {
            codec: codec.into(),
            hint: hint.into(),
        }
    }

    /// Creates a new [`DecodeError::Ffmpeg`].
    ///
    /// # Arguments
    ///
    /// * `code` - The raw `FFmpeg` error code (negative integer). Pass `0` when no
    ///   numeric code is available.
    /// * `message` - Human-readable description of the error.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_decode::DecodeError;
    ///
    /// let error = DecodeError::ffmpeg(-22, "Invalid data found when processing input");
    /// assert!(error.to_string().contains("Invalid data"));
    /// assert!(error.to_string().contains("code=-22"));
    /// ```
    #[must_use]
    pub fn ffmpeg(code: i32, message: impl Into<String>) -> Self {
        Self::Ffmpeg {
            code,
            message: message.into(),
        }
    }

    /// Returns `true` if this error is recoverable.
    ///
    /// Recoverable errors are those where the operation that raised the error
    /// can be retried (or the decoder can transparently reconnect) without
    /// rebuilding the decoder from scratch.
    ///
    /// | Variant | Recoverable |
    /// |---|---|
    /// | [`DecodingFailed`](Self::DecodingFailed) | ✓ — corrupt frame; skip and continue |
    /// | [`SeekFailed`](Self::SeekFailed) | ✓ — retry at a different position |
    /// | [`NetworkTimeout`](Self::NetworkTimeout) | ✓ — transient; reconnect |
    /// | [`StreamInterrupted`](Self::StreamInterrupted) | ✓ — transient; reconnect |
    /// | all others | ✗ |
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_decode::DecodeError;
    /// use std::time::Duration;
    ///
    /// // Decoding failures are recoverable
    /// assert!(DecodeError::decoding_failed("test").is_recoverable());
    ///
    /// // Seek failures are recoverable
    /// assert!(DecodeError::seek_failed(Duration::from_secs(1), "test").is_recoverable());
    ///
    /// ```
    #[must_use]
    pub fn is_recoverable(&self) -> bool {
        match self {
            Self::DecodingFailed { .. }
            | Self::SeekFailed { .. }
            | Self::NetworkTimeout { .. }
            | Self::StreamInterrupted { .. } => true,
            Self::FileNotFound { .. }
            | Self::NoVideoStream { .. }
            | Self::NoAudioStream { .. }
            | Self::UnsupportedCodec { .. }
            | Self::DecoderUnavailable { .. }
            | Self::HwAccelUnavailable { .. }
            | Self::InvalidOutputDimensions { .. }
            | Self::ConnectionFailed { .. }
            | Self::Io(_)
            | Self::Ffmpeg { .. }
            | Self::SeekNotSupported
            | Self::UnsupportedResolution { .. }
            | Self::StreamCorrupted { .. }
            | Self::NoFrameAtTimestamp { .. }
            | Self::AnalysisFailed { .. } => false,
        }
    }

    /// Returns `true` if this error is fatal.
    ///
    /// Fatal errors indicate that the decoder cannot continue operating and
    /// must be discarded; re-opening or reconfiguring is required.
    ///
    /// | Variant | Fatal |
    /// |---|---|
    /// | [`FileNotFound`](Self::FileNotFound) | ✓ |
    /// | [`NoVideoStream`](Self::NoVideoStream) | ✓ |
    /// | [`NoAudioStream`](Self::NoAudioStream) | ✓ |
    /// | [`UnsupportedCodec`](Self::UnsupportedCodec) | ✓ |
    /// | [`DecoderUnavailable`](Self::DecoderUnavailable) | ✓ |
    /// | [`HwAccelUnavailable`](Self::HwAccelUnavailable) | ✓ — must reconfigure without HW |
    /// | [`InvalidOutputDimensions`](Self::InvalidOutputDimensions) | ✓ — bad config |
    /// | [`ConnectionFailed`](Self::ConnectionFailed) | ✓ — host unreachable |
    /// | [`Io`](Self::Io) | ✓ — I/O failure |
    /// | all others | ✗ |
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_decode::DecodeError;
    /// use std::path::PathBuf;
    ///
    /// // File not found is fatal
    /// assert!(DecodeError::FileNotFound { path: PathBuf::new() }.is_fatal());
    ///
    /// // Unsupported codec is fatal
    /// assert!(DecodeError::UnsupportedCodec { codec: "test".to_string() }.is_fatal());
    ///
    /// ```
    #[must_use]
    pub fn is_fatal(&self) -> bool {
        match self {
            Self::FileNotFound { .. }
            | Self::NoVideoStream { .. }
            | Self::NoAudioStream { .. }
            | Self::UnsupportedCodec { .. }
            | Self::DecoderUnavailable { .. }
            | Self::HwAccelUnavailable { .. }
            | Self::InvalidOutputDimensions { .. }
            | Self::ConnectionFailed { .. }
            | Self::Io(_)
            | Self::StreamCorrupted { .. }
            | Self::AnalysisFailed { .. } => true,
            Self::DecodingFailed { .. }
            | Self::SeekFailed { .. }
            | Self::NetworkTimeout { .. }
            | Self::StreamInterrupted { .. }
            | Self::Ffmpeg { .. }
            | Self::SeekNotSupported
            | Self::UnsupportedResolution { .. }
            | Self::NoFrameAtTimestamp { .. } => false,
        }
    }
}

#[cfg(test)]
#[allow(clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn test_decode_error_display() {
        let error = DecodeError::FileNotFound {
            path: PathBuf::from("/path/to/video.mp4"),
        };
        assert!(error.to_string().contains("File not found"));
        assert!(error.to_string().contains("/path/to/video.mp4"));

        let error = DecodeError::NoVideoStream {
            path: PathBuf::from("/path/to/audio.mp3"),
        };
        assert!(error.to_string().contains("No video stream"));

        let error = DecodeError::UnsupportedCodec {
            codec: "unknown_codec".to_string(),
        };
        assert!(error.to_string().contains("Codec not supported"));
        assert!(error.to_string().contains("unknown_codec"));
    }

    #[test]
    fn test_decoding_failed_constructor() {
        let error = DecodeError::decoding_failed("Corrupted frame data");
        match error {
            DecodeError::DecodingFailed { timestamp, reason } => {
                assert!(timestamp.is_none());
                assert_eq!(reason, "Corrupted frame data");
            }
            _ => panic!("Wrong error type"),
        }
    }

    #[test]
    fn test_decoding_failed_at_constructor() {
        let error = DecodeError::decoding_failed_at(Duration::from_secs(30), "Invalid packet size");
        match error {
            DecodeError::DecodingFailed { timestamp, reason } => {
                assert_eq!(timestamp, Some(Duration::from_secs(30)));
                assert_eq!(reason, "Invalid packet size");
            }
            _ => panic!("Wrong error type"),
        }
    }

    #[test]
    fn test_seek_failed_constructor() {
        let error = DecodeError::seek_failed(Duration::from_secs(60), "Index not found");
        match error {
            DecodeError::SeekFailed { target, reason } => {
                assert_eq!(target, Duration::from_secs(60));
                assert_eq!(reason, "Index not found");
            }
            _ => panic!("Wrong error type"),
        }
    }

    #[test]
    fn test_ffmpeg_constructor() {
        let error = DecodeError::ffmpeg(-22, "AVERROR_INVALIDDATA");
        match error {
            DecodeError::Ffmpeg { code, message } => {
                assert_eq!(code, -22);
                assert_eq!(message, "AVERROR_INVALIDDATA");
            }
            _ => panic!("Wrong error type"),
        }
    }

    #[test]
    fn ffmpeg_should_format_with_code_and_message() {
        let error = DecodeError::ffmpeg(-22, "Invalid data");
        assert!(error.to_string().contains("code=-22"));
        assert!(error.to_string().contains("Invalid data"));
    }

    #[test]
    fn ffmpeg_with_zero_code_should_be_constructible() {
        let error = DecodeError::ffmpeg(0, "allocation failed");
        assert!(matches!(error, DecodeError::Ffmpeg { code: 0, .. }));
    }

    #[test]
    fn decoder_unavailable_should_include_codec_and_hint() {
        let e = DecodeError::decoder_unavailable(
            "exr",
            "Requires FFmpeg built with EXR support (--enable-decoder=exr)",
        );
        assert!(e.to_string().contains("exr"));
        assert!(e.to_string().contains("Requires FFmpeg"));
    }

    #[test]
    fn decoder_unavailable_should_be_fatal() {
        let e = DecodeError::decoder_unavailable("exr", "hint");
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn test_is_recoverable() {
        assert!(DecodeError::decoding_failed("test").is_recoverable());
        assert!(DecodeError::seek_failed(Duration::from_secs(1), "test").is_recoverable());
        assert!(
            !DecodeError::FileNotFound {
                path: PathBuf::new()
            }
            .is_recoverable()
        );
    }

    #[test]
    fn test_is_fatal() {
        assert!(
            DecodeError::FileNotFound {
                path: PathBuf::new()
            }
            .is_fatal()
        );
        assert!(
            DecodeError::NoVideoStream {
                path: PathBuf::new()
            }
            .is_fatal()
        );
        assert!(
            DecodeError::NoAudioStream {
                path: PathBuf::new()
            }
            .is_fatal()
        );
        assert!(
            DecodeError::UnsupportedCodec {
                codec: "test".to_string()
            }
            .is_fatal()
        );
        assert!(!DecodeError::decoding_failed("test").is_fatal());
    }

    #[test]
    fn test_io_error_conversion() {
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let decode_error: DecodeError = io_error.into();
        assert!(matches!(decode_error, DecodeError::Io(_)));
    }

    #[test]
    fn test_hw_accel_unavailable() {
        let error = DecodeError::HwAccelUnavailable {
            accel: HardwareAccel::Nvdec,
        };
        assert!(
            error
                .to_string()
                .contains("Hardware acceleration unavailable")
        );
        assert!(error.to_string().contains("Nvdec"));
    }

    // ── is_fatal / is_recoverable exhaustive coverage ────────────────────────

    #[test]
    fn file_not_found_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::FileNotFound {
            path: PathBuf::new(),
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn no_video_stream_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::NoVideoStream {
            path: PathBuf::new(),
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn no_audio_stream_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::NoAudioStream {
            path: PathBuf::new(),
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn unsupported_codec_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::UnsupportedCodec {
            codec: "test".to_string(),
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn decoder_unavailable_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::decoder_unavailable("exr", "hint");
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn decoding_failed_should_be_recoverable_and_not_fatal() {
        let e = DecodeError::decoding_failed("corrupt frame");
        assert!(e.is_recoverable());
        assert!(!e.is_fatal());
    }

    #[test]
    fn seek_failed_should_be_recoverable_and_not_fatal() {
        let e = DecodeError::seek_failed(Duration::from_secs(5), "index not found");
        assert!(e.is_recoverable());
        assert!(!e.is_fatal());
    }

    #[test]
    fn hw_accel_unavailable_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::HwAccelUnavailable {
            accel: HardwareAccel::Nvdec,
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn invalid_output_dimensions_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::InvalidOutputDimensions {
            width: 0,
            height: 0,
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn ffmpeg_error_should_be_neither_fatal_nor_recoverable() {
        let e = DecodeError::ffmpeg(-22, "AVERROR_INVALIDDATA");
        assert!(!e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn io_error_should_be_fatal_and_not_recoverable() {
        let e: DecodeError =
            std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied").into();
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn network_timeout_should_be_recoverable_and_not_fatal() {
        let e = DecodeError::NetworkTimeout {
            code: -110,
            endpoint: "rtmp://example.com/live".to_string(),
            message: "timed out".to_string(),
        };
        assert!(e.is_recoverable());
        assert!(!e.is_fatal());
    }

    #[test]
    fn connection_failed_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::ConnectionFailed {
            code: -111,
            endpoint: "rtmp://example.com/live".to_string(),
            message: "connection refused".to_string(),
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn stream_interrupted_should_be_recoverable_and_not_fatal() {
        let e = DecodeError::StreamInterrupted {
            code: -5,
            endpoint: "rtmp://example.com/live".to_string(),
            message: "I/O error".to_string(),
        };
        assert!(e.is_recoverable());
        assert!(!e.is_fatal());
    }

    #[test]
    fn seek_not_supported_should_be_neither_fatal_nor_recoverable() {
        let e = DecodeError::SeekNotSupported;
        assert!(!e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn unsupported_resolution_display_should_contain_width_x_height() {
        let e = DecodeError::UnsupportedResolution {
            width: 40000,
            height: 480,
        };
        let msg = e.to_string();
        assert!(msg.contains("40000x480"), "expected '40000x480' in '{msg}'");
    }

    #[test]
    fn unsupported_resolution_display_should_contain_axes_hint() {
        let e = DecodeError::UnsupportedResolution {
            width: 640,
            height: 40000,
        };
        let msg = e.to_string();
        assert!(msg.contains("32768"), "expected '32768' limit in '{msg}'");
    }

    #[test]
    fn unsupported_resolution_should_be_neither_fatal_nor_recoverable() {
        let e = DecodeError::UnsupportedResolution {
            width: 40000,
            height: 40000,
        };
        assert!(!e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn stream_corrupted_display_should_contain_packet_count() {
        let e = DecodeError::StreamCorrupted {
            consecutive_invalid_packets: 32,
        };
        let msg = e.to_string();
        assert!(msg.contains("32"), "expected '32' in '{msg}'");
    }

    #[test]
    fn stream_corrupted_display_should_mention_consecutive() {
        let e = DecodeError::StreamCorrupted {
            consecutive_invalid_packets: 32,
        };
        let msg = e.to_string();
        assert!(
            msg.contains("consecutive"),
            "expected 'consecutive' in '{msg}'"
        );
    }

    #[test]
    fn stream_corrupted_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::StreamCorrupted {
            consecutive_invalid_packets: 32,
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn decode_error_no_frame_at_timestamp_should_display_correctly() {
        let e = DecodeError::NoFrameAtTimestamp {
            timestamp: Duration::from_secs(5),
        };
        let msg = e.to_string();
        assert!(
            msg.contains("no frame found at timestamp"),
            "unexpected message: {msg}"
        );
        assert!(msg.contains("5s"), "expected timestamp in message: {msg}");
    }

    #[test]
    fn decode_error_analysis_failed_should_display_correctly() {
        let e = DecodeError::AnalysisFailed {
            reason: "interval must be non-zero".to_string(),
        };
        let msg = e.to_string();
        assert!(msg.contains("analysis failed"), "unexpected message: {msg}");
        assert!(
            msg.contains("interval must be non-zero"),
            "expected reason in message: {msg}"
        );
    }

    #[test]
    fn no_frame_at_timestamp_should_be_neither_fatal_nor_recoverable() {
        let e = DecodeError::NoFrameAtTimestamp {
            timestamp: Duration::from_secs(10),
        };
        assert!(!e.is_fatal());
        assert!(!e.is_recoverable());
    }

    #[test]
    fn analysis_failed_should_be_fatal_and_not_recoverable() {
        let e = DecodeError::AnalysisFailed {
            reason: "zero interval".to_string(),
        };
        assert!(e.is_fatal());
        assert!(!e.is_recoverable());
    }
}