shithub: dav1d

Download patch

ref: c98bbeb3cf7ec6bd2545fdb8704dae87fe7cc117
parent: 0749f4b0929bf9e4f0afa5bc96a12073b55a2408
author: James Almer <[email protected]>
date: Fri Jan 25 19:41:35 EST 2019

add a logging callback mechanism

--- a/include/common/attributes.h
+++ b/include/common/attributes.h
@@ -34,8 +34,10 @@
 
 #ifdef __GNUC__
 #define ATTR_ALIAS __attribute__((may_alias))
+#define ATTR_FORMAT_PRINTF(fmt, attr) __attribute__((__format__(__printf__, fmt, attr)));
 #else
 #define ATTR_ALIAS
+#define ATTR_FORMAT_PRINTF(fmt, attr)
 #endif
 
 #if ARCH_X86_64
--- a/include/dav1d/dav1d.h
+++ b/include/dav1d/dav1d.h
@@ -33,6 +33,7 @@
 #endif
 
 #include <errno.h>
+#include <stdarg.h>
 
 #include "common.h"
 #include "picture.h"
@@ -44,10 +45,23 @@
 #define DAV1D_MAX_FRAME_THREADS 256
 #define DAV1D_MAX_TILE_THREADS 64
 
+typedef struct Dav1dLogger {
+    void *cookie; ///< Custom data to pass to the callback.
+    /**
+     * Logger callback. Default prints to stderr. May be NULL to disable logging.
+     *
+     * @param cookie Custom pointer passed to all calls.
+     * @param format The vprintf compatible format string.
+     * @param     ap List of arguments referenced by the format string.
+     */
+    void (*callback)(void *cookie, const char *format, va_list ap);
+} Dav1dLogger;
+
 typedef struct Dav1dSettings {
     int n_frame_threads;
     int n_tile_threads;
     Dav1dPicAllocator allocator;
+    Dav1dLogger logger;
     int apply_grain;
     int operating_point; ///< select an operating point for scalable AV1 bitstreams (0 - 31)
     int all_layers; ///< output all spatial layers of a scalable AV1 biststream
--- a/meson.build
+++ b/meson.build
@@ -70,6 +70,10 @@
     error('asm causes false positive with memory sanitizer. Use \'-Dbuild_asm=false\'.')
 endif
 
+# Logging option
+if get_option('logging')
+    cdata.set('CONFIG_LOG', 1)
+endif
 
 #
 # OS/Compiler checks and defines
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -20,6 +20,11 @@
     value: true,
     description: 'Build dav1d tests')
 
+option('logging',
+    type: 'boolean',
+    value: true,
+    description: 'Print error log messages using the provided callback function')
+
 option('testdata_tests',
     type: 'boolean',
     value: false,
--- a/src/internal.h
+++ b/src/internal.h
@@ -123,6 +123,8 @@
     unsigned operating_point_idc;
     int all_layers;
     int drain;
+
+    Dav1dLogger logger;
 };
 
 struct Dav1dFrameContext {
--- a/src/lib.c
+++ b/src/lib.c
@@ -38,6 +38,7 @@
 #include "common/validate.h"
 
 #include "src/internal.h"
+#include "src/log.h"
 #include "src/obu.h"
 #include "src/qm.h"
 #include "src/ref.h"
@@ -62,6 +63,8 @@
     s->allocator.cookie = NULL;
     s->allocator.alloc_picture_callback = default_picture_allocator;
     s->allocator.release_picture_callback = default_picture_release;
+    s->logger.cookie = NULL;
+    s->logger.callback = dav1d_log_default_callback;
     s->operating_point = 0;
     s->all_layers = 1; // just until the tests are adjusted
 }
@@ -90,6 +93,7 @@
     memset(c, 0, sizeof(*c));
 
     c->allocator = s->allocator;
+    c->logger = s->logger;
     c->apply_grain = s->apply_grain;
     c->operating_point = s->operating_point;
     c->all_layers = s->all_layers;
--- /dev/null
+++ b/src/log.c
@@ -1,0 +1,57 @@
+/*
+ * Copyright © 2018, VideoLAN and dav1d authors
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "dav1d/dav1d.h"
+
+#include "common/validate.h"
+
+#include "src/internal.h"
+#include "src/log.h"
+
+void dav1d_log_default_callback(void *const cookie,
+                                const char *const format, va_list ap)
+{
+    vfprintf(stderr, format, ap);
+}
+
+#if CONFIG_LOG
+void dav1d_log(Dav1dContext *const c, const char *const format, ...) {
+    validate_input(c != NULL);
+
+    if (!c->logger.callback)
+        return;
+
+    va_list ap;
+    va_start(ap, format);
+    c->logger.callback(c->logger.cookie, format, ap);
+    va_end(ap);
+}
+#endif
--- /dev/null
+++ b/src/log.h
@@ -1,0 +1,47 @@
+/*
+ * Copyright © 2018, VideoLAN and dav1d authors
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __DAV1D_SRC_LOG_H__
+#define __DAV1D_SRC_LOG_H__
+
+#include "config.h"
+
+#include <stdarg.h>
+
+#include "dav1d/dav1d.h"
+
+#include "common/attributes.h"
+
+void dav1d_log_default_callback(void *cookie, const char *format, va_list ap);
+
+#if CONFIG_LOG
+#define dav1d_log dav1d_log
+void dav1d_log(Dav1dContext *c, const char *format, ...) ATTR_FORMAT_PRINTF(2, 3);
+#else
+#define dav1d_log(...) do { } while(0)
+#endif
+
+#endif /* __DAV1D_SRC_LOG_H__ */
--- a/src/meson.build
+++ b/src/meson.build
@@ -32,6 +32,7 @@
     'cpu.c',
     'data.c',
     'ref.c',
+    'log.c',
     'getbits.c',
     'obu.c',
     'decode.c',
--- a/src/obu.c
+++ b/src/obu.c
@@ -39,6 +39,7 @@
 #include "src/decode.h"
 #include "src/getbits.h"
 #include "src/levels.h"
+#include "src/log.h"
 #include "src/obu.h"
 #include "src/ref.h"
 #include "src/thread_task.h"