【问题标题】:write x264_encoder_encode output nals to h264 file将 x264_encoder_encode 输出 nals 写入 h264 文件
【发布时间】:2013-05-17 11:32:57
【问题描述】:

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

怎么了??????

提前致谢, 泽特布

【问题讨论】:

    标签: save encode disk x264


    【解决方案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之前有效。

    【讨论】:

    • 谢谢。它起作用了,问题是我认为数据指针“永远”有效......
    • 嗨@Zetlb。我有类似的问题。我通过 ffmpeg 使用 x264,那么我怎样才能像你一样获取最终数据??
    • 嗨,我使用 X264 编码帧并获取 NAL,而不是 ffmpeg。我还使用 ffmepg 解码 NAL 并获取编码帧。我认为我在使用 ffmpeg 编码时遇到了问题,我在 windows 环境中(获取工作库以在 windows 中使用并不容易)。
    【解决方案2】:

    好像忘记把h.264文件头写入h.264文件了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多