#ifndef _TS_DECODER_H_ #define _TS_DECODER_H_ #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 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(); cv::Mat src(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); telemetryData aTelem; 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); } } 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; }; #endif