FreeRDP
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 
74 typedef 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 
94 static 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 
108 static 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 = 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();
118 #else
119  mdecoder->frame = av_frame_alloc();
120 #endif
121  return TRUE;
122 }
123 
124 static BOOL tsmf_ffmpeg_init_audio_stream(ITSMFDecoder* decoder, const TS_AM_MEDIA_TYPE* media_type)
125 {
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;
131 #else
132  mdecoder->codec_context->channels = media_type->Channels;
133 #endif
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;
138 #else
139 #if LIBAVCODEC_VERSION_MAJOR < 53
140  mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMXEXT;
141 #else
142  mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMX2;
143 #endif
144 #endif
145 #else /* LIBAVCODEC_VERSION_MAJOR < 55 */
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);
149 #else
150  av_force_cpu_flags(AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMXEXT);
151 #endif
152 #else
153  av_set_cpu_flags_mask(FF_MM_SSE2 | FF_MM_MMX2);
154 #endif
155 #endif /* LIBAVCODEC_VERSION_MAJOR < 55 */
156  return TRUE;
157 }
158 
159 static BOOL tsmf_ffmpeg_init_stream(ITSMFDecoder* decoder, const TS_AM_MEDIA_TYPE* media_type)
160 {
161  BYTE* p = NULL;
162  UINT32 size = 0;
163  const BYTE* s = NULL;
164  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
165 
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
170 
171  if (!mdecoder->codec)
172  {
173  WLog_ERR(TAG, "avcodec_find_decoder failed.");
174  return FALSE;
175  }
176 
177  mdecoder->codec_context->codec_id = mdecoder->codec_id;
178  mdecoder->codec_context->codec_type = mdecoder->media_type;
179 
180  switch (mdecoder->media_type)
181  {
182  case AVMEDIA_TYPE_VIDEO:
183  if (!tsmf_ffmpeg_init_video_stream(decoder, media_type))
184  return FALSE;
185 
186  break;
187 
188  case AVMEDIA_TYPE_AUDIO:
189  if (!tsmf_ffmpeg_init_audio_stream(decoder, media_type))
190  return FALSE;
191 
192  break;
193 
194  default:
195  WLog_ERR(TAG, "unknown media_type %d", mdecoder->media_type);
196  break;
197  }
198 
199  if (media_type->ExtraData)
200  {
201  /* Add a padding to avoid invalid memory read in some codec */
202  mdecoder->codec_context->extradata_size = media_type->ExtraDataSize + 8;
203  mdecoder->codec_context->extradata = calloc(1, mdecoder->codec_context->extradata_size);
204 
205  if (!mdecoder->codec_context->extradata)
206  return FALSE;
207 
208  if (media_type->SubType == TSMF_SUB_TYPE_AVC1 &&
209  media_type->FormatType == TSMF_FORMAT_TYPE_MPEG2VIDEOINFO)
210  {
211  size_t required = 6;
212  /* The extradata format that FFmpeg uses is following CodecPrivate in Matroska.
213  See http://haali.su/mkv/codecs.pdf */
214  p = mdecoder->codec_context->extradata;
215  if ((mdecoder->codec_context->extradata_size < 0) ||
216  ((size_t)mdecoder->codec_context->extradata_size < required))
217  return FALSE;
218  *p++ = 1; /* Reserved? */
219  *p++ = media_type->ExtraData[8]; /* Profile */
220  *p++ = 0; /* Profile */
221  *p++ = media_type->ExtraData[12]; /* Level */
222  *p++ = 0xff; /* Flag? */
223  *p++ = 0xe0 | 0x01; /* Reserved | #sps */
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))
229  return FALSE;
230  memcpy(p, s, size + 2);
231  s += size + 2;
232  p += size + 2;
233  required++;
234  if ((mdecoder->codec_context->extradata_size < 0) ||
235  ((size_t)mdecoder->codec_context->extradata_size < required))
236  return FALSE;
237  *p++ = 1; /* #pps */
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))
242  return FALSE;
243  memcpy(p, s, size + 2);
244  }
245  else
246  {
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))
252  return FALSE;
253  memset(mdecoder->codec_context->extradata + media_type->ExtraDataSize, 0, 8);
254  }
255  }
256 
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;
260 #endif
261 
262  return TRUE;
263 }
264 
265 static BOOL tsmf_ffmpeg_prepare(ITSMFDecoder* decoder)
266 {
267  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
268 
269  if (avcodec_open2(mdecoder->codec_context, mdecoder->codec, NULL) < 0)
270  {
271  WLog_ERR(TAG, "avcodec_open2 failed.");
272  return FALSE;
273  }
274 
275  mdecoder->prepared = 1;
276  return TRUE;
277 }
278 
279 static BOOL tsmf_ffmpeg_set_format(ITSMFDecoder* decoder, TS_AM_MEDIA_TYPE* media_type)
280 {
281  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
282 
283  WINPR_ASSERT(mdecoder);
284  WINPR_ASSERT(media_type);
285 
286  switch (media_type->MajorType)
287  {
288  case TSMF_MAJOR_TYPE_VIDEO:
289  mdecoder->media_type = AVMEDIA_TYPE_VIDEO;
290  break;
291 
292  case TSMF_MAJOR_TYPE_AUDIO:
293  mdecoder->media_type = AVMEDIA_TYPE_AUDIO;
294  break;
295 
296  default:
297  return FALSE;
298  }
299 
300  switch (media_type->SubType)
301  {
302  case TSMF_SUB_TYPE_WVC1:
303  mdecoder->codec_id = AV_CODEC_ID_VC1;
304  break;
305 
306  case TSMF_SUB_TYPE_WMA2:
307  mdecoder->codec_id = AV_CODEC_ID_WMAV2;
308  break;
309 
310  case TSMF_SUB_TYPE_WMA9:
311  mdecoder->codec_id = AV_CODEC_ID_WMAPRO;
312  break;
313 
314  case TSMF_SUB_TYPE_MP3:
315  mdecoder->codec_id = AV_CODEC_ID_MP3;
316  break;
317 
318  case TSMF_SUB_TYPE_MP2A:
319  mdecoder->codec_id = AV_CODEC_ID_MP2;
320  break;
321 
322  case TSMF_SUB_TYPE_MP2V:
323  mdecoder->codec_id = AV_CODEC_ID_MPEG2VIDEO;
324  break;
325 
326  case TSMF_SUB_TYPE_WMV3:
327  mdecoder->codec_id = AV_CODEC_ID_WMV3;
328  break;
329 
330  case TSMF_SUB_TYPE_AAC:
331  mdecoder->codec_id = AV_CODEC_ID_AAC;
332 
333  /* For AAC the pFormat is a HEAACWAVEINFO struct, and the codec data
334  is at the end of it. See
335  http://msdn.microsoft.com/en-us/library/dd757806.aspx */
336  if (media_type->ExtraData)
337  {
338  if (media_type->ExtraDataSize < 12)
339  return FALSE;
340 
341  media_type->ExtraData += 12;
342  media_type->ExtraDataSize -= 12;
343  }
344 
345  break;
346 
347  case TSMF_SUB_TYPE_H264:
348  case TSMF_SUB_TYPE_AVC1:
349  mdecoder->codec_id = AV_CODEC_ID_H264;
350  break;
351 
352  case TSMF_SUB_TYPE_AC3:
353  mdecoder->codec_id = AV_CODEC_ID_AC3;
354  break;
355 
356  default:
357  return FALSE;
358  }
359 
360  if (!tsmf_ffmpeg_init_context(decoder))
361  return FALSE;
362 
363  if (!tsmf_ffmpeg_init_stream(decoder, media_type))
364  return FALSE;
365 
366  if (!tsmf_ffmpeg_prepare(decoder))
367  return FALSE;
368 
369  return TRUE;
370 }
371 
372 static BOOL tsmf_ffmpeg_decode_video(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
373  UINT32 extensions)
374 {
375  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
376  int decoded = 0;
377  int len = 0;
378  AVFrame* frame = NULL;
379  BOOL ret = TRUE;
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);
383 #else
384  {
385  AVPacket pkt = { 0 };
386 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
387  av_init_packet(&pkt);
388 #endif
389  pkt.data = WINPR_CAST_CONST_PTR_AWAY(data, BYTE*);
390  pkt.size = data_size;
391 
392  if (extensions & TSMM_SAMPLE_EXT_CLEANPOINT)
393  pkt.flags |= AV_PKT_FLAG_KEY;
394 
395 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
396  len = avcodec_decode_video2(mdecoder->codec_context, mdecoder->frame, &decoded, &pkt);
397 #else
398  len = avcodec_send_packet(mdecoder->codec_context, &pkt);
399  if (len > 0)
400  {
401  do
402  {
403  len = avcodec_receive_frame(mdecoder->codec_context, mdecoder->frame);
404  } while (len == AVERROR(EAGAIN));
405  }
406 #endif
407  }
408 #endif
409 
410  if (len < 0)
411  {
412  WLog_ERR(TAG, "data_size %" PRIu32 ", avcodec_decode_video failed (%d)", data_size, len);
413  ret = FALSE;
414  }
415  else if (!decoded)
416  {
417  WLog_ERR(TAG, "data_size %" PRIu32 ", no frame is decoded.", data_size);
418  ret = FALSE;
419  }
420  else
421  {
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);
432 
433  if (!mdecoder->decoded_data)
434  return FALSE;
435 
436 #if LIBAVCODEC_VERSION_MAJOR < 55
437  frame = avcodec_alloc_frame();
438 #else
439  frame = av_frame_alloc();
440 #endif
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);
444 
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];
448 
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);
452  av_free(frame);
453  }
454 
455  return ret;
456 }
457 
458 static BOOL tsmf_ffmpeg_decode_audio(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
459  UINT32 extensions)
460 {
461  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
462  int len = 0;
463  int frame_size = 0;
464 
465 #if 0
466  WLog_DBG(TAG, ("tsmf_ffmpeg_decode_audio: data_size %"PRIu32"", data_size));
467 
468  for (int i = 0; i < data_size; i++)
469  {
470  WLog_DBG(TAG, ("%02"PRIX8"", data[i]));
471 
472  if (i % 16 == 15)
473  WLog_DBG(TAG, ("\n"));
474  }
475 
476 #endif
477 
478  if (mdecoder->decoded_size_max == 0)
479  mdecoder->decoded_size_max = MAX_AUDIO_FRAME_SIZE + 16;
480 
481  mdecoder->decoded_data = calloc(1, mdecoder->decoded_size_max);
482 
483  if (!mdecoder->decoded_data)
484  return FALSE;
485 
486  /* align the memory for SSE2 needs */
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;
491 
492  while (src_size > 0)
493  {
494  /* Ensure enough space for decoding */
495  if (mdecoder->decoded_size_max - mdecoder->decoded_size < MAX_AUDIO_FRAME_SIZE)
496  {
497  BYTE* tmp_data = NULL;
498  tmp_data = realloc(mdecoder->decoded_data, mdecoder->decoded_size_max * 2 + 16);
499 
500  if (!tmp_data)
501  return FALSE;
502 
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);
506 
507  const size_t diff = (size_t)(dst - mdecoder->decoded_data);
508  if (diff != dst_offset)
509  {
510  /* re-align the memory if the alignment has changed after realloc */
511  memmove(dst, mdecoder->decoded_data + dst_offset, mdecoder->decoded_size);
512  dst_offset = diff;
513  }
514 
515  dst += mdecoder->decoded_size;
516  }
517 
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,
522  src_size);
523 #else
524  {
525 #if LIBAVCODEC_VERSION_MAJOR < 55
526  AVFrame* decoded_frame = avcodec_alloc_frame();
527 #else
528  AVFrame* decoded_frame = av_frame_alloc();
529 #endif
530  int got_frame = 0;
531  AVPacket pkt = { 0 };
532 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
533  av_init_packet(&pkt);
534 #endif
535 
536  pkt.data = WINPR_CAST_CONST_PTR_AWAY(src, BYTE*);
537  pkt.size = src_size;
538 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 101)
539  len = avcodec_decode_audio4(mdecoder->codec_context, decoded_frame, &got_frame, &pkt);
540 #else
541  len = avcodec_send_packet(mdecoder->codec_context, &pkt);
542  if (len > 0)
543  {
544  do
545  {
546  len = avcodec_receive_frame(mdecoder->codec_context, decoded_frame);
547  } while (len == AVERROR(EAGAIN));
548  }
549 #endif
550 
551  if (len >= 0 && got_frame)
552  {
553 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
554  const int channels = mdecoder->codec_context->ch_layout.nb_channels;
555 #else
556  const int channels = mdecoder->codec_context->channels;
557 #endif
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);
561  }
562  else
563  {
564  frame_size = 0;
565  }
566 
567  av_free(decoded_frame);
568  }
569 #endif
570 
571  if (len > 0)
572  {
573  src += len;
574  src_size -= len;
575  }
576 
577  if (frame_size > 0)
578  {
579  mdecoder->decoded_size += frame_size;
580  dst += frame_size;
581  }
582  }
583 
584  if (mdecoder->decoded_size == 0)
585  {
586  free(mdecoder->decoded_data);
587  mdecoder->decoded_data = NULL;
588  }
589  else if (dst_offset)
590  {
591  /* move the aligned decoded data to original place */
592  memmove(mdecoder->decoded_data, mdecoder->decoded_data + dst_offset,
593  mdecoder->decoded_size);
594  }
595 
596  DEBUG_TSMF("data_size %" PRIu32 " decoded_size %" PRIu32 "", data_size, mdecoder->decoded_size);
597  return TRUE;
598 }
599 
600 static BOOL tsmf_ffmpeg_decode(ITSMFDecoder* decoder, const BYTE* data, UINT32 data_size,
601  UINT32 extensions)
602 {
603  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
604 
605  if (mdecoder->decoded_data)
606  {
607  free(mdecoder->decoded_data);
608  mdecoder->decoded_data = NULL;
609  }
610 
611  mdecoder->decoded_size = 0;
612 
613  switch (mdecoder->media_type)
614  {
615  case AVMEDIA_TYPE_VIDEO:
616  return tsmf_ffmpeg_decode_video(decoder, data, data_size, extensions);
617 
618  case AVMEDIA_TYPE_AUDIO:
619  return tsmf_ffmpeg_decode_audio(decoder, data, data_size, extensions);
620 
621  default:
622  WLog_ERR(TAG, "unknown media type.");
623  return FALSE;
624  }
625 }
626 
627 static BYTE* tsmf_ffmpeg_get_decoded_data(ITSMFDecoder* decoder, UINT32* size)
628 {
629  BYTE* buf = NULL;
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;
635  return buf;
636 }
637 
638 static UINT32 tsmf_ffmpeg_get_decoded_format(ITSMFDecoder* decoder)
639 {
640  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
641 
642  switch (mdecoder->codec_context->pix_fmt)
643  {
644  case AV_PIX_FMT_YUV420P:
645  return RDP_PIXFMT_I420;
646 
647  default:
648  WLog_ERR(TAG, "unsupported pixel format %u", mdecoder->codec_context->pix_fmt);
649  return (UINT32)-1;
650  }
651 }
652 
653 static BOOL tsmf_ffmpeg_get_decoded_dimension(ITSMFDecoder* decoder, UINT32* width, UINT32* height)
654 {
655  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
656 
657  if (mdecoder->codec_context->width > 0 && mdecoder->codec_context->height > 0)
658  {
659  *width = mdecoder->codec_context->width;
660  *height = mdecoder->codec_context->height;
661  return TRUE;
662  }
663  else
664  {
665  return FALSE;
666  }
667 }
668 
669 static void tsmf_ffmpeg_free(ITSMFDecoder* decoder)
670 {
671  TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*)decoder;
672 
673  if (mdecoder->frame)
674  av_free(mdecoder->frame);
675 
676  free(mdecoder->decoded_data);
677 
678  if (mdecoder->codec_context)
679  {
680  if (mdecoder->prepared)
681  avcodec_close(mdecoder->codec_context);
682 
683  free(mdecoder->codec_context->extradata);
684  av_free(mdecoder->codec_context);
685  }
686 
687  free(decoder);
688 }
689 
690 static INIT_ONCE g_Initialized = INIT_ONCE_STATIC_INIT;
691 static BOOL CALLBACK InitializeAvCodecs(PINIT_ONCE once, PVOID param, PVOID* context)
692 {
693 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
694  avcodec_register_all();
695 #endif
696  return TRUE;
697 }
698 
699 FREERDP_ENTRY_POINT(UINT VCAPITYPE ffmpeg_freerdp_tsmf_client_decoder_subsystem_entry(void* ptr))
700 {
701  ITSMFDecoder** sptr = (ITSMFDecoder**)ptr;
702  WINPR_ASSERT(sptr);
703  *sptr = NULL;
704 
705  TSMFFFmpegDecoder* decoder = NULL;
706  InitOnceExecuteOnce(&g_Initialized, InitializeAvCodecs, NULL, NULL);
707  WLog_DBG(TAG, "TSMFDecoderEntry FFMPEG");
708  decoder = (TSMFFFmpegDecoder*)calloc(1, sizeof(TSMFFFmpegDecoder));
709 
710  if (!decoder)
711  return ERROR_OUTOFMEMORY;
712 
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;
721 }