OSDN Git Service

Don't drop subtitles when crossing PTS discontinuities by using buffer sequence numbe...
[handbrake-jp/handbrake-jp-git.git] / libhb / common.h
1 /* $Id: common.h,v 1.51 2005/11/04 13:09:40 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #ifndef HB_COMMON_H
8 #define HB_COMMON_H
9
10 #include <math.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdarg.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <inttypes.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <dirent.h>
20
21 #ifndef MIN
22 #define MIN( a, b ) ( (a) > (b) ? (b) : (a) )
23 #endif
24 #ifndef MAX
25 #define MAX( a, b ) ( (a) > (b) ? (a) : (b) )
26 #endif
27
28 #define EVEN( a )        ( (a) + ( (a) & 1 ) )
29 #define MULTIPLE_16( a ) ( 16 * ( ( (a) + 8 ) / 16 ) )
30
31 #define HB_DVD_READ_BUFFER_SIZE 2048
32
33 typedef struct hb_handle_s hb_handle_t;
34 typedef struct hb_list_s hb_list_t;
35 typedef struct hb_rate_s hb_rate_t;
36 typedef struct hb_mixdown_s hb_mixdown_t;
37 typedef struct hb_job_s  hb_job_t;
38 typedef struct hb_title_s hb_title_t;
39 typedef struct hb_chapter_s hb_chapter_t;
40 typedef struct hb_audio_s hb_audio_t;
41 typedef struct hb_subtitle_s hb_subtitle_t;
42 typedef struct hb_state_s hb_state_t;
43 typedef union  hb_esconfig_u     hb_esconfig_t;
44 typedef struct hb_work_private_s hb_work_private_t;
45 typedef struct hb_work_object_s  hb_work_object_t;
46 typedef struct hb_filter_private_s hb_filter_private_t;
47 typedef struct hb_filter_object_s  hb_filter_object_t;
48 typedef struct hb_buffer_s hb_buffer_t;
49 typedef struct hb_fifo_s hb_fifo_t;
50 typedef struct hb_lock_s hb_lock_t;
51
52 #include "ports.h"
53 #ifdef __LIBHB__
54 #include "internal.h"
55 #endif
56
57 hb_list_t * hb_list_init();
58 int         hb_list_count( hb_list_t * );
59 void        hb_list_add( hb_list_t *, void * );
60 void        hb_list_rem( hb_list_t *, void * );
61 void      * hb_list_item( hb_list_t *, int );
62 void        hb_list_close( hb_list_t ** );
63
64 void hb_reduce( int *x, int *y, int num, int den );
65
66 #define HB_KEEP_WIDTH  0
67 #define HB_KEEP_HEIGHT 1
68 void hb_fix_aspect( hb_job_t * job, int keep );
69
70 int hb_calc_bitrate( hb_job_t *, int size );
71
72 struct hb_rate_s
73 {
74     char * string;
75     int    rate;
76 };
77
78 struct hb_mixdown_s
79 {
80     char * human_readable_name;
81     char * internal_name;
82     char * short_name;
83     int    amixdown;
84 };
85
86 #define HB_ASPECT_BASE 9
87 #define HB_VIDEO_RATE_BASE   27000000
88
89 extern hb_rate_t    hb_video_rates[];
90 extern int          hb_video_rates_count;
91 extern hb_rate_t    hb_audio_rates[];
92 extern int          hb_audio_rates_count;
93 extern int          hb_audio_rates_default;
94 extern hb_rate_t    hb_audio_bitrates[];
95 extern int          hb_audio_bitrates_count;
96 extern int          hb_audio_bitrates_default;
97 extern hb_mixdown_t hb_audio_mixdowns[];
98 extern int          hb_audio_mixdowns_count;
99 int hb_mixdown_get_mixdown_from_short_name( const char * short_name );
100 const char * hb_mixdown_get_short_name_from_mixdown( int amixdown );
101
102 /******************************************************************************
103  * hb_job_t: settings to be filled by the UI
104  *****************************************************************************/
105 struct hb_job_s
106 {
107     /* ID assigned by UI so it can groups job passes together */
108     int             sequence_id;
109         
110     /* Pointer to the title to be ripped */
111     hb_title_t    * title;
112     
113     /* Chapter selection */
114     int             chapter_start;
115     int             chapter_end;
116
117         /* Include chapter marker track in mp4? */
118     int             chapter_markers;
119
120     /* Picture settings:
121          crop:                must be multiples of 2 (top/bottom/left/right)
122          deinterlace:         0 or 1
123          width:               must be a multiple of 16
124          height:              must be a multiple of 16
125          keep_ratio:          used by UIs 
126          pixel_ratio:         store pixel aspect ratio in the video
127          pixel_aspect_width:  numerator for pixel aspect ratio
128          pixel_aspect_height: denominator for pixel aspect ratio
129                  maxWidth:                        keep width below this
130                  maxHeight:                       keep height below this */
131
132     int             crop[4];
133     int             deinterlace;
134     hb_list_t     * filters;
135     int             width;
136     int             height;
137     int             keep_ratio;
138     int             grayscale;
139     int             pixel_ratio;
140     int             pixel_aspect_width;
141     int             pixel_aspect_height;
142         int                             maxWidth;
143         int                             maxHeight;
144
145
146     /* Video settings:
147          vcodec:            output codec
148          vquality:          output quality (0.0..1.0)
149                             if < 0.0 or > 1.0, bitrate is used instead
150          vbitrate:          output bitrate (kbps)
151          pass:              0, 1 or 2 (or -1 for scan)
152          vrate, vrate_base: output framerate is vrate / vrate_base
153          h264_level:        boolean for whether or not we're encoding for iPod
154          crf:               boolean for whether to use constant rate factor with x264
155          x264opts:          string of extra x264 options 
156          areBframes:        boolean to note if b-frames are included in x264opts */
157 #define HB_VCODEC_MASK   0x0000FF
158 #define HB_VCODEC_FFMPEG 0x000001
159 #define HB_VCODEC_XVID   0x000002
160 #define HB_VCODEC_X264   0x000004
161
162     int             vcodec;
163     float           vquality;
164     int             vbitrate;
165     int             vrate;
166     int             vrate_base;
167     int             pass;
168     int             h264_13;
169     int             h264_level;
170     int             crf;
171     char            *x264opts;
172     int             areBframes;
173     
174     /* Audio tracks:
175          audios:          Indexes in hb_title_t's audios list, starting from 0.
176                           -1 indicates the end of the list
177         audio_mixdowns:  The mixdown to be used for each audio track in audios[] */
178
179 /* define some masks, used to extract the various information from the HB_AMIXDOWN_XXXX values */
180 #define HB_AMIXDOWN_DCA_FORMAT_MASK             0x00FFF000
181 #define HB_AMIXDOWN_A52_FORMAT_MASK             0x00000FF0
182 #define HB_AMIXDOWN_DISCRETE_CHANNEL_COUNT_MASK 0x0000000F
183
184 /* define the HB_AMIXDOWN_XXXX values */
185
186 #define HB_AMIXDOWN_MONO                        0x01000001
187 // DCA_FORMAT of DCA_MONO                  = 0    = 0x000
188 // A52_FORMAT of A52_MONO                  = 1    = 0x01
189 // discrete channel count of 1
190
191 #define HB_AMIXDOWN_STEREO                      0x02002022
192 // DCA_FORMAT of DCA_STEREO                = 2    = 0x002
193 // A52_FORMAT of A52_STEREO                = 2    = 0x02
194 // discrete channel count of 2
195
196 #define HB_AMIXDOWN_DOLBY                       0x042070A2
197 // DCA_FORMAT of DCA_3F1R | DCA_OUT_DPLI   = 519  = 0x207
198 // A52_FORMAT of A52_DOLBY                 = 10   = 0x0A
199 // discrete channel count of 2
200
201 #define HB_AMIXDOWN_DOLBYPLII                   0x084094A2
202 // DCA_FORMAT of DCA_3F2R | DCA_OUT_DPLII  = 1033 = 0x409
203 // A52_FORMAT of A52_DOLBY | A52_USE_DPLII = 74   = 0x4A
204 // discrete channel count of 2
205
206 #define HB_AMIXDOWN_6CH                         0x10089176
207 // DCA_FORMAT of DCA_3F2R | DCA_LFE        = 137  = 0x089
208 // A52_FORMAT of A52_3F2R | A52_LFE        = 23   = 0x17
209 // discrete channel count of 6
210
211 /* define some macros to extract the various information from the HB_AMIXDOWN_XXXX values */
212 #define HB_AMIXDOWN_GET_DCA_FORMAT( a ) ( ( a & HB_AMIXDOWN_DCA_FORMAT_MASK ) >> 12 )
213 #define HB_AMIXDOWN_GET_A52_FORMAT( a ) ( ( a & HB_AMIXDOWN_A52_FORMAT_MASK ) >> 4 )
214 #define HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT( a ) ( ( a & HB_AMIXDOWN_DISCRETE_CHANNEL_COUNT_MASK ) )
215
216     int             audios[8];
217         int             audio_mixdowns[8];
218
219     /* Audio settings:
220          acodec:   output codec
221          abitrate: output bitrate (kbps)
222          arate:    output samplerate (Hz)
223        HB_ACODEC_AC3 means pass-through, then abitrate and arate are
224        ignored */
225 #define HB_ACODEC_MASK   0x00FF00
226 #define HB_ACODEC_FAAC   0x000100
227 #define HB_ACODEC_LAME   0x000200
228 #define HB_ACODEC_VORBIS 0x000400
229 #define HB_ACODEC_AC3    0x000800
230 #define HB_ACODEC_MPGA   0x001000
231 #define HB_ACODEC_LPCM   0x002000
232 #define HB_ACODEC_DCA    0x004000
233     int             acodec;
234     int             abitrate;
235     int             arate;
236
237     /* Subtitle settings:
238          subtitle: index in hb_title_t's subtitles list, starting
239          from 0. -1 means no subtitle */
240     int             subtitle;
241     int                         subtitleSmartAdjust;
242
243     /* Muxer settings
244          mux:  output file format
245          file: file path */
246 #define HB_MUX_MASK 0xFF0000
247 #define HB_MUX_MP4  0x010000
248 #define HB_MUX_PSP  0x020000
249 #define HB_MUX_AVI  0x040000
250 #define HB_MUX_OGM  0x080000
251 #define HB_MUX_IPOD 0x100000
252 #define HB_MUX_MKV  0x200000
253         
254     int             mux;
255     const char          * file;
256
257     /* Allow MP4 files > 4 gigs */
258     int             largeFileSize;
259     
260
261     int indepth_scan;
262     hb_subtitle_t ** select_subtitle;
263     int subtitle_force;
264     char * native_language;
265
266 #ifdef __LIBHB__
267     /* Internal data */
268     hb_handle_t   * h;
269     hb_lock_t     * pause;
270     volatile int  * die;
271     volatile int    done;
272
273     hb_fifo_t     * fifo_mpeg2;   /* MPEG-2 video ES */
274     hb_fifo_t     * fifo_raw;     /* Raw pictures */
275     hb_fifo_t     * fifo_sync;    /* Raw pictures, framerate corrected */
276     hb_fifo_t     * fifo_render;  /* Raw pictures, scaled */
277     hb_fifo_t     * fifo_mpeg4;   /* MPEG-4 video ES */
278
279     hb_thread_t   * reader;
280     hb_thread_t   * muxer;
281
282     hb_list_t     * list_work;
283
284     hb_esconfig_t config;
285
286     hb_mux_data_t * mux_data;
287 #endif
288 };
289
290 struct hb_audio_s
291 {
292     int  id;
293     char lang[1024];
294     char lang_simple[1024];
295     char iso639_2[4];
296     int  codec;
297     int  rate;
298     int  bitrate;
299     
300     /* ac3flags is only set when the source audio format is HB_ACODEC_AC3 */
301     int ac3flags;
302
303     /* dcaflags is only set when the source audio format is HB_ACODEC_DCA */
304     int dcaflags;
305
306 /* define some masks, used to extract the various information from the HB_AMIXDOWN_XXXX values */
307 #define HB_INPUT_CH_LAYOUT_DISCRETE_FRONT_MASK  0x00F0000
308 #define HB_INPUT_CH_LAYOUT_DISCRETE_REAR_MASK   0x000F000
309 #define HB_INPUT_CH_LAYOUT_DISCRETE_LFE_MASK    0x0000F00
310 #define HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK 0xFFFF0FF
311 #define HB_INPUT_CH_LAYOUT_ENCODED_FRONT_MASK   0x00000F0
312 #define HB_INPUT_CH_LAYOUT_ENCODED_REAR_MASK    0x000000F
313
314 /* define the input channel layouts used to describe the channel layout of this audio */
315 #define HB_INPUT_CH_LAYOUT_MONO    0x0110010
316 #define HB_INPUT_CH_LAYOUT_STEREO  0x0220020
317 #define HB_INPUT_CH_LAYOUT_DOLBY   0x0320031
318 #define HB_INPUT_CH_LAYOUT_3F      0x0430030
319 #define HB_INPUT_CH_LAYOUT_2F1R    0x0521021
320 #define HB_INPUT_CH_LAYOUT_3F1R    0x0631031
321 #define HB_INPUT_CH_LAYOUT_2F2R    0x0722022
322 #define HB_INPUT_CH_LAYOUT_3F2R    0x0832032
323 #define HB_INPUT_CH_LAYOUT_4F2R    0x0942042
324 #define HB_INPUT_CH_LAYOUT_HAS_LFE 0x0000100
325
326 /* define some macros to extract the various information from the HB_AMIXDOWN_XXXX values */
327 #define HB_INPUT_CH_LAYOUT_GET_DISCRETE_FRONT_COUNT( a ) ( ( a & HB_INPUT_CH_LAYOUT_DISCRETE_FRONT_MASK ) >> 16 )
328 #define HB_INPUT_CH_LAYOUT_GET_DISCRETE_REAR_COUNT( a )  ( ( a & HB_INPUT_CH_LAYOUT_DISCRETE_REAR_MASK ) >> 12 )
329 #define HB_INPUT_CH_LAYOUT_GET_DISCRETE_LFE_COUNT( a )   ( ( a & HB_INPUT_CH_LAYOUT_DISCRETE_LFE_MASK ) >> 8 )
330 #define HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT( a ) ( ( ( a & HB_INPUT_CH_LAYOUT_DISCRETE_FRONT_MASK ) >> 16 ) + ( ( a & HB_INPUT_CH_LAYOUT_DISCRETE_REAR_MASK ) >> 12 ) + ( ( a & HB_INPUT_CH_LAYOUT_DISCRETE_LFE_MASK ) >> 8 ) )
331 #define HB_INPUT_CH_LAYOUT_GET_ENCODED_FRONT_COUNT( a )   ( ( a & HB_INPUT_CH_LAYOUT_ENCODED_FRONT_MASK ) >> 4 )
332 #define HB_INPUT_CH_LAYOUT_GET_ENCODED_REAR_COUNT( a )   ( ( a & HB_INPUT_CH_LAYOUT_ENCODED_REAR_MASK ) )
333
334         /* input_channel_layout is the channel layout of this audio */
335         /* this is used to provide a common way of describing the source audio */
336         int input_channel_layout;
337
338 #ifdef __LIBHB__
339     /* Internal data */
340     hb_fifo_t * fifo_in;   /* AC3/MPEG/LPCM ES */
341     hb_fifo_t * fifo_raw;  /* Raw audio */
342     hb_fifo_t * fifo_sync; /* Resampled, synced raw audio */
343     hb_fifo_t * fifo_out;  /* MP3/AAC/Vorbis ES */
344
345     hb_esconfig_t config;
346     hb_mux_data_t * mux_data;
347
348         /* amixdown is the mixdown format to be used for this audio track */
349         int amixdown;
350
351         /* Source PID is only valid for MPEG Transport Streams */
352         int source_pid;
353 #endif
354 };
355
356 struct hb_chapter_s
357 {
358     int      index;
359     int      cell_start;
360     int      cell_end;
361     int      block_start;
362     int      block_end;
363     int      block_count;
364
365     /* Visual-friendly duration */
366     int      hours;
367     int      minutes;
368     int      seconds;
369
370     /* Exact duration (in 1/90000s) */
371     uint64_t duration;
372     
373     /* Optional chapter title */
374     char     title[1024];
375 };
376
377 struct hb_subtitle_s
378 {
379     int  id;
380     char lang[1024];
381     char iso639_2[4];
382
383     int hits;     /* How many hits/occurrences of this subtitle */
384     int forced_hits; /* How many forced hits in this subtitle */
385
386 #ifdef __LIBHB__
387     /* Internal data */
388     hb_fifo_t * fifo_in;  /* SPU ES */
389     hb_fifo_t * fifo_raw; /* Decodec SPU */
390 #endif
391 };
392
393 struct hb_title_s
394 {
395     char        dvd[1024];
396     char        name[1024];
397     int         index;
398     int         vts;
399     int         ttn;
400     int         cell_start;
401     int         cell_end;
402     int         block_start;
403     int         block_end;
404     int         block_count;
405
406     /* Visual-friendly duration */
407     int         hours;
408     int         minutes;
409     int         seconds;
410
411     /* Exact duration (in 1/90000s) */
412     uint64_t    duration;
413
414     int         width;
415     int         height;
416     int         aspect;
417     int         rate;
418     int         rate_base;
419     int         crop[4];
420
421     uint32_t    palette[16];
422
423     hb_list_t * list_chapter;
424     hb_list_t * list_audio;
425     hb_list_t * list_subtitle;
426
427     /* Job template for this title */
428     hb_job_t  * job;
429 };
430
431
432 struct hb_state_s
433 {
434 #define HB_STATE_IDLE     1
435 #define HB_STATE_SCANNING 2
436 #define HB_STATE_SCANDONE 4
437 #define HB_STATE_WORKING  8
438 #define HB_STATE_PAUSED   16
439 #define HB_STATE_WORKDONE 32
440 #define HB_STATE_MUXING   64
441     int state;
442
443     union
444     {
445         struct
446         {
447             /* HB_STATE_SCANNING */
448             int title_cur;
449             int title_count;
450         } scanning;
451
452         struct
453         {
454             /* HB_STATE_WORKING */
455             float progress;
456             int   job_cur;
457             int   job_count;
458             float rate_cur;
459             float rate_avg;
460             int   hours;
461             int   minutes;
462             int   seconds;
463         } working;
464
465         struct
466         {
467             /* HB_STATE_WORKDONE */
468 #define HB_ERROR_NONE     0
469 #define HB_ERROR_CANCELED 1
470 #define HB_ERROR_UNKNOWN  2
471             int error;
472         } workdone;
473
474         struct
475         {
476             /* HB_STATE_MUXING */
477             float progress;
478         } muxing;
479     } param;
480 };
481
482 struct hb_work_object_s
483 {
484     int                 id;
485     char              * name;
486
487 #ifdef __LIBHB__
488     int              (* init)  ( hb_work_object_t *, hb_job_t * );
489     int              (* work)  ( hb_work_object_t *, hb_buffer_t **,
490                                  hb_buffer_t ** );
491     void             (* close) ( hb_work_object_t * );
492
493     hb_fifo_t         * fifo_in;
494     hb_fifo_t         * fifo_out;
495     hb_esconfig_t     * config;
496
497         /* amixdown is the mixdown format to be used if the work object is an audio track */
498         int               amixdown;
499     /* source_acodec is the source audio codec if the work object is an audio track */
500     int               source_acodec;
501
502     hb_work_private_t * private_data;
503
504     hb_thread_t       * thread;
505     volatile int      * done;
506
507     hb_work_object_t  * next;
508         int                               thread_sleep_interval;
509 #endif
510 };
511
512 extern hb_work_object_t hb_sync;
513 extern hb_work_object_t hb_decmpeg2;
514 extern hb_work_object_t hb_decsub;
515 extern hb_work_object_t hb_render;
516 extern hb_work_object_t hb_encavcodec;
517 extern hb_work_object_t hb_encxvid;
518 extern hb_work_object_t hb_encx264;
519 extern hb_work_object_t hb_deca52;
520 extern hb_work_object_t hb_decdca;
521 extern hb_work_object_t hb_decavcodec;
522 extern hb_work_object_t hb_declpcm;
523 extern hb_work_object_t hb_encfaac;
524 extern hb_work_object_t hb_enclame;
525 extern hb_work_object_t hb_encvorbis;
526
527 #define FILTER_OK      0
528 #define FILTER_DELAY   1
529 #define FILTER_FAILED  2
530 #define FILTER_DROP    3
531
532 struct hb_filter_object_s
533 {
534     int                     id;
535     char                  * name;
536     char                  * settings;
537
538 #ifdef __LIBHB__
539     hb_filter_private_t* (* init)  ( int, int, int, char * );
540     
541     int                  (* work)  ( const hb_buffer_t *, hb_buffer_t **,
542                                      int, int, int, hb_filter_private_t * );
543     
544     void                 (* close) ( hb_filter_private_t * );
545     
546     hb_filter_private_t   * private_data;
547     //hb_buffer_t           * buffer;
548 #endif
549 };
550
551 extern hb_filter_object_t hb_filter_detelecine;
552 extern hb_filter_object_t hb_filter_deinterlace;
553 extern hb_filter_object_t hb_filter_deblock;
554 extern hb_filter_object_t hb_filter_denoise;
555
556 typedef void hb_error_handler_t( const char *errmsg );
557
558 extern void hb_register_error_handler( hb_error_handler_t * handler );
559
560 #endif