Question

I'm trying to do a life stream software. What I'm trying is to use x264's lib to encode to h264 on server side and ffmpeg to decode on client side. After some failed attemps of doing it directly I decided to simplify, and the first thing I am trying to do is simply to encode frames with x264_encoder_encode and writing the resulting NALs to a file. Now I want to test if that file es correct.

To initialize the encoder I do:

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);

Then when I have an image ( RGBA image ) I convert it to YUV420P and then I call 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;
}

Everything appears to be valid, frame_size returns non-zero positive value and all the others parameters appears to be ok. Every NAL starts with correct code.

So I'm writing all the nals to a file:

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 );
}

Now I have file.h264 file. So to test this file I use ffmpeg.exe ( I'm on windows ), and I get this output:

[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 also can't play the file.

ffplay tells:

file.h264: Invalid data found when processing input

What is wrong?????

Thanks in advance, Zetlb

Was it helpful?

Solution

Looks like you save only pointers and size returned by x264_encoder_encode and not actual data.

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

This pointed data is only valid before you call next x264_encoder_encode/x264_encoder_close.

OTHER TIPS

It seems that you forgot to write the h.264 file header to the h.264 file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top