将x264_encoder_encode输出nals写入h264文件

时间:2013-01-23 16:27:44

标签: save encode disk x264

我正在尝试使用生命周期软件。我正在尝试的是使用x264的lib在服务器端编码为h264,在客户端使用ffmpeg进行解码。    经过一些失败的尝试后,我决定简化,我要做的第一件事就是用x264_encoder_encode对帧进行编码,并将生成的NAL写入文件。现在我想测试该文件是否正确。

要初始化编码器,我会这样做:

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都以正确的代码开头。

所以我把所有的nals写到一个文件中:

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

2 个答案:

答案 0 :(得分: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之前有效。

答案 1 :(得分:1)

您似乎忘了将h.264文件头写入h.264文件。