summaryrefslogtreecommitdiff
path: root/src/rmd_load_cache.c
blob: 24c22e11ddb6294106df2742af1084f81d685d80 (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
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
/******************************************************************************
*                            recordMyDesktop                                  *
*******************************************************************************
*                                                                             *
*            Copyright (C) 2006,2007,2008 John Varouhakis                     *
*                                                                             *
*                                                                             *
*   This program is free software; you can redistribute it and/or modify      *
*   it under the terms of the GNU General Public License as published by      *
*   the Free Software Foundation; either version 2 of the License, or         *
*   (at your option) any later version.                                       *
*                                                                             *
*   This program is distributed in the hope that it will be useful,           *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*   GNU General Public License for more details.                              *
*                                                                             *
*   You should have received a copy of the GNU General Public License         *
*   along with this program; if not, write to the Free Software               *
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
*                                                                             *
*                                                                             *
*                                                                             *
*   For further information contact me at johnvarouhakis@gmail.com            *
******************************************************************************/

#include "config.h"
#include "rmd_load_cache.h"

#include "rmd_cache.h"
#include "rmd_encode_audio_buffer.h"
#include "rmd_encode_image_buffer.h"
#include "rmd_macro.h"
#include "rmd_threads.h"
#include "rmd_types.h"

#include <pthread.h>
#include <signal.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


//The number of bytes for every
//sub-block of the y,u and v planes.
//Since the blocks are square
//these are obviously the squares
//of the widths(specified above),
//but the definitions bellow are only
//for convenience anyway.
#define Y_UNIT_BYTES	0x0100
#define UV_UNIT_BYTES   0x0040


//The frame after retrieval.
//Based on the Header information
//we can read the correct amount of bytes.
typedef struct _CachedFrame{
	FrameHeader *header;
	u_int32_t	*YBlocks,	 //identifying number on the grid,
			*UBlocks,	 //starting at top left
			*VBlocks;	 //	   >>	  >>
	unsigned char	*YData,		//pointer to data for the blocks that have changed,
			*UData,		//which have to be remapped
			*VData;		//on the buffer when reading
}CachedFrame;


static void rmdLoadBlock(
			unsigned char *dest,
			unsigned char *source,
			int blockno,
			int width,
			int height,
			int blockwidth) {

	int	block_i = blockno / (width / blockwidth),//place on the grid
		block_k = blockno % (width / blockwidth);

	for (int j = 0; j < blockwidth; j++)//we copy rows
		memcpy(	&dest[(block_i * width + block_k) * blockwidth + j * width],
			&source[j * blockwidth],
			blockwidth);
}

//returns number of bytes
static int rmdReadZF(void * buffer, size_t size, size_t nmemb, FILE *ucfp, gzFile ifp) {
	if ((ifp != NULL && ucfp != NULL) ||
	   (ifp == NULL && ucfp == NULL))
		return -1;
	else if (ucfp != NULL) {
		return size * fread(buffer, size, nmemb, ucfp);
	} else
		return gzread(ifp, buffer, size * nmemb);
}

static int rmdReadFrame(CachedFrame *frame, FILE *ucfp, gzFile ifp) {
	int	index_entry_size = sizeof(u_int32_t);

	if (frame->header->Ynum > 0) {
		if (rmdReadZF(	frame->YBlocks,
				index_entry_size,
				frame->header->Ynum,
				ucfp,
				ifp) != index_entry_size * frame->header->Ynum) {
			return -1;
		}
	}

	if (frame->header->Unum > 0) {
		if (rmdReadZF(	frame->UBlocks,
				index_entry_size,
				frame->header->Unum,
				ucfp,
				ifp) != index_entry_size * frame->header->Unum) {
			return -1;
		}
	}

	if (frame->header->Vnum > 0) {
		if (rmdReadZF(	frame->VBlocks,
				index_entry_size,
				frame->header->Vnum,
				ucfp,
				ifp) != index_entry_size * frame->header->Vnum) {
			return -1;
		}
	}

	if (frame->header->Ynum > 0) {
		if (rmdReadZF(	frame->YData,
				Y_UNIT_BYTES,
				frame->header->Ynum,
				ucfp,
				ifp) != Y_UNIT_BYTES * frame->header->Ynum) {
			return -2;
		}
	}

	if (frame->header->Unum > 0) {
		if (rmdReadZF(	frame->UData,
				UV_UNIT_BYTES,
				frame->header->Unum,
				ucfp,
				ifp) != UV_UNIT_BYTES * frame->header->Unum) {
			return -2;
		}
	}

	if (frame->header->Vnum > 0) {
		if (rmdReadZF(	frame->VData,
				UV_UNIT_BYTES,
				frame->header->Vnum,
				ucfp,
				ifp) != UV_UNIT_BYTES * frame->header->Vnum) {
			return -2;
		}
	}

	return 0;
}

static int read_header(ProgData *pdata, gzFile ifp, FILE *ucfp, FrameHeader *fheader) {
	if (!pdata->args.zerocompression) {
		return gzread(ifp, fheader, sizeof(FrameHeader)) == sizeof(FrameHeader);
	} else {
		return fread(fheader, sizeof(FrameHeader), 1, ucfp) == 1;
	}
}

void *rmdLoadCache(ProgData *pdata) {

	yuv_buffer	*yuv = &pdata->enc_data->yuv;
	gzFile		ifp = NULL;
	FILE		*ucfp = NULL;
	FILE		*afp = pdata->cache_data->afp;
	FrameHeader	fheader;
	CachedFrame	frame;
	int		nth_cache = 1,
			audio_end = 0,
			thread_exit = 0,	//0 success, -1 couldn't find files,1 couldn't remove
			blocknum_x = pdata->enc_data->yuv.y_width / Y_UNIT_WIDTH,
			blocknum_y = pdata->enc_data->yuv.y_height / Y_UNIT_WIDTH,
			blockszy = Y_UNIT_BYTES,//size of y plane block in bytes
			blockszuv = UV_UNIT_BYTES;//size of u,v plane blocks in bytes
	signed char	*sound_data = (signed char *)malloc(pdata->periodsize * pdata->sound_framesize);

	u_int32_t	YBlocks[(yuv->y_width * yuv->y_height) / Y_UNIT_BYTES],
			UBlocks[(yuv->uv_width * yuv->uv_height) / UV_UNIT_BYTES],
			VBlocks[(yuv->uv_width * yuv->uv_height) / UV_UNIT_BYTES];
	unsigned int	last_capture_frameno = 0;

	rmdThreadsSetName("rmdEncodeCache");

	// We allocate the frame that we will use
	frame.header	= &fheader;
	frame.YBlocks	= YBlocks;
	frame.UBlocks	= UBlocks;
	frame.VBlocks	= VBlocks;
	frame.YData	= malloc(yuv->y_width  * yuv->y_height);
	frame.UData	= malloc(yuv->uv_width * yuv->uv_height);
	frame.VData	= malloc(yuv->uv_width * yuv->uv_height);

	//and the we open our files
	if (!pdata->args.zerocompression) {
		ifp = gzopen(pdata->cache_data->imgdata, "rb");
		if (ifp == NULL) {
			thread_exit = -1;
			pthread_exit(&thread_exit);
		}
	} else {
		ucfp = fopen(pdata->cache_data->imgdata, "rb");
		if (ucfp == NULL) {
			thread_exit = -1;
			pthread_exit(&thread_exit);
		}
	}

	if (!pdata->args.nosound) {
		afp = fopen(pdata->cache_data->audiodata, "rb");
		if (afp == NULL) {
			thread_exit = -1;
			pthread_exit(&thread_exit);
		}
	}

	//this will be used now to define if we proccess audio or video
	//on any given loop.
	pdata->avd = 0;
	//If sound finishes first,we go on with the video.
	//If video ends we will do one more run to flush audio in the ogg file
	while (pdata->running) {

		//video load and encoding
		if (pdata->avd <= 0 || pdata->args.nosound || audio_end) {

			if (read_header(pdata, ifp, ucfp, frame.header)) {

				fprintf(stdout,	"\r[%d%%] ",
						((frame.header->capture_frameno) * 100) / pdata->capture_frameno);
				fflush(stdout);

				if (	(frame.header->Ynum > blocknum_x * blocknum_y) ||
					(frame.header->Unum > blocknum_x * blocknum_y) ||
					(frame.header->Vnum > blocknum_x * blocknum_y) ||
					rmdReadFrame(	&frame,
							pdata->args.zerocompression ? ucfp : NULL,
							pdata->args.zerocompression ? NULL : ifp) < 0) {

					raise(SIGINT);
					continue;
				}

				//load the blocks for each buffer
				if (frame.header->Ynum)
					for (int j = 0; j < frame.header->Ynum; j++)
						rmdLoadBlock(	yuv->y,
								&frame.YData[j * blockszy],
								frame.YBlocks[j],
								yuv->y_width,
								yuv->y_height,
								Y_UNIT_WIDTH);
				if (frame.header->Unum)
					for (int j = 0; j < frame.header->Unum; j++)
						rmdLoadBlock(	yuv->u,
								&frame.UData[j * blockszuv],
								frame.UBlocks[j],
								yuv->uv_width,
								yuv->uv_height,
								UV_UNIT_WIDTH);
				if (frame.header->Vnum)
					for (int j = 0; j < frame.header->Vnum; j++)
						rmdLoadBlock(	yuv->v,
								&frame.VData[j * blockszuv],
								frame.VBlocks[j],
								yuv->uv_width,
								yuv->uv_height,
								UV_UNIT_WIDTH);

				rmdSyncEncodeImageBuffer(pdata, fheader.capture_frameno - last_capture_frameno);

				last_capture_frameno = fheader.capture_frameno;
			} else {
				if (rmdSwapCacheFilesRead(	pdata->cache_data->imgdata,
								nth_cache,
								&ifp,
								&ucfp)) {
					raise(SIGINT);
				} else {
					fprintf(stderr, "\t[Cache File %d]", nth_cache);
					nth_cache++;
				}
			}
		//audio load and encoding
		} else {
			if (!audio_end) {
				int nbytes = fread(sound_data, 1, pdata->periodsize*
								 pdata->sound_framesize, afp);
				if (nbytes <= 0)
					audio_end = 1;
				else
					rmdSyncEncodeSoundBuffer(pdata, sound_data);
			}
		}
	}

	pthread_mutex_lock(&pdata->theora_lib_mutex);
	pdata->th_encoding_clean = 1;
	pthread_cond_signal(&pdata->theora_lib_clean);
	pthread_mutex_unlock(&pdata->theora_lib_mutex);

	pthread_mutex_lock(&pdata->vorbis_lib_mutex);
	pdata->v_encoding_clean = 1;
	pthread_cond_signal(&pdata->vorbis_lib_clean);
	pthread_mutex_unlock(&pdata->vorbis_lib_mutex);
	fprintf(stdout, "\n");

	// Clear frame
	free(frame.YData);
	free(frame.UData);
	free(frame.VData);

	free(sound_data);

	if (!pdata->args.nosound)
		fclose(afp);

	pthread_exit(&thread_exit);
}
© All Rights Reserved