The "GR-PEACH_Audio_Playback_7InchLCD_Sample" is a sample code that can provides high-resolution audio playback of FLAC format files. It also allows the user to audio-playback control functions such as play, pause, and stop by manipulating key switches.

Dependencies:   GR-PEACH_video R_BSP TLV320_RBSP USBHost_custom

Fork of GR-PEACH_Audio_Playback_Sample by Renesas

Note

For a sample program of without LCD Board, please refer to GR-PEACH_Audio_Playback_Sample.

Introduction

The "GR-PEACH_Audio_Playback_7InchLCD_Sample" is a sample code that can provides high-resolution audio playback of FLAC format files. It also allows the user to audio-playback control functions such as play, pause, and stop by manipulating key switches.

1. Overview of the Sample Code

1.1 Software Block Diagram

Figure 1.1 shows the software block diagram.

/media/uploads/1050186/lcd_figure1_1.png

1.2 Pin Definitions

Table 1.1 shows the pins used in this sample code.

/media/uploads/1050186/lcd_table1_1.png

2. Sample Code Operating Environment

In order to operate this sample code, GR-PEACH, Audio Camera Shield and 7.1 inch LCD Shield must be needed. For details on Audio Camera Shield and 7.1 inch LCD Shield, please refer to the following links, respectively:

In this section, it is described that how board is configured and to control audio playback via command line and touch screen.

2.1 Operating Environment

Figure 2.1 shows the overview of the operating environment for this sample code.

/media/uploads/1050186/lcd_figure2_1.png

Figure 2.2 and 2.3 show how to configure GR-PEACH, Audio Camera Shield and 7.1 inch LCD shield when using USB0 and USB1, respectively.

/media/uploads/1050186/lcd_figure2_2.png /media/uploads/1050186/lcd_figure2_3.png

Table 2.1 lists the overview of Graphical User Interface (GUI) of this sample code.

/media/uploads/1050186/lcd_table2_1.png

2.2 List of User Operations

Table 2.2 shows the relationship among Audio Playback, Command Line and Onboard Switch.

/media/uploads/1050186/lcd_table2_2.png

3. Function Outline

Table 3.1, 3.2 and 3.3 shows the overview of functions implemented in this sample code.

/media/uploads/1050186/lcd_table3_1.png /media/uploads/1050186/lcd_table3_2.png /media/uploads/1050186/lcd_table3_3.png /media/uploads/1050186/lcd_figure3_1.png

3.1 Playback Control

This sample program supports the operation "play", "pause", "stop", "play next song" and "play previous song".

3.2 Trick Play Control

In order to enable/disable Repeat Mode, user need to type "repeat" on command line or click the corresponding icon shown in Table 2.2. By derault, Repeat Mode is enabled. When Repeat Mode is enabled, the first song is played back after the playback of the last song is finished. Otherwise, the playback is shopped when finishing to play back the last song.

3.3 How to see Song Information

The information of the song being played back can be seen by typing playinfo on command line. Table 3.4 lists the items user can see on the terminal.

/media/uploads/dkato/audioplayback_table3_4.png

3.4 How to analyze the folder structure in USB stick

In this sample code, the folder structure in USB stick is analyzed in the breadth-first order. Table 3.5 shows how the files in USB stick are numbered.

/media/uploads/dkato/audioplayback_table3_5.png

4.Others

4.1 Serial Communication Setting

With respect to the default serial communication related setting on mbed, please refer to the follwing link:
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication
Please set up the terminal software you would like to use on your PC in consideration of the above. For example, 9600 should be specified for the baud rate on the terminal in order to control this sample via command line.

4.2 Necessary modification when using GCC ARM Embedded

If you would like to use GCC ARM Embedded, you must revise the following linker script incorporated in mbed OS 5 package as follows:

  • Linker Script to be modified
    $(PROJECT_ROOT)/mbed-os/targets/TARGET_RENESAS/TARGET_RZ_A1H/device/TOOLCHAIN_GCC_ARM/RZA1H.ld

    Please note that $(PROJECT_ROOT) in the above denotes the root directory of this sample code

  • Before Modification

RZA1H.ld

/* Linker script for mbed RZ_A1H */

/* Linker script to configure memory regions. */
MEMORY
{
  ROM   (rx)  : ORIGIN = 0x00000000, LENGTH = 0x02000000
  BOOT_LOADER (rx) : ORIGIN = 0x18000000, LENGTH = 0x00004000 
  SFLASH (rx) : ORIGIN = 0x18004000, LENGTH = 0x07FFC000 
  L_TTB (rw)  : ORIGIN = 0x20000000, LENGTH = 0x00004000 
  RAM (rwx) : ORIGIN = 0x20020000, LENGTH = 0x00700000
  RAM_NC (rwx) : ORIGIN = 0x20900000, LENGTH = 0x00100000
}
(snip)
  • After Modification

RZA1H.ld

/* Linker script for mbed RZ_A1H */

/* Linker script to configure memory regions. */
MEMORY
{
  ROM   (rx)  : ORIGIN = 0x00000000, LENGTH = 0x02000000
  BOOT_LOADER (rx) : ORIGIN = 0x18000000, LENGTH = 0x00004000 
  SFLASH (rx) : ORIGIN = 0x18004000, LENGTH = 0x07FFC000 
  L_TTB (rw)  : ORIGIN = 0x20000000, LENGTH = 0x00004000 
  RAM (rwx) : ORIGIN = 0x20020000, LENGTH = 0x00180000
  RAM_NC (rwx) : ORIGIN = 0x20200000, LENGTH = 0x00680000
}
(snip)
Committer:
dkato
Date:
Fri Oct 16 04:28:07 2015 +0000
Revision:
0:ee40da884cfc
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:ee40da884cfc 1 /* libFLAC - Free Lossless Audio Codec library
dkato 0:ee40da884cfc 2 * Copyright (C) 2000-2009 Josh Coalson
dkato 0:ee40da884cfc 3 * Copyright (C) 2011-2014 Xiph.Org Foundation
dkato 0:ee40da884cfc 4 *
dkato 0:ee40da884cfc 5 * Redistribution and use in source and binary forms, with or without
dkato 0:ee40da884cfc 6 * modification, are permitted provided that the following conditions
dkato 0:ee40da884cfc 7 * are met:
dkato 0:ee40da884cfc 8 *
dkato 0:ee40da884cfc 9 * - Redistributions of source code must retain the above copyright
dkato 0:ee40da884cfc 10 * notice, this list of conditions and the following disclaimer.
dkato 0:ee40da884cfc 11 *
dkato 0:ee40da884cfc 12 * - Redistributions in binary form must reproduce the above copyright
dkato 0:ee40da884cfc 13 * notice, this list of conditions and the following disclaimer in the
dkato 0:ee40da884cfc 14 * documentation and/or other materials provided with the distribution.
dkato 0:ee40da884cfc 15 *
dkato 0:ee40da884cfc 16 * - Neither the name of the Xiph.org Foundation nor the names of its
dkato 0:ee40da884cfc 17 * contributors may be used to endorse or promote products derived from
dkato 0:ee40da884cfc 18 * this software without specific prior written permission.
dkato 0:ee40da884cfc 19 *
dkato 0:ee40da884cfc 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
dkato 0:ee40da884cfc 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
dkato 0:ee40da884cfc 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
dkato 0:ee40da884cfc 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
dkato 0:ee40da884cfc 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dkato 0:ee40da884cfc 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
dkato 0:ee40da884cfc 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
dkato 0:ee40da884cfc 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
dkato 0:ee40da884cfc 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
dkato 0:ee40da884cfc 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
dkato 0:ee40da884cfc 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dkato 0:ee40da884cfc 31 */
dkato 0:ee40da884cfc 32
dkato 0:ee40da884cfc 33 #ifdef HAVE_CONFIG_H
dkato 0:ee40da884cfc 34 # include <config.h>
dkato 0:ee40da884cfc 35 #endif
dkato 0:ee40da884cfc 36
dkato 0:ee40da884cfc 37 #include <math.h>
dkato 0:ee40da884cfc 38 #include <string.h>
dkato 0:ee40da884cfc 39 #include "share/compat.h"
dkato 0:ee40da884cfc 40 #include "private/bitmath.h"
dkato 0:ee40da884cfc 41 #include "private/fixed.h"
dkato 0:ee40da884cfc 42 #include "private/macros.h"
dkato 0:ee40da884cfc 43 #include "FLAC/assert.h"
dkato 0:ee40da884cfc 44
dkato 0:ee40da884cfc 45 #ifdef local_abs
dkato 0:ee40da884cfc 46 #undef local_abs
dkato 0:ee40da884cfc 47 #endif
dkato 0:ee40da884cfc 48 #define local_abs(x) ((unsigned)((x)<0? -(x) : (x)))
dkato 0:ee40da884cfc 49
dkato 0:ee40da884cfc 50 #ifdef FLAC__INTEGER_ONLY_LIBRARY
dkato 0:ee40da884cfc 51 /* rbps stands for residual bits per sample
dkato 0:ee40da884cfc 52 *
dkato 0:ee40da884cfc 53 * (ln(2) * err)
dkato 0:ee40da884cfc 54 * rbps = log (-----------)
dkato 0:ee40da884cfc 55 * 2 ( n )
dkato 0:ee40da884cfc 56 */
dkato 0:ee40da884cfc 57 static FLAC__fixedpoint local__compute_rbps_integerized(FLAC__uint32 err, FLAC__uint32 n)
dkato 0:ee40da884cfc 58 {
dkato 0:ee40da884cfc 59 FLAC__uint32 rbps;
dkato 0:ee40da884cfc 60 unsigned bits; /* the number of bits required to represent a number */
dkato 0:ee40da884cfc 61 int fracbits; /* the number of bits of rbps that comprise the fractional part */
dkato 0:ee40da884cfc 62
dkato 0:ee40da884cfc 63 FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
dkato 0:ee40da884cfc 64 FLAC__ASSERT(err > 0);
dkato 0:ee40da884cfc 65 FLAC__ASSERT(n > 0);
dkato 0:ee40da884cfc 66
dkato 0:ee40da884cfc 67 FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
dkato 0:ee40da884cfc 68 if(err <= n)
dkato 0:ee40da884cfc 69 return 0;
dkato 0:ee40da884cfc 70 /*
dkato 0:ee40da884cfc 71 * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1.
dkato 0:ee40da884cfc 72 * These allow us later to know we won't lose too much precision in the
dkato 0:ee40da884cfc 73 * fixed-point division (err<<fracbits)/n.
dkato 0:ee40da884cfc 74 */
dkato 0:ee40da884cfc 75
dkato 0:ee40da884cfc 76 fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2(err)+1);
dkato 0:ee40da884cfc 77
dkato 0:ee40da884cfc 78 err <<= fracbits;
dkato 0:ee40da884cfc 79 err /= n;
dkato 0:ee40da884cfc 80 /* err now holds err/n with fracbits fractional bits */
dkato 0:ee40da884cfc 81
dkato 0:ee40da884cfc 82 /*
dkato 0:ee40da884cfc 83 * Whittle err down to 16 bits max. 16 significant bits is enough for
dkato 0:ee40da884cfc 84 * our purposes.
dkato 0:ee40da884cfc 85 */
dkato 0:ee40da884cfc 86 FLAC__ASSERT(err > 0);
dkato 0:ee40da884cfc 87 bits = FLAC__bitmath_ilog2(err)+1;
dkato 0:ee40da884cfc 88 if(bits > 16) {
dkato 0:ee40da884cfc 89 err >>= (bits-16);
dkato 0:ee40da884cfc 90 fracbits -= (bits-16);
dkato 0:ee40da884cfc 91 }
dkato 0:ee40da884cfc 92 rbps = (FLAC__uint32)err;
dkato 0:ee40da884cfc 93
dkato 0:ee40da884cfc 94 /* Multiply by fixed-point version of ln(2), with 16 fractional bits */
dkato 0:ee40da884cfc 95 rbps *= FLAC__FP_LN2;
dkato 0:ee40da884cfc 96 fracbits += 16;
dkato 0:ee40da884cfc 97 FLAC__ASSERT(fracbits >= 0);
dkato 0:ee40da884cfc 98
dkato 0:ee40da884cfc 99 /* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */
dkato 0:ee40da884cfc 100 {
dkato 0:ee40da884cfc 101 const int f = fracbits & 3;
dkato 0:ee40da884cfc 102 if(f) {
dkato 0:ee40da884cfc 103 rbps >>= f;
dkato 0:ee40da884cfc 104 fracbits -= f;
dkato 0:ee40da884cfc 105 }
dkato 0:ee40da884cfc 106 }
dkato 0:ee40da884cfc 107
dkato 0:ee40da884cfc 108 rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1));
dkato 0:ee40da884cfc 109
dkato 0:ee40da884cfc 110 if(rbps == 0)
dkato 0:ee40da884cfc 111 return 0;
dkato 0:ee40da884cfc 112
dkato 0:ee40da884cfc 113 /*
dkato 0:ee40da884cfc 114 * The return value must have 16 fractional bits. Since the whole part
dkato 0:ee40da884cfc 115 * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits
dkato 0:ee40da884cfc 116 * must be >= -3, these assertion allows us to be able to shift rbps
dkato 0:ee40da884cfc 117 * left if necessary to get 16 fracbits without losing any bits of the
dkato 0:ee40da884cfc 118 * whole part of rbps.
dkato 0:ee40da884cfc 119 *
dkato 0:ee40da884cfc 120 * There is a slight chance due to accumulated error that the whole part
dkato 0:ee40da884cfc 121 * will require 6 bits, so we use 6 in the assertion. Really though as
dkato 0:ee40da884cfc 122 * long as it fits in 13 bits (32 - (16 - (-3))) we are fine.
dkato 0:ee40da884cfc 123 */
dkato 0:ee40da884cfc 124 FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
dkato 0:ee40da884cfc 125 FLAC__ASSERT(fracbits >= -3);
dkato 0:ee40da884cfc 126
dkato 0:ee40da884cfc 127 /* now shift the decimal point into place */
dkato 0:ee40da884cfc 128 if(fracbits < 16)
dkato 0:ee40da884cfc 129 return rbps << (16-fracbits);
dkato 0:ee40da884cfc 130 else if(fracbits > 16)
dkato 0:ee40da884cfc 131 return rbps >> (fracbits-16);
dkato 0:ee40da884cfc 132 else
dkato 0:ee40da884cfc 133 return rbps;
dkato 0:ee40da884cfc 134 }
dkato 0:ee40da884cfc 135
dkato 0:ee40da884cfc 136 static FLAC__fixedpoint local__compute_rbps_wide_integerized(FLAC__uint64 err, FLAC__uint32 n)
dkato 0:ee40da884cfc 137 {
dkato 0:ee40da884cfc 138 FLAC__uint32 rbps;
dkato 0:ee40da884cfc 139 unsigned bits; /* the number of bits required to represent a number */
dkato 0:ee40da884cfc 140 int fracbits; /* the number of bits of rbps that comprise the fractional part */
dkato 0:ee40da884cfc 141
dkato 0:ee40da884cfc 142 FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
dkato 0:ee40da884cfc 143 FLAC__ASSERT(err > 0);
dkato 0:ee40da884cfc 144 FLAC__ASSERT(n > 0);
dkato 0:ee40da884cfc 145
dkato 0:ee40da884cfc 146 FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
dkato 0:ee40da884cfc 147 if(err <= n)
dkato 0:ee40da884cfc 148 return 0;
dkato 0:ee40da884cfc 149 /*
dkato 0:ee40da884cfc 150 * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1.
dkato 0:ee40da884cfc 151 * These allow us later to know we won't lose too much precision in the
dkato 0:ee40da884cfc 152 * fixed-point division (err<<fracbits)/n.
dkato 0:ee40da884cfc 153 */
dkato 0:ee40da884cfc 154
dkato 0:ee40da884cfc 155 fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2_wide(err)+1);
dkato 0:ee40da884cfc 156
dkato 0:ee40da884cfc 157 err <<= fracbits;
dkato 0:ee40da884cfc 158 err /= n;
dkato 0:ee40da884cfc 159 /* err now holds err/n with fracbits fractional bits */
dkato 0:ee40da884cfc 160
dkato 0:ee40da884cfc 161 /*
dkato 0:ee40da884cfc 162 * Whittle err down to 16 bits max. 16 significant bits is enough for
dkato 0:ee40da884cfc 163 * our purposes.
dkato 0:ee40da884cfc 164 */
dkato 0:ee40da884cfc 165 FLAC__ASSERT(err > 0);
dkato 0:ee40da884cfc 166 bits = FLAC__bitmath_ilog2_wide(err)+1;
dkato 0:ee40da884cfc 167 if(bits > 16) {
dkato 0:ee40da884cfc 168 err >>= (bits-16);
dkato 0:ee40da884cfc 169 fracbits -= (bits-16);
dkato 0:ee40da884cfc 170 }
dkato 0:ee40da884cfc 171 rbps = (FLAC__uint32)err;
dkato 0:ee40da884cfc 172
dkato 0:ee40da884cfc 173 /* Multiply by fixed-point version of ln(2), with 16 fractional bits */
dkato 0:ee40da884cfc 174 rbps *= FLAC__FP_LN2;
dkato 0:ee40da884cfc 175 fracbits += 16;
dkato 0:ee40da884cfc 176 FLAC__ASSERT(fracbits >= 0);
dkato 0:ee40da884cfc 177
dkato 0:ee40da884cfc 178 /* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */
dkato 0:ee40da884cfc 179 {
dkato 0:ee40da884cfc 180 const int f = fracbits & 3;
dkato 0:ee40da884cfc 181 if(f) {
dkato 0:ee40da884cfc 182 rbps >>= f;
dkato 0:ee40da884cfc 183 fracbits -= f;
dkato 0:ee40da884cfc 184 }
dkato 0:ee40da884cfc 185 }
dkato 0:ee40da884cfc 186
dkato 0:ee40da884cfc 187 rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1));
dkato 0:ee40da884cfc 188
dkato 0:ee40da884cfc 189 if(rbps == 0)
dkato 0:ee40da884cfc 190 return 0;
dkato 0:ee40da884cfc 191
dkato 0:ee40da884cfc 192 /*
dkato 0:ee40da884cfc 193 * The return value must have 16 fractional bits. Since the whole part
dkato 0:ee40da884cfc 194 * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits
dkato 0:ee40da884cfc 195 * must be >= -3, these assertion allows us to be able to shift rbps
dkato 0:ee40da884cfc 196 * left if necessary to get 16 fracbits without losing any bits of the
dkato 0:ee40da884cfc 197 * whole part of rbps.
dkato 0:ee40da884cfc 198 *
dkato 0:ee40da884cfc 199 * There is a slight chance due to accumulated error that the whole part
dkato 0:ee40da884cfc 200 * will require 6 bits, so we use 6 in the assertion. Really though as
dkato 0:ee40da884cfc 201 * long as it fits in 13 bits (32 - (16 - (-3))) we are fine.
dkato 0:ee40da884cfc 202 */
dkato 0:ee40da884cfc 203 FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
dkato 0:ee40da884cfc 204 FLAC__ASSERT(fracbits >= -3);
dkato 0:ee40da884cfc 205
dkato 0:ee40da884cfc 206 /* now shift the decimal point into place */
dkato 0:ee40da884cfc 207 if(fracbits < 16)
dkato 0:ee40da884cfc 208 return rbps << (16-fracbits);
dkato 0:ee40da884cfc 209 else if(fracbits > 16)
dkato 0:ee40da884cfc 210 return rbps >> (fracbits-16);
dkato 0:ee40da884cfc 211 else
dkato 0:ee40da884cfc 212 return rbps;
dkato 0:ee40da884cfc 213 }
dkato 0:ee40da884cfc 214 #endif
dkato 0:ee40da884cfc 215
dkato 0:ee40da884cfc 216 #ifndef FLAC__INTEGER_ONLY_LIBRARY
dkato 0:ee40da884cfc 217 unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
dkato 0:ee40da884cfc 218 #else
dkato 0:ee40da884cfc 219 unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
dkato 0:ee40da884cfc 220 #endif
dkato 0:ee40da884cfc 221 {
dkato 0:ee40da884cfc 222 FLAC__int32 last_error_0 = data[-1];
dkato 0:ee40da884cfc 223 FLAC__int32 last_error_1 = data[-1] - data[-2];
dkato 0:ee40da884cfc 224 FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
dkato 0:ee40da884cfc 225 FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
dkato 0:ee40da884cfc 226 FLAC__int32 error, save;
dkato 0:ee40da884cfc 227 FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
dkato 0:ee40da884cfc 228 unsigned i, order;
dkato 0:ee40da884cfc 229
dkato 0:ee40da884cfc 230 for(i = 0; i < data_len; i++) {
dkato 0:ee40da884cfc 231 error = data[i] ; total_error_0 += local_abs(error); save = error;
dkato 0:ee40da884cfc 232 error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
dkato 0:ee40da884cfc 233 error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
dkato 0:ee40da884cfc 234 error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
dkato 0:ee40da884cfc 235 error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
dkato 0:ee40da884cfc 236 }
dkato 0:ee40da884cfc 237
dkato 0:ee40da884cfc 238 if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
dkato 0:ee40da884cfc 239 order = 0;
dkato 0:ee40da884cfc 240 else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
dkato 0:ee40da884cfc 241 order = 1;
dkato 0:ee40da884cfc 242 else if(total_error_2 < flac_min(total_error_3, total_error_4))
dkato 0:ee40da884cfc 243 order = 2;
dkato 0:ee40da884cfc 244 else if(total_error_3 < total_error_4)
dkato 0:ee40da884cfc 245 order = 3;
dkato 0:ee40da884cfc 246 else
dkato 0:ee40da884cfc 247 order = 4;
dkato 0:ee40da884cfc 248
dkato 0:ee40da884cfc 249 /* Estimate the expected number of bits per residual signal sample. */
dkato 0:ee40da884cfc 250 /* 'total_error*' is linearly related to the variance of the residual */
dkato 0:ee40da884cfc 251 /* signal, so we use it directly to compute E(|x|) */
dkato 0:ee40da884cfc 252 FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
dkato 0:ee40da884cfc 253 FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
dkato 0:ee40da884cfc 254 FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
dkato 0:ee40da884cfc 255 FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
dkato 0:ee40da884cfc 256 FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
dkato 0:ee40da884cfc 257 #ifndef FLAC__INTEGER_ONLY_LIBRARY
dkato 0:ee40da884cfc 258 residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 259 residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 260 residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 261 residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 262 residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 263 #else
dkato 0:ee40da884cfc 264 residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_integerized(total_error_0, data_len) : 0;
dkato 0:ee40da884cfc 265 residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_integerized(total_error_1, data_len) : 0;
dkato 0:ee40da884cfc 266 residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_integerized(total_error_2, data_len) : 0;
dkato 0:ee40da884cfc 267 residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_integerized(total_error_3, data_len) : 0;
dkato 0:ee40da884cfc 268 residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_integerized(total_error_4, data_len) : 0;
dkato 0:ee40da884cfc 269 #endif
dkato 0:ee40da884cfc 270
dkato 0:ee40da884cfc 271 return order;
dkato 0:ee40da884cfc 272 }
dkato 0:ee40da884cfc 273
dkato 0:ee40da884cfc 274 #ifndef FLAC__INTEGER_ONLY_LIBRARY
dkato 0:ee40da884cfc 275 unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
dkato 0:ee40da884cfc 276 #else
dkato 0:ee40da884cfc 277 unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
dkato 0:ee40da884cfc 278 #endif
dkato 0:ee40da884cfc 279 {
dkato 0:ee40da884cfc 280 FLAC__int32 last_error_0 = data[-1];
dkato 0:ee40da884cfc 281 FLAC__int32 last_error_1 = data[-1] - data[-2];
dkato 0:ee40da884cfc 282 FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
dkato 0:ee40da884cfc 283 FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
dkato 0:ee40da884cfc 284 FLAC__int32 error, save;
dkato 0:ee40da884cfc 285 /* total_error_* are 64-bits to avoid overflow when encoding
dkato 0:ee40da884cfc 286 * erratic signals when the bits-per-sample and blocksize are
dkato 0:ee40da884cfc 287 * large.
dkato 0:ee40da884cfc 288 */
dkato 0:ee40da884cfc 289 FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
dkato 0:ee40da884cfc 290 unsigned i, order;
dkato 0:ee40da884cfc 291
dkato 0:ee40da884cfc 292 for(i = 0; i < data_len; i++) {
dkato 0:ee40da884cfc 293 error = data[i] ; total_error_0 += local_abs(error); save = error;
dkato 0:ee40da884cfc 294 error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
dkato 0:ee40da884cfc 295 error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
dkato 0:ee40da884cfc 296 error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
dkato 0:ee40da884cfc 297 error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
dkato 0:ee40da884cfc 298 }
dkato 0:ee40da884cfc 299
dkato 0:ee40da884cfc 300 if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
dkato 0:ee40da884cfc 301 order = 0;
dkato 0:ee40da884cfc 302 else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
dkato 0:ee40da884cfc 303 order = 1;
dkato 0:ee40da884cfc 304 else if(total_error_2 < flac_min(total_error_3, total_error_4))
dkato 0:ee40da884cfc 305 order = 2;
dkato 0:ee40da884cfc 306 else if(total_error_3 < total_error_4)
dkato 0:ee40da884cfc 307 order = 3;
dkato 0:ee40da884cfc 308 else
dkato 0:ee40da884cfc 309 order = 4;
dkato 0:ee40da884cfc 310
dkato 0:ee40da884cfc 311 /* Estimate the expected number of bits per residual signal sample. */
dkato 0:ee40da884cfc 312 /* 'total_error*' is linearly related to the variance of the residual */
dkato 0:ee40da884cfc 313 /* signal, so we use it directly to compute E(|x|) */
dkato 0:ee40da884cfc 314 FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
dkato 0:ee40da884cfc 315 FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
dkato 0:ee40da884cfc 316 FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
dkato 0:ee40da884cfc 317 FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
dkato 0:ee40da884cfc 318 FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
dkato 0:ee40da884cfc 319 #ifndef FLAC__INTEGER_ONLY_LIBRARY
dkato 0:ee40da884cfc 320 residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 321 residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 322 residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 323 residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 324 residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
dkato 0:ee40da884cfc 325 #else
dkato 0:ee40da884cfc 326 residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_wide_integerized(total_error_0, data_len) : 0;
dkato 0:ee40da884cfc 327 residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_wide_integerized(total_error_1, data_len) : 0;
dkato 0:ee40da884cfc 328 residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_wide_integerized(total_error_2, data_len) : 0;
dkato 0:ee40da884cfc 329 residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_wide_integerized(total_error_3, data_len) : 0;
dkato 0:ee40da884cfc 330 residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_wide_integerized(total_error_4, data_len) : 0;
dkato 0:ee40da884cfc 331 #endif
dkato 0:ee40da884cfc 332
dkato 0:ee40da884cfc 333 return order;
dkato 0:ee40da884cfc 334 }
dkato 0:ee40da884cfc 335
dkato 0:ee40da884cfc 336 void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
dkato 0:ee40da884cfc 337 {
dkato 0:ee40da884cfc 338 const int idata_len = (int)data_len;
dkato 0:ee40da884cfc 339 int i;
dkato 0:ee40da884cfc 340
dkato 0:ee40da884cfc 341 switch(order) {
dkato 0:ee40da884cfc 342 case 0:
dkato 0:ee40da884cfc 343 FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
dkato 0:ee40da884cfc 344 memcpy(residual, data, sizeof(residual[0])*data_len);
dkato 0:ee40da884cfc 345 break;
dkato 0:ee40da884cfc 346 case 1:
dkato 0:ee40da884cfc 347 for(i = 0; i < idata_len; i++)
dkato 0:ee40da884cfc 348 residual[i] = data[i] - data[i-1];
dkato 0:ee40da884cfc 349 break;
dkato 0:ee40da884cfc 350 case 2:
dkato 0:ee40da884cfc 351 for(i = 0; i < idata_len; i++)
dkato 0:ee40da884cfc 352 #if 1 /* OPT: may be faster with some compilers on some systems */
dkato 0:ee40da884cfc 353 residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
dkato 0:ee40da884cfc 354 #else
dkato 0:ee40da884cfc 355 residual[i] = data[i] - 2*data[i-1] + data[i-2];
dkato 0:ee40da884cfc 356 #endif
dkato 0:ee40da884cfc 357 break;
dkato 0:ee40da884cfc 358 case 3:
dkato 0:ee40da884cfc 359 for(i = 0; i < idata_len; i++)
dkato 0:ee40da884cfc 360 #if 1 /* OPT: may be faster with some compilers on some systems */
dkato 0:ee40da884cfc 361 residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
dkato 0:ee40da884cfc 362 #else
dkato 0:ee40da884cfc 363 residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
dkato 0:ee40da884cfc 364 #endif
dkato 0:ee40da884cfc 365 break;
dkato 0:ee40da884cfc 366 case 4:
dkato 0:ee40da884cfc 367 for(i = 0; i < idata_len; i++)
dkato 0:ee40da884cfc 368 #if 1 /* OPT: may be faster with some compilers on some systems */
dkato 0:ee40da884cfc 369 residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
dkato 0:ee40da884cfc 370 #else
dkato 0:ee40da884cfc 371 residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
dkato 0:ee40da884cfc 372 #endif
dkato 0:ee40da884cfc 373 break;
dkato 0:ee40da884cfc 374 default:
dkato 0:ee40da884cfc 375 FLAC__ASSERT(0);
dkato 0:ee40da884cfc 376 }
dkato 0:ee40da884cfc 377 }
dkato 0:ee40da884cfc 378
dkato 0:ee40da884cfc 379 void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[])
dkato 0:ee40da884cfc 380 {
dkato 0:ee40da884cfc 381 int i, idata_len = (int)data_len;
dkato 0:ee40da884cfc 382
dkato 0:ee40da884cfc 383 switch(order) {
dkato 0:ee40da884cfc 384 case 0:
dkato 0:ee40da884cfc 385 FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
dkato 0:ee40da884cfc 386 memcpy(data, residual, sizeof(residual[0])*data_len);
dkato 0:ee40da884cfc 387 break;
dkato 0:ee40da884cfc 388 case 1:
dkato 0:ee40da884cfc 389 for(i = 0; i < idata_len; i++)
dkato 0:ee40da884cfc 390 data[i] = residual[i] + data[i-1];
dkato 0:ee40da884cfc 391 break;
dkato 0:ee40da884cfc 392 case 2:
dkato 0:ee40da884cfc 393 for(i = 0; i < idata_len; i++)
dkato 0:ee40da884cfc 394 #if 1 /* OPT: may be faster with some compilers on some systems */
dkato 0:ee40da884cfc 395 data[i] = residual[i] + (data[i-1]<<1) - data[i-2];
dkato 0:ee40da884cfc 396 #else
dkato 0:ee40da884cfc 397 data[i] = residual[i] + 2*data[i-1] - data[i-2];
dkato 0:ee40da884cfc 398 #endif
dkato 0:ee40da884cfc 399 break;
dkato 0:ee40da884cfc 400 case 3:
dkato 0:ee40da884cfc 401 for(i = 0; i < idata_len; i++)
dkato 0:ee40da884cfc 402 #if 1 /* OPT: may be faster with some compilers on some systems */
dkato 0:ee40da884cfc 403 data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3];
dkato 0:ee40da884cfc 404 #else
dkato 0:ee40da884cfc 405 data[i] = residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3];
dkato 0:ee40da884cfc 406 #endif
dkato 0:ee40da884cfc 407 break;
dkato 0:ee40da884cfc 408 case 4:
dkato 0:ee40da884cfc 409 for(i = 0; i < idata_len; i++)
dkato 0:ee40da884cfc 410 #if 1 /* OPT: may be faster with some compilers on some systems */
dkato 0:ee40da884cfc 411 data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4];
dkato 0:ee40da884cfc 412 #else
dkato 0:ee40da884cfc 413 data[i] = residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4];
dkato 0:ee40da884cfc 414 #endif
dkato 0:ee40da884cfc 415 break;
dkato 0:ee40da884cfc 416 default:
dkato 0:ee40da884cfc 417 FLAC__ASSERT(0);
dkato 0:ee40da884cfc 418 }
dkato 0:ee40da884cfc 419 }