// Allocates enough space in the metadata structure and writes the current
  // high word of the bitrate accumulator and the slow_level values to it. The
  // slow_level values are converted from 32-bit unsigned to our internal 16-bit
  // mylog2 values. Afterward, read_entropy_vars () is called to read the values
  // back because we must compensate for the loss through the log function and
  // the truncation of the bitrate.
  static void write_hybrid_profile(WavpackStream wps, WavpackMetadata wpmd) {
    byte[] byteptr;
    int byte_idx = 0;
    int temp;

    word_set_bitrate(wps);
    byteptr = wpmd.data = wpmd.temp_data;
    wpmd.id = Defines.ID_HYBRID_PROFILE;

    if ((wps.wphdr.flags & Defines.HYBRID_BITRATE) != 0) {
      temp = log2s((int) (wps.w.slow_level[0]));
      byteptr[byte_idx] = (byte) temp;
      byte_idx++;
      byteptr[byte_idx] = (byte) (temp >> 8);
      byte_idx++;

      if ((wps.wphdr.flags & (Defines.MONO_FLAG | Defines.FALSE_STEREO)) == 0) {
        temp = log2s((int) (wps.w.slow_level[1]));
        byteptr[byte_idx] = (byte) temp;
        byte_idx++;
        byteptr[byte_idx] = (byte) (temp >> 8);
        byte_idx++;
      }
    }

    temp = (int) (wps.w.bitrate_acc[0] >> 16);
    byteptr[byte_idx] = (byte) temp;
    byte_idx++;
    byteptr[byte_idx] = (byte) (temp >> 8);
    byte_idx++;

    if ((wps.wphdr.flags & (Defines.MONO_FLAG | Defines.FALSE_STEREO)) == 0) {
      temp = (int) (wps.w.bitrate_acc[1] >> 16);
      byteptr[byte_idx] = (byte) temp;
      byte_idx++;
      byteptr[byte_idx] = (byte) (temp >> 8);
      byte_idx++;
    }

    if ((wps.w.bitrate_delta[0] | wps.w.bitrate_delta[1]) != 0) {
      temp = log2s((int) (wps.w.bitrate_delta[0]));
      byteptr[byte_idx] = (byte) temp;
      byte_idx++;
      byteptr[byte_idx] = (byte) (temp >> 8);
      byte_idx++;

      if ((wps.wphdr.flags & (Defines.MONO_FLAG | Defines.FALSE_STEREO)) == 0) {
        temp = log2s((int) (wps.w.bitrate_delta[1]));
        byteptr[byte_idx] = (byte) temp;
        byte_idx++;
        byteptr[byte_idx] = (byte) (temp >> 8);
        byte_idx++;
      }
    }

    wpmd.byte_length = byte_idx;
    read_hybrid_profile(wps, wpmd);
  }
  // Allocates the correct space in the metadata structure and writes the
  // current median values to it. Values are converted from 32-bit unsigned
  // to our internal 16-bit mylog2 values, and read_entropy_vars () is called
  // to read the values back because we must compensate for the loss through
  // the log function.
  static void write_entropy_vars(WavpackStream wps, WavpackMetadata wpmd) {
    byte[] byteptr;
    int temp;
    int byte_idx = 0;

    byteptr = wpmd.data = wpmd.temp_data;
    wpmd.id = Defines.ID_ENTROPY_VARS;

    byteptr[byte_idx] = (byte) (temp = mylog2(wps.w.median[0][0]));
    byte_idx++;
    byteptr[byte_idx] = (byte) (temp >> 8);
    byte_idx++;
    byteptr[byte_idx] = (byte) (temp = mylog2(wps.w.median[1][0]));
    byte_idx++;
    byteptr[byte_idx] = (byte) (temp >> 8);
    byte_idx++;
    byteptr[byte_idx] = (byte) (temp = mylog2(wps.w.median[2][0]));
    byte_idx++;
    byteptr[byte_idx] = (byte) (temp >> 8);
    byte_idx++;

    if ((wps.wphdr.flags & (Defines.MONO_FLAG | Defines.FALSE_STEREO)) == 0) {
      byteptr[byte_idx] = (byte) (temp = mylog2(wps.w.median[0][1]));
      byte_idx++;
      byteptr[byte_idx] = (byte) (temp >> 8);
      byte_idx++;
      byteptr[byte_idx] = (byte) (temp = mylog2(wps.w.median[1][1]));
      byte_idx++;
      byteptr[byte_idx] = (byte) (temp >> 8);
      byte_idx++;
      byteptr[byte_idx] = (byte) (temp = mylog2(wps.w.median[2][1]));
      byte_idx++;
      byteptr[byte_idx] = (byte) (temp >> 8);
      byte_idx++;
    }

    wpmd.byte_length = byte_idx;

    read_entropy_vars(wps, wpmd);
  }