Welcome to mirror list, hosted at ThFree Co, Russian Federation.

indexer_dv.c « intern « imbuf « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d1202136d56ccb61cda77664fb1d57905e7d653b (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
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
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Peter Schlaile <peter [at] schlaile [dot] de> 2011
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
*/

#include "IMB_indexer.h"
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include <time.h>

typedef struct indexer_dv_bitstream {
	unsigned char* buffer;
	int bit_pos;
} indexer_dv_bitstream;

static indexer_dv_bitstream bitstream_new(unsigned char* buffer_) 
{
	indexer_dv_bitstream rv;

	rv.buffer = buffer_;
	rv.bit_pos = 0;

	return rv;
}

static unsigned long bitstream_get_bits(indexer_dv_bitstream * This, int num) 
{
	int byte_pos = This->bit_pos >> 3;
	unsigned long i = 
		This->buffer[byte_pos] | (This->buffer[byte_pos + 1] << 8) |
		(This->buffer[byte_pos + 2] << 16) | 
		(This->buffer[byte_pos + 3] << 24);
	int rval = (i >> (This->bit_pos & 0x7)) & ((1 << num) - 1);
	This->bit_pos += num;
	return rval;
}

static int parse_num(indexer_dv_bitstream * b, int numbits) {
	return bitstream_get_bits(b, numbits);
}

static int parse_bcd(indexer_dv_bitstream * b, int n) 
{
	char s[256];
	char * p = s + (n+3)/4;

	*p-- = 0;

	while (n > 4) {
		char a;
		int v = bitstream_get_bits(b, 4);

		n -= 4;
		a = '0' + v;

		if (a > '9') {
			bitstream_get_bits(b, n);
			return -1;
		}

		*p-- = a;
	}
	if (n) {
		char a;
		int v = bitstream_get_bits(b, n);
		a = '0' + v;
		if (a > '9') {
			return -1;
		}
		*p-- = a;
	}

	return atol(s);
}

typedef struct indexer_dv_context
{
	int rec_curr_frame;
	int rec_curr_second;
	int rec_curr_minute;
	int rec_curr_hour;

	int rec_curr_day;
	int rec_curr_month;
	int rec_curr_year;

	char got_record_date;
	char got_record_time;

	time_t ref_time_read;
	time_t ref_time_read_new;
	int curr_frame;

	time_t gap_start;
	int gap_frame;

	int frameno_offset;

	anim_index_entry backbuffer[31];
	int fsize;

	anim_index_builder * idx;
} indexer_dv_context;

static void parse_packet(indexer_dv_context * This, unsigned char * p)
{
	indexer_dv_bitstream b;
	int type = p[0];

	b = bitstream_new(p + 1);

	switch (type) {
		case 0x62: // Record date
			parse_num(&b, 8);
			This->rec_curr_day = parse_bcd(&b, 6);
			parse_num(&b, 2);
			This->rec_curr_month = parse_bcd(&b, 5);
			parse_num(&b, 3);
			This->rec_curr_year = parse_bcd(&b, 8);
			if (This->rec_curr_year < 25) {
				This->rec_curr_year += 2000;
			} else {
				This->rec_curr_year += 1900;
			}
			This->got_record_date = 1;
			break;
		case 0x63: // Record time
			This->rec_curr_frame = parse_bcd(&b, 6);
			parse_num(&b, 2);
			This->rec_curr_second = parse_bcd(&b, 7);
			parse_num(&b, 1);
			This->rec_curr_minute = parse_bcd(&b, 7);
			parse_num(&b, 1);
			This->rec_curr_hour = parse_bcd(&b, 6);
			This->got_record_time = 1;
			break;
	}
}

static void parse_header_block(indexer_dv_context * This, unsigned char* target)
{
	int i;
	for (i = 3; i < 80; i += 5) {
		if (target[i] != 0xff) {
			parse_packet(This, target + i);
		}
	}
}

static void parse_subcode_blocks(
        indexer_dv_context * This, unsigned char* target)
{
	int i,j;

	for (j = 0; j < 2; j++) {
		for (i = 3; i < 80; i += 5) {
			if (target[i] != 0xff) {
				parse_packet(This, target + i);
			}
		}
	}
}

static void parse_vaux_blocks(
        indexer_dv_context * This, unsigned char* target)
{
	int i,j;

	for (j = 0; j < 3; j++) {
		for (i = 3; i < 80; i += 5) {
			if (target[i] != 0xff) {
				parse_packet(This, target + i);
			}
		}
		target += 80;
	}
}

static void parse_audio_headers(
        indexer_dv_context * This, unsigned char* target)
{
	int i;

	for(i = 0; i < 9; i++) {
		if (target[3] != 0xff) {
			parse_packet(This, target + 3);
		}
		target += 16 * 80;
	}
}

static void parse_frame(indexer_dv_context * This, 
                        unsigned char * framebuffer, int isPAL)
{
	int numDIFseq = isPAL ? 12 : 10;
	unsigned char* target = framebuffer;
	int ds;

	for (ds = 0; ds < numDIFseq; ds++) {
		parse_header_block(This, target);
		target +=   1 * 80;
		parse_subcode_blocks(This, target);
		target +=   2 * 80;
		parse_vaux_blocks(This, target);
		target +=   3 * 80;
		parse_audio_headers(This, target);
		target += 144 * 80;
	}
}

static void inc_frame(int * frame, time_t * t, int isPAL)
{
	if ((isPAL && *frame >= 25) || (!isPAL && *frame >= 30)) {
		fprintf(stderr, "Ouchie: inc_frame: invalid_frameno: %d\n",
		        *frame);
	}
	(*frame)++;
	if (isPAL && *frame >= 25) {
		(*t)++;
		*frame = 0;
	} else if (!isPAL && *frame >= 30) {
		(*t)++;
		*frame = 0;
	}
}

static void write_index(indexer_dv_context * This, anim_index_entry * entry)
{
	IMB_index_builder_add_entry(
		This->idx, entry->frameno + This->frameno_offset, 
		entry->seek_pos, entry->seek_pos_dts, entry->pts);
}

static void fill_gap(indexer_dv_context * This, int isPAL)
{
	int i;

	for (i = 0; i < This->fsize; i++) {
		if (This->gap_start == This->ref_time_read &&
		        This->gap_frame == This->curr_frame) {
			fprintf(stderr,
			        "indexer_dv::fill_gap: "
			        "can't seek backwards !\n");
			break;
		}
		inc_frame(&This->gap_frame, &This->gap_start, isPAL);
	}

	while (This->gap_start != This->ref_time_read ||
	       This->gap_frame != This->curr_frame) {
		inc_frame(&This->gap_frame, &This->gap_start, isPAL);
		This->frameno_offset++;
	}

	for (i = 0; i < This->fsize; i++) {
		write_index(This, This->backbuffer + i);
	}
	This->fsize = 0;
}

static void proc_frame(indexer_dv_context * This,
                       unsigned char* UNUSED(framebuffer), int isPAL)
{
	struct tm recDate;
	time_t t;

	if (!This->got_record_date || !This->got_record_time) {
		return;
	}

	recDate.tm_sec = This->rec_curr_second;
	recDate.tm_min = This->rec_curr_minute;
	recDate.tm_hour = This->rec_curr_hour;
	recDate.tm_mday = This->rec_curr_day;
	recDate.tm_mon = This->rec_curr_month - 1;
	recDate.tm_year = This->rec_curr_year - 1900;
	recDate.tm_wday = -1;
	recDate.tm_yday = -1;
	recDate.tm_isdst = -1;
	
	t = mktime(&recDate);
	if (t == -1) {
		return;
	}

	This->ref_time_read_new = t;

	if (This->ref_time_read < 0) {
		This->ref_time_read = This->ref_time_read_new;
		This->curr_frame = 0;
	} else {
		if (This->ref_time_read_new - This->ref_time_read == 1) {
			This->curr_frame = 0;
			This->ref_time_read = This->ref_time_read_new;
			if (This->gap_frame >= 0) {
				fill_gap(This, isPAL);
				This->gap_frame = -1;
			}
		} else if (This->ref_time_read_new  == This->ref_time_read) {
			// do nothing
		} else {
			This->gap_start = This->ref_time_read;
			This->gap_frame = This->curr_frame;
			This->ref_time_read = This->ref_time_read_new;
			This->curr_frame = -1;
		}
	}
}

static void indexer_dv_proc_frame(anim_index_builder * idx, 
                                  unsigned char * buffer,
                                  int UNUSED(data_size),
                                  struct anim_index_entry * entry)
{
	int isPAL;
	
	indexer_dv_context * This = (indexer_dv_context *) idx->private_data;

	isPAL = (buffer[3] & 0x80);

	This->got_record_date = FALSE;
	This->got_record_time = FALSE;

	parse_frame(This, buffer, isPAL);
	proc_frame(This, buffer, isPAL);

	if (This->curr_frame >= 0) {
		write_index(This, entry);
		inc_frame(&This->curr_frame, &This->ref_time_read, isPAL);
	} else {
		This->backbuffer[This->fsize++] = *entry;
		if (This->fsize >= 31) {
			int i;

			fprintf(stderr, "indexer_dv::indexer_dv_proc_frame: "
			        "backbuffer overrun, emergency flush");

			for (i = 0; i < This->fsize; i++) {
				write_index(This, This->backbuffer+i);
			}
			This->fsize = 0;
		}
	}
}

static void indexer_dv_delete(anim_index_builder * idx)
{
	int i = 0;
	indexer_dv_context * This = (indexer_dv_context *) idx->private_data;

	for (i = 0; i < This->fsize; i++) {
		write_index(This, This->backbuffer+i);
	}

	MEM_freeN(This);
}

void IMB_indexer_dv_new(anim_index_builder * idx)
{
	indexer_dv_context * rv = MEM_callocN(
	            sizeof(indexer_dv_context), "index_dv builder context");

	rv->ref_time_read = -1;
	rv->curr_frame = -1;
	rv->gap_frame = -1;
	rv->idx = idx;
	
	idx->private_data = rv;
	idx->proc_frame = indexer_dv_proc_frame;
	idx->delete_priv_data = indexer_dv_delete;
}