video_scaler.cpp 5.43 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2013-2019 Savoir-faire Linux Inc.
Guillaume Roguez's avatar
Guillaume Roguez committed
3
 *
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *  Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
 *
 *  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
Guillaume Roguez's avatar
Guillaume Roguez committed
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
19 20
 */

21
#include "libav_deps.h" // MUST BE INCLUDED FIRST
22
#include "libav_utils.h"
23
#include "video_scaler.h"
24
#include "media_buffer.h"
Tristan Matthews's avatar
Tristan Matthews committed
25
#include "logger.h"
26

27 28
#include <cassert>

Adrien Béraud's avatar
Adrien Béraud committed
29
namespace jami { namespace video {
30

31 32 33
VideoScaler::VideoScaler()
    : ctx_(0), mode_(SWS_FAST_BILINEAR), tmp_data_()
{}
34

35 36 37 38
VideoScaler::~VideoScaler()
{
    sws_freeContext(ctx_);
}
39

40 41
void
VideoScaler::scale(const VideoFrame& input, VideoFrame& output)
42
{
43 44
    const auto input_frame = input.pointer();
    auto output_frame = output.pointer();
45 46 47 48

    ctx_ = sws_getCachedContext(ctx_,
                                input_frame->width,
                                input_frame->height,
49
                                (AVPixelFormat) input_frame->format,
50 51
                                output_frame->width,
                                output_frame->height,
52
                                (AVPixelFormat) output_frame->format,
53 54 55
                                mode_,
                                NULL, NULL, NULL);
    if (!ctx_) {
Adrien Béraud's avatar
Adrien Béraud committed
56
        JAMI_ERR("Unable to create a scaler context");
57 58 59 60
        return;
    }

    sws_scale(ctx_, input_frame->data, input_frame->linesize, 0,
61 62 63 64
              input_frame->height, output_frame->data,
              output_frame->linesize);
}

65 66
void
VideoScaler::scale_with_aspect(const VideoFrame& input, VideoFrame& output)
67
{
68 69 70 71 72 73 74 75 76 77 78 79
    if (input.width() == output.width() && input.height() == output.height()) {
        if (input.format() != output.format()) {
            auto outPtr = convertFormat(input, (AVPixelFormat)output.format());
            output.copyFrom(*outPtr);
        } else {
            output.copyFrom(input);
        }
    } else {
        auto output_frame = output.pointer();
        scale_and_pad(input, output, 0, 0, output_frame->width,
                      output_frame->height, true);
    }
80 81
}

82 83 84 85 86
void
VideoScaler::scale_and_pad(const VideoFrame& input, VideoFrame& output,
                           unsigned xoff, unsigned yoff,
                           unsigned dest_width, unsigned dest_height,
                           bool keep_aspect)
87
{
88 89
    const auto input_frame = input.pointer();
    auto output_frame = output.pointer();
90

91 92 93 94 95 96 97 98 99 100 101
    /* Correct destination width/height and offset if we need to keep input
     * frame aspect.
     */
    if (keep_aspect) {
        const float local_ratio = (float)dest_width / dest_height;
        const float input_ratio = (float)input_frame->width / input_frame->height;

        if (local_ratio > input_ratio) {
            auto old_dest_width = dest_width;
            dest_width = dest_height * input_ratio;
            xoff += (old_dest_width - dest_width) / 2;
102
        } else {
103 104 105
            auto old_dest_heigth = dest_height;
            dest_height = dest_width / input_ratio;
            yoff += (old_dest_heigth - dest_height) / 2;
106
        }
107 108
    }

109 110 111 112
    // buffer overflow checks
    assert(xoff + dest_width <= (unsigned)output_frame->width);
    assert(yoff + dest_height <= (unsigned)output_frame->height);

113 114 115 116 117 118 119 120 121 122
    ctx_ = sws_getCachedContext(ctx_,
                                input_frame->width,
                                input_frame->height,
                                (AVPixelFormat) input_frame->format,
                                dest_width,
                                dest_height,
                                (AVPixelFormat) output_frame->format,
                                mode_,
                                NULL, NULL, NULL);
    if (!ctx_) {
Adrien Béraud's avatar
Adrien Béraud committed
123
        JAMI_ERR("Unable to create a scaler context");
124 125 126
        return;
    }

127
    // Make an offset'ed copy of output data from xoff and yoff
128
    const auto out_desc = av_pix_fmt_desc_get((AVPixelFormat)output_frame->format);
129 130
    memset(tmp_data_, 0, sizeof(tmp_data_));
    for (int i = 0; i < 4 && output_frame->linesize[i]; i++) {
131
        signed x_shift=xoff, y_shift=yoff;
132 133 134 135
        if (i == 1 || i == 2) {
            x_shift = -((-x_shift) >> out_desc->log2_chroma_w);
            y_shift = -((-y_shift) >> out_desc->log2_chroma_h);
        }
136
        auto x_step = out_desc->comp[i].step;
137
        tmp_data_[i] = output_frame->data[i] + y_shift * output_frame->linesize[i] + x_shift * x_step;
138 139
    }

140
    sws_scale(ctx_, input_frame->data, input_frame->linesize, 0,
141
              input_frame->height, tmp_data_, output_frame->linesize);
142 143
}

144 145 146 147 148 149 150 151 152
std::unique_ptr<VideoFrame>
VideoScaler::convertFormat(const VideoFrame& input, AVPixelFormat pix)
{
    auto output = std::make_unique<VideoFrame>();
    output->reserve(pix, input.width(), input.height());
    scale(input, *output);
    return output;
}

153 154
void VideoScaler::reset()
{
Guillaume Roguez's avatar
Guillaume Roguez committed
155 156 157 158
    if (ctx_) {
        sws_freeContext(ctx_);
        ctx_ = nullptr;
    }
159 160
}

Adrien Béraud's avatar
Adrien Béraud committed
161
}} // namespace jami::video