libav_utils.cpp 3.9 KB
Newer Older
1
/*
Guillaume Roguez's avatar
Guillaume Roguez committed
2 3
 *  Copyright (C) 2004-2015 Savoir-faire Linux Inc.
 *
4
 *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
5
 *  Author: Luca Barbato <lu_zero@gentoo.org>
6
 *  Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  This program 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.
 *
 *  This program 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 this program; if not, write to the Free Software
20
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
21 22
 */

23 24
#include "libav_deps.h" // MUST BE INCLUDED FIRST

Alexandre Lision's avatar
Alexandre Lision committed
25
#include "config.h"
26
#include "video/video_base.h"
27 28
#include "logger.h"

29
#include <vector>
30 31 32
#include <algorithm>
#include <string>
#include <iostream>
33 34 35
#include <thread>
#include <mutex>
#include <exception>
36

Guillaume Roguez's avatar
Guillaume Roguez committed
37 38
namespace ring { namespace libav_utils {

39
// protect libav/ffmpeg access
40 41
static int
avcodecManageMutex(void **data, enum AVLockOp op)
42
{
43
    auto mutex = reinterpret_cast<std::mutex**>(data);
44 45
    int ret = 0;
    switch (op) {
Tristan Matthews's avatar
Tristan Matthews committed
46
        case AV_LOCK_CREATE:
47 48 49
            try {
                *mutex = new std::mutex;
            } catch (const std::bad_alloc& e) {
50
                return AVERROR(ENOMEM);
51
            }
52
            break;
Tristan Matthews's avatar
Tristan Matthews committed
53
        case AV_LOCK_OBTAIN:
54
            (*mutex)->lock();
Tristan Matthews's avatar
Tristan Matthews committed
55 56
            break;
        case AV_LOCK_RELEASE:
57
            (*mutex)->unlock();
Tristan Matthews's avatar
Tristan Matthews committed
58
            break;
59
        case AV_LOCK_DESTROY:
60 61
            delete *mutex;
            *mutex = nullptr;
62 63
            break;
        default:
64
#ifdef AVERROR_BUG
65
            return AVERROR_BUG;
66 67
#else
            break;
68
#endif
69
    }
70
    return AVERROR(ret);
71 72
}

73
static constexpr const char* AVLOGLEVEL = "AVLOGLEVEL";
74

75 76 77 78
static void
setAvLogLevel()
{
    char* envvar = getenv(AVLOGLEVEL);
79
    signed level = AV_LOG_WARNING;
80 81 82 83 84 85 86 87 88 89 90 91

    if (envvar != nullptr) {
        if (not (std::istringstream(envvar) >> level))
            level = AV_LOG_ERROR;

        level = std::max(AV_LOG_QUIET, std::min(level, AV_LOG_DEBUG));
    }
    av_log_set_level(level);
}

static void
init_once()
92
{
93
    av_register_all();
94
    avdevice_register_all();
95
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53, 13, 0)
96
    avformat_network_init();
97
#endif
98

99
    av_lockmgr_register(avcodecManageMutex);
100

Vittorio Giovara's avatar
Vittorio Giovara committed
101
    if (getDebugMode())
102
        setAvLogLevel();
103 104
}

105
static std::once_flag already_called;
106

107
void ring_avcodec_init()
108
{
109
    std::call_once(already_called, init_once);
110 111
}

112

113 114 115
int libav_pixel_format(int fmt)
{
    switch (fmt) {
116
        case video::VIDEO_PIXFMT_BGRA: return PIXEL_FORMAT(BGRA);
117
        case video::VIDEO_PIXFMT_RGBA: return PIXEL_FORMAT(RGBA);
118
        case video::VIDEO_PIXFMT_YUYV422: return PIXEL_FORMAT(YUYV422);
119 120 121 122
    }
    return fmt;
}

123
int ring_pixel_format(int fmt)
124 125
{
    switch (fmt) {
126
        case PIXEL_FORMAT(YUYV422): return video::VIDEO_PIXFMT_YUYV422;
127 128 129 130
    }
    return fmt;
}

131
void ring_url_split(const char *url,
132 133 134 135 136 137 138
                   char *hostname, size_t hostname_size, int *port,
                   char *path, size_t path_size)
{
    av_url_split(NULL, 0, NULL, 0, hostname, hostname_size, port,
                 path, path_size, url);
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152
bool
is_yuv_planar(const AVPixFmtDescriptor& desc)
{
    if (not (desc.flags & AV_PIX_FMT_FLAG_PLANAR) or desc.flags & AV_PIX_FMT_FLAG_RGB)
        return false;

    /* handle formats that do not use all planes */
    unsigned used_bit_mask = (1u << desc.nb_components) - 1;
    for (unsigned i = 0; i < desc.nb_components; ++i)
        used_bit_mask &= ~(1u << desc.comp[i].plane);

    return not used_bit_mask;
}

Guillaume Roguez's avatar
Guillaume Roguez committed
153
}} // namespace ring::libav_utils