Skip to content

Commit

Permalink
修复非scaletofill拉伸模式下,导致动态元素位置错乱的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyabin2 committed Sep 6, 2022
1 parent 3091515 commit 66ed646
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ use_frameworks!
platform :ios, '9.0'

target 'YYEVA_Example' do
pod 'YYEVA' , '1.0.4'
pod 'YYEVA' , '1.0.5'
end
5 changes: 1 addition & 4 deletions Example/YYEVA/YYEVAViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ - (void)viewDidLoad
textField.clearsOnBeginEditing = YES;
[toolBarView addSubview:textField];
self.textField = textField;

}


Expand All @@ -65,11 +64,9 @@ - (void)onClickNormalRenderBtn

YYEVAPlayer *player = [[YYEVAPlayer alloc] init];
player.delegate = self;
player.backgroundColor = [UIColor clearColor];
[self.view addSubview:player];
player.frame = [self playViewFrame];
[player play:file];

self.player = player;
}

Expand Down Expand Up @@ -105,7 +102,7 @@ - (void)onClickMaskRenderBtn

- (CGRect)playViewFrame
{
return CGRectMake(0, 150, 380, 422);
return CGRectMake(0, 150, 300, 400);
}


Expand Down
2 changes: 1 addition & 1 deletion YYEVA.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'YYEVA'
s.version = '1.0.4'
s.version = '1.0.5'
s.summary = "YYEVA 是一个能播放混合MP4的播放器"
s.description = <<-DESC
YYEVA 是一种在支持的静态MP4,动态插入元素的播放器解决方案,由 YY 团队主导开发;
Expand Down
6 changes: 4 additions & 2 deletions YYEVA/Classes/Render/YYEVAVideoEffectRender.m
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,10 @@ - (void)drawMergedAttachments:(NSArray<YYEVAEffectFrame *> *)merges
// vector_float4 positon; 4
// vector_float2 sourceTextureCoordinate; 2
// vector_float2 maskTextureCoordinate; 2
//
id<MTLBuffer> vertexBuffer = [mergeInfo vertexBufferWithContainerSize:size maskContianerSize:videoSize device:self.device];
//
id<MTLBuffer> vertexBuffer = [mergeInfo vertexBufferWithContainerSize:size
maskContianerSize:videoSize
device:self.device fillMode:self.fillMode trueSize:self.mtkView.bounds.size];
id<MTLBuffer> colorParamsBuffer = mergeInfo.src.colorParamsBuffer;
id<MTLBuffer> convertMatrix = self.convertMatrix;
if (!sourceTexture || !vertexBuffer || !convertMatrix) {
Expand Down
9 changes: 8 additions & 1 deletion YYEVA/Classes/model/YYEVAEffectInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign,readonly) CGRect outputFrame;
@property (nonatomic, assign,readonly) NSInteger effect_id;
@property (nonatomic, assign) YYEVAEffectSource *src;
- (id<MTLBuffer>)vertexBufferWithContainerSize:(CGSize)size maskContianerSize:(CGSize)mSize device:(id<MTLDevice>)device;
- (id<MTLBuffer>)vertexBufferWithContainerSize:(CGSize)size
maskContianerSize:(CGSize)mSize
device:(id<MTLDevice>)device;
- (id<MTLBuffer>)vertexBufferWithContainerSize:(CGSize)size
maskContianerSize:(CGSize)mSize
device:(id<MTLDevice>)device
fillMode:(YYEVAFillMode)fillMode
trueSize:(CGSize)trueSize;
@end

@interface YYEVAEffectSource : NSObject
Expand Down
8 changes: 5 additions & 3 deletions YYEVA/Classes/model/YYEVAEffectInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ - (instancetype)initWithDictionary:(NSDictionary *)dict
}
return self;
}


- (id<MTLBuffer>)vertexBufferWithContainerSize:(CGSize)size
maskContianerSize:(CGSize)mSize
device:(id<MTLDevice>)device
fillMode:(YYEVAFillMode)fillMode
trueSize:(CGSize)trueSize
{
if (size.width <= 0 || size.height <= 0 || mSize.width <= 0 || mSize.height <= 0) {
NSLog(@"--%@ - fail",NSStringFromSelector(_cmd));
Expand All @@ -62,7 +63,8 @@ - (instancetype)initWithDictionary:(NSDictionary *)dict
//4 + 2 + 2 = 8 * 4 = 32
const int colunmCountForVertices = 4, colunmCountForCoordinate = 2, vertexDataLength = 32;
float vertices[16], maskCoordinates[8];
normalVerticesWithFillMode(self.renderFrame, size, self.src.sourceImage.size,self.src.fillMode,vertices);
// normalVerticesWithFillMode(self.renderFrame, size, self.src.sourceImage.size,self.src.fillMode,vertices);
normalVerticesWithFillMode(self.renderFrame, size, self.src.sourceImage.size,self.src.fillMode,vertices,fillMode,trueSize);
textureCoordinateFromRect(self.outputFrame, mSize, maskCoordinates);
float sourceCoordinates[8] = {
0.0, 1.0,
Expand All @@ -88,7 +90,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)dict
id<MTLBuffer> vertexBuffer = [device newBufferWithBytes:vertexData length:allocationSize options:MTLResourceStorageModeShared];
return vertexBuffer;
}


@end

Expand Down
4 changes: 2 additions & 2 deletions YYEVA/Classes/utils/YSVideoMetalUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ NS_ASSUME_NONNULL_BEGIN

#ifdef __cplusplus
extern "C" {
#endif
extern void normalVerticesWithFillMode(CGRect rect, CGSize containerSize, CGSize picSize,YYEVAEffectSourceImageFillMode fillMode, float vertices[_Nullable 16]);
#endif
extern void normalVerticesWithFillMode(CGRect rect, CGSize containerSize, CGSize picSize,YYEVAEffectSourceImageFillMode fillMode, float vertices[16],YYEVAFillMode videoFillMode,CGSize trueSize);
extern void textureCoordinateFromRect(CGRect rect,CGSize containerSize,float coordinates[_Nullable 8]);

#ifdef __cplusplus
Expand Down
43 changes: 36 additions & 7 deletions YYEVA/Classes/utils/YSVideoMetalUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by guoyabin on 2021/7/15.
//
#import "YSVideoMetalUtils.h"

#import <AVFoundation/AVFoundation.h>
@import MetalKit;

matrix_float3x3 kColorConversion601FullRangeMatrix = (matrix_float3x3){
Expand Down Expand Up @@ -45,9 +45,9 @@ void generatorVertices(CGRect rect, CGSize containerSize, float vertices[16]) {
originX+width,originY-height,0.0, 1.0};
replaceArrayElements(vertices, tempVertices, 16);
}



void normalVerticesWithFillMode(CGRect rect, CGSize containerSize, CGSize picSize,YYEVAEffectSourceImageFillMode fillMode, float vertices[16]) {
void normalVerticesWithFillMode(CGRect rect, CGSize containerSize, CGSize picSize,YYEVAEffectSourceImageFillMode fillMode, float vertices[16],YYEVAFillMode videoFillMode,CGSize trueSize) {
if (picSize.width > 0 && picSize.height > 0) {
float picWidth = picSize.width;
float picHeight = picSize.height;
Expand Down Expand Up @@ -80,12 +80,39 @@ void normalVerticesWithFillMode(CGRect rect, CGSize containerSize, CGSize picSiz
}
}

//containerSize

//trueSize
float heightScaling = 1.0;
float widthScaling = 1.0;
CGSize drawableSize = trueSize;
CGRect bounds = CGRectMake(0, 0, drawableSize.width, drawableSize.height);
CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(containerSize, bounds);
switch (videoFillMode) {
case YYEVAContentMode_ScaleToFill:
heightScaling = 1.0;
widthScaling = 1.0;
break;

case YYEVAContentMode_ScaleAspectFit:
widthScaling = insetRect.size.width / drawableSize.width;
heightScaling = insetRect.size.height / drawableSize.height;
break;

case YYEVAContentMode_ScaleAspectFill:
widthScaling = drawableSize.height / insetRect.size.height;
heightScaling = drawableSize.width / insetRect.size.width;
break;
}

// rect
// rect = CGRectMake(rect.origin.x * widthScaling, rect.origin.y * heightScaling, rect.size.width, rect.size.height);

float originX, originY, width, height;
originX = -1+2*rect.origin.x/containerSize.width;
originY = 1-2*rect.origin.y/containerSize.height;
width = 2*rect.size.width/containerSize.width;
height = 2*rect.size.height/containerSize.height;
originX = (-1+2*rect.origin.x/containerSize.width) * widthScaling;
originY = (1-2*rect.origin.y/containerSize.height) * heightScaling;
width = (2*rect.size.width/containerSize.width) * widthScaling;
height = (2*rect.size.height/containerSize.height) * heightScaling;

float tempVertices[] = {
originX, originY,0.0, 1.0,
Expand All @@ -102,6 +129,8 @@ void textureCoordinateFromRect(CGRect rect,CGSize containerSize,float coordinate
originY = rect.origin.y/containerSize.height;
width = rect.size.width/containerSize.width;
height = rect.size.height/containerSize.height;



float tempCoordintes[] = {
originX, originY+height,//0,1
Expand Down

0 comments on commit 66ed646

Please sign in to comment.