[FFmpeg-devel] [PATCHv3 2/7] avpacket: add pack/unpack functions for AVDictionary
Michael Niedermayer
michaelni at gmx.at
Tue Oct 29 01:49:58 CET 2013
On Sun, Oct 27, 2013 at 10:47:30PM -0400, Ben Boeckel wrote:
> These functions are intended for use with side_data which comes in an
> AVPacket.
>
> Signed-off-by: Ben Boeckel <mathstuf at gmail.com>
> ---
> libavcodec/avcodec.h | 18 +++++++++++++++++
> libavcodec/avpacket.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 73 insertions(+)
>
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index ee2ba54..32ea394 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -3582,6 +3582,24 @@ int av_packet_merge_side_data(AVPacket *pkt);
>
> int av_packet_split_side_data(AVPacket *pkt);
>
> +/**
> + * Pack a dictionary for use in side_data.
> + *
> + * @param dict The dictionary to pack.
> + * @param size pointer to store the size of the returned data
> + * @return pointer to data if successful, NULL otherwise
> + */
> +uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size);
> +/**
> + * Unpack a dictionary from side_data.
> + *
> + * @param data data from side_data
> + * @param size size of the data
> + * @param dict the metadata storage dictionary
> + * @return 0 on success, < 0 on failure
> + */
> +int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict);
> +
>
> /**
> * Convenience function to free all the side data stored.
> diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
> index bee159d..d65c27c 100644
> --- a/libavcodec/avpacket.c
> +++ b/libavcodec/avpacket.c
> @@ -426,6 +426,61 @@ int av_packet_split_side_data(AVPacket *pkt){
> return 0;
> }
>
> +uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
> +{
> + AVDictionaryEntry *t = NULL;
> + uint8_t *data = NULL;
> + *size = 0;
> +
> + if (!dict)
> + return NULL;
> +
> + while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
> + const int keylen = strlen(t->key);
> + const int valuelen = strlen(t->value);
> + const size_t new_size = *size + keylen + 1 + valuelen + 1;
> + uint8_t *const new_data = av_realloc(data, new_size);
> +
> + if (!new_data)
> + goto fail;
> + data = new_data;
> +
> + memcpy(data + *size, t->key, keylen + 1);
> + memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
> +
> + *size = new_size;
> + }
> +
> + return data;
> +
> +fail:
> + av_freep(&data);
> + *size = 0;
> + return NULL;
> +}
> +
> +int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
> +{
> + const uint8_t *end = data + size;
> + int ret = 0;
> +
> + if (!dict || !data || !size)
> + return ret;
> +
please add a check that side data ends with 0
(its required as it should only contain zero terminated strings)
and would avoid a crash in case the padding isnt there for whatever
reason
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Why not whip the teacher when the pupil misbehaves? -- Diogenes of Sinope
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20131029/3058000e/attachment.asc>
More information about the ffmpeg-devel
mailing list