Skip to content

Commit 7d17f11

Browse files
authored
Add support for parsing all the codec flags
* Add support for parsing all the codec flags, e.g IntraFrameOnly, Lossy and Lossless.
1 parent 4569a57 commit 7d17f11

File tree

2 files changed

+137
-4
lines changed

2 files changed

+137
-4
lines changed

src/main/java/net/bramp/ffmpeg/info/Codec.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public class Codec {
2525
/** What type of codec is this */
2626
private final CodecType type;
2727

28+
/** Intra frame only codec */
29+
final boolean intraFrameOnly;
30+
31+
/** Codec supports lossy compression */
32+
final boolean lossyCompression;
33+
34+
/** Codeco supports lessless compression */
35+
final boolean losslessCompression;
36+
2837
/**
2938
* @param name short codec name
3039
* @param longName long codec name
@@ -35,6 +44,8 @@ public class Codec {
3544
* ..V... = Video codec
3645
* ..A... = Audio codec
3746
* ..S... = Subtitle codec
47+
* ..D... = Data codec
48+
* ..T... = Attachment codec
3849
* ...I.. = Intra frame-only codec
3950
* ....L. = Lossy compression
4051
* .....S = Lossless compression
@@ -45,9 +56,19 @@ public Codec(String name, String longName, String flags) {
4556
this.longName = Preconditions.checkNotNull(longName).trim();
4657

4758
Preconditions.checkNotNull(flags);
48-
Preconditions.checkArgument(flags.length() == 6, "Format flags is invalid '%s'", flags);
49-
this.canDecode = flags.charAt(0) == 'D';
50-
this.canEncode = flags.charAt(1) == 'E';
59+
Preconditions.checkArgument(flags.length() == 6, "Codec flags is invalid '%s'", flags);
60+
61+
switch (flags.charAt(0)) {
62+
case 'D': this.canDecode = true; break;
63+
case '.': this.canDecode = false; break;
64+
default: throw new IllegalArgumentException("Invalid decoding value '" + flags.charAt(0) + "'");
65+
}
66+
67+
switch (flags.charAt(1)) {
68+
case 'E': this.canEncode = true; break;
69+
case '.': this.canEncode = false; break;
70+
default: throw new IllegalArgumentException("Invalid encoding value '" + flags.charAt(1) + "'");
71+
}
5172

5273
switch (flags.charAt(2)) {
5374
case 'V':
@@ -69,7 +90,24 @@ public Codec(String name, String longName, String flags) {
6990
throw new IllegalArgumentException("Invalid codec type '" + flags.charAt(2) + "'");
7091
}
7192

72-
// TODO There are more flags to parse
93+
switch (flags.charAt(3)) {
94+
case 'I': this.intraFrameOnly = true; break;
95+
case '.': this.intraFrameOnly = false; break;
96+
default: throw new IllegalArgumentException("Invalid encoding value '" + flags.charAt(3) + "'");
97+
}
98+
99+
switch (flags.charAt(4)) {
100+
case 'L': this.lossyCompression = true; break;
101+
case '.': this.lossyCompression = false; break;
102+
default: throw new IllegalArgumentException("Invalid lossy compression value '" + flags.charAt(4) + "'");
103+
}
104+
105+
switch (flags.charAt(5)) {
106+
case 'S': this.losslessCompression = true; break;
107+
case '.': this.losslessCompression = false; break;
108+
default: throw new IllegalArgumentException("Invalid lossless compression value '" + flags.charAt(5) + "'");
109+
}
110+
73111
}
74112

75113
@Override
@@ -106,4 +144,17 @@ public boolean getCanEncode() {
106144
public CodecType getType() {
107145
return type;
108146
}
147+
148+
public boolean isIntraFrameOnly() {
149+
return intraFrameOnly;
150+
}
151+
152+
public boolean supportsLossyCompression() {
153+
return lossyCompression;
154+
}
155+
156+
public boolean supportsLosslessCompression() {
157+
return losslessCompression;
158+
}
159+
109160
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package net.bramp.ffmpeg.info;
2+
3+
import static org.hamcrest.Matchers.is;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
import net.bramp.ffmpeg.shared.CodecType;
9+
10+
public class CodecTest {
11+
@Test
12+
public void testCodecConstructor() {
13+
Codec c1 = new Codec("012v", "Uncompressed 4:2:2 10-bit", "D.VI.S");
14+
assertThat(c1.getName(), is("012v"));
15+
assertThat(c1.getLongName(), is("Uncompressed 4:2:2 10-bit"));
16+
assertThat(c1.getCanDecode(), is(true));
17+
assertThat(c1.getCanEncode(), is(false));
18+
assertThat(c1.getType(), is(CodecType.VIDEO));
19+
assertThat(c1.isIntraFrameOnly(), is(true));
20+
assertThat(c1.supportsLossyCompression(), is(false));
21+
assertThat(c1.losslessCompression, is(true));
22+
23+
Codec c2 = new Codec("4xm", "4X Movie", "D.V.L.");
24+
assertThat(c2.getName(), is("4xm"));
25+
assertThat(c2.getLongName(), is("4X Movie"));
26+
assertThat(c2.getCanDecode(), is(true));
27+
assertThat(c2.getCanEncode(), is(false));
28+
assertThat(c2.getType(), is(CodecType.VIDEO));
29+
assertThat(c2.isIntraFrameOnly(), is(false));
30+
assertThat(c2.supportsLossyCompression(), is(true));
31+
assertThat(c2.supportsLosslessCompression(), is(false));
32+
33+
Codec c3 = new Codec("alias_pix", "Alias/Wavefront PIX image", "DEVI.S");
34+
assertThat(c3.getName(), is("alias_pix"));
35+
assertThat(c3.getLongName(), is("Alias/Wavefront PIX image"));
36+
assertThat(c3.getCanDecode(), is(true));
37+
assertThat(c3.getCanEncode(), is(true));
38+
assertThat(c3.getType(), is(CodecType.VIDEO));
39+
assertThat(c3.isIntraFrameOnly(), is(true));
40+
assertThat(c3.supportsLossyCompression(), is(false));
41+
assertThat(c3.supportsLosslessCompression(), is(true));
42+
43+
Codec c4 = new Codec("binkaudio_rdft", "Bink Audio (RDFT)", "D.AIL.");
44+
assertThat(c4.getType(), is(CodecType.AUDIO));
45+
46+
Codec c6 = new Codec("mov_text", "MOV text", "DES...");
47+
assertThat(c6.getType(), is(CodecType.SUBTITLE));
48+
49+
Codec c7 = new Codec("bin_data", "binary data", "..D...");
50+
assertThat(c7.getType(), is(CodecType.DATA));
51+
}
52+
53+
@Test(expected = IllegalArgumentException.class)
54+
public void testBadDecodeValue() {
55+
new Codec("test", "test", "X.V...");
56+
}
57+
58+
@Test(expected = IllegalArgumentException.class)
59+
public void testBadEncodeValue() {
60+
new Codec("test", "test", ".XV...");
61+
}
62+
63+
@Test(expected = IllegalArgumentException.class)
64+
public void testBadCodecValue() {
65+
new Codec("test", "test", "..X...");
66+
}
67+
68+
@Test(expected = IllegalArgumentException.class)
69+
public void testBadIntraFrameOnlyValue() {
70+
new Codec("test", "test", "..VX..");
71+
}
72+
73+
@Test(expected = IllegalArgumentException.class)
74+
public void testBadLossyValue() {
75+
new Codec("test", "test", "..V.X.");
76+
}
77+
78+
@Test(expected = IllegalArgumentException.class)
79+
public void testBadLosslessValue() {
80+
new Codec("test", "test", "..V..X");
81+
}
82+
}

0 commit comments

Comments
 (0)