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 );
100 p = pv->buf + pv->pos;
101 q = pv->utf8_buf + pv->utf8_end;
102 in_size = pv->end - pv->pos;
104 retval = iconv( pv->iconv_context, &p, &in_size, &q, &out_size);
105 if( q != pv->utf8_buf + pv->utf8_pos )
108 pv->utf8_end = q - pv->utf8_buf;
109 pv->pos = p - pv->buf;
111 if( ( retval == -1 ) && ( errno == EINVAL ) )
113 /* Incomplete multibyte sequence, read more data */
114 memmove( pv->buf, p, pv->end - pv->pos );
117 bytes = fread( pv->buf + pv->end, 1, 1024 - pv->end, pv->file );
126 } else if ( ( retval == -1 ) && ( errno == EILSEQ ) )
128 hb_error( "Invalid byte for codeset in input, discard byte" );
129 /* Try the next byte of the input */
131 } else if ( ( retval == -1 ) && ( errno == E2BIG ) )
140 static int get_line( hb_work_private_t * pv, char *buf, int size )
145 /* Find newline in converted UTF-8 buffer */
146 for( i = 0; i < size - 1; i++ )
148 if( pv->utf8_pos >= pv->utf8_end )
150 if( !utf8_fill( pv ) )
158 c = pv->utf8_buf[pv->utf8_pos++];
172 * Read the SRT file and put the entries into the subtitle fifo for all to read
174 static hb_buffer_t *srt_read( hb_work_private_t *pv )
176 char line_buffer[1024];
183 while( get_line( pv, line_buffer, sizeof( line_buffer ) ) )
185 switch (pv->current_state)
187 case k_state_timecode:
189 struct start_and_end timing = read_time_from_string( line_buffer );
190 pv->current_entry.duration = timing.end - timing.start;
191 pv->current_entry.offset = timing.start - pv->current_time;
193 pv->current_time = timing.end;
195 pv->current_entry.start = timing.start;
196 pv->current_entry.stop = timing.end;
198 pv->current_state = k_state_inEntry;
202 case k_state_inEntry:
207 // If the current line is empty, we assume this is the
208 // seperation betwene two entries. In case we are wrong,
209 // the mistake is corrected in the next state.
210 if (strcmp(line_buffer, "\n") == 0 || strcmp(line_buffer, "\r\n") == 0) {
211 pv->current_state = k_state_potential_new_entry;
215 q = pv->current_entry.text + pv->current_entry.pos;
216 len = strlen( line_buffer );
217 size = MIN(1024 - pv->current_entry.pos - 1, len );
218 memcpy(q, line_buffer, size);
219 pv->current_entry.pos += size;
220 pv->current_entry.text[pv->current_entry.pos] = '\0';
224 case k_state_potential_new_entry:
226 const char endpoint[] = "\0";
227 const unsigned long potential_entry_number = strtol(line_buffer, (char**)&endpoint, 10);
228 hb_buffer_t *buffer = NULL;
230 * Is this really new next entry begin?
232 if (potential_entry_number == pv->number_of_entries + 1)
235 * We found the next entry - or a really rare error condition
237 if( *pv->current_entry.text )
242 uint64_t start_time = ( pv->current_entry.start +
243 pv->subtitle->config.offset ) * 90;
244 uint64_t stop_time = ( pv->current_entry.stop +
245 pv->subtitle->config.offset ) * 90;
247 if( !( start_time > pv->start_time && stop_time < pv->stop_time ) )
249 hb_deep_log( 3, "Discarding SRT at time start %"PRId64", stop %"PRId64, start_time, stop_time);
250 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
251 ++(pv->number_of_entries);
252 pv->current_state = k_state_timecode;
256 length = strlen( pv->current_entry.text );
258 for( q = p = pv->current_entry.text; *p; p++)
273 else if( *p != '\r' )
285 buffer = hb_buffer_init( length + 1 );
289 buffer->start = start_time - pv->start_time;
290 buffer->stop = stop_time - pv->start_time;
292 memcpy( buffer->data, pv->current_entry.text, length + 1 );
295 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
296 ++(pv->number_of_entries);
297 pv->current_state = k_state_timecode;
307 * Well.. looks like we are in the wrong mode.. lets add the
308 * newline we misinterpreted...
310 strncat(pv->current_entry.text, " ", 1024);
311 pv->current_state = k_state_inEntry;
322 static int decsrtInit( hb_work_object_t * w, hb_job_t * job )
325 hb_work_private_t * pv;
328 hb_chapter_t * chapter;
329 hb_title_t *title = job->title;
331 pv = calloc( 1, sizeof( hb_work_private_t ) );
334 w->private_data = pv;
338 buffer = hb_buffer_init( 0 );
339 hb_fifo_push( w->fifo_in, buffer);
341 pv->file = fopen( w->subtitle->config.src_filename, "r" );
343 pv->current_state = k_state_potential_new_entry;
344 pv->number_of_entries = 0;
345 pv->current_time = 0;
346 pv->subtitle = w->subtitle;
349 * Figure out the start and stop times from teh chapters being
350 * encoded - drop subtitle not in this range.
353 for( i = 1; i < job->chapter_start; ++i )
355 chapter = hb_list_item( title->list_chapter, i - 1 );
358 pv->start_time += chapter->duration;
360 hb_error( "Could not locate chapter %d for SRT start time", i );
364 pv->stop_time = pv->start_time;
365 for( i = job->chapter_start; i <= job->chapter_end; ++i )
367 chapter = hb_list_item( title->list_chapter, i - 1 );
370 pv->stop_time += chapter->duration;
372 hb_error( "Could not locate chapter %d for SRT start time", i );
377 hb_deep_log( 3, "SRT Start time %"PRId64", stop time %"PRId64, pv->start_time, pv->stop_time);
379 pv->iconv_context = iconv_open( "utf-8", pv->subtitle->config.src_codeset );
382 if( pv->iconv_context == (iconv_t) -1 )
384 hb_error("Could not open the iconv library with those file formats\n");
387 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
389 pv->file = fopen( w->subtitle->config.src_filename, "r" );
393 hb_error("Could not open the SRT subtitle file '%s'\n",
394 w->subtitle->config.src_filename);
404 static int decsrtWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
405 hb_buffer_t ** buf_out )
407 hb_work_private_t * pv = w->private_data;
408 hb_buffer_t * in = *buf_in;
409 hb_buffer_t * out = NULL;
411 out = srt_read( pv );
416 * Keep a buffer in our input fifo so that we get run.
418 hb_fifo_push( w->fifo_in, in);
429 static void decsrtClose( hb_work_object_t * w )
431 hb_work_private_t * pv = w->private_data;
433 iconv_close(pv->iconv_context);
434 free( w->private_data );
437 hb_work_object_t hb_decsrtsub =
440 "SRT Subtitle Decoder",