File: SubtitleDecoder.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 (138 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download
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
//
//

#include "SubtitleDecoder.h"

namespace {

double getFrameTime(int64_t pts, AVRational time_base) {
	return pts * av_q2d(time_base);
}

}

namespace cutscene {
namespace ffmpeg {

SubtitleDecoder::SubtitleDecoder(cutscene::ffmpeg::DecoderStatus* status) : FFMPEGStreamDecoder(status) {
}
SubtitleDecoder::~SubtitleDecoder() {

}
void SubtitleDecoder::decodePacket(AVPacket* packet) {
	int finishedFrame = 0;
	AVSubtitle subtitle;
	auto result = avcodec_decode_subtitle2(m_status->subtitleCodecCtx, &subtitle, &finishedFrame, packet);

	if (result >= 0 && finishedFrame) {
		pushSubtitleFrame(packet, &subtitle);

		avsubtitle_free(&subtitle);
	}
}
void SubtitleDecoder::finishDecoding() {
	// Handle those decoders that have a delay
	AVPacket nullPacket;
	memset(&nullPacket, 0, sizeof(nullPacket));
	nullPacket.data = nullptr;
	nullPacket.size = 0;

	AVSubtitle subtitle;
	while (true) {
		int finishedFrame = 1;
		auto err = avcodec_decode_subtitle2(m_status->subtitleCodecCtx, &subtitle, &finishedFrame, &nullPacket);

		if (err < 0 || !finishedFrame) {
			break;
		}

		pushSubtitleFrame(&nullPacket, &subtitle);

		avsubtitle_free(&subtitle);
	}
}
void SubtitleDecoder::pushSubtitleFrame(AVPacket* packet, AVSubtitle* subtitle) {
	if (subtitle->format != 1) {
		// Non-text subtitles are not supported yet.
		mprintf(("FFmpeg: Detected a non-text subtitle! This is not supported yet!\n"));
		return;
	}
	if (subtitle->num_rects < 1) {
		return;
	}

	int64_t pts = subtitle->pts;
	if (pts == AV_NOPTS_VALUE) {
		pts = packet->pts;
	}
	if (pts == AV_NOPTS_VALUE) {
		// Still no valid timestamp...
		return;
	}
	auto packet_time = getFrameTime(pts, m_status->subtitleStream->time_base);

	auto start_time = packet_time + (subtitle->start_display_time / 1000.0);
	auto end_time = packet_time + (subtitle->end_display_time / 1000.0);
	if (subtitle->end_display_time == 0 && m_status->subtitleStream->time_base.num != 0) {
		end_time = packet_time + getFrameTime(packet->duration, m_status->subtitleStream->time_base);
	}

	SCP_string processed_text;
	// For now we only use the first subtitle rectangle
	auto subtitle_rect = subtitle->rects[0];
	if (subtitle_rect->type == SUBTITLE_BITMAP) {
		// Same as above, non-text subtitles are not supported yet.
		mprintf(("FFmpeg: Detected a non-text subtitle! This is not supported yet!\n"));
		return;
	} else if (subtitle_rect->type == SUBTITLE_TEXT) {
		// Subtitle does not need to be processed any further
		processed_text = subtitle_rect->text;
	} else if (subtitle_rect->type == SUBTITLE_ASS) {
		SCP_string ass_text = subtitle_rect->ass;
		
		// We are not interested in any of the other information contained in the ASS line but we still need to figure
		// out where our text starts
		auto comma_pos = ass_text.find(',');
		for (auto i = 0; i < 8 && comma_pos != SCP_string::npos; ++i) {
			comma_pos = ass_text.find(',', comma_pos + 1);
		}
		Assertion(comma_pos != SCP_string::npos, "Received an ill-formed ASS line from FFmpeg! Text was '%s'.", subtitle_rect->ass);

		// This + 1 is safe since comma_pos points to a valid character so the next character is at worst the end of the string
		processed_text = ass_text.substr(comma_pos + 1);

		// ASS has a special sequence for new lines that needs to be converted to an actual new line
		auto newline_pos = processed_text.find("\\N");
		while(newline_pos != SCP_string::npos) {
			processed_text.replace(newline_pos, 2, "\n");

			newline_pos = processed_text.find("\\N");
		}
	} else {
		mprintf(("FFmpeg: Detected unknown subtitle name in movie!\n"));
		return;
	}

	// Remove \r from string. Some subtitles use \r\n for line breaks and we only support \n
	auto ret_pos = processed_text.find('\r');
	while(ret_pos != SCP_string::npos) {
		processed_text.erase(ret_pos, 1);

		ret_pos = processed_text.find('\r');
	}

	SubtitleFramePtr frame(new SubtitleFrame());
	frame->text = std::move(processed_text);

	frame->displayStartTime = start_time;
	frame->displayEndTime = end_time;

	pushFrame(std::move(frame));
}
void SubtitleDecoder::flushBuffers() {
	avcodec_flush_buffers(m_status->subtitleCodecCtx);
}

}
}