FreeRDP
Loading...
Searching...
No Matches
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 if (mdecoder->codec_context->extradata_size == 0)
209 return FALSE;
210 mdecoder->codec_context->extradata = calloc(1, mdecoder->codec_context->extradata_size);
211
212 if (!mdecoder->codec_context->extradata)
213 return FALSE;
214
215 if (media_type->SubType == TSMF_SUB_TYPE_AVC1 &&
216 media_type->FormatType == TSMF_FORMAT_TYPE_MPEG2VIDEOINFO)
217 {
218 size_t required = 6;
219 /* The extradata format that FFmpeg uses is following CodecPrivate in Matroska.
220 See http://haali.su/mkv/codecs.pdf */
221 p = mdecoder->codec_context->extradata;
222 if ((mdecoder->codec_context->extradata_size < 0) ||
223 ((size_t)mdecoder->codec_context->extradata_size < required))
224 return FALSE;
225 *p++ = 1; /* Reserved? */
226 *p++ = media_type->ExtraData[8]; /* Profile */
227 *p++ = 0; /* Profile */
228 *p++ = media_type->ExtraData[12]; /* Level */
229 *p++ = 0xff; /* Flag? */
230 *p++ = 0xe0 | 0x01; /* Reserved | #sps */
231 s = media_type->ExtraData + 20;
232 size = ((UINT32)(*s)) * 256 + ((UINT32)(*(s + 1)));
233 required += size + 2;
234 if ((mdecoder->codec_context->extradata_size < 0) ||
235 ((size_t)mdecoder->codec_context->extradata_size < required))
236 return FALSE;
237 memcpy(p, s, size + 2);
238 s += size + 2;
239 p += size + 2;
240 required++;
241 if ((mdecoder->codec_context->extradata_size < 0) ||
242 ((size_t)mdecoder->codec_context->extradata_size < required))
243 return FALSE;
244 *p++ = 1; /* #pps */
245 size = ((UINT32)(*s)) * 256 + ((UINT32)(*(s + 1)));
246 required += size + 2;
247 if ((mdecoder->codec_context->extradata_size < 0) ||
248 ((size_t)mdecoder->codec_context->extradata_size < required))
249 return FALSE;
250 memcpy(p, s, size + 2);
251 }
252 else
253 {
254 memcpy(mdecoder->codec_context->extradata, media_type->ExtraData,
255 media_type->ExtraDataSize);
256 if ((mdecoder->codec_context->extradata_size < 0) ||
257 ((size_t)mdecoder->codec_context->extradata_size <
258 media_type->ExtraDataSize + 8ull))
259 return FALSE;
260 memset(mdecoder->codec_context->extradata + media_type->ExtraDataSize, 0, 8);
261 }
262 }
263
264#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
265 if (mdecoder->codec->capabilities & AV_CODEC_CAP_TRUNCATED)
266 mdecoder->codec_context->flags |= AV_CODEC_FLAG_TRUNCATED;
267#endif
268
269 return TRUE;
270}
271
272static BOOL tsmf_ffmpeg_prepare(ITSMFDecoder* decoder)
273{
274 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
275
276 if (avcodec_open2(mdecoder->codec_context, mdecoder->codec, NULL) < 0)
277 {
278 WLog_ERR(TAG, "avcodec_open2 failed.");
279 return FALSE;
280 }
281
282 mdecoder->prepared = 1;
283 return TRUE;
284}
285
286static BOOL tsmf_ffmpeg_set_format(ITSMFDecoder* decoder, TS_AM_MEDIA_TYPE* media_type)
287{
288 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
289
290 WINPR_ASSERT(mdecoder);
291 WINPR_ASSERT(media_type);
292
293 switch (media_type->MajorType)
294 {
295 case TSMF_MAJOR_TYPE_VIDEO:
296 mdecoder->media_type = AVMEDIA_TYPE_VIDEO;
297 break;
298
299 case TSMF_MAJOR_TYPE_AUDIO:
300 mdecoder->media_type = AVMEDIA_TYPE_AUDIO;
301 break;
302
303 default:
304 return FALSE;
305 }
306
307 switch (media_type->SubType)
308 {
309 case TSMF_SUB_TYPE_WVC1:
310 mdecoder->codec_id = AV_CODEC_ID_VC1;
311 break;
312
313 case TSMF_SUB_TYPE_WMA2:
314 mdecoder->codec_id = AV_CODEC_ID_WMAV2;
315 break;
316
317 case TSMF_SUB_TYPE_WMA9:
318 mdecoder->codec_id = AV_CODEC_ID_WMAPRO;
319 break;
320
321 case TSMF_SUB_TYPE_MP3:
322 mdecoder->codec_id = AV_CODEC_ID_MP3;
323 break;
324
325 case TSMF_SUB_TYPE_MP2A:
326 mdecoder->codec_id = AV_CODEC_ID_MP2;
327 break;
328
329 case TSMF_SUB_TYPE_MP2V:
330 mdecoder->codec_id = AV_CODEC_ID_MPEG2VIDEO;
331 break;
332
333 case TSMF_SUB_TYPE_WMV3:
334 mdecoder->codec_id = AV_CODEC_ID_WMV3;
335 break;
336
337 case TSMF_SUB_TYPE_AAC:
338 mdecoder->codec_id = AV_CODEC_ID_AAC;
339
340 /* For AAC the pFormat is a HEAACWAVEINFO struct, and the codec data
341 is at the end of it. See
342 http://msdn.microsoft.com/en-us/library/dd757806.aspx */
343 if (media_type->ExtraData)
344 {
345 if (media_type->ExtraDataSize < 12)
346 return FALSE;
347
348 media_type->ExtraData += 12;
349 media_type->ExtraDataSize -= 12;
350 }
351
352 break;
353
354 case TSMF_SUB_TYPE_H264:
355 case TSMF_SUB_TYPE_AVC1:
356 mdecoder->codec_id = AV_CODEC_ID_H264;
357 break;
358
359 case TSMF_SUB_TYPE_AC3:
360 mdecoder->codec_id = AV_CODEC_ID_AC3;
361 break;
362
363 default:
364 return FALSE;
365 }
366
367 if (!tsmf_ffmpeg_init_context(decoder))
368 return FALSE;
369
370 if (!tsmf_ffmpeg_init_stream(decoder, media_type))
371 return FALSE;
372
373 if (!tsmf_ffmpeg_prepare(decoder))
374 return FALSE;
375
376 return TRUE;
377}
378
379static BOOL tsmf_ffmpeg_decode_video(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
380 UINT32 extensions)
381{
382 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
383 int decoded = 0;
384 int len = 0;
385 AVFrame* frame = NULL;
386 BOOL ret = TRUE;
387#if LIBAVCODEC_VERSION_MAJOR < 52 || \
388 (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
389 len = avcodec_decode_video(mdecoder->codec_context, mdecoder->frame, &decoded, data, data_size);
390#else
391 {
392 AVPacket pkt = { 0 };
393#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
394 av_init_packet(&pkt);
395#endif
396 pkt.data = WINPR_CAST_CONST_PTR_AWAY(data, BYTE*);
397 pkt.size = WINPR_ASSERTING_INT_CAST(int, data_size);
398
399 if (extensions & TSMM_SAMPLE_EXT_CLEANPOINT)
400 pkt.flags |= AV_PKT_FLAG_KEY;
401
402#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
403 len = avcodec_decode_video2(mdecoder->codec_context, mdecoder->frame, &decoded, &pkt);
404#else
405 len = avcodec_send_packet(mdecoder->codec_context, &pkt);
406 if (len > 0)
407 {
408 len = avcodec_receive_frame(mdecoder->codec_context, mdecoder->frame);
409 if (len == AVERROR(EAGAIN))
410 return TRUE;
411 }
412#endif
413 }
414#endif
415
416 if (len < 0)
417 {
418 WLog_ERR(TAG, "data_size %" PRIu32 ", avcodec_decode_video failed (%d)", data_size, len);
419 ret = FALSE;
420 }
421 else if (!decoded)
422 {
423 WLog_ERR(TAG, "data_size %" PRIu32 ", no frame is decoded.", data_size);
424 ret = FALSE;
425 }
426 else
427 {
428 DEBUG_TSMF("linesize[0] %d linesize[1] %d linesize[2] %d linesize[3] %d "
429 "pix_fmt %d width %d height %d",
430 mdecoder->frame->linesize[0], mdecoder->frame->linesize[1],
431 mdecoder->frame->linesize[2], mdecoder->frame->linesize[3],
432 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
433 mdecoder->codec_context->height);
434 mdecoder->decoded_size = av_image_get_buffer_size(mdecoder->codec_context->pix_fmt,
435 mdecoder->codec_context->width,
436 mdecoder->codec_context->height, 1);
437 mdecoder->decoded_data = calloc(1, mdecoder->decoded_size);
438
439 if (!mdecoder->decoded_data)
440 return FALSE;
441
442#if LIBAVCODEC_VERSION_MAJOR < 55
443 frame = avcodec_alloc_frame();
444#else
445 frame = av_frame_alloc();
446#endif
447 av_image_fill_arrays(frame->data, frame->linesize, mdecoder->decoded_data,
448 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
449 mdecoder->codec_context->height, 1);
450
451 const uint8_t* ptr[AV_NUM_DATA_POINTERS] = { 0 };
452 for (size_t x = 0; x < AV_NUM_DATA_POINTERS; x++)
453 ptr[x] = mdecoder->frame->data[x];
454
455 av_image_copy(frame->data, frame->linesize, ptr, mdecoder->frame->linesize,
456 mdecoder->codec_context->pix_fmt, mdecoder->codec_context->width,
457 mdecoder->codec_context->height);
458 av_free(frame);
459 }
460
461 return ret;
462}
463
464static BOOL tsmf_ffmpeg_decode_audio(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
465 UINT32 extensions)
466{
467 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
468 int len = 0;
469 int frame_size = 0;
470
471 if (mdecoder->decoded_size_max == 0)
472 mdecoder->decoded_size_max = MAX_AUDIO_FRAME_SIZE + 16;
473
474 mdecoder->decoded_data = calloc(1, mdecoder->decoded_size_max);
475
476 if (!mdecoder->decoded_data)
477 return FALSE;
478
479 /* align the memory for SSE2 needs */
480 BYTE* dst = (BYTE*)(((uintptr_t)mdecoder->decoded_data + 15) & ~0x0F);
481 size_t dst_offset = (size_t)(dst - mdecoder->decoded_data);
482 const BYTE* src = data;
483 UINT32 src_size = data_size;
484
485 while (src_size > 0)
486 {
487 /* Ensure enough space for decoding */
488 if (mdecoder->decoded_size_max - mdecoder->decoded_size < MAX_AUDIO_FRAME_SIZE)
489 {
490 BYTE* tmp_data = NULL;
491 tmp_data = realloc(mdecoder->decoded_data, mdecoder->decoded_size_max * 2 + 16);
492
493 if (!tmp_data)
494 return FALSE;
495
496 mdecoder->decoded_size_max = mdecoder->decoded_size_max * 2 + 16;
497 mdecoder->decoded_data = tmp_data;
498 dst = (BYTE*)(((uintptr_t)mdecoder->decoded_data + 15) & ~0x0F);
499
500 const size_t diff = (size_t)(dst - mdecoder->decoded_data);
501 if (diff != dst_offset)
502 {
503 /* re-align the memory if the alignment has changed after realloc */
504 memmove(dst, mdecoder->decoded_data + dst_offset, mdecoder->decoded_size);
505 dst_offset = diff;
506 }
507
508 dst += mdecoder->decoded_size;
509 }
510
511#if LIBAVCODEC_VERSION_MAJOR < 52 || \
512 (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR <= 20)
513 frame_size = mdecoder->decoded_size_max - mdecoder->decoded_size;
514 len = avcodec_decode_audio2(mdecoder->codec_context, (int16_t*)dst, &frame_size, src,
515 src_size);
516#else
517 {
518#if LIBAVCODEC_VERSION_MAJOR < 55
519 AVFrame* decoded_frame = avcodec_alloc_frame();
520#else
521 AVFrame* decoded_frame = av_frame_alloc();
522#endif
523 int got_frame = 0;
524 AVPacket pkt = { 0 };
525#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
526 av_init_packet(&pkt);
527#endif
528
529 pkt.data = WINPR_CAST_CONST_PTR_AWAY(src, BYTE*);
530 pkt.size = WINPR_ASSERTING_INT_CAST(int, src_size);
531#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
532 len = avcodec_decode_audio4(mdecoder->codec_context, decoded_frame, &got_frame, &pkt);
533#else
534 len = avcodec_send_packet(mdecoder->codec_context, &pkt);
535 if (len > 0)
536 {
537 len = avcodec_receive_frame(mdecoder->codec_context, decoded_frame);
538 if (len == AVERROR(EAGAIN))
539 return TRUE;
540 }
541#endif
542
543 if (len >= 0 && got_frame)
544 {
545#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
546 const int channels = mdecoder->codec_context->ch_layout.nb_channels;
547#else
548 const int channels = mdecoder->codec_context->channels;
549#endif
550 frame_size = av_samples_get_buffer_size(NULL, channels, decoded_frame->nb_samples,
551 mdecoder->codec_context->sample_fmt, 1);
552 memcpy(dst, decoded_frame->data[0], frame_size);
553 }
554 else
555 {
556 frame_size = 0;
557 }
558
559 av_free(decoded_frame);
560 }
561#endif
562
563 if (len > 0)
564 {
565 src += len;
566 src_size -= len;
567 }
568
569 if (frame_size > 0)
570 {
571 mdecoder->decoded_size += frame_size;
572 dst += frame_size;
573 }
574 }
575
576 if (mdecoder->decoded_size == 0)
577 {
578 free(mdecoder->decoded_data);
579 mdecoder->decoded_data = NULL;
580 }
581 else if (dst_offset)
582 {
583 /* move the aligned decoded data to original place */
584 memmove(mdecoder->decoded_data, mdecoder->decoded_data + dst_offset,
585 mdecoder->decoded_size);
586 }
587
588 DEBUG_TSMF("data_size %" PRIu32 " decoded_size %" PRIu32 "", data_size, mdecoder->decoded_size);
589 return TRUE;
590}
591
592static BOOL tsmf_ffmpeg_decode(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
593 UINT32 extensions)
594{
595 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
596
597 if (mdecoder->decoded_data)
598 {
599 free(mdecoder->decoded_data);
600 mdecoder->decoded_data = NULL;
601 }
602
603 mdecoder->decoded_size = 0;
604
605 switch (mdecoder->media_type)
606 {
607 case AVMEDIA_TYPE_VIDEO:
608 return tsmf_ffmpeg_decode_video(decoder, data, data_size, extensions);
609
610 case AVMEDIA_TYPE_AUDIO:
611 return tsmf_ffmpeg_decode_audio(decoder, data, data_size, extensions);
612
613 default:
614 WLog_ERR(TAG, "unknown media type.");
615 return FALSE;
616 }
617}
618
619static BYTE* tsmf_ffmpeg_get_decoded_data(ITSMFDecoder* decoder, UINT32* size)
620{
621 BYTE* buf = NULL;
622 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
623 *size = mdecoder->decoded_size;
624 buf = mdecoder->decoded_data;
625 mdecoder->decoded_data = NULL;
626 mdecoder->decoded_size = 0;
627 return buf;
628}
629
630static UINT32 tsmf_ffmpeg_get_decoded_format(ITSMFDecoder* decoder)
631{
632 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
633
634 switch (mdecoder->codec_context->pix_fmt)
635 {
636 case AV_PIX_FMT_YUV420P:
637 return RDP_PIXFMT_I420;
638
639 default:
640 WLog_ERR(TAG, "unsupported pixel format %d", mdecoder->codec_context->pix_fmt);
641 return (UINT32)-1;
642 }
643}
644
645static BOOL tsmf_ffmpeg_get_decoded_dimension(ITSMFDecoder* decoder, UINT32* width, UINT32* height)
646{
647 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
648
649 if (mdecoder->codec_context->width > 0 && mdecoder->codec_context->height > 0)
650 {
651 *width = mdecoder->codec_context->width;
652 *height = mdecoder->codec_context->height;
653 return TRUE;
654 }
655 else
656 {
657 return FALSE;
658 }
659}
660
661static void tsmf_ffmpeg_free(ITSMFDecoder* decoder)
662{
663 TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
664
665 if (mdecoder->frame)
666 av_free(mdecoder->frame);
667
668 free(mdecoder->decoded_data);
669
670 if (mdecoder->codec_context)
671 {
672 free(mdecoder->codec_context->extradata);
673 mdecoder->codec_context->extradata = NULL;
674
675#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100)
676 avcodec_free_context(&mdecoder->codec_context);
677#else
678 if (mdecoder->prepared)
679 avcodec_close(mdecoder->codec_context);
680
681 av_free(mdecoder->codec_context);
682#endif
683 }
684
685 free(decoder);
686}
687
688static INIT_ONCE g_Initialized = INIT_ONCE_STATIC_INIT;
689static BOOL CALLBACK InitializeAvCodecs(PINIT_ONCE once, PVOID param, PVOID* context)
690{
691#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
692 avcodec_register_all();
693#endif
694 return TRUE;
695}
696
697FREERDP_ENTRY_POINT(UINT VCAPITYPE ffmpeg_freerdp_tsmf_client_decoder_subsystem_entry(void* ptr))
698{
699 ITSMFDecoder** sptr = (ITSMFDecoder**)ptr;
700 WINPR_ASSERT(sptr);
701 *sptr = NULL;
702
703 TSMFFFmpegDecoder* decoder = NULL;
704 InitOnceExecuteOnce(&g_Initialized, InitializeAvCodecs, NULL, NULL);
705 WLog_DBG(TAG, "TSMFDecoderEntry FFMPEG");
706 decoder = (TSMFFFmpegDecoder*)calloc(1, sizeof(TSMFFFmpegDecoder));
707
708 if (!decoder)
709 return ERROR_OUTOFMEMORY;
710
711 decoder->iface.SetFormat = tsmf_ffmpeg_set_format;
712 decoder->iface.Decode = tsmf_ffmpeg_decode;
713 decoder->iface.GetDecodedData = tsmf_ffmpeg_get_decoded_data;
714 decoder->iface.GetDecodedFormat = tsmf_ffmpeg_get_decoded_format;
715 decoder->iface.GetDecodedDimension = tsmf_ffmpeg_get_decoded_dimension;
716 decoder->iface.Free = tsmf_ffmpeg_free;
717 *sptr = &decoder->iface;
718 return CHANNEL_RC_OK;
719}