---
 doc/bitstream_filters.texi     |  13 ++++
 libavcodec/Makefile            |   2 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/h264_trace_bsf.c    | 161 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 177 insertions(+)
 create mode 100644 libavcodec/h264_trace_bsf.c

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 64f91f4b5..e1c268251 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -41,6 +41,19 @@ bitstream after extraction.
 
 @section h264_mp4toannexb
 
+@section h264_trace
+
+Generate trace output containing all syntax elements in the H.264
+stream headers (everything above the level of @emph{slice_data()}).
+Includes SPS, PPS, AUD, slice headers, and some SEI.
+
+This is written to the normal logging facility by default.
+
+@table @option
+@item output_file
+Write trace output to this file instead.
+@end table
+
 @section imx_dump_header
 
 @section mjpeg2jpeg
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index e83513384..df0a563c3 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -766,6 +766,8 @@ OBJS-$(CONFIG_DUMP_EXTRADATA_BSF)         += 
dump_extradata_bsf.o
 OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF)      += extract_extradata_bsf.o    \
                                              h2645_parse.o
 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)       += h264_mp4toannexb_bsf.o
+OBJS-$(CONFIG_H264_TRACE_BSF)             += h264_trace_bsf.o \
+                                             h264_raw.o
 OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF)       += hevc_mp4toannexb_bsf.o
 OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF)        += imx_dump_header_bsf.o
 OBJS-$(CONFIG_MJPEG2JPEG_BSF)             += mjpeg2jpeg_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 79ce40f9e..f5c842a28 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -29,6 +29,7 @@ extern const AVBitStreamFilter ff_chomp_bsf;
 extern const AVBitStreamFilter ff_dump_extradata_bsf;
 extern const AVBitStreamFilter ff_extract_extradata_bsf;
 extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf;
+extern const AVBitStreamFilter ff_h264_trace_bsf;
 extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_imx_dump_header_bsf;
 extern const AVBitStreamFilter ff_mjpeg2jpeg_bsf;
diff --git a/libavcodec/h264_trace_bsf.c b/libavcodec/h264_trace_bsf.c
new file mode 100644
index 000000000..563261f1f
--- /dev/null
+++ b/libavcodec/h264_trace_bsf.c
@@ -0,0 +1,161 @@
+/*
+ * 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 "h264_raw.h"
+
+typedef struct H264TraceContext {
+    H264RawContext raw;
+
+    const char *output_file_name;
+    FILE *output_file;
+} H264TraceContext;
+
+
+static void h264_trace_callback(void *context, int new_block, const char 
*format, ...)
+{
+    AVBSFContext     *bsf = context;
+    H264TraceContext *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 h264_trace_init(AVBSFContext *bsf)
+{
+    H264TraceContext *ctx = bsf->priv_data;
+    int err;
+
+    err = ff_h264_raw_init(&ctx->raw, bsf);
+    if (err < 0)
+        return err;
+
+    ctx->raw.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->raw.trace_callback = &h264_trace_callback;
+    ctx->raw.trace_callback_context = bsf;
+
+    if (bsf->par_in->extradata) {
+        H264RawAccessUnit ps;
+
+        h264_trace_callback(bsf, 0, "\nExtradata\n");
+
+        err = ff_h264_raw_read_extradata(&ctx->raw, &ps, bsf->par_in);
+        if (err < 0) {
+            av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
+            return err;
+        }
+
+        ff_h264_raw_access_unit_uninit(&ps);
+    }
+
+    return 0;
+}
+
+static void h264_trace_close(AVBSFContext *bsf)
+{
+    H264TraceContext *ctx = bsf->priv_data;
+
+    ff_h264_raw_uninit(&ctx->raw);
+
+    if (ctx->output_file)
+        fclose(ctx->output_file);
+}
+
+static int h264_trace(AVBSFContext *bsf, AVPacket *out)
+{
+    H264TraceContext *ctx = bsf->priv_data;
+    H264RawAccessUnit au;
+    AVPacket *in;
+    int err;
+
+    err = ff_bsf_get_packet(bsf, &in);
+    if (err < 0)
+        return err;
+
+    h264_trace_callback(bsf, 0, "\nPacket (pts %"PRId64")\n",
+                        in->pts);
+
+    err = ff_h264_raw_read_packet(&ctx->raw, &au, in);
+    if (err < 0)
+        return err;
+
+    ff_h264_raw_access_unit_uninit(&au);
+
+    av_packet_move_ref(out, in);
+    av_packet_free(&in);
+
+    return 0;
+}
+
+#define OFFSET(x) offsetof(H264TraceContext, x)
+static const AVOption h264_trace_options[] = {
+    { "output_file", "Write output to file rather than log",
+        OFFSET(output_file_name), AV_OPT_TYPE_STRING, { .str = NULL } },
+    { NULL }
+};
+
+static const AVClass h264_trace_class = {
+    .class_name = "h264_trace_bsf",
+    .item_name  = av_default_item_name,
+    .option     = h264_trace_options,
+    .version    = LIBAVCODEC_VERSION_MAJOR,
+};
+
+static const enum AVCodecID h264_trace_codec_ids[] = {
+    AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
+};
+
+const AVBitStreamFilter ff_h264_trace_bsf = {
+    .name           = "h264_trace",
+    .priv_data_size = sizeof(H264TraceContext),
+    .priv_class     = &h264_trace_class,
+    .init           = &h264_trace_init,
+    .close          = &h264_trace_close,
+    .filter         = &h264_trace,
+    .codec_ids      = h264_trace_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