FFmpegfs Fuse Multi Media Filesystem 2.19
Loading...
Searching...
No Matches
ffmpeg_packet.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2026 Norbert Schlia (nschlia@oblivion-software.de)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 */
9
17#ifdef __cplusplus
18extern "C" {
19#endif
20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wconversion"
22#pragma GCC diagnostic ignored "-Wsign-conversion"
23#include <libavcodec/packet.h>
24#pragma GCC diagnostic pop
25#ifdef __cplusplus
26}
27#endif
28
29#include "ffmpeg_packet.h"
30
32 m_packet(av_packet_alloc()),
33 m_res(0)
34{
35 m_res = (m_packet != nullptr) ? 0 : AVERROR(ENOMEM);
36
37 if (m_packet != nullptr)
38 {
39 m_packet->stream_index = stream_index;
40 }
41}
42
43FFmpeg_Packet::FFmpeg_Packet(const AVPacket *packet) :
44 m_packet(nullptr),
45 m_res(0)
46{
47 if (packet != nullptr)
48 {
49 m_packet = av_packet_clone(packet);
50 m_res = (m_packet != nullptr) ? 0 : AVERROR(ENOMEM);
51 }
52 else
53 {
54 m_res = AVERROR(EINVAL);
55 }
56}
57
59 m_packet(nullptr),
60 m_res(0)
61{
62 if (packet.m_packet != nullptr)
63 {
64 m_packet = av_packet_clone(packet.m_packet);
65 m_res = (m_packet != nullptr) ? 0 : AVERROR(ENOMEM);
66 }
67 else
68 {
69 m_res = AVERROR(EINVAL);
70 }
71}
72
74 m_packet(packet.m_packet),
75 m_res(packet.m_res)
76{
77 packet.m_packet = nullptr;
78 packet.m_res = AVERROR(EINVAL);
79}
80
85
87{
88 if (this != &packet && m_packet != packet.m_packet)
89 {
90 AVPacket *new_packet = nullptr;
91 int new_res = AVERROR(EINVAL);
92
93 if (packet.m_packet != nullptr)
94 {
95 new_packet = av_packet_clone(packet.m_packet);
96 new_res = (new_packet != nullptr) ? 0 : AVERROR(ENOMEM);
97 }
98
99 free();
100
101 m_packet = new_packet;
102 m_res = new_res;
103 }
104
105 return *this;
106}
107
109{
110 if (this != &packet)
111 {
112 free();
113
114 m_packet = packet.m_packet;
115 m_res = packet.m_res;
116
117 packet.m_packet = nullptr;
118 packet.m_res = AVERROR(EINVAL);
119 }
120
121 return *this;
122}
123
124FFmpeg_Packet& FFmpeg_Packet::operator=(const AVPacket *packet) noexcept
125{
126 if (m_packet != packet)
127 {
128 AVPacket *new_packet = nullptr;
129 int new_res = AVERROR(EINVAL);
130
131 if (packet != nullptr)
132 {
133 new_packet = av_packet_clone(packet);
134 new_res = (new_packet != nullptr) ? 0 : AVERROR(ENOMEM);
135 }
136
137 free();
138
139 m_packet = new_packet;
140 m_res = new_res;
141 }
142
143 return *this;
144}
145
147{
148 return m_res;
149}
150
151AVPacket* FFmpeg_Packet::clone() const
152{
153 return av_packet_clone(m_packet);
154}
155
157{
158 if (m_packet != nullptr)
159 {
160 av_packet_unref(m_packet);
161 }
162}
163
165{
166 if (m_packet != nullptr)
167 {
168 av_packet_free(&m_packet);
169 m_packet = nullptr;
170 }
171}
172
174{
175 return m_packet;
176}
177
178const AVPacket* FFmpeg_Packet::get() const
179{
180 return m_packet;
181}
182
183FFmpeg_Packet::operator AVPacket*()
184{
185 return m_packet;
186}
187
188FFmpeg_Packet::operator const AVPacket*() const
189{
190 return m_packet;
191}
192
194{
195 return m_packet;
196}
197
198const AVPacket* FFmpeg_Packet::operator->() const
199{
200 return m_packet;
201}
RAII wrapper for AVPacket.
AVPacket * clone() const
Clone packet to a new AVPacket*. Caller owns the returned packet.
void unref()
Unreference packet payload, keep the AVPacket object allocated.
AVPacket * m_packet
Pointer to underlying AVPacket struct.
void free()
Free the underlying packet.
virtual ~FFmpeg_Packet()
Free the underlying packet.
AVPacket * operator->()
operator ->: behave like a packet pointer.
FFmpeg_Packet(int stream_index=INVALID_STREAM)
Allocate an empty packet.
int res() const
Get result of last operation.
int m_res
0 if last operation was successful, or negative AVERROR value
AVPacket * get()
Access the underlying packet.
FFmpeg_Packet & operator=(const FFmpeg_Packet &packet) noexcept
Deep-copy assign from another wrapper.
FFmpeg AVPacket RAII wrapper.