12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- /*************************************************************************
- * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *************************************************************************/
- #include "fixture.h"
- #ifdef CNIS_WITH_CONTRIB
- using infer_server::video::PixelFmt;
- #define ALIGN(w, a) ((w + a - 1) & ~(a - 1))
- bool cvt_bgr_to_yuv420sp(const cv::Mat& bgr_image, uint32_t alignment, PixelFmt pixel_fmt, uint8_t* yuv_2planes_data) {
- cv::Mat yuv_i420_image;
- uint32_t width, height, stride;
- uint8_t *src_y, *src_u, *src_v, *dst_y, *dst_u, *dst_v;
- cv::cvtColor(bgr_image, yuv_i420_image, cv::COLOR_BGR2YUV_I420);
- width = bgr_image.cols;
- height = bgr_image.rows;
- if (alignment > 0)
- stride = ALIGN(width, alignment);
- else
- stride = width;
- uint32_t y_len = width * height;
- src_y = yuv_i420_image.data;
- src_u = yuv_i420_image.data + y_len;
- src_v = yuv_i420_image.data + y_len * 5 / 4;
- dst_y = yuv_2planes_data;
- dst_u = yuv_2planes_data + stride * height;
- dst_v = yuv_2planes_data + stride * height * 5 / 4;
- for (uint32_t i = 0; i < height; i++) {
- // y data
- memcpy(dst_y + i * stride, src_y + i * width, width);
- // uv data
- if (i % 2 == 0) {
- if (pixel_fmt == PixelFmt::I420) {
- memcpy(dst_u + i * stride / 4, src_u + i * width / 4, width / 2);
- memcpy(dst_v + i * stride / 4, src_v + i * width / 4, width / 2);
- continue;
- }
- for (uint32_t j = 0; j < width / 2; j++) {
- if (pixel_fmt == PixelFmt::NV21) {
- *(dst_u + i * stride / 2 + 2 * j) = *(src_v + i * width / 4 + j);
- *(dst_u + i * stride / 2 + 2 * j + 1) = *(src_u + i * width / 4 + j);
- } else {
- *(dst_u + i * stride / 2 + 2 * j) = *(src_u + i * width / 4 + j);
- *(dst_u + i * stride / 2 + 2 * j + 1) = *(src_v + i * width / 4 + j);
- }
- }
- }
- }
- return true;
- }
- #endif
|