History

repack: add restrict qualifier to src/dst pointers

Allows compiler to do its job and optimize this code. We don't really want to repack in-place.

Kacper Michajłow committed 2 years ago

Commit 374470d1 parent e0b51791 file · +36 −36Browse files

Unified · Split

video/repack.c+36−36
@@ -63,14 +63,14 @@ struct mp_repack { · 62 unmodified lines
63 int components[4]; // b[n] = mp_image.planes[components[n]]63 int components[4]; // b[n] = mp_image.planes[components[n]]
64 // pack: a is dst, b is src64 // pack: a is dst, b is src
65 // unpack: a is src, b is dst65 // unpack: a is src, b is dst
66 void (*packed_repack_scanline)(void *a, void *b[], int w);66 void (*packed_repack_scanline)(void *restrict a, void *restrict b[], int w);
67 67
68 // Fringe RGB/YUV.68 // Fringe RGB/YUV.
69 uint8_t comp_size;69 uint8_t comp_size;
70 uint8_t comp_map[6];70 uint8_t comp_map[6];
71 uint8_t comp_shifts[3];71 uint8_t comp_shifts[3];
72 uint8_t *comp_lut;72 uint8_t *comp_lut;
73 void (*repack_fringe_yuv)(void *dst, void *src[], int w, uint8_t *c);73 void (*repack_fringe_yuv)(void *restrict dst, void *restrict src[], int w, uint8_t *restrict c);
74 74
75 // F32 repacking.75 // F32 repacking.
76 int f32_comp_size;76 int f32_comp_size;
@@ -133,8 +133,8 @@ static void copy_plane(struct mp_image *dst, int dst_x, int dst_y, · 56 unmodified lines
133 assert(dst->fmt.bpp[p] == src->fmt.bpp[p]);133 assert(dst->fmt.bpp[p] == src->fmt.bpp[p]);
134 134
135 for (int y = 0; y < h; y++) {135 for (int y = 0; y < h; y++) {
136 void *pd = mp_image_pixel_ptr_ny(dst, p, dst_x, dst_y + y);136 void *restrict pd = mp_image_pixel_ptr_ny(dst, p, dst_x, dst_y + y);
137 void *ps = mp_image_pixel_ptr_ny(src, p, src_x, src_y + y);137 void *restrict ps = mp_image_pixel_ptr_ny(src, p, src_x, src_y + y);
138 memcpy(pd, ps, size);138 memcpy(pd, ps, size);
139 }139 }
140}140}
@@ -157,8 +157,8 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, · 16 unmodified lines
157 assert(src->fmt.bpp[p] == bpp * 8);157 assert(src->fmt.bpp[p] == bpp * 8);
158 158
159 for (int y = 0; y < h; y++) {159 for (int y = 0; y < h; y++) {
160 void *s = mp_image_pixel_ptr_ny(src, p, src_x, src_y + y);160 void *restrict s = mp_image_pixel_ptr_ny(src, p, src_x, src_y + y);
161 void *d = mp_image_pixel_ptr_ny(dst, p, dst_x, dst_y + y);161 void *restrict d = mp_image_pixel_ptr_ny(dst, p, dst_x, dst_y + y);
162 switch (endian_size) {162 switch (endian_size) {
163 case 2:163 case 2:
164 for (int x = 0; x < num_words; x++)164 for (int x = 0; x < num_words; x++)
@@ -191,7 +191,7 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, · 26 unmodified lines
191// packers will use "z" because they write zero.191// packers will use "z" because they write zero.
192 192
193#define PA_WORD_4(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, sh_c3) \193#define PA_WORD_4(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, sh_c3) \
194 static void name(void *dst, void *src[], int w) { \194 static void name(void *restrict dst, void *restrict src[], int w) { \
195 for (int x = 0; x < w; x++) { \195 for (int x = 0; x < w; x++) { \
196 ((packed_t *)dst)[x] = \196 ((packed_t *)dst)[x] = \
197 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \197 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \
@@ -202,7 +202,7 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, · 4 unmodified lines
202 }202 }
203 203
204#define UN_WORD_4(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, sh_c3, mask)\204#define UN_WORD_4(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, sh_c3, mask)\
205 static void name(void *src, void *dst[], int w) { \205 static void name(void *restrict src, void *restrict dst[], int w) { \
206 for (int x = 0; x < w; x++) { \206 for (int x = 0; x < w; x++) { \
207 packed_t c = ((packed_t *)src)[x]; \207 packed_t c = ((packed_t *)src)[x]; \
208 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \208 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \
@@ -214,7 +214,7 @@ static void swap_endian(struct mp_image *dst, int dst_x, int dst_y, · 5 unmodified lines
214 214
215 215
216#define PA_WORD_3(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, pad) \216#define PA_WORD_3(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, pad) \
217 static void name(void *dst, void *src[], int w) { \217 static void name(void *restrict dst, void *restrict src[], int w) { \
218 for (int x = 0; x < w; x++) { \218 for (int x = 0; x < w; x++) { \
219 ((packed_t *)dst)[x] = (pad) | \219 ((packed_t *)dst)[x] = (pad) | \
220 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \220 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \
@@ -230,7 +230,7 @@ UN_WORD_4(un_cccc16, uint64_t, uint16_t, 0, 16, 32, 48, 0xFFFFu) · 9 unmodified lines
230PA_WORD_4(pa_cccc16, uint64_t, uint16_t, 0, 16, 32, 48)230PA_WORD_4(pa_cccc16, uint64_t, uint16_t, 0, 16, 32, 48)
231 231
232#define UN_WORD_3(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, mask) \232#define UN_WORD_3(name, packed_t, plane_t, sh_c0, sh_c1, sh_c2, mask) \
233 static void name(void *src, void *dst[], int w) { \233 static void name(void *restrict src, void *restrict dst[], int w) { \
234 for (int x = 0; x < w; x++) { \234 for (int x = 0; x < w; x++) { \
235 packed_t c = ((packed_t *)src)[x]; \235 packed_t c = ((packed_t *)src)[x]; \
236 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \236 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \
@@ -249,7 +249,7 @@ UN_WORD_3(un_ccc16x16, uint64_t, uint16_t, 0, 16, 32, 0xFFFFu) · 12 unmodified lines
249PA_WORD_3(pa_ccc16z16, uint64_t, uint16_t, 0, 16, 32, 0)249PA_WORD_3(pa_ccc16z16, uint64_t, uint16_t, 0, 16, 32, 0)
250 250
251#define PA_WORD_2(name, packed_t, plane_t, sh_c0, sh_c1, pad) \251#define PA_WORD_2(name, packed_t, plane_t, sh_c0, sh_c1, pad) \
252 static void name(void *dst, void *src[], int w) { \252 static void name(void *restrict dst, void *restrict src[], int w) { \
253 for (int x = 0; x < w; x++) { \253 for (int x = 0; x < w; x++) { \
254 ((packed_t *)dst)[x] = (pad) | \254 ((packed_t *)dst)[x] = (pad) | \
255 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \255 ((packed_t)((plane_t *)src[0])[x] << (sh_c0)) | \
@@ -258,7 +258,7 @@ PA_WORD_3(pa_ccc16z16, uint64_t, uint16_t, 0, 16, 32, 0) · 2 unmodified lines
258 }258 }
259 259
260#define UN_WORD_2(name, packed_t, plane_t, sh_c0, sh_c1, mask) \260#define UN_WORD_2(name, packed_t, plane_t, sh_c0, sh_c1, mask) \
261 static void name(void *src, void *dst[], int w) { \261 static void name(void *restrict src, void *restrict dst[], int w) { \
262 for (int x = 0; x < w; x++) { \262 for (int x = 0; x < w; x++) { \
263 packed_t c = ((packed_t *)src)[x]; \263 packed_t c = ((packed_t *)src)[x]; \
264 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \264 ((plane_t *)dst[0])[x] = (c >> (sh_c0)) & (mask); \
@@ -272,7 +272,7 @@ UN_WORD_2(un_cc16, uint32_t, uint16_t, 0, 16, 0xFFFFu) · 7 unmodified lines
272PA_WORD_2(pa_cc16, uint32_t, uint16_t, 0, 16, 0)272PA_WORD_2(pa_cc16, uint32_t, uint16_t, 0, 16, 0)
273 273
274#define PA_SEQ_3(name, comp_t) \274#define PA_SEQ_3(name, comp_t) \
275 static void name(void *dst, void *src[], int w) { \275 static void name(void *restrict dst, void *restrict src[], int w) { \
276 comp_t *r = dst; \276 comp_t *r = dst; \
277 for (int x = 0; x < w; x++) { \277 for (int x = 0; x < w; x++) { \
278 *r++ = ((comp_t *)src[0])[x]; \278 *r++ = ((comp_t *)src[0])[x]; \
@@ -282,7 +282,7 @@ PA_WORD_2(pa_cc16, uint32_t, uint16_t, 0, 16, 0) · 3 unmodified lines
282 }282 }
283 283
284#define UN_SEQ_3(name, comp_t) \284#define UN_SEQ_3(name, comp_t) \
285 static void name(void *src, void *dst[], int w) { \285 static void name(void *restrict src, void *restrict dst[], int w) { \
286 comp_t *r = src; \286 comp_t *r = src; \
287 for (int x = 0; x < w; x++) { \287 for (int x = 0; x < w; x++) { \
288 ((comp_t *)dst[0])[x] = *r++; \288 ((comp_t *)dst[0])[x] = *r++; \
@@ -302,8 +302,8 @@ struct regular_repacker { · 13 unmodified lines
302 int component_width; // number of bits for a single component302 int component_width; // number of bits for a single component
303 int prepadding; // number of bits of LSB padding303 int prepadding; // number of bits of LSB padding
304 int num_components; // number of components that can be accessed304 int num_components; // number of components that can be accessed
305 void (*pa_scanline)(void *a, void *b[], int w);305 void (*pa_scanline)(void *restrict a, void *restrict b[], int w);
306 void (*un_scanline)(void *a, void *b[], int w);306 void (*un_scanline)(void *restrict a, void *restrict b[], int w);
307};307};
308 308
309static const struct regular_repacker regular_repackers[] = {309static const struct regular_repacker regular_repackers[] = {
@@ -384,7 +384,7 @@ static void setup_packed_packer(struct mp_repack *rp) · 74 unmodified lines
384 384
385 int prepad = components[0] ? 0 : 8;385 int prepad = components[0] ? 0 : 8;
386 int first_comp = components[0] ? 0 : 1;386 int first_comp = components[0] ? 0 : 1;
387 void (*repack_cb)(void *pa, void *pb[], int w) =387 void (*repack_cb)(void *restrict pa, void *restrict pb[], int w) =
388 rp->pack ? pa->pa_scanline : pa->un_scanline;388 rp->pack ? pa->pa_scanline : pa->un_scanline;
389 389
390 if (pa->packed_width != desc.bpp[0] ||390 if (pa->packed_width != desc.bpp[0] ||
@@ -408,8 +408,8 @@ static void setup_packed_packer(struct mp_repack *rp) · 17 unmodified lines
408}408}
409 409
410#define PA_SHIFT_LUT8(name, packed_t) \410#define PA_SHIFT_LUT8(name, packed_t) \
411 static void name(void *dst, void *src[], int w, uint8_t *lut, \411 static void name(void *restrict dst, void *restrict src[], int w, \
412 uint8_t s0, uint8_t s1, uint8_t s2) { \412 uint8_t *restrict lut, uint8_t s0, uint8_t s1, uint8_t s2) { \
413 for (int x = 0; x < w; x++) { \413 for (int x = 0; x < w; x++) { \
414 ((packed_t *)dst)[x] = \414 ((packed_t *)dst)[x] = \
415 (lut[((uint8_t *)src[0])[x] + 256 * 0] << s0) | \415 (lut[((uint8_t *)src[0])[x] + 256 * 0] << s0) | \
@@ -420,8 +420,8 @@ static void setup_packed_packer(struct mp_repack *rp) · 4 unmodified lines
420 420
421 421
422#define UN_SHIFT_LUT8(name, packed_t) \422#define UN_SHIFT_LUT8(name, packed_t) \
423 static void name(void *src, void *dst[], int w, uint8_t *lut, \423 static void name(void *restrict src, void *restrict dst[], int w, \
424 uint8_t s0, uint8_t s1, uint8_t s2) { \424 uint8_t *restrict lut, uint8_t s0, uint8_t s1, uint8_t s2) { \
425 for (int x = 0; x < w; x++) { \425 for (int x = 0; x < w; x++) { \
426 packed_t c = ((packed_t *)src)[x]; \426 packed_t c = ((packed_t *)src)[x]; \
427 ((uint8_t *)dst[0])[x] = lut[((c >> s0) & 0xFF) + 256 * 0]; \427 ((uint8_t *)dst[0])[x] = lut[((c >> s0) & 0xFF) + 256 * 0]; \
@@ -449,7 +449,7 @@ static void fringe_rgb_repack(struct mp_repack *rp, · 21 unmodified lines
449 449
450 assert(rp->comp_size == 1 || rp->comp_size == 2);450 assert(rp->comp_size == 1 || rp->comp_size == 2);
451 451
452 void (*repack)(void *pa, void *pb[], int w, uint8_t *lut,452 void (*repack)(void *restrict pa, void *restrict pb[], int w, uint8_t *restrict lut,
453 uint8_t s0, uint8_t s1, uint8_t s2) = NULL;453 uint8_t s0, uint8_t s1, uint8_t s2) = NULL;
454 if (rp->pack) {454 if (rp->pack) {
455 repack = rp->comp_size == 1 ? pa_shift_lut8_8 : pa_shift_lut8_16;455 repack = rp->comp_size == 1 ? pa_shift_lut8_8 : pa_shift_lut8_16;
@@ -525,10 +525,10 @@ static void unpack_pal(struct mp_repack *rp, · 69 unmodified lines
525 struct mp_image *a, int a_x, int a_y,525 struct mp_image *a, int a_x, int a_y,
526 struct mp_image *b, int b_x, int b_y, int w)526 struct mp_image *b, int b_x, int b_y, int w)
527{527{
528 uint8_t *src = mp_image_pixel_ptr(a, 0, a_x, a_y);528 uint8_t *restrict src = mp_image_pixel_ptr(a, 0, a_x, a_y);
529 uint32_t *pal = (void *)a->planes[1];529 uint32_t *pal = (void *)a->planes[1];
530 530
531 uint8_t *dst[4] = {0};531 uint8_t *restrict dst[4] = {0};
532 for (int p = 0; p < b->num_planes; p++)532 for (int p = 0; p < b->num_planes; p++)
533 dst[p] = mp_image_pixel_ptr(b, p, b_x, b_y);533 dst[p] = mp_image_pixel_ptr(b, p, b_x, b_y);
534 534
@@ -545,8 +545,8 @@ static void bitmap_repack(struct mp_repack *rp, · 10 unmodified lines
545 struct mp_image *a, int a_x, int a_y,545 struct mp_image *a, int a_x, int a_y,
546 struct mp_image *b, int b_x, int b_y, int w)546 struct mp_image *b, int b_x, int b_y, int w)
547{547{
548 uint8_t *pa = mp_image_pixel_ptr(a, 0, a_x, a_y);548 uint8_t *restrict pa = mp_image_pixel_ptr(a, 0, a_x, a_y);
549 uint8_t *pb = mp_image_pixel_ptr(b, 0, b_x, b_y);549 uint8_t *restrict pb = mp_image_pixel_ptr(b, 0, b_x, b_y);
550 550
551 if (rp->pack) {551 if (rp->pack) {
552 for (unsigned x = 0; x < w; x += 8) {552 for (unsigned x = 0; x < w; x += 8) {
@@ -596,7 +596,7 @@ static void setup_misc_packer(struct mp_repack *rp) · 43 unmodified lines
596}596}
597 597
598#define PA_P422(name, comp_t) \598#define PA_P422(name, comp_t) \
599 static void name(void *dst, void *src[], int w, uint8_t *c) { \599 static void name(void *restrict dst, void *restrict src[], int w, uint8_t *restrict c) { \
600 for (int x = 0; x < w; x += 2) { \600 for (int x = 0; x < w; x += 2) { \
601 ((comp_t *)dst)[x * 2 + c[0]] = ((comp_t *)src[0])[x + 0]; \601 ((comp_t *)dst)[x * 2 + c[0]] = ((comp_t *)src[0])[x + 0]; \
602 ((comp_t *)dst)[x * 2 + c[1]] = ((comp_t *)src[0])[x + 1]; \602 ((comp_t *)dst)[x * 2 + c[1]] = ((comp_t *)src[0])[x + 1]; \
@@ -607,7 +607,7 @@ static void setup_misc_packer(struct mp_repack *rp) · 4 unmodified lines
607 607
608 608
609#define UN_P422(name, comp_t) \609#define UN_P422(name, comp_t) \
610 static void name(void *src, void *dst[], int w, uint8_t *c) { \610 static void name(void *restrict src, void *restrict dst[], int w, uint8_t *restrict c) { \
611 for (int x = 0; x < w; x += 2) { \611 for (int x = 0; x < w; x += 2) { \
612 ((comp_t *)dst[0])[x + 0] = ((comp_t *)src)[x * 2 + c[0]]; \612 ((comp_t *)dst[0])[x + 0] = ((comp_t *)src)[x * 2 + c[0]]; \
613 ((comp_t *)dst[0])[x + 1] = ((comp_t *)src)[x * 2 + c[1]]; \613 ((comp_t *)dst[0])[x + 1] = ((comp_t *)src)[x * 2 + c[1]]; \
@@ -621,7 +621,7 @@ PA_P422(pa_p422_16, uint16_t) · 7 unmodified lines
621UN_P422(un_p422_8, uint8_t)621UN_P422(un_p422_8, uint8_t)
622UN_P422(un_p422_16, uint16_t)622UN_P422(un_p422_16, uint16_t)
623 623
624static void pa_p411_8(void *dst, void *src[], int w, uint8_t *c)624static void pa_p411_8(void *restrict dst, void *restrict src[], int w, uint8_t *restrict c)
625{625{
626 for (int x = 0; x < w; x += 4) {626 for (int x = 0; x < w; x += 4) {
627 ((uint8_t *)dst)[x / 4 * 6 + c[0]] = ((uint8_t *)src[0])[x + 0];627 ((uint8_t *)dst)[x / 4 * 6 + c[0]] = ((uint8_t *)src[0])[x + 0];
@@ -634,7 +634,7 @@ static void pa_p411_8(void *dst, void *src[], int w, uint8_t *c) · 6 unmodified lines
634}634}
635 635
636 636
637static void un_p411_8(void *src, void *dst[], int w, uint8_t *c)637static void un_p411_8(void *restrict src, void *restrict dst[], int w, uint8_t *restrict c)
638{638{
639 for (int x = 0; x < w; x += 4) {639 for (int x = 0; x < w; x += 4) {
640 ((uint8_t *)dst[0])[x + 0] = ((uint8_t *)src)[x / 4 * 6 + c[0]];640 ((uint8_t *)dst[0])[x + 0] = ((uint8_t *)src)[x / 4 * 6 + c[0]];
@@ -773,7 +773,7 @@ static void setup_nv_packer(struct mp_repack *rp) · 132 unmodified lines
773 for (int i = 0; i < MP_ARRAY_SIZE(regular_repackers); i++) {773 for (int i = 0; i < MP_ARRAY_SIZE(regular_repackers); i++) {
774 const struct regular_repacker *pa = &regular_repackers[i];774 const struct regular_repacker *pa = &regular_repackers[i];
775 775
776 void (*repack_cb)(void *pa, void *pb[], int w) =776 void (*repack_cb)(void *restrict pa, void *restrict pb[], int w) =
777 rp->pack ? pa->pa_scanline : pa->un_scanline;777 rp->pack ? pa->pa_scanline : pa->un_scanline;
778 778
779 if (pa->packed_width != desc.component_size * 2 * 8 ||779 if (pa->packed_width != desc.component_size * 2 * 8 ||
@@ -794,8 +794,8 @@ static void setup_nv_packer(struct mp_repack *rp) · 14 unmodified lines
794}794}
795 795
796#define PA_F32(name, packed_t) \796#define PA_F32(name, packed_t) \
797 static void name(void *dst, float *src, int w, float m, float o, \797 static void name(void *restrict dst, float *restrict src, int w, float m, \
798 uint32_t p_max) { \798 float o, uint32_t p_max) { \
799 for (int x = 0; x < w; x++) { \799 for (int x = 0; x < w; x++) { \
800 ((packed_t *)dst)[x] = \800 ((packed_t *)dst)[x] = \
801 MPCLAMP(lrint((src[x] + o) * m), 0, (packed_t)p_max); \801 MPCLAMP(lrint((src[x] + o) * m), 0, (packed_t)p_max); \
@@ -803,8 +803,8 @@ static void setup_nv_packer(struct mp_repack *rp) · 1 unmodified line
803 }803 }
804 804
805#define UN_F32(name, packed_t) \805#define UN_F32(name, packed_t) \
806 static void name(void *src, float *dst, int w, float m, float o, \806 static void name(void *restrict src, float *restrict dst, int w, float m, \
807 uint32_t unused) { \807 float o, uint32_t unused) { \
808 for (int x = 0; x < w; x++) \808 for (int x = 0; x < w; x++) \
809 dst[x] = ((packed_t *)src)[x] * m + o; \809 dst[x] = ((packed_t *)src)[x] * m + o; \
810 }810 }
@@ -821,7 +821,7 @@ static void repack_float(struct mp_repack *rp, · 10 unmodified lines
821{821{
822 assert(rp->f32_comp_size == 1 || rp->f32_comp_size == 2);822 assert(rp->f32_comp_size == 1 || rp->f32_comp_size == 2);
823 823
824 void (*packer)(void *a, float *b, int w, float fm, float fb, uint32_t max)824 void (*packer)(void *restrict a, float *restrict b, int w, float fm, float fb, uint32_t max)
825 = rp->pack ? (rp->f32_comp_size == 1 ? pa_f32_8 : pa_f32_16)825 = rp->pack ? (rp->f32_comp_size == 1 ? pa_f32_8 : pa_f32_16)
826 : (rp->f32_comp_size == 1 ? un_f32_8 : un_f32_16);826 : (rp->f32_comp_size == 1 ? un_f32_8 : un_f32_16);
827 827

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t