我在本地网络上有一些摄像头ip,我用live555库接收视频流(我以testRtspClient为基础),用ffmpeg (avcodec_decode_video2)解码帧。一切都很完美。当我试图从互联网上解码一个流时,问题就开始了。
第一个问题--一些数据包丢失了,因此出现了缺陷。但这不是问题。问题-在停止并启动视频流之后,需要等待大约5分钟的流,然后ffmpeg才能从相同的ip摄像机中解码一些内容。如果包没有丢失,那么一切都好。
第二个问题是摄像头,它发送分辨率为2048х1538的视频。这种解析的帧由几个分组发送。live555通常将它们结合在一起,但是当帧被传送到解码器时,解码器会返回数据包长度,但是得到的帧总是0。
以下是我的一些代码:
#define RECEIVE_BUFFER_SIZE 1000000
AVCodecContext* avCodecContext; //definition
AVFrame *frame; //definition
...
//init code
_fReceiveBuffer = new uint8_t[RECEIVE_BUFFER_SIZE+512]; //buffer to receive frame
ZeroMemory(_fReceiveBuffer, RECEIVE_BUFFER_SIZE + 512); //zeros
_bufferSize = RECEIVE_BUFFER_SIZE * sizeof(uint8_t); //buffer size
static const uint8_t startCode[4] = { 0x00, 0x00, 0x00, 0x01 }; //this is for 0 0 0 1
//before frame will transfer to decoder
memcpy(_fReceiveBuffer, (void*)startCode, sizeof(uint8_t)* 4);
_fReceiveBuffer += sizeof(sizeof(uint8_t)* 4);
_bufferSize -= sizeof(sizeof(uint8_t)* 4);
AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264); //find codec
avCodecContext = avcodec_alloc_context3(codec);
avCodecContext->flags |= AV_PKT_FLAG_KEY;
avcodec_open2(avCodecContext, codec, NULL);
frame = av_frame_alloc();
//frame
void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
struct timeval presentationTime, unsigned durationInMicroseconds) {
if (strcmp(fSubsession.codecName(), "H264") == 0)
{
//code from onvif device manager
static const uint8_t startCode3[] = { 0x00, 0x00, 0x01 };
static const uint8_t startCode4[] = { 0x00, 0x00, 0x00, 0x01 };
auto correctedFrameSize = frameSize;
auto correctedBufferPtr = fPlObj->_fReceiveBuffer;
if (frameSize < sizeof(startCode4) || memcmp(startCode4, correctedBufferPtr, sizeof(startCode4)) != 0){
if (frameSize < sizeof(startCode3) || memcmp(startCode3, correctedBufferPtr, sizeof(startCode3)) != 0){
correctedFrameSize += sizeof(uint8_t)* 4;
correctedBufferPtr -= sizeof(uint8_t)* 4;
}
}
ProcessFrame(correctedBufferPtr, correctedFrameSize, presentationTime, durationInMicroseconds);
}
continuePlaying();
}
void DummySink::ProcessFrame(unsigned char* framePtr, int frameSize, struct timeval presentationTime, unsigned duration) {
AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.data = framePtr;
avpkt.size = frameSize;
while (avpkt.size > 0) {
int got_frame = 0;
int len = avcodec_decode_video2(avCodecContext, frame, &got_frame, &avpkt);
if (len < 0) {
//TODO: log error
return;
}
else if (got_frame == 0)
{
//I tried this code, bacause "codecs which have the AV_CODEC_CAP_DELAY capability set have a delay between input and output"
//but it didn't help
/*AVPacket emptyPacket;
av_init_packet(&emptyPacket);
emptyPacket.data = NULL;
emptyPacket.size = 0;
emptyPacket.stream_index = avpkt.stream_index;
len = avcodec_decode_video2(avCodecContext, frame, &got_frame, &emptyPacket);
if ( got_frame == 1) goto next;*/
return;
}
next:
//... here code for view with DirectDraw - everithing ok with it
avpkt.size -= len;
avpkt.data += len;
}
}我试图用sps和pps信息将帧发送到解码器:
0 0 0 1 sps 0 0 0 1 pps 0 0 0 1 frame但这是没有帮助的。
有趣的是,avcodec_decode_video2不返回带有第二个问题的帧(返回所有帧大小),但是avCodecContext中的宽度和高度是正确设置的。我无法理解为什么它不返回帧。
有人能帮忙解决这些问题吗?
发布于 2016-01-12 07:23:27
我通过使用tcp上的rtp而不是udp上的rtp来解决这些问题。
https://stackoverflow.com/questions/34619418
复制相似问题