gapi_streaming_tests_common.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2021 Intel Corporation
  6. #ifndef OPENCV_GAPI_STREAMING_TESTS_COMMON_HPP
  7. #define OPENCV_GAPI_STREAMING_TESTS_COMMON_HPP
  8. #include "gapi_tests_common.hpp"
  9. #include <opencv2/gapi/streaming/onevpl/source.hpp>
  10. #include <opencv2/gapi/streaming/onevpl/data_provider_interface.hpp>
  11. #include "streaming/onevpl/data_provider_defines.hpp"
  12. #ifdef HAVE_ONEVPL
  13. #include "streaming/onevpl/onevpl_export.hpp"
  14. namespace opencv_test {
  15. namespace streaming {
  16. namespace onevpl {
  17. struct StreamDataProvider : public cv::gapi::wip::onevpl::IDataProvider {
  18. StreamDataProvider(std::istream& in) : data_stream (in) {
  19. EXPECT_TRUE(in);
  20. }
  21. mfx_codec_id_type get_mfx_codec_id() const override {
  22. return MFX_CODEC_HEVC;
  23. }
  24. bool fetch_bitstream_data(std::shared_ptr<mfx_bitstream> &out_bitstream) override {
  25. if (empty()) {
  26. return false;
  27. }
  28. if (!out_bitstream) {
  29. out_bitstream = std::make_shared<mfx_bitstream>();
  30. out_bitstream->MaxLength = 2000000;
  31. out_bitstream->Data = (mfxU8 *)calloc(out_bitstream->MaxLength, sizeof(mfxU8));
  32. if(!out_bitstream->Data) {
  33. throw std::runtime_error("Cannot allocate bitstream.Data bytes: " +
  34. std::to_string(out_bitstream->MaxLength * sizeof(mfxU8)));
  35. }
  36. out_bitstream->CodecId = get_mfx_codec_id();
  37. }
  38. mfxU8 *p0 = out_bitstream->Data;
  39. mfxU8 *p1 = out_bitstream->Data + out_bitstream->DataOffset;
  40. EXPECT_FALSE(out_bitstream->DataOffset > out_bitstream->MaxLength - 1);
  41. EXPECT_FALSE(out_bitstream->DataLength + out_bitstream->DataOffset > out_bitstream->MaxLength);
  42. std::copy_n(p1, out_bitstream->DataLength, p0);
  43. out_bitstream->DataOffset = 0;
  44. out_bitstream->DataLength += static_cast<mfxU32>(fetch_data(out_bitstream->MaxLength - out_bitstream->DataLength,
  45. out_bitstream->Data + out_bitstream->DataLength));
  46. return out_bitstream->DataLength != 0;
  47. }
  48. size_t fetch_data(size_t out_data_size, void* out_data_buf) {
  49. data_stream.read(reinterpret_cast<char*>(out_data_buf), out_data_size);
  50. return data_stream.gcount();
  51. }
  52. bool empty() const override {
  53. return data_stream.eof() || data_stream.bad();
  54. }
  55. private:
  56. std::istream& data_stream;
  57. };
  58. static const unsigned char hevc_header[] = {
  59. 0x00, 0x00, 0x00, 0x01, 0x40, 0x01, 0x0C, 0x06, 0xFF, 0xFF, 0x01, 0x40, 0x00,
  60. 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x78, 0x00,
  61. 0x00, 0x04, 0x02, 0x10, 0x30, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00, 0x00, 0x03,
  62. 0x01, 0xE5, 0x00, 0x00, 0x00, 0x01, 0x42, 0x01, 0x06, 0x01, 0x40, 0x00, 0x00,
  63. 0x03, 0x00, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x78, 0x00, 0x00,
  64. 0xA0, 0x10, 0x20, 0x61, 0x63, 0x41, 0x00, 0x86, 0x49, 0x1B, 0x2B, 0x20, 0x00,
  65. 0x00, 0x00, 0x01, 0x44, 0x01, 0xC0, 0x71, 0xC0, 0xD9, 0x20, 0x00, 0x00, 0x00,
  66. 0x01, 0x26, 0x01, 0xAF, 0x0C
  67. };
  68. } // namespace onevpl
  69. } // namespace streaming
  70. } // namespace opencv_test
  71. #endif // HAVE_ONEVPL
  72. #endif // OPENCV_GAPI_STREAMING_TESTS_HPP