Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: TransPCM—use Float16/24 to reduce bit-depth, also promotes compression (Read 45340 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

When I first became aware of the Float16 type I found it interesting in terms of maybe having potential as a possible lossy storage type.

Attached is a processed version of the sample in this thread which has been decoded back to PCM. The processing is as follows (at this stage as there is no player that I am aware of):
Input file Sample [PCM] > FLOAT16 > [PCM] > Output File.

The conversion from integer to Float16 reduces the number of significant bits of the 24-bit sample to (a maximum of) 11. There is therefore an amount of added noise due to truncation. The added noise has been noise shaped using SG's adaptive noise shaping method as used in lossyWAV.

Original Sample

Lossy Float16 Processed Sample [edit] removed due to TransPCM executable / foobar2000 Float16 playback availability, 29th May 2012. [/edit]

If the 24-bit coded sample is encoded with FLAC (wFormatTag left as 0x0001 as FLAC does not handle Floating Point samples and wBitsPerSample is left as 24) there is a saving of in excess of 20% in filesize.

I don't see any practical advantage using this storage type for native 16-bit PCM.

Changelog:

TransPCM beta 0.1.3a 30/05/2013
  • Bug fix in Float24 handling (thanks Bryant!).
  • Bug fix in WAVE_FORMAT_EXTENSIBLE chunk handling (thanks again Bryant!).
[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--]TransPCM beta 0.1.2 02/06/2012
  • Modification to adaptive noise shaping for 44.1/48kHz input;
  • Amendment to tracer fingerprinting.
[/size]
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #1
This differs from simply reducing the bit depth?

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #2
Yes - in that values up to c. +/- 2^31 can still be stored (with an appropriate scaling factor). The granularity changes as sample absolute values get bigger. In this way, the original dynamic range can be preserved more effectively.

ABS(Input Sample) = 0 to 2047 : Output difference = 0;
ABS(Input Sample) = 2048 to 4095 = Output difference : 0 to -1;
ABS(Input Sample) = 4096 to 8191 = Output difference : 0 to -3;
ABS(Input Sample) = 8192 to 16383 = Output difference : 0 to -7;
ABS(Input Sample) = 16384 to 32767 = Output difference : 0 to -15;
ABS(Input Sample) = 32768 to 65535 = Output difference : 0 to -31;
etc.
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #3
Isn't this pretty much what ADPCM does?  Except I guess without the differential encoding.

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #4
You mean a-Law/µ-Law?

Makes me wonder how many bits suffice to code the range -32768...32767. IOW, let the section "ABS(Input Sample) = 16384 to 32767" be the outermost one. Is a float13 enough?

Chris
If I don't reply to your reply, it means I agree with you.

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #5
Attached is a processed version of the sample in this thread which has been decoded back to PCM. The processing is as follows (at this stage as there is no player that I am aware of):
Input file Sample [PCM] > FLOAT16 > [PCM] > Output File.

What you are describing is very similar to WavPack lossy. The difference is that you also want to add a decorrelation step before truncating to float16 to reduce the magnitude of the samples. This could be as simple as using the delta between samples, but it would be even better to have an adaptive filter to better handle some difficult cases.

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #6
The plan was to simply set wFormatTag= 0x0003 and wBitsPerSample =16. Any compliant player could play the WAV file without any decoding.
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #7
float16 could be very efficient on GPU hardware if that is relevant to you.

-k

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #8
The plan was to simply set wFormatTag= 0x0003 and wBitsPerSample =16. Any compliant player could play the WAV file without any decoding.


From experience, I doubt many media players will play that.

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #9
I understand that currently players are rather unlikely to accept a 16-bit floating point WAV file. However, the RIFF standard effectively allows the use of Float16 even if it wasn't a standard at the time that the RIFF standard was set.

Decoding Float16 values is trivial - I have written a basic decoder (no NaN, Infinity handling) to both Float64 and (scaled) Integer output in Delphi seperately in x86/x87 - not too many lines of code per sample.
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #10
Below is Delphi / IA32 & x87 code used to encode Double to Float16 and decode Float16 to Double (actually extended [80-bit] in this case). Hopefully someone will find it useful.

Compatibility at a player level would be a great starting point (i.e. simply playing a RIFF WAVE with wFormatTag=0x0003 and wBitsPerSample=0x0010).
[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--]
Code: [Select]
//==============================================================================
//
// Float16 encode / decode functions;
//
// 2011 Nick Currie
//
//==============================================================================

Type
  Float16 = Word;

  SingleRec = Packed Record
    Case Integer of
      1:(Single : Single);
      2:(Integer : Integer);
      3:(Words : Array[0..1] of Word);
      4:(BytePx : Byte; WordP1 : Word;);
      5:(Bytes : Array[0..3] of Byte);
    End;

  DoubleRec = Packed Record
    Case Integer of
      1:(Double : Double);
      2:(Int64 : Int64);
      3:(Integers : Array[0..1] of Integer);
      4:(Words : Array[0..3] of Word);
      5:(BytePx : Byte; WordP1,WordP3,WordP5 : Word;);
      6:(Bytes : Array[0..7] of Byte);
    End;

  ExtendedRec = Packed Record
    Case Integer of
      1:(Extended : Extended);
      2:(Int64 : Int64);
      3:(Integers : Array[0..1] of Integer);
      4:(Words : Array[0..4] of Word);
      5:(Bytes : Array[0..9] of Byte);
    End;

Var
  Powersof = Packed Record
two : array[-1024..1023] of Double;
  End;

  {$IFNDEF USEASM}
  //============================================================================
  Function F80_to_F16(F80_Value : Extended) : Float16;
  //============================================================================
  Var
    Exponent : Integer;
    Sign    : Word;
  Begin
  //============================================================================
    Sign:=ExtendedRec(F80_Value).Words[4] and $8000;

    If (F80_Value=0) then
      F80_to_F16:=Sign
    else
    Begin
      Exponent:=(ExtendedRec(F80_Value).Words[4] and $7FFF)-$3FFF;
      ExtendedRec(F80_Value).Words[4]:=ExtendedRec(F80_Value).Words[4] or $3FFF;

      if Exponent>15 then
        F80_to_F16:=Sign or $7C00
      else
        If (Exponent>-15) then
          F80_to_F16:=(Sign or ((Exponent+15) shl 10) or ((ExtendedRec(F80_Value).Words[3] and $7FFF) shr 5))
        else
          if Exponent>-25 then
            F80_to_F16:=(Sign or (ExtendedRec(F80_Value).Words[3] shr (-Exponent-9)))
          else
            F80_to_F16:=Sign;
    End;
  //============================================================================
  End;
  //============================================================================
  {$ELSE}
  //============================================================================
  Function F80_to_F16(F80_Value : Extended) : Float16; Assembler;
  //============================================================================
  asm
                push    ecx
                push    edx

                xor    eax,eax
                xor    edx,edx

                mov    cx,word ptr F80_Value+8
                mov    dh,ch
                and    dh,$80
                and    cx,$7FFF
                jz      @@EX

                sub    cx,$3FFF

                cmp    cx,-$19
                jle    @@EX

                mov    ah,$7C
                cmp    cx,$10
                jge    @@EX

      @@01:    mov    ax,word ptr F80_Value+6

                cmp    cx,-$0F
                jg      @@02

                neg    cl
                sub    cl,$09
                shr    eax,cl
                jmp    @@EX

      @@02:    add    cl,$0F
                shr    ax,$05
                shl    cl,$02
                and    ax,$03FF
                or      ah,cl

      @@EX:    or      ax,dx
                pop    edx
                pop    ecx
  End;
  //============================================================================
  {$ENDIF}

  {$IFNDEF USEASM}
  //============================================================================
  Function F64_to_F16(F64_Value : Double) : Float16;
  //============================================================================
  Var
    Exponent : Integer;
    Sign    : Word;
  Begin
  //============================================================================
    DoubleRec(F64_Value).Double:=F64_Value;
    Sign:=DoubleRec(F64_Value).Words[3] and $8000;

    If (F64_Value=0) then
      F64_to_F16:=Sign
    else
    Begin
      Exponent:=(((DoubleRec(F64_Value).Words[3] and $7FF0) shr 4)-$3FF);
      DoubleRec(F64_Value).Words[3]:=((DoubleRec(F64_Value).Words[3] and $000F) or $3FF0);

      if Exponent>15 then
        F64_to_F16:=Sign or $7C00
      else
        If (Exponent>-15) then
          F64_to_F16:=(Sign or ((Exponent+15) shl 10) or ((DoubleRec(F64_Value).WordP5 shr 2) and $3FF))
        else
          if Exponent>-25 then
            F64_to_F16:=(Sign or (((DoubleRec(F64_Value).WordP5 or $1000) and $1FFE) shr (-Exponent-12)))
          else
            F64_to_F16:=Sign;
    End;
  //============================================================================
  End;
  //============================================================================
  {$ELSE}
  //============================================================================
  Function F64_to_F16(F64_Value : Double) : Float16; Assembler;
  //============================================================================
  asm
                push    ecx
                push    edx

                xor    edx,edx
                xor    eax,eax

                mov    cx,word ptr F64_Value+6
                mov    dh,ch
                and    dh,$80
                and    cx,$7FF0
                jz      @@EX

                shr    cx,4
                sub    cx,$3FF

                cmp    cx,-$19
                jle    @@EX

                mov    ah,$7C
                cmp    cx,$10
                jge    @@EX

      @@01:    mov    ax,word ptr F64_Value+5
                shr    ax,2
                and    ax,$03FF

                cmp    cx,-$0F
                jg      @@02

                or      ah,$04
                neg    cl
                and    ax,$07FF
                sub    cl,14
                shr    eax,cl
                jmp    @@EX

      @@02:    add    cl,15
                shl    cl,2
                or      ah,cl

      @@EX:    or      ax,dx
                pop    edx
                pop    ecx
  End;
  //============================================================================
  {$ENDIF}


  {$IFNDEF USEASM}
  //============================================================================
  Function F16_to_F80(F16_Value : Float16) : Extended;
  //============================================================================
  Begin
  //============================================================================
    if F16_Value and $8000<>0 then
    Begin
      if ((F16_Value and $7FFF)=0) then
        F16_to_F80:=-0
      else
        if ((F16_Value and $7C00)=0) then
          F16_to_F80:=-(F16_Value and $3FF)*powersof.two[-24]
        else
          F16_to_F80:=-((F16_Value and $3FF) or $400)*powersof.two[((F16_Value shr 10) and $1F)-25]
    End
    else
    Begin
      if F16_Value=0 then
        F16_to_F80:=0
      else
        if ((F16_Value and $7C00)=0) then
          F16_to_F80:=(F16_Value and $3FF)*powersof.two[-24]
        else
          F16_to_F80:=((F16_Value and $3FF) or $400)*powersof.two[((F16_Value shr 10) and $1F)-25]
    End;
  //============================================================================
  End;
  //============================================================================
  {$ELSE}
  //============================================================================
  Function F16_to_F80(F16_Value : Float16) : Extended;
  //============================================================================
  asm
                push    ecx
                push    edx

                movzx  eax,F16_Value
                mov    dl,ah
                and    ax,$7FFF
                jnz    @@01

                fldz
                jmp    @@EX

        @@01:  fld1
                test    dl,$80
                jz      @@02

                fchs

        @@02:  mov    ecx,eax
                and    ax,$3FF
                shr    cx,10
                mov    edx,1000
                push    eax
                and    cl,$1F
                jz      @@03

                or      word ptr [esp],$400
                mov    dx,999
                add    dx,cx

        @@03:  fimul  dword ptr [esp]
                fmul    qword ptr powersof.two[edx*double_size]

        @@04:  pop    eax

        @@EX:  pop    edx
                pop    ecx
  //============================================================================
  End;
  //============================================================================
  {$ENDIF}
[/size] [edit] Code update. [/edit]
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #11
HalfPrecision beta 0.0.2cd attached. [bugfix] [superseded].

HalfPrecision is a tool to convert PCM in WAV format to Float16 in WAV format. Noise is added during the conversion process. By default this noise is adaptively shaped, however fixed noise shaping is also available along with the option to disable all noise shaping.

The -P, --precision <n> parameter selects the number of mantissa bits to use (8<=n<=11). Reducing the number of mantissa bits increases added noise.

By default, processed audio is output in Float16 format. Using the --decoded parameter converts the processed audio from Float16 back to the PCM in the original sample bitdepth (which may add a further small amount of noise) to allow the audio to be played in existing players.

NB: the Float16 audio is normalised in the range ±65504.0 rather than the standard ±1.0 (32bit and 64bit floating point audio). This allows a greater dynamic range of sample values (±65504 to ±6.1035E-05 [normalised]; ±5.9605E-08 [denormal]) to be stored. Returning samples to expected ±1.0 range is achieved by simple multiplication by 2^-16.

Bug-reports, comments and (constructive) criticism are welcomed.
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #12
foobar2000 1.1.9 beta1: "Implemented experimental support for 16-bit floating-point WAV files."

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #13
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #14
HalfPrecision beta 0.0.2e attached [superseded]. Fix to allow encoding to lossy.wav (and optional correction file) using foobar2000. "--stdinname %d" must be used in the command line parameters.

It occurs to me that a free-to-use (and platform agnostic) analog to HDCD could be created using Float16 encoded PCM written to a conventional CD. The trick would be to in some way tell the (software presumably) player that the audio was encoded in Float16. I'm guessing some sort of LSB encoding (minimal noise) of a periodic nature, maybe 32 to 64 LSBs per (codec-block x channels)?

[edit] beta 0.0.2e superseded [/edit]
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #15
I once had an idea for how to encode audio data. Take the square root of each data point, keeping the original sign. Decoding consists of squaring the encoded value, again keeping the sign. The result is that you lose about one bit of resolution on full-scale values, but you almost double the number of bits on small values.

For example, take 24 bit signed integer data, square root it and keep 12 bits. The values decode back to 24 bits with 15 bits of resolution on full-scale values.

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #16
That would be one way of doing it. However sample size range would be ±32767² to ±1², i.e. 1.0737E+09:1 . Sample range for Float16, as stated above, is ±65504 to ±6.1035E-05 [normalised]; ±5.9605E-08 [denormal], i.e. 1.0732E+09:1 [normalised]; 1.0990E+12:1 [denormal].

One idea for a signature / watermark to indicate Float16 encoding would be to use the bit pattern of 'Float16' with a bit inserted at either end and between the bits from each character, i.e. $231B0DE613A0C46C. This, when evaluated as a double precision float, equates to 1.41990141750237441E-0139. This pattern could be inserted, one bit per 7 samples, with a recurrence of, say, 512 samples, irrespective of number of channels (data span = 441 samples).
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #17
HalfPrecision beta 0.0.2f attached [edit] superseded [/edit].

Changelog:
  • Now outputs "halfp" and "hpcdf" files, i.e. halfp.wav and hpcdf.wav.
  • Fixes to encoding to ensure proper coding when precision limited to fewer than 10 mantissa bits.
  • Tweaks to adaptive noise shaping coefficients.
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #18
HalfPrecision beta 0.0.2g attached. [edit] Superseded. [/edit]

Changelog:
  • Output type can now be selected from 16/32 bit float; 16/24/32 bit integer;
  • Valid input types now include 8*/16/32 bit float; 8/16/24/32 bit integer;
The so called 8 bit floating point type turned up when I was searching for details of the Float16 type some time ago. The 8 bits are comprised: sign / 4-bit exponent / 3-bit mantissa, exponent bias is -2, minimum non-zero = ±1; maximum=±122880. Precision is basic (i.e. 0 to 7 are denormal (integer, exponent=0) then 8 upwards, 1.000 to 1.875 x 2^(exponent+2)). I'm still evaluating this type to see if merits further work.

Output to 8-bit unsigned integer is not yet enabled - I'm still trying to iron out some issues with clipping and adaptive noise shaping.
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #19
Nick.C Thank you for your innovation.
It remains to add support FLOAT16 in FLAC, WavPack, TAK...
P.S. lossyWav not correctly support FLOAT16.
MPC --quality 10 --tmn 20 --nmt 20 - %d || WV -miqhnb5x3 - %d

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #20
@Didjeridoo: Thanks - I am also looking forward to compatibility with a lossless format. The codebase for HalfPrecision is that of lossyWAV, so it is not inconceivable that an updated version of lossyWAV will appear at some point with the input capabilities of HalfPrecision. Output will still only be integer PCM though.

As an aside, and taking on board an early comment by Chris, I am also working on implementing read/write capability for µ-Law and A-Law samples.
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #21
HalfPrecision beta 0.0.2h attached. [edit] Superseded [/edit]

Changelog:
  • Now reads / writes CCITT 8-bit µLaw and ALaw;
  • Now writes 8-bit unsigned integer.
  • Work on so-called 8-bit float terminated after µLaw and ALaw implementation.
lossyWAV -q X -a 4 -s h -A --feedback 2 --limit 15848 --scale 0.5 | FLAC -5 -e -p -b 512 -P=4096 -S- (having set foobar to output 24-bit PCM; scaling by 0.5 gives the ANS headroom to work)

TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #22
Nick.C I watch all the moves and develops 
Is not the end, FLOAT24 also needed))
P.S. foobar2000 not read / write CCITT 8-bit µLaw and ALaw.
MPC --quality 10 --tmn 20 --nmt 20 - %d || WV -miqhnb5x3 - %d


TransPCM—use Float16/24 to reduce bit-depth, also promotes compression

Reply #24
Do you have any sample of command line for foobar.