EncodingJobInfo.cs 20 KB

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