Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
lwir_ssse3.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** @file
19 * @brief x86-64 SSSE3 expansion of the camera's own YUYV image into JPEG Y/Cb/Cr scanlines.
20 * @details This is the only translation unit built with `-mssse3` (see
21 * build-support/make/lwircam.mk), so the rest of LWIRcam stays baseline x86-64.
22 * lwir_render.c calls it from sensor_row() only after a cached CPUID check and only for
23 * horizontal (+1/-1 stride) display rows of at least eight pixels; transposed rows,
24 * the remainder tail and CPUs without SSSE3 use the scalar loop, which produces the
25 * same bytes. No camera bytes are modified and no allocation is made.
26 *
27 * Input layout: packed YUYV, four bytes per pixel pair `Y0 U Y1 V`, so the two pixels of a
28 * pair share one Cb (U) and one Cr (V). Output layout: three bytes per pixel `Y Cb Cr`,
29 * ready for a libjpeg YCbCr scanline without an RGB round trip.
30 */
31#include "lwir_render.h"
32
33#include <tmmintrin.h>
34
35/**
36 * @brief Expand eight YUYV pixels per iteration into interleaved Y/Cb/Cr, forwards or reversed.
37 * @details One unaligned 16-byte load holds exactly four YUYV pairs (pixels 0..7 in memory).
38 * Two PSHUFB masks scatter those bytes into the 24 output bytes: the `low` mask fills
39 * output bytes 0..15 (five whole pixels plus the sixth pixel's Y) and the `high` mask
40 * fills bytes 16..23 (the sixth pixel's Cb/Cr and the last two pixels); -128 lanes are
41 * zeroed by PSHUFB and fall outside the 8-byte store.
42 *
43 * Forward (step > 0) masks follow memory order: pixel p takes Y at byte 2p and the
44 * pair's U/V at bytes (p/2)*4+1 and (p/2)*4+3. Reversed (step < 0) rows load the block
45 * that ENDS at the current pixel (`pixel - 7`) and the masks list memory pixels 7..0, so
46 * mirroring costs no extra instruction.
47 * @param frame Image plane start (combined frame or image-only YUYV buffer).
48 * @param first Native pixel index of display column 0, from row_source().
49 * @param step Native pixel stride per display column; must be +1 or -1.
50 * @param width Display row width in pixels.
51 * @param output Destination row with room for at least `width * 3` bytes.
52 * @return Number of leading display columns written, a multiple of 8 (<= width); the
53 * caller completes the remainder with the scalar path.
54 * @warning Requires SSSE3; callers must check CPU support first. Reversed rows rely on
55 * `pixel - 7` being even (even native width puts the row's last pixel at an odd index),
56 * so each load starts on an intact YUYV pair; arbitrary `first` values would split pairs.
57 */
58unsigned lwir_sensor_ssse3(const uint8_t *frame, ptrdiff_t first, ptrdiff_t step,
59 unsigned width, uint8_t *output)
60{
61 /* Triplets are (Y, U, V) source bytes per output pixel; both pixels of a pair repeat
62 * bytes 1 and 3 because YUYV chroma is shared. Reversed masks walk pairs 3..0. */
63 const __m128i low = step > 0 ? _mm_setr_epi8(0,1,3,2,1,3,4,5,7,6,5,7,8,9,11,10) :
64 _mm_setr_epi8(14,13,15,12,13,15,10,9,11,8,9,11,6,5,7,4);
65 const __m128i high = step > 0 ? _mm_setr_epi8(9,11,12,13,15,14,13,15,-128,-128,-128,-128,-128,-128,-128,-128) :
66 _mm_setr_epi8(5,7,2,1,3,0,1,3,-128,-128,-128,-128,-128,-128,-128,-128);
67 unsigned column = 0;
68 for (; width - column >= 8; column += 8) {
69 const ptrdiff_t pixel = first + (ptrdiff_t)column * step;
70 /* Reading exactly 16 bytes (8 pixels) and writing 16 + 8 bytes stays inside both rows. */
71 const __m128i packed = _mm_loadu_si128((const __m128i *)(frame + (size_t)(step < 0 ? pixel - 7 : pixel) * 2));
72 _mm_storeu_si128((__m128i *)(output + column * 3), _mm_shuffle_epi8(packed, low));
73 _mm_storel_epi64((__m128i *)(output + column * 3 + 16), _mm_shuffle_epi8(packed, high));
74 }
75 return column;
76}
uint8_t frame[LWIR_MAX_FRAME_BYTES]
Definition lwir_mock.c:58
Display rendering of Tiny1-C frames into JPEG-ready Y/Cb/Cr scanlines.
unsigned lwir_sensor_ssse3(const uint8_t *frame, ptrdiff_t first, ptrdiff_t step, unsigned width, uint8_t *output)
Expand eight YUYV pixels per iteration into interleaved Y/Cb/Cr, forwards or reversed.
Definition lwir_ssse3.c:58
static FILE * output
Definition pose_log.c:77