FreeRDP
Loading...
Searching...
No Matches
encoding.c
1
20#include <winpr/assert.h>
21#include <winpr/winpr.h>
22
23#include "camera.h"
24
25#define TAG CHANNELS_TAG("rdpecam-video.client")
26
27#if defined(WITH_INPUT_FORMAT_H264)
28/*
29 * demux a H264 frame from a MJPG container
30 * args:
31 * srcData - pointer to buffer with h264 muxed in MJPG container
32 * srcSize - buff size
33 * h264_data - pointer to h264 data
34 * h264_max_size - maximum size allowed by h264_data buffer
35 *
36 * Credits:
37 * guvcview http://guvcview.sourceforge.net
38 * Paulo Assis <pj.assis@gmail.com>
39 *
40 * see Figure 5 Payload Size in USB_Video_Payload_H 264_1 0.pdf
41 * for format details
42 *
43 * @return: data size and copies demuxed data to h264 buffer
44 */
45static size_t demux_uvcH264(const BYTE* srcData, size_t srcSize, BYTE* h264_data,
46 size_t h264_max_size)
47{
48 WINPR_ASSERT(h264_data);
49 WINPR_ASSERT(srcData);
50
51 if (srcSize < 30)
52 {
53 WLog_ERR(TAG, "Expected srcSize >= 30, got %" PRIuz, srcSize);
54 return 0;
55 }
56 const uint8_t* spl = NULL;
57 uint8_t* ph264 = h264_data;
58
59 /* search for 1st APP4 marker
60 * (30 = 2 APP4 marker + 2 length + 22 header + 4 payload size)
61 */
62 for (const uint8_t* sp = srcData; sp < srcData + srcSize - 30; sp++)
63 {
64 if (sp[0] == 0xFF && sp[1] == 0xE4)
65 {
66 spl = sp + 2; /* exclude APP4 marker */
67 break;
68 }
69 }
70
71 if (spl == NULL)
72 {
73 WLog_ERR(TAG, "Expected 1st APP4 marker but none found");
74 return 0;
75 }
76
77 if (spl > srcData + srcSize - 4)
78 {
79 WLog_ERR(TAG, "Payload + Header size bigger than srcData buffer");
80 return 0;
81 }
82
83 /* 1st segment length in big endian
84 * includes payload size + header + 6 bytes (2 length + 4 payload size)
85 */
86 uint16_t length = (uint16_t)(spl[0] << 8) & UINT16_MAX;
87 length |= (uint16_t)spl[1];
88
89 spl += 2; /* header */
90 /* header length in little endian at offset 2 */
91 uint16_t header_length = (uint16_t)spl[2];
92 header_length |= (uint16_t)spl[3] << 8;
93
94 spl += header_length;
95 if (spl > srcData + srcSize)
96 {
97 WLog_ERR(TAG, "Header size bigger than srcData buffer");
98 return 0;
99 }
100
101 /* payload size in little endian */
102 uint32_t payload_size = (uint32_t)spl[0] << 0;
103 payload_size |= (uint32_t)spl[1] << 8;
104 payload_size |= (uint32_t)spl[2] << 16;
105 payload_size |= (uint32_t)spl[3] << 24;
106
107 if (payload_size > h264_max_size)
108 {
109 WLog_ERR(TAG, "Payload size bigger than h264_data buffer");
110 return 0;
111 }
112
113 spl += 4; /* payload start */
114 const uint8_t* epl = spl + payload_size; /* payload end */
115
116 if (epl > srcData + srcSize)
117 {
118 WLog_ERR(TAG, "Payload size bigger than srcData buffer");
119 return 0;
120 }
121
122 length -= header_length + 6;
123
124 /* copy 1st segment to h264 buffer */
125 memcpy(ph264, spl, length);
126 ph264 += length;
127 spl += length;
128
129 /* copy other segments */
130 while (epl > spl + 4)
131 {
132 if (spl[0] != 0xFF || spl[1] != 0xE4)
133 {
134 WLog_ERR(TAG, "Expected 2nd+ APP4 marker but none found");
135 const intptr_t diff = ph264 - h264_data;
136 return WINPR_ASSERTING_INT_CAST(size_t, diff);
137 }
138
139 /* 2nd+ segment length in big endian */
140 length = (uint16_t)(spl[2] << 8) & UINT16_MAX;
141 length |= (uint16_t)spl[3];
142 if (length < 2)
143 {
144 WLog_ERR(TAG, "Expected 2nd+ APP4 length >= 2 but have %" PRIu16, length);
145 return 0;
146 }
147
148 length -= 2;
149 spl += 4; /* APP4 marker + length */
150
151 /* copy segment to h264 buffer */
152 memcpy(ph264, spl, length);
153 ph264 += length;
154 spl += length;
155 }
156
157 const intptr_t diff = ph264 - h264_data;
158 return WINPR_ASSERTING_INT_CAST(size_t, diff);
159}
160#endif
161
167UINT32 h264_get_max_bitrate(UINT32 height)
168{
169 static struct Bitrates
170 {
171 UINT32 height;
172 UINT32 bitrate; /* kbps */
173
174 } bitrates[] = {
175 /* source: https://livekit.io/webrtc/bitrate-guide (webcam streaming)
176 *
177 * sorted by height in descending order
178 */
179 { 1080, 2700 }, { 720, 1250 }, { 480, 700 }, { 360, 400 },
180 { 240, 170 }, { 180, 140 }, { 0, 100 },
181 };
182 const size_t nBitrates = ARRAYSIZE(bitrates);
183
184 for (size_t i = 0; i < nBitrates; i++)
185 {
186 if (height >= bitrates[i].height)
187 {
188 UINT32 bitrate = bitrates[i].bitrate;
189 WLog_DBG(TAG, "Setting h264 max bitrate: %u kbps", bitrate);
190 return bitrate * 1000;
191 }
192 }
193
194 WINPR_ASSERT(FALSE);
195 return 0;
196}
197
203static enum AVPixelFormat ecamToAVPixFormat(CAM_MEDIA_FORMAT ecamFormat)
204{
205 switch (ecamFormat)
206 {
207 case CAM_MEDIA_FORMAT_YUY2:
208 return AV_PIX_FMT_YUYV422;
209 case CAM_MEDIA_FORMAT_NV12:
210 return AV_PIX_FMT_NV12;
211 case CAM_MEDIA_FORMAT_I420:
212 return AV_PIX_FMT_YUV420P;
213 case CAM_MEDIA_FORMAT_RGB24:
214 return AV_PIX_FMT_RGB24;
215 case CAM_MEDIA_FORMAT_RGB32:
216 return AV_PIX_FMT_RGB32;
217 default:
218 WLog_ERR(TAG, "Unsupported ecamFormat %u", ecamFormat);
219 return AV_PIX_FMT_NONE;
220 }
221}
222
223static void ecam_sws_free(CameraDeviceStream* stream)
224{
225 if (stream->sws)
226 {
227 sws_freeContext(stream->sws);
228 stream->sws = NULL;
229 }
230}
231
232static BOOL ecam_sws_valid(const CameraDeviceStream* stream)
233{
234 if (!stream->sws)
235 return FALSE;
236 if (stream->swsWidth != stream->currMediaType.Width)
237 return FALSE;
238 if (stream->swsHeight != stream->currMediaType.Height)
239 return FALSE;
240 if (stream->currMediaType.Width > INT32_MAX)
241 return FALSE;
242 if (stream->currMediaType.Height > INT32_MAX)
243 return FALSE;
244 return TRUE;
245}
246
253static BOOL ecam_init_sws_context(CameraDeviceStream* stream, enum AVPixelFormat pixFormat)
254{
255 WINPR_ASSERT(stream);
256
257 if (stream->currMediaType.Width > INT32_MAX)
258 return FALSE;
259 if (stream->currMediaType.Height > INT32_MAX)
260 return FALSE;
261
262 if (ecam_sws_valid(stream))
263 return TRUE;
264
265 ecam_sws_free(stream);
266
267 /* replacing deprecated JPEG formats, still produced by decoder */
268 switch (pixFormat)
269 {
270 case AV_PIX_FMT_YUVJ411P:
271 pixFormat = AV_PIX_FMT_YUV411P;
272 break;
273
274 case AV_PIX_FMT_YUVJ420P:
275 pixFormat = AV_PIX_FMT_YUV420P;
276 break;
277
278 case AV_PIX_FMT_YUVJ422P:
279 pixFormat = AV_PIX_FMT_YUV422P;
280 break;
281
282 case AV_PIX_FMT_YUVJ440P:
283 pixFormat = AV_PIX_FMT_YUV440P;
284 break;
285
286 case AV_PIX_FMT_YUVJ444P:
287 pixFormat = AV_PIX_FMT_YUV444P;
288 break;
289
290 default:
291 break;
292 }
293
294 stream->swsWidth = stream->currMediaType.Width;
295 stream->swsHeight = stream->currMediaType.Height;
296 const int width = WINPR_ASSERTING_INT_CAST(int, stream->currMediaType.Width);
297 const int height = WINPR_ASSERTING_INT_CAST(int, stream->currMediaType.Height);
298
299 const enum AVPixelFormat outPixFormat =
300 h264_context_get_option(stream->h264, H264_CONTEXT_OPTION_HW_ACCEL) ? AV_PIX_FMT_NV12
301 : AV_PIX_FMT_YUV420P;
302
303 stream->sws =
304 sws_getContext(width, height, pixFormat, width, height, outPixFormat, 0, NULL, NULL, NULL);
305 if (!stream->sws)
306 {
307 WLog_ERR(TAG, "sws_getContext failed");
308 return FALSE;
309 }
310
311 return TRUE;
312}
313
319static BOOL ecam_encoder_compress_h264(CameraDeviceStream* stream, const BYTE* srcData,
320 size_t srcSize, BYTE** ppDstData, size_t* pDstSize)
321{
322 UINT32 dstSize = 0;
323 BYTE* srcSlice[4] = { 0 };
324 int srcLineSizes[4] = { 0 };
325 BYTE* yuvData[3] = { 0 };
326 UINT32 yuvLineSizes[3] = { 0 };
327 prim_size_t size = { stream->currMediaType.Width, stream->currMediaType.Height };
328 CAM_MEDIA_FORMAT inputFormat = streamInputFormat(stream);
329 enum AVPixelFormat pixFormat = AV_PIX_FMT_NONE;
330
331#if defined(WITH_INPUT_FORMAT_H264)
332 if (inputFormat == CAM_MEDIA_FORMAT_MJPG_H264)
333 {
334 const size_t rc =
335 demux_uvcH264(srcData, srcSize, stream->h264Frame, stream->h264FrameMaxSize);
336 dstSize = WINPR_ASSERTING_INT_CAST(uint32_t, rc);
337 *ppDstData = stream->h264Frame;
338 *pDstSize = dstSize;
339 return dstSize > 0;
340 }
341 else
342#endif
343
344#if defined(WITH_INPUT_FORMAT_MJPG)
345 if (inputFormat == CAM_MEDIA_FORMAT_MJPG)
346 {
347 stream->avInputPkt->data = WINPR_CAST_CONST_PTR_AWAY(srcData, uint8_t*);
348 WINPR_ASSERT(srcSize <= INT32_MAX);
349 stream->avInputPkt->size = (int)srcSize;
350
351 if (avcodec_send_packet(stream->avContext, stream->avInputPkt) < 0)
352 {
353 WLog_ERR(TAG, "avcodec_send_packet failed");
354 return FALSE;
355 }
356
357 if (avcodec_receive_frame(stream->avContext, stream->avOutFrame) < 0)
358 {
359 WLog_ERR(TAG, "avcodec_receive_frame failed");
360 return FALSE;
361 }
362
363 for (size_t i = 0; i < 4; i++)
364 {
365 srcSlice[i] = stream->avOutFrame->data[i];
366 srcLineSizes[i] = stream->avOutFrame->linesize[i];
367 }
368
369 /* get pixFormat produced by MJPEG decoder */
370 pixFormat = stream->avContext->pix_fmt;
371 }
372 else
373#endif
374 {
375 pixFormat = ecamToAVPixFormat(inputFormat);
376
377 if (av_image_fill_linesizes(srcLineSizes, pixFormat, (int)size.width) < 0)
378 {
379 WLog_ERR(TAG, "av_image_fill_linesizes failed");
380 return FALSE;
381 }
382
383 if (av_image_fill_pointers(srcSlice, pixFormat, (int)size.height,
384 WINPR_CAST_CONST_PTR_AWAY(srcData, BYTE*), srcLineSizes) < 0)
385 {
386 WLog_ERR(TAG, "av_image_fill_pointers failed");
387 return FALSE;
388 }
389 }
390
391 /* get buffers for YUV420P or NV12 */
392 if (h264_get_yuv_buffer(stream->h264, 0, size.width, size.height, yuvData, yuvLineSizes) < 0)
393 return FALSE;
394
395 /* convert from source format to YUV420P or NV12 */
396 if (!ecam_init_sws_context(stream, pixFormat))
397 return FALSE;
398
399 const BYTE* cSrcSlice[4] = { srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3] };
400 if (sws_scale(stream->sws, cSrcSlice, srcLineSizes, 0, (int)size.height, yuvData,
401 (int*)yuvLineSizes) <= 0)
402 return FALSE;
403
404 /* encode from YUV420P or NV12 to H264 */
405 if (h264_compress(stream->h264, ppDstData, &dstSize) < 0)
406 return FALSE;
407
408 *pDstSize = dstSize;
409
410 return TRUE;
411}
412
417static void ecam_encoder_context_free_h264(CameraDeviceStream* stream)
418{
419 WINPR_ASSERT(stream);
420
421 ecam_sws_free(stream);
422
423#if defined(WITH_INPUT_FORMAT_MJPG)
424 if (stream->avOutFrame)
425 av_frame_free(&stream->avOutFrame); /* sets to NULL */
426
427 if (stream->avInputPkt)
428 {
429 stream->avInputPkt->data = NULL;
430 stream->avInputPkt->size = 0;
431 av_packet_free(&stream->avInputPkt); /* sets to NULL */
432 }
433
434 if (stream->avContext)
435 avcodec_free_context(&stream->avContext); /* sets to NULL */
436#endif
437
438#if defined(WITH_INPUT_FORMAT_H264)
439 if (stream->h264Frame)
440 {
441 free(stream->h264Frame);
442 stream->h264Frame = NULL;
443 }
444#endif
445
446 if (stream->h264)
447 {
448 h264_context_free(stream->h264);
449 stream->h264 = NULL;
450 }
451}
452
453#if defined(WITH_INPUT_FORMAT_MJPG)
459static BOOL ecam_init_mjpeg_decoder(CameraDeviceStream* stream)
460{
461 WINPR_ASSERT(stream);
462
463 const AVCodec* avcodec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
464 if (!avcodec)
465 {
466 WLog_ERR(TAG, "avcodec_find_decoder failed to find MJPEG codec");
467 return FALSE;
468 }
469
470 stream->avContext = avcodec_alloc_context3(avcodec);
471 if (!stream->avContext)
472 {
473 WLog_ERR(TAG, "avcodec_alloc_context3 failed");
474 return FALSE;
475 }
476
477 stream->avContext->width = WINPR_ASSERTING_INT_CAST(int, stream->currMediaType.Width);
478 stream->avContext->height = WINPR_ASSERTING_INT_CAST(int, stream->currMediaType.Height);
479
480 /* AV_EF_EXPLODE flag is to abort decoding on minor error detection,
481 * return error, so we can skip corrupted frames, if any */
482 stream->avContext->err_recognition |= AV_EF_EXPLODE;
483
484 if (avcodec_open2(stream->avContext, avcodec, NULL) < 0)
485 {
486 WLog_ERR(TAG, "avcodec_open2 failed");
487 return FALSE;
488 }
489
490 stream->avInputPkt = av_packet_alloc();
491 if (!stream->avInputPkt)
492 {
493 WLog_ERR(TAG, "av_packet_alloc failed");
494 return FALSE;
495 }
496
497 stream->avOutFrame = av_frame_alloc();
498 if (!stream->avOutFrame)
499 {
500 WLog_ERR(TAG, "av_frame_alloc failed");
501 return FALSE;
502 }
503
504 return TRUE;
505}
506#endif
507
513static BOOL ecam_encoder_context_init_h264(CameraDeviceStream* stream)
514{
515 WINPR_ASSERT(stream);
516
517#if defined(WITH_INPUT_FORMAT_H264)
518 if (streamInputFormat(stream) == CAM_MEDIA_FORMAT_MJPG_H264)
519 {
520 stream->h264FrameMaxSize = 1ULL * stream->currMediaType.Width *
521 stream->currMediaType.Height; /* 1 byte per pixel */
522 stream->h264Frame = (BYTE*)calloc(stream->h264FrameMaxSize, sizeof(BYTE));
523 return TRUE; /* encoder not needed */
524 }
525#endif
526
527 if (!stream->h264)
528 stream->h264 = h264_context_new(TRUE);
529
530 if (!stream->h264)
531 {
532 WLog_ERR(TAG, "h264_context_new failed");
533 return FALSE;
534 }
535
536 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_USAGETYPE,
537 H264_CAMERA_VIDEO_REAL_TIME))
538 goto fail;
539
540 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_FRAMERATE,
541 stream->currMediaType.FrameRateNumerator /
542 stream->currMediaType.FrameRateDenominator))
543 goto fail;
544
545 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_BITRATE,
546 h264_get_max_bitrate(stream->currMediaType.Height)))
547 goto fail;
548
549 /* Using CQP mode for rate control. It produces more comparable quality
550 * between VAAPI and software encoding than VBR mode
551 */
552 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_RATECONTROL,
553 H264_RATECONTROL_CQP))
554 goto fail;
555
556 /* Using 26 as CQP value. Lower values will produce better quality but
557 * higher bitrate; higher values - lower bitrate but degraded quality
558 */
559 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_QP, 26))
560 goto fail;
561
562 /* Requesting hardware acceleration before calling h264_context_reset */
563 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_HW_ACCEL, TRUE))
564 goto fail;
565
566 if (!h264_context_reset(stream->h264, stream->currMediaType.Width,
567 stream->currMediaType.Height))
568 {
569 WLog_ERR(TAG, "h264_context_reset failed");
570 goto fail;
571 }
572
573#if defined(WITH_INPUT_FORMAT_MJPG)
574 if (streamInputFormat(stream) == CAM_MEDIA_FORMAT_MJPG && !ecam_init_mjpeg_decoder(stream))
575 goto fail;
576#endif
577
578 return TRUE;
579
580fail:
581 ecam_encoder_context_free_h264(stream);
582 return FALSE;
583}
584
590BOOL ecam_encoder_context_init(CameraDeviceStream* stream)
591{
592 CAM_MEDIA_FORMAT format = streamOutputFormat(stream);
593
594 switch (format)
595 {
596 case CAM_MEDIA_FORMAT_H264:
597 return ecam_encoder_context_init_h264(stream);
598
599 default:
600 WLog_ERR(TAG, "Unsupported output format %u", format);
601 return FALSE;
602 }
603}
604
610BOOL ecam_encoder_context_free(CameraDeviceStream* stream)
611{
612 CAM_MEDIA_FORMAT format = streamOutputFormat(stream);
613 switch (format)
614 {
615 case CAM_MEDIA_FORMAT_H264:
616 ecam_encoder_context_free_h264(stream);
617 break;
618
619 default:
620 return FALSE;
621 }
622 return TRUE;
623}
624
630BOOL ecam_encoder_compress(CameraDeviceStream* stream, const BYTE* srcData, size_t srcSize,
631 BYTE** ppDstData, size_t* pDstSize)
632{
633 CAM_MEDIA_FORMAT format = streamOutputFormat(stream);
634 switch (format)
635 {
636 case CAM_MEDIA_FORMAT_H264:
637 return ecam_encoder_compress_h264(stream, srcData, srcSize, ppDstData, pDstSize);
638 default:
639 WLog_ERR(TAG, "Unsupported output format %u", format);
640 return FALSE;
641 }
642}