[FFmpeg-devel] [PATCH] avcodec/avpacket: add av_packet_remove_side_data()

James Almer jamrial at gmail.com
Wed Sep 9 01:58:55 EEST 2020


On 9/8/2020 7:42 PM, Andreas Rheinhardt wrote:
> James Almer:
>> This helper removes a side data entry from the packet, maintaining the
>> integrity and order of the remaining entries, if any.
>>
>> Signed-off-by: James Almer <jamrial at gmail.com>
>> ---
>> Missing APIChanges entry and version bump.
>>
>> Couldn't find a place in the tree where it could be used right now, but
>> it makes the API be more in line with the AVFrame one, so i decided to
>> write it.
>>
>>  libavcodec/avpacket.c | 24 ++++++++++++++++++++++++
>>  libavcodec/packet.h   |  8 ++++++++
>>  2 files changed, 32 insertions(+)
>>
>> diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
>> index 4801163227..61ea81698c 100644
>> --- a/libavcodec/avpacket.c
>> +++ b/libavcodec/avpacket.c
>> @@ -367,6 +367,30 @@ uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType
>>      return NULL;
>>  }
>>  
>> +void av_packet_remove_side_data(AVPacket *pkt, enum AVPacketSideDataType type)
>> +{
>> +    int i, elems;
>> +
>> +    for (i = 0; i < pkt->side_data_elems; i++) {
>> +        if (pkt->side_data[i].type != type)
>> +            continue;
>> +
>> +        elems = pkt->side_data_elems - 1;
>> +        av_freep(&pkt->side_data[i].data);
>> +        pkt->side_data[i].size = 0;
>> +
>> +        if (i < elems) {
>> +            memmove(&pkt->side_data[i], &pkt->side_data[i + 1],
>> +                    (elems - i) * sizeof(*pkt->side_data));
>> +        }
>> +        if (!elems)
>> +            av_freep(&pkt->side_data);
>> +        pkt->side_data_elems = elems;
>> +
>> +        break;
>> +    }
>> +}
>> +
>>  const char *av_packet_side_data_name(enum AVPacketSideDataType type)
>>  {
>>      switch(type) {
>> diff --git a/libavcodec/packet.h b/libavcodec/packet.h
>> index 0a19a0eff3..6ce3c91c07 100644
>> --- a/libavcodec/packet.h
>> +++ b/libavcodec/packet.h
>> @@ -595,6 +595,14 @@ int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
>>  uint8_t* av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
>>                                   int *size);
>>  
>> +/**
>> + * Remove and free a side data entry of the given type.
>> + *
>> + * @param pkt packet
>> + * @param type side data type to be removed
>> + */
>> +void av_packet_remove_side_data(AVPacket *pkt, enum AVPacketSideDataType type);
> 
> How about extending the functionality to optionally pass the side data
> to the caller instead of freeing it? All it needs are two pointer
> parameters for data and size. Then this function could be used to
> simplify
> https://ffmpeg.org/pipermail/ffmpeg-devel/2020-September/269319.html

I thought about it, but doing so would be tailoring the function for
this specific scenario, turning it into a combination of get and remove
rather than a simple and API consistent (with AVFrame) fire-and-forget
call to remove a side data type if present in the packet.

That one case above is also discarding the packet altogether immediately
after stealing its side data, so it will not even gain anything from the
extra measures to rearrange the resulting array post removal.

> 
> - Andreas
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".
> 



More information about the ffmpeg-devel mailing list