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;
31 * Store all context in the work private struct,
33 struct hb_work_private_s
37 unsigned long current_time;
38 unsigned long number_of_entries;
39 unsigned long current_state;
40 srt_entry_t current_entry;
41 iconv_t *iconv_context;
42 hb_subtitle_t *subtitle;
43 uint64_t start_time; // In HB time
44 uint64_t stop_time; // In HB time
47 static struct start_and_end read_time_from_string( const char* timeString )
49 // for ex. 00:00:15,248 --> 00:00:16,545
51 long houres1, minutes1, seconds1, milliseconds1,
52 houres2, minutes2, seconds2, milliseconds2;
54 sscanf(timeString, "%ld:%ld:%ld,%ld --> %ld:%ld:%ld,%ld\n", &houres1, &minutes1, &seconds1, &milliseconds1,
55 &houres2, &minutes2, &seconds2, &milliseconds2);
57 struct start_and_end result = {
58 milliseconds1 + seconds1*1000 + minutes1*60*1000 + houres1*60*60*1000,
59 milliseconds2 + seconds2*1000 + minutes2*60*1000 + houres2*60*60*1000};
64 * Read the SRT file and put the entries into the subtitle fifo for all to read
66 static hb_buffer_t *srt_read( hb_work_private_t *pv )
69 char line_buffer[1024];
76 while( fgets( line_buffer, sizeof( line_buffer ), pv->file ) )
78 switch (pv->current_state)
80 case k_state_timecode:
82 struct start_and_end timing = read_time_from_string( line_buffer );
83 pv->current_entry.duration = timing.end - timing.start;
84 pv->current_entry.offset = timing.start - pv->current_time;
86 pv->current_time = timing.end;
88 pv->current_entry.start = timing.start;
89 pv->current_entry.stop = timing.end;
91 pv->current_state = k_state_inEntry;
102 // If the current line is empty, we assume this is the
103 // seperation betwene two entries. In case we are wrong,
104 // the mistake is corrected in the next state.
105 if (strcmp(line_buffer, "\n") == 0 || strcmp(line_buffer, "\r\n") == 0) {
106 pv->current_state = k_state_potential_new_entry;
111 for( q = pv->current_entry.text; (q < pv->current_entry.text+1024) && *q; q++);
115 in_size = strlen(line_buffer);
116 out_size = (pv->current_entry.text+1024) - q;
118 retval = iconv( pv->iconv_context, &p, &in_size, &q, &out_size);
121 if( ( retval == -1 ) && ( errno == EINVAL ) )
123 hb_error( "Invalid shift sequence" );
124 } else if ( ( retval == -1 ) && ( errno == EILSEQ ) )
126 hb_error( "Invalid byte for codeset in input, %"PRId64" bytes discarded", (int64_t)in_size);
127 } else if ( ( retval == -1 ) && ( errno == E2BIG ) )
129 hb_error( "Not enough space in output buffer");
135 case k_state_potential_new_entry:
137 const char endpoint[] = "\0";
138 const unsigned long potential_entry_number = strtol(line_buffer, (char**)&endpoint, 10);
139 hb_buffer_t *buffer = NULL;
141 * Is this really new next entry begin?
143 if (potential_entry_number == pv->number_of_entries + 1) {
145 * We found the next entry - or a really rare error condition
147 if( *pv->current_entry.text )
151 uint64_t start_time = ( pv->current_entry.start +
152 pv->subtitle->config.offset ) * 90;
153 uint64_t stop_time = ( pv->current_entry.stop +
154 pv->subtitle->config.offset ) * 90;
156 if( !( start_time > pv->start_time && stop_time < pv->stop_time ) )
158 hb_deep_log( 3, "Discarding SRT at time start %"PRId64", stop %"PRId64, start_time, stop_time);
159 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
160 ++(pv->number_of_entries);
161 pv->current_state = k_state_timecode;
165 length = strlen( pv->current_entry.text );
167 for( p = pv->current_entry.text; *p; p++)
169 if( *p == '\n' || *p == '\r' )
175 buffer = hb_buffer_init( length + 1 );
179 buffer->start = start_time - pv->start_time;
180 buffer->stop = stop_time - pv->start_time;
182 memcpy( buffer->data, pv->current_entry.text, length + 1 );
185 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
186 ++(pv->number_of_entries);
187 pv->current_state = k_state_timecode;
195 * Well.. looks like we are in the wrong mode.. lets add the
196 * newline we misinterpreted...
198 strncat(pv->current_entry.text, " ", 1024);
199 pv->current_state = k_state_inEntry;
210 static int decsrtInit( hb_work_object_t * w, hb_job_t * job )
213 hb_work_private_t * pv;
216 hb_chapter_t * chapter;
217 hb_title_t *title = job->title;
219 pv = calloc( 1, sizeof( hb_work_private_t ) );
222 w->private_data = pv;
226 buffer = hb_buffer_init( 0 );
227 hb_fifo_push( w->fifo_in, buffer);
229 pv->file = fopen( w->subtitle->config.src_filename, "r" );
231 pv->current_state = k_state_potential_new_entry;
232 pv->number_of_entries = 0;
233 pv->current_time = 0;
234 pv->subtitle = w->subtitle;
237 * Figure out the start and stop times from teh chapters being
238 * encoded - drop subtitle not in this range.
241 for( i = 1; i < job->chapter_start; ++i )
243 chapter = hb_list_item( title->list_chapter, i - 1 );
246 pv->start_time += chapter->duration;
248 hb_error( "Could not locate chapter %d for SRT start time", i );
252 chapter = hb_list_item( title->list_chapter, i - 1 );
256 pv->stop_time = pv->start_time + chapter->duration;
258 hb_error( "Could not locate chapter %d for SRT stop time", i );
262 hb_deep_log( 3, "SRT Start time %"PRId64", stop time %"PRId64, pv->start_time, pv->stop_time);
264 pv->iconv_context = iconv_open( "utf8", pv->subtitle->config.src_codeset );
267 if( pv->iconv_context == (iconv_t) -1 )
269 hb_error("Could not open the iconv library with those file formats\n");
272 memset( &pv->current_entry, 0, sizeof( srt_entry_t ) );
274 pv->file = fopen( w->subtitle->config.src_filename, "r" );
278 hb_error("Could not open the SRT subtitle file '%s'\n",
279 w->subtitle->config.src_filename);
289 static int decsrtWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
290 hb_buffer_t ** buf_out )
292 hb_work_private_t * pv = w->private_data;
293 hb_buffer_t * in = *buf_in;
294 hb_buffer_t * out = NULL;
296 out = srt_read( pv );
301 * Keep a buffer in our input fifo so that we get run.
303 hb_fifo_push( w->fifo_in, in);
314 static void decsrtClose( hb_work_object_t * w )
316 hb_work_private_t * pv = w->private_data;
318 iconv_close(pv->iconv_context);
319 free( w->private_data );
322 hb_work_object_t hb_decsrtsub =
325 "SRT Subtitle Decoder",