0

the avcodec_decode_video2 method crashes, when I try to assign the avpkt.size. When I set it to 0 it works, but it doesn't decode anything (the return value is 0). Am I somthing missing, or what value has to be assigned to avpklt.size? I want to decode one Frame that is encoded in the istream parameter.

Thank you

    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;
}
user2672165
  • 2,986
  • 19
  • 27
klanm
  • 3,168
  • 3
  • 19
  • 22
  • Ok I found the mistake.. I need to use avcodec_find_decoder(CODEC_ID_WMV1) obviously ;) + severel other noob mistakes, like the outstream allocation etc. – klanm Jun 17 '14 at 17:13

1 Answers1

0

You are passing a buffer to avcodec_decode_video2() that does not contain encoded video. It is a completely invalid operation.

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • But with the two lines unsigned char *image = new unsigned char[BYTEPIC]; memcpy(image, istream, BYTEPIC); I am passing the filled buffer from the parameters (istream), and the encoding before gives me a packetsize, so I am sure there is content in this buffer – klanm Jun 12 '14 at 23:11
  • Where is BYTEPIC = width * height * 3; coming from? You are making an (bad) assumption about the size of the encoded frame. You are also not checking the result of got_picture. – szatmary Jun 12 '14 at 23:22
  • Yeah your're right I realized it an hour after posting it, and added an parameter where the exact size of the encoded package is passed and then used as avpkt.size, but it still crashes. and I can't check got_picture, because it crashes before – klanm Jun 13 '14 at 00:30