UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
hwaccel_videotoolbox.c
Go to the documentation of this file.
1
6/*
7 * Copyright (c) 2021-2023 CESNET z.s.p.o.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, is permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * 3. Neither the name of CESNET nor the names of its contributors may be
22 * used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
27 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
29 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifdef HAVE_CONFIG_H
40#include "config.h"
41#endif
42#include "config_unix.h"
43#include "config_win32.h"
44
45#include "compat/qsort_s.h"
46#include "debug.h"
48
49#include <libavutil/pixdesc.h>
50
51#define MOD_NAME "[videotoolbox dec.] "
52
53#ifdef QSORT_S_COMP_FIRST
54static int compare(void *ctx, const void *a, const void *b) {
55#else
56static int compare(const void *a, const void *b, void *ctx) {
57#endif
58 int bits_per_comp = ((const AVPixFmtDescriptor *) ctx)->comp[0].depth;
59 int deptha = av_pix_fmt_desc_get(*(const enum AVPixelFormat*) a)->comp[0].depth;
60 int depthb = av_pix_fmt_desc_get(*(const enum AVPixelFormat*) b)->comp[0].depth;
61 if (deptha != depthb) {
62 // either a or b is lower than bits_per_comp - sort higher bit depth first
63 if (deptha < bits_per_comp || depthb < bits_per_comp) {
64 return depthb - deptha;
65 }
66 // both are equal or higher - sort lower bit depth first
67 return deptha - depthb;
68 }
70 return 0;
71}
72
73static void sort_codecs(enum AVPixelFormat sw_format, int count, enum AVPixelFormat *formats) {
74#pragma clang diagnostic push
75#pragma clang diagnostic ignored "-Wcast-qual"
76 AVPixFmtDescriptor *sw_fmt_desc = (AVPixFmtDescriptor *) av_pix_fmt_desc_get(sw_format);
77#pragma clang diagnostic pop
78 qsort_s(formats, count, sizeof(enum AVPixelFormat), compare, sw_fmt_desc);
79}
80
81int videotoolbox_init(struct AVCodecContext *s,
82 struct hw_accel_state *state,
83 codec_t out_codec)
84{
85 (void) out_codec;
86 AVBufferRef *device_ref = NULL;
87 int ret = create_hw_device_ctx(AV_HWDEVICE_TYPE_VIDEOTOOLBOX, &device_ref);
88 if(ret < 0)
89 return ret;
90
91 enum AVPixelFormat probe_formats[] = { s->sw_pix_fmt, AV_PIX_FMT_UYVY422,
92#if P210_PRESENT
93 AV_PIX_FMT_P210LE,
94#endif // P210_PRESENT
95 AV_PIX_FMT_AYUV64,
96 };
97 sort_codecs(s->sw_pix_fmt, sizeof probe_formats / sizeof probe_formats[0] - 1, probe_formats + 1);
98
99 AVBufferRef *hw_frames_ctx = NULL;
100 for (unsigned i = 0; i < sizeof probe_formats / sizeof probe_formats[0]; ++i) {
101 log_msg(LOG_LEVEL_DEBUG, MOD_NAME "Trying SW pixel format: %s\n", av_get_pix_fmt_name(probe_formats[i]));
102 ret = create_hw_frame_ctx(device_ref,
103 s->coded_width,
104 s->coded_height,
105 AV_PIX_FMT_VIDEOTOOLBOX,
106 probe_formats[i],
107 0, //has to be 0, ffmpeg can't allocate frames by itself
108 &hw_frames_ctx);
109 if (ret >= 0) {
110 log_msg(LOG_LEVEL_VERBOSE, MOD_NAME "Successfully initialized with SW pixel format: %s\n",
111 av_get_pix_fmt_name(probe_formats[i]));
112 break;
113 }
114 }
115
116 if(ret < 0)
117 goto fail;
118
119 AVFrame *frame = av_frame_alloc();
120 if(!frame){
121 ret = -1;
122 goto fail;
123 }
124
126 state->copy = true;
127 state->tmp_frame = frame;
128
129 s->hw_frames_ctx = hw_frames_ctx;
130 s->hw_device_ctx = device_ref;
131
132 return 0;
133
134fail:
135 av_frame_free(&state->tmp_frame);
136 av_buffer_unref(&device_ref);
137 av_buffer_unref(&hw_frames_ctx);
138 return ret;
139}
void ** state
struct video_frame * frame
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_VERBOSE
Definition debug.h:63
int count
Definition hd-rum.c:44
static void sort_codecs(enum AVPixelFormat sw_format, int count, enum AVPixelFormat *formats)
static int compare(const void *a, const void *b, void *ctx)
int videotoolbox_init(struct AVCodecContext *s, struct hw_accel_state *state, codec_t out_codec)
#define MOD_NAME
#define LOG_LEVEL_DEBUG
Definition kernels.cu:48
#define qsort_s
Definition qsort_s.h:63
@ HWACCEL_VIDEOTOOLBOX
Definition types.h:118
codec_t
Definition types.h:65