x264.exe 為 libx264 庫的命令行封裝,負責參數解析、輸入輸出文件處理、濾鏡鏈構建,並調用核心編碼 API 將 YUV 編碼為 H.264 碼流。核心流程:
main() → parse() 解析命令行參數,初始化 x264_param_t、選擇輸入/輸出模組、濾鏡鏈。encode() 打開編碼器,寫入 SPS/PPS/SEI,循環逐幀編碼,刷新延遲幀,關閉編碼器。| API | 用途 |
|---|---|
x264_param_default() | 填充預設參數(CPU 檢測、RC、分析等) |
x264_param_default_preset() | 套用 preset/tune(ultrafast~placebo,film/animation/grain…) |
x264_param_parse() | 按「名稱=值」字串覆蓋單個參數 |
x264_param_apply_profile() | 依 profile(baseline/main/high…)降級參數 |
x264_encoder_open() | 創建編碼器句柄 x264_t* |
x264_encoder_headers() | 取得 SPS/PPS/SEI NAL 單元 |
x264_encoder_encode() | 編碼一幀,輸出 NAL 列表 |
x264_encoder_close() | 釋放編碼器資源 |
定義於 output/output.h,包含 open_file/set_param/write_headers/write_frame/close_file 五個回調。內建四種實現:
fwrite NAL 資料(裸流 .264)定義於 input/input.h,提供 open_file/picture_alloc/read_frame/release_frame/picture_clean/close_file。常用實現:
定義於 filters/video/video.h,將輸入模組統一為濾鏡接口。兩個基礎濾鏡:
cli_input_t,無實際處理。libswscale::sws_scale() 進行拉伸/色域轉換。編碼循環中:filter.get_frame() → encode_frame() → filter.release_frame()。
while (獲取幀) {
encode_frame() { // 內部調用 x264_encoder_encode()
cli_output.write_frame() // 封裝寫入
}
}
while (x264_encoder_delayed_frames()) { // 刷新緩衝
encode_frame(NULL)
}
cli_output_t 並註冊到 select_output()。cli_vid_filter_t 接口,掛載到濾鏡鏈。x264_param_default() 中的賦值。