aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/utils.c
diff options
context:
space:
mode:
authorClément Bœsch <ubitux@gmail.com>2012-10-16 23:49:03 +0200
committerClément Bœsch <ubitux@gmail.com>2012-10-21 17:29:10 +0200
commit6fb2fd895e858ab93f46e656a322778ee181c307 (patch)
treeff4ea436ee889f9d526f5c527fafea4110152535 /libavcodec/utils.c
parentd7b8a9a589d94550d2eb2ce4147e69311d6c5745 (diff)
downloadffmpeg-6fb2fd895e858ab93f46e656a322778ee181c307.tar.gz
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 <kuehnelth@googlemail.com> Signed-off-by: Clément Bœsch <ubitux@gmail.com>
Diffstat (limited to 'libavcodec/utils.c')
-rw-r--r--libavcodec/utils.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index e4b3ed71c6..06999f5425 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1592,6 +1592,31 @@ static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
}
}
+static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
+{
+ int size, ret = 0;
+ const uint8_t *side_metadata;
+ const uint8_t *end;
+
+ av_dict_free(&avctx->metadata);
+ side_metadata = av_packet_get_side_data(avctx->pkt,
+ AV_PKT_DATA_STRINGS_METADATA, &size);
+ if (!side_metadata)
+ goto end;
+ end = side_metadata + size;
+ while (side_metadata < end) {
+ const uint8_t *key = side_metadata;
+ const uint8_t *val = side_metadata + strlen(key) + 1;
+ int ret = av_dict_set(&frame->metadata, key, val, 0);
+ if (ret < 0)
+ break;
+ side_metadata = val + strlen(val) + 1;
+ }
+end:
+ avctx->metadata = frame->metadata;
+ return ret;
+}
+
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
int *got_picture_ptr,
const AVPacket *avpkt)
@@ -1630,6 +1655,7 @@ int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *pi
if (!picture->height) picture->height = avctx->height;
if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
}
+ add_metadata_from_side_data(avctx, picture);
emms_c(); //needed to avoid an emms_c() call before every return;
@@ -1749,6 +1775,7 @@ int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
if (!frame->sample_rate)
frame->sample_rate = avctx->sample_rate;
}
+ add_metadata_from_side_data(avctx, frame);
side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
if(side && side_size>=10) {
@@ -1901,6 +1928,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
avctx->internal->byte_buffer_size = 0;
av_freep(&avctx->internal->byte_buffer);
av_freep(&avctx->internal);
+ av_dict_free(&avctx->metadata);
}
if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)