FFmpegfs Fuse Multi Media Filesystem 2.16
ffmpeg_frame.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2024 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 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 * On Debian systems, the complete text of the GNU General Public License
19 * Version 3 can be found in `/usr/share/common-licenses/GPL-3'.
20 */
21
32#ifdef __cplusplus
33extern "C" {
34#endif
35// Disable annoying warnings outside our code
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wconversion"
38#pragma GCC diagnostic ignored "-Wsign-conversion"
39#include <libavcodec/avcodec.h>
40#pragma GCC diagnostic pop
41#ifdef __cplusplus
42}
43#endif
44
45#include "ffmpeg_utils.h"
46#include "ffmpeg_frame.h"
47
48FFmpeg_Frame::FFmpeg_Frame(int stream_index) :
49 m_frame(av_frame_alloc()),
50 m_res(0),
51 m_stream_idx(stream_index)
52{
53 m_res = (m_frame != nullptr) ? 0 : AVERROR(ENOMEM);
54}
55
57 m_frame(nullptr),
58 m_res(0),
59 m_stream_idx(frame.m_stream_idx)
60{
61 if (frame.m_frame != nullptr)
62 {
63 m_frame = av_frame_clone(frame.m_frame);
64
65 m_res = (m_frame != nullptr) ? 0 : AVERROR(ENOMEM);
66 }
67 else
68 {
69 m_res = AVERROR(EINVAL);
70 }
71}
72
73FFmpeg_Frame::FFmpeg_Frame(const AVFrame * frame) :
74 m_frame(nullptr),
75 m_res(0),
76 m_stream_idx(INVALID_STREAM)
77{
78 if (frame != nullptr)
79 {
80 m_frame = av_frame_clone(frame);
81
82 m_res = (m_frame != nullptr) ? 0 : AVERROR(ENOMEM);
83 }
84 else
85 {
86 m_res = AVERROR(EINVAL);
87 }
88}
89
91{
92 free();
93}
94
96{
97 return av_frame_clone(m_frame);
98}
99
101{
102 if (m_frame != nullptr)
103 {
104 av_frame_unref(m_frame);
105 }
106}
107
109{
110 if (m_frame != nullptr)
111 {
112 av_frame_free(&m_frame);
113 m_frame = nullptr;
114 }
115}
116
118{
119 return m_res;
120}
121
123{
124 return m_frame;
125}
126
127FFmpeg_Frame::operator AVFrame*()
128{
129 return m_frame;
130}
131
132FFmpeg_Frame::operator const AVFrame*() const
133{
134 return m_frame;
135}
136
138{
139 return m_frame;
140}
141
143{
144 // Do self assignment check
145 if (this != &frame && m_frame != frame.m_frame)
146 {
147 AVFrame *new_frame = av_frame_clone(frame.m_frame);
148
149 free();
150
151 m_frame = new_frame;
152
153 m_stream_idx = frame.m_stream_idx;
154
155 m_res = (m_frame != nullptr) ? 0 : AVERROR(ENOMEM);
156 }
157
158 return *this;
159}
160
161FFmpeg_Frame& FFmpeg_Frame::operator=(const AVFrame * frame) noexcept
162{
163 if (m_frame != frame)
164 {
165 AVFrame *new_frame = av_frame_clone(frame);
166
167 free();
168
169 m_frame = new_frame;
170
171 m_stream_idx = INVALID_STREAM;
172
173 m_res = (m_frame != nullptr) ? 0 : AVERROR(ENOMEM);
174 }
175
176 return *this;
177}
The FFmpeg_Frame class.
Definition: ffmpeg_frame.h:43
FFmpeg_Frame & operator=(const FFmpeg_Frame &frame) noexcept
Make copy from other FFmpeg_Frame object.
virtual ~FFmpeg_Frame()
Destruct FFmpeg_Frame object.
Definition: ffmpeg_frame.cc:90
AVFrame * operator->()
operator ->: Do as if we were a pointer to AVFrame
void unref()
Unreference underlying frame.
AVFrame * m_frame
Pointer to underlying AVFrame struct.
Definition: ffmpeg_frame.h:117
AVFrame * clone()
Clone frame to a new AVFrame * struct. Needs to be freed by a av_frame_free() call.
Definition: ffmpeg_frame.cc:95
AVFrame * get()
Access the underlying frame.
FFmpeg_Frame(int stream_index=INVALID_STREAM)
Construct FFmpeg_Frame object.
Definition: ffmpeg_frame.cc:48
void free()
Free underlying frame.
int m_res
0 if last operation was successful, or negative AVERROR value
Definition: ffmpeg_frame.h:118
int res() const
Get result of last operation.
FFmpeg AVFrame extension.
Various FFmpegfs utility functions.
#define INVALID_STREAM
Denote an invalid stream.
Definition: ffmpeg_utils.h:77