Supports all streams that the coded bitstream infrastructure does
(currently H.264, H.265 and MPEG-2).
---
 doc/bitstream_filters.texi     |  14 ++++
 libavcodec/Makefile            |   2 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/trace_headers_bsf.c | 167 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 184 insertions(+)
 create mode 100644 libavcodec/trace_headers_bsf.c

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 64f91f4b5..2e9106005 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -95,6 +95,20 @@ This bitstream filter passes the packets through unchanged.
 
 @section remove_extradata
 
+@section trace_headers
+
+Generate trace output containing all syntax elements in the coded
+stream headers (everything above the level of individual coded blocks).
+
+Supports H.264 and MPEG-2.
+
+The trace output is written to the normal logging facility by default.
+
+@table @option
+@item output_file
+Write trace output to this file instead.
+@end table
+
 @section vp9_superframe
 
 Combine VP9 frames into superframes.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index a1bb4b595..34b673ffe 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -777,6 +777,8 @@ OBJS-$(CONFIG_NOISE_BSF)                  += noise_bsf.o
 OBJS-$(CONFIG_NULL_BSF)                   += null_bsf.o
 OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF)       += remove_extradata_bsf.o
 OBJS-$(CONFIG_TEXT2MOVSUB_BSF)            += movsub_bsf.o
+OBJS-$(CONFIG_TRACE_HEADERS_BSF)          += trace_headers_bsf.o \
+                                             cbs.o cbs_h2645.o cbs_mpeg2.o
 OBJS-$(CONFIG_VP9_RAW_REORDER_BSF)        += vp9_raw_reorder_bsf.o
 OBJS-$(CONFIG_VP9_SUPERFRAME_BSF)         += vp9_superframe_bsf.o
 OBJS-$(CONFIG_VP9_SUPERFRAME_SPLIT_BSF)   += vp9_superframe_split_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 79ce40f9e..2e423acaf 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -38,6 +38,7 @@ extern const AVBitStreamFilter ff_null_bsf;
 extern const AVBitStreamFilter ff_text2movsub_bsf;
 extern const AVBitStreamFilter ff_noise_bsf;
 extern const AVBitStreamFilter ff_remove_extradata_bsf;
+extern const AVBitStreamFilter ff_trace_headers_bsf;
 extern const AVBitStreamFilter ff_vp9_raw_reorder_bsf;
 extern const AVBitStreamFilter ff_vp9_superframe_bsf;
 extern const AVBitStreamFilter ff_vp9_superframe_split_bsf;
diff --git a/libavcodec/trace_headers_bsf.c b/libavcodec/trace_headers_bsf.c
new file mode 100644
index 000000000..88bd7adf0
--- /dev/null
+++ b/libavcodec/trace_headers_bsf.c
@@ -0,0 +1,167 @@
+/*
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "libavutil/common.h"
+#include "libavutil/log.h"
+#include "libavutil/opt.h"
+
+#include "bsf.h"
+#include "cbs.h"
+#include "cbs_h264.h"
+#include "cbs_h265.h"
+#include "cbs_mpeg2.h"
+
+typedef struct TraceHeadersContext {
+    CodedBitstreamContext cbc;
+
+    const char *output_file_name;
+    FILE *output_file;
+} TraceHeadersContext;
+
+
+static void trace_headers_callback(void *context, int new_block, const char 
*format, ...)
+{
+    AVBSFContext     *bsf = context;
+    TraceHeadersContext *ctx = bsf->priv_data;
+    va_list vl;
+    va_start(vl, format);
+
+    if (ctx->output_file) {
+        if (new_block)
+            fprintf(ctx->output_file, "\n");
+
+        vfprintf(ctx->output_file, format, vl);
+
+        if (new_block)
+            fprintf(ctx->output_file, "\n");
+    } else {
+        av_vlog(bsf, AV_LOG_INFO, format, vl);
+    }
+
+    va_end(vl);
+}
+
+static int trace_headers_init(AVBSFContext *bsf)
+{
+    TraceHeadersContext *ctx = bsf->priv_data;
+    int err;
+
+    err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
+    if (err < 0)
+        return err;
+
+    ctx->cbc.trace_enable = 1;
+
+    if (ctx->output_file_name) {
+        ctx->output_file = fopen(ctx->output_file_name, "w");
+        if (!ctx->output_file) {
+            err = errno;
+            av_log(ctx, AV_LOG_ERROR, "Failed to open "
+                   "output file %s: %m.", ctx->output_file_name);
+            return AVERROR(err);
+        }
+    }
+    ctx->cbc.trace_callback = &trace_headers_callback;
+    ctx->cbc.trace_callback_context = bsf;
+
+    if (bsf->par_in->extradata) {
+        CodedBitstreamFragment ps;
+
+        trace_headers_callback(bsf, 0, "\nExtradata\n");
+
+        err = ff_cbs_read_extradata(&ctx->cbc, &ps, bsf->par_in);
+        if (err < 0) {
+            av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
+            return err;
+        }
+
+        ff_cbs_fragment_uninit(&ctx->cbc, &ps);
+    }
+
+    return 0;
+}
+
+static void trace_headers_close(AVBSFContext *bsf)
+{
+    TraceHeadersContext *ctx = bsf->priv_data;
+
+    ff_cbs_close(&ctx->cbc);
+
+    if (ctx->output_file)
+        fclose(ctx->output_file);
+}
+
+static int trace_headers(AVBSFContext *bsf, AVPacket *out)
+{
+    TraceHeadersContext *ctx = bsf->priv_data;
+    CodedBitstreamFragment au;
+    AVPacket *in;
+    int err;
+
+    err = ff_bsf_get_packet(bsf, &in);
+    if (err < 0)
+        return err;
+
+    trace_headers_callback(bsf, 0, "\nPacket (pts %"PRId64")\n",
+                        in->pts);
+
+    err = ff_cbs_read_packet(&ctx->cbc, &au, in);
+    if (err < 0)
+        return err;
+
+    ff_cbs_fragment_uninit(&ctx->cbc, &au);
+
+    av_packet_move_ref(out, in);
+    av_packet_free(&in);
+
+    return 0;
+}
+
+#define OFFSET(x) offsetof(TraceHeadersContext, x)
+static const AVOption trace_headers_options[] = {
+    { "output_file", "Write output to file rather than log",
+        OFFSET(output_file_name), AV_OPT_TYPE_STRING, { .str = NULL } },
+    { NULL }
+};
+
+static const AVClass trace_headers_class = {
+    .class_name = "trace_headers_bsf",
+    .item_name  = av_default_item_name,
+    .option     = trace_headers_options,
+    .version    = LIBAVCODEC_VERSION_MAJOR,
+};
+
+static const enum AVCodecID trace_headers_codec_ids[] = {
+    AV_CODEC_ID_MPEG2VIDEO,
+    AV_CODEC_ID_H264,
+    AV_CODEC_ID_HEVC,
+    AV_CODEC_ID_NONE,
+};
+
+const AVBitStreamFilter ff_trace_headers_bsf = {
+    .name           = "trace_headers",
+    .priv_data_size = sizeof(TraceHeadersContext),
+    .priv_class     = &trace_headers_class,
+    .init           = &trace_headers_init,
+    .close          = &trace_headers_close,
+    .filter         = &trace_headers,
+    .codec_ids      = trace_headers_codec_ids,
+};
-- 
2.11.0

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to