Skip to content
Snippets Groups Projects
EncoderQueue.h 11.4 KiB
Newer Older
dnewman-gpsw's avatar
dnewman-gpsw committed
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
/*! @file EncoderQueue.h

*  @brief
*
*  @version 1.0.0
*
*  (C) Copyright 2017 GoPro Inc (http://gopro.com/).
*
*  Licensed under either:
*  - Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0  
*  - MIT license, http://opensource.org/licenses/MIT
*  at your option.
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
*/


#pragma once

/*! @file EncoderQueue.h

	@brief Declaration of encoder jobs and the job queue for asynchronous encoders

	Each asynchronous encoder has a message queue.  The encoder pool creates an encoder
	job for each encoding request and adds the encoder job to the message queue for the
	asynchronous encoder that is assigned to encode the frame.
*/

// Forward reference
class EncoderJobQueue;

/*!
	@brief Status of an encoder job

	Every encoder job must be in one of three states: the input frame is
	waiting to be assigned to an encoder, the frame has been assigned to
	an encoder, or the frame has been encoded and the encoded sample is
	ready to be delivered.
*/
enum EncoderJobStatus
{
	ENCODER_JOB_STATUS_UNKNOWN = 0,		//!< Encoder job status is not known
	ENCODER_JOB_STATUS_UNASSIGNED,		//!< Job has not been assigned to an encoder
	ENCODER_JOB_STATUS_ENCODING,		//!< Encoding is in progress
	ENCODER_JOB_STATUS_FINISHED,		//!< The encoded sample is ready
};

/*!
	@brief Data structure for an encoder job

	Every encoder job has a status that is one of the valid states
	in the @ref EncoderJobStatus enumeration, the frame buffer and
	pitch of the input frame to encode, and a pointer to the sample
	buffer for the encoded sample.

	@todo Add support for non-key frames.
*/
struct EncoderJob
{
	friend class EncoderJobQueue;
	friend class CEncoderPool;
	friend class CAsyncEncoder;

	EncoderJob() :
		status(ENCODER_JOB_STATUS_UNKNOWN),
		error(CFHD_ERROR_OKAY),
		frameNumber(0),
		frameBuffer(NULL),
		framePitch(0),
		keyFrame(true),
		sampleBuffer(NULL),
		encoderMetadata(NULL)
	{
	}

	EncoderJob(uint32_t frameNumber,
			   void *frameBuffer,
			   ptrdiff_t framePitch,
			   bool keyFrame = true,
			   CSampleEncodeMetadata *encoderMetadata = NULL,
			   CFHD_EncodingQuality frameQuality = CFHD_ENCODING_QUALITY_FIXED) :
		status(ENCODER_JOB_STATUS_UNASSIGNED),
		error(CFHD_ERROR_OKAY),
		frameNumber(frameNumber),
		frameBuffer(frameBuffer),
		framePitch(framePitch),
		frameQuality(frameQuality),
		keyFrame(keyFrame),
		sampleBuffer(NULL),
		encoderMetadata(encoderMetadata)
	{
	}

	EncoderJob(const EncoderJob &job)
	{
		status = job.status;
		error = job.error;
		frameNumber = job.frameNumber;
		frameBuffer = job.frameBuffer;
		framePitch = job.framePitch;;
		frameQuality = job.frameQuality;
		keyFrame = job.keyFrame;
		sampleBuffer = job.sampleBuffer;
		encoderMetadata = job.encoderMetadata;
	}

	EncoderJob& operator= (const EncoderJob &job)
	{
		status = job.status;
		error = job.error;
		frameNumber = job.frameNumber;
		frameBuffer = job.frameBuffer;
		framePitch = job.framePitch;
		frameQuality = job.frameQuality;
		keyFrame = job.keyFrame;
		sampleBuffer = job.sampleBuffer;
		encoderMetadata = job.encoderMetadata;
		return *this;
	}

	~EncoderJob()
	{
		//DebugOutput("Deleting encoder job\n");
		if (sampleBuffer) {
			delete sampleBuffer;
			sampleBuffer = NULL;
		}

		if (encoderMetadata) {
			delete encoderMetadata;
			encoderMetadata = NULL;
		}
	}

	/*!
		@brief Get the sample buffer from the job

		The sample buffer in the encoder job is set to null so that it
		is not deleted when the encoder job is deleted.  After calling
		this method, the sample buffer belongs to the caller and the
		caller is responsible for releasing it.
	*/
	CSampleBuffer *GetSampleBuffer()
	{
		CSampleBuffer *sample = sampleBuffer;
		sampleBuffer = NULL;
		//DebugOutput("Encoder job returning sample: %p\n", sample);
		return sample;
	}

	EncoderJobStatus status;			//!< Status of the encoding job
	CFHD_Error error;					//!< Error code from the sample encoder
	uint32_t frameNumber;				//!< Frame number the identifies the encoding job
	void *frameBuffer;					//!< Address of the frame to encode
	ptrdiff_t framePitch;				//!< Pitch of the frame buffer (in bytes)
	bool keyFrame;						//!< True if this is the first frame in a GOP
	CFHD_EncodingQuality frameQuality;	//!< Compression quality override if non-zero

	//! Metadata that will be attached to the encoded sample for this frame
	CSampleEncodeMetadata *encoderMetadata;

private:

	CSampleBuffer *sampleBuffer;		//!< Buffer that contains the encoded sample

};

/*!
	@brief Use a counting semaphore to track usage of some resource
*/
class CResourceCounter
{
#ifdef _WINDOWS

public:

	CResourceCounter(long limit) :
		handle(NULL)
	{
		handle = CreateSemaphore(NULL, 0, limit, NULL);
		assert(handle != NULL);
	}

	~CResourceCounter()
	{
		CloseHandle(handle);
		handle = NULL;
	}

	int Wait()
	{
		// Wait for the semaphore indefinitely
		return WaitForSingleObject(handle, INFINITE);
	}

	int Release(LONG amount = 1)
	{
		ReleaseSemaphore(handle, amount, NULL);
		return 0;
	}

private:

	HANDLE handle;

#else

public:

	CResourceCounter(long limit)
	{
		assert(limit > 0);
		//sem_init(&sema, 0, limit);
		pthread_mutex_init(&mutex, NULL);
		pthread_cond_init(&cond, NULL);
		count = limit;
	}

	~CResourceCounter()
	{
		//sem_destroy(&sema);
		pthread_cond_destroy(&cond);
		pthread_mutex_destroy(&mutex);
	}

	int Wait()
	{
		// Wait for the semaphore indefinitely
		//return sem_wait(&sema);
		pthread_mutex_lock(&mutex);
		while (! (count > 0)) {
			pthread_cond_wait(&cond, &mutex);
		}
		count--;
		assert(count >= 0);
		pthread_mutex_unlock(&mutex);
		return 0;
	}

	int Release(size_t amount = 1)
	{
#if 0
		for (; amount > 0; amount--) {
			sem_post(&sema);
		}
#else
		// Increment the resource count
		pthread_mutex_lock(&mutex);
		count += amount;
		pthread_mutex_unlock(&mutex);

		// Signal waiting threads that the count has changed
		pthread_cond_signal(&cond);
#endif
		return 0;
	}

private:

	//sem_t sema;

	pthread_mutex_t mutex;
	pthread_cond_t cond;
	size_t count;

#endif

};

/*!
	@class EncoderJobQueue
*/
class EncoderJobQueue
{
protected:

	typedef std::deque<EncoderJob *> JobQueue;

	static const size_t DEFAULT_QUEUE_LENGTH = 1024;

public:

	EncoderJobQueue(size_t length) :
		available(length)
	{
		// Allocate a job queue of the specified size
		//queue.resize(encoderJobQueueSize);
		assert(length > 0);
		if (! (length > 0)) {
			available = DEFAULT_QUEUE_LENGTH;
		}
	}

	~EncoderJobQueue()
	{
		// Delete all of the jobs in the queue
		while (!queue.empty())
		{
			EncoderJob *job = queue.front();
			queue.pop_front();
			delete job;
		}
	}

	//! Add an encoding job to the end of the queue
	CFHD_Error AddEncoderJob(EncoderJob *job)
	{
		// Available space in the encoder job queue?
		CAutoLock lock(mutex);
		while (available == 0)
		{
			// Wait until there is space in the queue
			space.Wait(mutex);
		}
		assert(available > 0);

		// Add the encoder job to the end of the queue
		queue.push_back(job);

		// Decrease the amount of space in the encoder job queue
		available--;

		return CFHD_ERROR_OKAY;
	}

	EncoderJob *WaitForFinishedJob()
	{
		// Has the next encoding job in the queue finished?
		CAutoLock lock(mutex);
		EncoderJob *job = queue.size() > 0 ? queue.front() : NULL;
		while (job == NULL || job->status != ENCODER_JOB_STATUS_FINISHED)
		{
			// Wait until the next encoding job has finished
			ready.Wait(mutex);
			job = queue.size() > 0 ? queue.front() : NULL;
		}

		// Remove the encoding job from the front of the queue
		queue.pop_front();

		// Increase the amount of space in the encoder job queue
		available++;

		// Return the encoder job with the next encoded sample
		return job;
	}

	EncoderJob *TestForFinishedJob()
	{
		// Has the next encoding job in the queue finished?
		CAutoLock lock(mutex);
		EncoderJob *job = queue.size() > 0 ? queue.front() : NULL;
		if (job == NULL || job->status != ENCODER_JOB_STATUS_FINISHED) {
			return NULL;
		}

		// Remove the encoding job from the front of the queue
		queue.pop_front();

		// Increase the amount of space in the encoder job queue
		available++;

		// Return the encoder job with the next encoded sample
		return job;
	}

	void SignalJobFinished()
	{
		// Wake a thread that is waiting for an encoded sample
		ready.Wake();
	}

	//! Get the next encoded sample from the job queue
	CFHD_Error GetEncodedSample(uint32_t *frameNumberOut,
								CSampleBuffer **sampleBufferOut)
	{
		CFHD_Error error = CFHD_ERROR_OKAY;

		// Wait for the encoder to finish encoding the next job in the queue
		EncoderJob *job = WaitForFinishedJob();
		if (job == NULL) {
			return CFHD_ERROR_UNEXPECTED;
		}

		if (frameNumberOut != NULL && sampleBufferOut != NULL)
		{
			*frameNumberOut = job->frameNumber;
			*sampleBufferOut = job->sampleBuffer;
			error = CFHD_ERROR_OKAY;
		}
		else
		{
			error = CFHD_ERROR_INVALID_ARGUMENT;
		}

		// Free the encoder job and return
		delete job;
		return error;
	}

private:

	//! Array of encoder jobs in the queue
	JobQueue queue;

	//! Amount of available space in the encoder job queue
	size_t available;

	//! Wait until space is available in the encoder job queue
	ConditionVariable space;

	//! Wait until the next encoder job in the queue has finished
	ConditionVariable ready;

	//! Exclusive access to the encoder job queue
	CSimpleLock mutex;

};

/*!
	@brief Definition of the payload of messages sent to an encoder

	The encoder message contains a pointer to the encoder job that
	specifies the frame to be encoded.
*/

class EncoderMessage : public ThreadMessage
{
public:

	EncoderMessage() :
		ThreadMessage(ThreadMessage::THREAD_COMMAND_NULL),
		encoderJob(NULL)
	{
	}

	EncoderMessage(ThreadMessage::ThreadCommand command) :
		ThreadMessage(command),
		encoderJob(NULL)
	{
	}

	EncoderMessage(EncoderJob *job) :
		ThreadMessage(THREAD_COMMAND_ENCODE),
		encoderJob(job)
	{
	}

	// Must define a copy constructor for this class and the base class
	EncoderMessage(const EncoderMessage& message) :
		ThreadMessage(message),
		encoderJob(message.encoderJob)
	{
	}

	// Must define an assignment operator for this class and the base class
	const EncoderMessage& operator= (const EncoderMessage& message)
	{
		if (&message != this)
		{
			encoderJob = message.encoderJob;
			ThreadMessage::operator=(message);
		}
		return *this;
	}

	EncoderJob *Job()
	{
		return encoderJob;
	}

	void Job(EncoderJob *job)
	{
		encoderJob = job;
	}

private:

	EncoderJob *encoderJob;
};

/*! @class CEncoderMessageQueue

	@brief Each worker thread has its own message queue

	Each worker thread has a unique sample encoder and its own
	message queue.  All encoder jobs in the same GOP are added
	to the message queue for the same encoder so that the
	encoder state is maintained between frames in the GOP.
*/
typedef class MessageQueue<EncoderMessage> CEncoderMessageQueue;