EncodingJobInfo.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #nullable disable
  2. #pragma warning disable CS1591, SA1401
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using Jellyfin.Data.Entities;
  8. using Jellyfin.Data.Enums;
  9. using MediaBrowser.Model.Dlna;
  10. using MediaBrowser.Model.Drawing;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.MediaInfo;
  14. using MediaBrowser.Model.Net;
  15. using MediaBrowser.Model.Session;
  16. namespace MediaBrowser.Controller.MediaEncoding
  17. {
  18. // For now, a common base class until the API and MediaEncoding classes are unified
  19. public class EncodingJobInfo
  20. {
  21. public int? OutputAudioBitrate;
  22. public int? OutputAudioChannels;
  23. private TranscodeReason? _transcodeReasons = null;
  24. public EncodingJobInfo(TranscodingJobType jobType)
  25. {
  26. TranscodingType = jobType;
  27. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  28. SupportedAudioCodecs = Array.Empty<string>();
  29. SupportedVideoCodecs = Array.Empty<string>();
  30. SupportedSubtitleCodecs = Array.Empty<string>();
  31. }
  32. public TranscodeReason TranscodeReasons
  33. {
  34. get
  35. {
  36. if (!_transcodeReasons.HasValue)
  37. {
  38. if (BaseRequest.TranscodeReasons is null)
  39. {
  40. _transcodeReasons = 0;
  41. return 0;
  42. }
  43. _ = Enum.TryParse<TranscodeReason>(BaseRequest.TranscodeReasons, out var reason);
  44. _transcodeReasons = reason;
  45. }
  46. return _transcodeReasons.Value;
  47. }
  48. }
  49. public IProgress<double> Progress { get; set; }
  50. public MediaStream VideoStream { get; set; }
  51. public VideoType VideoType { get; set; }
  52. public Dictionary<string, string> RemoteHttpHeaders { get; set; }
  53. public string OutputVideoCodec { get; set; }
  54. public MediaProtocol InputProtocol { get; set; }
  55. public string MediaPath { get; set; }
  56. public bool IsInputVideo { get; set; }
  57. public string OutputAudioCodec { get; set; }
  58. public int? OutputVideoBitrate { get; set; }
  59. public MediaStream SubtitleStream { get; set; }
  60. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  61. public string[] SupportedSubtitleCodecs { get; set; }
  62. public int InternalSubtitleStreamOffset { get; set; }
  63. public MediaSourceInfo MediaSource { get; set; }
  64. public User User { get; set; }
  65. public long? RunTimeTicks { get; set; }
  66. public bool ReadInputAtNativeFramerate { get; set; }
  67. public string OutputFilePath { get; set; }
  68. public string MimeType { get; set; }
  69. public bool IgnoreInputDts => MediaSource.IgnoreDts;
  70. public bool IgnoreInputIndex => MediaSource.IgnoreIndex;
  71. public bool GenPtsInput => MediaSource.GenPtsInput;
  72. public bool DiscardCorruptFramesInput => false;
  73. public bool EnableFastSeekInput => false;
  74. public bool GenPtsOutput => false;
  75. public string OutputContainer { get; set; }
  76. public string OutputVideoSync { get; set; }
  77. public string AlbumCoverPath { get; set; }
  78. public string InputAudioSync { get; set; }
  79. public string InputVideoSync { get; set; }
  80. public TransportStreamTimestamp InputTimestamp { get; set; }
  81. public MediaStream AudioStream { get; set; }
  82. public string[] SupportedAudioCodecs { get; set; }
  83. public string[] SupportedVideoCodecs { get; set; }
  84. public string InputContainer { get; set; }
  85. public IsoType? IsoType { get; set; }
  86. public BaseEncodingJobOptions BaseRequest { get; set; }
  87. public bool IsVideoRequest { get; set; }
  88. public TranscodingJobType TranscodingType { get; set; }
  89. public long? StartTimeTicks => BaseRequest.StartTimeTicks;
  90. public bool CopyTimestamps => BaseRequest.CopyTimestamps;
  91. public bool IsSegmentedLiveStream
  92. => TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue;
  93. public int? TotalOutputBitrate => (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  94. public int? OutputWidth
  95. {
  96. get
  97. {
  98. if (VideoStream is not null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  99. {
  100. var size = new ImageDimensions(VideoStream.Width.Value, VideoStream.Height.Value);
  101. var newSize = DrawingUtils.Resize(
  102. size,
  103. BaseRequest.Width ?? 0,
  104. BaseRequest.Height ?? 0,
  105. BaseRequest.MaxWidth ?? 0,
  106. BaseRequest.MaxHeight ?? 0);
  107. return newSize.Width;
  108. }
  109. if (!IsVideoRequest)
  110. {
  111. return null;
  112. }
  113. return BaseRequest.MaxWidth ?? BaseRequest.Width;
  114. }
  115. }
  116. public int? OutputHeight
  117. {
  118. get
  119. {
  120. if (VideoStream is not null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  121. {
  122. var size = new ImageDimensions(VideoStream.Width.Value, VideoStream.Height.Value);
  123. var newSize = DrawingUtils.Resize(
  124. size,
  125. BaseRequest.Width ?? 0,
  126. BaseRequest.Height ?? 0,
  127. BaseRequest.MaxWidth ?? 0,
  128. BaseRequest.MaxHeight ?? 0);
  129. return newSize.Height;
  130. }
  131. if (!IsVideoRequest)
  132. {
  133. return null;
  134. }
  135. return BaseRequest.MaxHeight ?? BaseRequest.Height;
  136. }
  137. }
  138. public int? OutputAudioSampleRate
  139. {
  140. get
  141. {
  142. if (BaseRequest.Static
  143. || EncodingHelper.IsCopyCodec(OutputAudioCodec))
  144. {
  145. if (AudioStream is not null)
  146. {
  147. return AudioStream.SampleRate;
  148. }
  149. }
  150. else if (BaseRequest.AudioSampleRate.HasValue)
  151. {
  152. // Don't exceed what the encoder supports
  153. // Seeing issues of attempting to encode to 88200
  154. return BaseRequest.AudioSampleRate.Value;
  155. }
  156. return null;
  157. }
  158. }
  159. public int? OutputAudioBitDepth
  160. {
  161. get
  162. {
  163. if (BaseRequest.Static
  164. || EncodingHelper.IsCopyCodec(OutputAudioCodec))
  165. {
  166. if (AudioStream is not null)
  167. {
  168. return AudioStream.BitDepth;
  169. }
  170. }
  171. return null;
  172. }
  173. }
  174. /// <summary>
  175. /// Gets the target video level.
  176. /// </summary>
  177. public double? TargetVideoLevel
  178. {
  179. get
  180. {
  181. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  182. {
  183. return VideoStream?.Level;
  184. }
  185. var level = GetRequestedLevel(ActualOutputVideoCodec);
  186. if (double.TryParse(level, CultureInfo.InvariantCulture, out var result))
  187. {
  188. return result;
  189. }
  190. return null;
  191. }
  192. }
  193. /// <summary>
  194. /// Gets the target video bit depth.
  195. /// </summary>
  196. public int? TargetVideoBitDepth
  197. {
  198. get
  199. {
  200. if (BaseRequest.Static
  201. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  202. {
  203. return VideoStream?.BitDepth;
  204. }
  205. return null;
  206. }
  207. }
  208. /// <summary>
  209. /// Gets the target reference frames.
  210. /// </summary>
  211. /// <value>The target reference frames.</value>
  212. public int? TargetRefFrames
  213. {
  214. get
  215. {
  216. if (BaseRequest.Static
  217. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  218. {
  219. return VideoStream?.RefFrames;
  220. }
  221. return null;
  222. }
  223. }
  224. /// <summary>
  225. /// Gets the target framerate.
  226. /// </summary>
  227. public float? TargetFramerate
  228. {
  229. get
  230. {
  231. if (BaseRequest.Static
  232. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  233. {
  234. return VideoStream is null ? null : (VideoStream.AverageFrameRate ?? VideoStream.RealFrameRate);
  235. }
  236. return BaseRequest.MaxFramerate ?? BaseRequest.Framerate;
  237. }
  238. }
  239. public TransportStreamTimestamp TargetTimestamp
  240. {
  241. get
  242. {
  243. if (BaseRequest.Static)
  244. {
  245. return InputTimestamp;
  246. }
  247. return string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  248. TransportStreamTimestamp.Valid :
  249. TransportStreamTimestamp.None;
  250. }
  251. }
  252. /// <summary>
  253. /// Gets the target packet length.
  254. /// </summary>
  255. public int? TargetPacketLength
  256. {
  257. get
  258. {
  259. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  260. {
  261. return VideoStream?.PacketLength;
  262. }
  263. return null;
  264. }
  265. }
  266. /// <summary>
  267. /// Gets the target video profile.
  268. /// </summary>
  269. public string TargetVideoProfile
  270. {
  271. get
  272. {
  273. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  274. {
  275. return VideoStream?.Profile;
  276. }
  277. var requestedProfile = GetRequestedProfiles(ActualOutputVideoCodec).FirstOrDefault();
  278. if (!string.IsNullOrEmpty(requestedProfile))
  279. {
  280. return requestedProfile;
  281. }
  282. return null;
  283. }
  284. }
  285. /// <summary>
  286. /// Gets the target video range type.
  287. /// </summary>
  288. public VideoRangeType TargetVideoRangeType
  289. {
  290. get
  291. {
  292. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  293. {
  294. return VideoStream?.VideoRangeType ?? VideoRangeType.Unknown;
  295. }
  296. if (Enum.TryParse(GetRequestedRangeTypes(ActualOutputVideoCodec).FirstOrDefault() ?? "Unknown", true, out VideoRangeType requestedRangeType))
  297. {
  298. return requestedRangeType;
  299. }
  300. return VideoRangeType.Unknown;
  301. }
  302. }
  303. public string TargetVideoCodecTag
  304. {
  305. get
  306. {
  307. if (BaseRequest.Static
  308. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  309. {
  310. return VideoStream?.CodecTag;
  311. }
  312. return null;
  313. }
  314. }
  315. public bool? IsTargetAnamorphic
  316. {
  317. get
  318. {
  319. if (BaseRequest.Static
  320. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  321. {
  322. return VideoStream?.IsAnamorphic;
  323. }
  324. return false;
  325. }
  326. }
  327. public string ActualOutputVideoCodec
  328. {
  329. get
  330. {
  331. if (VideoStream is null)
  332. {
  333. return null;
  334. }
  335. if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
  336. {
  337. return VideoStream.Codec;
  338. }
  339. return OutputVideoCodec;
  340. }
  341. }
  342. public string ActualOutputAudioCodec
  343. {
  344. get
  345. {
  346. if (AudioStream is null)
  347. {
  348. return null;
  349. }
  350. if (EncodingHelper.IsCopyCodec(OutputAudioCodec))
  351. {
  352. return AudioStream.Codec;
  353. }
  354. return OutputAudioCodec;
  355. }
  356. }
  357. public bool? IsTargetInterlaced
  358. {
  359. get
  360. {
  361. if (BaseRequest.Static
  362. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  363. {
  364. return VideoStream?.IsInterlaced;
  365. }
  366. if (DeInterlace(ActualOutputVideoCodec, true))
  367. {
  368. return false;
  369. }
  370. return VideoStream?.IsInterlaced;
  371. }
  372. }
  373. public bool? IsTargetAVC
  374. {
  375. get
  376. {
  377. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  378. {
  379. return VideoStream?.IsAVC;
  380. }
  381. return false;
  382. }
  383. }
  384. public int? TargetVideoStreamCount
  385. {
  386. get
  387. {
  388. if (BaseRequest.Static)
  389. {
  390. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  391. }
  392. return GetMediaStreamCount(MediaStreamType.Video, 1);
  393. }
  394. }
  395. public int? TargetAudioStreamCount
  396. {
  397. get
  398. {
  399. if (BaseRequest.Static)
  400. {
  401. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  402. }
  403. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  404. }
  405. }
  406. public bool EnableAudioVbrEncoding
  407. {
  408. get
  409. {
  410. return BaseRequest.EnableAudioVbrEncoding;
  411. }
  412. }
  413. public int HlsListSize => 0;
  414. public bool EnableBreakOnNonKeyFrames(string videoCodec)
  415. {
  416. if (TranscodingType != TranscodingJobType.Progressive)
  417. {
  418. if (IsSegmentedLiveStream)
  419. {
  420. return false;
  421. }
  422. return BaseRequest.BreakOnNonKeyFrames && EncodingHelper.IsCopyCodec(videoCodec);
  423. }
  424. return false;
  425. }
  426. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  427. {
  428. var count = MediaSource.GetStreamCount(type);
  429. if (count.HasValue)
  430. {
  431. count = Math.Min(count.Value, limit);
  432. }
  433. return count;
  434. }
  435. public string GetMimeType(string outputPath, bool enableStreamDefault = true)
  436. {
  437. if (!string.IsNullOrEmpty(MimeType))
  438. {
  439. return MimeType;
  440. }
  441. if (enableStreamDefault)
  442. {
  443. return MimeTypes.GetMimeType(outputPath);
  444. }
  445. return MimeTypes.GetMimeType(outputPath, null);
  446. }
  447. public bool DeInterlace(string videoCodec, bool forceDeinterlaceIfSourceIsInterlaced)
  448. {
  449. var videoStream = VideoStream;
  450. var isInputInterlaced = videoStream is not null && videoStream.IsInterlaced;
  451. if (!isInputInterlaced)
  452. {
  453. return false;
  454. }
  455. // Support general param
  456. if (BaseRequest.DeInterlace)
  457. {
  458. return true;
  459. }
  460. if (!string.IsNullOrEmpty(videoCodec))
  461. {
  462. if (string.Equals(BaseRequest.GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase))
  463. {
  464. return true;
  465. }
  466. }
  467. return forceDeinterlaceIfSourceIsInterlaced;
  468. }
  469. public string[] GetRequestedProfiles(string codec)
  470. {
  471. if (!string.IsNullOrEmpty(BaseRequest.Profile))
  472. {
  473. return BaseRequest.Profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  474. }
  475. if (!string.IsNullOrEmpty(codec))
  476. {
  477. var profile = BaseRequest.GetOption(codec, "profile");
  478. if (!string.IsNullOrEmpty(profile))
  479. {
  480. return profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  481. }
  482. }
  483. return Array.Empty<string>();
  484. }
  485. public string[] GetRequestedRangeTypes(string codec)
  486. {
  487. if (!string.IsNullOrEmpty(BaseRequest.VideoRangeType))
  488. {
  489. return BaseRequest.VideoRangeType.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  490. }
  491. if (!string.IsNullOrEmpty(codec))
  492. {
  493. var rangetype = BaseRequest.GetOption(codec, "rangetype");
  494. if (!string.IsNullOrEmpty(rangetype))
  495. {
  496. return rangetype.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  497. }
  498. }
  499. return Array.Empty<string>();
  500. }
  501. public string[] GetRequestedCodecTags(string codec)
  502. {
  503. if (!string.IsNullOrEmpty(BaseRequest.CodecTag))
  504. {
  505. return BaseRequest.CodecTag.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  506. }
  507. if (!string.IsNullOrEmpty(codec))
  508. {
  509. var codectag = BaseRequest.GetOption(codec, "codectag");
  510. if (!string.IsNullOrEmpty(codectag))
  511. {
  512. return codectag.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  513. }
  514. }
  515. return Array.Empty<string>();
  516. }
  517. public string GetRequestedLevel(string codec)
  518. {
  519. if (!string.IsNullOrEmpty(BaseRequest.Level))
  520. {
  521. return BaseRequest.Level;
  522. }
  523. if (!string.IsNullOrEmpty(codec))
  524. {
  525. return BaseRequest.GetOption(codec, "level");
  526. }
  527. return null;
  528. }
  529. public int? GetRequestedMaxRefFrames(string codec)
  530. {
  531. if (BaseRequest.MaxRefFrames.HasValue)
  532. {
  533. return BaseRequest.MaxRefFrames;
  534. }
  535. if (!string.IsNullOrEmpty(codec))
  536. {
  537. var value = BaseRequest.GetOption(codec, "maxrefframes");
  538. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  539. {
  540. return result;
  541. }
  542. }
  543. return null;
  544. }
  545. public int? GetRequestedVideoBitDepth(string codec)
  546. {
  547. if (BaseRequest.MaxVideoBitDepth.HasValue)
  548. {
  549. return BaseRequest.MaxVideoBitDepth;
  550. }
  551. if (!string.IsNullOrEmpty(codec))
  552. {
  553. var value = BaseRequest.GetOption(codec, "videobitdepth");
  554. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  555. {
  556. return result;
  557. }
  558. }
  559. return null;
  560. }
  561. public int? GetRequestedAudioBitDepth(string codec)
  562. {
  563. if (BaseRequest.MaxAudioBitDepth.HasValue)
  564. {
  565. return BaseRequest.MaxAudioBitDepth;
  566. }
  567. if (!string.IsNullOrEmpty(codec))
  568. {
  569. var value = BaseRequest.GetOption(codec, "audiobitdepth");
  570. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  571. {
  572. return result;
  573. }
  574. }
  575. return null;
  576. }
  577. public int? GetRequestedAudioChannels(string codec)
  578. {
  579. if (!string.IsNullOrEmpty(codec))
  580. {
  581. var value = BaseRequest.GetOption(codec, "audiochannels");
  582. if (int.TryParse(value, CultureInfo.InvariantCulture, out var result))
  583. {
  584. return result;
  585. }
  586. }
  587. if (BaseRequest.MaxAudioChannels.HasValue)
  588. {
  589. return BaseRequest.MaxAudioChannels;
  590. }
  591. if (BaseRequest.AudioChannels.HasValue)
  592. {
  593. return BaseRequest.AudioChannels;
  594. }
  595. if (BaseRequest.TranscodingMaxAudioChannels.HasValue)
  596. {
  597. return BaseRequest.TranscodingMaxAudioChannels;
  598. }
  599. return null;
  600. }
  601. public virtual void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  602. {
  603. Progress.Report(percentComplete.Value);
  604. }
  605. }
  606. }