path
stringlengths
14
112
content
stringlengths
0
6.32M
size
int64
0
6.32M
max_lines
int64
1
100k
repo_name
stringclasses
2 values
autogenerated
bool
1 class
cosmopolitan/dsp/tty/setrawmode.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/assert.h" #include "libc/calls/termios.h" #include "libc/sysv/consts/termios.h" /** * Enables blocking raw mode for teletypewriter w/ recommended settings. * @see ttyconfig(), ttyrestore() */ int ttysetrawmode(struct termios *conf, int64_t flags) { conf->c_cc[VMIN] = 1; conf->c_cc[VTIME] = 1; return ttysetraw(conf, flags); }
2,213
33
jart/cosmopolitan
false
cosmopolitan/dsp/tty/xtermname.h
#ifndef COSMOPOLITAN_DSP_TTY_XTERMNAME_H_ #define COSMOPOLITAN_DSP_TTY_XTERMNAME_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ extern const char kXtermName[]; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_TTY_XTERMNAME_H_ */
292
11
jart/cosmopolitan
false
cosmopolitan/dsp/tty/ident.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/calls/calls.h" #include "libc/calls/termios.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/fmt/fmt.h" #include "libc/intrin/safemacros.internal.h" #include "libc/intrin/weaken.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" #include "libc/sock/struct/pollfd.h" #include "libc/str/str.h" #include "libc/sysv/consts/poll.h" #include "libc/sysv/errfuns.h" /* TODO(jart): DELETE */ static int ttyident_probe(struct TtyIdent *ti, int ttyinfd, int ttyoutfd, const char *msg) { ssize_t rc; size_t got; char buf[64]; int id, version; if ((rc = write(ttyoutfd, msg, strlen(msg))) != -1) { TryAgain: if ((rc = read(ttyinfd, buf, sizeof(buf))) != -1) { buf[min((got = (size_t)rc), sizeof(buf) - 1)] = '\0'; if (sscanf(buf, "\e[>%d;%d", &id, &version) >= 1) { ti->id = id; ti->version = version; rc = 0; } else { rc = eio(); } } else if (errno == EINTR) { goto TryAgain; } else if (errno == EAGAIN) { if (poll((struct pollfd[]){{ttyinfd, POLLIN}}, 1, 100) != 0) { goto TryAgain; } else { rc = etimedout(); } } } return rc; } /** * Identifies teletypewriter. * * For example, we can tell if process is running in a GNU Screen * session Gnome Terminal. * * @return object if TTY responds, or NULL w/ errno * @see ttyidentclear() */ int ttyident(struct TtyIdent *ti, int ttyinfd, int ttyoutfd) { int rc; struct termios old; struct TtyIdent outer; rc = -1; if (!IsWindows()) { if (ttyconfig(ttyinfd, ttysetrawdeadline, 3, &old) != -1) { if (ttyident_probe(ti, ttyinfd, ttyoutfd, "\e[>c") != -1) { rc = 0; memset(&outer, 0, sizeof(outer)); if (ti->id == 83 /* GNU Screen */ && (ti->next || _weaken(malloc)) && ttyident_probe(&outer, ttyinfd, ttyoutfd, "\eP\e[>c\e\\") != -1 && (ti->next = (ti->next ? ti->next : _weaken(malloc)(sizeof(struct TtyIdent))))) { memcpy(ti->next, &outer, sizeof(outer)); } else { free(ti->next); ti->next = 0; } } ttyrestore(ttyinfd, &old); } } return rc; }
4,148
102
jart/cosmopolitan
false
cosmopolitan/dsp/tty/setbgfg16.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/itoa8.h" #include "dsp/tty/quant.h" #include "libc/limits.h" static char *ansitoa(char *p, unsigned xt, unsigned base) { if (xt >= 8) xt -= 8, base += 60; return itoa8(p, xt + base); } static char *setansibgfg(char *p, unsigned bg, unsigned fg) { *p++ = '\e'; *p++ = '['; if (bg != -1u) p = ansitoa(p, bg, 40); if (bg != -1u && fg != -1u) *p++ = ';'; if (fg != -1u) p = ansitoa(p, fg, 30); *p++ = 'm'; return p; } char *setbg16_(char *p, struct TtyRgb bg) { return setansibgfg(p, bg.xt, -1u); } char *setfg16_(char *p, struct TtyRgb fg) { return setansibgfg(p, -1u, fg.xt); } char *setbgfg16_(char *p, struct TtyRgb bg, struct TtyRgb fg) { return setansibgfg(p, bg.xt, fg.xt); }
2,556
43
jart/cosmopolitan
false
cosmopolitan/dsp/tty/kxtermcubesteps.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/quant.h" const uint8_t kXtermCubeSteps[] = {0, 0137, 0207, 0257, 0327, 0377};
1,933
22
jart/cosmopolitan
false
cosmopolitan/dsp/tty/windex-sse4.S
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ │vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" // Returns index of minimum positive int16 in array. // // @param rdi points to nonempty array // @param esi is 16-byte aligned 8+ / 8 multiple array item count // @note needs sse4 (nehalem+) windex_sse4: push %rbp mov %rsp,%rbp .profilable mov $8,%eax sub $32,%rsp movdqa (%rdi),%xmm2 movdqa .Lidx(%rip),%xmm1 movdqa .Linc(%rip),%xmm6 movdqa %xmm1,%xmm3 0: cmp %eax,%esi je 1f add $8,%eax movdqa -16(%rdi,%rax,2),%xmm4 movdqa %xmm2,%xmm7 movdqa %xmm3,%xmm5 paddw %xmm6,%xmm5 movdqa %xmm5,%xmm3 pcmpgtw %xmm4,%xmm7 pminsw %xmm4,%xmm2 movdqa %xmm7,%xmm0 pblendvb %xmm0,%xmm5,%xmm1 jmp 0b 1: phminposuw %xmm2,%xmm0 movd %xmm0,%eax movdqa %xmm1,-32(%rbp) shr $16,%eax movzwl -32(%rbp,%rax,2),%eax leave ret .endfn windex_sse4,globl .rodata.cst16 .Lidx: .short 0,1,2,3,4,5,6,7 .Linc: .value 8,8,8,8,8,8,8,8
2,704
61
jart/cosmopolitan
false
cosmopolitan/dsp/tty/ttyraster.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/twixt8.h" #include "dsp/tty/quant.h" #include "dsp/tty/tty.h" #include "dsp/tty/ttyrgb.h" #include "dsp/tty/windex.h" #include "libc/assert.h" #include "libc/intrin/bits.h" #include "libc/intrin/safemacros.internal.h" #include "libc/limits.h" #include "libc/log/check.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/nexgen32e/x86feature.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #define SQR(X) ((X) * (X)) #define DIST(X, Y) ((X) - (Y)) #define QSUB(A, B, C) ABS(DIST(A##C, B##C)) #define DIFF(A, B) (QSUB(A, B, r) + QSUB(A, B, g) + QSUB(A, B, b)) #define JUDGE(M) (DIFF(ttl, M) + DIFF(ttr, M) + DIFF(tbl, M) + DIFF(tbr, M)) static const struct Glyph { char c1, c2, c3, len; } kGlyphs[2][11] = { {{0x20, 0x00, 0x00, 1}, /* */ {0xE2, 0x96, 0x84, 3}, /* ▄ */ {0xE2, 0x96, 0x8C, 3}, /* ▌ */ {0xE2, 0x96, 0x9D, 3}, /* ▝ */ {0xE2, 0x96, 0x97, 3}, /* ▗ */ {0xE2, 0x96, 0x96, 3}, /* ▖ */ {0xE2, 0x96, 0x9E, 3}, /* ▞ */ {0xE2, 0x96, 0x98, 3}, /* ▘ */ {0xE2, 0x96, 0x91, 3}, /* ░ */ {0xE2, 0x96, 0x93, 3}, /* ▓ */ {0xE2, 0x96, 0x92, 3}}, /* ▒ */ {{0xE2, 0x96, 0x88, 3}, /* █ */ {0xE2, 0x96, 0x80, 3}, /* ▀ */ {0xE2, 0x96, 0x90, 3}, /* ▐ */ {0xE2, 0x96, 0x99, 3}, /* ▙ */ {0xE2, 0x96, 0x9B, 3}, /* ▛ */ {0xE2, 0x96, 0x9C, 3}, /* ▜ */ {0xE2, 0x96, 0x9A, 3}, /* ▚ */ {0xE2, 0x96, 0x9F, 3}, /* ▟ */ {0xE2, 0x96, 0x93, 3}, /* ▓ */ {0xE2, 0x96, 0x91, 3}, /* ░ */ {0xE2, 0x96, 0x92, 3}}, /* ▒ */ }; static const struct Pick { unsigned char fg, bg, k; } kPicksUnicode[96] = { {-1, BL, 0}, /* */ {BR, BL, 4}, /* ▗ */ {TL, BL, 4}, /* ▗ */ {TR, BL, 4}, /* ▗ */ {BR, BL, 5}, /* ▖ */ {BR, BL, 1}, /* ▄ */ {TL, BL, 5}, /* ▖ */ {TL, BL, 1}, /* ▄ */ {TR, BL, 5}, /* ▖ */ {TR, BL, 1}, /* ▄ */ {BR, BL, 3}, /* ▝ */ {BL, BR, 2}, /* ▌ */ {BR, BL, 6}, /* ▞ */ {BL, BR, 7}, /* ▘ */ {TL, BL, 3}, /* ▝ */ {BL, TL, 2}, /* ▌ */ {TL, BL, 6}, /* ▞ */ {BL, TL, 7}, /* ▘ */ {TR, BL, 3}, /* ▝ */ {BL, TR, 2}, /* ▌ */ {TR, BL, 6}, /* ▞ */ {BL, TR, 7}, /* ▘ */ {BR, BL, 7}, /* ▘ */ {BL, BR, 6}, /* ▞ */ {BR, BL, 2}, /* ▌ */ {BL, BR, 3}, /* ▝ */ {BL, BR, 1}, /* ▄ */ {BL, BR, 5}, /* ▖ */ {BL, BR, 4}, /* ▗ */ {-1, BR, 0}, /* */ {TL, BR, 4}, /* ▗ */ {TR, BR, 4}, /* ▗ */ {TL, BR, 5}, /* ▖ */ {TL, BR, 1}, /* ▄ */ {TR, BR, 5}, /* ▖ */ {TR, BR, 1}, /* ▄ */ {TL, BR, 3}, /* ▝ */ {BR, TL, 2}, /* ▌ */ {TL, BR, 6}, /* ▞ */ {BR, TL, 7}, /* ▘ */ {TR, BR, 3}, /* ▝ */ {BR, TR, 2}, /* ▌ */ {TR, BR, 6}, /* ▞ */ {BR, TR, 7}, /* ▘ */ {TL, BL, 7}, /* ▘ */ {BL, TL, 6}, /* ▞ */ {TL, BL, 2}, /* ▌ */ {BL, TL, 3}, /* ▝ */ {TL, BR, 7}, /* ▘ */ {BR, TL, 6}, /* ▞ */ {TL, BR, 2}, /* ▌ */ {BR, TL, 3}, /* ▝ */ {BL, TL, 1}, /* ▄ */ {BL, TL, 5}, /* ▖ */ {BR, TL, 1}, /* ▄ */ {BR, TL, 5}, /* ▖ */ {BL, TL, 4}, /* ▗ */ {BR, TL, 4}, /* ▗ */ {-1, TL, 0}, /* */ {TR, TL, 4}, /* ▗ */ {TR, TL, 5}, /* ▖ */ {TR, TL, 1}, /* ▄ */ {TR, TL, 3}, /* ▝ */ {TL, TR, 2}, /* ▌ */ {TR, TL, 6}, /* ▞ */ {TL, TR, 7}, /* ▘ */ {TR, BL, 7}, /* ▘ */ {BL, TR, 6}, /* ▞ */ {TR, BL, 2}, /* ▌ */ {BL, TR, 3}, /* ▝ */ {TR, BR, 7}, /* ▘ */ {BR, TR, 6}, /* ▞ */ {TR, BR, 2}, /* ▌ */ {BR, TR, 3}, /* ▝ */ {TR, TL, 7}, /* ▘ */ {TL, TR, 6}, /* ▞ */ {TR, TL, 2}, /* ▌ */ {TL, TR, 3}, /* ▝ */ {BL, TR, 1}, /* ▄ */ {BL, TR, 5}, /* ▖ */ {BR, TR, 1}, /* ▄ */ {BR, TR, 5}, /* ▖ */ {TL, TR, 1}, /* ▄ */ {TL, TR, 5}, /* ▖ */ {BL, TR, 4}, /* ▗ */ {BR, TR, 4}, /* ▗ */ {TL, TR, 4}, /* ▗ */ {-1, TR, 0}, /* */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ }; static const struct Pick kPicksCp437[32] = { {-1, BL, 0}, /* */ {BR, BL, 1}, /* ▄ */ {TL, BL, 1}, /* ▄ */ {TR, BL, 1}, /* ▄ */ {BL, BR, 1}, /* ▄ */ {BL, TL, 1}, /* ▄ */ {BL, TR, 1}, /* ▄ */ {BL, BR, 2}, /* ▌ */ {BL, TL, 2}, /* ▌ */ {BL, TR, 2}, /* ▌ */ {BR, BL, 2}, /* ▌ */ {-1, BR, 0}, /* */ {TL, BR, 1}, /* ▄ */ {TR, BR, 1}, /* ▄ */ {BR, TL, 2}, /* ▌ */ {BR, TR, 2}, /* ▌ */ {TL, BL, 2}, /* ▌ */ {TL, BR, 2}, /* ▌ */ {BR, TL, 1}, /* ▄ */ {-1, TL, 0}, /* */ {TR, TL, 1}, /* ▄ */ {TL, TR, 2}, /* ▌ */ {TR, BL, 2}, /* ▌ */ {TR, BR, 2}, /* ▌ */ {TR, TL, 2}, /* ▌ */ {BR, TR, 1}, /* ▄ */ {TL, TR, 1}, /* ▄ */ {-1, TR, 0}, /* */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ {-1, -1, -1}, /* X */ }; static const struct Pick kPicksMixBlock[32] = { {BR, BL, 8}, /* ░ */ {BR, BL, 9}, /* ▓ */ {TL, BL, 8}, /* ░ */ {TL, BL, 9}, /* ▓ */ {TR, BL, 8}, /* ░ */ {TR, BL, 9}, /* ▓ */ {TL, BR, 8}, /* ░ */ {TL, BR, 9}, /* ▓ */ {TR, BR, 8}, /* ░ */ {TR, BR, 9}, /* ▓ */ {BL, TL, 8}, /* ░ */ {BL, TL, 9}, /* ▓ */ {BL, TL, 8}, /* ░ */ {BL, TL, 9}, /* ▓ */ {TR, TL, 8}, /* ░ */ {TR, TL, 9}, /* ▓ */ {BL, TR, 8}, /* ░ */ {BL, TR, 9}, /* ▓ */ {BR, TR, 8}, /* ░ */ {BR, TR, 9}, /* ▓ */ {TL, TR, 8}, /* ░ */ {TL, TR, 9}, /* ▓ */ /**/ {BL, TL, 8}, /* ░ */ {BL, TL, 9}, /* ▓ */ {TR, TL, 8}, /* ░ */ {TR, TL, 9}, /* ▓ */ {BL, TR, 8}, /* ░ */ {BL, TR, 9}, /* ▓ */ {BR, TR, 8}, /* ░ */ {BR, TR, 9}, /* ▓ */ {TL, TR, 8}, /* ░ */ {TL, TR, 9}, /* ▓ */ }; static unsigned short GetBlockDist(struct TtyRgb a, struct TtyRgb b, struct TtyRgb c, struct TtyRgb d, struct TtyRgb w, struct TtyRgb x, struct TtyRgb y, struct TtyRgb z) { unsigned short dist; dist = 0; dist += ABS(a.r - w.r); dist += ABS(a.g - w.g); dist += ABS(a.b - w.b); dist += ABS(b.r - x.r); dist += ABS(b.g - x.g); dist += ABS(b.b - x.b); dist += ABS(c.r - y.r); dist += ABS(c.g - y.g); dist += ABS(c.b - y.b); dist += ABS(d.r - z.r); dist += ABS(d.g - z.g); dist += ABS(d.b - z.b); return dist; } static uint16_t *MixBlock(uint16_t *p, struct TtyRgb ttl, struct TtyRgb ttr, struct TtyRgb tbl, struct TtyRgb tbr, struct TtyRgb qtl, struct TtyRgb qtr, struct TtyRgb qbl, struct TtyRgb qbr) { unsigned char ttlr, ttlg, ttlb, qtlr, qtlg, qtlb, ttrr, ttrg, ttrb, qtrr, qtrg, qtrb, tblr, tblg, tblb, qblr, qblg, qblb, tbrr, tbrg, tbrb, qbrr, qbrg, qbrb, l00r, l00g, l00b, l01r, l01g, l01b, l02r, l02g, l02b, l03r, l03g, l03b, l04r, l04g, l04b, l05r, l05g, l05b, l06r, l06g, l06b, l07r, l07g, l07b, l08r, l08g, l08b, l09r, l09g, l09b, l10r, l10g, l10b, l11r, l11g, l11b, l12r, l12g, l12b, l13r, l13g, l13b, l14r, l14g, l14b, l15r, l15g, l15b, l16r, l16g, l16b, l17r, l17g, l17b, l18r, l18g, l18b, l19r, l19g, l19b, l20r, l20g, l20b, l21r, l21g, l21b, l22r, l22g, l22b, l23r, l23g, l23b, l24r, l24g, l24b, l25r, l25g, l25b, l26r, l26g, l26b, l27r, l27g, l27b, l28r, l28g, l28b, l29r, l29g, l29b, l30r, l30g, l30b, l31r, l31g, l31b; unsigned short p00, p01, p02, p03, p04, p05, p06, p07, p08, p09, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31; ttlr = ttl.r; ttlg = ttl.g; ttlb = ttl.b; qtlr = qtl.r; qtlg = qtl.g; qtlb = qtl.b; ttrr = ttr.r; ttrg = ttr.g; ttrb = ttr.b; qtrr = qtr.r; qtrg = qtr.g; qtrb = qtr.b; tblr = tbl.r; tblg = tbl.g; tblb = tbl.b; qblr = qbl.r; qblg = qbl.g; qblb = qbl.b; tbrr = tbr.r; tbrg = tbr.g; tbrb = tbr.b; qbrr = qbr.r; qbrg = qbr.g; qbrb = qbr.b; l00r = twixt8(qblr, qbrr, 0100); l00g = twixt8(qblg, qbrg, 0100); l00b = twixt8(qblb, qbrb, 0100); l01r = twixt8(qblr, qbrr, 0300); l01g = twixt8(qblg, qbrg, 0300); l01b = twixt8(qblb, qbrb, 0300); l02r = twixt8(qblr, qbrr, 0200); l02g = twixt8(qblg, qbrg, 0200); l02b = twixt8(qblb, qbrb, 0200); l03r = twixt8(qblr, qtlr, 0100); l03g = twixt8(qblg, qtlg, 0100); l03b = twixt8(qblb, qtlb, 0100); l04r = twixt8(qblr, qtlr, 0300); l04g = twixt8(qblg, qtlg, 0300); l04b = twixt8(qblb, qtlb, 0300); l05r = twixt8(qblr, qtlr, 0200); l05g = twixt8(qblg, qtlg, 0200); l05b = twixt8(qblb, qtlb, 0200); l06r = twixt8(qblr, qtrr, 0100); l06g = twixt8(qblg, qtrg, 0100); l06b = twixt8(qblb, qtrb, 0100); l07r = twixt8(qblr, qtrr, 0300); l07g = twixt8(qblg, qtrg, 0300); l07b = twixt8(qblb, qtrb, 0300); l08r = twixt8(qblr, qtrr, 0200); l08g = twixt8(qblg, qtrg, 0200); l08b = twixt8(qblb, qtrb, 0200); l09r = twixt8(qbrr, qtlr, 0100); l09g = twixt8(qbrg, qtlg, 0100); l09b = twixt8(qbrb, qtlb, 0100); l10r = twixt8(qbrr, qtlr, 0300); l10g = twixt8(qbrg, qtlg, 0300); l10b = twixt8(qbrb, qtlb, 0300); l11r = twixt8(qbrr, qtlr, 0200); l11g = twixt8(qbrg, qtlg, 0200); l11b = twixt8(qbrb, qtlb, 0200); l12r = twixt8(qbrr, qtrr, 0100); l12g = twixt8(qbrg, qtrg, 0100); l12b = twixt8(qbrb, qtrb, 0100); l13r = twixt8(qbrr, qtrr, 0300); l13g = twixt8(qbrg, qtrg, 0300); l13b = twixt8(qbrb, qtrb, 0300); l14r = twixt8(qbrr, qtrr, 0200); l14g = twixt8(qbrg, qtrg, 0200); l14b = twixt8(qbrb, qtrb, 0200); l15r = twixt8(qtlr, qblr, 0100); l15g = twixt8(qtlg, qblg, 0100); l15b = twixt8(qtlb, qblb, 0100); l16r = twixt8(qtlr, qblr, 0300); l16g = twixt8(qtlg, qblg, 0300); l16b = twixt8(qtlb, qblb, 0300); l17r = twixt8(qtlr, qblr, 0200); l17g = twixt8(qtlg, qblg, 0200); l17b = twixt8(qtlb, qblb, 0200); l18r = twixt8(qtlr, qbrr, 0100); l18g = twixt8(qtlg, qbrg, 0100); l18b = twixt8(qtlb, qbrb, 0100); l19r = twixt8(qtlr, qbrr, 0300); l19g = twixt8(qtlg, qbrg, 0300); l19b = twixt8(qtlb, qbrb, 0300); l20r = twixt8(qtlr, qbrr, 0200); l20g = twixt8(qtlg, qbrg, 0200); l20b = twixt8(qtlb, qbrb, 0200); l21r = twixt8(qtlr, qtrr, 0100); l21g = twixt8(qtlg, qtrg, 0100); l21b = twixt8(qtlb, qtrb, 0100); l22r = twixt8(qtlr, qtrr, 0300); l22g = twixt8(qtlg, qtrg, 0300); l22b = twixt8(qtlb, qtrb, 0300); l23r = twixt8(qtlr, qtrr, 0200); l23g = twixt8(qtlg, qtrg, 0200); l23b = twixt8(qtlb, qtrb, 0200); l24r = twixt8(qtrr, qblr, 0100); l24g = twixt8(qtrg, qblg, 0100); l24b = twixt8(qtrb, qblb, 0100); l25r = twixt8(qtrr, qblr, 0300); l25g = twixt8(qtrg, qblg, 0300); l25b = twixt8(qtrb, qblb, 0300); l26r = twixt8(qtrr, qblr, 0200); l26g = twixt8(qtrg, qblg, 0200); l26b = twixt8(qtrb, qblb, 0200); l27r = twixt8(qtrr, qbrr, 0100); l27g = twixt8(qtrg, qbrg, 0100); l27b = twixt8(qtrb, qbrb, 0100); l28r = twixt8(qtrr, qbrr, 0300); l28g = twixt8(qtrg, qbrg, 0300); l28b = twixt8(qtrb, qbrb, 0300); l29r = twixt8(qtrr, qbrr, 0200); l29g = twixt8(qtrg, qbrg, 0200); l29b = twixt8(qtrb, qbrb, 0200); l30r = twixt8(qtrr, qtlr, 0100); l30g = twixt8(qtrg, qtlg, 0100); l30b = twixt8(qtrb, qtlb, 0100); l31r = twixt8(qtrr, qtlr, 0300); l31g = twixt8(qtrg, qtlg, 0300); l31b = twixt8(qtrb, qtlb, 0300); p00 = JUDGE(l00); p01 = JUDGE(l01); p02 = JUDGE(l02); p03 = JUDGE(l03); p04 = JUDGE(l04); p05 = JUDGE(l05); p06 = JUDGE(l06); p07 = JUDGE(l07); p08 = JUDGE(l08); p09 = JUDGE(l09); p10 = JUDGE(l10); p11 = JUDGE(l11); p12 = JUDGE(l12); p13 = JUDGE(l13); p14 = JUDGE(l14); p15 = JUDGE(l15); p16 = JUDGE(l16); p17 = JUDGE(l17); p18 = JUDGE(l18); p19 = JUDGE(l19); p20 = JUDGE(l20); p21 = JUDGE(l21); p22 = JUDGE(l22); p23 = JUDGE(l23); p24 = JUDGE(l24); p25 = JUDGE(l25); p26 = JUDGE(l26); p27 = JUDGE(l27); p28 = JUDGE(l28); p29 = JUDGE(l29); p30 = JUDGE(l30); p31 = JUDGE(l31); *p++ = p00; *p++ = p01; *p++ = p02; *p++ = p03; *p++ = p04; *p++ = p05; *p++ = p06; *p++ = p07; *p++ = p08; *p++ = p09; *p++ = p10; *p++ = p11; *p++ = p12; *p++ = p13; *p++ = p14; *p++ = p15; *p++ = p16; *p++ = p17; *p++ = p18; *p++ = p19; *p++ = p20; *p++ = p21; *p++ = p22; *p++ = p23; *p++ = p24; *p++ = p25; *p++ = p26; *p++ = p27; *p++ = p28; *p++ = p29; *p++ = p30; *p++ = p31; return p; } static struct TtyRgb GetQuant(struct TtyRgb rgb) { return g_ansi2rgb_[rgb.xt]; } static uint16_t *PickUnicode(uint16_t *p, struct TtyRgb tl, struct TtyRgb tr, struct TtyRgb bl, struct TtyRgb br, struct TtyRgb tl2, struct TtyRgb tr2, struct TtyRgb bl2, struct TtyRgb br2) { #define PICK(A, B, C, D) *p++ = GetBlockDist(tl, tr, bl, br, A, B, C, D) PICK(bl2, bl2, bl2, bl2); /* k=0 bg=bl fg=NULL */ PICK(bl2, bl2, bl2, br2); /* ▗ k=4 bg=bl fg=br */ PICK(bl2, bl2, bl2, tl2); /* ▗ k=4 bg=bl fg=tl */ PICK(bl2, bl2, bl2, tr2); /* ▗ k=4 bg=bl fg=tr */ PICK(bl2, bl2, br2, bl2); /* ▖ k=5 bg=bl fg=br */ PICK(bl2, bl2, br2, br2); /* ▄ k=1 bg=bl fg=br */ PICK(bl2, bl2, tl2, bl2); /* ▖ k=5 bg=bl fg=tl */ PICK(bl2, bl2, tl2, tl2); /* ▄ k=1 bg=bl fg=tl */ PICK(bl2, bl2, tr2, bl2); /* ▖ k=5 bg=bl fg=tr */ PICK(bl2, bl2, tr2, tr2); /* ▄ k=1 bg=bl fg=tr */ PICK(bl2, br2, bl2, bl2); /* ▝ k=3 bg=bl fg=br */ PICK(bl2, br2, bl2, br2); /* ▌ k=2 bg=br fg=bl */ PICK(bl2, br2, br2, bl2); /* ▞ k=6 bg=bl fg=br */ PICK(bl2, br2, br2, br2); /* ▘ k=7 bg=br fg=bl */ PICK(bl2, tl2, bl2, bl2); /* ▝ k=3 bg=bl fg=tl */ PICK(bl2, tl2, bl2, tl2); /* ▌ k=2 bg=tl fg=bl */ PICK(bl2, tl2, tl2, bl2); /* ▞ k=6 bg=bl fg=tl */ PICK(bl2, tl2, tl2, tl2); /* ▘ k=7 bg=tl fg=bl */ PICK(bl2, tr2, bl2, bl2); /* ▝ k=3 bg=bl fg=tr */ PICK(bl2, tr2, bl2, tr2); /* ▌ k=2 bg=tr fg=bl */ PICK(bl2, tr2, tr2, bl2); /* ▞ k=6 bg=bl fg=tr */ PICK(bl2, tr2, tr2, tr2); /* ▘ k=7 bg=tr fg=bl */ PICK(br2, bl2, bl2, bl2); /* ▘ k=7 bg=bl fg=br */ PICK(br2, bl2, bl2, br2); /* ▞ k=6 bg=br fg=bl */ PICK(br2, bl2, br2, bl2); /* ▌ k=2 bg=bl fg=br */ PICK(br2, bl2, br2, br2); /* ▝ k=3 bg=br fg=bl */ PICK(br2, br2, bl2, bl2); /* ▄ k=1 bg=br fg=bl */ PICK(br2, br2, bl2, br2); /* ▖ k=5 bg=br fg=bl */ PICK(br2, br2, br2, bl2); /* ▗ k=4 bg=br fg=bl */ PICK(br2, br2, br2, br2); /* k=0 bg=br fg=NULL */ PICK(br2, br2, br2, tl2); /* ▗ k=4 bg=br fg=tl */ PICK(br2, br2, br2, tr2); /* ▗ k=4 bg=br fg=tr */ PICK(br2, br2, tl2, br2); /* ▖ k=5 bg=br fg=tl */ PICK(br2, br2, tl2, tl2); /* ▄ k=1 bg=br fg=tl */ PICK(br2, br2, tr2, br2); /* ▖ k=5 bg=br fg=tr */ PICK(br2, br2, tr2, tr2); /* ▄ k=1 bg=br fg=tr */ PICK(br2, tl2, br2, br2); /* ▝ k=3 bg=br fg=tl */ PICK(br2, tl2, br2, tl2); /* ▌ k=2 bg=tl fg=br */ PICK(br2, tl2, tl2, br2); /* ▞ k=6 bg=br fg=tl */ PICK(br2, tl2, tl2, tl2); /* ▘ k=7 bg=tl fg=br */ PICK(br2, tr2, br2, br2); /* ▝ k=3 bg=br fg=tr */ PICK(br2, tr2, br2, tr2); /* ▌ k=2 bg=tr fg=br */ PICK(br2, tr2, tr2, br2); /* ▞ k=6 bg=br fg=tr */ PICK(br2, tr2, tr2, tr2); /* ▘ k=7 bg=tr fg=br */ PICK(tl2, bl2, bl2, bl2); /* ▘ k=7 bg=bl fg=tl */ PICK(tl2, bl2, bl2, tl2); /* ▞ k=6 bg=tl fg=bl */ PICK(tl2, bl2, tl2, bl2); /* ▌ k=2 bg=bl fg=tl */ PICK(tl2, bl2, tl2, tl2); /* ▝ k=3 bg=tl fg=bl */ PICK(tl2, br2, br2, br2); /* ▘ k=7 bg=br fg=tl */ PICK(tl2, br2, br2, tl2); /* ▞ k=6 bg=tl fg=br */ PICK(tl2, br2, tl2, br2); /* ▌ k=2 bg=br fg=tl */ PICK(tl2, br2, tl2, tl2); /* ▝ k=3 bg=tl fg=br */ PICK(tl2, tl2, bl2, bl2); /* ▄ k=1 bg=tl fg=bl */ PICK(tl2, tl2, bl2, tl2); /* ▖ k=5 bg=tl fg=bl */ PICK(tl2, tl2, br2, br2); /* ▄ k=1 bg=tl fg=br */ PICK(tl2, tl2, br2, tl2); /* ▖ k=5 bg=tl fg=br */ PICK(tl2, tl2, tl2, bl2); /* ▗ k=4 bg=tl fg=bl */ PICK(tl2, tl2, tl2, br2); /* ▗ k=4 bg=tl fg=br */ PICK(tl2, tl2, tl2, tl2); /* k=0 bg=tl fg=NULL */ PICK(tl2, tl2, tl2, tr2); /* ▗ k=4 bg=tl fg=tr */ PICK(tl2, tl2, tr2, tl2); /* ▖ k=5 bg=tl fg=tr */ PICK(tl2, tl2, tr2, tr2); /* ▄ k=1 bg=tl fg=tr */ PICK(tl2, tr2, tl2, tl2); /* ▝ k=3 bg=tl fg=tr */ PICK(tl2, tr2, tl2, tr2); /* ▌ k=2 bg=tr fg=tl */ PICK(tl2, tr2, tr2, tl2); /* ▞ k=6 bg=tl fg=tr */ PICK(tl2, tr2, tr2, tr2); /* ▘ k=7 bg=tr fg=tl */ PICK(tr2, bl2, bl2, bl2); /* ▘ k=7 bg=bl fg=tr */ PICK(tr2, bl2, bl2, tr2); /* ▞ k=6 bg=tr fg=bl */ PICK(tr2, bl2, tr2, bl2); /* ▌ k=2 bg=bl fg=tr */ PICK(tr2, bl2, tr2, tr2); /* ▝ k=3 bg=tr fg=bl */ PICK(tr2, br2, br2, br2); /* ▘ k=7 bg=br fg=tr */ PICK(tr2, br2, br2, tr2); /* ▞ k=6 bg=tr fg=br */ PICK(tr2, br2, tr2, br2); /* ▌ k=2 bg=br fg=tr */ PICK(tr2, br2, tr2, tr2); /* ▝ k=3 bg=tr fg=br */ PICK(tr2, tl2, tl2, tl2); /* ▘ k=7 bg=tl fg=tr */ PICK(tr2, tl2, tl2, tr2); /* ▞ k=6 bg=tr fg=tl */ PICK(tr2, tl2, tr2, tl2); /* ▌ k=2 bg=tl fg=tr */ PICK(tr2, tl2, tr2, tr2); /* ▝ k=3 bg=tr fg=tl */ PICK(tr2, tr2, bl2, bl2); /* ▄ k=1 bg=tr fg=bl */ PICK(tr2, tr2, bl2, tr2); /* ▖ k=5 bg=tr fg=bl */ PICK(tr2, tr2, br2, br2); /* ▄ k=1 bg=tr fg=br */ PICK(tr2, tr2, br2, tr2); /* ▖ k=5 bg=tr fg=br */ PICK(tr2, tr2, tl2, tl2); /* ▄ k=1 bg=tr fg=tl */ PICK(tr2, tr2, tl2, tr2); /* ▖ k=5 bg=tr fg=tl */ PICK(tr2, tr2, tr2, bl2); /* ▗ k=4 bg=tr fg=bl */ PICK(tr2, tr2, tr2, br2); /* ▗ k=4 bg=tr fg=br */ PICK(tr2, tr2, tr2, tl2); /* ▗ k=4 bg=tr fg=tl */ PICK(tr2, tr2, tr2, tr2); /* k=0 bg=tr fg=NULL */ #undef PICK return p; } static uint16_t *PickCp437(uint16_t *p, struct TtyRgb tl, struct TtyRgb tr, struct TtyRgb bl, struct TtyRgb br, struct TtyRgb tl2, struct TtyRgb tr2, struct TtyRgb bl2, struct TtyRgb br2) { #define PICK(A, B, C, D) *p++ = GetBlockDist(tl, tr, bl, br, A, B, C, D) PICK(bl2, bl2, bl2, bl2); /* k=0 bg=bl fg=NULL */ PICK(bl2, bl2, br2, br2); /* ▄ k=1 bg=bl fg=br */ PICK(bl2, bl2, tl2, tl2); /* ▄ k=1 bg=bl fg=tl */ PICK(bl2, bl2, tr2, tr2); /* ▄ k=1 bg=bl fg=tr */ PICK(br2, br2, bl2, bl2); /* ▄ k=1 bg=br fg=bl */ PICK(tl2, tl2, bl2, bl2); /* ▄ k=1 bg=tl fg=bl */ PICK(tr2, tr2, bl2, bl2); /* ▄ k=1 bg=tr fg=bl */ PICK(bl2, br2, bl2, br2); /* ▌ k=2 bg=br fg=bl */ PICK(bl2, tl2, bl2, tl2); /* ▌ k=2 bg=tl fg=bl */ PICK(bl2, tr2, bl2, tr2); /* ▌ k=2 bg=tr fg=bl */ PICK(br2, bl2, br2, bl2); /* ▌ k=2 bg=bl fg=br */ PICK(br2, br2, br2, br2); /* k=0 bg=br fg=NULL */ PICK(br2, br2, tl2, tl2); /* ▄ k=1 bg=br fg=tl */ PICK(br2, br2, tr2, tr2); /* ▄ k=1 bg=br fg=tr */ PICK(br2, tl2, br2, tl2); /* ▌ k=2 bg=tl fg=br */ PICK(br2, tr2, br2, tr2); /* ▌ k=2 bg=tr fg=br */ PICK(tl2, bl2, tl2, bl2); /* ▌ k=2 bg=bl fg=tl */ PICK(tl2, br2, tl2, br2); /* ▌ k=2 bg=br fg=tl */ PICK(tl2, tl2, br2, br2); /* ▄ k=1 bg=tl fg=br */ PICK(tl2, tl2, tl2, tl2); /* k=0 bg=tl fg=NULL */ PICK(tl2, tl2, tr2, tr2); /* ▄ k=1 bg=tl fg=tr */ PICK(tl2, tr2, tl2, tr2); /* ▌ k=2 bg=tr fg=tl */ PICK(tr2, bl2, tr2, bl2); /* ▌ k=2 bg=bl fg=tr */ PICK(tr2, br2, tr2, br2); /* ▌ k=2 bg=br fg=tr */ PICK(tr2, tl2, tr2, tl2); /* ▌ k=2 bg=tl fg=tr */ PICK(tr2, tr2, br2, br2); /* ▄ k=1 bg=tr fg=br */ PICK(tr2, tr2, tl2, tl2); /* ▄ k=1 bg=tr fg=tl */ PICK(tr2, tr2, tr2, tr2); /* k=0 bg=tr fg=NULL */ #undef PICK return p; } static struct Pick PickBlockUnicodeAnsi(struct TtyRgb tl, struct TtyRgb tr, struct TtyRgb bl, struct TtyRgb br) { struct TtyRgb tl2 = GetQuant(tl); struct TtyRgb tr2 = GetQuant(tr); struct TtyRgb bl2 = GetQuant(bl); struct TtyRgb br2 = GetQuant(br); unsigned i, p1, p2; uint16_t picks1[96] forcealign(32); uint16_t picks2[32] forcealign(32); memset(picks1, 0x79, sizeof(picks1)); memset(picks2, 0x79, sizeof(picks2)); PickUnicode(picks1, tl, tr, bl, br, tl2, tr2, bl2, br2); MixBlock(picks2, tl, tr, bl, br, tl2, tr2, bl2, br2); p1 = windex(picks1, 96); p2 = windex(picks2, 32); return picks1[p1] <= picks2[p2] ? kPicksUnicode[p1] : kPicksMixBlock[p2]; } static struct Pick PickBlockUnicodeTrue(struct TtyRgb tl, struct TtyRgb tr, struct TtyRgb bl, struct TtyRgb br) { unsigned i; uint16_t picks[96] forcealign(32); memset(picks, 0x79, sizeof(picks)); PickUnicode(picks, tl, tr, bl, br, tl, tr, bl, br); i = windex(picks, 96); if (i >= 88) { unsigned j; fprintf(stderr, "uint16_t picks[96] = {"); for (j = 0; j < 96; ++j) { fprintf(stderr, "%3d,", picks[j]); } fprintf(stderr, "}\n"); } CHECK_LT(i, 88); return kPicksUnicode[i]; } static struct Pick PickBlockCp437Ansi(struct TtyRgb tl, struct TtyRgb tr, struct TtyRgb bl, struct TtyRgb br) { struct TtyRgb tl2 = GetQuant(tl); struct TtyRgb tr2 = GetQuant(tr); struct TtyRgb bl2 = GetQuant(bl); struct TtyRgb br2 = GetQuant(br); unsigned i, p1, p2; uint16_t picks1[32] forcealign(32); uint16_t picks2[32] forcealign(32); memset(picks1, 0x79, sizeof(picks1)); memset(picks2, 0x79, sizeof(picks2)); PickCp437(picks1, tl, tr, bl, br, tl2, tr2, bl2, br2); MixBlock(picks2, tl, tr, bl, br, tl2, tr2, bl2, br2); p1 = windex(picks1, 32); p2 = windex(picks2, 32); return picks1[p1] <= picks2[p2] ? kPicksCp437[p1] : kPicksMixBlock[p2]; } static struct Pick PickBlockCp437True(struct TtyRgb tl, struct TtyRgb tr, struct TtyRgb bl, struct TtyRgb br) { unsigned i; uint16_t picks[32] forcealign(32); memset(picks, 0x79, sizeof(picks)); PickCp437(picks, tl, tr, bl, br, tl, tr, bl, br); return kPicksCp437[windex(picks, 32)]; } static char *CopyGlyph(char *v, struct Glyph glyph) { memcpy(v, &glyph, 4); return v + glyph.len; } static char *CopyBlock(char *v, const struct TtyRgb chunk[hasatleast 4], struct Pick pick, struct TtyRgb *bg, struct TtyRgb *fg, struct Glyph *glyph) { unsigned i; CHECK_LT(pick.bg, 4); if (pick.fg != 0xff) CHECK_LT(pick.fg, 4); i = 0; if (pick.fg == 0xff) { if (!ttyeq(*bg, chunk[pick.bg])) { if (ttyeq(*fg, chunk[pick.bg])) { if (memcmp(glyph, &kGlyphs[1][0], sizeof(*glyph)) == 0) { v = setbg(v, (*bg = *fg)); } else { i = 1; } } else { v = setbg(v, (*bg = chunk[pick.bg])); } } } else if (ttyeq(chunk[pick.bg], chunk[pick.fg])) { pick.k = 0; if (!ttyeq(*bg, chunk[pick.bg])) { if (ttyeq(*fg, chunk[pick.bg])) { if (memcmp(glyph, &kGlyphs[1][0], sizeof(*glyph)) == 0) { v = setbg(v, (*bg = *fg)); } else { i = 1; } } else { v = setbg(v, (*bg = chunk[pick.bg])); } } } else if (!ttyeq(*fg, chunk[pick.fg])) { if (!ttyeq(*bg, chunk[pick.bg])) { if (ttyeq(*fg, chunk[pick.bg]) && ttyeq(*bg, chunk[pick.fg])) { if (pick.k == 0 && memcmp(glyph, &kGlyphs[1][0], sizeof(*glyph)) == 0) { v = setbg(v, (*bg = *fg)); } else { i = 1; } } else { v = setbgfg(v, (*bg = chunk[pick.bg]), (*fg = chunk[pick.fg])); } } else { v = setfg(v, (*fg = chunk[pick.fg])); } } else if (!ttyeq(*bg, chunk[pick.bg])) { v = setbg(v, (*bg = chunk[pick.bg])); } return CopyGlyph(v, (*glyph = kGlyphs[i][pick.k])); } static bool ChunkEq(struct TtyRgb c[hasatleast 4], struct TtyRgb c2[hasatleast 4]) { return ttyeq(c[TL], c[TR]) && ttyeq(c[BL], c[BR]) && ttyeq(c2[TL], c2[TR]) && ttyeq(c2[BL], c2[BR]); } static struct TtyRgb *CopyChunk(struct TtyRgb chunk[hasatleast 4], const struct TtyRgb *c, size_t xn) { chunk[TL] = c[00 + 0]; chunk[TR] = c[00 + 1]; chunk[BL] = c[xn + 0]; chunk[BR] = c[xn + 1]; return chunk; } static dontinline char *CopyRun(char *v, size_t n, struct TtyRgb lastchunk[hasatleast 4], const struct TtyRgb **c, size_t *x, struct TtyRgb *bg, struct TtyRgb *fg, struct Glyph *glyph) { struct TtyRgb chunk[4]; if (memcmp(glyph, &kGlyphs[1][0], sizeof(*glyph)) == 0) { if (!ttyeq(*bg, *fg)) { v = setbg(v, (*bg = *fg)); } *glyph = kGlyphs[0][0]; } do { v = CopyGlyph(v, *glyph); *x += 2; *c += 2; if (*x >= n) break; CopyChunk(chunk, *c, n); } while (ChunkEq(chunk, lastchunk)); *x -= 2; *c -= 2; return v; } /** * Maps 2×2 pixel chunks onto ANSI UNICODE cells. * @note h/t Nick Black for his quadrant blitting work on notcurses * @note yn and xn need to be even */ char *ttyraster(char *v, const struct TtyRgb *c, size_t yn, size_t xn, struct TtyRgb bg, struct TtyRgb fg) { unsigned y, x; struct Pick p; struct Glyph glyph; struct TtyRgb chun[4], lastchunk[4]; for (y = 0; y < yn; y += 2, c += xn) { if (y) { v = stpcpy(v, "\e[0m\r\n"); v = setbgfg(v, bg, fg); } for (x = 0; x < xn; x += 2, c += 2) { CopyChunk(chun, c, xn); if (ttyquant()->alg == kTtyQuantTrue) { if (ttyquant()->blocks == kTtyBlocksCp437) { p = PickBlockCp437True(chun[TL], chun[TR], chun[BL], chun[BR]); } else { p = PickBlockUnicodeTrue(chun[TL], chun[TR], chun[BL], chun[BR]); } } else { if (ttyquant()->blocks == kTtyBlocksCp437) { p = PickBlockCp437Ansi(chun[TL], chun[TR], chun[BL], chun[BR]); } else { p = PickBlockUnicodeAnsi(chun[TL], chun[TR], chun[BL], chun[BR]); } } v = CopyBlock(v, chun, p, &bg, &fg, &glyph); memcpy(lastchunk, chun, sizeof(chun)); } } v = stpcpy(v, "\e[0m"); return v; }
28,781
804
jart/cosmopolitan
false
cosmopolitan/dsp/tty/rgb2xterm24f.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/quant.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "third_party/intel/xmmintrin.internal.h" struct TtyRgb rgb2tty24f_(ttyrgb_m128 rgb) { #ifdef __x86_64__ const ttyrgb_m128 kMax = {1, 1, 1, 1}; const ttyrgb_m128 kMin = {0, 0, 0, 0}; struct TtyRgb res; rgb = _mm_min_ps(_mm_max_ps(rgb, kMin), kMax) * 255; res = (struct TtyRgb){rgb[0], rgb[1], rgb[2], rgb[3]}; return res; #else return (struct TtyRgb){ MAX(0, MIN(1, rgb[0])) * 255, MAX(0, MIN(1, rgb[1])) * 255, MAX(0, MIN(1, rgb[2])) * 255, MAX(0, MIN(1, rgb[3])) * 255, }; #endif }
2,453
41
jart/cosmopolitan
false
cosmopolitan/dsp/tty/ttyquant.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/internal.h" #include "dsp/tty/quant.h" #include "libc/dce.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" struct TtyQuant g_ttyquant_; /** * Chooses xterm quantization mode. */ textstartup void ttyquantsetup(enum TtyQuantizationAlgorithm alg, enum TtyQuantizationChannels chans, enum TtyBlocksSelection blocks) { switch (alg) { case kTtyQuantAnsi: TTYQUANT()->rgb2tty = rgb2ansi_; TTYQUANT()->rgb2ttyf = rgb2ttyf2i_; TTYQUANT()->tty2rgb = tty2rgb_; TTYQUANT()->tty2rgbf = tty2rgbf_; TTYQUANT()->setbg = setbg16_; TTYQUANT()->setfg = setfg16_; TTYQUANT()->setbgfg = setbgfg16_; TTYQUANT()->min = 0; TTYQUANT()->max = 16; break; case kTtyQuantTrue: TTYQUANT()->rgb2tty = rgb2xterm24_; TTYQUANT()->rgb2ttyf = rgb2tty24f_; TTYQUANT()->tty2rgb = tty2rgb24_; TTYQUANT()->tty2rgbf = tty2rgbf24_; TTYQUANT()->setbg = setbg24_; TTYQUANT()->setfg = setfg24_; TTYQUANT()->setbgfg = setbgfg24_; TTYQUANT()->min = 16; TTYQUANT()->max = 256; break; case kTtyQuantXterm256: TTYQUANT()->rgb2tty = rgb2ansi_; TTYQUANT()->rgb2ttyf = rgb2ttyf2i_; TTYQUANT()->tty2rgb = tty2rgb_; TTYQUANT()->tty2rgbf = tty2rgbf_; TTYQUANT()->setbg = setbg256_; TTYQUANT()->setfg = setfg256_; TTYQUANT()->setbgfg = setbgfg256_; TTYQUANT()->min = 16; TTYQUANT()->max = 256; break; default: abort(); } TTYQUANT()->chans = chans; TTYQUANT()->alg = alg; TTYQUANT()->blocks = blocks; } textstartup void ttyquant_init(void) { ttyquantsetup(kTtyQuantXterm256, kTtyQuantRgb, kTtyBlocksUnicode); } const void *const ttyquant_init_ctor[] initarray = {ttyquant_init};
3,701
81
jart/cosmopolitan
false
cosmopolitan/dsp/tty/tty.h
#ifndef COSMOPOLITAN_DSP_TTY_TTY_H_ #define COSMOPOLITAN_DSP_TTY_TTY_H_ #define kTtyIdScreen 83 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct FILE; struct termios; struct TtyIdent { int id; /* first number sent back by \e[>c */ int version; /* second number sent back by \e[>c */ struct TtyIdent *next; /* yo dawg */ }; struct TtyCursor { int y; int x; int bg; int fg; }; enum TtyRawFlags { kTtyEcho = 1 << 0, /* echo input */ kTtyCursor = 1 << 1, /* show cursor */ kTtySigs = 1 << 2, /* auto raise() on CTRL+C, CTRL+Z, and CTRL+\ */ kTtyLfToCrLf = 1 << 3, /* enables unix newline magic */ }; typedef int (*ttyconf_f)(struct termios *, int64_t); int ttyraw(enum TtyRawFlags); int ttyhidecursor(int); int ttyshowcursor(int); int ttysavecursor(int); int ttyrestorecursor(int); int ttyenablealtbuf(int); int ttydisablealtbuf(int); int ttysend(int, const char *); ssize_t ttywrite(int, const void *, size_t); int ttysendtitle(int, const char *, const struct TtyIdent *); int ttyident(struct TtyIdent *, int, int); void ttyidentclear(struct TtyIdent *); char *ttydescribe(char *, size_t, const struct TtyIdent *); int ttyconfig(int, ttyconf_f, int64_t, struct termios *); int ttyrestore(int, const struct termios *); int ttysetrawdeadline(struct termios *, int64_t); int ttysetrawmode(struct termios *, int64_t); int ttysetraw(struct termios *, int64_t); char *ttymove(struct TtyCursor *, char *, int, int) paramsnonnull() returnsnonnull; void ttyhisto(uint32_t[hasatleast 256], uint8_t[hasatleast 256], const uint8_t *, const uint8_t *, size_t); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_TTY_TTY_H_ */
1,755
60
jart/cosmopolitan
false
cosmopolitan/dsp/tty/ttymove.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/itoa8.h" #include "dsp/tty/tty.h" #include "libc/intrin/safemacros.internal.h" #include "libc/limits.h" #include "libc/log/check.h" /** * Moves teletypewriter cursor to new coordinate. * * This uses codings defined by ANSI X3.4-1967 / X3.64-1979. * * @param c [in/out] tracks cursor coordinate * @param p [out] buffer receives ANSI/VT sequences * @param y is 0-indexed row * @param x is 0-indexed column * @return p + written, like mempcpy() */ char *ttymove(struct TtyCursor *c, char *p, int y, int x) { int d; DCHECK_GE(y, 0); DCHECK_GE(x, 0); DCHECK_LE(y, UINT16_MAX); DCHECK_LE(x, UINT16_MAX); if (y != c->y || x != c->x) { do { if (y != c->y && x != c->x) { if (y == c->y + 1 && x == 0) { p[0] = '\r'; p[1] = '\n'; p[2] = '\0'; p[3] = '\0'; p += 2; c->y++; c->x = 0; break; } else if (x < 256 && y < 256) { if (y == 0 && x == 0) { if (c->y == 0) { p[0] = '\r'; p[1] = '\0'; p += 1; c->x = 0; } else { p[0] = '\e'; /* CUP(1,1) */ p[1] = '['; p[2] = 'H'; p[3] = '\0'; p += 3; c->y = 0; c->x = 0; } } else if (x == 0) { *p++ = '\e'; /* CUP(y,1) */ *p++ = '['; p = itoa8(p, y + 1); *p++ = 'H'; *p = '\0'; c->y = y; c->x = 0; } else if (y == 0) { *p++ = '\e'; /* CUP(1,x) */ *p++ = '['; *p++ = ';'; *p = '\0'; p = itoa8(p, x + 1); *p++ = 'H'; *p = '\0'; c->y = 0; c->x = x; } else { *p++ = '\e'; /* CUP(y,1) */ *p++ = '['; p = itoa8(p, y + 1); *p++ = ';'; p = itoa8(p, x + 1); *p++ = 'H'; *p = '\0'; c->y = y; c->x = x; } break; } } if (x != c->x) { if (!x) { p[0] = '\r'; /* goto beginning of line */ p[1] = '\0'; p += 1; c->x = 0; } else if (x > c->x) { d = min(255, x - c->x); if (d == 1) { p[0] = '\e'; p[1] = '['; p[2] = 'C'; /* cursor forward (CUF) */ p[3] = '\0'; p += 3; } else { p[0] = '\e'; p[1] = '['; p = itoa8(p + 2, d); p[0] = 'C'; /* cursor forward (CUF) */ p[1] = '\0'; p[2] = '\0'; p[3] = '\0'; p += 1; } c->x += d; } else { d = min(255, c->x - x); if (d == 1) { p[0] = '\e'; p[1] = '['; p[2] = 'D'; /* cursor backward (CUB) */ p[3] = '\0'; p += 3; } else { p[0] = '\e'; p[1] = '['; p = itoa8(p + 2, d); p[0] = 'D'; /* cursor backward (CUB) */ p[1] = '\0'; p[2] = '\0'; p[3] = '\0'; p += 1; } c->x -= d; } } if (y != c->y) { if (y > c->y) { d = min(255, y - c->y); if (d == 1) { p[0] = '\e'; p[1] = 'D'; /* index down (IND) */ p[2] = '\0'; p[3] = '\0'; p += 2; } else { p[0] = '\e'; p[1] = '['; p = itoa8(p + 2, d); p[0] = 'B'; /* cursor down (CUD) */ p[1] = '\0'; p[2] = '\0'; p[3] = '\0'; p += 1; } c->y += d; } else { d = min(255, c->y - y); if (d == 1) { p[0] = '\e'; p[1] = 'M'; /* reverse index (RI) */ p[2] = '\0'; p[3] = '\0'; p += 2; } else { p[0] = '\e'; p[1] = '['; p = itoa8(p + 2, d); p[0] = 'A'; /* cursor up (CUU) */ p[1] = '\0'; p[2] = '\0'; p[3] = '\0'; p += 1; } c->y -= d; } } } while (x != c->x || y != c->y); } else { p[0] = '\0'; } DCHECK_EQ(y, c->y); DCHECK_EQ(x, c->x); DCHECK_EQ(p[0], '\0'); return p; }
6,357
198
jart/cosmopolitan
false
cosmopolitan/dsp/tty/config.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/assert.h" #include "libc/calls/termios.h" #include "libc/str/str.h" #include "libc/sysv/consts/termios.h" /** * Applies configuration to teletypewriter. * * @param opt_out_oldconf is only modified if successful * @return 0 on success, or -1 w/ errno * @see ttyconfig(), ttyrestore() */ int ttyconfig(int ttyfd, ttyconf_f fn, int64_t arg, struct termios *opt_out_oldconf) { struct termios conf[2]; if (tcgetattr(ttyfd, &conf[0]) != -1 && fn(memcpy(&conf[1], &conf[0], sizeof(conf[0])), arg) != -1 && tcsetattr(ttyfd, TCSAFLUSH, &conf[1]) != -1) { if (opt_out_oldconf) { memcpy(opt_out_oldconf, &conf[0], sizeof(conf[0])); } return 0; } else { return -1; } }
2,596
46
jart/cosmopolitan
false
cosmopolitan/dsp/tty/windex-k8.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/assert.h" unsigned windex_k8(short *a, size_t n) { unsigned short min, res, i; res = 0; min = a[0]; for (i = 1; i < n; ++i) { if (a[i] < min) { min = a[i]; res = i; } } return res; }
2,092
34
jart/cosmopolitan
false
cosmopolitan/dsp/tty/itoa8.h
#ifndef COSMOPOLITAN_DSP_TTY_ITOA8_H_ #define COSMOPOLITAN_DSP_TTY_ITOA8_H_ #include "libc/str/str.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct Itoa8 { uint8_t size[256]; uint32_t data[256]; }; extern struct Itoa8 kItoa8; forceinline char *itoa8(char *p, uint8_t c) { memcpy(p, &kItoa8.data[c], 4); return p + kItoa8.size[c]; } COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_TTY_ITOA8_H_ */
475
22
jart/cosmopolitan
false
cosmopolitan/dsp/tty/sendtitle.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/mem/arraylist2.internal.h" #include "libc/mem/gc.internal.h" #include "libc/x/x.h" /* TODO(jart): DELETE */ /** * Changes text in title bar of pseudo-teletypewriter window. * * @param title is trustworthy text without any BEL characters * @param ti comes from ttyident() and null means no-op */ int ttysendtitle(int ttyfd, const char *title, const struct TtyIdent *ti) { int res; if (ti) { char *p; if (ti->id == kTtyIdScreen) { res = ttysend(ttyfd, (p = xstrcat("\eP\e]0;", title, "\a\e\\"))); } else { res = ttysend(ttyfd, (p = xstrcat("\e]0;", title, "\a"))); } free(p); } else { res = 0; } return res; }
2,537
47
jart/cosmopolitan
false
cosmopolitan/dsp/tty/ttyraw.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/siginfo.h" #include "libc/calls/termios.h" #include "libc/calls/ucontext.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/mem/gc.internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/consts/fileno.h" #include "libc/sysv/consts/sa.h" #include "libc/sysv/consts/sig.h" #include "libc/sysv/errfuns.h" #include "libc/x/x.h" #include "libc/x/xsigaction.h" /* TODO(jart): DELETE */ #define FD STDOUT_FILENO static struct TtyRaw { bool setup; bool hasold; bool noreentry; bool initialized; enum TtyRawFlags flags; sigaction_f next[10]; unsigned char sigs[10]; struct termios old; } g_ttyraw; static textstartup int ttyraw_setup(void) { struct termios *old; if (isatty(FD)) { old = !g_ttyraw.hasold ? &g_ttyraw.old : NULL; if (ttyconfig(FD, ttysetrawmode, g_ttyraw.flags, old) != -1) { g_ttyraw.hasold = true; return 0; } } return -1; } static textstartup int ttyraw_enable(void) { int rc; g_ttyraw.setup = (rc = ttyraw_setup()) != -1 || g_ttyraw.setup; return rc; } static textstartup void ttyraw_hidecursor(void) { if (!g_ttyraw.setup) return; if (g_ttyraw.flags & kTtyCursor) return; ttyhidecursor(FD); } static textexit int ttyraw_disable(void) { if (!g_ttyraw.setup) return 0; ttyshowcursor(FD); return ttyrestore(FD, &g_ttyraw.old); } static textexit void ttyraw_onexit(void) { ttyraw_disable(); } static relegated void ttyraw_onsig(int sig, struct siginfo *info, struct ucontext *ctx) { size_t i; if (g_ttyraw.noreentry) _Exit(128 + sig); g_ttyraw.noreentry = true; if (g_ttyraw.flags != -1) { if (sig == SIGCONT) { ttyraw_enable(); } else { ttyraw_disable(); } } for (i = 0; i < ARRAYLEN(g_ttyraw.sigs); ++i) { if (g_ttyraw.sigs[i] == sig) { if (g_ttyraw.next[i] != (void *)SIG_IGN) { if (g_ttyraw.next[i] != (void *)SIG_DFL) { if (g_ttyraw.next[i]) { g_ttyraw.next[i](sig, info, ctx); } } else if (sig != SIGCONT) { _Exit(128 + sig); } } break; } } g_ttyraw.noreentry = false; } static textstartup void ttyraw_initsig(int sig, unsigned flags, unsigned mask) { static unsigned i; struct sigaction old; g_ttyraw.next[i] = xsigaction(sig, ttyraw_onsig, flags, mask, &old) != -1 ? old.sa_sigaction : (void *)SIG_DFL; g_ttyraw.sigs[i++] = sig; } static textstartup void ttyraw_init(void) { unsigned crashmask = ~(SIGILL | SIGBUS | SIGSEGV | SIGABRT); ttyraw_initsig(SIGILL, SA_SIGINFO | SA_NODEFER | SA_ONSTACK, crashmask); ttyraw_initsig(SIGSEGV, SA_SIGINFO | SA_NODEFER | SA_ONSTACK, crashmask); ttyraw_initsig(SIGBUS, SA_SIGINFO | SA_NODEFER | SA_ONSTACK, crashmask); ttyraw_initsig(SIGABRT, SA_SIGINFO | SA_NODEFER, crashmask); ttyraw_initsig(SIGFPE, SA_SIGINFO | SA_RESTART, crashmask); ttyraw_initsig(SIGTRAP, SA_SIGINFO | SA_RESTART, crashmask); ttyraw_initsig(SIGHUP, SA_SIGINFO | SA_RESTART, crashmask); ttyraw_initsig(SIGINT, SA_SIGINFO | SA_RESTART, crashmask); ttyraw_initsig(SIGQUIT, SA_SIGINFO | SA_RESTART, crashmask); ttyraw_initsig(SIGCONT, SA_SIGINFO | SA_RESTART, crashmask); atexit(ttyraw_onexit); } /** * Sets raw mode, safely, the easy way. * * This should be called after your signal handlers have been installed. */ textstartup int ttyraw(enum TtyRawFlags flags) { int rc; if ((g_ttyraw.flags = flags) != -1) { if (!g_ttyraw.initialized) { g_ttyraw.initialized = true; ttyraw_init(); } if ((rc = ttyraw_enable()) != -1) { ttyraw_hidecursor(); } } else { rc = ttyraw_disable(); } return rc; }
5,745
160
jart/cosmopolitan
false
cosmopolitan/dsp/tty/mpsadbw.S
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ │vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" // TODO(jart): write me movdqa a,%xmm0 mpsadbw $0,inv,%xmm0 .rodata.cst32 a: .byte 1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 inv: .byte 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
2,040
29
jart/cosmopolitan
false
cosmopolitan/dsp/tty/setraw.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/calls/termios.h" #include "libc/sysv/consts/termios.h" /** * Enables direct teletypewriter communication. * * @see ttyconfig(), ttyrestore() */ int ttysetraw(struct termios *conf, int64_t flags) { conf->c_iflag &= ~(INPCK | ISTRIP | PARMRK | INLCR | IGNCR | ICRNL | IXON); conf->c_lflag &= ~(IEXTEN | ICANON); conf->c_cflag &= ~(CSIZE | PARENB); conf->c_cflag |= CS8; conf->c_iflag |= IUTF8; if (!(flags & kTtySigs)) { conf->c_iflag &= ~(IGNBRK | BRKINT); conf->c_lflag &= ~(ISIG); } if (flags & kTtyEcho) { conf->c_lflag |= ECHO | ECHONL; } else { conf->c_lflag &= ~(ECHO | ECHONL); } return 0; }
2,517
45
jart/cosmopolitan
false
cosmopolitan/dsp/tty/write.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/calls/calls.h" #include "libc/calls/termios.h" #include "libc/errno.h" #include "libc/sock/sock.h" #include "libc/sock/struct/pollfd.h" #include "libc/sysv/consts/poll.h" #include "libc/sysv/consts/termios.h" /* TODO(jart): DELETE */ /** * Sends data to teletypewriter the pain-free way. * * This function blocks forever until the full amount is transmitted, * regardless of whether or not the character device is in non-blocking * mode, regardless of whether or not it's interrupted by a signal. It * can still be escaped by longjmp'ing out of a signal handler. * * @return 0 on success, or -1 w/ errno */ ssize_t ttywrite(int fd, const void *data, size_t size) { char *p; ssize_t rc; size_t wrote, n; p = data; n = size; do { TryAgain: if ((rc = write(fd, p, n)) != -1) { wrote = rc; p += wrote; n -= wrote; } else if (errno == EINTR) { goto TryAgain; } else if (errno == EAGAIN) { poll((struct pollfd[]){{fd, POLLOUT}}, 1, -1); } else { return -1; } } while (n); return 0; }
2,939
62
jart/cosmopolitan
false
cosmopolitan/dsp/tty/kcgapalette.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "dsp/tty/quant.h" const ttypalette_t kCgaPalette = { [0][0] = {0, 0, 0, 0}, /* normal black: \e[30m (or \e[38;5;0m) */ [1][0] = {85, 85, 85, 8}, /* bright black: \e[90m (or \e[38;5;8m) */ [0][1] = {170, 0, 0, 1}, /* normal red: \e[31m */ [1][1] = {255, 85, 85, 9}, /* bright red: \e[91m (or \e[1;31m) */ [0][2] = {0, 170, 0, 2}, /* normal green: \e[32m */ [1][2] = {85, 255, 85, 10}, /* bright green: \e[92m */ [0][3] = {170, 85, 0, 3}, /* normal yellow: \e[33m */ [1][3] = {255, 255, 85, 11}, /* bright yellow: \e[93m */ [0][4] = {0, 0, 170, 4}, /* normal blue: \e[34m */ [1][4] = {85, 85, 255, 12}, /* bright blue: \e[94m */ [0][5] = {170, 0, 170, 5}, /* normal magenta: \e[35m */ [1][5] = {255, 85, 255, 13}, /* bright magenta: \e[95m */ [0][6] = {0, 170, 170, 6}, /* normal cyan: \e[36m */ [1][6] = {85, 255, 255, 14}, /* bright cyan: \e[96m */ [0][7] = {170, 170, 170, 7}, /* normal white: \e[37m */ [1][7] = {255, 255, 255, 15}, /* bright white: \e[97m */ };
1,904
30
jart/cosmopolitan
false
cosmopolitan/dsp/tty/xtermname.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/xtermname.h" /** * 256-entry double-nul-terminated list of XTERM256 names. */ const char kXtermName[] = "\ Black\0\ Maroon\0\ Green\0\ Olive\0\ Navy\0\ Purple\0\ Teal\0\ Silver\0\ Grey\0\ Red\0\ Lime\0\ Yellow\0\ Blue\0\ Fuchsia\0\ Aqua\0\ White\0\ Grey0\0\ NavyBlue\0\ DarkBlue\0\ Blue3\0\ Blue3\0\ Blue1\0\ DarkGreen\0\ DeepSkyBlue4\0\ DeepSkyBlue4\0\ DeepSkyBlue4\0\ DodgerBlue3\0\ DodgerBlue2\0\ Green4\0\ SpringGreen4\0\ Turquoise4\0\ DeepSkyBlue3\0\ DeepSkyBlue3\0\ DodgerBlue1\0\ Green3\0\ SpringGreen3\0\ DarkCyan\0\ LightSeaGreen\0\ DeepSkyBlue2\0\ DeepSkyBlue1\0\ Green3\0\ SpringGreen3\0\ SpringGreen2\0\ Cyan3\0\ DarkTurquoise\0\ Turquoise2\0\ Green1\0\ SpringGreen2\0\ SpringGreen1\0\ MediumSpringGreen\0\ Cyan2\0\ Cyan1\0\ DarkRed\0\ DeepPink4\0\ Purple4\0\ Purple4\0\ Purple3\0\ BlueViolet\0\ Orange4\0\ Grey37\0\ MediumPurple4\0\ SlateBlue3\0\ SlateBlue3\0\ RoyalBlue1\0\ Chartreuse4\0\ DarkSeaGreen4\0\ PaleTurquoise4\0\ SteelBlue\0\ SteelBlue3\0\ CornflowerBlue\0\ Chartreuse3\0\ DarkSeaGreen4\0\ CadetBlue\0\ CadetBlue\0\ SkyBlue3\0\ SteelBlue1\0\ Chartreuse3\0\ PaleGreen3\0\ SeaGreen3\0\ Aquamarine3\0\ MediumTurquoise\0\ SteelBlue1\0\ Chartreuse2\0\ SeaGreen2\0\ SeaGreen1\0\ SeaGreen1\0\ Aquamarine1\0\ DarkSlateGray2\0\ DarkRed\0\ DeepPink4\0\ DarkMagenta\0\ DarkMagenta\0\ DarkViolet\0\ Purple\0\ Orange4\0\ LightPink4\0\ Plum4\0\ MediumPurple3\0\ MediumPurple3\0\ SlateBlue1\0\ Yellow4\0\ Wheat4\0\ Grey53\0\ LightSlateGrey\0\ MediumPurple\0\ LightSlateBlue\0\ Yellow4\0\ DarkOliveGreen3\0\ DarkSeaGreen\0\ LightSkyBlue3\0\ LightSkyBlue3\0\ SkyBlue2\0\ Chartreuse2\0\ DarkOliveGreen3\0\ PaleGreen3\0\ DarkSeaGreen3\0\ DarkSlateGray3\0\ SkyBlue1\0\ Chartreuse1\0\ LightGreen\0\ LightGreen\0\ PaleGreen1\0\ Aquamarine1\0\ DarkSlateGray1\0\ Red3\0\ DeepPink4\0\ MediumVioletRed\0\ Magenta3\0\ DarkViolet\0\ Purple\0\ DarkOrange3\0\ IndianRed\0\ HotPink3\0\ MediumOrchid3\0\ MediumOrchid\0\ MediumPurple2\0\ DarkGoldenrod\0\ LightSalmon3\0\ RosyBrown\0\ Grey63\0\ MediumPurple2\0\ MediumPurple1\0\ Gold3\0\ DarkKhaki\0\ NavajoWhite3\0\ Grey69\0\ LightSteelBlue3\0\ LightSteelBlue\0\ Yellow3\0\ DarkOliveGreen3\0\ DarkSeaGreen3\0\ DarkSeaGreen2\0\ LightCyan3\0\ LightSkyBlue1\0\ GreenYellow\0\ DarkOliveGreen2\0\ PaleGreen1\0\ DarkSeaGreen2\0\ DarkSeaGreen1\0\ PaleTurquoise1\0\ Red3\0\ DeepPink3\0\ DeepPink3\0\ Magenta3\0\ Magenta3\0\ Magenta2\0\ DarkOrange3\0\ IndianRed\0\ HotPink3\0\ HotPink2\0\ Orchid\0\ MediumOrchid1\0\ Orange3\0\ LightSalmon3\0\ LightPink3\0\ Pink3\0\ Plum3\0\ Violet\0\ Gold3\0\ LightGoldenrod3\0\ Tan\0\ MistyRose3\0\ Thistle3\0\ Plum2\0\ Yellow3\0\ Khaki3\0\ LightGoldenrod2\0\ LightYellow3\0\ Grey84\0\ LightSteelBlue1\0\ Yellow2\0\ DarkOliveGreen1\0\ DarkOliveGreen1\0\ DarkSeaGreen1\0\ Honeydew2\0\ LightCyan1\0\ Red1\0\ DeepPink2\0\ DeepPink1\0\ DeepPink1\0\ Magenta2\0\ Magenta1\0\ OrangeRed1\0\ IndianRed1\0\ IndianRed1\0\ HotPink\0\ HotPink\0\ MediumOrchid1\0\ DarkOrange\0\ Salmon1\0\ LightCoral\0\ PaleVioletRed1\0\ Orchid2\0\ Orchid1\0\ Orange1\0\ SandyBrown\0\ LightSalmon1\0\ LightPink1\0\ Pink1\0\ Plum1\0\ Gold1\0\ LightGoldenrod2\0\ LightGoldenrod2\0\ NavajoWhite1\0\ MistyRose1\0\ Thistle1\0\ Yellow1\0\ LightGoldenrod1\0\ Khaki1\0\ Wheat1\0\ Cornsilk1\0\ Grey100\0\ Grey3\0\ Grey7\0\ Grey11\0\ Grey15\0\ Grey19\0\ Grey23\0\ Grey27\0\ Grey30\0\ Grey35\0\ Grey39\0\ Grey42\0\ Grey46\0\ Grey50\0\ Grey54\0\ Grey58\0\ Grey62\0\ Grey66\0\ Grey70\0\ Grey74\0\ Grey78\0\ Grey82\0\ Grey85\0\ Grey89\0\ Grey93\0\ \0";
5,333
282
jart/cosmopolitan
false
cosmopolitan/dsp/tty/quant.h
#ifndef DSP_TTY_QUANT_H_ #define DSP_TTY_QUANT_H_ #include "dsp/tty/ttyrgb.h" #include "libc/assert.h" #include "libc/intrin/bits.h" #include "libc/limits.h" #include "libc/str/str.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ #define TL 0 #define TR 1 #define BL 2 #define BR 3 typedef float ttyrgb_m128 __attribute__((__vector_size__(16), __may_alias__)); typedef ttyrgb_m128 (*tty2rgbf_f)(struct TtyRgb); typedef char *(*setbg_f)(char *, struct TtyRgb); typedef char *(*setbgfg_f)(char *, struct TtyRgb, struct TtyRgb); typedef char *(*setfg_f)(char *, struct TtyRgb); typedef struct TtyRgb (*rgb2tty_f)(int, int, int); typedef struct TtyRgb (*rgb2ttyf_f)(ttyrgb_m128); typedef struct TtyRgb (*tty2rgb_f)(struct TtyRgb); typedef struct TtyRgb ttypalette_t[2][8]; enum TtyQuantizationAlgorithm { kTtyQuantAnsi, kTtyQuantTrue, kTtyQuantXterm256, }; enum TtyBlocksSelection { kTtyBlocksUnicode, kTtyBlocksCp437, }; enum TtyQuantizationChannels { kTtyQuantGrayscale = 1, kTtyQuantRgb = 3, }; struct TtyQuant { enum TtyQuantizationAlgorithm alg; enum TtyBlocksSelection blocks; enum TtyQuantizationChannels chans; unsigned min; unsigned max; setbg_f setbg; setfg_f setfg; setbgfg_f setbgfg; rgb2tty_f rgb2tty; rgb2ttyf_f rgb2ttyf; tty2rgb_f tty2rgb; tty2rgbf_f tty2rgbf; const ttypalette_t *palette; }; extern const ttypalette_t kCgaPalette; extern const ttypalette_t kXtermPalette; extern const ttypalette_t kTangoPalette; extern const uint8_t kXtermCubeSteps[6]; extern double g_xterm256_gamma; extern struct TtyRgb g_ansi2rgb_[256]; extern struct TtyQuant g_ttyquant_; extern const uint8_t kXtermXlat[2][256]; extern const uint8_t kXtermCube[6]; void ttyquantsetup(enum TtyQuantizationAlgorithm, enum TtyQuantizationChannels, enum TtyBlocksSelection); extern char *ttyraster(char *, const struct TtyRgb *, size_t, size_t, struct TtyRgb, struct TtyRgb); #ifndef ttyquant #define ttyquant() (&g_ttyquant_) #define TTYQUANT() VEIL("r", &g_ttyquant_) #define rgb2tty(...) (ttyquant()->rgb2tty(__VA_ARGS__)) #define tty2rgb(...) (ttyquant()->tty2rgb(__VA_ARGS__)) #define rgb2ttyf(...) (ttyquant()->rgb2ttyf(__VA_ARGS__)) #define tty2rgbf(...) (ttyquant()->tty2rgbf(__VA_ARGS__)) #define setbg(...) (ttyquant()->setbg(__VA_ARGS__)) #define setfg(...) (ttyquant()->setfg(__VA_ARGS__)) #define setbgfg(...) (ttyquant()->setbgfg(__VA_ARGS__)) #endif /* ttyquant */ forceinline bool ttyeq(struct TtyRgb x, struct TtyRgb y) { return x.r == y.r && x.g == y.g && x.b == y.b; } COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* DSP_TTY_QUANT_H_ */
2,693
95
jart/cosmopolitan
false
cosmopolitan/dsp/tty/rgb2ansi.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "dsp/tty/quant.h" #include "libc/assert.h" #include "libc/limits.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/str/str.h" #define CUTOFF_VALUE 200 #define DIST(X, Y) ((X) - (Y)) #define SQR(X) ((X) * (X)) #define SUM(X, Y, Z) ((X) + (Y) + (Z)) const uint8_t kXtermCube[6] = {0, 0137, 0207, 0257, 0327, 0377}; struct TtyRgb g_ansi2rgb_[256]; double g_xterm256_gamma; struct TtyRgb tty2rgb_(struct TtyRgb rgbxt) { return g_ansi2rgb_[rgbxt.xt]; } ttyrgb_m128 tty2rgbf_(struct TtyRgb rgbxt) { rgbxt = g_ansi2rgb_[rgbxt.xt]; return (ttyrgb_m128){(int)rgbxt.r, (int)rgbxt.g, (int)rgbxt.b} / 255; } static int rgb2xterm256_(int r, int g, int b) { int cerr, gerr, ir, ig, ib, gray, grai, cr, cg, cb, gv; gray = round(871024 / 4096299. * r + 8788810 / 12288897. * g + 887015 / 12288897. * b); grai = gray > 238 ? 23 : (gray - 3) / 10; ir = r < 48 ? 0 : r < 115 ? 1 : (r - 35) / 40; ig = g < 48 ? 0 : g < 115 ? 1 : (g - 35) / 40; ib = b < 48 ? 0 : b < 115 ? 1 : (b - 35) / 40; cr = kXtermCube[ir]; cg = kXtermCube[ig]; cb = kXtermCube[ib]; gv = 8 + 10 * grai; cerr = SQR(DIST(cr, r)) + SQR(DIST(cg, g)) + SQR(DIST(cb, b)); gerr = SQR(DIST(gv, r)) + SQR(DIST(gv, g)) + SQR(DIST(gv, b)); if (cerr <= gerr) { return 16 + 36 * ir + 6 * ig + ib; } else { return 232 + grai; } } /** * Quantizes RGB to ANSI w/ euclidean distance in 3D color space. */ struct TtyRgb rgb2ansi_(int r, int g, int b) { uint32_t d, least; size_t c, best, min, max; r = MAX(MIN(r, 255), 0); g = MAX(MIN(g, 255), 0); b = MAX(MIN(b, 255), 0); min = ttyquant()->min; max = ttyquant()->max; if (min == 16 && max == 256) { return (struct TtyRgb){r, g, b, rgb2xterm256_(r, g, b)}; } else { least = UINT32_MAX; best = 0; for (c = min; c < max; c++) { d = SUM(SQR(DIST(g_ansi2rgb_[c].r, r)), SQR(DIST(g_ansi2rgb_[c].g, g)), SQR(DIST(g_ansi2rgb_[c].b, b))); if (d < least) { least = d; best = c; } } return (struct TtyRgb){r, g, b, best}; } } static int uncube(int x) { return x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40; } static textstartup void rgb2ansi_init(void) { uint8_t c, y; uint32_t i, j; memcpy(g_ansi2rgb_, &kCgaPalette, sizeof(kCgaPalette)); for (i = 16; i < 232; ++i) { g_ansi2rgb_[i].r = kXtermCube[((i - 020) / 044) % 06]; g_ansi2rgb_[i].g = kXtermCube[((i - 020) / 06) % 06]; g_ansi2rgb_[i].b = kXtermCube[(i - 020) % 06]; g_ansi2rgb_[i].xt = i; } for (i = 232, c = 8; i < 256; i++, c += 10) { g_ansi2rgb_[i].r = c; g_ansi2rgb_[i].g = c; g_ansi2rgb_[i].b = c; g_ansi2rgb_[i].xt = i; } } const void *const rgb2ansi_init_ctor[] initarray = {rgb2ansi_init};
4,667
119
jart/cosmopolitan
false
cosmopolitan/dsp/tty/windex.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/windex.h" #include "dsp/tty/tty.h" #include "libc/nexgen32e/x86feature.h" extern unsigned windex_avx2(uint16_t *, size_t); extern unsigned windex_sse4(uint16_t *, size_t); extern unsigned windex_k8(uint16_t *, size_t); unsigned (*windex)(uint16_t *, size_t); __attribute__((__constructor__)) static void init_windex(void) { #ifdef __x86_64__ if (X86_HAVE(AVX2)) { windex = windex_avx2; } else if (X86_HAVE(SSE4_2)) { windex = windex_sse4; } else { windex = windex_k8; } #else windex = windex_k8; #endif }
2,384
42
jart/cosmopolitan
false
cosmopolitan/dsp/tty/hidecursor.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/tty.h" #include "libc/intrin/pushpop.h" #include "libc/dce.h" #include "libc/log/internal.h" #include "libc/log/log.h" #include "libc/nt/console.h" #include "libc/nt/runtime.h" #include "libc/nt/struct/consolecursorinfo.h" #include "libc/runtime/runtime.h" /* TODO(jart): DELETE */ static int ttysetcursor(int fd, bool visible) { struct NtConsoleCursorInfo ntcursor; char code[8] = "\e[?25l"; if (__nocolor) return 0; if (visible) code[5] = 'h'; if (SupportsWindows()) { GetConsoleCursorInfo(GetStdHandle(kNtStdOutputHandle), &ntcursor); ntcursor.bVisible = visible; SetConsoleCursorInfo(GetStdHandle(kNtStdOutputHandle), &ntcursor); } return ttysend(fd, code); } /** * Asks teletypewriter to hide blinking box. */ int ttyhidecursor(int fd) { return ttysetcursor(fd, false); } /** * Asks teletypewriter to restore blinking box. */ int ttyshowcursor(int fd) { return ttysetcursor(fd, true); }
2,784
57
jart/cosmopolitan
false
cosmopolitan/dsp/tty/windex.h
#ifndef COSMOPOLITAN_DSP_TTY_WINDEX_H_ #define COSMOPOLITAN_DSP_TTY_WINDEX_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ extern unsigned (*windex)(uint16_t *, size_t); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_TTY_WINDEX_H_ */
298
11
jart/cosmopolitan
false
cosmopolitan/dsp/tty/tty2rgb24.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/quant.h" struct TtyRgb tty2rgb24_(struct TtyRgb rgbxt) { return rgbxt; }
1,930
24
jart/cosmopolitan
false
cosmopolitan/dsp/tty/rgb2xterm24.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/quant.h" #include "libc/macros.internal.h" struct TtyRgb rgb2xterm24_(int r, int g, int b) { return (struct TtyRgb){MAX(MIN(r, 255), 0), MAX(MIN(g, 255), 0), MAX(MIN(b, 255), 0), 0}; }
2,067
26
jart/cosmopolitan
false
cosmopolitan/dsp/tty/rgb2ttyf2i.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/quant.h" typedef int ttyrgb_i4 __attribute__((__vector_size__(16))); struct TtyRgb rgb2ttyf2i_(ttyrgb_m128 rgb) { rgb *= 255; ttyrgb_i4 rgbi = {rgb[0], rgb[1], rgb[2], rgb[3]}; return rgb2tty(rgbi[0], rgbi[1], rgbi[2]); }
2,084
28
jart/cosmopolitan
false
cosmopolitan/dsp/tty/itoa8.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/tty/itoa8.h" #include "libc/intrin/bits.h" #include "libc/str/str.h" struct Itoa8 kItoa8; static textstartup void itoa8_init(void) { int i; uint8_t z; uint32_t w; for (i = 0; i < 256; ++i) { if (i < 10) { z = 1; w = '0' + i; } else if (i < 100) { z = 2; w = ('0' + i / 10) | ('0' + i % 10) << 8; } else { z = 3; w = ('0' + i / 100) | ('0' + i % 100 / 10) << 8 | ('0' + i % 100 % 10) << 16; } kItoa8.size[i] = z; kItoa8.data[i] = w; } } const void *const itoa8_init_ctor[] initarray = {itoa8_init};
2,431
47
jart/cosmopolitan
false
cosmopolitan/dsp/core/unmulaw.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "libc/intrin/likely.h" /** * Expands μ-Law coded to audio sample. * * @param x is coded number masked to range [0..255] * @return 16-bit signed linear audio sample * @note generate lut to save 2 cycles * @see ITU G.711 */ int unmulaw(int x) { int e, m, k, s; e = (~x >> 4) & 7; m = ~x & 15; k = 4 << (e + 1); s = (128 << e) + k * m + k / 2 - 4 * 33; return x & 128 ? s : -s; }
2,270
38
jart/cosmopolitan
false
cosmopolitan/dsp/core/getintegercoefficients.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/q.h" #include "libc/assert.h" #include "libc/dce.h" #include "libc/limits.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/str/str.h" /** * Precomputes integers that can replace floating-point operands. * * “G-d made the integers, all else is the work of man. * — Leopold Kronecker * * This function shifts the decimal point to the left: * * 𝑛ᵢ ← ROUND[𝑐ᵢ × 2ᵐ] + φᵢ * * With extra effort to compute φ which is normally all zeroes but gives * us better rounding when it isn't. It's assumed optimized coefficients * will be used like this: * * (Σᵢ𝑥ᵢ𝑛ᵢ + 2⁽ᵐ⁻¹⁾) / 2ᵐ where 𝑥∈[𝐿,𝐻] and 𝑖∈[0,6) * * Intended to compute this * * ROUND[Σᵢ𝑥ᵢ𝑐ᵢ] * * As accurately or approximately as you want it to be. Popular scaling * factors are 7, 15, 16, 22, and 31. Building this code under MODE=tiny * will DCE the math. * * @param N receives optimized integers * @param C provides ideal coefficients * @param M is log₂ scaling factor, e.g. 7 * @param L is minimum input data size, e.g. 0 * @param H is maximum input data size, e.g. 255 * @return sum of errors for all inputs * @see en.wikipedia.org/wiki/Binary_scaling * @see o/tool/build/coefficients.com * @cost ~300ns */ long GetIntegerCoefficients(long N[static 6], const double C[static 6], long M, long L, long H) { int i; int j[6], J[6]; int O[6] = {0}; int S[3] = {0, -1, +1}; double R[6], K[6], D[6], HM, HL, least, error; least = 1; HM = 1L << M; HL = H - L + 1; assert(H >= L); assert(HL <= HM); for (i = 0; i < 6; ++i) { least *= HL; if (fabs(C[i]) > DBL_MIN) { J[i] = ARRAYLEN(S); R[i] = C[i] * HM; K[i] = rint(R[i]); N[i] = K[i]; } else { J[i] = 1; R[i] = 0; K[i] = 0; N[i] = 0; } } if (!NoDebug() && least > 1) { for (j[0] = 0; j[0] < J[0]; ++j[0]) { for (j[1] = 0; j[1] < J[1]; ++j[1]) { for (j[2] = 0; j[2] < J[2]; ++j[2]) { for (j[3] = 0; j[3] < J[3]; ++j[3]) { for (j[4] = 0; j[4] < J[4]; ++j[4]) { for (j[5] = 0; j[5] < J[5]; ++j[5]) { for (i = 0; i < ARRAYLEN(J); ++i) { D[i] = S[j[i]] + K[i] - R[i]; } if ((error = DifferSumSq(D, L, H) / HM) < least) { least = error; memcpy(O, j, sizeof(j)); } } } } } } } for (i = 0; i < 6; ++i) { N[i] += S[O[i]]; } } return lround(least); }
4,546
113
jart/cosmopolitan
false
cosmopolitan/dsp/core/c1331s.h
#ifndef COSMOPOLITAN_DSP_CORE_C1331S_H_ #define COSMOPOLITAN_DSP_CORE_C1331S_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * Byte sized kernel for resampling difference samples in half. * * @define (1*(a-128)+3*(a-128)+3*(a-128)+1*(a-128))/(1+3+3+1)+128 * @see C1331(), Y420CbCr2RgbScale() */ forceinline pureconst artificial signed char C1331S(signed char al, signed char bl, signed char cl, signed char dl) { short ax, bx; bx = bl; bx += cl; bx *= 3; ax = al; ax += dl; ax += bx; ax += 4; ax >>= 3; return ax; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_C1331S_H_ */
788
29
jart/cosmopolitan
false
cosmopolitan/dsp/core/unalaw.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" /** * Expands a-Law coded to audio sample. * * @param x is coded number masked to range [0..255] * @return 16-bit signed linear audio sample * @note generate lut to save 2 cycles * @see ITU G.711 */ int unalaw(int x) { int e, i, m; i = (x ^ 85) & 127; e = i >> 4; m = i & 15; if (e > 0) m += 16; m = (m << 4) + 8; if (e > 1) m = m << (e - 1); return x & 128 ? m : -m; }
2,257
39
jart/cosmopolitan
false
cosmopolitan/dsp/core/matmul3.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "libc/str/str.h" /** * Multiplies 3×3 matrices. */ void *matmul3(double R[restrict 3][3], const double A[3][3], const double B[3][3]) { int i, j, k; memset(R, 0, sizeof(double) * 3 * 3); for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { for (k = 0; k < 3; ++k) { R[i][j] += A[k][j] * B[i][k]; } } } return R; }
2,241
38
jart/cosmopolitan
false
cosmopolitan/dsp/core/scalevolume.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "libc/intrin/bits.h" #include "libc/intrin/safemacros.internal.h" #include "libc/limits.h" /** * Increases or decreases audio volume. * * @param 𝑝 is two-power w/ effective range [-15,15] */ void scalevolume(size_t n, int16_t pcm[n][8], int p) { /* TODO(jart): This isn't acceptable. */ size_t i, j; if (p > 0) { if (p > 15) p = 15; for (i = 0; i < n; ++i) { for (j = 0; j < 8; ++j) { pcm[i][j] = MIN(SHRT_MAX, MAX(SHRT_MIN, (int)((unsigned)pcm[i][j] << p))); } } } else if (p < 0) { p = -p; if (p > 15) p = 15; for (i = 0; i < n; ++i) { for (j = 0; j < 8; ++j) { pcm[i][j] = pcm[i][j] >> p; } } } }
2,570
50
jart/cosmopolitan
false
cosmopolitan/dsp/core/double2byte.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/mem/mem.h" void *double2byte(long n, const void *p, double weight, double bias) { long i; const double *src; unsigned char *dst; if ((dst = valloc(n * sizeof(unsigned char)))) { for (src = p, i = 0; i < n; ++i) { dst[i] = MIN(255, MAX(0, rint(src[i] * weight + bias))); } } return dst; }
2,250
35
jart/cosmopolitan
false
cosmopolitan/dsp/core/core.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += DSP_CORE DSP_CORE_ARTIFACTS += DSP_CORE_A DSP_CORE = $(DSP_CORE_A_DEPS) $(DSP_CORE_A) DSP_CORE_A = o/$(MODE)/dsp/core/core.a DSP_CORE_A_FILES := $(wildcard dsp/core/*) DSP_CORE_A_HDRS = $(filter %.h,$(DSP_CORE_A_FILES)) DSP_CORE_A_SRCS = $(filter %.c,$(DSP_CORE_A_FILES)) DSP_CORE_A_OBJS = $(DSP_CORE_A_SRCS:%.c=o/$(MODE)/%.o) DSP_CORE_A_CHECKS = \ $(DSP_CORE_A).pkg \ $(DSP_CORE_A_HDRS:%=o/$(MODE)/%.ok) DSP_CORE_A_DIRECTDEPS = \ LIBC_INTRIN \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_STR \ LIBC_TINYMATH \ LIBC_STUBS DSP_CORE_A_DEPS := \ $(call uniq,$(foreach x,$(DSP_CORE_A_DIRECTDEPS),$($(x)))) $(DSP_CORE_A): dsp/core/ \ $(DSP_CORE_A).pkg \ $(DSP_CORE_A_OBJS) $(DSP_CORE_A).pkg: \ $(DSP_CORE_A_OBJS) \ $(foreach x,$(DSP_CORE_A_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/dsp/core/dct.o \ o/$(MODE)/dsp/core/c1331.o \ o/$(MODE)/dsp/core/magikarp.o \ o/$(MODE)/dsp/core/c93654369.o \ o/$(MODE)/dsp/core/float2short.o \ o/$(MODE)/dsp/core/scalevolume.o: private \ OVERRIDE_CFLAGS += \ $(MATHEMATICAL) o/tiny/dsp/core/scalevolume.o: private \ OVERRIDE_CFLAGS += \ -Os o/$(MODE)/dsp/core/det3.o: private \ OVERRIDE_CFLAGS += \ -ffast-math DSP_CORE_LIBS = $(foreach x,$(DSP_CORE_ARTIFACTS),$($(x))) DSP_CORE_SRCS = $(foreach x,$(DSP_CORE_ARTIFACTS),$($(x)_SRCS)) DSP_CORE_HDRS = $(foreach x,$(DSP_CORE_ARTIFACTS),$($(x)_HDRS)) DSP_CORE_CHECKS = $(foreach x,$(DSP_CORE_ARTIFACTS),$($(x)_CHECKS)) DSP_CORE_OBJS = $(foreach x,$(DSP_CORE_ARTIFACTS),$($(x)_OBJS)) $(DSP_CORE_OBJS): $(BUILD_FILES) dsp/core/core.mk .PHONY: o/$(MODE)/dsp/core o/$(MODE)/dsp/core: $(DSP_CORE_CHECKS)
1,858
63
jart/cosmopolitan
false
cosmopolitan/dsp/core/vmatmul3.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" /** * Computes V×M. * * @see matvmul3() for noncommutative corollary */ void *vmatmul3(double R[restrict 3], const double V[3], const double M[3][3]) { R[0] = V[0] * M[0][0] + V[1] * M[1][0] + V[2] * M[2][0]; R[1] = V[0] * M[0][1] + V[1] * M[1][1] + V[2] * M[2][1]; R[2] = V[0] * M[0][2] + V[1] * M[1][2] + V[2] * M[2][2]; return R; }
2,212
32
jart/cosmopolitan
false
cosmopolitan/dsp/core/matvmul3.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" /** * Computes M×V. * * @see vmatmul3() for noncommutative corollary */ void *matvmul3(double R[restrict 3], const double M[3][3], const double V[3]) { R[0] = V[0] * M[0][0] + V[1] * M[0][1] + V[2] * M[0][2]; R[1] = V[0] * M[1][0] + V[1] * M[1][1] + V[2] * M[1][2]; R[2] = V[0] * M[2][0] + V[1] * M[2][1] + V[2] * M[2][2]; return R; }
2,212
32
jart/cosmopolitan
false
cosmopolitan/dsp/core/illumination.h
#ifndef COSMOPOLITAN_DSP_CORE_ILLUMINANT_H_ #define COSMOPOLITAN_DSP_CORE_ILLUMINANT_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ extern const double kBradford[3][3]; extern const double kIlluminantA[3]; extern const double kIlluminantAD10[3]; extern const double kIlluminantC[3]; extern const double kIlluminantCD10[3]; extern const double kIlluminantD50[3]; extern const double kIlluminantD50D10[3]; extern const double kIlluminantD55[3]; extern const double kIlluminantD55D10[3]; extern const double kIlluminantD65[3]; extern const double kIlluminantD65D10[3]; extern const double kIlluminantD75[3]; extern const double kIlluminantD75D10[3]; extern const double kIlluminantF2[3]; extern const double kIlluminantF2D10[3]; extern const double kIlluminantF7[3]; extern const double kIlluminantF7D10[3]; extern const double kIlluminantF11[3]; extern const double kIlluminantF11D10[3]; void *GetChromaticAdaptationMatrix(double[3][3], const double[3], const double[3]); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_ILLUMINANT_H_ */
1,140
32
jart/cosmopolitan
false
cosmopolitan/dsp/core/gamma.h
#ifndef COSMOPOLITAN_DSP_CORE_GAMMA_H_ #define COSMOPOLITAN_DSP_CORE_GAMMA_H_ #include "libc/math.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) #define COMPANDLUMA(X, ...) COMPANDLUMA_(X, __VA_ARGS__) #define COMPANDLUMA_(X, K1, K2, K3, K4) \ ((X) > (K3) / (K4) ? (1 + (K2)) * pow((X), 1 / (K1)) - (K2) : (X) * (K4)) #define UNCOMPANDLUMA(X, ...) UNCOMPANDLUMA_(X, __VA_ARGS__) #define UNCOMPANDLUMA_(X, K1, K2, K3, K4) \ ((X) > (K3) ? pow(1 / (1 + (K2)) * ((X) + (K2)), K1) : (X) / (K4)) #define COMPANDLUMA_SRGB_MAGNUM .055, .04045, 12.92 #define COMPANDLUMA_SRGB(X, G) COMPANDLUMA(X, G, COMPANDLUMA_SRGB_MAGNUM) #define UNCOMPANDLUMA_SRGB(X, G) UNCOMPANDLUMA(X, G, COMPANDLUMA_SRGB_MAGNUM) #define COMPANDLUMA_BT1886_MAGNUM 1 / .45, .099, .081, 4.5 #define COMPANDLUMA_BT1886(X) COMPANDLUMA(X, COMPANDLUMA_BT1886_MAGNUM) #define UNCOMPANDLUMA_BT1886(X) UNCOMPANDLUMA(X, COMPANDLUMA_BT1886_MAGNUM) #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_GAMMA_H_ */
1,004
24
jart/cosmopolitan
false
cosmopolitan/dsp/core/c121.h
#ifndef COSMOPOLITAN_DSP_CORE_C121_H_ #define COSMOPOLITAN_DSP_CORE_C121_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ forceinline pureconst artificial unsigned char C121(unsigned char al, unsigned char bl, unsigned char cl) { unsigned short ax, bx; ax = al; ax += bl; ax += bl; ax += cl; ax += 2; ax >>= 2; return ax; } COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_C121_H_ */
570
22
jart/cosmopolitan
false
cosmopolitan/dsp/core/kss8.h
#ifndef COSMOPOLITAN_DSP_CORE_KSS8_H_ #define COSMOPOLITAN_DSP_CORE_KSS8_H_ #include "libc/limits.h" #include "libc/macros.internal.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * Performs 16-bit scaled rounded saturated madd w/ eight coefficients or fewer. * * (Σᵢ₌₀₋₈𝑘ᵢ𝑥ᵢ + 2ᵐ⁻¹)/2ᵐ * * @note compiler struggles with this */ #define KSS8(M, K1, K2, K3, K4, K5, K6, K7, K8, X1, X2, X3, X4, X5, X6, X7, \ X8) \ ({ \ short x1, x2, x3, x4, x5, x6, x7, x8; \ x1 = X1, x2 = X2, x3 = X3, x4 = X4; \ x5 = X5, x6 = X6, x7 = X7, x8 = X8; \ x1 = MIN(SHRT_MAX, MAX(SHRT_MIN, x1 * K1)); \ x2 = MIN(SHRT_MAX, MAX(SHRT_MIN, x2 * K2)); \ x3 = MIN(SHRT_MAX, MAX(SHRT_MIN, x3 * K3)); \ x4 = MIN(SHRT_MAX, MAX(SHRT_MIN, x4 * K4)); \ x5 = MIN(SHRT_MAX, MAX(SHRT_MIN, x5 * K5)); \ x6 = MIN(SHRT_MAX, MAX(SHRT_MIN, x6 * K6)); \ x7 = MIN(SHRT_MAX, MAX(SHRT_MIN, x7 * K7)); \ x8 = MIN(SHRT_MAX, MAX(SHRT_MIN, x8 * K8)); \ x1 = MIN(SHRT_MAX, MAX(SHRT_MIN, x1 + x2)); \ x3 = MIN(SHRT_MAX, MAX(SHRT_MIN, x3 + x4)); \ x5 = MIN(SHRT_MAX, MAX(SHRT_MIN, x5 + x6)); \ x7 = MIN(SHRT_MAX, MAX(SHRT_MIN, x7 + x8)); \ x1 = MIN(SHRT_MAX, MAX(SHRT_MIN, x1 + x3)); \ x5 = MIN(SHRT_MAX, MAX(SHRT_MIN, x5 + x7)); \ x1 = MIN(SHRT_MAX, MAX(SHRT_MIN, x1 + x5)); \ if (M) { \ x1 += 1 << MAX(0, M - 1); \ x1 >>= M; \ } \ x1; \ }) #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_KSS8_H_ */
2,487
44
jart/cosmopolitan
false
cosmopolitan/dsp/core/float2short.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/limits.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/str/str.h" /** * Converts floating point audio samples to pulse code modulation. * * @param rdi is number of sample chunks * @param rsi points to aligned pcm [-32k,+32k] output memory * @param rdx points to aligned float32 [-1,+1] input memory */ void float2short(size_t n, short pcm16[n][8], const float binary32[n][8]) { size_t i, j; for (i = 0; i < n; ++i) { for (j = 0; j < 8; ++j) { pcm16[i][j] = MIN(SHRT_MAX, MAX(SHRT_MIN, floorf(binary32[i][j] * (SHRT_MAX + 1)))); } } }
2,449
40
jart/cosmopolitan
false
cosmopolitan/dsp/core/byte2double.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "libc/mem/mem.h" void *byte2double(long n, const void *p, double weight, double bias) { long i; double f, *dst; const unsigned char *src; if ((dst = valloc(n * sizeof(double)))) { for (src = p, i = 0; i < n; ++i) { f = src[i]; f -= bias; f *= 1 / weight; dst[i] = f; } } return dst; }
2,202
36
jart/cosmopolitan
false
cosmopolitan/dsp/core/differsumsq.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" /** * Computes Σ𝑥ᵢΣ𝑥ⱼΣ𝑥ₖΣ𝑥ₗΣ𝑥ₘΣ𝑥ₙ(δ₁𝑥ᵢ+δ₂𝑥ⱼ+δ₃𝑥ₖ+δ₄𝑥ₗ+δ₅𝑥ₘ+δ₆𝑥ₙ)² over 𝐿..𝐻 * * “As soon as an Analytical Engine exists, it will necessarily * guide the future course of the science. Whenever any result * is sought by its aid, the question will then arise — by what * course of calculation can these results be arrived at by the * machine in the shortest time? * * — Charles Babbage (Life of a Philosopher, 1864) * * @see itu.int/rec/R-REC-BT.601/ */ double DifferSumSq(const double D[static 6], double L, double H) { double T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T2, T20, T21, T22, T23, T24, T25, T26, T27, T3, T4, T5, T6, T7, T8, T9; T2 = H * H, T3 = (H * H * H), T4 = (H * H * H * H), T5 = (H * H * H * H * H), T6 = (H * H * H * H * H * H), T7 = -10 * H, T8 = (H * H * H * H * H * H * H), T9 = (L * L * L * L * L * L * L * L), T10 = (L * L * L * L * L * L * L), T11 = (L * L * L * L * L * L), T12 = (L * L * L * L * L), T13 = -45 * T2, T14 = (L * L * L * L), T15 = 180 * T3, T16 = 120 * T2, T17 = (L * L * L), T18 = L * L, T19 = 18 * T2, T20 = (H * H * H * H * H * H * H * H); T21 = 45 * T4; T22 = 3 * T9 + (-12 * H - 18) * T10 + (12 * T2 + 54 * H + 45) * T11 + (12 * T3 - T19 - 90 * H - 60) * T12 + (-30 * T4 - 90 * T3 + T13 + 60 * H + 45) * T14 + (12 * T5 + 90 * T4 + T15 + T16 - 18) * T17 + (12 * T6 + 18 * T5 - T21 - 120 * T3 - 90 * T2 - 18 * H + 3) * T18 + (-12 * T8 - 54 * T6 - 90 * T5 - 60 * T4 + T19 + 6 * H) * L + 3 * T20 + 18 * T8 + 45 * T6 + 60 * T5 + T21 + 18 * T3 + 3 * T2; T23 = 2 * T9 + (T7 - 13) * T10 + (20 * T2 + 55 * H + 36) * T11 + (-22 * T3 - 93 * T2 - 126 * H - 55) * T12 + (20 * T4 + 95 * T3 + 180 * T2 + 155 * H + 50) * T14 + (-22 * T5 - 95 * T4 - T15 - 190 * T2 - 110 * H - 27) * T17 + (20 * T6 + 93 * T5 + 180 * T4 + 190 * T3 + T16 + 45 * H + 8) * T18 + (-10 * T8 - 55 * T6 - 126 * T5 - 155 * T4 - 110 * T3 + T13 + T7 - 1) * L + 2 * T20 + 13 * T8 + 36 * T6 + 55 * T5 + 50 * T4 + 27 * T3 + 8 * T2 + H; T24 = T22 * D[3], T25 = T22 * D[2], T26 = T22 * D[1], T27 = T22 * D[0]; return (T23 * D[5] * D[5] + (T22 * D[4] + T24 + T25 + T26 + T27) * D[5] + T23 * D[4] * D[4] + (T24 + T25 + T26 + T27) * D[4] + T23 * D[3] * D[3] + (T25 + T26 + T27) * D[3] + T23 * D[2] * D[2] + (T26 + T27) * D[2] + T23 * D[1] * D[1] + T22 * D[0] * D[1] + T23 * D[0] * D[0]) / 6; }
4,469
67
jart/cosmopolitan
false
cosmopolitan/dsp/core/getintegercoefficients8.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "dsp/core/q.h" #include "libc/dce.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/str/str.h" /** * Same as GetIntegerCoefficients() but with eight terms. * @cost ~3ms */ long GetIntegerCoefficients8(long N[static 8], const double C[static 8], long M, long L, long H) { int i; int j[8], J[8]; int O[8] = {0}; int S[3] = {0, -1, +1}; double R[8], K[8], D[8], Z, least, error; least = 1; Z = 1L << M; for (i = 0; i < ARRAYLEN(J); ++i) { least *= H - L; if (fabs(C[i]) > DBL_MIN) { J[i] = ARRAYLEN(S); R[i] = C[i] * Z; K[i] = rint(R[i]); N[i] = K[i]; } else { J[i] = 1; R[i] = 0; K[i] = 0; N[i] = 0; } } if (least > 1) { for (j[0] = 0; j[0] < J[0]; ++j[0]) { for (j[1] = 0; j[1] < J[1]; ++j[1]) { for (j[2] = 0; j[2] < J[2]; ++j[2]) { for (j[3] = 0; j[3] < J[3]; ++j[3]) { for (j[4] = 0; j[4] < J[4]; ++j[4]) { for (j[5] = 0; j[5] < J[5]; ++j[5]) { for (j[6] = 0; j[6] < J[6]; ++j[6]) { for (j[7] = 0; j[7] < J[7]; ++j[7]) { for (i = 0; i < ARRAYLEN(J); ++i) { D[i] = S[j[i]] + K[i] - R[i]; } if ((error = DifferSumSq8(D, L, H) / Z) < least) { least = error; memcpy(O, j, sizeof(j)); } } } } } } } } } for (i = 0; i < 8; ++i) { N[i] += S[O[i]]; } } return lround(least); }
3,525
83
jart/cosmopolitan
false
cosmopolitan/dsp/core/c11.h
#ifndef COSMOPOLITAN_DSP_CORE_C11_H_ #define COSMOPOLITAN_DSP_CORE_C11_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * Fixed-point 8-bit rounded mean kernel. * * @define (a + b) / 2 */ static inline pureconst artificial unsigned char C11(unsigned char al, unsigned char bl) { short ax; ax = al; ax += bl; ax += 1; ax /= 2; al = ax; return al; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_C11_H_ */
506
23
jart/cosmopolitan
false
cosmopolitan/dsp/core/core.h
#ifndef COSMOPOLITAN_DSP_CORE_CORE_H_ #define COSMOPOLITAN_DSP_CORE_CORE_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ int alaw(int); int unalaw(int); int mulaw(int); int unmulaw(int); void *double2byte(long, const void *, double, double) vallocesque; void *byte2double(long, const void *, double, double) vallocesque; void *dct(float[8][8], float, float, float, float, float); void *dctjpeg(float[8][8]); double det3(const double[3][3]) nosideeffect; void *inv3(double[restrict 3][3], const double[restrict 3][3], double); void *matmul3(double[restrict 3][3], const double[3][3], const double[3][3]); void *vmatmul3(double[restrict 3], const double[3], const double[3][3]); void *matvmul3(double[restrict 3], const double[3][3], const double[3]); double rgb2stdtv(double) pureconst; double rgb2lintv(double) pureconst; double rgb2stdpc(double, double) pureconst; double rgb2linpc(double, double) pureconst; double tv2pcgamma(double, double) pureconst; #ifndef __cplusplus void sad16x8n(size_t n, short[n][8], const short[n][8]); void float2short(size_t n, short[n][8], const float[n][8]); void scalevolume(size_t n, int16_t[n][8], int); #endif COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_CORE_H_ */
1,275
34
jart/cosmopolitan
false
cosmopolitan/dsp/core/sad16x8n.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "libc/limits.h" #include "libc/macros.internal.h" #include "third_party/aarch64/arm_neon.h" #include "third_party/intel/emmintrin.internal.h" /** * Mixes audio. * * This function performs saturated addition on an array of shorts. * * @param x needs to be 16-byte aligned * @param y needs to be 16-byte aligned */ void sad16x8n(size_t n, short x[n][8], const short y[n][8]) { size_t i, j; for (i = 0; i < n; ++i) { #ifdef __x86_64__ *(__m128i *)x[i] = _mm_adds_epi16(*(__m128i *)x[i], *(__m128i *)y[i]); #elif defined(__aarch64__) *(int16x4_t *)x[i] = vqadd_s16(*(int16x4_t *)x[i], *(int16x4_t *)y[i]); #else for (j = 0; j < 8; ++j) { x[i][j] = MIN(MAX(x[i][j] + y[i][j], INT16_MIN), INT16_MAX); } #endif } }
2,616
47
jart/cosmopolitan
false
cosmopolitan/dsp/core/c161s.h
#ifndef COSMOPOLITAN_DSP_CORE_C161S_H_ #define COSMOPOLITAN_DSP_CORE_C161S_H_ #include "dsp/core/c161.h" #include "libc/macros.internal.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline pureconst artificial signed char C161S(signed char al, signed char bl, signed char cl) { short ax, bx, cx; ax = al; bx = bl; cx = cl; ax *= -1 * EXTRA_SHARP; bx *= 6 * EXTRA_SHARP; cx *= -1 * EXTRA_SHARP; ax += bx; ax += cx; ax += 2 * EXTRA_SHARP; ax /= 4 * EXTRA_SHARP; al = MIN(112, MAX(-112, ax)); return al; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_C161S_H_ */
724
27
jart/cosmopolitan
false
cosmopolitan/dsp/core/det3.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #define LEIBNIZ_FORMULA(a, b, c, d, e, f, g, h, i) \ (a * e * i + b * f * g + c * d * h - c * e * g - b * d * i - a * f * h) /** * Computes determinant of 3×3 matrix. * i.e. how much space is inside the cube * * @param 𝐀 is input matrix * @param 𝑑 is det(𝐀) * @return |𝐀| or 0 if degenerate */ double det3(const double A[3][3]) { return LEIBNIZ_FORMULA(A[0][0], A[0][1], A[0][2], A[1][0], A[1][1], A[1][2], A[2][0], A[2][1], A[2][2]); }
2,351
36
jart/cosmopolitan
false
cosmopolitan/dsp/core/inv3.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "libc/math.h" #include "libc/str/str.h" /** * Computes 𝐀⁻¹ inverted 3×3 matrix, if it exists. * * @param 𝐁 is destination memory * @param 𝐀 is input matrix, which can't overlap 𝐁 * @param 𝑑 is |𝐀| the determinant scalar or 0 if degenerate * @return 𝐀⁻¹ stored inside 𝐁 or NaNs if 𝑑=0 * @define 𝐀⁻¹=𝐁 such that 𝐀×𝐁=𝐁×𝐀=𝐈ₙ * @see det3() */ void *inv3(double B[restrict 3][3], const double A[restrict 3][3], double d) { d = d ? 1 / d : NAN; B[0][0] = (A[1][1] * A[2][2] - A[2][1] * A[1][2]) * d; B[0][1] = (A[2][1] * A[0][2] - A[0][1] * A[2][2]) * d; B[0][2] = (A[0][1] * A[1][2] - A[1][1] * A[0][2]) * d; B[1][0] = (A[2][0] * A[1][2] - A[1][0] * A[2][2]) * d; B[1][1] = (A[0][0] * A[2][2] - A[2][0] * A[0][2]) * d; B[1][2] = (A[1][0] * A[0][2] - A[0][0] * A[1][2]) * d; B[2][0] = (A[1][0] * A[2][1] - A[2][0] * A[1][1]) * d; B[2][1] = (A[2][0] * A[0][1] - A[0][0] * A[2][1]) * d; B[2][2] = (A[0][0] * A[1][1] - A[1][0] * A[0][1]) * d; return B; }
2,911
46
jart/cosmopolitan
false
cosmopolitan/dsp/core/q.h
#ifndef COSMOPOLITAN_DSP_CORE_Q_H_ #define COSMOPOLITAN_DSP_CORE_Q_H_ #include "libc/limits.h" #include "libc/macros.internal.h" #include "libc/math.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * @fileoverview Fixed point arithmetic macros. * @see en.wikipedia.org/wiki/Q_(number_format) */ #define F2Q(Q, I) MIN((1 << Q) - 1, roundf((I) * (1.f * ((1 << Q) - 1)))) #define Q2F(Q, I) ((I) * (1.f / ((1 << Q) - 1))) #define QRS(Q, X) (((X) + (1 << (Q - 1))) >> Q) #define LQRS(Q, X) (((X) + (1L << (Q - 1))) >> Q) double DifferSumSq(const double[static 6], double, double); double DifferSumSq8(const double[static 8], double, double); long GetIntegerCoefficients(long[static 6], const double[static 6], long, long, long); long GetIntegerCoefficients8(long[static 8], const double[static 8], long, long, long); #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_Q_H_ */
963
28
jart/cosmopolitan
false
cosmopolitan/dsp/core/half.h
#ifndef COSMOPOLITAN_DSP_CORE_HALF_H_ #define COSMOPOLITAN_DSP_CORE_HALF_H_ #include "libc/macros.internal.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * Divides integer in half w/ rounding. */ #define HALF(X) (((X) + 1) / 2) #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_HALF_H_ */
320
13
jart/cosmopolitan
false
cosmopolitan/dsp/core/c1331.h
#ifndef COSMOPOLITAN_DSP_CORE_C1331_H_ #define COSMOPOLITAN_DSP_CORE_C1331_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * Byte sized kernel for resampling memory in half. * * @define (1*𝑎 + 3*𝑏 + 3*𝑐 + 1*𝑑) / (1 + 3 + 3 + 1) * @see C161() afterward for superior sin(𝑥)/𝑥 * @limit [0,255] → [0..2,044] → [0..255] */ forceinline pureconst artificial unsigned char C1331(unsigned char al, unsigned char bl, unsigned char cl, unsigned char dl) { short ax, bx; bx = bl; bx += cl; bx *= 3; ax = al; ax += dl; ax += bx; ax += 4; ax >>= 3; al = ax; return al; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_C1331_H_ */
856
31
jart/cosmopolitan
false
cosmopolitan/dsp/core/dct.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #define DCT(A, B, C, D, E, F, G, H, T, C0, C1, C2, C3, C4) \ do { \ T z1, z2, z3, z4, z5, z11, z13; \ T t0, t1, t2, t3, t4, t5, t6, t7, t10, t11, t12, t13; \ t0 = A + H; \ t7 = A - H; \ t1 = B + G; \ t6 = B - G; \ t2 = C + F; \ t5 = C - F; \ t3 = D + E; \ t4 = D - E; \ t10 = t0 + t3; \ t13 = t0 - t3; \ t11 = t1 + t2; \ t12 = t1 - t2; \ A = t10 + t11; \ E = t10 - t11; \ z1 = (t12 + t13) * C0; \ C = t13 + z1; \ G = t13 - z1; \ t10 = t4 + t5; \ t11 = t5 + t6; \ t12 = t6 + t7; \ z5 = (t10 - t12) * C1; \ z2 = t10 * C2 + z5; \ z4 = t12 * C3 + z5; \ z3 = t11 * C4; \ z11 = t7 + z3; \ z13 = t7 - z3; \ F = z13 + z2; \ D = z13 - z2; \ B = z11 + z4; \ H = z11 - z4; \ } while (0) /** * Performs float forward discrete cosine transform. * * This takes a tiny block of image data and shoves the information it * represents into the top left corner. It can be reversed using idct(). * It matters because iterating the result in a serpentine pattern makes * run-length delta encoding super effective. Furthermore, data downward * rightly may be divided away for lossy compression. * * @cost ~100ns */ void *dct(float M[8][8], float c0, float c1, float c2, float c3, float c4) { unsigned y, x; for (y = 0; y < 8; ++y) { DCT(M[y][0], M[y][1], M[y][2], M[y][3], M[y][4], M[y][5], M[y][6], M[y][7], float, c0, c1, c2, c3, c4); } for (x = 0; x < 8; ++x) { DCT(M[0][x], M[1][x], M[2][x], M[3][x], M[4][x], M[5][x], M[6][x], M[7][x], float, c0, c1, c2, c3, c4); } return M; } void *dctjpeg(float M[8][8]) { return dct(M, .707106781f, .382683433f, .541196100f, 1.306562965f, .707106781f); }
5,016
85
jart/cosmopolitan
false
cosmopolitan/dsp/core/alaw.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" /** * Compresses 16-bit signed linear audio sample with a-Law. * * @param x is clamped to 16-bits of which the top 14 are considered * @return coded number in range [0..255] * @see ITU G.711 */ int alaw(int x) { int a, b, e, i; if ((a = x) < 0) a = ~a; a >>= 4; if (a > 15) { if ((i = a >> 5)) { b = (__builtin_clz(i) ^ 31) + 1; a >>= b; a -= 16; a += (b + 1) << 4; } else { e = 1; a -= 16; a += 16; } } if (x >= 0) a |= 128; return a ^ 85; }
2,381
47
jart/cosmopolitan
false
cosmopolitan/dsp/core/c331.h
#ifndef COSMOPOLITAN_DSP_CORE_C331_H_ #define COSMOPOLITAN_DSP_CORE_C331_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ /** * Fixed-point 8-bit magic edge resampling kernel. * * @define (3*a + 3*b + 1*c) / 7 * @see C1331() */ static inline pureconst artificial unsigned char C331(unsigned char al, unsigned char bl, unsigned char cl) { unsigned eax, ebx, ecx; eax = al; ebx = bl; ecx = cl; eax += ebx; eax *= 3 * 2350; ecx *= 1 * 2350; eax += ecx; eax >>= 14; al = eax; return al; } COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_C331_H_ */
746
31
jart/cosmopolitan
false
cosmopolitan/dsp/core/c121s.h
#ifndef COSMOPOLITAN_DSP_CORE_C121S_H_ #define COSMOPOLITAN_DSP_CORE_C121S_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) forceinline pureconst artificial signed char C121S(signed char al, signed char bl, signed char cl) { short ax, bx; ax = al; ax += bl; ax += bl; ax += cl; ax += 2; ax >>= 2; return ax; } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_C121S_H_ */
513
20
jart/cosmopolitan
false
cosmopolitan/dsp/core/illumination.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "dsp/core/illumination.h" #include "libc/str/str.h" const double kIlluminantA[3] = {1.0985, 1, 0.35585}; const double kIlluminantAD10[3] = {1.11144, 1, 0.35200}; const double kIlluminantC[3] = {0.98074, 1, 1.18232}; const double kIlluminantCD10[3] = {0.97285, 1, 1.16145}; const double kIlluminantD50[3] = {0.96422, 1, 0.82521}; const double kIlluminantD50D10[3] = {0.9672, 1, 0.81427}; const double kIlluminantD55[3] = {0.95682, 1, 0.92149}; const double kIlluminantD55D10[3] = {0.95799, 1, 0.90926}; const double kIlluminantD65[3] = {0.95047, 1, 1.08883}; const double kIlluminantD65D10[3] = {0.94811, 1, 1.07304}; const double kIlluminantD75[3] = {0.94972, 1, 1.22638}; const double kIlluminantD75D10[3] = {0.94416, 1, 1.20641}; const double kIlluminantF2[3] = {0.99187, 1, 0.67395}; const double kIlluminantF2D10[3] = {1.0328, 1, 0.69026}; const double kIlluminantF7[3] = {0.95044, 1, 1.08755}; const double kIlluminantF7D10[3] = {0.95792, 1, 1.07687}; const double kIlluminantF11[3] = {1.00966, 1, 0.6437}; const double kIlluminantF11D10[3] = {1.03866, 1, 0.65627}; /** * System of equations used for changing illumination. * * @see brucelindbloom.com/Eqn_ChromAdapt.html “The Bradford method is * the newest of the three methods, and is considered by most * experts to be the best of them. This is the method used in Adobe * Photoshop. A related article comparing the chromatic adaptation * algorithms may be found here.” ─Quoth Bruce Lindbloom */ const double kBradford[3][3] = { {0.8951, 0.2664, -.1614}, {-.7502, 1.7135, 0.0367}, {0.0389, -.0685, 1.0296}, }; /** * Computes lightbulb changing coefficients. * * @param R will store result * @param S has intended input illuminant primaries * @param D has desired output illuminant primaries * @return R * @see brucelindbloom.com/Eqn_ChromAdapt.html * @see fluxometer.com/rainbow/ */ void *GetChromaticAdaptationMatrix(double R[3][3], const double S[3], const double D[3]) { double M[3][3], T[3][3], U[3][3], V[3], W[3]; matvmul3(V, kBradford, S); matvmul3(W, kBradford, D); memset(M, 0, sizeof(M)); M[0][0] = W[0] / V[0]; M[1][1] = W[1] / V[1]; M[2][2] = W[2] / V[2]; return matmul3(R, matmul3(T, kBradford, M), inv3(U, kBradford, det3(kBradford))); }
4,204
79
jart/cosmopolitan
false
cosmopolitan/dsp/core/differsumsq8.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "dsp/core/q.h" /** * Computes Σ𝑥ᵢΣ𝑥ⱼΣ𝑥ₖΣ𝑥ₗΣ𝑥ₘΣ𝑥ₙΣ𝑥ₚΣ𝑥ₛ(δ₁𝑥ᵢ+δ₂𝑥ⱼ+δ₃𝑥ₖ+δ₄𝑥ₗ+δ₅𝑥ₘ+δ₆𝑥ₙ+δₚ𝑥ₚ+δₛ𝑥ₛ)² * * “As soon as an Analytical Engine exists, it will necessarily * guide the future course of the science. Whenever any result * is sought by its aid, the question will then arise — by what * course of calculation can these results be arrived at by the * machine in the shortest time? * * — Charles Babbage (Life of a Philosopher, 1864) * */ double DifferSumSq8(const double D[static 8], double L, double H) { double T10, T100, T101, T102, T103, T104, T105, T106, T107, T108, T109, T11; double T110, T111, T112, T113, T114, T115, T116, T117, T118, T119, T12, T120; double T121, T122, T123, T124, T125, T126, T127, T128, T129, T13, T130, T131; double T132, T133, T134, T135, T136, T137, T138, T139, T14, T140, T141, T142; double T143, T144, T145, T146, T147, T148, T149, T15, T150, T151, T152, T153; double T154, T155, T156, T157, T158, T159, T16, T160, T161, T162, T163, T164; double T165, T166, T167, T168, T169, T17, T170, T171, T172, T173, T174, T175; double T176, T177, T178, T179, T18, T180, T181, T182, T183, T184, T185, T186; double T187, T188, T189, T19, T190, T191, T192, T193, T194, T195, T196, T197; double T198, T199, T2, T20, T200, T201, T202, T203, T204, T205, T206, T207; double T208, T209, T21, T210, T211, T212, T213, T214, T215, T216, T217, T218; double T219, T22, T220, T221, T222, T223, T224, T225, T226, T227, T228, T229; double T230, T231, T232, T233, T234, T235, T236, T237, T238, T239, T24, T240; double T241, T242, T243, T244, T245, T246, T247, T248, T249, T25, T250, T251; double T252, T253, T254, T255, T256, T257, T258, T259, T26, T260, T261, T262; double T263, T264, T265, T266, T267, T268, T269, T27, T270, T271, T272, T273; double T274, T275, T276, T277, T278, T279, T28, T280, T281, T282, T283, T284; double T285, T286, T287, T288, T289, T29, T290, T291, T292, T293, T294, T295; double T296, T297, T298, T299, T3, T30, T300, T301, T302, T303, T304, T305; double T307, T308, T309, T31, T310, T311, T312, T313, T314, T315, T316, T317; double T318, T319, T32, T320, T321, T322, T323, T324, T325, T326, T327, T328; double T329, T33, T330, T331, T332, T333, T334, T335, T336, T337, T338, T339; double T34, T340, T341, T342, T343, T344, T345, T346, T347, T348, T349, T35; double T350, T351, T352, T353, T354, T355, T356, T357, T358, T359, T36, T360; double T361, T362, T363, T364, T365, T366, T367, T368, T369, T37, T370, T371; double T372, T373, T374, T375, T376, T377, T378, T379, T38, T380, T381, T382; double T383, T384, T385, T386, T387, T388, T389, T39, T390, T391, T392, T393; double T394, T395, T396, T397, T398, T399, T4, T40, T400, T401, T402, T403; double T405, T406, T407, T41, T42, T43, T44, T45, T46, T47, T48, T49, T5, T50; double T51, T52, T53, T54, T55, T56, T57, T58, T59, T6, T60, T61, T62, T63; double T65, T66, T67, T68, T69, T7, T70, T71, T72, T73, T74, T75, T76, T77; double T79, T8, T80, T81, T82, T83, T84, T85, T86, T87, T88, T89, T9, T90; double T92, T93, T94, T95, T96, T97, T98, T99, T23, T306, T91, T78, T64, T404; T2 = 4 * D[5], T3 = 3 * D[4], T4 = 3 * D[3], T5 = 3 * D[2], T6 = 3 * D[1], T7 = 3 * D[0], T8 = D[7] * D[7], T9 = D[6] * D[6], T10 = -28 * D[5], T11 = -18 * D[4], T12 = -18 * D[3], T13 = -18 * D[2], T14 = -18 * D[1], T15 = -18 * D[0], T16 = D[5] * D[5], T17 = D[4] * D[4], T18 = D[3] * D[3], T19 = D[2] * D[2], T20 = D[1] * D[1], T21 = D[0] * D[0], T22 = -34 * D[5], T23 = -24 * D[4], T24 = -24 * D[3], T25 = -24 * D[2], T26 = -24 * D[1], T27 = -24 * D[0], T28 = 84 * D[5], T29 = 39 * D[4], T30 = 39 * D[3], T31 = 39 * D[2], T32 = 39 * D[1], T33 = 39 * D[0], T34 = 210 * D[5], T35 = 120 * D[4], T36 = 120 * D[3], T37 = 120 * D[2], T38 = 120 * D[1], T39 = 120 * D[0], T40 = 128 * D[5], T41 = 84 * D[4], T42 = 84 * D[3], T43 = 84 * D[2], T44 = 84 * D[1], T45 = 84 * D[0]; T46 = -144 * D[5], T47 = (T23 + T24 + T25 + T26 + T27) * D[5], T48 = (T24 + T25 + T26 + T27) * D[4], T49 = (T25 + T26 + T27) * D[3], T50 = (T26 + T27) * D[2], T51 = -24 * D[0] * D[1], T52 = -552 * D[5], T53 = -192 * D[4], T54 = -192 * D[3], T55 = -192 * D[2], T56 = -192 * D[1], T57 = -192 * D[0], T58 = H * H, T59 = -688 * D[5], T60 = -336 * D[4], T61 = -336 * D[3], T62 = -336 * D[2], T63 = -336 * D[1], T64 = -336 * D[0], T65 = -280 * D[5], T66 = -168 * D[4], T67 = -168 * D[3], T68 = -168 * D[2], T69 = -168 * D[1], T70 = -168 * D[0], T71 = 168 * D[5], T72 = -42 * D[4], T73 = -42 * D[3], T74 = -42 * D[2], T75 = -42 * D[1], T76 = -42 * D[0], T77 = (H * H * H), T78 = 336 * D[4], T79 = 336 * D[3], T80 = 336 * D[2], T81 = 336 * D[1], T82 = 336 * D[0], T83 = 1568 * D[5], T84 = 336 * D[0] * D[1], T85 = 1288 * D[5], T86 = 504 * D[4], T87 = 504 * D[3], T88 = 504 * D[2], T89 = 504 * D[1], T90 = 504 * D[0], T91 = 392 * D[5], T92 = 210 * D[4], T93 = 210 * D[3], T94 = 210 * D[2], T95 = 210 * D[1], T96 = 210 * D[0], T97 = 84 * T8, T98 = 168 * D[6], T99 = 84 * T9, T100 = 84 * T16, T101 = (T41 + T42 + T43 + T44 + T45) * D[5], T102 = 84 * T17, T103 = (T42 + T43 + T44 + T45) * D[4], T104 = 84 * T18, T105 = (T43 + T44 + T45) * D[3], T106 = 84 * T19, T107 = (T44 + T45) * D[2], T108 = 84 * T20, T109 = 84 * D[0] * D[1], T110 = 84 * T21, T111 = -924 * D[5], T112 = (T78 + T79 + T80 + T81 + T82) * D[5], T113 = (T79 + T80 + T81 + T82) * D[4], T114 = (T80 + T81 + T82) * D[3], T115 = (T81 + T82) * D[2], T116 = (H * H * H * H), T117 = -2128 * D[5], T118 = -2520 * D[5], T119 = (T66 + T67 + T68 + T69 + T70) * D[5], T120 = (T67 + T68 + T69 + T70) * D[4], T121 = (T68 + T69 + T70) * D[3], T122 = (T69 + T70) * D[2], T123 = -168 * D[0] * D[1], T124 = -1512 * D[5], T125 = -420 * D[4], T126 = -420 * D[3], T127 = -420 * D[2], T128 = -420 * D[1], T129 = -420 * D[0], T130 = -364 * D[5]; T131 = T97 + (T98 + T71 + T72 + T73 + T74 + T75 + T76) * D[7] + T99 + (T71 + T72 + T73 + T74 + T75 + T76) * D[6] + T100 + (T72 + T73 + T74 + T75 + T76) * D[5] + T102 + (T73 + T74 + T75 + T76) * D[4] + T104 + (T74 + T75 + T76) * D[3] + T106 + (T75 + T76) * D[2] + T108 - 42 * D[0] * D[1] + T110; T132 = 462 * T8, T133 = 924 * D[6], T134 = 924 * D[5], T135 = 462 * T9, T136 = 462 * T16, T137 = (T60 + T61 + T62 + T63 + T64) * D[5], T138 = 462 * T17, T139 = (T61 + T62 + T63 + T64) * D[4], T140 = 462 * T18, T141 = (T62 + T63 + T64) * D[3], T142 = 462 * T19, T143 = (T63 + T64) * D[2], T144 = 462 * T20, T145 = 462 * T21, T146 = (H * H * H * H * H), T147 = 2240 * D[5], T148 = -840 * D[4], T149 = -840 * D[3], T150 = -840 * D[2], T151 = -840 * D[1], T152 = -840 * D[0], T153 = 3080 * D[5], T154 = (T148 + T149 + T150 + T151 + T152) * D[5], T155 = (T149 + T150 + T151 + T152) * D[4], T156 = (T150 + T151 + T152) * D[3], T157 = (T151 + T152) * D[2], T158 = -840 * D[0] * D[1], T159 = 1260 * T8, T160 = 2520 * D[6], T161 = 2520 * D[5], T162 = 1260 * T9, T163 = 1260 * T16, T164 = 1260 * T17, T165 = 1260 * T18, T166 = 1260 * T19, T167 = 1260 * T20, T168 = 210 * D[0] * D[1], T169 = 1260 * T21, T170 = 168 * D[4], T171 = 168 * D[3], T172 = 168 * D[2], T173 = 168 * D[1], T174 = 168 * D[0], T175 = 1148 * D[5], T176 = 168 * D[0] * D[1], T177 = 224 * D[5]; T178 = -72 * T8 + (-144 * D[6] + T46 + T23 + T24 + T25 + T26 + T27) * D[7] - 72 * T9 + (T46 + T23 + T24 + T25 + T26 + T27) * D[6] - 72 * T16 + T47 - 72 * T17 + T48 - 72 * T18 + T49 - 72 * T19 + T50 - 72 * T20 + T51 - 72 * T21; T179 = 420 * T8, T180 = 840 * D[6], T181 = 840 * D[5], T182 = 420 * T9, T183 = 840 * D[5] * D[6], T184 = 420 * T16, T185 = 420 * T17, T186 = 420 * T18, T187 = 420 * T19, T188 = 420 * T20, T189 = 420 * T21, T190 = (H * H * H * H * H * H); T191 = -1064 * T8 + (-2128 * D[6] + T117 + T78 + T79 + T80 + T81 + T82) * D[7] - 1064 * T9 + (T117 + T78 + T79 + T80 + T81 + T82) * D[6] - 1064 * T16 + T112 - 1064 * T17 + T113 - 1064 * T18 + T114 - 1064 * T19 + T115 - 1064 * T20 + T84 - 1064 * T21; T192 = 1540 * T8, T193 = 3080 * D[6], T194 = 840 * D[4], T195 = 840 * D[3], T196 = 840 * D[2], T197 = 840 * D[1], T198 = 840 * D[0], T199 = 1540 * T9, T200 = 1540 * T16, T201 = 1540 * T17, T202 = 1540 * T18, T203 = 1540 * T19, T204 = 1540 * T20, T205 = 840 * D[0] * D[1], T206 = 1540 * T21, T207 = -2800 * D[5], T208 = (T194 + T195 + T196 + T197 + T198) * D[5], T209 = (T195 + T196 + T197 + T198) * D[4], T210 = (T196 + T197 + T198) * D[3], T211 = (T197 + T198) * D[2], T212 = -1624 * D[5], T213 = -88 * D[5]; T214 = 42 * T8 + (84 * D[6] + T28 + T29 + T30 + T31 + T32 + T33) * D[7] + 42 * T9 + (T28 + T29 + T30 + T31 + T32 + T33) * D[6] + 42 * T16 + (T29 + T30 + T31 + T32 + T33) * D[5] + 42 * T17 + (T30 + T31 + T32 + T33) * D[4] + 42 * T18 + (T31 + T32 + T33) * D[3] + 42 * T19 + (T32 + T33) * D[2] + 42 * T20 + 39 * D[0] * D[1] + 42 * T21; T215 = 276 * T8, T216 = 552 * D[6], T217 = 552 * D[5], T218 = 192 * D[4], T219 = 192 * D[3], T220 = 192 * D[2], T221 = 192 * D[1], T222 = 192 * D[0], T223 = 276 * T9, T224 = 276 * T16, T225 = 276 * T17, T226 = 276 * T18, T227 = 276 * T19, T228 = 276 * T20, T229 = 192 * D[0] * D[1], T230 = 276 * T21, T231 = (H * H * H * H * H * H * H); T232 = 784 * T8 + (1568 * D[6] + T83 + T78 + T79 + T80 + T81 + T82) * D[7] + 784 * T9 + (T83 + T78 + T79 + T80 + T81 + T82) * D[6] + 784 * T16 + T112 + 784 * T17 + T113 + 784 * T18 + T114 + 784 * T19 + T115 + 784 * T20 + T84 + 784 * T21; T233 = (T170 + T171 + T172 + T173 + T174) * D[5]; T234 = (T171 + T172 + T173 + T174) * D[4]; T235 = (T172 + T173 + T174) * D[3]; T236 = (T173 + T174) * D[2]; T237 = T159 + (T160 + T161 - T92 - T93 - T94 - T95 - T96) * D[7] + T162 + (T161 - T92 - T93 - T94 - T95 - T96) * D[6] + T163 + (-T92 - T93 - T94 - T95 - T96) * D[5] + T164 + (-T93 - T94 - T95 - T96) * D[4] + T165 + (-T94 - T95 - T96) * D[3] + T166 + (-T95 - T96) * D[2] + T167 - T168 + T169; T238 = 812 * T8, T239 = 1624 * D[6], T240 = 1624 * D[5], T241 = 812 * T9, T242 = 812 * T16, T243 = 812 * T17, T244 = 812 * T18, T245 = 812 * T19, T246 = 812 * T20, T247 = 812 * T21, T248 = 672 * D[5], T249 = 20 * D[5], T250 = (T3 + T4 + T5 + T6 + T7) * D[5], T251 = (T4 + T5 + T6 + T7) * D[4], T252 = (T5 + T6 + T7) * D[3], T253 = (T6 + T7) * D[2], T254 = 3 * D[0] * D[1]; T255 = -14 * T8 + (-28 * D[6] + T10 + T11 + T12 + T13 + T14 + T15) * D[7] - 14 * T9 + (T10 + T11 + T12 + T13 + T14 + T15) * D[6] - 14 * T16 + (T11 + T12 + T13 + T14 + T15) * D[5] - 14 * T17 + (T12 + T13 + T14 + T15) * D[4] - 14 * T18 + (T13 + T14 + T15) * D[3] - 14 * T19 + (T14 + T15) * D[2] - 14 * T20 - 18 * D[0] * D[1] - 14 * T21; T256 = 105 * T8, T257 = 210 * D[6], T258 = 105 * T9, T259 = 105 * T16, T260 = 105 * T17, T261 = 105 * T18, T262 = 105 * T19, T263 = 105 * T20, T264 = 120 * D[0] * D[1], T265 = 105 * T21, T266 = (H * H * H * H * H * H * H * H); T267 = -344 * T8 + (-688 * D[6] + T59 + T60 + T61 + T62 + T63 + T64) * D[7] - 344 * T9 + (T59 + T60 + T61 + T62 + T63 + T64) * D[6] - 344 * T16 + T137 - 344 * T17 + T139 - 344 * T18 + T141 - 344 * T19 + T143 - 344 * T20 - T84 - 344 * T21; T268 = 644 * T8, T269 = 1288 * D[6], T270 = 644 * T9, T271 = 644 * T16, T272 = 644 * T17, T273 = 644 * T18, T274 = 644 * T19, T275 = 644 * T20, T276 = 504 * D[0] * D[1], T277 = 644 * T21; T278 = -756 * T8 + (-1512 * D[6] + T124 + T125 + T126 + T127 + T128 + T129) * D[7] - 756 * T9 + (T124 + T125 + T126 + T127 + T128 + T129) * D[6] - 756 * T16 + (T125 + T126 + T127 + T128 + T129) * D[5] - 756 * T17 + (T126 + T127 + T128 + T129) * D[4] - 756 * T18 + (T127 + T128 + T129) * D[3] - 756 * T19 + (T128 + T129) * D[2] - 756 * T20 - 420 * D[0] * D[1] - 756 * T21; T279 = 574 * T8, T280 = 1148 * D[6], T281 = 574 * T9, T282 = 574 * T16, T283 = 574 * T17, T284 = 574 * T18, T285 = 574 * T19, T286 = 574 * T20, T287 = 574 * T21; T288 = -280 * T8 + (-560 * D[6] - 560 * D[5]) * D[7] - 280 * T9 - 560 * D[5] * D[6] - 280 * T16 - 280 * T17 - 280 * T18 - 280 * T19 - 280 * T20 - 280 * T21; T289 = 24 * D[4], T290 = 24 * D[3], T291 = 24 * D[2], T292 = 24 * D[1], T293 = 24 * D[0], T294 = 24 * D[0] * D[1], T295 = -14 * T8, T296 = -28 * D[6], T297 = -14 * T9, T298 = 6 * D[4], T299 = 6 * D[3], T300 = 6 * D[2], T301 = 6 * D[1], T302 = 6 * D[0], T303 = -14 * T16, T304 = -14 * T17, T305 = -14 * T18, T306 = -14 * T19, T307 = -14 * T20, T308 = -14 * T21; T309 = 2 * T8 + (4 * D[6] + T2 + T3 + T4 + T5 + T6 + T7) * D[7] + 2 * T9 + (T2 + T3 + T4 + T5 + T6 + T7) * D[6] + 2 * T16 + T250 + 2 * T17 + T251 + 2 * T18 + T252 + 2 * T19 + T253 + 2 * T20 + T254 + 2 * T21; T310 = 17 * T8, T311 = 34 * D[6], T312 = 34 * D[5], T313 = 17 * T9, T314 = 17 * T16, T315 = (T289 + T290 + T291 + T292 + T293) * D[5]; T316 = 17 * T17, T317 = (T290 + T291 + T292 + T293) * D[4]; T318 = 17 * T18, T319 = (T291 + T292 + T293) * D[3]; T320 = 17 * T19, T321 = (T292 + T293) * D[2]; T322 = 17 * T20, T323 = 17 * T21, T324 = (H * H * H * H * H * H * H * H * H); T325 = 64 * T8, T326 = (128 * D[6] + T40 + T41 + T42 + T43 + T44 + T45) * D[7]; T327 = 64 * T9, T328 = (T40 + T41 + T42 + T43 + T44 + T45) * D[6]; T329 = 64 * T16, T330 = 64 * T17, T331 = 64 * T18, T332 = 64 * T19, T333 = 64 * T20, T334 = 64 * T21, T335 = 140 * T8, T336 = 280 * D[6], T337 = 280 * D[5], T338 = 140 * T9, T339 = 140 * T16, T340 = 140 * T17, T341 = 140 * T18, T342 = 140 * T19, T343 = 140 * T20, T344 = 140 * T21, T345 = 196 * T8; T346 = (392 * D[6] + T91 + T92 + T93 + T94 + T95 + T96) * D[7]; T347 = 196 * T9; T348 = (T91 + T92 + T93 + T94 + T95 + T96) * D[6]; T349 = 196 * T16; T350 = (T92 + T93 + T94 + T95 + T96) * D[5]; T351 = 196 * T17; T352 = (T93 + T94 + T95 + T96) * D[4]; T353 = 196 * T18; T354 = (T94 + T95 + T96) * D[3]; T355 = 196 * T19; T356 = (T95 + T96) * D[2]; T357 = 196 * T20, T358 = 196 * T21, T359 = 182 * T8, T360 = 364 * D[6], T361 = 364 * D[5], T362 = 182 * T9, T363 = 182 * T16, T364 = 182 * T17, T365 = 182 * T18, T366 = 182 * T19, T367 = 182 * T20, T368 = 182 * T21, T369 = 112 * T8; T370 = (224 * D[6] + T177 + T41 + T42 + T43 + T44 + T45) * D[7]; T371 = 112 * T9; T372 = (T177 + T41 + T42 + T43 + T44 + T45) * D[6]; T373 = 112 * T16, T374 = 112 * T17, T375 = 112 * T18, T376 = 112 * T19, T377 = 112 * T20, T378 = 112 * T21, T379 = 44 * T8, T380 = 88 * D[6], T381 = 88 * D[5], T382 = 44 * T9, T383 = 44 * T16, T384 = 44 * T17, T385 = 44 * T18, T386 = 44 * T19, T387 = 44 * T20, T388 = 44 * T21, T389 = 10 * T8, T390 = (20 * D[6] + T249 + T3 + T4 + T5 + T6 + T7) * D[7]; T391 = 10 * T9, T392 = (T249 + T3 + T4 + T5 + T6 + T7) * D[6]; T393 = 10 * T16, T394 = 10 * T17, T395 = 10 * T18, T396 = 10 * T19, T397 = 10 * T20, T398 = 10 * T21, T399 = 2 * D[6], T400 = 2 * D[5], T401 = 2 * D[5] * D[6]; T402 = T309 * (L * L * L * L * L * L * L * L * L * L) + (T255 * H - T310 + (-T311 + T22 + T23 + T24 + T25 + T26 + T27) * D[7] - T313 + (T22 + T23 + T24 + T25 + T26 + T27) * D[6] - T314 + T47 - T316 + T48 - T318 + T49 - T320 + T50 - T322 + T51 - T323) * (L * L * L * L * L * L * L * L * L) + (T214 * T58 + (T256 + (T257 + T34 + T35 + T36 + T37 + T38 + T39) * D[7] + T258 + (T34 + T35 + T36 + T37 + T38 + T39) * D[6] + T259 + (T35 + T36 + T37 + T38 + T39) * D[5] + T260 + (T36 + T37 + T38 + T39) * D[4] + T261 + (T37 + T38 + T39) * D[3] + T262 + (T38 + T39) * D[2] + T263 + T264 + T265) * H + T325 + T326 + T327 + T328 + T329 + T101 + T330 + T103 + T331 + T105 + T332 + T107 + T333 + T109 + T334) * (L * L * L * L * L * L * L * L) + (T178 * T77 + (-T215 + (-T216 + T52 + T53 + T54 + T55 + T56 + T57) * D[7] - T223 + (T52 + T53 + T54 + T55 + T56 + T57) * D[6] - T224 + (T53 + T54 + T55 + T56 + T57) * D[5] - T225 + (T54 + T55 + T56 + T57) * D[4] - T226 + (T55 + T56 + T57) * D[3] - T227 + (T56 + T57) * D[2] - T228 - T229 - T230) * T58 + T267 * H - T335 + (-T336 + T65 + T66 + T67 + T68 + T69 + T70) * D[7] - T338 + (T65 + T66 + T67 + T68 + T69 + T70) * D[6] - T339 + T119 - T340 + T120 - T341 + T121 - T342 + T122 - T343 + T123 - T344) * (L * L * L * L * L * L * L); T403 = T402 + (T131 * T116 + (T179 + (T180 + T181) * D[7] + T182 + T183 + T184 + T185 + T186 + T187 + T188 + T189) * T77 + T232 * T58 + (T268 + (T269 + T85 + T86 + T87 + T88 + T89 + T90) * D[7] + T270 + (T85 + T86 + T87 + T88 + T89 + T90) * D[6] + T271 + (T86 + T87 + T88 + T89 + T90) * D[5] + T272 + (T87 + T88 + T89 + T90) * D[4] + T273 + (T88 + T89 + T90) * D[3] + T274 + (T89 + T90) * D[2] + T275 + T276 + T277) * H + T345 + T346 + T347 + T348 + T349 + T350 + T351 + T352 + T353 + T354 + T355 + T356 + T357 + T168 + T358) * (L * L * L * L * L * L) + ((-T97 + (-T98 - T71 + T41 + T42 + T43 + T44 + T45) * D[7] - T99 + (-T71 + T41 + T42 + T43 + T44 + T45) * D[6] - T100 + T101 - T102 + T103 - T104 + T105 - T106 + T107 - T108 + T109 - T110) * T146 + (-T132 + (-T133 + T111 + T78 + T79 + T80 + T81 + T82) * D[7] - T135 + (T111 + T78 + T79 + T80 + T81 + T82) * D[6] - T136 + T112 - T138 + T113 - T140 + T114 - T142 + T115 - T144 + T84 - T145) * T116 + T191 * T77 + (-T159 + (-T160 + T118 + T66 + T67 + T68 + T69 + T70) * D[7] - T162 + (T118 + T66 + T67 + T68 + T69 + T70) * D[6] - T163 + T119 - T164 + T120 - T165 + T121 - T166 + T122 - T167 + T123 - T169) * T58 + T278 * H - T359 + (-T360 + T130 + T66 + T67 + T68 + T69 + T70) * D[7] - T362 + (T130 + T66 + T67 + T68 + T69 + T70) * D[6] - T363 + T119 - T364 + T120 - T365 + T121 - T366 + T122 - T367 + T123 - T368) * (L * L * L * L * L); T404 = T403 + (T131 * T190 + (T132 + (T133 + T134 + T60 + T61 + T62 + T63 + T64) * D[7] + T135 + (T134 + T60 + T61 + T62 + T63 + T64) * D[6] + T136 + T137 + T138 + T139 + T140 + T141 + T142 + T143 + T144 - T84 + T145) * T146 + (1120 * T8 + (2240 * D[6] + T147 + T148 + T149 + T150 + T151 + T152) * D[7] + 1120 * T9 + (T147 + T148 + T149 + T150 + T151 + T152) * D[6] + 1120 * T16 + T154 + 1120 * T17 + T155 + 1120 * T18 + T156 + 1120 * T19 + T157 + 1120 * T20 + T158 + 1120 * T21) * T116 + (T192 + (T193 + T153 + T148 + T149 + T150 + T151 + T152) * D[7] + T199 + (T153 + T148 + T149 + T150 + T151 + T152) * D[6] + T200 + T154 + T201 + T155 + T202 + T156 + T203 + T157 + T204 + T158 + T206) * T77 + T237 * T58 + (T279 + (T280 + T175 + T170 + T171 + T172 + T173 + T174) * D[7] + T281 + (T175 + T170 + T171 + T172 + T173 + T174) * D[6] + T282 + T233 + T283 + T234 + T284 + T235 + T285 + T236 + T286 + T176 + T287) * H + T369 + T370 + T371 + T372 + T373 + T101 + T374 + T103 + T375 + T105 + T376 + T107 + T377 + T109 + T378) * (L * L * L * L) + (T178 * T231 + (-T179 + (-T180 - T181) * D[7] - T182 - T183 - T184 - T185 - T186 - T187 - T188 - T189) * T190 + T191 * T146 + (-T192 + (-T193 - T153 + T194 + T195 + T196 + T197 + T198) * D[7] - T199 + (-T153 + T194 + T195 + T196 + T197 + T198) * D[6] - T200 + T208 - T201 + T209 - T202 + T210 - T203 + T211 - T204 + T205 - T206) * T116 + (-1400 * T8 + (-2800 * D[6] + T207 + T194 + T195 + T196 + T197 + T198) * D[7] - 1400 * T9 + (T207 + T194 + T195 + T196 + T197 + T198) * D[6] - 1400 * T16 + T208 - 1400 * T17 + T209 - 1400 * T18 + T210 - 1400 * T19 + T211 - 1400 * T20 + T205 - 1400 * T21) * T77 + (-T238 + (-T239 + T212 + T78 + T79 + T80 + T81 + T82) * D[7] - T241 + (T212 + T78 + T79 + T80 + T81 + T82) * D[6] - T242 + T112 - T243 + T113 - T244 + T114 - T245 + T115 - T246 + T84 - T247) * T58 + T288 * H - T379 + (-T380 + T213 + T23 + T24 + T25 + T26 + T27) * D[7] - T382 + (T213 + T23 + T24 + T25 + T26 + T27) * D[6] - T383 + T47 - T384 + T48 - T385 + T49 - T386 + T50 - T387 + T51 - T388) * (L * L * L); T405 = T214 * T266 + (T215 + (T216 + T217 + T218 + T219 + T220 + T221 + T222) * D[7] + T223 + (T217 + T218 + T219 + T220 + T221 + T222) * D[6] + T224 + (T218 + T219 + T220 + T221 + T222) * D[5] + T225 + (T219 + T220 + T221 + T222) * D[4] + T226 + (T220 + T221 + T222) * D[3] + T227 + (T221 + T222) * D[2] + T228 + T229 + T230) * T231 + T232 * T190 + (T159 + (T160 + T161 + T170 + T171 + T172 + T173 + T174) * D[7] + T162 + (T161 + T170 + T171 + T172 + T173 + T174) * D[6] + T163 + T233 + T164 + T234 + T165 + T235 + T166 + T236 + T167 + T176 + T169) * T146 + T237 * T116 + (T238 + (T239 + T240 + T60 + T61 + T62 + T63 + T64) * D[7] + T241 + (T240 + T60 + T61 + T62 + T63 + T64) * D[6] + T242 + T137 + T243 + T139 + T244 + T141 + T245 + T143 + T246 - T84 + T247) * T77 + (336 * T8 + (672 * D[6] + T248 + T66 + T67 + T68 + T69 + T70) * D[7] + 336 * T9 + (T248 + T66 + T67 + T68 + T69 + T70) * D[6] + 336 * T16 + T119 + 336 * T17 + T120 + 336 * T18 + T121 + 336 * T19 + T122 + 336 * T20 - T176 + 336 * T21) * T58 + (T97 + (T98 + T71 + T23 + T24 + T25 + T26 + T27) * D[7] + T99 + (T71 + T23 + T24 + T25 + T26 + T27) * D[6] + T100 + T47 + T102 + T48 + T104 + T49 + T106 + T50 + T108 + T51 + T110) * H + T389 + T390 + T391; T406 = T255 * T324 + (-T256 + (-T257 - T34 - T35 - T36 - T37 - T38 - T39) * D[7] - T258 + (-T34 - T35 - T36 - T37 - T38 - T39) * D[6] - T259 + (-T35 - T36 - T37 - T38 - T39) * D[5] - T260 + (-T36 - T37 - T38 - T39) * D[4] - T261 + (-T37 - T38 - T39) * D[3] - T262 + (-T38 - T39) * D[2] - T263 - T264 - T265) * T266 + T267 * T231 + (-T268 + (-T269 - T85 - T86 - T87 - T88 - T89 - T90) * D[7] - T270 + (-T85 - T86 - T87 - T88 - T89 - T90) * D[6] - T271 + (-T86 - T87 - T88 - T89 - T90) * D[5] - T272 + (-T87 - T88 - T89 - T90) * D[4] - T273 + (-T88 - T89 - T90) * D[3] - T274 + (-T89 - T90) * D[2] - T275 - T276 - T277) * T190 + T278 * T146 + (-T279 + (-T280 - T175 + T66 + T67 + T68 + T69 + T70) * D[7] - T281 + (-T175 + T66 + T67 + T68 + T69 + T70) * D[6] - T282 + T119 - T283 + T120 - T284 + T121 - T285 + T122 - T286 - T176 - T287) * T116 + T288 * T77 + (-T97 + (-T98 - T71 + T289 + T290 + T291 + T292 + T293) * D[7] - T99 + (-T71 + T289 + T290 + T291 + T292 + T293) * D[6] - T100 + T315 - T102 + T317 - T104 + T319 - T106 + T321 - T108 + T294 - T110) * T58; T407 = T404 + (T405 + T392 + T393 + T250 + T394 + T251 + T395 + T252 + T396 + T253 + T397 + T254 + T398) * L * L + (T406 + (T295 + (T296 + T10 + T298 + T299 + T300 + T301 + T302) * D[7] + T297 + (T10 + T298 + T299 + T300 + T301 + T302) * D[6] + T303 + (T298 + T299 + T300 + T301 + T302) * D[5] + T304 + (T299 + T300 + T301 + T302) * D[4] + T305 + (T300 + T301 + T302) * D[3] + T306 + (T301 + T302) * D[2] + T307 + 6 * D[0] * D[1] + T308) * H - T8 + (-T399 - T400) * D[7] - T9 - T401 - T16 - T17 - T18 - T19 - T20 - T21) * L + T309 * (H * H * H * H * H * H * H * H * H * H) + (T310 + (T311 + T312 + T289 + T290 + T291 + T292 + T293) * D[7] + T313 + (T312 + T289 + T290 + T291 + T292 + T293) * D[6] + T314 + T315 + T316 + T317 + T318 + T319 + T320 + T321 + T322 + T294 + T323) * T324 + (T325 + T326 + T327 + T328 + T329 + T101 + T330 + T103 + T331 + T105 + T332 + T107 + T333 + T109 + T334) * T266 + (T335 + (T336 + T337 + T170 + T171 + T172 + T173 + T174) * D[7] + T338 + (T337 + T170 + T171 + T172 + T173 + T174) * D[6] + T339 + T233 + T340 + T234 + T341 + T235 + T342 + T236 + T343 + T176 + T344) * T231 + (T345 + T346 + T347 + T348 + T349 + T350 + T351 + T352 + T353 + T354 + T355 + T356 + T357 + T168 + T358) * T190; return (T407 + (T359 + (T360 + T361 + T170 + T171 + T172 + T173 + T174) * D[7] + T362 + (T361 + T170 + T171 + T172 + T173 + T174) * D[6] + T363 + T233 + T364 + T234 + T365 + T235 + T366 + T236 + T367 + T176 + T368) * T146 + (T369 + T370 + T371 + T372 + T373 + T101 + T374 + T103 + T375 + T105 + T376 + T107 + T377 + T109 + T378) * T116 + (T379 + (T380 + T381 + T289 + T290 + T291 + T292 + T293) * D[7] + T382 + (T381 + T289 + T290 + T291 + T292 + T293) * D[6] + T383 + T315 + T384 + T317 + T385 + T319 + T386 + T321 + T387 + T294 + T388) * T77 + (T389 + T390 + T391 + T392 + T393 + T250 + T394 + T251 + T395 + T252 + T396 + T253 + T397 + T254 + T398) * T58 + (T8 + (T399 + T400) * D[7] + T9 + T401 + T16 + T17 + T18 + T19 + T20 + T21) * H) / 6; }
27,966
463
jart/cosmopolitan
false
cosmopolitan/dsp/core/twixt8.h
#ifndef COSMOPOLITAN_DSP_CORE_TWIXT8_H_ #define COSMOPOLITAN_DSP_CORE_TWIXT8_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ /** * 8-bit linear interpolation kernel. */ static inline pureconst artificial unsigned char twixt8(unsigned char al, unsigned char bl, unsigned char p) { short bx; bx = bl; bx -= al; bx *= p; bx >>= 8; bx += al; al = bx; return al; } COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_TWIXT8_H_ */
619
25
jart/cosmopolitan
false
cosmopolitan/dsp/core/ks8.h
#ifndef COSMOPOLITAN_DSP_CORE_KS8_H_ #define COSMOPOLITAN_DSP_CORE_KS8_H_ #include "libc/macros.internal.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * Performs 16-bit scaled rounded madd w/ eight coefficients or fewer. * * (Σᵢ₌₀₋₈𝑘ᵢ𝑥ᵢ + 2ᵐ⁻¹)/2ᵐ * * @note intent is avoiding type promotion */ #define KS8(M, K1, K2, K3, K4, K5, K6, K7, K8, X1, X2, X3, X4, X5, X6, X7, X8) \ ({ \ short x1, x2, x3, x4, x5, x6, x7, x8; \ x1 = X1; \ x2 = X2; \ x3 = X3; \ x4 = X4; \ x5 = X5; \ x6 = X6; \ x7 = X7; \ x8 = X8; \ x1 *= K1; \ x2 *= K2; \ x3 *= K3; \ x4 *= K4; \ x5 *= K5; \ x6 *= K6; \ x7 *= K7; \ x8 *= K8; \ x1 += x2; \ x3 += x4; \ x5 += x6; \ x7 += x8; \ x1 += x3; \ x5 += x7; \ x1 += x5; \ if (M) { \ x1 += 1 << MAX(0, M - 1); \ x1 >>= M; \ } \ x1; \ }) #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_KS8_H_ */
2,937
48
jart/cosmopolitan
false
cosmopolitan/dsp/core/mulaw.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" /** * Compresses 16-bit signed linear audio sample with μ-Law. * * @param x is clamped to 16-bits of which the top 14 are considered * @return coded number in range [0..255] * @see ITU G.711 */ int mulaw(int x) { int b, i, a, s, l, h; a = x < 0 ? (~x >> 2) + 33 : (x >> 2) + 33; if (a > 8191) a = 8191; i = a >> 6; s = i ? (__builtin_clz(i) ^ 31) + 2 : 1; h = 8 - s; l = (a >> s) & 15; l = 15 - l; b = (h << 4) | l; if (x >= 0) b |= 128; return b; }
2,343
41
jart/cosmopolitan
false
cosmopolitan/dsp/core/ituround.h
#ifndef COSMOPOLITAN_DSP_CORE_ITUROUND_H_ #define COSMOPOLITAN_DSP_CORE_ITUROUND_H_ #include "libc/math.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) /** * An ITU recommended rounding function. * * 1. Negative numbers round toward zero * 2. Positive numbers round toward infinity * * @see round(), rint() */ static inline pureconst artificial long ituround(double x) { return floor(x + .5); } static inline pureconst artificial int ituroundf(float x) { return floorf(x + .5f); } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_ITUROUND_H_ */
582
24
jart/cosmopolitan
false
cosmopolitan/dsp/core/gamma.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/core.h" #include "dsp/core/gamma.h" #include "libc/math.h" double rgb2stdpc(double x, double g) { return COMPANDLUMA_SRGB(x, g); } double rgb2linpc(double x, double g) { return UNCOMPANDLUMA_SRGB(x, g); } double rgb2stdtv(double x) { return COMPANDLUMA_BT1886(x); } double rgb2lintv(double x) { return UNCOMPANDLUMA_BT1886(x); } double tv2pcgamma(double x, double g) { return rgb2stdpc(rgb2lintv(x), g); }
2,276
42
jart/cosmopolitan
false
cosmopolitan/dsp/core/c161.h
#ifndef COSMOPOLITAN_DSP_CORE_C161_H_ #define COSMOPOLITAN_DSP_CORE_C161_H_ #include "libc/macros.internal.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) #define EXTRA_SHARP 2 /** * Byte sized kernel for restoring sharpness of resampled memory. * * @define CLAMP[(-1*𝑎 + 6*𝑏 + -1*𝑐) / (-1 + 6 + -1)] * @limit [0..255] → [-510..1,532] → [-127..383] → [0..255] * @see C1331() */ forceinline pureconst artificial unsigned char C161(unsigned char al, unsigned char bl, unsigned char cl) { short ax, bx, cx; ax = al; bx = bl; cx = cl; ax *= -1; bx *= +6; cx *= -1; ax += bx; ax += cx; ax += 2; ax >>= 2; return MIN(255, MAX(0, ax)); } #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_CORE_C161_H_ */
866
34
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/demux.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ │vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer │ │ Dominic Szablewski - https://phoboslab.org │ │ │ │ The MIT License(MIT) │ │ Copyright(c) 2019 Dominic Szablewski │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files(the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and / or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND │ │ NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE │ │ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN │ │ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN │ │ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE │ │ SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/mpeg/buffer.h" #include "dsp/mpeg/demux.h" #include "dsp/mpeg/mpeg.h" #include "libc/mem/mem.h" #include "libc/str/str.h" asm(".ident\t\"\\n\\n\ PL_MPEG (MIT License)\\n\ Copyright(c) 2019 Dominic Szablewski\\n\ https://phoboslab.org\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ // ---------------------------------------------------------------------------- // plm_demux implementation plm_demux_t *plm_demux_create(plm_buffer_t *buffer, int destroy_when_done) { plm_demux_t *self = (plm_demux_t *)malloc(sizeof(plm_demux_t)); memset(self, 0, sizeof(plm_demux_t)); self->buffer = buffer; self->destroy_buffer_when_done = destroy_when_done; if (plm_buffer_find_start_code(self->buffer, START_PACK) != -1) { plm_demux_decode_pack_header(self); } if (plm_buffer_find_start_code(self->buffer, START_SYSTEM) != -1) { plm_demux_decode_system_header(self); } return self; } void plm_demux_destroy(plm_demux_t *self) { if (self->destroy_buffer_when_done) { plm_buffer_destroy(self->buffer); } free(self); } int plm_demux_get_num_video_streams(plm_demux_t *self) { return self->num_video_streams; } int plm_demux_get_num_audio_streams(plm_demux_t *self) { return self->num_audio_streams; } void plm_demux_rewind(plm_demux_t *self) { plm_buffer_rewind(self->buffer); } plm_packet_t *plm_demux_decode(plm_demux_t *self) { if (self->current_packet.length) { size_t bits_till_next_packet = self->current_packet.length << 3; if (!plm_buffer_has(self->buffer, bits_till_next_packet)) { return NULL; } plm_buffer_skip(self->buffer, bits_till_next_packet); self->current_packet.length = 0; } if (!self->has_pack_header) { if (plm_buffer_find_start_code(self->buffer, START_PACK) != -1) { plm_demux_decode_pack_header(self); } else { return NULL; } } if (!self->has_system_header) { if (plm_buffer_find_start_code(self->buffer, START_SYSTEM) != -1) { plm_demux_decode_system_header(self); } else { return NULL; } } // pending packet just waiting for data? if (self->next_packet.length) { return plm_demux_get_packet(self); } int code; do { code = plm_buffer_next_start_code(self->buffer); if ( code == PLM_DEMUX_PACKET_VIDEO_1 || code == PLM_DEMUX_PACKET_PRIVATE || (code >= PLM_DEMUX_PACKET_AUDIO_1 && code <= PLM_DEMUX_PACKET_AUDIO_4) ) { return plm_demux_decode_packet(self, code); } } while (code != -1); return NULL; } double plm_demux_read_time(plm_demux_t *self) { int64_t clock = plm_buffer_read(self->buffer, 3) << 30; plm_buffer_skip(self->buffer, 1); clock |= plm_buffer_read(self->buffer, 15) << 15; plm_buffer_skip(self->buffer, 1); clock |= plm_buffer_read(self->buffer, 15); plm_buffer_skip(self->buffer, 1); return (double)clock / 90000.0; } void plm_demux_decode_pack_header(plm_demux_t *self) { if (plm_buffer_read(self->buffer, 4) != 0x02) { return; // invalid } self->system_clock_ref = plm_demux_read_time(self); plm_buffer_skip(self->buffer, 1); plm_buffer_skip(self->buffer, 22); // mux_rate * 50 plm_buffer_skip(self->buffer, 1); self->has_pack_header = true; } void plm_demux_decode_system_header(plm_demux_t *self) { plm_buffer_skip(self->buffer, 16); // header_length plm_buffer_skip(self->buffer, 24); // rate bound self->num_audio_streams = plm_buffer_read(self->buffer, 6); plm_buffer_skip(self->buffer, 5); // misc flags self->num_video_streams = plm_buffer_read(self->buffer, 5); self->has_system_header = true; } plm_packet_t *plm_demux_decode_packet(plm_demux_t *self, int start_code) { if (!plm_buffer_has(self->buffer, 8 << 3)) { return NULL; } self->next_packet.type = start_code; self->next_packet.length = plm_buffer_read(self->buffer, 16); self->next_packet.length -= plm_buffer_skip_bytes(self->buffer, 0xff); // stuffing // skip P-STD if (plm_buffer_read(self->buffer, 2) == 0x01) { plm_buffer_skip(self->buffer, 16); self->next_packet.length -= 2; } int pts_dts_marker = plm_buffer_read(self->buffer, 2); if (pts_dts_marker == 0x03) { self->next_packet.pts = plm_demux_read_time(self); plm_buffer_skip(self->buffer, 40); // skip dts self->next_packet.length -= 10; } else if (pts_dts_marker == 0x02) { self->next_packet.pts = plm_demux_read_time(self); self->next_packet.length -= 5; } else if (pts_dts_marker == 0x00) { self->next_packet.pts = 0; plm_buffer_skip(self->buffer, 4); self->next_packet.length -= 1; } else { return NULL; // invalid } return plm_demux_get_packet(self); } plm_packet_t *plm_demux_get_packet(plm_demux_t *self) { if (!plm_buffer_has(self->buffer, self->next_packet.length << 3)) { return NULL; } self->current_packet.data = self->buffer->bytes + (self->buffer->bit_index >> 3); self->current_packet.length = self->next_packet.length; self->current_packet.type = self->next_packet.type; self->current_packet.pts = self->next_packet.pts; self->next_packet.length = 0; return &self->current_packet; }
7,758
209
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/video.h
#ifndef COSMOPOLITAN_DSP_MPEG_VIDEO_H_ #define COSMOPOLITAN_DSP_MPEG_VIDEO_H_ #include "dsp/mpeg/mpeg.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ typedef struct { int full_px; int is_set; int r_size; int h; int v; } plm_video_motion_t; typedef struct plm_video_t { double framerate; double time; double pixel_aspect_ratio; int frames_decoded; int width; int height; int mb_width; int mb_height; int mb_size; int luma_width; int luma_height; int chroma_width; int chroma_height; int start_code; int picture_type; plm_video_motion_t motion_forward; plm_video_motion_t motion_backward; int has_sequence_header; int quantizer_scale; int slice_begin; int macroblock_address; int mb_row; int mb_col; int macroblock_type; int macroblock_intra; int dc_predictor[3]; plm_buffer_t *buffer; int destroy_buffer_when_done; plm_frame_t frame_current; plm_frame_t frame_forward; plm_frame_t frame_backward; uint8_t *frames_data; int block_data[64]; uint8_t intra_quant_matrix[64]; uint8_t non_intra_quant_matrix[64]; int has_reference_frame; int assume_no_b_frames; } plm_video_t; void plm_video_process_macroblock_8(plm_video_t *, uint8_t *restrict, uint8_t *restrict, int, int, bool); void plm_video_process_macroblock_16(plm_video_t *, uint8_t *restrict, uint8_t *restrict, int, int, bool); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_MPEG_VIDEO_H_ */
1,570
63
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/README.txt
PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer Dominic Szablewski - https://phoboslab.org -- Synopsis // This function gets called for each decoded video frame void my_video_callback(plm_t *plm, plm_frame_t *frame, void *user) { // Do something with frame->y.data, frame->cr.data, frame->cb.data } // This function gets called for each decoded audio frame void my_audio_callback(plm_t *plm, plm_samples_t *frame, void *user) { // Do something with samples->interleaved } // Load a .mpg (MPEG Program Stream) file plm_t *plm = plm_create_with_filename("some-file.mpg"); // Install the video & audio decode callbacks plm_set_video_decode_callback(plm, my_video_callback, my_data); plm_set_audio_decode_callback(plm, my_audio_callback, my_data); // Decode do { plm_decode(plm, time_since_last_call); } while (!plm_has_ended(plm)); // All done plm_destroy(plm); -- Documentation This library provides several interfaces to load, demux and decode MPEG video and audio data. A high-level API combines the demuxer, video & audio decoders in an easy to use wrapper. Lower-level APIs for accessing the demuxer, video decoder and audio decoder, as well as providing different data sources are also available. Interfaces are written in an object orientet style, meaning you create object instances via various different constructor functions (plm_*create()), do some work on them and later dispose them via plm_*destroy(). plm_* -- the high-level interface, combining demuxer and decoders plm_buffer_* -- the data source used by all interfaces plm_demux_* -- the MPEG-PS demuxer plm_video_* -- the MPEG1 Video ("mpeg1") decoder plm_audio_* -- the MPEG1 Audio Layer II ("mp2") decoder This library uses malloc(), realloc() and free() to manage memory. Typically all allocation happens up-front when creating the interface. However, the default buffer size may be too small for certain inputs. In these cases plmpeg will realloc() the buffer with a larger size whenever needed. You can configure the default buffer size by defining PLM_BUFFER_DEFAULT_SIZE *before* including this library. With the high-level interface you have two options to decode video & audio: 1) Use plm_decode() and just hand over the delta time since the last call. It will decode everything needed and call your callbacks (specified through plm_set_{video|audio}_decode_callback()) any number of times. 2) Use plm_decode_video() and plm_decode_audio() to decode exactly one frame of video or audio data at a time. How you handle the synchronization of both streams is up to you. If you only want to decode video *or* audio through these functions, you should disable the other stream (plm_set_{video|audio}_enabled(false)) Video data is decoded into a struct with all 3 planes (Y, Cr, Cb) stored in separate buffers. You can either convert this to RGB on the CPU (slow) via the plm_frame_to_rgb() function or do it on the GPU with the following matrix: mat4 rec601 = mat4( 1.16438, 0.00000, 1.59603, -0.87079, 1.16438, -0.39176, -0.81297, 0.52959, 1.16438, 2.01723, 0.00000, -1.08139, 0, 0, 0, 1 ); gl_FragColor = vec4(y, cb, cr, 1.0) * rec601; Audio data is decoded into a struct with either one single float array with the samples for the left and right channel interleaved, or if the PLM_AUDIO_SEPARATE_CHANNELS is defined *before* including this library, into two separate float arrays - one for each channel. See below for detailed the API documentation.
3,483
93
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/blockset.h
#ifndef COSMOPOLITAN_DSP_MPEG_BLOCKSET_H_ #define COSMOPOLITAN_DSP_MPEG_BLOCKSET_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) #define PLM_BLOCK_SET(DEST, DEST_INDEX, DEST_WIDTH, SOURCE_INDEX, \ SOURCE_WIDTH, BLOCK_SIZE, OP) \ do { \ int dest_scan = DEST_WIDTH - BLOCK_SIZE; \ int source_scan = SOURCE_WIDTH - BLOCK_SIZE; \ for (int y = 0; y < BLOCK_SIZE; y++) { \ for (int x = 0; x < BLOCK_SIZE; x++) { \ DEST[DEST_INDEX] = OP; \ SOURCE_INDEX++; \ DEST_INDEX++; \ } \ SOURCE_INDEX += source_scan; \ DEST_INDEX += dest_scan; \ } \ } while (false) #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_MPEG_BLOCKSET_H_ */
1,188
23
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/plm.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ │vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer │ │ Dominic Szablewski - https://phoboslab.org │ │ │ │ The MIT License(MIT) │ │ Copyright(c) 2019 Dominic Szablewski │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files(the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and / or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND │ │ NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE │ │ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN │ │ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN │ │ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE │ │ SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/mpeg/mpeg.h" #include "libc/log/log.h" #include "libc/mem/mem.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" asm(".ident\t\"\\n\\n\ PL_MPEG (MIT License)\\n\ Copyright(c) 2019 Dominic Szablewski\\n\ https://phoboslab.org\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ // ----------------------------------------------------------------------------- // plm (high-level interface) implementation typedef struct plm_t { plm_demux_t *demux; double time; int has_ended; int loop; int video_packet_type; plm_buffer_t *video_buffer; plm_video_t *video_decoder; int audio_packet_type; double audio_lead_time; plm_buffer_t *audio_buffer; plm_audio_t *audio_decoder; plm_video_decode_callback video_decode_callback; void *video_decode_callback_user_data; plm_audio_decode_callback audio_decode_callback; void *audio_decode_callback_user_data; } plm_t; void plm_handle_end(plm_t *self); void plm_read_video_packet(plm_buffer_t *buffer, void *user); void plm_read_audio_packet(plm_buffer_t *buffer, void *user); void plm_read_packets(plm_t *self, int requested_type); plm_t *plm_create_with_filename(const char *filename) { plm_buffer_t *buffer = plm_buffer_create_with_filename(filename); if (!buffer) { return NULL; } return plm_create_with_buffer(buffer, true); } plm_t *plm_create_with_file(FILE *fh, int close_when_done) { plm_buffer_t *buffer = plm_buffer_create_with_file(fh, close_when_done); return plm_create_with_buffer(buffer, true); } plm_t *plm_create_with_memory(uint8_t *bytes, size_t length, int free_when_done) { plm_buffer_t *buffer = plm_buffer_create_with_memory(bytes, length, free_when_done); return plm_create_with_buffer(buffer, true); } plm_t *plm_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done) { plm_t *self = (plm_t *)malloc(sizeof(plm_t)); memset(self, 0, sizeof(plm_t)); self->demux = plm_demux_create(buffer, destroy_when_done); // In theory we should check plm_demux_get_num_video_streams() and // plm_demux_get_num_audio_streams() here, but older files typically // do not specify these correctly. So we just assume we have a video and // audio stream and create the decoders. self->video_packet_type = PLM_DEMUX_PACKET_VIDEO_1; self->video_buffer = plm_buffer_create_with_capacity(PLM_BUFFER_DEFAULT_SIZE); plm_buffer_set_load_callback(self->video_buffer, plm_read_video_packet, self); self->audio_packet_type = PLM_DEMUX_PACKET_AUDIO_1; self->audio_buffer = plm_buffer_create_with_capacity(PLM_BUFFER_DEFAULT_SIZE); plm_buffer_set_load_callback(self->audio_buffer, plm_read_audio_packet, self); self->video_decoder = plm_video_create_with_buffer(self->video_buffer, true); self->audio_decoder = plm_audio_create_with_buffer(self->audio_buffer, true); return self; } void plm_destroy(plm_t *self) { plm_video_destroy(self->video_decoder); plm_audio_destroy(self->audio_decoder); plm_demux_destroy(self->demux); free(self); } int plm_get_audio_enabled(plm_t *self) { return (self->audio_packet_type != 0); } void plm_set_audio_enabled(plm_t *self, int enabled, int stream_index) { /* int num_streams = plm_demux_get_num_audio_streams(self->demux); */ self->audio_packet_type = (enabled && stream_index >= 0 && stream_index < 4) ? PLM_DEMUX_PACKET_AUDIO_1 + stream_index : 0; } int plm_get_video_enabled(plm_t *self) { return (self->video_packet_type != 0); } void plm_set_video_enabled(plm_t *self, int enabled) { self->video_packet_type = (enabled) ? PLM_DEMUX_PACKET_VIDEO_1 : 0; } int plm_get_width(plm_t *self) { return plm_video_get_width(self->video_decoder); } double plm_get_pixel_aspect_ratio(plm_t *self) { return plm_video_get_pixel_aspect_ratio(self->video_decoder); } int plm_get_height(plm_t *self) { return plm_video_get_height(self->video_decoder); } double plm_get_framerate(plm_t *self) { return plm_video_get_framerate(self->video_decoder); } int plm_get_num_audio_streams(plm_t *self) { // Some files do not specify the number of audio streams in the system header. // If the reported number of streams is 0, we check if we have a samplerate, // indicating at least one audio stream. int num_streams = plm_demux_get_num_audio_streams(self->demux); return num_streams == 0 && plm_get_samplerate(self) ? 1 : num_streams; } int plm_get_samplerate(plm_t *self) { return plm_audio_get_samplerate(self->audio_decoder); } double plm_get_audio_lead_time(plm_t *self) { return self->audio_lead_time; } void plm_set_audio_lead_time(plm_t *self, double lead_time) { self->audio_lead_time = lead_time; } double plm_get_time(plm_t *self) { return self->time; } void plm_rewind(plm_t *self) { plm_video_rewind(self->video_decoder); plm_audio_rewind(self->audio_decoder); plm_demux_rewind(self->demux); self->time = 0; } int plm_get_loop(plm_t *self) { return self->loop; } void plm_set_loop(plm_t *self, int loop) { self->loop = loop; } int plm_has_ended(plm_t *self) { return self->has_ended; } void plm_set_video_decode_callback(plm_t *self, plm_video_decode_callback fp, void *user) { self->video_decode_callback = fp; self->video_decode_callback_user_data = user; } void plm_set_audio_decode_callback(plm_t *self, plm_audio_decode_callback fp, void *user) { self->audio_decode_callback = fp; self->audio_decode_callback_user_data = user; } int plm_decode(plm_t *self, double tick) { DEBUGF("%s", "plm_decode"); int decode_video = (self->video_decode_callback && self->video_packet_type); int decode_audio = (self->audio_decode_callback && self->audio_packet_type); if (!decode_video && !decode_audio) { // Nothing to do here return false; } int did_decode = false; int video_ended = false; int audio_ended = false; double video_target_time = self->time + tick; double audio_target_time = self->time + tick; if (self->audio_lead_time > 0 && decode_audio) { video_target_time -= self->audio_lead_time; } else { audio_target_time -= self->audio_lead_time; } do { did_decode = false; if (decode_video && plm_video_get_time(self->video_decoder) < video_target_time) { plm_frame_t *frame = plm_video_decode(self->video_decoder); if (frame) { self->video_decode_callback(self, frame, self->video_decode_callback_user_data); did_decode = true; } else { video_ended = true; } } if (decode_audio && plm_audio_get_time(self->audio_decoder) < audio_target_time) { plm_samples_t *samples = plm_audio_decode(self->audio_decoder); if (samples) { self->audio_decode_callback(self, samples, self->audio_decode_callback_user_data); did_decode = true; } else { audio_ended = true; } } } while (did_decode); // We wanted to decode something but failed -> the source must have ended if ((!decode_video || video_ended) && (!decode_audio || audio_ended)) { plm_handle_end(self); } else { self->time += tick; } return did_decode ? true : false; } plm_frame_t *plm_decode_video(plm_t *self) { if (!self->video_packet_type) { return NULL; } plm_frame_t *frame = plm_video_decode(self->video_decoder); if (frame) { self->time = frame->time; } else { plm_handle_end(self); } return frame; } plm_samples_t *plm_decode_audio(plm_t *self) { if (!self->audio_packet_type) { return NULL; } plm_samples_t *samples = plm_audio_decode(self->audio_decoder); if (samples) { self->time = samples->time; } else { plm_handle_end(self); } return samples; } void plm_handle_end(plm_t *self) { if (self->loop) { plm_rewind(self); } else { self->has_ended = true; } } void plm_read_video_packet(plm_buffer_t *buffer, void *user) { plm_t *self = (plm_t *)user; plm_read_packets(self, self->video_packet_type); } void plm_read_audio_packet(plm_buffer_t *buffer, void *user) { plm_t *self = (plm_t *)user; plm_read_packets(self, self->audio_packet_type); } void plm_read_packets(plm_t *self, int requested_type) { plm_packet_t *packet; while ((packet = plm_demux_decode(self->demux))) { if (packet->type == self->video_packet_type) { plm_buffer_write(self->video_buffer, packet->data, packet->length); } else if (packet->type == self->audio_packet_type) { plm_buffer_write(self->audio_buffer, packet->data, packet->length); } if (packet->type == requested_type) { return; } } }
11,098
338
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/mpeg.h
#ifndef COSMOPOLITAN_DSP_MPEG_MPEG_H_ #define COSMOPOLITAN_DSP_MPEG_MPEG_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct FILE; typedef struct plm_t plm_t; typedef struct plm_buffer_t plm_buffer_t; typedef struct plm_demux_t plm_demux_t; typedef struct plm_video_t plm_video_t; typedef struct plm_audio_t plm_audio_t; /** * Demuxed MPEG PS packet * * The type maps directly to the various MPEG-PES start codes. pts is * the presentation time stamp of the packet in seconds. Not all packets * have a pts value. */ typedef struct plm_packet_t { int type; double pts; size_t length; uint8_t *data; } plm_packet_t; /** * Decoded Video Plane * * The byte length of the data is width * height. Note that different * planes have different sizes: the Luma plane (Y) is double the size of * each of the two Chroma planes (Cr, Cb) - i.e. 4 times the byte * length. Also note that the size of the plane does *not* denote the * size of the displayed frame. The sizes of planes are always rounded * up to the nearest macroblock (16px). */ typedef struct plm_plane_t { unsigned int width; unsigned int height; uint8_t *data; } plm_plane_t; /** * Decoded Video Frame * * Width and height denote the desired display size of the frame. This * may be different from the internal size of the 3 planes. */ typedef struct plm_frame_t { double time; unsigned int width; unsigned int height; plm_plane_t y; plm_plane_t cr; plm_plane_t cb; } plm_frame_t; /** * Callback function type for decoded video frames used by the high-level * plm_* interface */ typedef void (*plm_video_decode_callback)(plm_t *self, plm_frame_t *frame, void *user); /** * Decoded Audio Samples * * Samples are stored as normalized (-1, 1) float either interleaved, or if * PLM_AUDIO_SEPARATE_CHANNELS is defined, in two separate arrays. * The `count` is always PLM_AUDIO_SAMPLES_PER_FRAME and just there for * convenience. */ #define PLM_AUDIO_SAMPLES_PER_FRAME 1152 struct plm_samples_t { double time; unsigned int count; #ifdef PLM_AUDIO_SEPARATE_CHANNELS float left[PLM_AUDIO_SAMPLES_PER_FRAME] forcealign(32); float right[PLM_AUDIO_SAMPLES_PER_FRAME] forcealign(32); #else float interleaved[PLM_AUDIO_SAMPLES_PER_FRAME * 2] forcealign(32); #endif } forcealign(32); typedef struct plm_samples_t plm_samples_t; /** * Callback function type for decoded audio samples used by the high-level * plm_* interface */ typedef void (*plm_audio_decode_callback)(plm_t *self, plm_samples_t *samples, void *user); /** * Callback function for plm_buffer when it needs more data */ typedef void (*plm_buffer_load_callback)(plm_buffer_t *self, void *user); /** * ----------------------------------------------------------------------------- * plm_* public API * High-Level API for loading/demuxing/decoding MPEG-PS data * * Create a plmpeg instance with a filename. Returns NULL if the file could not * be opened. */ plm_t *plm_create_with_filename(const char *filename); /** * Create a plmpeg instance with file handle. Pass true to close_when_done * to let plmpeg call fclose() on the handle when plm_destroy() is * called. */ plm_t *plm_create_with_file(struct FILE *fh, int close_when_done); /** * Create a plmpeg instance with pointer to memory as source. This assumes the * whole file is in memory. Pass true to free_when_done to let plmpeg call * free() on the pointer when plm_destroy() is called. */ plm_t *plm_create_with_memory(uint8_t *bytes, size_t length, int free_when_done); /** * Create a plmpeg instance with a plm_buffer as source. This is also * called internally by all the above constructor functions. */ plm_t *plm_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done); /** * Destroy a plmpeg instance and free all data */ void plm_destroy(plm_t *self); /** * Get or set whether video decoding is enabled. */ int plm_get_video_enabled(plm_t *self); void plm_set_video_enabled(plm_t *self, int enabled); /** * Get or set whether audio decoding is enabled. When enabling, you can set the * desired audio stream (0-3) to decode. */ int plm_get_audio_enabled(plm_t *self); void plm_set_audio_enabled(plm_t *self, int enabled, int stream_index); /** * Get the display width/height of the video stream */ int plm_get_width(plm_t *self); int plm_get_height(plm_t *self); double plm_get_pixel_aspect_ratio(plm_t *); /** * Get the framerate of the video stream in frames per second */ double plm_get_framerate(plm_t *self); /** * Get the number of available audio streams in the file */ int plm_get_num_audio_streams(plm_t *self); /** * Get the samplerate of the audio stream in samples per second */ int plm_get_samplerate(plm_t *self); /** * Get or set the audio lead time in seconds - the time in which audio samples * are decoded in advance (or behind) the video decode time. Default 0. */ double plm_get_audio_lead_time(plm_t *self); void plm_set_audio_lead_time(plm_t *self, double lead_time); /** * Get the current internal time in seconds */ double plm_get_time(plm_t *self); /** * Rewind all buffers back to the beginning. */ void plm_rewind(plm_t *self); /** * Get or set looping. Default false. */ int plm_get_loop(plm_t *self); void plm_set_loop(plm_t *self, int loop); /** * Get whether the file has ended. If looping is enabled, this will always * return false. */ int plm_has_ended(plm_t *self); /** * Set the callback for decoded video frames used with plm_decode(). If no * callback is set, video data will be ignored and not be decoded. */ void plm_set_video_decode_callback(plm_t *self, plm_video_decode_callback fp, void *user); /** * Set the callback for decoded audio samples used with plm_decode(). If no * callback is set, audio data will be ignored and not be decoded. */ void plm_set_audio_decode_callback(plm_t *self, plm_audio_decode_callback fp, void *user); /** * Advance the internal timer by seconds and decode video/audio up to * this time. Returns true/false whether anything was decoded. */ int plm_decode(plm_t *self, double seconds); /** * Decode and return one video frame. Returns NULL if no frame could be decoded * (either because the source ended or data is corrupt). If you only want to * decode video, you should disable audio via plm_set_audio_enabled(). * The returned plm_frame_t is valid until the next call to * plm_decode_video call or until the plm_destroy is called. */ plm_frame_t *plm_decode_video(plm_t *self); /** * Decode and return one audio frame. Returns NULL if no frame could be decoded * (either because the source ended or data is corrupt). If you only want to * decode audio, you should disable video via plm_set_video_enabled(). * The returned plm_samples_t is valid until the next call to * plm_decode_video or until the plm_destroy is called. */ plm_samples_t *plm_decode_audio(plm_t *self); /* ----------------------------------------------------------------------------- * plm_buffer public API * Provides the data source for all other plm_* interfaces * * The default size for buffers created from files or by the high-level API */ #ifndef PLM_BUFFER_DEFAULT_SIZE #define PLM_BUFFER_DEFAULT_SIZE (128 * 1024) #endif /** * Create a buffer instance with a filename. Returns NULL if the file could not * be opened. */ plm_buffer_t *plm_buffer_create_with_filename(const char *filename); /** * Create a buffer instance with file handle. Pass true to close_when_done * to let plmpeg call fclose() on the handle when plm_destroy() is * called. */ plm_buffer_t *plm_buffer_create_with_file(struct FILE *fh, int close_when_done); /** * Create a buffer instance with a pointer to memory as source. This assumes * the whole file is in memory. Pass 1 to free_when_done to let plmpeg call * free() on the pointer when plm_destroy() is called. */ plm_buffer_t *plm_buffer_create_with_memory(uint8_t *bytes, size_t length, int free_when_done); /** * Create an empty buffer with an initial capacity. The buffer will grow * as needed. */ plm_buffer_t *plm_buffer_create_with_capacity(size_t capacity); /** * Destroy a buffer instance and free all data */ void plm_buffer_destroy(plm_buffer_t *self); /** * Copy data into the buffer. If the data to be written is larger than the * available space, the buffer will realloc() with a larger capacity. * Returns the number of bytes written. This will always be the same as the * passed in length, except when the buffer was created _with_memory() for * which _write() is forbidden. */ size_t plm_buffer_write(plm_buffer_t *self, uint8_t *bytes, size_t length); /** * Set a callback that is called whenever the buffer needs more data */ void plm_buffer_set_load_callback(plm_buffer_t *self, plm_buffer_load_callback fp, void *user); /** * Rewind the buffer back to the beginning. When loading from a file handle, * this also seeks to the beginning of the file. */ void plm_buffer_rewind(plm_buffer_t *self); /** * ----------------------------------------------------------------------------- * plm_demux public API * Demux an MPEG Program Stream (PS) data into separate packages * * Various Packet Types */ #define PLM_DEMUX_PACKET_PRIVATE 0xBD #define PLM_DEMUX_PACKET_AUDIO_1 0xC0 #define PLM_DEMUX_PACKET_AUDIO_2 0xC1 #define PLM_DEMUX_PACKET_AUDIO_3 0xC2 #define PLM_DEMUX_PACKET_AUDIO_4 0xC2 #define PLM_DEMUX_PACKET_VIDEO_1 0xE0 /** * Create a demuxer with a plm_buffer as source */ plm_demux_t *plm_demux_create(plm_buffer_t *buffer, int destroy_when_done); /** * Destroy a demuxer and free all data */ void plm_demux_destroy(plm_demux_t *self); /** * Returns the number of video streams found in the system header. */ int plm_demux_get_num_video_streams(plm_demux_t *self); /** * Returns the number of audio streams found in the system header. */ int plm_demux_get_num_audio_streams(plm_demux_t *self); /** * Rewinds the internal buffer. See plm_buffer_rewind(). */ void plm_demux_rewind(plm_demux_t *self); /** * Decode and return the next packet. The returned packet_t is valid until * the next call to plm_demux_decode() or until the demuxer is destroyed. */ plm_packet_t *plm_demux_decode(plm_demux_t *self); /* ----------------------------------------------------------------------------- * plm_video public API * Decode MPEG1 Video ("mpeg1") data into raw YCrCb frames */ /** * Create a video decoder with a plm_buffer as source */ plm_video_t *plm_video_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done); /** * Destroy a video decoder and free all data */ void plm_video_destroy(plm_video_t *self); /** * Get the framerate in frames per second */ double plm_video_get_framerate(plm_video_t *); double plm_video_get_pixel_aspect_ratio(plm_video_t *); /** * Get the display width/height */ int plm_video_get_width(plm_video_t *); int plm_video_get_height(plm_video_t *); /** * Set "no delay" mode. When enabled, the decoder assumes that the video does * *not* contain any B-Frames. This is useful for reducing lag when streaming. */ void plm_video_set_no_delay(plm_video_t *self, int no_delay); /** * Get the current internal time in seconds */ double plm_video_get_time(plm_video_t *self); /** * Rewinds the internal buffer. See plm_buffer_rewind(). */ void plm_video_rewind(plm_video_t *self); /** * Decode and return one frame of video and advance the internal time by * 1/framerate seconds. The returned frame_t is valid until the next call of * plm_video_decode() or until the video decoder is destroyed. */ plm_frame_t *plm_video_decode(plm_video_t *self); /** * Convert the YCrCb data of a frame into an interleaved RGB buffer. The buffer * pointed to by *rgb must have a size of (frame->width * frame->height * 3) * bytes. */ void plm_frame_to_rgb(plm_frame_t *frame, uint8_t *rgb); /* ----------------------------------------------------------------------------- * plm_audio public API * Decode MPEG-1 Audio Layer II ("mp2") data into raw samples */ /** * Create an audio decoder with a plm_buffer as source. */ plm_audio_t *plm_audio_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done); /** * Destroy an audio decoder and free all data */ void plm_audio_destroy(plm_audio_t *self); /** * Get the samplerate in samples per second */ int plm_audio_get_samplerate(plm_audio_t *self); /** * Get the current internal time in seconds */ double plm_audio_get_time(plm_audio_t *self); /** * Rewinds the internal buffer. See plm_buffer_rewind(). */ void plm_audio_rewind(plm_audio_t *self); /** * Decode and return one "frame" of audio and advance the internal time by * (PLM_AUDIO_SAMPLES_PER_FRAME/samplerate) seconds. The returned samples_t * is valid until the next call of plm_audio_decode() or until the audio * decoder is destroyed. */ plm_samples_t *plm_audio_decode(plm_audio_t *self); extern long plmpegdecode_latency_; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_MPEG_MPEG_H_ */
13,409
451
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/idct.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ │vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer │ │ Dominic Szablewski - https://phoboslab.org │ │ │ │ The MIT License(MIT) │ │ Copyright(c) 2019 Dominic Szablewski │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files(the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and / or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND │ │ NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE │ │ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN │ │ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN │ │ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE │ │ SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/core/half.h" asm(".ident\t\"\\n\\n\ PL_MPEG (MIT License)\\n\ Copyright(c) 2019 Dominic Szablewski\\n\ https://phoboslab.org\""); asm(".include \"libc/disclaimer.inc\""); /** * Computes Fixed-Point 8x8 Inverse Discrete Cosine Transform. * * @note discovered by Nasir Ahmed */ void plm_video_idct(int block[8][8]) { int i, j, t1, t2, m0; int b1, b3, b4, b6, b7; int y3, y4, y5, y6, y7; int x0, x1, x2, x3, x4; for (i = 0; i < 8; ++i) { b1 = block[4][i]; b3 = block[2][i] + block[6][i]; b4 = block[5][i] - block[3][i]; t1 = block[1][i] + block[7][i]; t2 = block[3][i] + block[5][i]; b6 = block[1][i] - block[7][i]; b7 = t1 + t2; m0 = block[0][i]; x4 = ((b6 * 473 - b4 * 196 + 128) >> 8) - b7; x0 = x4 - (((t1 - t2) * 362 + 128) >> 8); x1 = m0 - b1; x2 = (((block[2][i] - block[6][i]) * 362 + 128) >> 8) - b3; x3 = m0 + b1; y3 = x1 + x2; y4 = x3 + b3; y5 = x1 - x2; y6 = x3 - b3; y7 = -x0 - ((b4 * 473 + b6 * 196 + 128) >> 8); block[0][i] = b7 + y4; block[1][i] = x4 + y3; block[2][i] = y5 - x0; block[3][i] = y6 - y7; block[4][i] = y6 + y7; block[5][i] = x0 + y5; block[6][i] = y3 - x4; block[7][i] = y4 - b7; } for (i = 0; i < 8; ++i) { b1 = block[i][4]; b3 = block[i][2] + block[i][6]; b4 = block[i][5] - block[i][3]; t1 = block[i][1] + block[i][7]; t2 = block[i][3] + block[i][5]; b6 = block[i][1] - block[i][7]; b7 = t1 + t2; m0 = block[i][0]; x4 = ((b6 * 473 - b4 * 196 + 128) >> 8) - b7; x0 = x4 - (((t1 - t2) * 362 + 128) >> 8); x1 = m0 - b1; x2 = (((block[i][2] - block[i][6]) * 362 + 128) >> 8) - b3; x3 = m0 + b1; y3 = x1 + x2; y4 = x3 + b3; y5 = x1 - x2; y6 = x3 - b3; y7 = -x0 - ((b4 * 473 + b6 * 196 + 128) >> 8); block[i][0] = (b7 + y4 + 128) >> 8; block[i][1] = (x4 + y3 + 128) >> 8; block[i][2] = (y5 - x0 + 128) >> 8; block[i][3] = (y6 - y7 + 128) >> 8; block[i][4] = (y6 + y7 + 128) >> 8; block[i][5] = (x0 + y5 + 128) >> 8; block[i][6] = (y3 - x4 + 128) >> 8; block[i][7] = (y4 - b7 + 128) >> 8; } }
4,934
107
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/mpeg1.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ │vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer │ │ Dominic Szablewski - https://phoboslab.org │ │ │ │ The MIT License(MIT) │ │ Copyright(c) 2019 Dominic Szablewski │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files(the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and / or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND │ │ NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE │ │ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN │ │ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN │ │ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE │ │ SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/mpeg/blockset.h" #include "dsp/mpeg/buffer.h" #include "dsp/mpeg/idct.h" #include "dsp/mpeg/mpeg.h" #include "dsp/mpeg/video.h" #include "libc/fmt/conv.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/mem/mem.h" #include "libc/str/str.h" #include "libc/time/time.h" #include "libc/x/x.h" asm(".ident\t\"\\n\\n\ PL_MPEG (MIT License)\\n\ Copyright(c) 2019 Dominic Szablewski\\n\ https://phoboslab.org\""); asm(".include \"libc/disclaimer.inc\""); // ----------------------------------------------------------------------------- // plm_video implementation // Inspired by Java MPEG-1 Video Decoder and Player by Zoltan Korandi // https://sourceforge.net/projects/javampeg1video/ #define GETCONST(ARRAY, DEFAULT) static const int PLM_VIDEO_PICTURE_TYPE_INTRA = 1; static const int PLM_VIDEO_PICTURE_TYPE_PREDICTIVE = 2; static const int PLM_VIDEO_PICTURE_TYPE_B = 3; static const int PLM_START_SEQUENCE = 0xB3; static const int PLM_START_SLICE_FIRST = 0x01; static const int PLM_START_SLICE_LAST = 0xAF; static const int PLM_START_PICTURE = 0x00; static const int PLM_START_EXTENSION = 0xB5; static const int PLM_START_USER_DATA = 0xB2; static const float PLM_VIDEO_PIXEL_ASPECT_RATIO[] = { 1.0000, /* square pixels */ 0.6735, /* 3:4? */ 0.7031, /* MPEG-1 / MPEG-2 video encoding divergence? */ 0.7615, 0.8055, 0.8437, 0.8935, 0.9157, 0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2051, }; static const float PLM_VIDEO_PICTURE_RATE[] = { 23.976, /* NTSC-Film */ 24.000, /* NTSC-Film (enriched for foreign nations) */ 25.000, /* PAL (Britain, Africa, China, etc.) */ 29.970, /* NTSC */ 30.000, /* NTSC (enriched for foreign nations) */ 50.000, /* PAL? */ 59.940, /* NTSC-Wow */ 60.000 /* NTSC-Wow (enriched for foreign nations) */ }; static const uint8_t PLM_VIDEO_ZIG_ZAG[] = /* clang-format off */ { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, } /* clang-format on */; static const uint8_t PLM_VIDEO_INTRAQUANT_MATRIX[] = /* clang-format off */ { 8, 16, 19, 22, 26, 27, 29, 34, 16, 16, 22, 24, 27, 29, 34, 37, 19, 22, 26, 27, 29, 34, 34, 38, 22, 22, 26, 27, 29, 34, 37, 40, 22, 26, 27, 29, 32, 35, 40, 48, 26, 27, 29, 32, 35, 40, 48, 58, 26, 27, 29, 34, 38, 46, 56, 69, 27, 29, 35, 38, 46, 56, 69, 83, } /* clang-format on */; static const uint8_t PLM_VIDEO_NONINTRAQUANT_MATRIX[] = /* clang-format off */ { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, } /* clang-format on */; static const uint8_t PLM_VIDEO_PREMULTIPLIER_MATRIX[] = /* clang-format off */ { 32, 44, 42, 38, 32, 25, 17, 9, 44, 62, 58, 52, 44, 35, 24, 12, 42, 58, 55, 49, 42, 33, 23, 12, 38, 52, 49, 44, 38, 30, 20, 10, 32, 44, 42, 38, 32, 25, 17, 9, 25, 35, 33, 30, 25, 20, 14, 7, 17, 24, 23, 20, 17, 14, 9, 5, 9, 12, 12, 10, 9, 7, 5, 2, } /* clang-format on */; static const plm_vlc_t PLM_VIDEO_MACROBLOCK_ADDRESS_INCREMENT[] = { {1 << 1, 0}, {0, 1}, // 0: x {2 << 1, 0}, {3 << 1, 0}, // 1: 0x {4 << 1, 0}, {5 << 1, 0}, // 2: 00x {0, 3}, {0, 2}, // 3: 01x {6 << 1, 0}, {7 << 1, 0}, // 4: 000x {0, 5}, {0, 4}, // 5: 001x {8 << 1, 0}, {9 << 1, 0}, // 6: 0000x {0, 7}, {0, 6}, // 7: 0001x {10 << 1, 0}, {11 << 1, 0}, // 8: 0000 0x {12 << 1, 0}, {13 << 1, 0}, // 9: 0000 1x {14 << 1, 0}, {15 << 1, 0}, // 10: 0000 00x {16 << 1, 0}, {17 << 1, 0}, // 11: 0000 01x {18 << 1, 0}, {19 << 1, 0}, // 12: 0000 10x {0, 9}, {0, 8}, // 13: 0000 11x {-1, 0}, {20 << 1, 0}, // 14: 0000 000x {-1, 0}, {21 << 1, 0}, // 15: 0000 001x {22 << 1, 0}, {23 << 1, 0}, // 16: 0000 010x {0, 15}, {0, 14}, // 17: 0000 011x {0, 13}, {0, 12}, // 18: 0000 100x {0, 11}, {0, 10}, // 19: 0000 101x {24 << 1, 0}, {25 << 1, 0}, // 20: 0000 0001x {26 << 1, 0}, {27 << 1, 0}, // 21: 0000 0011x {28 << 1, 0}, {29 << 1, 0}, // 22: 0000 0100x {30 << 1, 0}, {31 << 1, 0}, // 23: 0000 0101x {32 << 1, 0}, {-1, 0}, // 24: 0000 0001 0x {-1, 0}, {33 << 1, 0}, // 25: 0000 0001 1x {34 << 1, 0}, {35 << 1, 0}, // 26: 0000 0011 0x {36 << 1, 0}, {37 << 1, 0}, // 27: 0000 0011 1x {38 << 1, 0}, {39 << 1, 0}, // 28: 0000 0100 0x {0, 21}, {0, 20}, // 29: 0000 0100 1x {0, 19}, {0, 18}, // 30: 0000 0101 0x {0, 17}, {0, 16}, // 31: 0000 0101 1x {0, 35}, {-1, 0}, // 32: 0000 0001 00x {-1, 0}, {0, 34}, // 33: 0000 0001 11x {0, 33}, {0, 32}, // 34: 0000 0011 00x {0, 31}, {0, 30}, // 35: 0000 0011 01x {0, 29}, {0, 28}, // 36: 0000 0011 10x {0, 27}, {0, 26}, // 37: 0000 0011 11x {0, 25}, {0, 24}, // 38: 0000 0100 00x {0, 23}, {0, 22}, // 39: 0000 0100 01x }; static const plm_vlc_t PLM_VIDEO_MACROBLOCK_TYPE_INTRA[] = { {1 << 1, 0}, {0, 0x01}, // 0: x {-1, 0}, {0, 0x11}, // 1: 0x }; static const plm_vlc_t PLM_VIDEO_MACROBLOCK_TYPE_PREDICTIVE[] = { {1 << 1, 0}, {0, 0x0a}, // 0: x {2 << 1, 0}, {0, 0x02}, // 1: 0x {3 << 1, 0}, {0, 0x08}, // 2: 00x {4 << 1, 0}, {5 << 1, 0}, // 3: 000x {6 << 1, 0}, {0, 0x12}, // 4: 0000x {0, 0x1a}, {0, 0x01}, // 5: 0001x {-1, 0}, {0, 0x11}, // 6: 0000 0x }; static const plm_vlc_t PLM_VIDEO_MACROBLOCK_TYPE_B[] = { {1 << 1, 0}, {2 << 1, 0}, // 0: x {3 << 1, 0}, {4 << 1, 0}, // 1: 0x {0, 0x0c}, {0, 0x0e}, // 2: 1x {5 << 1, 0}, {6 << 1, 0}, // 3: 00x {0, 0x04}, {0, 0x06}, // 4: 01x {7 << 1, 0}, {8 << 1, 0}, // 5: 000x {0, 0x08}, {0, 0x0a}, // 6: 001x {9 << 1, 0}, {10 << 1, 0}, // 7: 0000x {0, 0x1e}, {0, 0x01}, // 8: 0001x {-1, 0}, {0, 0x11}, // 9: 0000 0x {0, 0x16}, {0, 0x1a}, // 10: 0000 1x }; static const plm_vlc_t PLM_VIDEO_CODE_BLOCK_PATTERN[] = { {1 << 1, 0}, {2 << 1, 0}, // 0: x {3 << 1, 0}, {4 << 1, 0}, // 1: 0x {5 << 1, 0}, {6 << 1, 0}, // 2: 1x {7 << 1, 0}, {8 << 1, 0}, // 3: 00x {9 << 1, 0}, {10 << 1, 0}, // 4: 01x {11 << 1, 0}, {12 << 1, 0}, // 5: 10x {13 << 1, 0}, {0, 60}, // 6: 11x {14 << 1, 0}, {15 << 1, 0}, // 7: 000x {16 << 1, 0}, {17 << 1, 0}, // 8: 001x {18 << 1, 0}, {19 << 1, 0}, // 9: 010x {20 << 1, 0}, {21 << 1, 0}, // 10: 011x {22 << 1, 0}, {23 << 1, 0}, // 11: 100x {0, 32}, {0, 16}, // 12: 101x {0, 8}, {0, 4}, // 13: 110x {24 << 1, 0}, {25 << 1, 0}, // 14: 0000x {26 << 1, 0}, {27 << 1, 0}, // 15: 0001x {28 << 1, 0}, {29 << 1, 0}, // 16: 0010x {30 << 1, 0}, {31 << 1, 0}, // 17: 0011x {0, 62}, {0, 2}, // 18: 0100x {0, 61}, {0, 1}, // 19: 0101x {0, 56}, {0, 52}, // 20: 0110x {0, 44}, {0, 28}, // 21: 0111x {0, 40}, {0, 20}, // 22: 1000x {0, 48}, {0, 12}, // 23: 1001x {32 << 1, 0}, {33 << 1, 0}, // 24: 0000 0x {34 << 1, 0}, {35 << 1, 0}, // 25: 0000 1x {36 << 1, 0}, {37 << 1, 0}, // 26: 0001 0x {38 << 1, 0}, {39 << 1, 0}, // 27: 0001 1x {40 << 1, 0}, {41 << 1, 0}, // 28: 0010 0x {42 << 1, 0}, {43 << 1, 0}, // 29: 0010 1x {0, 63}, {0, 3}, // 30: 0011 0x {0, 36}, {0, 24}, // 31: 0011 1x {44 << 1, 0}, {45 << 1, 0}, // 32: 0000 00x {46 << 1, 0}, {47 << 1, 0}, // 33: 0000 01x {48 << 1, 0}, {49 << 1, 0}, // 34: 0000 10x {50 << 1, 0}, {51 << 1, 0}, // 35: 0000 11x {52 << 1, 0}, {53 << 1, 0}, // 36: 0001 00x {54 << 1, 0}, {55 << 1, 0}, // 37: 0001 01x {56 << 1, 0}, {57 << 1, 0}, // 38: 0001 10x {58 << 1, 0}, {59 << 1, 0}, // 39: 0001 11x {0, 34}, {0, 18}, // 40: 0010 00x {0, 10}, {0, 6}, // 41: 0010 01x {0, 33}, {0, 17}, // 42: 0010 10x {0, 9}, {0, 5}, // 43: 0010 11x {-1, 0}, {60 << 1, 0}, // 44: 0000 000x {61 << 1, 0}, {62 << 1, 0}, // 45: 0000 001x {0, 58}, {0, 54}, // 46: 0000 010x {0, 46}, {0, 30}, // 47: 0000 011x {0, 57}, {0, 53}, // 48: 0000 100x {0, 45}, {0, 29}, // 49: 0000 101x {0, 38}, {0, 26}, // 50: 0000 110x {0, 37}, {0, 25}, // 51: 0000 111x {0, 43}, {0, 23}, // 52: 0001 000x {0, 51}, {0, 15}, // 53: 0001 001x {0, 42}, {0, 22}, // 54: 0001 010x {0, 50}, {0, 14}, // 55: 0001 011x {0, 41}, {0, 21}, // 56: 0001 100x {0, 49}, {0, 13}, // 57: 0001 101x {0, 35}, {0, 19}, // 58: 0001 110x {0, 11}, {0, 7}, // 59: 0001 111x {0, 39}, {0, 27}, // 60: 0000 0001x {0, 59}, {0, 55}, // 61: 0000 0010x {0, 47}, {0, 31}, // 62: 0000 0011x }; static const plm_vlc_t PLM_VIDEO_MOTION[] = { {1 << 1, 0}, {0, 0}, // 0: x {2 << 1, 0}, {3 << 1, 0}, // 1: 0x {4 << 1, 0}, {5 << 1, 0}, // 2: 00x {0, 1}, {0, -1}, // 3: 01x {6 << 1, 0}, {7 << 1, 0}, // 4: 000x {0, 2}, {0, -2}, // 5: 001x {8 << 1, 0}, {9 << 1, 0}, // 6: 0000x {0, 3}, {0, -3}, // 7: 0001x {10 << 1, 0}, {11 << 1, 0}, // 8: 0000 0x {12 << 1, 0}, {13 << 1, 0}, // 9: 0000 1x {-1, 0}, {14 << 1, 0}, // 10: 0000 00x {15 << 1, 0}, {16 << 1, 0}, // 11: 0000 01x {17 << 1, 0}, {18 << 1, 0}, // 12: 0000 10x {0, 4}, {0, -4}, // 13: 0000 11x {-1, 0}, {19 << 1, 0}, // 14: 0000 001x {20 << 1, 0}, {21 << 1, 0}, // 15: 0000 010x {0, 7}, {0, -7}, // 16: 0000 011x {0, 6}, {0, -6}, // 17: 0000 100x {0, 5}, {0, -5}, // 18: 0000 101x {22 << 1, 0}, {23 << 1, 0}, // 19: 0000 0011x {24 << 1, 0}, {25 << 1, 0}, // 20: 0000 0100x {26 << 1, 0}, {27 << 1, 0}, // 21: 0000 0101x {28 << 1, 0}, {29 << 1, 0}, // 22: 0000 0011 0x {30 << 1, 0}, {31 << 1, 0}, // 23: 0000 0011 1x {32 << 1, 0}, {33 << 1, 0}, // 24: 0000 0100 0x {0, 10}, {0, -10}, // 25: 0000 0100 1x {0, 9}, {0, -9}, // 26: 0000 0101 0x {0, 8}, {0, -8}, // 27: 0000 0101 1x {0, 16}, {0, -16}, // 28: 0000 0011 00x {0, 15}, {0, -15}, // 29: 0000 0011 01x {0, 14}, {0, -14}, // 30: 0000 0011 10x {0, 13}, {0, -13}, // 31: 0000 0011 11x {0, 12}, {0, -12}, // 32: 0000 0100 00x {0, 11}, {0, -11}, // 33: 0000 0100 01x }; static const plm_vlc_t PLM_VIDEO_DCT_SIZE_LUMINANCE[] = { {1 << 1, 0}, {2 << 1, 0}, // 0: x {0, 1}, {0, 2}, // 1: 0x {3 << 1, 0}, {4 << 1, 0}, // 2: 1x {0, 0}, {0, 3}, // 3: 10x {0, 4}, {5 << 1, 0}, // 4: 11x {0, 5}, {6 << 1, 0}, // 5: 111x {0, 6}, {7 << 1, 0}, // 6: 1111x {0, 7}, {8 << 1, 0}, // 7: 1111 1x {0, 8}, {-1, 0}, // 8: 1111 11x }; static const plm_vlc_t PLM_VIDEO_DCT_SIZE_CHROMINANCE[] = { {1 << 1, 0}, {2 << 1, 0}, // 0: x {0, 0}, {0, 1}, // 1: 0x {0, 2}, {3 << 1, 0}, // 2: 1x {0, 3}, {4 << 1, 0}, // 3: 11x {0, 4}, {5 << 1, 0}, // 4: 111x {0, 5}, {6 << 1, 0}, // 5: 1111x {0, 6}, {7 << 1, 0}, // 6: 1111 1x {0, 7}, {8 << 1, 0}, // 7: 1111 11x {0, 8}, {-1, 0}, // 8: 1111 111x }; // dct_coeff bitmap: // 0xff00 run // 0x00ff level // Decoded values are unsigned. Sign bit follows in the stream. static const plm_vlc_uint_t PLM_VIDEO_DCT_COEFF[] = { {1 << 1, 0}, {0, 0x0001}, // 0: x {2 << 1, 0}, {3 << 1, 0}, // 1: 0x {4 << 1, 0}, {5 << 1, 0}, // 2: 00x {6 << 1, 0}, {0, 0x0101}, // 3: 01x {7 << 1, 0}, {8 << 1, 0}, // 4: 000x {9 << 1, 0}, {10 << 1, 0}, // 5: 001x {0, 0x0002}, {0, 0x0201}, // 6: 010x {11 << 1, 0}, {12 << 1, 0}, // 7: 0000x {13 << 1, 0}, {14 << 1, 0}, // 8: 0001x {15 << 1, 0}, {0, 0x0003}, // 9: 0010x {0, 0x0401}, {0, 0x0301}, // 10: 0011x {16 << 1, 0}, {0, 0xffff}, // 11: 0000 0x {17 << 1, 0}, {18 << 1, 0}, // 12: 0000 1x {0, 0x0701}, {0, 0x0601}, // 13: 0001 0x {0, 0x0102}, {0, 0x0501}, // 14: 0001 1x {19 << 1, 0}, {20 << 1, 0}, // 15: 0010 0x {21 << 1, 0}, {22 << 1, 0}, // 16: 0000 00x {0, 0x0202}, {0, 0x0901}, // 17: 0000 10x {0, 0x0004}, {0, 0x0801}, // 18: 0000 11x {23 << 1, 0}, {24 << 1, 0}, // 19: 0010 00x {25 << 1, 0}, {26 << 1, 0}, // 20: 0010 01x {27 << 1, 0}, {28 << 1, 0}, // 21: 0000 000x {29 << 1, 0}, {30 << 1, 0}, // 22: 0000 001x {0, 0x0d01}, {0, 0x0006}, // 23: 0010 000x {0, 0x0c01}, {0, 0x0b01}, // 24: 0010 001x {0, 0x0302}, {0, 0x0103}, // 25: 0010 010x {0, 0x0005}, {0, 0x0a01}, // 26: 0010 011x {31 << 1, 0}, {32 << 1, 0}, // 27: 0000 0000x {33 << 1, 0}, {34 << 1, 0}, // 28: 0000 0001x {35 << 1, 0}, {36 << 1, 0}, // 29: 0000 0010x {37 << 1, 0}, {38 << 1, 0}, // 30: 0000 0011x {39 << 1, 0}, {40 << 1, 0}, // 31: 0000 0000 0x {41 << 1, 0}, {42 << 1, 0}, // 32: 0000 0000 1x {43 << 1, 0}, {44 << 1, 0}, // 33: 0000 0001 0x {45 << 1, 0}, {46 << 1, 0}, // 34: 0000 0001 1x {0, 0x1001}, {0, 0x0502}, // 35: 0000 0010 0x {0, 0x0007}, {0, 0x0203}, // 36: 0000 0010 1x {0, 0x0104}, {0, 0x0f01}, // 37: 0000 0011 0x {0, 0x0e01}, {0, 0x0402}, // 38: 0000 0011 1x {47 << 1, 0}, {48 << 1, 0}, // 39: 0000 0000 00x {49 << 1, 0}, {50 << 1, 0}, // 40: 0000 0000 01x {51 << 1, 0}, {52 << 1, 0}, // 41: 0000 0000 10x {53 << 1, 0}, {54 << 1, 0}, // 42: 0000 0000 11x {55 << 1, 0}, {56 << 1, 0}, // 43: 0000 0001 00x {57 << 1, 0}, {58 << 1, 0}, // 44: 0000 0001 01x {59 << 1, 0}, {60 << 1, 0}, // 45: 0000 0001 10x {61 << 1, 0}, {62 << 1, 0}, // 46: 0000 0001 11x {-1, 0}, {63 << 1, 0}, // 47: 0000 0000 000x {64 << 1, 0}, {65 << 1, 0}, // 48: 0000 0000 001x {66 << 1, 0}, {67 << 1, 0}, // 49: 0000 0000 010x {68 << 1, 0}, {69 << 1, 0}, // 50: 0000 0000 011x {70 << 1, 0}, {71 << 1, 0}, // 51: 0000 0000 100x {72 << 1, 0}, {73 << 1, 0}, // 52: 0000 0000 101x {74 << 1, 0}, {75 << 1, 0}, // 53: 0000 0000 110x {76 << 1, 0}, {77 << 1, 0}, // 54: 0000 0000 111x {0, 0x000b}, {0, 0x0802}, // 55: 0000 0001 000x {0, 0x0403}, {0, 0x000a}, // 56: 0000 0001 001x {0, 0x0204}, {0, 0x0702}, // 57: 0000 0001 010x {0, 0x1501}, {0, 0x1401}, // 58: 0000 0001 011x {0, 0x0009}, {0, 0x1301}, // 59: 0000 0001 100x {0, 0x1201}, {0, 0x0105}, // 60: 0000 0001 101x {0, 0x0303}, {0, 0x0008}, // 61: 0000 0001 110x {0, 0x0602}, {0, 0x1101}, // 62: 0000 0001 111x {78 << 1, 0}, {79 << 1, 0}, // 63: 0000 0000 0001x {80 << 1, 0}, {81 << 1, 0}, // 64: 0000 0000 0010x {82 << 1, 0}, {83 << 1, 0}, // 65: 0000 0000 0011x {84 << 1, 0}, {85 << 1, 0}, // 66: 0000 0000 0100x {86 << 1, 0}, {87 << 1, 0}, // 67: 0000 0000 0101x {88 << 1, 0}, {89 << 1, 0}, // 68: 0000 0000 0110x {90 << 1, 0}, {91 << 1, 0}, // 69: 0000 0000 0111x {0, 0x0a02}, {0, 0x0902}, // 70: 0000 0000 1000x {0, 0x0503}, {0, 0x0304}, // 71: 0000 0000 1001x {0, 0x0205}, {0, 0x0107}, // 72: 0000 0000 1010x {0, 0x0106}, {0, 0x000f}, // 73: 0000 0000 1011x {0, 0x000e}, {0, 0x000d}, // 74: 0000 0000 1100x {0, 0x000c}, {0, 0x1a01}, // 75: 0000 0000 1101x {0, 0x1901}, {0, 0x1801}, // 76: 0000 0000 1110x {0, 0x1701}, {0, 0x1601}, // 77: 0000 0000 1111x {92 << 1, 0}, {93 << 1, 0}, // 78: 0000 0000 0001 0x {94 << 1, 0}, {95 << 1, 0}, // 79: 0000 0000 0001 1x {96 << 1, 0}, {97 << 1, 0}, // 80: 0000 0000 0010 0x {98 << 1, 0}, {99 << 1, 0}, // 81: 0000 0000 0010 1x {100 << 1, 0}, {101 << 1, 0}, // 82: 0000 0000 0011 0x {102 << 1, 0}, {103 << 1, 0}, // 83: 0000 0000 0011 1x {0, 0x001f}, {0, 0x001e}, // 84: 0000 0000 0100 0x {0, 0x001d}, {0, 0x001c}, // 85: 0000 0000 0100 1x {0, 0x001b}, {0, 0x001a}, // 86: 0000 0000 0101 0x {0, 0x0019}, {0, 0x0018}, // 87: 0000 0000 0101 1x {0, 0x0017}, {0, 0x0016}, // 88: 0000 0000 0110 0x {0, 0x0015}, {0, 0x0014}, // 89: 0000 0000 0110 1x {0, 0x0013}, {0, 0x0012}, // 90: 0000 0000 0111 0x {0, 0x0011}, {0, 0x0010}, // 91: 0000 0000 0111 1x {104 << 1, 0}, {105 << 1, 0}, // 92: 0000 0000 0001 00x {106 << 1, 0}, {107 << 1, 0}, // 93: 0000 0000 0001 01x {108 << 1, 0}, {109 << 1, 0}, // 94: 0000 0000 0001 10x {110 << 1, 0}, {111 << 1, 0}, // 95: 0000 0000 0001 11x {0, 0x0028}, {0, 0x0027}, // 96: 0000 0000 0010 00x {0, 0x0026}, {0, 0x0025}, // 97: 0000 0000 0010 01x {0, 0x0024}, {0, 0x0023}, // 98: 0000 0000 0010 10x {0, 0x0022}, {0, 0x0021}, // 99: 0000 0000 0010 11x {0, 0x0020}, {0, 0x010e}, // 100: 0000 0000 0011 00x {0, 0x010d}, {0, 0x010c}, // 101: 0000 0000 0011 01x {0, 0x010b}, {0, 0x010a}, // 102: 0000 0000 0011 10x {0, 0x0109}, {0, 0x0108}, // 103: 0000 0000 0011 11x {0, 0x0112}, {0, 0x0111}, // 104: 0000 0000 0001 000x {0, 0x0110}, {0, 0x010f}, // 105: 0000 0000 0001 001x {0, 0x0603}, {0, 0x1002}, // 106: 0000 0000 0001 010x {0, 0x0f02}, {0, 0x0e02}, // 107: 0000 0000 0001 011x {0, 0x0d02}, {0, 0x0c02}, // 108: 0000 0000 0001 100x {0, 0x0b02}, {0, 0x1f01}, // 109: 0000 0000 0001 101x {0, 0x1e01}, {0, 0x1d01}, // 110: 0000 0000 0001 110x {0, 0x1c01}, {0, 0x1b01}, // 111: 0000 0000 0001 111x }; long plmpegdecode_latency_; static plm_vlc_t *PLM_VIDEO_MACROBLOCK_TYPE[4]; static plm_vlc_t *PLM_VIDEO_DCT_SIZE[3]; #define plm_clamp(n) MIN(255, MAX(0, n)) void plm_video_destroy(plm_video_t *self) { if (self->destroy_buffer_when_done) { plm_buffer_destroy(self->buffer); } if (self->has_sequence_header) { free(self->frames_data); } free(self); } double plm_video_get_pixel_aspect_ratio(plm_video_t *self) { return self->pixel_aspect_ratio; } double plm_video_get_framerate(plm_video_t *self) { return self->framerate; } int plm_video_get_width(plm_video_t *self) { return self->width; } int plm_video_get_height(plm_video_t *self) { return self->height; } void plm_video_set_no_delay(plm_video_t *self, int no_delay) { self->assume_no_b_frames = no_delay; } double plm_video_get_time(plm_video_t *self) { return self->time; } void plm_video_rewind(plm_video_t *self) { plm_buffer_rewind(self->buffer); self->time = 0; self->frames_decoded = 0; self->has_reference_frame = false; } void plm_video_init_frame(plm_video_t *self, plm_frame_t *frame, uint8_t *base) { size_t plane_size = self->luma_width * self->luma_height; frame->width = self->width; frame->height = self->height; frame->y.width = self->luma_width; frame->y.height = self->luma_height; frame->y.data = base; frame->cr.width = self->chroma_width; frame->cr.height = self->chroma_height; frame->cr.data = base + plane_size; frame->cb.width = self->chroma_width; frame->cb.height = self->chroma_height; frame->cb.data = base + plane_size * 2; } void plm_video_decode_sequence_header(plm_video_t *self) { int previous_width = self->width; int previous_height = self->height; self->width = plm_buffer_read(self->buffer, 12); self->height = plm_buffer_read(self->buffer, 12); int pixel_aspect_ratio_code; pixel_aspect_ratio_code = plm_buffer_read(self->buffer, 4); pixel_aspect_ratio_code -= 1; pixel_aspect_ratio_code = MAX(pixel_aspect_ratio_code, 0); pixel_aspect_ratio_code = MIN(pixel_aspect_ratio_code, ARRAYLEN(PLM_VIDEO_PIXEL_ASPECT_RATIO) - 1); self->pixel_aspect_ratio = PLM_VIDEO_PIXEL_ASPECT_RATIO[pixel_aspect_ratio_code]; int framerate_code; framerate_code = plm_buffer_read(self->buffer, 4); framerate_code -= 1; framerate_code = MAX(framerate_code, 0); framerate_code = MIN(framerate_code, ARRAYLEN(PLM_VIDEO_PICTURE_RATE) - 1); self->framerate = PLM_VIDEO_PICTURE_RATE[framerate_code]; // skip bitRate, marker, bufferSize and constrained bit plm_buffer_skip(self->buffer, 18 + 1 + 10 + 1); if (plm_buffer_read(self->buffer, 1)) { // load custom intra quant matrix? for (int i = 0; i < 64; i++) { int idx = PLM_VIDEO_ZIG_ZAG[i]; self->intra_quant_matrix[idx] = plm_buffer_read(self->buffer, 8); } } else { memcpy(self->intra_quant_matrix, PLM_VIDEO_INTRAQUANT_MATRIX, 64); } if (plm_buffer_read(self->buffer, 1)) { // load custom non intra quant matrix? for (int i = 0; i < 64; i++) { int idx = PLM_VIDEO_ZIG_ZAG[i]; self->non_intra_quant_matrix[idx] = plm_buffer_read(self->buffer, 8); } } else { memcpy(self->non_intra_quant_matrix, PLM_VIDEO_NONINTRAQUANT_MATRIX, 64); } if (self->has_sequence_header) { if (self->width == previous_width && self->height == previous_height) { // We already had a sequence header with the same width/height; // nothing else to do here. return; } // We had a sequence header but with different dimensions; // delete the previous planes and allocate new. free(self->frames_data); } self->mb_width = (self->width + 15) >> 4; self->mb_height = (self->height + 15) >> 4; self->mb_size = self->mb_width * self->mb_height; self->luma_width = self->mb_width << 4; self->luma_height = self->mb_height << 4; self->chroma_width = self->mb_width << 3; self->chroma_height = self->mb_height << 3; size_t plane_size = self->luma_width * self->luma_height; self->frames_data = memalign(64, plane_size * 9); plm_video_init_frame(self, &self->frame_current, self->frames_data + plane_size * 0); plm_video_init_frame(self, &self->frame_forward, self->frames_data + plane_size * 3); plm_video_init_frame(self, &self->frame_backward, self->frames_data + plane_size * 6); self->has_sequence_header = true; INFOF("%s:\n" "\t%-20s = %15d;\n" "\t%-20s = %15d;\n" "\t%-20s = %15f;\n" "\t%-20s = %15f;\n" "\t%-20s = %15d;\n" "\t%-20s = %15d;\n" "\t%-20s = %15d;\n" "\t%-20s = %15d;\n" "\t%-20s = %15d;\n" "\t%-20s = %15d;\n" "\t%-20s = %15d;", "New MPEG Sequence", "width", self->width, "height", self->height, "framerate", self->framerate, "pixel_aspect_ratio", self->pixel_aspect_ratio, "mb_size", self->mb_size, "mb_width", self->mb_width, "mb_height", self->mb_height, "luma_width", self->luma_width, "luma_height", self->luma_height, "chroma_width", self->chroma_width, "chroma_height", self->chroma_height); } static void plm_video_copy_macroblock(plm_video_t *self, int motion_h, int motion_v, plm_frame_t *d) { plm_frame_t *s = &self->frame_current; plm_video_process_macroblock_16(self, s->y.data, d->y.data, motion_h, motion_v, false); plm_video_process_macroblock_8(self, s->cr.data, d->cr.data, motion_h / 2, motion_v / 2, false); plm_video_process_macroblock_8(self, s->cb.data, d->cb.data, motion_h / 2, motion_v / 2, false); } static void plm_video_interpolate_macroblock(plm_video_t *self, int motion_h, int motion_v, plm_frame_t *d) { plm_frame_t *s = &self->frame_current; plm_video_process_macroblock_16(self, s->y.data, d->y.data, motion_h, motion_v, true); plm_video_process_macroblock_8(self, s->cr.data, d->cr.data, motion_h / 2, motion_v / 2, true); plm_video_process_macroblock_8(self, s->cb.data, d->cb.data, motion_h / 2, motion_v / 2, true); } static int plm_video_decode_motion_vector(plm_video_t *self, int r_size, int motion) { int fscale = 1u << r_size; int m_code = plm_buffer_read_vlc(self->buffer, PLM_VIDEO_MOTION); int r = 0; int d; if ((m_code != 0) && (fscale != 1)) { r = plm_buffer_read(self->buffer, r_size); d = ((abs(m_code) - 1) << r_size) + r + 1; if (m_code < 0) { d = -d; } } else { d = m_code; } motion += d; if (motion > (fscale << 4) - 1) { motion -= fscale << 5; } else if (motion < (int)(((unsigned)-fscale) << 4)) { motion += fscale << 5; } return motion; } static void plm_video_decode_motion_vectors(plm_video_t *self) { // Forward if (self->motion_forward.is_set) { int r_size = self->motion_forward.r_size; self->motion_forward.h = plm_video_decode_motion_vector(self, r_size, self->motion_forward.h); self->motion_forward.v = plm_video_decode_motion_vector(self, r_size, self->motion_forward.v); } else if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE) { // No motion information in P-picture, reset vectors self->motion_forward.h = 0; self->motion_forward.v = 0; } if (self->motion_backward.is_set) { int r_size = self->motion_backward.r_size; self->motion_backward.h = plm_video_decode_motion_vector(self, r_size, self->motion_backward.h); self->motion_backward.v = plm_video_decode_motion_vector(self, r_size, self->motion_backward.v); } } static void plm_video_predict_macroblock(plm_video_t *self) { int fw_h = self->motion_forward.h; int fw_v = self->motion_forward.v; if (self->motion_forward.full_px) { fw_h <<= 1; fw_v <<= 1; } if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_B) { int bw_h = self->motion_backward.h; int bw_v = self->motion_backward.v; if (self->motion_backward.full_px) { bw_h <<= 1; bw_v <<= 1; } if (self->motion_forward.is_set) { plm_video_copy_macroblock(self, fw_h, fw_v, &self->frame_forward); if (self->motion_backward.is_set) { plm_video_interpolate_macroblock(self, bw_h, bw_v, &self->frame_backward); } } else { plm_video_copy_macroblock(self, bw_h, bw_v, &self->frame_backward); } } else { plm_video_copy_macroblock(self, fw_h, fw_v, &self->frame_forward); } } static void plm_video_decode_block(plm_video_t *self, int block) { int n = 0; uint8_t *quant_matrix; // Decode DC coefficient of intra-coded blocks if (self->macroblock_intra) { int predictor; int dct_size; // DC prediction int plane_index = block > 3 ? block - 3 : 0; predictor = self->dc_predictor[plane_index]; dct_size = plm_buffer_read_vlc(self->buffer, PLM_VIDEO_DCT_SIZE[plane_index]); // Read DC coeff if (dct_size > 0) { int differential = plm_buffer_read(self->buffer, dct_size); if ((differential & (1 << (dct_size - 1))) != 0) { self->block_data[0] = predictor + differential; } else { self->block_data[0] = predictor + ((-1u << dct_size) | (differential + 1)); } } else { self->block_data[0] = predictor; } // Save predictor value self->dc_predictor[plane_index] = self->block_data[0]; // Dequantize + premultiply self->block_data[0] <<= (3 + 5); quant_matrix = self->intra_quant_matrix; n = 1; } else { quant_matrix = self->non_intra_quant_matrix; } // Decode AC coefficients (+DC for non-intra) int level = 0; while (true) { int run = 0; uint16_t coeff = plm_buffer_read_vlc_uint(self->buffer, PLM_VIDEO_DCT_COEFF); if ((coeff == 0x0001) && (n > 0) && (plm_buffer_read(self->buffer, 1) == 0)) { // end_of_block break; } if (coeff == 0xffff) { // escape run = plm_buffer_read(self->buffer, 6); level = plm_buffer_read(self->buffer, 8); if (level == 0) { level = plm_buffer_read(self->buffer, 8); } else if (level == 128) { level = plm_buffer_read(self->buffer, 8) - 256; } else if (level > 128) { level = level - 256; } } else { run = coeff >> 8; level = coeff & 0xff; if (plm_buffer_read(self->buffer, 1)) { level = -level; } } n += run; if (n < 0 || n >= 64) { return; // invalid } int de_zig_zagged = PLM_VIDEO_ZIG_ZAG[n]; n++; // Dequantize, oddify, clip level = (unsigned)level << 1; if (!self->macroblock_intra) { level += (level < 0 ? -1 : 1); } level = (level * self->quantizer_scale * quant_matrix[de_zig_zagged]) >> 4; if ((level & 1) == 0) { level -= level > 0 ? 1 : -1; } if (level > 2047) { level = 2047; } else if (level < -2048) { level = -2048; } // Save premultiplied coefficient self->block_data[de_zig_zagged] = level * PLM_VIDEO_PREMULTIPLIER_MATRIX[de_zig_zagged]; } // Move block to its place uint8_t *d; int dw; int di; if (block < 4) { d = self->frame_current.y.data; dw = self->luma_width; di = (self->mb_row * self->luma_width + self->mb_col) << 4; if ((block & 1) != 0) { di += 8; } if ((block & 2) != 0) { di += self->luma_width << 3; } } else { d = (block == 4) ? self->frame_current.cb.data : self->frame_current.cr.data; dw = self->chroma_width; di = ((self->mb_row * self->luma_width) << 2) + (self->mb_col << 3); } int *s = self->block_data; int si = 0; if (self->macroblock_intra) { // Overwrite (no prediction) if (n == 1) { int clamped = plm_clamp((s[0] + 128) >> 8); PLM_BLOCK_SET(d, di, dw, si, 8, 8, clamped); s[0] = 0; } else { plm_video_idct(s); PLM_BLOCK_SET(d, di, dw, si, 8, 8, plm_clamp(s[si])); memset(self->block_data, 0, sizeof(self->block_data)); } } else { // Add data to the predicted macroblock if (n == 1) { int value = (s[0] + 128) >> 8; PLM_BLOCK_SET(d, di, dw, si, 8, 8, plm_clamp(d[di] + value)); s[0] = 0; } else { plm_video_idct(s); PLM_BLOCK_SET(d, di, dw, si, 8, 8, plm_clamp(d[di] + s[si])); memset(self->block_data, 0, sizeof(self->block_data)); } } } static void plm_video_decode_macroblock(plm_video_t *self) { // Decode self->macroblock_address_increment int increment = 0; int t = plm_buffer_read_vlc(self->buffer, PLM_VIDEO_MACROBLOCK_ADDRESS_INCREMENT); while (t == 34) { // macroblock_stuffing t = plm_buffer_read_vlc(self->buffer, PLM_VIDEO_MACROBLOCK_ADDRESS_INCREMENT); } while (t == 35) { // macroblock_escape increment += 33; t = plm_buffer_read_vlc(self->buffer, PLM_VIDEO_MACROBLOCK_ADDRESS_INCREMENT); } increment += t; // Process any skipped macroblocks if (self->slice_begin) { // The first self->macroblock_address_increment of each slice is relative // to beginning of the preverious row, not the preverious macroblock self->slice_begin = false; self->macroblock_address += increment; } else { if (self->macroblock_address + increment >= self->mb_size) { return; // invalid } if (increment > 1) { // Skipped macroblocks reset DC predictors self->dc_predictor[0] = 128; self->dc_predictor[1] = 128; self->dc_predictor[2] = 128; // Skipped macroblocks in P-pictures reset motion vectors if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE) { self->motion_forward.h = 0; self->motion_forward.v = 0; } } // Predict skipped macroblocks while (increment > 1) { self->macroblock_address++; self->mb_row = self->macroblock_address / self->mb_width; self->mb_col = self->macroblock_address % self->mb_width; plm_video_predict_macroblock(self); increment--; } self->macroblock_address++; } self->mb_row = self->macroblock_address / self->mb_width; self->mb_col = self->macroblock_address % self->mb_width; if (self->mb_col >= self->mb_width || self->mb_row >= self->mb_height) { return; // corrupt stream; } // Process the current macroblock // static const s16 *mbTable = MACROBLOCK_TYPE[self->picture_type]; // macroblock_type = read_huffman(self->bits, mbTable); const plm_vlc_t *table = PLM_VIDEO_MACROBLOCK_TYPE[self->picture_type]; self->macroblock_type = plm_buffer_read_vlc(self->buffer, table); self->macroblock_intra = (self->macroblock_type & 0x01); self->motion_forward.is_set = (self->macroblock_type & 0x08); self->motion_backward.is_set = (self->macroblock_type & 0x04); // Quantizer scale if ((self->macroblock_type & 0x10) != 0) { self->quantizer_scale = plm_buffer_read(self->buffer, 5); } if (self->macroblock_intra) { // Intra-coded macroblocks reset motion vectors self->motion_backward.h = self->motion_forward.h = 0; self->motion_backward.v = self->motion_forward.v = 0; } else { // Non-intra macroblocks reset DC predictors self->dc_predictor[0] = 128; self->dc_predictor[1] = 128; self->dc_predictor[2] = 128; plm_video_decode_motion_vectors(self); plm_video_predict_macroblock(self); } // Decode blocks int cbp = ((self->macroblock_type & 0x02) != 0) ? plm_buffer_read_vlc(self->buffer, PLM_VIDEO_CODE_BLOCK_PATTERN) : (self->macroblock_intra ? 0x3f : 0); for (int block = 0, mask = 0x20; block < 6; block++) { if ((cbp & mask) != 0) { plm_video_decode_block(self, block); } mask >>= 1; } } static void plm_video_decode_slice(plm_video_t *self, int slice) { self->slice_begin = true; self->macroblock_address = (slice - 1) * self->mb_width - 1; // Reset motion vectors and DC predictors self->motion_backward.h = self->motion_forward.h = 0; self->motion_backward.v = self->motion_forward.v = 0; self->dc_predictor[0] = 128; self->dc_predictor[1] = 128; self->dc_predictor[2] = 128; self->quantizer_scale = plm_buffer_read(self->buffer, 5); // Skip extra while (plm_buffer_read(self->buffer, 1)) { plm_buffer_skip(self->buffer, 8); } do { plm_video_decode_macroblock(self); } while (self->macroblock_address < self->mb_size - 1 && plm_buffer_no_start_code(self->buffer)); } static void plm_video_decode_picture(plm_video_t *self) { plm_buffer_skip(self->buffer, 10); // skip temporalReference self->picture_type = plm_buffer_read(self->buffer, 3); plm_buffer_skip(self->buffer, 16); // skip vbv_delay // D frames or unknown coding type if (self->picture_type <= 0 || self->picture_type > PLM_VIDEO_PICTURE_TYPE_B) { return; } // forward full_px, f_code if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE || self->picture_type == PLM_VIDEO_PICTURE_TYPE_B) { self->motion_forward.full_px = plm_buffer_read(self->buffer, 1); int f_code = plm_buffer_read(self->buffer, 3); if (f_code == 0) { // Ignore picture with zero f_code return; } self->motion_forward.r_size = f_code - 1; } // backward full_px, f_code if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_B) { self->motion_backward.full_px = plm_buffer_read(self->buffer, 1); int f_code = plm_buffer_read(self->buffer, 3); if (f_code == 0) { // Ignore picture with zero f_code return; } self->motion_backward.r_size = f_code - 1; } plm_frame_t frame_temp = self->frame_forward; if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_INTRA || self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE) { self->frame_forward = self->frame_backward; } // Skip extensions, user data do { self->start_code = plm_buffer_next_start_code(self->buffer); } while (self->start_code == PLM_START_EXTENSION || self->start_code == PLM_START_USER_DATA); while (self->start_code >= PLM_START_SLICE_FIRST && self->start_code <= PLM_START_SLICE_LAST) { plm_video_decode_slice(self, self->start_code & 0x000000FF); if (self->macroblock_address == self->mb_size - 1) { break; } self->start_code = plm_buffer_next_start_code(self->buffer); } // If this is a reference picutre rotate the prediction pointers if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_INTRA || self->picture_type == PLM_VIDEO_PICTURE_TYPE_PREDICTIVE) { self->frame_backward = self->frame_current; self->frame_current = frame_temp; } } static plm_frame_t *plm_video_decode_impl(plm_video_t *self) { plm_frame_t *frame = NULL; if (!self->has_sequence_header) { self->start_code = plm_buffer_find_start_code(self->buffer, PLM_START_SEQUENCE); if (self->start_code == -1) { return NULL; } plm_video_decode_sequence_header(self); } do { if (self->start_code != PLM_START_PICTURE) { self->start_code = plm_buffer_find_start_code(self->buffer, PLM_START_PICTURE); } if (self->start_code == -1) { return NULL; } plm_video_decode_picture(self); if (self->assume_no_b_frames) { frame = &self->frame_backward; } else if (self->picture_type == PLM_VIDEO_PICTURE_TYPE_B) { frame = &self->frame_current; } else if (self->has_reference_frame) { frame = &self->frame_forward; } else { self->has_reference_frame = true; } } while (!frame); frame->time = self->time; self->frames_decoded++; self->time = (double)self->frames_decoded / self->framerate; return frame; } plm_frame_t *plm_video_decode(plm_video_t *self) { long double tsc; plm_frame_t *res; INFOF("plm_video_decode"); tsc = nowl(); res = plm_video_decode_impl(self); plmpegdecode_latency_ = lroundl((nowl() - tsc) * 1e6l); return res; } plm_video_t *plm_video_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done) { plm_video_t *self = (plm_video_t *)memalign(64, sizeof(plm_video_t)); memset(self, 0, sizeof(plm_video_t)); self->buffer = buffer; self->destroy_buffer_when_done = destroy_when_done; self->start_code = plm_buffer_find_start_code(self->buffer, PLM_START_SEQUENCE); if (self->start_code != -1) { plm_video_decode_sequence_header(self); } return self; } static textstartup void plm_video_init(void) { PLM_VIDEO_MACROBLOCK_TYPE[0] = NULL; PLM_VIDEO_MACROBLOCK_TYPE[1] = PLM_VIDEO_MACROBLOCK_TYPE_INTRA; PLM_VIDEO_MACROBLOCK_TYPE[2] = PLM_VIDEO_MACROBLOCK_TYPE_PREDICTIVE, PLM_VIDEO_MACROBLOCK_TYPE[3] = PLM_VIDEO_MACROBLOCK_TYPE_B; PLM_VIDEO_DCT_SIZE[0] = PLM_VIDEO_DCT_SIZE_LUMINANCE; PLM_VIDEO_DCT_SIZE[1] = PLM_VIDEO_DCT_SIZE_CHROMINANCE; PLM_VIDEO_DCT_SIZE[2] = PLM_VIDEO_DCT_SIZE_CHROMINANCE; } const void *const plm_video_init_ctor[] initarray = {plm_video_init};
43,121
1,117
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/macroblock.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ │vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer │ │ Dominic Szablewski - https://phoboslab.org │ │ │ │ The MIT License(MIT) │ │ Copyright(c) 2019 Dominic Szablewski │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files(the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and / or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND │ │ NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE │ │ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN │ │ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN │ │ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE │ │ SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/mpeg/mpeg.h" #include "dsp/mpeg/video.h" #include "libc/log/check.h" forceinline void plm_video_process_macroblock(plm_video_t *self, uint8_t *restrict d, uint8_t *restrict s, int motion_h, int motion_v, bool interpolate, unsigned BW) { unsigned si, di, max_address; int y, x, dest_scan, source_scan, dw, hp, vp, odd_h, odd_v; dw = self->mb_width * BW; hp = motion_h >> 1; vp = motion_v >> 1; odd_h = (motion_h & 1) == 1; odd_v = (motion_v & 1) == 1; si = ((self->mb_row * BW) + vp) * dw + (self->mb_col * BW) + hp; di = (self->mb_row * dw + self->mb_col) * BW; max_address = (dw * (self->mb_height * BW - BW + 1) - BW); if (si > max_address || di > max_address) return; d += di; s += si; switch (((interpolate << 2) | (odd_h << 1) | (odd_v)) & 7) { case 0: dest_scan = dw - BW; source_scan = dw - BW; for (y = 0; y < BW; y++) { for (x = 0; x < BW; x++) { *d++ = *s++; } s += source_scan; d += dest_scan; } break; case 1: dest_scan = dw - BW; source_scan = dw - BW; for (y = 0; y < BW; y++) { for (x = 0; x < BW; x++) { *d++ = (s[0] + s[dw] + 1) >> 1; s++; } s += source_scan; d += dest_scan; } break; case 2: dest_scan = dw - BW; source_scan = dw - BW; for (y = 0; y < BW; y++) { for (x = 0; x < BW; x++) { *d++ = (s[0] + s[1] + 1) >> 1; s++; } s += source_scan; d += dest_scan; } break; case 3: dest_scan = dw - BW; source_scan = dw - BW; for (y = 0; y < BW; y++) { for (x = 0; x < BW; x++) { *d++ = (s[0] + s[1] + s[dw] + s[dw + 1] + 2) >> 2; s++; } s += source_scan; d += dest_scan; } break; case 4: dest_scan = dw - BW; source_scan = dw - BW; for (y = 0; y < BW; y++) { for (x = 0; x < BW; x++) { d[0] = (d[0] + (s[0]) + 1) >> 1; d++; s++; } s += source_scan; d += dest_scan; } break; case 5: dest_scan = dw - BW; source_scan = dw - BW; for (y = 0; y < BW; y++) { for (x = 0; x < BW; x++) { d[0] = (d[0] + ((s[0] + s[dw] + 1) >> 1) + 1) >> 1; d++; s++; } s += source_scan; d += dest_scan; } break; case 6: dest_scan = dw - BW; source_scan = dw - BW; for (y = 0; y < BW; y++) { for (x = 0; x < BW; x++) { d[0] = (d[0] + ((s[0] + s[1] + 1) >> 1) + 1) >> 1; d++; s++; } s += source_scan; d += dest_scan; } break; case 7: dest_scan = dw - BW; source_scan = dw - BW; for (y = 0; y < BW; y++) { for (x = 0; x < BW; x++) { d[0] = (d[0] + ((s[0] + s[1] + s[dw] + s[dw + 1] + 2) >> 2) + 1) >> 1; d++; s++; } s += source_scan; d += dest_scan; } break; default: break; } } void plm_video_process_macroblock_8(plm_video_t *self, uint8_t *restrict d, uint8_t *restrict s, int motion_h, int motion_v, bool interpolate) { DCHECK_ALIGNED(8, d); DCHECK_ALIGNED(8, s); plm_video_process_macroblock(self, d, s, motion_h, motion_v, interpolate, 8); } void plm_video_process_macroblock_16(plm_video_t *self, uint8_t *restrict d, uint8_t *restrict s, int motion_h, int motion_v, bool interpolate) { DCHECK_ALIGNED(16, d); DCHECK_ALIGNED(16, s); plm_video_process_macroblock(self, d, s, motion_h, motion_v, interpolate, 16); }
6,797
172
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/demux.h
#ifndef COSMOPOLITAN_DSP_MPEG_DEMUX_H_ #define COSMOPOLITAN_DSP_MPEG_DEMUX_H_ #include "dsp/mpeg/mpeg.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ #define START_PACK 0xBA #define START_END 0xB9 #define START_SYSTEM 0xBB typedef struct plm_demux_t { plm_buffer_t *buffer; int destroy_buffer_when_done; double system_clock_ref; int has_pack_header; int has_system_header; int num_audio_streams; int num_video_streams; plm_packet_t current_packet; plm_packet_t next_packet; } plm_demux_t; double plm_demux_read_time(plm_demux_t *self); void plm_demux_decode_pack_header(plm_demux_t *self); void plm_demux_decode_system_header(plm_demux_t *self); plm_packet_t *plm_demux_decode_packet(plm_demux_t *self, int start_code); plm_packet_t *plm_demux_get_packet(plm_demux_t *self); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_MPEG_DEMUX_H_ */
923
32
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/buffer.h
#ifndef COSMOPOLITAN_DSP_MPEG_BUFFER_H_ #define COSMOPOLITAN_DSP_MPEG_BUFFER_H_ #include "dsp/mpeg/mpeg.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ struct FILE; enum plm_buffer_mode { PLM_BUFFER_MODE_FILE, PLM_BUFFER_MODE_FIXED_MEM, PLM_BUFFER_MODE_DYNAMIC_MEM }; typedef struct plm_buffer_t { unsigned bit_index; unsigned capacity; unsigned length; int free_when_done; int close_when_done; struct FILE *fh; plm_buffer_load_callback load_callback; void *load_callback_user_data; unsigned char *bytes; enum plm_buffer_mode mode; } plm_buffer_t; typedef struct { int16_t index; int16_t value; } plm_vlc_t; typedef struct { int16_t index; uint16_t value; } plm_vlc_uint_t; /* bool plm_buffer_has(plm_buffer_t *, size_t); */ /* int plm_buffer_read(plm_buffer_t *, int); */ /* void plm_buffer_align(plm_buffer_t *); */ /* void plm_buffer_skip(plm_buffer_t *, size_t); */ /* int plm_buffer_skip_bytes(plm_buffer_t *, unsigned char); */ /* int plm_buffer_next_start_code(plm_buffer_t *); */ /* int plm_buffer_find_start_code(plm_buffer_t *, int); */ /* int plm_buffer_no_start_code(plm_buffer_t *); */ /* int16_t plm_buffer_read_vlc(plm_buffer_t *, const plm_vlc_t *); */ /* uint16_t plm_buffer_read_vlc_uint(plm_buffer_t *, const plm_vlc_uint_t *); */ void plm_buffer_discard_read_bytes(plm_buffer_t *); relegated void plm_buffer_load_file_callback(plm_buffer_t *, void *); forceinline bool plm_buffer_has(plm_buffer_t *b, size_t bits) { unsigned have; have = b->length; have <<= 3; have -= b->bit_index; if (bits <= have) { return true; } else { if (b->load_callback) { b->load_callback(b, b->load_callback_user_data); return ((b->length << 3) - b->bit_index) >= bits; } else { return false; } } } forceinline int plm_buffer_read(plm_buffer_t *self, int count) { if (!plm_buffer_has(self, count)) return 0; int value = 0; while (count) { int current_byte = self->bytes[self->bit_index >> 3]; int remaining = 8 - (self->bit_index & 7); // Remaining bits in byte int read = remaining < count ? remaining : count; // Bits in self run int shift = remaining - read; int mask = (0xff >> (8 - read)); value = (value << read) | ((current_byte & (mask << shift)) >> shift); self->bit_index += read; count -= read; } return value; } forceinline void plm_buffer_align(plm_buffer_t *self) { self->bit_index = ((self->bit_index + 7) >> 3) << 3; } forceinline void plm_buffer_skip(plm_buffer_t *self, size_t count) { if (plm_buffer_has(self, count)) { self->bit_index += count; } } forceinline int plm_buffer_skip_bytes(plm_buffer_t *self, unsigned char v) { unsigned skipped; plm_buffer_align(self); skipped = 0; while (plm_buffer_has(self, 8)) { if (v == self->bytes[self->bit_index >> 3]) { self->bit_index += 8; ++skipped; } else { break; } } return skipped; } forceinline int plm_buffer_next_start_code(plm_buffer_t *self) { plm_buffer_align(self); while (plm_buffer_has(self, (5 << 3))) { size_t byte_index = (self->bit_index) >> 3; if (self->bytes[byte_index] == 0x00 && self->bytes[byte_index + 1] == 0x00 && self->bytes[byte_index + 2] == 0x01) { self->bit_index = (byte_index + 4) << 3; return self->bytes[byte_index + 3]; } self->bit_index += 8; } self->bit_index = (self->length << 3); return -1; } forceinline int plm_buffer_find_start_code(plm_buffer_t *self, int code) { int current = 0; while (true) { current = plm_buffer_next_start_code(self); if (current == code || current == -1) { return current; } } return -1; } forceinline int plm_buffer_no_start_code(plm_buffer_t *self) { if (!plm_buffer_has(self, (5 << 3))) { return false; } size_t byte_index = ((self->bit_index + 7) >> 3); return !(self->bytes[byte_index] == 0x00 && self->bytes[byte_index + 1] == 0x00 && self->bytes[byte_index + 2] == 0x01); } forceinline int16_t plm_buffer_read_vlc(plm_buffer_t *self, const plm_vlc_t *table) { plm_vlc_t state = {0, 0}; do { state = table[state.index + plm_buffer_read(self, 1)]; } while (state.index > 0); return state.value; } forceinline uint16_t plm_buffer_read_vlc_uint(plm_buffer_t *self, const plm_vlc_uint_t *table) { return (uint16_t)plm_buffer_read_vlc(self, (plm_vlc_t *)table); } COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_MPEG_BUFFER_H_ */
4,639
164
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/mpeg.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += DSP_MPEG DSP_MPEG_ARTIFACTS += DSP_MPEG_A DSP_MPEG = $(DSP_MPEG_A_DEPS) $(DSP_MPEG_A) DSP_MPEG_A = o/$(MODE)/dsp/mpeg/mpeg.a DSP_MPEG_A_FILES := $(wildcard dsp/mpeg/*) DSP_MPEG_A_HDRS = $(filter %.h,$(DSP_MPEG_A_FILES)) DSP_MPEG_A_SRCS_S = $(filter %.S,$(DSP_MPEG_A_FILES)) DSP_MPEG_A_SRCS_C = $(filter %.c,$(DSP_MPEG_A_FILES)) DSP_MPEG_A_SRCS = \ $(DSP_MPEG_A_SRCS_S) \ $(DSP_MPEG_A_SRCS_C) DSP_MPEG_A_OBJS = \ $(DSP_MPEG_A_SRCS_S:%.S=o/$(MODE)/%.o) \ $(DSP_MPEG_A_SRCS_C:%.c=o/$(MODE)/%.o) DSP_MPEG_A_CHECKS = \ $(DSP_MPEG_A).pkg \ $(DSP_MPEG_A_HDRS:%=o/$(MODE)/%.ok) DSP_MPEG_A_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_LOG \ LIBC_LOG \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_STDIO \ LIBC_STR \ LIBC_STUBS \ LIBC_SYSV \ LIBC_TIME \ LIBC_TINYMATH \ THIRD_PARTY_COMPILER_RT DSP_MPEG_A_DEPS := \ $(call uniq,$(foreach x,$(DSP_MPEG_A_DIRECTDEPS),$($(x)))) $(DSP_MPEG_A): dsp/mpeg/ \ $(DSP_MPEG_A).pkg \ $(DSP_MPEG_A_OBJS) $(DSP_MPEG_A).pkg: \ $(DSP_MPEG_A_OBJS) \ $(foreach x,$(DSP_MPEG_A_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/dsp/mpeg/clamp4int256-k8.o: private \ OVERRIDE_CFLAGS += \ -Os DSP_MPEG_LIBS = $(foreach x,$(DSP_MPEG_ARTIFACTS),$($(x))) DSP_MPEG_SRCS = $(foreach x,$(DSP_MPEG_ARTIFACTS),$($(x)_SRCS)) DSP_MPEG_HDRS = $(foreach x,$(DSP_MPEG_ARTIFACTS),$($(x)_HDRS)) DSP_MPEG_CHECKS = $(foreach x,$(DSP_MPEG_ARTIFACTS),$($(x)_CHECKS)) DSP_MPEG_OBJS = $(foreach x,$(DSP_MPEG_ARTIFACTS),$($(x)_OBJS)) $(DSP_MPEG_OBJS): $(BUILD_FILES) dsp/mpeg/mpeg.mk .PHONY: o/$(MODE)/dsp/mpeg o/$(MODE)/dsp/mpeg: $(DSP_MPEG_CHECKS)
1,872
67
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/clamp4int256-core.S
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ │vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" clamp4int256$core: .leafprologue pxor %xmm1,%xmm1 pmaxsd %xmm1,%xmm0 pminsd 0f(%rip),%xmm0 .leafepilogue .endfn clamp4int256$core,globl .rodata.cst16 0: .long 255,255,255,255
2,054
31
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/mp2.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ │vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer │ │ Dominic Szablewski - https://phoboslab.org │ │ │ │ The MIT License(MIT) │ │ Copyright(c) 2019 Dominic Szablewski │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files(the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and / or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND │ │ NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE │ │ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN │ │ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN │ │ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE │ │ SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/mpeg/buffer.h" #include "dsp/mpeg/mpeg.h" #include "libc/log/log.h" #include "libc/mem/mem.h" #include "libc/str/str.h" asm(".ident\t\"\\n\\n\ PL_MPEG (MIT License)\\n\ Copyright(c) 2019 Dominic Szablewski\\n\ https://phoboslab.org\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ // ----------------------------------------------------------------------------- // plm_audio implementation // Based on kjmp2 by Martin J. Fiedler // http://keyj.emphy.de/kjmp2/ #define PLM_AUDIO_FRAME_SYNC 0x7ff #define PLM_AUDIO_MPEG_2_5 0x0 #define PLM_AUDIO_MPEG_2 0x2 #define PLM_AUDIO_MPEG_1 0x3 #define PLM_AUDIO_LAYER_III 0x1 #define PLM_AUDIO_LAYER_II 0x2 #define PLM_AUDIO_LAYER_I 0x3 #define PLM_AUDIO_MODE_STEREO 0x0 #define PLM_AUDIO_MODE_JOINT_STEREO 0x1 #define PLM_AUDIO_MODE_DUAL_CHANNEL 0x2 #define PLM_AUDIO_MODE_MONO 0x3 static const unsigned short PLM_AUDIO_SAMPLE_RATE[] = { 44100, 48000, 32000, 0, // MPEG-1 22050, 24000, 16000, 0 // MPEG-2 }; static const short PLM_AUDIO_BIT_RATE[] = { 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, // MPEG-1 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 // MPEG-2 }; static const int PLM_AUDIO_SCALEFACTOR_BASE[] = { 0x02000000, 0x01965FEA, 0x01428A30 }; static const float PLM_AUDIO_SYNTHESIS_WINDOW[] = { 0.0, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -1.0, -1.0, -1.0, -1.0, -1.5, -1.5, -2.0, -2.0, -2.5, -2.5, -3.0, -3.5, -3.5, -4.0, -4.5, -5.0, -5.5, -6.5, -7.0, -8.0, -8.5, -9.5, -10.5, -12.0, -13.0, -14.5, -15.5, -17.5, -19.0, -20.5, -22.5, -24.5, -26.5, -29.0, -31.5, -34.0, -36.5, -39.5, -42.5, -45.5, -48.5, -52.0, -55.5, -58.5, -62.5, -66.0, -69.5, -73.5, -77.0, -80.5, -84.5, -88.0, -91.5, -95.0, -98.0, -101.0, -104.0, 106.5, 109.0, 111.0, 112.5, 113.5, 114.0, 114.0, 113.5, 112.0, 110.5, 107.5, 104.0, 100.0, 94.5, 88.5, 81.5, 73.0, 63.5, 53.0, 41.5, 28.5, 14.5, -1.0, -18.0, -36.0, -55.5, -76.5, -98.5, -122.0, -147.0, -173.5, -200.5, -229.5, -259.5, -290.5, -322.5, -355.5, -389.5, -424.0, -459.5, -495.5, -532.0, -568.5, -605.0, -641.5, -678.0, -714.0, -749.0, -783.5, -817.0, -849.0, -879.5, -908.5, -935.0, -959.5, -981.0, -1000.5, -1016.0, -1028.5, -1037.5, -1042.5, -1043.5, -1040.0, -1031.5, 1018.5, 1000.0, 976.0, 946.5, 911.0, 869.5, 822.0, 767.5, 707.0, 640.0, 565.5, 485.0, 397.0, 302.5, 201.0, 92.5, -22.5, -144.0, -272.5, -407.0, -547.5, -694.0, -846.0, -1003.0, -1165.0, -1331.5, -1502.0, -1675.5, -1852.5, -2031.5, -2212.5, -2394.0, -2576.5, -2758.5, -2939.5, -3118.5, -3294.5, -3467.5, -3635.5, -3798.5, -3955.0, -4104.5, -4245.5, -4377.5, -4499.0, -4609.5, -4708.0, -4792.5, -4863.5, -4919.0, -4958.0, -4979.5, -4983.0, -4967.5, -4931.5, -4875.0, -4796.0, -4694.5, -4569.5, -4420.0, -4246.0, -4046.0, -3820.0, -3567.0, 3287.0, 2979.5, 2644.0, 2280.5, 1888.0, 1467.5, 1018.5, 541.0, 35.0, -499.0, -1061.0, -1650.0, -2266.5, -2909.0, -3577.0, -4270.0, -4987.5, -5727.5, -6490.0, -7274.0, -8077.5, -8899.5, -9739.0, -10594.5, -11464.5, -12347.0, -13241.0, -14144.5, -15056.0, -15973.5, -16895.5, -17820.0, -18744.5, -19668.0, -20588.0, -21503.0, -22410.5, -23308.5, -24195.0, -25068.5, -25926.5, -26767.0, -27589.0, -28389.0, -29166.5, -29919.0, -30644.5, -31342.0, -32009.5, -32645.0, -33247.0, -33814.5, -34346.0, -34839.5, -35295.0, -35710.0, -36084.5, -36417.5, -36707.5, -36954.0, -37156.5, -37315.0, -37428.0, -37496.0, 37519.0, 37496.0, 37428.0, 37315.0, 37156.5, 36954.0, 36707.5, 36417.5, 36084.5, 35710.0, 35295.0, 34839.5, 34346.0, 33814.5, 33247.0, 32645.0, 32009.5, 31342.0, 30644.5, 29919.0, 29166.5, 28389.0, 27589.0, 26767.0, 25926.5, 25068.5, 24195.0, 23308.5, 22410.5, 21503.0, 20588.0, 19668.0, 18744.5, 17820.0, 16895.5, 15973.5, 15056.0, 14144.5, 13241.0, 12347.0, 11464.5, 10594.5, 9739.0, 8899.5, 8077.5, 7274.0, 6490.0, 5727.5, 4987.5, 4270.0, 3577.0, 2909.0, 2266.5, 1650.0, 1061.0, 499.0, -35.0, -541.0, -1018.5, -1467.5, -1888.0, -2280.5, -2644.0, -2979.5, 3287.0, 3567.0, 3820.0, 4046.0, 4246.0, 4420.0, 4569.5, 4694.5, 4796.0, 4875.0, 4931.5, 4967.5, 4983.0, 4979.5, 4958.0, 4919.0, 4863.5, 4792.5, 4708.0, 4609.5, 4499.0, 4377.5, 4245.5, 4104.5, 3955.0, 3798.5, 3635.5, 3467.5, 3294.5, 3118.5, 2939.5, 2758.5, 2576.5, 2394.0, 2212.5, 2031.5, 1852.5, 1675.5, 1502.0, 1331.5, 1165.0, 1003.0, 846.0, 694.0, 547.5, 407.0, 272.5, 144.0, 22.5, -92.5, -201.0, -302.5, -397.0, -485.0, -565.5, -640.0, -707.0, -767.5, -822.0, -869.5, -911.0, -946.5, -976.0, -1000.0, 1018.5, 1031.5, 1040.0, 1043.5, 1042.5, 1037.5, 1028.5, 1016.0, 1000.5, 981.0, 959.5, 935.0, 908.5, 879.5, 849.0, 817.0, 783.5, 749.0, 714.0, 678.0, 641.5, 605.0, 568.5, 532.0, 495.5, 459.5, 424.0, 389.5, 355.5, 322.5, 290.5, 259.5, 229.5, 200.5, 173.5, 147.0, 122.0, 98.5, 76.5, 55.5, 36.0, 18.0, 1.0, -14.5, -28.5, -41.5, -53.0, -63.5, -73.0, -81.5, -88.5, -94.5, -100.0, -104.0, -107.5, -110.5, -112.0, -113.5, -114.0, -114.0, -113.5, -112.5, -111.0, -109.0, 106.5, 104.0, 101.0, 98.0, 95.0, 91.5, 88.0, 84.5, 80.5, 77.0, 73.5, 69.5, 66.0, 62.5, 58.5, 55.5, 52.0, 48.5, 45.5, 42.5, 39.5, 36.5, 34.0, 31.5, 29.0, 26.5, 24.5, 22.5, 20.5, 19.0, 17.5, 15.5, 14.5, 13.0, 12.0, 10.5, 9.5, 8.5, 8.0, 7.0, 6.5, 5.5, 5.0, 4.5, 4.0, 3.5, 3.5, 3.0, 2.5, 2.5, 2.0, 2.0, 1.5, 1.5, 1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 }; // Quantizer lookup, step 1: bitrate classes static const uint8_t PLM_AUDIO_QUANT_LUT_STEP_1[2][16] = { // 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384 <- bitrate { 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, // mono // 16, 24, 28, 32, 40, 48, 56, 64, 80, 96,112,128,160,192 <- bitrate / chan { 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2 } // stereo }; // Quantizer lookup, step 2: bitrate class, sample rate -> B2 table idx, sblimit static const uint8_t PLM_AUDIO_QUANT_TAB_A = (27 | 64); // Table 3-B.2a: high-rate, sblimit = 27 static const uint8_t PLM_AUDIO_QUANT_TAB_B = (30 | 64); // Table 3-B.2b: high-rate, sblimit = 30 static const uint8_t PLM_AUDIO_QUANT_TAB_C = 8; // Table 3-B.2c: low-rate, sblimit = 8 static const uint8_t PLM_AUDIO_QUANT_TAB_D = 12; // Table 3-B.2d: low-rate, sblimit = 12 static const uint8_t QUANT_LUT_STEP_2[3][3] = { // 44.1 kHz, 48 kHz, 32 kHz { PLM_AUDIO_QUANT_TAB_C, PLM_AUDIO_QUANT_TAB_C, PLM_AUDIO_QUANT_TAB_D }, // 32 - 48 kbit/sec/ch { PLM_AUDIO_QUANT_TAB_A, PLM_AUDIO_QUANT_TAB_A, PLM_AUDIO_QUANT_TAB_A }, // 56 - 80 kbit/sec/ch { PLM_AUDIO_QUANT_TAB_B, PLM_AUDIO_QUANT_TAB_A, PLM_AUDIO_QUANT_TAB_B } // 96+ kbit/sec/ch }; // Quantizer lookup, step 3: B2 table, subband -> nbal, row index // (upper 4 bits: nbal, lower 4 bits: row index) static const uint8_t PLM_AUDIO_QUANT_LUT_STEP_3[3][32] = { // Low-rate table (3-B.2c and 3-B.2d) { 0x44,0x44, 0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34,0x34 }, // High-rate table (3-B.2a and 3-B.2b) { 0x43,0x43,0x43, 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42, 0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31, 0x20,0x20,0x20,0x20,0x20,0x20,0x20 }, // MPEG-2 LSR table (B.2 in ISO 13818-3) { 0x45,0x45,0x45,0x45, 0x34,0x34,0x34,0x34,0x34,0x34,0x34, 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24, 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24 } }; // Quantizer lookup, step 4: table row, allocation[] value -> quant table index static const uint8_t PLM_AUDIO_QUANT_LUT_STEP4[6][16] = { { 0, 1, 2, 17 }, { 0, 1, 2, 3, 4, 5, 6, 17 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17 }, { 0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }, { 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } }; typedef struct plm_quantizer_spec_t { unsigned short levels; unsigned char group; unsigned char bits; } plm_quantizer_spec_t; static const plm_quantizer_spec_t PLM_AUDIO_QUANT_TAB[] = { { 3, 1, 5 }, // 1 { 5, 1, 7 }, // 2 { 7, 0, 3 }, // 3 { 9, 1, 10 }, // 4 { 15, 0, 4 }, // 5 { 31, 0, 5 }, // 6 { 63, 0, 6 }, // 7 { 127, 0, 7 }, // 8 { 255, 0, 8 }, // 9 { 511, 0, 9 }, // 10 { 1023, 0, 10 }, // 11 { 2047, 0, 11 }, // 12 { 4095, 0, 12 }, // 13 { 8191, 0, 13 }, // 14 { 16383, 0, 14 }, // 15 { 32767, 0, 15 }, // 16 { 65535, 0, 16 } // 17 }; struct plm_audio_t { double time; int samples_decoded; int samplerate_index; int bitrate_index; int version; int layer; int mode; int bound; int v_pos; int next_frame_data_size; plm_buffer_t *buffer; int destroy_buffer_when_done; const plm_quantizer_spec_t *allocation[2][32]; uint8_t scale_factor_info[2][32]; int scale_factor[2][32][3]; int sample[2][32][3]; plm_samples_t samples; float D[1024]; float V[1024]; float U[32]; } forcealign(64); typedef plm_audio_t plm_audio_t; int plm_audio_decode_header(plm_audio_t *self); void plm_audio_decode_frame(plm_audio_t *self); const plm_quantizer_spec_t *plm_audio_read_allocation(plm_audio_t *self, int sb, int tab3); void plm_audio_read_samples(plm_audio_t *self, int ch, int sb, int part); void plm_audio_matrix_transform(int s[32][3], int ss, float *d, int dp); plm_audio_t *plm_audio_create_with_buffer(plm_buffer_t *buffer, int destroy_when_done) { plm_audio_t *self = (plm_audio_t *)memalign(_Alignof(plm_audio_t), sizeof(plm_audio_t)); memset(self, 0, sizeof(plm_audio_t)); self->samples.count = PLM_AUDIO_SAMPLES_PER_FRAME; self->buffer = buffer; self->destroy_buffer_when_done = destroy_when_done; self->samplerate_index = 3; // indicates 0 samplerate memcpy(self->D, PLM_AUDIO_SYNTHESIS_WINDOW, 512 * sizeof(float)); memcpy(self->D + 512, PLM_AUDIO_SYNTHESIS_WINDOW, 512 * sizeof(float)); // Decode first header if (plm_buffer_has(self->buffer, 48)) { self->next_frame_data_size = plm_audio_decode_header(self); } return self; } void plm_audio_destroy(plm_audio_t *self) { if (self->destroy_buffer_when_done) { plm_buffer_destroy(self->buffer); } free(self); } int plm_audio_get_samplerate(plm_audio_t *self) { return PLM_AUDIO_SAMPLE_RATE[self->samplerate_index]; } double plm_audio_get_time(plm_audio_t *self) { return self->time; } void plm_audio_rewind(plm_audio_t *self) { plm_buffer_rewind(self->buffer); self->time = 0; self->samples_decoded = 0; self->next_frame_data_size = 0; // TODO: needed? memset(self->V, 0, sizeof(self->V)); memset(self->U, 0, sizeof(self->U)); } plm_samples_t *plm_audio_decode(plm_audio_t *self) { DEBUGF("%s", "plm_audio_decode"); // Do we have at least enough information to decode the frame header? if (!self->next_frame_data_size) { if (!plm_buffer_has(self->buffer, 48)) { return NULL; } self->next_frame_data_size = plm_audio_decode_header(self); } if ( self->next_frame_data_size == 0 || !plm_buffer_has(self->buffer, self->next_frame_data_size << 3) ) { return NULL; } plm_audio_decode_frame(self); self->next_frame_data_size = 0; self->samples.time = self->time; self->samples_decoded += PLM_AUDIO_SAMPLES_PER_FRAME; self->time = (double)self->samples_decoded / (double)PLM_AUDIO_SAMPLE_RATE[self->samplerate_index]; return &self->samples; } int plm_audio_decode_header(plm_audio_t *self) { // Check for valid header: syncword OK, MPEG-Audio Layer 2 plm_buffer_skip_bytes(self->buffer, 0x00); int sync = plm_buffer_read(self->buffer, 11); self->version = plm_buffer_read(self->buffer, 2); self->layer = plm_buffer_read(self->buffer, 2); int hasCRC = !plm_buffer_read(self->buffer, 1); if ( sync != PLM_AUDIO_FRAME_SYNC || self->version != PLM_AUDIO_MPEG_1 || self->layer != PLM_AUDIO_LAYER_II ) { return false; // Invalid header or unsupported version } self->bitrate_index = plm_buffer_read(self->buffer, 4) - 1; if (self->bitrate_index > 13) { return false; // Invalid bit rate or 'free format' } self->samplerate_index = plm_buffer_read(self->buffer, 2); if (self->samplerate_index == 3) { return false; // Invalid sample rate } if (self->version == PLM_AUDIO_MPEG_2) { self->samplerate_index += 4; self->bitrate_index += 14; } int padding = plm_buffer_read(self->buffer, 1); plm_buffer_skip(self->buffer, 1); // f_private self->mode = plm_buffer_read(self->buffer, 2); // Parse the mode_extension, set up the stereo bound self->bound = 0; if (self->mode == PLM_AUDIO_MODE_JOINT_STEREO) { self->bound = (plm_buffer_read(self->buffer, 2) + 1) << 2; } else { plm_buffer_skip(self->buffer, 2); self->bound = (self->mode == PLM_AUDIO_MODE_MONO) ? 0 : 32; } // Discard the last 4 bits of the header and the CRC value, if present plm_buffer_skip(self->buffer, 4); if (hasCRC) { plm_buffer_skip(self->buffer, 16); } // Compute frame size, check if we have enough data to decode the whole // frame. int bitrate = PLM_AUDIO_BIT_RATE[self->bitrate_index]; int samplerate = PLM_AUDIO_SAMPLE_RATE[self->samplerate_index]; int frame_size = (144000 * bitrate / samplerate) + padding; return frame_size - (hasCRC ? 6 : 4); } void plm_audio_decode_frame(plm_audio_t *self) { // Prepare the quantizer table lookups int tab3 = 0; int sblimit = 0; if (self->version == PLM_AUDIO_MPEG_2) { // MPEG-2 (LSR) tab3 = 2; sblimit = 30; } else { // MPEG-1 int tab1 = (self->mode == PLM_AUDIO_MODE_MONO) ? 0 : 1; int tab2 = PLM_AUDIO_QUANT_LUT_STEP_1[tab1][self->bitrate_index]; tab3 = QUANT_LUT_STEP_2[tab2][self->samplerate_index]; sblimit = tab3 & 63; tab3 >>= 6; } if (self->bound > sblimit) { self->bound = sblimit; } // Read the allocation information for (int sb = 0; sb < self->bound; sb++) { self->allocation[0][sb] = plm_audio_read_allocation(self, sb, tab3); self->allocation[1][sb] = plm_audio_read_allocation(self, sb, tab3); } for (int sb = self->bound; sb < sblimit; sb++) { self->allocation[0][sb] = self->allocation[1][sb] = plm_audio_read_allocation(self, sb, tab3); } // Read scale factor selector information int channels = (self->mode == PLM_AUDIO_MODE_MONO) ? 1 : 2; for (int sb = 0; sb < sblimit; sb++) { for (int ch = 0; ch < channels; ch++) { if (self->allocation[ch][sb]) { self->scale_factor_info[ch][sb] = plm_buffer_read(self->buffer, 2); } } if (self->mode == PLM_AUDIO_MODE_MONO) { self->scale_factor_info[1][sb] = self->scale_factor_info[0][sb]; } } // Read scale factors for (int sb = 0; sb < sblimit; sb++) { for (int ch = 0; ch < channels; ch++) { if (self->allocation[ch][sb]) { int *sf = self->scale_factor[ch][sb]; switch (self->scale_factor_info[ch][sb]) { case 0: sf[0] = plm_buffer_read(self->buffer, 6); sf[1] = plm_buffer_read(self->buffer, 6); sf[2] = plm_buffer_read(self->buffer, 6); break; case 1: sf[0] = sf[1] = plm_buffer_read(self->buffer, 6); sf[2] = plm_buffer_read(self->buffer, 6); break; case 2: sf[0] = sf[1] = sf[2] = plm_buffer_read(self->buffer, 6); break; case 3: sf[0] = plm_buffer_read(self->buffer, 6); sf[1] = sf[2] = plm_buffer_read(self->buffer, 6); break; } } } if (self->mode == PLM_AUDIO_MODE_MONO) { self->scale_factor[1][sb][0] = self->scale_factor[0][sb][0]; self->scale_factor[1][sb][1] = self->scale_factor[0][sb][1]; self->scale_factor[1][sb][2] = self->scale_factor[0][sb][2]; } } // Coefficient input and reconstruction int out_pos = 0; for (int part = 0; part < 3; part++) { for (int granule = 0; granule < 4; granule++) { // Read the samples for (int sb = 0; sb < self->bound; sb++) { plm_audio_read_samples(self, 0, sb, part); plm_audio_read_samples(self, 1, sb, part); } for (int sb = self->bound; sb < sblimit; sb++) { plm_audio_read_samples(self, 0, sb, part); self->sample[1][sb][0] = self->sample[0][sb][0]; self->sample[1][sb][1] = self->sample[0][sb][1]; self->sample[1][sb][2] = self->sample[0][sb][2]; } for (int sb = sblimit; sb < 32; sb++) { self->sample[0][sb][0] = 0; self->sample[0][sb][1] = 0; self->sample[0][sb][2] = 0; self->sample[1][sb][0] = 0; self->sample[1][sb][1] = 0; self->sample[1][sb][2] = 0; } // Synthesis loop for (int p = 0; p < 3; p++) { // Shifting step self->v_pos = (self->v_pos - 64) & 1023; for (int ch = 0; ch < 2; ch++) { plm_audio_matrix_transform(self->sample[ch], p, self->V, self->v_pos); // Build U, windowing, calculate output memset(self->U, 0, sizeof(self->U)); int d_index = 512 - (self->v_pos >> 1); int v_index = (self->v_pos % 128) >> 1; while (v_index < 1024) { for (int i = 0; i < 32; ++i) { self->U[i] += self->D[d_index++] * self->V[v_index++]; } v_index += 128 - 32; d_index += 64 - 32; } d_index -= (512 - 32); v_index = (128 - 32 + 1024) - v_index; while (v_index < 1024) { for (int i = 0; i < 32; ++i) { self->U[i] += self->D[d_index++] * self->V[v_index++]; } v_index += 128 - 32; d_index += 64 - 32; } // Output samples #ifdef PLM_AUDIO_SEPARATE_CHANNELS float *out_channel = ch == 0 ? self->samples.left : self->samples.right; for (int j = 0; j < 32; j++) { out_channel[out_pos + j] = self->U[j] / 2147418112.0f; } #else for (int j = 0; j < 32; j++) { self->samples.interleaved[((out_pos + j) << 1) + ch] = self->U[j] / 2147418112.0f; } #endif } // End of synthesis channel loop out_pos += 32; } // End of synthesis sub-block loop } // Decoding of the granule finished } plm_buffer_align(self->buffer); } const plm_quantizer_spec_t *plm_audio_read_allocation(plm_audio_t *self, int sb, int tab3) { int tab4 = PLM_AUDIO_QUANT_LUT_STEP_3[tab3][sb]; int qtab = PLM_AUDIO_QUANT_LUT_STEP4[tab4 & 15][plm_buffer_read(self->buffer, tab4 >> 4)]; return qtab ? (&PLM_AUDIO_QUANT_TAB[qtab - 1]) : 0; } void plm_audio_read_samples(plm_audio_t *self, int ch, int sb, int part) { const plm_quantizer_spec_t *q = self->allocation[ch][sb]; int sf = self->scale_factor[ch][sb][part]; int *sample = self->sample[ch][sb]; int val = 0; if (!q) { // No bits allocated for this subband sample[0] = sample[1] = sample[2] = 0; return; } // Resolve scalefactor if (sf == 63) { sf = 0; } else { int shift = (sf / 3) | 0; sf = (PLM_AUDIO_SCALEFACTOR_BASE[sf % 3] + ((1u << shift) >> 1)) >> shift; } // Decode samples int adj = q->levels; if (q->group) { // Decode grouped samples val = plm_buffer_read(self->buffer, q->bits); sample[0] = val % adj; val /= adj; sample[1] = val % adj; sample[2] = val / adj; } else { // Decode direct samples sample[0] = plm_buffer_read(self->buffer, q->bits); sample[1] = plm_buffer_read(self->buffer, q->bits); sample[2] = plm_buffer_read(self->buffer, q->bits); } // Postmultiply samples int scale = 65536 / (adj + 1); adj = ((adj + 1) >> 1) - 1; val = (adj - sample[0]) * scale; sample[0] = (val * (sf >> 12) + ((val * (sf & 4095) + 2048) >> 12)) >> 12; val = (adj - sample[1]) * scale; sample[1] = (val * (sf >> 12) + ((val * (sf & 4095) + 2048) >> 12)) >> 12; val = (adj - sample[2]) * scale; sample[2] = (val * (sf >> 12) + ((val * (sf & 4095) + 2048) >> 12)) >> 12; } void plm_audio_matrix_transform(int s[32][3], int ss, float *d, int dp) { float t01, t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31, t32, t33; t01 = (float)(s[0][ss] + s[31][ss]); t02 = (float)(s[0][ss] - s[31][ss]) * 0.500602998235f; t03 = (float)(s[1][ss] + s[30][ss]); t04 = (float)(s[1][ss] - s[30][ss]) * 0.505470959898f; t05 = (float)(s[2][ss] + s[29][ss]); t06 = (float)(s[2][ss] - s[29][ss]) * 0.515447309923f; t07 = (float)(s[3][ss] + s[28][ss]); t08 = (float)(s[3][ss] - s[28][ss]) * 0.53104259109f; t09 = (float)(s[4][ss] + s[27][ss]); t10 = (float)(s[4][ss] - s[27][ss]) * 0.553103896034f; t11 = (float)(s[5][ss] + s[26][ss]); t12 = (float)(s[5][ss] - s[26][ss]) * 0.582934968206f; t13 = (float)(s[6][ss] + s[25][ss]); t14 = (float)(s[6][ss] - s[25][ss]) * 0.622504123036f; t15 = (float)(s[7][ss] + s[24][ss]); t16 = (float)(s[7][ss] - s[24][ss]) * 0.674808341455f; t17 = (float)(s[8][ss] + s[23][ss]); t18 = (float)(s[8][ss] - s[23][ss]) * 0.744536271002f; t19 = (float)(s[9][ss] + s[22][ss]); t20 = (float)(s[9][ss] - s[22][ss]) * 0.839349645416f; t21 = (float)(s[10][ss] + s[21][ss]); t22 = (float)(s[10][ss] - s[21][ss]) * 0.972568237862f; t23 = (float)(s[11][ss] + s[20][ss]); t24 = (float)(s[11][ss] - s[20][ss]) * 1.16943993343f; t25 = (float)(s[12][ss] + s[19][ss]); t26 = (float)(s[12][ss] - s[19][ss]) * 1.48416461631f; t27 = (float)(s[13][ss] + s[18][ss]); t28 = (float)(s[13][ss] - s[18][ss]) * 2.05778100995f; t29 = (float)(s[14][ss] + s[17][ss]); t30 = (float)(s[14][ss] - s[17][ss]) * 3.40760841847f; t31 = (float)(s[15][ss] + s[16][ss]); t32 = (float)(s[15][ss] - s[16][ss]) * 10.1900081235f; t33 = t01 + t31; t31 = (t01 - t31) * 0.502419286188f; t01 = t03 + t29; t29 = (t03 - t29) * 0.52249861494f; t03 = t05 + t27; t27 = (t05 - t27) * 0.566944034816f; t05 = t07 + t25; t25 = (t07 - t25) * 0.64682178336f; t07 = t09 + t23; t23 = (t09 - t23) * 0.788154623451f; t09 = t11 + t21; t21 = (t11 - t21) * 1.06067768599f; t11 = t13 + t19; t19 = (t13 - t19) * 1.72244709824f; t13 = t15 + t17; t17 = (t15 - t17) * 5.10114861869f; t15 = t33 + t13; t13 = (t33 - t13) * 0.509795579104f; t33 = t01 + t11; t01 = (t01 - t11) * 0.601344886935f; t11 = t03 + t09; t09 = (t03 - t09) * 0.899976223136f; t03 = t05 + t07; t07 = (t05 - t07) * 2.56291544774f; t05 = t15 + t03; t15 = (t15 - t03) * 0.541196100146f; t03 = t33 + t11; t11 = (t33 - t11) * 1.30656296488f; t33 = t05 + t03; t05 = (t05 - t03) * 0.707106781187f; t03 = t15 + t11; t15 = (t15 - t11) * 0.707106781187f; t03 += t15; t11 = t13 + t07; t13 = (t13 - t07) * 0.541196100146f; t07 = t01 + t09; t09 = (t01 - t09) * 1.30656296488f; t01 = t11 + t07; t07 = (t11 - t07) * 0.707106781187f; t11 = t13 + t09; t13 = (t13 - t09) * 0.707106781187f; t11 += t13; t01 += t11; t11 += t07; t07 += t13; t09 = t31 + t17; t31 = (t31 - t17) * 0.509795579104f; t17 = t29 + t19; t29 = (t29 - t19) * 0.601344886935f; t19 = t27 + t21; t21 = (t27 - t21) * 0.899976223136f; t27 = t25 + t23; t23 = (t25 - t23) * 2.56291544774f; t25 = t09 + t27; t09 = (t09 - t27) * 0.541196100146f; t27 = t17 + t19; t19 = (t17 - t19) * 1.30656296488f; t17 = t25 + t27; t27 = (t25 - t27) * 0.707106781187f; t25 = t09 + t19; t19 = (t09 - t19) * 0.707106781187f; t25 += t19; t09 = t31 + t23; t31 = (t31 - t23) * 0.541196100146f; t23 = t29 + t21; t21 = (t29 - t21) * 1.30656296488f; t29 = t09 + t23; t23 = (t09 - t23) * 0.707106781187f; t09 = t31 + t21; t31 = (t31 - t21) * 0.707106781187f; t09 += t31; t29 += t09; t09 += t23; t23 += t31; t17 += t29; t29 += t25; t25 += t09; t09 += t27; t27 += t23; t23 += t19; t19 += t31; t21 = t02 + t32; t02 = (t02 - t32) * 0.502419286188f; t32 = t04 + t30; t04 = (t04 - t30) * 0.52249861494f; t30 = t06 + t28; t28 = (t06 - t28) * 0.566944034816f; t06 = t08 + t26; t08 = (t08 - t26) * 0.64682178336f; t26 = t10 + t24; t10 = (t10 - t24) * 0.788154623451f; t24 = t12 + t22; t22 = (t12 - t22) * 1.06067768599f; t12 = t14 + t20; t20 = (t14 - t20) * 1.72244709824f; t14 = t16 + t18; t16 = (t16 - t18) * 5.10114861869f; t18 = t21 + t14; t14 = (t21 - t14) * 0.509795579104f; t21 = t32 + t12; t32 = (t32 - t12) * 0.601344886935f; t12 = t30 + t24; t24 = (t30 - t24) * 0.899976223136f; t30 = t06 + t26; t26 = (t06 - t26) * 2.56291544774f; t06 = t18 + t30; t18 = (t18 - t30) * 0.541196100146f; t30 = t21 + t12; t12 = (t21 - t12) * 1.30656296488f; t21 = t06 + t30; t30 = (t06 - t30) * 0.707106781187f; t06 = t18 + t12; t12 = (t18 - t12) * 0.707106781187f; t06 += t12; t18 = t14 + t26; t26 = (t14 - t26) * 0.541196100146f; t14 = t32 + t24; t24 = (t32 - t24) * 1.30656296488f; t32 = t18 + t14; t14 = (t18 - t14) * 0.707106781187f; t18 = t26 + t24; t24 = (t26 - t24) * 0.707106781187f; t18 += t24; t32 += t18; t18 += t14; t26 = t14 + t24; t14 = t02 + t16; t02 = (t02 - t16) * 0.509795579104f; t16 = t04 + t20; t04 = (t04 - t20) * 0.601344886935f; t20 = t28 + t22; t22 = (t28 - t22) * 0.899976223136f; t28 = t08 + t10; t10 = (t08 - t10) * 2.56291544774f; t08 = t14 + t28; t14 = (t14 - t28) * 0.541196100146f; t28 = t16 + t20; t20 = (t16 - t20) * 1.30656296488f; t16 = t08 + t28; t28 = (t08 - t28) * 0.707106781187f; t08 = t14 + t20; t20 = (t14 - t20) * 0.707106781187f; t08 += t20; t14 = t02 + t10; t02 = (t02 - t10) * 0.541196100146f; t10 = t04 + t22; t22 = (t04 - t22) * 1.30656296488f; t04 = t14 + t10; t10 = (t14 - t10) * 0.707106781187f; t14 = t02 + t22; t02 = (t02 - t22) * 0.707106781187f; t14 += t02; t04 += t14; t14 += t10; t10 += t02; t16 += t04; t04 += t08; t08 += t14; t14 += t28; t28 += t10; t10 += t20; t20 += t02; t21 += t16; t16 += t32; t32 += t04; t04 += t06; t06 += t08; t08 += t18; t18 += t14; t14 += t30; t30 += t28; t28 += t26; t26 += t10; t10 += t12; t12 += t20; t20 += t24; t24 += t02; d[dp + 48] = -t33; d[dp + 49] = d[dp + 47] = -t21; d[dp + 50] = d[dp + 46] = -t17; d[dp + 51] = d[dp + 45] = -t16; d[dp + 52] = d[dp + 44] = -t01; d[dp + 53] = d[dp + 43] = -t32; d[dp + 54] = d[dp + 42] = -t29; d[dp + 55] = d[dp + 41] = -t04; d[dp + 56] = d[dp + 40] = -t03; d[dp + 57] = d[dp + 39] = -t06; d[dp + 58] = d[dp + 38] = -t25; d[dp + 59] = d[dp + 37] = -t08; d[dp + 60] = d[dp + 36] = -t11; d[dp + 61] = d[dp + 35] = -t18; d[dp + 62] = d[dp + 34] = -t09; d[dp + 63] = d[dp + 33] = -t14; d[dp + 32] = -t05; d[dp + 0] = t05; d[dp + 31] = -t30; d[dp + 1] = t30; d[dp + 30] = -t27; d[dp + 2] = t27; d[dp + 29] = -t28; d[dp + 3] = t28; d[dp + 28] = -t07; d[dp + 4] = t07; d[dp + 27] = -t26; d[dp + 5] = t26; d[dp + 26] = -t23; d[dp + 6] = t23; d[dp + 25] = -t10; d[dp + 7] = t10; d[dp + 24] = -t15; d[dp + 8] = t15; d[dp + 23] = -t12; d[dp + 9] = t12; d[dp + 22] = -t19; d[dp + 10] = t19; d[dp + 21] = -t20; d[dp + 11] = t20; d[dp + 20] = -t13; d[dp + 12] = t13; d[dp + 19] = -t24; d[dp + 13] = t24; d[dp + 18] = -t31; d[dp + 14] = t31; d[dp + 17] = -t02; d[dp + 15] = t02; d[dp + 16] = 0.0; };
30,344
776
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/buffer.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ │vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer │ │ Dominic Szablewski - https://phoboslab.org │ │ │ │ The MIT License(MIT) │ │ Copyright(c) 2019 Dominic Szablewski │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files(the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and / or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND │ │ NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE │ │ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN │ │ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN │ │ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE │ │ SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/mpeg/buffer.h" #include "dsp/mpeg/mpeg.h" #include "libc/calls/calls.h" #include "libc/log/check.h" #include "libc/mem/mem.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/madv.h" asm(".ident\t\"\\n\\n\ PL_MPEG (MIT License)\\n\ Copyright(c) 2019 Dominic Szablewski\\n\ https://phoboslab.org\""); asm(".include \"libc/disclaimer.inc\""); /* clang-format off */ // ----------------------------------------------------------------------------- // plm_buffer implementation plm_buffer_t *plm_buffer_create_with_filename(const char *filename) { FILE *fh = fopen(filename, "rb"); if (!fh) { return NULL; } fadvise(fileno(fh), 0, 0, MADV_SEQUENTIAL); return plm_buffer_create_with_file(fh, true); } plm_buffer_t *plm_buffer_create_with_file(FILE *fh, int close_when_done) { plm_buffer_t *b; b = plm_buffer_create_with_capacity(PLM_BUFFER_DEFAULT_SIZE); b->fh = fh; b->close_when_done = close_when_done; b->mode = PLM_BUFFER_MODE_FILE; plm_buffer_set_load_callback(b, plm_buffer_load_file_callback, NULL); return b; } plm_buffer_t *plm_buffer_create_with_memory(unsigned char *bytes, size_t length, int free_when_done) { plm_buffer_t *b; b = memalign(_Alignof(plm_buffer_t), sizeof(plm_buffer_t)); memset(b, 0, sizeof(plm_buffer_t)); b->capacity = length; b->length = length; b->free_when_done = free_when_done; b->bytes = bytes; b->mode = PLM_BUFFER_MODE_FIXED_MEM; return b; } plm_buffer_t * plm_buffer_create_with_capacity(size_t capacity) { plm_buffer_t *b; b = memalign(_Alignof(plm_buffer_t), sizeof(plm_buffer_t)); memset(b, 0, sizeof(plm_buffer_t)); b->capacity = capacity; b->free_when_done = true; b->bytes = (unsigned char *)malloc(capacity); b->mode = PLM_BUFFER_MODE_DYNAMIC_MEM; return b; } void plm_buffer_destroy(plm_buffer_t *self) { if (self->fh && self->close_when_done) { fclose(self->fh); } if (self->free_when_done) { free(self->bytes); } free(self); } size_t plm_buffer_write(plm_buffer_t *self, unsigned char *bytes, size_t length) { if (self->mode == PLM_BUFFER_MODE_FIXED_MEM) { return 0; } // This should be a ring buffer, but instead it just shifts all unread data // to the beginning of the buffer and appends new data at the end. Seems // to be good enough. plm_buffer_discard_read_bytes(self); // Do we have to resize to fit the new data? size_t bytes_available = self->capacity - self->length; if (bytes_available < length) { size_t new_size = self->capacity; do { new_size *= 2; } while (new_size - self->length < length); self->bytes = (unsigned char *)realloc(self->bytes, new_size); self->capacity = new_size; } memcpy(self->bytes + self->length, bytes, length); self->length += length; return length; } void plm_buffer_set_load_callback(plm_buffer_t *self, plm_buffer_load_callback fp, void *user) { self->load_callback = fp; self->load_callback_user_data = user; } void plm_buffer_rewind(plm_buffer_t *self) { if (self->fh) { fseek(self->fh, 0, SEEK_SET); self->length = 0; } if (self->mode != PLM_BUFFER_MODE_FIXED_MEM) { self->length = 0; } self->bit_index = 0; } void plm_buffer_discard_read_bytes(plm_buffer_t *self) { size_t byte_pos = self->bit_index >> 3; if (byte_pos == self->length) { self->bit_index = 0; self->length = 0; } else if (byte_pos > 0) { memmove(self->bytes, self->bytes + byte_pos, self->length - byte_pos); self->bit_index -= byte_pos << 3; self->length -= byte_pos; } } void plm_buffer_load_file_callback(plm_buffer_t *self, void *user) { plm_buffer_discard_read_bytes(self); unsigned bytes_available = self->capacity - self->length; unsigned bytes_read = fread(self->bytes + self->length, 1, bytes_available, self->fh); self->length += bytes_read; }
6,542
159
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/slowrgb.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ │vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer │ │ Dominic Szablewski - https://phoboslab.org │ │ │ │ The MIT License(MIT) │ │ Copyright(c) 2019 Dominic Szablewski │ │ │ │ Permission is hereby granted, free of charge, to any person obtaining │ │ a copy of this software and associated documentation files(the │ │ "Software"), to deal in the Software without restriction, including │ │ without limitation the rights to use, copy, modify, merge, publish, │ │ distribute, sublicense, and / or sell copies of the Software, and to │ │ permit persons to whom the Software is furnished to do so, subject to │ │ the following conditions: │ │ │ │ The above copyright notice and this permission notice shall be │ │ included in all copies or substantial portions of the Software. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ │ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ │ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND │ │ NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE │ │ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN │ │ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN │ │ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE │ │ SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "dsp/mpeg/mpeg.h" #include "libc/macros.internal.h" asm(".ident\t\"\\n\\n\ PL_MPEG (MIT License)\\n\ Copyright(c) 2019 Dominic Szablewski\\n\ https://phoboslab.org\""); asm(".include \"libc/disclaimer.inc\""); /** * @see YCbCr2RGB() in tool/viz/lib/ycbcr2rgb.c */ void plm_frame_to_rgb(plm_frame_t *frame, uint8_t *rgb) { // Chroma values are the same for each block of 4 pixels, so we process // 2 lines at a time, 2 neighboring pixels each. int w = frame->y.width, w2 = w >> 1; int y_index1 = 0, y_index2 = w, y_next_2_lines = w + (w - frame->width); int c_index = 0, c_next_line = w2 - (frame->width >> 1); int rgb_index1 = 0, rgb_index2 = frame->width * 3, rgb_next_2_lines = frame->width * 3; int cols = frame->width >> 1, rows = frame->height >> 1; int ccb, ccr, r, g, b; uint8_t *y = frame->y.data, *cb = frame->cb.data, *cr = frame->cr.data; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { ccb = cb[c_index]; ccr = cr[c_index]; c_index++; r = (ccr + ((ccr * 103) >> 8)) - 179; g = ((ccb * 88) >> 8) - 44 + ((ccr * 183) >> 8) - 91; b = (ccb + ((ccb * 198) >> 8)) - 227; // Line 1 int y1 = y[y_index1++]; int y2 = y[y_index1++]; rgb[rgb_index1 + 0] = MAX(0, MIN(255, y1 + r)); rgb[rgb_index1 + 1] = MAX(0, MIN(255, y1 - g)); rgb[rgb_index1 + 2] = MAX(0, MIN(255, y1 + b)); rgb[rgb_index1 + 3] = MAX(0, MIN(255, y2 + r)); rgb[rgb_index1 + 4] = MAX(0, MIN(255, y2 - g)); rgb[rgb_index1 + 5] = MAX(0, MIN(255, y2 + b)); rgb_index1 += 6; // Line 2 int y3 = y[y_index2++]; int y4 = y[y_index2++]; rgb[rgb_index2 + 0] = MAX(0, MIN(255, y3 + r)); rgb[rgb_index2 + 1] = MAX(0, MIN(255, y3 - g)); rgb[rgb_index2 + 2] = MAX(0, MIN(255, y3 + b)); rgb[rgb_index2 + 3] = MAX(0, MIN(255, y4 + r)); rgb[rgb_index2 + 4] = MAX(0, MIN(255, y4 - g)); rgb[rgb_index2 + 5] = MAX(0, MIN(255, y4 + b)); rgb_index2 += 6; } y_index1 += y_next_2_lines; y_index2 += y_next_2_lines; rgb_index1 += rgb_next_2_lines; rgb_index2 += rgb_next_2_lines; c_index += c_next_line; } }
4,979
89
jart/cosmopolitan
false
cosmopolitan/dsp/mpeg/idct.h
#ifndef COSMOPOLITAN_DSP_MPEG_IDCT_H_ #define COSMOPOLITAN_DSP_MPEG_IDCT_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ void plm_video_idct(int *); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_DSP_MPEG_IDCT_H_ */
276
11
jart/cosmopolitan
false
cosmopolitan/examples/defer-statements.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/mem/mem.h" #include "libc/mem/gc.h" #include "libc/stdio/stdio.h" /** * Cosmopolitan C is just as awesome as Go! * Example contributed by @Keithcat1 (#266) */ main() { _defer(printf, "Done!\n"); for (long i = 0; i < 100000; i++) { printf("%i ", i); _defer(free, malloc(100)); // longhand for _gc(malloc(100)) } }
1,131
25
jart/cosmopolitan
false
cosmopolitan/examples/getcpucount.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/intrin/kprintf.h" #include "libc/runtime/runtime.h" int main() { kprintf("%d\n", _getcpucount()); }
902
16
jart/cosmopolitan
false
cosmopolitan/examples/ls.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/calls/calls.h" #include "libc/calls/struct/dirent.h" #include "libc/calls/struct/stat.h" #include "libc/log/check.h" #include "libc/mem/gc.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/dt.h" #include "libc/sysv/consts/s.h" #include "libc/x/xasprintf.h" struct stat st; const char *TypeToString(uint8_t type) { switch (type) { case DT_UNKNOWN: return "DT_UNKNOWN"; case DT_FIFO: return "DT_FIFO"; case DT_CHR: return "DT_CHR"; case DT_DIR: return "DT_DIR"; case DT_BLK: return "DT_BLK"; case DT_REG: return "DT_REG"; case DT_LNK: return "DT_LNK"; case DT_SOCK: return "DT_SOCK"; default: return "UNKNOWN"; } } void List(const char *path) { DIR *d; struct dirent *e; const char *vpath; if (strcmp(path, ".") == 0) { vpath = ""; } else if (!_endswith(path, "/")) { vpath = _gc(xasprintf("%s/", path)); } else { vpath = path; } if (stat(path, &st) != -1) { if (S_ISDIR(st.st_mode)) { CHECK((d = opendir(path))); while ((e = readdir(d))) { printf("0x%016x 0x%016x %-10s %s%s\n", e->d_ino, e->d_off, TypeToString(e->d_type), vpath, e->d_name); } closedir(d); } else { printf("%s\n", path); } } else { fprintf(stderr, "not found: %s\n", path); } } int main(int argc, char *argv[]) { int i; if (argc == 1) { List("."); } else { for (i = 1; i < argc; ++i) { List(argv[i]); } } return 0; }
2,346
84
jart/cosmopolitan
false
cosmopolitan/examples/fastmod.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/stdio/stdio.h" #include "libc/testlib/ezbench.h" /** * @fileoverview Fast Modulus Using Multiplication Tutorial * * Expected program output: * * 23 / 3 = 7 * 0x5555555555555556 1 1 * modulus l: 15𝑐 5𝑛𝑠 * fastmod l: 4𝑐 1𝑛𝑠 * precomp l: 18𝑐 6𝑛𝑠 */ struct Modulus { uint64_t c; uint64_t d; }; struct Modulus GetModulus(uint64_t d) { return (struct Modulus){0xFFFFFFFFFFFFFFFFull / d + 1, d}; } uint64_t Modulus(uint64_t x, struct Modulus m) { return ((uint128_t)(m.c * x) * m.d) >> 64; } int main(int argc, char *argv[]) { printf("%#lx %% %d = %d\n", 0x23, 3, Modulus(23, GetModulus(3))); printf("%#lx %% %d = %d\n", 0x123, 17, Modulus(0x123, GetModulus(17))); volatile struct Modulus v = GetModulus(3); volatile uint64_t x = 23, y = 3, z; EZBENCH2("modulus", donothing, z = x % y); EZBENCH2("fastmod", donothing, z = Modulus(x, v)); EZBENCH2("precomp", donothing, v = GetModulus(y)); return 0; }
1,789
48
jart/cosmopolitan
false
cosmopolitan/examples/hertz.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/calls/calls.h" #include "libc/calls/struct/itimerval.h" #include "libc/errno.h" #include "libc/log/check.h" #include "libc/stdio/stdio.h" #include "libc/sysv/consts/itimer.h" #include "libc/sysv/consts/sig.h" #include "libc/x/xsigaction.h" const struct itimerval kHertz = { {1, 0}, {0, 1}, }; bool hertz; void OnAlrm(void) { hertz = true; } int main(int argc, char *argv[]) { CHECK_NE(-1, xsigaction(SIGALRM, OnAlrm, 0, 0, 0)); CHECK_NE(-1, setitimer(ITIMER_REAL, &kHertz, NULL)); for (;;) { CHECK_EQ(-1, pause()); CHECK_EQ(EINTR, errno); if (hertz) { hertz = false; printf("ding\n"); } } return 0; }
1,452
43
jart/cosmopolitan
false
cosmopolitan/examples/time.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/calls/calls.h" #include "libc/calls/struct/timespec.h" #include "libc/fmt/itoa.h" #include "libc/log/log.h" #include "libc/math.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/consts/ex.h" #include "libc/time/time.h" #include "libc/x/xspawn.h" /** * @fileoverview command for showing how long a command takes * * This offers the following improvements over the existing `time` * command that's incorporated into most shells: * * - This will show microseconds if seconds < 1 * - This will launch APE binaries on systems with broken libc execv() */ #define WRITE(FD, STR) write(FD, STR, strlen(STR)) void OnChild(void *arg) { char **argv = arg; execv(argv[0], argv); _exit(127); } long double GetTimeval(struct timeval t) { return t.tv_sec + t.tv_usec * 1e-6l; } void PrintMetric(const char *name, long double d) { char buf[256], *p = buf; long mins, secs, mils, mics; mins = d / 60; secs = fmodl(d, 60); mils = fmodl(d * 1000, 1000); mics = fmodl(d * 1000000, 1000); p = stpcpy(p, name), *p++ = '\t'; p = FormatInt64(p, mins), *p++ = 'm'; p = FormatInt64(p, secs), *p++ = '.'; *p++ = '0' + mils / 100; *p++ = '0' + mils / 10 % 10; *p++ = '0' + mils % 10; if (!secs) { *p++ = '0' + mics / 100; *p++ = '0' + mics / 10 % 10; *p++ = '0' + mics % 10; } *p++ = 's'; *p++ = '\n'; write(2, buf, p - buf); } int main(int argc, char *argv[]) { int ws; char *exepath; struct rusage r; long double real; char exebuf[PATH_MAX]; if (argc >= 2) { if ((exepath = commandv(argv[1], exebuf, sizeof(exebuf)))) { real = nowl(); argv[1] = exepath; if ((ws = xvspawn(OnChild, argv + 1, &r)) != -1) { PrintMetric("real", nowl() - real); PrintMetric("user", GetTimeval(r.ru_utime)); PrintMetric("sys", GetTimeval(r.ru_stime)); if (WIFEXITED(ws)) { return WEXITSTATUS(ws); } else { return 128 + WTERMSIG(ws); } } else { perror("xvspawn"); return 127; } } else { perror(argv[1]); return 127; } } else { WRITE(2, "Usage: "); WRITE(2, argv[0]); WRITE(2, " PROG [ARGS...]\n"); return EX_USAGE; } }
3,040
100
jart/cosmopolitan
false
cosmopolitan/examples/thread.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/thread/thread.h" #include "libc/stdio/stdio.h" /** * @fileoverview Basic POSIX Threads Example. * * $ make -j8 o//examples/thread.com * $ o//examples/thread.com * hi there * */ void *worker(void *arg) { fputs(arg, stdout); return "there\n"; } int main() { void *result; pthread_t id; pthread_create(&id, 0, worker, "hi "); pthread_join(id, &result); fputs(result, stdout); }
1,210
34
jart/cosmopolitan
false
cosmopolitan/examples/certapp.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright The Mbed TLS Contributors │ │ │ │ Licensed under the Apache License, Version 2.0 (the "License"); │ │ you may not use this file except in compliance with the License. │ │ You may obtain a copy of the License at │ │ │ │ http://www.apache.org/licenses/LICENSE-2.0 │ │ │ │ Unless required by applicable law or agreed to in writing, software │ │ distributed under the License is distributed on an "AS IS" BASIS, │ │ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │ │ See the License for the specific language governing permissions and │ │ limitations under the License. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/fmt/conv.h" #include "libc/stdio/stdio.h" #include "libc/sysv/consts/exit.h" #include "third_party/mbedtls/ctr_drbg.h" #include "third_party/mbedtls/debug.h" #include "third_party/mbedtls/entropy.h" #include "third_party/mbedtls/net_sockets.h" #include "third_party/mbedtls/ssl.h" #include "third_party/mbedtls/x509_crt.h" STATIC_YOINK("ssl_root_support"); #define MODE_NONE 0 #define MODE_FILE 1 #define MODE_SSL 2 #define DFL_MODE MODE_NONE #define DFL_FILENAME "cert.crt" #define DFL_CA_FILE "" #define DFL_CRL_FILE "" #define DFL_CA_PATH "/zip/usr/share/ssl/root" #define DFL_SERVER_NAME "localhost" #define DFL_SERVER_PORT "4433" #define DFL_DEBUG_LEVEL 0 #define DFL_PERMISSIVE 0 #define USAGE_IO \ " ca_file=%%s file containing top-level CAs\n" \ " ca_path=%%s dir containing top-level CAs\n" \ " crl_file=%%s The single CRL file you want to use\n" #define USAGE \ "\n usage: %s param=<>...\n" \ "\n acceptable parameters:\n" \ " mode=file|ssl default: none\n" \ " filename=%%s default: cert.crt\n" USAGE_IO \ " server_name=%%s default: localhost\n" \ " server_port=%%d default: 4433\n" \ " debug_level=%%d default: 0 (disabled)\n" \ " permissive=%%d default: 0 (disabled)\n" \ "\n" /* * global options */ struct options { int mode; /* the mode to run the application in */ const char *filename; /* filename of the certificate file */ const char *ca_file; /* the file with the CA certificate(s) */ const char *crl_file; /* the file with the CRL to use */ const char *ca_path; /* the path with the CA certificate(s) reside */ const char *server_name; /* hostname of the server (client only) */ const char *server_port; /* port on which the ssl service runs */ int debug_level; /* level of debugging */ int permissive; /* permissive parsing */ } opt; static void my_debug(void *ctx, int level, const char *file, int line, const char *str) { fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str); fflush((FILE *)ctx); } static int my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags) { char buf[1024]; printf("\nVerify requested for (Depth %d):\n", depth); mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt); printf("%s", buf); if (*flags) { mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", *flags); printf("%s\n", buf); } return 0; } mbedtls_net_context server_fd; unsigned char buf[1024]; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ssl_context ssl; mbedtls_ssl_config conf; mbedtls_x509_crt cacert; mbedtls_x509_crl cacrl; int main(int argc, char *argv[]) { int ret = 1; int exit_code = EXIT_FAILURE; int i, j; uint32_t flags; int verify = 0; char *p, *q; const char *pers = "cert_app"; /* * Set to sane values */ mbedtls_net_init(&server_fd); mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ssl_init(&ssl); mbedtls_ssl_config_init(&conf); mbedtls_x509_crt_init(&cacert); #if defined(MBEDTLS_X509_CRL_PARSE_C) mbedtls_x509_crl_init(&cacrl); #else /* Zeroize structure as CRL parsing is not supported and we have to pass it to the verify function */ memset(&cacrl, 0, sizeof(mbedtls_x509_crl)); #endif if (argc == 0) { usage: printf(USAGE, program_invocation_name); goto exit; } opt.mode = DFL_MODE; opt.filename = DFL_FILENAME; opt.ca_file = DFL_CA_FILE; opt.crl_file = DFL_CRL_FILE; opt.ca_path = DFL_CA_PATH; opt.server_name = DFL_SERVER_NAME; opt.server_port = DFL_SERVER_PORT; opt.debug_level = DFL_DEBUG_LEVEL; opt.permissive = DFL_PERMISSIVE; for (i = 1; i < argc; i++) { p = argv[i]; if ((q = strchr(p, '=')) == NULL) goto usage; *q++ = '\0'; for (j = 0; p + j < q; j++) { if (argv[i][j] >= 'A' && argv[i][j] <= 'Z') argv[i][j] |= 0x20; } if (strcmp(p, "mode") == 0) { if (strcmp(q, "file") == 0) opt.mode = MODE_FILE; else if (strcmp(q, "ssl") == 0) opt.mode = MODE_SSL; else goto usage; } else if (strcmp(p, "filename") == 0) opt.filename = q; else if (strcmp(p, "ca_file") == 0) opt.ca_file = q; else if (strcmp(p, "crl_file") == 0) opt.crl_file = q; else if (strcmp(p, "ca_path") == 0) opt.ca_path = q; else if (strcmp(p, "server_name") == 0) opt.server_name = q; else if (strcmp(p, "server_port") == 0) opt.server_port = q; else if (strcmp(p, "debug_level") == 0) { opt.debug_level = atoi(q); if (opt.debug_level < 0 || opt.debug_level > 65535) goto usage; } else if (strcmp(p, "permissive") == 0) { opt.permissive = atoi(q); if (opt.permissive < 0 || opt.permissive > 1) goto usage; } else goto usage; } /* * 1.1. Load the trusted CA */ printf(" . Loading the CA root certificate ..."); fflush(stdout); if (strlen(opt.ca_path)) { if ((ret = mbedtls_x509_crt_parse_path(&cacert, opt.ca_path)) < 0) { printf(" failed\n ! mbedtls_x509_crt_parse_path returned -0x%x\n\n", (unsigned int)-ret); goto exit; } verify = 1; } else if (strlen(opt.ca_file)) { if ((ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file)) < 0) { printf(" failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n", (unsigned int)-ret); goto exit; } verify = 1; } printf(" ok (%d skipped)\n", ret); #if defined(MBEDTLS_X509_CRL_PARSE_C) if (strlen(opt.crl_file)) { if ((ret = mbedtls_x509_crl_parse_file(&cacrl, opt.crl_file)) != 0) { printf(" failed\n ! mbedtls_x509_crl_parse returned -0x%x\n\n", (unsigned int)-ret); goto exit; } verify = 1; } #endif if (opt.mode == MODE_FILE) { mbedtls_x509_crt crt; mbedtls_x509_crt *cur = &crt; mbedtls_x509_crt_init(&crt); /* * 1.1. Load the certificate(s) */ printf("\n . Loading the certificate(s) ..."); fflush(stdout); ret = mbedtls_x509_crt_parse_file(&crt, opt.filename); if (ret < 0) { printf(" failed\n ! mbedtls_x509_crt_parse_file returned -0x%04x\n\n", -ret); mbedtls_x509_crt_free(&crt); goto exit; } if (opt.permissive == 0 && ret > 0) { printf(" failed\n ! mbedtls_x509_crt_parse failed to parse %d " "certificates\n\n", ret); mbedtls_x509_crt_free(&crt); goto exit; } printf(" ok\n"); /* * 1.2 Print the certificate(s) */ while (cur != NULL) { printf(" . Peer certificate information ...\n"); ret = mbedtls_x509_crt_info((char *)buf, sizeof(buf) - 1, " ", cur); if (ret == -1) { printf(" failed\n ! mbedtls_x509_crt_info returned -0x%04x\n\n", -ret); mbedtls_x509_crt_free(&crt); goto exit; } printf("%s\n", buf); cur = cur->next; } /* * 1.3 Verify the certificate */ if (verify) { printf(" . Verifying X.509 certificate..."); if ((ret = mbedtls_x509_crt_verify(&crt, &cacert, &cacrl, NULL, &flags, my_verify, NULL)) != 0) { char vrfy_buf[512]; printf(" failed\n"); mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags); printf("%s\n", vrfy_buf); } else printf(" ok\n"); } mbedtls_x509_crt_free(&crt); } else if (opt.mode == MODE_SSL) { /* * 1. Initialize the RNG and the session data */ printf("\n . Seeding the random number generator..."); fflush(stdout); mbedtls_entropy_init(&entropy); if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)pers, strlen(pers))) != 0) { printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret); goto ssl_exit; } printf(" ok\n"); #if defined(MBEDTLS_DEBUG_C) mbedtls_debug_set_threshold(opt.debug_level); #endif /* * 2. Start the connection */ printf(" . Connecting to tcp/%s/%s...\n", opt.server_name, opt.server_port); if ((ret = mbedtls_net_connect(&server_fd, opt.server_name, opt.server_port, MBEDTLS_NET_PROTO_TCP)) != 0) { printf(" ! mbedtls_net_connect returned -0x%04x\n\n", -ret); goto ssl_exit; } /* * 3. Setup stuff */ if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { printf(" ! mbedtls_ssl_config_defaults returned -0x%04x\n\n", -ret); goto exit; } if (verify) { mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED); mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); mbedtls_ssl_conf_verify(&conf, my_verify, NULL); } else mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE); mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); mbedtls_ssl_conf_dbg(&conf, my_debug, stdout); if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) { printf(" ! mbedtls_ssl_setup returned -0x%04x\n\n", -ret); goto ssl_exit; } if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) { printf(" ! mbedtls_ssl_set_hostname returned -0x%04x\n\n", -ret); goto ssl_exit; } mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL); /* * 4. Handshake */ while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { printf(" ! mbedtls_ssl_handshake returned -0x%04x\n\n", -ret); goto ssl_exit; } } /* * 5. Print the certificate */ #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) printf(" . Peer certificate information ... skipped\n"); #else printf(" . Peer certificate information ...\n"); ret = mbedtls_x509_crt_info((char *)buf, sizeof(buf) - 1, " ", mbedtls_ssl_get_peer_cert(&ssl)); if (ret == -1) { printf(" failed\n ! mbedtls_x509_crt_info returned -0x%04x\n\n", -ret); goto ssl_exit; } printf("%s\n", buf); #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ mbedtls_ssl_close_notify(&ssl); ssl_exit: mbedtls_ssl_free(&ssl); mbedtls_ssl_config_free(&conf); } else goto usage; exit_code = MBEDTLS_EXIT_SUCCESS; exit: mbedtls_net_free(&server_fd); mbedtls_x509_crt_free(&cacert); #if defined(MBEDTLS_X509_CRL_PARSE_C) mbedtls_x509_crl_free(&cacrl); #endif mbedtls_ctr_drbg_free(&ctr_drbg); mbedtls_entropy_free(&entropy); mbedtls_exit(exit_code); }
13,091
404
jart/cosmopolitan
false
cosmopolitan/examples/lstime.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/calls/calls.h" #include "libc/calls/struct/dirent.h" #include "libc/calls/struct/stat.h" #include "libc/calls/struct/timespec.h" #include "libc/intrin/safemacros.internal.h" #include "libc/log/check.h" #include "libc/mem/alg.h" #include "libc/mem/arraylist2.internal.h" #include "libc/mem/mem.h" #include "libc/mem/gc.h" #include "libc/stdio/stdio.h" #include "libc/sysv/consts/dt.h" #include "libc/x/x.h" #include "libc/x/xiso8601.h" struct stat st; struct Files { size_t i, n; struct File { struct timespec mt; char *path; } * p; } g_files; int CompareFiles(struct File *a, struct File *b) { if (a->mt.tv_sec > b->mt.tv_sec) return 1; if (a->mt.tv_sec < b->mt.tv_sec) return -1; if (a->mt.tv_nsec > b->mt.tv_nsec) return 1; if (a->mt.tv_nsec < b->mt.tv_nsec) return -1; return strcmp(a->path, b->path); } void WalkPaths(const char *dirpath) { DIR *d; char *path; struct File f; struct dirent *e; CHECK((d = opendir(dirpath))); while ((e = readdir(d))) { if (strcmp(e->d_name, ".") == 0) continue; if (strcmp(e->d_name, "..") == 0) continue; path = xjoinpaths(dirpath, e->d_name); if (strcmp(e->d_name, "o") == 0) continue; if (strcmp(e->d_name, ".git") == 0) continue; if (e->d_type == DT_DIR) { WalkPaths(_gc(path)); } else { CHECK_NE(-1, lstat(path, &st), "%s", path); f.mt = st.st_mtim; f.path = path; APPEND(&g_files.p, &g_files.i, &g_files.n, &f); } } closedir(d); } void SortPaths(void) { qsort(g_files.p, g_files.i, sizeof(*g_files.p), (void *)CompareFiles); } void PrintPaths(void) { long i; char *ts; for (i = 0; i < g_files.i; ++i) { ts = xiso8601(&g_files.p[i].mt); printf("%s %s\n", ts, g_files.p[i].path); free(ts); } } void FreePaths(void) { long i; for (i = 0; i < g_files.i; ++i) free(g_files.p[i].path); g_files.i = 0; } void LsTime(void) { WalkPaths("."); SortPaths(); PrintPaths(); FreePaths(); } int main(int argc, char *argv[]) { LsTime(); return 0; }
2,824
97
jart/cosmopolitan
false
cosmopolitan/examples/nanosleep.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/calls/calls.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/time/time.h" #include "libc/x/x.h" /** * 16kb sleep executable that runs on all operating systems. * https://justine.lol/cosmopolitan/demos/sleep.c * https://justine.lol/cosmopolitan/demos/sleep.com * https://justine.lol/cosmopolitan/index.html */ #define WRITE(s) write(2, s, strlen(s)) int main(int argc, char *argv[]) { char *p; long double x, y; if (argc != 2) { WRITE("Usage: "); WRITE(argv[0]); WRITE(" SECONDS\n"); exit(1); } x = 0; for (p = argv[1]; isdigit(*p); ++p) { x *= 10; x += *p - '0'; } if (*p == '.') { y = 1; for (++p; isdigit(*p); ++p) { x += (*p - '0') * (y /= 10); } } switch (*p) { case 'm': x *= 60; break; case 'h': x *= 60 * 60; break; case 'd': x *= 60 * 60 * 24; break; default: break; } dsleep(x); return 0; }
1,792
62
jart/cosmopolitan
false
cosmopolitan/examples/auto-memory-safety-crash3.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/intrin/bits.h" #include "libc/dce.h" #include "libc/log/log.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" /** * ASAN use-after-free memory safety crash example. * * make -j8 MODE=dbg o/dbg/examples/auto-memory-safety-crash3.com * o/dbg/examples/auto-memory-safety-crash3.com * */ int main(int argc, char *argv[]) { if (!IsAsan()) { printf("this example is intended for MODE=asan or MODE=dbg\n"); exit(1); } char *buffer; ShowCrashReports(); /* not needed but yoinks appropriate symbols */ buffer = malloc(13); strcpy(buffer, "hello"); free(buffer); asm("" : "+r"(buffer)); /* prevent compiler being smart */ buffer[0] = 1; return 0; }
1,551
40
jart/cosmopolitan
false
cosmopolitan/examples/exit.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/fmt/conv.h" int main(int argc, char *argv[]) { return atoi(argc > 1 ? argv[1] : "0"); }
890
15
jart/cosmopolitan
false
cosmopolitan/examples/wall.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/calls/calls.h" #include "libc/calls/struct/timespec.h" #include "libc/calls/termios.h" #include "libc/errno.h" #include "libc/fmt/fmt.h" #include "libc/runtime/runtime.h" #include "libc/stdio/append.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" #include "libc/time/struct/tm.h" #include "third_party/getopt/getopt.h" #include "third_party/musl/passwd.h" /** * @fileoverview program for broadcasting message to all terminals, e.g. * * o//examples/wall.com hello friends * * Will place some bold text into the top left corners of all terminals: * * Broadcast message from jart@host (pts/4) (Tue Oct 11 03:52:24 2022): * hello friends * * It's useful for having one terminal tab notify your other ones when * something like a long running job completes. In the old days back * when unix systems had multiple users, the wall command provided the * ultimate lulz especially if it's a setuid binary with root privs. */ #define GETOPTS "v" #define USAGE \ "\ Usage: wall.com [-v] [TEXTMSG] [<TEXTMSG]\n\ -v increase verbosity\n\ Examples:\n\ wall.com 'hello my friends'\n\ echo hello my friends | wall.com\n\ " int g_verbose; static void GetOpts(int argc, char *argv[]) { int opt; while ((opt = getopt(argc, argv, GETOPTS)) != -1) { switch (opt) { case 'v': g_verbose++; break; case '?': write(1, USAGE, sizeof(USAGE) - 1); exit(0); default: write(2, USAGE, sizeof(USAGE) - 1); exit(64); } } } char *GetHost(void) { static char host[254]; if (gethostname(host, sizeof(host))) { strcpy(host, "localhost"); } return host; } char *GetTime(void) { int64_t t; struct tm tm; struct timespec ts; clock_gettime(0, &ts); localtime_r(&ts.tv_sec, &tm); return _chomp(asctime(&tm)); } int main(int argc, char *argv[]) { GetOpts(argc, argv); // create broadcast banner // we use \e[1m so the text is strong & visually distinct // we use \e[K so it doesn't get lost in a forest of text // we map \n → \r\n because teletype might be in raw mode char *msg = 0; const char *s; appends(&msg, "\e7"); // save cursor position appends(&msg, "\e[H"); // goto top left corne appends(&msg, "\e[K"); // clear line forward appends(&msg, "\e[1m"); // bold text appendf(&msg, "Broadcast message from %s@%s", getpwuid(getuid())->pw_name, GetHost()); if (isatty(0) && (s = ttyname(0))) appendf(&msg, " (%s)", s); appendf(&msg, " (%s):\r\n", GetTime()); appends(&msg, "\e[K"); // append broadcast message if (optind < argc) { // use cli arguments as message if they exist for (int i = 0; optind + i < argc; ++i) { if (i) appends(&msg, " "); for (s = argv[optind + i]; *s; ++s) { if (*s == '\n') { appends(&msg, "\r\n\e[K"); } else { appendw(&msg, *s & 255); } } } } else { // otherwise use stdin as message ssize_t i, rc; char buf[512]; while ((rc = read(0, buf, sizeof(buf))) > 0) { for (i = 0; i < rc; ++i) { if (buf[i] == '\n') { appends(&msg, "\r\n\e[K"); // clear line forward } else { appendw(&msg, buf[i] & 255); } } } } appends(&msg, "\r\n\e[K\e[0m\e8"); // restore // try to send message to all pseudoteletypewriters for (int i = 0; i < 100; ++i) { int fd; char pts[32]; snprintf(pts, sizeof(pts), "/dev/pts/%d", i); if ((fd = open(pts, O_WRONLY | O_NOCTTY)) == -1) { if (errno == ENOENT) continue; if (g_verbose) perror(pts); } write(fd, msg, appendz(msg).i); close(fd); } return 0; }
4,519
149
jart/cosmopolitan
false
cosmopolitan/examples/breakpoint.c
#if 0 /*─────────────────────────────────────────────────────────────────╗ │ To the extent possible under law, Justine Tunney has waived │ │ all copyright and related or neighboring rights to this file, │ │ as it is written in the following disclaimers: │ │ • http://unlicense.org/ │ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/intrin/kprintf.h" #include "libc/log/backtrace.internal.h" #include "libc/log/log.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" int main(int argc, char *argv[]) { ShowCrashReports(); if (IsDebuggerPresent(false)) { kprintf("debugger found!%n"); } else { kprintf("try running: gdb %s%n", argv[0]); kprintf("try running: o//tool/build/strace.com %s%n", argv[0]); } __builtin_trap(); printf("recovered from SIGTRAP without handler\r\n"); return 0; }
1,310
31
jart/cosmopolitan
false