OSDN Git Service

MacGui: Thinine's custom file size and x264 fix.
[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 typedef struct hb_handle_s hb_handle_t;
32 typedef struct hb_list_s hb_list_t;
33 typedef struct hb_rate_s hb_rate_t;
34 typedef struct hb_job_s  hb_job_t;
35 typedef struct hb_title_s hb_title_t;
36 typedef struct hb_chapter_s hb_chapter_t;
37 typedef struct hb_audio_s hb_audio_t;
38 typedef struct hb_subtitle_s hb_subtitle_t;
39 typedef struct hb_state_s hb_state_t;
40 typedef union  hb_esconfig_u     hb_esconfig_t;
41 typedef struct hb_work_private_s hb_work_private_t;
42 typedef struct hb_work_object_s  hb_work_object_t;
43 typedef struct hb_buffer_s hb_buffer_t;
44 typedef struct hb_fifo_s hb_fifo_t;
45 typedef struct hb_lock_s hb_lock_t;
46
47 #include "ports.h"
48 #ifdef __LIBHB__
49 #include "internal.h"
50 #endif
51
52 hb_list_t * hb_list_init();
53 int         hb_list_count( hb_list_t * );
54 void        hb_list_add( hb_list_t *, void * );
55 void        hb_list_rem( hb_list_t *, void * );
56 void      * hb_list_item( hb_list_t *, int );
57 void        hb_list_close( hb_list_t ** );
58
59 void hb_reduce( int *x, int *y, int num, int den );
60
61 #define HB_KEEP_WIDTH  0
62 #define HB_KEEP_HEIGHT 1
63 void hb_fix_aspect( hb_job_t * job, int keep );
64
65 int hb_calc_bitrate( hb_job_t *, int size );
66
67 struct hb_rate_s
68 {
69     char * string;
70     int    rate;
71 };
72
73 #define HB_ASPECT_BASE 9
74 #define HB_VIDEO_RATE_BASE   27000000
75
76 extern hb_rate_t hb_video_rates[];
77 extern int       hb_video_rates_count;
78 extern hb_rate_t hb_audio_rates[];
79 extern int       hb_audio_rates_count;
80 extern int       hb_audio_rates_default;
81 extern hb_rate_t hb_audio_bitrates[];
82 extern int       hb_audio_bitrates_count;
83 extern int       hb_audio_bitrates_default;
84
85 /******************************************************************************
86  * hb_job_t: settings to be filled by the UI
87  *****************************************************************************/
88 struct hb_job_s
89 {
90     /* Pointer to the title to be ripped */
91     hb_title_t    * title;
92     
93     /* Chapter selection */
94     int             chapter_start;
95     int             chapter_end;
96
97     /* Picture settings:
98          crop:                must be multiples of 2 (top/bottom/left/right)
99          deinterlace:         0 or 1
100          width:               must be a multiple of 16
101          height:              must be a multiple of 16
102          keep_ratio:          used by UIs 
103          pixel_ratio:         store pixel aspect ratio in the video
104          pixel_aspect_width:  numerator for pixel aspect ratio
105          pixel_aspect_height: denominator for pixel aspect ratio
106                  maxWidth:                        keep width below this
107                  maxHeight:                       keep height below this */
108
109     int             crop[4];
110     int             deinterlace;
111     int             width;
112     int             height;
113     int             keep_ratio;
114     int             grayscale;
115     int             pixel_ratio;
116     int             pixel_aspect_width;
117     int             pixel_aspect_height;
118         int                             maxWidth;
119         int                             maxHeight;
120
121
122     /* Video settings:
123          vcodec:            output codec
124          vquality:          output quality (0.0..1.0)
125                             if < 0.0 or > 1.0, bitrate is used instead
126          vbitrate:          output bitrate (kbps)
127          pass:              0, 1 or 2
128          vrate, vrate_base: output framerate is vrate / vrate_base
129                  h264_level:            boolean for whether or not we're encoding for iPod
130                  crf:                           boolean for whether to use constant rate factor with x264
131                  x264opts:                      string of extra x264 options 
132                  areBframes:            boolean to note if b-frames are included in x264opts */
133 #define HB_VCODEC_MASK   0x0000FF
134 #define HB_VCODEC_FFMPEG 0x000001
135 #define HB_VCODEC_XVID   0x000002
136 #define HB_VCODEC_X264   0x000004
137
138     int             vcodec;
139     float           vquality;
140     int             vbitrate;
141     int             vrate;
142     int             vrate_base;
143     int             pass;
144     int             h264_13;
145         int                             h264_level;
146         int                             crf;
147         const char              *x264opts;
148         int                             areBframes;
149         
150     /* Audio tracks:
151          audios:         Indexes in hb_title_t's audios list, starting from 0.
152                          -1 indicates the end of the list
153                  channels:       The # of normal channels in the last used audio
154                  lfechannels:    The # of lfe channels in the last used audio
155                  channelsused:   The # of channels we will actually use for this job -
156                                  calculated based on surround, channels and lfechannels
157                                                  in work.c
158          ac3flags:       stores the flags from the AC3 source, as found in scan.c
159              surround:       1 if 5.1 should be preserved for AAC, 0 otherwise */
160     int             audios[8];
161     int             channels;
162     int             lfechannels;
163     int             channelsused;
164         int             ac3flags;
165         int             surround;
166
167     /* Audio settings:
168          acodec:   output codec
169          abitrate: output bitrate (kbps)
170          arate:    output samplerate (Hz)
171        HB_ACODEC_AC3 means pass-through, then abitrate and arate are
172        ignored */
173 #define HB_ACODEC_MASK   0x00FF00
174 #define HB_ACODEC_FAAC   0x000100
175 #define HB_ACODEC_LAME   0x000200
176 #define HB_ACODEC_VORBIS 0x000400
177 #define HB_ACODEC_AC3    0x000800
178 #define HB_ACODEC_MPGA   0x001000
179 #define HB_ACODEC_LPCM   0x002000
180     int             acodec;
181     int             abitrate;
182     int             arate;
183
184     /* Subtitle settings:
185          subtitle: index in hb_title_t's subtitles list, starting
186          from 0. -1 means no subtitle */
187     int             subtitle;
188
189     /* Muxer settings
190          mux:  output file format
191          file: file path */
192 #define HB_MUX_MASK 0xFF0000
193 #define HB_MUX_MP4  0x010000
194 #define HB_MUX_PSP  0x020000
195 #define HB_MUX_AVI  0x040000
196 #define HB_MUX_OGM  0x080000
197 #define HB_MUX_IPOD 0x100000
198         
199     int             mux;
200     const char          * file;
201
202 #ifdef __LIBHB__
203     /* Internal data */
204     hb_handle_t   * h;
205     hb_lock_t     * pause;
206     volatile int  * die;
207     volatile int    done;
208
209     hb_fifo_t     * fifo_mpeg2;   /* MPEG-2 video ES */
210     hb_fifo_t     * fifo_raw;     /* Raw pictures */
211     hb_fifo_t     * fifo_sync;    /* Raw pictures, framerate corrected */
212     hb_fifo_t     * fifo_render;  /* Raw pictures, scaled */
213     hb_fifo_t     * fifo_mpeg4;   /* MPEG-4 video ES */
214
215     hb_thread_t   * reader;
216     hb_thread_t   * muxer;
217
218     hb_list_t     * list_work;
219
220     hb_esconfig_t config;
221
222     hb_mux_data_t * mux_data;
223 #endif
224 };
225
226 struct hb_audio_s
227 {
228     int  id;
229     char lang[1024];
230     char lang_simple[1024];
231     int  codec;
232     int  rate;
233     int  bitrate;
234         /* indicates the number of normal channels the source audio has */
235     int  channels;
236         /* indicates the number of lfe channels the source audio has */
237         int  lfechannels;
238         /* stores the flags from the AC3 source, as found in scan.c */
239         int  ac3flags;
240
241 #ifdef __LIBHB__
242     /* Internal data */
243     hb_fifo_t * fifo_in;   /* AC3/MPEG/LPCM ES */
244     hb_fifo_t * fifo_raw;  /* Raw audio */
245     hb_fifo_t * fifo_sync; /* Resampled, synced raw audio */
246     hb_fifo_t * fifo_out;  /* MP3/AAC/Vorbis ES */
247
248     hb_esconfig_t config;
249     hb_mux_data_t * mux_data;
250 #endif
251 };
252
253 struct hb_chapter_s
254 {
255     int      index;
256     int      cell_start;
257     int      cell_end;
258     int      block_start;
259     int      block_end;
260     int      block_count;
261
262     /* Visual-friendly duration */
263     int      hours;
264     int      minutes;
265     int      seconds;
266
267     /* Exact duration (in 1/90000s) */
268     uint64_t duration;
269 };
270
271 struct hb_subtitle_s
272 {
273     int  id;
274     char lang[1024];
275
276 #ifdef __LIBHB__
277     /* Internal data */
278     hb_fifo_t * fifo_in;  /* SPU ES */
279     hb_fifo_t * fifo_raw; /* Decodec SPU */
280 #endif
281 };
282
283 struct hb_title_s
284 {
285     char        dvd[1024];
286     char        name[1024];
287     int         index;
288     int         vts;
289     int         ttn;
290     int         cell_start;
291     int         cell_end;
292     int         block_start;
293     int         block_end;
294     int         block_count;
295
296     /* Visual-friendly duration */
297     int         hours;
298     int         minutes;
299     int         seconds;
300
301     /* Exact duration (in 1/90000s) */
302     uint64_t    duration;
303
304     int         width;
305     int         height;
306     int         aspect;
307     int         rate;
308     int         rate_base;
309     int         crop[4];
310
311     uint32_t    palette[16];
312
313     hb_list_t * list_chapter;
314     hb_list_t * list_audio;
315     hb_list_t * list_subtitle;
316
317     /* Job template for this title */
318     hb_job_t  * job;
319 };
320
321
322 struct hb_state_s
323 {
324 #define HB_STATE_IDLE     1
325 #define HB_STATE_SCANNING 2
326 #define HB_STATE_SCANDONE 4
327 #define HB_STATE_WORKING  8
328 #define HB_STATE_PAUSED   16
329 #define HB_STATE_WORKDONE 32
330 #define HB_STATE_MUXING   64
331     int state;
332
333     union
334     {
335         struct
336         {
337             /* HB_STATE_SCANNING */
338             int title_cur;
339             int title_count;
340         } scanning;
341
342         struct
343         {
344             /* HB_STATE_WORKING */
345             float progress;
346             int   job_cur;
347             int   job_count;
348             float rate_cur;
349             float rate_avg;
350             int   hours;
351             int   minutes;
352             int   seconds;
353         } working;
354
355         struct
356         {
357             /* HB_STATE_WORKDONE */
358 #define HB_ERROR_NONE     0
359 #define HB_ERROR_CANCELED 1
360 #define HB_ERROR_UNKNOWN  2
361             int error;
362         } workdone;
363
364         struct
365         {
366             /* HB_STATE_MUXING */
367             float progress;
368         } muxing;
369     } param;
370 };
371
372 struct hb_work_object_s
373 {
374     int                 id;
375     char              * name;
376
377 #ifdef __LIBHB__
378     int              (* init)  ( hb_work_object_t *, hb_job_t * );
379     int              (* work)  ( hb_work_object_t *, hb_buffer_t **,
380                                  hb_buffer_t ** );
381     void             (* close) ( hb_work_object_t * );
382
383     hb_fifo_t         * fifo_in;
384     hb_fifo_t         * fifo_out;
385     hb_esconfig_t     * config;
386
387     hb_work_private_t * private_data;
388
389     hb_thread_t       * thread;
390     volatile int      * done;
391
392     hb_work_object_t  * next;
393         int                               thread_sleep_interval;
394 #endif
395 };
396
397 extern hb_work_object_t hb_sync;
398 extern hb_work_object_t hb_decmpeg2;
399 extern hb_work_object_t hb_decsub;
400 extern hb_work_object_t hb_render;
401 extern hb_work_object_t hb_encavcodec;
402 extern hb_work_object_t hb_encxvid;
403 extern hb_work_object_t hb_encx264;
404 extern hb_work_object_t hb_deca52;
405 extern hb_work_object_t hb_decavcodec;
406 extern hb_work_object_t hb_declpcm;
407 extern hb_work_object_t hb_encfaac;
408 extern hb_work_object_t hb_enclame;
409 extern hb_work_object_t hb_encvorbis;
410
411 #endif