lavc: Add trace_headers bitstream filter
authorMark Thompson <sw@jkqxz.net>
Thu, 4 May 2017 22:06:20 +0000 (23:06 +0100)
committerMark Thompson <sw@jkqxz.net>
Sat, 12 Aug 2017 21:17:20 +0000 (22:17 +0100)
Supports all streams that the coded bitstream infrastructure does
(currently H.264 and H.265).

configure
doc/bitstream_filters.texi
libavcodec/Makefile
libavcodec/bitstream_filters.c
libavcodec/trace_headers_bsf.c [new file with mode: 0644]

index 535862f..13dd7f5 100755 (executable)
--- a/configure
+++ b/configure
@@ -2324,6 +2324,7 @@ vc1_parser_select="vc1dsp"
 # bitstream_filters
 aac_adtstoasc_bsf_select="adts_header"
 mjpeg2jpeg_bsf_select="jpegtables"
+trace_headers_bsf_select="cbs_h264 cbs_h265"
 
 # external libraries
 avisynth_deps="LoadLibrary"
index 64f91f4..119a226 100644 (file)
@@ -95,6 +95,14 @@ This bitstream filter passes the packets through unchanged.
 
 @section remove_extradata
 
+@section trace_headers
+
+Log trace output containing all syntax elements in the coded stream
+headers (everything above the level of individual coded blocks).
+This can be useful for debugging low-level stream issues.
+
+Supports H.264 and H.265.
+
 @section vp9_superframe
 
 Combine VP9 frames into superframes.
index 9791fb3..09772a8 100644 (file)
@@ -786,6 +786,7 @@ 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
 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
index 79ce40f..2e423ac 100644 (file)
@@ -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 (file)
index 0000000..2d1fe63
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * 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 <stdio.h>
+
+#include "libavutil/avstring.h"
+#include "libavutil/common.h"
+#include "libavutil/log.h"
+
+#include "bsf.h"
+#include "cbs.h"
+
+
+typedef struct TraceHeadersContext {
+    CodedBitstreamContext cbc;
+} TraceHeadersContext;
+
+
+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;
+    ctx->cbc.trace_level  = AV_LOG_INFO;
+
+    if (bsf->par_in->extradata) {
+        CodedBitstreamFragment ps;
+
+        av_log(bsf, AV_LOG_INFO, "Extradata\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);
+}
+
+static int trace_headers(AVBSFContext *bsf, AVPacket *out)
+{
+    TraceHeadersContext *ctx = bsf->priv_data;
+    CodedBitstreamFragment au;
+    AVPacket *in;
+    char tmp[256] = { 0 };
+    int err;
+
+    err = ff_bsf_get_packet(bsf, &in);
+    if (err < 0)
+        return err;
+
+    if (in->flags & AV_PKT_FLAG_KEY)
+        av_strlcat(tmp, ", key frame", sizeof(tmp));
+    if (in->flags & AV_PKT_FLAG_CORRUPT)
+        av_strlcat(tmp, ", corrupt", sizeof(tmp));
+
+    if (in->pts != AV_NOPTS_VALUE)
+        av_strlcatf(tmp, sizeof(tmp), ", pts %"PRId64, in->pts);
+    else
+        av_strlcat(tmp, ", no pts", sizeof(tmp));
+    if (in->dts != AV_NOPTS_VALUE)
+        av_strlcatf(tmp, sizeof(tmp), ", dts %"PRId64, in->dts);
+    else
+        av_strlcat(tmp, ", no dts", sizeof(tmp));
+    if (in->duration > 0)
+        av_strlcatf(tmp, sizeof(tmp), ", duration %"PRId64, in->duration);
+
+    av_log(bsf, AV_LOG_INFO, "Packet: %d bytes%s.\n", in->size, tmp);
+
+    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;
+}
+
+static const enum AVCodecID trace_headers_codec_ids[] = {
+    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),
+    .init           = &trace_headers_init,
+    .close          = &trace_headers_close,
+    .filter         = &trace_headers,
+    .codec_ids      = trace_headers_codec_ids,
+};