The incoming data received by the client can be an incomplete frame (or may be more than one frame, since the data is not organized as the same as you wrote at server end), so you can not decode it directly.<div>Use function <span style="white-space:pre-wrap">av_parser_parse2</span><span style="white-space:pre-wrap">() first to get complete frame one by one from the incoming data.</span></div>
<div><span style="white-space:pre-wrap"><br></span></div><div><span style="white-space:pre-wrap">for usage of av_parse_parse2(), please refer to avcodec.h</span></div><div><span style="white-space:pre-wrap">Here is my code for decoding. I also tried a lot, since the examples in ffmpeg only tell about decode complete frames.</span></div>
<div><pre style="word-wrap:break-word;white-space:pre-wrap">void Server::process()
{
        QTcpSocket *socket = nextPendingConnection();
        cout << "client connected." << endl;

        uint8_t *inbuf = new uint8_t [20000000];
        int inbuf_start = 0;
        int inbuf_len = 0;

        while (socket->waitForReadyRead())
        {
                QByteArray ba = socket->readAll();
                if (ba.size() == 0)
                {
                        cerr << "read 0 bytes data." << endl;
                        continue;
                }
                memcpy(inbuf + inbuf_len, ba.data(), ba.size());
                inbuf_len += ba.size();

                uint8_t *poutbuf;
                int poutbuf_size;
                while (inbuf_len)
                {
                        int len = av_parser_parse2(parser, ctx, &poutbuf, &poutbuf_size,
                                        inbuf + inbuf_start, inbuf_len,
                                        AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);

                        inbuf_start += len;
                        inbuf_len -= len;

                        if (poutbuf_size)
                        {
                                av_init_packet(&avpkt);
                                avpkt.data = poutbuf;
                                avpkt.size = poutbuf_size;

                                int got_picture;
                                int retval = avcodec_decode_video2(ctx, frame, &got_picture, &avpkt);
                                if (got_picture && retval > 0)
                                {
                                        for (int i = 0; i < 480; i++)
                                                memcpy(img.data + i*640, frame->data[0] + i*frame->linesize[0], 640);

                                        emit rawDataReady();
                                }
                        }
                        else // this group of data is less than a complete frame
                        {
                                if (inbuf_start > 0)
                                {
                                        uint8_t *tmp = new uint8_t [20000000];
                                        memcpy(tmp, inbuf + inbuf_start, inbuf_len);
                                        memcpy(inbuf, tmp, inbuf_len);
                                        inbuf_start = 0;
                                        delete [] tmp;
                                }

                                break;
                        }
                }
        }
}</pre><br><div class="gmail_quote">2012/3/23 Yinxia <span dir="ltr"><<a href="mailto:hydezhao@gmail.com">hydezhao@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi dear all,<br>
<br>
I am a newbie to ffmpeg and I would like to decode the bitstream, which is<br>
encoded using libx264 at the server end and sent to client frame by frame.<br>
<br>
I wrote a function decodeFrame to decode this bitstream frame by frame,<br>
this function is called in a loop, each time recieves a new frame:<br>
<br>
void decodeFrame( int size, unsigned char *data)<br>
{<br>
 avpkt.size = size;<br>
avpkt.data = (uint8_t *)(data);<br>
<br>
/*decode each frame*/<br>
 len = avcodec_decode_video2(c, picture, &got_picture, avpkt.size);<br>
if (len < 0) {<br>
 fprintf(stderr, "[avcodec][Err] Error while decoding frame %d\n",frame);<br>
 exit(1);<br>
}<br>
 if (got_picture) {<br>
printf("[avcodec] Saving frame %3d\n",frame);<br>
 fflush(stdout);<br>
_snprintf(buf, sizeof(buf), "/temp/video%d.pgm", frame); // not found<br>
 pgmSave(picture->data[0], picture->linesize[0], c->width, c->height, buf);<br>
 frame++;<br>
}<br>
}<br>
<br>
I can successfully get the value of len, which is exactly the size of<br>
frame, however, *the value of *got_picture *is always 0. *Can anyone help<br>
me with this?<br>
*<br>
*<br>
PS. this is the init function of decoder.<br>
<br>
void initDecoder()<br>
{<br>
avcodec_register_all();<br>
<br>
av_init_packet(&avpkt);<br>
memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);<br>
 printf("[avcodec] Frame decoding initializing\n");<br>
<br>
/*choose correct video decoder*/<br>
 codec = avcodec_find_decoder(CODEC_ID_MPEG4);<br>
if ( !codec ) {<br>
fprintf(stderr, "[avcodec][Err] codec not found!\n");<br>
 exit(1);<br>
}<br>
<br>
c = avcodec_alloc_context3(codec);<br>
 picture = avcodec_alloc_frame();<br>
<br>
if(codec->capabilities&CODEC_CAP_TRUNCATED)<br>
           c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete<br>
frames */<br>
<br>
    /* open it */<br>
    if (avcodec_open2(c, codec, NULL) < 0) {<br>
        fprintf(stderr, "[avcodec][Err] could not open codec\n");<br>
        exit(1);<br>
    }<br>
<br>
<br>
frame = 0;<br>
<br>
}<br>
<br>
<br>
Thank you very much!<br>
Yinxia<br>
<br>
<br>
--<br>
View this message in context: <a href="http://libav-users.943685.n4.nabble.com/Decoding-Problem-got-picture-always-return-0-tp4496039p4496039.html" target="_blank">http://libav-users.943685.n4.nabble.com/Decoding-Problem-got-picture-always-return-0-tp4496039p4496039.html</a><br>

Sent from the libav-users mailing list archive at Nabble.com.<br>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
</blockquote></div><br></div>