AVPacket中的用户自定义私人数据

时间:2018-08-15 09:49:34

标签: ffmpeg

有没有一种方法可以在解码之前将一些用户私有数据附加到AVPacket上,以使其能够匹配输入AVPacket和解码输出AVFrame?某种AVFrame::opaque

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

1 个答案:

答案 0 :(得分:0)

由于@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);