Debian Codegraph @

lib/subtitle/subtitle.c

raw
1 #include "subtitle.h"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5
6 static subtitle_line_t* copy_line(subtitle_line_t* to, const subtitle_line_t* from)
7 {
8 int i;
9 if (to)
10 {
11 for (i = 0; i < SUBTITLE_MAX_LINES; i++)
12 {
13 if (from->line[i] && i < from->lines)
14 {
15 size_t l = strlen(from->line[i]);
16 to->line[i] = (char*) realloc(to->line[i], l + 1);
17 if (to->line[i])
18 strcpy(to->line[i], from->line[i]);
19 }
20 else if (to->line[i])
21 {
22 free(to->line[i]);
23 to->line[i] = NULL;
24 }
25 }
26
27 to->start = from->start;
28 to->end = from->end;
29 to->lines = from->lines;
30 }
31 return to;
32 }
33
34 static void free_line(subtitle_line_t* sl)
35 {
36 int i;
37 for (i = 0; i < sl->lines; i++)
38 {
39 free(sl->line[i]);
40 sl->line[i] = NULL;
41 }
42 sl->start = 0;
43 sl->end = 0;
44 sl->lines = 0;
45 }
46
47 void subtitle_close(subtitles_t* st)
48 {
49 if (st->subtitle)
50 {
51 int i;
52 for (i = 0; i < st->count; i++)
53 {
54 int j;
55 for (j = 0; j < SUBTITLE_MAX_LINES; j++)
56 {
57 if (st->subtitle[i].line[j])
58 free(st->subtitle[i].line[j]);
59 }
60 }
61 free(st->subtitle);
62 }
63 if (st->encoding)
64 free(st->encoding);
65 free(st);
66 }
67
68 int subtitle_get(subtitle_line_t* sl, subtitles_t* st, double timepos)
69 {
70 unsigned int lo = 0;
71 unsigned int hi;
72 unsigned int fp = (unsigned int) (timepos * 1000.0);
73 subtitle_line_t* line = NULL;
74
75 if (st->count > 1)
76 {
77 hi = st->count - 1;
78 if (st->frame_based && st->fps > 0.0)
79 fp = (unsigned int) (timepos * st->fps);
80
81 //printf("get: %f -> %d %f\n", timepos, fp, st->fps);
82 while (lo < hi)
83 {
84 unsigned int m = lo + (hi - lo) / 2;
85
86 if (fp < st->subtitle[m].start)
87 hi = m;
88 else if (fp >= st->subtitle[m + 1].start)
89 lo = m + 1;
90 else
91 {
92 lo = m;
93 break;
94 }
95 }
96 if (st->subtitle[lo].start <= fp && fp < st->subtitle[lo].end)
97 line = &st->subtitle[lo];
98 }
99 //printf("pos %d %p %d %d %d\n", lo, line, fp, st->subtitle[lo].start, st->subtitle[lo].end);
100 if (line)
101 {
102 if (!subtitle_line_equals(sl, line))
103 copy_line(sl, line);
104 }
105 else
106 free_line(sl);
107
108 return (!line) ? -1 : 0 ;
109 }
110
111 unsigned int subtitle_get_lines(subtitles_t* st)
112 {
113 return st->count;
114 }
115
116 subtitle_t subtitle_get_type(subtitles_t* st, const char** r)
117 {
118 const char t[][11] = {
119 "unknown", "microdvd", "subrip", "vplayer", "aqt", "sami",
120 "subviewer", "mpsub"
121 };
122 if (r && st->type < SUBTITLE_LAST)
123 *r = t[st->type];
124 return st->type;
125 }
126
127 subtitle_line_t* subtitle_line_new()
128 {
129 subtitle_line_t* nl;
130 nl = (subtitle_line_t*) malloc(sizeof(*nl));
131 if (nl != NULL)
132 memset(nl, 0, sizeof(*nl));
133 return nl;
134 }
135
136 subtitle_line_t* subtitle_line_copy(const subtitle_line_t* sl)
137 {
138 return (!sl) ? NULL : copy_line(subtitle_line_new(), sl);
139 }
140
141 int subtitle_line_equals(const subtitle_line_t* l1, const subtitle_line_t* l2)
142 {
143 /*
144 * for now we assume we have just one subtitle with the same time
145 * and the line count
146 */
147 return (l1 != NULL && l2 != NULL
148 && l1->lines == l2->lines
149 && l1->start == l2->start
150 && l1->end == l2->end);
151 }
152
153 void subtitle_line_free(subtitle_line_t* sl)
154 {
155 if (sl != NULL)
156 {
157 free_line(sl);
158 free(sl);
159 }
160 }
161
162 void subtitle_set_fps(subtitles_t* st, double fps)
163 {
164 st->fps = fps;
165 }