根据Need to convert h264 stream from annex-b format to AVCC format :
AnnexB format:
([start code] NALU) | ( [start code] NALU) |
AVCC format:
([extradata]) | ([length] NALU) | ([length] NALU) |
目前,您可以使用 av_parser_parse2 解析比特流(AnnexB 格式)然后将此缓冲区传递给 avcodec_send_frame
,但是 AVCC 格式呢?有解析器吗?
最佳答案
是的,这是可能的。
您需要传递 avcC
的内容框至AVCodecContext::extradata解码器将对其进行解析,以获得解码所需的 PPS 和 SPS NALU。
这是一个如何做到这一点的例子:
// `avcC` box retrieved from an MP4
char avcC[] = "...";
// Create codec parameters and copy the avcC box as extradata
codec_params = avcodec_parameters_alloc();
memcpy(codec_params.extradata, &avcC, sizeof(avcC));
codec_params.extradata_size = sizeof(avcC);
// Create the codec context and initialize it with codec parameters
codec_context = avcodec_alloc_context3();
avcodec_parameters_to_context(codec_context, codec_params);
// Create the codec and initialize it
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
avcodec_open2(codec_context, codec, NULL);
AVCC 格式假定 PPS 和 SPS NALU 是“带外”提供的,这意味着它们不像附件 B 格式那样是流的一部分。 This excellent SO answer提供有关差异的更多详细信息。
在 MP4 容器中,这些 NALU 位于
avcC
框,位于 MP4 框层次结构中的以下路径:moov > trak > mdia > minf > stbl > stsd > avc1 > avcC
关于ffmpeg 可以解析/解码 AVCC 格式的数据包吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68877677/