#include "decoder.hpp"
|
|
#include "../ffmpeg/bridge/cvbridge.hpp"
|
#include "../ffmpeg/format/FormatIn.hpp"
|
#include "../ffmpeg/data/CodedData.hpp"
|
#include "../ffmpeg/log/log.hpp"
|
#include "../common.hpp"
|
|
extern "C"{
|
#include <libavformat/avformat.h>
|
#include <libavutil/opt.h>
|
#include <libswscale/swscale.h>
|
#include <libavcodec/avcodec.h>
|
}
|
|
using namespace ffwrapper;
|
using namespace logif;
|
|
namespace cffmpeg_wrap
|
{
|
decoder::decoder(ffwrapper::FormatIn *dec)
|
:decRef_(dec)
|
{}
|
|
decoder::~decoder(){
|
|
std::lock_guard<std::mutex> l(mutex_frm_);
|
for(auto i : list_frm_){
|
if (i.avframe)
|
av_frame_free(&i.avframe);
|
}
|
list_frm_.clear();
|
}
|
|
int decoder::initDecoder(){
|
if (!decRef_) return -1;
|
|
if(decRef_->getCodecContext() == NULL){
|
|
bool flag = true;
|
flag = decRef_->openCodec(NULL);
|
|
if (!flag){
|
logIt("FormatIn openCodec Failed!");
|
return -1;
|
}
|
}
|
return 0;
|
}
|
|
int decoder::SetFrame(const CPacket &pkt){
|
auto data = pkt.data;
|
|
if (!data) return -10;
|
if (!decRef_->isVideoPkt(&data->getAVPacket())) return -20;
|
|
if (decRef_->getCodecContext() == NULL){
|
if (initDecoder() != 0) return -30;
|
}
|
|
AVFrame *frame = av_frame_alloc();
|
AVPacket np(data->getAVPacket());
|
av_copy_packet(&np, &data->getAVPacket());
|
auto ret = decRef_->decode(frame, &np);
|
av_packet_unref(&np);
|
|
if (ret == 0){
|
return saveFrame(frame, pkt.v_id);
|
}
|
|
return ret;
|
}
|
|
void decoder::GetFrame(unsigned char **data, int *w, int *h, int *format, int *length, int64_t *id){
|
FRM frm;
|
{
|
std::lock_guard<std::mutex> l(mutex_frm_);
|
if(list_frm_.empty()){
|
return;
|
}
|
frm = list_frm_.front();
|
list_frm_.pop_front();
|
}
|
|
AVFrame *frame = frm.avframe;
|
int pix_fmt = frame->format;
|
uint8_t *origin = cvbridge::extractFrame(frame, &frm.length);
|
av_frame_free(&frame);
|
if (!origin) return;
|
|
uint8_t *finale = NULL;
|
if (pix_fmt != AV_PIX_FMT_NV12){
|
finale = (uint8_t*)malloc(frm.length);
|
|
unsigned char* SrcU = origin + frm.width * frm.height;
|
unsigned char* SrcV = SrcU + frm.width * frm.height / 4 ;
|
unsigned char* DstU = finale + frm.width * frm.height;
|
memcpy(finale, origin, frm.width * frm.height);
|
int i = 0;
|
for( i = 0 ; i < frm.width * frm.height / 4 ; i++ ){
|
*(DstU++) = *(SrcU++);
|
*(DstU++) = *(SrcV++);
|
}
|
free(origin);
|
}else{
|
finale = origin;
|
}
|
|
*data = finale;
|
*id = frm.id;
|
*w = frm.width;
|
*h = frm.height;
|
*format = pix_fmt;
|
*length = frm.length;
|
}
|
////////////////////////////////////////////////////////////////////////
|
static const int maxSize = 5;
|
int decoder::saveFrame(AVFrame *frame, const int64_t &id){
|
FRM frm;
|
frm.avframe = frame;
|
frm.width = frame->width;
|
frm.height = frame->height;
|
frm.id = id;
|
|
std::lock_guard<std::mutex> l(mutex_frm_);
|
while(list_frm_.size() > maxSize){
|
for(int i = 0; i < (maxSize>1); i++){
|
auto t = list_frm_.front();
|
av_frame_free(&t.avframe);
|
list_frm_.pop_front();
|
}
|
}
|
|
list_frm_.push_back(frm);
|
return list_frm_.size();
|
}
|
|
} // namespace cffmpeg_wrap
|