avcpp  2.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
packet.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "avconfig.h"
4 #include "avcompat.h"
5 
6 #include <vector>
7 #include <type_traits>
8 
9 #if AVCPP_CXX_STANDARD >= 20
10 #include <span>
11 #endif
12 
13 #include "ffmpeg.h"
14 #include "avutils.h"
15 #include "rational.h"
16 #include "stream.h"
17 #include "averror.h"
18 #include "timestamp.h"
19 
20 extern "C" {
21 #include <libavutil/attributes.h>
22 }
23 
24 namespace av {
25 
26 #if AVCPP_HAS_PKT_SIDE_DATA
30 class PacketSideData : public FFWrapperRef<AVPacketSideData>
31 {
32 public:
34 
35  std::string_view name() const noexcept;
36  AVPacketSideDataType type() const noexcept;
37  std::span<const uint8_t> data() const noexcept;
38  std::span<uint8_t> data() noexcept;
39 
40  static std::string_view name(AVPacketSideDataType type);
41 
42  bool empty() const noexcept;
43  operator bool() const noexcept;
44 };
45 #endif // AVCPP_HAS_PKT_SIDE_DATA
46 
47 class Packet :
48 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
49  public FFWrapperPtr<AVPacket>
50 #else
51  public FFWrapperRef<AVPacket>
52 #endif
53 {
54 private:
55  // if deepCopy true - make deep copy, instead - reference is created
56  void initFromAVPacket(const AVPacket *avpacket, bool deepCopy, OptionalErrorCode ec);
57 
58 public:
62  struct wrap_data {};
67  struct wrap_data_static {};
68 
69  Packet();
70  Packet(const Packet &packet, OptionalErrorCode ec);
71  Packet(const Packet &packet);
72  Packet(Packet &&packet);
73  explicit Packet(const AVPacket *packet, OptionalErrorCode ec = throws());
74  explicit Packet(const std::vector<uint8_t> &data);
75 
76  Packet(const uint8_t *data, size_t size, bool doAllign = true);
77  // data must be allocated with av_malloc() family
78  Packet(uint8_t *data, size_t size, wrap_data, OptionalErrorCode ec = throws());
79  Packet(uint8_t *data, size_t size, wrap_data_static, OptionalErrorCode ec = throws());
80 
81 #if AVCPP_CXX_STANDARD >= 20
82  explicit Packet(std::span<const uint8_t> data, bool doAllign = true);
83  // data must be allocated with av_malloc() family
84  Packet(std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
85  Packet(std::span<uint8_t> data, wrap_data_static, OptionalErrorCode ec = throws());
86 #endif
87 
88  ~Packet();
89 
90  bool setData(const std::vector<uint8_t> &newData, OptionalErrorCode ec = throws());
91  bool setData(const uint8_t *newData, size_t size, OptionalErrorCode ec = throws());
92 
93  const uint8_t* data() const;
94  uint8_t* data();
95 
96 #if AVCPP_CXX_STANDARD >= 20
97  bool setData(std::span<const uint8_t> newData, OptionalErrorCode ec = throws());
98  std::span<const uint8_t> span() const;
99  std::span<uint8_t> span();
100 #endif
101 
102  Timestamp pts() const;
103  Timestamp dts() const;
104  Timestamp ts() const;
105  size_t size() const;
106 
114  attribute_deprecated void setPts(int64_t pts, const Rational &tsTimeBase = Rational(0, 0));
115  attribute_deprecated void setDts(int64_t dts, const Rational &tsTimeBase = Rational(0, 0));
116 
117  void setPts(const Timestamp &pts);
118  void setDts(const Timestamp &dts);
119 
120  int streamIndex() const;
121  bool isKeyPacket() const;
122  int duration() const;
123  bool isComplete() const;
124  bool isNull() const;
125 
126  void setStreamIndex(int idx);
127  void setKeyPacket(bool keyPacket);
128  void setDuration(int duration, const Rational &durationTimeBase = Rational(0, 0));
129  void setComplete(bool complete);
130 
131  // Flags
132  int flags() const;
133  void setFlags(int flags);
134  void addFlags(int flags);
135  void clearFlags(int flags);
136 
137 #if AVCPP_HAS_AVFORMAT
138  void dump(const Stream & st, bool dumpPayload = false) const;
139 #endif // if AVCPP_HAS_AVFORMAT
140 
141  const Rational& timeBase() const { return m_timeBase; }
142  void setTimeBase(const Rational &value);
143 
144 #if AVCPP_HAS_PKT_SIDE_DATA
151  std::span<const uint8_t> sideData(AVPacketSideDataType type) const;
152  std::span<uint8_t> sideData(AVPacketSideDataType type);
153 
158  std::size_t sideDataElements() const noexcept;
159 
165  PacketSideData sideData(std::size_t index) noexcept;
166 
174  ArrayView<AVPacketSideData, PacketSideData, std::size_t> sideData() noexcept;
175  ArrayView<const AVPacketSideData, PacketSideData, std::size_t> sideData() const noexcept;
176 
183  void addSideData(AVPacketSideDataType type, std::span<const uint8_t> data, OptionalErrorCode ec = throws());
184 
193  void addSideData(AVPacketSideDataType type, std::span<uint8_t> data, wrap_data, OptionalErrorCode ec = throws());
194 
202  std::span<uint8_t> allocateSideData(AVPacketSideDataType type, std::size_t size, OptionalErrorCode ec = throws());
203 #endif
204 
205  bool isReferenced() const;
206  int refCount() const;
207 
208 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
209  AVPacket* makeRef(OptionalErrorCode ec) const;
210 #else
211  AVPacket makeRef(OptionalErrorCode ec = throws()) const;
212 #endif
213 
214  Packet clone(OptionalErrorCode ec = throws()) const;
215 
216  Packet &operator=(const Packet &rhs);
217  Packet &operator=(Packet &&rhs);
218 
219 #if AVCPP_API_AVCODEC_NEW_INIT_PACKET
220  Packet &operator=(const AVPacket *rhs);
221 #else
222  Packet &operator=(const AVPacket &rhs);
223 #endif
224 
225  void swap(Packet &other);
226 
227  operator bool() const { return isComplete(); }
228 
229 private:
230  bool m_completeFlag;
231  Rational m_timeBase;
232 };
233 
234 
235 
236 } // ::av
237 
Definition: averror.h:64
Definition: packet.h:53
const Rational & timeBase() const
Definition: packet.h:141
bool setData(const std::vector< uint8_t > &newData, OptionalErrorCode ec=throws())
Packet(const std::vector< uint8_t > &data)
Definition: rational.h:26
The Timestamp class represents timestamp value and it timebase.
Definition: timestamp.h:14
Definition: audioresampler.cpp:8
OptionalErrorCode throws()
Helper to construct null OptionalErrorCode object.
Definition: averror.h:179
Definition: averror.h:228
Definition: ffmpeg.h:22
Definition: ffmpeg.h:62
Wrap static data, do not owning and free.
Definition: packet.h:67
Wrap data and take owning.
Definition: packet.h:62