raullalves
raullalves

Reputation: 856

Error while decoding Raw H264 frames using ffmpeg

I would really appreciate some help in this issue. I'm new to FFmpeg, therefore forgive me if this is a dumb question

I have an application streamming H264 raw data from a camera via UDP. That communication works fine. The problem is while trying to decoding

I simply want to decode that data I receive, in order to apply a few Computer Vision techniques to another project.

I got error -22 right after calling

avcodec_send_packet

Does any of you know what could it be? Here is part of my code

Mat DecodificadorH264::h264decode(char* rawH264, int size){
 ...
 ...
AVPacket av_packet;
av_init_packet(&av_packet);
uint8_t data;
data = *rawH264;
av_packet.data = &data;
av_packet.size = size;

cout << "size = "<<av_packet.size;

int frame_finished = 0;

l_pCodec = avcodec_find_decoder(AV_CODEC_ID_H264);

l_pAVCodecContext = avcodec_alloc_context3(l_pCodec);

l_pAVCodecContext->width = FRAME_WIDTH;
l_pAVCodecContext->height = FRAME_HEIGHT;
l_pAVCodecContext->extradata = NULL;

l_pAVCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;

int av_return = avcodec_send_packet(l_pAVCodecContext,
(AVPacket*)&av_packet);
if (av_return == AVERROR(EINVAL))cout << "codec not opened, or it is an encoder" << endl;
 ....

av_return returns "AVERROR(EINVAL)", which is defined as -22

What could be the problem?

Thanks in advance!

Upvotes: 0

Views: 2538

Answers (2)

Colin Jensen
Colin Jensen

Reputation: 3809

You need one more step to making a usable decode context: avcodec_open2()

codec = avcodec_find_decoder (AV_CODEC_ID_H264);
context = avcodec_alloc_context3 (codec);
AVDictionary *dict = nullptr;
avcodec_open2(context, codec, &dict);

Upvotes: 0

Tony J
Tony J

Reputation: 651

Well I see one problem so far:

uint8_t data;
data = *rawH264;
av_packet.data = &data; // all you did here is copy the first char from rawH264 into data

What you need is

av_packet.data = rawH264; // you might have to cast rawH264 to uint8_t* first

According to https://ffmpeg.org/doxygen/0.6/error_8h.html: #define AVERROR_INVALIDDATA AVERROR(EINVAL) Invalid data found when processing input.

So it makes sense the result is EINVAL, because you passed in &data, which is on the stack, and with size parameter, it was reading the stack as if it's actual encoded video data.

Upvotes: 1

Related Questions