FFmpegfs Fuse Multi Media Filesystem 2.19
Loading...
Searching...
No Matches
ffmpeg_formatcontext.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 <libavformat/avformat.h>
24#include <libavformat/avio.h>
25#include <libavutil/mem.h>
26#pragma GCC diagnostic pop
27#ifdef __cplusplus
28}
29#endif
30
32
34 m_context(nullptr),
35 m_type(type),
36 m_custom_io(false)
37{
38}
39
44
46 m_context(context.m_context),
47 m_type(context.m_type),
48 m_custom_io(context.m_custom_io)
49{
50 context.m_context = nullptr;
51 context.m_custom_io = false;
52}
53
54FFmpeg_FormatContext& FFmpeg_FormatContext::operator=(FFmpeg_FormatContext&& context) noexcept
55{
56 if (this != &context)
57 {
58 reset();
59
60 m_context = context.m_context;
61 m_type = context.m_type;
62 m_custom_io = context.m_custom_io;
63
64 context.m_context = nullptr;
65 context.m_custom_io = false;
66 }
67
68 return *this;
69}
70
72{
73 reset();
74
76 m_context = avformat_alloc_context();
77
78 return (m_context != nullptr) ? 0 : AVERROR(ENOMEM);
79}
80
81int FFmpeg_FormatContext::alloc_output_context(const char *format_name, const char *filename)
82{
83 reset();
84
86
87 int ret = avformat_alloc_output_context2(&m_context, nullptr, format_name, filename);
88 if (ret < 0)
89 {
90 return ret;
91 }
92
93 return (m_context != nullptr) ? 0 : AVERROR(ENOMEM);
94}
95
97{
98 m_type = type;
99}
100
102{
103 m_custom_io = custom_io;
104}
105
107{
108 bool closed = (m_context != nullptr);
109
110 if (m_context != nullptr)
111 {
113
114 if (m_type == TYPE::OUTPUT)
115 {
116 avformat_free_context(m_context);
117 m_context = nullptr;
118 }
119 else
120 {
121 avformat_close_input(&m_context);
122 }
123 }
124
125 m_custom_io = false;
126
127 return closed;
128}
129
131{
132 return (m_context == nullptr);
133}
134
136{
137 return m_context;
138}
139
140const AVFormatContext* FFmpeg_FormatContext::get() const
141{
142 return m_context;
143}
144
146{
147 return &m_context;
148}
149
151{
152 AVFormatContext *context = m_context;
153
154 m_context = nullptr;
155 m_custom_io = false;
156
157 return context;
158}
159
160FFmpeg_FormatContext::operator AVFormatContext*()
161{
162 return m_context;
163}
164
165FFmpeg_FormatContext::operator const AVFormatContext*() const
166{
167 return m_context;
168}
169
171{
172 return m_context;
173}
174
175const AVFormatContext* FFmpeg_FormatContext::operator->() const
176{
177 return m_context;
178}
179
181{
182 if (m_custom_io && m_context != nullptr && m_context->pb != nullptr)
183 {
184#if (LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 80, 0))
185 av_freep(&m_context->pb->buffer);
186 avio_context_free(&m_context->pb);
187#else
188 av_freep(m_context->pb);
189 m_context->pb = nullptr;
190#endif
191 }
192}
RAII wrapper for AVFormatContext.
~FFmpeg_FormatContext()
Release the owned format context, if any.
void set_type(TYPE type)
Set the context direction used for later cleanup.
AVFormatContext * m_context
Pointer to underlying AVFormatContext.
bool m_custom_io
true if pb was allocated with avio_alloc_context().
TYPE m_type
Input or output context.
FFmpeg_FormatContext(TYPE type=TYPE::INPUT)
Construct an empty format-context wrapper.
TYPE
Managed AVFormatContext direction.
@ OUTPUT
Output context freed with avformat_free_context().
@ INPUT
Input context closed with avformat_close_input().
int alloc_input_context()
Allocate a new input format context.
bool reset()
Free the owned format context and reset the wrapper to empty.
AVFormatContext * get()
Get the owned FFmpeg format context pointer.
AVFormatContext ** address()
Get a writable pointer-to-pointer for FFmpeg APIs.
AVFormatContext * operator->()
Access members of the underlying mutable format context.
AVFormatContext * release()
Release ownership without freeing the format context.
void set_custom_io(bool custom_io=true)
Mark whether the context owns a custom AVIOContext.
void free_custom_io()
Free the custom AVIOContext attached to the format context.
int alloc_output_context(const char *format_name, const char *filename=nullptr)
Allocate a new output format context.
bool empty() const
Check whether the wrapper currently owns a format context.
FFmpeg AVFormatContext RAII wrapper.