Skip to content

Commit

Permalink
Merge pull request #803 from atsampson/ac3-test1
Browse files Browse the repository at this point in the history
ats's fixes for ac3
  • Loading branch information
happycube authored Oct 9, 2022
2 parents 540ffec + 746b010 commit c0b3c35
Show file tree
Hide file tree
Showing 15 changed files with 771 additions and 348 deletions.
4 changes: 2 additions & 2 deletions lddecode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ def ac3_pipe(outname: str):
processes = []

cmd1 = "sox -r 40000000 -b 16 -c 1 -e signed -t raw - -b 8 -r 46080000 -e unsigned -c 1 -t raw - sinc -n 500 2600000-3160000"
cmd2 = "ac3_demodulate - -"
cmd3 = f"ac3_decode - {outname}"
cmd2 = "ld-ac3-demodulate - -"
cmd3 = f"ld-ac3-decode - {outname}"

# This is done in reverse order to allow for pipe building
processes.append(subprocess.Popen(cmd3.split(' '),
Expand Down
14 changes: 0 additions & 14 deletions tools/ld-process-ac3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
cmake_minimum_required(VERSION 3.16)
project(ldecode_ac3)

set(CMAKE_CXX_STANDARD 17)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif ()

#set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

include_directories(../ld-process-efm)
add_subdirectory(demodulate)
add_subdirectory(decode)
14 changes: 7 additions & 7 deletions tools/ld-process-ac3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Developed for [LD-Decode](https://github.com/happycube/ld-decode), this project

# Description

The first executable, ac3_demodulate, takes a stream of unsigned 8-bit samples at 46.08MHz to produce a stream of QPSK
symbols, ready for the next executable. The second executable, ac3_demodulate, decodes the stream of symbols into
The first executable, ld-ac3-demodulate, takes a stream of unsigned 8-bit samples at 46.08MHz to produce a stream of QPSK
symbols, ready for the next executable. The second executable, ld-ac3-demodulate, decodes the stream of symbols into
playable ac3 audio frames, while producing
Reed-solomon, CRC and other statistics in the log file.

Expand All @@ -18,21 +18,21 @@ Reed-solomon, CRC and other statistics in the log file.
#### Syntax

```
ac3_demodulate [options] source_file output_file [log_file]
ac3_decode [options] source_file output_file [log_file]
ld-ac3-demodulate [options] source_file output_file [log_file]
ld-ac3-decode [options] source_file output_file [log_file]
```

#### example_usage.sh

```
ffmpeg -hide_banner -y -i "$path" -f s16le -c:a pcm_s16le TP0
sox -r 40000000 -b 16 -c 1 -e signed -t raw TP0 -b 8 -r 46080000 -e unsigned -c 1 -t raw TP1 sinc -n 500 2600000-3160000
cmake-build-debug/demodulate/ac3_demodulate TP1 TP2 demodulate_log
cmake-build-debug/decode/ac3_decode TP2 "$outpath" decode_log
cmake-build-debug/demodulate/ld-ac3-demodulate TP1 TP2 demodulate_log
cmake-build-debug/decode/ld-ac3-decode TP2 "$outpath" decode_log
```

In the example usage, ffmpeg and sox are used to format, resample and filter the source signal before processing with
ac3_demodulate and ac3_decode. The TP0, TP1 and TP3 files are intermediate files, used for caching and are not used
ld-ac3-demodulate and ld-ac3-decode. The TP0, TP1 and TP3 files are intermediate files, used for caching and are not used
when piping directly between the commands.

## Credit
Expand Down
9 changes: 7 additions & 2 deletions tools/ld-process-ac3/decode/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
add_executable(ac3_decode main.cpp QPSKFramer.hpp Blocker.hpp Corrector.hpp AC3Framer.hpp ac3_parsing.hpp ../logger.hpp ac3_parsing.cpp)
add_executable(ld-ac3-decode
ac3_parsing.cpp
main.cpp
)

target_include_directories(ac3_decode PRIVATE .)
target_include_directories(ld-ac3-decode PRIVATE . ../../ld-process-efm)

install(TARGETS ld-ac3-decode)
6 changes: 4 additions & 2 deletions tools/ld-process-ac3/decode/ac3_parsing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@

#pragma once

#include <iostream>
#include <algorithm>
#include <cstdint>
#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>


const double fscod_lookup[]{48, 44.1, 32};
Expand Down
6 changes: 5 additions & 1 deletion tools/ld-process-ac3/demodulate/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
add_executable(ac3_demodulate main.cpp AC3Filter.hpp OneBitADC.hpp Resampler.hpp Demodulator.hpp Reclocker.hpp ../logger.hpp)
add_executable(ld-ac3-demodulate
main.cpp
)

install(TARGETS ld-ac3-demodulate)
8 changes: 4 additions & 4 deletions tools/ld-process-ac3/example_usage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ cmake --build ./cmake-build-debug -j 3
# individual steps (useful to cache while developing)
#ffmpeg -hide_banner -y -i "$path" -f s16le -c:a pcm_s16le TP0
#sox -r 40000000 -b 16 -c 1 -e signed -t raw TP0 -b 8 -r 46080000 -e unsigned -c 1 -t raw TP1 sinc -n 500 2600000-3160000
#time cmake-build-debug/demodulate/ac3_demodulate TP1 TP2 demodulate_log
#time cmake-build-debug/decode/ac3_decode TP2 "$outpath" decode_log
#time cmake-build-debug/demodulate/ld-ac3-demodulate TP1 TP2 demodulate_log
#time cmake-build-debug/decode/ld-ac3-decode TP2 "$outpath" decode_log

time (
ffmpeg -hide_banner -loglevel error -y -i "$path" -f s16le -c:a pcm_s16le - |
sox -r 40000000 -b 16 -c 1 -e signed -t raw - -b 8 -r 46080000 -e unsigned -c 1 -t raw - sinc -n 500 2600000-3160000 |
cmake-build-debug/demodulate/ac3_demodulate - - demodulate_log |
cmake-build-debug/decode/ac3_decode - "$outpath" decode_log
cmake-build-debug/demodulate/ld-ac3-demodulate - - demodulate_log |
cmake-build-debug/decode/ld-ac3-decode - "$outpath" decode_log
)
ffplay -codec:a:0 ac3 "$outpath"
6 changes: 4 additions & 2 deletions tools/ld-process-efm/Datatypes/sector.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@

// CD-ROM Q and P specific CIRC configuration for Reed-Solomon forward error correction
template < size_t SYMBOLS, size_t PAYLOAD > struct QRS;
template < size_t PAYLOAD > struct QRS<255, PAYLOAD> : public __RS(QRS, uint8_t, 255, PAYLOAD, 0x11d, 0, 1);
template < size_t PAYLOAD > struct QRS<255, PAYLOAD>
: public __RS(QRS, uint8_t, 255, PAYLOAD, 0x11d, 0, 1, false);

template < size_t SYMBOLS, size_t PAYLOAD > struct PRS;
template < size_t PAYLOAD > struct PRS<255, PAYLOAD> : public __RS(PRS, uint8_t, 255, PAYLOAD, 0x11d, 0, 1);
template < size_t PAYLOAD > struct PRS<255, PAYLOAD>
: public __RS(PRS, uint8_t, 255, PAYLOAD, 0x11d, 0, 1, false);

#include "tracktime.h"

Expand Down
3 changes: 2 additions & 1 deletion tools/ld-process-efm/Decoders/c1circ.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@

// CD-ROM specific CIRC configuration for Reed-Solomon forward error correction
template < size_t SYMBOLS, size_t PAYLOAD > struct C1RS;
template < size_t PAYLOAD > struct C1RS<255, PAYLOAD> : public __RS(C1RS, uint8_t, 255, PAYLOAD, 0x11d, 0, 1);
template < size_t PAYLOAD > struct C1RS<255, PAYLOAD>
: public __RS(C1RS, uint8_t, 255, PAYLOAD, 0x11d, 0, 1, false);

#include "Datatypes/f3frame.h"

Expand Down
3 changes: 2 additions & 1 deletion tools/ld-process-efm/Decoders/c2circ.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@

// CD-ROM specific CIRC configuration for Reed-Solomon forward error correction
template < size_t SYMBOLS, size_t PAYLOAD > struct C2RS;
template < size_t PAYLOAD > struct C2RS<255, PAYLOAD> : public __RS(C2RS, uint8_t, 255, PAYLOAD, 0x11d, 0, 1);
template < size_t PAYLOAD > struct C2RS<255, PAYLOAD>
: public __RS(C2RS, uint8_t, 255, PAYLOAD, 0x11d, 0, 1, false);

class C2Circ
{
Expand Down
2 changes: 1 addition & 1 deletion tools/ld-process-efm/ezpwd/bch_base
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ namespace ezpwd {
*/

extern "C" {
#include "../../djelic_bch.h"
#include "bch_include/linux/bch.h"
}

//
Expand Down
8 changes: 4 additions & 4 deletions tools/ld-process-efm/ezpwd/corrector
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ namespace ezpwd {
// external language APIs.
//
template <
size_t PARITY,
size_t N = 64,
unsigned PARITY,
unsigned N = 64,
typename SERIAL = serialize::base< N, serialize::ezpwd< N >>>
class corrector {
public:
Expand Down Expand Up @@ -488,13 +488,13 @@ namespace ezpwd {
rscodec;
};

template < size_t PARITY, size_t N, typename SERIAL >
template < unsigned PARITY, unsigned N, typename SERIAL >
const ezpwd::RS<N-1,N-1-PARITY>
corrector<PARITY,N,SERIAL>::rscodec;

} // namespace ezpwd

template < size_t PARITY, size_t N, typename SERIAL >
template < unsigned PARITY, unsigned N, typename SERIAL >
std::ostream &operator<<(
std::ostream &lhs,
const ezpwd::corrector<PARITY,N,SERIAL>
Expand Down
3 changes: 2 additions & 1 deletion tools/ld-process-efm/ezpwd/definitions
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#define _EZPWD_DEFINITIONS

#include "serialize_definitions"
#include "rs_definitions"

#endif // _EZPWD_DEFINITIONS
#endif // _EZPWD_DEFINITIONS
66 changes: 39 additions & 27 deletions tools/ld-process-efm/ezpwd/rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

//
// ezpwd::RS<SYMBOLS,PAYLOAD> -- Implements an RS(SYMBOLS,PAYLOAD) codec
// ezpwd::RS_CCSDS<...> -- CCSDS standard 8-bit R-S codec
// ezpwd::RS_CCSDS<...> -- CCSDS standard 8-bit R-S codec w/ dual-basis encoding
//
// Support for Reed-Solomon codecs for symbols of 2 to 16 bits is supported. The R-S "codeword"
// for an N-bit symbol is defined to be 2^N-1 symbols in size. For example, for 5-bit symbols,
Expand All @@ -42,20 +42,21 @@ namespace ezpwd {
// @POLY: A primitive polynomial appropriate to the SYMBOLS size
// @FCR: The first consecutive root of the Reed-Solomon generator polynomial
// @PRIM: The primitive root of the generator polynomial
// @DUAL: Assumes user data/parity is Berleskamp dual-basis encoded
//
# define __RS_TYP( TYPE, SYMBOLS, PAYLOAD, POLY, FCR, PRIM ) \
# define __RS_TYP( TYPE, SYMBOLS, PAYLOAD, POLY, FCR, PRIM, DUAL ) \
ezpwd::reed_solomon< \
TYPE, \
ezpwd::log_< (SYMBOLS) + 1 >::value, \
(SYMBOLS) - (PAYLOAD), FCR, PRIM, \
ezpwd::gfpoly< \
ezpwd::log_< (SYMBOLS) + 1 >::value, \
POLY >>
POLY >, DUAL >

# define __RS( NAME, TYPE, SYMBOLS, PAYLOAD, POLY, FCR, PRIM ) \
__RS_TYP( TYPE, SYMBOLS, PAYLOAD, POLY, FCR, PRIM ) { \
# define __RS( NAME, TYPE, SYMBOLS, PAYLOAD, POLY, FCR, PRIM, DUAL ) \
__RS_TYP( TYPE, SYMBOLS, PAYLOAD, POLY, FCR, PRIM, DUAL ) { \
NAME() \
: __RS_TYP( TYPE, SYMBOLS, PAYLOAD, POLY, FCR, PRIM )() \
: __RS_TYP( TYPE, SYMBOLS, PAYLOAD, POLY, FCR, PRIM, DUAL )() \
{;} \
}

Expand All @@ -71,25 +72,36 @@ namespace ezpwd {
// default constructor must be provided.
//
template < size_t SYMBOLS, size_t PAYLOAD > struct RS;
template < size_t PAYLOAD > struct RS< 3, PAYLOAD> : public __RS( RS, uint8_t, 3, PAYLOAD, 0x7, 1, 1 );
template < size_t PAYLOAD > struct RS< 7, PAYLOAD> : public __RS( RS, uint8_t, 7, PAYLOAD, 0xb, 1, 1 );
template < size_t PAYLOAD > struct RS< 15, PAYLOAD> : public __RS( RS, uint8_t, 15, PAYLOAD, 0x13, 1, 1 );
template < size_t PAYLOAD > struct RS< 31, PAYLOAD> : public __RS( RS, uint8_t, 31, PAYLOAD, 0x25, 1, 1 );
template < size_t PAYLOAD > struct RS< 63, PAYLOAD> : public __RS( RS, uint8_t, 63, PAYLOAD, 0x43, 1, 1 );
template < size_t PAYLOAD > struct RS< 127, PAYLOAD> : public __RS( RS, uint8_t, 127, PAYLOAD, 0x89, 1, 1 );
template < size_t PAYLOAD > struct RS< 255, PAYLOAD> : public __RS( RS, uint8_t, 255, PAYLOAD, 0x11d, 1, 1 );
template < size_t PAYLOAD > struct RS< 511, PAYLOAD> : public __RS( RS, uint16_t, 511, PAYLOAD, 0x211, 1, 1 );
template < size_t PAYLOAD > struct RS< 1023, PAYLOAD> : public __RS( RS, uint16_t, 1023, PAYLOAD, 0x409, 1, 1 );
template < size_t PAYLOAD > struct RS< 2047, PAYLOAD> : public __RS( RS, uint16_t, 2047, PAYLOAD, 0x805, 1, 1 );
template < size_t PAYLOAD > struct RS< 4095, PAYLOAD> : public __RS( RS, uint16_t, 4095, PAYLOAD, 0x1053, 1, 1 );
template < size_t PAYLOAD > struct RS< 8191, PAYLOAD> : public __RS( RS, uint16_t, 8191, PAYLOAD, 0x201b, 1, 1 );
template < size_t PAYLOAD > struct RS<16383, PAYLOAD> : public __RS( RS, uint16_t, 16383, PAYLOAD, 0x4443, 1, 1 );
template < size_t PAYLOAD > struct RS<32767, PAYLOAD> : public __RS( RS, uint16_t, 32767, PAYLOAD, 0x8003, 1, 1 );
template < size_t PAYLOAD > struct RS<65535, PAYLOAD> : public __RS( RS, uint16_t, 65535, PAYLOAD, 0x1100b, 1, 1 );

template < size_t SYMBOLS, size_t PAYLOAD > struct RS_CCSDS;
template < size_t PAYLOAD > struct RS_CCSDS<255, PAYLOAD> : public __RS( RS_CCSDS, uint8_t, 255, PAYLOAD, 0x187, 112, 11 );
template < size_t PAYLOAD > struct RS< 3, PAYLOAD>: public __RS( RS, uint8_t, 3, PAYLOAD, 0x7, 1, 1, false );
template < size_t PAYLOAD > struct RS< 7, PAYLOAD>: public __RS( RS, uint8_t, 7, PAYLOAD, 0xb, 1, 1, false );
template < size_t PAYLOAD > struct RS< 15, PAYLOAD>: public __RS( RS, uint8_t, 15, PAYLOAD, 0x13, 1, 1, false );
template < size_t PAYLOAD > struct RS< 31, PAYLOAD>: public __RS( RS, uint8_t, 31, PAYLOAD, 0x25, 1, 1, false );
template < size_t PAYLOAD > struct RS< 63, PAYLOAD>: public __RS( RS, uint8_t, 63, PAYLOAD, 0x43, 1, 1, false );
template < size_t PAYLOAD > struct RS< 127, PAYLOAD>: public __RS( RS, uint8_t, 127, PAYLOAD, 0x89, 1, 1, false );
template < size_t PAYLOAD > struct RS< 255, PAYLOAD>: public __RS( RS, uint8_t, 255, PAYLOAD, 0x11d, 1, 1, false );
template < size_t PAYLOAD > struct RS< 511, PAYLOAD>: public __RS( RS, uint16_t, 511, PAYLOAD, 0x211, 1, 1, false );
template < size_t PAYLOAD > struct RS< 1023, PAYLOAD>: public __RS( RS, uint16_t, 1023, PAYLOAD, 0x409, 1, 1, false );
template < size_t PAYLOAD > struct RS< 2047, PAYLOAD>: public __RS( RS, uint16_t, 2047, PAYLOAD, 0x805, 1, 1, false );
template < size_t PAYLOAD > struct RS< 4095, PAYLOAD>: public __RS( RS, uint16_t, 4095, PAYLOAD, 0x1053, 1, 1, false );
template < size_t PAYLOAD > struct RS< 8191, PAYLOAD>: public __RS( RS, uint16_t, 8191, PAYLOAD, 0x201b, 1, 1, false );
template < size_t PAYLOAD > struct RS<16383, PAYLOAD>: public __RS( RS, uint16_t, 16383, PAYLOAD, 0x4443, 1, 1, false );
template < size_t PAYLOAD > struct RS<32767, PAYLOAD>: public __RS( RS, uint16_t, 32767, PAYLOAD, 0x8003, 1, 1, false );
template < size_t PAYLOAD > struct RS<65535, PAYLOAD>: public __RS( RS, uint16_t, 65535, PAYLOAD, 0x1100b, 1, 1, false );

// RS_CCSDS... specifies an R-S encoder using the CCSDS polynomial; RS_CCSDS assumes Berlekamp
// style dual-basis encoding, while RS_CCSDS_CONV assumes conventional encoding. To match
// standard CCSDS encoding, transformational lookup tables (to remove the dual-basis encoding)
// are used before encoding on the data and then afterwards on the parity (to return dual-basis
// encoded parity). For decoding, the dual-basis is removed, the data decoded, and then the
// corrected data/parity is restored to dual-basis encoding. This is done automatically. See
// CCSDS 131.0-B-3 Annex F for details. See also the discussion about the Falcon 9 telemetry at
// https://destevez.net/2021/03/decoding-the-falcon-9-upper-stage/, which uses the CCSDS
// (255,239) coding w/ 16 parity symbols, 8 symbols of error-correction capacity.
// RS_CCSDS... works *only* for PAYLOAD values of 223 and 239.
template < size_t SYMBOLS, size_t PAYLOAD > struct RS_CCSDS_CONV;
template < size_t PAYLOAD > struct RS_CCSDS_CONV<255, PAYLOAD>: public __RS( RS_CCSDS_CONV, uint8_t, 255, PAYLOAD, 0x187, 128-(255-PAYLOAD)/2, 11, false );
template < size_t SYMBOLS, size_t PAYLOAD > struct RS_CCSDS;
template < size_t PAYLOAD > struct RS_CCSDS<255, PAYLOAD>: public __RS( RS_CCSDS, uint8_t, 255, PAYLOAD, 0x187, 128-(255-PAYLOAD)/2, 11, true );

//
// strength<PARITY> -- compute strength (given N parity symbols) of R-S correction
Expand All @@ -109,11 +121,11 @@ namespace ezpwd {
// effective strength -- all symbols in the R-S complete codeword are equally effective in
// recovering any other symbol in error/erasure.
//
template < size_t PARITY >
template < size_t PARITY, typename ERR = unsigned >
int strength(
int corrected,
const std::vector<int>&erasures, // original erasures positions
const std::vector<int>&positions ) // all reported correction positions
const std::vector<ERR>&erasures, // original erasures positions
const std::vector<ERR>&positions ) // all reported correction positions
{
// -'ve indicates R-S failure; all parity consumed, but insufficient to correct the R-S
// codeword. Missing an unknown number of additional required parity symbols, so just
Expand Down
Loading

0 comments on commit c0b3c35

Please sign in to comment.