[FFmpeg-devel] [PATCH 19/21] doc/examples/extract_timed_metadata: added a bare-bones metadata extractor; find only the frames
erkki.seppala.ext at nokia.com
erkki.seppala.ext at nokia.com
Tue Aug 23 12:03:37 EEST 2016
From: Erkki Seppälä <erkki.seppala.ext at nokia.com>
Signed-off-by: Erkki Seppälä <erkki.seppala.ext at nokia.com>
Signed-off-by: OZOPlayer <OZOPL at nokia.com>
---
doc/examples/Makefile | 1 +
doc/examples/extract_timed_metadata.c | 233 ++++++++++++++++++++++++++++++++++
2 files changed, 234 insertions(+)
create mode 100644 doc/examples/extract_timed_metadata.c
diff --git a/doc/examples/Makefile b/doc/examples/Makefile
index af38159..c10033e 100644
--- a/doc/examples/Makefile
+++ b/doc/examples/Makefile
@@ -16,6 +16,7 @@ EXAMPLES= avio_dir_cmd \
decoding_encoding \
demuxing_decoding \
extract_mvs \
+ extract_timed_metadata \
filtering_video \
filtering_audio \
http_multiclient \
diff --git a/doc/examples/extract_timed_metadata.c b/doc/examples/extract_timed_metadata.c
new file mode 100644
index 0000000..ef84ed9
--- /dev/null
+++ b/doc/examples/extract_timed_metadata.c
@@ -0,0 +1,233 @@
+/*
+ * Copyright (c) 2010 Nicolas George
+ * Copyright (c) 2011 Stefano Sabatini
+ * Copyright (c) 2015 Erkki Seppälä <erkki.seppala.ext at nokia.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * @file
+ * API example for decoding and filtering
+ * @example filtering_video.c
+ */
+
+#define _XOPEN_SOURCE 600 /* for usleep */
+#include <unistd.h>
+#include <stdint.h>
+
+#include <libavcodec/avcodec.h>
+#include <libavformat/avformat.h>
+#include <libavutil/opt.h>
+#include <assert.h>
+#include <ctype.h>
+
+#define MAX_METADATA_STREAMS 10
+
+static AVFormatContext *fmt_ctx;
+static AVCodecContext *dec_ctx[MAX_METADATA_STREAMS];
+static int metadata_stream_indices[MAX_METADATA_STREAMS + 1] = { -1 }; /* terminated with -1 */
+
+static int open_input_file(const char *filename)
+{
+ int ret;
+ AVCodec *dec = avcodec_find_decoder(AV_CODEC_ID_META);
+ int i;
+ int nb_metadata = 0;
+
+ if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
+ return ret;
+ }
+
+ if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
+ return ret;
+ }
+
+ for (i = 0; i < fmt_ctx->nb_streams; i++) {
+ if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
+ assert(nb_metadata < MAX_METADATA_STREAMS);
+ metadata_stream_indices[nb_metadata] = i;
+ nb_metadata++;
+ }
+ }
+ metadata_stream_indices[nb_metadata] = -1;
+
+ if (nb_metadata == 0) {
+ av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
+ return ret;
+ }
+
+ for (i = 0; i < nb_metadata; i++) {
+ dec_ctx[i] = avcodec_alloc_context3(dec);
+ if ((ret = avcodec_open2(dec_ctx[i], dec, NULL)) < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Cannot open metadata decoder for metadata stream on track %d\n", i);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static void* memndup(void* ptr, int n)
+{
+ void* buffer = malloc(n);
+ memcpy(buffer, ptr, n);
+ return buffer;
+}
+
+static char* local_strndup(char* str, int n)
+{
+ int len = strlen(str);
+ if (len > n) {
+ len = n;
+ }
+ char* buffer = malloc(n + 1);
+ memcpy(buffer, str, n);
+ buffer[len] = 0;
+ return buffer;
+}
+
+int main(int argc, char **argv)
+{
+ int ret;
+ AVPacket packet;
+ AVData *metadata = avdata_alloc();
+ int got_frame;
+ int i;
+
+ if (!metadata) {
+ perror("Could not allocate metadata");
+ exit(1);
+ }
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s file\n", argv[0]);
+ exit(1);
+ }
+
+ av_register_all();
+
+ if ((ret = open_input_file(argv[1])) < 0)
+ goto end;
+
+ for (i = 0; metadata_stream_indices[i] >= 0; i++) {
+ char *uri = NULL;
+ int nb_src_idxs;
+ int *src_idxs = NULL;
+ char *tref_tag = NULL;
+ int size;
+ int trefsSize;
+ AVTimedMetadata* metadata =
+ (AVTimedMetadata*) av_stream_get_side_data(fmt_ctx->streams[metadata_stream_indices[i]],
+ AV_PKT_DATA_TIMED_METADATA_INFO,
+ &size);
+ AVTrackReferences* trefs =
+ (AVTrackReferences*) av_stream_get_side_data(fmt_ctx->streams[metadata_stream_indices[i]],
+ AV_PKT_DATA_TRACK_REFERENCES,
+ &trefsSize);
+ if (metadata) {
+ tref_tag = local_strndup(metadata->meta_tag, 4);
+ uri = local_strndup((char*) (metadata + 1), metadata->meta_length);
+ int j;
+ int conf_len = metadata->conf_length;
+ void* conf = memndup((char*) (metadata + 1) + metadata->meta_length, metadata->conf_length);
+ char* conf_tag = local_strndup(metadata->conf_tag, 4);
+ nb_src_idxs = trefs ? trefs->nb_tracks : 0;
+ src_idxs = (int*) (trefs + 1);
+ printf("Track #%d describing track%s with tref %s",
+ metadata_stream_indices[i] + 1,
+ nb_src_idxs > 1 ? "s" : "",
+ tref_tag);
+ for (j = 0; j < nb_src_idxs; j++)
+ printf(" #%d", src_idxs[j]);
+ printf(" url: %s \n", uri);
+ printf(" configuration conf tag %s, %d bytes:", conf_tag, conf_len);
+ for (j = 0; j < conf_len; ++j) {
+ unsigned char ch = ((unsigned char*) conf)[j];
+ if (isprint(ch)) {
+ printf(" %c", ch);
+ } else {
+ printf(" %2x", ((unsigned char*) conf)[j]);
+ }
+ }
+ printf("\n");
+ free(conf);
+ free(conf_tag);
+ free(uri);
+ free(tref_tag);
+ } else {
+ printf("No meta data info for track %d?!\n", metadata_stream_indices[i]);
+ }
+ }
+
+ /* read all packets */
+ while (1) {
+ if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
+ break;
+
+ for (i = 0; metadata_stream_indices[i] >= 0; i++) {
+ if (packet.stream_index == metadata_stream_indices[i]) {
+ got_frame = 0;
+
+ ret = dec_ctx[i]->codec->decode(dec_ctx[i], metadata, &got_frame, &packet);
+
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Error decoding meta data\n");
+ break;
+ }
+
+ if (got_frame) {
+ int c;
+ printf("track #%d at %" PRId64 " %d, ",
+ packet.stream_index + 1,
+ metadata->pts,
+ metadata->data->size);
+ for (c = 0; c < metadata->data->size; ++c) {
+ char ch = ((char*) metadata->data->data)[c];
+ if (ch == '\\') {
+ printf("\\\\");
+ } else if (isprint(ch)) {
+ putchar(ch);
+ } else {
+ printf("\\0x%2x", (unsigned char) ch);
+ }
+ }
+ putchar('\n');
+ } else {
+ printf("no frame..\n");
+ }
+ }
+ }
+ av_packet_unref(&packet);
+ }
+end:
+ for (i = 0; metadata_stream_indices[i] >= 0; i++) {
+ avcodec_close(dec_ctx[i]);
+ }
+ avformat_close_input(&fmt_ctx);
+ avdata_free(metadata);
+
+ if (ret < 0 && ret != AVERROR_EOF) {
+ fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
+ exit(1);
+ }
+
+ exit(0);
+}
--
2.7.4
More information about the ffmpeg-devel
mailing list