EncodingJobInfo.cs 21 KB

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