Skip to content

Commit

Permalink
Allow to encode H.264 videos without motion search.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandelshofer committed Oct 5, 2024
1 parent 7118688 commit fa6e55f
Show file tree
Hide file tree
Showing 30 changed files with 290 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.monte.demo.javafx.movieplayer.model.AudioTrackInterface;
import org.monte.demo.javafx.movieplayer.model.MediaPlayerInterface;
import org.monte.demo.javafx.movieplayer.model.TrackInterface;
import org.monte.media.util.MathUtil;

import java.net.URL;
import java.text.NumberFormat;
Expand Down Expand Up @@ -115,17 +116,14 @@ public void setPlayer(MediaPlayerInterface p) {
@FXML
void togglePlayPause(ActionEvent event) {
MediaPlayerInterface p = getPlayer();
if (p != null) {
if (p != null && p.getStatus() != null) {
switch (p.getStatus()) {
case null -> {
// do nothing
}
default -> {
default:
p.play();
}
case PLAYING -> {
break;
case PLAYING:
p.pause();
}
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,26 @@ protected void doRealizing() throws Exception {
Format format = trackFormat;
final Map<String, Object> metadata = new LinkedHashMap<>();
format.getProperties().entrySet().iterator().forEachRemaining(e -> metadata.put(e.getKey().getName(), e.getValue()));
tracks.add(switch (format.get(MediaTypeKey)) {
case FormatKeys.MediaType.VIDEO -> {
TrackInterface newTrack;
switch (format.get(MediaTypeKey)) {
case VIDEO:
MonteVideoTrack videoTrack = realizeVideoTrack(i, metadata, format, trackFormat);
trackWidth = Math.max(trackWidth, trackFormat.get(VideoFormatKeys.WidthKey));
trackHeight = Math.max(trackHeight, trackFormat.get(VideoFormatKeys.HeightKey));
yield videoTrack;
}
case FormatKeys.MediaType.AUDIO -> {
newTrack = videoTrack;
break;
case AUDIO:
MonteAudioTrack audioTrack = realizeAudioTrack(i, metadata, trackFormat, format);
yield audioTrack;
}
case FormatKeys.MediaType.TEXT -> realizeSubtitleTrack(i, metadata);
default -> new MonteUnsupportedTrack(Locale.ENGLISH, i, i + "", metadata);
});
newTrack = audioTrack;
break;
case TEXT:
newTrack = realizeSubtitleTrack(i, metadata);
break;
default:
newTrack = new MonteUnsupportedTrack(Locale.ENGLISH, i, i + "", metadata);
break;
}
tracks.add(newTrack);
}
int finalWidth = mediaWidth == 0 ? trackWidth : mediaWidth;
int finalHeight = mediaHeight == 0 ? trackHeight : mediaHeight;
Expand Down
4 changes: 4 additions & 0 deletions org.monte.demo.moviewriter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
<groupId>ch.randelshofer</groupId>
<artifactId>org.monte.media</artifactId>
</dependency>
<dependency>
<groupId>ch.randelshofer</groupId>
<artifactId>org.monte.media.jcodec</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
requires java.desktop;

requires org.monte.media;
requires static org.monte.media.jcodec;
requires org.monte.media.jcodec;

exports org.monte.demo.moviewriter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
*/
package org.monte.demo.moviewriter;

import org.monte.media.av.Codec;
import org.monte.media.av.CodecSpi;
import org.monte.media.av.Format;
import org.monte.media.av.FormatKeys.MediaType;
import org.monte.media.av.MovieWriter;
import org.monte.media.av.MovieWriterSpi;
import org.monte.media.av.Registry;
import org.monte.media.av.codec.video.VideoFormatKeys.PixelFormat;
import org.monte.media.avi.AVIReader;
import org.monte.media.avi.AVIWriter;
import org.monte.media.color.Colors;
import org.monte.media.jcodec.mp4.JCodecMP4WriterSpi;
import org.monte.media.math.Rational;
import org.monte.media.mp4.MP4WriterSpi;
import org.monte.media.mp4.codec.video.H264CodecSpi;

import javax.imageio.ImageIO;
import java.awt.BasicStroke;
Expand All @@ -26,8 +32,11 @@
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.monte.media.av.FormatKeys.DataClassKey;
import static org.monte.media.av.FormatKeys.EncodingKey;
import static org.monte.media.av.FormatKeys.FrameRateKey;
import static org.monte.media.av.FormatKeys.KeyFrameIntervalKey;
Expand All @@ -39,6 +48,7 @@
import static org.monte.media.av.codec.video.VideoFormatKeys.ENCODING_AVI_PNG;
import static org.monte.media.av.codec.video.VideoFormatKeys.ENCODING_AVI_RLE8;
import static org.monte.media.av.codec.video.VideoFormatKeys.ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE;
import static org.monte.media.av.codec.video.VideoFormatKeys.ENCODING_BUFFERED_IMAGE;
import static org.monte.media.av.codec.video.VideoFormatKeys.ENCODING_QUICKTIME_ANIMATION;
import static org.monte.media.av.codec.video.VideoFormatKeys.ENCODING_QUICKTIME_JPEG;
import static org.monte.media.av.codec.video.VideoFormatKeys.ENCODING_QUICKTIME_PNG;
Expand Down Expand Up @@ -128,7 +138,7 @@ private static void drawClockHand(Graphics2D g, int cx, int cy, int radius1, int
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
System.out.println("MovieWriterDemo " + Main.class.getPackage().getImplementationVersion());
System.out.println("This is a demo of the Monte Media library.");
System.out.println("Copyright © Werner Randelshofer. All Rights Reserved.");
Expand All @@ -138,40 +148,52 @@ public static void main(String[] args) {
try {
var m = new Main();
Format baseFormat = new Format(QualityKey, 0.75f, KeyFrameIntervalKey, 60);
m.test(new File("moviewriterdemo-h264-motion0.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 0));
m.test(new File("moviewriterdemo-h264-motion0.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 0));
m.test(new File("moviewriterdemo-h264-motion0.mp4"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 0));
m.test(new File("moviewriterdemo-h264-motion16.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 16));
m.test(new File("moviewriterdemo-h264-motion16.mp4"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 16));
m.test(new File("moviewriterdemo-jpg-q0.75.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_MJPG, DepthKey, 24, QualityKey, 0.75f));
m.test(new File("moviewriterdemo-jpg-q0.75.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_JPEG, DepthKey, 24, QualityKey, 0.75f));
m.test(new File("moviewriterdemo-png.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_PNG, DepthKey, 24));
m.test(new File("moviewriterdemo-png.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_PNG, DepthKey, 24));
m.test(new File("moviewriterdemo-png.zip"), baseFormat.prepend(EncodingKey, ENCODING_AVI_PNG, DepthKey, 24));
m.test(new File("moviewriterdemo-raw24.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_DIB, DepthKey, 24));
m.test(new File("moviewriterdemo-raw24.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_RAW, DepthKey, 24));
m.test(new File("moviewriterdemo-raw8.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_DIB, DepthKey, 8));
m.test(new File("moviewriterdemo-raw8.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_RAW, DepthKey, 8));
m.test(new File("moviewriterdemo-raw8gray.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_DIB, DepthKey, 8, PixelFormatKey, PixelFormat.GRAY));
m.test(new File("moviewriterdemo-rle16.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_ANIMATION, DepthKey, 16));
m.test(new File("moviewriterdemo-rle24.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_ANIMATION, DepthKey, 24));
m.test(new File("moviewriterdemo-rle8.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_RLE8, DepthKey, 8));
m.test(new File("moviewriterdemo-rle8.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_ANIMATION, DepthKey, 8));
m.test(new File("moviewriterdemo-rle8gray.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_RLE8, DepthKey, 8, PixelFormatKey, PixelFormat.GRAY));
m.test(new File("moviewriterdemo-tscc16.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 16));
m.test(new File("moviewriterdemo-tscc16.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 16));
m.test(new File("moviewriterdemo-tscc24.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 24));
m.test(new File("moviewriterdemo-tscc24.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 24));
m.test(new File("moviewriterdemo-tscc8.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 8));
m.test(new File("moviewriterdemo-tscc8.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 8));
m.test(new File("moviewriterdemo-tscc8gray.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 8, PixelFormatKey, PixelFormat.GRAY));
m.test(new File("moviewriterdemo-tscc8gray.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 8, PixelFormatKey, PixelFormat.GRAY));
List<TestData> list = new ArrayList<>();
list.add(new TestData(new File("moviewriterdemo-h264-motion0-jcodec.mp4"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 0), null, new JCodecMP4WriterSpi()));
list.add(new TestData(new File("moviewriterdemo-h264-motion0.mp4"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 0), new H264CodecSpi(), new MP4WriterSpi()));
list.add(new TestData(new File("moviewriterdemo-h264-motion0.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 0)));
list.add(new TestData(new File("moviewriterdemo-h264-motion0.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 0)));
list.add(new TestData(new File("moviewriterdemo-h264-motion16.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 16)));
list.add(new TestData(new File("moviewriterdemo-h264-motion16.mp4"), baseFormat.prepend(EncodingKey, ENCODING_AVC1, DepthKey, 24, MotionSearchRangeKey, 16)));
list.add(new TestData(new File("moviewriterdemo-jpg-q0.75.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_MJPG, DepthKey, 24, QualityKey, 0.75f)));
list.add(new TestData(new File("moviewriterdemo-jpg-q0.75.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_JPEG, DepthKey, 24, QualityKey, 0.75f)));
list.add(new TestData(new File("moviewriterdemo-png.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_PNG, DepthKey, 24)));
list.add(new TestData(new File("moviewriterdemo-png.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_PNG, DepthKey, 24)));
list.add(new TestData(new File("moviewriterdemo-png.zip"), baseFormat.prepend(EncodingKey, ENCODING_AVI_PNG, DepthKey, 24)));
list.add(new TestData(new File("moviewriterdemo-raw24.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_DIB, DepthKey, 24)));
list.add(new TestData(new File("moviewriterdemo-raw24.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_RAW, DepthKey, 24)));
list.add(new TestData(new File("moviewriterdemo-raw8.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_DIB, DepthKey, 8)));
list.add(new TestData(new File("moviewriterdemo-raw8.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_RAW, DepthKey, 8)));
list.add(new TestData(new File("moviewriterdemo-raw8gray.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_DIB, DepthKey, 8, PixelFormatKey, PixelFormat.GRAY)));
list.add(new TestData(new File("moviewriterdemo-rle16.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_ANIMATION, DepthKey, 16)));
list.add(new TestData(new File("moviewriterdemo-rle24.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_ANIMATION, DepthKey, 24)));
list.add(new TestData(new File("moviewriterdemo-rle8.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_RLE8, DepthKey, 8)));
list.add(new TestData(new File("moviewriterdemo-rle8.mov"), baseFormat.prepend(EncodingKey, ENCODING_QUICKTIME_ANIMATION, DepthKey, 8)));
list.add(new TestData(new File("moviewriterdemo-rle8gray.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_RLE8, DepthKey, 8, PixelFormatKey, PixelFormat.GRAY)));
list.add(new TestData(new File("moviewriterdemo-tscc16.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 16)));
list.add(new TestData(new File("moviewriterdemo-tscc16.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 16)));
list.add(new TestData(new File("moviewriterdemo-tscc24.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 24)));
list.add(new TestData(new File("moviewriterdemo-tscc24.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 24)));
list.add(new TestData(new File("moviewriterdemo-tscc8.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 8)));
list.add(new TestData(new File("moviewriterdemo-tscc8.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 8)));
list.add(new TestData(new File("moviewriterdemo-tscc8gray.avi"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 8, PixelFormatKey, PixelFormat.GRAY)));
list.add(new TestData(new File("moviewriterdemo-tscc8gray.mov"), baseFormat.prepend(EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 8, PixelFormatKey, PixelFormat.GRAY)));

for (TestData data : list) {
m.test(data.file, data.format, data.codecSpi, data.movieWriterSpi);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

private void test(File file, Format format) throws IOException {
private record TestData(File file, Format format, CodecSpi codecSpi, MovieWriterSpi movieWriterSpi) {
public TestData(File file, Format format) {
this(file, format, null, null);
}
}

private void test(File file, Format format, CodecSpi codecSpi, MovieWriterSpi movieWriterSpi) throws IOException {
System.out.print("Writing " + file.getAbsolutePath());
long startTime = System.nanoTime();

Expand All @@ -192,10 +214,18 @@ private void test(File file, Format format) throws IOException {
int n = frameRate.multiply(60).intValue();
try {
// Create the writer
out = Registry.getInstance().getWriter(file);
out = movieWriterSpi == null ? Registry.getInstance().getWriter(file) : movieWriterSpi.create(file);

// Add a track to the writer
out.addTrack(format);
int trackIndex = out.addTrack(format);
if (codecSpi != null) {
Codec codec = codecSpi.create();
Format actualInputFormat = codec.setInputFormat(format.prepend(
EncodingKey, ENCODING_BUFFERED_IMAGE,
DataClassKey, BufferedImage.class));
codec.setOutputFormat(actualInputFormat.prepend(EncodingKey, format.get(EncodingKey), DataClassKey, byte[].class));
out.setCodec(trackIndex, codec);
}

// Draw the animation
for (int i = 0; i < n; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.monte.media.swing.BackgroundTask;
import org.monte.media.swing.JLabelHyperlinkHandler;
import org.monte.media.swing.datatransfer.DropFileTransferHandler;
import org.monte.media.util.MathUtil;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
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.Codec;
import org.monte.media.av.Format;
import org.monte.media.av.Multiplexer;
import org.monte.media.math.Rational;
Expand Down Expand Up @@ -40,6 +41,11 @@ public int addTrack(Format fmt) throws IOException {
return 0;
}

@Override
public void setCodec(int trackIndex, Codec codec) {
// do nothing
}

@Override
public void write(int trackIndex, Buffer buf) throws IOException {
if (!buf.isFlag(DISCARD)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
org.monte.media.jcodec.codec.JCodecPictureCodecSpi
org.monte.media.jcodec.codec.JCodecH264CodecSpi
org.monte.media.jcodec.h264.JCodecPictureCodecSpi
org.monte.media.jcodec.h264.JCodecH264CodecSpi
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Copyright © 2017 Werner Randelshofer, Switzerland. MIT License.
*/

import org.monte.media.jcodec.codec.JCodecH264CodecSpi;
import org.monte.media.jcodec.codec.JCodecPictureCodecSpi;
import org.monte.media.jcodec.h264.JCodecH264CodecSpi;
import org.monte.media.jcodec.h264.JCodecPictureCodecSpi;
import org.monte.media.jcodec.mp4.JCodecMP4WriterSpi;

/**
Expand All @@ -16,6 +16,9 @@
requires jcodec;
requires java.desktop;

exports org.monte.media.jcodec.h264;
exports org.monte.media.jcodec.mp4;

requires org.monte.media;

provides org.monte.media.av.MovieWriterSpi with JCodecMP4WriterSpi;
Expand Down
Loading

0 comments on commit fa6e55f

Please sign in to comment.