Hello,

I need a little help.

Currently I'm doing a research project in which I use ffmpeg.

For that project I need to extract motion vectors so I looked on the web
for solution and found the use of AVMotionVector (like in file
frame.cpp) but with that method I get a segfault so I looked for the
reason and found that avcodec_decode_video2 is deprecated. I looked for
a replacement solution but I didn't find any, so here is my question :

How to replace avcodec_decode_video2 in the version ffmpeg-3.2.4 ?

Best regards,
DELMAS P.

P.S : You can find attached two c++ class which are one for frame and
another one for video


#include "alloc/lib_alloc.h"
#include "computing/lib_convert.h"
#include "frame.hpp"

Frame::Frame(AVFrame &pict,AVCodecContext &cont)
{
    context = &cont;
    frame = &pict;

    motion = new vector<AVMotionVector>;
}

AVCodecContext *Frame::getContext() const
{
    return context;
}

void Frame::setContext(AVCodecContext *value)
{
    context = value;
}

AVFrame *Frame::getFrame() const
{
    return frame;
}

void Frame::setFrame(AVFrame *value)
{
    frame = value;
}

vector<AVMotionVector> *Frame::getMotion() const
{
    return motion;
}

void Frame::setMotion(vector<AVMotionVector> *value)
{
    motion = value;
}

int Frame::extract()
{
    AVFrameSideData *sd = av_frame_get_side_data(frame,AV_FRAME_DATA_MOTION_VECTORS);

    if (sd)
    {
            const AVMotionVector *mvs = (const AVMotionVector*)sd->data;
            for (unsigned int i = 0; i < sd->size / sizeof(*mvs); i++)
            {
                const AVMotionVector *mv = &mvs[i];
                const int direction = mv->source > 0;
                motion->push_back(*mv);
            }
    }

    return 0;
}

/*int Frame::modifyOne(int motionVectorValue,int valueSup)
{
    //int* working = convertIntToBinary(motionVectorValue,8);

    return -1;
}*/






#ifndef FRAME_HPP
#define FRAME_HPP

#include <string>
#include <vector>

#ifdef __cplusplus
extern "C"
{
#endif

#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/motion_vector.h>


#ifdef __cplusplus
}
#endif

using namespace std;

class Frame
{
public:
    Frame(AVFrame &pict, AVCodecContext &context);

    AVCodecContext *getContext() const;
    void setContext(AVCodecContext *value);
    AVFrame *getFrame() const;
    void setFrame(AVFrame *value);
    vector<AVMotionVector> *getMotion() const;
    void setMotion(vector<AVMotionVector> *value);

    int extract();
    //int modifyOne(int motionVectorValue, int valueSup);

private:
    AVFrame* frame;
    AVCodecContext* context;
    vector<AVMotionVector> *motion;
};

#endif // FRAME_HPP
#include <vector>
#include "framesVector.hpp"
#include "video.hpp"


#define FF_I_TYPE AV_PICTURE_TYPE_I ///< Intra

Video::Video(string n)
{
    formatContext = NULL;
    codec = NULL;
    error=0;

    av_register_all();
    name = n;
    contextTest = gettingContext();
    mvInfoTest = gettingInfo();

    if(contextTest!=0||mvInfoTest!=0)
    {
        error = 1;
    }
}

string Video::getName() const
{
    return name;
}

void Video::setName(const string &value)
{
    name = value;
}

int Video::getNbi() const
{
    return nbi;
}

int Video::getNbj() const
{
    return nbj;
}

int Video::getGOP() const
{
    return gop;
}

int Video::getNb() const
{
    return nb;
}

int Video::getMvInfoTest() const
{
    return mvInfoTest;
}

int Video::getContextTest() const
{
    return contextTest;
}


AVCodecContext *Video::getContext() const
{
    return context;
}

int Video::gettingContext()
{
    int ret = -1;

    if(avformat_open_input(&formatContext, name.c_str(), NULL, NULL)==0)
    {
        if (avformat_find_stream_info(formatContext, NULL)>=0)
        {
            for(unsigned int i=0; i<formatContext->nb_streams; i++)
            {
                if(formatContext->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO)
                {
                    videoStream = i;
                }
            }
            if(videoStream!=-1)
            {
                context = formatContext->streams[videoStream]->codec;
                ret = 0;
            }
        }
    }

    return ret;
}

int Video::gettingInfo()
{
    int ret=-1;

    int NB=0;
    int NBI=0;
    int NBJ=0;

    if(contextTest==0)
    {
        NBI = (int) (context->height)/16;
        NBJ = (int) (context->width)/16;

        bool test1 = (16*((int) (context->height)/16)!=(context->height));
        bool test2 = (16*((int) (context->width)/16)!=(context->width));

        if(test1==false&&test2==false)
        {
            int N = context->gop_size;
            int M = context->max_b_frames;

            codec = avcodec_find_decoder(context->codec_id);

            if(codec!=NULL)
            {
                if(avcodec_open2(context, codec, NULL)>=0)
                {
                    nbi = NBI;
                    nbj = NBJ;
                    gop = N + N*M/(M+1) - 1;
                    nb = NB;

                    ret = 0;
                }
            }
        }
    }

    return ret;
}

vector<AVFrame> Video::readAllFrames()
{
    vector<AVFrame> *all = new vector<AVFrame>;
    AVPacket packet;

    int i = 0;
    int f=1;
    int frameFinished=0;
    int NB=0;

    //AVFrame* frame;

    if(error==0)
    {
        while(av_read_frame(formatContext, &packet)>=0)
        {
            if(packet.stream_index==videoStream)
            {
                AVFrame *frame = av_frame_alloc();

                avcodec_decode_video2(context, frame, &frameFinished, &packet);

                if(frameFinished)
                {
                    if (frame->pict_type != FF_I_TYPE)
                    {
                        all->push_back(*frame);

                        NB = NB +1;
                    }

                    ++f;
                }
            }


            i = i+1;
        }
    }

    return *all;
}

void Video::writeVideo()
{

}
#ifndef VIDEO_H
#define VIDEO_H

#include <string>
#include <vector>

#ifdef __cplusplus
extern "C"
{
#endif

#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>

#ifdef __cplusplus
}
#endif

using namespace std;

class Video
{
public:
    Video(string n);
    int getNbi() const;
    int getNbj() const;
    int getNb() const;
    int getGOP() const;
    int getMvInfoTest() const;
    int getContextTest() const;
    string getName() const;
    void setName(const string &value);
    AVCodecContext *getContext() const;
    int gettingContext();
    int gettingInfo();
    vector<AVFrame> readAllFrames();
    void writeVideo();

private:
    int nbi;
    int nbj;
    int nb;
    int gop;
    int contextTest;
    int mvInfoTest;
    int videoStream;
    int error;
    AVCodecContext *context;
    AVFormatContext *formatContext;
    AVCodec *codec;
    string name;
};

#endif // VIDEO_H

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
ffmpeg-user mailing list
ffmpeg-user@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
ffmpeg-user-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to