001package net.bramp.ffmpeg.info; 002 003import com.google.common.base.Preconditions; 004import com.google.errorprone.annotations.Immutable; 005import org.apache.commons.lang3.builder.EqualsBuilder; 006import org.apache.commons.lang3.builder.HashCodeBuilder; 007 008/** 009 * Information about supported Codecs 010 * 011 * @author bramp 012 */ 013@Immutable 014public class Codec { 015 016 enum Type { 017 VIDEO, 018 AUDIO, 019 SUBTITLE 020 } 021 022 final String name; 023 final String longName; 024 025 /** Can I decode with this codec */ 026 final boolean canDecode; 027 028 /** Can I encode with this codec */ 029 final boolean canEncode; 030 031 /** What type of codec is this */ 032 final Type type; 033 034 /** 035 * @param name short codec name 036 * @param longName long codec name 037 * @param flags is expected to be in the following format: 038 * <pre> 039 * D..... = Decoding supported 040 * .E.... = Encoding supported 041 * ..V... = Video codec 042 * ..A... = Audio codec 043 * ..S... = Subtitle codec 044 * ...S.. = Supports draw_horiz_band 045 * ....D. = Supports direct rendering method 1 046 * .....T = Supports weird frame truncation 047 * </pre> 048 */ 049 public Codec(String name, String longName, String flags) { 050 this.name = Preconditions.checkNotNull(name).trim(); 051 this.longName = Preconditions.checkNotNull(longName).trim(); 052 053 Preconditions.checkNotNull(flags); 054 Preconditions.checkArgument(flags.length() == 6, "Format flags is invalid '{}'", flags); 055 this.canDecode = flags.charAt(0) == 'D'; 056 this.canEncode = flags.charAt(1) == 'E'; 057 058 switch (flags.charAt(2)) { 059 case 'V': 060 this.type = Type.VIDEO; 061 break; 062 case 'A': 063 this.type = Type.AUDIO; 064 break; 065 case 'S': 066 this.type = Type.SUBTITLE; 067 break; 068 default: 069 throw new IllegalArgumentException("Invalid codec type '" + flags.charAt(3) + "'"); 070 } 071 072 // TODO There are more flags to parse 073 } 074 075 @Override 076 public String toString() { 077 return name + " " + longName; 078 } 079 080 @Override 081 public boolean equals(Object obj) { 082 return EqualsBuilder.reflectionEquals(this, obj); 083 } 084 085 @Override 086 public int hashCode() { 087 return HashCodeBuilder.reflectionHashCode(this); 088 } 089 090 public String getName() { 091 return name; 092 } 093 094 public String getLongName() { 095 return longName; 096 } 097 098 public boolean getCanDecode() { 099 return canDecode; 100 } 101 102 public boolean getCanEncode() { 103 return canEncode; 104 } 105 106 public Type getType() { 107 return type; 108 } 109}