-2

I have a problem with my code.

I want convert YUV to RGB, but sws_scale returns 0 always and data is filled 00000000..NULL

I tried googling, but I could not find any problems.

Do I need AVPicture or QImage? Can anyone point out my problem?

 pVFrame = av_frame_alloc();
 while (av_read_frame(pFormatCtx, &packet) == 0) {
        if (packet.stream_index == VSI) {
            if (bool res = avcodec_send_packet(pVideoCodecCtx, &packet)) {
                printf("avcodec_send_packet failed %d %d %d\n", res, AVERROR(EINVAL), AVERROR(ENOMEM));
            }
            avcodec_receive_frame(pVideoCodecCtx, pVFrame);

            AVFrame* YUV_frame = Con_yuv_YUV420P(pVFrame);
            QFrame->push(Con_yuv_RGB(YUV_frame));
        }
    }
}


AVFrame* Con_yuv_RGB(AVFrame* YUV_frame)
{
AVFrame* RGBFrame = av_frame_alloc();

SwsContext* sws_Context = NULL;
sws_Context = sws_getCachedContext(sws_Context, YUV_frame->width, YUV_frame->height, pVideoCodecCtx->pix_fmt,
    YUV_frame->width, YUV_frame->height, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);

if (sws_Context == NULL) {
    return false;
}
result = sws_scale(sws_Context, YUV_frame->data, YUV_frame->linesize, 0, (int)YUV_frame->height, RGBFrame->data, RGBFrame->linesize);
if (result < 0)
{
    return false;
}
av_frame_unref(YUV_frame);
if (RGBFrame == NULL) {
    av_frame_unref(RGBFrame);
    return false;
}

sws_freeContext(sws_Context);

return RGBFrame;

What is the problem?

enter image description here

Reference link - https://gist.github.com/nakaly/11eb992ebd134ee08b75e4c67afb5703,

http://codefromabove.com/2014/10/ffmpeg-convert-rgba-to-yuv/,

sws_scale YUV --> RGB distorted image,

https://www.gamedev.net/forums/topic/464977-sws_scale-problems/

https://ffmpeg.org/doxygen/2.7/group__libsws.html#gae531c9754c9205d90ad6800015046d74

ADD: enter image description here

before convert. It has to fill the buffer.

RGBFrame->width = YUV_frame->width;     RGBFrame->format = AV_PIX_FMT_RGB32;
RGBFrame->height = YUV_frame->height;
int result = av_frame_get_buffer(RGBFrame, 32);
if (result != 0)    return false;
Birds
  • 65
  • 2
  • 13

1 Answers1

0

The ffmpeg documentation covers it:

https://www.ffmpeg.org/doxygen/2.3/group__lavu__frame.html#gac700017c5270c79c1e1befdeeb008b2f

AVFrame* av_frame_alloc ( void )

Allocate an AVFrame and set its fields to default values.

The resulting struct must be freed using av_frame_free().

Returns An AVFrame filled with default values or NULL on failure. Note this only allocates the AVFrame itself, not the data buffers. Those must be allocated through other means, e.g. with av_frame_get_buffer() or manually.

https://www.ffmpeg.org/doxygen/2.3/group__lavu__frame.html#ga6b1acbfa82c79bf7fd78d868572f0ceb

int av_frame_get_buffer ( AVFrame * frame, int align ) Allocate new buffer(s) for audio or video data.

The following fields must be set on frame before calling this function:

format (pixel format for video, sample format for audio) width and height for video nb_samples and channel_layout for audio This function will fill AVFrame.data and AVFrame.buf arrays and, if necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf. For planar formats, one buffer will be allocated for each plane.

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • Thank you for your answer! with I saw your other answers. You are my idol. So I add my code Width, height, and format. and av_frame_get_buffer looks like working right way. But I can't sure it is all complete convert RGB. How can I check it's RGBFrame? when debugging Frame->data has Invalid characters in string. I add capture image at question – Birds Jan 10 '19 at 04:50
  • `data` is a pointer to a pointer, so make sure you are looking at RGBFrame->data[0]. And since pixel values can be anything 0-255 per channel, its not possible to have an invalid byte. – szatmary Jan 10 '19 at 04:53
  • RGBFrame->data [0] has strange data.. It's mean to convert failed? Please more explanation. – Birds Jan 10 '19 at 05:06
  • please expand. what is strange about it? – szatmary Jan 10 '19 at 05:07
  • In debugging, RGBFrame->data[0] shows me "Invalid characters in string." I guess this convert successfully. Is this right? – Birds Jan 10 '19 at 05:13
  • I cant say without seeing the data, But because every possible byte value is valid in a raw RGB buffer, there is literally no such thing as "Invalid characters". I don't know what that message if from, but what ever it is, it doesn't know the format of the data it is parsing. – szatmary Jan 10 '19 at 05:19
  • I added capture image in question. could you check it please? jpeg decode frame and I will try to H.264 after solve the problem. – Birds Jan 10 '19 at 05:27
  • Because its not a string. it's an image. Your IDE is trying to parse it as text, and it's failing because it's not text. There is literally no such thing as Invalid characters in a raw RGB buffer. Ignore that message – szatmary Jan 10 '19 at 05:34
  • I understood. Thanks a lot. It's so much helpful. – Birds Jan 10 '19 at 05:39