private void createCodec(VideoTrack vt) {
    Format fmt = vt.videoFormat;
    String enc = fmt.get(EncodingKey);
    if (enc.equals(ENCODING_AVI_MJPG) //
        || enc.equals(ENCODING_QUICKTIME_JPEG) //
    ) {
      vt.codec = new JPEGCodec();
    } else if (enc.equals(ENCODING_AVI_PNG) //
        || enc.equals(ENCODING_QUICKTIME_PNG) //
    ) {
      vt.codec = new PNGCodec();
    }

    vt.codec.setInputFormat(
        fmt.prepend(
            MediaTypeKey,
            MediaType.VIDEO,
            MimeTypeKey,
            MIME_JAVA,
            EncodingKey,
            ENCODING_BUFFERED_IMAGE,
            DataClassKey,
            BufferedImage.class));
    vt.codec.setOutputFormat(
        fmt.prepend(MediaTypeKey, MediaType.VIDEO, EncodingKey, enc, DataClassKey, byte[].class));
    //    vt.codec.setQuality(vt.videoQuality);
  }
示例#2
0
  /** Creates a buffered image of the specified depth with a random color palette. */
  private static BufferedImage createImage(Format format) {
    int depth = format.get(DepthKey);
    int width = format.get(WidthKey);
    int height = format.get(HeightKey);

    Random rnd = new Random(0); // use seed 0 to get reproducable output
    BufferedImage img;
    switch (depth) {
      case 24:
      default:
        {
          img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
          break;
        }
      case 8:
        {
          byte[] red = new byte[256];
          byte[] green = new byte[256];
          byte[] blue = new byte[256];
          for (int i = 0; i < 255; i++) {
            red[i] = (byte) rnd.nextInt(256);
            green[i] = (byte) rnd.nextInt(256);
            blue[i] = (byte) rnd.nextInt(256);
          }
          rnd.setSeed(0); // set back to 0 for reproducable output
          IndexColorModel palette = new IndexColorModel(8, 256, red, green, blue);
          img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, palette);
          break;
        }
      case 4:
        {
          byte[] red = new byte[16];
          byte[] green = new byte[16];
          byte[] blue = new byte[16];
          for (int i = 0; i < 15; i++) {
            red[i] = (byte) rnd.nextInt(16);
            green[i] = (byte) rnd.nextInt(16);
            blue[i] = (byte) rnd.nextInt(16);
          }
          rnd.setSeed(0); // set back to 0 for reproducable output
          IndexColorModel palette = new IndexColorModel(4, 16, red, green, blue);
          img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, palette);
          break;
        }
    }
    return img;
  }
  public int process(Buffer in, Buffer out) {
    out.setMetaTo(in);
    if (in.isFlag(DISCARD)) {
      return CODEC_OK;
    }
    out.format = outputFormat;

    SeekableByteArrayOutputStream tmp;
    if (out.data instanceof byte[]) {
      tmp = new SeekableByteArrayOutputStream((byte[]) out.data);
    } else {
      tmp = new SeekableByteArrayOutputStream();
    }
    Format vf = outputFormat;

    // Handle sub-image
    Rectangle r;
    int scanlineStride;
    if (in.data instanceof BufferedImage) {
      BufferedImage image = (BufferedImage) in.data;
      WritableRaster raster = image.getRaster();
      scanlineStride = raster.getSampleModel().getWidth();
      r = raster.getBounds();
      r.x -= raster.getSampleModelTranslateX();
      r.y -= raster.getSampleModelTranslateY();
    } else {
      r = new Rectangle(0, 0, vf.get(WidthKey), vf.get(HeightKey));
      scanlineStride = vf.get(WidthKey);
    }

    try {
      switch (vf.get(DepthKey)) {
        case 8:
          {
            writeKey8(
                tmp,
                getIndexed8(in),
                r.width,
                r.height,
                r.x + r.y * scanlineStride,
                scanlineStride);
            break;
          }
        case 16:
          {
            writeKey16(
                tmp, getRGB15(in), r.width, r.height, r.x + r.y * scanlineStride, scanlineStride);
            break;
          }
        case 24:
          {
            writeKey24(
                tmp, getRGB24(in), r.width, r.height, r.x + r.y * scanlineStride, scanlineStride);
            break;
          }
        case 32:
          {
            writeKey24(
                tmp, getARGB32(in), r.width, r.height, r.x + r.y * scanlineStride, scanlineStride);
            break;
          }
        default:
          {
            out.setFlag(DISCARD);
            return CODEC_FAILED;
          }
      }

      out.format = outputFormat;
      out.sampleCount = 1;
      out.setFlag(KEYFRAME);
      out.data = tmp.getBuffer();
      out.offset = 0;
      out.length = (int) tmp.getStreamPosition();
      return CODEC_OK;
    } catch (IOException ex) {
      ex.printStackTrace();
      out.setFlag(DISCARD);
      return CODEC_FAILED;
    }
  }