History

avfilter: add metal utilities

Aman Karmani committed 4 years ago

Commit ecee6af1 parent edca1fa2 files · +108 −0Browse files

Showing the changes to libavfilter (2 of 2 files). Show all

Unified · Split

2 files
  1. libavfilter/metal/utils.h +35 −0
  2. libavfilter/metal/utils.m +73 −0
libavfilter/metal/utils.h+35−0
@@ -0,0 +1,35 @@
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+#ifndef AVFILTER_METAL_UTILS_H
20+#define AVFILTER_METAL_UTILS_H
21+
22+#include <Metal/Metal.h>
23+#include <CoreVideo/CoreVideo.h>
24+
25+void ff_metal_compute_encoder_dispatch(id<MTLDevice> device,
26+ id<MTLComputePipelineState> pipeline,
27+ id<MTLComputeCommandEncoder> encoder,
28+ NSUInteger width, NSUInteger height);
29+
30+CVMetalTextureRef ff_metal_texture_from_pixbuf(void *avclass,
31+ CVMetalTextureCacheRef textureCache,
32+ CVPixelBufferRef pixbuf,
33+ int plane,
34+ MTLPixelFormat format);
35+#endif /* AVFILTER_METAL_UTILS_H */
libavfilter/metal/utils.m+73−0
@@ -0,0 +1,73 @@
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/log.h"
20+#include <libavfilter/metal/utils.h>
21+
22+void ff_metal_compute_encoder_dispatch(id<MTLDevice> device,
23+ id<MTLComputePipelineState> pipeline,
24+ id<MTLComputeCommandEncoder> encoder,
25+ NSUInteger width, NSUInteger height)
26+{
27+ [encoder setComputePipelineState:pipeline];
28+ NSUInteger w = pipeline.threadExecutionWidth;
29+ NSUInteger h = pipeline.maxTotalThreadsPerThreadgroup / w;
30+ MTLSize threadsPerThreadgroup = MTLSizeMake(w, h, 1);
31+ BOOL fallback = YES;
32+ if (@available(macOS 10.15, iOS 11, tvOS 14.5, *)) {
33+ if ([device supportsFamily:MTLGPUFamilyCommon3]) {
34+ MTLSize threadsPerGrid = MTLSizeMake(width, height, 1);
35+ [encoder dispatchThreads:threadsPerGrid threadsPerThreadgroup:threadsPerThreadgroup];
36+ fallback = NO;
37+ }
38+ }
39+ if (fallback) {
40+ MTLSize threadgroups = MTLSizeMake((width + w - 1) / w,
41+ (height + h - 1) / h,
42+ 1);
43+ [encoder dispatchThreadgroups:threadgroups threadsPerThreadgroup:threadsPerThreadgroup];
44+ }
45+}
46+
47+CVMetalTextureRef ff_metal_texture_from_pixbuf(void *ctx,
48+ CVMetalTextureCacheRef textureCache,
49+ CVPixelBufferRef pixbuf,
50+ int plane,
51+ MTLPixelFormat format)
52+{
53+ CVMetalTextureRef tex = NULL;
54+ CVReturn ret;
55+
56+ ret = CVMetalTextureCacheCreateTextureFromImage(
57+ NULL,
58+ textureCache,
59+ pixbuf,
60+ NULL,
61+ format,
62+ CVPixelBufferGetWidthOfPlane(pixbuf, plane),
63+ CVPixelBufferGetHeightOfPlane(pixbuf, plane),
64+ plane,
65+ &tex
66+ );
67+ if (ret != kCVReturnSuccess) {
68+ av_log(ctx, AV_LOG_ERROR, "Failed to create CVMetalTexture from image: %d\n", ret);
69+ return NULL;
70+ }
71+
72+ return tex;
73+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t