133 lines
3.0 KiB
C++

#ifndef LIBAV_FMT_CONTEXT_HPP
#define LIBAV_FMT_CONTEXT_HPP
extern "C"{
# include <libavformat/avformat.h>
}
#include "libav/libav.hpp"
#include <utility> //exchange
namespace libav::fmt{
class input_context{
private:
AVFormatContext* m_context = nullptr;
public:
input_context(void) = default;
input_context(const char* filename){
if(avformat_open_input(&m_context, filename, 0, 0) < 0){
return;
}
if(avformat_find_stream_info(m_context, 0) < 0){
reset();
}
}
input_context(const input_context&) = delete;
input_context(input_context&& i)noexcept:
m_context(std::exchange(i.m_context, nullptr)){}
~input_context(void){
reset();
}
input_context& operator=(const input_context&) = delete;
input_context& operator=(input_context&& i){
std::swap(m_context, i.m_context);
return *this;
}
void reset(AVFormatContext* context = nullptr){
if(m_context)
avformat_close_input(&m_context);
m_context = context;
}
AVFormatContext* release(void){
return std::exchange(m_context, nullptr);
}
AVFormatContext* operator->(void){
return m_context;
}
const AVFormatContext* operator->(void)const{
return m_context;
}
AVFormatContext* get(void){
return m_context;
}
const AVFormatContext* get(void)const{
return m_context;
}
operator AVFormatContext*(void){
return m_context;
}
operator const AVFormatContext*(void)const{
return m_context;
}
operator bool(void)const{
return m_context;
}
};
class output_context{
private:
AVFormatContext* m_context = nullptr;
public:
output_context(const char* filename, const char* format = nullptr){
if(avformat_alloc_output_context2(&m_context, av_guess_format(format, NULL, NULL), NULL, filename) < 0)
return;
if(!(m_context->oformat->flags & AVFMT_NOFILE)){
if(avio_open(&m_context->pb, filename, AVIO_FLAG_WRITE) < 0){
avformat_free_context(m_context);
}
}
}
output_context(const output_context&) = delete;
output_context(output_context&& o)noexcept:
m_context(std::exchange(o.m_context, nullptr)){}
~output_context(void){
reset();
}
output_context& operator=(const output_context&) = delete;
output_context& operator=(output_context&& o)noexcept{
std::swap(m_context, o.m_context);
return *this;
}
void reset(AVFormatContext* context = nullptr){
if(m_context){
if(!(m_context->oformat->flags & AVFMT_NOFILE)){
avio_close(m_context->pb);
}
avformat_free_context(m_context);
}
m_context = context;
}
AVFormatContext* release(void){
return std::exchange(m_context, nullptr);
}
AVFormatContext* operator->(void){
return m_context;
}
const AVFormatContext* operator->(void)const{
return m_context;
}
AVFormatContext* get(void){
return m_context;
}
const AVFormatContext* get(void)const{
return m_context;
}
operator AVFormatContext*(void){
return m_context;
}
operator const AVFormatContext*(void)const{
return m_context;
}
operator bool(void)const{
return m_context;
}
};
}
#endif