Commit 0d2f1ccd authored by Sebastian Jedruszkiewicz's avatar Sebastian Jedruszkiewicz
Browse files

Add support for passing surface via legacy hwaccel_context.

Remove surface creation inside the encoder, throw an error if user did not provide input surface.
parent 2732876c
......@@ -946,15 +946,30 @@ int ff_mediacodec_enc_init(AVCodecContext *avctx, MediaCodecContext *s,
}
if (s->color_format == COLOR_FormatAndroidOpaque) {
AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
AVMediaCodecDeviceContext *mediacodec_ctx = (AVMediaCodecDeviceContext *)device_ctx->hwctx;
if (mediacodec_ctx->surface == NULL) {
//save surface in the MediaCodecContext so it can be released codec is being closed.
s->surface = mediacodec_ctx->surface = ff_AMediaCodec_createInputSurface(s->codec);
av_log(avctx, AV_LOG_INFO, "Creating surface %p\n", s->surface);
} else {
ff_AMediaCodec_setInputSurface(s->codec, mediacodec_ctx->surface);
av_log(avctx, AV_LOG_INFO, "Setting surface %p\n", s->surface);
AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
if (avctx->hw_device_ctx) {
AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
if (device_ctx->type == AV_HWDEVICE_TYPE_MEDIACODEC && device_ctx->hwctx) {
AVMediaCodecDeviceContext *mediacodec_ctx = (AVMediaCodecDeviceContext *)device_ctx->hwctx;
if (mediacodec_ctx->surface) {
s->surface = ff_mediacodec_surface_ref(mediacodec_ctx->surface, avctx);
ff_AMediaCodec_setInputSurface(s->codec, mediacodec_ctx->surface);
av_log(avctx, AV_LOG_INFO, "Setting surface %p\n", mediacodec_ctx->surface);
}
}
}
if (!s->surface && user_ctx && user_ctx->surface) {
s->surface = ff_mediacodec_surface_ref(user_ctx->surface, avctx);
ff_AMediaCodec_setInputSurface(s->codec, user_ctx->surface);
av_log(avctx, AV_LOG_INFO, "Setting surface %p\n", user_ctx->surface);
}
if (!s->surface) {
av_log(avctx, AV_LOG_ERROR, "No input surface provided\n");
ret = AVERROR_EXTERNAL;
goto fail;
}
}
......
......@@ -1753,29 +1753,6 @@ fail:
return ret;
}
int ff_Surface_release(void* surface, void* log_ctx)
{
JNIEnv *env = NULL;
jmethodID method_id;
jclass class;
JNI_GET_ENV_OR_RETURN(env, log_ctx, -1);
class = (*env)->FindClass(env, "android/view/Surface");
method_id = (*env)->GetMethodID(env, class, "release", "()V");
if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
return AVERROR_EXTERNAL;
}
(*env)->CallVoidMethod(env, surface, method_id);
if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
return AVERROR_EXTERNAL;
}
(*env)->DeleteGlobalRef(env, surface);
return 0;
}
int ff_Build_SDK_INT(AVCodecContext *avctx)
{
int ret = -1;
......
......@@ -136,8 +136,6 @@ int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec);
int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec);
int ff_Surface_release(void* surface, void* log_ctx);
int ff_Build_SDK_INT(AVCodecContext *avctx);
#endif /* AVCODEC_MEDIACODEC_WRAPPER_H */
......@@ -56,13 +56,9 @@ static av_cold int mediacodec_encode_close(AVCodecContext *avctx)
{
MediaCodecH264EncContext *s = avctx->priv_data;
if (s->ctx->surface) {
ff_Surface_release(s->ctx->surface, avctx);
s->ctx->surface = NULL;
}
ff_mediacodec_enc_close(avctx, s->ctx);
s->ctx = NULL;
return 0;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment