Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Programming and Hacking > Development
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread Display Modes
Old 31st August 2010, 17:57   #1  |  Link
SmutjeSanji
Registered User
 
Join Date: Mar 2010
Posts: 2
Q: Proper way of creating NALs of encoded frame.....

Hello,
I hope you can give me some hints. Till yet it was enough to use Google (to search forums etc) for learning but Iam really stuck right now and need your ideas.

-- What iam doing in general and what the problem is

Ive devoloped a server app so far that it is encoding a video with x264 - the video should be a live stream later, now I use videofiles -, packeting the encoded frames in NALs (x264) and sending it to a client who asks for a stream. And all steps happens in real time which means f.e. that after encoding 3-4 frames and packeting in NALs the server sends the NALs immediatly to the client.

The client is developed so far that it gets the stream - in form of NAL packets - and tries to decode it (with ffmpeg) - I give the NALs directly to ffmpeg without modifing them.

The problem is that on client side ffmpeg cant decode the NALs it gets - its telling me that no frame is found inside the NAL packets.

I know that you will answer me just in topics which involves x264, so Iam just asking you about that and not about the other parts of the program.

-- What I want to ask: Do I create NALs correctly?

As codebasis for the x264 encoding part of the server app I ve used x264.c and modified it so that the encoded frames are not written to harddisk anymore. Instead of that I create NAL packets of them and put them in a deque which another thread handles to send the NALs through network.

The modified "Encode_frame()" looks like this:

Code:
static int Encode_frame( x264_t *h, x264_picture_t *pic, int64_t *last_dts,
                         std::deque<NALBuffer>* nalQueue, pthread_mutex_t* lockQueue )
{
    x264_picture_t pic_out;
    x264_nal_t *nal;
    int i_nal;
    int i_frame_size = 0;

    i_frame_size = x264_encoder_encode( h, &nal, &i_nal, pic, &pic_out );

    if( i_frame_size < 0 )
    {
        fprintf( stderr, "x264 [error]: x264_encoder_encode failed\n" );
        std::cout << "i_frame_size: " << i_frame_size << std::endl;
        return -1;
    }

    if( i_frame_size )
    {
        for(int i = 0; i < i_nal; i++)
        {

            int long_startcode = !i || nal[i].i_type == NAL_SPS || nal[i].i_type == NAL_PPS;
            NALBuffer nalBuffer;
            nalBuffer.size = x264_nal_encode( h, nalBuffer.buffer, &nal[i], long_startcode );
            nalBuffer.lastNAL = ((i+1) == i_nal) ? true : false;

            // One NAL (of probably more) of a videoframe is created

            pthread_mutex_lock(lockQueue);
            nalQueue->push_front(nalBuffer);
            pthread_mutex_unlock(lockQueue);

            // NAL is added to (sending) queue - another thread is reading the queue and send the NALs
        }

        *last_dts = pic_out.i_dts;
    }
    
    return i_frame_size;
}
Its working. But as I ve said ffmpeg cant decode the NALs by telling me that there are no frames inside it.

The questions are now: Do the NALs are created correctly? Or do I have misconfigurations of ffmpeg on client side which cant decode the NALs? Or do the NALs get corrupt during transport - but right now Iam just testing at localhost, so that this shouldnt be the case?


Last edited by SmutjeSanji; 31st August 2010 at 18:08.
SmutjeSanji is offline   Reply With Quote
Old 31st August 2010, 18:35   #2  |  Link
onesloth
Registered User
 
Join Date: Jul 2007
Posts: 137
try doom10
onesloth is offline   Reply With Quote
Old 1st September 2010, 10:46   #3  |  Link
Warperus
Registered User
 
Join Date: Apr 2010
Location: Sain-Petersburg, Russia
Posts: 139
1) Client should be able to decode video as far as it gets enough frames to cover the limit for b-frames reference.
As far as I remember b-frame can reference any frame in VBV buffer (before or after it). Frames referenced by b-frame are packed and saved to file before corresponding b-frame.
So if you send not enough frames decoder can't start.

2) NAL packet size is probably much less than even a single frame so I'm nearly sure one NAL is not enough for decoder to start. From your text I can assume you are trying to feed decoder as soon as you get new NAL (it might be incorrect assumption though).
I don't know of ffmpeg ability to handle NAL packets...
If I were you I'd check ffmpeg documentation for possibility and limitations of this method. I'm pretty sure it says all the criteria for decoding your client should comply before calling ffmpeg.
Warperus is offline   Reply With Quote
Old 4th September 2010, 11:38   #4  |  Link
SmutjeSanji
Registered User
 
Join Date: Mar 2010
Posts: 2
Hi, thanks for your hints. Ive posted my problem at doom10 and got some hints. If youre interested in youre welcome: http://doom10.org/index.php?topic=534.0
SmutjeSanji is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:41.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.