aboutsummaryrefslogtreecommitdiff
path: root/tests/c2_e2e_test/jni/video_frame.cpp
blob: 6a9b36112a631af8ab01e7529123e17c54bc161e (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// #define LOG_NDEBUG 0
#define LOG_TAG "VideoFrame"

#include <string.h>

#include "video_frame.h"

#include <log/log.h>

namespace android {

namespace {

void CopyWindow(const uint8_t* src, uint8_t* dst, size_t stride, size_t width, size_t height,
                size_t inc) {
    if (inc == 1 && stride == width) {
        // Could copy by plane.
        memcpy(dst, src, width * height);
        return;
    }

    if (inc == 1) {
        // Could copy by row.
        for (size_t row = 0; row < height; ++row) {
            memcpy(dst, src, width);
            dst += width;
            src += stride;
        }
        return;
    }

    // inc != 1, copy by pixel.
    for (size_t row = 0; row < height; ++row) {
        for (size_t col = 0; col < width; ++col) {
            memcpy(dst, src, 1);
            dst++;
            src += inc;
        }
        src += (stride - width) * inc;
    }
}

}  // namespace

// static
std::unique_ptr<VideoFrame> VideoFrame::Create(const uint8_t* data, size_t data_size,
                                               const Size& coded_size, const Size& visible_size,
                                               int32_t color_format) {
    if (coded_size.IsEmpty() || visible_size.IsEmpty() || (visible_size.width > coded_size.width) ||
        (visible_size.height > coded_size.height) || (coded_size.width % 2 != 0) ||
        (coded_size.height % 2 != 0) || (visible_size.width % 2 != 0) ||
        (visible_size.height % 2 != 0)) {
        ALOGE("Size are not valid: coded: %dx%d, visible: %dx%d", coded_size.width,
              coded_size.height, visible_size.width, visible_size.height);
        return nullptr;
    }

    if (color_format != YUV_420_PLANAR && color_format != YUV_420_FLEXIBLE &&
        color_format != HAL_PIXEL_FORMAT_YV12 && color_format != HAL_PIXEL_FORMAT_NV12) {
        ALOGE("color_format is unknown: 0x%x", color_format);
        return nullptr;
    }

    if (data_size < coded_size.width * coded_size.height * 3 / 2) {
        ALOGE("data_size(=%zu) is not enough for coded_size(=%dx%d)", data_size, coded_size.width,
              coded_size.height);
        return nullptr;
    }
    // We've found in ARC++P H264 decoding, |data_size| of some output buffers are
    // bigger than the area which |coded_size| needs (not observed on other codec
    // and ARC++N).
    // TODO(johnylin): find the root cause (b/130398258)
    if (data_size > coded_size.width * coded_size.height * 3 / 2) {
        ALOGV("data_size(=%zu) is bigger than the area coded_size(=%dx%d) needs.", data_size,
              coded_size.width, coded_size.height);
    }

    return std::unique_ptr<VideoFrame>(
            new VideoFrame(data, coded_size, visible_size, color_format));
}

VideoFrame::VideoFrame(const uint8_t* data, const Size& coded_size, const Size& visible_size,
                       int32_t color_format)
      : data_(data),
        coded_size_(coded_size),
        visible_size_(visible_size),
        color_format_(color_format) {
    frame_data_[0] =
            std::unique_ptr<uint8_t[]>(new uint8_t[visible_size_.width * visible_size_.height]());
    frame_data_[1] = std::unique_ptr<uint8_t[]>(
            new uint8_t[visible_size_.width * visible_size_.height / 4]());
    frame_data_[2] = std::unique_ptr<uint8_t[]>(
            new uint8_t[visible_size_.width * visible_size_.height / 4]());
    if (IsFlexibleFormat()) {
        ALOGV("Cannot convert video frame now, should be done later by matching HAL "
              "format.");
        return;
    }
    CopyAndConvertToI420Frame(color_format_);
}

bool VideoFrame::IsFlexibleFormat() const {
    return color_format_ == YUV_420_FLEXIBLE;
}

void VideoFrame::CopyAndConvertToI420Frame(int32_t curr_format) {
    size_t stride = coded_size_.width;
    size_t slice_height = coded_size_.height;
    size_t width = visible_size_.width;
    size_t height = visible_size_.height;

    const uint8_t* src = data_;
    CopyWindow(src, frame_data_[0].get(), stride, width, height, 1);  // copy Y
    src += stride * slice_height;

    switch (curr_format) {
    case YUV_420_PLANAR:
        CopyWindow(src, frame_data_[1].get(), stride / 2, width / 2, height / 2,
                   1);  // copy U
        src += stride * slice_height / 4;
        CopyWindow(src, frame_data_[2].get(), stride / 2, width / 2, height / 2,
                   1);  // copy V
        return;
    case HAL_PIXEL_FORMAT_NV12:
        // NV12: semiplanar = true, crcb_swap = false.
        CopyWindow(src, frame_data_[1].get(), stride / 2, width / 2, height / 2,
                   2);  // copy U
        src++;
        CopyWindow(src, frame_data_[2].get(), stride / 2, width / 2, height / 2,
                   2);  // copy V
        return;
    case HAL_PIXEL_FORMAT_YV12:
        // YV12: semiplanar = false, crcb_swap = true.
        CopyWindow(src, frame_data_[2].get(), stride / 2, width / 2, height / 2,
                   1);  // copy V
        src += stride * slice_height / 4;
        CopyWindow(src, frame_data_[1].get(), stride / 2, width / 2, height / 2,
                   1);  // copy U
        return;
    default:
        ALOGE("Unknown format: 0x%x", curr_format);
        return;
    }
}

bool VideoFrame::MatchHalFormatByGoldenMD5(const std::string& golden) {
    if (!IsFlexibleFormat()) return true;

    // Try to match with HAL_PIXEL_FORMAT_NV12 first.
    int32_t format_candidates[2] = {HAL_PIXEL_FORMAT_NV12, HAL_PIXEL_FORMAT_YV12};
    for (int32_t format : format_candidates) {
        CopyAndConvertToI420Frame(format);
        color_format_ = format;
        std::string frame_md5 = ComputeMD5FromFrame();
        if (!strcmp(frame_md5.c_str(), golden.c_str())) {
            ALOGV("Matched YUV Flexible to HAL pixel format: 0x%x", format);
            return true;
        } else {
            ALOGV("Tried HAL pixel format: 0x%x un-matched (%s vs %s)", format, frame_md5.c_str(),
                  golden.c_str());
        }
    }

    // Change back to flexible format.
    color_format_ = YUV_420_FLEXIBLE;
    return false;
}

std::string VideoFrame::ComputeMD5FromFrame() const {
    if (IsFlexibleFormat()) {
        ALOGE("Cannot compute MD5 with format YUV_420_FLEXIBLE");
        return std::string();
    }

    MD5Context context;
    MD5Init(&context);
    MD5Update(&context, std::string(reinterpret_cast<const char*>(frame_data_[0].get()),
                                    visible_size_.width * visible_size_.height));
    MD5Update(&context, std::string(reinterpret_cast<const char*>(frame_data_[1].get()),
                                    visible_size_.width * visible_size_.height / 4));
    MD5Update(&context, std::string(reinterpret_cast<const char*>(frame_data_[2].get()),
                                    visible_size_.width * visible_size_.height / 4));
    MD5Digest digest;
    MD5Final(&digest, &context);
    return MD5DigestToBase16(digest);
}

bool VideoFrame::VerifyMD5(const std::string& golden) {
    if (IsFlexibleFormat()) {
        // Color format is YUV_420_FLEXIBLE and we haven't match its HAL pixel
        // format yet. Try to match now.
        if (!MatchHalFormatByGoldenMD5(golden)) {
            ALOGE("Failed to match any HAL format");
            return false;
        }
    } else {
        std::string md5 = ComputeMD5FromFrame();
        if (strcmp(md5.c_str(), golden.c_str())) {
            ALOGE("MD5 mismatched. expect: %s, got: %s", golden.c_str(), md5.c_str());
            return false;
        }
    }
    return true;
}

bool VideoFrame::WriteFrame(std::ofstream* output_file) const {
    if (IsFlexibleFormat()) {
        ALOGE("Cannot write frame with format YUV_420_FLEXIBLE");
        return false;
    }

    output_file->write(reinterpret_cast<const char*>(frame_data_[0].get()),
                       visible_size_.width * visible_size_.height);
    output_file->write(reinterpret_cast<const char*>(frame_data_[1].get()),
                       visible_size_.width * visible_size_.height / 4);
    output_file->write(reinterpret_cast<const char*>(frame_data_[2].get()),
                       visible_size_.width * visible_size_.height / 4);
    return output_file->good();
}

}  // namespace android