[Live-devel] How can i detect the size of nal unit?

aviad rozenhek aviadr1 at gmail.com
Sun Apr 15 04:28:56 PDT 2012


try this

extern "C" int find_nal_unit(const unsigned char* buf, int size, int*
nal_start, int* nal_end)
{
    int i;
    // find start
    *nal_start = 0;
    *nal_end = 0;

    i = 0;
    while (   //( next_bits( 24 ) != 0x000001 && next_bits( 32 ) !=
0x00000001 )
           (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) &&
           (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] !=
0x01)
           )
    {
        i++; // skip leading zero
        if (i+4 >= size) { return 0; } // did not find nal start
    }

    if  (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) // ( next_bits(
24 ) != 0x000001 )
    {
        i++;
    }

    if  (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01) { /* error,
should never happen */ return 0; }
    i+= 3;
    *nal_start = i;

    while (   //( next_bits( 24 ) != 0x000000 && next_bits( 24 ) !=
0x000001 )
           (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0) &&
           (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0x01)
           )
    {
        i++;
        // FIXME the next line fails when reading a nal that ends exactly
at the end of the data
        if (i+3 >= size) { *nal_end = size; return size - *nal_start; } //
did not find nal end, stream ended first
    }

    *nal_end = i;
    return (*nal_end - *nal_start);
}

/**
 * test method showing how to use find_nal_unit
 * for the first frame in inglorious bastards, it prints out:
 *    NAL: type=6, size=678, start=4, end=682
 *    NAL: type=7, size=33, start=686, end=719
 *    NAL: type=8, size=5, start=723, end=728
 *    NAL: type=5, size=96, start=731, end=824
 */
/*void test(MediaSample* pSample)
{
    int s           = pSample->GetActualDataLength();
    uint8_t* p      = pSample->GetPointer();
    int nal_start, nal_end, nal_offset = 0;
    while(s > 0) {
        int nal_size = find_nal_unit(p, s, &nal_start, &nal_end);
        int unit_type = p[nal_start] & 0x1f;
        std::cout << "NAL: type=" << unit_type << ", size=" << nal_size <<
", start=" << nal_offset + nal_start << ", end=" << nal_offset + nal_end <<
std::endl;
        p          += nal_end;
        s          -= nal_end;
        nal_offset += nal_end;
    }
}*/


On Sun, Apr 15, 2012 at 04:07, Ross Finlayson <finlayson at live555.com> wrote:

> 1.>How can i detect the size of nal unit?
> 2/>How can i detect the end of nal unit?
> 3.>How can i detect the I-Frame in an packet?
> 4.>How can i detect theB,P-Frame in an packet?
>
>
> For this you need a H.264 decoder - which we don't provide.  (Therefore,
> your question is off-topic for this mailing list.)
>
>
> Ross Finlayson
> Live Networks, Inc.
> http://www.live555.com/
>
>
> _______________________________________________
> live-devel mailing list
> live-devel at lists.live555.com
> http://lists.live555.com/mailman/listinfo/live-devel
>
>


-- 
Aviad Rozenhek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20120415/e4cf5d6a/attachment-0001.html>


More information about the live-devel mailing list