FreeRDP
Loading...
Searching...
No Matches
h264_openh264.c
1
21#include <freerdp/config.h>
22
23#include <winpr/winpr.h>
24#include <winpr/library.h>
25#include <winpr/assert.h>
26#include <winpr/cast.h>
27
28#include <freerdp/log.h>
29#include <freerdp/codec/h264.h>
30
31#include <wels/codec_def.h>
32#include <wels/codec_api.h>
33#include <wels/codec_ver.h>
34
35#include "h264.h"
36
37typedef void (*pWelsGetCodecVersionEx)(OpenH264Version* pVersion);
38
39typedef long (*pWelsCreateDecoder)(ISVCDecoder** ppDecoder);
40typedef void (*pWelsDestroyDecoder)(ISVCDecoder* pDecoder);
41
42typedef int (*pWelsCreateSVCEncoder)(ISVCEncoder** ppEncoder);
43typedef void (*pWelsDestroySVCEncoder)(ISVCEncoder* pEncoder);
44
45typedef struct
46{
47#if defined(WITH_OPENH264_LOADING)
48 HMODULE lib;
49 OpenH264Version version;
50#endif
51 pWelsGetCodecVersionEx WelsGetCodecVersionEx;
52 WINPR_ATTR_NODISCARD pWelsCreateDecoder WelsCreateDecoder;
53 pWelsDestroyDecoder WelsDestroyDecoder;
54 WINPR_ATTR_NODISCARD pWelsCreateSVCEncoder WelsCreateSVCEncoder;
55 pWelsDestroySVCEncoder WelsDestroySVCEncoder;
56 ISVCDecoder* pDecoder;
57 ISVCEncoder* pEncoder;
58 SEncParamExt EncParamExt;
59} H264_CONTEXT_OPENH264;
60
61#if defined(WITH_OPENH264_LOADING)
62static const char* openh264_library_names[] = {
63#if defined(_WIN32)
64 "openh264.dll"
65#elif defined(__APPLE__)
66 "libopenh264.dylib"
67#else
68 "libopenh264.so.8", "libopenh264.so.2.6.0", "libopenh264.so.7",
69 "libopenh264.so.2.5.0", "libopenh264.so.2.4.1", "libopenh264.so.2.4.0",
70 "libopenh264.so.2.3.1", "libopenh264.so.2.3.0", "libopenh264.so",
71
72#endif
73};
74#endif
75
76static void openh264_trace_callback(void* ctx, int level, const char* message)
77{
78 H264_CONTEXT* h264 = ctx;
79 if (h264)
80 WLog_Print(h264->log, WLOG_TRACE, "%d - %s", level, message);
81}
82
83static int openh264_decompress(H264_CONTEXT* WINPR_RESTRICT h264,
84 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize)
85{
86 DECODING_STATE state = dsInvalidArgument;
87 SBufferInfo sBufferInfo = WINPR_C_ARRAY_INIT;
88 SSysMEMBuffer* pSystemBuffer = nullptr;
89 H264_CONTEXT_OPENH264* sys = nullptr;
90 UINT32* iStride = nullptr;
91 BYTE** pYUVData = nullptr;
92
93 WINPR_ASSERT(h264);
94 WINPR_ASSERT(pSrcData || (SrcSize == 0));
95
96 sys = (H264_CONTEXT_OPENH264*)h264->pSystemData;
97 WINPR_ASSERT(sys);
98
99 iStride = h264->iStride;
100 WINPR_ASSERT(iStride);
101
102 pYUVData = h264->pYUVData;
103 WINPR_ASSERT(pYUVData);
104
105 if (!sys->pDecoder)
106 return -2001;
107
108 /*
109 * Decompress the image. The RDP host only seems to send I420 format.
110 */
111 pYUVData[0] = nullptr;
112 pYUVData[1] = nullptr;
113 pYUVData[2] = nullptr;
114
115 WINPR_ASSERT(sys->pDecoder);
116 state = (*sys->pDecoder)
117 ->DecodeFrame2(sys->pDecoder, pSrcData, WINPR_ASSERTING_INT_CAST(int, SrcSize),
118 pYUVData, &sBufferInfo);
119
120 if (sBufferInfo.iBufferStatus != 1)
121 {
122 if (state == dsNoParamSets)
123 {
124 /* this happens on the first frame due to missing parameter sets */
125 state =
126 (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, nullptr, 0, pYUVData, &sBufferInfo);
127 }
128 else if (state == dsErrorFree)
129 {
130 /* call DecodeFrame2 again to decode without delay */
131 state =
132 (*sys->pDecoder)->DecodeFrame2(sys->pDecoder, nullptr, 0, pYUVData, &sBufferInfo);
133 }
134 else
135 {
136 WLog_Print(h264->log, WLOG_WARN, "DecodeFrame2 state: 0x%04X iBufferStatus: %d", state,
137 sBufferInfo.iBufferStatus);
138 return -2002;
139 }
140 }
141
142 if (state != dsErrorFree)
143 {
144 WLog_Print(h264->log, WLOG_WARN, "DecodeFrame2 state: 0x%02X", state);
145 return -2003;
146 }
147
148#if OPENH264_MAJOR >= 2
149 state = (*sys->pDecoder)->FlushFrame(sys->pDecoder, pYUVData, &sBufferInfo);
150 if (state != dsErrorFree)
151 {
152 WLog_Print(h264->log, WLOG_WARN, "FlushFrame state: 0x%02X", state);
153 return -2003;
154 }
155#endif
156
157 pSystemBuffer = &sBufferInfo.UsrData.sSystemBuffer;
158 iStride[0] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[0]);
159 iStride[1] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[1]);
160 iStride[2] = WINPR_ASSERTING_INT_CAST(uint32_t, pSystemBuffer->iStride[1]);
161
162 if (sBufferInfo.iBufferStatus != 1)
163 {
164 WLog_Print(h264->log, WLOG_WARN, "DecodeFrame2 iBufferStatus: %d",
165 sBufferInfo.iBufferStatus);
166 return 0;
167 }
168
169 if (state != dsErrorFree)
170 {
171 WLog_Print(h264->log, WLOG_WARN, "DecodeFrame2 state: 0x%02X", state);
172 return -2003;
173 }
174
175 if (pSystemBuffer->iFormat != videoFormatI420)
176 return -2004;
177
178 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
179 return -2005;
180
181 if ((pSystemBuffer->iWidth <= 0) || (pSystemBuffer->iHeight <= 0))
182 return -2006;
183
184 h264->YUVWidth = WINPR_ASSERTING_INT_CAST(UINT32, pSystemBuffer->iWidth);
185 h264->YUVHeight = WINPR_ASSERTING_INT_CAST(UINT32, pSystemBuffer->iHeight);
186
187 return 1;
188}
189
190static int openh264_compress(H264_CONTEXT* WINPR_RESTRICT h264,
191 const BYTE** WINPR_RESTRICT pYUVData,
192 const UINT32* WINPR_RESTRICT iStride, BYTE** WINPR_RESTRICT ppDstData,
193 UINT32* WINPR_RESTRICT pDstSize)
194{
195 int status = 0;
196 SFrameBSInfo info = WINPR_C_ARRAY_INIT;
197 SSourcePicture pic = WINPR_C_ARRAY_INIT;
198
199 H264_CONTEXT_OPENH264* sys = nullptr;
200
201 WINPR_ASSERT(h264);
202 WINPR_ASSERT(pYUVData);
203 WINPR_ASSERT(iStride);
204 WINPR_ASSERT(ppDstData);
205 WINPR_ASSERT(pDstSize);
206
207 sys = &((H264_CONTEXT_OPENH264*)h264->pSystemData)[0];
208 WINPR_ASSERT(sys);
209
210 if (!sys->pEncoder)
211 return -1;
212
213 if (!pYUVData[0] || !pYUVData[1] || !pYUVData[2])
214 return -1;
215
216 if ((h264->width > INT_MAX) || (h264->height > INT_MAX))
217 return -1;
218
219 if ((h264->FrameRate > INT_MAX) || (h264->NumberOfThreads > INT_MAX) ||
220 (h264->BitRate > INT_MAX) || (h264->QP > INT_MAX))
221 return -1;
222
223 WINPR_ASSERT(sys->pEncoder);
224 if ((sys->EncParamExt.iPicWidth != (int)h264->width) ||
225 (sys->EncParamExt.iPicHeight != (int)h264->height))
226 {
227 WINPR_ASSERT((*sys->pEncoder)->GetDefaultParams);
228 status = (*sys->pEncoder)->GetDefaultParams(sys->pEncoder, &sys->EncParamExt);
229
230 if (status != cmResultSuccess)
231 {
232 WLog_Print(h264->log, WLOG_ERROR,
233 "Failed to get OpenH264 default parameters (status=%d)", status);
234 return status > 0 ? -status : status;
235 }
236
237 EUsageType usageType = SCREEN_CONTENT_REAL_TIME;
238
239 switch (h264->UsageType)
240 {
241 case H264_CAMERA_VIDEO_NON_REAL_TIME:
242 usageType = CAMERA_VIDEO_NON_REAL_TIME;
243 break;
244 case H264_CAMERA_VIDEO_REAL_TIME:
245 usageType = CAMERA_VIDEO_REAL_TIME;
246 break;
247 case H264_SCREEN_CONTENT_NON_REAL_TIME:
248 usageType = SCREEN_CONTENT_NON_REAL_TIME;
249 break;
250 case H264_SCREEN_CONTENT_REAL_TIME:
251 default:
252 break;
253 }
254
255 sys->EncParamExt.iUsageType = usageType;
256 sys->EncParamExt.iPicWidth = WINPR_ASSERTING_INT_CAST(int, h264->width);
257 sys->EncParamExt.iPicHeight = WINPR_ASSERTING_INT_CAST(int, h264->height);
258 sys->EncParamExt.fMaxFrameRate = WINPR_ASSERTING_INT_CAST(short, h264->FrameRate);
259 sys->EncParamExt.iMaxBitrate = UNSPECIFIED_BIT_RATE;
260 sys->EncParamExt.bEnableDenoise = 0;
261 sys->EncParamExt.bEnableLongTermReference = 0;
262 sys->EncParamExt.iSpatialLayerNum = 1;
263 sys->EncParamExt.iMultipleThreadIdc =
264 WINPR_ASSERTING_INT_CAST(unsigned short, h264->NumberOfThreads);
265 sys->EncParamExt.sSpatialLayers[0].fFrameRate =
266 WINPR_ASSERTING_INT_CAST(short, h264->FrameRate);
267 sys->EncParamExt.sSpatialLayers[0].iVideoWidth = sys->EncParamExt.iPicWidth;
268 sys->EncParamExt.sSpatialLayers[0].iVideoHeight = sys->EncParamExt.iPicHeight;
269 sys->EncParamExt.sSpatialLayers[0].iMaxSpatialBitrate = sys->EncParamExt.iMaxBitrate;
270
271 switch (h264->RateControlMode)
272 {
273 case H264_RATECONTROL_VBR:
274 sys->EncParamExt.iRCMode = RC_BITRATE_MODE;
275 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
276 sys->EncParamExt.sSpatialLayers[0].iSpatialBitrate =
277 sys->EncParamExt.iTargetBitrate;
278 sys->EncParamExt.bEnableFrameSkip = 1;
279 break;
280
281 case H264_RATECONTROL_CQP:
282 sys->EncParamExt.iRCMode = RC_OFF_MODE;
283 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
284 sys->EncParamExt.bEnableFrameSkip = 0;
285 break;
286 default:
287 break;
288 }
289
290 if (sys->EncParamExt.iMultipleThreadIdc > 1)
291 {
292#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
293 sys->EncParamExt.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE;
294#else
295 sys->EncParamExt.sSpatialLayers[0].sSliceArgument.uiSliceMode = SM_FIXEDSLCNUM_SLICE;
296#endif
297 }
298
299 WINPR_ASSERT((*sys->pEncoder)->InitializeExt);
300 status = (*sys->pEncoder)->InitializeExt(sys->pEncoder, &sys->EncParamExt);
301
302 if (status != cmResultSuccess)
303 {
304 WLog_Print(h264->log, WLOG_ERROR, "Failed to initialize OpenH264 encoder (status=%d)",
305 status);
306 return status > 0 ? -status : status;
307 }
308
309 WINPR_ASSERT((*sys->pEncoder)->GetOption);
310 status =
311 (*sys->pEncoder)
312 ->GetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &sys->EncParamExt);
313
314 if (status != cmResultSuccess)
315 {
316 WLog_Print(h264->log, WLOG_ERROR,
317 "Failed to get initial OpenH264 encoder parameters (status=%d)", status);
318 return status > 0 ? -status : status;
319 }
320 }
321 else
322 {
323 switch (h264->RateControlMode)
324 {
325 case H264_RATECONTROL_VBR:
326 if (sys->EncParamExt.iTargetBitrate != (int)h264->BitRate)
327 {
328 SBitrateInfo bitrate = WINPR_C_ARRAY_INIT;
329
330 sys->EncParamExt.iTargetBitrate = (int)h264->BitRate;
331 bitrate.iLayer = SPATIAL_LAYER_ALL;
332 bitrate.iBitrate = (int)h264->BitRate;
333
334 WINPR_ASSERT((*sys->pEncoder)->SetOption);
335 status = (*sys->pEncoder)
336 ->SetOption(sys->pEncoder, ENCODER_OPTION_BITRATE, &bitrate);
337
338 if (status != cmResultSuccess)
339 {
340 WLog_Print(h264->log, WLOG_ERROR,
341 "Failed to set encoder bitrate (status=%d)", status);
342 return status > 0 ? -status : status;
343 }
344 }
345
346 if ((uint32_t)sys->EncParamExt.fMaxFrameRate != h264->FrameRate)
347 {
348 sys->EncParamExt.fMaxFrameRate =
349 WINPR_ASSERTING_INT_CAST(float, h264->FrameRate);
350
351 WINPR_ASSERT((*sys->pEncoder)->SetOption);
352 status = (*sys->pEncoder)
353 ->SetOption(sys->pEncoder, ENCODER_OPTION_FRAME_RATE,
354 &sys->EncParamExt.fMaxFrameRate);
355
356 if (status != cmResultSuccess)
357 {
358 WLog_Print(h264->log, WLOG_ERROR,
359 "Failed to set encoder framerate (status=%d)", status);
360 return status > 0 ? -status : status;
361 }
362 }
363
364 break;
365
366 case H264_RATECONTROL_CQP:
367 if (sys->EncParamExt.sSpatialLayers[0].iDLayerQp != (int)h264->QP)
368 {
369 sys->EncParamExt.sSpatialLayers[0].iDLayerQp = (int)h264->QP;
370
371 WINPR_ASSERT((*sys->pEncoder)->SetOption);
372 status = (*sys->pEncoder)
373 ->SetOption(sys->pEncoder, ENCODER_OPTION_SVC_ENCODE_PARAM_EXT,
374 &sys->EncParamExt);
375
376 if (status != cmResultSuccess)
377 {
378 WLog_Print(h264->log, WLOG_ERROR,
379 "Failed to set encoder parameters (status=%d)", status);
380 return status > 0 ? -status : status;
381 }
382 }
383
384 break;
385 default:
386 break;
387 }
388 }
389
390 pic.iPicWidth = (int)h264->width;
391 pic.iPicHeight = (int)h264->height;
392 pic.iColorFormat = videoFormatI420;
393 pic.iStride[0] = (int)iStride[0];
394 pic.iStride[1] = (int)iStride[1];
395 pic.iStride[2] = (int)iStride[2];
396 pic.pData[0] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[0], BYTE*);
397 pic.pData[1] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[1], BYTE*);
398 pic.pData[2] = WINPR_CAST_CONST_PTR_AWAY(pYUVData[2], BYTE*);
399
400 WINPR_ASSERT((*sys->pEncoder)->EncodeFrame);
401 status = (*sys->pEncoder)->EncodeFrame(sys->pEncoder, &pic, &info);
402
403 if (status != cmResultSuccess)
404 {
405 WLog_Print(h264->log, WLOG_ERROR, "Failed to encode frame (status=%d)", status);
406 return status > 0 ? -status : status;
407 }
408
409 *ppDstData = info.sLayerInfo[0].pBsBuf;
410 *pDstSize = 0;
411
412 for (int i = 0; i < info.iLayerNum; i++)
413 {
414 for (int j = 0; j < info.sLayerInfo[i].iNalCount; j++)
415 {
416 const int val = info.sLayerInfo[i].pNalLengthInByte[j];
417 *pDstSize += WINPR_ASSERTING_INT_CAST(uint32_t, val);
418 }
419 }
420
421 return 1;
422}
423
424static void openh264_uninit(H264_CONTEXT* h264)
425{
426 H264_CONTEXT_OPENH264* sysContexts = nullptr;
427
428 WINPR_ASSERT(h264);
429
430 sysContexts = (H264_CONTEXT_OPENH264*)h264->pSystemData;
431
432 if (sysContexts)
433 {
434 for (UINT32 x = 0; x < h264->numSystemData; x++)
435 {
436 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
437
438 if (sys->pDecoder)
439 {
440 (*sys->pDecoder)->Uninitialize(sys->pDecoder);
441 sysContexts->WelsDestroyDecoder(sys->pDecoder);
442 sys->pDecoder = nullptr;
443 }
444
445 if (sys->pEncoder)
446 {
447 (*sys->pEncoder)->Uninitialize(sys->pEncoder);
448 sysContexts->WelsDestroySVCEncoder(sys->pEncoder);
449 sys->pEncoder = nullptr;
450 }
451 }
452
453#if defined(WITH_OPENH264_LOADING)
454 if (sysContexts->lib)
455 FreeLibrary(sysContexts->lib);
456#endif
457 free(h264->pSystemData);
458 h264->pSystemData = nullptr;
459 }
460}
461
462#if defined(WITH_OPENH264_LOADING)
463static BOOL openh264_load_functionpointers(H264_CONTEXT* h264, const char* name)
464{
465 H264_CONTEXT_OPENH264* sysContexts;
466
467 WINPR_ASSERT(name);
468
469 if (!h264)
470 return FALSE;
471
472 sysContexts = h264->pSystemData;
473
474 if (!sysContexts)
475 return FALSE;
476
477 sysContexts->lib = LoadLibraryA(name);
478
479 if (!sysContexts->lib)
480 return FALSE;
481
482 sysContexts->WelsGetCodecVersionEx =
483 GetProcAddressAs(sysContexts->lib, "WelsGetCodecVersionEx", pWelsGetCodecVersionEx);
484 sysContexts->WelsCreateDecoder =
485 GetProcAddressAs(sysContexts->lib, "WelsCreateDecoder", pWelsCreateDecoder);
486 sysContexts->WelsDestroyDecoder =
487 GetProcAddressAs(sysContexts->lib, "WelsDestroyDecoder", pWelsDestroyDecoder);
488 sysContexts->WelsCreateSVCEncoder =
489 GetProcAddressAs(sysContexts->lib, "WelsCreateSVCEncoder", pWelsCreateSVCEncoder);
490 sysContexts->WelsDestroySVCEncoder =
491 GetProcAddressAs(sysContexts->lib, "WelsDestroySVCEncoder", pWelsDestroySVCEncoder);
492
493 if (!sysContexts->WelsCreateDecoder || !sysContexts->WelsDestroyDecoder ||
494 !sysContexts->WelsCreateSVCEncoder || !sysContexts->WelsDestroySVCEncoder ||
495 !sysContexts->WelsGetCodecVersionEx)
496 {
497 FreeLibrary(sysContexts->lib);
498 sysContexts->lib = nullptr;
499 return FALSE;
500 }
501
502 sysContexts->WelsGetCodecVersionEx(&sysContexts->version);
503 WLog_Print(h264->log, WLOG_DEBUG, "loaded %s %u.%u.%u", name, sysContexts->version.uMajor,
504 sysContexts->version.uMinor, sysContexts->version.uRevision);
505
506 if ((sysContexts->version.uMajor < 1) ||
507 ((sysContexts->version.uMajor == 1) && (sysContexts->version.uMinor < 6)))
508 {
509 WLog_Print(
510 h264->log, WLOG_ERROR,
511 "OpenH264 %s %u.%u.%u is too old, need at least version 1.6.0 for dynamic loading",
512 name, sysContexts->version.uMajor, sysContexts->version.uMinor,
513 sysContexts->version.uRevision);
514 FreeLibrary(sysContexts->lib);
515 sysContexts->lib = nullptr;
516 return FALSE;
517 }
518
519 return TRUE;
520}
521#endif
522
523static BOOL openh264_init(H264_CONTEXT* h264)
524{
525#if defined(WITH_OPENH264_LOADING)
526 BOOL success = FALSE;
527#endif
528 long status = 0;
529 H264_CONTEXT_OPENH264* sysContexts = nullptr;
530 static int traceLevel = WELS_LOG_DEBUG;
531#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
532 static EVideoFormatType videoFormat = videoFormatI420;
533#endif
534 static WelsTraceCallback traceCallback = openh264_trace_callback;
535
536 WINPR_ASSERT(h264);
537
538 h264->numSystemData = 1;
539 sysContexts =
540 (H264_CONTEXT_OPENH264*)calloc(h264->numSystemData, sizeof(H264_CONTEXT_OPENH264));
541
542 if (!sysContexts)
543 goto EXCEPTION;
544
545 h264->pSystemData = (void*)sysContexts;
546#if defined(WITH_OPENH264_LOADING)
547
548 for (size_t i = 0; i < ARRAYSIZE(openh264_library_names); i++)
549 {
550 const char* current = openh264_library_names[i];
551 success = openh264_load_functionpointers(h264, current);
552
553 if (success)
554 break;
555 }
556
557 if (!success)
558 goto EXCEPTION;
559
560#else
561 sysContexts->WelsGetCodecVersionEx = WelsGetCodecVersionEx;
562 sysContexts->WelsCreateDecoder = WelsCreateDecoder;
563 sysContexts->WelsDestroyDecoder = WelsDestroyDecoder;
564 sysContexts->WelsCreateSVCEncoder = WelsCreateSVCEncoder;
565 sysContexts->WelsDestroySVCEncoder = WelsDestroySVCEncoder;
566#endif
567
568 for (UINT32 x = 0; x < h264->numSystemData; x++)
569 {
570 SDecodingParam sDecParam = WINPR_C_ARRAY_INIT;
571 H264_CONTEXT_OPENH264* sys = &sysContexts[x];
572
573 if (h264->Compressor)
574 {
575#if defined(WITH_OPENH264_LOADING)
576 if (sysContexts->version.uMajor != OPENH264_MAJOR ||
577 sysContexts->version.uMinor != OPENH264_MINOR)
578 {
579 WLog_Print(h264->log, WLOG_WARN,
580 "OpenH264 encoder ABI mismatch: runtime %d.%d.%d vs compiled %d.%d.%d",
581 sysContexts->version.uMajor, sysContexts->version.uMinor,
582 sysContexts->version.uRevision, OPENH264_MAJOR, OPENH264_MINOR,
583 OPENH264_REVISION);
584 goto EXCEPTION;
585 }
586#endif
587 const int rc = sysContexts->WelsCreateSVCEncoder(&sys->pEncoder);
588 if (rc != 0)
589 {
590 WLog_Print(h264->log, WLOG_ERROR, "Failed to create OpenH264 encoder: %d", rc);
591 goto EXCEPTION;
592 }
593 if (!sys->pEncoder)
594 {
595 WLog_Print(h264->log, WLOG_ERROR, "Failed to create OpenH264 encoder");
596 goto EXCEPTION;
597 }
598 }
599 else
600 {
601 const long rc = sysContexts->WelsCreateDecoder(&sys->pDecoder);
602 if (rc != 0)
603 {
604 WLog_Print(h264->log, WLOG_ERROR, "Failed to create OpenH264 decoder: %ld", rc);
605 goto EXCEPTION;
606 }
607 if (!sys->pDecoder)
608 {
609 WLog_Print(h264->log, WLOG_ERROR, "Failed to create OpenH264 decoder");
610 goto EXCEPTION;
611 }
612
613#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
614 sDecParam.eOutputColorFormat = videoFormatI420;
615#endif
616 sDecParam.eEcActiveIdc = ERROR_CON_FRAME_COPY;
617 sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_AVC;
618 status = (*sys->pDecoder)->Initialize(sys->pDecoder, &sDecParam);
619
620 if (status != 0)
621 {
622 WLog_Print(h264->log, WLOG_ERROR,
623 "Failed to initialize OpenH264 decoder (status=%ld)", status);
624 goto EXCEPTION;
625 }
626
627#if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5)
628 status =
629 (*sys->pDecoder)->SetOption(sys->pDecoder, DECODER_OPTION_DATAFORMAT, &videoFormat);
630#endif
631
632 if (status != 0)
633 {
634 WLog_Print(h264->log, WLOG_ERROR,
635 "Failed to set data format option on OpenH264 decoder (status=%ld)",
636 status);
637 goto EXCEPTION;
638 }
639
640 if (WLog_GetLogLevel(h264->log) == WLOG_TRACE)
641 {
642 status = (*sys->pDecoder)
643 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_LEVEL, &traceLevel);
644
645 if (status != 0)
646 {
647 WLog_Print(h264->log, WLOG_ERROR,
648 "Failed to set trace level option on OpenH264 decoder (status=%ld)",
649 status);
650 goto EXCEPTION;
651 }
652
653 status = (*sys->pDecoder)
654 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT,
655 (void*)&h264);
656
657 if (status != 0)
658 {
659 WLog_Print(h264->log, WLOG_ERROR,
660 "Failed to set trace callback context option on OpenH264 decoder "
661 "(status=%ld)",
662 status);
663 goto EXCEPTION;
664 }
665
666 status = (*sys->pDecoder)
667 ->SetOption(sys->pDecoder, DECODER_OPTION_TRACE_CALLBACK,
668 (void*)&traceCallback);
669
670 if (status != 0)
671 {
672 WLog_Print(
673 h264->log, WLOG_ERROR,
674 "Failed to set trace callback option on OpenH264 decoder (status=%ld)",
675 status);
676 goto EXCEPTION;
677 }
678 }
679 }
680 }
681
682 h264->hwAccel = FALSE; /* not supported */
683 return TRUE;
684EXCEPTION:
685 openh264_uninit(h264);
686 return FALSE;
687}
688
689const H264_CONTEXT_SUBSYSTEM g_Subsystem_OpenH264 = { "OpenH264", openh264_init, openh264_uninit,
690 openh264_decompress, openh264_compress };