From 6fb2fd895e858ab93f46e656a322778ee181c307 Mon Sep 17 00:00:00 2001 From: Clément Bœsch Date: Tue, 16 Oct 2012 23:49:03 +0200 Subject: lavc: add lavfi metadata support. This commit introduces a new AVPacket side data type: AV_PKT_DATA_STRINGS_METADATA. Its main goal is to provide a way to transmit the metadata from the AVFilterBufferRef up to the AVFrame. This is at the moment "only" useful for lavfi input from libavdevice: lavd/lavfi only outputs packets, and the metadata from the buffer ref kept in its context needs to be transmitted from the packet to the frame by the decoders. The buffer ref can be destroyed at any time (along with the metadata), and a duplication of the AVPacket needs to duplicate the metadata as well, so the choice of using the side data to store them was selected. Making sure lavd/lavfi raises the metadata is useful to allow tools like ffprobe to access the filters metadata (it is at the moment the only way); ffprobe will now automatically show the AVFrame metadata in any customizable output format for users. API users will also be able to access the AVFrame->metadata pointer the same way ffprobe does (av_frame_get_metadata). All the changes are done in this single commit to avoid some memory leaks: for instances, the changes in lavfi/avcodec.c are meant to duplicate the metadata from the buffer ref into the AVFrame. Unless we have an internal way of freeing the AVFrame->metadata automatically, it will leak in most of the user apps. To fix this problem, we introduce AVCodecContext->metadata and link avctx->metadata to the current frame->metadata and free it at each decode frame call (and in the codec closing callback for the last one). But doing this also means to update the way the tiff decoder already handles the AVFrame->metadata (it's the only one decoder with frame metadata at the moment), by making sure it is not trying to free a pointer already freed by the lavc internals. The lavfi/avcodec.c buffer ref code is based on an old Thomas Kühnel work, the rest of the code belongs to the commit author. Signed-off-by: Thomas Kühnel Signed-off-by: Clément Bœsch --- libavdevice/lavfi.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'libavdevice/lavfi.c') diff --git a/libavdevice/lavfi.c b/libavdevice/lavfi.c index 944794fb87..82c5844fab 100644 --- a/libavdevice/lavfi.c +++ b/libavdevice/lavfi.c @@ -27,6 +27,7 @@ #include "float.h" /* DBL_MIN, DBL_MAX */ +#include "libavutil/bprint.h" #include "libavutil/log.h" #include "libavutil/mem.h" #include "libavutil/opt.h" @@ -339,6 +340,28 @@ static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt) memcpy(pkt->data, ref->data[0], size); } + if (ref->metadata) { + uint8_t *metadata; + AVDictionaryEntry *e = NULL; + AVBPrint meta_buf; + + av_bprint_init(&meta_buf, 0, AV_BPRINT_SIZE_UNLIMITED); + while ((e = av_dict_get(ref->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) { + av_bprintf(&meta_buf, "%s", e->key); + av_bprint_chars(&meta_buf, '\0', 1); + av_bprintf(&meta_buf, "%s", e->value); + av_bprint_chars(&meta_buf, '\0', 1); + } + if (!av_bprint_is_complete(&meta_buf) || + !(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, + meta_buf.len))) { + av_bprint_finalize(&meta_buf, NULL); + return AVERROR(ENOMEM); + } + memcpy(metadata, meta_buf.str, meta_buf.len); + av_bprint_finalize(&meta_buf, NULL); + } + pkt->stream_index = stream_idx; pkt->pts = ref->pts; pkt->pos = ref->pos; -- cgit v1.2.3