2013-01-23 49 views
1

我正在嘗試做一個生活流軟件。我正在嘗試使用x264的lib在服務器端對h264進行編碼,並在客戶端對ffmpeg進行解碼。 直接做了一些失敗的嘗試後,我決定簡化,我試圖做的第一件事情就是簡單地使用x264_encoder_encode對幀進行編碼,並將生成的NAL寫入文件。現在我想測試該文件是否正確。寫x264_encoder_encode輸出nals到h264文件

要初始化我做的編碼器:

x264_param_t param; 
x264_param_default_preset(&param, "veryfast", "zerolatency"); 
param.i_threads = 1; 
param.i_width = a_iWidth; 
param.i_height = a_iHeight; 
param.i_fps_num = a_iFPS; 
param.i_fps_den = 1; 
param.i_keyint_max = a_iFPS; 
param.b_intra_refresh = 1; 
param.rc.i_rc_method = X264_RC_CRF; 
param.rc.i_vbv_buffer_size = 1000000; 
param.rc.i_vbv_max_bitrate =500; //For streaming: 
param.b_repeat_headers = 1; 
param.b_annexb = 1; 
x264_param_apply_profile(&param, "baseline"); 
encoder = x264_encoder_open(&param); 

後來,當我有一個形象(RGBA圖像),我將其轉換爲YUV420P,然後我打電話x264_encoder_encode:

int frame_size = x264_encoder_encode(encoder, &nals, &num_nals, picture, &pic_out); 
if (frame_size > 0) 
{ 
    m_vNALs.push_back((char*)nals[0].p_payload); 
    m_vSizes.push_back(frame_size); 

    return frame_size; 
} 

一切似乎都有效的,frame_size返回非零正值,其他所有參數看起來都沒問題。每個NAL都以正確的代碼開始。

所以我寫的所有的NAL到一個文件:

FILE* pFile; 
pFile = fopen("file.h264", "wb"); 
for(int nIndex = 0; nIndex < m_vNALs.size(); nIndex++) 
{ 
    fwrite(m_vNALs[ nIndex ], m_vSizes[ nIndex ], 1, pFile); 
} 

現在我有file.h264文件。 所以測試這個文件我用ffmpeg.exe(我在Windows上),我得到這個輸出:

[h264 @ 00000000021c5a60] non-existing PPS referenced 
[h264 @ 00000000021c5a60] non-existing PPS 0 referenced 
[h264 @ 00000000021c5a60] decode_slice_header error 
[h264 @ 00000000021c5a60] no frame! 
[h264 @ 00000000021c5a60] non-existing PPS referenced 
[h264 @ 00000000021b74a0] max_analyze_duration 5000000 reached at 5000000 
[h264 @ 00000000021b74a0] decoding for stream 0 failed 
[h264 @ 00000000021b74a0] Could not find codec parameters for stream 0 (Video: h 
264): unspecified size 
Consider increasing the value for the 'analyzeduration' and 'probesize' options 
[h264 @ 00000000021b74a0] Estimating duration from bitrate, this may be inaccura 
te 
file.h264: could not find codec parameters 

VLC還不能播放文件。

ffplay講述:

file.h264: Invalid data found when processing input 

什麼是錯的?????

由於提前, Zetlb

回答

1

看起來你保存只能通過x264_encoder_encode返回指針和大小,而不是實際的數據。

m_vNALs.push_back((char*)nals[0].p_payload); 
m_vSizes.push_back(frame_size); 

此所指的數據調用next x264_encoder_encode/x264_encoder_close之前纔有效。

+0

謝謝。它的工作,問題是,我認爲數據指針是有效的「永遠」... –

+0

嗨@Zetlb。我有類似的問題。我通過ffmpeg使用x264,那麼如何才能像你一樣獲得最終數據? – nmxprime

+0

嗨,我用[X264](http://www.videolan.org/developers/x264.html)來編碼幀並獲取NAL,而不是[ffmpeg](http://www.ffmpeg.org/)。此外,我用ffmepg來解碼NAL並獲取編碼後的幀。我認爲我使用ffmpeg進行編碼時遇到了問題,我在windows環境下(獲取工作庫以在Windows中使用並不容易)。 –

1

看來你忘了將h.264文件頭寫入h.264文件。