File: FFMPEGDecoder.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (701 lines) | stat: -rw-r--r-- 20,186 bytes parent folder | download | duplicates (2)
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#include <limits>

#include "cutscene/ffmpeg/FFMPEGDecoder.h"

#include "cfile/cfile.h"
#include "libs/ffmpeg/FFmpegContext.h"

#include "cutscene/ffmpeg/internal.h"

#include "cutscene/ffmpeg/AudioDecoder.h"
#include "cutscene/ffmpeg/VideoDecoder.h"
#include "cutscene/ffmpeg/SubtitleDecoder.h"

#include "FFMPEGDecoder.h"
#include "localization/localize.h"

using namespace libs::ffmpeg;

namespace {
using namespace cutscene;
using namespace cutscene::ffmpeg;

class AVPacketScope
{
	AVPacket* _packet;
 public:
	explicit AVPacketScope(AVPacket* av_packet)
		: _packet(av_packet) {
	}

	AVPacketScope(const AVPacketScope&) = delete;
	AVPacketScope& operator=(const AVPacketScope&) = delete;

	~AVPacketScope() {
		av_packet_unref(_packet);
	}
};

const char* CHECKED_EXTENSIONS[] = {"webm", "mp4", "ogg",
                                    "png", // This is designed to be used with APNG animations
                                    "mve"};

const char* CHECKED_SUBT_EXTENSIONS[] = {"srt"};

double getFrameRate(AVStream* stream, AVCodecContext* codecCtx) {
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(58, 3, 102)
	auto fps = av_q2d(stream->r_frame_rate);
#else
	auto fps = av_q2d(av_stream_get_r_frame_rate(stream));
#endif

	if (fps < 0.000001)
	{
		fps = av_q2d(stream->avg_frame_rate);
	}

	if (fps < 0.000001)
	{
		fps = 1.0 / av_q2d(codecCtx->time_base);
	}

	return fps;
}

CodecContextParameters getCodecParameters(AVStream* stream) {
	CodecContextParameters paras;
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(57, 24, 255)
	paras.width        = stream->codecpar->width;
	paras.height       = stream->codecpar->height;
	paras.pixel_format = (AVPixelFormat)stream->codecpar->format;

	#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(59, 36, 255)
		paras.channel_layout = stream->codecpar->ch_layout.u.mask;
	#else
		paras.channel_layout = stream->codecpar->channel_layout;
	#endif
	paras.sample_rate    = stream->codecpar->sample_rate;
	paras.audio_format   = (AVSampleFormat)stream->codecpar->format;

	paras.codec_id = stream->codecpar->codec_id;
#else
	paras.width        = stream->codec->width;
	paras.height       = stream->codec->height;
	paras.pixel_format = stream->codec->pix_fmt;

	paras.channel_layout = stream->codec->channel_layout;
	paras.sample_rate    = stream->codec->sample_rate;
	paras.audio_format   = stream->codec->sample_fmt;

	paras.codec_id = stream->codec->codec_id;
#endif
	return paras;
}

SCP_string normalizeLanguage(const char* langauge_name) {
	if (!stricmp(langauge_name, "eng")) {
		return "English";
	}
	if (!stricmp(langauge_name, "ger")) {
		return "German";
	}
	if (!stricmp(langauge_name, "spa")) {
		return "Spanish";
	}

	// Print to log so that we can find the actual value more easily later
	mprintf(("FFmpeg log: Found unknown language value '%s'!\n", langauge_name));

	// Default to english for everything else
	return "English";
}

/**
 * @brief Given a FFmpeg pixel format returns the format it should be converted to
 * @param in_fmt The input format
 * @return The output format
 */
AVPixelFormat getConversionFormat(AVPixelFormat in_fmt)
{
	switch (in_fmt) {
	case AV_PIX_FMT_RGB24:
	case AV_PIX_FMT_BGR24:
	case AV_PIX_FMT_BGR8:
	case AV_PIX_FMT_BGR4:
	case AV_PIX_FMT_BGR4_BYTE:
	case AV_PIX_FMT_RGB8:
	case AV_PIX_FMT_RGB4:
	case AV_PIX_FMT_RGB4_BYTE:
	case AV_PIX_FMT_RGB48BE:
	case AV_PIX_FMT_RGB48LE:
	case AV_PIX_FMT_RGB565BE:
	case AV_PIX_FMT_RGB565LE:
	case AV_PIX_FMT_RGB555BE:
	case AV_PIX_FMT_RGB555LE:
	case AV_PIX_FMT_BGR565BE:
	case AV_PIX_FMT_BGR565LE:
	case AV_PIX_FMT_BGR555BE:
	case AV_PIX_FMT_BGR555LE:
	case AV_PIX_FMT_RGB444LE:
	case AV_PIX_FMT_RGB444BE:
	case AV_PIX_FMT_BGR444LE:
	case AV_PIX_FMT_BGR444BE:
	case AV_PIX_FMT_BGR48BE:
	case AV_PIX_FMT_BGR48LE:
	case AV_PIX_FMT_0RGB:
	case AV_PIX_FMT_RGB0:
	case AV_PIX_FMT_0BGR:
	case AV_PIX_FMT_BGR0:
		return AV_PIX_FMT_BGR24;

	case AV_PIX_FMT_RGBA64BE:
	case AV_PIX_FMT_RGBA64LE:
	case AV_PIX_FMT_BGRA64BE:
	case AV_PIX_FMT_BGRA64LE:
	case AV_PIX_FMT_ARGB:
	case AV_PIX_FMT_RGBA:
	case AV_PIX_FMT_ABGR:
	case AV_PIX_FMT_BGRA:
		return AV_PIX_FMT_BGRA;

	default:
		// Assume that everything else is converted to YUV 420
		return AV_PIX_FMT_YUV420P;
	}
}

FramePixelFormat getPixelFormat(AVPixelFormat fmt)
{
	switch (fmt) {
	case AV_PIX_FMT_BGR24:
		return FramePixelFormat::BGR;
	case AV_PIX_FMT_BGRA:
		return FramePixelFormat::BGRA;
	default:
		return FramePixelFormat::YUV420;
	}
}
}

namespace cutscene {
namespace ffmpeg {
struct InputStream {
	std::unique_ptr<FFmpegContext> m_ctx;
};

FFMPEGDecoder::FFMPEGDecoder() {
}

FFMPEGDecoder::~FFMPEGDecoder() {
}

namespace {
std::unique_ptr<InputStream> openStream(const SCP_string& name) {
	// Only check the root and movies folders
	int dirType;
	if (cf_exists_full(name.c_str(), CF_TYPE_ROOT)) {
		dirType = CF_TYPE_ROOT;
	} else if (cf_exists_full(name.c_str(), CF_TYPE_MOVIES)) {
		dirType = CF_TYPE_MOVIES;
	} else {
		return nullptr;
	}

	std::unique_ptr<InputStream> input(new InputStream());

	try {
		// Create the FFmpeg context
		input->m_ctx = FFmpegContext::createContext(name, dirType);

		// This may fail, in that case return null
		if (!input->m_ctx) {
			return nullptr;
		}
	}
	catch (const FFmpegException& e) {
		mprintf(("Error opening %s: %s\n", name.c_str(), e.what()));
		return nullptr;
	}

	return input;
}

std::unique_ptr<DecoderStatus> initializeStatus(std::unique_ptr<InputStream>& stream,
                                                std::unique_ptr<InputStream>& subt,
                                                const PlaybackProperties& properties)
{
	if (subt && properties.looping) {
		mprintf(("FFmpeg: External subtitles and looping movies are not supported!\n"));
		return nullptr;
	}

	std::unique_ptr<DecoderStatus> status(new DecoderStatus());

	auto ctx = stream->m_ctx->ctx();

	auto videoStream = av_find_best_stream(ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
	if (videoStream < 0) {
		if (videoStream == AVERROR_STREAM_NOT_FOUND) {
			mprintf(("FFmpeg: No video stream found in file!\n"));
		} else if (videoStream == AVERROR_DECODER_NOT_FOUND) {
			mprintf(("FFmpeg: Codec for video stream could not be found!\n"));
		} else {
			mprintf(("FFmpeg: Unknown error while finding video stream!\n"));
		}

		return nullptr;
	}

	status->videoStreamIndex = videoStream;
	status->videoStream = ctx->streams[videoStream];
	status->videoCodecPars = getCodecParameters(status->videoStream);

	status->videoCodec = avcodec_find_decoder(status->videoCodecPars.codec_id);

	if ( !status->videoCodec ) {
		mprintf(("FFmpeg: Cannot find decoder for video stream!\n"));
		return nullptr;
	}

	if (properties.looping && status->videoCodecPars.codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
		mprintf(("FFmpeg: Looping is not supported for inteplay (MVE) movies!\n"));
		return nullptr;
	}

	int audioStream = -1;

	if (properties.with_audio) {
		audioStream = av_find_best_stream(ctx, AVMEDIA_TYPE_AUDIO, -1, videoStream, nullptr, 0);
		if (audioStream < 0) {
			if (audioStream == AVERROR_STREAM_NOT_FOUND) {
				mprintf(("FFmpeg: No audio stream found in file!\n"));
			} else if (audioStream == AVERROR_DECODER_NOT_FOUND) {
				mprintf(("FFmpeg: Codec for audio stream could not be found!\n"));
			} else {
				mprintf(("FFmpeg: Unknown error while finding audio stream!\n"));
			}
		} else {
			status->audioStreamIndex = audioStream;
			status->audioStream = ctx->streams[audioStream];
			status->audioCodecPars = getCodecParameters(status->audioStream);

			status->audioCodec = avcodec_find_decoder(status->audioCodecPars.codec_id);

			if ( !status->audioCodec ) {
				mprintf(("FFmpeg: Cannot find decoder for audio stream!\n"));
				status->audioStreamIndex = -1;
				audioStream = -1;
			}
		}
	}

	if (subt) {
		auto subtCtx = subt->m_ctx->ctx();

		auto subtStream = av_find_best_stream(subtCtx, AVMEDIA_TYPE_SUBTITLE, -1, -1, nullptr, 0);
		if (subtStream < 0) {
			if (subtStream == AVERROR_STREAM_NOT_FOUND) {
				mprintf(("FFmpeg: No subtitle stream found in subtitle file!\n"));
			} else if (subtStream == AVERROR_DECODER_NOT_FOUND) {
				mprintf(("FFmpeg: Codec for subtitle stream could not be found!\n"));
			} else {
				mprintf(("FFmpeg: Unknown error while finding subtitle stream!\n"));
			}
		} else {
			status->subtitleStreamIndex = subtStream;
			status->subtitleStream      = subtCtx->streams[subtStream];
			status->externalSubtitles   = true;
			// Not really sure if this makes sense for subtitles but it shouldn't break anything...
			status->subtitleCodecPars = getCodecParameters(status->subtitleStream);

			status->subtitleCodec = avcodec_find_decoder(status->subtitleCodecPars.codec_id);
		}
	}

	if (status->subtitleStream == nullptr) {
		// We don't have an external subtitle stream or loading from that file has failed
		auto& current_language = Lcl_languages[lcl_get_current_lang_index()];
		for (uint32_t i = 0; i < ctx->nb_streams; ++i) {
			auto test_stream = ctx->streams[i];

#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(57, 24, 255)
			auto& pars = test_stream->codecpar;

			if (!(pars->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
				continue;
			}
#endif
			AVDictionaryEntry* tag = nullptr;
			if ((tag = av_dict_get(test_stream->metadata, "language", nullptr, 0)) == nullptr) {
				continue;
			}

			auto normalized_language = normalizeLanguage(tag->value);

			if (!stricmp(normalized_language.c_str(), current_language.lang_name)) {
				status->subtitleStreamIndex = i;
				status->subtitleStream      = test_stream;
				break;
			}
		}
	}

	int err;
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(57, 24, 255)
	status->videoCodecCtx = avcodec_alloc_context3(status->videoCodec);

	// Copy codec parameters from input stream to output codec context
	err = avcodec_parameters_to_context(status->videoCodecCtx, status->videoStream->codecpar);
	if (err < 0) {
		char errorStr[512];
		av_strerror(err, errorStr, sizeof(errorStr));
		mprintf(("FFMPEG: Failed to copy context parameters! Error: %s\n", errorStr));
		return nullptr;
	}
#else
	status->videoCodecCtx = status->videoStream->codec;
#endif

	err = avcodec_open2(status->videoCodecCtx, status->videoCodec, nullptr);
	if (err < 0) {
		char errorStr[512];
		av_strerror(err, errorStr, sizeof(errorStr));
		mprintf(("FFMPEG: Failed to open video codec! Error: %s\n", errorStr));
		return nullptr;
	}

	mprintf(("FFmpeg: Using video codec %s (%s).\n", status->videoCodec->long_name ? status->videoCodec->long_name : "<Unknown>",
		status->videoCodec->name ? status->videoCodec->name : "<Unknown>"));

	// Now initialize audio, if this fails it's not a fatal error
	if (audioStream >= 0) {
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(57, 24, 255)
		status->audioCodecCtx = avcodec_alloc_context3(status->audioCodec);

		err = avcodec_parameters_to_context(status->audioCodecCtx, status->audioStream->codecpar);
		if (err < 0) {
			char errorStr[512];
			av_strerror(err, errorStr, sizeof(errorStr));
			mprintf(("FFMPEG: Failed to copy context parameters! Error: %s\n", errorStr));
			return nullptr;
		}
#else
		status->audioCodecCtx = status->audioStream->codec;
#endif

		err = avcodec_open2(status->audioCodecCtx, status->audioCodec, nullptr);
		if (err < 0) {
			char errorStr[512];
			av_strerror(err, errorStr, sizeof(errorStr));
			mprintf(("FFMPEG: Failed to open audio codec! Error: %s\n", errorStr));
		}

		mprintf(("FFmpeg: Using audio codec %s (%s).\n", status->audioCodec->long_name ? status->audioCodec->long_name : "<Unknown>",
			status->audioCodec->name ? status->audioCodec->name : "<Unknown>"));
	}

	if (status->subtitleStream != nullptr) {
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(57, 24, 255)
		status->subtitleCodecCtx = avcodec_alloc_context3(status->subtitleCodec);

		err = avcodec_parameters_to_context(status->subtitleCodecCtx, status->subtitleStream->codecpar);
		if (err < 0) {
			char errorStr[512];
			av_strerror(err, errorStr, sizeof(errorStr));
			mprintf(("FFMPEG: Failed to copy context parameters! Error: %s\n", errorStr));
			return nullptr;
		}
#else
		status->subtitleCodecCtx = status->subtitleStream->codec;
#endif

		err = avcodec_open2(status->subtitleCodecCtx, status->subtitleCodec, nullptr);
		if (err < 0) {
			char errorStr[512];
			av_strerror(err, errorStr, sizeof(errorStr));
			mprintf(("FFMPEG: Failed to open subtitle codec! Error: %s\n", errorStr));
		}

		mprintf(("FFmpeg: Using subtitle codec %s (%s).\n",
		         status->subtitleCodec->long_name ? status->subtitleCodec->long_name : "<Unknown>",
		         status->subtitleCodec->name ? status->subtitleCodec->name : "<Unknown>"));
	}

	return status;
}

std::unique_ptr<InputStream> openInputStream(const SCP_string& name) {
	// Check a list of extensions we might use
	// The actual format of the file may be whatever FFmpeg supports
	for (auto ext : CHECKED_EXTENSIONS) {
		auto fileName = name + "." + ext;

		auto input = openStream(fileName);

		if (input) {
			return input;
		}
	}

	return nullptr;
}

std::unique_ptr<InputStream> openSubtitleStream(const SCP_string& name)
{
	auto& current_language = Lcl_languages[lcl_get_current_lang_index()];

	for (auto ext : CHECKED_SUBT_EXTENSIONS) {
		SCP_string fileName;
		if (strlen(current_language.lang_ext) == 0) {
			fileName = name + "." + ext;
		} else {
			fileName = name + "-" + current_language.lang_ext + "." + ext;
		}

		auto input = openStream(fileName);

		if (input) {
			return input;
		}
	}

	return nullptr;
}
}

bool FFMPEGDecoder::initialize(const SCP_string& fileName, const PlaybackProperties& properties)
{
	SCP_string movieName = fileName;
	// First make the file name lower case
	SCP_tolower(movieName);

	// Then remove the extension
	size_t dotPos = movieName.find('.');
	if (dotPos != SCP_string::npos) {
		movieName.resize(dotPos);
	}

	// Try to open the input stream
	auto input = openInputStream(movieName);
	if (!input) {
		return false;
	}

	auto subt = openSubtitleStream(movieName);

	// We now have a valid input stream, try to find the correct streams
	auto status = initializeStatus(input, subt, properties);
	if (!status) {
		return false;
	}

	// Buffer ~ 2 seconds of video and audio
	initializeQueues(static_cast<size_t>(ceil(getFrameRate(status->videoStream, status->videoCodecCtx))) * 2);

	// We're done, now just put the pointer into this
	std::swap(m_input, input);
	std::swap(m_subtitleInput, subt);
	std::swap(m_status, status);
	m_properties = properties;
	return true;
}

MovieProperties FFMPEGDecoder::getProperties() const
{
	MovieProperties props;
	props.size.width = static_cast<size_t>(m_status->videoCodecPars.width);
	props.size.height = static_cast<size_t>(m_status->videoCodecPars.height);

	props.fps = static_cast<float>(getFrameRate(m_status->videoStream, m_status->videoCodecCtx));
	props.duration = static_cast<float>(m_status->videoStream->duration * av_q2d(m_status->videoStream->time_base));

	props.pixelFormat = getPixelFormat(getConversionFormat(m_status->videoCodecPars.pixel_format));

	return props;
}

void FFMPEGDecoder::startDecoding() {
	std::unique_ptr<VideoDecoder> videoDecoder(new VideoDecoder(m_status.get(), getConversionFormat(m_status->videoCodecPars.pixel_format)));

	std::unique_ptr<AudioDecoder> audioDecoder;
	std::unique_ptr<SubtitleDecoder> subtitleDecoder;

	if (hasAudio()) {
		audioDecoder.reset(new AudioDecoder(m_status.get()));
	}
	if (hasSubtitles()) {
		subtitleDecoder.reset(new SubtitleDecoder(m_status.get()));
	}

	std::unique_ptr<std::thread> subtitle_thread;
	if (hasExternalSubtitle()) {
		// Start a new thread for the subtitles since otherwise we would need to make sure that neither of the input
		// streames starves while the other waits for more space in the queues
		subtitle_thread.reset(
		    new std::thread([this, &subtitleDecoder]() { runSubtitleDecoder(subtitleDecoder.get()); }));
	}

	auto ctx = m_input->m_ctx->ctx();
	AVPacket packet;
	do {
		if (m_properties.looping) {
			// Only seek if we are looping the movie. The MVE code doesn't support seeking to this function may not be
			// called there
			auto seek_err = av_seek_frame(ctx, -1, 0, AVSEEK_FLAG_BACKWARD);
			if (seek_err < 0) {
				char errorStr[512];
				av_strerror(seek_err, errorStr, sizeof(errorStr));
				mprintf(("FFMPEG: Failed to seek to start of movie file! Error: %s\n", errorStr));
				break;
			}
		}

		videoDecoder->flushBuffers();
		if (audioDecoder) {
			audioDecoder->flushBuffers();
		}
		if (subtitleDecoder) {
			subtitleDecoder->flushBuffers();
		}

		while (isDecoding()) {
			auto read_err = av_read_frame(ctx, &packet);
			AVPacketScope scope(&packet);

			if (read_err < 0) {
				if (read_err == AVERROR_EOF) {
					// Finished reading -> break out of loop
					break;
				} else if (avio_feof(ctx->pb) != 0) {
					// Also EOF!
					break;
				} else {
					// Some kind of other error, try to continue reading
					char errorStr[512];
					av_strerror(read_err, errorStr, sizeof(errorStr));
					mprintf(("FFMPEG: Failed to read frame! Error: %s\n", errorStr));

					// Skip packet
					continue;
				}
			}

			if (packet.stream_index == m_status->videoStreamIndex) {
				videoDecoder->decodePacket(&packet);

				VideoFramePtr ptr;
				while ((ptr = videoDecoder->getFrame()) != nullptr) {
					pushFrameData(std::move(ptr));
				}
			} else if (audioDecoder && packet.stream_index == m_status->audioStreamIndex) {
				audioDecoder->decodePacket(&packet);

				AudioFramePtr ptr;
				while ((ptr = audioDecoder->getFrame()) != nullptr) {
					pushAudioData(std::move(ptr));
				}
			} else if (subtitleDecoder && packet.stream_index == m_status->subtitleStreamIndex) {
				subtitleDecoder->decodePacket(&packet);

				SubtitleFramePtr ptr;
				while ((ptr = subtitleDecoder->getFrame()) != nullptr) {
					pushSubtitleData(std::move(ptr));
				}
			}
		}

		if (isDecoding()) {
			// If we are still alive then read the last frames from the decoders
			videoDecoder->finishDecoding();
			VideoFramePtr video_ptr;
			while ((video_ptr = videoDecoder->getFrame()) != nullptr) {
				pushFrameData(std::move(video_ptr));
			}

			if (audioDecoder) {
				audioDecoder->finishDecoding();
				AudioFramePtr audio_frame;
				while ((audio_frame = audioDecoder->getFrame()) != nullptr) {
					pushAudioData(std::move(audio_frame));
				}
			}
		}
		// If the video is being looped then we start again from the beginning
	} while (m_properties.looping && isDecoding());

	// Stop the decoder first to let the subtitle thread know that it shouldn't continue
	stopDecoder();

	if (subtitle_thread) {
		subtitle_thread->join();
	}
}

bool FFMPEGDecoder::hasAudio() const { return m_status->audioStreamIndex >= 0; }

bool FFMPEGDecoder::hasSubtitles() const { return m_status->subtitleStream != nullptr; }

bool FFMPEGDecoder::hasExternalSubtitle() const { return m_status->externalSubtitles; }

void FFMPEGDecoder::close() {
	if (m_status) {
		m_status = nullptr;
	}

	if (m_input) {
		// This will delete the InputStream pointer and free all data
		m_input = nullptr;
	}

	if (m_subtitleInput) {
		m_subtitleInput = nullptr;
	}
}
void FFMPEGDecoder::runSubtitleDecoder(SubtitleDecoder* decoder)
{
	auto ctx = m_subtitleInput->m_ctx->ctx();
	AVPacket packet;

	while (isDecoding()) {
		auto read_err = av_read_frame(ctx, &packet);
		AVPacketScope scope(&packet);

		if (read_err < 0) {
			if (read_err == AVERROR_EOF) {
				// Finished reading -> break out of loop
				break;
			} else if (avio_feof(ctx->pb) != 0) {
				// Also EOF!
				break;
			} else {
				// Some kind of other error, try to continue reading
				char errorStr[512];
				av_strerror(read_err, errorStr, sizeof(errorStr));
				mprintf(("FFMPEG: Failed to read frame! Error: %s\n", errorStr));

				// Skip packet
				continue;
			}
		}

		if (packet.stream_index == m_status->subtitleStreamIndex) {
			decoder->decodePacket(&packet);

			SubtitleFramePtr ptr;
			while ((ptr = decoder->getFrame()) != nullptr) {
				pushSubtitleData(std::move(ptr));
			}
		}
	}
	if (isDecoding()) {
		// If we are still alive then read the last frames from the decoders
		decoder->finishDecoding();

		SubtitleFramePtr ptr;
		while ((ptr = decoder->getFrame()) != nullptr) {
			pushSubtitleData(std::move(ptr));
		}
	}
}
}
}