2 This file is part of the HandBrake source code.
3 Homepage: <http://handbrake.fr/>.
4 It may be used under the terms of the GNU General Public License. */
13 struct start_and_end {
14 unsigned long start, end;
20 k_state_potential_new_entry,
24 typedef struct srt_entry_s {
25 long offset, duration;
32 * Store all context in the work private struct,
34 struct hb_work_private_s
44 unsigned long current_time;
45 unsigned long number_of_entries;
46 unsigned long current_state;
47 srt_entry_t current_entry;
48 iconv_t *iconv_context;
49 hb_subtitle_t *subtitle;
50 uint64_t start_time; // In HB time
51 uint64_t stop_time; // In HB time
54 static struct start_and_end read_time_from_string( const char* timeString )
56 // for ex. 00:00:15,248 --> 00:00:16,545
58 long houres1, minutes1, seconds1, milliseconds1,
59 houres2, minutes2, seconds2, milliseconds2;
61 sscanf(timeString, "%ld:%ld:%ld,%ld --> %ld:%ld:%ld,%ld\n", &houres1, &minutes1, &seconds1, &milliseconds1,
62 &houres2, &minutes2, &seconds2, &milliseconds2);
64 struct start_and_end result = {
65 milliseconds1 + seconds1*1000 + minutes1*60*1000 + houres1*60*60*1000,
66 milliseconds2 + seconds2*1000 + minutes2*60*1000 + houres2*60*60*1000};
70 static int utf8_fill( hb_work_private_t * pv )
72 int bytes, conversion = 0;
75 /* Align utf8 data to beginning of the buffer so that we can
76 * fill the buffer to its maximum */
77 memmove( pv->utf8_buf, pv->utf8_buf + pv->utf8_pos, pv->utf8_end - pv->utf8_pos );
78 pv->utf8_end -= pv->utf8_pos;
80 out_size = 2048 - pv->utf8_end;
84 size_t in_size, retval;
86 if( pv->end == pv->pos )
88 bytes = fread( pv->buf, 1, 1024, pv->file );
95 p = pv->buf + pv->pos;
96 q = pv->utf8_buf + pv->utf8_end;
97 in_size = pv->end - pv->pos;
99 retval = iconv( pv->iconv_context, &p, &in_size, &q, &out_size);
100 if( q != pv->utf8_buf + pv->utf8_pos )
103 pv->utf8_end = q - pv->utf8_buf;
104 pv->pos = p - pv->buf;
106 if( ( retval == -1 ) && ( errno == EINVAL ) )
108 /* Incomplete multibyte sequence, read more data */
109 memmove( pv->buf, p, pv->end - pv->pos );
112 bytes = fread( pv->buf + pv->end, 1, 1024 - pv->end, pv->file );
121 } else if ( ( retval == -1 ) && ( errno == EILSEQ ) )
123 hb_error( "Invalid byte for codeset in input, discard byte" );
124 /* Try the next byte of the input */
126 } else if ( ( retval == -1 ) && ( errno == E2BIG ) )
135 static int get_line( hb_work_private_t * pv, char *buf, int size )
140 /* Find newline in converted UTF-8 buffer */
141 for( i = 0; i < size - 1; i++ )
143 if( pv->utf8_pos >= pv->utf8_end )
145 if( !utf8_fill( pv ) )
153 c = pv->utf8_buf[pv->utf8_pos++];
167 * Read the SRT file and put the entries into the subtitle fifo for all to read
169 static hb_buffer_t *srt_read( hb_work_private_t *pv )
171 char line_buffer[1024];
178 while( get_line( pv, line_buffer, sizeof( line_buffer ) ) )
180 switch (pv->current_state)
182 case k_state_timecode:
184 struct start_and_end timing = read_time_from_string( line_buffer );
185 pv->current_entry.duration = timing.end - timing.start;
186 pv->current_entry.offset = timing.start - pv->current_time;
188 pv->current_time = timing.end;
190 pv->current_entry.start = timing.start;
191 pv->current_entry.stop = timing.end;
193 pv->current_state = k_state_inEntry;
197 case k_state_inEntry:
202 // If the current line is empty, we assume this is the
203 // seperation betwene two entries. In case we are wrong,
204 // the mistake is corrected in the next state.
205 if (strcmp(line_buffer, "\n") == 0 || strcmp(line_buffer, "\r\n") == 0) {
206 pv->current_state = k_state_potential_new_entry;
210 q = pv->current_entry.text + pv->current_entry.pos;
211 len = strlen( line_buffer );
212 size = MIN(1024 - pv->current_entry.pos - 1, len );
213 memcpy(q, line_buffer, size);
214 pv->current_entry.pos += size;
215 pv->current_entry.text[pv->current_entry.pos] = '\0';
219 case k_state_potential_new_entry:
221 const char endpoint[] = "\0";
222 const unsigned long potential_entry_number = strtol(line_buffer, (char**)&endpoint, 10);
223 hb_buffer_t *buffer = NULL;
225 * Is this really new next entry begin?
227 if (potential_entry_number == pv->number_of_entries + 1)
230 * We found the next entry - or a really rare error condition
232 if( *pv->current_entry.text )
236 uint64_t start_time = ( pv->current_entry.start +
237 pv->subtitle->config.offset ) * 90;
238 uint64_t stop_time = ( pv->current_entry.stop +
239 pv->subtitle->config.offset ) * 90;
241 if( !( start_time > pv->start_time && stop_time < pv->stop_time ) )
243 hb_deep_log( 3, "Discarding SRT at time start %"PRId64", stop %"PRId64, start_time, stop_time);
244 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
245 ++(pv->number_of_entries);
246 pv->current_state = k_state_timecode;
250 length = strlen( pv->current_entry.text );
252 for( p = pv->current_entry.text; *p; p++)
254 if( *p == '\n' || *p == '\r' )
260 buffer = hb_buffer_init( length + 1 );
264 buffer->start = start_time - pv->start_time;
265 buffer->stop = stop_time - pv->start_time;
267 memcpy( buffer->data, pv->current_entry.text, length + 1 );
270 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
271 ++(pv->number_of_entries);
272 pv->current_state = k_state_timecode;
282 * Well.. looks like we are in the wrong mode.. lets add the
283 * newline we misinterpreted...
285 strncat(pv->current_entry.text, " ", 1024);
286 pv->current_state = k_state_inEntry;
297 static int decsrtInit( hb_work_object_t * w, hb_job_t * job )
300 hb_work_private_t * pv;
303 hb_chapter_t * chapter;
304 hb_title_t *title = job->title;
306 pv = calloc( 1, sizeof( hb_work_private_t ) );
309 w->private_data = pv;
313 buffer = hb_buffer_init( 0 );
314 hb_fifo_push( w->fifo_in, buffer);
316 pv->file = fopen( w->subtitle->config.src_filename, "r" );
318 pv->current_state = k_state_potential_new_entry;
319 pv->number_of_entries = 0;
320 pv->current_time = 0;
321 pv->subtitle = w->subtitle;
324 * Figure out the start and stop times from teh chapters being
325 * encoded - drop subtitle not in this range.
328 for( i = 1; i < job->chapter_start; ++i )
330 chapter = hb_list_item( title->list_chapter, i - 1 );
333 pv->start_time += chapter->duration;
335 hb_error( "Could not locate chapter %d for SRT start time", i );
339 pv->stop_time = pv->start_time;
340 for( i = job->chapter_start; i <= job->chapter_end; ++i )
342 chapter = hb_list_item( title->list_chapter, i - 1 );
345 pv->stop_time += chapter->duration;
347 hb_error( "Could not locate chapter %d for SRT start time", i );
352 hb_deep_log( 3, "SRT Start time %"PRId64", stop time %"PRId64, pv->start_time, pv->stop_time);
354 pv->iconv_context = iconv_open( "utf-8", pv->subtitle->config.src_codeset );
357 if( pv->iconv_context == (iconv_t) -1 )
359 hb_error("Could not open the iconv library with those file formats\n");
362 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
364 pv->file = fopen( w->subtitle->config.src_filename, "r" );
368 hb_error("Could not open the SRT subtitle file '%s'\n",
369 w->subtitle->config.src_filename);
379 static int decsrtWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
380 hb_buffer_t ** buf_out )
382 hb_work_private_t * pv = w->private_data;
383 hb_buffer_t * in = *buf_in;
384 hb_buffer_t * out = NULL;
386 out = srt_read( pv );
391 * Keep a buffer in our input fifo so that we get run.
393 hb_fifo_push( w->fifo_in, in);
404 static void decsrtClose( hb_work_object_t * w )
406 hb_work_private_t * pv = w->private_data;
408 iconv_close(pv->iconv_context);
409 free( w->private_data );
412 hb_work_object_t hb_decsrtsub =
415 "SRT Subtitle Decoder",