Skip to content

Commit

Permalink
Use Math.clamp().
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandelshofer committed Aug 9, 2024
1 parent 5d68072 commit c9a07a8
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.monte.media.av.Format;
import org.monte.media.math.Rational;
import org.monte.media.mp3.MP3AudioInputStream;
import org.monte.media.quicktime.QuickTimeOutputStream;
import org.monte.media.quicktime.QuickTimeWriter;
import org.monte.media.swing.BackgroundTask;
import org.monte.media.swing.datatransfer.FileTextFieldTransferHandler;
Expand Down Expand Up @@ -851,9 +852,9 @@ private void writeVideoAndAudio(File movieFile, File[] imgFiles, File audioFile,
}
}
if (longerTrack != -1) {
LinkedList<QuickTimeWriter.Edit> l = new LinkedList<>();
l.add(new QuickTimeWriter.Edit(shorterDuration, 0, 1.0)); // sampleDuration, media time, media rate
qtOut.setEditList(longerTrack, l.toArray(new QuickTimeWriter.Edit[l.size()]));
LinkedList<QuickTimeOutputStream.Edit> l = new LinkedList<>();
l.add(new QuickTimeOutputStream.Edit(shorterDuration, 0, 1.0)); // sampleDuration, media time, media rate
qtOut.setEditList(longerTrack, l.toArray(new QuickTimeOutputStream.Edit[l.size()]));
}
}
break;
Expand All @@ -880,21 +881,21 @@ private void writeVideoAndAudio(File movieFile, File[] imgFiles, File audioFile,
}
}
if (longerTrack != -1) {
LinkedList<QuickTimeWriter.Edit> l = new LinkedList<>();
LinkedList<QuickTimeOutputStream.Edit> l = new LinkedList<>();
for (; longerDuration > 0; longerDuration -= shorterDuration) {
l.add(new QuickTimeWriter.Edit(min(shorterDuration, longerDuration), 0, 1.0)); // sampleDuration, media time, media rate
l.add(new QuickTimeOutputStream.Edit(min(shorterDuration, longerDuration), 0, 1.0)); // sampleDuration, media time, media rate
}
qtOut.setEditList(shorterTrack, l.toArray(new QuickTimeWriter.Edit[l.size()]));
qtOut.setEditList(shorterTrack, l.toArray(new QuickTimeOutputStream.Edit[l.size()]));
}
}
break;
case STRETCH_AND_SQUASH_VIDEO_TRACK: {
long d0 = qtOut.getTrackDuration(at);
long d1 = qtOut.getTrackDuration(vt);
if (d0 != d1 && d0 != 0 && d1 != 0) {
LinkedList<QuickTimeWriter.Edit> l = new LinkedList<>();
l.add(new QuickTimeWriter.Edit((int) d0, 0, d1 / (float) d0)); // sampleDuration, media time, media rate
qtOut.setEditList(1, l.toArray(new QuickTimeWriter.Edit[l.size()]));
LinkedList<QuickTimeOutputStream.Edit> l = new LinkedList<>();
l.add(new QuickTimeOutputStream.Edit((int) d0, 0, d1 / (float) d0)); // sampleDuration, media time, media rate
qtOut.setEditList(1, l.toArray(new QuickTimeOutputStream.Edit[l.size()]));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private void createFrame() {
JPanel panel = new JPanel(new BorderLayout());
imageLabel = new JLabel();
slider = new JSlider();
slider.setMinimum(0);
panel.add(imageLabel, BorderLayout.CENTER);
panel.add(slider, BorderLayout.SOUTH);
frame.getContentPane().add(panel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.monte.media.amigabitmap.AmigaBitmapImage;
import org.monte.media.av.Buffer;
import org.monte.media.av.Format;
import org.monte.media.av.Multiplexer;
import org.monte.media.math.Rational;

Expand All @@ -21,16 +22,22 @@
*
* @author Werner Randelshofer
*/
public class ANIMMultiplexer extends ANIMOutputStream implements Multiplexer {
public class ANIMMultiplexer implements Multiplexer {
private final ANIMOutputStream out;

protected Rational inputTime;

public ANIMMultiplexer(File file) throws IOException {
super(file);
this.out = new ANIMOutputStream(file);
}

public ANIMMultiplexer(ImageOutputStream out) throws IOException {
super(out);
this.out = new ANIMOutputStream(out);
}

@Override
public int addTrack(Format fmt) throws IOException {
return 0;
}

@Override
Expand All @@ -40,14 +47,14 @@ public void write(int trackIndex, Buffer buf) throws IOException {
// Or maybe, just let them accumulate. In case the
// frames are compressed, we can't do anything at this
// stage anyway.
long jiffies = getJiffies();
long jiffies = out.getJiffies();

if (inputTime == null) {
inputTime = new Rational(0, 1);
}
inputTime = inputTime.add(buf.sampleDuration.multiply(buf.sampleCount));

Rational outputTime = new Rational(getMovieTime(), jiffies);
Rational outputTime = new Rational(out.getMovieTime(), jiffies);
Rational outputDuration = inputTime.subtract(outputTime);


Expand All @@ -58,7 +65,28 @@ public void write(int trackIndex, Buffer buf) throws IOException {
outputTime.add(new Rational(outputMediaDuration, jiffies));
// System.out.println("ANIMMultiplexer #" + frameCount + " jiffies:"+jiffies+" movieT:" + outputTime + " inputT:" + inputTime+" diff:"+(outputTime.subtract(inputTime))+ " sampleDuration:" + outputMediaDuration + " == " + outputDuration+" ~= "+buf.sampleDuration);

writeFrame((AmigaBitmapImage) buf.data, outputMediaDuration);
out.writeFrame((AmigaBitmapImage) buf.data, outputMediaDuration);
}
}

@Override
public void close() throws IOException {
out.close();
}

/**
* Sets the Commodore Amiga Graphics Mode. The default value is 0.
* <p>
* The graphics mode is an or-combination of the monitor ID and the mode ID.
* <p>
* Example:
* <pre>
* setCAMG(PAL_MONITOR_ID|HAM_MODE);
* </pre>
* <p>
* Also sets the Jiffies for the Graphics Mode.
*/
public void setCAMG(int newValue) {
out.setCAMG(newValue);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.util.TreeSet;
import java.util.zip.InflaterInputStream;

import static org.monte.media.math.MathUtils.clamp;

/**
* {@code QuickTimeDeserializer}. This is an internal class of
* QuickTimeInputStream.
Expand Down Expand Up @@ -1019,8 +1017,12 @@ protected void parseVideoSampleDescription(QTFFImageInputStream in, long remaini
int descriptionVersion = in.readUnsignedShort();
int revisionLevel = in.readUnsignedShort();
int vendor = in.readInt();
d.videoTemporalQuality = clamp(in.readInt() / 1024f, 0.0f, 1.0f);
d.videoSpatialQuality = clamp(in.readInt() / 1024f, 0.0f, 1.0f);
float value1 = in.readInt() / 1024f;

d.videoTemporalQuality = Math.clamp(value1, 0.0f, 1.0f);
float value = in.readInt() / 1024f;

d.videoSpatialQuality = Math.clamp(value, 0.0f, 1.0f);
d.videoWidth = in.readUnsignedShort();
d.videoHeight = in.readUnsignedShort();
d.videoHorizontalResolution = in.readFixed16D16();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,126 @@ public void setMovieTimeScale(long timeScale) {
public void finish() throws IOException {
out.finish();
}


/**
* Sets the sync interval for the specified video track.
*
* @param track The track number.
* @param i Interval between sync samples (keyframes). 0 = automatic. 1 =
* write all samples as sync samples. n = sync every n-th sample.
*/
public void setSyncInterval(int track, int i) {
out.setSyncInterval(track, i);
}

/**
* Writes an already encoded sample from a file into a track. <p> This
* method does not inspect the contents of the samples. The contents has to
* match the format and dimensions of the media in this track.
*
* @param track The track index.
* @param file The file which holds the encoded data sample.
* @param duration The duration of the sample in media time scale units.
* @param isSync whether the sample is a sync sample (key frame).
* @throws IOException if writing the sample data failed.
*/
public void writeSample(int track, File file, long duration, boolean isSync) throws IOException {
out.writeSample(track, file, duration, isSync);
}

/**
* Writes multiple sync samples from a byte array into a track. <p> This
* method does not inspect the contents of the samples. The contents has to
* match the format and dimensions of the media in this track.
*
* @param track The track index.
* @param sampleCount The number of samples.
* @param data The encoded sample data.
* @param off The start offset in the data.
* @param len The number of bytes to write. Must be dividable by
* sampleCount.
* @param sampleDuration The duration of a sample. All samples must have the
* same duration.
* @throws IllegalArgumentException if the duration is less than 1.
* @throws IOException if writing the sample data failed.
*/
public void writeSamples(int track, int sampleCount, byte[] data, int off, int len, long sampleDuration) throws IOException {
out.writeSamples(track, sampleCount, data, off, len, sampleDuration);
}

/**
* Writes a version of the movie which is optimized for the web into the
* specified output file. <p> This method finishes the movie and then copies
* its content into the specified file. The web-optimized file starts with
* the movie header.
*
* @param outputFile The output file
* @param compressHeader Whether the movie header shall be compressed.
*/
public void toWebOptimizedMovie(File outputFile, boolean compressHeader) throws IOException {
out.toWebOptimizedMovie(outputFile, compressHeader);
}

/**
* Returns the time scale of the media in a track.
*
* @param track Track index.
* @return time scale
* @see #setMovieTimeScale(long)
*/
public long getMediaTimeScale(int track) {
return out.getMediaTimeScale(track);
}

/**
* Sets the compression quality of a track. <p> A value of 0 stands for
* "high compression is important" a value of 1 for "high image quality is
* important". <p> Changing this value affects the encoding of video frames
* which are subsequently written into the track. Frames which have already
* been written are not changed. <p> This value has no effect on videos
* encoded with lossless encoders such as the PNG format. <p> The default
* value is 0.97.
*
* @param newValue
*/
public void setCompressionQuality(int track, float newValue) {
out.setCompressionQuality(track, newValue);
}

/**
* Returns the time scale of the movie.
*
* @return time scale
* @see #setMovieTimeScale(long)
*/
public long getMovieTimeScale() {
return out.getMovieTimeScale();
}

/**
* Returns the track duration in the movie's time scale. <p> If the track
* has an edit-list, the track duration is the sum of all edit durations.
* <p> If the track does not have an edit-list, then this method returns the
* media duration of the track in the movie's time scale.
*
* @param track Track index.
* @return track duration
*/
public long getTrackDuration(int track) {
return out.getTrackDuration(track);
}


/**
* Sets the edit list for the specified track. <p> In the absence of an edit
* list, the presentation of the track starts immediately. An empty edit is
* used to offset the start time of a track. <p>
*
* @throws IllegalArgumentException If the edit list ends with an empty
* edit.
*/
public void setEditList(int track, AbstractQuickTimeStream.Edit[] editList) {
out.setEditList(track, editList);
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
</modules>

<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>${git.commit.time}</project.build.outputTimestamp>
Expand Down

0 comments on commit c9a07a8

Please sign in to comment.