コード例 #1
0
        public void iteration_loop(LameGlobalFlags gfp, float[][] pe, float[] ms_ener_ratio, III_psy_ratio[][] ratio)
        {
            var gfc     = gfp.internal_flags;
            var l3_xmin = new float[L3Side.SFBMAX];
            var xrpow   = new float[576];

            var targ_bits           = Arrays.ReturnRectangularArray <int>(2, 2);
            var max_frame_bits      = new int[1];
            var analog_silence_bits = new int[1];

            var l3_side = gfc.l3_side;

            var mean_bits = 0;

            quantize.calc_target_bits(gfp, pe, ms_ener_ratio, targ_bits, analog_silence_bits, max_frame_bits);

            /*
             * encode granules
             */
            for (var gr = 0; gr < gfc.mode_gr; gr++)
            {
                if (gfc.mode_ext == Encoder.MPG_MD_MS_LR)
                {
                    quantize.ms_convert(gfc.l3_side, gr);
                }

                for (var ch = 0; ch < gfc.channels_out; ch++)
                {
                    float adjust, masking_lower_db;
                    var   cod_info = l3_side.tt[gr][ch];

                    if (cod_info.block_type != Encoder.SHORT_TYPE)
                    {
                        // NORM, START or STOP type
                        adjust           = 0;
                        masking_lower_db = gfc.PSY.mask_adjust - adjust;
                    }
                    else
                    {
                        adjust           = 0;
                        masking_lower_db = gfc.PSY.mask_adjust_short - adjust;
                    }

                    gfc.masking_lower = (float)Math.Pow(10.0, masking_lower_db * 0.1);

                    /*
                     * cod_info, scalefac and xrpow get initialized in
                     * init_outer_loop
                     */
                    quantize.init_outer_loop(gfc, cod_info);
                    if (quantize.init_xrpow(gfc, cod_info, xrpow))
                    {
                        /*
                         * xr contains energy we will have to encode calculate the
                         * masking abilities find some good quantization in
                         * outer_loop
                         */
                        var ath_over = quantize.qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info, l3_xmin);
                        if (0 == ath_over) // analog silence
                        {
                            targ_bits[gr][ch] = analog_silence_bits[0];
                        }

                        quantize.outer_loop(gfp, cod_info, l3_xmin, xrpow, ch, targ_bits[gr][ch]);
                    }

                    quantize.iteration_finish_one(gfc, gr, ch);
                } // ch
            }     // gr

            /*
             * find a bitrate which can refill the resevoir to positive size.
             */
            for (gfc.bitrate_index = gfc.VBR_min_bitrate; gfc.bitrate_index <= gfc.VBR_max_bitrate; gfc.bitrate_index++)
            {
                var mb = new MeanBits(mean_bits);
                var rc = quantize.rv.ResvFrameBegin(gfp, mb);
                mean_bits = mb.bits;
                if (rc >= 0)
                {
                    break;
                }
            }

            Debug.Assert(gfc.bitrate_index <= gfc.VBR_max_bitrate);

            quantize.rv.ResvFrameEnd(gfc, mean_bits);
        }
コード例 #2
0
        public void iteration_loop(LameGlobalFlags gfp, float[][] pe, float[] ms_ener_ratio, III_psy_ratio[][] ratio)
        {
            var gfc = gfp.internal_flags;
            var l3_xmin = new float[L3Side.SFBMAX];
            var xrpow = new float[576];
            var targ_bits = new int[2];
            int mean_bits = 0, max_bits;

            var l3_side = gfc.l3_side;

            var mb = new MeanBits(mean_bits);

            quantize.rv.ResvFrameBegin(gfp, mb);
            mean_bits = mb.bits;

            /* quantize! */
            for (var gr = 0; gr < gfc.mode_gr; gr++)
            {
                /*
                 * calculate needed bits
                 */
                max_bits = quantize.qupvt.on_pe(gfp, pe, targ_bits, mean_bits, gr, gr);

                if (gfc.mode_ext == Encoder.MPG_MD_MS_LR)
                {
                    quantize.ms_convert(gfc.l3_side, gr);
                    quantize.qupvt.reduce_side(targ_bits, ms_ener_ratio[gr], mean_bits, max_bits);
                }

                for (var ch = 0; ch < gfc.channels_out; ch++)
                {
                    float adjust, masking_lower_db;
                    var   cod_info = l3_side.tt[gr][ch];

                    if (cod_info.block_type != Encoder.SHORT_TYPE)
                    {
                        // NORM, START or STOP type
                        adjust           = 0;
                        masking_lower_db = gfc.PSY.mask_adjust - adjust;
                    }
                    else
                    {
                        adjust           = 0;
                        masking_lower_db = gfc.PSY.mask_adjust_short - adjust;
                    }

                    gfc.masking_lower = (float)Math.Pow(10.0, masking_lower_db * 0.1);

                    /*
                     * init_outer_loop sets up cod_info, scalefac and xrpow
                     */
                    quantize.init_outer_loop(gfc, cod_info);
                    if (quantize.init_xrpow(gfc, cod_info, xrpow))
                    {
                        /*
                         * xr contains energy we will have to encode calculate the
                         * masking abilities find some good quantization in
                         * outer_loop
                         */
                        quantize.qupvt.calc_xmin(gfp, ratio[gr][ch], cod_info, l3_xmin);
                        quantize.outer_loop(gfp, cod_info, l3_xmin, xrpow, ch, targ_bits[ch]);
                    }

                    quantize.iteration_finish_one(gfc, gr, ch);
                    Debug.Assert(cod_info.part2_3_length <= LameInternalFlags.MAX_BITS_PER_CHANNEL);
                    Debug.Assert(cod_info.part2_3_length <= targ_bits[ch]);
                } // for ch
            }     // for gr

            quantize.rv.ResvFrameEnd(gfc, mean_bits);
        }
コード例 #3
0
        public void iteration_loop(LameGlobalFlags gfp, float[][] pe, float[] ms_ener_ratio, III_psy_ratio[][] ratio)
        {
            var gfc = gfp.internal_flags;

            var l3_xmin = Arrays.ReturnRectangularArray <float>(2, 2, L3Side.SFBMAX);

            var xrpow = new float[576];

            var bands     = Arrays.ReturnRectangularArray <int>(2, 2);
            var frameBits = new int[15];

            var min_bits  = Arrays.ReturnRectangularArray <int>(2, 2);
            var max_bits  = Arrays.ReturnRectangularArray <int>(2, 2);
            var mean_bits = 0;

            var l3_side = gfc.l3_side;

            var analog_silence = quantize.VBR_old_prepare(
                gfp,
                pe,
                ms_ener_ratio,
                ratio,
                l3_xmin,
                frameBits,
                min_bits,
                max_bits,
                bands);

            /*---------------------------------*/
            for (;;)
            {
                /*
                 * quantize granules with lowest possible number of bits
                 */
                var used_bits = 0;

                for (var gr = 0; gr < gfc.mode_gr; gr++)
                {
                    for (var ch = 0; ch < gfc.channels_out; ch++)
                    {
                        var cod_info = l3_side.tt[gr][ch];

                        /*
                         * init_outer_loop sets up cod_info, scalefac and xrpow
                         */
                        var ret = quantize.init_xrpow(gfc, cod_info, xrpow);
                        if (!ret || max_bits[gr][ch] == 0)
                        {
                            continue; // with next channel
                        }
                        quantize.VBR_encode_granule(
                            gfp,
                            cod_info,
                            l3_xmin[gr][ch],
                            xrpow,
                            ch,
                            min_bits[gr][ch],
                            max_bits[gr][ch]);

                        /*
                         * do the 'substep shaping'
                         */
                        if ((gfc.substep_shaping & 1) != 0)
                        {
                            quantize.trancate_smallspectrums(gfc, l3_side.tt[gr][ch], l3_xmin[gr][ch], xrpow);
                        }

                        var usedB = cod_info.part2_3_length + cod_info.part2_length;
                        used_bits += usedB;
                    } // for ch
                }

                /*
                 * find lowest bitrate able to hold used bits
                 */
                if (analog_silence != 0 && 0 == gfp.VBR_hard_min)
                {
                    gfc.bitrate_index = 1;
                }
                else
                {
                    gfc.bitrate_index = gfc.VBR_min_bitrate;
                }

                for (; gfc.bitrate_index < gfc.VBR_max_bitrate; gfc.bitrate_index++)
                {
                    if (used_bits <= frameBits[gfc.bitrate_index])
                    {
                        break;
                    }
                }

                var mb   = new MeanBits(mean_bits);
                var bits = quantize.rv.ResvFrameBegin(gfp, mb);
                mean_bits = mb.bits;

                if (used_bits <= bits)
                {
                    break;
                }

                quantize.bitpressure_strategy(gfc, l3_xmin, min_bits, max_bits);
            }
            /* breaks adjusted */
            /*--------------------------------------*/

            for (var gr = 0; gr < gfc.mode_gr; gr++)
            {
                for (var ch = 0; ch < gfc.channels_out; ch++)
                {
                    quantize.iteration_finish_one(gfc, gr, ch);
                }
            }

            quantize.rv.ResvFrameEnd(gfc, mean_bits);
        }