[Libav-user] swscaler bad src image pointers, can not figure out why.

Dorian Green doriangreen97 at gmail.com
Mon Sep 16 04:11:37 EEST 2019


hello, I am trying to become more adept at using the libav libraries and have

practising them. The problem I am having is that swscale at the bottom of my

code listed here always returns bad src image pointers. this below is the main

chuck of my learning test code.


format_context = avformat_alloc_context();

if(!format_context){
    std::cout << "failed to allocate memory for AVFormatContext " <<
filename << std::endl;
    return 0;
}else{
    std::cout << "allocated memory for AVFormatContext " << filename
<< " successfully" << std::endl;
}

if(avformat_open_input(&format_context,filename,nullptr,nullptr) != 0){
    std::cout << " could not open the file" << std::endl;
    return 0;
}else{
    std::cout << "opened file successfully" << std::endl;
}

if(avformat_find_stream_info(format_context, nullptr) < 0){
    std::cout << "could not get stream info" << std::endl;
    return 0;
}

av_dump_format(format_context,0,filename, 0);

video_stream_index = av_find_best_stream(
        format_context,
        AVMEDIA_TYPE_VIDEO,
        -1,
        -1,
        &av_video_codec_decoder,
        0
);

if(video_stream_index == AVERROR_STREAM_NOT_FOUND || !av_video_codec_decoder){
    std::cout << "video stream not found." << std::endl;
    return 0;
}else{
    std::cout << "video stream found successfully." << std::endl;
}

audio_stream_index = av_find_best_stream(
        format_context,
        AVMEDIA_TYPE_AUDIO,
        -1,
        -1,
        &av_audio_codec_decoder,
        0
);

if(audio_stream_index == AVERROR_STREAM_NOT_FOUND || !av_audio_codec_decoder){
    std::cout << "audio stream not found." << std::endl;
    return 0;
}else{
    std::cout << "audio stream found successfully." << std::endl;
}

codec_context = avcodec_alloc_context3(av_video_codec_decoder);

if(!codec_context){
    std::cout << std::endl << "failed to allocate memory for
AVCodecContext" << std::endl;
    return 0;
}else{
    std::cout << std::endl << "allocated memory for AVCodecContext" <<
std::endl;
}

if(avcodec_parameters_to_context(codec_context,
format_context->streams[video_stream_index]->codecpar) < 0){
    std::cout << "Failed to set parameters." << std::endl;
    return 0;
}else{
    std::cout << "parameters set successfully." << std::endl;
}

if (avcodec_open2(codec_context, av_video_codec_decoder, nullptr) < 0) {
    std::cout << "Could not open codec." << std::endl;
    return -1;
}else{
    std::cout << "Codec opened successfully" << std::endl;
}

if(codec_context-> height == 0 || codec_context->width == 0){
    std::cout << "something went wrong. codec_context height or width
of 0 detected" << std::endl;
    return 0;
}else{
    std::cout << "codec_context appears correct.." << std::endl;
}

secondary_av_frame = av_frame_alloc();

if(!secondary_av_frame){
    std::cout << "failed to allocate memory for secondary_av_frame
(AVFrame)." << std::endl;
    return 0;
}else{
    std::cout << "allocated memory for secondary_av_frame (AVFrame)"
<< std::endl;
}

primary_av_frame = av_frame_alloc();

if(!primary_av_frame){
    std::cout << "failed to allocate memory for primary_av_frame
(AVFrame)." << std::endl;
    return 0;
}else{
    std::cout << "allocated memory for primary_av_frame (AVFrame)" << std::endl;
}

primary_av_frame->format = codec_context->pix_fmt;
primary_av_frame->width = codec_context->width;
primary_av_frame->height = codec_context->height;

int ret = av_frame_get_buffer(primary_av_frame, 0);

if(ret < 0){
    std::cout << "failed av_frame_get_buffer" << std::endl;
    return 0;
}else{
    std::cout << "av_frame_get_buffer successful" << std::endl;
}

buffer_size = av_image_get_buffer_size(
        codec_context->pix_fmt,
        codec_context->width,
        codec_context->height,
        1);

if(!buffer_size){
    std::cout << "failed to allocate buffer_size (size_t)" << std::endl;
    return 0;
}else{
    std::cout << "allocated memory for buffer_size (size_t)" << std::endl;
}

sws_context = sws_getContext(codec_context->width,
codec_context->height, codec_context->pix_fmt,
                             codec_context->width, codec_context->height,
                             AV_PIX_FMT_RGB24, SWS_BICUBIC, nullptr,
nullptr, nullptr);

if(sws_context){
    std::cout << "allocated sws_context" << std::endl;
}

while(av_read_frame(format_context, primary_packet) >=0 ) {
    glClear(GL_COLOR_BUFFER_BIT);
    std::cout << "video current framecount: " +
std::to_string(++framecount) << std::endl;

    if (primary_packet->stream_index == video_stream_index) {

        return_value = avcodec_send_packet(codec_context, primary_packet);

        if (return_value != 0) {

            if (return_value == AVERROR_EOF) {
                end_of_file = true;
                std::cout << "end of file: the decoder has been
flushed. (this can also mean more than one flush packet.)" <<
std::endl;
            } else if (return_value == AVERROR(EINVAL)) {
                std::cout << "codec not opened or requires flush" << std::endl;
            } else if (return_value == AVERROR(ENOMEM)) {
                std::cout << "failed to add packet to internal queue,
or similar" << std::endl;
            } else {
                std::cout << "Unexpected Error: " +
std::to_string(return_value) << std::endl;
            }

        } else {
            std::cout << "packet sent successfully" << std::endl;

            return_value = avcodec_receive_frame(codec_context,
primary_av_frame)

            sws_scale(sws_context,
                      primary_av_frame->data,
                      primary_av_frame->linesize,
                      0,
                      codec_context->height,
                      primary_display_frame->data,
                      primary_display_frame->linesize);

sws_freeContext(sws_context);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20190915/f48db2ab/attachment.html>


More information about the Libav-user mailing list