~gabriel1984sibiu/simplescreenrecorder/simplescreenrecorder

« back to all changes in this revision

Viewing changes to src/AV/AVWrapper.h

  • Committer: Grevutiu Gabriel
  • Date: 2014-12-24 12:30:41 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20141224123041-xndgql40iww2bh2m
original source code from upstreamer

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2012-2014 Maarten Baert <maarten-baert@hotmail.com>
 
3
 
 
4
This file is part of SimpleScreenRecorder.
 
5
 
 
6
SimpleScreenRecorder is free software: you can redistribute it and/or modify
 
7
it under the terms of the GNU General Public License as published by
 
8
the Free Software Foundation, either version 3 of the License, or
 
9
(at your option) any later version.
 
10
 
 
11
SimpleScreenRecorder is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with SimpleScreenRecorder.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#pragma once
 
21
#include "Global.h"
 
22
 
 
23
#if !SSR_USE_AV_CODEC_ID
 
24
#define AV_CODEC_ID_NONE CODEC_ID_NONE
 
25
#endif
 
26
 
 
27
#if !SSR_USE_AV_PIX_FMT
 
28
#define AV_PIX_FMT_NONE PIX_FMT_NONE
 
29
#define AV_PIX_FMT_PAL8 PIX_FMT_PAL8
 
30
#define AV_PIX_FMT_RGB565 PIX_FMT_RGB565
 
31
#define AV_PIX_FMT_RGB555 PIX_FMT_RGB555
 
32
#define AV_PIX_FMT_BGR24 PIX_FMT_BGR24
 
33
#define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
 
34
#define AV_PIX_FMT_BGRA PIX_FMT_BGRA
 
35
#define AV_PIX_FMT_RGBA PIX_FMT_RGBA
 
36
#define AV_PIX_FMT_ABGR PIX_FMT_ABGR
 
37
#define AV_PIX_FMT_ARGB PIX_FMT_ARGB
 
38
#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
 
39
#define AV_PIX_FMT_YUV422P PIX_FMT_YUV422P
 
40
#define AV_PIX_FMT_YUV444P PIX_FMT_YUV444P
 
41
#endif
 
42
 
 
43
// A trivial class that holds (aligned) frame data. This makes it easy to implement reference counting through std::shared_ptr.
 
44
class AVFrameData {
 
45
private:
 
46
        uint8_t *m_data;
 
47
public:
 
48
        inline AVFrameData(size_t size) {
 
49
                m_data = (uint8_t*) av_malloc(size);
 
50
                if(m_data == NULL)
 
51
                        throw std::bad_alloc();
 
52
        }
 
53
        inline ~AVFrameData() {
 
54
                av_free(m_data);
 
55
        }
 
56
        inline uint8_t* GetData() {
 
57
                return m_data;
 
58
        }
 
59
};
 
60
 
 
61
// A wrapper around AVFrame to manage memory allocation and reference counting.
 
62
// Note: This reference counting mechanism is unrelated to the mechanism added in later versions of ffmpeg/libav.
 
63
class AVFrameWrapper {
 
64
 
 
65
private:
 
66
        AVFrame *m_frame;
 
67
        std::shared_ptr<AVFrameData> m_refcounted_data;
 
68
 
 
69
public:
 
70
        AVFrameWrapper(const std::shared_ptr<AVFrameData>& refcounted_data);
 
71
        ~AVFrameWrapper();
 
72
 
 
73
        AVFrameWrapper(const AVFrameWrapper&) = delete;
 
74
        AVFrameWrapper& operator=(const AVFrameWrapper&) = delete;
 
75
 
 
76
public:
 
77
        inline AVFrame* GetFrame() { return m_frame; }
 
78
        inline uint8_t* GetRawData() { return m_refcounted_data->GetData(); }
 
79
        inline std::shared_ptr<AVFrameData> GetFrameData() { return m_refcounted_data; }
 
80
 
 
81
};
 
82
 
 
83
// A wrapper around AVPacket to manage memory allocation. There is no copying or reference counting in this case.
 
84
class AVPacketWrapper {
 
85
 
 
86
private:
 
87
        AVPacket m_packet;
 
88
        bool m_free_on_destruct;
 
89
 
 
90
public:
 
91
        AVPacketWrapper();
 
92
        AVPacketWrapper(size_t size);
 
93
        ~AVPacketWrapper();
 
94
 
 
95
        AVPacketWrapper(const AVPacketWrapper&) = delete;
 
96
        AVPacketWrapper& operator=(const AVPacketWrapper&) = delete;
 
97
 
 
98
public:
 
99
        inline AVPacket* GetPacket() { return &m_packet; }
 
100
        inline void SetFreeOnDestruct(bool free_on_destruct) { m_free_on_destruct = free_on_destruct; }
 
101
 
 
102
};
 
103
 
 
104
bool AVFormatIsInstalled(const QString& format_name);
 
105
bool AVCodecIsInstalled(const QString& codec_name);
 
106
bool AVCodecSupportsPixelFormat(const AVCodec* codec, PixelFormat pixel_fmt);
 
107
bool AVCodecSupportsSampleFormat(const AVCodec* codec, AVSampleFormat sample_fmt);
 
108
 
 
109
#if !SSR_USE_AV_CODEC_IS_ENCODER
 
110
inline int av_codec_is_encoder(const AVCodec* codec) {
 
111
        return (codec != NULL && (codec->encode != NULL || codec->encode2 != NULL));
 
112
}
 
113
#endif