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