Signed-off-by: James Almer <jamr...@gmail.com> --- libavcodec/avcodec.c | 2 ++ libavcodec/avcodec.h | 8 +++++ libavcodec/avpacket.c | 84 +++++++++++++++++++++++++++++++++++++++++++ libavcodec/packet.h | 54 ++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+)
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index 340abe830e..16869e97f2 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -467,6 +467,8 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_freep(&avctx->internal); } + av_packet_free_side_data_set(&avctx->side_data_set); + for (i = 0; i < avctx->nb_coded_side_data; i++) av_freep(&avctx->coded_side_data[i].data); av_freep(&avctx->coded_side_data); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index fe41ecc3c9..2902ecf6cb 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -2102,6 +2102,14 @@ typedef struct AVCodecContext { * an error. */ int64_t frame_num; + + /** + * Additional data associated with the entire stream. + * + * - decoding: set by user + * - encoding: unused + */ + AVPacketSideDataSet side_data_set; } AVCodecContext; /** diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 5fef65e97a..f569731403 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -645,3 +645,87 @@ int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp) return 0; } + +AVPacketSideData *av_packet_get_side_data_from_set(const AVPacketSideDataSet *set, + enum AVPacketSideDataType type) +{ + for (int i = 0; i < set->nb_side_data; i++) + if (set->side_data[i].type == type) + return &set->side_data[i]; + + return NULL; +} + +static int add_side_data_to_set(AVPacketSideDataSet *set, + AVPacketSideData **psd, + enum AVPacketSideDataType type, + uint8_t *data, size_t size) +{ + AVPacketSideData *sd, *tmp; + + for (int i = 0; i < set->nb_side_data; i++) { + sd = &set->side_data[i]; + + if (sd->type == type) { + av_freep(&sd->data); + sd->data = data; + sd->size = size; + *psd = sd; + return 0; + } + } + + if (set->nb_side_data + 1U > FFMIN(INT_MAX, SIZE_MAX / sizeof(*tmp))) + return AVERROR(ERANGE); + + tmp = av_realloc_array(set->side_data, set->nb_side_data + 1, sizeof(*tmp)); + if (!tmp) + return AVERROR(ENOMEM); + + set->side_data = tmp; + set->nb_side_data++; + + sd = &set->side_data[set->nb_side_data - 1]; + sd->type = type; + sd->data = data; + sd->size = size; + *psd = sd; + + return 0; +} + +int av_packet_add_side_data_to_set(AVPacketSideDataSet *set, + enum AVPacketSideDataType type, + uint8_t *data, size_t size) +{ + AVPacketSideData *sd; + + return add_side_data_to_set(set, &sd, type, data, size); +} + +AVPacketSideData *av_packet_new_side_data_to_set(AVPacketSideDataSet *set, + enum AVPacketSideDataType type, + size_t size) +{ + AVPacketSideData *sd = NULL; + uint8_t *data = av_malloc(size); + int ret; + + if (!data) + return NULL; + + ret = add_side_data_to_set(set, &sd, type, data, size); + if (ret < 0) + av_freep(&data); + + return sd; +} + +void av_packet_free_side_data_set(AVPacketSideDataSet *set) +{ + int i; + for (i = 0; i < set->nb_side_data; i++) + av_freep(&set->side_data[i].data); + av_freep(&set->side_data); + set->nb_side_data = 0; +} diff --git a/libavcodec/packet.h b/libavcodec/packet.h index f28e7e7011..590c2bf15a 100644 --- a/libavcodec/packet.h +++ b/libavcodec/packet.h @@ -318,6 +318,14 @@ typedef struct AVPacketSideData { enum AVPacketSideDataType type; } AVPacketSideData; +/** + * Structure to hold a set of AVPacketSideDataSet + */ +typedef struct AVPacketSideDataSet { + AVPacketSideData *side_data; + int nb_side_data; +} AVPacketSideDataSet; + /** * This structure stores compressed data. It is typically exported by demuxers * and then passed as input to decoders, or received as output from encoders and @@ -724,6 +732,52 @@ int av_packet_make_writable(AVPacket *pkt); */ void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst); +/** + * Allocate a new side data entry into to a set. + * + * @param set a set to which the side data should be added + * @param type side data type + * @param size side data size + * @return pointer to freshly allocated side data entry or NULL otherwise. + */ +AVPacketSideData *av_packet_new_side_data_to_set(AVPacketSideDataSet *set, + enum AVPacketSideDataType type, + size_t size); + +/** + * Wrap an existing array as a packet side data into a set. + * + * @param set a set to which the side data should be added + * @param type side information type + * @param data the side data array. It must be allocated with the av_malloc() + * family of functions. The ownership of the data is transferred to + * pkt. + * @param size side information size + * @return a non-negative number on success, a negative AVERROR code on + * failure. On failure, the set is unchanged and the data remains + * owned by the caller. + */ +int av_packet_add_side_data_to_set(AVPacketSideDataSet *set, + enum AVPacketSideDataType type, + uint8_t *data, size_t size); + +/** + * Get side information from set. + * + * @param set a set from which the side data should be fetched + * @param type desired side information type + * @return pointer to side data if present or NULL otherwise + */ +AVPacketSideData *av_packet_get_side_data_from_set(const AVPacketSideDataSet *set, + enum AVPacketSideDataType type); + +/** + * Convenience function to free all the side data stored in a set. + * + * @param set a set + */ +void av_packet_free_side_data_set(AVPacketSideDataSet *set); + /** * @} */ -- 2.41.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".