dav1d.h 5.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * 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_H__
#define __DAV1D_H__

31 32 33 34
#ifdef __cplusplus
extern "C" {
#endif

35 36
#include <errno.h>

37 38 39 40 41 42 43
#include "common.h"
#include "picture.h"
#include "data.h"

typedef struct Dav1dContext Dav1dContext;
typedef struct Dav1dRef Dav1dRef;

44 45 46
#define DAV1D_MAX_FRAME_THREADS 256
#define DAV1D_MAX_TILE_THREADS 64

47 48 49
typedef struct Dav1dSettings {
    int n_frame_threads;
    int n_tile_threads;
50
    Dav1dPicAllocator allocator;
51
    int apply_grain;
52
    int operating_point; ///< select an operating point for scalable AV1 bitstreams (0 - 31)
53
    int all_layers; ///< output all spatial layers of a scalable AV1 biststream
54 55 56 57 58 59 60 61 62
} Dav1dSettings;

/**
 * Get library version.
 */
DAV1D_API const char *dav1d_version(void);

/**
 * Initialize settings to default values.
63 64
 *
 * @param s Input settings context.
65 66 67 68
 */
DAV1D_API void dav1d_default_settings(Dav1dSettings *s);

/**
69
 * Allocate and open a decoder instance.
70
 *
71 72
 * @param c_out The decoder instance to open. *c_out will be set to the
 *              allocated context.
73
 * @param     s Input settings context.
74
 *
75 76
 * @note The context must be freed using dav1d_close() when decoding is
 *       finished.
77
 *
78
 * @return 0 on success, or < 0 (a negative errno code) on error.
79 80 81
 */
DAV1D_API int dav1d_open(Dav1dContext **c_out, const Dav1dSettings *s);

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/**
 * Parse a Sequence Header OBU from bitstream data.
 *
 * @param out Output Sequence Header.
 * @param buf The data to be parser.
 * @param sz  Size of the data.
 *
 * @return 0 on success, or < 0 (a negative errno code) on error.
 *
 * @note It is safe to feed this function data containing other OBUs than a
 *       Sequence Header, as they will simply be ignored. If there is more than
 *       one Sequence Header OBU present, only the last will be returned.
 */
DAV1D_API int dav1d_parse_sequence_header(Dav1dSequenceHeader *out,
                                          const uint8_t *buf, const size_t sz);

98
/**
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
 * Feed bitstream data to the decoder.
 *
 * @param   c Input decoder instance.
 * @param  in Input bitstream data. On success, ownership of the reference is
 *            passed to the library.
 *
 * @return
 *         0: Success, and the data was consumed.
 *   -EAGAIN: The data can't be consumed. dav1d_get_picture() should be called
 *            to get one or more frames before the function can consume new
 *            data.
 *   other negative errno codes: Error during decoding or because of invalid
 *                               passed-in arguments.
 */
DAV1D_API int dav1d_send_data(Dav1dContext *c, Dav1dData *in);

/**
 * Return a decoded picture.
117 118 119 120 121 122 123
 *
 * @param   c Input decoder instance.
 * @param out Output frame. The caller assumes ownership of the returned
 *            reference.
 *
 * @return
 *         0: Success, and a frame is returned.
124 125
 *   -EAGAIN: Not enough data to output a frame. dav1d_send_data() should be
 *            called with new input.
126 127
 *   other negative errno codes: Error during decoding or because of invalid
 *                               passed-in arguments.
128
 *
129 130
 * @note To drain buffered frames from the decoder (i.e. on end of stream),
 *       call this function until it returns -EAGAIN.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
 *
 * @code{.c}
 *  Dav1dData data = { 0 };
 *  Dav1dPicture p = { 0 };
 *  int res;
 *
 *  read_data(&data);
 *  do {
 *      res = dav1d_send_data(c, &data);
 *      // Keep going even if the function can't consume the current data
 *         packet. It eventually will after one or more frames have been
 *         returned in this loop.
 *      if (res < 0 && res != -EAGAIN)
 *          free_and_abort();
 *      res = dav1d_get_picture(c, &p);
 *      if (res < 0) {
 *          if (res != -EAGAIN)
 *              free_and_abort();
 *      } else
 *          output_and_unref_picture(&p);
 *  // Stay in the loop as long as there's data to consume.
 *  } while (data.sz || read_data(&data) == SUCCESS);
 *
 *  // Handle EOS by draining all buffered frames.
 *  do {
 *      res = dav1d_get_picture(c, &p);
 *      if (res < 0) {
 *          if (res != -EAGAIN)
 *              free_and_abort();
 *      } else
 *          output_and_unref_picture(&p);
 *  } while (res == 0);
 * @endcode
164
 */
165
DAV1D_API int dav1d_get_picture(Dav1dContext *c, Dav1dPicture *out);
166 167

/**
168 169 170
 * Close a decoder instance and free all associated memory.
 *
 * @param c_out The decoder instance to close. *c_out will be set to NULL.
171
 */
172
DAV1D_API void dav1d_close(Dav1dContext **c_out);
173

Ronald S. Bultje's avatar
Ronald S. Bultje committed
174 175
/**
 * Flush all delayed frames in decoder, to be used when seeking.
176 177
 *
 * @param c Input decoder instance.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
178 179 180
 */
DAV1D_API void dav1d_flush(Dav1dContext *c);

181 182 183 184
# ifdef __cplusplus
}
# endif

185
#endif /* __DAV1D_H__ */