• Main Page
  • Classes
  • Files
  • File List
  • File Members

libhb/internal.h

Go to the documentation of this file.
00001 /* internal.h
00002 
00003    Copyright (c) 2003-2013 HandBrake Team
00004    This file is part of the HandBrake source code
00005    Homepage: <http://handbrake.fr/>.
00006    It may be used under the terms of the GNU General Public License v2.
00007    For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
00008  */
00009 
00010 #include "hbffmpeg.h"
00011 
00012 /***********************************************************************
00013  * common.c
00014  **********************************************************************/
00015 void hb_log( char * log, ... ) HB_WPRINTF(1,2);
00016 extern int global_verbosity_level; // Global variable for hb_deep_log
00017 typedef enum hb_debug_level_s
00018 {
00019     HB_SUPPORT_LOG      = 1, // helpful in tech support
00020     HB_HOUSEKEEPING_LOG = 2, // stuff we hate scrolling through  
00021     HB_GRANULAR_LOG     = 3  // sample-by-sample
00022 } hb_debug_level_t;
00023 void hb_valog( hb_debug_level_t level, const char * prefix, const char * log, va_list args) HB_WPRINTF(3,0);
00024 void hb_deep_log( hb_debug_level_t level, char * log, ... ) HB_WPRINTF(2,3);
00025 void hb_error( char * fmt, ...) HB_WPRINTF(1,2);
00026 void hb_hexdump( hb_debug_level_t level, const char * label, const uint8_t * data, int len );
00027 
00028 int  hb_list_bytes( hb_list_t * );
00029 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size );
00030 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
00031                        uint64_t * pts, uint64_t * pos );
00032 void hb_list_empty( hb_list_t ** );
00033 
00034 hb_title_t * hb_title_init( char * dvd, int index );
00035 void         hb_title_close( hb_title_t ** );
00036 
00037 /***********************************************************************
00038  * hb.c
00039  **********************************************************************/
00040 int  hb_get_pid( hb_handle_t * );
00041 void hb_set_state( hb_handle_t *, hb_state_t * );
00042 
00043 /***********************************************************************
00044  * fifo.c
00045  **********************************************************************/
00046 
00047 /*
00048  * Holds a packet of data that is moving through the transcoding process.
00049  * 
00050  * May have metadata associated with it via extra fields
00051  * that are conditionally used depending on the type of packet.
00052  */
00053 struct hb_buffer_s
00054 {
00055     int           size;     // size of this packet
00056     int           alloc;    // used internally by the packet allocator (hb_buffer_init)
00057     uint8_t *     data;     // packet data
00058     int           offset;   // used internally by packet lists (hb_list_t)
00059 
00060     /*
00061      * Corresponds to the order that this packet was read from the demuxer.
00062      * 
00063      * It is important that video decoder work-objects pass this value through
00064      * from their input packets to the output packets they generate. Otherwise
00065      * RENDERSUB subtitles (especially VOB subtitles) will break.
00066      * 
00067      * Subtitle decoder work-objects that output a renderable subtitle
00068      * format (ex: PICTURESUB) must also be careful to pass the sequence number
00069      * through for the same reason.
00070      */
00071     int64_t       sequence;
00072 
00073     struct settings
00074     {
00075         enum { AUDIO_BUF, VIDEO_BUF, SUBTITLE_BUF, FRAME_BUF, OTHER_BUF } type;
00076 
00077         int           id;           // ID of the track that the packet comes from
00078         int64_t       start;        // start time of frame
00079         double        duration;     // Actual duration, may be fractional ticks
00080         int64_t       stop;         // stop time of frame
00081         int64_t       renderOffset; // DTS used by b-frame offsets in muxmp4
00082         int64_t       pcr;
00083         uint8_t       discontinuity;
00084         int           new_chap;     // Video packets: if non-zero, is the index of the chapter whose boundary was crossed
00085 
00086     #define HB_FRAME_IDR    0x01
00087     #define HB_FRAME_I      0x02
00088     #define HB_FRAME_AUDIO  0x04
00089     #define HB_FRAME_P      0x10
00090     #define HB_FRAME_B      0x20
00091     #define HB_FRAME_BREF   0x40
00092     #define HB_FRAME_KEY    0x0F
00093     #define HB_FRAME_REF    0xF0
00094         uint8_t       frametype;
00095         uint16_t      flags;
00096     } s;
00097 
00098     struct format
00099     {
00100         int           x;
00101         int           y;
00102         int           width;
00103         int           height;
00104         int           fmt;
00105     } f;
00106 
00107     struct plane
00108     {
00109         uint8_t     * data;
00110         int           stride;
00111         int           width;
00112         int           height;
00113         int           height_stride;
00114         int           size;
00115     } plane[4]; // 3 Color components + alpha
00116 
00117     // PICTURESUB subtitle packets:
00118 
00119     // Video packets (after processing by the hb_sync_video work-object):
00120     //   A (copy of a) PICTURESUB subtitle packet that needs to be burned into 
00121     //   this video packet by the vobsub renderer filter
00122     //
00123     //   Subtitles that are simply passed thru are NOT attached to the 
00124     //   associated video packets.
00125     hb_buffer_t * sub;
00126 
00127     // Packets in a list:
00128     //   the next packet in the list
00129     hb_buffer_t * next;
00130 };
00131 
00132 void hb_buffer_pool_init( void );
00133 void hb_buffer_pool_free( void );
00134 
00135 hb_buffer_t * hb_buffer_init( int size );
00136 hb_buffer_t * hb_frame_buffer_init( int pix_fmt, int w, int h);
00137 void          hb_buffer_init_planes( hb_buffer_t * b );
00138 void          hb_buffer_realloc( hb_buffer_t *, int size );
00139 void          hb_video_buffer_realloc( hb_buffer_t * b, int w, int h );
00140 void          hb_buffer_reduce( hb_buffer_t * b, int size );
00141 void          hb_buffer_close( hb_buffer_t ** );
00142 hb_buffer_t * hb_buffer_dup( const hb_buffer_t * src );
00143 int           hb_buffer_copy( hb_buffer_t * dst, const hb_buffer_t * src );
00144 void          hb_buffer_swap_copy( hb_buffer_t *src, hb_buffer_t *dst );
00145 void          hb_buffer_move_subs( hb_buffer_t * dst, hb_buffer_t * src );
00146 
00147 hb_fifo_t   * hb_fifo_init( int capacity, int thresh );
00148 int           hb_fifo_size( hb_fifo_t * );
00149 int           hb_fifo_size_bytes( hb_fifo_t * );
00150 int           hb_fifo_is_full( hb_fifo_t * );
00151 float         hb_fifo_percent_full( hb_fifo_t * f );
00152 hb_buffer_t * hb_fifo_get( hb_fifo_t * );
00153 hb_buffer_t * hb_fifo_get_wait( hb_fifo_t * );
00154 hb_buffer_t * hb_fifo_see( hb_fifo_t * );
00155 hb_buffer_t * hb_fifo_see_wait( hb_fifo_t * );
00156 hb_buffer_t * hb_fifo_see2( hb_fifo_t * );
00157 void          hb_fifo_push( hb_fifo_t *, hb_buffer_t * );
00158 void          hb_fifo_push_wait( hb_fifo_t *, hb_buffer_t * );
00159 int           hb_fifo_full_wait( hb_fifo_t * f );
00160 void          hb_fifo_push_head( hb_fifo_t *, hb_buffer_t * );
00161 void          hb_fifo_push_list_element( hb_fifo_t *fifo, hb_buffer_t *buffer_list );
00162 hb_buffer_t * hb_fifo_get_list_element( hb_fifo_t *fifo );
00163 void          hb_fifo_close( hb_fifo_t ** );
00164 void          hb_fifo_flush( hb_fifo_t * f );
00165 
00166 static inline int hb_image_stride( int pix_fmt, int width, int plane )
00167 {
00168     int linesize = av_image_get_linesize( pix_fmt, width, plane );
00169 
00170     // Make buffer SIMD friendly.
00171     // Decomb requires stride aligned to 32 bytes
00172     // TODO: eliminate extra buffer copies in decomb
00173     linesize = MULTIPLE_MOD_UP( linesize, 32 );
00174     return linesize;
00175 }
00176 
00177 static inline int hb_image_width(int pix_fmt, int width, int plane)
00178 {
00179     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
00180 
00181     if (desc != NULL && (plane == 1 || plane == 2))
00182     {
00183         // The wacky arithmatic assures rounding up.
00184         width = -((-width) >> desc->log2_chroma_w);
00185     }
00186 
00187     return width;
00188 }
00189 
00190 static inline int hb_image_height_stride(int pix_fmt, int height, int plane)
00191 {
00192     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
00193 
00194     // Decomb requires 6 extra lines and stride aligned to 32 bytes
00195     height = MULTIPLE_MOD_UP(height + 6, 32);
00196     if (desc != NULL && (plane == 1 || plane == 2))
00197     {
00198         height = height >> desc->log2_chroma_h;
00199     }
00200 
00201     return height;
00202 }
00203 
00204 static inline int hb_image_height(int pix_fmt, int height, int plane)
00205 {
00206     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
00207 
00208     if (desc != NULL && (plane == 1 || plane == 2))
00209     {
00210         // The wacky arithmatic assures rounding up.
00211         height = -((-height) >> desc->log2_chroma_h);
00212     }
00213 
00214     return height;
00215 }
00216 
00217 // this routine gets a buffer for an uncompressed YUV420 video frame
00218 // with dimensions width x height.
00219 static inline hb_buffer_t * hb_video_buffer_init( int width, int height )
00220 {
00221     return hb_frame_buffer_init( AV_PIX_FMT_YUV420P, width, height );
00222 }
00223 
00224 /***********************************************************************
00225  * Threads: update.c, scan.c, work.c, reader.c, muxcommon.c
00226  **********************************************************************/
00227 hb_thread_t * hb_update_init( int * build, char * version );
00228 hb_thread_t * hb_scan_init( hb_handle_t *, volatile int * die, 
00229                             const char * path, int title_index, 
00230                             hb_title_set_t * title_set, int preview_count, 
00231                             int store_previews, uint64_t min_duration );
00232 hb_thread_t * hb_work_init( hb_list_t * jobs,
00233                             volatile int * die, int * error, hb_job_t ** job );
00234 void ReadLoop( void * _w );
00235 hb_work_object_t * hb_muxer_init( hb_job_t * );
00236 hb_work_object_t * hb_get_work( int );
00237 hb_work_object_t * hb_codec_decoder( int );
00238 hb_work_object_t * hb_codec_encoder( int );
00239 
00240 /***********************************************************************
00241  * sync.c
00242  **********************************************************************/
00243 hb_work_object_t * hb_sync_init( hb_job_t * job );
00244 
00245 /***********************************************************************
00246  * mpegdemux.c
00247  **********************************************************************/
00248 typedef struct {
00249     int64_t last_scr;       /* unadjusted SCR from most recent pack */
00250     int64_t scr_delta;
00251     int64_t last_pts;       /* last pts we saw */
00252     int     scr_changes;    /* number of SCR discontinuities */
00253     int     dts_drops;      /* number of drops because DTS too far from SCR */
00254     int     new_chap;
00255 } hb_psdemux_t;
00256 
00257 typedef void (*hb_muxer_t)(hb_buffer_t *, hb_list_t *, hb_psdemux_t*);
00258 
00259 void hb_demux_ps( hb_buffer_t * ps_buf, hb_list_t * es_list, hb_psdemux_t * );
00260 void hb_demux_ts( hb_buffer_t * ps_buf, hb_list_t * es_list, hb_psdemux_t * );
00261 void hb_demux_null( hb_buffer_t * ps_buf, hb_list_t * es_list, hb_psdemux_t * );
00262 
00263 extern const hb_muxer_t hb_demux[];
00264 
00265 /***********************************************************************
00266  * decmetadata.c
00267  **********************************************************************/
00268 extern int decmetadata( hb_title_t *title );
00269 
00270 /***********************************************************************
00271  * batch.c
00272  **********************************************************************/
00273 typedef struct hb_batch_s hb_batch_t;
00274 
00275 hb_batch_t  * hb_batch_init( char * path );
00276 void          hb_batch_close( hb_batch_t ** _d );
00277 int           hb_batch_title_count( hb_batch_t * d );
00278 hb_title_t  * hb_batch_title_scan( hb_batch_t * d, int t );
00279 
00280 /***********************************************************************
00281  * dvd.c
00282  **********************************************************************/
00283 typedef struct hb_bd_s hb_bd_t;
00284 typedef union  hb_dvd_s hb_dvd_t;
00285 typedef struct hb_stream_s hb_stream_t;
00286 
00287 hb_dvd_t *   hb_dvd_init( char * path );
00288 int          hb_dvd_title_count( hb_dvd_t * );
00289 hb_title_t * hb_dvd_title_scan( hb_dvd_t *, int title, uint64_t min_duration );
00290 int          hb_dvd_start( hb_dvd_t *, hb_title_t *title, int chapter );
00291 void         hb_dvd_stop( hb_dvd_t * );
00292 int          hb_dvd_seek( hb_dvd_t *, float );
00293 hb_buffer_t * hb_dvd_read( hb_dvd_t * );
00294 int          hb_dvd_chapter( hb_dvd_t * );
00295 int          hb_dvd_is_break( hb_dvd_t * d );
00296 void         hb_dvd_close( hb_dvd_t ** );
00297 int          hb_dvd_angle_count( hb_dvd_t * d );
00298 void         hb_dvd_set_angle( hb_dvd_t * d, int angle );
00299 int          hb_dvd_main_feature( hb_dvd_t * d, hb_list_t * list_title );
00300 
00301 hb_bd_t     * hb_bd_init( char * path );
00302 int           hb_bd_title_count( hb_bd_t * d );
00303 hb_title_t  * hb_bd_title_scan( hb_bd_t * d, int t, uint64_t min_duration );
00304 int           hb_bd_start( hb_bd_t * d, hb_title_t *title );
00305 void          hb_bd_stop( hb_bd_t * d );
00306 int           hb_bd_seek( hb_bd_t * d, float f );
00307 int           hb_bd_seek_pts( hb_bd_t * d, uint64_t pts );
00308 int           hb_bd_seek_chapter( hb_bd_t * d, int chapter );
00309 hb_buffer_t * hb_bd_read( hb_bd_t * d );
00310 int           hb_bd_chapter( hb_bd_t * d );
00311 void          hb_bd_close( hb_bd_t ** _d );
00312 void          hb_bd_set_angle( hb_bd_t * d, int angle );
00313 int           hb_bd_main_feature( hb_bd_t * d, hb_list_t * list_title );
00314 
00315 hb_stream_t * hb_bd_stream_open( hb_title_t *title );
00316 void hb_ts_stream_reset(hb_stream_t *stream);
00317 hb_stream_t * hb_stream_open( char * path, hb_title_t *title, int scan );
00318 void             hb_stream_close( hb_stream_t ** );
00319 hb_title_t * hb_stream_title_scan( hb_stream_t *, hb_title_t *);
00320 hb_buffer_t * hb_stream_read( hb_stream_t * );
00321 int          hb_stream_seek( hb_stream_t *, float );
00322 int          hb_stream_seek_ts( hb_stream_t * stream, int64_t ts );
00323 int          hb_stream_seek_chapter( hb_stream_t *, int );
00324 int          hb_stream_chapter( hb_stream_t * );
00325 
00326 hb_buffer_t * hb_ts_decode_pkt( hb_stream_t *stream, const uint8_t * pkt );
00327 
00328 
00329 #define STR4_TO_UINT32(p) \
00330     ((((const uint8_t*)(p))[0] << 24) | \
00331      (((const uint8_t*)(p))[1] << 16) | \
00332      (((const uint8_t*)(p))[2] <<  8) | \
00333       ((const uint8_t*)(p))[3])
00334 
00335 /***********************************************************************
00336  * Work objects
00337  **********************************************************************/
00338 #define HB_CONFIG_MAX_SIZE (2*8192)
00339 union hb_esconfig_u
00340 {
00341 
00342     struct
00343     {
00344         uint8_t bytes[HB_CONFIG_MAX_SIZE];
00345         int     length;
00346     } mpeg4;
00347 
00348         struct
00349         {
00350             uint8_t  sps[HB_CONFIG_MAX_SIZE];
00351             int       sps_length;
00352             uint8_t  pps[HB_CONFIG_MAX_SIZE];
00353             int       pps_length;
00354         uint32_t init_delay;
00355         } h264;
00356 
00357     struct
00358     {
00359         uint8_t headers[3][HB_CONFIG_MAX_SIZE];
00360     } theora;
00361 
00362     struct
00363     {
00364         uint8_t bytes[HB_CONFIG_MAX_SIZE];
00365         int     length;
00366     } extradata;
00367 
00368     struct
00369     {
00370         uint8_t headers[3][HB_CONFIG_MAX_SIZE];
00371         char *language;
00372     } vorbis;
00373 
00374     struct
00375     {
00376         /* ac3flags stores the flags from the AC3 source, as found in scan.c */
00377         int     ac3flags;
00378         // next two items are used by the bsinfo routine to accumulate small
00379         // frames until we have enough to validate the crc.
00380         int     len;        // space currently used in 'buf'
00381         uint8_t buf[HB_CONFIG_MAX_SIZE-sizeof(int)];
00382     } a52;
00383 
00384     struct
00385     {
00386         /* dcaflags stores the flags from the DCA source, as found in scan.c */
00387         int  dcaflags;
00388     } dca;
00389 
00390 };
00391 
00392 enum
00393 {
00394     WORK_SYNC_VIDEO = 1,
00395     WORK_SYNC_AUDIO,
00396     WORK_DECMPEG2,
00397     WORK_DECCC608,
00398     WORK_DECVOBSUB,
00399     WORK_DECSRTSUB,
00400     WORK_DECUTF8SUB,
00401     WORK_DECTX3GSUB,
00402     WORK_DECSSASUB,
00403     WORK_ENCVOBSUB,
00404     WORK_RENDER,
00405     WORK_ENCAVCODEC,
00406     WORK_ENCX264,
00407     WORK_ENCTHEORA,
00408     WORK_DECA52,
00409     WORK_DECAVCODEC,
00410     WORK_DECAVCODECV,
00411     WORK_DECLPCM,
00412     WORK_ENCFAAC,
00413     WORK_ENCLAME,
00414     WORK_ENCVORBIS,
00415     WORK_ENC_CA_AAC,
00416     WORK_ENC_CA_HAAC,
00417     WORK_ENCAVCODEC_AUDIO,
00418     WORK_MUX,
00419     WORK_READER,
00420     WORK_DECPGSSUB
00421 };
00422 
00423 extern hb_filter_object_t hb_filter_detelecine;
00424 extern hb_filter_object_t hb_filter_deinterlace;
00425 extern hb_filter_object_t hb_filter_deblock;
00426 extern hb_filter_object_t hb_filter_denoise;
00427 extern hb_filter_object_t hb_filter_decomb;
00428 extern hb_filter_object_t hb_filter_rotate;
00429 extern hb_filter_object_t hb_filter_crop_scale;
00430 extern hb_filter_object_t hb_filter_render_sub;
00431 extern hb_filter_object_t hb_filter_vfr;
00432 
00433 // Picture flags used by filters
00434 #ifndef PIC_FLAG_REPEAT_FIRST_FIELD
00435 #define PIC_FLAG_REPEAT_FIRST_FIELD 256
00436 #endif
00437 #ifndef PIC_FLAG_TOP_FIELD_FIRST
00438 #define PIC_FLAG_TOP_FIELD_FIRST 8
00439 #endif
00440 #ifndef PIC_FLAG_PROGRESSIVE_FRAME
00441 #define PIC_FLAG_PROGRESSIVE_FRAME 16
00442 #endif
00443 
00444 #define PIC_FLAG_REPEAT_FRAME 512
00445 
00446 extern hb_work_object_t * hb_objects;
00447 
00448 #define HB_WORK_IDLE     0
00449 #define HB_WORK_OK       1
00450 #define HB_WORK_ERROR    2
00451 #define HB_WORK_DONE     3
00452 
00453 /***********************************************************************
00454  * Muxers
00455  **********************************************************************/
00456 typedef struct hb_mux_object_s hb_mux_object_t;
00457 typedef struct hb_mux_data_s   hb_mux_data_t;
00458 
00459 #define HB_MUX_COMMON \
00460     int (*init)      ( hb_mux_object_t * ); \
00461     int (*mux)       ( hb_mux_object_t *, hb_mux_data_t *, \
00462                        hb_buffer_t * ); \
00463     int (*end)       ( hb_mux_object_t * );
00464 
00465 #define DECLARE_MUX( a ) \
00466     hb_mux_object_t  * hb_mux_##a##_init( hb_job_t * );
00467 
00468 DECLARE_MUX( mp4 );
00469 DECLARE_MUX( avi );
00470 DECLARE_MUX( ogm );
00471 DECLARE_MUX( mkv );
00472 

Generated on Wed May 22 2013 05:31:50 for HandBrake by  doxygen 1.7.1