diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2024-06-18 23:01:36 +0000 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2024-06-26 20:14:13 +0000 |
commit | 1d99ba9937d6588f4b00dc3766e165e9e3ff834d (patch) | |
tree | 931a13e03da012f856b42677f87a7c5b5568c714 /src | |
parent | 23a4e5f1dd7ce24f65a2af0598d1f92af4b5c424 (diff) | |
download | atracdenc-1d99ba9937d6588f4b00dc3766e165e9e3ff834d.tar.gz |
[AT3P] Introduce at3p development branch
- Simpe code just to produce correct at3p zero frame
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/atrac/at3p/at3p.cpp | 55 | ||||
-rw-r--r-- | src/atrac/at3p/at3p_bitstream.cpp | 61 | ||||
-rw-r--r-- | src/atrac/at3p/at3p_bitstream.h | 34 | ||||
-rw-r--r-- | src/atrac1denc.h | 5 | ||||
-rw-r--r-- | src/atrac3p.h | 38 | ||||
-rw-r--r-- | src/main.cpp | 71 | ||||
-rw-r--r-- | src/oma.cpp | 6 |
8 files changed, 265 insertions, 7 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0411a88..382e59d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -82,6 +82,8 @@ set(SOURCE_ATRACDENC_IMPL atrac3denc.cpp atrac/atrac3.cpp atrac/atrac3_bitstream.cpp + atrac/at3p/at3p.cpp + atrac/at3p/at3p_bitstream.cpp lib/mdct/mdct.cpp ) diff --git a/src/atrac/at3p/at3p.cpp b/src/atrac/at3p/at3p.cpp new file mode 100644 index 0000000..f6e41f0 --- /dev/null +++ b/src/atrac/at3p/at3p.cpp @@ -0,0 +1,55 @@ +/* + * This file is part of AtracDEnc. + * + * AtracDEnc is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * AtracDEnc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with AtracDEnc; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <atrac3p.h> + +#include "at3p_bitstream.h" + +namespace NAtracDEnc { + +class TAt3PEnc::TImpl { +public: + TImpl(ICompressedOutput* out) + : BitStream(out, 2048) + {} + void EncodeFrame(const TFloat* data, int channels); +private: + TAt3PBitStream BitStream; +}; + +void TAt3PEnc::TImpl:: +EncodeFrame(const TFloat* data, int channels) +{ + BitStream.WriteFrame(channels); +} + +TAt3PEnc::TAt3PEnc(TCompressedOutputPtr&& out, int channels) + : Out(std::move(out)) + , Channels(channels) +{ +} + +TPCMEngine<TFloat>::TProcessLambda TAt3PEnc::GetLambda() { + Impl.reset(new TImpl(Out.get())); + + return [this](TFloat* data, const TPCMEngine<TFloat>::ProcessMeta&) { + Impl->EncodeFrame(data, Channels); + }; +} + +} diff --git a/src/atrac/at3p/at3p_bitstream.cpp b/src/atrac/at3p/at3p_bitstream.cpp new file mode 100644 index 0000000..7bf5829 --- /dev/null +++ b/src/atrac/at3p/at3p_bitstream.cpp @@ -0,0 +1,61 @@ +/* + * This file is part of AtracDEnc. + * + * AtracDEnc is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * AtracDEnc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with AtracDEnc; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#include "at3p_bitstream.h" +#include <lib/bitstream/bitstream.h> +#include <env.h> +#include <iostream> + +namespace NAtracDEnc { + +TAt3PBitStream::TAt3PBitStream(ICompressedOutput* container, uint16_t frameSz) + : Container(container) + , FrameSz(frameSz) +{ + NEnv::SetRoundFloat(); +} + +void TAt3PBitStream::WriteFrame(int channels) +{ + NBitStream::TBitStream bitStream; + // First bit must be zero + bitStream.Write(0, 1); + // Channel block type + // 0 - MONO block + // 1 - STEREO block + // 2 - Nobody know + bitStream.Write(channels - 1, 2); + + // Skip some bits to produce correct zero bitstream + bitStream.Write(0, 10); + if (channels == 2) { + bitStream.Write(0, 14); + } else { + bitStream.Write(0, 5); + } + // Terminator + bitStream.Write(3, 2); + + std::vector<char> buf = bitStream.GetBytes(); + + buf.resize(FrameSz); + Container->WriteFrame(buf); +} + +} diff --git a/src/atrac/at3p/at3p_bitstream.h b/src/atrac/at3p/at3p_bitstream.h new file mode 100644 index 0000000..a293c0d --- /dev/null +++ b/src/atrac/at3p/at3p_bitstream.h @@ -0,0 +1,34 @@ +/* + * This file is part of AtracDEnc. + * + * AtracDEnc is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * AtracDEnc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with AtracDEnc; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include "compressed_io.h" + +namespace NAtracDEnc { + +class TAt3PBitStream { +public: + TAt3PBitStream(ICompressedOutput* container, uint16_t frameSz); + void WriteFrame(int channels); +private: + ICompressedOutput* Container; + const uint16_t FrameSz; +}; + +} diff --git a/src/atrac1denc.h b/src/atrac1denc.h index 5752f22..ac5c481 100644 --- a/src/atrac1denc.h +++ b/src/atrac1denc.h @@ -30,11 +30,6 @@ namespace NAtracDEnc { -enum EMode { - E_ENCODE = 1, - E_DECODE = 2, - E_ATRAC3 = 4 -}; class TAtrac1MDCT : public virtual NAtrac1::TAtrac1Data { NMDCT::TMDCT<512> Mdct512; diff --git a/src/atrac3p.h b/src/atrac3p.h new file mode 100644 index 0000000..31371d1 --- /dev/null +++ b/src/atrac3p.h @@ -0,0 +1,38 @@ +/* + * This file is part of AtracDEnc. + * + * AtracDEnc is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * AtracDEnc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with AtracDEnc; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include "config.h" +#include "pcmengin.h" +#include "compressed_io.h" + +namespace NAtracDEnc { + +class TAt3PEnc : public IProcessor<TFloat> { +public: + TAt3PEnc(TCompressedOutputPtr&& out, int channels); + TPCMEngine<TFloat>::TProcessLambda GetLambda() override; +private: + TCompressedOutputPtr Out; + int Channels; + class TImpl; + std::unique_ptr<TImpl> Impl; +}; + +} diff --git a/src/main.cpp b/src/main.cpp index 2e64aed..6561a2a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,7 @@ #include "config.h" #include "atrac1denc.h" #include "atrac3denc.h" +#include "atrac3p.h" #ifdef PLATFORM_WINDOWS #include <windows.h> @@ -89,6 +90,13 @@ static int checkedStoi(const char* data, int min, int max, int def) } } +enum EMode { + E_ENCODE = 1, + E_DECODE = 2, + E_ATRAC3 = 4, + E_ATRAC3PLUS = 8 +}; + enum EOptions { O_ENCODE = 'e', @@ -243,6 +251,59 @@ static void PrepareAtrac3Encoder(const string& inFile, atracProcessor->reset(new TAtrac3Encoder(std::move(omaIO), std::move(encoderSettings))); } +static void PrepareAtrac3PEncoder(const string& inFile, + const string& outFile, + const bool noStdOut, + int numChannels, + uint64_t* totalSamples, + const TWavPtr& wavIO, + TPcmEnginePtr* pcmEngine, + TAtracProcessorPtr* atracProcessor) +{ + *totalSamples = wavIO->GetTotalSamples(); + const uint64_t numFrames = (*totalSamples) / 2048; + if (numFrames >= UINT32_MAX) { + std::cerr << "Number of input samples exceeds output format limitation," + "the result will be incorrect" << std::endl; + } + + const string ext = GetFileExt(outFile); + + TCompressedOutputPtr omaIO; + + string contName; + if (ext == "wav" || ext == "at3") { + throw std::runtime_error("Not implemented"); + } else if (ext == "rm") { + throw std::runtime_error("RealMedia container is not supported for ATRAC3PLUS"); + } else { + contName = "OMA"; + omaIO.reset(new TOma(outFile, + "test", + numChannels, + (int32_t)numFrames, OMAC_ID_ATRAC3PLUS, + 2048, + false)); + } + + if (!noStdOut) + cout << "Input:\n Filename: " << inFile + << "\n Channels: " << (int)numChannels + << "\n SampleRate: " << wavIO->GetSampleRate() + << "\n Duration (sec): " << *totalSamples / wavIO->GetSampleRate() + << "\nOutput:\n Filename: " << outFile + << "\n Codec: ATRAC3Plus" + << "\n Container: " << contName + //<< "\n Bitrate: " << encoderSettings.ConteinerParams->Bitrate + << endl; + + pcmEngine->reset(new TPCMEngine<TFloat>(4096, + numChannels, + TPCMEngine<TFloat>::TReaderPtr(wavIO->GetPCMReader<TFloat>()))); + atracProcessor->reset(new TAt3PEnc(std::move(omaIO), numChannels)); +} + + int main_(int argc, char* const* argv) { const char* myName = argv[0]; @@ -282,6 +343,8 @@ int main_(int argc, char* const* argv) } else if (strcmp(optarg, "atrac3_lp4") == 0) { mode |= E_ATRAC3; bitrate = 64; + } else if (strcmp(optarg, "atrac3plus") == 0) { + mode |= E_ATRAC3PLUS; } else if (strcmp(optarg, "atrac1") == 0) { // this is the default } else { @@ -399,6 +462,14 @@ int main_(int argc, char* const* argv) pcmFrameSz = TAtrac3Data::NumSamples;; } break; + case (E_ENCODE | E_ATRAC3PLUS): + { + wavIO = OpenWavFile(inFile); + PrepareAtrac3PEncoder(inFile, outFile, noStdOut, wavIO->GetChannelNum(), + &totalSamples, wavIO, &pcmEngine, &atracProcessor); + pcmFrameSz = 2048; + } + break; default: { throw std::runtime_error("Processing mode was not specified"); diff --git a/src/oma.cpp b/src/oma.cpp index 78be514..22f9388 100644 --- a/src/oma.cpp +++ b/src/oma.cpp @@ -24,12 +24,14 @@ using std::string; using std::vector; using std::unique_ptr; -TOma::TOma(const string& filename, const string&, uint8_t /*numChannel*/, +TOma::TOma(const string& filename, const string&, uint8_t numChannel, uint32_t /*numFrames*/, int cid, uint32_t framesize, bool jointStereo) { oma_info_t info; info.codec = cid; info.samplerate = 44100; - info.channel_format = jointStereo ? OMA_STEREO_JS : OMA_STEREO; + info.channel_format = (cid == OMAC_ID_ATRAC3) + ? (jointStereo ? OMA_STEREO_JS : OMA_STEREO) + : (numChannel == 1 ? OMA_MONO : OMA_STEREO); info.framesize = framesize; File = oma_open(filename.c_str(), OMAM_W, &info); if (!File) |