1

我正在尝试做一个生活流软件。我正在尝试的是使用 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

4

2 回答 2

1

看起来您只保存 x264_encoder_encode 返回的指针和大小,而不是实际数据。

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

该指向的数据仅在您调用下一个 x264_encoder_encode/x264_encoder_close 之前有效。

于 2013-01-24T15:08:48.417 回答
1

看来您忘记将 h.264 文件头写入 h.264 文件了。

于 2013-05-17T03:09:30.123 回答