avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
subtitle.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <string_view>
11#include <vector>
12
13#include "avcompat.h"
14#include "averror.h"
15#include "ffmpeg.h"
16#include "rational.h"
17#include "timestamp.h"
18
19#if AVCPP_CXX_STANDARD >= 20
20#include <span>
21#endif
22
23extern "C" {
24#include <libavcodec/avcodec.h>
25}
26
27namespace av {
28
29namespace subtitle {
30
39std::string_view defaultAssHeader() noexcept;
40
41} // namespace subtitle
42
50class SubtitleRect : public FFWrapperPtr<AVSubtitleRect>
51{
52public:
53 SubtitleRect() = default;
61 explicit SubtitleRect(AVSubtitleRect *rect);
62
68 bool isValid() const noexcept;
74 operator bool() const noexcept;
75
82 AVSubtitleType type() const noexcept;
88 void setType(AVSubtitleType type) noexcept;
89
95 int x() const noexcept;
101 int y() const noexcept;
107 int width() const noexcept;
113 int height() const noexcept;
122 void setArea(int x, int y, int width, int height) noexcept;
123
129 int nbColors() const noexcept;
130
135 std::string_view text() const noexcept;
140 std::string_view ass() const noexcept;
141
151 void setText(std::string_view text, OptionalErrorCode ec = throws());
158 void setAss(std::string_view ass, OptionalErrorCode ec = throws());
162 void clearText() noexcept;
166 void clearAss() noexcept;
167
176 bool isForced() const noexcept;
182 void setForced(bool forced) noexcept;
183
193 uint8_t *data(size_t plane = 0) noexcept;
201 const uint8_t *data(size_t plane = 0) const noexcept;
209 int linesize(size_t plane = 0) const noexcept;
210
220 void setBitmap(const uint8_t *data, int stride, int width, int height,
228 void setPalette(const uint32_t *palette, int count, OptionalErrorCode ec = throws());
229
230#if AVCPP_CXX_STANDARD >= 20
235 std::span<const uint8_t> bitmap() const noexcept;
240 std::span<const uint32_t> palette() const noexcept;
241
251 void setBitmap(std::span<const uint8_t> data, int stride, int width, int height,
259 void setPalette(std::span<const uint32_t> palette, OptionalErrorCode ec = throws());
260#endif
261};
262
273class SubtitleFrame
274{
275public:
276 SubtitleFrame() noexcept;
282 SubtitleFrame(SubtitleFrame &&other) noexcept;
290 SubtitleFrame &operator=(SubtitleFrame &&other) noexcept;
291 SubtitleFrame(const SubtitleFrame &) = delete;
292 SubtitleFrame &operator=(const SubtitleFrame &) = delete;
293 ~SubtitleFrame();
294
301 SubtitleFrame clone(OptionalErrorCode ec = throws()) const;
302
310 AVSubtitle *raw() noexcept;
316 const AVSubtitle *raw() const noexcept;
317
323 bool isValid() const noexcept;
330 operator bool() const noexcept;
331
336 uint16_t format() const noexcept;
342 void setFormat(uint16_t format) noexcept;
348 bool isText() const noexcept;
349
355 uint32_t startDisplayTime() const noexcept;
361 uint32_t endDisplayTime() const noexcept;
367 void setStartDisplayTime(uint32_t ms) noexcept;
373 void setEndDisplayTime(uint32_t ms) noexcept;
374
380 Timestamp pts() const noexcept;
386 void setPts(const Timestamp &ts);
392 const Rational &timeBase() const noexcept;
398 void setTimeBase(const Rational &value) noexcept;
399
404 Timestamp startTime() const noexcept;
409 Timestamp endTime() const noexcept;
410
416 int streamIndex() const noexcept;
422 void setStreamIndex(int index) noexcept;
423
428 bool isComplete() const noexcept;
434 void setComplete(bool complete) noexcept;
435
441 size_t numRects() const noexcept;
449 SubtitleRect rect(size_t index) const;
455 std::vector<SubtitleRect> rects() const;
456
463 SubtitleRect addRect(AVSubtitleType type = SUBTITLE_NONE,
471 void clear() noexcept;
472
478 std::string_view text() const noexcept;
484 std::string_view ass() const noexcept;
485
497 static SubtitleFrame fromAss(std::string_view ass, OptionalErrorCode ec = throws());
509 static SubtitleFrame fromText(std::string_view text, OptionalErrorCode ec = throws());
510
511private:
512 void swap(SubtitleFrame &other) noexcept;
513
514 AVSubtitle m_raw{};
515 Rational m_timeBase{1, AV_TIME_BASE};
516 int m_streamIndex{-1};
517 bool m_isComplete{false};
518};
519
520} // namespace av
FFmpeg version and API compatibility switches.
Error codes, error categories and the OptionalErrorCode convention.
Carries the caller's choice between exceptions and error codes.
Definition averror.h:96
Rational number: a numerator and a denominator.
Definition rational.h:49
void setTimeBase(const Rational &value) noexcept
Set the time base, rescaling the stored timestamp.
Timestamp startTime() const noexcept
pts + startDisplayTime(), in the frame time base.
bool isComplete() const noexcept
True when a decoder produced this frame, including empty subtitles.
const Rational & timeBase() const noexcept
Time base of pts().
void setPts(const Timestamp &ts)
Set the presentation timestamp.
bool isText() const noexcept
Whether the subtitle is textual.
std::string_view text() const noexcept
Text of the subtitle: the payload of the first text rectangle.
uint32_t endDisplayTime() const noexcept
End of the display interval, relative to pts().
SubtitleRect rect(size_t index) const
Rectangle by index.
Timestamp endTime() const noexcept
pts + endDisplayTime(), in the frame time base.
void setEndDisplayTime(uint32_t ms) noexcept
Set the end of the display interval.
std::vector< SubtitleRect > rects() const
All rectangles as a vector of views.
void setComplete(bool complete) noexcept
Mark the frame as produced by a decoder.
SubtitleFrame clone(OptionalErrorCode ec=throws()) const
Deep copy: strings, bitmap (linesize(0) * height) and palette are copied.
static SubtitleFrame fromText(std::string_view text, OptionalErrorCode ec=throws())
Build a frame from plain text.
void clear() noexcept
Releases all rectangles and resets the frame state.
size_t numRects() const noexcept
Number of rectangles in the frame.
bool isValid() const noexcept
True when the frame contains rectangles or is a decoded empty subtitle (for example,...
SubtitleFrame(SubtitleFrame &&other) noexcept
Move the frame.
static SubtitleFrame fromAss(std::string_view ass, OptionalErrorCode ec=throws())
Build a frame from one ASS dialogue line.
std::string_view ass() const noexcept
ASS line of the subtitle: the payload of the first ASS rectangle.
SubtitleRect addRect(AVSubtitleType type=SUBTITLE_NONE, OptionalErrorCode ec=throws())
Appends a new empty rectangle owned by this frame.
void setStartDisplayTime(uint32_t ms) noexcept
Set the start of the display interval.
void setStreamIndex(int index) noexcept
Set the stream index.
int streamIndex() const noexcept
Index of the stream the subtitle came from.
uint32_t startDisplayTime() const noexcept
Start of the display interval, relative to pts().
Timestamp pts() const noexcept
Presentation timestamp of the subtitle.
AVSubtitle * raw() noexcept
The underlying FFmpeg subtitle.
void setFormat(uint16_t format) noexcept
Set the subtitle kind.
SubtitleFrame & operator=(SubtitleFrame &&other) noexcept
Move assignment: frees the current content and takes the other frame's.
uint16_t format() const noexcept
0 for graphics, non-zero for text.
Non-owning view over AVSubtitleRect.
Definition subtitle.h:51
int y() const noexcept
Vertical position of the rectangle.
int width() const noexcept
Width of the bitmap.
void setArea(int x, int y, int width, int height) noexcept
Set the position and the size at once.
int nbColors() const noexcept
Number of palette entries of a bitmap rectangle.
std::span< const uint8_t > bitmap() const noexcept
Plane 0 as an index bitmap: linesize(0) * height() bytes.
std::span< const uint32_t > palette() const noexcept
Plane 1 as a palette: nbColors() entries.
std::string_view ass() const noexcept
ASS/SSA event line.
void setForced(bool forced) noexcept
Mark the rectangle as forced.
void setBitmap(const uint8_t *data, int stride, int width, int height, OptionalErrorCode ec=throws())
Sets plane 0 to an index bitmap and updates width, height and linesize.
int x() const noexcept
Horizontal position of the rectangle.
std::string_view text() const noexcept
Plain UTF-8 text.
void setAss(std::string_view ass, OptionalErrorCode ec=throws())
Store an ASS/SSA event line in the rectangle.
SubtitleRect(AVSubtitleRect *rect)
Wrap a rectangle.
void setText(std::string_view text, OptionalErrorCode ec=throws())
Store plain UTF-8 text in the rectangle.
void clearText() noexcept
Free the text payload and clear the pointer.
AVSubtitleType type() const noexcept
Kind of payload the rectangle carries.
void clearAss() noexcept
Free the ASS payload and clear the pointer.
void setPalette(const uint32_t *palette, int count, OptionalErrorCode ec=throws())
Sets plane 1 to a palette and updates nb_colors.
bool isValid() const noexcept
Whether the view refers to a rectangle.
int height() const noexcept
Height of the bitmap.
void setType(AVSubtitleType type) noexcept
Set the payload kind.
bool isForced() const noexcept
Whether the rectangle is flagged as forced.
uint8_t * data(size_t plane=0) noexcept
Pointer to one image plane.
int linesize(size_t plane=0) const noexcept
Distance between two rows of a plane.
The Timestamp class represents timestamp value and it timebase.
Definition timestamp.h:25
FFmpeg types re-exported for the public API and small value wrappers.
Root namespace of the library.
Definition audioresampler.h:19
OptionalErrorCode throws()
Helper to construct null OptionalErrorCode object.
Definition averror.h:310
STL namespace.
Rational numbers, time bases and arithmetic on them.
std::string_view defaultAssHeader() noexcept
Returns a built-in ASS/SSA script header accepted by the FFmpeg text subtitle encoders (subrip,...
Frame timestamps and their arithmetic.