hwcontext: initialize sw_format in av_hwframe_ctx_alloc()
[libav.git] / libavutil / hwcontext.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "config.h"
20
21 #include "buffer.h"
22 #include "common.h"
23 #include "hwcontext.h"
24 #include "hwcontext_internal.h"
25 #include "imgutils.h"
26 #include "log.h"
27 #include "mem.h"
28 #include "pixdesc.h"
29 #include "pixfmt.h"
30
31 static const HWContextType *hw_table[] = {
32 #if CONFIG_CUDA
33 &ff_hwcontext_type_cuda,
34 #endif
35 #if CONFIG_VAAPI
36 &ff_hwcontext_type_vaapi,
37 #endif
38 #if CONFIG_VDPAU
39 &ff_hwcontext_type_vdpau,
40 #endif
41 NULL,
42 };
43
44 static const AVClass hwdevice_ctx_class = {
45 .class_name = "AVHWDeviceContext",
46 .item_name = av_default_item_name,
47 .version = LIBAVUTIL_VERSION_INT,
48 };
49
50 static void hwdevice_ctx_free(void *opaque, uint8_t *data)
51 {
52 AVHWDeviceContext *ctx = (AVHWDeviceContext*)data;
53
54 /* uninit might still want access the hw context and the user
55 * free() callback might destroy it, so uninit has to be called first */
56 if (ctx->internal->hw_type->device_uninit)
57 ctx->internal->hw_type->device_uninit(ctx);
58
59 if (ctx->free)
60 ctx->free(ctx);
61
62 av_freep(&ctx->hwctx);
63 av_freep(&ctx->internal->priv);
64 av_freep(&ctx->internal);
65 av_freep(&ctx);
66 }
67
68 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
69 {
70 AVHWDeviceContext *ctx;
71 AVBufferRef *buf;
72 const HWContextType *hw_type = NULL;
73 int i;
74
75 for (i = 0; hw_table[i]; i++) {
76 if (hw_table[i]->type == type) {
77 hw_type = hw_table[i];
78 break;
79 }
80 }
81 if (!hw_type)
82 return NULL;
83
84 ctx = av_mallocz(sizeof(*ctx));
85 if (!ctx)
86 return NULL;
87
88 ctx->internal = av_mallocz(sizeof(*ctx->internal));
89 if (!ctx->internal)
90 goto fail;
91
92 if (hw_type->device_priv_size) {
93 ctx->internal->priv = av_mallocz(hw_type->device_priv_size);
94 if (!ctx->internal->priv)
95 goto fail;
96 }
97
98 if (hw_type->device_hwctx_size) {
99 ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
100 if (!ctx->hwctx)
101 goto fail;
102 }
103
104 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
105 hwdevice_ctx_free, NULL,
106 AV_BUFFER_FLAG_READONLY);
107 if (!buf)
108 goto fail;
109
110 ctx->type = type;
111 ctx->av_class = &hwdevice_ctx_class;
112
113 ctx->internal->hw_type = hw_type;
114
115 return buf;
116
117 fail:
118 if (ctx->internal)
119 av_freep(&ctx->internal->priv);
120 av_freep(&ctx->internal);
121 av_freep(&ctx->hwctx);
122 av_freep(&ctx);
123 return NULL;
124 }
125
126 int av_hwdevice_ctx_init(AVBufferRef *ref)
127 {
128 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
129 int ret;
130
131 if (ctx->internal->hw_type->device_init) {
132 ret = ctx->internal->hw_type->device_init(ctx);
133 if (ret < 0)
134 goto fail;
135 }
136
137 return 0;
138 fail:
139 if (ctx->internal->hw_type->device_uninit)
140 ctx->internal->hw_type->device_uninit(ctx);
141 return ret;
142 }
143
144 static const AVClass hwframe_ctx_class = {
145 .class_name = "AVHWFramesContext",
146 .item_name = av_default_item_name,
147 .version = LIBAVUTIL_VERSION_INT,
148 };
149
150 static void hwframe_ctx_free(void *opaque, uint8_t *data)
151 {
152 AVHWFramesContext *ctx = (AVHWFramesContext*)data;
153
154 if (ctx->internal->pool_internal)
155 av_buffer_pool_uninit(&ctx->internal->pool_internal);
156
157 if (ctx->internal->hw_type->frames_uninit)
158 ctx->internal->hw_type->frames_uninit(ctx);
159
160 if (ctx->free)
161 ctx->free(ctx);
162
163 av_buffer_unref(&ctx->device_ref);
164
165 av_freep(&ctx->hwctx);
166 av_freep(&ctx->internal->priv);
167 av_freep(&ctx->internal);
168 av_freep(&ctx);
169 }
170
171 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
172 {
173 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)device_ref_in->data;
174 const HWContextType *hw_type = device_ctx->internal->hw_type;
175 AVHWFramesContext *ctx;
176 AVBufferRef *buf, *device_ref = NULL;;
177
178 ctx = av_mallocz(sizeof(*ctx));
179 if (!ctx)
180 return NULL;
181
182 ctx->internal = av_mallocz(sizeof(*ctx->internal));
183 if (!ctx->internal)
184 goto fail;
185
186 if (hw_type->frames_priv_size) {
187 ctx->internal->priv = av_mallocz(hw_type->frames_priv_size);
188 if (!ctx->internal->priv)
189 goto fail;
190 }
191
192 if (hw_type->frames_hwctx_size) {
193 ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
194 if (!ctx->hwctx)
195 goto fail;
196 }
197
198 device_ref = av_buffer_ref(device_ref_in);
199 if (!device_ref)
200 goto fail;
201
202 buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
203 hwframe_ctx_free, NULL,
204 AV_BUFFER_FLAG_READONLY);
205 if (!buf)
206 goto fail;
207
208 ctx->av_class = &hwframe_ctx_class;
209 ctx->device_ref = device_ref;
210 ctx->device_ctx = device_ctx;
211 ctx->format = AV_PIX_FMT_NONE;
212 ctx->sw_format = AV_PIX_FMT_NONE;
213
214 ctx->internal->hw_type = hw_type;
215
216 return buf;
217
218 fail:
219 if (device_ref)
220 av_buffer_unref(&device_ref);
221 if (ctx->internal)
222 av_freep(&ctx->internal->priv);
223 av_freep(&ctx->internal);
224 av_freep(&ctx->hwctx);
225 av_freep(&ctx);
226 return NULL;
227 }
228
229 static int hwframe_pool_prealloc(AVBufferRef *ref)
230 {
231 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
232 AVFrame **frames;
233 int i, ret = 0;
234
235 frames = av_mallocz_array(ctx->initial_pool_size, sizeof(*frames));
236 if (!frames)
237 return AVERROR(ENOMEM);
238
239 for (i = 0; i < ctx->initial_pool_size; i++) {
240 frames[i] = av_frame_alloc();
241 if (!frames[i])
242 goto fail;
243
244 ret = av_hwframe_get_buffer(ref, frames[i], 0);
245 if (ret < 0)
246 goto fail;
247 }
248
249 fail:
250 for (i = 0; i < ctx->initial_pool_size; i++)
251 av_frame_free(&frames[i]);
252 av_freep(&frames);
253
254 return ret;
255 }
256
257 int av_hwframe_ctx_init(AVBufferRef *ref)
258 {
259 AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
260 const enum AVPixelFormat *pix_fmt;
261 int ret;
262
263 /* validate the pixel format */
264 for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
265 if (*pix_fmt == ctx->format)
266 break;
267 }
268 if (*pix_fmt == AV_PIX_FMT_NONE) {
269 av_log(ctx, AV_LOG_ERROR,
270 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
271 av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name);
272 return AVERROR(ENOSYS);
273 }
274
275 /* validate the dimensions */
276 ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
277 if (ret < 0)
278 return ret;
279
280 /* format-specific init */
281 if (ctx->internal->hw_type->frames_init) {
282 ret = ctx->internal->hw_type->frames_init(ctx);
283 if (ret < 0)
284 goto fail;
285 }
286
287 if (ctx->internal->pool_internal && !ctx->pool)
288 ctx->pool = ctx->internal->pool_internal;
289
290 /* preallocate the frames in the pool, if requested */
291 if (ctx->initial_pool_size > 0) {
292 ret = hwframe_pool_prealloc(ref);
293 if (ret < 0)
294 goto fail;
295 }
296
297 return 0;
298 fail:
299 if (ctx->internal->hw_type->frames_uninit)
300 ctx->internal->hw_type->frames_uninit(ctx);
301 return ret;
302 }
303
304 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
305 enum AVHWFrameTransferDirection dir,
306 enum AVPixelFormat **formats, int flags)
307 {
308 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
309
310 if (!ctx->internal->hw_type->transfer_get_formats)
311 return AVERROR(ENOSYS);
312
313 return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats);
314 }
315
316 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
317 {
318 AVFrame *frame_tmp;
319 int ret = 0;
320
321 frame_tmp = av_frame_alloc();
322 if (!frame_tmp)
323 return AVERROR(ENOMEM);
324
325 /* if the format is set, use that
326 * otherwise pick the first supported one */
327 if (dst->format >= 0) {
328 frame_tmp->format = dst->format;
329 } else {
330 enum AVPixelFormat *formats;
331
332 ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
333 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
334 &formats, 0);
335 if (ret < 0)
336 goto fail;
337 frame_tmp->format = formats[0];
338 av_freep(&formats);
339 }
340 frame_tmp->width = src->width;
341 frame_tmp->height = src->height;
342
343 ret = av_frame_get_buffer(frame_tmp, 32);
344 if (ret < 0)
345 goto fail;
346
347 ret = av_hwframe_transfer_data(frame_tmp, src, flags);
348 if (ret < 0)
349 goto fail;
350
351 av_frame_move_ref(dst, frame_tmp);
352
353 fail:
354 av_frame_free(&frame_tmp);
355 return ret;
356 }
357
358 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
359 {
360 AVHWFramesContext *ctx;
361 int ret;
362
363 if (!dst->buf[0])
364 return transfer_data_alloc(dst, src, flags);
365
366 if (src->hw_frames_ctx) {
367 ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
368
369 ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src);
370 if (ret < 0)
371 return ret;
372 } else if (dst->hw_frames_ctx) {
373 ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data;
374
375 ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src);
376 if (ret < 0)
377 return ret;
378 } else
379 return AVERROR(ENOSYS);
380
381 return 0;
382 }
383
384 int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
385 {
386 AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
387 int ret;
388
389 if (!ctx->internal->hw_type->frames_get_buffer)
390 return AVERROR(ENOSYS);
391
392 if (!ctx->pool)
393 return AVERROR(EINVAL);
394
395 frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
396 if (!frame->hw_frames_ctx)
397 return AVERROR(ENOMEM);
398
399 ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame);
400 if (ret < 0) {
401 av_buffer_unref(&frame->hw_frames_ctx);
402 return ret;
403 }
404
405 return 0;
406 }
407
408 void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
409 {
410 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
411 const HWContextType *hw_type = ctx->internal->hw_type;
412
413 if (hw_type->device_hwconfig_size == 0)
414 return NULL;
415
416 return av_mallocz(hw_type->device_hwconfig_size);
417 }
418
419 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
420 const void *hwconfig)
421 {
422 AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data;
423 const HWContextType *hw_type = ctx->internal->hw_type;
424 AVHWFramesConstraints *constraints;
425
426 if (!hw_type->frames_get_constraints)
427 return NULL;
428
429 constraints = av_mallocz(sizeof(*constraints));
430 if (!constraints)
431 return NULL;
432
433 constraints->min_width = constraints->min_height = 0;
434 constraints->max_width = constraints->max_height = INT_MAX;
435
436 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
437 return constraints;
438 } else {
439 av_hwframe_constraints_free(&constraints);
440 return NULL;
441 }
442 }
443
444 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
445 {
446 if (*constraints) {
447 av_freep(&(*constraints)->valid_hw_formats);
448 av_freep(&(*constraints)->valid_sw_formats);
449 }
450 av_freep(constraints);
451 }