29 #include <VisionX/interface/core/ImageProviderInterface.h>
34 #include <Image/ImageProcessor.h>
43 visionx::ImageProcessorPropertyDefinitions(prefix)
45 defineOptionalProperty<std::string>(
"imageProviderProxyName",
"TestImageProvider",
"Name of the image provider");
46 defineOptionalProperty<std::string>(
"imageStreamTopicName",
"ImageStream",
"Name of the image streaming topic");
47 defineOptionalProperty<std::string>(
"h264Preset",
"veryfast",
"Preset for the h264 codec");
48 defineOptionalProperty<std::string>(
"h264Profile",
"Main",
"profile for the h264 codec");
52 defineOptionalProperty<float>(
"CompressionRate", 25,
"QRC value of h264 codec");
53 defineOptionalProperty<CodecType>(
"Codec", eH264,
"Codec used for compression")
54 .setCaseInsensitive(
true)
56 defineOptionalProperty<float>(
"Framerate", 25,
"Framerate at which the input images are encoded. Slower or fasting pushing of input is possible. Frames will be skipped then. ");
67 srand(
static_cast<unsigned int>(IceUtil::Time::now().toMicroSeconds()));
74 fps = getProperty<float>(
"Framerate");
75 Logging::setTag(
"StreamProvider");
79 fpsCalculator->start();
81 imageProviderProxyName = getProperty<std::string>(
"imageProviderProxyName").getValue();
95 imageProviderProxy = getProxy<visionx::ImageProviderInterfacePrx>(imageProviderProxyName);
99 if (imageFormat.type != visionx::eRgb)
101 throw armarx::LocalException(
"The StreamProvider supports only RGB at the moment.");
104 imgWidth = imageFormat.dimension.width;
105 imgHeight = imageFormat.dimension.height;
106 encodedImgHeight = imgHeight * numberImages;
107 imgType = imageFormat.type;
110 ppInputImages =
new CByteImage*[size_t(numberImages)];
111 for (
int i = 0; i < numberImages ; i++)
117 pImageForEncoder =
new CByteImage(imageProviderInfo.
imageFormat.dimension.width,
118 imageProviderInfo.
imageFormat.dimension.height * numberImages,
122 x264_param_default_preset(¶m, getProperty<std::string>(
"h264Preset").getValue().c_str(),
"zerolatency");
124 param.i_width = imgWidth;
125 param.i_height = encodedImgHeight;
126 param.i_fps_num = uint32_t(fps);
129 param.i_keyint_max = int(fps);
130 param.b_intra_refresh = 1;
133 param.rc.i_rc_method = X264_RC_CRF;
134 param.rc.f_rf_constant = getProperty<float>(
"CompressionRate");
135 param.rc.f_rf_constant_max = param.rc.f_rf_constant * 1.4f;
142 param.b_repeat_headers = 1;
144 if (x264_param_apply_profile(¶m, getProperty<std::string>(
"h264Profile").getValue().c_str()) != 0)
146 ARMARX_WARNING <<
"Could not set '" << getProperty<std::string>(
"h264Profile").getValue() <<
"' profile for x264 codec";
158 std::cout <<
"exiting StreamProviderI" << std::endl;
161 fpsCalculator->stop();
166 for (
int i = 0; i < numberImages ; i++)
168 delete ppInputImages[i];
170 delete[] ppInputImages;
193 armarx::MetaInfoSizeBasePtr info;
194 getImages(imageProviderProxyName, ppInputImages, info);
197 for (
int i = 0; i < numberImages; ++i)
199 int imageByteSize = ppInputImages[i]->width * ppInputImages[i]->height * ppInputImages[i]->bytesPerPixel;
200 memcpy(pImageForEncoder->pixels + i * imageByteSize, ppInputImages[i]->pixels,
size_t(imageByteSize));
202 int srcstride = imgWidth * 3;
203 const uint8_t* pixels = pImageForEncoder->pixels;
204 sws_scale(convertCtx, &pixels, &srcstride, 0, encodedImgHeight, pic_in.img.plane, pic_in.img.i_stride);
207 int frameSize = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);
215 chunk.reserve(
size_t(frameSize));
216 chunk.assign(nals->p_payload, nals->p_payload + frameSize);
217 listener->reportNewStreamData(chunk, info->timeProvided);
232 return "StreamProvider";
245 encoder = x264_encoder_open(¶m);
247 x264_picture_alloc(&pic_in, X264_CSP_I420, imgWidth, encodedImgHeight);
249 convertCtx = sws_getContext(imgWidth, encodedImgHeight, AV_PIX_FMT_RGB24, imgWidth, encodedImgHeight,
250 AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR,
nullptr,
nullptr,
nullptr);
263 x264_picture_clean(&pic_in);
264 memset(
reinterpret_cast<char*
>(&pic_in), 0,
sizeof(pic_in));
265 memset(
reinterpret_cast<char*
>(&pic_out), 0,
sizeof(pic_out));
267 x264_encoder_close(encoder);
273 sws_freeContext(convertCtx);
274 convertCtx =
nullptr;
282 return getProperty<CodecType>(
"Codec").getValue();
289 void StreamProviderI::calculateFps()
306 imageWidth = imgWidth;
307 imageHeight = imgHeight;