2011-09-20 10:27:10 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-09-20 10:27:10 +04:00
|
|
|
* Video Redirection Virtual Channel - FFmpeg Decoder
|
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-11-20 07:31:15 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2014-08-11 11:12:01 +04:00
|
|
|
#include <freerdp/channels/log.h>
|
2012-10-09 04:33:58 +04:00
|
|
|
#include <freerdp/client/tsmf.h>
|
2013-03-21 22:45:03 +04:00
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
#include <libavcodec/avcodec.h>
|
2014-08-07 18:51:49 +04:00
|
|
|
#include <libavutil/common.h>
|
2011-09-20 10:27:10 +04:00
|
|
|
|
|
|
|
#include "tsmf_constants.h"
|
|
|
|
#include "tsmf_decoder.h"
|
|
|
|
|
|
|
|
/* Compatibility with older FFmpeg */
|
|
|
|
#if LIBAVUTIL_VERSION_MAJOR < 50
|
|
|
|
#define AVMEDIA_TYPE_VIDEO 0
|
|
|
|
#define AVMEDIA_TYPE_AUDIO 1
|
|
|
|
#endif
|
|
|
|
|
2013-11-21 14:35:53 +04:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 54
|
|
|
|
#define MAX_AUDIO_FRAME_SIZE AVCODEC_MAX_AUDIO_FRAME_SIZE
|
|
|
|
#else
|
|
|
|
#define MAX_AUDIO_FRAME_SIZE 192000
|
|
|
|
#endif
|
|
|
|
|
2014-08-08 16:05:06 +04:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 55
|
|
|
|
#define AV_CODEC_ID_VC1 CODEC_ID_VC1
|
|
|
|
#define AV_CODEC_ID_WMAV2 CODEC_ID_WMAV2
|
|
|
|
#define AV_CODEC_ID_WMAPRO CODEC_ID_WMAPRO
|
|
|
|
#define AV_CODEC_ID_MP3 CODEC_ID_MP3
|
|
|
|
#define AV_CODEC_ID_MP2 CODEC_ID_MP2
|
|
|
|
#define AV_CODEC_ID_MPEG2VIDEO CODEC_ID_MPEG2VIDEO
|
|
|
|
#define AV_CODEC_ID_WMV3 CODEC_ID_WMV3
|
|
|
|
#define AV_CODEC_ID_AAC CODEC_ID_AAC
|
|
|
|
#define AV_CODEC_ID_H264 CODEC_ID_H264
|
|
|
|
#define AV_CODEC_ID_AC3 CODEC_ID_AC3
|
|
|
|
#endif
|
|
|
|
|
2015-09-17 11:36:44 +03:00
|
|
|
#if LIBAVUTIL_VERSION_MAJOR < 52
|
|
|
|
#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
|
|
|
|
#endif
|
2014-08-08 16:05:06 +04:00
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
typedef struct _TSMFFFmpegDecoder
|
|
|
|
{
|
|
|
|
ITSMFDecoder iface;
|
|
|
|
|
|
|
|
int media_type;
|
2013-11-21 16:26:03 +04:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 55
|
2011-09-20 10:27:10 +04:00
|
|
|
enum CodecID codec_id;
|
2013-11-21 16:26:03 +04:00
|
|
|
#else
|
|
|
|
enum AVCodecID codec_id;
|
|
|
|
#endif
|
2014-05-23 15:12:34 +04:00
|
|
|
AVCodecContext *codec_context;
|
|
|
|
AVCodec *codec;
|
|
|
|
AVFrame *frame;
|
2011-09-20 10:27:10 +04:00
|
|
|
int prepared;
|
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
BYTE *decoded_data;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 decoded_size;
|
|
|
|
UINT32 decoded_size_max;
|
2011-09-20 10:27:10 +04:00
|
|
|
} TSMFFFmpegDecoder;
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_init_context(ITSMFDecoder* decoder)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
2012-09-14 07:27:46 +04:00
|
|
|
mdecoder->codec_context = avcodec_alloc_context3(NULL);
|
2014-11-10 22:02:54 +03:00
|
|
|
if (!mdecoder->codec_context)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "avcodec_alloc_context failed.");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_init_video_stream(ITSMFDecoder* decoder, const TS_AM_MEDIA_TYPE *media_type)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->codec_context->width = media_type->Width;
|
|
|
|
mdecoder->codec_context->height = media_type->Height;
|
|
|
|
mdecoder->codec_context->bit_rate = media_type->BitRate;
|
|
|
|
mdecoder->codec_context->time_base.den = media_type->SamplesPerSecond.Numerator;
|
|
|
|
mdecoder->codec_context->time_base.num = media_type->SamplesPerSecond.Denominator;
|
2015-09-17 11:36:44 +03:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 55
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->frame = avcodec_alloc_frame();
|
2015-09-17 11:36:44 +03:00
|
|
|
#else
|
|
|
|
mdecoder->frame = av_frame_alloc();
|
|
|
|
#endif
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_init_audio_stream(ITSMFDecoder* decoder, const TS_AM_MEDIA_TYPE *media_type)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->codec_context->sample_rate = media_type->SamplesPerSecond.Numerator;
|
|
|
|
mdecoder->codec_context->bit_rate = media_type->BitRate;
|
|
|
|
mdecoder->codec_context->channels = media_type->Channels;
|
|
|
|
mdecoder->codec_context->block_align = media_type->BlockAlign;
|
2013-11-21 16:14:04 +04:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 55
|
2011-09-20 10:27:10 +04:00
|
|
|
#ifdef AV_CPU_FLAG_SSE2
|
|
|
|
mdecoder->codec_context->dsp_mask = AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMX2;
|
2011-11-10 21:02:10 +04:00
|
|
|
#else
|
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 53
|
|
|
|
mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMXEXT;
|
|
|
|
#else
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMX2;
|
2011-11-10 21:02:10 +04:00
|
|
|
#endif
|
2011-09-20 10:27:10 +04:00
|
|
|
#endif
|
2013-11-21 16:14:04 +04:00
|
|
|
#else /* LIBAVCODEC_VERSION_MAJOR < 55 */
|
|
|
|
#ifdef AV_CPU_FLAG_SSE2
|
|
|
|
av_set_cpu_flags_mask(AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMX2);
|
|
|
|
#else
|
|
|
|
av_set_cpu_flags_mask(FF_MM_SSE2 | FF_MM_MMX2);
|
|
|
|
#endif
|
|
|
|
#endif /* LIBAVCODEC_VERSION_MAJOR < 55 */
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_init_stream(ITSMFDecoder* decoder, const TS_AM_MEDIA_TYPE *media_type)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-05-23 15:12:34 +04:00
|
|
|
BYTE *p;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 size;
|
2014-05-23 15:12:34 +04:00
|
|
|
const BYTE *s;
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
2015-06-29 16:21:53 +03:00
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->codec = avcodec_find_decoder(mdecoder->codec_id);
|
2014-11-10 22:02:54 +03:00
|
|
|
if (!mdecoder->codec)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "avcodec_find_decoder failed.");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
2015-06-29 16:21:53 +03:00
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->codec_context->codec_id = mdecoder->codec_id;
|
|
|
|
mdecoder->codec_context->codec_type = mdecoder->media_type;
|
2015-06-29 16:21:53 +03:00
|
|
|
switch(mdecoder->media_type)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2015-06-29 16:21:53 +03:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
if (!tsmf_ffmpeg_init_video_stream(decoder, media_type))
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2014-11-10 22:02:54 +03:00
|
|
|
if (!tsmf_ffmpeg_init_audio_stream(decoder, media_type))
|
2014-05-23 15:12:34 +04:00
|
|
|
return FALSE;
|
2015-06-29 16:21:53 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "unknown media_type %d", mdecoder->media_type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
if (media_type->ExtraData)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2015-06-29 16:21:53 +03:00
|
|
|
/* Add a padding to avoid invalid memory read in some codec */
|
|
|
|
mdecoder->codec_context->extradata_size = media_type->ExtraDataSize + 8;
|
|
|
|
mdecoder->codec_context->extradata = calloc(1, mdecoder->codec_context->extradata_size);
|
|
|
|
if (!mdecoder->codec_context->extradata)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
if (media_type->SubType == TSMF_SUB_TYPE_AVC1 &&
|
2014-05-23 15:12:34 +04:00
|
|
|
media_type->FormatType == TSMF_FORMAT_TYPE_MPEG2VIDEOINFO)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
/* The extradata format that FFmpeg uses is following CodecPrivate in Matroska.
|
|
|
|
See http://haali.su/mkv/codecs.pdf */
|
|
|
|
p = mdecoder->codec_context->extradata;
|
|
|
|
*p++ = 1; /* Reserved? */
|
|
|
|
*p++ = media_type->ExtraData[8]; /* Profile */
|
|
|
|
*p++ = 0; /* Profile */
|
|
|
|
*p++ = media_type->ExtraData[12]; /* Level */
|
|
|
|
*p++ = 0xff; /* Flag? */
|
|
|
|
*p++ = 0xe0 | 0x01; /* Reserved | #sps */
|
|
|
|
s = media_type->ExtraData + 20;
|
2012-10-09 11:26:39 +04:00
|
|
|
size = ((UINT32)(*s)) * 256 + ((UINT32)(*(s + 1)));
|
2011-09-20 10:27:10 +04:00
|
|
|
memcpy(p, s, size + 2);
|
|
|
|
s += size + 2;
|
|
|
|
p += size + 2;
|
|
|
|
*p++ = 1; /* #pps */
|
2012-10-09 11:26:39 +04:00
|
|
|
size = ((UINT32)(*s)) * 256 + ((UINT32)(*(s + 1)));
|
2011-09-20 10:27:10 +04:00
|
|
|
memcpy(p, s, size + 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(mdecoder->codec_context->extradata, media_type->ExtraData, media_type->ExtraDataSize);
|
|
|
|
memset(mdecoder->codec_context->extradata + media_type->ExtraDataSize, 0, 8);
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 16:21:53 +03:00
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
if (mdecoder->codec->capabilities & CODEC_CAP_TRUNCATED)
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->codec_context->flags |= CODEC_FLAG_TRUNCATED;
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_prepare(ITSMFDecoder* decoder)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
|
|
|
if (avcodec_open2(mdecoder->codec_context, mdecoder->codec, NULL) < 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "avcodec_open2 failed.");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
mdecoder->prepared = 1;
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_set_format(ITSMFDecoder* decoder, TS_AM_MEDIA_TYPE *media_type)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
2014-05-23 15:12:34 +04:00
|
|
|
switch(media_type->MajorType)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
case TSMF_MAJOR_TYPE_VIDEO:
|
|
|
|
mdecoder->media_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
break;
|
|
|
|
case TSMF_MAJOR_TYPE_AUDIO:
|
|
|
|
mdecoder->media_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
break;
|
|
|
|
default:
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
2014-05-23 15:12:34 +04:00
|
|
|
switch(media_type->SubType)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
case TSMF_SUB_TYPE_WVC1:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_VC1;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_WMA2:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_WMAV2;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_WMA9:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_WMAPRO;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_MP3:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_MP3;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_MP2A:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_MP2;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_MP2V:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_MPEG2VIDEO;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_WMV3:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_WMV3;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_AAC:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_AAC;
|
2011-09-20 10:27:10 +04:00
|
|
|
/* For AAC the pFormat is a HEAACWAVEINFO struct, and the codec data
|
|
|
|
is at the end of it. See
|
|
|
|
http://msdn.microsoft.com/en-us/library/dd757806.aspx */
|
2014-11-10 22:02:54 +03:00
|
|
|
if (media_type->ExtraData)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
media_type->ExtraData += 12;
|
|
|
|
media_type->ExtraDataSize -= 12;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_H264:
|
|
|
|
case TSMF_SUB_TYPE_AVC1:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_H264;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
case TSMF_SUB_TYPE_AC3:
|
2014-08-08 12:05:03 +04:00
|
|
|
mdecoder->codec_id = AV_CODEC_ID_AC3;
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
default:
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
2014-11-10 22:02:54 +03:00
|
|
|
if (!tsmf_ffmpeg_init_context(decoder))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2014-11-10 22:02:54 +03:00
|
|
|
if (!tsmf_ffmpeg_init_stream(decoder, media_type))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2014-11-10 22:02:54 +03:00
|
|
|
if (!tsmf_ffmpeg_prepare(decoder))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_decode_video(ITSMFDecoder* decoder, const BYTE *data, UINT32 data_size, UINT32 extensions)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
2011-09-20 10:27:10 +04:00
|
|
|
int decoded;
|
|
|
|
int len;
|
2014-05-23 15:12:34 +04:00
|
|
|
AVFrame *frame;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL ret = TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 52 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
|
|
|
|
len = avcodec_decode_video(mdecoder->codec_context, mdecoder->frame, &decoded, data, data_size);
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
AVPacket pkt;
|
|
|
|
av_init_packet(&pkt);
|
2014-05-23 15:12:34 +04:00
|
|
|
pkt.data = (BYTE *) data;
|
2011-09-20 10:27:10 +04:00
|
|
|
pkt.size = data_size;
|
2014-11-10 22:02:54 +03:00
|
|
|
if (extensions & TSMM_SAMPLE_EXT_CLEANPOINT)
|
2011-09-20 10:27:10 +04:00
|
|
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
|
|
|
len = avcodec_decode_video2(mdecoder->codec_context, mdecoder->frame, &decoded, &pkt);
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-10 22:02:54 +03:00
|
|
|
if (len < 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "data_size %d, avcodec_decode_video failed (%d)", data_size, len);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (!decoded)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "data_size %d, no frame is decoded.", data_size);
|
2012-10-09 10:31:28 +04:00
|
|
|
ret = FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
else
|
2014-09-12 18:19:32 +04:00
|
|
|
{
|
|
|
|
DEBUG_TSMF("linesize[0] %d linesize[1] %d linesize[2] %d linesize[3] %d "
|
|
|
|
"pix_fmt %d width %d height %d",
|
|
|
|
mdecoder->frame->linesize[0], mdecoder->frame->linesize[1],
|
|
|
|
mdecoder->frame->linesize[2], mdecoder->frame->linesize[3],
|
|
|
|
mdecoder->codec_context->pix_fmt,
|
|
|
|
mdecoder->codec_context->width, mdecoder->codec_context->height);
|
|
|
|
mdecoder->decoded_size = avpicture_get_size(mdecoder->codec_context->pix_fmt,
|
|
|
|
mdecoder->codec_context->width, mdecoder->codec_context->height);
|
2015-06-29 16:21:53 +03:00
|
|
|
mdecoder->decoded_data = calloc(1, mdecoder->decoded_size);
|
|
|
|
if (!mdecoder->decoded_data)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-09-17 11:36:44 +03:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 55
|
2014-09-12 18:19:32 +04:00
|
|
|
frame = avcodec_alloc_frame();
|
2015-09-17 11:36:44 +03:00
|
|
|
#else
|
|
|
|
frame = av_frame_alloc();
|
|
|
|
#endif
|
2014-09-12 18:19:32 +04:00
|
|
|
avpicture_fill((AVPicture*) frame, mdecoder->decoded_data,
|
2014-05-23 15:12:34 +04:00
|
|
|
mdecoder->codec_context->pix_fmt,
|
|
|
|
mdecoder->codec_context->width, mdecoder->codec_context->height);
|
2014-09-12 18:19:32 +04:00
|
|
|
av_picture_copy((AVPicture*) frame, (AVPicture*) mdecoder->frame,
|
|
|
|
mdecoder->codec_context->pix_fmt,
|
|
|
|
mdecoder->codec_context->width, mdecoder->codec_context->height);
|
|
|
|
av_free(frame);
|
|
|
|
}
|
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_decode_audio(ITSMFDecoder* decoder, const BYTE *data, UINT32 data_size, UINT32 extensions)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
2011-09-20 10:27:10 +04:00
|
|
|
int len;
|
|
|
|
int frame_size;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 src_size;
|
2014-05-23 15:12:34 +04:00
|
|
|
const BYTE *src;
|
|
|
|
BYTE *dst;
|
2011-09-20 10:27:10 +04:00
|
|
|
int dst_offset;
|
|
|
|
#if 0
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, ("tsmf_ffmpeg_decode_audio: data_size %d", data_size));
|
2011-09-20 10:27:10 +04:00
|
|
|
int i;
|
2014-05-23 15:12:34 +04:00
|
|
|
for(i = 0; i < data_size; i++)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, ("%02X ", data[i]));
|
2014-11-10 22:02:54 +03:00
|
|
|
if (i % 16 == 15)
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, ("\n"));
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
#endif
|
2014-11-10 22:02:54 +03:00
|
|
|
if (mdecoder->decoded_size_max == 0)
|
2013-11-21 14:35:53 +04:00
|
|
|
mdecoder->decoded_size_max = MAX_AUDIO_FRAME_SIZE + 16;
|
2015-06-29 16:21:53 +03:00
|
|
|
|
|
|
|
mdecoder->decoded_data = calloc(1, mdecoder->decoded_size_max);
|
|
|
|
if (!mdecoder->decoded_data)
|
|
|
|
return FALSE;
|
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
/* align the memory for SSE2 needs */
|
2014-05-23 15:12:34 +04:00
|
|
|
dst = (BYTE *)(((uintptr_t) mdecoder->decoded_data + 15) & ~ 0x0F);
|
2011-09-20 10:27:10 +04:00
|
|
|
dst_offset = dst - mdecoder->decoded_data;
|
|
|
|
src = data;
|
|
|
|
src_size = data_size;
|
2014-05-23 15:12:34 +04:00
|
|
|
while(src_size > 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
/* Ensure enough space for decoding */
|
2014-11-10 22:02:54 +03:00
|
|
|
if (mdecoder->decoded_size_max - mdecoder->decoded_size < MAX_AUDIO_FRAME_SIZE)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2015-03-11 06:04:36 +03:00
|
|
|
BYTE *tmp_data;
|
|
|
|
|
|
|
|
tmp_data = realloc(mdecoder->decoded_data, mdecoder->decoded_size_max * 2 + 16);
|
|
|
|
if (!tmp_data)
|
|
|
|
return FALSE;
|
2011-09-20 18:03:05 +04:00
|
|
|
mdecoder->decoded_size_max = mdecoder->decoded_size_max * 2 + 16;
|
2015-03-11 06:04:36 +03:00
|
|
|
mdecoder->decoded_data = tmp_data;
|
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
dst = (BYTE *)(((uintptr_t)mdecoder->decoded_data + 15) & ~ 0x0F);
|
2014-11-10 22:02:54 +03:00
|
|
|
if (dst - mdecoder->decoded_data != dst_offset)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
/* re-align the memory if the alignment has changed after realloc */
|
|
|
|
memmove(dst, mdecoder->decoded_data + dst_offset, mdecoder->decoded_size);
|
|
|
|
dst_offset = dst - mdecoder->decoded_data;
|
|
|
|
}
|
|
|
|
dst += mdecoder->decoded_size;
|
|
|
|
}
|
|
|
|
frame_size = mdecoder->decoded_size_max - mdecoder->decoded_size;
|
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 52 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
|
|
|
|
len = avcodec_decode_audio2(mdecoder->codec_context,
|
2014-05-23 15:12:34 +04:00
|
|
|
(int16_t *) dst, &frame_size, src, src_size);
|
2011-09-20 10:27:10 +04:00
|
|
|
#else
|
|
|
|
{
|
2015-09-17 11:36:44 +03:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 55
|
2014-05-23 15:12:34 +04:00
|
|
|
AVFrame *decoded_frame = avcodec_alloc_frame();
|
2015-09-17 11:36:44 +03:00
|
|
|
#else
|
|
|
|
AVFrame *decoded_frame = av_frame_alloc();
|
|
|
|
#endif
|
2012-09-14 07:27:46 +04:00
|
|
|
int got_frame = 0;
|
2011-09-20 10:27:10 +04:00
|
|
|
AVPacket pkt;
|
|
|
|
av_init_packet(&pkt);
|
2014-05-23 15:12:34 +04:00
|
|
|
pkt.data = (BYTE *) src;
|
2011-09-20 10:27:10 +04:00
|
|
|
pkt.size = src_size;
|
2012-09-14 07:27:46 +04:00
|
|
|
len = avcodec_decode_audio4(mdecoder->codec_context, decoded_frame, &got_frame, &pkt);
|
2014-11-10 22:02:54 +03:00
|
|
|
if (len >= 0 && got_frame)
|
2012-09-14 07:27:46 +04:00
|
|
|
{
|
2012-11-20 07:31:15 +04:00
|
|
|
frame_size = av_samples_get_buffer_size(NULL, mdecoder->codec_context->channels,
|
2014-05-23 15:12:34 +04:00
|
|
|
decoded_frame->nb_samples, mdecoder->codec_context->sample_fmt, 1);
|
2012-09-14 07:27:46 +04:00
|
|
|
memcpy(dst, decoded_frame->data[0], frame_size);
|
|
|
|
}
|
|
|
|
av_free(decoded_frame);
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
#endif
|
2014-11-10 22:02:54 +03:00
|
|
|
if (len <= 0 || frame_size <= 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "error decoding");
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
src += len;
|
|
|
|
src_size -= len;
|
|
|
|
mdecoder->decoded_size += frame_size;
|
|
|
|
dst += frame_size;
|
|
|
|
}
|
2014-11-10 22:02:54 +03:00
|
|
|
if (mdecoder->decoded_size == 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(mdecoder->decoded_data);
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->decoded_data = NULL;
|
|
|
|
}
|
2014-05-23 15:12:34 +04:00
|
|
|
else
|
2014-11-10 22:02:54 +03:00
|
|
|
if (dst_offset)
|
2014-05-23 15:12:34 +04:00
|
|
|
{
|
|
|
|
/* move the aligned decoded data to original place */
|
|
|
|
memmove(mdecoder->decoded_data, mdecoder->decoded_data + dst_offset, mdecoder->decoded_size);
|
|
|
|
}
|
|
|
|
DEBUG_TSMF("data_size %d decoded_size %d",
|
|
|
|
data_size, mdecoder->decoded_size);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_decode(ITSMFDecoder* decoder, const BYTE *data, UINT32 data_size, UINT32 extensions)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
|
|
|
if (mdecoder->decoded_data)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(mdecoder->decoded_data);
|
2011-09-20 10:27:10 +04:00
|
|
|
mdecoder->decoded_data = NULL;
|
|
|
|
}
|
|
|
|
mdecoder->decoded_size = 0;
|
2014-05-23 15:12:34 +04:00
|
|
|
switch(mdecoder->media_type)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
return tsmf_ffmpeg_decode_video(decoder, data, data_size, extensions);
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
return tsmf_ffmpeg_decode_audio(decoder, data, data_size, extensions);
|
|
|
|
default:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "unknown media type.");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BYTE *tsmf_ffmpeg_get_decoded_data(ITSMFDecoder* decoder, UINT32 *size)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-05-23 15:12:34 +04:00
|
|
|
BYTE *buf;
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
2011-09-20 10:27:10 +04:00
|
|
|
*size = mdecoder->decoded_size;
|
|
|
|
buf = mdecoder->decoded_data;
|
|
|
|
mdecoder->decoded_data = NULL;
|
|
|
|
mdecoder->decoded_size = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static UINT32 tsmf_ffmpeg_get_decoded_format(ITSMFDecoder* decoder)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
|
|
|
|
|
|
|
switch (mdecoder->codec_context->pix_fmt)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2015-09-17 11:36:44 +03:00
|
|
|
case AV_PIX_FMT_YUV420P:
|
2011-09-20 10:27:10 +04:00
|
|
|
return RDP_PIXFMT_I420;
|
|
|
|
default:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "unsupported pixel format %u",
|
|
|
|
mdecoder->codec_context->pix_fmt);
|
2012-10-09 11:26:39 +04:00
|
|
|
return (UINT32) -1;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static BOOL tsmf_ffmpeg_get_decoded_dimension(ITSMFDecoder* decoder, UINT32 *width, UINT32 *height)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
|
|
|
|
|
|
|
if (mdecoder->codec_context->width > 0 && mdecoder->codec_context->height > 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
*width = mdecoder->codec_context->width;
|
|
|
|
*height = mdecoder->codec_context->height;
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
static void tsmf_ffmpeg_free(ITSMFDecoder* decoder)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
|
|
|
|
if (mdecoder->frame)
|
2011-09-20 10:27:10 +04:00
|
|
|
av_free(mdecoder->frame);
|
2015-05-11 10:07:39 +03:00
|
|
|
|
|
|
|
free(mdecoder->decoded_data);
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
if (mdecoder->codec_context)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
if (mdecoder->prepared)
|
2011-09-20 10:27:10 +04:00
|
|
|
avcodec_close(mdecoder->codec_context);
|
2015-05-11 10:07:39 +03:00
|
|
|
|
|
|
|
free(mdecoder->codec_context->extradata);
|
2011-09-20 10:27:10 +04:00
|
|
|
av_free(mdecoder->codec_context);
|
|
|
|
}
|
2012-10-09 07:21:26 +04:00
|
|
|
free(decoder);
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL initialized = FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
|
2016-06-15 14:36:27 +03:00
|
|
|
#ifdef BUILTIN_CHANNELS
|
2014-09-18 03:19:37 +04:00
|
|
|
#define freerdp_tsmf_client_subsystem_entry ffmpeg_freerdp_tsmf_client_decoder_subsystem_entry
|
2016-03-03 19:11:26 +03:00
|
|
|
#else
|
|
|
|
#define freerdp_tsmf_client_subsystem_entry FREERDP_API freerdp_tsmf_client_decoder_subsystem_entry
|
2012-11-20 07:31:15 +04:00
|
|
|
#endif
|
|
|
|
|
2014-11-10 22:02:54 +03:00
|
|
|
ITSMFDecoder* freerdp_tsmf_client_subsystem_entry(void)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-11-10 22:02:54 +03:00
|
|
|
TSMFFFmpegDecoder* decoder;
|
|
|
|
|
|
|
|
if (!initialized)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
avcodec_register_all();
|
2012-10-09 10:31:28 +04:00
|
|
|
initialized = TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
2014-09-12 18:19:32 +04:00
|
|
|
|
2014-09-15 21:38:37 +04:00
|
|
|
WLog_DBG(TAG, "TSMFDecoderEntry FFMPEG");
|
2014-11-10 22:02:54 +03:00
|
|
|
|
|
|
|
decoder = (TSMFFFmpegDecoder*) calloc(1, sizeof(TSMFFFmpegDecoder));
|
|
|
|
|
|
|
|
if (!decoder)
|
|
|
|
return NULL;
|
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
decoder->iface.SetFormat = tsmf_ffmpeg_set_format;
|
|
|
|
decoder->iface.Decode = tsmf_ffmpeg_decode;
|
|
|
|
decoder->iface.GetDecodedData = tsmf_ffmpeg_get_decoded_data;
|
|
|
|
decoder->iface.GetDecodedFormat = tsmf_ffmpeg_get_decoded_format;
|
|
|
|
decoder->iface.GetDecodedDimension = tsmf_ffmpeg_get_decoded_dimension;
|
|
|
|
decoder->iface.Free = tsmf_ffmpeg_free;
|
2014-11-10 22:02:54 +03:00
|
|
|
|
|
|
|
return (ITSMFDecoder*) decoder;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|