[Libav-user] A very mysterious question about FFMpeg encoding.

Denis info at denisgottardello.it
Thu Sep 22 23:02:52 CEST 2011


> You need to show the source of your data. How do you get the frames from a
> webcam, does it come in packets? RGB data? that's the code you should show.
> The problem that I'm seeing is that you're using an avio context when you
> don't actually intend to write data with it to a file. Just take the webcam
> frame data call avcodec_encode_video and that function writes it to a
> buffer. I'm not sure why you're making the all these avio calls. I suggest
> you also look at the headers for avcodec_encode_video.




The source code is below. Can you explain me which are the steps that I must 
follow?


void QThCamera::run() {
    qDebug() << "QThCamera::run() start";
    QString MPGFileName= "http://localhost:3000/a.mp4";

    CvCapture *Capture= NULL;
    Capture= cvCreateCameraCapture(Index, Width, Height);
    if (!Capture) qDebug() << "cvCreateCameraCapture Error!";
    else {
        if (ExternalFrame) cvNamedWindow("Frame", CV_WINDOW_AUTOSIZE);
        IplImage *frame= 0;
        CvFont font;
        cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5f, 0.5f);

        AVOutputFormat *pOutputFormat= av_guess_format("mp4", NULL, NULL); // 
CODEC_ID_H264 -> mp4, CODEC_ID_THEORA -> ogg, CODEC_ID_MPEG4 -> mpegts, 
CODEC_ID_VP8 -> webm
        if (!pOutputFormat) {
            qDebug() << "Could not set output format, using MPEG.";
            pOutputFormat= av_guess_format("mpeg", NULL, NULL);
        }
        if (!pOutputFormat) qDebug() << "av_guess_format Error!";
        else {
            AVFormatContext *pFormatCtx;
            if (avformat_alloc_output_context2(&pFormatCtx, pOutputFormat, 
NULL, NULL)< 0) qDebug() << "avformat_alloc_output_context2 Error!";
            else {
                AVStream *pVideoStream= av_new_stream(pFormatCtx, 0);
                if (!pVideoStream) qDebug() << "av_new_stream Error!";
                else {
                    AVCodecContext *pCodecCtx= pVideoStream->codec;
                    pCodecCtx->codec_id= CODEC_ID_H264;
                    pCodecCtx->codec_type= AVMEDIA_TYPE_VIDEO;
                    pCodecCtx->bit_rate= 40000;
                    pCodecCtx->width= Width;
                    pCodecCtx->height= Height;
                    pCodecCtx->time_base.den= 25;
                    pCodecCtx->time_base.num= 1;
                    pCodecCtx->gop_size= 10;
                    pCodecCtx->pix_fmt= PIX_FMT_YUV420P;
                    if (pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) 
pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
                    av_dump_format(pFormatCtx, 0, NULL, 1);
                    AVCodec *pCodec= avcodec_find_encoder(pCodecCtx-
>codec_id);
                    if (!pCodec) qDebug() << "avcodec_find_encoder Error!";
                    else {
                        if (avcodec_open(pCodecCtx, pCodec)< 0) qDebug() << 
"avcodec_open Error!";
                        else {
                            /*QFMPGFileOut.setFileName(MPGFileName);
                            if (QFMPGFileOut.open(QIODevice::WriteOnly)) {*/
                                if (avio_open(&pFormatCtx->pb, 
MPGFileName.toStdString().c_str(), AVIO_FLAG_WRITE)< 0) qDebug() << "avio_open 
Error!";
                                else {

                                    /*pFormatCtx->pb->opaque= this;
                                    pFormatCtx->pb->write_packet= WritePacket;
                                    pFormatCtx->pb->write_flag= 
AVIO_FLAG_WRITE;*/

                                    if (avformat_write_header(pFormatCtx, 
NULL)!= 0) qDebug() << "av_write_header Error!";
                                    int BYTEPIC= Width * Height * 3;
                                    uint8_t *pOutBuffer= 
(uint8_t*)malloc(BYTEPIC); {
                                        int Frames= 0;
                                        QDateTime QDTStart= 
QDateTime::currentDateTime();
                                        while (DoStart) {
                                            if (!cvSetChannel(Capture, 
Channel)) qDebug() << "cvSetChannel Error!";
                                            frame= cvQueryFrame(Capture);
                                            if (frame) {
                                                if (MainForm->ui-
>QCBAutoBrightness->isChecked()) {
                                                    int Brightness= 
BrightnessOfAPixel(frame);
                                                    Brightness= ((Brightness * 
200 / 256) - 100) * -1;
                                                    IplImage *frame2= 
ContrastBrightness(frame, 0, Brightness); {
                                                        cvCopyImage(frame2, 
frame);
                                                    }{
                                                        cvReleaseImage(&frame2);
                                                    }
                                                } else {
                                                    IplImage *frame2= 
ContrastBrightness(frame, MainForm->ui->QHSContrast->value(), MainForm->ui-
>QHSBrightness->value()); {
                                                        cvCopyImage(frame2, 
frame);
                                                    }{
                                                        cvReleaseImage(&frame2);
                                                    }
                                                }
                                                if (MainForm->ui-
>QCBShowDateTime->isChecked()) cvPutText(frame, 
QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss").toAscii(), 
cvPoint(10, 20), &font, cvScalar(255, 255, 255));
                                                if (MainForm->ui->QRBRotate90-
>isChecked()) {
                                                    IplImage *frame2= 
Rotate(frame, 90); {
                                                        cvCopyImage(frame2, 
frame);
                                                    }{
                                                        cvReleaseImage(&frame2);
                                                    }
                                                } else if (MainForm->ui-
>QRBRotate180->isChecked()) {
                                                    IplImage *frame2= 
Rotate(frame, 180); {
                                                        cvCopyImage(frame2, 
frame);
                                                    }{
                                                        cvReleaseImage(&frame2);
                                                    }
                                                } else if (MainForm->ui-
>QRBRotate270->isChecked()) {
                                                    IplImage *frame2= 
Rotate(frame, 270); {
                                                        cvCopyImage(frame2, 
frame);
                                                    }{
                                                        cvReleaseImage(&frame2);
                                                    }
                                                }
                                                if (ExternalFrame) 
cvShowImage("Frame", frame);
                                                QImage Image(frame->width, 
frame->height, QImage::Format_RGB888);
                                                const unsigned char *data= 
NULL;
                                                data= (unsigned char*)(frame-
>imageData);
                                                Image= QImage(data, frame-
>width, frame->height, frame->widthStep, QImage::Format_RGB888);
                                                if (DoStart) emit 
SendNewImage(Image);

                                                AVFrame *pAVFrame= 
avcodec_alloc_frame();
                                                uint8_t *pBuffer= 
(uint8_t*)malloc(avpicture_get_size(PIX_FMT_YUV420P, Width, Height)); {
                                                    avpicture_fill((AVPicture*)pAVFrame, 
pBuffer, PIX_FMT_YUV420P, Width, Height);
                                                    IplImageToAVFrame(frame, 
pAVFrame, Width, Height, PIX_FMT_YUV420P);
                                                    pAVFrame->pts= Frames;
                                                    int OutSize= 
avcodec_encode_video(pCodecCtx, pOutBuffer, BYTEPIC, pAVFrame);
                                                    if (OutSize> 0) {
                                                        AVPacket Packet;
                                                        av_init_packet(&Packet);
                                                        //Packet.pts= Frames;
                                                        if (pCodecCtx-
>coded_frame->pts != AV_NOPTS_VALUE) {
                                                            Packet.pts= 
av_rescale_q(pCodecCtx->coded_frame->pts, pCodecCtx->time_base, pVideoStream-
>time_base);
                                                        }
                                                        if (pCodecCtx-
>coded_frame->key_frame) Packet.flags |= AV_PKT_FLAG_KEY;
                                                        Packet.stream_index= 
pVideoStream->index;
                                                        Packet.data= 
pOutBuffer;
                                                        Packet.size= OutSize;
                                                        //qDebug() << 
"av_interleaved_write_frame before";
                                                        if 
(av_interleaved_write_frame(pFormatCtx, &Packet)!= 0) qDebug() << 
"av_interleaved_write_frame Error!";
                                                        //qDebug() << 
"av_interleaved_write_frame after";
                                                    }
                                                    Frames++;
                                                    if (Frames> pCodecCtx-
>time_base.den / pCodecCtx->time_base.num * 10) break;
                                                }{
                                                    av_free(pAVFrame);
                                                    free(pBuffer);
                                                }


                                            } else qDebug() << 
"QThCamera::run() frame= false";
                                        }
                                        qDebug() << Frames / 
QDTStart.secsTo(QDateTime::currentDateTime());
                                    }{
                                        free(pOutBuffer);
                                    }
                                    if (av_write_trailer(pFormatCtx)!= 0) 
qDebug() << "av_write_trailer Error!"; // Crash with H264!!!
                                    //pFormatCtx->pb->opaque= NULL;
                                    avio_close(pFormatCtx->pb);
                                }
                                QFMPGFileOut.close();
                            //} else qDebug() << "QFMPGFileOut.open() Error!";
                            avcodec_close(pVideoStream->codec);
                        }
                    }
                    for (int count= 0; (unsigned)count< pFormatCtx-
>nb_streams; count++) {
                        av_freep(&pFormatCtx->streams[count]->codec);
                        av_freep(&pFormatCtx->streams[count]);
                    }
                }
                av_free(pFormatCtx);
            }
        }
        cvReleaseCapture(&Capture);
        if (ExternalFrame) cvDestroyWindow("Frame");
    }
    qDebug() << "QThCamera::run() stop";
}




-- 
www.denisgottardello.it
Skype: mrdebug
Videosurveillance and home automation! 
http://www.denisgottardello.it/DomusBoss/DomusBossIndice.php
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20110922/86602b63/attachment.html>


More information about the Libav-user mailing list