forked from edman007/chiton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chiton_ffmpeg.hpp
135 lines (116 loc) · 4.64 KB
/
chiton_ffmpeg.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef __CHITON_FFMPEG_HPP__
#define __CHITON_FFMPEG_HPP__
/**************************************************************************
*
* This file is part of Chiton.
*
* Chiton is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiton is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiton. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright 2020-2021 Ed Martin <[email protected]>
*
**************************************************************************
*/
#include "chiton_config.hpp"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/timestamp.h>
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/imgutils.h>
};
#include <mutex>
#ifdef HAVE_VDPAU
#include <vdpau/vdpau.h>
#include <libavutil/hwcontext_vdpau.h>
#endif
//fix FFMPEG and c++1x issues
#ifdef av_err2str
#undef av_err2str
av_always_inline char* av_err2str(int errnum){
thread_local static char str[AV_ERROR_MAX_STRING_SIZE];
memset(str, 0, sizeof(str));
return av_make_error_string(str, AV_ERROR_MAX_STRING_SIZE, errnum);
}
#endif
#ifdef av_ts2timestr
#undef av_ts2timestr
av_always_inline char* av_ts2timestr(int64_t ts, AVRational * tb){
thread_local static char str[AV_TS_MAX_STRING_SIZE];
memset(str, 0, sizeof(str));
return av_ts_make_time_string(str, ts, tb);
}
#endif
#ifdef av_ts2str
#undef av_ts2str
av_always_inline char* av_ts2str(int64_t ts){
thread_local static char str[AV_TS_MAX_STRING_SIZE];
memset(str, 0, sizeof(str));
return av_ts_make_string(str, ts);
}
#endif
#ifdef av_fourcc2str
#undef av_fourcc2str
av_always_inline char* av_fourcc2str(uint32_t fourcc){
thread_local static char str[AV_FOURCC_MAX_STRING_SIZE];
memset(str, 0, sizeof(str));
return av_fourcc_make_string(str, fourcc);
}
#endif
//make AVRounding valid in bitwise operations
inline AVRounding operator|(AVRounding a, AVRounding b)
{
return static_cast<AVRounding>(static_cast<int>(a) | static_cast<int>(b));
}
/*
* Utility/Mangement class for libav* and ffmpeg things
*/
class CFFUtil {
public:
CFFUtil(void);
~CFFUtil(void);
void load_ffmpeg(void);//init ffmpeg
void free_hw(void);//free/close HW encoder/decoders
void lock(void);//global ffmpeg lock/unlock
void unlock(void);
//return a ref or null to the context iff it can handle wxh
AVBufferRef *get_vaapi_ctx(AVCodecID codec_id, int codec_profile, int width, int height);
AVBufferRef *get_vdpau_ctx(AVCodecID codec_id, int codec_profile, int width, int height);
bool have_vaapi(AVCodecID codec_id, int codec_profile, int width, int height);//returns true if VAAPI should work
bool have_vdpau(AVCodecID codec_id, int codec_profile, int width, int height);//returns true if VDPAU should work
bool sw_format_is_hw_compatable(const enum AVPixelFormat pix_fmt);//return true if the format is HW compatable
std::string get_sw_hw_format_list(Config &cfg);//return the suggested list of formats for use with later HW functions
private:
void load_vaapi(void);//init global vaapi context
void free_vaapi(void);//free the vaapi context
void load_vdpau(void);//init global vdpau context
void free_vdpau(void);//free the vdpau context
#ifdef HAVE_VDPAU
int get_vdpau_profile(const AVCodecID codec_id, const int codec_profile, VdpDecoderProfile *profile);//get the VDPAU Profile
#endif
AVBufferRef *vaapi_ctx = NULL;
bool vaapi_failed = false;//if we failed to initilize vaapi
AVBufferRef *vdpau_ctx = NULL;
bool vdpau_failed = false;//if we failed to initilize vdpau
std::mutex codec_lock;
};
enum AVPixelFormat get_vaapi_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts);//global VAAPI format selector
enum AVPixelFormat get_vdpau_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts);//global VDPAU format selector
enum AVPixelFormat get_sw_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts);//global SW format selector that prefers VAAPI compatible formats
extern CFFUtil gcff_util;//global FFmpeg lib mangement class
//for passing image coordinates
struct rect { int x, y, w, h; };
#endif