| | |
| | | #include "PL_H264Decoder.h"
|
| | | #include "MaterialBuffer.h"
|
| | | #include "logger.h"
|
| | |
|
| | | #include <H264VideoRTPSource.hh> // for SPropRecord
|
| | | #include <libbase64.h>
|
| | |
| | |
|
| | | struct H264Decoder_Internal
|
| | | {
|
| | | uint8_t buffer[1920*1080*3];
|
| | | size_t buffSize;
|
| | | size_t buffSizeMax;
|
| | | //uint8_t buffer[1920*1080*3];
|
| | | //size_t buffSize;
|
| | | //size_t buffSizeMax;
|
| | | bool fmtp_set_to_context;
|
| | | bool payError;
|
| | |
|
| | | AVCodecContext* pAVCodecContext;
|
| | | AVFrame* pAVFrame;
|
| | | AVFrame* pAVFrame;//#todo delete
|
| | | MB_Frame lastFrame;
|
| | |
|
| | | H264Decoder_Internal() :
|
| | | buffSize(0), buffSizeMax(sizeof(buffer)), fmtp_set_to_context(false), |
| | | payError(false), |
| | | pAVCodecContext(nullptr), pAVFrame(nullptr)
|
| | | //buffSize(0), buffSizeMax(sizeof(buffer)), |
| | | fmtp_set_to_context(false), |
| | | payError(true), |
| | | pAVCodecContext(nullptr), pAVFrame(nullptr), |
| | | lastFrame()
|
| | | {
|
| | | }
|
| | |
|
| | |
| | |
|
| | | void reset()
|
| | | {
|
| | | buffSize = 0;
|
| | | //buffSize = 0;
|
| | | fmtp_set_to_context = false;
|
| | | payError = false;
|
| | | payError = true;
|
| | | |
| | | MB_Frame _lastFrame;
|
| | | lastFrame = _lastFrame;
|
| | | }
|
| | | };
|
| | |
|
| | |
| | |
|
| | | if (!avCodec)
|
| | | {
|
| | | printf("codec not found!\n"); |
| | | LOG(WARN) << "codec not found!"; |
| | | return false;
|
| | | }
|
| | |
|
| | |
| | | in->pAVCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
|
| | | in->pAVCodecContext->bit_rate = 0;
|
| | | in->pAVCodecContext->time_base.den = 25;
|
| | | in->pAVCodecContext->width = 1920;
|
| | | in->pAVCodecContext->width = 1920;//#todo get from pm
|
| | | in->pAVCodecContext->height = 1080;
|
| | |
|
| | | if (in->pAVCodecContext->extradata == NULL)
|
| | |
| | | return true;
|
| | | }
|
| | |
|
| | | bool decodeH264(H264Decoder_Internal* in, uint8_t* buffer, size_t buffSize) |
| | | bool decodeH264(H264Decoder_Internal* in, uint8_t* buffer, size_t buffSize, timeval pts) |
| | | {
|
| | | AVPacket packet = {0};
|
| | | int frameFinished = buffSize;
|
| | | int gotPicture = buffSize; // frameFinished
|
| | |
|
| | | if (av_packet_from_data(&packet, buffer, buffSize) != 0)
|
| | | {
|
| | | printf("av_packet_from_data error\n");
|
| | | LOG(WARN) << "av_packet_from_data error";
|
| | | return false;
|
| | | } |
| | | }
|
| | | |
| | | packet.pts = packet.dts = (pts.tv_sec * 1000 * 1000 + pts.tv_usec) / 90000;
|
| | |
|
| | | // decode
|
| | | avcodec_decode_video2(in->pAVCodecContext, in->pAVFrame, &frameFinished, &packet);
|
| | | if(frameFinished)
|
| | | avcodec_decode_video2(in->pAVCodecContext, in->pAVFrame, &gotPicture, &packet);
|
| | | if(gotPicture)
|
| | | {
|
| | | // decode ok
|
| | |
|
| | | int picSize = in->pAVCodecContext->height * in->pAVCodecContext->width;
|
| | | in->buffSize = picSize * 1.5;
|
| | |
|
| | | int height = in->pAVFrame->height;
|
| | | int width = in->pAVFrame->width;
|
| | |
|
| | | // write yuv420
|
| | | int a=0;
|
| | | for (int i = 0; i < height; i++)
|
| | | {
|
| | | memcpy(in->buffer + a, in->pAVFrame->data[0] + i * in->pAVFrame->linesize[0], width);
|
| | | a += width;
|
| | | }
|
| | | for (int i=0; i<height/2; i++)
|
| | | {
|
| | | memcpy(in->buffer + a, in->pAVFrame->data[1] + i * in->pAVFrame->linesize[1], width / 2);
|
| | | a += width / 2;
|
| | | }
|
| | | for (int i=0; i<height/2; i++)
|
| | | {
|
| | | memcpy(in->buffer + a, in->pAVFrame->data[2] + i * in->pAVFrame->linesize[2], width / 2);
|
| | | a += width / 2;
|
| | | }
|
| | |
|
| | | //in->buffer readly
|
| | |
|
| | | //static size_t f=0;
|
| | | //char fname[50];
|
| | | //sprintf(fname, "%u.yuv420", ++f);
|
| | | //FILE * pFile = fopen (fname,"wb");
|
| | | //fwrite (in->buffer , sizeof(char), in->buffSize, pFile);
|
| | | //fclose(pFile);
|
| | | return true;
|
| | | }
|
| | | else
|
| | | printf("incomplete frame\n");
|
| | | {
|
| | | LOG(WARN) << "incomplete frame";
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | bool PL_H264Decoder::pay(const PipeMaterial& pm)
|
| | | {
|
| | | H264Decoder_Internal* in = (H264Decoder_Internal*)internal;
|
| | | |
| | | in->payError = true;
|
| | |
|
| | | if (!in->fmtp_set_to_context)
|
| | | {
|
| | |
| | |
|
| | | bool ret = initH264DecoderEnv(in, sps.sPropBytes, sps.sPropLength, pps.sPropBytes, pps.sPropLength);
|
| | | if (!ret)
|
| | | {
|
| | | LOG(ERROR) << "PL_H264Decoder::pay initH264DecoderEnv error";
|
| | | return false; // #todo log
|
| | | }
|
| | | else
|
| | | in->fmtp_set_to_context = true;
|
| | | }
|
| | |
|
| | | in->payError = decodeH264(in, pm.buffer, pm.buffSize);
|
| | | return in->payError;
|
| | | if (pm.buffer == nullptr)
|
| | | return false;
|
| | | |
| | | bool ret = false;
|
| | | if (pm.type == PipeMaterial::PMT_BYTES)
|
| | | {
|
| | | if (pm.buffSize <= 0)
|
| | | return false;
|
| | |
|
| | | timeval pts = {0};
|
| | | ret = decodeH264(in, (uint8_t*)pm.buffer, pm.buffSize, pts);
|
| | | }
|
| | | else if (pm.type == PipeMaterial::PMT_FRAME)
|
| | | {
|
| | | MB_Frame* frame = (MB_Frame*)pm.buffer;
|
| | | |
| | | if (frame->buffSize <= 0)
|
| | | return false;
|
| | | |
| | | ret = decodeH264(in, (uint8_t*)frame->buffer, frame->buffSize, frame->pts);
|
| | | if (ret)
|
| | | {
|
| | | in->lastFrame.type = MB_Frame::MBFT_PTR_AVFRAME;
|
| | | in->lastFrame.buffer = (uint8_t*)(in->pAVFrame);
|
| | | in->lastFrame.buffSize = sizeof(in->pAVFrame);
|
| | | in->lastFrame.width = in->pAVFrame->width;
|
| | | in->lastFrame.height = in->pAVFrame->height;
|
| | | //in->lastFrame.pts = frame->pts;//#todo
|
| | | gettimeofday(&(in->lastFrame.pts),NULL);
|
| | | }
|
| | | }
|
| | | |
| | | in->payError = !ret;
|
| | | return ret;
|
| | | }
|
| | |
|
| | | bool PL_H264Decoder::gain(PipeMaterial& pm)
|
| | |
| | |
|
| | | if (!in->payError)
|
| | | {
|
| | | pm.buffer = in->buffer;
|
| | | pm.buffSize = in->buffSize;
|
| | | pm.type = PipeMaterial::PMT_FRAME;
|
| | | pm.buffer = &(in->lastFrame);
|
| | | pm.buffSize = 0;
|
| | | }
|
| | | pm.former = this;
|
| | | return in->payError;
|
| | | return !in->payError;
|
| | | }
|