jellyfin/MediaBrowser.Model/Dlna/MediaFormatProfileResolver.cs

416 lines
19 KiB
C#
Raw Normal View History

2014-04-18 05:03:01 +00:00
using System;
using System.Collections.Generic;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
2014-04-18 05:03:01 +00:00
namespace MediaBrowser.Model.Dlna
{
public class MediaFormatProfileResolver
{
2014-04-24 05:08:10 +00:00
public IEnumerable<MediaFormatProfile> ResolveVideoFormat(string container, string videoCodec, string audioCodec, int? width, int? height, TransportStreamTimestamp timestampType)
2014-04-18 05:03:01 +00:00
{
if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
{
var val = ResolveVideoASFFormat(videoCodec, audioCodec, width, height);
return val.HasValue ? new List<MediaFormatProfile> { val.Value } : new List<MediaFormatProfile>();
}
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
{
var val = ResolveVideoMP4Format(videoCodec, audioCodec, width, height);
return val.HasValue ? new List<MediaFormatProfile> { val.Value } : new List<MediaFormatProfile>();
}
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "avi", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
return new[] { MediaFormatProfile.AVI };
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "mkv", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
return new[] { MediaFormatProfile.MATROSKA };
if (string.Equals(container, "mpeg2ps", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "ts", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
return new[] { MediaFormatProfile.MPEG_PS_NTSC, MediaFormatProfile.MPEG_PS_PAL };
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "mpeg1video", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
return new[] { MediaFormatProfile.MPEG1 };
if (string.Equals(container, "mpeg2ts", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "mpegts", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "m2ts", StringComparison.OrdinalIgnoreCase))
{
2014-04-24 05:08:10 +00:00
return ResolveVideoMPEG2TSFormat(videoCodec, audioCodec, width, height, timestampType);
}
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "flv", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
return new[] { MediaFormatProfile.FLV };
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "wtv", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
return new[] { MediaFormatProfile.WTV };
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "3gp", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
{
var val = ResolveVideo3GPFormat(videoCodec, audioCodec);
return val.HasValue ? new List<MediaFormatProfile> { val.Value } : new List<MediaFormatProfile>();
}
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "ogv", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase))
2014-04-24 05:08:10 +00:00
return new[] { MediaFormatProfile.OGV };
2014-04-18 05:03:01 +00:00
2014-04-24 05:08:10 +00:00
return new List<MediaFormatProfile>();
2014-04-18 05:03:01 +00:00
}
2014-04-23 02:47:46 +00:00
private IEnumerable<MediaFormatProfile> ResolveVideoMPEG2TSFormat(string videoCodec, string audioCodec, int? width, int? height, TransportStreamTimestamp timestampType)
2014-04-18 05:03:01 +00:00
{
2014-04-18 17:16:25 +00:00
var suffix = "";
2014-04-18 05:03:01 +00:00
2014-04-18 17:16:25 +00:00
switch (timestampType)
{
2014-04-25 02:45:06 +00:00
case TransportStreamTimestamp.None:
2014-04-18 17:16:25 +00:00
suffix = "_ISO";
break;
2014-04-25 02:45:06 +00:00
case TransportStreamTimestamp.Valid:
2014-04-18 17:16:25 +00:00
suffix = "_T";
break;
}
var resolution = "S";
2014-04-18 17:16:25 +00:00
if ((width.HasValue && width.Value > 720) || (height.HasValue && height.Value > 576))
{
resolution = "H";
}
2014-04-18 05:03:01 +00:00
if (string.Equals(videoCodec, "mpeg2video", StringComparison.OrdinalIgnoreCase))
{
var list = new List<MediaFormatProfile>();
list.Add(ValueOf("MPEG_TS_SD_NA" + suffix));
list.Add(ValueOf("MPEG_TS_SD_EU" + suffix));
list.Add(ValueOf("MPEG_TS_SD_KO" + suffix));
2014-04-18 05:03:01 +00:00
2014-04-25 02:45:06 +00:00
if ((timestampType == TransportStreamTimestamp.Valid) && string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
{
list.Add(MediaFormatProfile.MPEG_TS_JP_T);
}
return list;
}
2014-04-18 17:16:25 +00:00
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(audioCodec, "lpcm", StringComparison.OrdinalIgnoreCase))
return new[] { MediaFormatProfile.AVC_TS_HD_50_LPCM_T };
2014-04-18 17:16:25 +00:00
if (string.Equals(audioCodec, "dts", StringComparison.OrdinalIgnoreCase))
{
2014-04-25 02:45:06 +00:00
if (timestampType == TransportStreamTimestamp.None)
2014-04-18 17:16:25 +00:00
{
return new[] { MediaFormatProfile.AVC_TS_HD_DTS_ISO };
2014-04-18 17:16:25 +00:00
}
return new[] { MediaFormatProfile.AVC_TS_HD_DTS_T };
2014-04-18 17:16:25 +00:00
}
if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
{
2014-04-25 02:45:06 +00:00
if (timestampType == TransportStreamTimestamp.None)
{
return new[] { ValueOf(string.Format("AVC_TS_HP_{0}D_MPEG1_L2_ISO", resolution)) };
}
return new[] { ValueOf(string.Format("AVC_TS_HP_{0}D_MPEG1_L2_T", resolution)) };
}
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
return new[] { ValueOf(string.Format("AVC_TS_MP_{0}D_AAC_MULT5{1}", resolution, suffix)) };
if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
return new[] { ValueOf(string.Format("AVC_TS_MP_{0}D_MPEG1_L3{1}", resolution, suffix)) };
if (string.IsNullOrEmpty(audioCodec) ||
string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
return new[] { ValueOf(string.Format("AVC_TS_MP_{0}D_AC3{1}", resolution, suffix)) };
2014-04-18 17:16:25 +00:00
}
else if (string.Equals(videoCodec, "vc1", StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
{
if ((width.HasValue && width.Value > 720) || (height.HasValue && height.Value > 576))
{
return new[] { MediaFormatProfile.VC1_TS_AP_L2_AC3_ISO };
2014-04-18 17:16:25 +00:00
}
return new[] { MediaFormatProfile.VC1_TS_AP_L1_AC3_ISO };
2014-04-18 17:16:25 +00:00
}
2014-04-24 05:08:10 +00:00
if (string.Equals(audioCodec, "dts", StringComparison.OrdinalIgnoreCase))
{
suffix = string.Equals(suffix, "_ISO") ? suffix : "_T";
return new[] { ValueOf(string.Format("VC1_TS_HD_DTS{0}", suffix)) };
}
}
else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) || string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
return new[] { ValueOf(string.Format("MPEG4_P2_TS_ASP_AAC{0}", suffix)) };
if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
return new[] { ValueOf(string.Format("MPEG4_P2_TS_ASP_MPEG1_L3{0}", suffix)) };
if (string.Equals(audioCodec, "mp2", StringComparison.OrdinalIgnoreCase))
return new[] { ValueOf(string.Format("MPEG4_P2_TS_ASP_MPEG2_L2{0}", suffix)) };
if (string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
return new[] { ValueOf(string.Format("MPEG4_P2_TS_ASP_AC3{0}", suffix)) };
2014-04-18 17:16:25 +00:00
}
2014-04-18 05:03:01 +00:00
return new List<MediaFormatProfile>();
}
private MediaFormatProfile ValueOf(string value)
{
return (MediaFormatProfile)Enum.Parse(typeof(MediaFormatProfile), value, true);
2014-04-18 05:03:01 +00:00
}
2014-04-24 05:08:10 +00:00
private MediaFormatProfile? ResolveVideoMP4Format(string videoCodec, string audioCodec, int? width, int? height)
2014-04-18 05:03:01 +00:00
{
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(audioCodec, "lpcm", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.AVC_MP4_LPCM;
if (string.IsNullOrEmpty(audioCodec) ||
string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.AVC_MP4_MP_SD_AC3;
}
if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.AVC_MP4_MP_SD_MPEG1_L3;
}
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 720) && (height.Value <= 576))
{
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.AVC_MP4_MP_SD_AAC_MULT5;
}
else if ((width.Value <= 1280) && (height.Value <= 720))
{
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.AVC_MP4_MP_HD_720p_AAC;
}
else if ((width.Value <= 1920) && (height.Value <= 1080))
{
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.AVC_MP4_MP_HD_1080i_AAC;
}
}
}
}
else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) ||
string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
{
if (width.HasValue && height.HasValue && width.Value <= 720 && height.Value <= 576)
{
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.MPEG4_P2_MP4_ASP_AAC;
if (string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase) || string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.MPEG4_P2_MP4_NDSD;
}
}
else if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.MPEG4_P2_MP4_SP_L6_AAC;
}
}
else if (string.Equals(videoCodec, "h263", StringComparison.OrdinalIgnoreCase) && string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.MPEG4_H263_MP4_P0_L10_AAC;
}
return null;
2014-04-18 05:03:01 +00:00
}
private MediaFormatProfile? ResolveVideo3GPFormat(string videoCodec, string audioCodec)
2014-04-18 05:03:01 +00:00
{
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.AVC_3GPP_BL_QCIF15_AAC;
}
else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) ||
string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.MPEG4_P2_3GPP_SP_L0B_AAC;
if (string.Equals(audioCodec, "amrnb", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.MPEG4_P2_3GPP_SP_L0B_AMR;
}
else if (string.Equals(videoCodec, "h263", StringComparison.OrdinalIgnoreCase) && string.Equals(audioCodec, "amrnb", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.MPEG4_H263_3GPP_P0_L10_AMR;
}
return null;
2014-04-18 05:03:01 +00:00
}
private MediaFormatProfile? ResolveVideoASFFormat(string videoCodec, string audioCodec, int? width, int? height)
2014-04-18 05:03:01 +00:00
{
if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase) &&
(string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase) || string.Equals(videoCodec, "wmapro", StringComparison.OrdinalIgnoreCase)))
{
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 720) && (height.Value <= 576))
{
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.WMVMED_FULL;
}
return MediaFormatProfile.WMVMED_PRO;
}
}
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.WMVHIGH_FULL;
}
return MediaFormatProfile.WMVHIGH_PRO;
}
if (string.Equals(videoCodec, "vc1", StringComparison.OrdinalIgnoreCase))
{
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 720) && (height.Value <= 576))
return MediaFormatProfile.VC1_ASF_AP_L1_WMA;
if ((width.Value <= 1280) && (height.Value <= 720))
return MediaFormatProfile.VC1_ASF_AP_L2_WMA;
if ((width.Value <= 1920) && (height.Value <= 1080))
return MediaFormatProfile.VC1_ASF_AP_L3_WMA;
}
}
else if (string.Equals(videoCodec, "mpeg2video", StringComparison.OrdinalIgnoreCase))
{
return MediaFormatProfile.DVR_MS;
}
return null;
2014-04-18 05:03:01 +00:00
}
public MediaFormatProfile? ResolveAudioFormat(string container, int? bitrate, int? frequency, int? channels)
2014-04-18 05:03:01 +00:00
{
if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
return ResolveAudioASFFormat(bitrate);
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "mp3", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.MP3;
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "lpcm", StringComparison.OrdinalIgnoreCase))
return ResolveAudioLPCMFormat(frequency, channels);
if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "aac", StringComparison.OrdinalIgnoreCase))
return ResolveAudioMP4Format(bitrate);
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "adts", StringComparison.OrdinalIgnoreCase))
return ResolveAudioADTSFormat(bitrate);
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "flac", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.FLAC;
if (string.Equals(container, "oga", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase))
2014-04-18 05:03:01 +00:00
return MediaFormatProfile.OGG;
return null;
2014-04-18 05:03:01 +00:00
}
private MediaFormatProfile ResolveAudioASFFormat(int? bitrate)
2014-04-18 05:03:01 +00:00
{
if (bitrate.HasValue && bitrate.Value <= 193)
{
return MediaFormatProfile.WMA_BASE;
}
return MediaFormatProfile.WMA_FULL;
}
private MediaFormatProfile? ResolveAudioLPCMFormat(int? frequency, int? channels)
2014-04-18 05:03:01 +00:00
{
if (frequency.HasValue && channels.HasValue)
{
if (frequency.Value == 44100 && channels.Value == 1)
{
return MediaFormatProfile.LPCM16_44_MONO;
}
if (frequency.Value == 44100 && channels.Value == 2)
{
return MediaFormatProfile.LPCM16_44_STEREO;
}
if (frequency.Value == 48000 && channels.Value == 1)
{
return MediaFormatProfile.LPCM16_48_MONO;
}
if (frequency.Value == 48000 && channels.Value == 1)
{
return MediaFormatProfile.LPCM16_48_STEREO;
}
return null;
2014-04-18 05:03:01 +00:00
}
return MediaFormatProfile.LPCM16_48_STEREO;
}
private MediaFormatProfile ResolveAudioMP4Format(int? bitrate)
2014-04-18 05:03:01 +00:00
{
if (bitrate.HasValue && bitrate.Value <= 320)
{
return MediaFormatProfile.AAC_ISO_320;
}
return MediaFormatProfile.AAC_ISO;
}
private MediaFormatProfile ResolveAudioADTSFormat(int? bitrate)
2014-04-18 05:03:01 +00:00
{
if (bitrate.HasValue && bitrate.Value <= 320)
{
return MediaFormatProfile.AAC_ADTS_320;
}
return MediaFormatProfile.AAC_ADTS;
}
public MediaFormatProfile? ResolveImageFormat(string container, int? width, int? height)
2014-04-18 05:03:01 +00:00
{
if (string.Equals(container, "jpeg", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "jpg", StringComparison.OrdinalIgnoreCase))
return ResolveImageJPGFormat(width, height);
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "png", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.PNG_LRG;
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "gif", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.GIF_LRG;
2014-04-18 05:03:01 +00:00
if (string.Equals(container, "raw", StringComparison.OrdinalIgnoreCase))
return MediaFormatProfile.RAW;
return null;
2014-04-18 05:03:01 +00:00
}
private MediaFormatProfile ResolveImageJPGFormat(int? width, int? height)
{
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 640) && (height.Value <= 480))
return MediaFormatProfile.JPEG_SM;
if ((width.Value <= 1024) && (height.Value <= 768))
{
return MediaFormatProfile.JPEG_MED;
}
}
return MediaFormatProfile.JPEG_LRG;
}
}
}