#include "H264DecoderThread.h"
#include "Poco/Thread.h"
#include<iostream>
#include<fstream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
using namespace std;
using Poco::Thread;
FFmpegErrorCode H264DecoderThread::connect()
{
AVDictionary *opts = 0;
av_dict_set(&opts, "rtsp_transport", "tcp", 0);
av_dict_set(&opts, "stimeout", "1000000", 0);
FFmpegErrorCode res = FFmpeg_NoError;
printf("H264DecoderThread connect()\n");
do
{
if (avformat_open_input(&m_pFormatCtx, m_strMrl.c_str(), NULL, &opts) != 0)
{
res = FFmpeg_ConnectFail;
break;
}
if (avformat_find_stream_info(m_pFormatCtx, NULL) < 0)
{
res = FFmpeg_FindStreamInfo;
break;
}
m_nVideoStreamIndex = av_find_best_stream(m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (m_nVideoStreamIndex < 0)
{
res = FFmpeg_FindBestStream;
break;
}
AVStream* st = m_pFormatCtx->streams[m_nVideoStreamIndex];
AVCodec *pCodec = avcodec_find_decoder(st->codec->codec_id);
if (!pCodec || avcodec_open2(st->codec, pCodec, NULL) < 0)
{
res = FFmpeg_FindDecoder;
break;
}
} while (false);
if (res != FFmpeg_NoError)
{
avformat_close_input(&m_pFormatCtx);
}
else
{
m_bConnected = true;
string names_file = "coco.names";
string cfg_file = "yolov3.cfg";
string weights_file = "yolov3.weights";
pDetector = new Detector(cfg_file, weights_file);
if (pDetector == NULL)
{
printf("error pDetector == NULL\n");
}
#if 0
m_dst_pix_fmt = AV_PIX_FMT_RGB24;
m_dst_w = 1280;
m_dst_h = 720;
m_dst_c = 3;
#else
m_dst_pix_fmt = AV_PIX_FMT_BGR24;
m_dst_w = 416;
m_dst_h = 416;
m_dst_c = 3;
#endif
}
av_dict_free(&opts);
return res;
}
void H264DecoderThread::AddTask(PicInfo &picInfo)
{
printf("H264DecoderThread AddTask()\n");
}
static image_t my_make_empty_image(int w, int h, int c)
{
image_t out;
out.data = 0;
out.h = h;
out.w = w;
out.c = c;
return out;
}
static image_t my_make_image_custom(int w, int h, int c)
{
image_t out = my_make_empty_image(w, h, c);
out.data = (float *)calloc(h*w*c, sizeof(float));
return out;
}
void H264DecoderThread::run()
{
printf("H264DecoderThread run()\n");
AVFrame *frame = av_mallocz(sizeof(*frame));
printf("H264DecoderThread run()sizeof(*frame) %d\n", sizeof(*frame));
printf("H264DecoderThread run()sizeof(AVFrame) %d\n", sizeof(AVFrame));
/*
H264DecoderThread run()sizeof(*frame) 488
H264DecoderThread run()sizeof(AVFrame) 488
*/
AVCodecContext* avctx = m_pFormatCtx->streams[m_nVideoStreamIndex]->codec;
//sws_scale将输入参数, 转成RGB24
/*
enum AVPixelFormat {
AV_PIX_FMT_NONE = -1,
AV_PIX_FMT_YUV420P, ///< planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
AV_PIX_FMT_YUYV422, ///< packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
AV_PIX_FMT_RGB24, ///< packed RGB 8:8:8, 24bpp, RGBRGB