www in docker support

This commit is contained in:
MaddoScientisto 2026-04-22 18:41:37 +02:00
commit c227fce036
2145 changed files with 399596 additions and 58 deletions

View file

@ -0,0 +1,72 @@
package org.jcodec.common.tools;
import java.nio.ShortBuffer;
import org.jcodec.common.ArrayUtil;
public class Debug {
public static final void print8x8i(int[] output) {
int i = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
System.out.printf("%3d, ", output[i]);
i++;
}
System.out.println();
}
}
public static final void print8x8s(short[] output) {
int i = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
System.out.printf("%3d, ", output[i]);
i++;
}
System.out.println();
}
}
public static final void print8x8sb(ShortBuffer output) {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++)
System.out.printf("%3d, ", output.get());
System.out.println();
}
}
public static void prints(short[] table) {
int i = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
System.out.printf("%3d, ", table[i]);
i++;
}
System.out.println();
}
}
public static void trace(Object... arguments) {
if (debug && arguments.length > 0) {
String format = (String)arguments[0];
ArrayUtil.shiftLeft1(arguments);
System.out.printf(format + ": %d\n", arguments);
}
}
public static boolean debug = false;
public static void printInt(int i) {
if (debug)
System.out.print(i);
}
public static void print(String string) {
if (debug)
System.out.print(string);
}
public static void println(String string) {
if (debug)
System.out.println(string);
}
}

View file

@ -0,0 +1,65 @@
package org.jcodec.common.tools;
import org.jcodec.common.model.ColorSpace;
import org.jcodec.common.model.Picture;
import org.jcodec.common.model.Rect;
public class ImageOP {
public static void subImageWithFillInt(int[] src, int width, int height, int[] dst, int dstW, int dstH, int offX, int offY) {
int srcHeight = Math.min(height - offY, dstH);
int srcWidth = Math.min(width - offX, dstW);
int srcStride = width;
int dstOff = 0, srcOff = offY * srcStride + offX;
int i;
for (i = 0; i < srcHeight; i++) {
int j;
for (j = 0; j < srcWidth; j++)
dst[dstOff + j] = src[srcOff + j];
int lastPix = dst[j - 1];
for (; j < dstW; j++)
dst[dstOff + j] = lastPix;
srcOff += srcStride;
dstOff += dstW;
}
int lastLine = dstOff - dstW;
for (; i < dstH; i++) {
System.arraycopy(dst, lastLine, dst, dstOff, dstW);
dstOff += dstW;
}
}
public static void subImageWithFill(byte[] src, int width, int height, byte[] dst, int dstW, int dstH, int offX, int offY) {
int srcHeight = Math.min(height - offY, dstH);
int srcWidth = Math.min(width - offX, dstW);
int srcStride = width;
int dstOff = 0, srcOff = offY * srcStride + offX;
int i;
for (i = 0; i < srcHeight; i++) {
int j;
for (j = 0; j < srcWidth; j++)
dst[dstOff + j] = src[srcOff + j];
byte lastPix = dst[j - 1];
for (; j < dstW; j++)
dst[dstOff + j] = lastPix;
srcOff += srcStride;
dstOff += dstW;
}
int lastLine = dstOff - dstW;
for (; i < dstH; i++) {
System.arraycopy(dst, lastLine, dst, dstOff, dstW);
dstOff += dstW;
}
}
public static void subImageWithFillPic8(Picture _in, Picture out, Rect rect) {
int width = _in.getWidth();
int height = _in.getHeight();
ColorSpace color = _in.getColor();
byte[][] data = _in.getData();
for (int i = 0; i < data.length; i++)
subImageWithFill(data[i], width >> color.compWidth[i], height >> color.compHeight[i],
out.getPlaneData(i), rect.getWidth() >> color.compWidth[i],
rect.getHeight() >> color.compHeight[i], rect.getX() >> color.compWidth[i],
rect.getY() >> color.compHeight[i]);
}
}

View file

@ -0,0 +1,42 @@
package org.jcodec.common.tools;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
public static String md5sumBytes(byte[] bytes) {
MessageDigest md5 = getDigest();
md5.update(bytes);
return digestToString(md5.digest());
}
private static String digestToString(byte[] digest) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
byte item = digest[i];
int b = item & 0xFF;
if (b < 16)
sb.append("0");
sb.append(Integer.toHexString(b));
}
return sb.toString();
}
public static String md5sum(ByteBuffer bytes) {
MessageDigest md5 = getDigest();
md5.update(bytes);
byte[] digest = md5.digest();
return digestToString(digest);
}
public static MessageDigest getDigest() {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return md5;
}
}

View file

@ -0,0 +1,451 @@
package org.jcodec.common.tools;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jcodec.common.StringUtils;
import org.jcodec.common.io.IOUtils;
import org.jcodec.platform.Platform;
public class MainUtils {
private static final String KEY_GIT_REVISION = "git.commit.id.abbrev";
private static final String JCODEC_LOG_SINK_COLOR = "jcodec.colorPrint";
private static final String GIT_PROPERTIES = "git.properties";
public static boolean isColorSupported = (System.console() != null ||
Boolean.parseBoolean(System.getProperty("jcodec.colorPrint")));
public enum FlagType {
VOID, STRING, INT, LONG, DOUBLE, MULT, ENUM, ANY;
}
public static class Flag {
private String longName;
private String shortName;
private String description;
private MainUtils.FlagType type;
public Flag(String longName, String shortName, String description, MainUtils.FlagType type) {
this.longName = longName;
this.shortName = shortName;
this.description = description;
this.type = type;
}
public static Flag flag(String longName, String shortName, String description) {
return new Flag(longName, shortName, description, MainUtils.FlagType.ANY);
}
public String getLongName() {
return this.longName;
}
public String getDescription() {
return this.description;
}
public String getShortName() {
return this.shortName;
}
public MainUtils.FlagType getType() {
return this.type;
}
}
public static class Cmd {
public Map<String, String> longFlags;
public Map<String, String> shortFlags;
public String[] args;
private Map<String, String>[] longArgFlags;
private Map<String, String>[] shortArgFlags;
public Cmd(Map<String, String> longFlags, Map<String, String> shortFlags, String[] args, Map<String, String>[] longArgFlags, Map<String, String>[] shortArgFlags) {
this.args = args;
this.longFlags = longFlags;
this.shortFlags = shortFlags;
this.longArgFlags = longArgFlags;
this.shortArgFlags = shortArgFlags;
}
private Long getLongFlagInternal(Map<String, String> longFlags, Map<String, String> shortFlags, MainUtils.Flag flag, Long defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? new Long(longFlags.get(flag.getLongName())) : (
shortFlags.containsKey(flag.getShortName()) ? new Long(shortFlags.get(flag.getShortName())) :
defaultValue);
}
private Integer getIntegerFlagInternal(Map<String, String> longFlags, Map<String, String> shortFlags, MainUtils.Flag flag, Integer defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? new Integer(longFlags.get(flag.getLongName())) : (
shortFlags.containsKey(flag.getShortName()) ? new Integer(shortFlags.get(flag.getShortName())) :
defaultValue);
}
private Boolean getBooleanFlagInternal(Map<String, String> longFlags, Map<String, String> shortFlags, MainUtils.Flag flag, Boolean defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? (
!"false".equalsIgnoreCase(longFlags.get(flag.getLongName()))) : (
shortFlags.containsKey(flag.getShortName()) ? (
!"false".equalsIgnoreCase(shortFlags.get(flag.getShortName()))) : defaultValue);
}
private Double getDoubleFlagInternal(Map<String, String> longFlags, Map<String, String> shortFlags, MainUtils.Flag flag, Double defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? new Double(longFlags.get(flag.getLongName())) : (
shortFlags.containsKey(flag.getShortName()) ? new Double(shortFlags.get(flag.getShortName())) :
defaultValue);
}
private String getStringFlagInternal(Map<String, String> longFlags, Map<String, String> shortFlags, MainUtils.Flag flag, String defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? longFlags.get(flag.getLongName()) : (
shortFlags.containsKey(flag.getShortName()) ? shortFlags.get(flag.getShortName()) :
defaultValue);
}
private int[] getMultiIntegerFlagInternal(Map<String, String> longFlags, Map<String, String> shortFlags, MainUtils.Flag flag, int[] defaultValue) {
String flagValue;
if (longFlags.containsKey(flag.getLongName())) {
flagValue = longFlags.get(flag.getLongName());
} else if (shortFlags.containsKey(flag.getShortName())) {
flagValue = shortFlags.get(flag.getShortName());
} else {
return defaultValue;
}
String[] split = StringUtils.splitS(flagValue, ",");
int[] result = new int[split.length];
for (int i = 0; i < split.length; i++)
result[i] = Integer.parseInt(split[i]);
return result;
}
private <T extends Enum<T>> T getEnumFlagInternal(Map<String, String> longFlags, Map<String, String> shortFlags, MainUtils.Flag flag, T defaultValue, Class<T> class1) {
String flagValue;
if (longFlags.containsKey(flag.getLongName())) {
flagValue = longFlags.get(flag.getLongName());
} else if (shortFlags.containsKey(flag.getShortName())) {
flagValue = shortFlags.get(flag.getShortName());
} else {
return defaultValue;
}
String strVal = flagValue.toLowerCase();
EnumSet<T> allOf = EnumSet.allOf(class1);
for (T val : (Iterable<T>)allOf) {
if (val.name().toLowerCase().equals(strVal))
return val;
}
return null;
}
public Long getLongFlagD(MainUtils.Flag flagName, Long defaultValue) {
return getLongFlagInternal(this.longFlags, this.shortFlags, flagName, defaultValue);
}
public Long getLongFlag(MainUtils.Flag flagName) {
return getLongFlagInternal(this.longFlags, this.shortFlags, flagName, null);
}
public Long getLongFlagID(int arg, MainUtils.Flag flagName, Long defaultValue) {
return getLongFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, defaultValue);
}
public Long getLongFlagI(int arg, MainUtils.Flag flagName) {
return getLongFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, null);
}
public Integer getIntegerFlagD(MainUtils.Flag flagName, Integer defaultValue) {
return getIntegerFlagInternal(this.longFlags, this.shortFlags, flagName, defaultValue);
}
public Integer getIntegerFlag(MainUtils.Flag flagName) {
return getIntegerFlagInternal(this.longFlags, this.shortFlags, flagName, null);
}
public Integer getIntegerFlagID(int arg, MainUtils.Flag flagName, Integer defaultValue) {
return getIntegerFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, defaultValue);
}
public Integer getIntegerFlagI(int arg, MainUtils.Flag flagName) {
return getIntegerFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, null);
}
public Boolean getBooleanFlagD(MainUtils.Flag flagName, Boolean defaultValue) {
return getBooleanFlagInternal(this.longFlags, this.shortFlags, flagName, defaultValue);
}
public Boolean getBooleanFlag(MainUtils.Flag flagName) {
return getBooleanFlagInternal(this.longFlags, this.shortFlags, flagName, Boolean.valueOf(false));
}
public Boolean getBooleanFlagID(int arg, MainUtils.Flag flagName, Boolean defaultValue) {
return getBooleanFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, defaultValue);
}
public Boolean getBooleanFlagI(int arg, MainUtils.Flag flagName) {
return getBooleanFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, Boolean.valueOf(false));
}
public Double getDoubleFlagD(MainUtils.Flag flagName, Double defaultValue) {
return getDoubleFlagInternal(this.longFlags, this.shortFlags, flagName, defaultValue);
}
public Double getDoubleFlag(MainUtils.Flag flagName) {
return getDoubleFlagInternal(this.longFlags, this.shortFlags, flagName, null);
}
public Double getDoubleFlagID(int arg, MainUtils.Flag flagName, Double defaultValue) {
return getDoubleFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, defaultValue);
}
public Double getDoubleFlagI(int arg, MainUtils.Flag flagName) {
return getDoubleFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, null);
}
public String getStringFlagD(MainUtils.Flag flagName, String defaultValue) {
return getStringFlagInternal(this.longFlags, this.shortFlags, flagName, defaultValue);
}
public String getStringFlag(MainUtils.Flag flagName) {
return getStringFlagInternal(this.longFlags, this.shortFlags, flagName, null);
}
public String getStringFlagID(int arg, MainUtils.Flag flagName, String defaultValue) {
return getStringFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, defaultValue);
}
public String getStringFlagI(int arg, MainUtils.Flag flagName) {
return getStringFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, null);
}
public int[] getMultiIntegerFlagD(MainUtils.Flag flagName, int[] defaultValue) {
return getMultiIntegerFlagInternal(this.longFlags, this.shortFlags, flagName, defaultValue);
}
public int[] getMultiIntegerFlag(MainUtils.Flag flagName) {
return getMultiIntegerFlagInternal(this.longFlags, this.shortFlags, flagName, new int[0]);
}
public int[] getMultiIntegerFlagID(int arg, MainUtils.Flag flagName, int[] defaultValue) {
return getMultiIntegerFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, defaultValue);
}
public int[] getMultiIntegerFlagI(int arg, MainUtils.Flag flagName) {
return getMultiIntegerFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, new int[0]);
}
public <T extends Enum<T>> T getEnumFlagD(MainUtils.Flag flagName, T defaultValue, Class<T> class1) {
return getEnumFlagInternal(this.longFlags, this.shortFlags, flagName, defaultValue, class1);
}
public <T extends Enum<T>> T getEnumFlag(MainUtils.Flag flagName, Class<T> class1) {
return getEnumFlagInternal(this.longFlags, this.shortFlags, flagName, null, class1);
}
public <T extends Enum<T>> T getEnumFlagID(int arg, MainUtils.Flag flagName, T defaultValue, Class<T> class1) {
return getEnumFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, defaultValue, class1);
}
public <T extends Enum<T>> T getEnumFlagI(int arg, MainUtils.Flag flagName, Class<T> class1) {
return getEnumFlagInternal(this.longArgFlags[arg], this.shortArgFlags[arg], flagName, null, class1);
}
public String getArg(int i) {
return (i < this.args.length) ? this.args[i] : null;
}
public int argsLength() {
return this.args.length;
}
public void popArg() {
this.args = Platform.<String>copyOfRangeO(this.args, 1, this.args.length);
}
}
private static Pattern flagPattern = Pattern.compile("^--([^=]+)=(.*)$");
public static Cmd parseArguments(String[] args, Flag[] flags) {
Map<String, String> longFlags = new HashMap<>();
Map<String, String> shortFlags = new HashMap<>();
Map<String, String> allLongFlags = new HashMap<>();
Map<String, String> allShortFlags = new HashMap<>();
List<String> outArgs = new ArrayList<>();
List<Map<String, String>> argLongFlags = new ArrayList<>();
List<Map<String, String>> argShortFlags = new ArrayList<>();
int arg = 0;
for (; arg < args.length; arg++) {
if (args[arg].startsWith("--")) {
Matcher matcher = flagPattern.matcher(args[arg]);
if (matcher.matches()) {
longFlags.put(matcher.group(1), matcher.group(2));
} else {
longFlags.put(args[arg].substring(2), "true");
}
} else if (args[arg].startsWith("-")) {
String shortName = args[arg].substring(1);
boolean found = false;
for (Flag flag : flags) {
if (shortName.equals(flag.getShortName())) {
found = true;
if (flag.getType() != FlagType.VOID) {
shortFlags.put(shortName, args[++arg]);
} else {
shortFlags.put(shortName, "true");
}
}
}
if (!found)
arg++;
} else {
allLongFlags.putAll(longFlags);
allShortFlags.putAll(shortFlags);
outArgs.add(args[arg]);
argLongFlags.add(longFlags);
argShortFlags.add(shortFlags);
longFlags = new HashMap<>();
shortFlags = new HashMap<>();
}
}
return new Cmd(allLongFlags, allShortFlags, outArgs.<String>toArray(new String[0]),
argLongFlags.<Map<String, String>>toArray((Map<String, String>[])Array.newInstance(longFlags.getClass(), 0)),
argShortFlags.<Map<String, String>>toArray((Map<String, String>[])Array.newInstance(shortFlags.getClass(), 0)));
}
public static void printHelpArgs(Flag[] flags, String[] args) {
printHelpOut(System.out, "", flags, Arrays.asList(args));
}
public static void printHelp(Flag[] flags, List<String> params) {
printHelpOut(System.out, "", flags, params);
}
public static void printHelpNoFlags(String... arguments) {
printHelpOut(System.out, "", new Flag[0], Arrays.asList(arguments));
}
public static void printHelpCmdVa(String command, Flag[] flags, String args) {
printHelpOut(System.out, command, flags, Collections.singletonList(args));
}
public static void printHelpCmd(String command, Flag[] flags, List<String> params) {
printHelpOut(System.out, command, flags, params);
}
private static String getGitRevision() {
InputStream is = null;
try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream("git.properties");
if (is == null)
return null;
Properties properties = new Properties();
properties.load(is);
return (String)properties.get("git.commit.id.abbrev");
} catch (IOException e) {
} finally {
IOUtils.closeQuietly(is);
}
return null;
}
public static void printHelpOut(PrintStream out, String command, Flag[] flags, List<String> params) {
String version = MainUtils.class.getPackage().getImplementationVersion();
String gitRevision = getGitRevision();
if (command == null || command.isEmpty())
command = "jcodec";
if (gitRevision != null || version != null) {
out.println(command + command);
out.println();
}
out.print(bold("Syntax: " + command));
StringBuilder sample = new StringBuilder();
StringBuilder detail = new StringBuilder();
for (Flag flag : flags) {
sample.append(" [");
detail.append("\t");
if (flag.getLongName() != null) {
sample.append(bold(color("--" + flag.getLongName() + "=<value>", ANSIColor.MAGENTA)));
detail.append(bold(color("--" + flag.getLongName(), ANSIColor.MAGENTA)));
}
if (flag.getShortName() != null) {
if (flag.getLongName() != null) {
sample.append(" (");
detail.append(" (");
}
sample.append(bold(color("-" + flag.getShortName() + " <value>", ANSIColor.MAGENTA)));
detail.append(bold(color("-" + flag.getShortName(), ANSIColor.MAGENTA)));
if (flag.getLongName() != null) {
sample.append(")");
detail.append(")");
}
}
sample.append("]");
detail.append("\t\t" + flag.getDescription() + "\n");
}
for (String param : params) {
if (param.charAt(0) != '?') {
sample.append(bold(" <" + param + ">"));
continue;
}
sample.append(bold(" [" + param.substring(1) + "]"));
}
out.println(sample);
out.println(bold("Where:"));
out.println(detail);
}
public enum ANSIColor {
BLACK, RED, GREEN, BROWN, BLUE, MAGENTA, CYAN, GREY;
}
public static String bold(String str) {
return isColorSupported ? ("\033[1m" + str + "\033[0m") : str;
}
public static String colorString(String str, String placeholder) {
return isColorSupported ? ("\033[" + placeholder + "m" + str + "\033[0m") : str;
}
public static String color(String str, ANSIColor fg) {
return isColorSupported ? ("\033[" + 30 + (fg.ordinal() & 0x7) + "m" + str + "\033[0m") : str;
}
public static String colorBright(String str, ANSIColor fg, boolean bright) {
return isColorSupported ? ("\033[" + 30 + (fg.ordinal() & 0x7) + ";" + (bright ? true : 2) + "m" + str + "\033[0m") :
str;
}
public static String color3(String str, ANSIColor fg, ANSIColor bg) {
return isColorSupported ? ("\033[" +
30 + (fg.ordinal() & 0x7) + ";" + 40 + (bg.ordinal() & 0x7) + ";1m" + str + "\033[0m") :
str;
}
public static String color4(String str, ANSIColor fg, ANSIColor bg, boolean bright) {
return isColorSupported ? ("\033[" +
30 + (fg.ordinal() & 0x7) + ";" + 40 + (bg.ordinal() & 0x7) + ";" + (bright ? true : 2) + "m" + str + "\033[0m") : str;
}
public static File tildeExpand(String path) {
if (path.startsWith("~"))
path = path.replaceFirst("~", System.getProperty("user.home"));
return new File(path);
}
}

View file

@ -0,0 +1,166 @@
package org.jcodec.common.tools;
public class MathUtil {
private static final int[] logTab = new int[] {
0, 0, 1, 1, 2, 2, 2, 2, 3, 3,
3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7 };
private static final int[] reverseTab = new int[] {
0, 128, 64, 192, 32, 160, 96, 224, 16, 144,
80, 208, 48, 176, 112, 240, 8, 136, 72, 200,
40, 168, 104, 232, 24, 152, 88, 216, 56, 184,
120, 248, 4, 132, 68, 196, 36, 164, 100, 228,
20, 148, 84, 212, 52, 180, 116, 244, 12, 140,
76, 204, 44, 172, 108, 236, 28, 156, 92, 220,
60, 188, 124, 252, 2, 130, 66, 194, 34, 162,
98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
10, 138, 74, 202, 42, 170, 106, 234, 26, 154,
90, 218, 58, 186, 122, 250, 6, 134, 70, 198,
38, 166, 102, 230, 22, 150, 86, 214, 54, 182,
118, 246, 14, 142, 78, 206, 46, 174, 110, 238,
30, 158, 94, 222, 62, 190, 126, 254, 1, 129,
65, 193, 33, 161, 97, 225, 17, 145, 81, 209,
49, 177, 113, 241, 9, 137, 73, 201, 41, 169,
105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
5, 133, 69, 197, 37, 165, 101, 229, 21, 149,
85, 213, 53, 181, 117, 245, 13, 141, 77, 205,
45, 173, 109, 237, 29, 157, 93, 221, 61, 189,
125, 253, 3, 131, 67, 195, 35, 163, 99, 227,
19, 147, 83, 211, 51, 179, 115, 243, 11, 139,
75, 203, 43, 171, 107, 235, 27, 155, 91, 219,
59, 187, 123, 251, 7, 135, 71, 199, 39, 167,
103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
15, 143, 79, 207, 47, 175, 111, 239, 31, 159,
95, 223, 63, 191, 127, 255 };
public static int log2(int v) {
int n = 0;
if ((v & 0xFFFF0000) != 0) {
v >>= 16;
n += 16;
}
if ((v & 0xFF00) != 0) {
v >>= 8;
n += 8;
}
n += logTab[v];
return n;
}
public static int log2l(long v) {
int n = 0;
if ((v & 0xFFFFFFFF00000000L) != 0L) {
v >>= 32L;
n += 32;
}
if ((v & 0xFFFF0000L) != 0L) {
v >>= 16L;
n += 16;
}
if ((v & 0xFF00L) != 0L) {
v >>= 8L;
n += 8;
}
n += logTab[(int)v];
return n;
}
public static int log2Slow(int val) {
int i = 0;
while ((val & Integer.MIN_VALUE) == 0) {
val <<= 1;
i++;
}
return 31 - i;
}
public static int gcd(int a, int b) {
if (b != 0)
return gcd(b, a % b);
return a;
}
public static long gcdLong(long a, long b) {
if (b != 0L)
return gcdLong(b, a % b);
return a;
}
public static final int clip(int val, int from, int to) {
return (val < from) ? from : ((val > to) ? to : val);
}
public static final int clipMax(int val, int max) {
return (val < max) ? val : max;
}
public static int cubeRoot(int n) {
return 0;
}
public static final int reverse(int b) {
return reverseTab[b & 0xFF];
}
public static int nextPowerOfTwo(int n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
public static final int abs(int val) {
int sign = val >> 31;
return (val ^ sign) - sign;
}
public static final int golomb(int signedLevel) {
if (signedLevel == 0)
return 0;
return (abs(signedLevel) << 1) - ((signedLevel ^ 0xFFFFFFFF) >>> 31);
}
public static final int toSigned(int val, int sign) {
return (val ^ sign) - sign;
}
public static final int sign(int val) {
return -(val >> 31);
}
public static int wrap(int picNo, int maxFrames) {
return (picNo < 0) ? (picNo + maxFrames) : ((picNo >= maxFrames) ? (picNo - maxFrames) : picNo);
}
public static int max3(int a, int b, int c) {
return Math.max(Math.max(a, b), c);
}
}

View file

@ -0,0 +1,226 @@
package org.jcodec.common.tools;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jcodec.common.IntArrayList;
import org.jcodec.common.io.NIOUtils;
public class ToJSON {
private static final Set<Class> primitive = new HashSet<>();
private static final Set<String> omitMethods = new HashSet<>();
static {
primitive.add(Boolean.class);
primitive.add(Byte.class);
primitive.add(Short.class);
primitive.add(Integer.class);
primitive.add(Long.class);
primitive.add(Float.class);
primitive.add(Double.class);
primitive.add(Character.class);
omitMethods.add("getClass");
omitMethods.add("get");
}
public static String toJSON(Object obj) {
StringBuilder builder = new StringBuilder();
IntArrayList stack = IntArrayList.createIntArrayList();
toJSONSub(obj, stack, builder);
return builder.toString();
}
private static void toJSONSub(Object obj, IntArrayList stack, StringBuilder builder) {
if (obj == null) {
builder.append("null");
return;
}
String className = obj.getClass().getName();
if (className.startsWith("java.lang") && !className.equals("java.lang.String")) {
builder.append("null");
return;
}
int id = System.identityHashCode(obj);
if (stack.contains(id)) {
builder.append("null");
return;
}
stack.push(id);
if (obj instanceof ByteBuffer)
obj = NIOUtils.toArray((ByteBuffer)obj);
if (obj == null) {
builder.append("null");
} else if (obj instanceof String) {
builder.append("\"");
escape((String)obj, builder);
builder.append("\"");
} else if (obj instanceof Map) {
Iterator<Map.Entry> it = ((Map)obj).entrySet().iterator();
builder.append("{");
while (it.hasNext()) {
Map.Entry e = it.next();
builder.append("\"");
builder.append(e.getKey());
builder.append("\":");
toJSONSub(e.getValue(), stack, builder);
if (it.hasNext())
builder.append(",");
}
builder.append("}");
} else if (obj instanceof Iterable) {
Iterator it = ((Iterable)obj).iterator();
builder.append("[");
while (it.hasNext()) {
toJSONSub(it.next(), stack, builder);
if (it.hasNext())
builder.append(",");
}
builder.append("]");
} else if (obj instanceof Object[]) {
builder.append("[");
int len = Array.getLength(obj);
for (int i = 0; i < len; i++) {
toJSONSub(Array.get(obj, i), stack, builder);
if (i < len - 1)
builder.append(",");
}
builder.append("]");
} else if (obj instanceof long[]) {
long[] a = (long[])obj;
builder.append("[");
for (int i = 0; i < a.length; i++) {
builder.append(String.format("0x%016x", a[i]));
if (i < a.length - 1)
builder.append(",");
}
builder.append("]");
} else if (obj instanceof int[]) {
int[] a = (int[])obj;
builder.append("[");
for (int i = 0; i < a.length; i++) {
builder.append(String.format("0x%08x", a[i]));
if (i < a.length - 1)
builder.append(",");
}
builder.append("]");
} else if (obj instanceof float[]) {
float[] a = (float[])obj;
builder.append("[");
for (int i = 0; i < a.length; i++) {
builder.append(String.format("%.3f", a[i]));
if (i < a.length - 1)
builder.append(",");
}
builder.append("]");
} else if (obj instanceof double[]) {
double[] a = (double[])obj;
builder.append("[");
for (int i = 0; i < a.length; i++) {
builder.append(String.format("%.6f", a[i]));
if (i < a.length - 1)
builder.append(",");
}
builder.append("]");
} else if (obj instanceof short[]) {
short[] a = (short[])obj;
builder.append("[");
for (int i = 0; i < a.length; i++) {
builder.append(String.format("0x%04x", a[i]));
if (i < a.length - 1)
builder.append(",");
}
builder.append("]");
} else if (obj instanceof byte[]) {
byte[] a = (byte[])obj;
builder.append("[");
for (int i = 0; i < a.length; i++) {
builder.append(String.format("0x%02x", a[i]));
if (i < a.length - 1)
builder.append(",");
}
builder.append("]");
} else if (obj instanceof boolean[]) {
boolean[] a = (boolean[])obj;
builder.append("[");
for (int i = 0; i < a.length; i++) {
builder.append(a[i]);
if (i < a.length - 1)
builder.append(",");
}
builder.append("]");
} else if (obj.getClass().isEnum()) {
builder.append(String.valueOf(obj));
} else {
builder.append("{");
Method[] methods = obj.getClass().getMethods();
List<Method> filteredMethods = new ArrayList<>();
for (Method method : methods) {
if (!omitMethods.contains(method.getName()) && isGetter(method))
filteredMethods.add(method);
}
Iterator<Method> iterator = filteredMethods.iterator();
while (iterator.hasNext()) {
Method method = iterator.next();
String name = toName(method);
invoke(obj, stack, builder, method, name);
if (iterator.hasNext())
builder.append(",");
}
builder.append("}");
}
stack.pop();
}
private static void invoke(Object obj, IntArrayList stack, StringBuilder builder, Method method, String name) {
try {
Object invoke = method.invoke(obj);
builder.append('"');
builder.append(name);
builder.append("\":");
if (invoke != null && primitive.contains(invoke.getClass())) {
builder.append(invoke);
} else {
toJSONSub(invoke, stack, builder);
}
} catch (Exception e) {}
}
private static void escape(String invoke, StringBuilder sb) {
char[] ch = invoke.toCharArray();
for (char c : ch) {
if (c < ' ') {
sb.append(String.format("\\%02x", Integer.valueOf(c)));
} else {
sb.append(c);
}
}
}
private static String toName(Method method) {
if (!isGetter(method))
throw new IllegalArgumentException("Not a getter");
char[] name = method.getName().toCharArray();
int ind = (name[0] == 'g') ? 3 : 2;
name[ind] = Character.toLowerCase(name[ind]);
return new String(name, ind, name.length - ind);
}
private static boolean isGetter(Method method) {
if (!Modifier.isPublic(method.getModifiers()))
return false;
if (!method.getName().startsWith("get") && (
!method.getName().startsWith("is") || method.getReturnType() != boolean.class))
return false;
if ((method.getParameterTypes()).length != 0)
return false;
return true;
}
}

View file

@ -0,0 +1,74 @@
package org.jcodec.common.tools;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import org.jcodec.codecs.wav.WavHeader;
import org.jcodec.common.AudioUtil;
import org.jcodec.common.io.IOUtils;
import org.jcodec.common.io.NIOUtils;
public class WavMerge {
public static void main1(String[] args) throws Exception {
if (args.length < 3) {
System.out.println("wavmerge <output wav> <input wav> .... <input wav>");
System.exit(-1);
}
File out = new File(args[0]);
File[] ins = new File[args.length - 1];
for (int i = 1; i < args.length; i++)
ins[i - 1] = new File(args[i]);
merge(out, ins);
}
public static void merge(File result, File[] src) throws IOException {
WritableByteChannel out = null;
ReadableByteChannel[] inputs = new ReadableByteChannel[src.length];
WavHeader[] headers = new WavHeader[src.length];
ByteBuffer[] ins = new ByteBuffer[src.length];
try {
int sampleSize = -1;
for (int i = 0; i < src.length; i++) {
inputs[i] = NIOUtils.readableChannel(src[i]);
WavHeader hdr = WavHeader.readChannel(inputs[i]);
if (sampleSize != -1 && sampleSize != hdr.fmt.bitsPerSample)
throw new RuntimeException("Input files have different sample sizes");
sampleSize = hdr.fmt.bitsPerSample;
headers[i] = hdr;
ins[i] = ByteBuffer.allocate(hdr.getFormat().framesToBytes(4096));
}
ByteBuffer outb = ByteBuffer.allocate(headers[0].getFormat().framesToBytes(4096) * src.length);
WavHeader newHeader = WavHeader.multiChannelWav(headers);
out = NIOUtils.writableChannel(result);
newHeader.write(out);
boolean readOnce = true;
while (true) {
readOnce = false;
for (int j = 0; j < ins.length; j++) {
if (inputs[j] != null) {
ins[j].clear();
if (inputs[j].read(ins[j]) == -1) {
NIOUtils.closeQuietly(inputs[j]);
inputs[j] = null;
} else {
readOnce = true;
}
ins[j].flip();
}
}
if (!readOnce)
break;
outb.clear();
AudioUtil.interleave(headers[0].getFormat(), ins, outb);
outb.flip();
out.write(outb);
}
} finally {
IOUtils.closeQuietly(out);
for (ReadableByteChannel inputStream : inputs)
IOUtils.closeQuietly(inputStream);
}
}
}

View file

@ -0,0 +1,62 @@
package org.jcodec.common.tools;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.util.Arrays;
import org.jcodec.codecs.wav.WavHeader;
import org.jcodec.common.AudioFormat;
import org.jcodec.common.AudioUtil;
import org.jcodec.common.Preconditions;
import org.jcodec.common.io.FileChannelWrapper;
import org.jcodec.common.io.NIOUtils;
import org.jcodec.common.io.SeekableByteChannel;
public class WavSplit {
public static final MainUtils.Flag FLAG_PATTERN = MainUtils.Flag.flag("pattern", "p", "Output file name pattern, i.e. out%02d.wav");
private static final MainUtils.Flag[] ALL_FLAGS = new MainUtils.Flag[] { FLAG_PATTERN };
public static void main1(String[] args) throws Exception {
MainUtils.Cmd cmd = MainUtils.parseArguments(args, ALL_FLAGS);
if (cmd.argsLength() < 1) {
MainUtils.printHelp(ALL_FLAGS, Arrays.asList("filename.wav"));
System.exit(-1);
}
File s = new File(args[0]);
String pattern = cmd.getStringFlagD(FLAG_PATTERN, "c%02d.wav");
WavHeader wavHeader = WavHeader.read(s);
System.out.println("WAV: " + String.valueOf(wavHeader.getFormat()));
Preconditions.checkState((2 == wavHeader.fmt.numChannels));
int dataOffset = wavHeader.dataOffset;
FileChannelWrapper is = NIOUtils.readableChannel(s);
is.setPosition((long)dataOffset);
int channels = wavHeader.getFormat().getChannels();
SeekableByteChannel[] out = new SeekableByteChannel[channels];
for (int j = 0; j < channels; j++) {
out[j] = NIOUtils.writableChannel(new File(s.getParentFile(), String.format(pattern, j)));
WavHeader.copyWithChannels(wavHeader, 1).write(out[j]);
}
copy(wavHeader.getFormat(), is, out);
for (int i = 0; i < channels; i++)
out[i].close();
}
private static void copy(AudioFormat format, ReadableByteChannel is, SeekableByteChannel[] out) throws IOException {
ByteBuffer[] outs = new ByteBuffer[out.length];
for (int i = 0; i < out.length; i++)
outs[i] = ByteBuffer.allocate(format.framesToBytes(4096));
ByteBuffer inb = ByteBuffer.allocate(format.framesToBytes(4096) * out.length);
while (is.read(inb) != -1) {
inb.flip();
AudioUtil.deinterleave(format, inb, outs);
inb.clear();
for (int j = 0; j < out.length; j++) {
outs[j].flip();
out[j].write(outs[j]);
outs[j].clear();
}
}
}
}