summaryrefslogtreecommitdiff
path: root/src/lib/fastvideo.cc
blob: 8e67f4b26af02b9633c0e25e98d6646d852816b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "exceptions.h"
#include "fastvideo.h"
#include <fastvideo_decoder_j2k.h>
#include <fastvideo_sdk.h>
#include <iostream>


using boost::shared_ptr;


void
handle (fastExportToHostHandle_t& adapter, fastGpuTimerHandle_t& device_to_host_timer)
{
	fastExportParameters_t exportParameters;
	exportParameters.convert = options.ConvertToBGR ? FAST_CONVERT_BGR : FAST_CONVERT_NONE;

	int aligned_width = info.width * 3;
	aligned_width += 4 - (aligned_width % FAST_ALIGNMENT);
	size_t decoded_size = info.height * aligned_width;
	uint8_t* decoded = 0;
	fastStatus_t r = fastMalloc(reinterpret_cast<void**>(&decoded), decoded_size);
	if (r != FAST_OK) {
		throw FastvideoError ("fastMalloc");
	}

	fastGpuTimerStart(device_to_host_timer	);

	CHECK_FAST(fastExportToHostCopy(
			adapter,

			img.data.get(),
			img.w,
			img.wPitch,
			img.h,

			&exportParameters
			));

	float elapsedTimeGpu = 0.;
	fastGpuTimerStop(deviceToHostTimer);
	fastGpuTimerGetTime(deviceToHostTimer, &elapsedTimeGpu);

	totalInternalTime += elapsedTimeGpu / 1000.0;

	outputImgs->push_back(img);
	if (imagesLeft == 0)
		break;
	CHECK_FAST(fastDecoderJ2kGetNextDecodedImage(decoder, &report, &imagesLeft));
}


shared_ptr<OpenJPEGImage>
fastvideo_decompress_j2k (dcp::Data data, int reduce)
{
	const int max_batch_size = 16;
	const int images_to_convert = 64;

	fastTraceCreate("/home/carl/trace.log");

	/*
	fastStatus_t r = fastInit(0, true);
	if (r != FAST_OK) {
		throw FastvideoError ("Init", r);
	}
	*/

	fastSdkParametersHandle_t sdk_parameters;
	fastStatus_t r = fastGetSdkParametersHandle(&sdk_parameters);
	if (r != FAST_OK) {
		throw FastvideoError ("GetSdkParametersHandle", r);
	}
	r = fastDecoderJ2kLibraryInit(sdk_parameters);
	if (r != FAST_OK) {
		throw FastvideoError ("J2kLibraryInit", r);
	}

	fastJ2kImageInfo_t info;
	r = fastDecoderJ2kPredecode(&info, data.data().get(), data.size());
	if (r != FAST_OK) {
		throw FastvideoError ("J2kPredecode");
	}

	std::cout << "predecode -> " << info.width << "x" << info.height << " stream size " << info.streamSize << "\n";

	/* Init */

	fastDecoderJ2kStaticParameters_t parameters;
	memset(&parameters, 0, sizeof(fastDecoderJ2kStaticParameters_t));

	parameters.ResolutionLevels = 0;
	parameters.verboseLevel = 1;
	parameters.enableROI = 0;

	parameters.maxTileHeight = info.height;
	parameters.maxTileWidth = info.width;

	parameters.windowX0 = 0;
	parameters.windowY0 = 0;
	parameters.windowWidth = info.width;
	parameters.windowHeight = info.height;

	parameters.truncationLength = 0;
	parameters.truncationMode = 0;
	parameters.truncationRate = 0;

	parameters.DecodePasses = 0;
	parameters.imageInfo = &info;
	parameters.maxStreamSize = info.streamSize;

	std::cout << "verboseLevel " << parameters.verboseLevel << "\n";
	std::cout << "maxTileWidth " << parameters.maxTileWidth << "\n";
	std::cout << "maxTileHeight " << parameters.maxTileHeight << "\n";
	std::cout << "ResolutionLevels " << parameters.ResolutionLevels << "\n";
	std::cout << "DecodePasses " << parameters.DecodePasses << "\n";
	std::cout << "maxStreamSize " << parameters.maxStreamSize << "\n";
	std::cout << "truncationMode " << parameters.truncationMode << "\n";
	std::cout << "truncationRate " << parameters.truncationRate << "\n";
	std::cout << "truncationLength " << parameters.truncationLength << "\n";
	std::cout << "windowX0 " << parameters.windowX0 << "\n";
	std::cout << "windowY0 " << parameters.windowY0 << "\n";
	std::cout << "windowWidth " << parameters.windowWidth << "\n";
	std::cout << "windowHeight " << parameters.windowHeight << "\n";
	std::cout << "enableROI " << parameters.enableROI << "\n";
	std::cout << "enableMemoryReallocation " << parameters.enableMemoryReallocation << "\n";

	fastDecoderJ2kHandle_t decoder = 0;
	fastDeviceSurfaceBufferHandle_t buffer;
	r = fastDecoderJ2kCreate(
			&decoder,
			&parameters,
			FAST_RGB8, info.width, info.height,
			max_batch_size,
			&buffer
			);
	if (r != FAST_OK) {
		fastTraceClose ();
		throw FastvideoError ("J2kCreate", r);
	}

	unsigned long long requested_mem_size = 0;

	{
		unsigned long long component_mem_size = 0;
		r = fastDecoderJ2kGetAllocatedGpuMemorySize(decoder, &component_mem_size);
		if (r != FAST_OK) {
			throw FastvideoError ("J2kGetAllocatedGpuMemorySize");
		}
		requested_mem_size += component_mem_size;
	}

	fastExportToHostHandle_t adapter;
	fastSurfaceFormat_t surface_format;
	r = fastExportToHostCreate(&adapter, &surface_format, buffer);
	if (r != FAST_OK) {
		throw FastvideoError ("ExportToHostCreate");
	}

	{
		unsigned int component_mem_size = 0;
		r = fastExportToHostGetAllocatedGpuMemorySize(adapter, &component_mem_size);
		if (r != FAST_OK) {
			throw FastvideoError ("ExportToHostGetAllocatedGpuMemorySize");
		}
		requested_mem_size += component_mem_size;
	}

	const double gigabyte = 1024.0 * 1024.0 * 1024.0;
	printf("\nRequested GPU memory size: %.2lf GB\n", requested_mem_size / gigabyte);

	/* Transform */

	double total_time = 0;
	fastGpuTimerHandle_t device_to_host_timer = 0;
	fastGpuTimerCreate(&device_to_host_timer);

	fastDecoderJ2kReport_t report;

	fastExportParameters_t export_parameters;
	export_parameters.convert = FAST_CONVERT_NONE;

	int aligned_width = info.width * 3;
	aligned_width += 4 - (aligned_width % FAST_ALIGNMENT);
	size_t decoded_size = info.height * aligned_width;
	uint8_t* decoded = 0;
	r = fastMalloc(reinterpret_cast<void**>(&decoded), decoded_size);
	if (r != FAST_OK) {
		throw FastvideoError ("fastMalloc");
	}

	for (int i = 0; i < images_to_convert; ++i) {
		fastDecoderJ2kAddImageToBatch(decoder, data.data().get(), data.size());
		int free_slots = 0;
		fastDecoderJ2kFreeSlotsInBatch(decoder, &free_slots);
		if (free_slots == 0) {
			CHECK_FAST(TransformAndExtractBatch(img, &outputImgs));
		}
	}

	int unprocessed_images_count = 0;
	r = fastDecoderJ2kUnprocessedImagesCount(decoder, &unprocessed_images_count);
	if (unprocessed_images_count > 0) { // Process the last non-complete batch

		CHECK_FAST(TransformAndExtractBatch(img, &outputImgs));
	}

	total_time += report.elapsedTime;

	fastGpuTimerStart(device_to_host_timer);
	r = fastExportToHostCopy(adapter, decoded, info.width, aligned_width, info.height, &export_parameters);
	if (r != FAST_OK) {
		throw FastvideoError ("ExportToHostCopy");
	}

	float elapsed_time_gpu = 0.;
	fastGpuTimerStop(device_to_host_timer);
	fastGpuTimerGetTime(device_to_host_timer, &elapsed_time_gpu);

	total_time += elapsed_time_gpu / 1000.0;

	if (report.codeblockCount > 0) {
		printf("Input image : (%dx%d pixels; %d %d-bit channel(s))\n", info.width, info.height, report.channels, report.bitsPerChannel);
		printf("Tile count: %d (%dx%d)\n", report.tileCount, report.tilesX, report.tilesY);
		printf("Window mode disabled: decode full image\n");
		printf("Resolution levels: %d\n", report.resolutionLevels);
		printf("Codeblock size: %dx%d\n", report.cbX, report.cbY);

		const double megabyte = 1024.0 * 1024.0;
		printf("%7.2f ms 1) Tier-2 time (%d codeblocks)\n", report.s1_tier2 * 1000.0, report.codeblockCount);
		printf("%7.2f ms 2) CPU->GPU copy time (%.2lf MB, %.2lf MB/s)\n",
			report.s2_copy * 1000.0,
			report.copyToGpu_size / megabyte,
			report.copyToGpu_size == 0 ? 0 : (report.copyToGpu_size / report.s2_copy / megabyte));
		printf("%7.2f ms 3) Tier-1 time\n", report.s3_tier1 * 1000.0);
		printf("%7.2f ms 4) DWT time\n", report.s6_dwt * 1000.0);
		printf("%7.2f ms 5) Postprocessing time (MCT, DC-shift)\n", report.s7_postprocessing * 1000.0);
		printf("              Output size = ");
		if (report.outStreamSize < 1024)
			printf("%lld bytes", report.outStreamSize);
		else
			printf("%.0f KB", report.outStreamSize / 1024.0f);
		printf(" (%.1f:1)\n", report.outStreamSize == 0 ? 0 : (float)report.outStreamSize / report.inStreamSize);
	}

	printf("Total decode time for %d images = %.1f ms; %.1f FPS;\n", 1,
		total_time * 1000.0, 1 / total_time);

	fastGpuTimerDestroy(device_to_host_timer);

	/* Close */

	r = fastDecoderJ2kDestroy(decoder);
	if (r != FAST_OK) {
		throw FastvideoError ("J2kDestroy");
	}
	r = fastExportToHostDestroy(adapter);
	if (r != FAST_OK) {
		throw FastvideoError ("ExportToHostDestroy");
	}

	fastFree(decoded);

	return shared_ptr<OpenJPEGImage>();
}