StormByte C++ Library: Multimedia module 0.0.9999
StormByte-Multimedia is a StormByte library module for parsing configuration files
Loading...
Searching...
No Matches
transcoder.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Multimedia.
5 *
6 * StormByte-Multimedia original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Multimedia source in this
20 * file. Third-party components — including FFmpeg and embedded trained data —
21 * remain under their own licenses and are not covered by the commercial grant.
22 *
23 * Neither license grants any patent rights. Any patent licenses required
24 * to use this software or third-party components must be obtained separately
25 * from the patent holders.
26 *
27 * StormByte-Multimedia is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 *
32 * You should have received a copy of the GNU Lesser General Public License
33 * version 3 along with StormByte-Multimedia. If not, see
34 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
35 *
36 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
37 */
38
39#pragma once
40
41#include <StormByte/buffer/consumer.hxx>
42#include <StormByte/clonable.hxx>
43#include <StormByte/logger/log.hxx>
53
54#include <chrono>
55#include <cstdint>
56#include <filesystem>
57#include <map>
58#include <memory>
59#include <optional>
60#include <string>
61#include <string_view>
62#include <type_traits>
63#include <utility>
64#include <vector>
65
73 class Transcoder;
74}
75
83 class Encoder;
84 class Transcoder;
85
96 enum class Status {
97 Stopped,
98 Running,
99 Paused,
100 Done,
101 Error,
102 Aborted
103 };
104
116 public StormByte::Clonable<TrackSettled, std::unique_ptr<TrackSettled>> {
117 public:
126 TrackSettled() noexcept = default;
127
132 TrackSettled(const TrackSettled& other) = default;
133
138 TrackSettled(TrackSettled&& other) noexcept = default;
139
143 virtual ~TrackSettled() noexcept override = default;
144
150 TrackSettled& operator=(const TrackSettled& other) = default;
151
157 TrackSettled& operator=(TrackSettled&& other) noexcept = default;
158
167 inline PointerType Clone() const override {
168 return MakePointer<TrackSettled>(*this);
169 }
170
175 inline PointerType Move() override {
176 return MakePointer<TrackSettled>(std::move(*this));
177 }
178
183 virtual std::string ToString() const;
184
185 int In = -1;
186 int Out = -1;
187 Type Kind = Type::Unknown;
188 const Codec* Source = nullptr;
189 const Codec* Destination = nullptr;
190 std::optional<std::string> Implementation;
191 std::optional<int> Crf;
192 std::optional<std::int64_t> BitRate;
193 std::optional<std::int64_t> MaxBitRate;
194 std::optional<std::string> Preset;
195 std::optional<std::string> Tune;
196 std::map<std::string, std::string> FineTune;
197 std::optional<int> SampleFormat;
198 std::optional<int> SourceChannels;
199 std::optional<int> EncoderChannels;
200 std::optional<int> FrameSize;
201 std::optional<int> SampleRate;
202 };
203
255 public:
266 public:
276 Track(const Track& other) noexcept = default;
277
282 Track(Track&& other) noexcept = default;
283
287 ~Track() noexcept = default;
288
294 Track& operator=(const Track& other) noexcept = default;
295
301 Track& operator=(Track&& other) noexcept = default;
302
311 Track& Remux() noexcept;
312
318 Track& Codec(const Codec& codec) noexcept;
319
325 Track& Implementation(std::string name) noexcept;
326
332 Track& CRF(int value) noexcept;
333
339 Track& BitRate(std::int64_t bits_per_second) noexcept;
340
346 Track& MaxBitRate(std::int64_t bits_per_second) noexcept;
347
353 Track& Preset(std::string name) noexcept;
354
360 Track& Tune(std::string name) noexcept;
361
367 Track& FineTune(std::map<std::string, std::string> options) noexcept;
368
374 Track& Language(std::string language) noexcept;
375
381 Track& Title(std::string title) noexcept;
382
394 template<typename FilterType, typename... Args>
395 Track& Filter(Args&&... args) noexcept {
396 m_owner->AttachFilter(m_slot,
397 std::make_shared<FilterType>(std::forward<Args>(args)...));
398 return *this;
399 }
400
401 private:
402 friend class Transcoder;
403
409 Track(Transcoder& owner, std::size_t slot) noexcept;
410
411 Transcoder* m_owner;
412 std::size_t m_slot;
413 };
414
428 Transcoder(std::shared_ptr<StormByte::Logger::Log> logger, File&& file) noexcept;
429
434 Transcoder(const Transcoder& other) = delete;
435
440 Transcoder(Transcoder&& other) noexcept = delete;
441
445 virtual ~Transcoder() noexcept;
446
452 Transcoder& operator=(const Transcoder& other) = delete;
453
459 Transcoder& operator=(Transcoder&& other) noexcept = delete;
460
485 static ExpectedTranscoder Open(std::shared_ptr<StormByte::Logger::Log> logger,
486 const std::filesystem::path& source,
487 const std::filesystem::path& destination,
488 std::optional<std::chrono::nanoseconds> duration = std::nullopt) noexcept;
489
505 const File& Source() const noexcept;
506
515 const std::shared_ptr<StormByte::Logger::Log>& Logger() const noexcept;
516
521 inline const std::shared_ptr<class Plan>& Plan() const noexcept {
522 return m_plan;
523 }
524
539 Track Video(int in) noexcept;
540
546 Track Audio(int in) noexcept;
547
553 Track Subtitle(int in) noexcept;
554
564
570 Transcoder& Attachments(std::string_view mime) noexcept;
571
577 Transcoder& Ignore(int in) noexcept;
578
588 template<typename FilterType, typename... Args>
589 Transcoder& Filter(Args&&... args) noexcept {
590 static_assert(std::is_base_of_v<Filter::Analytics, FilterType>,
591 "Track filters attach on Track::Filter");
592 AttachAnalytics(std::make_shared<FilterType>(std::forward<Args>(args)...));
593 return *this;
594 }
595
605 Transcoder& Destination(const Container& container, std::filesystem::path path) noexcept;
606
623 void Run() noexcept;
624
628 void Cancel() noexcept;
629
633 void Pause() noexcept;
634
638 void Resume() noexcept;
639
644 enum Status Status() const noexcept;
645
650 bool Failed() const noexcept;
651
656 std::optional<std::string> Error() const noexcept;
657
662 std::optional<unsigned> Progress() const noexcept;
663
677 std::vector<std::pair<std::string, Filter::Report>> Reports() const noexcept;
678
683 explicit operator bool() const noexcept;
684
689 protected:
700 virtual void InstallLog() noexcept;
701
709 inline const std::shared_ptr<StormByte::Logger::Log>& ApplicationLog() const noexcept {
710 return m_app_log;
711 }
712
723 virtual std::unique_ptr<class Plan> EmptyPlan(File&& source,
724 const Container& container,
725 std::filesystem::path destination) const noexcept;
726
734 virtual std::unique_ptr<TrackSettled> EmptySettled() const noexcept;
735
739 virtual void OnConfigure() noexcept;
740
745 virtual enum Status OnStart() noexcept;
746
751 virtual void OnPlan(const class Plan& plan) noexcept;
752
757 virtual void OnSettled(const TrackSettled& track) noexcept;
758
763 virtual void OnProgress(unsigned percent) noexcept;
764
768 virtual void OnDone() noexcept;
769
774 virtual void OnError(const std::string& message) noexcept;
775
779 virtual void OnAborted() noexcept;
780
781 private:
782 friend class Backend::Pipeline::Transcoder;
783 friend class Track;
784
789 void Fail(std::string reason) noexcept;
790
797 static ExpectedTranscoder BindLoggerAndFile(std::shared_ptr<StormByte::Logger::Log> logger,
798 ExpectedFile opened) noexcept;
799
806 Track AddTrack(int in, Type kind) noexcept;
807
813 void MarkSettled(int in, Encoder& encoder) noexcept;
814
819 void SetProgress(unsigned percent) noexcept;
820
826 bool ValidSlot(std::size_t slot) const noexcept;
827
833 void AttachFilter(std::size_t slot, std::shared_ptr<Filter::FFmpeg> filter) noexcept;
834
839 void AttachAnalytics(std::shared_ptr<Filter::FFmpeg> filter) noexcept;
840
841 std::shared_ptr<StormByte::Logger::Log> m_app_log;
842 std::shared_ptr<StormByte::Logger::Log> m_logger;
843 std::unique_ptr<File> m_file;
844 std::shared_ptr<class Plan> m_plan;
845 std::unique_ptr<Backend::Pipeline::Transcoder> m_backend;
846 };
847}
Immutable codec identity owned by Registry.
Definition codec.hxx:58
Immutable container identity owned by StormByte::Multimedia::Registry.
Definition container.hxx:59
Snapshot of a media source: path or Consumer, container, streams and tags.
Definition file.hxx:84
Encodes frames of one output track into packets.
Definition encoder.hxx:103
Closed job intention: source File, destination container and tracks that enter the tube.
Definition plan.hxx:76
What an encoder actually opened.
Definition transcoder.hxx:116
std::optional< std::int64_t > MaxBitRate
VBV actually used.
Definition transcoder.hxx:193
virtual std::string ToString() const
Human-readable line for logs.
std::optional< std::int64_t > BitRate
Bitrate actually used.
Definition transcoder.hxx:192
std::optional< int > EncoderChannels
Encoder channel count.
Definition transcoder.hxx:199
std::optional< int > SampleFormat
Encoder AVSampleFormat.
Definition transcoder.hxx:197
PointerType Move() override
Move into a new pointer.
Definition transcoder.hxx:175
std::optional< int > SourceChannels
Decoded channel count.
Definition transcoder.hxx:198
std::optional< int > Crf
CRF/CQ actually used.
Definition transcoder.hxx:191
std::optional< std::string > Implementation
Opened encoder pin.
Definition transcoder.hxx:190
std::optional< std::string > Tune
Tune actually used.
Definition transcoder.hxx:195
std::optional< int > SampleRate
Samples per second.
Definition transcoder.hxx:201
std::map< std::string, std::string > FineTune
Vendor leftovers actually used.
Definition transcoder.hxx:196
TrackSettled() noexcept=default
Empty settled row.
std::optional< std::string > Preset
Preset actually used.
Definition transcoder.hxx:194
std::optional< int > FrameSize
Encoder frame_size.
Definition transcoder.hxx:200
Fluent handle for one origin stream in this job.
Definition transcoder.hxx:265
Track(Track &&other) noexcept=default
Move constructor.
Track(const Track &other) noexcept=default
Copy constructor.
Facade that maps tracks and runs one file-to-file job.
Definition transcoder.hxx:254
Track Subtitle(int in) noexcept
Maps a subtitle origin stream.
Transcoder(std::shared_ptr< StormByte::Logger::Log > logger, File &&file) noexcept
Constructs an empty job.
Transcoder & Attachments() noexcept
Keeps every attachment from the source File.
void Run() noexcept
Builds the Plan, starts the coordinator and returns.
Track Video(int in) noexcept
Maps a video origin stream.
Track Audio(int in) noexcept
Maps an audio origin stream.
virtual ~Transcoder() noexcept
Destructor.
Transcoder(Transcoder &&other) noexcept=delete
Move constructor.
virtual std::unique_ptr< class Plan > EmptyPlan(File &&source, const Container &container, std::filesystem::path destination) const noexcept
Allocates the Plan type for this job.
Transcoder & Destination(const Container &container, std::filesystem::path path) noexcept
Sets the destination container and path.
virtual std::unique_ptr< TrackSettled > EmptySettled() const noexcept
Allocates the settled-row type.
Transcoder(const Transcoder &other)=delete
Copy constructor.
Frame and packet steps attached to a job or a raw pipeline.
Multimedia-owned pipeline stages and unit holders.
Demux / decode / filter / encode / mux types.
Kind
Definition typedefs.hxx:78
Status
Definition transcoder.hxx:96
@ Running
Coordinator is alive and not paused.
@ Aborted
Cancel() while Running/Paused, or OnStart returned Aborted.
@ Error
A stage failed; Error() has text.
@ Stopped
Open succeeded; Run has not started, or OnStart declined.
@ Paused
Pause(); coordinator blocks until Resume or Cancel.
StormByte::Expected< std::unique_ptr< Pipeline::Transcoder >, TranscodeException > ExpectedTranscoder
Result of Transcoder::Open.
Definition typedefs.hxx:71
Type
Kind of media stream or codec.
Definition type.hxx:55
std::vector< Attachment > Attachments
Definition attachment.hxx:122
StormByte::Expected< File, FileOpenException > ExpectedFile
Result of OpenFile.
Definition typedefs.hxx:70
#define STORMBYTE_MULTIMEDIA_PUBLIC
Imported symbol.
Definition visibility.h:47