[Libav-user] Encoding FLV gives green screen output

yy-zed samhalligan15 at gmail.com
Sun May 5 13:59:38 CEST 2013


I've written a piece of C++ code that can capture webcam video frame, decode
them, convert them to YUV420P, encode them and then write them to a file. If
I use the mpeg2 codec and write to a .mpg file, everything works perfectly.
But, if I use flv, then the output produced is just a green screen. I'm not
sure if there are different encoder settings I need to set for encoding flv
video?Or maybe I'm doing something wrong in sws_scale? Here's my code(the
relevant parts):

Encoder settings:

c->codec_id = codec_id;
c->bit_rate = 400000;
// Resolution must be a multiple of two.
c->width    = 352;
c->height   = 288;
c->time_base.den = STREAM_FRAME_RATE;
c->time_base.num = 1;
//emit one intra frame every twelve frames at most
c->gop_size      = 12;
c->pix_fmt       = STREAM_PIX_FMT;

Encoding a frame:

int ret;
uint8_t *buffer = NULL;
static struct SwsContext *sws_ctx;

//Setup the codec context, and set its width and height to be equal to the
input video width and height
AVCodecContext *c = st->codec;
c->width = pCodecCtx->width;
c->height = pCodecCtx->height;

av_init_packet(&packet);
frameYUV = avcodec_alloc_frame();

//Determine how big the buffer will need to be to store the captured frame
numBytes =
avpicture_get_size(STREAM_PIX_FMT,pCodecCtx->width,pCodecCtx->height);

//Allocate the needed buffer size
buffer = new uint8_t[numBytes];
sws_ctx =
sws_getContext(pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt,
                        pCodecCtx->width,pCodecCtx->height,
                        STREAM_PIX_FMT,SWS_BICUBIC,NULL,NULL,NULL);

//Fill the output frame
avpicture_fill((AVPicture
*)frameYUV,buffer,STREAM_PIX_FMT,pCodecCtx->width,pCodecCtx->height);

//Read a video frame in
av_read_frame(pFormatCtx,&packet);

//Decode the contents of packet into pFrame
avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);

//Scale pFrame into frameYUV, and convert to PIXFMTYUV420P
sws_scale
(
    sws_ctx,
    (uint8_t const * const *)pFrame->data,
    pFrame->linesize,
    0,
    pCodecCtx->height,
    frameYUV->data,
    frameYUV->linesize
);
av_init_packet(&samsPacket);
//Encode frameYUV
avcodec_encode_video2(c, &samsPacket, frameYUV, &gotSamsPacket);

AVPacket pkt = { 0 };
int got_packet;
av_init_packet(&pkt);
// encode the image
ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
if (ret < 0){
    debugLogStreamSave->debug("Error encoding video frame");
    exit(1);
}
if (!ret && got_packet && pkt.size){
    pkt.stream_index = st->index;

    // Write the compressed frame to our output
    ret = av_interleaved_write_frame(oc, &pkt);

Really scratching my head over this one, any help would be really
appreciated!



--
View this message in context: http://libav-users.943685.n4.nabble.com/Encoding-FLV-gives-green-screen-output-tp4657503.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list