当分配avpkt.size时,avcodec_decode_video2崩溃

时间:2014-06-12 10:38:19

标签: c++ ffmpeg decoding decoder

当我尝试分配avpkt.size时,avcodec_decode_video2方法崩溃了。当我将其设置为0时,它可以工作,但它不会解码任何东西(返回值为0)。我错过了什么,或者为avpklt.size分配了什么价值?我想解码一个在istream参数中编码的帧。

谢谢

    int DecodeVideoFFMPEG::dec_main( void *istream, void *outstream,  int width, int height )
{

    avcodec_register_all();
    int got_picture;

    int BYTEPIC = width * height * 3;
    AVCodecContext *d;
    AVCodec *decodec = avcodec_find_encoder(CODEC_ID_WMV1);


    d = avcodec_alloc_context3(decodec);
    d->codec_id = CODEC_ID_WMV1;
    d->pix_fmt = PIX_FMT_YUV420P;
    d->width = width;
    d->height = height;
    d->time_base.num = 1;
    d->time_base.den = 25;

    int status = avcodec_open2(d, decodec, NULL);

    AVFrame *picture2 = alloc_picture420P(width, height);
    AVFrame *pictureBGR = alloc_pictureBGR24(width, height);

    AVPacket avpkt;
    av_init_packet(&avpkt);

    unsigned char *image = new unsigned char[BYTEPIC];
    memcpy(image, istream, BYTEPIC);

    avpkt.data = image;
    avpkt.size = BYTEPIC;  // ????

    //prepare for changing color space
    struct SwsContext *img_convert_ctx2 =
    sws_getContext(width, height, PIX_FMT_YUV420P,
    width, height, PIX_FMT_BGR24,
    SWS_BICUBIC, NULL, NULL, NULL);

    int len = avcodec_decode_video2(d, picture2, &got_picture, &avpkt);

    sws_scale(img_convert_ctx2, picture2->data, picture2->linesize, 0, height, pictureBGR->data, pictureBGR->linesize);

    outstream = pictureBGR->data;

    return len;
}

1 个答案:

答案 0 :(得分:0)

您正在将缓冲区传递给不包含编码视频的avcodec_decode_video2()。这是一个完全无效的操作。