2022-01-07 17:08:45 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* H.264 Bitmap Compression
|
|
|
|
*
|
|
|
|
* Copyright 2022 Ely Ronnen <elyronnen@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <winpr/wlog.h>
|
2022-01-26 12:11:58 +03:00
|
|
|
#include <winpr/assert.h>
|
2022-01-25 16:45:45 +03:00
|
|
|
#include <winpr/library.h>
|
|
|
|
|
2022-01-07 17:08:45 +03:00
|
|
|
#include <freerdp/log.h>
|
|
|
|
#include <freerdp/codec/h264.h>
|
|
|
|
|
|
|
|
#include <media/NdkMediaCodec.h>
|
|
|
|
#include <media/NdkMediaFormat.h>
|
|
|
|
|
|
|
|
#include "h264.h"
|
|
|
|
|
2022-02-24 13:53:27 +03:00
|
|
|
static const char* CODEC_NAME = "video/avc";
|
|
|
|
|
2022-01-31 12:33:42 +03:00
|
|
|
static const int COLOR_FormatYUV420Planar = 19;
|
|
|
|
static const int COLOR_FormatYUV420Flexible = 0x7f420888;
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-31 12:33:42 +03:00
|
|
|
/* https://developer.android.com/reference/android/media/MediaCodec#qualityFloor */
|
|
|
|
static const int MEDIACODEC_MINIMUM_WIDTH = 320;
|
|
|
|
static const int MEDIACODEC_MINIMUM_HEIGHT = 240;
|
2022-01-30 00:02:58 +03:00
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2022-01-07 17:08:45 +03:00
|
|
|
{
|
2022-01-26 11:53:26 +03:00
|
|
|
AMediaCodec* decoder;
|
|
|
|
AMediaFormat* inputFormat;
|
|
|
|
AMediaFormat* outputFormat;
|
|
|
|
int32_t width;
|
|
|
|
int32_t height;
|
2022-01-30 00:02:58 +03:00
|
|
|
int32_t outputWidth;
|
|
|
|
int32_t outputHeight;
|
2022-01-26 11:53:26 +03:00
|
|
|
ssize_t currentOutputBufferIndex;
|
2022-02-14 16:59:22 +03:00
|
|
|
} H264_CONTEXT_MEDIACODEC;
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
static AMediaFormat* mediacodec_format_new(wLog* log, int width, int height)
|
2022-01-25 16:45:45 +03:00
|
|
|
{
|
2022-02-24 12:55:45 +03:00
|
|
|
const char* media_format;
|
|
|
|
AMediaFormat* format = AMediaFormat_new();
|
|
|
|
if (format == NULL)
|
2022-01-26 11:53:26 +03:00
|
|
|
{
|
2022-02-24 12:55:45 +03:00
|
|
|
WLog_Print(log, WLOG_ERROR, "AMediaFormat_new failed");
|
|
|
|
return NULL;
|
2022-01-26 11:53:26 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 13:53:27 +03:00
|
|
|
AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, CODEC_NAME);
|
2022-02-24 12:55:45 +03:00
|
|
|
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, width);
|
|
|
|
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, height);
|
|
|
|
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_COLOR_FORMAT, COLOR_FormatYUV420Planar);
|
2022-01-26 12:11:05 +03:00
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
media_format = AMediaFormat_toString(format);
|
|
|
|
if (media_format == NULL)
|
2022-01-26 11:53:26 +03:00
|
|
|
{
|
2022-02-24 12:55:45 +03:00
|
|
|
WLog_Print(log, WLOG_ERROR, "AMediaFormat_toString failed");
|
|
|
|
AMediaFormat_delete(format);
|
|
|
|
return NULL;
|
2022-01-26 11:53:26 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
WLog_Print(log, WLOG_DEBUG, "MediaCodec configuring with desired output format [%s]",
|
|
|
|
media_format);
|
|
|
|
|
|
|
|
return format;
|
2022-01-25 16:45:45 +03:00
|
|
|
}
|
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
static void set_mediacodec_format(H264_CONTEXT* h264, AMediaFormat** formatVariable,
|
|
|
|
AMediaFormat* newFormat)
|
2022-01-07 17:08:45 +03:00
|
|
|
{
|
2022-01-26 11:53:26 +03:00
|
|
|
media_status_t status = AMEDIA_OK;
|
|
|
|
H264_CONTEXT_MEDIACODEC* sys;
|
|
|
|
|
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
WINPR_ASSERT(formatVariable);
|
|
|
|
|
|
|
|
sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
|
|
|
|
WINPR_ASSERT(sys);
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
if (*formatVariable == newFormat)
|
|
|
|
return;
|
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
if (*formatVariable != NULL)
|
|
|
|
{
|
2022-02-24 12:55:45 +03:00
|
|
|
status = AMediaFormat_delete(*formatVariable);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Error AMediaFormat_delete %d", status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*formatVariable = newFormat;
|
2022-01-07 17:08:45 +03:00
|
|
|
}
|
|
|
|
|
2022-01-30 00:02:58 +03:00
|
|
|
static int update_mediacodec_inputformat(H264_CONTEXT* h264)
|
|
|
|
{
|
|
|
|
H264_CONTEXT_MEDIACODEC* sys;
|
|
|
|
AMediaFormat* inputFormat;
|
|
|
|
const char* mediaFormatName;
|
|
|
|
|
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
|
|
|
|
sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
|
|
|
|
WINPR_ASSERT(sys);
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
#if __ANDROID__ >= 21
|
|
|
|
inputFormat = AMediaCodec_getInputFormat(sys->decoder);
|
2022-01-30 00:02:58 +03:00
|
|
|
if (inputFormat == NULL)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_getInputFormat failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-24 12:55:45 +03:00
|
|
|
#else
|
|
|
|
inputFormat = sys->inputFormat;
|
|
|
|
#endif
|
2022-01-30 00:02:58 +03:00
|
|
|
set_mediacodec_format(h264, &sys->inputFormat, inputFormat);
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
mediaFormatName = AMediaFormat_toString(sys->inputFormat);
|
2022-01-30 00:02:58 +03:00
|
|
|
if (mediaFormatName == NULL)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaFormat_toString failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
WLog_Print(h264->log, WLOG_DEBUG, "Using MediaCodec with input MediaFormat [%s]",
|
|
|
|
mediaFormatName);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_mediacodec_outputformat(H264_CONTEXT* h264)
|
|
|
|
{
|
|
|
|
H264_CONTEXT_MEDIACODEC* sys;
|
|
|
|
AMediaFormat* outputFormat;
|
|
|
|
const char* mediaFormatName;
|
|
|
|
int32_t outputWidth, outputHeight;
|
|
|
|
|
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
|
|
|
|
sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
|
|
|
|
WINPR_ASSERT(sys);
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
outputFormat = AMediaCodec_getOutputFormat(sys->decoder);
|
2022-01-30 00:02:58 +03:00
|
|
|
if (outputFormat == NULL)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_getOutputFormat failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
set_mediacodec_format(h264, &sys->outputFormat, outputFormat);
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
mediaFormatName = AMediaFormat_toString(sys->outputFormat);
|
2022-01-30 00:02:58 +03:00
|
|
|
if (mediaFormatName == NULL)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaFormat_toString failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
WLog_Print(h264->log, WLOG_DEBUG, "Using MediaCodec with output MediaFormat [%s]",
|
|
|
|
mediaFormatName);
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
if (!AMediaFormat_getInt32(sys->outputFormat, AMEDIAFORMAT_KEY_WIDTH, &outputWidth))
|
2022-01-30 00:02:58 +03:00
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "fnAMediaFormat_getInt32 failed getting width");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
if (!AMediaFormat_getInt32(sys->outputFormat, AMEDIAFORMAT_KEY_HEIGHT, &outputHeight))
|
2022-01-30 00:02:58 +03:00
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "fnAMediaFormat_getInt32 failed getting height");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys->outputWidth = outputWidth;
|
|
|
|
sys->outputHeight = outputHeight;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-01-07 17:08:45 +03:00
|
|
|
static void release_current_outputbuffer(H264_CONTEXT* h264)
|
|
|
|
{
|
2022-01-26 11:53:26 +03:00
|
|
|
media_status_t status = AMEDIA_OK;
|
|
|
|
H264_CONTEXT_MEDIACODEC* sys;
|
|
|
|
|
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
|
|
|
|
WINPR_ASSERT(sys);
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
if (sys->currentOutputBufferIndex < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
status = AMediaCodec_releaseOutputBuffer(sys->decoder, sys->currentOutputBufferIndex, FALSE);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Error AMediaCodec_releaseOutputBuffer %d", status);
|
|
|
|
}
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
sys->currentOutputBufferIndex = -1;
|
2022-01-07 17:08:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mediacodec_compress(H264_CONTEXT* h264, const BYTE** pSrcYuv, const UINT32* pStride,
|
|
|
|
BYTE** ppDstData, UINT32* pDstSize)
|
|
|
|
{
|
2022-01-26 11:53:26 +03:00
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
WINPR_ASSERT(pSrcYuv);
|
|
|
|
WINPR_ASSERT(pStride);
|
|
|
|
WINPR_ASSERT(ppDstData);
|
|
|
|
WINPR_ASSERT(pDstSize);
|
|
|
|
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "MediaCodec is not supported as an encoder");
|
|
|
|
return -1;
|
2022-01-07 17:08:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mediacodec_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize)
|
|
|
|
{
|
2022-01-26 11:53:26 +03:00
|
|
|
ssize_t inputBufferId = -1;
|
|
|
|
size_t inputBufferSize, outputBufferSize;
|
|
|
|
uint8_t* inputBuffer;
|
|
|
|
media_status_t status;
|
|
|
|
BYTE** pYUVData;
|
|
|
|
UINT32* iStride;
|
|
|
|
H264_CONTEXT_MEDIACODEC* sys;
|
|
|
|
|
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
WINPR_ASSERT(pSrcData);
|
|
|
|
|
|
|
|
sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
|
|
|
|
WINPR_ASSERT(sys);
|
|
|
|
|
|
|
|
pYUVData = h264->pYUVData;
|
|
|
|
WINPR_ASSERT(pYUVData);
|
|
|
|
|
|
|
|
iStride = h264->iStride;
|
|
|
|
WINPR_ASSERT(iStride);
|
|
|
|
|
|
|
|
release_current_outputbuffer(h264);
|
|
|
|
|
2022-01-30 00:02:58 +03:00
|
|
|
if (sys->width != h264->width || sys->height != h264->height)
|
2022-01-26 11:53:26 +03:00
|
|
|
{
|
2022-01-30 00:02:58 +03:00
|
|
|
sys->width = h264->width;
|
|
|
|
sys->height = h264->height;
|
|
|
|
|
|
|
|
if (sys->width < MEDIACODEC_MINIMUM_WIDTH || sys->height < MEDIACODEC_MINIMUM_HEIGHT)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"MediaCodec got width or height smaller than minimum [%d,%d]", sys->width,
|
|
|
|
sys->height);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
WLog_Print(h264->log, WLOG_DEBUG, "MediaCodec setting new input width and height [%d,%d]",
|
|
|
|
sys->width, sys->height);
|
2022-01-26 11:53:26 +03:00
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
#if __ANDROID__ >= 26
|
|
|
|
AMediaFormat_setInt32(sys->inputFormat, AMEDIAFORMAT_KEY_WIDTH, sys->width);
|
|
|
|
AMediaFormat_setInt32(sys->inputFormat, AMEDIAFORMAT_KEY_HEIGHT, sys->height);
|
|
|
|
status = AMediaCodec_setParameters(sys->decoder, sys->inputFormat);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_setParameters failed: %d", status);
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-24 12:55:45 +03:00
|
|
|
#else
|
|
|
|
set_mediacodec_format(h264, &sys->inputFormat,
|
|
|
|
mediacodec_format_new(h264->log, sys->width, sys->height));
|
|
|
|
#endif
|
2022-01-26 11:53:26 +03:00
|
|
|
|
2022-01-30 00:02:58 +03:00
|
|
|
// The codec can change output width and height
|
|
|
|
if (update_mediacodec_outputformat(h264) < 0)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "MediaCodec failed updating input format");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-01-26 11:53:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
UINT32 inputBufferCurrnetOffset = 0;
|
|
|
|
while (inputBufferCurrnetOffset < SrcSize)
|
|
|
|
{
|
|
|
|
UINT32 numberOfBytesToCopy = SrcSize - inputBufferCurrnetOffset;
|
2022-02-24 12:55:45 +03:00
|
|
|
inputBufferId = AMediaCodec_dequeueInputBuffer(sys->decoder, -1);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (inputBufferId < 0)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_dequeueInputBuffer failed [%d]",
|
|
|
|
inputBufferId);
|
|
|
|
// TODO: sleep?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
inputBuffer = AMediaCodec_getInputBuffer(sys->decoder, inputBufferId, &inputBufferSize);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (inputBuffer == NULL)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_getInputBuffer failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numberOfBytesToCopy > inputBufferSize)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_WARN,
|
|
|
|
"MediaCodec inputBufferSize: got [%d] but wanted [%d]", inputBufferSize,
|
|
|
|
numberOfBytesToCopy);
|
|
|
|
numberOfBytesToCopy = inputBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(inputBuffer, pSrcData + inputBufferCurrnetOffset, numberOfBytesToCopy);
|
|
|
|
inputBufferCurrnetOffset += numberOfBytesToCopy;
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
status = AMediaCodec_queueInputBuffer(sys->decoder, inputBufferId, 0,
|
|
|
|
numberOfBytesToCopy, 0, 0);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Error AMediaCodec_queueInputBuffer %d", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
AMediaCodecBufferInfo bufferInfo;
|
2022-02-24 12:55:45 +03:00
|
|
|
ssize_t outputBufferId = AMediaCodec_dequeueOutputBuffer(sys->decoder, &bufferInfo, -1);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (outputBufferId >= 0)
|
|
|
|
{
|
|
|
|
sys->currentOutputBufferIndex = outputBufferId;
|
|
|
|
|
|
|
|
uint8_t* outputBuffer;
|
2022-02-24 12:55:45 +03:00
|
|
|
outputBuffer =
|
|
|
|
AMediaCodec_getOutputBuffer(sys->decoder, outputBufferId, &outputBufferSize);
|
2022-01-26 11:53:26 +03:00
|
|
|
sys->currentOutputBufferIndex = outputBufferId;
|
|
|
|
|
2022-01-30 00:02:58 +03:00
|
|
|
if (outputBufferSize !=
|
|
|
|
(sys->outputWidth * sys->outputHeight +
|
|
|
|
((sys->outputWidth + 1) / 2) * ((sys->outputHeight + 1) / 2) * 2))
|
2022-01-26 11:53:26 +03:00
|
|
|
{
|
2022-01-30 00:02:58 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Error MediaCodec unexpected output buffer size %d",
|
2022-01-26 11:53:26 +03:00
|
|
|
outputBufferSize);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: work with AImageReader and get AImage object instead of
|
|
|
|
// COLOR_FormatYUV420Planar buffer.
|
2022-01-30 00:02:58 +03:00
|
|
|
iStride[0] = sys->outputWidth;
|
|
|
|
iStride[1] = (sys->outputWidth + 1) / 2;
|
|
|
|
iStride[2] = (sys->outputWidth + 1) / 2;
|
2022-01-26 11:53:26 +03:00
|
|
|
pYUVData[0] = outputBuffer;
|
2022-01-30 00:02:58 +03:00
|
|
|
pYUVData[1] = outputBuffer + iStride[0] * sys->outputHeight;
|
|
|
|
pYUVData[2] = outputBuffer + iStride[0] * sys->outputHeight +
|
|
|
|
iStride[1] * ((sys->outputHeight + 1) / 2);
|
2022-01-26 11:53:26 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (outputBufferId == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED)
|
|
|
|
{
|
2022-01-30 00:02:58 +03:00
|
|
|
if (update_mediacodec_outputformat(h264) < 0)
|
2022-01-26 11:53:26 +03:00
|
|
|
{
|
2022-01-30 00:02:58 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"MediaCodec failed updating output format in decompress");
|
2022-01-26 11:53:26 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (outputBufferId == AMEDIACODEC_INFO_TRY_AGAIN_LATER)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_WARN,
|
|
|
|
"AMediaCodec_dequeueOutputBuffer need to try again later");
|
|
|
|
// TODO: sleep?
|
|
|
|
}
|
|
|
|
else if (outputBufferId == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_WARN,
|
|
|
|
"AMediaCodec_dequeueOutputBuffer returned deprecated value "
|
|
|
|
"AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED, ignoring");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"AMediaCodec_dequeueOutputBuffer returned unknown value [%d]",
|
|
|
|
outputBufferId);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2022-01-07 17:08:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mediacodec_uninit(H264_CONTEXT* h264)
|
|
|
|
{
|
2022-01-26 11:53:26 +03:00
|
|
|
media_status_t status = AMEDIA_OK;
|
|
|
|
H264_CONTEXT_MEDIACODEC* sys;
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
WINPR_ASSERT(h264);
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
sys = (H264_CONTEXT_MEDIACODEC*)h264->pSystemData;
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
WLog_Print(h264->log, WLOG_DEBUG, "Uninitializing MediaCodec");
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
if (!sys)
|
|
|
|
return;
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
if (sys->decoder != NULL)
|
|
|
|
{
|
|
|
|
release_current_outputbuffer(h264);
|
2022-02-24 12:55:45 +03:00
|
|
|
status = AMediaCodec_stop(sys->decoder);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Error AMediaCodec_stop %d", status);
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
status = AMediaCodec_delete(sys->decoder);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Error AMediaCodec_delete %d", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
sys->decoder = NULL;
|
|
|
|
}
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
set_mediacodec_format(h264, &sys->inputFormat, NULL);
|
|
|
|
set_mediacodec_format(h264, &sys->outputFormat, NULL);
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
free(sys);
|
2022-01-07 17:08:45 +03:00
|
|
|
h264->pSystemData = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL mediacodec_init(H264_CONTEXT* h264)
|
|
|
|
{
|
2022-01-26 11:53:26 +03:00
|
|
|
H264_CONTEXT_MEDIACODEC* sys;
|
|
|
|
media_status_t status;
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
WINPR_ASSERT(h264);
|
2022-01-07 17:08:45 +03:00
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
if (h264->Compressor)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "MediaCodec is not supported as an encoder");
|
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
WLog_Print(h264->log, WLOG_DEBUG, "Initializing MediaCodec");
|
2022-01-07 17:08:45 +03:00
|
|
|
|
|
|
|
sys = (H264_CONTEXT_MEDIACODEC*)calloc(1, sizeof(H264_CONTEXT_MEDIACODEC));
|
|
|
|
|
|
|
|
if (!sys)
|
|
|
|
{
|
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
h264->pSystemData = (void*)sys;
|
|
|
|
|
2022-01-26 11:53:26 +03:00
|
|
|
sys->currentOutputBufferIndex = -1;
|
2022-01-30 00:02:58 +03:00
|
|
|
|
|
|
|
// updated when we're given the height and width for the first time
|
|
|
|
sys->width = sys->outputWidth = MEDIACODEC_MINIMUM_WIDTH;
|
|
|
|
sys->height = sys->outputHeight = MEDIACODEC_MINIMUM_HEIGHT;
|
2022-02-24 13:53:27 +03:00
|
|
|
sys->decoder = AMediaCodec_createDecoderByType(CODEC_NAME);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (sys->decoder == NULL)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_createCodecByName failed");
|
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
#if __ANDROID_API__ >= 28
|
|
|
|
char* codec_name;
|
|
|
|
status = AMediaCodec_getName(sys->decoder, &codec_name);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_getName failed: %d", status);
|
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2022-02-24 13:53:27 +03:00
|
|
|
WLog_Print(h264->log, WLOG_DEBUG, "MediaCodec using %s codec [%s]", CODEC_NAME, codec_name);
|
2022-02-24 12:55:45 +03:00
|
|
|
AMediaCodec_releaseName(sys->decoder, codec_name);
|
|
|
|
#endif
|
2022-01-26 11:53:26 +03:00
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
set_mediacodec_format(h264, &sys->inputFormat,
|
|
|
|
mediacodec_format_new(h264->log, sys->width, sys->height));
|
2022-01-30 00:02:58 +03:00
|
|
|
|
2022-02-24 12:55:45 +03:00
|
|
|
status = AMediaCodec_configure(sys->decoder, sys->inputFormat, NULL, NULL, 0);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_configure failed: %d", status);
|
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2022-01-30 00:02:58 +03:00
|
|
|
if (update_mediacodec_inputformat(h264) < 0)
|
2022-01-26 11:53:26 +03:00
|
|
|
{
|
2022-01-30 00:02:58 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "MediaCodec failed updating input format");
|
2022-01-26 12:11:05 +03:00
|
|
|
goto EXCEPTION;
|
2022-01-26 11:53:26 +03:00
|
|
|
}
|
|
|
|
|
2022-01-30 00:02:58 +03:00
|
|
|
if (update_mediacodec_outputformat(h264) < 0)
|
2022-01-26 11:53:26 +03:00
|
|
|
{
|
2022-01-30 00:02:58 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "MediaCodec failed updating output format");
|
2022-01-26 11:53:26 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
WLog_Print(h264->log, WLOG_DEBUG, "Starting MediaCodec");
|
2022-02-24 12:55:45 +03:00
|
|
|
status = AMediaCodec_start(sys->decoder);
|
2022-01-26 11:53:26 +03:00
|
|
|
if (status != AMEDIA_OK)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "AMediaCodec_start failed %d", status);
|
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2022-01-07 17:08:45 +03:00
|
|
|
EXCEPTION:
|
|
|
|
mediacodec_uninit(h264);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
H264_CONTEXT_SUBSYSTEM g_Subsystem_mediacodec = { "MediaCodec", mediacodec_init, mediacodec_uninit,
|
2022-01-26 11:53:26 +03:00
|
|
|
mediacodec_decompress, mediacodec_compress };
|