[Libav-user] help

Carlos Esponda esponda.carlos9 at gmail.com
Wed Aug 5 03:38:34 EEST 2020


stuck once more with FFMPEG using RTSP

I am not sure why avcodec_send_packet is returning invalid data found while
processing, I went through the code and made sure my inputs are correct
any help would be appreciated! thank you

#include <iostream>

extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/codec.h>
#include "libswscale/swscale.h"
#include "libavutil/avutil.h"
#include "libavutil/frame.h"
#include "libavcodec/codec_id.h"
#include "libavutil/pixfmt.h"
#include "libavutil/imgutils.h"
}


int decode_interrupt(void*);

int main()
{
    AVFrame* frame = NULL;
    AVFormatContext* formatc = NULL;
    const AVIOInterruptCB int_cb = { decode_interrupt, NULL };
    AVCodec* codec = NULL;
    AVCodecContext* codecc = NULL;
    AVPacket* packet = NULL;
    char* error = new char[255];
    uint8_t startCode4[] = { 0x00, 0x00, 0x00, 0x01 };
    int got_frame = 0;

    if (!(frame = av_frame_alloc())) {
        std::cout << " failed to alloc frame";
        return -1;
    }
    formatc = avformat_alloc_context();
    formatc->interrupt_callback = int_cb;

    codec = avcodec_find_decoder(AV_CODEC_ID_H264);
    codecc = avcodec_alloc_context3(codec);
    int result = avcodec_open2(codecc, codec, NULL);
    if (result != 0) {
        std::cout << "failed to open codec";
        return -1;
    }

    result = avformat_open_input(&formatc, "rtsp://127.0.0.1:8554/stream",
NULL, NULL);
    if (result != 0) {
        av_strerror(result, error, 255);
        std::cout << "couldnt open input " << error<< std::endl;
        avformat_close_input(&formatc);
        return -1;
    }

    result = avformat_find_stream_info(formatc, NULL);
    if (result != 0) {
        std::cout << "could not find stream information\n";
        return -1;
    }

    packet = av_packet_alloc();
    if (packet == NULL) {
        std::cout << "packet is null" << std::endl;
        return -1;
    }

    packet->data = NULL;
    packet->size = 0;

    if (!avcodec_is_open(codecc) || !av_codec_is_decoder(codecc->codec)) {
        std::cout << "error is here" << std::endl;
    }

    if (packet && !packet->size && packet->data) {
        std::cout << "error is packet" << std::endl;
    }

    while (av_read_frame(formatc,packet) >= 0)
    {
        //Decode the video frame of size avpkt->size from avpkt->data into
picture.
        int res = avcodec_send_packet(codecc, packet);
        if (res < 0)
        {
            av_strerror(res, error, 255);
            std::cout << "failed to send packet " << error << std::endl;
            return -1;
        }


        while (res >= 0)
        {


            res = avcodec_receive_frame(codecc, frame);
            if (res == AVERROR(EAGAIN) || res == AVERROR_EOF)
            {
                break;
            }
            else if (res < 0) {
                exit(1);
            }

            //Allocate and return an SwsContext.
            struct SwsContext * scale_ctx = sws_getContext(codecc->width,
                codecc->height,
                codecc->pix_fmt,
                codecc->width,
                codecc->height,
                AV_PIX_FMT_BGR24,
                SWS_BICUBIC,
                NULL,
                NULL,
                NULL);

            //Calculate the size in bytes that a picture of the given width
and height would occupy if stored in the given picture format.
            int numBytes = av_image_get_buffer_size(AV_PIX_FMT_BGR24,
                codecc->width,
                codecc->height,
                32/* used for pratical reasons*/);

            uint8_t * fbufffer = (uint8_t *)av_malloc(numBytes *
sizeof(uint8_t));

            //Fill our frame buffer with the rgb image
            av_image_fill_arrays(frame->data,
                frame->linesize,
                fbufffer,
                AV_PIX_FMT_BGR24,
                codecc->width,
                codecc->height, 32);

            //Scale the image slice in srcSlice and put the resulting
scaled slice in the image in dst.
            sws_scale(scale_ctx,
                frame->data,
                frame->linesize,
                0,
                codecc->height,
                frame->data,
                frame->linesize);

            frame->linesize[0] = codecc->width * 3;
            sws_freeContext(scale_ctx);
            //we got our frame now its time to move on
            break;
        }
    }

    return 0;
}

int decode_interrupt(void * ctx) {
    std::cout << "interupted\n";
    return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200804/9de9c4b4/attachment.html>


More information about the Libav-user mailing list