首页 > 解决方案 > AVPacket 中的用户自定义私有数据

问题描述

有没有办法AVPacket在解码之前附加一些用户私有数据以匹配输入AVPacket和解码输出AVFrame?某种AVFrame::opaque

具体来说,在存在 B 帧的情况下,h264 码流的解码过程可以重新排序,我想确定哪个AVPacket被解码为哪个AVFrame

标签: ffmpeg

解决方案


感谢@Gyan,我能够在主解码循环中使用以下代码解决问题。

static uint64_t privateId = 0;

// Allocate dictionary and add appropriate key/value record
AVDictionary * frameDict = NULL;
av_dict_set(&frameDict, "private_id", std::to_string(privateId++).c_str(), 0);

// Pack dictionary to be able to use it as a side data in AVPacket
int frameDictSize = 0;
uint8_t *frameDictData = av_packet_pack_dictionary(frameDict, &frameDictSize);

// Free dictionary not used any more
av_dict_free(&frameDict);

// Add side_data to AVPacket which will be decoded
av_packet_add_side_data(&avPacket, AVPacketSideDataType::AV_PKT_DATA_STRINGS_METADATA, frameDictData, frameDictSize);

// Do the actual decoding
...

// Free side data from packet
av_packet_free_side_data(&avPacket);

// Obtain privateId from decoded frame
uint64_t privateId = std::stoul(av_dict_get(avFrame->metadata, "private_id", NULL, 0)->value);

// Free dictionary from decoded frame
av_dict_free(&avFrame->metadata);

推荐阅读