20 #include <freerdp/config.h>
26 #include <winpr/crt.h>
28 #include <freerdp/channels/log.h>
29 #include <freerdp/client/tsmf.h>
31 #include <libavcodec/avcodec.h>
32 #include <libavutil/common.h>
33 #include <libavutil/cpu.h>
34 #include <libavutil/imgutils.h>
36 #include "tsmf_constants.h"
37 #include "tsmf_decoder.h"
38 #include "tsmf_audio.h"
41 #if LIBAVUTIL_VERSION_MAJOR < 50
42 #define AVMEDIA_TYPE_VIDEO 0
43 #define AVMEDIA_TYPE_AUDIO 1
46 #if LIBAVCODEC_VERSION_MAJOR < 54
47 #define MAX_AUDIO_FRAME_SIZE AVCODEC_MAX_AUDIO_FRAME_SIZE
49 #define MAX_AUDIO_FRAME_SIZE 192000
52 #if LIBAVCODEC_VERSION_MAJOR < 55
53 #define AV_CODEC_ID_VC1 CODEC_ID_VC1
54 #define AV_CODEC_ID_WMAV2 CODEC_ID_WMAV2
55 #define AV_CODEC_ID_WMAPRO CODEC_ID_WMAPRO
56 #define AV_CODEC_ID_MP3 CODEC_ID_MP3
57 #define AV_CODEC_ID_MP2 CODEC_ID_MP2
58 #define AV_CODEC_ID_MPEG2VIDEO CODEC_ID_MPEG2VIDEO
59 #define AV_CODEC_ID_WMV3 CODEC_ID_WMV3
60 #define AV_CODEC_ID_AAC CODEC_ID_AAC
61 #define AV_CODEC_ID_H264 CODEC_ID_H264
62 #define AV_CODEC_ID_AC3 CODEC_ID_AC3
65 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 34, 2)
66 #define AV_CODEC_CAP_TRUNCATED CODEC_CAP_TRUNCATED
67 #define AV_CODEC_FLAG_TRUNCATED CODEC_FLAG_TRUNCATED
70 #if LIBAVUTIL_VERSION_MAJOR < 52
71 #define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
79 #if LIBAVCODEC_VERSION_MAJOR < 55
80 enum CodecID codec_id;
82 enum AVCodecID codec_id;
84 AVCodecContext* codec_context;
91 UINT32 decoded_size_max;
94 static BOOL tsmf_ffmpeg_init_context(ITSMFDecoder* decoder)
96 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
97 mdecoder->codec_context = avcodec_alloc_context3(NULL);
99 if (!mdecoder->codec_context)
101 WLog_ERR(TAG,
"avcodec_alloc_context failed.");
108 static BOOL tsmf_ffmpeg_init_video_stream(ITSMFDecoder* decoder,
const TS_AM_MEDIA_TYPE* media_type)
110 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
111 mdecoder->codec_context->width = media_type->Width;
112 mdecoder->codec_context->height = media_type->Height;
113 mdecoder->codec_context->bit_rate = media_type->BitRate;
114 mdecoder->codec_context->time_base.den = media_type->SamplesPerSecond.Numerator;
115 mdecoder->codec_context->time_base.num = media_type->SamplesPerSecond.Denominator;
116 #if LIBAVCODEC_VERSION_MAJOR < 55
117 mdecoder->frame = avcodec_alloc_frame();
119 mdecoder->frame = av_frame_alloc();
124 static BOOL tsmf_ffmpeg_init_audio_stream(ITSMFDecoder* decoder,
const TS_AM_MEDIA_TYPE* media_type)
126 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
127 mdecoder->codec_context->sample_rate = media_type->SamplesPerSecond.Numerator;
128 mdecoder->codec_context->bit_rate = media_type->BitRate;
129 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
130 mdecoder->codec_context->ch_layout.nb_channels = media_type->Channels;
132 mdecoder->codec_context->channels = media_type->Channels;
134 mdecoder->codec_context->block_align = media_type->BlockAlign;
135 #if LIBAVCODEC_VERSION_MAJOR < 55
136 #ifdef AV_CPU_FLAG_SSE2
137 mdecoder->codec_context->dsp_mask = AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMX2;
139 #if LIBAVCODEC_VERSION_MAJOR < 53
140 mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMXEXT;
142 mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMX2;
146 #ifdef AV_CPU_FLAG_SSE2
147 #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 17, 100)
148 av_set_cpu_flags_mask(AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMXEXT);
150 av_force_cpu_flags(AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMXEXT);
153 av_set_cpu_flags_mask(FF_MM_SSE2 | FF_MM_MMX2);
159 static BOOL tsmf_ffmpeg_init_stream(ITSMFDecoder* decoder,
const TS_AM_MEDIA_TYPE* media_type)
163 const BYTE* s = NULL;
164 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
166 WINPR_PRAGMA_DIAG_PUSH
167 WINPR_PRAGMA_DIAG_IGNORED_QUALIFIERS
168 mdecoder->codec = avcodec_find_decoder(mdecoder->codec_id);
169 WINPR_PRAGMA_DIAG_POP
171 if (!mdecoder->codec)
173 WLog_ERR(TAG,
"avcodec_find_decoder failed.");
177 mdecoder->codec_context->codec_id = mdecoder->codec_id;
178 mdecoder->codec_context->codec_type = mdecoder->media_type;
180 switch (mdecoder->media_type)
182 case AVMEDIA_TYPE_VIDEO:
183 if (!tsmf_ffmpeg_init_video_stream(decoder, media_type))
188 case AVMEDIA_TYPE_AUDIO:
189 if (!tsmf_ffmpeg_init_audio_stream(decoder, media_type))
195 WLog_ERR(TAG,
"unknown media_type %d", mdecoder->media_type);
199 if (media_type->ExtraData)
202 mdecoder->codec_context->extradata_size = media_type->ExtraDataSize + 8;
203 mdecoder->codec_context->extradata = calloc(1, mdecoder->codec_context->extradata_size);
205 if (!mdecoder->codec_context->extradata)
208 if (media_type->SubType == TSMF_SUB_TYPE_AVC1 &&
209 media_type->FormatType == TSMF_FORMAT_TYPE_MPEG2VIDEOINFO)
214 p = mdecoder->codec_context->extradata;
215 if ((mdecoder->codec_context->extradata_size < 0) ||
216 ((
size_t)mdecoder->codec_context->extradata_size < required))
219 *p++ = media_type->ExtraData[8];
221 *p++ = media_type->ExtraData[12];
224 s = media_type->ExtraData + 20;
225 size = ((UINT32)(*s)) * 256 + ((UINT32)(*(s + 1)));
226 required += size + 2;
227 if ((mdecoder->codec_context->extradata_size < 0) ||
228 ((size_t)mdecoder->codec_context->extradata_size < required))
230 memcpy(p, s, size + 2);
234 if ((mdecoder->codec_context->extradata_size < 0) ||
235 ((
size_t)mdecoder->codec_context->extradata_size < required))
238 size = ((UINT32)(*s)) * 256 + ((UINT32)(*(s + 1)));
239 required += size + 2;
240 if ((mdecoder->codec_context->extradata_size < 0) ||
241 ((size_t)mdecoder->codec_context->extradata_size < required))
243 memcpy(p, s, size + 2);
247 memcpy(mdecoder->codec_context->extradata, media_type->ExtraData,
248 media_type->ExtraDataSize);
249 if ((mdecoder->codec_context->extradata_size < 0) ||
250 ((
size_t)mdecoder->codec_context->extradata_size <
251 media_type->ExtraDataSize + 8ull))
253 memset(mdecoder->codec_context->extradata + media_type->ExtraDataSize, 0, 8);
257 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
258 if (mdecoder->codec->capabilities & AV_CODEC_CAP_TRUNCATED)
259 mdecoder->codec_context->flags |= AV_CODEC_FLAG_TRUNCATED;
265 static BOOL tsmf_ffmpeg_prepare(ITSMFDecoder* decoder)
267 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
269 if (avcodec_open2(mdecoder->codec_context, mdecoder->codec, NULL) < 0)
271 WLog_ERR(TAG,
"avcodec_open2 failed.");
275 mdecoder->prepared = 1;
279 static BOOL tsmf_ffmpeg_set_format(ITSMFDecoder* decoder,
TS_AM_MEDIA_TYPE* media_type)
281 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
283 WINPR_ASSERT(mdecoder);
284 WINPR_ASSERT(media_type);
286 switch (media_type->MajorType)
288 case TSMF_MAJOR_TYPE_VIDEO:
289 mdecoder->media_type = AVMEDIA_TYPE_VIDEO;
292 case TSMF_MAJOR_TYPE_AUDIO:
293 mdecoder->media_type = AVMEDIA_TYPE_AUDIO;
300 switch (media_type->SubType)
302 case TSMF_SUB_TYPE_WVC1:
303 mdecoder->codec_id = AV_CODEC_ID_VC1;
306 case TSMF_SUB_TYPE_WMA2:
307 mdecoder->codec_id = AV_CODEC_ID_WMAV2;
310 case TSMF_SUB_TYPE_WMA9:
311 mdecoder->codec_id = AV_CODEC_ID_WMAPRO;
314 case TSMF_SUB_TYPE_MP3:
315 mdecoder->codec_id = AV_CODEC_ID_MP3;
318 case TSMF_SUB_TYPE_MP2A:
319 mdecoder->codec_id = AV_CODEC_ID_MP2;
322 case TSMF_SUB_TYPE_MP2V:
323 mdecoder->codec_id = AV_CODEC_ID_MPEG2VIDEO;
326 case TSMF_SUB_TYPE_WMV3:
327 mdecoder->codec_id = AV_CODEC_ID_WMV3;
330 case TSMF_SUB_TYPE_AAC:
331 mdecoder->codec_id = AV_CODEC_ID_AAC;
336 if (media_type->ExtraData)
338 if (media_type->ExtraDataSize < 12)
341 media_type->ExtraData += 12;
342 media_type->ExtraDataSize -= 12;
347 case TSMF_SUB_TYPE_H264:
348 case TSMF_SUB_TYPE_AVC1:
349 mdecoder->codec_id = AV_CODEC_ID_H264;
352 case TSMF_SUB_TYPE_AC3:
353 mdecoder->codec_id = AV_CODEC_ID_AC3;
360 if (!tsmf_ffmpeg_init_context(decoder))
363 if (!tsmf_ffmpeg_init_stream(decoder, media_type))
366 if (!tsmf_ffmpeg_prepare(decoder))
372 static BOOL tsmf_ffmpeg_decode_video(ITSMFDecoder* decoder,
const BYTE* data, UINT32 data_size,
375 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
378 AVFrame* frame = NULL;
380 #if LIBAVCODEC_VERSION_MAJOR < 52 || \
381 (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
382 len = avcodec_decode_video(mdecoder->codec_context, mdecoder->frame, &decoded, data, data_size);
385 AVPacket pkt = { 0 };
386 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
387 av_init_packet(&pkt);
389 pkt.data = WINPR_CAST_CONST_PTR_AWAY(data, BYTE*);
390 pkt.size = data_size;
392 if (extensions & TSMM_SAMPLE_EXT_CLEANPOINT)
393 pkt.flags |= AV_PKT_FLAG_KEY;
395 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
396 len = avcodec_decode_video2(mdecoder->codec_context, mdecoder->frame, &decoded, &pkt);
398 len = avcodec_send_packet(mdecoder->codec_context, &pkt);
403 len = avcodec_receive_frame(mdecoder->codec_context, mdecoder->frame);
404 }
while (len == AVERROR(EAGAIN));
412 WLog_ERR(TAG,
"data_size %" PRIu32
", avcodec_decode_video failed (%d)", data_size, len);
417 WLog_ERR(TAG,
"data_size %" PRIu32
", no frame is decoded.", data_size);
422 DEBUG_TSMF(
"linesize[0] %d linesize[1] %d linesize[2] %d linesize[3] %d "
423 "pix_fmt %d width %d height %d",
424 mdecoder->frame->linesize[0], mdecoder->frame->linesize[1],
425 mdecoder->frame->linesize[2], mdecoder->frame->linesize[3],
426 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
427 mdecoder->codec_context->height);
428 mdecoder->decoded_size = av_image_get_buffer_size(mdecoder->codec_context->pix_fmt,
429 mdecoder->codec_context->width,
430 mdecoder->codec_context->height, 1);
431 mdecoder->decoded_data = calloc(1, mdecoder->decoded_size);
433 if (!mdecoder->decoded_data)
436 #if LIBAVCODEC_VERSION_MAJOR < 55
437 frame = avcodec_alloc_frame();
439 frame = av_frame_alloc();
441 av_image_fill_arrays(frame->data, frame->linesize, mdecoder->decoded_data,
442 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
443 mdecoder->codec_context->height, 1);
445 const uint8_t*
const* ptr = (
const uint8_t*
const*)mdecoder->frame->data;
446 av_image_copy(frame->data, frame->linesize, ptr, mdecoder->frame->linesize,
447 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
448 mdecoder->codec_context->height);
455 static BOOL tsmf_ffmpeg_decode_audio(ITSMFDecoder* decoder,
const BYTE* data, UINT32 data_size,
458 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
463 WLog_DBG(TAG, (
"tsmf_ffmpeg_decode_audio: data_size %"PRIu32
"", data_size));
465 for (
int i = 0; i < data_size; i++)
467 WLog_DBG(TAG, (
"%02"PRIX8
"", data[i]));
470 WLog_DBG(TAG, (
"\n"));
475 if (mdecoder->decoded_size_max == 0)
476 mdecoder->decoded_size_max = MAX_AUDIO_FRAME_SIZE + 16;
478 mdecoder->decoded_data = calloc(1, mdecoder->decoded_size_max);
480 if (!mdecoder->decoded_data)
484 BYTE* dst = (BYTE*)(((uintptr_t)mdecoder->decoded_data + 15) & ~0x0F);
485 size_t dst_offset = (size_t)(dst - mdecoder->decoded_data);
486 const BYTE* src = data;
487 UINT32 src_size = data_size;
492 if (mdecoder->decoded_size_max - mdecoder->decoded_size < MAX_AUDIO_FRAME_SIZE)
494 BYTE* tmp_data = NULL;
495 tmp_data = realloc(mdecoder->decoded_data, mdecoder->decoded_size_max * 2 + 16);
500 mdecoder->decoded_size_max = mdecoder->decoded_size_max * 2 + 16;
501 mdecoder->decoded_data = tmp_data;
502 dst = (BYTE*)(((uintptr_t)mdecoder->decoded_data + 15) & ~0x0F);
504 const size_t diff = (size_t)(dst - mdecoder->decoded_data);
505 if (diff != dst_offset)
508 memmove(dst, mdecoder->decoded_data + dst_offset, mdecoder->decoded_size);
512 dst += mdecoder->decoded_size;
515 frame_size = mdecoder->decoded_size_max - mdecoder->decoded_size;
516 #if LIBAVCODEC_VERSION_MAJOR < 52 || \
517 (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
518 len = avcodec_decode_audio2(mdecoder->codec_context, (int16_t*)dst, &frame_size, src,
522 #if LIBAVCODEC_VERSION_MAJOR < 55
523 AVFrame* decoded_frame = avcodec_alloc_frame();
525 AVFrame* decoded_frame = av_frame_alloc();
528 AVPacket pkt = { 0 };
529 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
530 av_init_packet(&pkt);
533 pkt.data = WINPR_CAST_CONST_PTR_AWAY(src, BYTE*);
535 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
536 len = avcodec_decode_audio4(mdecoder->codec_context, decoded_frame, &got_frame, &pkt);
538 len = avcodec_send_packet(mdecoder->codec_context, &pkt);
543 len = avcodec_receive_frame(mdecoder->codec_context, decoded_frame);
544 }
while (len == AVERROR(EAGAIN));
548 if (len >= 0 && got_frame)
550 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
551 const int channels = mdecoder->codec_context->ch_layout.nb_channels;
553 const int channels = mdecoder->codec_context->channels;
555 frame_size = av_samples_get_buffer_size(NULL, channels, decoded_frame->nb_samples,
556 mdecoder->codec_context->sample_fmt, 1);
557 memcpy(dst, decoded_frame->data[0], frame_size);
564 av_free(decoded_frame);
576 mdecoder->decoded_size += frame_size;
581 if (mdecoder->decoded_size == 0)
583 free(mdecoder->decoded_data);
584 mdecoder->decoded_data = NULL;
589 memmove(mdecoder->decoded_data, mdecoder->decoded_data + dst_offset,
590 mdecoder->decoded_size);
593 DEBUG_TSMF(
"data_size %" PRIu32
" decoded_size %" PRIu32
"", data_size, mdecoder->decoded_size);
597 static BOOL tsmf_ffmpeg_decode(ITSMFDecoder* decoder,
const BYTE* data, UINT32 data_size,
600 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
602 if (mdecoder->decoded_data)
604 free(mdecoder->decoded_data);
605 mdecoder->decoded_data = NULL;
608 mdecoder->decoded_size = 0;
610 switch (mdecoder->media_type)
612 case AVMEDIA_TYPE_VIDEO:
613 return tsmf_ffmpeg_decode_video(decoder, data, data_size, extensions);
615 case AVMEDIA_TYPE_AUDIO:
616 return tsmf_ffmpeg_decode_audio(decoder, data, data_size, extensions);
619 WLog_ERR(TAG,
"unknown media type.");
624 static BYTE* tsmf_ffmpeg_get_decoded_data(ITSMFDecoder* decoder, UINT32* size)
627 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
628 *size = mdecoder->decoded_size;
629 buf = mdecoder->decoded_data;
630 mdecoder->decoded_data = NULL;
631 mdecoder->decoded_size = 0;
635 static UINT32 tsmf_ffmpeg_get_decoded_format(ITSMFDecoder* decoder)
637 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
639 switch (mdecoder->codec_context->pix_fmt)
641 case AV_PIX_FMT_YUV420P:
642 return RDP_PIXFMT_I420;
645 WLog_ERR(TAG,
"unsupported pixel format %u", mdecoder->codec_context->pix_fmt);
650 static BOOL tsmf_ffmpeg_get_decoded_dimension(ITSMFDecoder* decoder, UINT32* width, UINT32* height)
652 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
654 if (mdecoder->codec_context->width > 0 && mdecoder->codec_context->height > 0)
656 *width = mdecoder->codec_context->width;
657 *height = mdecoder->codec_context->height;
666 static void tsmf_ffmpeg_free(ITSMFDecoder* decoder)
668 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
671 av_free(mdecoder->frame);
673 free(mdecoder->decoded_data);
675 if (mdecoder->codec_context)
677 if (mdecoder->prepared)
678 avcodec_close(mdecoder->codec_context);
680 free(mdecoder->codec_context->extradata);
681 av_free(mdecoder->codec_context);
687 static INIT_ONCE g_Initialized = INIT_ONCE_STATIC_INIT;
688 static BOOL CALLBACK InitializeAvCodecs(
PINIT_ONCE once, PVOID param, PVOID* context)
690 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
691 avcodec_register_all();
696 FREERDP_ENTRY_POINT(UINT VCAPITYPE ffmpeg_freerdp_tsmf_client_decoder_subsystem_entry(
void* ptr))
698 ITSMFDecoder** sptr = (ITSMFDecoder**)ptr;
702 TSMFFFmpegDecoder* decoder = NULL;
703 InitOnceExecuteOnce(&g_Initialized, InitializeAvCodecs, NULL, NULL);
704 WLog_DBG(TAG,
"TSMFDecoderEntry FFMPEG");
705 decoder = (TSMFFFmpegDecoder*)calloc(1,
sizeof(TSMFFFmpegDecoder));
708 return ERROR_OUTOFMEMORY;
710 decoder->iface.SetFormat = tsmf_ffmpeg_set_format;
711 decoder->iface.Decode = tsmf_ffmpeg_decode;
712 decoder->iface.GetDecodedData = tsmf_ffmpeg_get_decoded_data;
713 decoder->iface.GetDecodedFormat = tsmf_ffmpeg_get_decoded_format;
714 decoder->iface.GetDecodedDimension = tsmf_ffmpeg_get_decoded_dimension;
715 decoder->iface.Free = tsmf_ffmpeg_free;
716 *sptr = &decoder->iface;
717 return CHANNEL_RC_OK;