demux: es: fix swab usage
[vlc.git] / modules / demux / stl.c
blob5748238c4d3dfb49b367b0492d531d9908831eaa
1 /*****************************************************************************
2 * stl.c: EBU STL demuxer
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <assert.h>
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 /*****************************************************************************
37 * Module descriptor
38 *****************************************************************************/
39 static int Open (vlc_object_t *);
40 static void Close(vlc_object_t *);
42 vlc_module_begin()
43 set_description(N_("EBU STL subtitles parser"))
44 set_category(CAT_INPUT)
45 set_subcategory(SUBCAT_INPUT_DEMUX)
46 set_capability("demux", 1)
47 set_callbacks(Open, Close)
48 add_shortcut("stl", "subtitle")
49 vlc_module_end()
51 /*****************************************************************************
52 * Local definitions/prototypes
53 *****************************************************************************/
54 typedef struct {
55 vlc_tick_t start;
56 vlc_tick_t stop;
57 size_t blocknumber;
58 size_t count;
59 } stl_entry_t;
61 typedef struct
63 size_t count;
64 stl_entry_t *index;
66 es_out_id_t *es;
68 size_t current;
69 vlc_tick_t next_date;
70 bool b_slave;
71 bool b_first_time;
72 } demux_sys_t;
74 static size_t ParseInteger(uint8_t *data, size_t size)
76 char tmp[16];
77 assert(size < sizeof(tmp));
78 memcpy(tmp, data, size);
79 tmp[size] = '\0';
81 return strtol(tmp, NULL, 10);
83 static vlc_tick_t ParseTimeCode(uint8_t *data, double fps)
85 return CLOCK_FREQ * (data[0] * 3600 +
86 data[1] * 60 +
87 data[2] * 1 +
88 data[3] / fps);
90 static vlc_tick_t ParseTextTimeCode(uint8_t *data, double fps)
92 uint8_t tmp[4];
93 for (int i = 0; i < 4; i++)
94 tmp[i] = ParseInteger(&data[2 * i], 2);
95 return ParseTimeCode(tmp, fps);
98 static int Control(demux_t *demux, int query, va_list args)
100 demux_sys_t *sys = demux->p_sys;
101 switch(query) {
102 case DEMUX_CAN_SEEK:
103 return vlc_stream_vaControl(demux->s, query, args);
104 case DEMUX_GET_LENGTH: {
105 *va_arg(args, vlc_tick_t *) =
106 sys->count > 0 ? sys->index[sys->count-1].stop : 0;
107 return VLC_SUCCESS;
109 case DEMUX_GET_TIME: {
110 vlc_tick_t *t = va_arg(args, vlc_tick_t *);
111 *t = sys->next_date - var_GetInteger(demux->obj.parent, "spu-delay");
112 if( *t < 0 )
113 *t = sys->next_date;
114 return VLC_SUCCESS;
116 case DEMUX_SET_NEXT_DEMUX_TIME: {
117 sys->b_slave = true;
118 sys->next_date = va_arg(args, vlc_tick_t);
119 return VLC_SUCCESS;
121 case DEMUX_SET_TIME: {
122 vlc_tick_t t = va_arg(args, vlc_tick_t);
123 for( size_t i = 0; i + 1< sys->count; i++ )
125 if( sys->index[i + 1].start >= t &&
126 vlc_stream_Seek(demux->s, 1024 + 128LL * sys->index[i].blocknumber) == VLC_SUCCESS )
128 sys->current = i;
129 sys->next_date = t;
130 sys->b_first_time = true;
131 return VLC_SUCCESS;
134 break;
136 case DEMUX_SET_POSITION:
138 double f = va_arg( args, double );
139 if(sys->count && sys->index[sys->count-1].stop > 0)
141 vlc_tick_t i64 = f * sys->index[sys->count-1].stop;
142 return demux_Control(demux, DEMUX_SET_TIME, i64);
144 break;
146 case DEMUX_GET_POSITION:
148 double *pf = va_arg(args, double *);
149 if(sys->current >= sys->count)
151 *pf = 1.0;
153 else if(sys->count > 0 && sys->index[sys->count-1].stop > 0)
155 *pf = sys->next_date - var_GetInteger(demux->obj.parent, "spu-delay");
156 if(*pf < 0)
157 *pf = sys->next_date;
158 *pf /= sys->index[sys->count-1].stop;
160 else
162 *pf = 0.0;
164 return VLC_SUCCESS;
166 case DEMUX_CAN_PAUSE:
167 case DEMUX_SET_PAUSE_STATE:
168 case DEMUX_CAN_CONTROL_PACE:
169 case DEMUX_GET_PTS_DELAY: {
170 return demux_vaControlHelper( demux->s, 0, -1, 0, 1, query, args );
172 default:
173 break;
175 return VLC_EGENERIC;
178 static int Demux(demux_t *demux)
180 demux_sys_t *sys = demux->p_sys;
182 vlc_tick_t i_barrier = sys->next_date - var_GetInteger(demux->obj.parent, "spu-delay");
183 if(i_barrier < 0)
184 i_barrier = sys->next_date;
186 while(sys->current < sys->count &&
187 sys->index[sys->current].start <= i_barrier)
189 stl_entry_t *s = &sys->index[sys->current];
191 if (!sys->b_slave && sys->b_first_time)
193 es_out_SetPCR(demux->out, VLC_TICK_0 + i_barrier);
194 sys->b_first_time = false;
197 /* Might be a gap in block # */
198 const uint64_t i_pos = 1024 + 128LL * s->blocknumber;
199 if(i_pos != vlc_stream_Tell(demux->s) &&
200 vlc_stream_Seek( demux->s, i_pos ) != VLC_SUCCESS )
201 return VLC_DEMUXER_EOF;
203 block_t *b = vlc_stream_Block(demux->s, 128);
204 if (b && b->i_buffer == 128)
206 b->i_dts =
207 b->i_pts = VLC_TICK_0 + s->start;
208 if (s->stop > s->start)
209 b->i_length = s->stop - s->start;
210 es_out_Send(demux->out, sys->es, b);
212 else
214 if(b)
215 block_Release(b);
216 return VLC_DEMUXER_EOF;
218 sys->current++;
221 if (!sys->b_slave)
223 es_out_SetPCR(demux->out, VLC_TICK_0 + i_barrier);
224 sys->next_date += VLC_TICK_FROM_MS(125);
227 return sys->current < sys->count ? VLC_DEMUXER_SUCCESS : VLC_DEMUXER_EOF;
230 static int Open(vlc_object_t *object)
232 demux_t *demux = (demux_t*)object;
234 const uint8_t *peek;
235 if (vlc_stream_Peek(demux->s, &peek, 11) != 11)
236 return VLC_EGENERIC;
238 bool is_stl_25 = !memcmp(&peek[3], "STL25.01", 8);
239 bool is_stl_30 = !memcmp(&peek[3], "STL30.01", 8);
240 if (!is_stl_25 && !is_stl_30)
241 return VLC_EGENERIC;
242 const double fps = is_stl_25 ? 25 : 30;
244 uint8_t header[1024];
245 if (vlc_stream_Read(demux->s, header, sizeof(header)) != sizeof(header)) {
246 msg_Err(demux, "Incomplete EBU STL header");
247 return VLC_EGENERIC;
249 const int cct = ParseInteger(&header[12], 2);
250 const vlc_tick_t program_start = ParseTextTimeCode(&header[256], fps);
251 const size_t tti_count = ParseInteger(&header[238], 5);
252 if (!tti_count)
253 return VLC_EGENERIC;
254 msg_Dbg(demux, "Detected EBU STL : CCT=%d TTI=%zu start=%8.8s %"PRId64, cct, tti_count, &header[256], program_start);
256 demux_sys_t *sys = malloc(sizeof(*sys));
257 if(!sys)
258 return VLC_EGENERIC;
260 sys->b_slave = false;
261 sys->b_first_time = true;
262 sys->next_date = 0;
263 sys->current = 0;
264 sys->count = 0;
265 sys->index = calloc(tti_count, sizeof(*sys->index));
266 if(!sys->index)
268 free(sys);
269 return VLC_EGENERIC;
272 bool comment = false;
273 stl_entry_t *s = &sys->index[0];
274 s->count = 0;
276 for (size_t i = 0; i < tti_count; i++) {
277 uint8_t tti[16];
278 if (vlc_stream_Read(demux->s, tti, 16) != 16 ||
279 vlc_stream_Read(demux->s, NULL, 112) != 112) {
280 msg_Warn(demux, "Incomplete EBU STL file");
281 break;
283 const int ebn = tti[3];
284 if (ebn >= 0xf0 && ebn <= 0xfd)
285 continue;
286 if (ebn == 0xfe)
287 continue;
289 if (s->count <= 0) {
290 comment = tti[15] != 0;
291 s->start = ParseTimeCode(&tti[5], fps) - program_start;
292 s->stop = ParseTimeCode(&tti[9], fps) - program_start;
293 s->blocknumber = i;
295 s->count++;
296 if (ebn == 0xff && !comment)
297 s = &sys->index[++sys->count];
298 if (ebn == 0xff && sys->count < tti_count)
299 s->count = 0;
302 demux->p_sys = sys;
303 if (sys->count == 0 ||
304 vlc_stream_Seek(demux->s, 1024 + 128LL * sys->index[0].blocknumber) != VLC_SUCCESS)
306 Close(object);
307 return VLC_EGENERIC;
310 es_format_t fmt;
311 es_format_Init(&fmt, SPU_ES, VLC_CODEC_EBU_STL);
312 fmt.i_extra = sizeof(header);
313 fmt.p_extra = header;
315 sys->es = es_out_Add(demux->out, &fmt);
316 fmt.i_extra = 0;
317 fmt.p_extra = NULL;
318 es_format_Clean(&fmt);
320 if(sys->es == NULL)
322 Close(object);
323 return VLC_EGENERIC;
326 demux->p_sys = sys;
327 demux->pf_demux = Demux;
328 demux->pf_control = Control;
329 return VLC_SUCCESS;
332 static void Close(vlc_object_t *object)
334 demux_t *demux = (demux_t*)object;
335 demux_sys_t *sys = demux->p_sys;
337 free(sys->index);
338 free(sys);