Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support of custom rotate degrees #71

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
VERSION_NAME=0.3.9
VERSION_NAME=0.3.9.1
VERSION_CODE=1
BUILD_TOOLS_VERSION=29.0.2
COMPILE_SDK_VERSION=29
Expand Down
13 changes: 13 additions & 0 deletions mp4compose/src/main/java/com/daasuu/mp4compose/FillMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,17 @@ public static float[] getScaleAspectCrop(int angle, int widthIn, int heightIn, i

return scale;
}

public static float[] getScaleAspectCustom(int angle, int widthIn, int heightIn, int widthOut, int heightOut) {
final float[] scale = {1, 1};

if (angle == 90 || angle == 270){
scale[1] = scale[0] / ((float) widthIn / (float) heightIn);
} else {
float ratio = (float) widthOut / (float) heightOut;
scale[0] = ratio;
scale[1] = ratio / ((float) widthIn / (float) heightIn);
}
return scale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;

/**
* Created by sudamasayuki2 on 2018/01/08.
Expand All @@ -24,6 +25,18 @@ public FillModeCustomItem(float scale, float rotate, float translateX, float tra
this.videoHeight = videoHeight;
}

public FillModeCustomItem(View view, float startWidthSize, float videoWidth, float videoHeight) {
int minSide = Math.min(view.getWidth(), view.getHeight());
int maxSide = Math.max(view.getWidth(), view.getHeight());

this.scale = view.getScaleX();
this.rotate = view.getRotation();
this.translateX = view.getTranslationX() / startWidthSize;
this.translateY = view.getTranslationY() / (startWidthSize / minSide * maxSide);
this.videoWidth = videoWidth;
this.videoHeight = videoHeight;
}

public float getScale() {
return scale;
}
Expand All @@ -40,6 +53,14 @@ public float getTranslateY() {
return translateY;
}

public float getTranslateX(float coef) {
return translateX * (coef * 2);
}

public float getTranslateY(float coef) {
return translateY * (coef * 2);
}

public float getVideoWidth() {
return videoWidth;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.daasuu.mp4compose.FillMode;
import com.daasuu.mp4compose.FillModeCustomItem;
import com.daasuu.mp4compose.Rotation;
import com.daasuu.mp4compose.filter.GlBackgroundFilter;
import com.daasuu.mp4compose.filter.GlFilter;
import com.daasuu.mp4compose.gl.GlFramebufferObject;
import com.daasuu.mp4compose.gl.GlPreviewFilter;
Expand Down Expand Up @@ -73,6 +74,8 @@ class DecoderSurface implements SurfaceTexture.OnFrameAvailableListener {
private float[] VMatrix = new float[16];
private float[] STMatrix = new float[16];

private float[] RotateProjMatrix = new float[16];
private float[] RotateMVPMatrix = new float[16];

private Rotation rotation = Rotation.NORMAL;
private Size outputResolution;
Expand All @@ -81,9 +84,11 @@ class DecoderSurface implements SurfaceTexture.OnFrameAvailableListener {
private FillModeCustomItem fillModeCustomItem;
private boolean flipVertical = false;
private boolean flipHorizontal = false;
private float outputRatioResolution;

private final Logger logger;

private static final int DEFAULT_TOP = 1;
/**
* Creates an DecoderSurface using the current EGL context (rather than establishing a
* new one). Creates a Surface that can be passed to MediaCodec.configure().
Expand Down Expand Up @@ -242,10 +247,14 @@ void drawImage() {
Matrix.multiplyMM(MVPMatrix, 0, VMatrix, 0, MMatrix, 0);
Matrix.multiplyMM(MVPMatrix, 0, ProjMatrix, 0, MVPMatrix, 0);

Matrix.multiplyMM(RotateMVPMatrix, 0, VMatrix, 0, MMatrix, 0);
Matrix.multiplyMM(RotateMVPMatrix, 0, RotateProjMatrix, 0, RotateMVPMatrix, 0);

float scaleDirectionX = flipHorizontal ? -1 : 1;
float scaleDirectionY = flipVertical ? -1 : 1;

float scale[];

switch (fillMode) {
case PRESERVE_ASPECT_FIT:
scale = FillMode.getScaleAspectFit(rotation.getRotation(), inputResolution.getWidth(), inputResolution.getHeight(), outputResolution.getWidth(), outputResolution.getHeight());
Expand All @@ -266,44 +275,31 @@ void drawImage() {
break;
case CUSTOM:
if (fillModeCustomItem != null) {
Matrix.translateM(MVPMatrix, 0, fillModeCustomItem.getTranslateX(), -fillModeCustomItem.getTranslateY(), 0f);
scale = FillMode.getScaleAspectCrop(rotation.getRotation(), inputResolution.getWidth(), inputResolution.getHeight(), outputResolution.getWidth(), outputResolution.getHeight());

if (fillModeCustomItem.getRotate() == 0 || fillModeCustomItem.getRotate() == 180) {
Matrix.scaleM(MVPMatrix,
0,
fillModeCustomItem.getScale() * scale[0] * scaleDirectionX,
fillModeCustomItem.getScale() * scale[1] * scaleDirectionY,
1);
} else {
Matrix.scaleM(MVPMatrix,
0,
fillModeCustomItem.getScale() * scale[0] * (1 / fillModeCustomItem.getVideoWidth() * fillModeCustomItem.getVideoHeight()) * scaleDirectionX,
fillModeCustomItem.getScale() * scale[1] * (fillModeCustomItem.getVideoWidth() / fillModeCustomItem.getVideoHeight()) * scaleDirectionY,
1);
}
scale = FillMode.getScaleAspectCustom(rotation.getRotation(), inputResolution.getWidth(), inputResolution.getHeight(), outputResolution.getWidth(), outputResolution.getHeight());

Matrix.rotateM(MVPMatrix, 0, -(rotation.getRotation() + fillModeCustomItem.getRotate()), 0.f, 0.f, 1.f);

// Log.d(TAG, "inputResolution = " + inputResolution.getWidth() + " height = " + inputResolution.getHeight());
// Log.d(TAG, "out = " + outputResolution.getWidth() + " height = " + outputResolution.getHeight());
// Log.d(TAG, "rotation = " + rotation.getRotation());
// Log.d(TAG, "scale[0] = " + scale[0] + " scale[1] = " + scale[1]);
float[] rotationMatrix = new float[16];
Matrix.setIdentityM(rotationMatrix, 0);
Matrix.setRotateM(rotationMatrix, 0, -(rotation.getRotation() + fillModeCustomItem.getRotate()), 0f, 0f, 1.0f);
Matrix.scaleM(rotationMatrix, 0, fillModeCustomItem.getScale() * scale[0], fillModeCustomItem.getScale() * scale[1], 1.0f);
Matrix.translateM(RotateMVPMatrix, 0, fillModeCustomItem.getTranslateX(outputRatioResolution), -fillModeCustomItem.getTranslateY(DEFAULT_TOP), 0.0f);


float[] result = new float[16];
Matrix.multiplyMM(result, 0, RotateMVPMatrix, 0, rotationMatrix, 0);
MVPMatrix = result;
}
default:
break;
}

if (filter != null && filter instanceof GlBackgroundFilter) {
drawFilter();
}

previewShader.draw(texName, MVPMatrix, STMatrix, 1f);

if (filter != null) {
// 一度shaderに描画したものを、fboを利用して、drawする。drawには必要なさげだけど。
framebufferObject.enable();
GLES20.glClear(GL_COLOR_BUFFER_BIT);
filter.draw(filterFramebufferObject.getTexName(), framebufferObject);
if (filter != null && !(filter instanceof GlBackgroundFilter)) {
drawFilter();
}


Expand All @@ -316,6 +312,13 @@ void drawImage() {
normalShader.draw(framebufferObject.getTexName(), null);
}

private void drawFilter() {
// 一度shaderに描画したものを、fboを利用して、drawする。drawには必要なさげだけど。
framebufferObject.enable();
GLES20.glClear(GL_COLOR_BUFFER_BIT);
filter.draw(filterFramebufferObject.getTexName(), framebufferObject);
}

@Override
public void onFrameAvailable(SurfaceTexture st) {
if (VERBOSE) logger.debug(TAG, "new frame available");
Expand Down Expand Up @@ -373,5 +376,7 @@ void completeParams() {
filter.setFrameSize(width, height);
}

outputRatioResolution = (float) width / (float) height;
Matrix.frustumM(RotateProjMatrix, 0, -outputRatioResolution, outputRatioResolution,-DEFAULT_TOP, DEFAULT_TOP, 5, 7);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,12 @@ private static MediaFormat createVideoOutputFormatWithAvailableEncoders(@NonNull
return mp4vesMediaFormat;
}

return createVideoFormat(VideoFormatMimeType.H263.getFormat(), bitrate, outputResolution);
final MediaFormat h263MediaFormat = createVideoFormat(VideoFormatMimeType.H263.getFormat(), bitrate, outputResolution);
if (mediaCodecList.findEncoderForFormat(h263MediaFormat) != null) {
return mp4vesMediaFormat;
}

return createVideoFormat(VideoFormatMimeType.AVC.getFormat(), bitrate, outputResolution);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.daasuu.mp4compose.filter;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class GlBackgroundFilter extends GlOverlayFilter {

private Bitmap backgorund;

public GlBackgroundFilter(Bitmap backgorund) {
this.backgorund = backgorund;
}

@Override
protected void drawCanvas(Canvas canvas) {
canvas.drawBitmap(backgorund, 0, 0, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,8 @@ private void codec() {
Size resolution = getVideoResolution(srcPath);

FillModeCustomItem fillModeCustomItem = new FillModeCustomItem(
playerTextureView.getScaleX(),
playerTextureView.getRotation(),
playerTextureView.getTranslationX() / baseWidthSize * 2f,
playerTextureView.getTranslationY() / (baseWidthSize / 9f * 16) * 2f,
playerTextureView,
baseWidthSize,
resolution.getWidth(),
resolution.getHeight()
);
Expand Down