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* ptr[AV_NUM_DATA_POINTERS] = { 0 };
446 for (
size_t x = 0; x < AV_NUM_DATA_POINTERS; x++)
447 ptr[x] = mdecoder->frame->data[x];
449 av_image_copy(frame->data, frame->linesize, ptr, mdecoder->frame->linesize,
450 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
451 mdecoder->codec_context->height);
458 static BOOL tsmf_ffmpeg_decode_audio(ITSMFDecoder* decoder,
const BYTE* data, UINT32 data_size,
461 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
466 WLog_DBG(TAG, (
"tsmf_ffmpeg_decode_audio: data_size %"PRIu32
"", data_size));
468 for (
int i = 0; i < data_size; i++)
470 WLog_DBG(TAG, (
"%02"PRIX8
"", data[i]));
473 WLog_DBG(TAG, (
"\n"));
478 if (mdecoder->decoded_size_max == 0)
479 mdecoder->decoded_size_max = MAX_AUDIO_FRAME_SIZE + 16;
481 mdecoder->decoded_data = calloc(1, mdecoder->decoded_size_max);
483 if (!mdecoder->decoded_data)
487 BYTE* dst = (BYTE*)(((uintptr_t)mdecoder->decoded_data + 15) & ~0x0F);
488 size_t dst_offset = (size_t)(dst - mdecoder->decoded_data);
489 const BYTE* src = data;
490 UINT32 src_size = data_size;
495 if (mdecoder->decoded_size_max - mdecoder->decoded_size < MAX_AUDIO_FRAME_SIZE)
497 BYTE* tmp_data = NULL;
498 tmp_data = realloc(mdecoder->decoded_data, mdecoder->decoded_size_max * 2 + 16);
503 mdecoder->decoded_size_max = mdecoder->decoded_size_max * 2 + 16;
504 mdecoder->decoded_data = tmp_data;
505 dst = (BYTE*)(((uintptr_t)mdecoder->decoded_data + 15) & ~0x0F);
507 const size_t diff = (size_t)(dst - mdecoder->decoded_data);
508 if (diff != dst_offset)
511 memmove(dst, mdecoder->decoded_data + dst_offset, mdecoder->decoded_size);
515 dst += mdecoder->decoded_size;
518 frame_size = mdecoder->decoded_size_max - mdecoder->decoded_size;
519 #if LIBAVCODEC_VERSION_MAJOR < 52 || \
520 (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
521 len = avcodec_decode_audio2(mdecoder->codec_context, (int16_t*)dst, &frame_size, src,
525 #if LIBAVCODEC_VERSION_MAJOR < 55
526 AVFrame* decoded_frame = avcodec_alloc_frame();
528 AVFrame* decoded_frame = av_frame_alloc();
531 AVPacket pkt = { 0 };
532 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
533 av_init_packet(&pkt);
536 pkt.data = WINPR_CAST_CONST_PTR_AWAY(src, BYTE*);
538 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
539 len = avcodec_decode_audio4(mdecoder->codec_context, decoded_frame, &got_frame, &pkt);
541 len = avcodec_send_packet(mdecoder->codec_context, &pkt);
546 len = avcodec_receive_frame(mdecoder->codec_context, decoded_frame);
547 }
while (len == AVERROR(EAGAIN));
551 if (len >= 0 && got_frame)
553 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
554 const int channels = mdecoder->codec_context->ch_layout.nb_channels;
556 const int channels = mdecoder->codec_context->channels;
558 frame_size = av_samples_get_buffer_size(NULL, channels, decoded_frame->nb_samples,
559 mdecoder->codec_context->sample_fmt, 1);
560 memcpy(dst, decoded_frame->data[0], frame_size);
567 av_free(decoded_frame);
579 mdecoder->decoded_size += frame_size;
584 if (mdecoder->decoded_size == 0)
586 free(mdecoder->decoded_data);
587 mdecoder->decoded_data = NULL;
592 memmove(mdecoder->decoded_data, mdecoder->decoded_data + dst_offset,
593 mdecoder->decoded_size);
596 DEBUG_TSMF(
"data_size %" PRIu32
" decoded_size %" PRIu32
"", data_size, mdecoder->decoded_size);
600 static BOOL tsmf_ffmpeg_decode(ITSMFDecoder* decoder,
const BYTE* data, UINT32 data_size,
603 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
605 if (mdecoder->decoded_data)
607 free(mdecoder->decoded_data);
608 mdecoder->decoded_data = NULL;
611 mdecoder->decoded_size = 0;
613 switch (mdecoder->media_type)
615 case AVMEDIA_TYPE_VIDEO:
616 return tsmf_ffmpeg_decode_video(decoder, data, data_size, extensions);
618 case AVMEDIA_TYPE_AUDIO:
619 return tsmf_ffmpeg_decode_audio(decoder, data, data_size, extensions);
622 WLog_ERR(TAG,
"unknown media type.");
627 static BYTE* tsmf_ffmpeg_get_decoded_data(ITSMFDecoder* decoder, UINT32* size)
630 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
631 *size = mdecoder->decoded_size;
632 buf = mdecoder->decoded_data;
633 mdecoder->decoded_data = NULL;
634 mdecoder->decoded_size = 0;
638 static UINT32 tsmf_ffmpeg_get_decoded_format(ITSMFDecoder* decoder)
640 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
642 switch (mdecoder->codec_context->pix_fmt)
644 case AV_PIX_FMT_YUV420P:
645 return RDP_PIXFMT_I420;
648 WLog_ERR(TAG,
"unsupported pixel format %u", mdecoder->codec_context->pix_fmt);
653 static BOOL tsmf_ffmpeg_get_decoded_dimension(ITSMFDecoder* decoder, UINT32* width, UINT32* height)
655 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
657 if (mdecoder->codec_context->width > 0 && mdecoder->codec_context->height > 0)
659 *width = mdecoder->codec_context->width;
660 *height = mdecoder->codec_context->height;
669 static void tsmf_ffmpeg_free(ITSMFDecoder* decoder)
671 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
674 av_free(mdecoder->frame);
676 free(mdecoder->decoded_data);
678 if (mdecoder->codec_context)
680 if (mdecoder->prepared)
681 avcodec_close(mdecoder->codec_context);
683 free(mdecoder->codec_context->extradata);
684 av_free(mdecoder->codec_context);
690 static INIT_ONCE g_Initialized = INIT_ONCE_STATIC_INIT;
691 static BOOL CALLBACK InitializeAvCodecs(
PINIT_ONCE once, PVOID param, PVOID* context)
693 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
694 avcodec_register_all();
699 FREERDP_ENTRY_POINT(UINT VCAPITYPE ffmpeg_freerdp_tsmf_client_decoder_subsystem_entry(
void* ptr))
701 ITSMFDecoder** sptr = (ITSMFDecoder**)ptr;
705 TSMFFFmpegDecoder* decoder = NULL;
706 InitOnceExecuteOnce(&g_Initialized, InitializeAvCodecs, NULL, NULL);
707 WLog_DBG(TAG,
"TSMFDecoderEntry FFMPEG");
708 decoder = (TSMFFFmpegDecoder*)calloc(1,
sizeof(TSMFFFmpegDecoder));
711 return ERROR_OUTOFMEMORY;
713 decoder->iface.SetFormat = tsmf_ffmpeg_set_format;
714 decoder->iface.Decode = tsmf_ffmpeg_decode;
715 decoder->iface.GetDecodedData = tsmf_ffmpeg_get_decoded_data;
716 decoder->iface.GetDecodedFormat = tsmf_ffmpeg_get_decoded_format;
717 decoder->iface.GetDecodedDimension = tsmf_ffmpeg_get_decoded_dimension;
718 decoder->iface.Free = tsmf_ffmpeg_free;
719 *sptr = &decoder->iface;
720 return CHANNEL_RC_OK;