[FFmpeg-devel] Annex B H.264 decoding with FFMPEG

Felipe Lalanne flalanne
Tue Feb 3 15:10:24 CET 2009

I have the following question, i haven't been able to find an answer
anywhere. I'm implementing a H.264 video tranmission application, the way it
works is very simple, I take a h.264 annex B file, divide it into the NAL
units according to the specification, and then send each NAL unit using UDP
over the network.

On the reception side, I want to receive the NAL units and start decoding
inmediatly (or as soon as posible) for playback, without necessarily saving
it into a file. I've been trying to do it using ffmpeg, and
avcodec_decode_video using a code similar to the one following the text.
This doesn't work, as I understand, because a nal unit usually doesn't
contain a whole frame (hence the [h264 @ 0x8055550]no frame! message)

I'm thinking of concatenating the received NAL units and try to parse the
union with av_parser_parse, before decoding the frame, but I'm not sure if
that would be most efficient solucion.
What do you think? Could you give me any suggestions?

Now that I'm ending the text I'm thinking that I might be in the wrong list.
If that is the case, I apologize (and could you maybe point me to the right

Thank you all in advance.
Best regards
Felipe Lalanne

--- Code --

/* NAL Structure description */
typedef struct NAL {
    int         ref_idc;
    int         unit_type;
    int         slice_type;
    unsigned char *        buf;
    int        len;
} NALUnit;

int decode_video() {
    AVCodec * codec = avcodec_find_decoder(CODEC_ID_H264);
    if (!pCodec) {
        return -1;

    AVCodecContext * context = avcodec_alloc_context();
    context->width = width;
    context->height = height;

    if (avcodec_open(context, codec) < 0) {;
        return -1;

    int frameFinished, len;

    AVFrame * frame = avcodec_alloc_frame();
    while (true) {
        NAL * nal = receive_nal();

        len = avcodec_decode_video(context, frame,
                &frameFinished, nal->buf, nal->len);

        if (frameFinished) {

More information about the ffmpeg-devel mailing list