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>
54
55#include <chrono>
56#include <cstdint>
57#include <filesystem>
58#include <map>
59#include <memory>
60#include <optional>
61#include <string>
62#include <string_view>
63#include <type_traits>
64#include <utility>
65#include <vector>
66
74 class Transcoder;
75}
76
84 class Encoder;
85 class Transcoder;
86
105
117 public StormByte::Clonable<TrackSettled, std::unique_ptr<TrackSettled>> {
118 public:
123
127 TrackSettled() noexcept = default;
128
133 TrackSettled(const TrackSettled& other) = default;
134
139 TrackSettled(TrackSettled&& other) noexcept = default;
140
144 virtual ~TrackSettled() noexcept override = default;
145
151 TrackSettled& operator=(const TrackSettled& other) = default;
152
158 TrackSettled& operator=(TrackSettled&& other) noexcept = default;
159
163
168 inline PointerType Clone() const override {
169 return MakePointer<TrackSettled>(*this);
170 }
171
176 inline PointerType Move() override {
177 return MakePointer<TrackSettled>(std::move(*this));
178 }
179
184 virtual std::string ToString() const;
185
186 int In = -1;
187 int Out = -1;
189 const Codec* Source = nullptr;
190 const Codec* Destination = nullptr;
191 std::optional<std::string> Implementation;
192 std::optional<int> Crf;
193 std::optional<std::int64_t> BitRate;
194 std::optional<std::int64_t> MaxBitRate;
195 std::optional<std::string> Preset;
196 std::optional<std::string> Tune;
197 std::map<std::string, std::string> FineTune;
198 std::optional<int> SampleFormat;
199 std::optional<int> SourceChannels;
200 std::optional<int> EncoderChannels;
201 std::optional<int> FrameSize;
202 std::optional<int> SampleRate;
203 };
204
259 public:
270 public:
275
280 Track(const Track& other) noexcept = default;
281
286 Track(Track&& other) noexcept = default;
287
291 ~Track() noexcept = default;
292
298 Track& operator=(const Track& other) noexcept = default;
299
305 Track& operator=(Track&& other) noexcept = default;
306
310
315 Track& Remux() noexcept;
316
322 Track& Codec(const Codec& codec) noexcept;
323
329 Track& Implementation(std::string name) noexcept;
330
336 Track& CRF(int value) noexcept;
337
343 Track& BitRate(std::int64_t bits_per_second) noexcept;
344
350 Track& MaxBitRate(std::int64_t bits_per_second) noexcept;
351
357 Track& Preset(std::string name) noexcept;
358
364 Track& Tune(std::string name) noexcept;
365
371 Track& FineTune(std::map<std::string, std::string> options) noexcept;
372
378 Track& Language(std::string language) noexcept;
379
385 Track& Title(std::string title) noexcept;
386
398 template<typename FilterType, typename... Args>
399 Track& Filter(Args&&... args) noexcept {
400 m_owner->AttachFilter(m_slot,
401 std::make_shared<FilterType>(std::forward<Args>(args)...));
402 return *this;
403 }
404
405 private:
406 friend class Transcoder;
407
413 Track(Transcoder& owner, std::size_t slot) noexcept;
414
415 Transcoder* m_owner;
416 std::size_t m_slot;
417 };
418
423
432 Transcoder(std::shared_ptr<StormByte::Logger::Log> logger, File&& file) noexcept;
433
438 Transcoder(const Transcoder& other) = delete;
439
444 Transcoder(Transcoder&& other) noexcept = delete;
445
449 virtual ~Transcoder() noexcept;
450
456 Transcoder& operator=(const Transcoder& other) = delete;
457
463 Transcoder& operator=(Transcoder&& other) noexcept = delete;
464
468
473
489 static ExpectedTranscoder Open(std::shared_ptr<StormByte::Logger::Log> logger,
490 const std::filesystem::path& source,
491 const std::filesystem::path& destination,
492 std::optional<std::chrono::nanoseconds> duration = std::nullopt) noexcept;
493
497
502
509 const File& Source() const noexcept;
510
519 const std::shared_ptr<StormByte::Logger::Log>& Logger() const noexcept;
520
525 inline const std::shared_ptr<class Plan>& Plan() const noexcept {
526 return m_plan;
527 }
528
532
537
543 Track Video(int in) noexcept;
544
550 Track Audio(int in) noexcept;
551
557 Track Subtitle(int in) noexcept;
558
568
574 Transcoder& Attachments(std::string_view mime) noexcept;
575
581 Transcoder& Ignore(int in) noexcept;
582
592 template<typename FilterType, typename... Args>
593 Transcoder& Filter(Args&&... args) noexcept {
594 static_assert(std::is_base_of_v<Filter::Analytics, FilterType>,
595 "Track filters attach on Track::Filter");
596 AttachAnalytics(std::make_shared<FilterType>(std::forward<Args>(args)...));
597 return *this;
598 }
599
609 Transcoder& Destination(const Container& container, std::filesystem::path path) noexcept;
610
614
619
627 void Run() noexcept;
628
632 void Cancel() noexcept;
633
637 void Pause() noexcept;
638
642 void Resume() noexcept;
643
648 enum Status Status() const noexcept;
649
654 bool Failed() const noexcept;
655
660 std::optional<std::string> Error() const noexcept;
661
670 Progress::Pointer Progress() const noexcept;
671
685 std::vector<std::pair<std::string, Filter::Report>> Reports() const noexcept;
686
691 explicit operator bool() const noexcept;
692
696
697 protected:
708 virtual void InstallLog() noexcept;
709
717 inline const std::shared_ptr<StormByte::Logger::Log>& ApplicationLog() const noexcept {
718 return m_app_log;
719 }
720
731 virtual std::unique_ptr<class Plan> EmptyPlan(File&& source,
732 const Container& container,
733 std::filesystem::path destination) const noexcept;
734
742 virtual std::unique_ptr<TrackSettled> EmptySettled() const noexcept;
743
747 virtual void OnConfigure() noexcept;
748
753 virtual enum Status OnStart() noexcept;
754
759 virtual void OnPlan(const class Plan& plan) noexcept;
760
765 virtual void OnSettled(const TrackSettled& track) noexcept;
766
772 virtual void OnMeasureDone() noexcept;
773
779 virtual void OnAnalyticsDone() noexcept;
780
784 virtual void OnProgress() noexcept;
785
789 virtual void OnDone() noexcept;
790
795 virtual void OnError(const std::string& message) noexcept;
796
800 virtual void OnAborted() noexcept;
801
802 private:
804 friend class Track;
805
810 void Fail(std::string reason) noexcept;
811
818 static ExpectedTranscoder BindLoggerAndFile(std::shared_ptr<StormByte::Logger::Log> logger,
819 ExpectedFile opened) noexcept;
820
827 Track AddTrack(int in, Type kind) noexcept;
828
834 void MarkSettled(int in, Encoder& encoder) noexcept;
835
841 bool ValidSlot(std::size_t slot) const noexcept;
842
848 void AttachFilter(std::size_t slot, std::shared_ptr<Filter::FFmpeg> filter) noexcept;
849
854 void AttachAnalytics(std::shared_ptr<Filter::FFmpeg> filter) noexcept;
855
856 std::shared_ptr<StormByte::Logger::Log> m_app_log;
857 std::shared_ptr<StormByte::Logger::Log> m_logger;
858 std::unique_ptr<File> m_file;
859 std::shared_ptr<class Plan> m_plan;
860 std::unique_ptr<Backend::Pipeline::Transcoder> m_backend;
861 };
862}
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
What an encoder actually opened.
Definition transcoder.hxx:117
std::optional< std::int64_t > MaxBitRate
VBV actually used.
Definition transcoder.hxx:194
virtual std::string ToString() const
Human-readable line for logs.
std::optional< std::int64_t > BitRate
Bitrate actually used.
Definition transcoder.hxx:193
std::optional< int > EncoderChannels
Encoder channel count.
Definition transcoder.hxx:200
std::optional< int > SampleFormat
Encoder AVSampleFormat.
Definition transcoder.hxx:198
const Codec * Destination
Opened encoder codec, or nullptr if remux.
Definition transcoder.hxx:190
PointerType Move() override
Move into a new pointer.
Definition transcoder.hxx:176
std::optional< int > SourceChannels
Decoded channel count.
Definition transcoder.hxx:199
int In
Origin stream or attachment slot.
Definition transcoder.hxx:186
std::optional< int > Crf
CRF/CQ actually used.
Definition transcoder.hxx:192
std::optional< std::string > Implementation
Opened encoder pin.
Definition transcoder.hxx:191
std::optional< std::string > Tune
Tune actually used.
Definition transcoder.hxx:196
std::optional< int > SampleRate
Samples per second.
Definition transcoder.hxx:202
Type Kind
Media kind.
Definition transcoder.hxx:188
const Codec * Source
Origin codec.
Definition transcoder.hxx:189
std::map< std::string, std::string > FineTune
Vendor leftovers actually used.
Definition transcoder.hxx:197
TrackSettled() noexcept=default
Empty settled row.
std::optional< std::string > Preset
Preset actually used.
Definition transcoder.hxx:195
std::optional< int > FrameSize
Encoder frame_size.
Definition transcoder.hxx:201
PointerType Clone() const override
Deep copy.
Definition transcoder.hxx:168
int Out
Index in Plan::Tracks after Add.
Definition transcoder.hxx:187
Fluent handle for one origin stream in this job.
Definition transcoder.hxx:269
Track(Track &&other) noexcept=default
Move constructor.
Track & CRF(int value) noexcept
Sets CRF/CQ.
Track & Tune(std::string name) noexcept
Sets encoder tune.
Track & BitRate(std::int64_t bits_per_second) noexcept
Sets target bitrate.
Track & Remux() noexcept
Remux this track (no destination codec).
Track & MaxBitRate(std::int64_t bits_per_second) noexcept
Sets VBV / max bitrate.
Track & Implementation(std::string name) noexcept
Pins an FFmpeg encoder name.
Track(const Track &other) noexcept=default
Copy constructor.
Track & Language(std::string language) noexcept
Overrides stream language.
Track & Title(std::string title) noexcept
Overrides stream title.
Track & Preset(std::string name) noexcept
Sets encoder preset.
Track & FineTune(std::map< std::string, std::string > options) noexcept
Replaces vendor leftovers.
Track & Codec(const Codec &codec) noexcept
Encodes this track to codec.
friend class Transcoder
Definition transcoder.hxx:406
Facade that maps tracks and runs one file-to-file job.
Definition transcoder.hxx:258
const std::shared_ptr< StormByte::Logger::Log > & Logger() const noexcept
Logger used by this job after InstallLog.
void Cancel() noexcept
Requests abort.
static ExpectedTranscoder Open(std::shared_ptr< StormByte::Logger::Log > logger, const std::filesystem::path &source, const std::filesystem::path &destination, std::optional< std::chrono::nanoseconds > duration=std::nullopt) noexcept
Opens source and binds destination.
virtual void InstallLog() noexcept
Scopes this job's own logger.
enum Status Status() const noexcept
Current lifecycle value.
void Pause() noexcept
Pauses the coordinator.
std::vector< std::pair< std::string, Filter::Report > > Reports() const noexcept
Analytics snapshots after the job is Idle.
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.
virtual void OnSettled(const TrackSettled &track) noexcept
One encode lane finished Encoder open.
const std::shared_ptr< StormByte::Logger::Log > & ApplicationLog() const noexcept
Application logger passed to Open.
Definition transcoder.hxx:717
friend class Backend::Pipeline::Transcoder
Definition transcoder.hxx:803
virtual void OnAnalyticsDone() noexcept
Analytics taps idle.
virtual void OnAborted() noexcept
Cancel completed.
Transcoder & Attachments() noexcept
Keeps every attachment from the source File.
void Run() noexcept
Builds the Plan, starts the coordinator and returns.
virtual void OnMeasureDone() noexcept
Measure pass closed.
const std::shared_ptr< class Plan > & Plan() const noexcept
Intention built for this job, if any.
Definition transcoder.hxx:525
std::optional< std::string > Error() const noexcept
Failure text.
Track Video(int in) noexcept
Maps a video origin stream.
virtual void OnError(const std::string &message) noexcept
Hard error.
Transcoder & Ignore(int in) noexcept
Drops an origin stream (omit from the Plan).
Track Audio(int in) noexcept
Maps an audio origin stream.
virtual enum Status OnStart() noexcept
Gate after the Plan is filled and Destination is set.
virtual void OnProgress() noexcept
Progress tick.
virtual ~Transcoder() noexcept
Destructor.
bool Failed() const noexcept
Whether the job failed.
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.
Progress::Pointer Progress() const noexcept
Shared tube clock.
Transcoder & Destination(const Container &container, std::filesystem::path path) noexcept
Sets the destination container and path.
const File & Source() const noexcept
Opened source file.
virtual std::unique_ptr< TrackSettled > EmptySettled() const noexcept
Allocates the settled-row type.
virtual void OnConfigure() noexcept
Last chance to raise ceilings before the job starts.
void Resume() noexcept
Resumes after Pause.
virtual void OnPlan(const class Plan &plan) noexcept
Intention, just before plan >> demuxer.
Transcoder(const Transcoder &other)=delete
Copy constructor.
virtual void OnDone() noexcept
Successful flush.
Frame and packet steps attached to a job or a raw pipeline.
Multimedia-owned pipeline stages and unit holders.
Definition AVFormatContext.hxx:52
Definition AVFormatContext.hxx:52
Private RAII wrappers over libav*.
Demux / decode / filter / encode / mux types.
Status
Lifecycle of a Transcoder instance.
Definition transcoder.hxx:97
@ Running
Coordinator is alive and not paused.
Definition transcoder.hxx:99
@ Aborted
Cancel() while Running/Paused, or OnStart returned Aborted.
Definition transcoder.hxx:103
@ Error
A stage failed; Error() has text.
Definition transcoder.hxx:102
@ Stopped
Open succeeded; Run has not started, or OnStart declined.
Definition transcoder.hxx:98
@ Paused
Pause(); coordinator blocks until Resume or Cancel.
Definition transcoder.hxx:100
@ Done
Finished and flushed.
Definition transcoder.hxx:101
Type
Kind of media stream or codec.
Definition type.hxx:55
@ Unknown
Unclassified type.
Definition type.hxx:60
StormByte::Expected< File, FileOpenException > ExpectedFile
Result of OpenFile.
Definition typedefs.hxx:70
StormByte::Expected< std::unique_ptr< Pipeline::Transcoder >, TranscodeException > ExpectedTranscoder
Result of Transcoder::Open.
Definition typedefs.hxx:71
#define STORMBYTE_MULTIMEDIA_PUBLIC
Imported symbol.
Definition visibility.h:47