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 %d", ecamFormat);
219 return AV_PIX_FMT_NONE;
220 }
221}
222
229static BOOL ecam_init_sws_context(CameraDeviceStream* stream, enum AVPixelFormat pixFormat)
230{
231 WINPR_ASSERT(stream);
232
233 if (stream->sws)
234 return TRUE;
235
236 /* replacing deprecated JPEG formats, still produced by decoder */
237 switch (pixFormat)
238 {
239 case AV_PIX_FMT_YUVJ411P:
240 pixFormat = AV_PIX_FMT_YUV411P;
241 break;
242
243 case AV_PIX_FMT_YUVJ420P:
244 pixFormat = AV_PIX_FMT_YUV420P;
245 break;
246
247 case AV_PIX_FMT_YUVJ422P:
248 pixFormat = AV_PIX_FMT_YUV422P;
249 break;
250
251 case AV_PIX_FMT_YUVJ440P:
252 pixFormat = AV_PIX_FMT_YUV440P;
253 break;
254
255 case AV_PIX_FMT_YUVJ444P:
256 pixFormat = AV_PIX_FMT_YUV444P;
257 break;
258
259 default:
260 break;
261 }
262
263 const int width = (int)stream->currMediaType.Width;
264 const int height = (int)stream->currMediaType.Height;
265
266 const enum AVPixelFormat outPixFormat =
267 h264_context_get_option(stream->h264, H264_CONTEXT_OPTION_HW_ACCEL) ? AV_PIX_FMT_NV12
268 : AV_PIX_FMT_YUV420P;
269
270 stream->sws =
271 sws_getContext(width, height, pixFormat, width, height, outPixFormat, 0, NULL, NULL, NULL);
272 if (!stream->sws)
273 {
274 WLog_ERR(TAG, "sws_getContext failed");
275 return FALSE;
276 }
277
278 return TRUE;
279}
280
286static BOOL ecam_encoder_compress_h264(CameraDeviceStream* stream, const BYTE* srcData,
287 size_t srcSize, BYTE** ppDstData, size_t* pDstSize)
288{
289 UINT32 dstSize = 0;
290 BYTE* srcSlice[4] = { 0 };
291 int srcLineSizes[4] = { 0 };
292 BYTE* yuvData[3] = { 0 };
293 UINT32 yuvLineSizes[3] = { 0 };
294 prim_size_t size = { stream->currMediaType.Width, stream->currMediaType.Height };
295 CAM_MEDIA_FORMAT inputFormat = streamInputFormat(stream);
296 enum AVPixelFormat pixFormat = AV_PIX_FMT_NONE;
297
298#if defined(WITH_INPUT_FORMAT_H264)
299 if (inputFormat == CAM_MEDIA_FORMAT_MJPG_H264)
300 {
301 const size_t rc =
302 demux_uvcH264(srcData, srcSize, stream->h264Frame, stream->h264FrameMaxSize);
303 dstSize = WINPR_ASSERTING_INT_CAST(uint32_t, rc);
304 *ppDstData = stream->h264Frame;
305 *pDstSize = dstSize;
306 return dstSize > 0;
307 }
308 else
309#endif
310
311#if defined(WITH_INPUT_FORMAT_MJPG)
312 if (inputFormat == CAM_MEDIA_FORMAT_MJPG)
313 {
314 stream->avInputPkt->data = WINPR_CAST_CONST_PTR_AWAY(srcData, uint8_t*);
315 WINPR_ASSERT(srcSize <= INT32_MAX);
316 stream->avInputPkt->size = (int)srcSize;
317
318 if (avcodec_send_packet(stream->avContext, stream->avInputPkt) < 0)
319 {
320 WLog_ERR(TAG, "avcodec_send_packet failed");
321 return FALSE;
322 }
323
324 if (avcodec_receive_frame(stream->avContext, stream->avOutFrame) < 0)
325 {
326 WLog_ERR(TAG, "avcodec_receive_frame failed");
327 return FALSE;
328 }
329
330 for (size_t i = 0; i < 4; i++)
331 {
332 srcSlice[i] = stream->avOutFrame->data[i];
333 srcLineSizes[i] = stream->avOutFrame->linesize[i];
334 }
335
336 /* get pixFormat produced by MJPEG decoder */
337 pixFormat = stream->avContext->pix_fmt;
338 }
339 else
340#endif
341 {
342 pixFormat = ecamToAVPixFormat(inputFormat);
343
344 if (av_image_fill_linesizes(srcLineSizes, pixFormat, (int)size.width) < 0)
345 {
346 WLog_ERR(TAG, "av_image_fill_linesizes failed");
347 return FALSE;
348 }
349
350 if (av_image_fill_pointers(srcSlice, pixFormat, (int)size.height,
351 WINPR_CAST_CONST_PTR_AWAY(srcData, BYTE*), srcLineSizes) < 0)
352 {
353 WLog_ERR(TAG, "av_image_fill_pointers failed");
354 return FALSE;
355 }
356 }
357
358 /* get buffers for YUV420P or NV12 */
359 if (h264_get_yuv_buffer(stream->h264, 0, size.width, size.height, yuvData, yuvLineSizes) < 0)
360 return FALSE;
361
362 /* convert from source format to YUV420P or NV12 */
363 if (!ecam_init_sws_context(stream, pixFormat))
364 return FALSE;
365
366 const BYTE* cSrcSlice[4] = { srcSlice[0], srcSlice[1], srcSlice[2], srcSlice[3] };
367 if (sws_scale(stream->sws, cSrcSlice, srcLineSizes, 0, (int)size.height, yuvData,
368 (int*)yuvLineSizes) <= 0)
369 return FALSE;
370
371 /* encode from YUV420P or NV12 to H264 */
372 if (h264_compress(stream->h264, ppDstData, &dstSize) < 0)
373 return FALSE;
374
375 *pDstSize = dstSize;
376
377 return TRUE;
378}
379
384static void ecam_encoder_context_free_h264(CameraDeviceStream* stream)
385{
386 WINPR_ASSERT(stream);
387
388 if (stream->sws)
389 {
390 sws_freeContext(stream->sws);
391 stream->sws = NULL;
392 }
393
394#if defined(WITH_INPUT_FORMAT_MJPG)
395 if (stream->avOutFrame)
396 av_frame_free(&stream->avOutFrame); /* sets to NULL */
397
398 if (stream->avInputPkt)
399 {
400 stream->avInputPkt->data = NULL;
401 stream->avInputPkt->size = 0;
402 av_packet_free(&stream->avInputPkt); /* sets to NULL */
403 }
404
405 if (stream->avContext)
406 avcodec_free_context(&stream->avContext); /* sets to NULL */
407#endif
408
409#if defined(WITH_INPUT_FORMAT_H264)
410 if (stream->h264Frame)
411 {
412 free(stream->h264Frame);
413 stream->h264Frame = NULL;
414 }
415#endif
416
417 if (stream->h264)
418 {
419 h264_context_free(stream->h264);
420 stream->h264 = NULL;
421 }
422}
423
424#if defined(WITH_INPUT_FORMAT_MJPG)
430static BOOL ecam_init_mjpeg_decoder(CameraDeviceStream* stream)
431{
432 WINPR_ASSERT(stream);
433
434 const AVCodec* avcodec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
435 if (!avcodec)
436 {
437 WLog_ERR(TAG, "avcodec_find_decoder failed to find MJPEG codec");
438 return FALSE;
439 }
440
441 stream->avContext = avcodec_alloc_context3(avcodec);
442 if (!stream->avContext)
443 {
444 WLog_ERR(TAG, "avcodec_alloc_context3 failed");
445 return FALSE;
446 }
447
448 stream->avContext->width = WINPR_ASSERTING_INT_CAST(int, stream->currMediaType.Width);
449 stream->avContext->height = WINPR_ASSERTING_INT_CAST(int, stream->currMediaType.Height);
450
451 /* AV_EF_EXPLODE flag is to abort decoding on minor error detection,
452 * return error, so we can skip corrupted frames, if any */
453 stream->avContext->err_recognition |= AV_EF_EXPLODE;
454
455 if (avcodec_open2(stream->avContext, avcodec, NULL) < 0)
456 {
457 WLog_ERR(TAG, "avcodec_open2 failed");
458 return FALSE;
459 }
460
461 stream->avInputPkt = av_packet_alloc();
462 if (!stream->avInputPkt)
463 {
464 WLog_ERR(TAG, "av_packet_alloc failed");
465 return FALSE;
466 }
467
468 stream->avOutFrame = av_frame_alloc();
469 if (!stream->avOutFrame)
470 {
471 WLog_ERR(TAG, "av_frame_alloc failed");
472 return FALSE;
473 }
474
475 return TRUE;
476}
477#endif
478
484static BOOL ecam_encoder_context_init_h264(CameraDeviceStream* stream)
485{
486 WINPR_ASSERT(stream);
487
488#if defined(WITH_INPUT_FORMAT_H264)
489 if (streamInputFormat(stream) == CAM_MEDIA_FORMAT_MJPG_H264)
490 {
491 stream->h264FrameMaxSize = 1ULL * stream->currMediaType.Width *
492 stream->currMediaType.Height; /* 1 byte per pixel */
493 stream->h264Frame = (BYTE*)calloc(stream->h264FrameMaxSize, sizeof(BYTE));
494 return TRUE; /* encoder not needed */
495 }
496#endif
497
498 if (!stream->h264)
499 stream->h264 = h264_context_new(TRUE);
500
501 if (!stream->h264)
502 {
503 WLog_ERR(TAG, "h264_context_new failed");
504 return FALSE;
505 }
506
507 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_USAGETYPE,
508 H264_CAMERA_VIDEO_REAL_TIME))
509 goto fail;
510
511 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_FRAMERATE,
512 stream->currMediaType.FrameRateNumerator /
513 stream->currMediaType.FrameRateDenominator))
514 goto fail;
515
516 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_BITRATE,
517 h264_get_max_bitrate(stream->currMediaType.Height)))
518 goto fail;
519
520 /* Using CQP mode for rate control. It produces more comparable quality
521 * between VAAPI and software encoding than VBR mode
522 */
523 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_RATECONTROL,
524 H264_RATECONTROL_CQP))
525 goto fail;
526
527 /* Using 26 as CQP value. Lower values will produce better quality but
528 * higher bitrate; higher values - lower bitrate but degraded quality
529 */
530 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_QP, 26))
531 goto fail;
532
533 /* Requesting hardware acceleration before calling h264_context_reset */
534 if (!h264_context_set_option(stream->h264, H264_CONTEXT_OPTION_HW_ACCEL, TRUE))
535 goto fail;
536
537 if (!h264_context_reset(stream->h264, stream->currMediaType.Width,
538 stream->currMediaType.Height))
539 {
540 WLog_ERR(TAG, "h264_context_reset failed");
541 goto fail;
542 }
543
544#if defined(WITH_INPUT_FORMAT_MJPG)
545 if (streamInputFormat(stream) == CAM_MEDIA_FORMAT_MJPG && !ecam_init_mjpeg_decoder(stream))
546 goto fail;
547#endif
548
549 return TRUE;
550
551fail:
552 ecam_encoder_context_free_h264(stream);
553 return FALSE;
554}
555
561BOOL ecam_encoder_context_init(CameraDeviceStream* stream)
562{
563 CAM_MEDIA_FORMAT format = streamOutputFormat(stream);
564
565 switch (format)
566 {
567 case CAM_MEDIA_FORMAT_H264:
568 return ecam_encoder_context_init_h264(stream);
569
570 default:
571 WLog_ERR(TAG, "Unsupported output format %d", format);
572 return FALSE;
573 }
574}
575
581BOOL ecam_encoder_context_free(CameraDeviceStream* stream)
582{
583 CAM_MEDIA_FORMAT format = streamOutputFormat(stream);
584 switch (format)
585 {
586 case CAM_MEDIA_FORMAT_H264:
587 ecam_encoder_context_free_h264(stream);
588 break;
589
590 default:
591 return FALSE;
592 }
593 return TRUE;
594}
595
601BOOL ecam_encoder_compress(CameraDeviceStream* stream, const BYTE* srcData, size_t srcSize,
602 BYTE** ppDstData, size_t* pDstSize)
603{
604 CAM_MEDIA_FORMAT format = streamOutputFormat(stream);
605 switch (format)
606 {
607 case CAM_MEDIA_FORMAT_H264:
608 return ecam_encoder_compress_h264(stream, srcData, srcSize, ppDstData, pDstSize);
609 default:
610 WLog_ERR(TAG, "Unsupported output format %d", format);
611 return FALSE;
612 }
613}