diff options
Diffstat (limited to 'third_party/dav1d/examples')
-rw-r--r-- | third_party/dav1d/examples/dav1dplay.c | 583 | ||||
-rw-r--r-- | third_party/dav1d/examples/dp_fifo.c | 123 | ||||
-rw-r--r-- | third_party/dav1d/examples/dp_fifo.h | 61 | ||||
-rw-r--r-- | third_party/dav1d/examples/dp_renderer.h | 132 | ||||
-rw-r--r-- | third_party/dav1d/examples/dp_renderer_placebo.c | 723 | ||||
-rw-r--r-- | third_party/dav1d/examples/dp_renderer_sdl.c | 164 | ||||
-rw-r--r-- | third_party/dav1d/examples/meson.build | 74 |
7 files changed, 1860 insertions, 0 deletions
diff --git a/third_party/dav1d/examples/dav1dplay.c b/third_party/dav1d/examples/dav1dplay.c new file mode 100644 index 0000000000..d6bb262b56 --- /dev/null +++ b/third_party/dav1d/examples/dav1dplay.c @@ -0,0 +1,583 @@ +/* + * Copyright © 2019, 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 "vcs_version.h" + +#include <getopt.h> +#include <stdbool.h> + +#include <SDL.h> + +#include "dav1d/dav1d.h" + +#include "common/attributes.h" +#include "tools/input/input.h" +#include "dp_fifo.h" +#include "dp_renderer.h" + +// Selected renderer callbacks and cookie +static const Dav1dPlayRenderInfo *renderer_info = { NULL }; + +/** + * Render context structure + * This structure contains informations necessary + * to be shared between the decoder and the renderer + * threads. + */ +typedef struct render_context +{ + Dav1dPlaySettings settings; + Dav1dSettings lib_settings; + + // Renderer private data (passed to callbacks) + void *rd_priv; + + // Lock to protect access to the context structure + SDL_mutex *lock; + + // Timestamp of previous decoded frame + int64_t last_pts; + // Timestamp of current decoded frame + int64_t current_pts; + // Ticks when last frame was received + uint32_t last_ticks; + // PTS time base + double timebase; + + // Fifo + Dav1dPlayPtrFifo *fifo; + + // Custom SDL2 event type + uint32_t renderer_event_type; + + // Indicates if termination of the decoder thread was requested + uint8_t dec_should_terminate; +} Dav1dPlayRenderContext; + +static void dp_settings_print_usage(const char *const app, + const char *const reason, ...) +{ + if (reason) { + va_list args; + + va_start(args, reason); + vfprintf(stderr, reason, args); + va_end(args); + fprintf(stderr, "\n\n"); + } + fprintf(stderr, "Usage: %s [options]\n\n", app); + fprintf(stderr, "Supported options:\n" + " --input/-i $file: input file\n" + " --untimed/-u: ignore PTS, render as fast as possible\n" + " --framethreads $num: number of frame threads (default: 1)\n" + " --tilethreads $num: number of tile threads (default: 1)\n" + " --highquality: enable high quality rendering\n" + " --zerocopy/-z: enable zero copy upload path\n" + " --gpugrain/-g: enable GPU grain synthesis\n" + " --version/-v: print version and exit\n" + " --renderer/-r: select renderer backend (default: auto)\n"); + exit(1); +} + +static unsigned parse_unsigned(const char *const optarg, const int option, + const char *const app) +{ + char *end; + const unsigned res = (unsigned) strtoul(optarg, &end, 0); + if (*end || end == optarg) + dp_settings_print_usage(app, "Invalid argument \"%s\" for option %s; should be an integer", + optarg, option); + return res; +} + +static void dp_rd_ctx_parse_args(Dav1dPlayRenderContext *rd_ctx, + const int argc, char *const *const argv) +{ + int o; + Dav1dPlaySettings *settings = &rd_ctx->settings; + Dav1dSettings *lib_settings = &rd_ctx->lib_settings; + + // Short options + static const char short_opts[] = "i:vuzgr:"; + + enum { + ARG_FRAME_THREADS = 256, + ARG_TILE_THREADS, + ARG_HIGH_QUALITY, + }; + + // Long options + static const struct option long_opts[] = { + { "input", 1, NULL, 'i' }, + { "version", 0, NULL, 'v' }, + { "untimed", 0, NULL, 'u' }, + { "framethreads", 1, NULL, ARG_FRAME_THREADS }, + { "tilethreads", 1, NULL, ARG_TILE_THREADS }, + { "highquality", 0, NULL, ARG_HIGH_QUALITY }, + { "zerocopy", 0, NULL, 'z' }, + { "gpugrain", 0, NULL, 'g' }, + { "renderer", 0, NULL, 'r'}, + { NULL, 0, NULL, 0 }, + }; + + while ((o = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { + switch (o) { + case 'i': + settings->inputfile = optarg; + break; + case 'v': + fprintf(stderr, "%s\n", dav1d_version()); + exit(0); + case 'u': + settings->untimed = true; + break; + case ARG_HIGH_QUALITY: + settings->highquality = true; + break; + case 'z': + settings->zerocopy = true; + break; + case 'g': + settings->gpugrain = true; + break; + case 'r': + settings->renderer_name = optarg; + break; + case ARG_FRAME_THREADS: + lib_settings->n_frame_threads = + parse_unsigned(optarg, ARG_FRAME_THREADS, argv[0]); + break; + case ARG_TILE_THREADS: + lib_settings->n_tile_threads = + parse_unsigned(optarg, ARG_TILE_THREADS, argv[0]); + break; + default: + dp_settings_print_usage(argv[0], NULL); + } + } + + if (optind < argc) + dp_settings_print_usage(argv[0], + "Extra/unused arguments found, e.g. '%s'\n", argv[optind]); + if (!settings->inputfile) + dp_settings_print_usage(argv[0], "Input file (-i/--input) is required"); + if (settings->renderer_name && strcmp(settings->renderer_name, "auto") == 0) + settings->renderer_name = NULL; +} + +/** + * Destroy a Dav1dPlayRenderContext + */ +static void dp_rd_ctx_destroy(Dav1dPlayRenderContext *rd_ctx) +{ + assert(rd_ctx != NULL); + + renderer_info->destroy_renderer(rd_ctx->rd_priv); + dp_fifo_destroy(rd_ctx->fifo); + SDL_DestroyMutex(rd_ctx->lock); + free(rd_ctx); +} + +/** + * Create a Dav1dPlayRenderContext + * + * \note The Dav1dPlayRenderContext must be destroyed + * again by using dp_rd_ctx_destroy. + */ +static Dav1dPlayRenderContext *dp_rd_ctx_create(int argc, char **argv) +{ + Dav1dPlayRenderContext *rd_ctx; + + // Alloc + rd_ctx = malloc(sizeof(Dav1dPlayRenderContext)); + if (rd_ctx == NULL) { + return NULL; + } + + // Register a custom event to notify our SDL main thread + // about new frames + rd_ctx->renderer_event_type = SDL_RegisterEvents(1); + if (rd_ctx->renderer_event_type == UINT32_MAX) { + fprintf(stderr, "Failure to create custom SDL event type!\n"); + free(rd_ctx); + return NULL; + } + + rd_ctx->fifo = dp_fifo_create(5); + if (rd_ctx->fifo == NULL) { + fprintf(stderr, "Failed to create FIFO for output pictures!\n"); + free(rd_ctx); + return NULL; + } + + rd_ctx->lock = SDL_CreateMutex(); + if (rd_ctx->lock == NULL) { + fprintf(stderr, "SDL_CreateMutex failed: %s\n", SDL_GetError()); + dp_fifo_destroy(rd_ctx->fifo); + free(rd_ctx); + return NULL; + } + + // Parse and validate arguments + dav1d_default_settings(&rd_ctx->lib_settings); + memset(&rd_ctx->settings, 0, sizeof(rd_ctx->settings)); + dp_rd_ctx_parse_args(rd_ctx, argc, argv); + + // Select renderer + renderer_info = dp_get_renderer(rd_ctx->settings.renderer_name); + + if (renderer_info == NULL) { + printf("No suitable rendered matching %s found.\n", + (rd_ctx->settings.renderer_name) ? rd_ctx->settings.renderer_name : "auto"); + } else { + printf("Using %s renderer\n", renderer_info->name); + } + + rd_ctx->rd_priv = (renderer_info) ? renderer_info->create_renderer() : NULL; + if (rd_ctx->rd_priv == NULL) { + SDL_DestroyMutex(rd_ctx->lock); + dp_fifo_destroy(rd_ctx->fifo); + free(rd_ctx); + return NULL; + } + + rd_ctx->last_pts = 0; + rd_ctx->last_ticks = 0; + rd_ctx->current_pts = 0; + rd_ctx->timebase = 0; + rd_ctx->dec_should_terminate = 0; + + return rd_ctx; +} + +/** + * Notify about new available frame + */ +static void dp_rd_ctx_post_event(Dav1dPlayRenderContext *rd_ctx, uint32_t code) +{ + SDL_Event event; + SDL_zero(event); + event.type = rd_ctx->renderer_event_type; + event.user.code = code; + SDL_PushEvent(&event); +} + +/** + * Update the decoder context with a new dav1d picture + * + * Once the decoder decoded a new picture, this call can be used + * to update the internal texture of the render context with the + * new picture. + */ +static void dp_rd_ctx_update_with_dav1d_picture(Dav1dPlayRenderContext *rd_ctx, + Dav1dPicture *dav1d_pic) +{ + renderer_info->update_frame(rd_ctx->rd_priv, dav1d_pic, &rd_ctx->settings); + rd_ctx->current_pts = dav1d_pic->m.timestamp; +} + +/** + * Terminate decoder thread (async) + */ +static void dp_rd_ctx_request_shutdown(Dav1dPlayRenderContext *rd_ctx) +{ + SDL_LockMutex(rd_ctx->lock); + rd_ctx->dec_should_terminate = 1; + SDL_UnlockMutex(rd_ctx->lock); +} + +/** + * Query state of decoder shutdown request + */ +static int dp_rd_ctx_should_terminate(Dav1dPlayRenderContext *rd_ctx) +{ + int ret = 0; + SDL_LockMutex(rd_ctx->lock); + ret = rd_ctx->dec_should_terminate; + SDL_UnlockMutex(rd_ctx->lock); + return ret; +} + +/** + * Render the currently available texture + * + * Renders the currently available texture, if any. + */ +static void dp_rd_ctx_render(Dav1dPlayRenderContext *rd_ctx) +{ + // Calculate time since last frame was received + uint32_t ticks_now = SDL_GetTicks(); + uint32_t ticks_diff = (rd_ctx->last_ticks != 0) ? ticks_now - rd_ctx->last_ticks : 0; + + // Calculate when to display the frame + int64_t pts_diff = rd_ctx->current_pts - rd_ctx->last_pts; + int32_t wait_time = (pts_diff * rd_ctx->timebase) * 1000 - ticks_diff; + rd_ctx->last_pts = rd_ctx->current_pts; + + // In untimed mode, simply don't wait + if (rd_ctx->settings.untimed) + wait_time = 0; + + // This way of timing the playback is not accurate, as there is no guarantee + // that SDL_Delay will wait for exactly the requested amount of time so in a + // accurate player this would need to be done in a better way. + if (wait_time > 0) { + SDL_Delay(wait_time); + } else if (wait_time < -10) { // Do not warn for minor time drifts + fprintf(stderr, "Frame displayed %f seconds too late\n", wait_time/(float)1000); + } + + renderer_info->render(rd_ctx->rd_priv, &rd_ctx->settings); + + rd_ctx->last_ticks = SDL_GetTicks(); +} + +/* Decoder thread "main" function */ +static int decoder_thread_main(void *cookie) +{ + Dav1dPlayRenderContext *rd_ctx = cookie; + + Dav1dPicture *p; + Dav1dContext *c = NULL; + Dav1dData data; + DemuxerContext *in_ctx = NULL; + int res = 0; + unsigned n_out = 0, total, timebase[2], fps[2]; + + // Store current ticks for stats calculation + uint32_t decoder_start = SDL_GetTicks(); + + Dav1dPlaySettings settings = rd_ctx->settings; + + if ((res = input_open(&in_ctx, "ivf", + settings.inputfile, + fps, &total, timebase)) < 0) + { + fprintf(stderr, "Failed to open demuxer\n"); + res = 1; + goto cleanup; + } + + double timebase_d = timebase[1]/(double)timebase[0]; + rd_ctx->timebase = timebase_d; + + if ((res = dav1d_open(&c, &rd_ctx->lib_settings))) { + fprintf(stderr, "Failed opening dav1d decoder\n"); + res = 1; + goto cleanup; + } + + if ((res = input_read(in_ctx, &data)) < 0) { + fprintf(stderr, "Failed demuxing input\n"); + res = 1; + goto cleanup; + } + + // Decoder loop + do { + if (dp_rd_ctx_should_terminate(rd_ctx)) + break; + + // Send data packets we got from the demuxer to dav1d + if ((res = dav1d_send_data(c, &data)) < 0) { + // On EAGAIN, dav1d can not consume more data and + // dav1d_get_picture needs to be called first, which + // will happen below, so just keep going in that case + // and do not error out. + if (res != DAV1D_ERR(EAGAIN)) { + dav1d_data_unref(&data); + fprintf(stderr, "Error decoding frame: %s\n", + strerror(-res)); + break; + } + } + + p = calloc(1, sizeof(*p)); + + // Try to get a decoded frame + if ((res = dav1d_get_picture(c, p)) < 0) { + // In all error cases, even EAGAIN, p needs to be freed as + // it is never added to the queue and would leak. + free(p); + + // On EAGAIN, it means dav1d has not enough data to decode + // therefore this is not a decoding error but just means + // we need to feed it more data, which happens in the next + // run of this decoder loop. + if (res != DAV1D_ERR(EAGAIN)) { + fprintf(stderr, "Error decoding frame: %s\n", + strerror(-res)); + break; + } + res = 0; + } else { + + // Queue frame + dp_fifo_push(rd_ctx->fifo, p); + dp_rd_ctx_post_event(rd_ctx, DAV1D_EVENT_NEW_FRAME); + + n_out++; + } + } while ((data.sz > 0 || !input_read(in_ctx, &data))); + + // Release remaining data + if (data.sz > 0) dav1d_data_unref(&data); + + // Do not drain in case an error occured and caused us to leave the + // decoding loop early. + if (res < 0) + goto cleanup; + + // Drain decoder + // When there is no more data to feed to the decoder, for example + // because the file ended, we still need to request pictures, as + // even though we do not have more data, there can be frames decoded + // from data we sent before. So we need to call dav1d_get_picture until + // we get an EAGAIN error. + do { + if (dp_rd_ctx_should_terminate(rd_ctx)) + break; + + p = calloc(1, sizeof(*p)); + res = dav1d_get_picture(c, p); + if (res < 0) { + free(p); + if (res != DAV1D_ERR(EAGAIN)) { + fprintf(stderr, "Error decoding frame: %s\n", + strerror(-res)); + break; + } + } else { + // Queue frame + dp_fifo_push(rd_ctx->fifo, p); + dp_rd_ctx_post_event(rd_ctx, DAV1D_EVENT_NEW_FRAME); + + n_out++; + } + } while (res != DAV1D_ERR(EAGAIN)); + + // Print stats + uint32_t decoding_time_ms = SDL_GetTicks() - decoder_start; + printf("Decoded %u frames in %d seconds, avg %.02f fps\n", + n_out, decoding_time_ms/1000, n_out / (decoding_time_ms / 1000.0)); + +cleanup: + dp_rd_ctx_post_event(rd_ctx, DAV1D_EVENT_DEC_QUIT); + + if (in_ctx) + input_close(in_ctx); + if (c) + dav1d_close(&c); + + return (res != DAV1D_ERR(EAGAIN) && res < 0); +} + +int main(int argc, char **argv) +{ + SDL_Thread *decoder_thread; + + // Check for version mismatch between library and tool + const char *version = dav1d_version(); + if (strcmp(version, DAV1D_VERSION)) { + fprintf(stderr, "Version mismatch (library: %s, executable: %s)\n", + version, DAV1D_VERSION); + return 1; + } + + // Init SDL2 library + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) + return 10; + + // Create render context + Dav1dPlayRenderContext *rd_ctx = dp_rd_ctx_create(argc, argv); + if (rd_ctx == NULL) { + fprintf(stderr, "Failed creating render context\n"); + return 5; + } + + if (rd_ctx->settings.zerocopy) { + if (renderer_info->alloc_pic) { + rd_ctx->lib_settings.allocator = (Dav1dPicAllocator) { + .cookie = rd_ctx->rd_priv, + .alloc_picture_callback = renderer_info->alloc_pic, + .release_picture_callback = renderer_info->release_pic, + }; + } else { + fprintf(stderr, "--zerocopy unsupported by selected renderer\n"); + } + } + + if (rd_ctx->settings.gpugrain) { + if (renderer_info->supports_gpu_grain) { + rd_ctx->lib_settings.apply_grain = 0; + } else { + fprintf(stderr, "--gpugrain unsupported by selected renderer\n"); + } + } + + // Start decoder thread + decoder_thread = SDL_CreateThread(decoder_thread_main, "Decoder thread", rd_ctx); + + // Main loop + while (1) { + + SDL_Event e; + if (SDL_WaitEvent(&e)) { + if (e.type == SDL_QUIT) { + dp_rd_ctx_request_shutdown(rd_ctx); + } else if (e.type == SDL_WINDOWEVENT) { + if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + // TODO: Handle window resizes + } + } else if (e.type == rd_ctx->renderer_event_type) { + if (e.user.code == DAV1D_EVENT_NEW_FRAME) { + // Dequeue frame and update the render context with it + Dav1dPicture *p = dp_fifo_shift(rd_ctx->fifo); + + // Do not update textures during termination + if (!dp_rd_ctx_should_terminate(rd_ctx)) + dp_rd_ctx_update_with_dav1d_picture(rd_ctx, p); + dav1d_picture_unref(p); + free(p); + } else if (e.user.code == DAV1D_EVENT_DEC_QUIT) { + break; + } + } + } + + // Do not render during termination + if (!dp_rd_ctx_should_terminate(rd_ctx)) + dp_rd_ctx_render(rd_ctx); + } + + int decoder_ret = 0; + SDL_WaitThread(decoder_thread, &decoder_ret); + + dp_rd_ctx_destroy(rd_ctx); + + return decoder_ret; +} diff --git a/third_party/dav1d/examples/dp_fifo.c b/third_party/dav1d/examples/dp_fifo.c new file mode 100644 index 0000000000..243d2e933b --- /dev/null +++ b/third_party/dav1d/examples/dp_fifo.c @@ -0,0 +1,123 @@ +/* + * Copyright © 2019, 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 <SDL.h> +#include <assert.h> + +#include "dp_fifo.h" + +// FIFO structure +struct dp_fifo +{ + SDL_mutex *lock; + SDL_cond *cond_change; + size_t capacity; + size_t count; + void **entries; +}; + + +Dav1dPlayPtrFifo *dp_fifo_create(size_t capacity) +{ + Dav1dPlayPtrFifo *fifo; + + assert(capacity > 0); + if (capacity <= 0) + return NULL; + + fifo = malloc(sizeof(*fifo)); + if (fifo == NULL) + return NULL; + + fifo->capacity = capacity; + fifo->count = 0; + + fifo->lock = SDL_CreateMutex(); + if (fifo->lock == NULL) { + free(fifo); + return NULL; + } + fifo->cond_change = SDL_CreateCond(); + if (fifo->cond_change == NULL) { + SDL_DestroyMutex(fifo->lock); + free(fifo); + return NULL; + } + + fifo->entries = calloc(capacity, sizeof(void*)); + if (fifo->entries == NULL) { + dp_fifo_destroy(fifo); + return NULL; + } + + return fifo; +} + +// Destroy FIFO +void dp_fifo_destroy(Dav1dPlayPtrFifo *fifo) +{ + assert(fifo->count == 0); + SDL_DestroyMutex(fifo->lock); + SDL_DestroyCond(fifo->cond_change); + free(fifo->entries); + free(fifo); +} + +// Push to FIFO +void dp_fifo_push(Dav1dPlayPtrFifo *fifo, void *element) +{ + SDL_LockMutex(fifo->lock); + while (fifo->count == fifo->capacity) + SDL_CondWait(fifo->cond_change, fifo->lock); + fifo->entries[fifo->count++] = element; + if (fifo->count == 1) + SDL_CondSignal(fifo->cond_change); + SDL_UnlockMutex(fifo->lock); +} + +// Helper that shifts the FIFO array +static void *dp_fifo_array_shift(void **arr, size_t len) +{ + void *shifted_element = arr[0]; + for (size_t i = 1; i < len; ++i) + arr[i-1] = arr[i]; + return shifted_element; +} + +// Get item from FIFO +void *dp_fifo_shift(Dav1dPlayPtrFifo *fifo) +{ + SDL_LockMutex(fifo->lock); + while (fifo->count == 0) + SDL_CondWait(fifo->cond_change, fifo->lock); + void *res = dp_fifo_array_shift(fifo->entries, fifo->count--); + if (fifo->count == fifo->capacity - 1) + SDL_CondSignal(fifo->cond_change); + SDL_UnlockMutex(fifo->lock); + return res; +} + + diff --git a/third_party/dav1d/examples/dp_fifo.h b/third_party/dav1d/examples/dp_fifo.h new file mode 100644 index 0000000000..a94b089b20 --- /dev/null +++ b/third_party/dav1d/examples/dp_fifo.h @@ -0,0 +1,61 @@ +/* + * Copyright © 2019, 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. + */ + +/* + * Dav1dPlay FIFO helper + */ + +typedef struct dp_fifo Dav1dPlayPtrFifo; + +/* Create a FIFO + * + * Creates a FIFO with the given capacity. + * If the capacity is reached, new inserts into the FIFO + * will block until enough space is available again. + */ +Dav1dPlayPtrFifo *dp_fifo_create(size_t capacity); + +/* Destroy a FIFO + * + * The FIFO must be empty before it is destroyed! + */ +void dp_fifo_destroy(Dav1dPlayPtrFifo *fifo); + +/* Shift FIFO + * + * Return the first item from the FIFO, thereby removing it from + * the FIFO and making room for new entries. + */ +void *dp_fifo_shift(Dav1dPlayPtrFifo *fifo); + +/* Push to FIFO + * + * Add an item to the end of the FIFO. + * If the FIFO is full, this call will block until there is again enough + * space in the FIFO, so calling this from the "consumer" thread if no + * other thread will call dp_fifo_shift will lead to a deadlock. + */ +void dp_fifo_push(Dav1dPlayPtrFifo *fifo, void *element); diff --git a/third_party/dav1d/examples/dp_renderer.h b/third_party/dav1d/examples/dp_renderer.h new file mode 100644 index 0000000000..4c6f2954f7 --- /dev/null +++ b/third_party/dav1d/examples/dp_renderer.h @@ -0,0 +1,132 @@ +/* + * Copyright © 2020, 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 <inttypes.h> +#include <string.h> + +#include "dav1d/dav1d.h" + +#include <SDL.h> +#ifdef HAVE_PLACEBO +# include <libplacebo/config.h> +#endif + +// Check libplacebo Vulkan rendering +#if defined(HAVE_VULKAN) && defined(SDL_VIDEO_VULKAN) +# if defined(PL_HAVE_VULKAN) && PL_HAVE_VULKAN +# define HAVE_RENDERER_PLACEBO +# define HAVE_PLACEBO_VULKAN +# endif +#endif + +// Check libplacebo OpenGL rendering +#if defined(PL_HAVE_OPENGL) && PL_HAVE_OPENGL +# define HAVE_RENDERER_PLACEBO +# define HAVE_PLACEBO_OPENGL +#endif + +/** + * Settings structure + * Hold all settings available for the player, + * this is usually filled by parsing arguments + * from the console. + */ +typedef struct { + const char *inputfile; + const char *renderer_name; + int highquality; + int untimed; + int zerocopy; + int gpugrain; +} Dav1dPlaySettings; + +#define WINDOW_WIDTH 910 +#define WINDOW_HEIGHT 512 + +#define DAV1D_EVENT_NEW_FRAME 1 +#define DAV1D_EVENT_DEC_QUIT 2 + +/** + * Renderer info + */ +typedef struct rdr_info +{ + // Renderer name + const char *name; + // Cookie passed to the renderer implementation callbacks + void *cookie; + // Callback to create the renderer + void* (*create_renderer)(); + // Callback to destroy the renderer + void (*destroy_renderer)(void *cookie); + // Callback to the render function that renders a prevously sent frame + void (*render)(void *cookie, const Dav1dPlaySettings *settings); + // Callback to the send frame function + int (*update_frame)(void *cookie, Dav1dPicture *dav1d_pic, + const Dav1dPlaySettings *settings); + // Callback for alloc/release pictures (optional) + int (*alloc_pic)(Dav1dPicture *pic, void *cookie); + void (*release_pic)(Dav1dPicture *pic, void *cookie); + // Whether or not this renderer can apply on-GPU film grain synthesis + int supports_gpu_grain; +} Dav1dPlayRenderInfo; + +extern const Dav1dPlayRenderInfo rdr_placebo_vk; +extern const Dav1dPlayRenderInfo rdr_placebo_gl; +extern const Dav1dPlayRenderInfo rdr_sdl; + +// Available renderes ordered by priority +static const Dav1dPlayRenderInfo* const dp_renderers[] = { + &rdr_placebo_vk, + &rdr_placebo_gl, + &rdr_sdl, +}; + +static inline const Dav1dPlayRenderInfo *dp_get_renderer(const char *name) +{ + for (size_t i = 0; i < (sizeof(dp_renderers)/sizeof(*dp_renderers)); ++i) + { + if (dp_renderers[i]->name == NULL) + continue; + + if (name == NULL || strcmp(name, dp_renderers[i]->name) == 0) { + return dp_renderers[i]; + } + } + return NULL; +} + +static inline SDL_Window *dp_create_sdl_window(int window_flags) +{ + SDL_Window *win; + window_flags |= SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI; + + win = SDL_CreateWindow("Dav1dPlay", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + WINDOW_WIDTH, WINDOW_HEIGHT, window_flags); + SDL_SetWindowResizable(win, SDL_TRUE); + + return win; +} diff --git a/third_party/dav1d/examples/dp_renderer_placebo.c b/third_party/dav1d/examples/dp_renderer_placebo.c new file mode 100644 index 0000000000..2dd7bea12d --- /dev/null +++ b/third_party/dav1d/examples/dp_renderer_placebo.c @@ -0,0 +1,723 @@ +/* + * Copyright © 2020, 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 "dp_renderer.h" + +#ifdef HAVE_RENDERER_PLACEBO +#include <assert.h> + +#include <libplacebo/renderer.h> +#include <libplacebo/utils/upload.h> + +#ifdef HAVE_PLACEBO_VULKAN +# include <libplacebo/vulkan.h> +# include <SDL_vulkan.h> +#endif +#ifdef HAVE_PLACEBO_OPENGL +# include <libplacebo/opengl.h> +# include <SDL_opengl.h> +#endif + + +/** + * Renderer context for libplacebo + */ +typedef struct renderer_priv_ctx +{ + // SDL window + SDL_Window *win; + // Placebo context + struct pl_context *ctx; + // Placebo renderer + struct pl_renderer *renderer; +#ifdef HAVE_PLACEBO_VULKAN + // Placebo Vulkan handle + const struct pl_vulkan *vk; + // Placebo Vulkan instance + const struct pl_vk_inst *vk_inst; + // Vulkan surface + VkSurfaceKHR surf; +#endif +#ifdef HAVE_PLACEBO_OPENGL + // Placebo OpenGL handle + const struct pl_opengl *gl; +#endif + // Placebo GPU + const struct pl_gpu *gpu; + // Placebo swapchain + const struct pl_swapchain *swapchain; + // Lock protecting access to the texture + SDL_mutex *lock; + // Image to render, and planes backing them + struct pl_image image; + const struct pl_tex *plane_tex[3]; +} Dav1dPlayRendererPrivateContext; + +static Dav1dPlayRendererPrivateContext* + placebo_renderer_create_common(int window_flags) +{ + // Create Window + SDL_Window *sdlwin = dp_create_sdl_window(window_flags | SDL_WINDOW_RESIZABLE); + if (sdlwin == NULL) + return NULL; + + // Alloc + Dav1dPlayRendererPrivateContext *rd_priv_ctx = malloc(sizeof(Dav1dPlayRendererPrivateContext)); + if (rd_priv_ctx == NULL) { + return NULL; + } + + *rd_priv_ctx = (Dav1dPlayRendererPrivateContext) {0}; + rd_priv_ctx->win = sdlwin; + + // Init libplacebo + rd_priv_ctx->ctx = pl_context_create(PL_API_VER, &(struct pl_context_params) { + .log_cb = pl_log_color, +#ifndef NDEBUG + .log_level = PL_LOG_DEBUG, +#else + .log_level = PL_LOG_WARN, +#endif + }); + if (rd_priv_ctx->ctx == NULL) { + free(rd_priv_ctx); + return NULL; + } + + // Create Mutex + rd_priv_ctx->lock = SDL_CreateMutex(); + if (rd_priv_ctx->lock == NULL) { + fprintf(stderr, "SDL_CreateMutex failed: %s\n", SDL_GetError()); + pl_context_destroy(&(rd_priv_ctx->ctx)); + free(rd_priv_ctx); + return NULL; + } + + return rd_priv_ctx; +} + +#ifdef HAVE_PLACEBO_OPENGL +static void *placebo_renderer_create_gl() +{ + SDL_Window *sdlwin = NULL; + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); + + // Common init + Dav1dPlayRendererPrivateContext *rd_priv_ctx = + placebo_renderer_create_common(SDL_WINDOW_OPENGL); + + if (rd_priv_ctx == NULL) + return NULL; + sdlwin = rd_priv_ctx->win; + + // Init OpenGL + struct pl_opengl_params params = pl_opengl_default_params; +# ifndef NDEBUG + params.debug = true; +# endif + + SDL_GLContext glcontext = SDL_GL_CreateContext(sdlwin); + SDL_GL_MakeCurrent(sdlwin, glcontext); + + rd_priv_ctx->gl = pl_opengl_create(rd_priv_ctx->ctx, ¶ms); + if (!rd_priv_ctx->gl) { + fprintf(stderr, "Failed creating opengl device!\n"); + exit(2); + } + + rd_priv_ctx->swapchain = pl_opengl_create_swapchain(rd_priv_ctx->gl, + &(struct pl_opengl_swapchain_params) { + .swap_buffers = (void (*)(void *)) SDL_GL_SwapWindow, + .priv = sdlwin, + }); + + if (!rd_priv_ctx->swapchain) { + fprintf(stderr, "Failed creating opengl swapchain!\n"); + exit(2); + } + + int w = WINDOW_WIDTH, h = WINDOW_HEIGHT; + SDL_GL_GetDrawableSize(sdlwin, &w, &h); + + if (!pl_swapchain_resize(rd_priv_ctx->swapchain, &w, &h)) { + fprintf(stderr, "Failed resizing vulkan swapchain!\n"); + exit(2); + } + + rd_priv_ctx->gpu = rd_priv_ctx->gl->gpu; + + if (w != WINDOW_WIDTH || h != WINDOW_HEIGHT) + printf("Note: window dimensions differ (got %dx%d)\n", w, h); + + return rd_priv_ctx; +} +#endif + +#ifdef HAVE_PLACEBO_VULKAN +static void *placebo_renderer_create_vk() +{ + SDL_Window *sdlwin = NULL; + + // Common init + Dav1dPlayRendererPrivateContext *rd_priv_ctx = + placebo_renderer_create_common(SDL_WINDOW_VULKAN); + + if (rd_priv_ctx == NULL) + return NULL; + sdlwin = rd_priv_ctx->win; + + // Init Vulkan + unsigned num = 0; + if (!SDL_Vulkan_GetInstanceExtensions(sdlwin, &num, NULL)) { + fprintf(stderr, "Failed enumerating Vulkan extensions: %s\n", SDL_GetError()); + exit(1); + } + + const char **extensions = malloc(num * sizeof(const char *)); + assert(extensions); + + SDL_bool ok = SDL_Vulkan_GetInstanceExtensions(sdlwin, &num, extensions); + if (!ok) { + fprintf(stderr, "Failed getting Vk instance extensions\n"); + exit(1); + } + + if (num > 0) { + printf("Requesting %d additional Vulkan extensions:\n", num); + for (unsigned i = 0; i < num; i++) + printf(" %s\n", extensions[i]); + } + + struct pl_vk_inst_params iparams = pl_vk_inst_default_params; + iparams.extensions = extensions; + iparams.num_extensions = num; + + rd_priv_ctx->vk_inst = pl_vk_inst_create(rd_priv_ctx->ctx, &iparams); + if (!rd_priv_ctx->vk_inst) { + fprintf(stderr, "Failed creating Vulkan instance!\n"); + exit(1); + } + free(extensions); + + if (!SDL_Vulkan_CreateSurface(sdlwin, rd_priv_ctx->vk_inst->instance, &rd_priv_ctx->surf)) { + fprintf(stderr, "Failed creating vulkan surface: %s\n", SDL_GetError()); + exit(1); + } + + struct pl_vulkan_params params = pl_vulkan_default_params; + params.instance = rd_priv_ctx->vk_inst->instance; + params.surface = rd_priv_ctx->surf; + params.allow_software = true; + + rd_priv_ctx->vk = pl_vulkan_create(rd_priv_ctx->ctx, ¶ms); + if (!rd_priv_ctx->vk) { + fprintf(stderr, "Failed creating vulkan device!\n"); + exit(2); + } + + // Create swapchain + rd_priv_ctx->swapchain = pl_vulkan_create_swapchain(rd_priv_ctx->vk, + &(struct pl_vulkan_swapchain_params) { + .surface = rd_priv_ctx->surf, + .present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR, + }); + + if (!rd_priv_ctx->swapchain) { + fprintf(stderr, "Failed creating vulkan swapchain!\n"); + exit(2); + } + + int w = WINDOW_WIDTH, h = WINDOW_HEIGHT; + if (!pl_swapchain_resize(rd_priv_ctx->swapchain, &w, &h)) { + fprintf(stderr, "Failed resizing vulkan swapchain!\n"); + exit(2); + } + + rd_priv_ctx->gpu = rd_priv_ctx->vk->gpu; + + if (w != WINDOW_WIDTH || h != WINDOW_HEIGHT) + printf("Note: window dimensions differ (got %dx%d)\n", w, h); + + return rd_priv_ctx; +} +#endif + +static void placebo_renderer_destroy(void *cookie) +{ + Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie; + assert(rd_priv_ctx != NULL); + + pl_renderer_destroy(&(rd_priv_ctx->renderer)); + pl_swapchain_destroy(&(rd_priv_ctx->swapchain)); + for (int i = 0; i < 3; i++) + pl_tex_destroy(rd_priv_ctx->gpu, &(rd_priv_ctx->plane_tex[i])); + +#ifdef HAVE_PLACEBO_VULKAN + if (rd_priv_ctx->vk) { + pl_vulkan_destroy(&(rd_priv_ctx->vk)); + vkDestroySurfaceKHR(rd_priv_ctx->vk_inst->instance, rd_priv_ctx->surf, NULL); + pl_vk_inst_destroy(&(rd_priv_ctx->vk_inst)); + } +#endif +#ifdef HAVE_PLACEBO_OPENGL + if (rd_priv_ctx->gl) + pl_opengl_destroy(&(rd_priv_ctx->gl)); +#endif + + SDL_DestroyWindow(rd_priv_ctx->win); + + pl_context_destroy(&(rd_priv_ctx->ctx)); +} + +static void placebo_render(void *cookie, const Dav1dPlaySettings *settings) +{ + Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie; + assert(rd_priv_ctx != NULL); + + SDL_LockMutex(rd_priv_ctx->lock); + if (!rd_priv_ctx->image.num_planes) { + SDL_UnlockMutex(rd_priv_ctx->lock); + return; + } + + // Prepare rendering + if (rd_priv_ctx->renderer == NULL) { + rd_priv_ctx->renderer = pl_renderer_create(rd_priv_ctx->ctx, rd_priv_ctx->gpu); + } + + struct pl_swapchain_frame frame; + bool ok = pl_swapchain_start_frame(rd_priv_ctx->swapchain, &frame); + if (!ok) { + SDL_UnlockMutex(rd_priv_ctx->lock); + return; + } + + struct pl_render_params render_params = {0}; + if (settings->highquality) + render_params = pl_render_default_params; + + struct pl_render_target target; + pl_render_target_from_swapchain(&target, &frame); + target.profile = (struct pl_icc_profile) { + .data = NULL, + .len = 0, + }; + +#if PL_API_VER >= 66 + pl_rect2df_aspect_copy(&target.dst_rect, &rd_priv_ctx->image.src_rect, 0.0); + if (pl_render_target_partial(&target)) + pl_tex_clear(rd_priv_ctx->gpu, target.fbo, (float[4]){ 0.0 }); +#endif + + if (!pl_render_image(rd_priv_ctx->renderer, &rd_priv_ctx->image, &target, &render_params)) { + fprintf(stderr, "Failed rendering frame!\n"); + pl_tex_clear(rd_priv_ctx->gpu, target.fbo, (float[4]){ 1.0 }); + } + + ok = pl_swapchain_submit_frame(rd_priv_ctx->swapchain); + if (!ok) { + fprintf(stderr, "Failed submitting frame!\n"); + SDL_UnlockMutex(rd_priv_ctx->lock); + return; + } + + pl_swapchain_swap_buffers(rd_priv_ctx->swapchain); + SDL_UnlockMutex(rd_priv_ctx->lock); +} + +static int placebo_upload_image(void *cookie, Dav1dPicture *dav1d_pic, + const Dav1dPlaySettings *settings) +{ + Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie; + assert(rd_priv_ctx != NULL); + + SDL_LockMutex(rd_priv_ctx->lock); + + if (dav1d_pic == NULL) { + SDL_UnlockMutex(rd_priv_ctx->lock); + return 0; + } + + int width = dav1d_pic->p.w; + int height = dav1d_pic->p.h; + int sub_x = 0, sub_y = 0; + int bytes = (dav1d_pic->p.bpc + 7) / 8; // rounded up + enum pl_chroma_location chroma_loc = PL_CHROMA_UNKNOWN; + + struct pl_image *image = &rd_priv_ctx->image; + *image = (struct pl_image) { + .num_planes = 3, + .width = width, + .height = height, + .src_rect = {0, 0, width, height}, + + .repr = { + .bits = { + .sample_depth = bytes * 8, + .color_depth = dav1d_pic->p.bpc, + }, + }, + }; + + // Figure out the correct plane dimensions/count + switch (dav1d_pic->p.layout) { + case DAV1D_PIXEL_LAYOUT_I400: + image->num_planes = 1; + break; + case DAV1D_PIXEL_LAYOUT_I420: + sub_x = sub_y = 1; + break; + case DAV1D_PIXEL_LAYOUT_I422: + sub_x = 1; + break; + case DAV1D_PIXEL_LAYOUT_I444: + break; + } + + // Set the right colorspace metadata etc. + switch (dav1d_pic->seq_hdr->pri) { + case DAV1D_COLOR_PRI_UNKNOWN: image->color.primaries = PL_COLOR_PRIM_UNKNOWN; break; + case DAV1D_COLOR_PRI_BT709: image->color.primaries = PL_COLOR_PRIM_BT_709; break; + case DAV1D_COLOR_PRI_BT470M: image->color.primaries = PL_COLOR_PRIM_BT_470M; break; + case DAV1D_COLOR_PRI_BT470BG: image->color.primaries = PL_COLOR_PRIM_BT_601_625; break; + case DAV1D_COLOR_PRI_BT601: image->color.primaries = PL_COLOR_PRIM_BT_601_625; break; + case DAV1D_COLOR_PRI_BT2020: image->color.primaries = PL_COLOR_PRIM_BT_2020; break; + + case DAV1D_COLOR_PRI_XYZ: + // Handled below + assert(dav1d_pic->seq_hdr->mtrx == DAV1D_MC_IDENTITY); + break; + + default: + printf("warning: unknown dav1d color primaries %d.. ignoring, picture " + "may be very incorrect\n", dav1d_pic->seq_hdr->pri); + break; + } + + switch (dav1d_pic->seq_hdr->trc) { + case DAV1D_TRC_BT709: + case DAV1D_TRC_BT470M: + case DAV1D_TRC_BT470BG: + case DAV1D_TRC_BT601: + case DAV1D_TRC_SMPTE240: + case DAV1D_TRC_BT2020_10BIT: + case DAV1D_TRC_BT2020_12BIT: + // These all map to the effective "SDR" CRT-based EOTF, BT.1886 + image->color.transfer = PL_COLOR_TRC_BT_1886; + break; + + case DAV1D_TRC_UNKNOWN: image->color.transfer = PL_COLOR_TRC_UNKNOWN; break; + case DAV1D_TRC_LINEAR: image->color.transfer = PL_COLOR_TRC_LINEAR; break; + case DAV1D_TRC_SRGB: image->color.transfer = PL_COLOR_TRC_SRGB; break; + case DAV1D_TRC_SMPTE2084: image->color.transfer = PL_COLOR_TRC_PQ; break; + case DAV1D_TRC_HLG: image->color.transfer = PL_COLOR_TRC_HLG; break; + + default: + printf("warning: unknown dav1d color transfer %d.. ignoring, picture " + "may be very incorrect\n", dav1d_pic->seq_hdr->trc); + break; + } + + switch (dav1d_pic->seq_hdr->mtrx) { + case DAV1D_MC_IDENTITY: + // This is going to be either RGB or XYZ + if (dav1d_pic->seq_hdr->pri == DAV1D_COLOR_PRI_XYZ) { + image->repr.sys = PL_COLOR_SYSTEM_XYZ; + } else { + image->repr.sys = PL_COLOR_SYSTEM_RGB; + } + break; + + case DAV1D_MC_UNKNOWN: + // PL_COLOR_SYSTEM_UNKNOWN maps to RGB, so hard-code this one + image->repr.sys = pl_color_system_guess_ycbcr(width, height); + break; + + case DAV1D_MC_BT709: image->repr.sys = PL_COLOR_SYSTEM_BT_709; break; + case DAV1D_MC_BT601: image->repr.sys = PL_COLOR_SYSTEM_BT_601; break; + case DAV1D_MC_SMPTE240: image->repr.sys = PL_COLOR_SYSTEM_SMPTE_240M; break; + case DAV1D_MC_SMPTE_YCGCO: image->repr.sys = PL_COLOR_SYSTEM_YCGCO; break; + case DAV1D_MC_BT2020_NCL: image->repr.sys = PL_COLOR_SYSTEM_BT_2020_NC; break; + case DAV1D_MC_BT2020_CL: image->repr.sys = PL_COLOR_SYSTEM_BT_2020_C; break; + + case DAV1D_MC_ICTCP: + // This one is split up based on the actual HDR curve in use + if (dav1d_pic->seq_hdr->trc == DAV1D_TRC_HLG) { + image->repr.sys = PL_COLOR_SYSTEM_BT_2100_HLG; + } else { + image->repr.sys = PL_COLOR_SYSTEM_BT_2100_PQ; + } + break; + + default: + printf("warning: unknown dav1d color matrix %d.. ignoring, picture " + "may be very incorrect\n", dav1d_pic->seq_hdr->mtrx); + break; + } + + if (dav1d_pic->seq_hdr->color_range) { + image->repr.levels = PL_COLOR_LEVELS_PC; + } else { + image->repr.levels = PL_COLOR_LEVELS_TV; + } + + switch (dav1d_pic->seq_hdr->chr) { + case DAV1D_CHR_UNKNOWN: chroma_loc = PL_CHROMA_UNKNOWN; break; + case DAV1D_CHR_VERTICAL: chroma_loc = PL_CHROMA_LEFT; break; + case DAV1D_CHR_COLOCATED: chroma_loc = PL_CHROMA_TOP_LEFT; break; + } + +#if PL_API_VER >= 63 + if (settings->gpugrain && dav1d_pic->frame_hdr->film_grain.present) { + Dav1dFilmGrainData *src = &dav1d_pic->frame_hdr->film_grain.data; + struct pl_av1_grain_data *dst = &image->av1_grain; + *dst = (struct pl_av1_grain_data) { + .grain_seed = src->seed, + .num_points_y = src->num_y_points, + .chroma_scaling_from_luma = src->chroma_scaling_from_luma, + .num_points_uv = { src->num_uv_points[0], src->num_uv_points[1] }, + .scaling_shift = src->scaling_shift, + .ar_coeff_lag = src->ar_coeff_lag, + .ar_coeff_shift = (int)src->ar_coeff_shift, + .grain_scale_shift = src->grain_scale_shift, + .uv_mult = { src->uv_mult[0], src->uv_mult[1] }, + .uv_mult_luma = { src->uv_luma_mult[0], src->uv_luma_mult[1] }, + .uv_offset = { src->uv_offset[0], src->uv_offset[1] }, + .overlap = src->overlap_flag, + }; + + assert(sizeof(dst->points_y) == sizeof(src->y_points)); + assert(sizeof(dst->points_uv) == sizeof(src->uv_points)); + assert(sizeof(dst->ar_coeffs_y) == sizeof(src->ar_coeffs_y)); + memcpy(dst->points_y, src->y_points, sizeof(src->y_points)); + memcpy(dst->points_uv, src->uv_points, sizeof(src->uv_points)); + memcpy(dst->ar_coeffs_y, src->ar_coeffs_y, sizeof(src->ar_coeffs_y)); + + // this one has different row sizes for alignment + for (int c = 0; c < 2; c++) { + for (int i = 0; i < 25; i++) + dst->ar_coeffs_uv[c][i] = src->ar_coeffs_uv[c][i]; + } + } +#endif + + // Upload the actual planes + struct pl_plane_data data[3] = { + { + // Y plane + .type = PL_FMT_UNORM, + .width = width, + .height = height, + .pixel_stride = bytes, + .row_stride = dav1d_pic->stride[0], + .component_size = {bytes * 8}, + .component_map = {0}, + }, { + // U plane + .type = PL_FMT_UNORM, + .width = width >> sub_x, + .height = height >> sub_y, + .pixel_stride = bytes, + .row_stride = dav1d_pic->stride[1], + .component_size = {bytes * 8}, + .component_map = {1}, + }, { + // V plane + .type = PL_FMT_UNORM, + .width = width >> sub_x, + .height = height >> sub_y, + .pixel_stride = bytes, + .row_stride = dav1d_pic->stride[1], + .component_size = {bytes * 8}, + .component_map = {2}, + }, + }; + + bool ok = true; + + for (int i = 0; i < image->num_planes; i++) { + if (settings->zerocopy) { + const struct pl_buf *buf = dav1d_pic->allocator_data; + assert(buf); + data[i].buf = buf; + data[i].buf_offset = (uintptr_t) dav1d_pic->data[i] - (uintptr_t) buf->data; + } else { + data[i].pixels = dav1d_pic->data[i]; + } + + ok &= pl_upload_plane(rd_priv_ctx->gpu, &image->planes[i], &rd_priv_ctx->plane_tex[i], &data[i]); + } + + // Apply the correct chroma plane shift. This has to be done after pl_upload_plane +#if PL_API_VER >= 67 + pl_image_set_chroma_location(image, chroma_loc); +#else + pl_chroma_location_offset(chroma_loc, &image->planes[1].shift_x, &image->planes[1].shift_y); + pl_chroma_location_offset(chroma_loc, &image->planes[2].shift_x, &image->planes[2].shift_y); +#endif + + if (!ok) { + fprintf(stderr, "Failed uploading planes!\n"); + *image = (struct pl_image) {0}; + } + + SDL_UnlockMutex(rd_priv_ctx->lock); + return !ok; +} + +// Align to power of 2 +#define ALIGN2(x, align) (((x) + (align) - 1) & ~((align) - 1)) + +static int placebo_alloc_pic(Dav1dPicture *const p, void *cookie) +{ + Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie; + assert(rd_priv_ctx != NULL); + SDL_LockMutex(rd_priv_ctx->lock); + + const struct pl_gpu *gpu = rd_priv_ctx->gpu; + int ret = DAV1D_ERR(ENOMEM); + + // Copied from dav1d_default_picture_alloc + const int hbd = p->p.bpc > 8; + const int aligned_w = ALIGN2(p->p.w, 128); + const int aligned_h = ALIGN2(p->p.h, 128); + const int has_chroma = p->p.layout != DAV1D_PIXEL_LAYOUT_I400; + const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420; + const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444; + p->stride[0] = aligned_w << hbd; + p->stride[1] = has_chroma ? (aligned_w >> ss_hor) << hbd : 0; + + // Align strides up to multiples of the GPU performance hints + p->stride[0] = ALIGN2(p->stride[0], gpu->limits.align_tex_xfer_stride); + p->stride[1] = ALIGN2(p->stride[1], gpu->limits.align_tex_xfer_stride); + + // Aligning offsets to 4 also implicity aligns to the texel size (1 or 2) + size_t off_align = ALIGN2(gpu->limits.align_tex_xfer_offset, 4); + const size_t y_sz = ALIGN2(p->stride[0] * aligned_h, off_align); + const size_t uv_sz = ALIGN2(p->stride[1] * (aligned_h >> ss_ver), off_align); + + // The extra DAV1D_PICTURE_ALIGNMENTs are to brute force plane alignment, + // even in the case that the driver gives us insane alignments + const size_t pic_size = y_sz + 2 * uv_sz; + const size_t total_size = pic_size + DAV1D_PICTURE_ALIGNMENT * 4; + + // Validate size limitations + if (total_size > gpu->limits.max_xfer_size) { + printf("alloc of %zu bytes exceeds limits\n", total_size); + goto err; + } + + const struct pl_buf *buf = pl_buf_create(gpu, &(struct pl_buf_params) { + .type = PL_BUF_TEX_TRANSFER, + .host_mapped = true, + .size = total_size, + .memory_type = PL_BUF_MEM_HOST, + .user_data = p, + }); + + if (!buf) { + printf("alloc of GPU mapped buffer failed\n"); + goto err; + } + + assert(buf->data); + uintptr_t base = (uintptr_t) buf->data, data[3]; + data[0] = ALIGN2(base, DAV1D_PICTURE_ALIGNMENT); + data[1] = ALIGN2(data[0] + y_sz, DAV1D_PICTURE_ALIGNMENT); + data[2] = ALIGN2(data[1] + uv_sz, DAV1D_PICTURE_ALIGNMENT); + + // Sanity check offset alignment for the sake of debugging + if (data[0] - base != ALIGN2(data[0] - base, off_align) || + data[1] - base != ALIGN2(data[1] - base, off_align) || + data[2] - base != ALIGN2(data[2] - base, off_align)) + { + printf("GPU buffer horribly misaligned, expect slowdown!\n"); + } + + p->allocator_data = (void *) buf; + p->data[0] = (void *) data[0]; + p->data[1] = (void *) data[1]; + p->data[2] = (void *) data[2]; + ret = 0; + + // fall through +err: + SDL_UnlockMutex(rd_priv_ctx->lock); + return ret; +} + +static void placebo_release_pic(Dav1dPicture *pic, void *cookie) +{ + Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie; + assert(rd_priv_ctx != NULL); + assert(pic->allocator_data); + + SDL_LockMutex(rd_priv_ctx->lock); + const struct pl_gpu *gpu = rd_priv_ctx->gpu; + pl_buf_destroy(gpu, (const struct pl_buf **) &pic->allocator_data); + SDL_UnlockMutex(rd_priv_ctx->lock); +} + +#ifdef HAVE_PLACEBO_VULKAN +const Dav1dPlayRenderInfo rdr_placebo_vk = { + .name = "placebo-vk", + .create_renderer = placebo_renderer_create_vk, + .destroy_renderer = placebo_renderer_destroy, + .render = placebo_render, + .update_frame = placebo_upload_image, + .alloc_pic = placebo_alloc_pic, + .release_pic = placebo_release_pic, + +# if PL_API_VER >= 63 + .supports_gpu_grain = 1, +# endif +}; +#else +const Dav1dPlayRenderInfo rdr_placebo_vk = { NULL }; +#endif + +#ifdef HAVE_PLACEBO_OPENGL +const Dav1dPlayRenderInfo rdr_placebo_gl = { + .name = "placebo-gl", + .create_renderer = placebo_renderer_create_gl, + .destroy_renderer = placebo_renderer_destroy, + .render = placebo_render, + .update_frame = placebo_upload_image, + .alloc_pic = placebo_alloc_pic, + .release_pic = placebo_release_pic, + +# if PL_API_VER >= 63 + .supports_gpu_grain = 1, +# endif +}; +#else +const Dav1dPlayRenderInfo rdr_placebo_gl = { NULL }; +#endif + +#else +const Dav1dPlayRenderInfo rdr_placebo_vk = { NULL }; +const Dav1dPlayRenderInfo rdr_placebo_gl = { NULL }; +#endif diff --git a/third_party/dav1d/examples/dp_renderer_sdl.c b/third_party/dav1d/examples/dp_renderer_sdl.c new file mode 100644 index 0000000000..078d613492 --- /dev/null +++ b/third_party/dav1d/examples/dp_renderer_sdl.c @@ -0,0 +1,164 @@ +/* + * Copyright © 2020, 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 "dp_renderer.h" + +#include <assert.h> + +/** + * Renderer context for SDL + */ +typedef struct renderer_priv_ctx +{ + // SDL window + SDL_Window *win; + // SDL renderer + SDL_Renderer *renderer; + // Lock protecting access to the texture + SDL_mutex *lock; + // Texture to render + SDL_Texture *tex; +} Dav1dPlayRendererPrivateContext; + +static void *sdl_renderer_create() +{ + SDL_Window *win = dp_create_sdl_window(0); + if (win == NULL) + return NULL; + + // Alloc + Dav1dPlayRendererPrivateContext *rd_priv_ctx = malloc(sizeof(Dav1dPlayRendererPrivateContext)); + if (rd_priv_ctx == NULL) { + return NULL; + } + rd_priv_ctx->win = win; + + // Create renderer + rd_priv_ctx->renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); + // Set scale quality + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); + + // Create Mutex + rd_priv_ctx->lock = SDL_CreateMutex(); + if (rd_priv_ctx->lock == NULL) { + fprintf(stderr, "SDL_CreateMutex failed: %s\n", SDL_GetError()); + free(rd_priv_ctx); + return NULL; + } + + rd_priv_ctx->tex = NULL; + + return rd_priv_ctx; +} + +static void sdl_renderer_destroy(void *cookie) +{ + Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie; + assert(rd_priv_ctx != NULL); + + SDL_DestroyRenderer(rd_priv_ctx->renderer); + SDL_DestroyMutex(rd_priv_ctx->lock); + free(rd_priv_ctx); +} + +static void sdl_render(void *cookie, const Dav1dPlaySettings *settings) +{ + Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie; + assert(rd_priv_ctx != NULL); + + SDL_LockMutex(rd_priv_ctx->lock); + + if (rd_priv_ctx->tex == NULL) { + SDL_UnlockMutex(rd_priv_ctx->lock); + return; + } + + // Display the frame + SDL_RenderClear(rd_priv_ctx->renderer); + SDL_RenderCopy(rd_priv_ctx->renderer, rd_priv_ctx->tex, NULL, NULL); + SDL_RenderPresent(rd_priv_ctx->renderer); + + SDL_UnlockMutex(rd_priv_ctx->lock); +} + +static int sdl_update_texture(void *cookie, Dav1dPicture *dav1d_pic, + const Dav1dPlaySettings *settings) +{ + Dav1dPlayRendererPrivateContext *rd_priv_ctx = cookie; + assert(rd_priv_ctx != NULL); + + SDL_LockMutex(rd_priv_ctx->lock); + + if (dav1d_pic == NULL) { + rd_priv_ctx->tex = NULL; + SDL_UnlockMutex(rd_priv_ctx->lock); + return 0; + } + + int width = dav1d_pic->p.w; + int height = dav1d_pic->p.h; + int tex_w = width; + int tex_h = height; + + enum Dav1dPixelLayout dav1d_layout = dav1d_pic->p.layout; + + if (DAV1D_PIXEL_LAYOUT_I420 != dav1d_layout || dav1d_pic->p.bpc != 8) { + fprintf(stderr, "Unsupported pixel format, only 8bit 420 supported so far.\n"); + exit(50); + } + + SDL_Texture *texture = rd_priv_ctx->tex; + if (texture != NULL) { + SDL_QueryTexture(texture, NULL, NULL, &tex_w, &tex_h); + if (tex_w != width || tex_h != height) { + SDL_DestroyTexture(texture); + texture = NULL; + } + } + + if (texture == NULL) { + texture = SDL_CreateTexture(rd_priv_ctx->renderer, SDL_PIXELFORMAT_IYUV, + SDL_TEXTUREACCESS_STREAMING, width, height); + } + + SDL_UpdateYUVTexture(texture, NULL, + dav1d_pic->data[0], (int)dav1d_pic->stride[0], // Y + dav1d_pic->data[1], (int)dav1d_pic->stride[1], // U + dav1d_pic->data[2], (int)dav1d_pic->stride[1] // V + ); + + rd_priv_ctx->tex = texture; + SDL_UnlockMutex(rd_priv_ctx->lock); + return 0; +} + +const Dav1dPlayRenderInfo rdr_sdl = { + .name = "sdl", + .create_renderer = sdl_renderer_create, + .destroy_renderer = sdl_renderer_destroy, + .render = sdl_render, + .update_frame = sdl_update_texture +}; diff --git a/third_party/dav1d/examples/meson.build b/third_party/dav1d/examples/meson.build new file mode 100644 index 0000000000..50e097a8df --- /dev/null +++ b/third_party/dav1d/examples/meson.build @@ -0,0 +1,74 @@ +# 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. + +# +# Build definition for the dav1d examples +# + +# Leave subdir if examples are disabled +if not get_option('enable_examples') + subdir_done() +endif + + +# dav1d player sources +dav1dplay_sources = files( + 'dav1dplay.c', + 'dp_fifo.c', + 'dp_renderer_placebo.c', + 'dp_renderer_sdl.c', +) + +sdl2_dependency = dependency('sdl2', version: '>= 2.0.1', required: true) + +if sdl2_dependency.found() + dav1dplay_deps = [sdl2_dependency] + dav1dplay_cflags = [] + + placebo_dependency = dependency('libplacebo', version: '>= 1.18.0', required: false) + + if placebo_dependency.found() + dav1dplay_deps += placebo_dependency + dav1dplay_cflags += '-DHAVE_PLACEBO' + + # If libplacebo is found, we might be able to use Vulkan + # with it, in which case we need the Vulkan library too. + vulkan_dependency = dependency('vulkan', required: false) + if vulkan_dependency.found() + dav1dplay_deps += vulkan_dependency + dav1dplay_cflags += '-DHAVE_VULKAN' + endif + endif + + dav1dplay = executable('dav1dplay', + dav1dplay_sources, + rev_target, + + link_with : [libdav1d, dav1d_input_objs], + include_directories : [dav1d_inc_dirs], + dependencies : [getopt_dependency, dav1dplay_deps], + install : true, + c_args : dav1dplay_cflags, + ) +endif |