1 /* Copyright (C) 2008 Vincent Penquerc'h.
2 This file is part of the Kate codec library.
3 Written by Vincent Penquerc'h.
4
5 Use, distribution and reproduction of this library is governed
6 by a BSD style source license included with this source in the
7 file 'COPYING'. Please read these terms before distributing. */
8
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <ogg/ogg.h>
16 #include "kate/kate.h"
17 #include "kutil.h"
18 #include "ksrt.h"
19
new_srt_data(void)20 void *new_srt_data(void)
21 {
22 struct srt_data *srt_data=kate_malloc(sizeof(struct srt_data));
23 if (srt_data) {
24 srt_data->event_index=0;
25 }
26 return srt_data;
27 }
28
free_srt_data(void * data)29 void free_srt_data(void *data)
30 {
31 kate_free(data);
32 }
33
granule_to_milliseconds(const kate_info * ki,ogg_int64_t granpos)34 static ogg_int64_t granule_to_milliseconds(const kate_info *ki,ogg_int64_t granpos)
35 {
36 return (1000*granpos*ki->gps_denominator+ki->gps_numerator/2)/ki->gps_numerator;
37 }
38
milliseconds_to_time(ogg_int64_t ms,int * hours,int * minutes,int * seconds,int * milliseconds)39 static void milliseconds_to_time(ogg_int64_t ms,int *hours,int *minutes,int *seconds,int *milliseconds)
40 {
41 *hours=ms/(60*60*1000);
42 *minutes=ms/(60*1000)%60;
43 *seconds=ms/1000%60;
44 *milliseconds=ms%1000;
45 }
46
write_srt_event(FILE * fout,void * data,const kate_event * ev,ogg_int64_t granpos)47 void write_srt_event(FILE *fout,void *data,const kate_event *ev,ogg_int64_t granpos)
48 {
49 int h0,m0,s0,ms0;
50 int h1,m1,s1,ms1;
51 struct srt_data *srt_data=(struct srt_data*)data;
52
53 milliseconds_to_time(granule_to_milliseconds(ev->ki,ev->start),&h0,&m0,&s0,&ms0);
54 milliseconds_to_time(granule_to_milliseconds(ev->ki,ev->start+ev->duration),&h1,&m1,&s1,&ms1);
55
56 (void)granpos;
57 (void)data;
58 fprintf(fout,"%d\n",++srt_data->event_index);
59 fprintf(fout,"%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n",h0,m0,s0,ms0,h1,m1,s1,ms1);
60 fprintf(fout,"%s\n",ev->text);
61 fprintf(fout,"\n");
62 fflush(fout);
63 }
64
65