2017-06-14 17:04:54 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* H.264 Bitmap Compression
|
|
|
|
*
|
|
|
|
* Copyright 2015 Marc-André Moreau <marcandre.moreau@gmail.com>
|
|
|
|
* Copyright 2014 Mike McDonald <Mike.McDonald@software.dell.com>
|
|
|
|
* Copyright 2014 erbth <t.erbesdobler@team103.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.
|
|
|
|
*/
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2018-01-27 09:10:51 +03:00
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
#include <winpr/wlog.h>
|
|
|
|
#include <freerdp/log.h>
|
|
|
|
#include <freerdp/codec/h264.h>
|
|
|
|
#include <libavcodec/avcodec.h>
|
2017-07-20 13:46:04 +03:00
|
|
|
#include <libavutil/opt.h>
|
2018-01-27 09:10:51 +03:00
|
|
|
|
2020-10-22 19:11:21 +03:00
|
|
|
#include "h264.h"
|
|
|
|
|
2018-02-02 22:30:31 +03:00
|
|
|
#ifdef WITH_VAAPI
|
2018-01-27 09:10:51 +03:00
|
|
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 9, 0)
|
|
|
|
#include <libavutil/hwcontext.h>
|
|
|
|
#else
|
2019-11-06 17:24:51 +03:00
|
|
|
#pragma warning You have asked for VA - API decoding, \
|
|
|
|
but your version of libavutil is too old !Disabling.
|
2018-01-27 09:10:51 +03:00
|
|
|
#undef WITH_VAAPI
|
|
|
|
#endif
|
2018-02-02 22:30:31 +03:00
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
/* Fallback support for older libavcodec versions */
|
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
|
|
|
|
#define AV_CODEC_ID_H264 CODEC_ID_H264
|
|
|
|
#endif
|
|
|
|
|
2017-12-29 19:59:45 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 34, 2)
|
|
|
|
#define AV_CODEC_FLAG_LOOP_FILTER CODEC_FLAG_LOOP_FILTER
|
|
|
|
#define AV_CODEC_CAP_TRUNCATED CODEC_CAP_TRUNCATED
|
|
|
|
#define AV_CODEC_FLAG_TRUNCATED CODEC_FLAG_TRUNCATED
|
|
|
|
#endif
|
|
|
|
|
2017-11-13 13:28:43 +03:00
|
|
|
#if LIBAVUTIL_VERSION_MAJOR < 52
|
|
|
|
#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
|
|
|
|
#endif
|
|
|
|
|
2017-11-08 14:37:23 +03:00
|
|
|
/* Ubuntu 14.04 ships without the functions provided by avutil,
|
|
|
|
* so define error to string methods here. */
|
|
|
|
#if !defined(av_err2str)
|
|
|
|
static inline char* error_string(char* errbuf, size_t errbuf_size, int errnum)
|
|
|
|
{
|
|
|
|
av_strerror(errnum, errbuf, errbuf_size);
|
|
|
|
return errbuf;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define av_err2str(errnum) error_string((char[64]){ 0 }, 64, errnum)
|
2017-11-08 14:37:23 +03:00
|
|
|
#endif
|
|
|
|
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
|
|
|
#define VAAPI_DEVICE "/dev/dri/renderD128"
|
|
|
|
#endif
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2022-10-10 12:57:13 +03:00
|
|
|
const AVCodec* codecDecoder;
|
2017-07-20 13:46:04 +03:00
|
|
|
AVCodecContext* codecDecoderContext;
|
2022-10-10 12:57:13 +03:00
|
|
|
const AVCodec* codecEncoder;
|
2017-07-20 13:46:04 +03:00
|
|
|
AVCodecContext* codecEncoderContext;
|
2017-06-14 17:04:54 +03:00
|
|
|
AVCodecParserContext* codecParser;
|
|
|
|
AVFrame* videoFrame;
|
2021-12-17 19:08:08 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
|
|
|
|
AVPacket bufferpacket;
|
|
|
|
#endif
|
|
|
|
AVPacket* packet;
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
|
|
|
AVBufferRef* hwctx;
|
|
|
|
AVFrame* hwVideoFrame;
|
|
|
|
enum AVPixelFormat hw_pix_fmt;
|
2018-02-02 23:49:59 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100)
|
|
|
|
AVBufferRef* hw_frames_ctx;
|
|
|
|
#endif
|
2018-01-27 09:10:51 +03:00
|
|
|
#endif
|
2022-02-14 16:59:22 +03:00
|
|
|
} H264_CONTEXT_LIBAVCODEC;
|
2017-06-14 17:04:54 +03:00
|
|
|
|
2024-05-30 14:26:04 +03:00
|
|
|
static void libavcodec_destroy_encoder(H264_CONTEXT* WINPR_RESTRICT h264)
|
2017-07-20 13:46:04 +03:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
H264_CONTEXT_LIBAVCODEC* sys = NULL;
|
2017-07-20 13:46:04 +03:00
|
|
|
|
|
|
|
if (!h264 || !h264->subsystem)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
|
|
|
|
|
|
|
|
if (sys->codecEncoderContext)
|
|
|
|
{
|
|
|
|
avcodec_close(sys->codecEncoderContext);
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
|
|
|
|
avcodec_free_context(&sys->codecEncoderContext);
|
|
|
|
#else
|
|
|
|
av_free(sys->codecEncoderContext);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
sys->codecEncoder = NULL;
|
|
|
|
sys->codecEncoderContext = NULL;
|
|
|
|
}
|
|
|
|
|
2024-05-30 14:26:04 +03:00
|
|
|
static BOOL libavcodec_create_encoder(H264_CONTEXT* WINPR_RESTRICT h264)
|
2017-07-20 13:46:04 +03:00
|
|
|
{
|
|
|
|
BOOL recreate = FALSE;
|
2024-01-23 18:49:54 +03:00
|
|
|
H264_CONTEXT_LIBAVCODEC* sys = NULL;
|
2017-07-20 13:46:04 +03:00
|
|
|
|
|
|
|
if (!h264 || !h264->subsystem)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-02-07 16:31:17 +03:00
|
|
|
if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-07-20 13:46:04 +03:00
|
|
|
sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
|
2022-03-29 20:05:39 +03:00
|
|
|
if (!sys)
|
|
|
|
return FALSE;
|
2017-07-20 13:46:04 +03:00
|
|
|
recreate = !sys->codecEncoder || !sys->codecEncoderContext;
|
|
|
|
|
|
|
|
if (sys->codecEncoderContext)
|
|
|
|
{
|
2019-02-07 16:31:17 +03:00
|
|
|
if ((sys->codecEncoderContext->width != (int)h264->width) ||
|
|
|
|
(sys->codecEncoderContext->height != (int)h264->height))
|
2017-07-20 13:46:04 +03:00
|
|
|
recreate = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recreate)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
libavcodec_destroy_encoder(h264);
|
|
|
|
sys->codecEncoder = avcodec_find_encoder(AV_CODEC_ID_H264);
|
|
|
|
|
|
|
|
if (!sys->codecEncoder)
|
|
|
|
goto EXCEPTION;
|
|
|
|
|
|
|
|
sys->codecEncoderContext = avcodec_alloc_context3(sys->codecEncoder);
|
|
|
|
|
|
|
|
if (!sys->codecEncoderContext)
|
|
|
|
goto EXCEPTION;
|
|
|
|
|
|
|
|
switch (h264->RateControlMode)
|
|
|
|
{
|
|
|
|
case H264_RATECONTROL_VBR:
|
|
|
|
sys->codecEncoderContext->bit_rate = h264->BitRate;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case H264_RATECONTROL_CQP:
|
|
|
|
/* TODO: sys->codecEncoderContext-> = h264->QP; */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-17 19:08:08 +03:00
|
|
|
sys->codecEncoderContext->width = (int)MIN(INT32_MAX, h264->width);
|
|
|
|
sys->codecEncoderContext->height = (int)MIN(INT32_MAX, h264->height);
|
2017-07-20 13:46:04 +03:00
|
|
|
sys->codecEncoderContext->delay = 0;
|
2017-11-08 14:37:23 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 13, 100)
|
2019-11-06 17:24:51 +03:00
|
|
|
sys->codecEncoderContext->framerate = (AVRational){ h264->FrameRate, 1 };
|
2017-11-08 14:37:23 +03:00
|
|
|
#endif
|
2019-11-06 17:24:51 +03:00
|
|
|
sys->codecEncoderContext->time_base = (AVRational){ 1, h264->FrameRate };
|
2018-02-02 13:03:27 +03:00
|
|
|
av_opt_set(sys->codecEncoderContext, "preset", "medium", AV_OPT_SEARCH_CHILDREN);
|
2017-08-18 20:19:44 +03:00
|
|
|
av_opt_set(sys->codecEncoderContext, "tune", "zerolatency", AV_OPT_SEARCH_CHILDREN);
|
2017-12-28 18:41:45 +03:00
|
|
|
sys->codecEncoderContext->flags |= AV_CODEC_FLAG_LOOP_FILTER;
|
2017-07-20 13:46:04 +03:00
|
|
|
sys->codecEncoderContext->pix_fmt = AV_PIX_FMT_YUV420P;
|
|
|
|
|
|
|
|
if (avcodec_open2(sys->codecEncoderContext, sys->codecEncoder, NULL) < 0)
|
|
|
|
goto EXCEPTION;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
EXCEPTION:
|
|
|
|
libavcodec_destroy_encoder(h264);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2024-05-30 14:26:04 +03:00
|
|
|
static int libavcodec_decompress(H264_CONTEXT* WINPR_RESTRICT h264,
|
|
|
|
const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2022-04-27 22:02:18 +03:00
|
|
|
union
|
|
|
|
{
|
|
|
|
const BYTE* cpv;
|
|
|
|
BYTE* pv;
|
|
|
|
} cnv;
|
2022-03-30 09:27:59 +03:00
|
|
|
int rc = -1;
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
2017-06-14 17:04:54 +03:00
|
|
|
int gotFrame = 0;
|
2022-03-30 09:27:59 +03:00
|
|
|
AVPacket* packet = NULL;
|
2022-03-29 20:05:39 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
WINPR_ASSERT(pSrcData || (SrcSize == 0));
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
|
2017-06-14 17:04:54 +03:00
|
|
|
BYTE** pYUVData = h264->pYUVData;
|
|
|
|
UINT32* iStride = h264->iStride;
|
2022-02-15 12:30:31 +03:00
|
|
|
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(sys);
|
|
|
|
|
2021-12-17 19:08:08 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
|
2022-03-30 09:27:59 +03:00
|
|
|
packet = &sys->bufferpacket;
|
|
|
|
WINPR_ASSERT(packet);
|
|
|
|
av_init_packet(packet);
|
2021-12-17 19:08:08 +03:00
|
|
|
#else
|
2022-03-30 09:27:59 +03:00
|
|
|
packet = av_packet_alloc();
|
2021-12-17 19:08:08 +03:00
|
|
|
#endif
|
2022-03-30 09:27:59 +03:00
|
|
|
if (!packet)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate AVPacket");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2022-04-27 22:02:18 +03:00
|
|
|
cnv.cpv = pSrcData;
|
|
|
|
packet->data = cnv.pv;
|
2022-03-30 09:27:59 +03:00
|
|
|
packet->size = (int)MIN(SrcSize, INT32_MAX);
|
2022-03-29 20:05:39 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(sys->codecDecoderContext);
|
2017-06-14 17:04:54 +03:00
|
|
|
/* avcodec_decode_video2 is deprecated with libavcodec 57.48.101 */
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
2022-03-30 09:27:59 +03:00
|
|
|
status = avcodec_send_packet(sys->codecDecoderContext, packet);
|
2022-02-15 12:30:31 +03:00
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
if (status < 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to decode video frame (status=%d)", status);
|
2022-03-30 09:27:59 +03:00
|
|
|
goto fail;
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
|
2018-01-27 09:10:51 +03:00
|
|
|
sys->videoFrame->format = AV_PIX_FMT_YUV420P;
|
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
do
|
|
|
|
{
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
2018-02-02 23:49:59 +03:00
|
|
|
status = avcodec_receive_frame(sys->codecDecoderContext,
|
|
|
|
sys->hwctx ? sys->hwVideoFrame : sys->videoFrame);
|
2018-01-27 09:10:51 +03:00
|
|
|
#else
|
2017-07-20 13:46:04 +03:00
|
|
|
status = avcodec_receive_frame(sys->codecDecoderContext, sys->videoFrame);
|
2018-01-27 09:10:51 +03:00
|
|
|
#endif
|
2019-11-06 17:24:51 +03:00
|
|
|
} while (status == AVERROR(EAGAIN));
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
gotFrame = (status == 0);
|
2018-01-27 09:10:51 +03:00
|
|
|
#else
|
|
|
|
#ifdef WITH_VAAPI
|
2019-11-06 17:24:51 +03:00
|
|
|
status =
|
2022-03-30 09:27:59 +03:00
|
|
|
avcodec_decode_video2(sys->codecDecoderContext,
|
|
|
|
sys->hwctx ? sys->hwVideoFrame : sys->videoFrame, &gotFrame, packet);
|
|
|
|
#else
|
|
|
|
status = avcodec_decode_video2(sys->codecDecoderContext, sys->videoFrame, &gotFrame, packet);
|
2022-03-30 19:20:55 +03:00
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
#endif
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to decode video frame (status=%d)", status);
|
2022-03-30 09:27:59 +03:00
|
|
|
goto fail;
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
|
|
|
|
|
|
|
if (sys->hwctx)
|
|
|
|
{
|
|
|
|
if (sys->hwVideoFrame->format == sys->hw_pix_fmt)
|
|
|
|
{
|
|
|
|
sys->videoFrame->width = sys->hwVideoFrame->width;
|
|
|
|
sys->videoFrame->height = sys->hwVideoFrame->height;
|
|
|
|
status = av_hwframe_transfer_data(sys->videoFrame, sys->hwVideoFrame, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = av_frame_copy(sys->videoFrame, sys->hwVideoFrame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gotFrame = (status == 0);
|
2018-02-02 23:49:59 +03:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2018-04-05 11:48:45 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to transfer video frame (status=%d) (%s)", status,
|
|
|
|
av_err2str(status));
|
2022-03-30 09:27:59 +03:00
|
|
|
goto fail;
|
2018-02-02 23:49:59 +03:00
|
|
|
}
|
|
|
|
|
2018-01-27 09:10:51 +03:00
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
#if 0
|
2018-01-19 16:26:04 +03:00
|
|
|
WLog_Print(h264->log, WLOG_INFO,
|
|
|
|
"libavcodec_decompress: frame decoded (status=%d, gotFrame=%d, width=%d, height=%d, Y=[%p,%d], U=[%p,%d], V=[%p,%d])",
|
|
|
|
status, gotFrame, sys->videoFrame->width, sys->videoFrame->height,
|
|
|
|
(void*) sys->videoFrame->data[0], sys->videoFrame->linesize[0],
|
|
|
|
(void*) sys->videoFrame->data[1], sys->videoFrame->linesize[1],
|
|
|
|
(void*) sys->videoFrame->data[2], sys->videoFrame->linesize[2]);
|
2017-06-14 17:04:54 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (gotFrame)
|
|
|
|
{
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(sys->videoFrame);
|
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
pYUVData[0] = sys->videoFrame->data[0];
|
|
|
|
pYUVData[1] = sys->videoFrame->data[1];
|
|
|
|
pYUVData[2] = sys->videoFrame->data[2];
|
2021-12-17 19:08:08 +03:00
|
|
|
iStride[0] = (UINT32)MAX(0, sys->videoFrame->linesize[0]);
|
|
|
|
iStride[1] = (UINT32)MAX(0, sys->videoFrame->linesize[1]);
|
|
|
|
iStride[2] = (UINT32)MAX(0, sys->videoFrame->linesize[2]);
|
2022-03-30 09:27:59 +03:00
|
|
|
|
|
|
|
rc = 1;
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
else
|
2022-03-30 09:27:59 +03:00
|
|
|
rc = -2;
|
2017-06-14 17:04:54 +03:00
|
|
|
|
2022-03-30 09:27:59 +03:00
|
|
|
fail:
|
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
|
|
|
|
av_packet_unref(packet);
|
|
|
|
#else
|
|
|
|
av_packet_free(&packet);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rc;
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
|
2024-05-30 14:26:04 +03:00
|
|
|
static int libavcodec_compress(H264_CONTEXT* WINPR_RESTRICT h264,
|
|
|
|
const BYTE** WINPR_RESTRICT pSrcYuv,
|
|
|
|
const UINT32* WINPR_RESTRICT pStride,
|
|
|
|
BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2022-04-27 22:02:18 +03:00
|
|
|
union
|
|
|
|
{
|
|
|
|
const BYTE* cpv;
|
|
|
|
uint8_t* pv;
|
|
|
|
} cnv;
|
2022-03-30 09:27:59 +03:00
|
|
|
int rc = -1;
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
2017-07-20 13:46:04 +03:00
|
|
|
int gotFrame = 0;
|
2022-03-29 20:05:39 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(sys);
|
2017-07-20 13:46:04 +03:00
|
|
|
|
|
|
|
if (!libavcodec_create_encoder(h264))
|
|
|
|
return -1;
|
|
|
|
|
2022-03-30 19:34:21 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
|
2022-03-29 23:31:39 +03:00
|
|
|
sys->packet = &sys->bufferpacket;
|
2021-12-17 19:08:08 +03:00
|
|
|
av_packet_unref(sys->packet);
|
|
|
|
av_init_packet(sys->packet);
|
|
|
|
#else
|
2022-03-30 19:34:21 +03:00
|
|
|
av_packet_free(&sys->packet);
|
2021-12-17 19:08:08 +03:00
|
|
|
sys->packet = av_packet_alloc();
|
|
|
|
#endif
|
2022-03-30 09:27:59 +03:00
|
|
|
if (!sys->packet)
|
|
|
|
{
|
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate AVPacket");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(sys->packet);
|
2021-12-17 19:08:08 +03:00
|
|
|
sys->packet->data = NULL;
|
|
|
|
sys->packet->size = 0;
|
2022-03-29 20:05:39 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(sys->videoFrame);
|
|
|
|
WINPR_ASSERT(sys->codecEncoderContext);
|
2017-07-20 13:46:04 +03:00
|
|
|
sys->videoFrame->format = sys->codecEncoderContext->pix_fmt;
|
2019-11-06 17:24:51 +03:00
|
|
|
sys->videoFrame->width = sys->codecEncoderContext->width;
|
2017-07-20 13:46:04 +03:00
|
|
|
sys->videoFrame->height = sys->codecEncoderContext->height;
|
2017-11-08 14:37:23 +03:00
|
|
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(52, 48, 100)
|
2017-07-20 13:46:04 +03:00
|
|
|
sys->videoFrame->colorspace = AVCOL_SPC_BT709;
|
2017-11-08 14:37:23 +03:00
|
|
|
#endif
|
2017-11-13 01:31:22 +03:00
|
|
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(52, 92, 100)
|
2017-08-18 20:19:44 +03:00
|
|
|
sys->videoFrame->chroma_location = AVCHROMA_LOC_LEFT;
|
2017-11-13 01:31:22 +03:00
|
|
|
#endif
|
2022-04-27 22:02:18 +03:00
|
|
|
cnv.cpv = pSrcYuv[0];
|
|
|
|
sys->videoFrame->data[0] = cnv.pv;
|
|
|
|
|
|
|
|
cnv.cpv = pSrcYuv[1];
|
|
|
|
sys->videoFrame->data[1] = cnv.pv;
|
|
|
|
|
|
|
|
cnv.cpv = pSrcYuv[2];
|
|
|
|
sys->videoFrame->data[2] = cnv.pv;
|
|
|
|
|
2018-04-05 11:48:45 +03:00
|
|
|
sys->videoFrame->linesize[0] = (int)pStride[0];
|
|
|
|
sys->videoFrame->linesize[1] = (int)pStride[1];
|
|
|
|
sys->videoFrame->linesize[2] = (int)pStride[2];
|
2017-07-20 13:46:04 +03:00
|
|
|
sys->videoFrame->pts++;
|
|
|
|
/* avcodec_encode_video2 is deprecated with libavcodec 57.48.101 */
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
|
|
|
|
status = avcodec_send_frame(sys->codecEncoderContext, sys->videoFrame);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to encode video frame (%s [%d])",
|
|
|
|
av_err2str(status), status);
|
2022-03-30 09:27:59 +03:00
|
|
|
goto fail;
|
2017-07-20 13:46:04 +03:00
|
|
|
}
|
|
|
|
|
2021-12-17 19:08:08 +03:00
|
|
|
status = avcodec_receive_packet(sys->codecEncoderContext, sys->packet);
|
2017-07-20 13:46:04 +03:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to encode video frame (%s [%d])",
|
|
|
|
av_err2str(status), status);
|
2022-03-30 09:27:59 +03:00
|
|
|
goto fail;
|
2017-07-20 13:46:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gotFrame = (status == 0);
|
2017-11-14 11:15:43 +03:00
|
|
|
#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 59, 100)
|
2017-07-20 13:46:04 +03:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-12-17 19:08:08 +03:00
|
|
|
status = avcodec_encode_video2(sys->codecEncoderContext, sys->packet, sys->videoFrame,
|
2019-11-06 17:24:51 +03:00
|
|
|
&gotFrame);
|
|
|
|
} while ((status >= 0) && (gotFrame == 0));
|
2017-07-20 13:46:04 +03:00
|
|
|
|
2017-11-14 11:15:43 +03:00
|
|
|
#else
|
2021-12-17 19:08:08 +03:00
|
|
|
sys->packet->size =
|
2019-11-06 17:24:51 +03:00
|
|
|
avpicture_get_size(sys->codecDecoderContext->pix_fmt, sys->codecDecoderContext->width,
|
|
|
|
sys->codecDecoderContext->height);
|
2021-12-17 19:08:08 +03:00
|
|
|
sys->packet->data = av_malloc(sys->packet->size);
|
2017-11-14 11:15:43 +03:00
|
|
|
|
2021-12-17 19:08:08 +03:00
|
|
|
if (!sys->packet->data)
|
2017-11-14 11:15:43 +03:00
|
|
|
status = -1;
|
|
|
|
else
|
|
|
|
{
|
2021-12-17 19:08:08 +03:00
|
|
|
status = avcodec_encode_video(sys->codecDecoderContext, sys->packet->data,
|
|
|
|
sys->packet->size, sys->videoFrame);
|
2017-11-14 11:15:43 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 13:46:04 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to encode video frame (%s [%d])",
|
|
|
|
av_err2str(status), status);
|
2022-03-30 09:27:59 +03:00
|
|
|
goto fail;
|
2017-07-20 13:46:04 +03:00
|
|
|
}
|
|
|
|
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(sys->packet);
|
2021-12-17 19:08:08 +03:00
|
|
|
*ppDstData = sys->packet->data;
|
|
|
|
*pDstSize = (UINT32)MAX(0, sys->packet->size);
|
2017-07-20 13:46:04 +03:00
|
|
|
|
|
|
|
if (!gotFrame)
|
2017-08-18 20:19:44 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Did not get frame! (%s [%d])", av_err2str(status),
|
|
|
|
status);
|
2022-03-30 09:27:59 +03:00
|
|
|
rc = -2;
|
2017-08-18 20:19:44 +03:00
|
|
|
}
|
2022-03-30 09:27:59 +03:00
|
|
|
else
|
|
|
|
rc = 1;
|
|
|
|
fail:
|
|
|
|
return rc;
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
|
2024-06-04 12:23:14 +03:00
|
|
|
static void libavcodec_uninit(H264_CONTEXT* h264)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (!sys)
|
|
|
|
return;
|
|
|
|
|
2022-03-30 09:27:59 +03:00
|
|
|
if (sys->packet)
|
|
|
|
{
|
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
|
|
|
|
av_packet_unref(sys->packet);
|
|
|
|
#else
|
|
|
|
av_packet_free(&sys->packet);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
if (sys->videoFrame)
|
|
|
|
{
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
|
|
|
|
av_frame_free(&sys->videoFrame);
|
|
|
|
#else
|
|
|
|
av_free(sys->videoFrame);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
|
|
|
|
|
|
|
if (sys->hwVideoFrame)
|
|
|
|
{
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
|
|
|
|
av_frame_free(&sys->hwVideoFrame);
|
|
|
|
#else
|
|
|
|
av_free(sys->hwVideoFrame);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sys->hwctx)
|
|
|
|
av_buffer_unref(&sys->hwctx);
|
|
|
|
|
2018-02-02 23:49:59 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100)
|
|
|
|
|
|
|
|
if (sys->hw_frames_ctx)
|
|
|
|
av_buffer_unref(&sys->hw_frames_ctx);
|
|
|
|
|
|
|
|
#endif
|
2018-01-27 09:10:51 +03:00
|
|
|
#endif
|
|
|
|
|
2017-06-14 17:04:54 +03:00
|
|
|
if (sys->codecParser)
|
|
|
|
av_parser_close(sys->codecParser);
|
|
|
|
|
2017-07-20 13:46:04 +03:00
|
|
|
if (sys->codecDecoderContext)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2017-07-20 13:46:04 +03:00
|
|
|
avcodec_close(sys->codecDecoderContext);
|
2017-06-14 17:04:54 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
|
2017-07-20 13:46:04 +03:00
|
|
|
avcodec_free_context(&sys->codecDecoderContext);
|
2017-06-14 17:04:54 +03:00
|
|
|
#else
|
2017-07-20 13:46:04 +03:00
|
|
|
av_free(sys->codecDecoderContext);
|
2017-06-14 17:04:54 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-07-20 13:46:04 +03:00
|
|
|
libavcodec_destroy_encoder(h264);
|
2017-06-14 17:04:54 +03:00
|
|
|
free(sys);
|
|
|
|
h264->pSystemData = NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
|
|
|
static enum AVPixelFormat libavcodec_get_format(struct AVCodecContext* ctx,
|
2019-11-06 17:24:51 +03:00
|
|
|
const enum AVPixelFormat* fmts)
|
2018-01-27 09:10:51 +03:00
|
|
|
{
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(ctx);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
H264_CONTEXT* h264 = (H264_CONTEXT*)ctx->opaque;
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(h264);
|
|
|
|
|
2020-01-16 11:53:57 +03:00
|
|
|
H264_CONTEXT_LIBAVCODEC* sys = (H264_CONTEXT_LIBAVCODEC*)h264->pSystemData;
|
2022-03-29 20:05:39 +03:00
|
|
|
WINPR_ASSERT(sys);
|
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (const enum AVPixelFormat* p = fmts; *p != AV_PIX_FMT_NONE; p++)
|
2018-01-27 09:10:51 +03:00
|
|
|
{
|
|
|
|
if (*p == sys->hw_pix_fmt)
|
|
|
|
{
|
2018-02-02 23:49:59 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 80, 100)
|
|
|
|
sys->hw_frames_ctx = av_hwframe_ctx_alloc(sys->hwctx);
|
|
|
|
|
|
|
|
if (!sys->hw_frames_ctx)
|
|
|
|
{
|
|
|
|
return AV_PIX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys->codecDecoderContext->pix_fmt = *p;
|
2019-11-06 17:24:51 +03:00
|
|
|
AVHWFramesContext* frames = (AVHWFramesContext*)sys->hw_frames_ctx->data;
|
2018-02-02 23:49:59 +03:00
|
|
|
frames->format = *p;
|
|
|
|
frames->height = sys->codecDecoderContext->coded_height;
|
|
|
|
frames->width = sys->codecDecoderContext->coded_width;
|
2019-11-06 17:24:51 +03:00
|
|
|
frames->sw_format =
|
|
|
|
(sys->codecDecoderContext->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ? AV_PIX_FMT_P010
|
|
|
|
: AV_PIX_FMT_NV12);
|
2018-02-02 23:49:59 +03:00
|
|
|
frames->initial_pool_size = 20;
|
|
|
|
|
|
|
|
if (sys->codecDecoderContext->active_thread_type & FF_THREAD_FRAME)
|
|
|
|
frames->initial_pool_size += sys->codecDecoderContext->thread_count;
|
|
|
|
|
|
|
|
int err = av_hwframe_ctx_init(sys->hw_frames_ctx);
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Could not init hwframes context: %s",
|
|
|
|
av_err2str(err));
|
2018-02-02 23:49:59 +03:00
|
|
|
return AV_PIX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys->codecDecoderContext->hw_frames_ctx = av_buffer_ref(sys->hw_frames_ctx);
|
|
|
|
#endif
|
2018-01-27 09:10:51 +03:00
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AV_PIX_FMT_NONE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-06-04 12:23:14 +03:00
|
|
|
static BOOL libavcodec_init(H264_CONTEXT* h264)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
H264_CONTEXT_LIBAVCODEC* sys = NULL;
|
2022-03-29 20:05:39 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(h264);
|
2019-11-06 17:24:51 +03:00
|
|
|
sys = (H264_CONTEXT_LIBAVCODEC*)calloc(1, sizeof(H264_CONTEXT_LIBAVCODEC));
|
2017-06-14 17:04:54 +03:00
|
|
|
|
|
|
|
if (!sys)
|
|
|
|
{
|
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
h264->pSystemData = (void*)sys;
|
2021-04-29 14:18:57 +03:00
|
|
|
|
2021-12-17 19:08:08 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
|
2017-06-14 17:04:54 +03:00
|
|
|
avcodec_register_all();
|
2021-04-29 14:18:57 +03:00
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
|
2017-07-20 13:46:04 +03:00
|
|
|
if (!h264->Compressor)
|
2017-06-14 17:04:54 +03:00
|
|
|
{
|
2017-07-20 13:46:04 +03:00
|
|
|
sys->codecDecoder = avcodec_find_decoder(AV_CODEC_ID_H264);
|
|
|
|
|
|
|
|
if (!sys->codecDecoder)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to find libav H.264 codec");
|
2017-07-20 13:46:04 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys->codecDecoderContext = avcodec_alloc_context3(sys->codecDecoder);
|
|
|
|
|
|
|
|
if (!sys->codecDecoderContext)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate libav codec context");
|
2017-07-20 13:46:04 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2023-02-14 09:48:54 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
|
2017-12-28 18:41:45 +03:00
|
|
|
if (sys->codecDecoder->capabilities & AV_CODEC_CAP_TRUNCATED)
|
2017-07-20 13:46:04 +03:00
|
|
|
{
|
2017-12-28 18:41:45 +03:00
|
|
|
sys->codecDecoderContext->flags |= AV_CODEC_FLAG_TRUNCATED;
|
2017-07-20 13:46:04 +03:00
|
|
|
}
|
2023-02-14 09:48:54 +03:00
|
|
|
#endif
|
2017-07-20 13:46:04 +03:00
|
|
|
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
|
|
|
|
|
|
|
if (!sys->hwctx)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
int ret =
|
|
|
|
av_hwdevice_ctx_create(&sys->hwctx, AV_HWDEVICE_TYPE_VAAPI, VAAPI_DEVICE, NULL, 0);
|
2018-01-27 09:10:51 +03:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-04-05 11:48:45 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR,
|
|
|
|
"Could not initialize hardware decoder, falling back to software: %s",
|
|
|
|
av_err2str(ret));
|
2018-02-02 22:30:31 +03:00
|
|
|
sys->hwctx = NULL;
|
|
|
|
goto fail_hwdevice_create;
|
2018-01-27 09:10:51 +03:00
|
|
|
}
|
|
|
|
}
|
2024-04-17 22:56:37 +03:00
|
|
|
WLog_Print(h264->log, WLOG_INFO, "Using VAAPI for accelerated H264 decoding");
|
2018-01-27 09:10:51 +03:00
|
|
|
|
|
|
|
sys->codecDecoderContext->get_format = libavcodec_get_format;
|
|
|
|
sys->hw_pix_fmt = AV_PIX_FMT_VAAPI;
|
2018-02-02 23:49:59 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 80, 100)
|
2018-01-27 09:10:51 +03:00
|
|
|
sys->codecDecoderContext->hw_device_ctx = av_buffer_ref(sys->hwctx);
|
2018-02-02 23:49:59 +03:00
|
|
|
#endif
|
2019-11-06 17:24:51 +03:00
|
|
|
sys->codecDecoderContext->opaque = (void*)h264;
|
2018-02-02 22:30:31 +03:00
|
|
|
fail_hwdevice_create:
|
2018-01-27 09:10:51 +03:00
|
|
|
#endif
|
|
|
|
|
2017-07-20 13:46:04 +03:00
|
|
|
if (avcodec_open2(sys->codecDecoderContext, sys->codecDecoder, NULL) < 0)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to open libav codec");
|
2017-07-20 13:46:04 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys->codecParser = av_parser_init(AV_CODEC_ID_H264);
|
|
|
|
|
|
|
|
if (!sys->codecParser)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to initialize libav parser");
|
2017-07-20 13:46:04 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
2017-06-14 17:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 18, 102)
|
|
|
|
sys->videoFrame = av_frame_alloc();
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
|
|
|
sys->hwVideoFrame = av_frame_alloc();
|
|
|
|
#endif
|
2017-06-14 17:04:54 +03:00
|
|
|
#else
|
|
|
|
sys->videoFrame = avcodec_alloc_frame();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!sys->videoFrame)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate libav frame");
|
2017-06-14 17:04:54 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2018-01-27 09:10:51 +03:00
|
|
|
#ifdef WITH_VAAPI
|
|
|
|
|
|
|
|
if (!sys->hwVideoFrame)
|
|
|
|
{
|
2018-01-09 19:22:56 +03:00
|
|
|
WLog_Print(h264->log, WLOG_ERROR, "Failed to allocate libav hw frame");
|
2018-01-27 09:10:51 +03:00
|
|
|
goto EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2017-07-20 13:46:04 +03:00
|
|
|
sys->videoFrame->pts = 0;
|
2017-06-14 17:04:54 +03:00
|
|
|
return TRUE;
|
|
|
|
EXCEPTION:
|
|
|
|
libavcodec_uninit(h264);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-04-28 13:58:22 +03:00
|
|
|
const H264_CONTEXT_SUBSYSTEM g_Subsystem_libavcodec = { "libavcodec", libavcodec_init,
|
|
|
|
libavcodec_uninit, libavcodec_decompress,
|
|
|
|
libavcodec_compress };
|