32 #include <VisionX/interface/core/ImageProviderInterface.h>
36 #include <Image/ImageProcessor.h>
43 visionx::ImageProcessorPropertyDefinitions(prefix)
45 defineOptionalProperty<std::string>(
46 "imageProviderProxyName",
"TestImageProvider",
"Name of the image provider");
47 defineOptionalProperty<std::string>(
48 "imageStreamTopicName",
"ImageStream",
"Name of the image streaming topic");
49 defineOptionalProperty<std::string>(
"h264Preset",
"veryfast",
"Preset for the h264 codec");
50 defineOptionalProperty<std::string>(
"h264Profile",
"Main",
"profile for the h264 codec");
54 defineOptionalProperty<float>(
"CompressionRate", 25,
"QRC value of h264 codec");
55 defineOptionalProperty<CodecType>(
"Codec", eH264,
"Codec used for compression")
56 .setCaseInsensitive(
true)
58 defineOptionalProperty<float>(
61 "Framerate at which the input images are encoded. Slower or fasting pushing of input "
62 "is possible. Frames will be skipped then. ");
72 srand(
static_cast<unsigned int>(IceUtil::Time::now().toMicroSeconds()));
79 fps = getProperty<float>(
"Framerate");
80 Logging::setTag(
"StreamProvider");
84 this, &StreamProviderI::calculateFps, 995,
true,
"FPSCalcThread");
85 fpsCalculator->start();
87 imageProviderProxyName = getProperty<std::string>(
"imageProviderProxyName").getValue();
101 imageProviderProxy = getProxy<visionx::ImageProviderInterfacePrx>(imageProviderProxyName);
105 if (imageFormat.type != visionx::eRgb)
107 throw armarx::LocalException(
"The StreamProvider supports only RGB at the moment.");
110 imgWidth = imageFormat.dimension.width;
111 imgHeight = imageFormat.dimension.height;
112 encodedImgHeight = imgHeight * numberImages;
113 imgType = imageFormat.type;
116 ppInputImages =
new CByteImage*[size_t(numberImages)];
117 for (
int i = 0; i < numberImages; i++)
124 new CByteImage(imageProviderInfo.
imageFormat.dimension.width,
125 imageProviderInfo.
imageFormat.dimension.height * numberImages,
129 x264_param_default_preset(
130 ¶m, getProperty<std::string>(
"h264Preset").getValue().c_str(),
"zerolatency");
132 param.i_width = imgWidth;
133 param.i_height = encodedImgHeight;
134 param.i_fps_num = uint32_t(fps);
137 param.i_keyint_max = int(fps);
138 param.b_intra_refresh = 1;
141 param.rc.i_rc_method = X264_RC_CRF;
142 param.rc.f_rf_constant = getProperty<float>(
"CompressionRate");
143 param.rc.f_rf_constant_max = param.rc.f_rf_constant * 1.4f;
150 param.b_repeat_headers = 1;
152 if (x264_param_apply_profile(
153 ¶m, getProperty<std::string>(
"h264Profile").getValue().c_str()) != 0)
156 << getProperty<std::string>(
"h264Profile").getValue()
157 <<
"' profile for x264 codec";
170 std::cout <<
"exiting StreamProviderI" << std::endl;
173 fpsCalculator->stop();
178 for (
int i = 0; i < numberImages; i++)
180 delete ppInputImages[i];
182 delete[] ppInputImages;
205 armarx::MetaInfoSizeBasePtr info;
206 getImages(imageProviderProxyName, ppInputImages, info);
209 for (
int i = 0; i < numberImages; ++i)
211 int imageByteSize = ppInputImages[i]->width * ppInputImages[i]->height *
212 ppInputImages[i]->bytesPerPixel;
213 memcpy(pImageForEncoder->pixels + i * imageByteSize,
214 ppInputImages[i]->pixels,
215 size_t(imageByteSize));
217 int srcstride = imgWidth * 3;
218 const uint8_t* pixels = pImageForEncoder->pixels;
219 sws_scale(convertCtx,
225 pic_in.img.i_stride);
228 int frameSize = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);
236 chunk.reserve(
size_t(frameSize));
237 chunk.assign(nals->p_payload, nals->p_payload + frameSize);
238 listener->reportNewStreamData(chunk, info->timeProvided);
251 return "StreamProvider";
262 encoder = x264_encoder_open(¶m);
264 x264_picture_alloc(&pic_in, X264_CSP_I420, imgWidth, encodedImgHeight);
266 convertCtx = sws_getContext(imgWidth,
289 x264_picture_clean(&pic_in);
290 memset(
reinterpret_cast<char*
>(&pic_in), 0,
sizeof(pic_in));
291 memset(
reinterpret_cast<char*
>(&pic_out), 0,
sizeof(pic_out));
293 x264_encoder_close(encoder);
299 sws_freeContext(convertCtx);
300 convertCtx =
nullptr;
307 return getProperty<CodecType>(
"Codec").getValue();
311 StreamProviderI::calculateFps()
329 imageWidth = imgWidth;
330 imageHeight = imgHeight;