OSDN Git Service

- nasty bug: at eof, dropping a delay queue frame for CFR could link the render...
[handbrake-jp/handbrake-jp-git.git] / libhb / decvobsub.c
1 /* $Id: decsub.c,v 1.12 2005/04/14 17:37:54 titer Exp $
2
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. */
6
7 #include "hb.h"
8
9 struct hb_work_private_s
10 {
11     hb_job_t    * job;
12
13     hb_buffer_t * buf;
14     int           size_sub;
15     int           size_got;
16     int           size_rle;
17     int64_t       pts;
18     int64_t       pts_start;
19     int64_t       pts_stop;
20     int           pts_forced;
21     int           x;
22     int           y;
23     int           width;
24     int           height;
25     int           stream_id;
26
27     int           offsets[2];
28     uint8_t       lum[4];
29     uint8_t       chromaU[4];
30     uint8_t       chromaV[4];
31     uint8_t       alpha[4];
32 };
33
34 static hb_buffer_t * Decode( hb_work_object_t * );
35
36 int decsubInit( hb_work_object_t * w, hb_job_t * job )
37 {
38     hb_work_private_t * pv;
39
40     pv              = calloc( 1, sizeof( hb_work_private_t ) );
41     w->private_data = pv;
42
43     pv->job = job;
44     pv->pts = -1;
45
46     return 0;
47 }
48
49 int decsubWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
50                 hb_buffer_t ** buf_out )
51 {
52     hb_work_private_t * pv = w->private_data;
53     hb_buffer_t * in = *buf_in;
54     int size_sub, size_rle;
55
56     if ( in->size <= 0 )
57     {
58         /* EOF on input stream - send it downstream & say that we're done */
59         *buf_out = in;
60         *buf_in = NULL;
61         return HB_WORK_DONE;
62     }
63
64     pv->stream_id = in->id;
65
66     size_sub = ( in->data[0] << 8 ) | in->data[1];
67     size_rle = ( in->data[2] << 8 ) | in->data[3];
68
69     if( !pv->size_sub )
70     {
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 )
74         {
75             /* Looks all right so far */
76             pv->size_sub = size_sub;
77             pv->size_rle = size_rle;
78
79             pv->buf      = hb_buffer_init( 0xFFFF );
80             memcpy( pv->buf->data, in->data, in->size );
81             pv->buf->id = in->id;
82             pv->buf->sequence = in->sequence;
83             pv->size_got = in->size;
84             pv->pts      = in->start;
85         }
86     }
87     else
88     {
89         /* We are waiting for the end of the current subtitle */
90         if( in->size <= pv->size_sub - pv->size_got )
91         {
92             memcpy( pv->buf->data + pv->size_got, in->data, in->size );
93             pv->buf->id = in->id;
94             pv->buf->sequence = in->sequence;
95             pv->size_got += in->size;
96             if( in->start >= 0 )
97             {
98                 pv->pts = in->start;
99             }
100         }
101     }
102
103     *buf_out = NULL;
104
105     if( pv->size_sub && pv->size_sub == pv->size_got )
106     {
107         pv->buf->size = pv->size_sub;
108
109         /* We got a complete subtitle, decode it */
110         *buf_out = Decode( w );
111
112         if( buf_out && *buf_out )
113         {
114             (*buf_out)->id = in->id;
115             (*buf_out)->sequence = in->sequence;
116         }
117
118         /* Wait for the next one */
119         pv->size_sub = 0;
120         pv->size_got = 0;
121         pv->size_rle = 0;
122         pv->pts      = -1;
123     }
124
125     return HB_WORK_OK;
126 }
127
128 void decsubClose( hb_work_object_t * w )
129 {
130     free( w->private_data );
131 }
132
133 hb_work_object_t hb_decvobsub =
134 {
135     WORK_DECVOBSUB,
136     "VOBSUB decoder",
137     decsubInit,
138     decsubWork,
139     decsubClose
140 };
141
142
143 /***********************************************************************
144  * ParseControls
145  ***********************************************************************
146  * Get the start and end dates (relative to the PTS from the PES
147  * header), the width and height of the subpicture and the colors and
148  * alphas used in it
149  **********************************************************************/
150 static void ParseControls( hb_work_object_t * w )
151 {
152     hb_work_private_t * pv = w->private_data;
153     hb_job_t * job = pv->job;
154     hb_title_t * title = job->title;
155     hb_subtitle_t * subtitle;
156     uint8_t * buf = pv->buf->data;
157
158     int i, n;
159     int command;
160     int date, next;
161
162     pv->pts_start = 0;
163     pv->pts_stop  = 0;
164     pv->pts_forced  = 0;
165
166     pv->alpha[3] = 0;
167     pv->alpha[2] = 0;
168     pv->alpha[1] = 0;
169     pv->alpha[0] = 0;
170
171     for( i = pv->size_rle; ; )
172     {
173         date = ( buf[i] << 8 ) | buf[i+1]; i += 2;
174         next = ( buf[i] << 8 ) | buf[i+1]; i += 2;
175
176         for( ;; )
177         {
178             command = buf[i++];
179
180             /*
181              * There are eight commands available for
182              * Sub-Pictures. The first SP_DCSQ should contain, as a
183              * minimum, SET_COLOR, SET_CONTR, SET_DAREA, and
184              * SET_DSPXA
185              */
186
187             if( command == 0xFF ) // 0xFF - CMD_END - ends one SP_DCSQ
188             {
189                 break;
190             }
191
192             switch( command )
193             {
194                 case 0x00: // 0x00 - FSTA_DSP - Forced Start Display, no arguments
195                     pv->pts_start = pv->pts + date * 900;
196                     pv->pts_forced = 1;
197
198                     /*
199                      * If we are doing a subtitle scan then note down
200                      */
201                     if( job->indepth_scan )
202                     {
203                         for( n=0; n < hb_list_count(title->list_subtitle); n++ )
204                         {
205                             subtitle = hb_list_item( title->list_subtitle, n);
206                             if( pv->stream_id == subtitle->id ) {
207                                 /*
208                                  * A hit, count it.
209                                  */
210                                 subtitle->forced_hits++;
211                             }
212                         }
213                     }
214                     break;
215
216                 case 0x01: // 0x01 - STA_DSP - Start Display, no arguments
217                     pv->pts_start = pv->pts + date * 900;
218                     pv->pts_forced  = 0;
219                     break;
220
221                 case 0x02: // 0x02 - STP_DSP - Stop Display, no arguments
222                     if(!pv->pts_stop)
223                         pv->pts_stop = pv->pts + date * 900;
224                     break;
225
226                 case 0x03: // 0x03 - SET_COLOR - Set Colour indices
227                 {
228                     /*
229                      * SET_COLOR - provides four indices into the CLUT
230                      * for the current PGC to associate with the four
231                      * pixel values
232                      */
233                     int colors[4];
234                     int j;
235
236                     colors[0] = (buf[i+0]>>4)&0x0f;
237                     colors[1] = (buf[i+0])&0x0f;
238                     colors[2] = (buf[i+1]>>4)&0x0f;
239                     colors[3] = (buf[i+1])&0x0f;
240
241                     for( j = 0; j < 4; j++ )
242                     {
243                         /*
244                          * Not sure what is happening here, in theory
245                          * the palette is in YCbCr. And we want YUV.
246                          *
247                          * However it looks more like YCrCb (according
248                          * to pgcedit). And the scalers for YCrCb don't
249                          * work, but I get the right colours by doing
250                          * no conversion.
251                          */
252                         uint32_t color = title->palette[colors[j]];
253                         uint8_t Cr, Cb, y;
254                         y = (color>>16) & 0xff;
255                         Cr = (color>>8) & 0xff;
256                         Cb = (color) & 0xff;
257                         pv->lum[3-j] = y;
258                         pv->chromaU[3-j] = Cb;
259                         pv->chromaV[3-j] = Cr;
260                         /* hb_log("color[%d] y = %d, u = %d, v = %d",
261                                3-j,
262                                pv->lum[3-j],
263                                pv->chromaU[3-j],
264                                pv->chromaV[3-j]);
265                         */
266                     }
267                     i += 2;
268                     break;
269                 }
270                 case 0x04: // 0x04 - SET_CONTR - Set Contrast
271                 {
272                     /*
273                      * SET_CONTR - directly provides the four contrast
274                      * (alpha blend) values to associate with the four
275                      * pixel values
276                      */
277                     uint8_t    alpha[4];
278
279                     alpha[3] = (buf[i+0]>>4)&0x0f;
280                     alpha[2] = (buf[i+0])&0x0f;
281                     alpha[1] = (buf[i+1]>>4)&0x0f;
282                     alpha[0] = (buf[i+1])&0x0f;
283
284
285                     int lastAlpha = pv->alpha[3] + pv->alpha[2] + pv->alpha[1] + pv->alpha[0];
286                     int currAlpha = alpha[3] + alpha[2] + alpha[1] + alpha[0];
287
288                     // fading-in, save the highest alpha value
289                     if( currAlpha > lastAlpha )
290                     {
291                         pv->alpha[3] = alpha[3];
292                         pv->alpha[2] = alpha[2];
293                         pv->alpha[1] = alpha[1];
294                         pv->alpha[0] = alpha[0];
295                     }
296
297                     // fading-out
298                     if( currAlpha < lastAlpha && !pv->pts_stop )
299                     {
300                         pv->pts_stop = pv->pts + date * 900;
301                     }
302
303                     i += 2;
304                     break;
305                 }
306                 case 0x05: // 0x05 - SET_DAREA - defines the display area
307                 {
308                     pv->x     = (buf[i+0]<<4) | ((buf[i+1]>>4)&0x0f);
309                     pv->width = (((buf[i+1]&0x0f)<<8)| buf[i+2]) - pv->x + 1;
310                     pv->y     = (buf[i+3]<<4)| ((buf[i+4]>>4)&0x0f);
311                     pv->height = (((buf[i+4]&0x0f)<<8)| buf[i+5]) - pv->y + 1;
312                     i += 6;
313                     break;
314                 }
315                 case 0x06: // 0x06 - SET_DSPXA - defines the pixel data addresses
316                 {
317                     pv->offsets[0] = ( buf[i] << 8 ) | buf[i+1]; i += 2;
318                     pv->offsets[1] = ( buf[i] << 8 ) | buf[i+1]; i += 2;
319                     break;
320                 }
321             }
322         }
323
324
325
326         if( i > next )
327         {
328             break;
329         }
330         i = next;
331     }
332
333     if( !pv->pts_stop )
334     {
335         /* Show it for 3 seconds */
336         pv->pts_stop = pv->pts_start + 3 * 90000;
337     }
338 }
339
340 /***********************************************************************
341  * CropSubtitle
342  ***********************************************************************
343  * Given a raw decoded subtitle, detects transparent borders and
344  * returns a cropped subtitle in a hb_buffer_t ready to be used by
345  * the renderer, or NULL if the subtitle was completely transparent
346  **********************************************************************/
347 static int LineIsTransparent( hb_work_object_t * w, uint8_t * p )
348 {
349     hb_work_private_t * pv = w->private_data;
350     int i;
351     for( i = 0; i < pv->width; i++ )
352     {
353         if( p[i] )
354         {
355             return 0;
356         }
357     }
358     return 1;
359 }
360 static int ColumnIsTransparent( hb_work_object_t * w, uint8_t * p )
361 {
362     hb_work_private_t * pv = w->private_data;
363     int i;
364     for( i = 0; i < pv->height; i++ )
365     {
366         if( p[i*pv->width] )
367         {
368             return 0;
369         }
370     }
371     return 1;
372 }
373 static hb_buffer_t * CropSubtitle( hb_work_object_t * w, uint8_t * raw )
374 {
375     hb_work_private_t * pv = w->private_data;
376     int i;
377     int crop[4] = { -1,-1,-1,-1 };
378     uint8_t * alpha;
379     int realwidth, realheight;
380     hb_buffer_t * buf;
381     uint8_t * lum_in, * lum_out, * alpha_in, * alpha_out;
382     uint8_t * u_in, * u_out, * v_in, * v_out;
383
384     alpha = raw + pv->width * pv->height;
385
386     /* Top */
387     for( i = 0; i < pv->height; i++ )
388     {
389         if( !LineIsTransparent( w, &alpha[i*pv->width] ) )
390         {
391             crop[0] = i;
392             break;
393         }
394     }
395
396     if( crop[0] < 0 )
397     {
398         /* Empty subtitle */
399         return NULL;
400     }
401
402     /* Bottom */
403     for( i = pv->height - 1; i >= 0; i-- )
404     {
405         if( !LineIsTransparent( w, &alpha[i*pv->width] ) )
406         {
407             crop[1] = i;
408             break;
409         }
410     }
411
412     /* Left */
413     for( i = 0; i < pv->width; i++ )
414     {
415         if( !ColumnIsTransparent( w, &alpha[i] ) )
416         {
417             crop[2] = i;
418             break;
419         }
420     }
421
422     /* Right */
423     for( i = pv->width - 1; i >= 0; i-- )
424     {
425         if( !ColumnIsTransparent( w, &alpha[i] ) )
426         {
427             crop[3] = i;
428             break;
429         }
430     }
431
432     realwidth  = crop[3] - crop[2] + 1;
433     realheight = crop[1] - crop[0] + 1;
434
435     buf         = hb_buffer_init( realwidth * realheight * 4 );
436     buf->start  = pv->pts_start;
437     buf->stop   = pv->pts_stop;
438     buf->x      = pv->x + crop[2];
439     buf->y      = pv->y + crop[0];
440     buf->width  = realwidth;
441     buf->height = realheight;
442
443     lum_in    = raw + crop[0] * pv->width + crop[2];
444     alpha_in  = lum_in + pv->width * pv->height;
445     u_in      = alpha_in + pv->width * pv->height;
446     v_in      = u_in + pv->width * pv->height;
447
448     lum_out   = buf->data;
449     alpha_out = lum_out + realwidth * realheight;
450     u_out     = alpha_out + realwidth * realheight;
451     v_out     = u_out + realwidth * realheight;
452
453     for( i = 0; i < realheight; i++ )
454     {
455         memcpy( lum_out, lum_in, realwidth );
456         memcpy( alpha_out, alpha_in, realwidth );
457         memcpy( u_out, u_in, realwidth );
458         memcpy( v_out, v_in, realwidth );
459
460         lum_in    += pv->width;
461         alpha_in  += pv->width;
462         u_in      += pv->width;
463         v_in      += pv->width;
464
465         lum_out   += realwidth;
466         alpha_out += realwidth;
467         u_out     += realwidth;
468         v_out     += realwidth;
469     }
470
471     return buf;
472 }
473
474 static hb_buffer_t * Decode( hb_work_object_t * w )
475 {
476     hb_work_private_t * pv = w->private_data;
477     int code, line, col;
478     int offsets[2];
479     int * offset;
480     hb_buffer_t * buf;
481     uint8_t * buf_raw = NULL;
482     hb_job_t * job = pv->job;
483
484     /* Get infos about the subtitle */
485     ParseControls( w );
486
487     if( job->indepth_scan || ( w->subtitle->config.force && pv->pts_forced == 0 ) )
488     {
489         /*
490          * Don't encode subtitles when doing a scan.
491          *
492          * When forcing subtitles, ignore all those that don't
493          * have the forced flag set.
494          */
495         return NULL;
496     }
497
498     if (w->subtitle->config.dest == PASSTHRUSUB)
499     {
500         pv->buf->start  = pv->pts_start;
501         pv->buf->stop   = pv->pts_stop;
502         return pv->buf;
503     }
504
505     /* Do the actual decoding now */
506     buf_raw = malloc( ( pv->width * pv->height ) * 4 );
507
508 #define GET_NEXT_NIBBLE code = ( code << 4 ) | ( ( ( *offset & 1 ) ? \
509 ( pv->buf->data[((*offset)>>1)] & 0xF ) : ( pv->buf->data[((*offset)>>1)] >> 4 ) ) ); \
510 (*offset)++
511
512     offsets[0] = pv->offsets[0] * 2;
513     offsets[1] = pv->offsets[1] * 2;
514
515     for( line = 0; line < pv->height; line++ )
516     {
517         /* Select even or odd field */
518         offset = ( line & 1 ) ? &offsets[1] : &offsets[0];
519
520         for( col = 0; col < pv->width; col += code >> 2 )
521         {
522             uint8_t * lum, * alpha,  * chromaU, * chromaV;
523
524             code = 0;
525             GET_NEXT_NIBBLE;
526             if( code < 0x4 )
527             {
528                 GET_NEXT_NIBBLE;
529                 if( code < 0x10 )
530                 {
531                     GET_NEXT_NIBBLE;
532                     if( code < 0x40 )
533                     {
534                         GET_NEXT_NIBBLE;
535                         if( code < 0x100 )
536                         {
537                             /* End of line */
538                             code |= ( pv->width - col ) << 2;
539                         }
540                     }
541                 }
542             }
543
544             lum   = buf_raw;
545             alpha = lum + pv->width * pv->height;
546             chromaU = alpha + pv->width * pv->height;
547             chromaV = chromaU + pv->width * pv->height;
548
549             memset( lum + line * pv->width + col,
550                     pv->lum[code & 3], code >> 2 );
551             memset( alpha + line * pv->width + col,
552                     pv->alpha[code & 3], code >> 2 );
553             memset( chromaU + line * pv->width + col,
554                     pv->chromaU[code & 3], code >> 2 );
555             memset( chromaV + line * pv->width + col,
556                     pv->chromaV[code & 3], code >> 2 );
557         }
558
559         /* Byte-align */
560         if( *offset & 1 )
561         {
562             (*offset)++;
563         }
564     }
565
566     hb_buffer_close( &pv->buf );
567
568     /* Crop subtitle (remove transparent borders) */
569     buf = CropSubtitle( w, buf_raw );
570
571     free( buf_raw );
572
573     return buf;
574 }