OSDN Git Service

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