1、ffmpeg自动添加, 在avcodec_open2之前添加一行代码就行了:
cpp
codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
2、手动添加, 在avcodec_open2之前添加如下代码, 0x00000001或者0x000001是起始码,0x67是sps的开头,0x68是pps的开头。
cpp
unsigned char sps_pps[23] = { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x0a, 0xf8, 0x0f, 0x00, 0x44, 0xbe, 0x8,
0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x38, 0x80 };
codec_ctx->extradata_size = 23;
codec_ctx->extradata = (uint8_t*)av_malloc(23 + AV_INPUT_BUFFER_PADDING_SIZE);
if (codec_ctx->extradata == NULL) {
printf("could not av_malloc the video params extradata!\n");
return -1;
}
memcpy(codec_ctx->extradata, sps_pps, 23);