summaryrefslogtreecommitdiffstats
path: root/dvdimagejob.cpp
blob: ebc818a88ff021dd1772a89d706da6934462f348 (plain) (blame)
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
#include "dvdimagejob.h"
#include "dvddrive.h"
#include "dvdimagejobgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <dvdcss/dvdcss.h>
#include <dvdread/dvd_reader.h>
#include <dvdread/dvd_udf.h>
#include <QDebug>
#include <QIODevice>
#include <QFile>

DVDImageJob::DVDImageJob(Video *video)
		: Job(video, true),
		m_terminate(false)
{
}

DVDImageJob::~DVDImageJob()
{
	disconnect(this, 0, 0, 0);
	terminate();
}

int DVDImageJob::cmpvob(const void *p1, const void *p2)
{
	vobfile *v1 = (vobfile*)p1;
	vobfile *v2 = (vobfile*)p2;
	if (v1->start < v2->start)
		return -1;
	else if (v1->start > v2->start)
		return 1;
	else
		return 0;
}

bool DVDImageJob::executeJob()
{
	return saveImageToPath(video()->imagePath());
}

Video::Jobs DVDImageJob::jobType() const
{
	return Video::DVDImage;
}

bool DVDImageJob::saveImageToPath(const QString &path)
{
	QFile file(path);
	file.open(QFile::WriteOnly);
	bool ret = saveImageToDevice(file);
	file.close();
	if (!ret)
		file.remove();
	return ret;
}

// Much of the CSS logic comes from Bryan Ford's Netsteria
// http://www.google.com/codesearch/p?hl=en&sa=N&cd=10&ct=rc#PY4_fj37fsw/uia/netsteria/dvd/read.cc&q=DVDCSS_SEEK_KEY
bool DVDImageJob::saveImageToDevice(QIODevice &out)
{
	m_locker.lockForRead();
	if (m_terminate) {
		m_locker.unlock();
		return false;
	}
	m_locker.unlock();
	QString dvdDevice = DVDDrive::instance()->dvdDevice();
	dvd_reader_t *dvdr = DVDOpen(dvdDevice.toStdString().c_str());
	if (!dvdr) {
		qDebug() << "can't open DVD (dvdread)";
		return false;
	}
	m_locker.lockForRead();
	if (m_terminate) {
		m_locker.unlock();
		return false;
	}
	m_locker.unlock();
	// Find the extents of all the potentially-encrypted VOB files	
	uint32_t discend = 0;
	vobfile vobs[100*10];
	int nvobs = 0;
	const int NBLOCKS = 16;
	char buf[DVDCSS_BLOCK_SIZE * NBLOCKS];
	for (int i = 0; i < 100; i++) {
		// Find the IFO and BUP files for this titleset,
		// just to make sure hiblock accounts for them.
		for (int j = 0; j < 2; j++) {
			m_locker.lockForRead();
			if (m_terminate) {
				m_locker.unlock();
				return false;
			}
			m_locker.unlock();
			char filename[30];
			const char *ext = j ? "BUP" : "IFO";
			if (i == 0) {
				sprintf(filename, "/VIDEO_TS/VIDEO_TS.%s", ext);
			} else {
				sprintf(filename, "/VIDEO_TS/VTS_%02d_0.%s", i, ext);
			}
			uint32_t size;
			uint32_t start = UDFFindFile(dvdr, filename, &size);
			if (start == 0 || size == 0)
				break;
			uint32_t end = start + (size + 2047) / DVDCSS_BLOCK_SIZE;
			qDebug() << ext << "at blocks" << start << "-" << end << "(size" << size << "bytes)";
			if (discend < end)
				discend = end;
		}

		// Find each VOB part for decryption
		for (int j = 0; j < 10; j++) {
			m_locker.lockForRead();
			if (m_terminate) {
				m_locker.unlock();
				return false;
			}
			m_locker.unlock();
			char filename[30];
			if (i == 0) {
				if (j > 0)
					break;  // VIDEO_TS.VOB has only 1 part
				sprintf(filename, "/VIDEO_TS/VIDEO_TS.VOB");
			} else {
				sprintf(filename, "/VIDEO_TS/VTS_%02d_%d.VOB", i, j);
			}
			uint32_t size;
			uint32_t start = UDFFindFile(dvdr, filename, &size);
			if (start == 0 || size == 0)
				break;  // No more parts in this title set
			uint32_t end = start + (size + (DVDCSS_BLOCK_SIZE - 1)) / DVDCSS_BLOCK_SIZE;

			qDebug() << "VOB at blocks" << start << "-" << end << "(size" << size << "bytes)";
			vobs[nvobs].start = start;
			vobs[nvobs].end = end;
			if (discend < end)
				discend = end;
			nvobs++;
		}
	}
	m_locker.lockForRead();
	if (m_terminate) {
		m_locker.unlock();
		return false;
	}
	m_locker.unlock();
	qsort(&vobs, nvobs, sizeof(vobfile), cmpvob);
	vobs[nvobs].start = vobs[nvobs].end = INT_MAX;

	DVDClose(dvdr);
	dvdr = NULL;

	dvdcss_t dcss = dvdcss_open((char*)dvdDevice.toStdString().c_str());
	if (!dcss) {
		qDebug() << "can't open DVD (dvdcss)";
		return false;
	}
	int blkno = 0;
	int curvob = 0;
	while (1) {
		m_locker.lockForRead();
		if (m_terminate) {
			m_locker.unlock();
			return false;
		}
		m_locker.unlock();;
		//printf("% 3d%%: block %d of %d (byte %lld of %lld)\r",
		//	   (int)((long long)blkno*100/discend), blkno, discend,
		//	   (long long)blkno*DVDCSS_BLOCK_SIZE, (long long)discend*DVDCSS_BLOCK_SIZE);
		//fflush(stdout);
		emit extractProgress(blkno, discend);

		int maxblks;
		int cssflags;
		while (blkno >= vobs[curvob].end)
			curvob++;
		if (blkno < vobs[curvob].start) {
			// We haven't yet reached the next VOB.
			// Just read without decrypting.
			cssflags = DVDCSS_NOFLAGS;
			maxblks = vobs[curvob].start - blkno;

		} else if (blkno == vobs[curvob].start) {
			// We're just starting a new VOB - re-key libdvdcss.
			// (Probably only needed per-title set, but...)
			qDebug() << "Re-keying at block" << blkno;
			int actblk = dvdcss_seek(dcss, blkno, DVDCSS_SEEK_KEY);
			if (actblk != blkno) {
				qDebug() << "error seeking in DVD with dvdcss";
				return false;
			}

			cssflags = DVDCSS_READ_DECRYPT;
			maxblks = vobs[curvob].end - blkno;

		} else if (blkno < vobs[curvob].end) {
			// We're in the middle of a VOB.  Decrypt it.
			cssflags = DVDCSS_READ_DECRYPT;
			maxblks = vobs[curvob].end - blkno;

		} else {
			qDebug() << "read past end";
			return false;
		}

		if (maxblks > NBLOCKS)
			maxblks = NBLOCKS;

		// Read some blocks
		int actblks = dvdcss_read(dcss, buf, maxblks, cssflags);
		if (actblks < 0) {
			qDebug() << "read error at block" <<  blkno;
			return false;
		}
		if ((cssflags & DVDCSS_READ_DECRYPT) && (buf[0x14] & 0x30))
			qDebug() << "block" << blkno << ": still scrambled!?";

		// Write them
		out.write(buf, (qint64)actblks * DVDCSS_BLOCK_SIZE);

		blkno += actblks;

		// XX dvdcss has a bug in libc_read():
		// on partial reads it seeks to the wrong position,
		// so we can't iterate until actblks == 0.
		if (actblks < maxblks)
			break;
	}
	emit extractProgress(blkno, discend);

	if (blkno < (int)discend) {
		qDebug() << "SHORT READ: only " << blkno << "of" << discend << "blocks copied";
		return false;
	}

	qDebug() << "Success:" << blkno << "blocks copied (" << (long long)blkno * DVDCSS_BLOCK_SIZE << ") of" << discend << "expected";
	return true;
}

QWidget* DVDImageJob::gui()
{
	return new DVDImageJobGui(this);
}

void DVDImageJob::terminate()
{
	m_locker.lockForWrite();
	m_terminate = true;
	m_locker.unlock();
	watcher()->waitForFinished();
}