1 /* $Id: decsub.c,v 1.12 2005/04/14 17:37:54 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
9 struct hb_work_private_s
34 static hb_buffer_t * Decode( hb_work_object_t * );
36 int decsubInit( hb_work_object_t * w, hb_job_t * job )
38 hb_work_private_t * pv;
40 pv = calloc( 1, sizeof( hb_work_private_t ) );
49 int decsubWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
50 hb_buffer_t ** buf_out )
52 hb_work_private_t * pv = w->private_data;
53 hb_buffer_t * in = *buf_in;
54 int size_sub, size_rle;
58 /* EOF on input stream - send it downstream & say that we're done */
64 pv->stream_id = in->id;
66 size_sub = ( in->data[0] << 8 ) | in->data[1];
67 size_rle = ( in->data[2] << 8 ) | in->data[3];
71 /* We are looking for the start of a new subtitle */
72 if( size_sub && size_rle && size_sub > size_rle &&
73 in->size <= size_sub )
75 /* Looks all right so far */
76 pv->size_sub = size_sub;
77 pv->size_rle = size_rle;
79 memcpy( pv->buf, in->data, in->size );
80 pv->size_got = in->size;
86 /* We are waiting for the end of the current subtitle */
87 if( in->size <= pv->size_sub - pv->size_got )
89 memcpy( pv->buf + pv->size_got, in->data, in->size );
90 pv->size_got += in->size;
100 if( pv->size_sub && pv->size_sub == pv->size_got )
102 /* We got a complete subtitle, decode it */
103 *buf_out = Decode( w );
105 if( buf_out && *buf_out )
107 (*buf_out)->sequence = in->sequence;
110 /* Wait for the next one */
120 void decsubClose( hb_work_object_t * w )
122 free( w->private_data );
125 hb_work_object_t hb_decsub =
135 /***********************************************************************
137 ***********************************************************************
138 * Get the start and end dates (relative to the PTS from the PES
139 * header), the width and height of the subpicture and the colors and
141 **********************************************************************/
142 static void ParseControls( hb_work_object_t * w )
144 hb_work_private_t * pv = w->private_data;
145 hb_job_t * job = pv->job;
146 hb_title_t * title = job->title;
147 hb_subtitle_t * subtitle;
162 for( i = pv->size_rle; ; )
164 date = ( pv->buf[i] << 8 ) | pv->buf[i+1]; i += 2;
165 next = ( pv->buf[i] << 8 ) | pv->buf[i+1]; i += 2;
169 command = pv->buf[i++];
172 * There are eight commands available for
173 * Sub-Pictures. The first SP_DCSQ should contain, as a
174 * minimum, SET_COLOR, SET_CONTR, SET_DAREA, and
178 if( command == 0xFF ) // 0xFF - CMD_END - ends one SP_DCSQ
185 case 0x00: // 0x00 - FSTA_DSP - Forced Start Display, no arguments
186 pv->pts_start = pv->pts + date * 900;
190 * If we are doing a subtitle scan then note down
192 if( job->indepth_scan )
194 for( n=0; n < hb_list_count(title->list_subtitle); n++ )
196 subtitle = hb_list_item( title->list_subtitle, n);
197 if( pv->stream_id == subtitle->id ) {
201 subtitle->forced_hits++;
207 case 0x01: // 0x01 - STA_DSP - Start Display, no arguments
208 pv->pts_start = pv->pts + date * 900;
212 case 0x02: // 0x02 - STP_DSP - Stop Display, no arguments
214 pv->pts_stop = pv->pts + date * 900;
217 case 0x03: // 0x03 - SET_COLOR - Set Colour indices
220 * SET_COLOR - provides four indices into the CLUT
221 * for the current PGC to associate with the four
227 colors[0] = (pv->buf[i+0]>>4)&0x0f;
228 colors[1] = (pv->buf[i+0])&0x0f;
229 colors[2] = (pv->buf[i+1]>>4)&0x0f;
230 colors[3] = (pv->buf[i+1])&0x0f;
232 for( j = 0; j < 4; j++ )
235 * Not sure what is happening here, in theory
236 * the palette is in YCbCr. And we want YUV.
238 * However it looks more like YCrCb (according
239 * to pgcedit). And the scalers for YCrCb don't
240 * work, but I get the right colours by doing
243 uint32_t color = title->palette[colors[j]];
245 y = (color>>16) & 0xff;
246 Cr = (color>>8) & 0xff;
249 pv->chromaU[3-j] = Cb;
250 pv->chromaV[3-j] = Cr;
251 /* hb_log("color[%d] y = %d, u = %d, v = %d",
261 case 0x04: // 0x04 - SET_CONTR - Set Contrast
264 * SET_CONTR - directly provides the four contrast
265 * (alpha blend) values to associate with the four
270 alpha[3] = (pv->buf[i+0]>>4)&0x0f;
271 alpha[2] = (pv->buf[i+0])&0x0f;
272 alpha[1] = (pv->buf[i+1]>>4)&0x0f;
273 alpha[0] = (pv->buf[i+1])&0x0f;
276 int lastAlpha = pv->alpha[3] + pv->alpha[2] + pv->alpha[1] + pv->alpha[0];
277 int currAlpha = alpha[3] + alpha[2] + alpha[1] + alpha[0];
279 // fading-in, save the highest alpha value
280 if( currAlpha > lastAlpha )
282 pv->alpha[3] = alpha[3];
283 pv->alpha[2] = alpha[2];
284 pv->alpha[1] = alpha[1];
285 pv->alpha[0] = alpha[0];
289 if( currAlpha < lastAlpha && !pv->pts_stop )
291 pv->pts_stop = pv->pts + date * 900;
297 case 0x05: // 0x05 - SET_DAREA - defines the display area
299 pv->x = (pv->buf[i+0]<<4) | ((pv->buf[i+1]>>4)&0x0f);
300 pv->width = (((pv->buf[i+1]&0x0f)<<8)| pv->buf[i+2]) - pv->x + 1;
301 pv->y = (pv->buf[i+3]<<4)| ((pv->buf[i+4]>>4)&0x0f);
302 pv->height = (((pv->buf[i+4]&0x0f)<<8)| pv->buf[i+5]) - pv->y + 1;
306 case 0x06: // 0x06 - SET_DSPXA - defines the pixel data addresses
308 pv->offsets[0] = ( pv->buf[i] << 8 ) | pv->buf[i+1]; i += 2;
309 pv->offsets[1] = ( pv->buf[i] << 8 ) | pv->buf[i+1]; i += 2;
326 /* Show it for 3 seconds */
327 pv->pts_stop = pv->pts_start + 3 * 90000;
331 /***********************************************************************
333 ***********************************************************************
334 * Given a raw decoded subtitle, detects transparent borders and
335 * returns a cropped subtitle in a hb_buffer_t ready to be used by
336 * the renderer, or NULL if the subtitle was completely transparent
337 **********************************************************************/
338 static int LineIsTransparent( hb_work_object_t * w, uint8_t * p )
340 hb_work_private_t * pv = w->private_data;
342 for( i = 0; i < pv->width; i++ )
351 static int ColumnIsTransparent( hb_work_object_t * w, uint8_t * p )
353 hb_work_private_t * pv = w->private_data;
355 for( i = 0; i < pv->height; i++ )
364 static hb_buffer_t * CropSubtitle( hb_work_object_t * w, uint8_t * raw )
366 hb_work_private_t * pv = w->private_data;
368 int crop[4] = { -1,-1,-1,-1 };
370 int realwidth, realheight;
372 uint8_t * lum_in, * lum_out, * alpha_in, * alpha_out;
373 uint8_t * u_in, * u_out, * v_in, * v_out;
375 alpha = raw + pv->width * pv->height;
378 for( i = 0; i < pv->height; i++ )
380 if( !LineIsTransparent( w, &alpha[i*pv->width] ) )
394 for( i = pv->height - 1; i >= 0; i-- )
396 if( !LineIsTransparent( w, &alpha[i*pv->width] ) )
404 for( i = 0; i < pv->width; i++ )
406 if( !ColumnIsTransparent( w, &alpha[i] ) )
414 for( i = pv->width - 1; i >= 0; i-- )
416 if( !ColumnIsTransparent( w, &alpha[i] ) )
423 realwidth = crop[3] - crop[2] + 1;
424 realheight = crop[1] - crop[0] + 1;
426 buf = hb_buffer_init( realwidth * realheight * 4 );
427 buf->start = pv->pts_start;
428 buf->stop = pv->pts_stop;
429 buf->x = pv->x + crop[2];
430 buf->y = pv->y + crop[0];
431 buf->width = realwidth;
432 buf->height = realheight;
434 lum_in = raw + crop[0] * pv->width + crop[2];
435 alpha_in = lum_in + pv->width * pv->height;
436 u_in = alpha_in + pv->width * pv->height;
437 v_in = u_in + pv->width * pv->height;
440 alpha_out = lum_out + realwidth * realheight;
441 u_out = alpha_out + realwidth * realheight;
442 v_out = u_out + realwidth * realheight;
444 for( i = 0; i < realheight; i++ )
446 memcpy( lum_out, lum_in, realwidth );
447 memcpy( alpha_out, alpha_in, realwidth );
448 memcpy( u_out, u_in, realwidth );
449 memcpy( v_out, v_in, realwidth );
452 alpha_in += pv->width;
456 lum_out += realwidth;
457 alpha_out += realwidth;
465 static hb_buffer_t * Decode( hb_work_object_t * w )
467 hb_work_private_t * pv = w->private_data;
472 uint8_t * buf_raw = NULL;
473 hb_job_t * job = pv->job;
475 /* Get infos about the subtitle */
478 if( job->indepth_scan || ( job->subtitle_force && pv->pts_forced == 0 ) )
481 * Don't encode subtitles when doing a scan.
483 * When forcing subtitles, ignore all those that don't
484 * have the forced flag set.
489 /* Do the actual decoding now */
490 buf_raw = malloc( ( pv->width * pv->height ) * 4 );
492 #define GET_NEXT_NIBBLE code = ( code << 4 ) | ( ( ( *offset & 1 ) ? \
493 ( pv->buf[((*offset)>>1)] & 0xF ) : ( pv->buf[((*offset)>>1)] >> 4 ) ) ); \
496 offsets[0] = pv->offsets[0] * 2;
497 offsets[1] = pv->offsets[1] * 2;
499 for( line = 0; line < pv->height; line++ )
501 /* Select even or odd field */
502 offset = ( line & 1 ) ? &offsets[1] : &offsets[0];
504 for( col = 0; col < pv->width; col += code >> 2 )
506 uint8_t * lum, * alpha, * chromaU, * chromaV;
522 code |= ( pv->width - col ) << 2;
529 alpha = lum + pv->width * pv->height;
530 chromaU = alpha + pv->width * pv->height;
531 chromaV = chromaU + pv->width * pv->height;
533 memset( lum + line * pv->width + col,
534 pv->lum[code & 3], code >> 2 );
535 memset( alpha + line * pv->width + col,
536 pv->alpha[code & 3], code >> 2 );
537 memset( chromaU + line * pv->width + col,
538 pv->chromaU[code & 3], code >> 2 );
539 memset( chromaV + line * pv->width + col,
540 pv->chromaV[code & 3], code >> 2 );
550 /* Crop subtitle (remove transparent borders) */
551 buf = CropSubtitle( w, buf_raw );