Files
matrix_thing/include/libav/fmt/context.hpp
2019-03-10 10:21:35 -07:00

114 lines
2.4 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(void){
reset();
}
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){
reset();
}
}
}
~output_context(void){
reset();
}
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