#ifndef _TS_DECODER_H_ #define _TS_DECODER_H_ #include #include #include #include #include #include #include "opencv2/opencv.hpp" extern "C" { #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libavdevice/avdevice.h" #include "libswscale/swscale.h" #include "libavutil/imgutils.h" } #include "commondefine.h" #include "TsPacker.hpp" #include #include "API_UnderStitch.h" class TsDecoder { public: void decoder(){ std::cout<<"decode loop"<data[0], pFrame->linesize[0]); // file.write((char *)pFrame->data[1], pFrame->linesize[1]); // } // file.close(); src = cv::Mat(1024, 1280, CV_8UC1, (char*)pFrame->data[0]); // cv::imshow("", src); // cv::waitKey(1); qDebug() << "get image"; } } // if packet is video pkt //_packer->demuxer(aPacket.buf->data, aPacket.buf->size, aPacket.pts, TsPacker::VIDEO); printf(""); index ++; } else if (aPacket.stream_index == telemStreamID) { // QFile file(QString("./img_raw_%1.yuv").arg(raw_index)); // if(file.open(QIODevice::WriteOnly)){ // file.write((char *)aPacket.buf->data, aPacket.buf->size); // } // file.close(); // if it is a telem pkt // _packer->demuxer(&aPacket, TsPacker::BINDATA); // _packer->demuxer(aPacket.buf->data, aPacket.buf->size, aPacket.pts, TsPacker::BINDATA); memset(&aTelem, 0, sizeof(telemetryData)); telemCodec::telemDecode(aPacket.buf->data, aPacket.buf->size, aTelem); char buff[1024] = {0}; int times = raw_index / 25; int minute = times / 60; int misc = times % 60; sprintf(buff, "[%02d:%02d:%02d:%04d] Lon: %.06f, %.06f, %.03f, Drone: yaw:%.06f, %.06f, %.06f, IMU: yaw:%.06f, %.06f, %.06f, Comp: yaw:%.06f, %.06f, %.06f, SF: %.06f, %.06f, Target: %.06f, %.06f, %.03f, Dis: %.03f, speed: %.06f, %.06f, %.06f\n", aTelem.mHour, aTelem.mMinute, aTelem.mSecond, aTelem.mMillSecond, aTelem.mDroneLongtitude, aTelem.mDroneLatitude, aTelem.mDroneAltitude, aTelem.mDroneYaw, aTelem.mDronePitch, aTelem.mDroneRoll, aTelem.mSensorYaw, aTelem.mSensorPitch, aTelem.mSensorRoll, aTelem.mComposedYaw, aTelem.mComposedPitch, aTelem.mComposedRoll, aTelem.mFrameYaw, aTelem.mFramePitch, aTelem.mMarkLongtitude, aTelem.mMarkLatitude, aTelem.mMarkAltitude, aTelem.mLaserDistance, aTelem.mDroneSpeedEast * 100 * 32767 / 1024, aTelem.mDroneSpeedNorth * 100 * 32767 / 1024, aTelem.mDroneSpeedSky * 100 * 32767 / 1024); // file->write(buff, strlen(buff)); qDebug("[%s] \n", buff); raw_index++; } else { av_packet_unref(&aPacket); } if(!src.empty()) { info.craft.stPos.B = aTelem.mDroneLatitude; info.craft.stPos.L = aTelem.mDroneLongtitude; info.craft.stPos.H = aTelem.mDroneAltitude; info.craft.stAtt.fYaw = aTelem.mComposedYaw; info.craft.stAtt.fPitch = aTelem.mComposedPitch; info.craft.stAtt.fRoll = aTelem.mComposedRoll; info.nEvHeight = aTelem.mDroneAltitude - 1250; info.servoInfo.fServoAz = 0; info.servoInfo.fServoPt = 0; info.camInfo.fPixelSize = 12; info.camInfo.nFocus = info.camInfo.fPixelSize/(aTelem.mSensorHFOV/1280.0/0.057); info.nWidth = src.cols; info.nHeight = src.rows; info.nFrmID++; if (!bUnderInit) { pUnderStitch = API_UnderStitch::Create("./cache"); pUnderStitch->SetOutput("S7215", "./output"); pUnderStitch->Init(info); bUnderInit = true; } else { UPanConfig config = {0}; config.bUseBA = 1; config.bOutGoogleTile = 1; config.bOutLocalPanGeoTIFF = 1; pUnderStitch->SetConfig(config); pUnderStitch->Run(src, info); cv::Mat pan = pUnderStitch->ExportPanMat(); int dsize = 8; cv::Mat pan_resized_RGBA, pan_resizedRGB; cv::resize(pan, pan_resized_RGBA, cv::Size(pan.cols / dsize, pan.rows / dsize)); cv::cvtColor(pan_resized_RGBA, pan_resizedRGB, cv::COLOR_BGRA2BGR); cv::imshow("pan", pan_resizedRGB); cv::waitKey(1); } // cv::imshow("",src); // cv::waitKey(1); // if (info.nFrmID % 100 == 0) // { // char filename[1024] = {0}; // sprintf(filename, "%d_%02d_%02d_%02d_%04d.tif", aTelem.mSensorID, aTelem.mHour, aTelem.mMinute, aTelem.mSecond, aTelem.mMillSecond); // exportFrameGeoTIFF(src, info, filename); // } } } else { std::cout<<"end of file"<nb_streams; j++) { qDebug("nb[%d] type :%d,%d", j, pFormatCtx->streams[j]->codecpar->codec_id, pFormatCtx->streams[j]->codecpar->codec_type); if (pFormatCtx->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_DATA && pFormatCtx->streams[j]->codecpar->codec_id == AV_CODEC_ID_BIN_DATA) { if (dataStreamID < pFormatCtx->streams[j]->id) { telemStreamID = j; dataStreamID = pFormatCtx->streams[j]->id; } } } // set stream if (videoStreamID > -1) pVideoStream = pFormatCtx->streams[videoStreamID]; if (telemStreamID > -1) pTelemStream = pFormatCtx->streams[telemStreamID]; printf("aPacket.stream_index --- %d:%d\n", videoStreamID, telemStreamID); // if video stream is validate, open codec and context if (pVideoStream) { pVideoCodec = avcodec_find_decoder(pVideoStream->codecpar->codec_id); pVideoCtx = pVideoCodec ? avcodec_alloc_context3(pVideoCodec) : nullptr; if (pVideoCtx) { avcodec_parameters_to_context(pVideoCtx, pVideoStream->codecpar); avcodec_open2(pVideoCtx, pVideoCodec, nullptr); } if (pVideoCtx->width > 0 && pVideoCtx->height > 0) { mImgHeight = pVideoCtx->height; mImgWidth = pVideoCtx->width; mBytesPerPixel = 3; } } pFrame = av_frame_alloc(); qDebug("codec id:%d, %d", videoStreamID, telemStreamID); //file = new QFile(namelist[0] + "_param.txt"); //file->open(QIODevice::WriteOnly); //std::thread d1(&TsDecoder::decoder, std::ref(*this)); //d1.detach(); } ~TsDecoder(){ if (pFormatCtx != nullptr) { avformat_free_context(pFormatCtx); pFormatCtx = nullptr; } if (pVideoCtx != nullptr) { avcodec_free_context(&pVideoCtx); pVideoCodec = nullptr; } // release avformat_network_deinit(); } private: // define ffmpeg format contex AVFormatContext *pFormatCtx = nullptr; AVStream *pVideoStream = nullptr; AVStream *pTelemStream = nullptr; AVCodecContext *pVideoCtx = nullptr; AVCodec *pVideoCodec = nullptr; AVFrame* pFrame = nullptr; int videoStreamID = 0; int telemStreamID = 0; float _change_angle = 0.0; TsPacker *_packer = nullptr; QFile *file = nullptr; API_UnderStitch* pUnderStitch = nullptr; bool bUnderInit = false; }; #endif