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