== Sequencer ==
[blender-staging.git] / source / blender / imbuf / intern / indexer_dv.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Peter Schlaile <peter [at] schlaile [dot] de> 2011
21  *
22  * Contributor(s): none yet.
23  *
24  * ***** END GPL LICENSE BLOCK *****
25 */
26
27 #include "IMB_indexer.h"
28 #include "MEM_guardedalloc.h"
29 #include <time.h>
30
31 typedef struct indexer_dv_bitstream {
32         unsigned char* buffer;
33         int bit_pos;
34 } indexer_dv_bitstream;
35
36 static indexer_dv_bitstream bitstream_new(unsigned char* buffer_) 
37 {
38         indexer_dv_bitstream rv;
39
40         rv.buffer = buffer_;
41         rv.bit_pos = 0;
42
43         return rv;
44 }
45
46 static unsigned long bitstream_get_bits(indexer_dv_bitstream * This, int num) 
47 {
48         int byte_pos = This->bit_pos >> 3;
49         unsigned long i = 
50                 This->buffer[byte_pos] | (This->buffer[byte_pos + 1] << 8) |
51                 (This->buffer[byte_pos + 2] << 16) | 
52                 (This->buffer[byte_pos + 3] << 24);
53         int rval = (i >> (This->bit_pos & 0x7)) & ((1 << num) - 1);
54         This->bit_pos += num;
55         return rval;
56 }
57
58 static int parse_num(indexer_dv_bitstream * b, int numbits) {
59         return bitstream_get_bits(b, numbits);
60 }
61
62 static int parse_bcd(indexer_dv_bitstream * b, int n) 
63 {
64         char s[256];
65         char * p = s + (n+3)/4;
66
67         *p-- = 0;
68
69         while (n > 4) {
70                 char a;
71                 int v = bitstream_get_bits(b, 4);
72
73                 n -= 4;
74                 a = '0' + v;
75
76                 if (a > '9') {
77                         bitstream_get_bits(b, n); 
78                         return -1;
79                 }
80
81                 *p-- = a;
82         }
83         if (n) {
84                 char a;
85                 int v = bitstream_get_bits(b, n);
86                 a = '0' + v;
87                 if (a > '9') {
88                         return -1;
89                 }
90                 *p-- = a;
91         }
92
93         return atol(s);
94 }
95
96 typedef struct indexer_dv_context
97 {
98         int rec_curr_frame;
99         int rec_curr_second;
100         int rec_curr_minute;
101         int rec_curr_hour;
102
103         int rec_curr_day;
104         int rec_curr_month;
105         int rec_curr_year;
106
107         char got_record_date;
108         char got_record_time;
109
110         time_t ref_time_read;
111         time_t ref_time_read_new;
112         int curr_frame;
113
114         time_t gap_start;
115         int gap_frame;
116
117         int frameno_offset;
118
119         anim_index_entry backbuffer[31];
120         int fsize;
121
122         anim_index_builder * idx;
123 } indexer_dv_context;
124
125 static void parse_packet(indexer_dv_context * This, unsigned char * p)
126 {
127         indexer_dv_bitstream b;
128         int type = p[0];
129
130         b = bitstream_new(p + 1);
131
132         switch (type) {
133         case 0x62: // Record date
134                 parse_num(&b, 8);
135                 This->rec_curr_day = parse_bcd(&b, 6);
136                 parse_num(&b, 2);
137                 This->rec_curr_month = parse_bcd(&b, 5);
138                 parse_num(&b, 3);
139                 This->rec_curr_year = parse_bcd(&b, 8);
140                 if (This->rec_curr_year < 25) {
141                         This->rec_curr_year += 2000;
142                 } else {
143                         This->rec_curr_year += 1900;
144                 }
145                 This->got_record_date = 1;
146                 break;
147         case 0x63: // Record time
148                 This->rec_curr_frame = parse_bcd(&b, 6);
149                 parse_num(&b, 2);
150                 This->rec_curr_second = parse_bcd(&b, 7);
151                 parse_num(&b, 1);
152                 This->rec_curr_minute = parse_bcd(&b, 7);
153                 parse_num(&b, 1);
154                 This->rec_curr_hour = parse_bcd(&b, 6);
155                 This->got_record_time = 1;
156                 break;
157         }
158 }
159
160 static void parse_header_block(indexer_dv_context * This, unsigned char* target)
161 {
162         int i;
163         for (i = 3; i < 80; i += 5) {
164                   if (target[i] != 0xff) {
165                           parse_packet(This, target + i);
166                   }
167         }
168 }
169
170 static void parse_subcode_blocks(
171         indexer_dv_context * This, unsigned char* target)
172 {
173         int i,j;
174
175         for (j = 0; j < 2; j++) {
176                 for (i = 3; i < 80; i += 5) {
177                         if (target[i] != 0xff) {
178                                 parse_packet(This, target + i);
179                         }
180                 }
181         }
182 }
183
184 static void parse_vaux_blocks(
185         indexer_dv_context * This, unsigned char* target)
186 {
187         int i,j;
188
189         for (j = 0; j < 3; j++) {
190                 for (i = 3; i < 80; i += 5) {
191                         if (target[i] != 0xff) {
192                                 parse_packet(This, target + i);
193                         }
194                 }
195                 target += 80;
196         }
197 }
198
199 static void parse_audio_headers(
200         indexer_dv_context * This, unsigned char* target)
201 {
202         int i;
203
204         for(i = 0; i < 9; i++) {
205                 if (target[3] != 0xff) {
206                         parse_packet(This, target + 3);
207                 }
208                 target += 16 * 80;
209         }
210 }
211
212 static void parse_frame(indexer_dv_context * This, 
213                         unsigned char * framebuffer, int isPAL)
214 {
215         int numDIFseq = isPAL ? 12 : 10;
216         unsigned char* target = framebuffer;
217         int ds;
218
219         for (ds = 0; ds < numDIFseq; ds++) { 
220                 parse_header_block(This, target);
221                 target +=   1 * 80;
222                 parse_subcode_blocks(This, target);
223                 target +=   2 * 80;
224                 parse_vaux_blocks(This, target);
225                 target +=   3 * 80;
226                 parse_audio_headers(This, target);
227                 target += 144 * 80;
228         }
229 }
230
231 static void inc_frame(int * frame, time_t * t, int isPAL)
232 {
233         if ((isPAL && *frame >= 25) || (!isPAL && *frame >= 30)) {
234                 fprintf(stderr, "Ouchie: inc_frame: invalid_frameno: %d\n",
235                         *frame);
236         }
237         (*frame)++;
238         if (isPAL && *frame >= 25) {
239                 (*t)++;
240                 *frame = 0;
241         } else if (!isPAL && *frame >= 30) {
242                 (*t)++;
243                 *frame = 0;
244         }
245 }
246
247 static void write_index(indexer_dv_context * This, anim_index_entry * entry)
248 {
249         IMB_index_builder_add_entry(
250                 This->idx, entry->frameno + This->frameno_offset, 
251                 entry->seek_pos, entry->seek_pos_dts, entry->pts);
252 }
253
254 static void fill_gap(indexer_dv_context * This, int isPAL)
255 {
256         int i;
257
258         for (i = 0; i < This->fsize; i++) {
259                 if (This->gap_start == This->ref_time_read && 
260                     This->gap_frame == This->curr_frame) {
261                         fprintf(stderr, 
262                                 "indexer_dv::fill_gap: "
263                                 "can't seek backwards !\n");
264                         break;
265                 }
266                 inc_frame(&This->gap_frame, &This->gap_start, isPAL);
267         }
268
269         while (This->gap_start != This->ref_time_read || 
270                This->gap_frame != This->curr_frame) {
271                 inc_frame(&This->gap_frame, &This->gap_start, isPAL);
272                 This->frameno_offset++;
273         }
274
275         for (i = 0; i < This->fsize; i++) {
276                 write_index(This, This->backbuffer + i);
277         }
278         This->fsize = 0;
279 }
280
281 static void proc_frame(indexer_dv_context * This,
282                        unsigned char* framebuffer, int isPAL)
283 {
284         struct tm recDate;
285         time_t t;
286
287         if (!This->got_record_date || !This->got_record_time) {
288                 return;
289         }
290
291         recDate.tm_sec = This->rec_curr_second;
292         recDate.tm_min = This->rec_curr_minute;
293         recDate.tm_hour = This->rec_curr_hour;
294         recDate.tm_mday = This->rec_curr_day;
295         recDate.tm_mon = This->rec_curr_month - 1;
296         recDate.tm_year = This->rec_curr_year - 1900;
297         recDate.tm_wday = -1;
298         recDate.tm_yday = -1;
299         recDate.tm_isdst = -1;
300         
301         t = mktime(&recDate);
302         if (t == -1) {
303                 return;
304         }
305
306         This->ref_time_read_new = t;
307
308         if (This->ref_time_read < 0) {
309                 This->ref_time_read = This->ref_time_read_new;
310                 This->curr_frame = 0;
311         } else {
312                 if (This->ref_time_read_new - This->ref_time_read == 1) {
313                         This->curr_frame = 0;
314                         This->ref_time_read = This->ref_time_read_new;
315                         if (This->gap_frame >= 0) {
316                                 fill_gap(This, isPAL);
317                                 This->gap_frame = -1;
318                         }
319                 } else if (This->ref_time_read_new  == This->ref_time_read) {
320                         // do nothing
321                 } else {
322                         This->gap_start = This->ref_time_read;
323                         This->gap_frame = This->curr_frame;
324                         This->ref_time_read = This->ref_time_read_new;
325                         This->curr_frame = -1;
326                 }
327         }
328 }
329
330 static void indexer_dv_proc_frame(anim_index_builder * idx, 
331                                   unsigned char * buffer,
332                                   int data_size, 
333                                   struct anim_index_entry * entry)
334 {
335         int isPAL;
336         
337         indexer_dv_context * This = (indexer_dv_context *) idx->private_data;
338
339         isPAL = (buffer[3] & 0x80);
340
341         This->got_record_date = FALSE;
342         This->got_record_time = FALSE;
343
344         parse_frame(This, buffer, isPAL);
345         proc_frame(This, buffer, isPAL);
346
347         if (This->curr_frame >= 0) {
348                 write_index(This, entry);
349                 inc_frame(&This->curr_frame, &This->ref_time_read, isPAL);
350         } else {
351                 This->backbuffer[This->fsize++] = *entry;
352                 if (This->fsize >= 31) {
353                         int i;
354
355                         fprintf(stderr, "indexer_dv::indexer_dv_proc_frame: "
356                                 "backbuffer overrun, emergency flush");
357
358                         for (i = 0; i < This->fsize; i++) {
359                                 write_index(This, This->backbuffer+i);
360                         }
361                         This->fsize = 0;
362                 }
363         }
364 }
365
366 static void indexer_dv_delete(anim_index_builder * idx)
367 {
368         int i = 0;
369         indexer_dv_context * This = (indexer_dv_context *) idx->private_data;
370
371         for (i = 0; i < This->fsize; i++) {
372                 write_index(This, This->backbuffer+i);
373         }
374
375         MEM_freeN(This);
376 }
377
378 void IMB_indexer_dv_new(anim_index_builder * idx)
379 {
380         indexer_dv_context * rv = MEM_callocN( 
381                 sizeof(indexer_dv_context), "index_dv builder context");
382
383         rv->ref_time_read = -1;
384         rv->curr_frame = -1;
385         rv->gap_frame = -1;
386         rv->idx = idx;
387         
388         idx->private_data = rv;
389         idx->proc_frame = indexer_dv_proc_frame;
390         idx->delete_priv_data = indexer_dv_delete;
391 }