<div dir="ltr"><div class="gmail-gE gmail-iv gmail-gt" style="padding:20px 0px 0px;font-size:0.875rem;font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif">stuck once more with FFMPEG using RTSP <br><br>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</div><div class="gmail-gE gmail-iv gmail-gt" style="padding:20px 0px 0px;font-size:0.875rem;font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif">any help would be appreciated! thank you </div><div class="gmail-gE gmail-iv gmail-gt" style="padding:20px 0px 0px;font-size:0.875rem;font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif"><br></div><div id="gmail-:v6" style="font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:medium"><div class="gmail-qQVYZb"></div><div class="gmail-utdU2e"></div><div class="gmail-lQs8Hd"></div><div class="gmail-btm"></div></div><div class="gmail-" style="font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:medium"><div class="gmail-aHl" style=""></div><div id="gmail-:vj" tabindex="-1"></div><div id="gmail-:v8" class="gmail-ii gmail-gt" style="font-size:0.875rem;direction:ltr;margin:8px 0px 0px;padding:0px"><div id="gmail-:v7" class="gmail-a3s gmail-aXjCH" style="overflow:hidden;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:small;line-height:1.5;font-family:Arial,Helvetica,sans-serif"><div dir="ltr">#include <iostream><br><br>extern "C"<br>{<br>#include <libavformat/avformat.h><br>#include <libavcodec/avcodec.h><br>#include <libavcodec/codec.h><br>#include "libswscale/swscale.h"<br>#include "libavutil/avutil.h"<br>#include "libavutil/frame.h"<br>#include "libavcodec/codec_id.h"<br>#include "libavutil/pixfmt.h"<br>#include "libavutil/imgutils.h"<br>}<br><br><br>int decode_interrupt(void*);<br><br>int main()<br>{<br>    AVFrame* frame = NULL;<br>    AVFormatContext* formatc = NULL;<br>    const AVIOInterruptCB int_cb = { decode_interrupt, NULL };<br>    AVCodec* codec = NULL;<br>    AVCodecContext* codecc = NULL;<br>    AVPacket* packet = NULL;<br>    char* error = new char[255];<br>    uint8_t startCode4[] = { 0x00, 0x00, 0x00, 0x01 };<br>    int got_frame = 0;<br><br>    if (!(frame = av_frame_alloc())) {<br>        std::cout << " failed to alloc frame";<br>        return -1;<br>    }<br>    formatc = avformat_alloc_context();<br>    formatc->interrupt_callback = int_cb;<br><br>    codec = avcodec_find_decoder(AV_CODEC_ID_H264);<br>    codecc = avcodec_alloc_context3(codec);<br>    int result = avcodec_open2(codecc, codec, NULL);<br>    if (result != 0) {<br>        std::cout << "failed to open codec";<br>        return -1;<br>    }<br><br>    result = avformat_open_input(&formatc, "rtsp://<a href="http://127.0.0.1:8554/stream" target="_blank">127.0.0.1:8554/stream</a>", NULL, NULL);<br>    if (result != 0) {<br>        av_strerror(result, error, 255);<br>        std::cout << "couldnt open input " << error<< std::endl;<br>        avformat_close_input(&formatc);<br>        return -1;<br>    }<br><br>    result = avformat_find_stream_info(formatc, NULL);<br>    if (result != 0) {<br>        std::cout << "could not find stream information\n";<br>        return -1;<br>    }<br><br>    packet = av_packet_alloc();<br>    if (packet == NULL) {<br>        std::cout << "packet is null" << std::endl;<br>        return -1;<br>    }<br><br>    packet->data = NULL;<br>    packet->size = 0;<br><br>    if (!avcodec_is_open(codecc) || !av_codec_is_decoder(codecc->codec)) {<br>        std::cout << "error is here" << std::endl;<br>    }<br><br>    if (packet && !packet->size && packet->data) {<br>        std::cout << "error is packet" << std::endl;<br>    }<br><br>    while (av_read_frame(formatc,packet) >= 0)<br>    {<br>        //Decode the video frame of size avpkt->size from avpkt->data into picture.<br>        int res = avcodec_send_packet(codecc, packet);<br>        if (res < 0)<br>        {<br>            av_strerror(res, error, 255);<br>            std::cout << "failed to send packet " << error << std::endl;<br>            return -1;<br>        }<br><br><br>        while (res >= 0)<br>        {<br><br><br>            res = avcodec_receive_frame(codecc, frame);<br>            if (res == AVERROR(EAGAIN) || res == AVERROR_EOF)<br>            {<br>                break;<br>            }<br>            else if (res < 0) {<br>                exit(1);<br>            }<br><br>            //Allocate and return an SwsContext.<br>            struct SwsContext * scale_ctx = sws_getContext(codecc->width,<br>                codecc->height,<br>                codecc->pix_fmt,<br>                codecc->width,<br>                codecc->height,<br>                AV_PIX_FMT_BGR24,<br>                SWS_BICUBIC,<br>                NULL,<br>                NULL,<br>                NULL);<br><br>            //Calculate the size in bytes that a picture of the given width and height would occupy if stored in the given picture format.<br>            int numBytes = av_image_get_buffer_size(AV_PIX_FMT_BGR24,<br>                codecc->width,<br>                codecc->height,<br>                32/* used for pratical reasons*/);<br><br>            uint8_t * fbufffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));<br><br>            //Fill our frame buffer with the rgb image<br>            av_image_fill_arrays(frame->data,<br>                frame->linesize,<br>                fbufffer,<br>                AV_PIX_FMT_BGR24,<br>                codecc->width,<br>                codecc->height, 32);<br><br>            //Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst.<br>            sws_scale(scale_ctx,<br>                frame->data,<br>                frame->linesize,<br>                0,<br>                codecc->height,<br>                frame->data,<br>                frame->linesize);<br><br>            frame->linesize[0] = codecc->width * 3;<br>            sws_freeContext(scale_ctx);<br>            //we got our frame now its time to move on<br>            break;<br>        }<br>    }<br><br>    return 0;<br>}<br><br>int decode_interrupt(void * ctx) {<br>    std::cout << "interupted\n";<br>    return 0;<br>}</div></div></div></div></div>