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  public enum Type {
017    VIDEO,
018    AUDIO,
019    SUBTITLE,
020    DATA
021  }
022
023  final String name;
024  final String longName;
025
026  /** Can I decode with this codec */
027  final boolean canDecode;
028
029  /** Can I encode with this codec */
030  final boolean canEncode;
031
032  /** What type of codec is this */
033  final Type type;
034
035  /**
036   * @param name short codec name
037   * @param longName long codec name
038   * @param flags is expected to be in the following format:
039   *     <pre>
040   * D..... = Decoding supported
041   * .E.... = Encoding supported
042   * ..V... = Video codec
043   * ..A... = Audio codec
044   * ..S... = Subtitle codec
045   * ...I.. = Intra frame-only codec
046   * ....L. = Lossy compression
047   * .....S = Lossless compression
048   * </pre>
049   */
050  public Codec(String name, String longName, String flags) {
051    this.name = Preconditions.checkNotNull(name).trim();
052    this.longName = Preconditions.checkNotNull(longName).trim();
053
054    Preconditions.checkNotNull(flags);
055    Preconditions.checkArgument(flags.length() == 6, "Format flags is invalid '{}'", flags);
056    this.canDecode = flags.charAt(0) == 'D';
057    this.canEncode = flags.charAt(1) == 'E';
058
059    switch (flags.charAt(2)) {
060      case 'V':
061        this.type = Type.VIDEO;
062        break;
063      case 'A':
064        this.type = Type.AUDIO;
065        break;
066      case 'S':
067        this.type = Type.SUBTITLE;
068        break;
069      case 'D':
070        this.type = Type.DATA;
071        break;
072      default:
073        throw new IllegalArgumentException("Invalid codec type '" + flags.charAt(2) + "'");
074    }
075
076    // TODO There are more flags to parse
077  }
078
079  @Override
080  public String toString() {
081    return name + " " + longName;
082  }
083
084  @Override
085  public boolean equals(Object obj) {
086    return EqualsBuilder.reflectionEquals(this, obj);
087  }
088
089  @Override
090  public int hashCode() {
091    return HashCodeBuilder.reflectionHashCode(this);
092  }
093
094  public String getName() {
095    return name;
096  }
097
098  public String getLongName() {
099    return longName;
100  }
101
102  public boolean getCanDecode() {
103    return canDecode;
104  }
105
106  public boolean getCanEncode() {
107    return canEncode;
108  }
109
110  public Type getType() {
111    return type;
112  }
113}