| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg 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 | * FFmpeg 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 FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include "libavutil/buffer.h" | ||
| 20 | #include "libavutil/hwcontext.h" | ||
| 21 | #include "libavutil/log.h" | ||
| 22 | #include "libavutil/opt.h" | ||
| 23 | #include "libavutil/pixdesc.h" | ||
| 24 | |||
| 25 | #include "avfilter.h" | ||
| 26 | #include "filters.h" | ||
| 27 | #include "formats.h" | ||
| 28 | #include "video.h" | ||
| 29 | |||
| 30 | typedef struct HWMapContext { | ||
| 31 | const AVClass *class; | ||
| 32 | |||
| 33 | AVBufferRef *hwframes_ref; | ||
| 34 | |||
| 35 | int mode; | ||
| 36 | char *derive_device_type; | ||
| 37 | int reverse; | ||
| 38 | } HWMapContext; | ||
| 39 | |||
| 40 | ✗ | static int hwmap_query_formats(const AVFilterContext *avctx, | |
| 41 | AVFilterFormatsConfig **cfg_in, | ||
| 42 | AVFilterFormatsConfig **cfg_out) | ||
| 43 | { | ||
| 44 | int ret; | ||
| 45 | |||
| 46 | ✗ | if ((ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO), | |
| 47 | ✗ | &cfg_in[0]->formats)) < 0 || | |
| 48 | ✗ | (ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO), | |
| 49 | ✗ | &cfg_out[0]->formats)) < 0) | |
| 50 | ✗ | return ret; | |
| 51 | |||
| 52 | ✗ | return 0; | |
| 53 | } | ||
| 54 | |||
| 55 | ✗ | static int hwmap_config_output(AVFilterLink *outlink) | |
| 56 | { | ||
| 57 | ✗ | FilterLink *outl = ff_filter_link(outlink); | |
| 58 | ✗ | AVFilterContext *avctx = outlink->src; | |
| 59 | ✗ | HWMapContext *ctx = avctx->priv; | |
| 60 | ✗ | AVFilterLink *inlink = avctx->inputs[0]; | |
| 61 | ✗ | FilterLink *inl = ff_filter_link(inlink); | |
| 62 | AVHWFramesContext *hwfc; | ||
| 63 | AVBufferRef *device; | ||
| 64 | const AVPixFmtDescriptor *desc; | ||
| 65 | int err, device_is_derived; | ||
| 66 | |||
| 67 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Configure hwmap %s -> %s.\n", | |
| 68 | ✗ | av_get_pix_fmt_name(inlink->format), | |
| 69 | ✗ | av_get_pix_fmt_name(outlink->format)); | |
| 70 | |||
| 71 | ✗ | av_buffer_unref(&ctx->hwframes_ref); | |
| 72 | |||
| 73 | ✗ | device = avctx->hw_device_ctx; | |
| 74 | ✗ | device_is_derived = 0; | |
| 75 | |||
| 76 | ✗ | if (inl->hw_frames_ctx) { | |
| 77 | ✗ | hwfc = (AVHWFramesContext*)inl->hw_frames_ctx->data; | |
| 78 | |||
| 79 | ✗ | if (ctx->derive_device_type) { | |
| 80 | enum AVHWDeviceType type; | ||
| 81 | |||
| 82 | ✗ | type = av_hwdevice_find_type_by_name(ctx->derive_device_type); | |
| 83 | ✗ | if (type == AV_HWDEVICE_TYPE_NONE) { | |
| 84 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid device type.\n"); | |
| 85 | ✗ | err = AVERROR(EINVAL); | |
| 86 | ✗ | goto fail; | |
| 87 | } | ||
| 88 | |||
| 89 | ✗ | err = av_hwdevice_ctx_create_derived(&device, type, | |
| 90 | hwfc->device_ref, 0); | ||
| 91 | ✗ | if (err < 0) { | |
| 92 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to created derived " | |
| 93 | "device context: %d.\n", err); | ||
| 94 | ✗ | goto fail; | |
| 95 | } | ||
| 96 | ✗ | device_is_derived = 1; | |
| 97 | } | ||
| 98 | |||
| 99 | ✗ | desc = av_pix_fmt_desc_get(outlink->format); | |
| 100 | ✗ | if (!desc) { | |
| 101 | ✗ | err = AVERROR(EINVAL); | |
| 102 | ✗ | goto fail; | |
| 103 | } | ||
| 104 | |||
| 105 | ✗ | if (inlink->format == hwfc->format && | |
| 106 | ✗ | (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) && | |
| 107 | ✗ | !ctx->reverse) { | |
| 108 | // Map between two hardware formats (including the case of | ||
| 109 | // undoing an existing mapping). | ||
| 110 | |||
| 111 | ✗ | if (!device) { | |
| 112 | ✗ | av_log(avctx, AV_LOG_ERROR, "A device reference is " | |
| 113 | "required to map to a hardware format.\n"); | ||
| 114 | ✗ | err = AVERROR(EINVAL); | |
| 115 | ✗ | goto fail; | |
| 116 | } | ||
| 117 | |||
| 118 | ✗ | err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref, | |
| 119 | ✗ | outlink->format, | |
| 120 | device, | ||
| 121 | inl->hw_frames_ctx, | ||
| 122 | ctx->mode); | ||
| 123 | ✗ | if (err < 0) { | |
| 124 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to create derived " | |
| 125 | "frames context: %d.\n", err); | ||
| 126 | ✗ | goto fail; | |
| 127 | } | ||
| 128 | |||
| 129 | ✗ | } else if (inlink->format == hwfc->format && | |
| 130 | ✗ | (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) && | |
| 131 | ✗ | ctx->reverse) { | |
| 132 | // Map between two hardware formats, but do it in reverse. | ||
| 133 | // Make a new hwframe context for the target type, and then | ||
| 134 | // overwrite the input hwframe context with a derived context | ||
| 135 | // mapped from that back to the source type. | ||
| 136 | AVBufferRef *source; | ||
| 137 | AVHWFramesContext *frames; | ||
| 138 | |||
| 139 | ✗ | ctx->hwframes_ref = av_hwframe_ctx_alloc(device); | |
| 140 | ✗ | if (!ctx->hwframes_ref) { | |
| 141 | ✗ | err = AVERROR(ENOMEM); | |
| 142 | ✗ | goto fail; | |
| 143 | } | ||
| 144 | ✗ | frames = (AVHWFramesContext*)ctx->hwframes_ref->data; | |
| 145 | |||
| 146 | ✗ | frames->format = outlink->format; | |
| 147 | ✗ | frames->sw_format = hwfc->sw_format; | |
| 148 | ✗ | frames->width = hwfc->width; | |
| 149 | ✗ | frames->height = hwfc->height; | |
| 150 | |||
| 151 | ✗ | if (avctx->extra_hw_frames >= 0) | |
| 152 | ✗ | frames->initial_pool_size = 2 + avctx->extra_hw_frames; | |
| 153 | |||
| 154 | ✗ | err = av_hwframe_ctx_init(ctx->hwframes_ref); | |
| 155 | ✗ | if (err < 0) { | |
| 156 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to initialise " | |
| 157 | "target frames context: %d.\n", err); | ||
| 158 | ✗ | goto fail; | |
| 159 | } | ||
| 160 | |||
| 161 | ✗ | err = av_hwframe_ctx_create_derived(&source, | |
| 162 | ✗ | inlink->format, | |
| 163 | hwfc->device_ref, | ||
| 164 | ctx->hwframes_ref, | ||
| 165 | ctx->mode); | ||
| 166 | ✗ | if (err < 0) { | |
| 167 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to create " | |
| 168 | "derived source frames context: %d.\n", err); | ||
| 169 | ✗ | goto fail; | |
| 170 | } | ||
| 171 | |||
| 172 | // Here is the naughty bit. This overwriting changes what | ||
| 173 | // ff_get_video_buffer() in the previous filter returns - | ||
| 174 | // it will now give a frame allocated here mapped back to | ||
| 175 | // the format it expects. If there were any additional | ||
| 176 | // constraints on the output frames there then this may | ||
| 177 | // break nastily. | ||
| 178 | ✗ | av_buffer_unref(&inl->hw_frames_ctx); | |
| 179 | ✗ | inl->hw_frames_ctx = source; | |
| 180 | |||
| 181 | ✗ | } else if ((outlink->format == hwfc->format && | |
| 182 | ✗ | inlink->format == hwfc->sw_format) || | |
| 183 | ✗ | inlink->format == hwfc->format) { | |
| 184 | // Map from a hardware format to a software format, or | ||
| 185 | // undo an existing such mapping. | ||
| 186 | |||
| 187 | ✗ | ctx->hwframes_ref = av_buffer_ref(inl->hw_frames_ctx); | |
| 188 | ✗ | if (!ctx->hwframes_ref) { | |
| 189 | ✗ | err = AVERROR(ENOMEM); | |
| 190 | ✗ | goto fail; | |
| 191 | } | ||
| 192 | |||
| 193 | } else { | ||
| 194 | // Non-matching formats - not supported. | ||
| 195 | |||
| 196 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unsupported formats for " | |
| 197 | "hwmap: from %s (%s) to %s.\n", | ||
| 198 | ✗ | av_get_pix_fmt_name(inlink->format), | |
| 199 | av_get_pix_fmt_name(hwfc->format), | ||
| 200 | ✗ | av_get_pix_fmt_name(outlink->format)); | |
| 201 | ✗ | err = AVERROR(EINVAL); | |
| 202 | ✗ | goto fail; | |
| 203 | } | ||
| 204 | ✗ | } else if (avctx->hw_device_ctx) { | |
| 205 | // Map from a software format to a hardware format. This | ||
| 206 | // creates a new hwframe context like hwupload, but then | ||
| 207 | // returns frames mapped from that to the previous link in | ||
| 208 | // order to fill them without an additional copy. | ||
| 209 | |||
| 210 | ✗ | if (!device) { | |
| 211 | ✗ | av_log(avctx, AV_LOG_ERROR, "A device reference is " | |
| 212 | "required to create new frames with reverse " | ||
| 213 | "mapping.\n"); | ||
| 214 | ✗ | err = AVERROR(EINVAL); | |
| 215 | ✗ | goto fail; | |
| 216 | } | ||
| 217 | |||
| 218 | ✗ | ctx->reverse = 1; | |
| 219 | |||
| 220 | ✗ | ctx->hwframes_ref = av_hwframe_ctx_alloc(device); | |
| 221 | ✗ | if (!ctx->hwframes_ref) { | |
| 222 | ✗ | err = AVERROR(ENOMEM); | |
| 223 | ✗ | goto fail; | |
| 224 | } | ||
| 225 | ✗ | hwfc = (AVHWFramesContext*)ctx->hwframes_ref->data; | |
| 226 | |||
| 227 | ✗ | hwfc->format = outlink->format; | |
| 228 | ✗ | hwfc->sw_format = inlink->format; | |
| 229 | ✗ | hwfc->width = inlink->w; | |
| 230 | ✗ | hwfc->height = inlink->h; | |
| 231 | |||
| 232 | ✗ | if (avctx->extra_hw_frames >= 0) | |
| 233 | ✗ | hwfc->initial_pool_size = 2 + avctx->extra_hw_frames; | |
| 234 | |||
| 235 | ✗ | err = av_hwframe_ctx_init(ctx->hwframes_ref); | |
| 236 | ✗ | if (err < 0) { | |
| 237 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to create frame " | |
| 238 | "context for reverse mapping: %d.\n", err); | ||
| 239 | ✗ | goto fail; | |
| 240 | } | ||
| 241 | |||
| 242 | } else { | ||
| 243 | ✗ | av_log(avctx, AV_LOG_ERROR, "Mapping requires a hardware " | |
| 244 | "context (a device, or frames on input).\n"); | ||
| 245 | ✗ | return AVERROR(EINVAL); | |
| 246 | } | ||
| 247 | |||
| 248 | ✗ | outl->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref); | |
| 249 | ✗ | if (!outl->hw_frames_ctx) { | |
| 250 | ✗ | err = AVERROR(ENOMEM); | |
| 251 | ✗ | goto fail; | |
| 252 | } | ||
| 253 | |||
| 254 | ✗ | outlink->w = inlink->w; | |
| 255 | ✗ | outlink->h = inlink->h; | |
| 256 | |||
| 257 | ✗ | if (device_is_derived) | |
| 258 | ✗ | av_buffer_unref(&device); | |
| 259 | ✗ | return 0; | |
| 260 | |||
| 261 | ✗ | fail: | |
| 262 | ✗ | if (device_is_derived) | |
| 263 | ✗ | av_buffer_unref(&device); | |
| 264 | ✗ | av_buffer_unref(&ctx->hwframes_ref); | |
| 265 | ✗ | return err; | |
| 266 | } | ||
| 267 | |||
| 268 | ✗ | static AVFrame *hwmap_get_buffer(AVFilterLink *inlink, int w, int h) | |
| 269 | { | ||
| 270 | ✗ | FilterLink *l = ff_filter_link(inlink); | |
| 271 | ✗ | AVFilterContext *avctx = inlink->dst; | |
| 272 | ✗ | AVFilterLink *outlink = avctx->outputs[0]; | |
| 273 | ✗ | HWMapContext *ctx = avctx->priv; | |
| 274 | |||
| 275 | ✗ | if (ctx->reverse && !l->hw_frames_ctx) { | |
| 276 | AVFrame *src, *dst; | ||
| 277 | int err; | ||
| 278 | |||
| 279 | ✗ | src = ff_get_video_buffer(outlink, w, h); | |
| 280 | ✗ | if (!src) { | |
| 281 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to allocate source " | |
| 282 | "frame for software mapping.\n"); | ||
| 283 | ✗ | return NULL; | |
| 284 | } | ||
| 285 | |||
| 286 | ✗ | dst = av_frame_alloc(); | |
| 287 | ✗ | if (!dst) { | |
| 288 | ✗ | av_frame_free(&src); | |
| 289 | ✗ | return NULL; | |
| 290 | } | ||
| 291 | |||
| 292 | ✗ | err = av_hwframe_map(dst, src, ctx->mode); | |
| 293 | ✗ | if (err) { | |
| 294 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to map frame to " | |
| 295 | "software: %d.\n", err); | ||
| 296 | ✗ | av_frame_free(&src); | |
| 297 | ✗ | av_frame_free(&dst); | |
| 298 | ✗ | return NULL; | |
| 299 | } | ||
| 300 | |||
| 301 | ✗ | av_frame_free(&src); | |
| 302 | ✗ | return dst; | |
| 303 | } else { | ||
| 304 | ✗ | return ff_default_get_video_buffer(inlink, w, h); | |
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | ✗ | static int hwmap_filter_frame(AVFilterLink *link, AVFrame *input) | |
| 309 | { | ||
| 310 | ✗ | AVFilterContext *avctx = link->dst; | |
| 311 | ✗ | AVFilterLink *outlink = avctx->outputs[0]; | |
| 312 | ✗ | HWMapContext *ctx = avctx->priv; | |
| 313 | ✗ | AVFrame *map = NULL; | |
| 314 | int err; | ||
| 315 | |||
| 316 | ✗ | av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n", | |
| 317 | ✗ | av_get_pix_fmt_name(input->format), | |
| 318 | ✗ | input->width, input->height, input->pts); | |
| 319 | |||
| 320 | ✗ | map = av_frame_alloc(); | |
| 321 | ✗ | if (!map) { | |
| 322 | ✗ | err = AVERROR(ENOMEM); | |
| 323 | ✗ | goto fail; | |
| 324 | } | ||
| 325 | |||
| 326 | ✗ | map->format = outlink->format; | |
| 327 | ✗ | map->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref); | |
| 328 | ✗ | if (!map->hw_frames_ctx) { | |
| 329 | ✗ | err = AVERROR(ENOMEM); | |
| 330 | ✗ | goto fail; | |
| 331 | } | ||
| 332 | |||
| 333 | ✗ | if (ctx->reverse && !input->hw_frames_ctx) { | |
| 334 | // If we mapped backwards from hardware to software, we need | ||
| 335 | // to attach the hardware frame context to the input frame to | ||
| 336 | // make the mapping visible to av_hwframe_map(). | ||
| 337 | ✗ | input->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref); | |
| 338 | ✗ | if (!input->hw_frames_ctx) { | |
| 339 | ✗ | err = AVERROR(ENOMEM); | |
| 340 | ✗ | goto fail; | |
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | ✗ | err = av_hwframe_map(map, input, ctx->mode); | |
| 345 | ✗ | if (err < 0) { | |
| 346 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to map frame: %d.\n", err); | |
| 347 | ✗ | goto fail; | |
| 348 | } | ||
| 349 | |||
| 350 | ✗ | err = av_frame_copy_props(map, input); | |
| 351 | ✗ | if (err < 0) | |
| 352 | ✗ | goto fail; | |
| 353 | |||
| 354 | ✗ | av_frame_free(&input); | |
| 355 | |||
| 356 | ✗ | av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n", | |
| 357 | ✗ | av_get_pix_fmt_name(map->format), | |
| 358 | ✗ | map->width, map->height, map->pts); | |
| 359 | |||
| 360 | ✗ | return ff_filter_frame(outlink, map); | |
| 361 | |||
| 362 | ✗ | fail: | |
| 363 | ✗ | av_frame_free(&input); | |
| 364 | ✗ | av_frame_free(&map); | |
| 365 | ✗ | return err; | |
| 366 | } | ||
| 367 | |||
| 368 | ✗ | static av_cold void hwmap_uninit(AVFilterContext *avctx) | |
| 369 | { | ||
| 370 | ✗ | HWMapContext *ctx = avctx->priv; | |
| 371 | |||
| 372 | ✗ | av_buffer_unref(&ctx->hwframes_ref); | |
| 373 | ✗ | } | |
| 374 | |||
| 375 | #define OFFSET(x) offsetof(HWMapContext, x) | ||
| 376 | #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM) | ||
| 377 | static const AVOption hwmap_options[] = { | ||
| 378 | { "mode", "Frame mapping mode", | ||
| 379 | OFFSET(mode), AV_OPT_TYPE_FLAGS, | ||
| 380 | { .i64 = AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE }, | ||
| 381 | 0, INT_MAX, FLAGS, .unit = "mode" }, | ||
| 382 | |||
| 383 | { "read", "Mapping should be readable", | ||
| 384 | 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_READ }, | ||
| 385 | INT_MIN, INT_MAX, FLAGS, .unit = "mode" }, | ||
| 386 | { "write", "Mapping should be writeable", | ||
| 387 | 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_WRITE }, | ||
| 388 | INT_MIN, INT_MAX, FLAGS, .unit = "mode" }, | ||
| 389 | { "overwrite", "Mapping will always overwrite the entire frame", | ||
| 390 | 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_OVERWRITE }, | ||
| 391 | INT_MIN, INT_MAX, FLAGS, .unit = "mode" }, | ||
| 392 | { "direct", "Mapping should not involve any copying", | ||
| 393 | 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT }, | ||
| 394 | INT_MIN, INT_MAX, FLAGS, .unit = "mode" }, | ||
| 395 | |||
| 396 | { "derive_device", "Derive a new device of this type", | ||
| 397 | OFFSET(derive_device_type), AV_OPT_TYPE_STRING, | ||
| 398 | { .str = NULL }, 0, 0, FLAGS }, | ||
| 399 | { "reverse", "Map in reverse (create and allocate in the sink)", | ||
| 400 | OFFSET(reverse), AV_OPT_TYPE_INT, | ||
| 401 | { .i64 = 0 }, 0, 1, FLAGS }, | ||
| 402 | |||
| 403 | { NULL } | ||
| 404 | }; | ||
| 405 | |||
| 406 | AVFILTER_DEFINE_CLASS(hwmap); | ||
| 407 | |||
| 408 | static const AVFilterPad hwmap_inputs[] = { | ||
| 409 | { | ||
| 410 | .name = "default", | ||
| 411 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 412 | .get_buffer.video = hwmap_get_buffer, | ||
| 413 | .filter_frame = hwmap_filter_frame, | ||
| 414 | }, | ||
| 415 | }; | ||
| 416 | |||
| 417 | static const AVFilterPad hwmap_outputs[] = { | ||
| 418 | { | ||
| 419 | .name = "default", | ||
| 420 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 421 | .config_props = hwmap_config_output, | ||
| 422 | }, | ||
| 423 | }; | ||
| 424 | |||
| 425 | const FFFilter ff_vf_hwmap = { | ||
| 426 | .p.name = "hwmap", | ||
| 427 | .p.description = NULL_IF_CONFIG_SMALL("Map hardware frames"), | ||
| 428 | .p.priv_class = &hwmap_class, | ||
| 429 | .p.flags = AVFILTER_FLAG_HWDEVICE, | ||
| 430 | .uninit = hwmap_uninit, | ||
| 431 | .priv_size = sizeof(HWMapContext), | ||
| 432 | FILTER_INPUTS(hwmap_inputs), | ||
| 433 | FILTER_OUTPUTS(hwmap_outputs), | ||
| 434 | FILTER_QUERY_FUNC2(hwmap_query_formats), | ||
| 435 | .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE, | ||
| 436 | }; | ||
| 437 |