FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
tsmf_ffmpeg.c
1
20#include <freerdp/config.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <winpr/crt.h>
27
28#include <freerdp/channels/log.h>
29#include <freerdp/client/tsmf.h>
30
31#include <libavcodec/avcodec.h>
32#include <libavutil/common.h>
33#include <libavutil/cpu.h>
34#include <libavutil/imgutils.h>
35
36#include "tsmf_constants.h"
37#include "tsmf_decoder.h"
38#include "tsmf_audio.h"
39
40/* Compatibility with older FFmpeg */
41#if LIBAVUTIL_VERSION_MAJOR < 50
42#define AVMEDIA_TYPE_VIDEO 0
43#define AVMEDIA_TYPE_AUDIO 1
44#endif
45
46#if LIBAVCODEC_VERSION_MAJOR < 54
47#define MAX_AUDIO_FRAME_SIZE AVCODEC_MAX_AUDIO_FRAME_SIZE
48#else
49#define MAX_AUDIO_FRAME_SIZE 192000
50#endif
51
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
63#endif
64
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
68#endif
69
70#if LIBAVUTIL_VERSION_MAJOR < 52
71#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
72#endif
73
74typedef struct
75{
76 ITSMFDecoder iface;
77
78 int media_type;
79#if LIBAVCODEC_VERSION_MAJOR < 55
80 enum CodecID codec_id;
81#else
82 enum AVCodecID codec_id;
83#endif
84 AVCodecContext* codec_context;
85 const AVCodec* codec;
86 AVFrame* frame;
87 int prepared;
88
89 BYTE* decoded_data;
90 UINT32 decoded_size;
91 UINT32 decoded_size_max;
92} TSMFFFmpegDecoder;
93
94static BOOL tsmf_ffmpeg_init_context(ITSMFDecoder* decoder)
95{
96 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
97 mdecoder->codec_context = avcodec_alloc_context3(NULL);
98
99 if (!mdecoder->codec_context)
100 {
101 WLog_ERR(TAG, "avcodec_alloc_context failed.");
102 return FALSE;
103 }
104
105 return TRUE;
106}
107
108static BOOL tsmf_ffmpeg_init_video_stream(ITSMFDecoder* decoder, const TS_AM_MEDIA_TYPE* media_type)
109{
110 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
111 mdecoder->codec_context->width = WINPR_ASSERTING_INT_CAST(int, media_type->Width);
112 mdecoder->codec_context->height = WINPR_ASSERTING_INT_CAST(int, media_type->Height);
113 mdecoder->codec_context->bit_rate = WINPR_ASSERTING_INT_CAST(int, media_type->BitRate);
114 mdecoder->codec_context->time_base.den =
115 WINPR_ASSERTING_INT_CAST(int, media_type->SamplesPerSecond.Numerator);
116 mdecoder->codec_context->time_base.num =
117 WINPR_ASSERTING_INT_CAST(int, media_type->SamplesPerSecond.Denominator);
118#if LIBAVCODEC_VERSION_MAJOR < 55
119 mdecoder->frame = avcodec_alloc_frame();
120#else
121 mdecoder->frame = av_frame_alloc();
122#endif
123 return TRUE;
124}
125
126static BOOL tsmf_ffmpeg_init_audio_stream(ITSMFDecoder* decoder, const TS_AM_MEDIA_TYPE* media_type)
127{
128 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
129 mdecoder->codec_context->sample_rate =
130 WINPR_ASSERTING_INT_CAST(int, media_type->SamplesPerSecond.Numerator);
131 mdecoder->codec_context->bit_rate = WINPR_ASSERTING_INT_CAST(int, media_type->BitRate);
132#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
133 mdecoder->codec_context->ch_layout.nb_channels =
134 WINPR_ASSERTING_INT_CAST(int, media_type->Channels);
135#else
136 mdecoder->codec_context->channels = WINPR_ASSERTING_INT_CAST(int, media_type->Channels);
137#endif
138 mdecoder->codec_context->block_align = WINPR_ASSERTING_INT_CAST(int, media_type->BlockAlign);
139#if LIBAVCODEC_VERSION_MAJOR < 55
140#ifdef AV_CPU_FLAG_SSE2
141 mdecoder->codec_context->dsp_mask = AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMX2;
142#else
143#if LIBAVCODEC_VERSION_MAJOR < 53
144 mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMXEXT;
145#else
146 mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMX2;
147#endif
148#endif
149#else /* LIBAVCODEC_VERSION_MAJOR < 55 */
150#ifdef AV_CPU_FLAG_SSE2
151#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 17, 100)
152 av_set_cpu_flags_mask(AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMXEXT);
153#else
154 av_force_cpu_flags(AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMXEXT);
155#endif
156#else
157 av_set_cpu_flags_mask(FF_MM_SSE2 | FF_MM_MMX2);
158#endif
159#endif /* LIBAVCODEC_VERSION_MAJOR < 55 */
160 return TRUE;
161}
162
163static BOOL tsmf_ffmpeg_init_stream(ITSMFDecoder* decoder, const TS_AM_MEDIA_TYPE* media_type)
164{
165 BYTE* p = NULL;
166 UINT32 size = 0;
167 const BYTE* s = NULL;
168 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
169
170 WINPR_PRAGMA_DIAG_PUSH
171 WINPR_PRAGMA_DIAG_IGNORED_QUALIFIERS
172 mdecoder->codec = avcodec_find_decoder(mdecoder->codec_id);
173 WINPR_PRAGMA_DIAG_POP
174
175 if (!mdecoder->codec)
176 {
177 WLog_ERR(TAG, "avcodec_find_decoder failed.");
178 return FALSE;
179 }
180
181 mdecoder->codec_context->codec_id = mdecoder->codec_id;
182 mdecoder->codec_context->codec_type = mdecoder->media_type;
183
184 switch (mdecoder->media_type)
185 {
186 case AVMEDIA_TYPE_VIDEO:
187 if (!tsmf_ffmpeg_init_video_stream(decoder, media_type))
188 return FALSE;
189
190 break;
191
192 case AVMEDIA_TYPE_AUDIO:
193 if (!tsmf_ffmpeg_init_audio_stream(decoder, media_type))
194 return FALSE;
195
196 break;
197
198 default:
199 WLog_ERR(TAG, "unknown media_type %d", mdecoder->media_type);
200 break;
201 }
202
203 if (media_type->ExtraData)
204 {
205 /* Add a padding to avoid invalid memory read in some codec */
206 mdecoder->codec_context->extradata_size =
207 WINPR_ASSERTING_INT_CAST(int, media_type->ExtraDataSize + 8);
208 mdecoder->codec_context->extradata = calloc(1, mdecoder->codec_context->extradata_size);
209
210 if (!mdecoder->codec_context->extradata)
211 return FALSE;
212
213 if (media_type->SubType == TSMF_SUB_TYPE_AVC1 &&
214 media_type->FormatType == TSMF_FORMAT_TYPE_MPEG2VIDEOINFO)
215 {
216 size_t required = 6;
217 /* The extradata format that FFmpeg uses is following CodecPrivate in Matroska.
218 See http://haali.su/mkv/codecs.pdf */
219 p = mdecoder->codec_context->extradata;
220 if ((mdecoder->codec_context->extradata_size < 0) ||
221 ((size_t)mdecoder->codec_context->extradata_size < required))
222 return FALSE;
223 *p++ = 1; /* Reserved? */
224 *p++ = media_type->ExtraData[8]; /* Profile */
225 *p++ = 0; /* Profile */
226 *p++ = media_type->ExtraData[12]; /* Level */
227 *p++ = 0xff; /* Flag? */
228 *p++ = 0xe0 | 0x01; /* Reserved | #sps */
229 s = media_type->ExtraData + 20;
230 size = ((UINT32)(*s)) * 256 + ((UINT32)(*(s + 1)));
231 required += size + 2;
232 if ((mdecoder->codec_context->extradata_size < 0) ||
233 ((size_t)mdecoder->codec_context->extradata_size < required))
234 return FALSE;
235 memcpy(p, s, size + 2);
236 s += size + 2;
237 p += size + 2;
238 required++;
239 if ((mdecoder->codec_context->extradata_size < 0) ||
240 ((size_t)mdecoder->codec_context->extradata_size < required))
241 return FALSE;
242 *p++ = 1; /* #pps */
243 size = ((UINT32)(*s)) * 256 + ((UINT32)(*(s + 1)));
244 required += size + 2;
245 if ((mdecoder->codec_context->extradata_size < 0) ||
246 ((size_t)mdecoder->codec_context->extradata_size < required))
247 return FALSE;
248 memcpy(p, s, size + 2);
249 }
250 else
251 {
252 memcpy(mdecoder->codec_context->extradata, media_type->ExtraData,
253 media_type->ExtraDataSize);
254 if ((mdecoder->codec_context->extradata_size < 0) ||
255 ((size_t)mdecoder->codec_context->extradata_size <
256 media_type->ExtraDataSize + 8ull))
257 return FALSE;
258 memset(mdecoder->codec_context->extradata + media_type->ExtraDataSize, 0, 8);
259 }
260 }
261
262#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
263 if (mdecoder->codec->capabilities & AV_CODEC_CAP_TRUNCATED)
264 mdecoder->codec_context->flags |= AV_CODEC_FLAG_TRUNCATED;
265#endif
266
267 return TRUE;
268}
269
270static BOOL tsmf_ffmpeg_prepare(ITSMFDecoder* decoder)
271{
272 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
273
274 if (avcodec_open2(mdecoder->codec_context, mdecoder->codec, NULL) < 0)
275 {
276 WLog_ERR(TAG, "avcodec_open2 failed.");
277 return FALSE;
278 }
279
280 mdecoder->prepared = 1;
281 return TRUE;
282}
283
284static BOOL tsmf_ffmpeg_set_format(ITSMFDecoder* decoder, TS_AM_MEDIA_TYPE* media_type)
285{
286 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
287
288 WINPR_ASSERT(mdecoder);
289 WINPR_ASSERT(media_type);
290
291 switch (media_type->MajorType)
292 {
293 case TSMF_MAJOR_TYPE_VIDEO:
294 mdecoder->media_type = AVMEDIA_TYPE_VIDEO;
295 break;
296
297 case TSMF_MAJOR_TYPE_AUDIO:
298 mdecoder->media_type = AVMEDIA_TYPE_AUDIO;
299 break;
300
301 default:
302 return FALSE;
303 }
304
305 switch (media_type->SubType)
306 {
307 case TSMF_SUB_TYPE_WVC1:
308 mdecoder->codec_id = AV_CODEC_ID_VC1;
309 break;
310
311 case TSMF_SUB_TYPE_WMA2:
312 mdecoder->codec_id = AV_CODEC_ID_WMAV2;
313 break;
314
315 case TSMF_SUB_TYPE_WMA9:
316 mdecoder->codec_id = AV_CODEC_ID_WMAPRO;
317 break;
318
319 case TSMF_SUB_TYPE_MP3:
320 mdecoder->codec_id = AV_CODEC_ID_MP3;
321 break;
322
323 case TSMF_SUB_TYPE_MP2A:
324 mdecoder->codec_id = AV_CODEC_ID_MP2;
325 break;
326
327 case TSMF_SUB_TYPE_MP2V:
328 mdecoder->codec_id = AV_CODEC_ID_MPEG2VIDEO;
329 break;
330
331 case TSMF_SUB_TYPE_WMV3:
332 mdecoder->codec_id = AV_CODEC_ID_WMV3;
333 break;
334
335 case TSMF_SUB_TYPE_AAC:
336 mdecoder->codec_id = AV_CODEC_ID_AAC;
337
338 /* For AAC the pFormat is a HEAACWAVEINFO struct, and the codec data
339 is at the end of it. See
340 http://msdn.microsoft.com/en-us/library/dd757806.aspx */
341 if (media_type->ExtraData)
342 {
343 if (media_type->ExtraDataSize < 12)
344 return FALSE;
345
346 media_type->ExtraData += 12;
347 media_type->ExtraDataSize -= 12;
348 }
349
350 break;
351
352 case TSMF_SUB_TYPE_H264:
353 case TSMF_SUB_TYPE_AVC1:
354 mdecoder->codec_id = AV_CODEC_ID_H264;
355 break;
356
357 case TSMF_SUB_TYPE_AC3:
358 mdecoder->codec_id = AV_CODEC_ID_AC3;
359 break;
360
361 default:
362 return FALSE;
363 }
364
365 if (!tsmf_ffmpeg_init_context(decoder))
366 return FALSE;
367
368 if (!tsmf_ffmpeg_init_stream(decoder, media_type))
369 return FALSE;
370
371 if (!tsmf_ffmpeg_prepare(decoder))
372 return FALSE;
373
374 return TRUE;
375}
376
377static BOOL tsmf_ffmpeg_decode_video(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
378 UINT32 extensions)
379{
380 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
381 int decoded = 0;
382 int len = 0;
383 AVFrame* frame = NULL;
384 BOOL ret = TRUE;
385#if LIBAVCODEC_VERSION_MAJOR < 52 || \
386 (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
387 len = avcodec_decode_video(mdecoder->codec_context, mdecoder->frame, &decoded, data, data_size);
388#else
389 {
390 AVPacket pkt = { 0 };
391#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
392 av_init_packet(&pkt);
393#endif
394 pkt.data = WINPR_CAST_CONST_PTR_AWAY(data, BYTE*);
395 pkt.size = WINPR_ASSERTING_INT_CAST(int, data_size);
396
397 if (extensions & TSMM_SAMPLE_EXT_CLEANPOINT)
398 pkt.flags |= AV_PKT_FLAG_KEY;
399
400#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
401 len = avcodec_decode_video2(mdecoder->codec_context, mdecoder->frame, &decoded, &pkt);
402#else
403 len = avcodec_send_packet(mdecoder->codec_context, &pkt);
404 if (len > 0)
405 {
406 len = avcodec_receive_frame(mdecoder->codec_context, mdecoder->frame);
407 if (len == AVERROR(EAGAIN))
408 return TRUE;
409 }
410#endif
411 }
412#endif
413
414 if (len < 0)
415 {
416 WLog_ERR(TAG, "data_size %" PRIu32 ", avcodec_decode_video failed (%d)", data_size, len);
417 ret = FALSE;
418 }
419 else if (!decoded)
420 {
421 WLog_ERR(TAG, "data_size %" PRIu32 ", no frame is decoded.", data_size);
422 ret = FALSE;
423 }
424 else
425 {
426 DEBUG_TSMF("linesize[0] %d linesize[1] %d linesize[2] %d linesize[3] %d "
427 "pix_fmt %d width %d height %d",
428 mdecoder->frame->linesize[0], mdecoder->frame->linesize[1],
429 mdecoder->frame->linesize[2], mdecoder->frame->linesize[3],
430 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
431 mdecoder->codec_context->height);
432 mdecoder->decoded_size = av_image_get_buffer_size(mdecoder->codec_context->pix_fmt,
433 mdecoder->codec_context->width,
434 mdecoder->codec_context->height, 1);
435 mdecoder->decoded_data = calloc(1, mdecoder->decoded_size);
436
437 if (!mdecoder->decoded_data)
438 return FALSE;
439
440#if LIBAVCODEC_VERSION_MAJOR < 55
441 frame = avcodec_alloc_frame();
442#else
443 frame = av_frame_alloc();
444#endif
445 av_image_fill_arrays(frame->data, frame->linesize, mdecoder->decoded_data,
446 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
447 mdecoder->codec_context->height, 1);
448
449 const uint8_t* ptr[AV_NUM_DATA_POINTERS] = { 0 };
450 for (size_t x = 0; x < AV_NUM_DATA_POINTERS; x++)
451 ptr[x] = mdecoder->frame->data[x];
452
453 av_image_copy(frame->data, frame->linesize, ptr, mdecoder->frame->linesize,
454 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
455 mdecoder->codec_context->height);
456 av_free(frame);
457 }
458
459 return ret;
460}
461
462static BOOL tsmf_ffmpeg_decode_audio(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
463 UINT32 extensions)
464{
465 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
466 int len = 0;
467 int frame_size = 0;
468
469 if (mdecoder->decoded_size_max == 0)
470 mdecoder->decoded_size_max = MAX_AUDIO_FRAME_SIZE + 16;
471
472 mdecoder->decoded_data = calloc(1, mdecoder->decoded_size_max);
473
474 if (!mdecoder->decoded_data)
475 return FALSE;
476
477 /* align the memory for SSE2 needs */
478 BYTE* dst = (BYTE*)(((uintptr_t)mdecoder->decoded_data + 15) & ~0x0F);
479 size_t dst_offset = (size_t)(dst - mdecoder->decoded_data);
480 const BYTE* src = data;
481 UINT32 src_size = data_size;
482
483 while (src_size > 0)
484 {
485 /* Ensure enough space for decoding */
486 if (mdecoder->decoded_size_max - mdecoder->decoded_size < MAX_AUDIO_FRAME_SIZE)
487 {
488 BYTE* tmp_data = NULL;
489 tmp_data = realloc(mdecoder->decoded_data, mdecoder->decoded_size_max * 2 + 16);
490
491 if (!tmp_data)
492 return FALSE;
493
494 mdecoder->decoded_size_max = mdecoder->decoded_size_max * 2 + 16;
495 mdecoder->decoded_data = tmp_data;
496 dst = (BYTE*)(((uintptr_t)mdecoder->decoded_data + 15) & ~0x0F);
497
498 const size_t diff = (size_t)(dst - mdecoder->decoded_data);
499 if (diff != dst_offset)
500 {
501 /* re-align the memory if the alignment has changed after realloc */
502 memmove(dst, mdecoder->decoded_data + dst_offset, mdecoder->decoded_size);
503 dst_offset = diff;
504 }
505
506 dst += mdecoder->decoded_size;
507 }
508
509#if LIBAVCODEC_VERSION_MAJOR < 52 || \
510 (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
511 frame_size = mdecoder->decoded_size_max - mdecoder->decoded_size;
512 len = avcodec_decode_audio2(mdecoder->codec_context, (int16_t*)dst, &frame_size, src,
513 src_size);
514#else
515 {
516#if LIBAVCODEC_VERSION_MAJOR < 55
517 AVFrame* decoded_frame = avcodec_alloc_frame();
518#else
519 AVFrame* decoded_frame = av_frame_alloc();
520#endif
521 int got_frame = 0;
522 AVPacket pkt = { 0 };
523#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
524 av_init_packet(&pkt);
525#endif
526
527 pkt.data = WINPR_CAST_CONST_PTR_AWAY(src, BYTE*);
528 pkt.size = WINPR_ASSERTING_INT_CAST(int, src_size);
529#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
530 len = avcodec_decode_audio4(mdecoder->codec_context, decoded_frame, &got_frame, &pkt);
531#else
532 len = avcodec_send_packet(mdecoder->codec_context, &pkt);
533 if (len > 0)
534 {
535 len = avcodec_receive_frame(mdecoder->codec_context, decoded_frame);
536 if (len == AVERROR(EAGAIN))
537 return TRUE;
538 }
539#endif
540
541 if (len >= 0 && got_frame)
542 {
543#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
544 const int channels = mdecoder->codec_context->ch_layout.nb_channels;
545#else
546 const int channels = mdecoder->codec_context->channels;
547#endif
548 frame_size = av_samples_get_buffer_size(NULL, channels, decoded_frame->nb_samples,
549 mdecoder->codec_context->sample_fmt, 1);
550 memcpy(dst, decoded_frame->data[0], frame_size);
551 }
552 else
553 {
554 frame_size = 0;
555 }
556
557 av_free(decoded_frame);
558 }
559#endif
560
561 if (len > 0)
562 {
563 src += len;
564 src_size -= len;
565 }
566
567 if (frame_size > 0)
568 {
569 mdecoder->decoded_size += frame_size;
570 dst += frame_size;
571 }
572 }
573
574 if (mdecoder->decoded_size == 0)
575 {
576 free(mdecoder->decoded_data);
577 mdecoder->decoded_data = NULL;
578 }
579 else if (dst_offset)
580 {
581 /* move the aligned decoded data to original place */
582 memmove(mdecoder->decoded_data, mdecoder->decoded_data + dst_offset,
583 mdecoder->decoded_size);
584 }
585
586 DEBUG_TSMF("data_size %" PRIu32 " decoded_size %" PRIu32 "", data_size, mdecoder->decoded_size);
587 return TRUE;
588}
589
590static BOOL tsmf_ffmpeg_decode(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
591 UINT32 extensions)
592{
593 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
594
595 if (mdecoder->decoded_data)
596 {
597 free(mdecoder->decoded_data);
598 mdecoder->decoded_data = NULL;
599 }
600
601 mdecoder->decoded_size = 0;
602
603 switch (mdecoder->media_type)
604 {
605 case AVMEDIA_TYPE_VIDEO:
606 return tsmf_ffmpeg_decode_video(decoder, data, data_size, extensions);
607
608 case AVMEDIA_TYPE_AUDIO:
609 return tsmf_ffmpeg_decode_audio(decoder, data, data_size, extensions);
610
611 default:
612 WLog_ERR(TAG, "unknown media type.");
613 return FALSE;
614 }
615}
616
617static BYTE* tsmf_ffmpeg_get_decoded_data(ITSMFDecoder* decoder, UINT32* size)
618{
619 BYTE* buf = NULL;
620 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
621 *size = mdecoder->decoded_size;
622 buf = mdecoder->decoded_data;
623 mdecoder->decoded_data = NULL;
624 mdecoder->decoded_size = 0;
625 return buf;
626}
627
628static UINT32 tsmf_ffmpeg_get_decoded_format(ITSMFDecoder* decoder)
629{
630 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
631
632 switch (mdecoder->codec_context->pix_fmt)
633 {
634 case AV_PIX_FMT_YUV420P:
635 return RDP_PIXFMT_I420;
636
637 default:
638 WLog_ERR(TAG, "unsupported pixel format %u", mdecoder->codec_context->pix_fmt);
639 return (UINT32)-1;
640 }
641}
642
643static BOOL tsmf_ffmpeg_get_decoded_dimension(ITSMFDecoder* decoder, UINT32* width, UINT32* height)
644{
645 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
646
647 if (mdecoder->codec_context->width > 0 && mdecoder->codec_context->height > 0)
648 {
649 *width = mdecoder->codec_context->width;
650 *height = mdecoder->codec_context->height;
651 return TRUE;
652 }
653 else
654 {
655 return FALSE;
656 }
657}
658
659static void tsmf_ffmpeg_free(ITSMFDecoder* decoder)
660{
661 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
662
663 if (mdecoder->frame)
664 av_free(mdecoder->frame);
665
666 free(mdecoder->decoded_data);
667
668 if (mdecoder->codec_context)
669 {
670 free(mdecoder->codec_context->extradata);
671 mdecoder->codec_context->extradata = NULL;
672
673#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
674 avcodec_free_context(&mdecoder->codec_context);
675#else
676 if (mdecoder->prepared)
677 avcodec_close(mdecoder->codec_context);
678
679 av_free(mdecoder->codec_context);
680#endif
681 }
682
683 free(decoder);
684}
685
686static INIT_ONCE g_Initialized = INIT_ONCE_STATIC_INIT;
687static BOOL CALLBACK InitializeAvCodecs(PINIT_ONCE once, PVOID param, PVOID* context)
688{
689#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
690 avcodec_register_all();
691#endif
692 return TRUE;
693}
694
695FREERDP_ENTRY_POINT(UINT VCAPITYPE ffmpeg_freerdp_tsmf_client_decoder_subsystem_entry(void* ptr))
696{
697 ITSMFDecoder** sptr = (ITSMFDecoder**)ptr;
698 WINPR_ASSERT(sptr);
699 *sptr = NULL;
700
701 TSMFFFmpegDecoder* decoder = NULL;
702 InitOnceExecuteOnce(&g_Initialized, InitializeAvCodecs, NULL, NULL);
703 WLog_DBG(TAG, "TSMFDecoderEntry FFMPEG");
704 decoder = (TSMFFFmpegDecoder*)calloc(1, sizeof(TSMFFFmpegDecoder));
705
706 if (!decoder)
707 return ERROR_OUTOFMEMORY;
708
709 decoder->iface.SetFormat = tsmf_ffmpeg_set_format;
710 decoder->iface.Decode = tsmf_ffmpeg_decode;
711 decoder->iface.GetDecodedData = tsmf_ffmpeg_get_decoded_data;
712 decoder->iface.GetDecodedFormat = tsmf_ffmpeg_get_decoded_format;
713 decoder->iface.GetDecodedDimension = tsmf_ffmpeg_get_decoded_dimension;
714 decoder->iface.Free = tsmf_ffmpeg_free;
715 *sptr = &decoder->iface;
716 return CHANNEL_RC_OK;
717}