OSDN Git Service

a2be02a87b8bf07a56eb68fe1148a2aefb4af6dc
[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              surround:       1 if 5.1 should be preserved for AAC, 0 otherwise */
154     int             audios[8];
155         int             surround;
156
157     /* Audio settings:
158          acodec:   output codec
159          abitrate: output bitrate (kbps)
160          arate:    output samplerate (Hz)
161        HB_ACODEC_AC3 means pass-through, then abitrate and arate are
162        ignored */
163 #define HB_ACODEC_MASK   0x00FF00
164 #define HB_ACODEC_FAAC   0x000100
165 #define HB_ACODEC_LAME   0x000200
166 #define HB_ACODEC_VORBIS 0x000400
167 #define HB_ACODEC_AC3    0x000800
168 #define HB_ACODEC_MPGA   0x001000
169 #define HB_ACODEC_LPCM   0x002000
170     int             acodec;
171     int             abitrate;
172     int             arate;
173
174     /* Subtitle settings:
175          subtitle: index in hb_title_t's subtitles list, starting
176          from 0. -1 means no subtitle */
177     int             subtitle;
178
179     /* Muxer settings
180          mux:  output file format
181          file: file path */
182 #define HB_MUX_MASK 0xFF0000
183 #define HB_MUX_MP4  0x010000
184 #define HB_MUX_PSP  0x020000
185 #define HB_MUX_AVI  0x040000
186 #define HB_MUX_OGM  0x080000
187 #define HB_MUX_IPOD 0x100000
188         
189     int             mux;
190     const char          * file;
191
192 #ifdef __LIBHB__
193     /* Internal data */
194     hb_handle_t   * h;
195     hb_lock_t     * pause;
196     volatile int  * die;
197     volatile int    done;
198
199     hb_fifo_t     * fifo_mpeg2;   /* MPEG-2 video ES */
200     hb_fifo_t     * fifo_raw;     /* Raw pictures */
201     hb_fifo_t     * fifo_sync;    /* Raw pictures, framerate corrected */
202     hb_fifo_t     * fifo_render;  /* Raw pictures, scaled */
203     hb_fifo_t     * fifo_mpeg4;   /* MPEG-4 video ES */
204
205     hb_thread_t   * reader;
206     hb_thread_t   * muxer;
207
208     hb_list_t     * list_work;
209
210     hb_esconfig_t config;
211
212     hb_mux_data_t * mux_data;
213 #endif
214 };
215
216 struct hb_audio_s
217 {
218     int  id;
219     char lang[1024];
220     char lang_simple[1024];
221     int  codec;
222     int  rate;
223     int  bitrate;
224         /* channels:       The # of normal channels in the last used audio
225            lfechannels:    The # of lfe channels in the last used audio
226            channelsused:   The # of channels we will actually use for this job -
227                            calculated based on surround, channels and lfechannels
228                            in work.c */
229     int  channels;
230         int  lfechannels;
231         int channelsused;
232
233 #ifdef __LIBHB__
234     /* Internal data */
235     hb_fifo_t * fifo_in;   /* AC3/MPEG/LPCM ES */
236     hb_fifo_t * fifo_raw;  /* Raw audio */
237     hb_fifo_t * fifo_sync; /* Resampled, synced raw audio */
238     hb_fifo_t * fifo_out;  /* MP3/AAC/Vorbis ES */
239
240     hb_esconfig_t config;
241     hb_mux_data_t * mux_data;
242 #endif
243 };
244
245 struct hb_chapter_s
246 {
247     int      index;
248     int      cell_start;
249     int      cell_end;
250     int      block_start;
251     int      block_end;
252     int      block_count;
253
254     /* Visual-friendly duration */
255     int      hours;
256     int      minutes;
257     int      seconds;
258
259     /* Exact duration (in 1/90000s) */
260     uint64_t duration;
261 };
262
263 struct hb_subtitle_s
264 {
265     int  id;
266     char lang[1024];
267
268 #ifdef __LIBHB__
269     /* Internal data */
270     hb_fifo_t * fifo_in;  /* SPU ES */
271     hb_fifo_t * fifo_raw; /* Decodec SPU */
272 #endif
273 };
274
275 struct hb_title_s
276 {
277     char        dvd[1024];
278     char        name[1024];
279     int         index;
280     int         vts;
281     int         ttn;
282     int         cell_start;
283     int         cell_end;
284     int         block_start;
285     int         block_end;
286     int         block_count;
287
288     /* Visual-friendly duration */
289     int         hours;
290     int         minutes;
291     int         seconds;
292
293     /* Exact duration (in 1/90000s) */
294     uint64_t    duration;
295
296     int         width;
297     int         height;
298     int         aspect;
299     int         rate;
300     int         rate_base;
301     int         crop[4];
302
303     uint32_t    palette[16];
304
305     hb_list_t * list_chapter;
306     hb_list_t * list_audio;
307     hb_list_t * list_subtitle;
308
309     /* Job template for this title */
310     hb_job_t  * job;
311 };
312
313
314 struct hb_state_s
315 {
316 #define HB_STATE_IDLE     1
317 #define HB_STATE_SCANNING 2
318 #define HB_STATE_SCANDONE 4
319 #define HB_STATE_WORKING  8
320 #define HB_STATE_PAUSED   16
321 #define HB_STATE_WORKDONE 32
322 #define HB_STATE_MUXING   64
323     int state;
324
325     union
326     {
327         struct
328         {
329             /* HB_STATE_SCANNING */
330             int title_cur;
331             int title_count;
332         } scanning;
333
334         struct
335         {
336             /* HB_STATE_WORKING */
337             float progress;
338             int   job_cur;
339             int   job_count;
340             float rate_cur;
341             float rate_avg;
342             int   hours;
343             int   minutes;
344             int   seconds;
345         } working;
346
347         struct
348         {
349             /* HB_STATE_WORKDONE */
350 #define HB_ERROR_NONE     0
351 #define HB_ERROR_CANCELED 1
352 #define HB_ERROR_UNKNOWN  2
353             int error;
354         } workdone;
355
356         struct
357         {
358             /* HB_STATE_MUXING */
359             float progress;
360         } muxing;
361     } param;
362 };
363
364 struct hb_work_object_s
365 {
366     int                 id;
367     char              * name;
368
369 #ifdef __LIBHB__
370     int              (* init)  ( hb_work_object_t *, hb_job_t * );
371     int              (* work)  ( hb_work_object_t *, hb_buffer_t **,
372                                  hb_buffer_t ** );
373     void             (* close) ( hb_work_object_t * );
374
375     hb_fifo_t         * fifo_in;
376     hb_fifo_t         * fifo_out;
377     hb_esconfig_t     * config;
378
379     hb_work_private_t * private_data;
380
381     hb_thread_t       * thread;
382     volatile int      * done;
383
384     hb_work_object_t  * next;
385         int                               thread_sleep_interval;
386 #endif
387 };
388
389 extern hb_work_object_t hb_sync;
390 extern hb_work_object_t hb_decmpeg2;
391 extern hb_work_object_t hb_decsub;
392 extern hb_work_object_t hb_render;
393 extern hb_work_object_t hb_encavcodec;
394 extern hb_work_object_t hb_encxvid;
395 extern hb_work_object_t hb_encx264;
396 extern hb_work_object_t hb_deca52;
397 extern hb_work_object_t hb_decavcodec;
398 extern hb_work_object_t hb_declpcm;
399 extern hb_work_object_t hb_encfaac;
400 extern hb_work_object_t hb_enclame;
401 extern hb_work_object_t hb_encvorbis;
402
403 #endif