VideoV4lSource::VideoV4lSource(const Pipeline &pipeline, const VideoSourceConfig &config) : VideoSource(config), expectedStandard_("NTSC"), actualStandard_("") { source_ = pipeline.makeElement(config_.source(), NULL); // set a v4l2src if given to config as an arg, otherwise use default if (config_.hasDeviceName() && config_.deviceExists()) g_object_set(G_OBJECT(source_), "device", config_.deviceName(), NULL); if (!v4l2util::checkStandard(expectedStandard_, actualStandard_, deviceStr())) if (not actualStandard_.empty()) LOG_WARNING("V4l2 device " << deviceStr() << " is not set to expected standard " << expectedStandard_ << ", it is " << actualStandard_); LOG_DEBUG("v4l width is " << v4l2util::captureWidth(deviceStr())); LOG_DEBUG("v4l height is " << v4l2util::captureHeight(deviceStr())); if (willModifyCaptureResolution()) { LOG_INFO("Changing v4l resolution to " << config_.captureWidth() << "x" << config_.captureHeight()); v4l2util::setFormatVideo(deviceStr(), config_.captureWidth(), config_.captureHeight()); } capsFilter_ = pipeline.makeElement("capsfilter", NULL); setCapsFilter(srcCaps()); gstlinkable::link(source_, capsFilter_); }
std::string VideoV4lSource::srcCaps(unsigned int framerateIndex) const { std::ostringstream capsStr; GstStateChangeReturn ret = gst_element_set_state(source_, GST_STATE_READY); if (ret not_eq GST_STATE_CHANGE_SUCCESS) THROW_ERROR("Could not change v4l2src state to READY"); GstPad *srcPad = gst_element_get_static_pad(source_, "src"); GstCaps *caps = gst_pad_get_caps(srcPad); GstStructure *structure = gst_caps_get_structure(caps, 0); const GValue *val = gst_structure_get_value(structure, "framerate"); LOG_DEBUG("Caps structure from v4l2src srcpad: " << gst_structure_to_string(structure)); gint framerate_numerator, framerate_denominator; if (GST_VALUE_HOLDS_LIST(val)) { // trying another one if (framerateIndex >= gst_value_list_get_size(val)) THROW_ERROR("Framerate index out of range"); framerate_numerator = gst_value_get_fraction_numerator((gst_value_list_get_value(val, framerateIndex))); framerate_denominator = gst_value_get_fraction_denominator((gst_value_list_get_value(val, framerateIndex))); } else { // FIXME: this is really bad, we should be iterating over framerates and resolutions until we find a good one if (framerateIndex > 0) LOG_ERROR("Caps parameters haven't been changed and have failed before"); framerate_numerator = gst_value_get_fraction_numerator(val); framerate_denominator = gst_value_get_fraction_denominator(val); } gst_caps_unref(caps); gst_object_unref(srcPad); // use default from gst std::string capsSuffix = boost::lexical_cast<std::string>(framerate_numerator); capsSuffix += "/"; capsSuffix += boost::lexical_cast<std::string>(framerate_denominator); if (v4l2util::isInterlaced(deviceStr())) capsSuffix +=", interlaced=true"; capsSuffix += ", pixel-aspect-ratio="; capsSuffix += config_.pixelAspectRatio(); capsStr << "video/x-raw-yuv, width=" << config_.captureWidth() << ", height=" << config_.captureHeight() << ", framerate=" << capsSuffix; LOG_DEBUG("V4l2src caps are " << capsStr.str()); ret = gst_element_set_state(source_, GST_STATE_NULL); if (ret not_eq GST_STATE_CHANGE_SUCCESS) THROW_ERROR("Could not change v4l2src state to NULL"); return capsStr.str(); }
int ComputeBench::setupCL(void) { cl_int status = 0; cl_device_type dType; if (sampleArgs->deviceType.compare("cpu") == 0) { dType = CL_DEVICE_TYPE_CPU; } else //deviceType = "gpu" { dType = CL_DEVICE_TYPE_GPU; if (sampleArgs->isThereGPU() == false) { std::cout << "GPU not found. Falling back to CPU device" << std::endl; dType = CL_DEVICE_TYPE_CPU; } } /* * Have a look at the available platforms and pick either * the AMD one if available or a reasonable default. */ cl_platform_id platform = NULL; int retValue = getPlatform(platform, sampleArgs->platformId, sampleArgs->isPlatformEnabled()); CHECK_ERROR(retValue, SDK_SUCCESS, "getPlatform() failed"); // Display available devices. retValue = displayDevices(platform, dType); CHECK_ERROR(retValue, SDK_SUCCESS, "displayDevices() failed"); /* * If we could find our platform, use it. Otherwise use just available platform. */ cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties) platform, 0 }; context = clCreateContextFromType(cps, dType, NULL, NULL, &status); CHECK_OPENCL_ERROR(status, "clCreateContextFromType failed."); // getting device on which to run the sample status = getDevices(context, &devices, sampleArgs->deviceId, sampleArgs->isDeviceIdEnabled()); CHECK_ERROR(status, SDK_SUCCESS, "getDevices() failed"); //Set device info of given cl_device_id retValue = deviceInfo.setDeviceInfo(devices[sampleArgs->deviceId]); CHECK_ERROR(retValue, SDK_SUCCESS, "SDKDeviceInfo::setDeviceInfo() failed"); std::string deviceStr(deviceInfo.deviceVersion); size_t vStart = deviceStr.find(" ", 0); size_t vEnd = deviceStr.find(" ", vStart + 1); std::string vStrVal = deviceStr.substr(vStart + 1, vEnd - vStart - 1); // OpenCL 1.1 has inbuilt support for vec3 data types if (vec3 == true) { OPENCL_EXPECTED_ERROR("Device doesn't support built-in 3 component vectors!"); } // The block is to move the declaration of prop closer to its use /* Note: Using deprecated clCreateCommandQueue as CL_QUEUE_PROFILING_ENABLE flag not currently working ***with clCreateCommandQueueWithProperties*/ cl_command_queue_properties prop = 0; prop |= CL_QUEUE_PROFILING_ENABLE; commandQueue = clCreateCommandQueue(context, devices[sampleArgs->deviceId], prop, &status); CHECK_OPENCL_ERROR(status, "clCreateCommandQueue failed."); if (sampleArgs->isLoadBinaryEnabled()) { // Always assuming kernel was dumped for vector-width 1 if (vectorSize != 0) { std::cout << "Ignoring specified vector-width. Assuming kernel was dumped for vector-width 1" << std::endl; } vectorSize = 1; } else { // If vector-size is not specified in the command-line, choose the preferred size for the device if (vectorSize == 0) { vectorSize = deviceInfo.preferredFloatVecWidth; } else if (vectorSize == 3) { //Make vectorSize as 4 if -v option is 3. //This memory alignment is required as per OpenCL for type3 vectors vec3 = true; vectorSize = 4; } else if ((1 != vectorSize) && (2 != vectorSize) && (4 != vectorSize) && (8 != vectorSize) && (16 != vectorSize)) { std::cout << "The vectorsize can only be one of 1,2,3(4),4,8,16!" << std::endl; return SDK_FAILURE; } } outputKadd = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof (cl_float) * vectorSize * length, 0, &status); CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (outputKadd)"); // create a CL program using the kernel source char buildOption[512]; if (vectorSize == 1) { sprintf(buildOption, "-D DATATYPE=uint -D DATATYPE2=uint4 "); //sprintf(buildOption, "-D DATATYPE=float -D DATATYPE2=float4 "); } else { sprintf(buildOption, "-D DATATYPE=uint%d -D DATATYPE2=uint%d ", (vec3 == true) ? 3 : vectorSize, (vec3 == true) ? 3 : vectorSize); //sprintf(buildOption, "-D DATATYPE=float%d -D DATATYPE2=float%d ", (vec3 == true) ? 3 : vectorSize, (vec3 == true) ? 3 : vectorSize); } strcat(buildOption, "-D IDXTYPE=uint "); // create a CL program using the kernel source buildProgramData buildData; buildData.kernelName = std::string("ComputeBench.cl"); buildData.devices = devices; buildData.deviceId = sampleArgs->deviceId; buildData.flagsStr = std::string(buildOption); if (sampleArgs->isLoadBinaryEnabled()) { buildData.binaryName = std::string(sampleArgs->loadBinary.c_str()); } if (sampleArgs->isComplierFlagsSpecified()) { buildData.flagsFileName = std::string(sampleArgs->flags.c_str()); } retValue = buildOpenCLProgram(program, context, buildData); CHECK_ERROR(retValue, SDK_SUCCESS, "buildOpenCLProgram() failed"); // Global memory bandwidth from read-single access kernel[0] = clCreateKernel(program, "Kadd", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(Kadd)"); return SDK_SUCCESS; }
int GlobalMemoryBandwidth::setupCL(void) { cl_int status = 0; cl_device_type dType; if(deviceType.compare("cpu") == 0) { dType = CL_DEVICE_TYPE_CPU; } else //deviceType = "gpu" { dType = CL_DEVICE_TYPE_GPU; if(isThereGPU() == false) { std::cout << "GPU not found. Falling back to CPU device" << std::endl; dType = CL_DEVICE_TYPE_CPU; } } /* * Have a look at the available platforms and pick either * the AMD one if available or a reasonable default. */ cl_platform_id platform = NULL; int retValue = sampleCommon->getPlatform(platform, platformId, isPlatformEnabled()); CHECK_ERROR(retValue, SDK_SUCCESS, "sampleCommon::getPlatform() failed"); // Display available devices. retValue = sampleCommon->displayDevices(platform, dType); CHECK_ERROR(retValue, SDK_SUCCESS, "sampleCommon::displayDevices() failed"); /* * If we could find our platform, use it. Otherwise use just available platform. */ cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 }; context = clCreateContextFromType(cps, dType, NULL, NULL, &status); CHECK_OPENCL_ERROR(status, "clCreateContextFromType failed."); // getting device on which to run the sample status = sampleCommon->getDevices(context, &devices, deviceId, isDeviceIdEnabled()); CHECK_ERROR(status, SDK_SUCCESS, "sampleCommon::getDevices() failed"); //Set device info of given cl_device_id retValue = deviceInfo.setDeviceInfo(devices[deviceId]); CHECK_ERROR(retValue, SDK_SUCCESS, "SDKDeviceInfo::setDeviceInfo() failed"); std::string deviceStr(deviceInfo.deviceVersion); size_t vStart = deviceStr.find(" ", 0); size_t vEnd = deviceStr.find(" ", vStart + 1); std::string vStrVal = deviceStr.substr(vStart + 1, vEnd - vStart - 1); #ifdef CL_VERSION_1_1 if(vStrVal.compare("1.0") > 0) { char openclVersion[1024]; status = clGetDeviceInfo(devices[deviceId], CL_DEVICE_OPENCL_C_VERSION, sizeof(openclVersion), openclVersion, 0); CHECK_OPENCL_ERROR(status, "clGetDeviceInfo failed."); std::string tempStr(openclVersion); size_t dotPos = tempStr.find_first_of("."); size_t spacePos = tempStr.find_last_of(" "); tempStr = tempStr.substr(dotPos + 1, spacePos - dotPos); int minorVersion = atoi(tempStr.c_str()); // OpenCL 1.1 has inbuilt support for vec3 data types if(minorVersion < 1 && vec3 == true) { OPENCL_EXPECTED_ERROR("Device doesn't support built-in 3 component vectors!"); } } else { // OpenCL 1.1 has inbuilt support for vec3 data types if(vec3 == true) { OPENCL_EXPECTED_ERROR("Device doesn't support built-in 3 component vectors!"); } } #else // OpenCL 1.1 has inbuilt support for vec3 data types if(vec3 == true) { OPENCL_EXPECTED_ERROR("Device doesn't support built-in 3 component vectors!"); } #endif { // The block is to move the declaration of prop closer to its use cl_command_queue_properties prop = 0; prop |= CL_QUEUE_PROFILING_ENABLE; commandQueue = clCreateCommandQueue(context, devices[deviceId], prop, &status); CHECK_OPENCL_ERROR(status, "clCreateCommandQueue failed."); } cl_uint sizeElement = vectorSize * sizeof(cl_float); cl_uint readLength = length + (NUM_READS * 1024 / sizeElement) + EXTRA_BYTES; cl_uint size = readLength * vectorSize * sizeof(cl_float); // Create input buffer inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY, size, 0, &status); CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (inputBuffer)"); // Write data to buffer status = clEnqueueWriteBuffer(commandQueue, inputBuffer, 1, 0, size, input, 0, 0, 0); CHECK_OPENCL_ERROR(status, "clEnqueueWriteBuffer failed. (inputBuffer)"); outputBufferReadSingle = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_float) * vectorSize * length, 0, &status); CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (outputBufferReadSingle)"); // Write data to buffer status = clEnqueueWriteBuffer(commandQueue, outputBufferReadSingle, CL_TRUE, 0, sizeof(cl_float) * vectorSize * length, outputReadSingle, 0, NULL, NULL); CHECK_OPENCL_ERROR(status, "clEnqueueWriteBuffer failed. (outputBufferReadSingle)"); outputBufferReadLinear = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_float) * vectorSize * length, 0, &status); CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (outputBufferReadLinear)"); // Write data to buffer status = clEnqueueWriteBuffer(commandQueue, outputBufferReadLinear, CL_TRUE, 0, sizeof(cl_float) * vectorSize * length, outputReadLinear, 0, NULL, NULL); CHECK_OPENCL_ERROR(status, "clEnqueueWriteBuffer failed. (outputBufferReadLinear)"); outputBufferReadLU = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_float) * vectorSize * length, 0, &status); CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (outputBufferReadLU)"); // Write data to buffer status = clEnqueueWriteBuffer(commandQueue, outputBufferReadLU, CL_TRUE, 0, sizeof(cl_float) * vectorSize * length, outputReadLU, 0, NULL, NULL); CHECK_OPENCL_ERROR(status, "clEnqueueWriteBuffer failed. (outputBufferReadLU)"); outputBufferWriteLinear = clCreateBuffer(context, CL_MEM_WRITE_ONLY, size, 0, &status); CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (outputBufferWriteLinear)"); // Write data to buffer status = clEnqueueWriteBuffer(commandQueue, outputBufferWriteLinear, CL_TRUE, 0, size, outputWriteLinear, 0, NULL, NULL); CHECK_OPENCL_ERROR(status, "clEnqueueWriteBuffer failed. (outputBufferWriteLinear)"); // create a CL program using the kernel source char buildOption[128]; if(vectorSize == 1) sprintf(buildOption, "-D DATATYPE=float -D OFFSET=%d ", OFFSET); else sprintf(buildOption, "-D DATATYPE=float%d -D OFFSET=%d ", (vec3 == true) ? 3 : vectorSize, OFFSET); // create a CL program using the kernel source streamsdk::buildProgramData buildData; buildData.kernelName = std::string("GlobalMemoryBandwidth_Kernels.cl"); buildData.devices = devices; buildData.deviceId = deviceId; buildData.flagsStr = std::string(buildOption); if(isLoadBinaryEnabled()) buildData.binaryName = std::string(loadBinary.c_str()); if(isComplierFlagsSpecified()) buildData.flagsFileName = std::string(flags.c_str()); retValue = sampleCommon->buildOpenCLProgram(program, context, buildData); CHECK_ERROR(retValue, SDK_SUCCESS, "sampleCommon::buildOpenCLProgram() failed"); // Global memory bandwidth from read-single access kernel[0] = clCreateKernel(program, "read_single", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(read_single)"); // Global memory bandwidth from read-linear access kernel[1] = clCreateKernel(program, "read_linear", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(read_linear)"); // Global memory bandwidth from read-linear access kernel[2] = clCreateKernel(program, "read_linear_uncached", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(read_linear_uncached)"); // Global memory bandwidth from write-linear access kernel[3] = clCreateKernel(program, "write_linear", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(GlobalBandwidth_write_linear)"); return SDK_SUCCESS; }
int LDSBandwidth::setupCL(void) { cl_int status = 0; cl_device_type dType; if(sampleArgs->deviceType.compare("cpu") == 0) { dType = CL_DEVICE_TYPE_CPU; } else //deviceType = "gpu" { dType = CL_DEVICE_TYPE_GPU; if(sampleArgs->isThereGPU() == false) { std::cout << "GPU not found. Falling back to CPU device" << std::endl; dType = CL_DEVICE_TYPE_CPU; } } /* * Have a look at the available platforms and pick either * the AMD one if available or a reasonable default. */ cl_platform_id platform = NULL; int retValue = getPlatform(platform, sampleArgs->platformId, sampleArgs->isPlatformEnabled()); CHECK_ERROR(retValue, SDK_SUCCESS, "getPlatform() failed"); // Display available devices. retValue = displayDevices(platform, dType); CHECK_ERROR(retValue, SDK_SUCCESS, "displayDevices() failed"); /* * If we could find our platform, use it. Otherwise use just available platform. */ cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 }; context = clCreateContextFromType(cps, dType, NULL, NULL, &status); CHECK_OPENCL_ERROR(status, "clCreateContextFromType failed."); // getting device on which to run the sample status = getDevices(context, &devices, sampleArgs->deviceId, sampleArgs->isDeviceIdEnabled()); CHECK_ERROR(status, SDK_SUCCESS, "getDevices() failed"); //Set device info of given cl_device_id retValue = deviceInfo.setDeviceInfo(devices[sampleArgs->deviceId]); CHECK_ERROR(retValue, SDK_SUCCESS, "SDKDeviceInfo::setDeviceInfo() failed"); std::string deviceStr(deviceInfo.deviceVersion); size_t vStart = deviceStr.find(" ", 0); size_t vEnd = deviceStr.find(" ", vStart + 1); std::string vStrVal = deviceStr.substr(vStart + 1, vEnd - vStart - 1); #ifdef CL_VERSION_1_1 if(vStrVal.compare("1.0") > 0) { char openclVersion[1024]; status = clGetDeviceInfo(devices[sampleArgs->deviceId], CL_DEVICE_OPENCL_C_VERSION, sizeof(openclVersion), openclVersion, 0); CHECK_OPENCL_ERROR(status, "clGetDeviceInfo failed."); std::string tempStr(openclVersion); size_t dotPos = tempStr.find_first_of("."); size_t spacePos = tempStr.find_last_of(" "); tempStr = tempStr.substr(dotPos + 1, spacePos - dotPos); int minorVersion = atoi(tempStr.c_str()); // OpenCL 1.1 has inbuilt support for vec3 data types if(minorVersion < 1 && vec3 == true) { OPENCL_EXPECTED_ERROR("Device doesn't support built-in 3 component vectors!"); } } else { // OpenCL 1.1 has inbuilt support for vec3 data types if(vec3 == true) { OPENCL_EXPECTED_ERROR("Device doesn't support built-in 3 component vectors!"); } } #else // OpenCL 1.1 has inbuilt support for vec3 data types if(vec3 == true) { OPENCL_EXPECTED_ERROR("Device doesn't support built-in 3 component vectors!"); } #endif { // The block is to move the declaration of prop closer to its use cl_command_queue_properties prop = 0; prop |= CL_QUEUE_PROFILING_ENABLE; commandQueue = clCreateCommandQueue(context, devices[sampleArgs->deviceId], prop, &status); CHECK_OPENCL_ERROR(status, "clCreateCommandQueue failed."); } outputBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_float) * vectorSize * length, 0, &status); CHECK_OPENCL_ERROR(status, "clCreateBuffer failed. (outputBuffer)"); // create a CL program using the kernel source char buildOption[64]; if(vectorSize == 1) { sprintf(buildOption, "-D DATATYPE=float "); } else { sprintf(buildOption, "-D DATATYPE=float%d ", vec3 == true ? 3 : vectorSize); } buildProgramData buildData; buildData.kernelName = std::string("LDSBandwidth_Kernels.cl"); buildData.devices = devices; buildData.deviceId = sampleArgs->deviceId; buildData.flagsStr = std::string(buildOption); if(sampleArgs->isLoadBinaryEnabled()) { buildData.binaryName = std::string(sampleArgs->loadBinary.c_str()); } if(sampleArgs->isComplierFlagsSpecified()) { buildData.flagsFileName = std::string(sampleArgs->flags.c_str()); } retValue = buildOpenCLProgram(program, context, buildData); CHECK_ERROR(retValue, SDK_SUCCESS, "buildOpenCLProgram() failed"); // ConstantBuffer bandwidth from single access kernel[0] = clCreateKernel(program, "LDSBandwidth_single", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(LDSBandwidth_single)"); // ConstantBuffer bandwidth from linear access kernel[1] = clCreateKernel(program, "LDSBandwidth_linear", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(LDSBandwidth_linear)"); kernel[2] = clCreateKernel(program, "LDSBandwidth_single_verify", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(LDSBandwidth_single_verify)"); kernel[3] = clCreateKernel(program, "LDSBandwidth_linear_verify", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(LDSBandwidth_linear_verify)"); kernel[4] = clCreateKernel(program, "LDSBandwidth_write_linear", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(LDSBandwidth_linear_verify)"); kernel[5] = clCreateKernel(program, "LDSBandwidth_write_linear_verify", &status); CHECK_OPENCL_ERROR(status, "clCreateKernel failed.(LDSBandwidth_linear_verify)"); return SDK_SUCCESS; }
bool VideoV4lSource::willModifyCaptureResolution() const { return v4l2util::captureWidth(deviceStr()) != config_.captureWidth() or v4l2util::captureHeight(deviceStr()) != config_.captureHeight(); }