/ src / library / av_processing / BitCounter.php
<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\library\av_processing;

/**
 * BitCounter counts bits without keeping them, standing in for a BitWriter
 * where only the length of the result matters. The encoder tries several step
 * sizes and keeps the smallest that fits. Only the kept one needs its bytes;
 * the tries only need to know how long they would have been. Handing the same
 * writing code one of these instead of a real writer gets the length without
 * the work of building the bytes, and without a second copy of the writing code
 * that could drift from the first.
 */
class BitCounter
{
    /**
     * written stores how many bits have been counted so far.
     * @var int
     */
    public $written = 0;
    /**
     * add counts how many bits a value would take without writing it. The
     * encoder weighs several ways of coding a frame, so it needs the
     * cost of each before choosing
     *
     * @param int $value the value that would be written
     * @param int $bits how many bits it would take
     */
    public function add($value, $bits)
    {
        $this->written += $bits;
    }
    /**
     * length says how many bits everything counted so far would take, so
     * the encoder can compare one way of coding a frame with another
     *
     * @return int how many bits
     */
    public function length()
    {
        return $this->written;
    }
    /**
     * finish ends the run, which for a counter has nothing to give back
     *
     * @return string an empty run of bytes
     */
    public function finish()
    {
        return "";
    }
}
X