avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
packet.h
Go to the documentation of this file.
1#pragma once
2
9
10#include "avcompat.h"
11
12#include <vector>
13#include <type_traits>
14
15#if AVCPP_CXX_STANDARD >= 20
16#include <span>
17#endif
18
19#include "ffmpeg.h"
20#include "avutils.h"
21#include "rational.h"
22#include "stream.h"
23#include "averror.h"
24#include "timestamp.h"
25
26extern "C" {
27#include <libavutil/attributes.h>
28}
29
30namespace av {
31
32#if AVCPP_HAS_PKT_SIDE_DATA
36class PacketSideData : public FFWrapperRef<AVPacketSideData>
37{
38public:
39 using FFWrapperRef<AVPacketSideData>::FFWrapperRef;
40
46 std::string_view name() const noexcept;
52 AVPacketSideDataType type() const noexcept;
58 std::span<const uint8_t> span() const noexcept;
64 std::span<uint8_t> span() noexcept;
65
74 static std::string_view name(AVPacketSideDataType type);
75
81 bool empty() const noexcept;
87 operator bool() const noexcept;
88};
89#endif // AVCPP_HAS_PKT_SIDE_DATA
90
104class Packet :
105#if AVCPP_API_AVCODEC_NEW_INIT_PACKET
106 public FFWrapperPtr<AVPacket>
107#else
108 public FFWrapperRef<AVPacket>
109#endif
110{
111private:
112 // if deepCopy true - make deep copy, instead - reference is created
113 void initFromAVPacket(const AVPacket *avpacket, bool deepCopy, OptionalErrorCode ec);
114
115public:
119 struct wrap_data {};
125
126#if AVCPP_API_AVCODEC_NEW_INIT_PACKET
133 explicit Packet(std::nullptr_t); // explicit nullptr packet. There is no extra internal checks, so be careful
134#endif
135
146 Packet(const Packet &packet, OptionalErrorCode ec);
152 Packet(const Packet &packet);
158 Packet(Packet &&packet);
167 explicit Packet(const AVPacket *packet, OptionalErrorCode ec = throws());
173 explicit Packet(const std::vector<uint8_t> &data);
174
184 Packet(const uint8_t *data, size_t size, bool doAllign = true);
185 // data must be allocated with av_malloc() family
196 Packet(uint8_t *data, size_t size, wrap_data, OptionalErrorCode ec = throws());
207 Packet(uint8_t *data, size_t size, wrap_data_static, OptionalErrorCode ec = throws());
208
209#if AVCPP_CXX_STANDARD >= 20
216 explicit Packet(std::span<const uint8_t> data, bool doAllign = true);
217 // data must be allocated with av_malloc() family
226 Packet(std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
234 Packet(std::span<uint8_t> data, wrap_data_static, OptionalErrorCode ec = throws());
235#endif
236
237 ~Packet();
238
247 bool setData(const std::vector<uint8_t> &newData, OptionalErrorCode ec = throws());
260 bool setData(const uint8_t *newData, size_t size, OptionalErrorCode ec = throws());
261
267 const uint8_t* data() const;
276 uint8_t* data();
277
278#if AVCPP_CXX_STANDARD >= 20
287 bool setData(std::span<const uint8_t> newData, OptionalErrorCode ec = throws());
293 std::span<const uint8_t> span() const;
299 std::span<uint8_t> span();
300#endif
301
307 Timestamp pts() const;
313 Timestamp dts() const;
319 Timestamp ts() const;
325 size_t size() const;
326
337 attribute_deprecated void setPts(int64_t pts, const Rational &tsTimeBase = Rational(0, 0));
348 attribute_deprecated void setDts(int64_t dts, const Rational &tsTimeBase = Rational(0, 0));
349
355 void setPts(const Timestamp &pts);
361 void setDts(const Timestamp &dts);
362
368 int streamIndex() const;
374 bool isKeyPacket() const;
380 int duration() const;
386 bool isComplete() const;
392 bool isNull() const;
393
402 void setStreamIndex(int idx);
408 void setKeyPacket(bool keyPacket);
416 void setDuration(int duration, const Rational &durationTimeBase = Rational(0, 0));
422 void setComplete(bool complete);
423
424 // Flags
430 int flags() const;
436 void setFlags(int flags);
442 void addFlags(int flags);
448 void clearFlags(int flags);
449
450#if AVCPP_HAS_AVFORMAT
458 void dump(const Stream & st, bool dumpPayload = false) const;
459#endif // if AVCPP_HAS_AVFORMAT
460
466 const Rational& timeBase() const { return m_timeBase; }
472 void setTimeBase(const Rational &value);
473
474#if AVCPP_HAS_PKT_SIDE_DATA
482 std::span<const uint8_t> sideData(AVPacketSideDataType type) const;
490 std::span<uint8_t> sideData(AVPacketSideDataType type);
491
496 std::size_t sideDataCount() const noexcept;
497
503 PacketSideData sideDataIndex(std::size_t index) noexcept;
504
512 ArrayView<AVPacketSideData, PacketSideData, std::size_t> sideData() noexcept;
518 ArrayView<const AVPacketSideData, PacketSideData, std::size_t> sideData() const noexcept;
519
523 void freeSideData() noexcept;
524
531 void addSideData(AVPacketSideDataType type, std::span<const uint8_t> data, OptionalErrorCode ec = throws());
532
541 void addSideData(AVPacketSideDataType type, std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
542
550 std::span<uint8_t> allocateSideData(AVPacketSideDataType type, std::size_t size, OptionalErrorCode ec = throws());
551#endif
552
558 bool isReferenced() const;
564 int refCount() const;
565
566#if AVCPP_API_AVCODEC_NEW_INIT_PACKET
575 AVPacket* makeRef(OptionalErrorCode ec) const;
576#else
584 AVPacket makeRef(OptionalErrorCode ec = throws()) const;
585#endif
586
594 Packet clone(OptionalErrorCode ec = throws()) const;
595
603 Packet &operator=(const Packet &rhs);
612
613#if AVCPP_API_AVCODEC_NEW_INIT_PACKET
621 Packet &operator=(const AVPacket *rhs);
622#else
630 Packet &operator=(const AVPacket &rhs);
631#endif
632
638 void swap(Packet &other);
639
645 operator bool() const { return isComplete(); }
646
647private:
648 bool m_completeFlag;
649 Rational m_timeBase;
650};
651
652
653
654} // ::av
655
FFmpeg version and API compatibility switches.
Error codes, error categories and the OptionalErrorCode convention.
Common helpers: comparisons, value lookup, non-copyable base, string utilities.
C-array (FFmpeg) wrapper witch can iterate over collection and represents any element via light-weigh...
Definition avutils.h:862
Carries the caller's choice between exceptions and error codes.
Definition averror.h:96
Simple view for the AVPacketSideData elements.
Definition packet.h:37
std::span< const uint8_t > span() const noexcept
Read-only view of the payload.
AVPacketSideDataType type() const noexcept
Identifier of the side data type.
std::string_view name() const noexcept
Name of the side data type, as FFmpeg spells it.
bool empty() const noexcept
Whether the entry carries no payload.
Timestamp ts() const
Timestamp FFmpeg would use for display, falling back to the decoding one.
const Rational & timeBase() const
Time base of pts() and dts().
Definition packet.h:466
Timestamp pts() const
Presentation timestamp.
std::span< const uint8_t > span() const
Read-only view of the payload.
Packet(uint8_t *data, size_t size, wrap_data_static, OptionalErrorCode ec=throws())
Wrap static data without taking ownership.
const uint8_t * data() const
Read access to the payload.
attribute_deprecated void setDts(int64_t dts, const Rational &tsTimeBase=Rational(0, 0))
Set the decoding timestamp.
Packet & operator=(const AVPacket *rhs)
Assign from an FFmpeg packet.
attribute_deprecated void setPts(int64_t pts, const Rational &tsTimeBase=Rational(0, 0))
Set the presentation timestamp.
int flags() const
Raw AV_PKT_FLAG_* mask.
void setKeyPacket(bool keyPacket)
Mark the packet as a key frame.
void setDts(const Timestamp &dts)
Set the decoding timestamp.
bool isComplete() const
Whether the packet is marked complete.
Timestamp dts() const
Decoding timestamp.
Packet & operator=(Packet &&rhs)
Move assignment.
void setComplete(bool complete)
Mark the packet as complete.
PacketSideData sideDataIndex(std::size_t index) noexcept
Get side data element by index.
Packet & operator=(const Packet &rhs)
Assign by referencing the other packet's payload.
void dump(const Stream &st, bool dumpPayload=false) const
Print the packet through the FFmpeg log system.
void setPts(const Timestamp &pts)
Set the presentation timestamp.
bool isNull() const
Whether the packet has no payload.
void freeSideData() noexcept
Remove and free all side data entries.
size_t size() const
Payload size.
int streamIndex() const
Index of the stream the packet belongs to.
std::span< uint8_t > sideData(AVPacketSideDataType type)
Mutable access to the side data of the given type.
std::span< const uint8_t > sideData(AVPacketSideDataType type) const
Read-only access to the side data of the given type.
Packet clone(OptionalErrorCode ec=throws()) const
Deep copy of the packet, payload included.
std::span< uint8_t > span()
Mutable view of the payload.
Packet(const uint8_t *data, size_t size, bool doAllign=true)
Copy a memory block into the packet payload.
Packet(const Packet &packet)
Copy a packet as a reference to the same data.
uint8_t * data()
Write access to the payload.
int refCount() const
Number of references to the payload.
AVPacket * makeRef(OptionalErrorCode ec) const
Create an independent reference to the packet.
Packet(Packet &&packet)
Move a packet.
Packet(std::span< uint8_t > data, wrap_data_static, OptionalErrorCode ec=throws())
Wrap static data from a span without taking ownership.
std::span< uint8_t > allocateSideData(AVPacketSideDataType type, std::size_t size, OptionalErrorCode ec=throws())
Allocate storage for the packet side data of the given type and return reference to it.
void addSideData(AVPacketSideDataType type, std::span< const uint8_t > data, OptionalErrorCode ec=throws())
Add side data of the given type into packet.
void setDuration(int duration, const Rational &durationTimeBase=Rational(0, 0))
Set the packet duration.
Packet(uint8_t *data, size_t size, wrap_data, OptionalErrorCode ec=throws())
Take ownership of a buffer allocated with the FFmpeg allocator.
void clearFlags(int flags)
Remove flags.
Packet()
Create an empty, valid packet with an allocated FFmpeg structure.
void addFlags(int flags)
Add flags.
bool isKeyPacket() const
Whether the packet is a key frame.
void setTimeBase(const Rational &value)
Set the time base used by pts() and dts().
std::size_t sideDataCount() const noexcept
Return count of the side data elements.
void setStreamIndex(int idx)
Set the stream index.
bool setData(const std::vector< uint8_t > &newData, OptionalErrorCode ec=throws())
Replace the payload with a copy of a byte vector.
int duration() const
Duration of the packet, in time base units.
bool isReferenced() const
Whether the payload is reference counted.
bool setData(std::span< const uint8_t > newData, OptionalErrorCode ec=throws())
Replace the payload with a copy of a span.
Packet(std::nullptr_t)
Create an empty packet without allocating.
Packet(const Packet &packet, OptionalErrorCode ec)
Copy a packet as a reference to the same data.
Packet(std::span< uint8_t > data, wrap_data, OptionalErrorCode ec=throws())
Take ownership of a span allocated with the FFmpeg allocator.
Packet(std::span< const uint8_t > data, bool doAllign=true)
Copy a span into the packet payload.
bool setData(const uint8_t *newData, size_t size, OptionalErrorCode ec=throws())
Replace the payload with a copy of a memory block.
void swap(Packet &other)
Exchange the contents of two packets.
void setFlags(int flags)
Replace the AV_PKT_FLAG_* mask.
Packet(const std::vector< uint8_t > &data)
Copy a byte vector into the packet payload.
Packet(const AVPacket *packet, OptionalErrorCode ec=throws())
Wrap an FFmpeg packet.
Rational number: a numerator and a denominator.
Definition rational.h:49
One stream inside a container.
Definition stream.h:34
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.
Streams inside a container: parameters, time base and disposition.
Base for wrappers that hold a small FFmpeg structure by value.
Definition ffmpeg.h:189
Wrap static data, do not owning and free.
Definition packet.h:124
Wrap data and take owning.
Definition packet.h:119
Frame timestamps and their arithmetic.