コード例 #1
0
 public FlacSubframeInfo()
 {
     best = new FlacSubframe();
     lpc_ctx = new LpcContext[lpc.MAX_LPC_WINDOWS];
     for (int i = 0; i < lpc.MAX_LPC_WINDOWS; i++)
         lpc_ctx[i] = new LpcContext();
 }
コード例 #2
0
 public FlacFrame(int subframes_count)
 {
     subframes = new FlacSubframeInfo[subframes_count];
     for (int ch = 0; ch < subframes_count; ch++)
     {
         subframes[ch] = new FlacSubframeInfo();
     }
     current = new FlacSubframe();
 }
コード例 #3
0
 public FlacSubframeInfo()
 {
     best    = new FlacSubframe();
     lpc_ctx = new LpcContext[lpc.MAX_LPC_WINDOWS];
     for (int i = 0; i < lpc.MAX_LPC_WINDOWS; i++)
     {
         lpc_ctx[i] = new LpcContext();
     }
 }
コード例 #4
0
        public void ChooseBestSubframe(int ch)
        {
            if (current.size >= subframes[ch].best.size)
            {
                return;
            }
            FlacSubframe tmp = subframes[ch].best;

            subframes[ch].best = current;
            current            = tmp;
        }
コード例 #5
0
ファイル: FlakeWriter.cs プロジェクト: teekay/FLACTools
        unsafe void postprocess_coefs(FlacFrame frame, FlacSubframe sf, int ch)
        {
            if (eparams.development_mode < 0)
                return;
            if (sf.type != SubframeType.LPC || sf.order > 30)
                return;
            int orig_window = sf.window;
            int orig_order = sf.order;
            int orig_shift = sf.shift;
            int orig_cbits = sf.cbits;
            uint orig_size = sf.size;
            var orig_coefs = stackalloc int[orig_order];
            for (int i = 0; i < orig_order; i++) orig_coefs[i] = sf.coefs[i];
            int orig_xx = -1;
            int orig_seq = 0;
            int maxxx = Math.Min(good_x[orig_order].Length, eparams.development_mode);
            var pmax = get_max_p_order(eparams.max_partition_order, frame.blocksize, orig_order);
            var pmin = Math.Min(eparams.min_partition_order, pmax);
            ulong* sums = stackalloc ulong[(pmax + 1) * Flake.MAX_PARTITIONS];

            while (true)
            {
                var best_coefs = stackalloc int[orig_order];
                int best_shift = orig_shift;
                int best_cbits = orig_cbits;
                uint best_size = orig_size;
                int best_xx = -1;
                for (int xx = -1; xx < maxxx; xx++)
                {
                    int x = xx;
                    if (xx < 0)
                    {
                        if (orig_xx < 0 || maxxx < 1/*3*/)// || (orig_xx >> orig_order) != 0)
                            continue;
                        x = orig_xx;
                        orig_seq++;
                    }
                    else
                    {
                        orig_seq = 0;
                        if (orig_order < good_x.Length && good_x[orig_order] != null)
                            x = good_x[orig_order][xx];
                    }

                    frame.current.type = SubframeType.LPC;
                    frame.current.order = orig_order;
                    frame.current.window = orig_window;
                    frame.current.shift = orig_shift;
                    frame.current.cbits = orig_cbits;

                    if (((x >> orig_order) & 1) != 0)
                    {
                        frame.current.shift--;
                        frame.current.cbits--;
                        if (frame.current.shift < 0 || frame.current.cbits < 2)
                            continue;
                    }

                    ulong csum = 0;
                    int qmax = (1 << (frame.current.cbits - 1)) - 1;
                    for (int i = 0; i < frame.current.order; i++)
                    {
                        int shift = (x >> orig_order) & 1;
                        int increment = (x == 1 << orig_order) ? 0 : (((x >> i) & 1) << 1) - 1;
                        frame.current.coefs[i] = (orig_coefs[i] + (increment << orig_seq)) >> shift;
                        if (frame.current.coefs[i] < -(qmax + 1)) frame.current.coefs[i] = -(qmax + 1);
                        if (frame.current.coefs[i] > qmax) frame.current.coefs[i] = qmax;
                        csum += (ulong)Math.Abs(frame.current.coefs[i]);
                    }

                    fixed (int* coefs = frame.current.coefs)
                    {
                        if ((csum << frame.subframes[ch].obits) >= 1UL << 32)
                            lpc.encode_residual_long(frame.current.residual, frame.subframes[ch].samples, frame.blocksize, frame.current.order, coefs, frame.current.shift);
                        else
                            lpc.encode_residual(frame.current.residual, frame.subframes[ch].samples, frame.blocksize, frame.current.order, coefs, frame.current.shift);
                    }

                    var cur_size = calc_rice_params(frame.current.rc, pmin, pmax, frame.current.residual, (uint)frame.blocksize, (uint)frame.current.order, Settings.PCM.BitsPerSample);
                    frame.current.size = (uint)(frame.current.order * frame.subframes[ch].obits + 4 + 5 + frame.current.order * frame.current.cbits + 6 + (int)cur_size);

                    if (frame.current.size < best_size)
                    {
                        //var dif = best_size - frame.current.size;
                        for (int i = 0; i < frame.current.order; i++) best_coefs[i] = frame.current.coefs[i];
                        best_shift = frame.current.shift;
                        best_cbits = frame.current.cbits;
                        best_size = frame.current.size;
                        best_xx = x;
                        frame.ChooseBestSubframe(ch);
                        //if (dif > orig_order * 5)
                        //    break;
                    }

                    if (xx < 0 && best_size < orig_size)
                        break;
                }

                if (best_size < orig_size)
                {
                    //if (best_xx >= 0) best_x[order, best_xx]++;
                    //if (orig_size != 0x7FFFFFFF)
                    //    System.Console.Write(string.Format(" {0}[{1:x}]", orig_size - best_size, best_xx));
                    for (int i = 0; i < orig_order; i++) orig_coefs[i] = best_coefs[i];
                    orig_shift = best_shift;
                    orig_cbits = best_cbits;
                    orig_size = best_size;
                    orig_xx = best_xx;
                }
                else
                {
                    break;
                }
            }

            //if (orig_size != 0x7FFFFFFF)
            //    System.Console.WriteLine();

            //if (frame_count % 0x400 == 0)
            //{
            //    for (int o = 0; o < best_x.GetLength(0); o++)
            //    {
            //        //for (int x = 0; x <= (1 << o); x++)
            //        //    if (best_x[o, x] != 0)
            //        //        System.Console.WriteLine(string.Format("{0:x2}\t{1:x4}\t{2}", o, x, best_x[o, x]));
            //        var s = new List<KeyValuePair<int, int>>();
            //        for (int x = 0; x < (1 << o); x++)
            //            if (best_x[o, x] != 0)
            //                s.Add(new KeyValuePair<int, int>(x, best_x[o, x]));
            //        s.Sort((x, y) => y.Value.CompareTo(x.Value));
            //        foreach (var x in s)
            //            System.Console.WriteLine(string.Format("{0:x2}\t{1:x4}\t{2}", o, x.Key, x.Value));
            //        int i = 0;
            //        foreach (var x in s)
            //        {
            //            System.Console.Write(string.Format(o <= 8 ? "0x{0:x2}," : "0x{0:x3},", x.Key));
            //            if ((++i) % 16 == 0)
            //                System.Console.WriteLine();
            //        }
            //        System.Console.WriteLine();
            //    }
            //}
        }
コード例 #6
0
 public void ChooseBestSubframe(int ch)
 {
     if (current.size >= subframes[ch].best.size)
         return;
     FlacSubframe tmp = subframes[ch].best;
     subframes[ch].best = current;
     current = tmp;
 }
コード例 #7
0
 public FlacFrame(int subframes_count)
 {
     subframes = new FlacSubframeInfo[subframes_count];
     for (int ch = 0; ch < subframes_count; ch++)
         subframes[ch] = new FlacSubframeInfo();
     current = new FlacSubframe();
 }