ffmpeg
错误描述:
[h264 @ 003e4df0] non-existing PPS 0 referenced
[h264 @ 003e4df0] decode_slice_header error
[h264 @ 003e4df0] non-existing PPS 0 referenced
[h264 @ 003e4df0] decode_slice_header error
说明:上面的错误原因是PPS在解码的时候没有被设置
解决方法:
AVCodecContext *pCodecCtx; //解码环境
pCodecCtx = avcodec_alloc_context();//分配解码环境
pCodecCtx->extradata = new uint8_t[32];//给extradata成员参数分配内存
pCodecCtx->extradata_size = 32;//extradata成员参数分配内存大小
//给extradata成员参数设置值
//00 00 00 01
pCodecCtx->extradata[0] = 0x00;
pCodecCtx->extradata[1] = 0x00;
pCodecCtx->extradata[2] = 0x00;
pCodecCtx->extradata[3] = 0x01;
//67 42 80 1e
pCodecCtx->extradata[4] = 0x67;
pCodecCtx->extradata[5] = 0x42;
pCodecCtx->extradata[6] = 0x80;
pCodecCtx->extradata[7] = 0x1e;
//88 8b 40 50
pCodecCtx->extradata[8] = 0x88;
pCodecCtx->extradata[9] = 0x8b;
pCodecCtx->extradata[10] = 0x40;
pCodecCtx->extradata[11] = 0x50;
//1e d0 80 00
pCodecCtx->extradata[12] = 0x1e;
pCodecCtx->extradata[13] = 0xd0;
pCodecCtx->extradata[14] = 0x80;
pCodecCtx->extradata[15] = 0x00;
//03 84 00 00
pCodecCtx->extradata[16] = 0x03;
pCodecCtx->extradata[17] = 0x84;
pCodecCtx->extradata[18] = 0x00;
pCodecCtx->extradata[19] = 0x00;
//af c8 02 00
pCodecCtx->extradata[20] = 0xaf;
pCodecCtx->extradata[21] = 0xc8;
pCodecCtx->extradata[22] = 0x02;
pCodecCtx->extradata[23] = 0x00;
//00 00 00 01
pCodecCtx->extradata[24] = 0x00;
pCodecCtx->extradata[25] = 0x00;
pCodecCtx->extradata[26] = 0x00;
pCodecCtx->extradata[27] = 0x01;
//68 ce 38 80
pCodecCtx->extradata[28] = 0x68;
pCodecCtx->extradata[29] = 0xce;
pCodecCtx->extradata[30] = 0x38;
pCodecCtx->extradata[31] = 0x80;
Decode()
{
FILE * inpf;
int nWrite;
int i,p;
int nalLen;
unsigned char* Buf;
int got_picture, consumed_bytes;
unsigned char *DisplayBuf;
DisplayBuf=(unsigned char *)malloc(60000);
char outfile[] = "test.pgm";
//1.打开输入javascript:;" onClick="javascript:tagshow(event, '文件');" target="_self">文件
inpf = fopen("test.264", "rb");
//outf = fopen("out.yuv", "wb");
if(!inpf)
{
goto Decodereturn;
}
nalLen = 0;
Buf = (unsigned char*)calloc ( 1000000, sizeof(char)); //准备解码文件缓冲
//2.注册解码器,并且找到H264解码器
avcodec_init();
avcodec_register_all();
codec = avcodec_find_decoder(CODEC_ID_H264);
if (!codec) {
return 0;
}
//allocate codec context
//分配解码器内存
c = avcodec_alloc_context();
if(!c){
return 0;
}
//open codec
//3.打开解码器
if (avcodec_open(c, codec) < 0) {
return 0;
}
//allocate frame buffer
//分配解码器用的帧缓冲
picture = avcodec_alloc_frame();
if(!picture){
return 0;
}
rgbdatanew = (unsigned char *)malloc(sizeof(unsigned char)*(3 * width * height));
while(!feof(inpf))
{
//4.获取下一个NAL的长度,并且将NAL放入Buf
nalLen = getNextNal(inpf, Buf);
//5.对改NAL解码,解码的YUV数据存在picture中
consumed_bytes= avcodec_decode_video(c, picture, &got_picture, Buf, nalLen);
if(consumed_bytes > 0)
{
//6.将picture中的YUV数据显示或者保存到文件
p=0;
for(i=0; i<c->height; i++)
{
memcpy(DisplayBuf+p,picture->data[0] + i * picture->linesize[0], c->width);
p+=c->width;
}
for(i=0; i<c->height/2; i++)
{
memcpy(DisplayBuf+p,picture->data[1] + i * picture->linesize[1], c->width/2);
p+=c->width/2;
}
for(i=0; i<c->height/2; i++)
{
memcpy(DisplayBuf+p,picture->data[2] + i * picture->linesize[2], c->width/2);
p+=c->width/2;
}
//显示画面
DisplayVideo(DisplayBuf);
}
}
//7.关闭输入文件
if(inpf)
fclose(inpf);
Decodereturn:
//8.关闭解码器,释放解码器内存
if(c) {
avcodec_close(c);
av_free(c);
c = NULL;
}
//9.释放解码画面内存
if(picture) {
av_free(picture);
picture = NULL;
}
//10.释放解码文件缓冲
if(Buf)
{
free(Buf);
Buf = NULL;
}
free(DisplayBuf);
}