fixture.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include "fixture.h"
  21. #ifdef CNIS_WITH_CONTRIB
  22. using infer_server::video::PixelFmt;
  23. #define ALIGN(w, a) ((w + a - 1) & ~(a - 1))
  24. bool cvt_bgr_to_yuv420sp(const cv::Mat& bgr_image, uint32_t alignment, PixelFmt pixel_fmt, uint8_t* yuv_2planes_data) {
  25. cv::Mat yuv_i420_image;
  26. uint32_t width, height, stride;
  27. uint8_t *src_y, *src_u, *src_v, *dst_y, *dst_u, *dst_v;
  28. cv::cvtColor(bgr_image, yuv_i420_image, cv::COLOR_BGR2YUV_I420);
  29. width = bgr_image.cols;
  30. height = bgr_image.rows;
  31. if (alignment > 0)
  32. stride = ALIGN(width, alignment);
  33. else
  34. stride = width;
  35. uint32_t y_len = width * height;
  36. src_y = yuv_i420_image.data;
  37. src_u = yuv_i420_image.data + y_len;
  38. src_v = yuv_i420_image.data + y_len * 5 / 4;
  39. dst_y = yuv_2planes_data;
  40. dst_u = yuv_2planes_data + stride * height;
  41. dst_v = yuv_2planes_data + stride * height * 5 / 4;
  42. for (uint32_t i = 0; i < height; i++) {
  43. // y data
  44. memcpy(dst_y + i * stride, src_y + i * width, width);
  45. // uv data
  46. if (i % 2 == 0) {
  47. if (pixel_fmt == PixelFmt::I420) {
  48. memcpy(dst_u + i * stride / 4, src_u + i * width / 4, width / 2);
  49. memcpy(dst_v + i * stride / 4, src_v + i * width / 4, width / 2);
  50. continue;
  51. }
  52. for (uint32_t j = 0; j < width / 2; j++) {
  53. if (pixel_fmt == PixelFmt::NV21) {
  54. *(dst_u + i * stride / 2 + 2 * j) = *(src_v + i * width / 4 + j);
  55. *(dst_u + i * stride / 2 + 2 * j + 1) = *(src_u + i * width / 4 + j);
  56. } else {
  57. *(dst_u + i * stride / 2 + 2 * j) = *(src_u + i * width / 4 + j);
  58. *(dst_u + i * stride / 2 + 2 * j + 1) = *(src_v + i * width / 4 + j);
  59. }
  60. }
  61. }
  62. }
  63. return true;
  64. }
  65. #endif