// <copyright file="MediaStream.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

#nullable disable
#pragma warning disable CS1591

namespace MediaBrowser.Model.Entities;

using global::System;
using global::System.Collections.Frozen;
using global::System.Collections.Generic;
using global::System.ComponentModel;
using global::System.Globalization;
using global::System.Linq;
using global::System.Text;
using global::System.Text.Json.Serialization;
using Jellyfin.Data.Enums;
using Jellyfin.Extensions;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.MediaInfo;

/// <summary>
/// Class MediaStream.
/// </summary>
public class MediaStream
{
    /// <summary>
    /// Special ISO 639-2 language codes that should not be displayed in the UI.
    /// </summary>
    private static readonly string[] SpecialCodes =
    {
           // Uncoded languages.
           "mis",

           // Multiple languages.
           "mul",

           // Undetermined.
           "und",

           // No linguistic content; not applicable.
           "zxx",
    };

    /// <summary>
    /// Gets or sets the codec.
    /// </summary>
    /// <value>The codec.</value>
    public string Codec { get; set; }

    /// <summary>
    /// Gets or sets the codec tag.
    /// </summary>
    /// <value>The codec tag.</value>
    public string CodecTag { get; set; }

    /// <summary>
    /// Gets or sets the language.
    /// </summary>
    /// <value>The language.</value>
    public string Language { get; set; }

    /// <summary>
    /// Gets or sets the color range.
    /// </summary>
    /// <value>The color range.</value>
    public string ColorRange { get; set; }

    /// <summary>
    /// Gets or sets the color space.
    /// </summary>
    /// <value>The color space.</value>
    public string ColorSpace { get; set; }

    /// <summary>
    /// Gets or sets the color transfer.
    /// </summary>
    /// <value>The color transfer.</value>
    public string ColorTransfer { get; set; }

    /// <summary>
    /// Gets or sets the color primaries.
    /// </summary>
    /// <value>The color primaries.</value>
    public string ColorPrimaries { get; set; }

    /// <summary>
    /// Gets or sets the Dolby Vision version major.
    /// </summary>
    /// <value>The Dolby Vision version major.</value>
    public int? DvVersionMajor { get; set; }

    /// <summary>
    /// Gets or sets the Dolby Vision version minor.
    /// </summary>
    /// <value>The Dolby Vision version minor.</value>
    public int? DvVersionMinor { get; set; }

    /// <summary>
    /// Gets or sets the Dolby Vision profile.
    /// </summary>
    /// <value>The Dolby Vision profile.</value>
    public int? DvProfile { get; set; }

    /// <summary>
    /// Gets or sets the Dolby Vision level.
    /// </summary>
    /// <value>The Dolby Vision level.</value>
    public int? DvLevel { get; set; }

    /// <summary>
    /// Gets or sets the Dolby Vision rpu present flag.
    /// </summary>
    /// <value>The Dolby Vision rpu present flag.</value>
    public int? RpuPresentFlag { get; set; }

    /// <summary>
    /// Gets or sets the Dolby Vision el present flag.
    /// </summary>
    /// <value>The Dolby Vision el present flag.</value>
    public int? ElPresentFlag { get; set; }

    /// <summary>
    /// Gets or sets the Dolby Vision bl present flag.
    /// </summary>
    /// <value>The Dolby Vision bl present flag.</value>
    public int? BlPresentFlag { get; set; }

    /// <summary>
    /// Gets or sets the Dolby Vision bl signal compatibility id.
    /// </summary>
    /// <value>The Dolby Vision bl signal compatibility id.</value>
    public int? DvBlSignalCompatibilityId { get; set; }

    /// <summary>
    /// Gets or sets the Rotation in degrees.
    /// </summary>
    /// <value>The video rotation.</value>
    public int? Rotation { get; set; }

    /// <summary>
    /// Gets or sets the comment.
    /// </summary>
    /// <value>The comment.</value>
    public string Comment { get; set; }

    /// <summary>
    /// Gets or sets the time base.
    /// </summary>
    /// <value>The time base.</value>
    public string TimeBase { get; set; }

    /// <summary>
    /// Gets or sets the codec time base.
    /// </summary>
    /// <value>The codec time base.</value>
    public string CodecTimeBase { get; set; }

    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    /// <value>The title.</value>
    public string Title { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether the HDR10+ present flag.
    /// </summary>
    /// <value>The HDR10+ present flag.</value>
    public bool? Hdr10PlusPresentFlag { get; set; }

    /// <summary>
    /// Gets the video range.
    /// </summary>
    /// <value>The video range.</value>
    [DefaultValue(VideoRange.Unknown)]
    public VideoRange VideoRange
    {
        get
        {
            var (videoRange, _) = this.GetVideoColorRange();

            return videoRange;
        }
    }

    /// <summary>
    /// Gets the video range type.
    /// </summary>
    /// <value>The video range type.</value>
    [DefaultValue(VideoRangeType.Unknown)]
    public VideoRangeType VideoRangeType
    {
        get
        {
            var (_, videoRangeType) = this.GetVideoColorRange();

            return videoRangeType;
        }
    }

    /// <summary>
    /// Gets the video dovi title.
    /// </summary>
    /// <value>The video dovi title.</value>
    public string VideoDoViTitle
    {
        get
        {
            var dvProfile = this.DvProfile;
            var rpuPresentFlag = this.RpuPresentFlag == 1;
            var blPresentFlag = this.BlPresentFlag == 1;
            var dvBlCompatId = this.DvBlSignalCompatibilityId;

            if (rpuPresentFlag
                && blPresentFlag
                && (dvProfile == 4
                    || dvProfile == 5
                    || dvProfile == 7
                    || dvProfile == 8
                    || dvProfile == 9
                    || dvProfile == 10))
            {
                var title = "Dolby Vision Profile " + dvProfile;

                if (dvBlCompatId > 0)
                {
                    title += "." + dvBlCompatId;
                }

                return dvBlCompatId switch
                {
                    1 => title + " (HDR10)",
                    2 => title + " (SDR)",
                    4 => title + " (HLG)",
                    6 => title + " (HDR10)", // Technically means Blu-ray, but practically always HDR10
                    _ => title,
                };
            }

            return null;
        }
    }

    /// <summary>
    /// Gets the audio spatial format.
    /// </summary>
    /// <value>The audio spatial format.</value>
    [DefaultValue(AudioSpatialFormat.None)]
    public AudioSpatialFormat AudioSpatialFormat
    {
        get
        {
            if (this.Type != MediaStreamType.Audio || string.IsNullOrEmpty(this.Profile))
            {
                return AudioSpatialFormat.None;
            }

            return
                this.Profile.Contains("Dolby Atmos", StringComparison.OrdinalIgnoreCase) ? AudioSpatialFormat.DolbyAtmos :
                this.Profile.Contains("DTS:X", StringComparison.OrdinalIgnoreCase) ? AudioSpatialFormat.DTSX :
                AudioSpatialFormat.None;
        }
    }

    /// <summary>
    /// Gets or sets the localized text for undefined language.
    /// </summary>
    /// <value>The localized text for undefined language.</value>
    public string LocalizedUndefined { get; set; }

    /// <summary>
    /// Gets or sets the localized text for default stream indicator.
    /// </summary>
    /// <value>The localized text for default stream indicator.</value>
    public string LocalizedDefault { get; set; }

    /// <summary>
    /// Gets or sets the localized text for forced stream indicator.
    /// </summary>
    /// <value>The localized text for forced stream indicator.</value>
    public string LocalizedForced { get; set; }

    /// <summary>
    /// Gets or sets the localized text for external stream indicator.
    /// </summary>
    /// <value>The localized text for external stream indicator.</value>
    public string LocalizedExternal { get; set; }

    /// <summary>
    /// Gets or sets the localized text for hearing impaired indicator.
    /// </summary>
    /// <value>The localized text for hearing impaired indicator.</value>
    public string LocalizedHearingImpaired { get; set; }

    /// <summary>
    /// Gets or sets the localized language name.
    /// </summary>
    /// <value>The localized language name.</value>
    public string LocalizedLanguage { get; set; }

    /// <summary>
    /// Gets the display title for the media stream.
    /// </summary>
    /// <value>The display title.</value>
    public string DisplayTitle
    {
        get
        {
            switch (this.Type)
            {
                case MediaStreamType.Audio:
                    {
                        var attributes = new List<string>();

                        // Do not display the language code in display titles if unset or set to a special code. Show it in all other cases (possibly expanded).
                        if (!string.IsNullOrEmpty(this.Language) && !SpecialCodes.Contains(this.Language, StringComparison.OrdinalIgnoreCase))
                        {
                            // Use pre-resolved localized language name, falling back to raw language code.
                            attributes.Add(StringHelper.FirstToUpper(this.LocalizedLanguage ?? this.Language));
                        }

                        if (!string.IsNullOrEmpty(this.Profile) && !string.Equals(this.Profile, "lc", StringComparison.OrdinalIgnoreCase))
                        {
                            attributes.Add(this.Profile);
                        }
                        else if (!string.IsNullOrEmpty(this.Codec))
                        {
                            attributes.Add(AudioCodec.GetFriendlyName(this.Codec));
                        }

                        if (!string.IsNullOrEmpty(this.ChannelLayout))
                        {
                            attributes.Add(StringHelper.FirstToUpper(this.ChannelLayout));
                        }
                        else if (this.Channels.HasValue)
                        {
                            attributes.Add(this.Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
                        }

                        if (this.IsDefault)
                        {
                            attributes.Add(string.IsNullOrEmpty(this.LocalizedDefault) ? "Default" : this.LocalizedDefault);
                        }

                        if (this.IsExternal)
                        {
                            attributes.Add(string.IsNullOrEmpty(this.LocalizedExternal) ? "External" : this.LocalizedExternal);
                        }

                        if (!string.IsNullOrEmpty(this.Title))
                        {
                            var result = new StringBuilder(this.Title);
                            foreach (var tag in attributes)
                            {
                                // Keep Tags that are not already in Title.
                                if (!this.Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
                                {
                                    result.Append(" - ").Append(tag);
                                }
                            }

                            return result.ToString();
                        }

                        return string.Join(" - ", attributes);
                    }

                case MediaStreamType.Video:
                    {
                        var attributes = new List<string>();

                        var resolutionText = this.GetResolutionText();

                        if (!string.IsNullOrEmpty(resolutionText))
                        {
                            attributes.Add(resolutionText);
                        }

                        if (!string.IsNullOrEmpty(this.Codec))
                        {
                            attributes.Add(this.Codec.ToUpperInvariant());
                        }

                        if (this.VideoDoViTitle is not null)
                        {
                            attributes.Add(this.VideoDoViTitle);
                        }
                        else if (this.VideoRange != VideoRange.Unknown)
                        {
                            attributes.Add(this.VideoRange.ToString());
                        }

                        if (!string.IsNullOrEmpty(this.Title))
                        {
                            var result = new StringBuilder(this.Title);
                            foreach (var tag in attributes)
                            {
                                // Keep Tags that are not already in Title.
                                if (!this.Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
                                {
                                    result.Append(" - ").Append(tag);
                                }
                            }

                            return result.ToString();
                        }

                        return string.Join(' ', attributes);
                    }

                case MediaStreamType.Subtitle:
                    {
                        var attributes = new List<string>();

                        if (!string.IsNullOrEmpty(this.Language))
                        {
                            // Use pre-resolved localized language name, falling back to raw language code.
                            attributes.Add(StringHelper.FirstToUpper(this.LocalizedLanguage ?? this.Language));
                        }
                        else
                        {
                            attributes.Add(string.IsNullOrEmpty(this.LocalizedUndefined) ? "Und" : this.LocalizedUndefined);
                        }

                        if (this.IsHearingImpaired == true)
                        {
                            attributes.Add(string.IsNullOrEmpty(this.LocalizedHearingImpaired) ? "Hearing Impaired" : this.LocalizedHearingImpaired);
                        }

                        if (this.IsDefault)
                        {
                            attributes.Add(string.IsNullOrEmpty(this.LocalizedDefault) ? "Default" : this.LocalizedDefault);
                        }

                        if (this.IsForced)
                        {
                            attributes.Add(string.IsNullOrEmpty(this.LocalizedForced) ? "Forced" : this.LocalizedForced);
                        }

                        if (!string.IsNullOrEmpty(this.Codec))
                        {
                            attributes.Add(this.Codec.ToUpperInvariant());
                        }

                        if (this.IsExternal)
                        {
                            attributes.Add(string.IsNullOrEmpty(this.LocalizedExternal) ? "External" : this.LocalizedExternal);
                        }

                        if (!string.IsNullOrEmpty(this.Title))
                        {
                            var result = new StringBuilder(this.Title);
                            foreach (var tag in attributes)
                            {
                                // Keep Tags that are not already in Title.
                                if (!this.Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
                                {
                                    result.Append(" - ").Append(tag);
                                }
                            }

                            return result.ToString();
                        }

                        return string.Join(" - ", attributes);
                    }

                default:
                    return null;
            }
        }
    }

    /// <summary>
    /// Gets or sets the NAL length size.
    /// </summary>
    /// <value>The NAL length size.</value>
    public string NalLengthSize { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is interlaced.
    /// </summary>
    /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
    public bool IsInterlaced { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is AVC.
    /// </summary>
    /// <value><c>true</c> if this instance is AVC; otherwise, <c>false</c>.</value>
    public bool? IsAVC { get; set; }

    /// <summary>
    /// Gets or sets the channel layout.
    /// </summary>
    /// <value>The channel layout.</value>
    public string ChannelLayout { get; set; }

    /// <summary>
    /// Gets or sets the bit rate.
    /// </summary>
    /// <value>The bit rate.</value>
    public int? BitRate { get; set; }

    /// <summary>
    /// Gets or sets the bit depth.
    /// </summary>
    /// <value>The bit depth.</value>
    public int? BitDepth { get; set; }

    /// <summary>
    /// Gets or sets the reference frames.
    /// </summary>
    /// <value>The reference frames.</value>
    public int? RefFrames { get; set; }

    /// <summary>
    /// Gets or sets the length of the packet.
    /// </summary>
    /// <value>The length of the packet.</value>
    public int? PacketLength { get; set; }

    /// <summary>
    /// Gets or sets the channels.
    /// </summary>
    /// <value>The channels.</value>
    public int? Channels { get; set; }

    /// <summary>
    /// Gets or sets the sample rate.
    /// </summary>
    /// <value>The sample rate.</value>
    public int? SampleRate { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is default.
    /// </summary>
    /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
    public bool IsDefault { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is forced.
    /// </summary>
    /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
    public bool IsForced { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is for the hearing impaired.
    /// </summary>
    /// <value><c>true</c> if this instance is for the hearing impaired; otherwise, <c>false</c>.</value>
    public bool IsHearingImpaired { get; set; }

    /// <summary>
    /// Gets or sets the height.
    /// </summary>
    /// <value>The height.</value>
    public int? Height { get; set; }

    /// <summary>
    /// Gets or sets the width.
    /// </summary>
    /// <value>The width.</value>
    public int? Width { get; set; }

    /// <summary>
    /// Gets or sets the average frame rate.
    /// </summary>
    /// <value>The average frame rate.</value>
    public float? AverageFrameRate { get; set; }

    /// <summary>
    /// Gets or sets the real frame rate.
    /// </summary>
    /// <value>The real frame rate.</value>
    public float? RealFrameRate { get; set; }

    /// <summary>
    /// Gets the framerate used as reference.
    /// Prefer AverageFrameRate, if that is null or an unrealistic value
    /// then fallback to RealFrameRate.
    /// </summary>
    /// <value>The reference frame rate.</value>
    public float? ReferenceFrameRate
    {
        get
        {
            // In some cases AverageFrameRate for videos will be read as 1000fps even if it is not.
            // This is probably due to a library compatibility issue.
            // See https://github.com/jellyfin/jellyfin/pull/12603#discussion_r1748044018 for more info.
            return this.AverageFrameRate < 1000 ? this.AverageFrameRate : this.RealFrameRate;
        }
    }

    /// <summary>
    /// Gets or sets the profile.
    /// </summary>
    /// <value>The profile.</value>
    public string Profile { get; set; }

    /// <summary>
    /// Gets or sets the type.
    /// </summary>
    /// <value>The type.</value>
    public MediaStreamType Type { get; set; }

    /// <summary>
    /// Gets or sets the aspect ratio.
    /// </summary>
    /// <value>The aspect ratio.</value>
    public string AspectRatio { get; set; }

    /// <summary>
    /// Gets or sets the index.
    /// </summary>
    /// <value>The index.</value>
    public int Index { get; set; }

    /// <summary>
    /// Gets or sets the score.
    /// </summary>
    /// <value>The score.</value>
    public int? Score { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is external.
    /// </summary>
    /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
    public bool IsExternal { get; set; }

    /// <summary>
    /// Gets or sets the method.
    /// </summary>
    /// <value>The method.</value>
    public SubtitleDeliveryMethod? DeliveryMethod { get; set; }

    /// <summary>
    /// Gets or sets the delivery URL.
    /// </summary>
    /// <value>The delivery URL.</value>
    public string DeliveryUrl { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is external URL.
    /// </summary>
    /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
    public bool? IsExternalUrl { get; set; }

    /// <summary>
    /// Gets a value indicating whether this instance is a text subtitle stream.
    /// </summary>
    /// <value><c>true</c> if this instance is a text subtitle stream; otherwise, <c>false</c>.</value>
    public bool IsTextSubtitleStream
    {
        get
        {
            if (this.Type != MediaStreamType.Subtitle)
            {
                return false;
            }

            if (string.IsNullOrEmpty(this.Codec) && !this.IsExternal)
            {
                return false;
            }

            return IsTextFormat(this.Codec);
        }
    }

    /// <summary>
    /// Gets a value indicating whether this instance is a PGS subtitle stream.
    /// </summary>
    /// <value><c>true</c> if this instance is a PGS subtitle stream; otherwise, <c>false</c>.</value>
    [JsonIgnore]
    public bool IsPgsSubtitleStream
    {
        get
        {
            if (this.Type != MediaStreamType.Subtitle)
            {
                return false;
            }

            if (string.IsNullOrEmpty(this.Codec) && !this.IsExternal)
            {
                return false;
            }

            return IsPgsFormat(this.Codec);
        }
    }

    /// <summary>
    /// Gets a value indicating whether this is a subtitle steam that is extractable by ffmpeg.
    /// All text-based and pgs subtitles can be extracted.
    /// </summary>
    /// <value><c>true</c> if this is a extractable subtitle steam otherwise, <c>false</c>.</value>
    [JsonIgnore]
    public bool IsExtractableSubtitleStream => this.IsTextSubtitleStream || this.IsPgsSubtitleStream;

    /// <summary>
    /// Gets or sets a value indicating whether [supports external stream].
    /// </summary>
    /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
    public bool SupportsExternalStream { get; set; }

    /// <summary>
    /// Gets or sets the filename.
    /// </summary>
    /// <value>The filename.</value>
    public string Path { get; set; }

    /// <summary>
    /// Gets or sets the pixel format.
    /// </summary>
    /// <value>The pixel format.</value>
    public string PixelFormat { get; set; }

    /// <summary>
    /// Gets or sets the level.
    /// </summary>
    /// <value>The level.</value>
    public double? Level { get; set; }

    /// <summary>
    /// Gets or sets whether this instance is anamorphic.
    /// </summary>
    /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
    public bool? IsAnamorphic { get; set; }

    /// <summary>
    /// Determines whether the specified format is a text-based subtitle format.
    /// </summary>
    /// <param name="format">The subtitle format/codec to check.</param>
    /// <returns><c>true</c> if the format is text-based; otherwise, <c>false</c>.</returns>
    public static bool IsTextFormat(string format)
    {
        string codec = format ?? string.Empty;

        // microdvd and dvdsub/vobsub share the ".sub" file extension, but it's text-based.
        return codec.Contains("microdvd", StringComparison.OrdinalIgnoreCase)
               || (!codec.Contains("pgs", StringComparison.OrdinalIgnoreCase)
                   && !codec.Contains("dvdsub", StringComparison.OrdinalIgnoreCase)
                   && !codec.Contains("dvbsub", StringComparison.OrdinalIgnoreCase)
                   && !string.Equals(codec, "sup", StringComparison.OrdinalIgnoreCase)
                   && !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase));
    }

    /// <summary>
    /// Determines whether the specified format is a PGS (Presentation Graphic Stream) subtitle format.
    /// </summary>
    /// <param name="format">The subtitle format/codec to check.</param>
    /// <returns><c>true</c> if the format is PGS; otherwise, <c>false</c>.</returns>
    public static bool IsPgsFormat(string format)
    {
        string codec = format ?? string.Empty;

        return codec.Contains("pgs", StringComparison.OrdinalIgnoreCase)
               || string.Equals(codec, "sup", StringComparison.OrdinalIgnoreCase);
    }

    /// <summary>
    /// Determines whether this subtitle stream can be converted to the specified codec.
    /// </summary>
    /// <param name="toCodec">The target codec to convert to.</param>
    /// <returns><c>true</c> if the subtitle stream can be converted to the specified codec; otherwise, <c>false</c>.</returns>
    public bool SupportsSubtitleConversionTo(string toCodec)
    {
        if (!this.IsTextSubtitleStream)
        {
            return false;
        }

        var fromCodec = this.Codec;

        // Can't convert from this
        if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
        {
            return false;
        }

        if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
        {
            return false;
        }

        // Can't convert to this
        if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
        {
            return false;
        }

        if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
        {
            return false;
        }

        return true;
    }

    /// <summary>
    /// Gets the video color range information based on codec tags, Dolby Vision properties, and color transfer characteristics.
    /// </summary>
    /// <returns>A tuple containing the video range (SDR/HDR) and the specific video range type.</returns>
    public (VideoRange VideoRange, VideoRangeType VideoRangeType) GetVideoColorRange()
    {
        if (this.Type != MediaStreamType.Video)
        {
            return (VideoRange.Unknown, VideoRangeType.Unknown);
        }

        var codecTag = this.CodecTag;
        var dvProfile = this.DvProfile;
        var rpuPresentFlag = this.RpuPresentFlag == 1;
        var blPresentFlag = this.BlPresentFlag == 1;
        var dvBlCompatId = this.DvBlSignalCompatibilityId;

        var isDoViProfile = dvProfile is 5 or 7 or 8 or 10;
        var isDoViFlag = rpuPresentFlag && blPresentFlag && dvBlCompatId is 0 or 1 or 4 or 2 or 6;

        if ((isDoViProfile && isDoViFlag)
            || string.Equals(codecTag, "dovi", StringComparison.OrdinalIgnoreCase)
            || string.Equals(codecTag, "dvh1", StringComparison.OrdinalIgnoreCase)
            || string.Equals(codecTag, "dvhe", StringComparison.OrdinalIgnoreCase)
            || string.Equals(codecTag, "dav1", StringComparison.OrdinalIgnoreCase))
        {
            var dvRangeSet = dvProfile switch
            {
                5 => (VideoRange.HDR, VideoRangeType.DOVI),
                8 => dvBlCompatId switch
                {
                    1 => (VideoRange.HDR, VideoRangeType.DOVIWithHDR10),
                    4 => (VideoRange.HDR, VideoRangeType.DOVIWithHLG),
                    2 => (VideoRange.SDR, VideoRangeType.DOVIWithSDR),

                    // Out of Dolby Spec files should be marked as invalid
                    _ => (VideoRange.HDR, VideoRangeType.DOVIInvalid),
                },
                7 => (VideoRange.HDR, VideoRangeType.DOVIWithEL),
                10 => dvBlCompatId switch
                {
                    0 => (VideoRange.HDR, VideoRangeType.DOVI),
                    1 => (VideoRange.HDR, VideoRangeType.DOVIWithHDR10),
                    2 => (VideoRange.SDR, VideoRangeType.DOVIWithSDR),
                    4 => (VideoRange.HDR, VideoRangeType.DOVIWithHLG),

                    // Out of Dolby Spec files should be marked as invalid
                    _ => (VideoRange.HDR, VideoRangeType.DOVIInvalid),
                },
                _ => (VideoRange.SDR, VideoRangeType.SDR),
            };

            if (this.Hdr10PlusPresentFlag == true)
            {
                return dvRangeSet.Item2 switch
                {
                    VideoRangeType.DOVIWithHDR10 => (VideoRange.HDR, VideoRangeType.DOVIWithHDR10Plus),
                    VideoRangeType.DOVIWithEL => (VideoRange.HDR, VideoRangeType.DOVIWithELHDR10Plus),
                    _ => dvRangeSet,
                };
            }

            return dvRangeSet;
        }

        var colorTransfer = this.ColorTransfer;

        if (string.Equals(colorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase))
        {
            return this.Hdr10PlusPresentFlag == true ? (VideoRange.HDR, VideoRangeType.HDR10Plus) : (VideoRange.HDR, VideoRangeType.HDR10);
        }
        else if (string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
        {
            return (VideoRange.HDR, VideoRangeType.HLG);
        }

        return (VideoRange.SDR, VideoRangeType.SDR);
    }

    /// <summary>
    /// Gets the resolution text based on width and height.
    /// </summary>
    /// <returns>The resolution text (e.g., "1080p", "4K") or null if width/height are not available.</returns>
    internal string GetResolutionText()
    {
        if (!this.Width.HasValue || !this.Height.HasValue)
        {
            return null;
        }

        return this.Width switch
        {
            // 256x144 (16:9 square pixel format)
            <= 256 when this.Height <= 144 => this.IsInterlaced ? "144i" : "144p",

            // 426x240 (16:9 square pixel format)
            <= 426 when this.Height <= 240 => this.IsInterlaced ? "240i" : "240p",

            // 640x360 (16:9 square pixel format)
            <= 640 when this.Height <= 360 => this.IsInterlaced ? "360i" : "360p",

            // 682x384 (16:9 square pixel format)
            <= 682 when this.Height <= 384 => this.IsInterlaced ? "384i" : "384p",

            // 720x404 (16:9 square pixel format)
            <= 720 when this.Height <= 404 => this.IsInterlaced ? "404i" : "404p",

            // 854x480 (16:9 square pixel format)
            <= 854 when this.Height <= 480 => this.IsInterlaced ? "480i" : "480p",

            // 960x544 (16:9 square pixel format)
            <= 960 when this.Height <= 544 => this.IsInterlaced ? "540i" : "540p",

            // 1024x576 (16:9 square pixel format)
            <= 1024 when this.Height <= 576 => this.IsInterlaced ? "576i" : "576p",

            // 1280x720
            <= 1280 when this.Height <= 962 => this.IsInterlaced ? "720i" : "720p",

            // 2560x1080 (FHD ultra wide 21:9) using 1440px width to accommodate WQHD
            <= 2560 when this.Height <= 1440 => this.IsInterlaced ? "1080i" : "1080p",

            // 4K
            <= 4096 when this.Height <= 3072 => "4K",

            // 8K
            <= 8192 when this.Height <= 6144 => "8K",
            _ => null,
        };
    }
}
