LCOV - code coverage report
Current view: top level - source/soundmanager/data - ogg.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 121 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 15 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : #include "precompiled.h"
      18             : 
      19             : #include "ogg.h"
      20             : 
      21             : #if CONFIG2_AUDIO
      22             : 
      23             : #include "lib/byte_order.h"
      24             : #include "lib/code_annotation.h"
      25             : #include "lib/debug.h"
      26             : #include "lib/file/io/io.h"
      27             : #include "maths/MathUtil.h"
      28             : #include "ps/CLogger.h"
      29             : 
      30             : #include <AL/al.h>
      31             : #include <algorithm>
      32             : #include <fcntl.h>
      33             : #include <fmt/format.h>
      34             : #include <iterator>
      35             : #include <ogg/config_types.h>
      36             : #include <stdexcept>
      37             : #include <vorbis/codec.h>
      38             : #include <vorbis/vorbisfile.h>
      39             : 
      40           0 : static Status LibErrorFromVorbis(int err)
      41             : {
      42           0 :     switch(err)
      43             :     {
      44             :     case 0:
      45             :         return INFO::OK;
      46           0 :     case OV_HOLE:
      47           0 :         return ERR::AGAIN;
      48           0 :     case OV_EREAD:
      49           0 :         return ERR::IO;
      50           0 :     case OV_EFAULT:
      51           0 :         return ERR::LOGIC;
      52             :     case OV_EIMPL:
      53             :         return ERR::NOT_SUPPORTED;
      54           0 :     case OV_EINVAL:
      55           0 :         return ERR::INVALID_PARAM;
      56             :     case OV_ENOTVORBIS:
      57             :         return ERR::NOT_SUPPORTED;
      58           0 :     case OV_EBADHEADER:
      59           0 :         return ERR::CORRUPTED;
      60           0 :     case OV_EVERSION:
      61           0 :         return ERR::INVALID_VERSION;
      62           0 :     case OV_ENOTAUDIO:
      63           0 :         return ERR::_1;
      64           0 :     case OV_EBADPACKET:
      65           0 :         return ERR::_2;
      66           0 :     case OV_EBADLINK:
      67           0 :         return ERR::_3;
      68           0 :     case OV_ENOSEEK:
      69           0 :         return ERR::_4;
      70           0 :     default:
      71           0 :         return ERR::FAIL;
      72             :     }
      73             : }
      74             : 
      75             : //-----------------------------------------------------------------------------
      76             : 
      77           0 : class VorbisBufferAdapter
      78             : {
      79             : public:
      80           0 :     VorbisBufferAdapter(const std::shared_ptr<std::uint8_t>& buffer, size_t size)
      81           0 :         : m_Buffer(buffer)
      82           0 :         , m_Size(size)
      83             :     {
      84             :     }
      85             : 
      86           0 :     static size_t Read(void* bufferToFill, size_t itemSize, size_t numItems, void* context)
      87             :     {
      88           0 :         VorbisBufferAdapter* adapter{static_cast<VorbisBufferAdapter*>(context)};
      89             : 
      90           0 :         const ogg_int64_t sizeRequested{static_cast<ogg_int64_t>(numItems * itemSize)};
      91           0 :         const ogg_int64_t sizeRemaining{adapter->m_Size - adapter->m_Offset};
      92           0 :         const size_t sizeToRead{static_cast<size_t>(std::min(sizeRequested, sizeRemaining))};
      93             : 
      94           0 :         std::copy_n(
      95           0 :             adapter->m_Buffer.get() + adapter->m_Offset,
      96             :             sizeToRead,
      97             :             static_cast<std::uint8_t*>(bufferToFill)
      98             :         );
      99             : 
     100           0 :         adapter->m_Offset += sizeToRead;
     101           0 :         return sizeToRead;
     102             :     }
     103             : 
     104           0 :     static int Seek(void* context, ogg_int64_t offset, int whence)
     105             :     {
     106           0 :         VorbisBufferAdapter* adapter{static_cast<VorbisBufferAdapter*>(context)};
     107             : 
     108           0 :         ogg_int64_t origin{0};
     109           0 :         switch(whence)
     110             :         {
     111             :         case SEEK_SET:
     112             :             origin = 0;
     113             :             break;
     114           0 :         case SEEK_CUR:
     115           0 :             origin = adapter->m_Offset;
     116           0 :             break;
     117           0 :         case SEEK_END:
     118           0 :             origin = adapter->m_Size + 1;
     119           0 :             break;
     120           0 :             NODEFAULT;
     121             :         }
     122             : 
     123           0 :         adapter->m_Offset = Clamp<ogg_int64_t>(origin + offset, 0, adapter->m_Size);
     124           0 :         return 0;
     125             :     }
     126             : 
     127           0 :     static int Close(void* context)
     128             :     {
     129           0 :         VorbisBufferAdapter* adapter{static_cast<VorbisBufferAdapter*>(context)};
     130           0 :         adapter->m_Buffer.reset();
     131           0 :         return 0;   // return value is ignored
     132             :     }
     133             : 
     134           0 :     static long Tell(void* context)
     135             :     {
     136           0 :         VorbisBufferAdapter* adapter{static_cast<VorbisBufferAdapter*>(context)};
     137           0 :         return adapter->m_Offset;
     138             :     }
     139             : 
     140             : private:
     141             :     std::shared_ptr<std::uint8_t> m_Buffer;
     142             :     ogg_int64_t m_Size;
     143             :     ogg_int64_t m_Offset{0};
     144             : };
     145             : 
     146             : 
     147             : //-----------------------------------------------------------------------------
     148             : 
     149             : template <typename Adapter>
     150             : class OggStreamImpl : public OggStream
     151             : {
     152             : public:
     153           0 :     OggStreamImpl(const Adapter& adapter)
     154           0 :         : m_Adapter(adapter),
     155           0 :         m_FileEOF(false),
     156           0 :         m_Info(nullptr)
     157             :     {
     158           0 :         Open();
     159           0 :     }
     160             : 
     161           0 :     ~OggStreamImpl()
     162             :     {
     163           0 :         ov_clear(&m_VorbisFile);
     164           0 :     }
     165             : 
     166           0 :     virtual ALenum Format()
     167             :     {
     168           0 :         return (m_Info->channels == 1)? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
     169             :     }
     170             : 
     171           0 :     virtual ALsizei SamplingRate()
     172             :     {
     173           0 :         return m_Info->rate;
     174             :     }
     175           0 :     virtual bool AtFileEOF()
     176             :     {
     177           0 :         return m_FileEOF;
     178             :     }
     179             : 
     180           0 :     virtual Status ResetFile()
     181             :     {
     182           0 :         ov_time_seek(&m_VorbisFile, 0);
     183           0 :         m_FileEOF = false;
     184           0 :         return INFO::OK;
     185             :     }
     186             : 
     187           0 :     virtual size_t GetNextChunk(std::span<std::uint8_t> buffer)
     188             :     {
     189             :         // We may have to call ov_read multiple times because it
     190             :         // treats the buffer size "as a limit and not a request".
     191           0 :         size_t bytesRead{0};
     192           0 :         while (bytesRead < buffer.size())
     193             :         {
     194           0 :             constexpr int isBigEndian{(BYTE_ORDER == BIG_ENDIAN)};
     195           0 :             constexpr int wordSize{sizeof(std::int16_t)};
     196           0 :             constexpr int isSigned{1};
     197             :             // Unused.
     198             :             int bitstream;
     199           0 :             const int ret{static_cast<int>(ov_read(
     200             :                 &m_VorbisFile,
     201           0 :                 reinterpret_cast<char*>(buffer.data() + bytesRead),
     202           0 :                 static_cast<int>(buffer.size() - bytesRead),
     203             :                 isBigEndian,
     204             :                 wordSize,
     205             :                 isSigned,
     206             :                 &bitstream
     207             :             ))};
     208           0 :             if(ret == 0) {
     209           0 :                 m_FileEOF = true;
     210           0 :                 return bytesRead;
     211             :             }
     212             : 
     213           0 :             if(ret < 0)
     214           0 :                 WARN_RETURN(LibErrorFromVorbis(ret));
     215             : 
     216           0 :             bytesRead += ret;
     217             :         }
     218             :         return bytesRead;
     219             :     }
     220             : 
     221             : private:
     222           0 :     void Open()
     223             :     {
     224             :         ov_callbacks callbacks;
     225           0 :         callbacks.read_func = Adapter::Read;
     226           0 :         callbacks.close_func = Adapter::Close;
     227           0 :         callbacks.seek_func = Adapter::Seek;
     228           0 :         callbacks.tell_func = Adapter::Tell;
     229           0 :         const int ret{ov_open_callbacks(&m_Adapter, &m_VorbisFile, 0, 0, callbacks)};
     230           0 :         switch (ret)
     231             :         {
     232             :             case 0:
     233             :                 break;
     234           0 :             case OV_EBADHEADER:
     235             :             case OV_EREAD:
     236             :             case OV_ENOTVORBIS:
     237             :             case OV_EVERSION:
     238           0 :                 throw std::runtime_error(fmt::format("Failed to open Ogg Vorbis file: {}", LibErrorFromVorbis(ret)));
     239           0 :             default:
     240           0 :                 throw std::runtime_error(fmt::format("Unknown error opening Ogg Vorbis file: {}", LibErrorFromVorbis(ret)));
     241             :         }
     242             : 
     243             :         // Retrieve info for current bitstream.
     244           0 :         const int link{-1};
     245           0 :         m_Info = ov_info(&m_VorbisFile, link);
     246           0 :         if(!m_Info)
     247           0 :             DEBUG_WARN_ERR(ERR::INVALID_HANDLE);
     248           0 :     }
     249             : 
     250             :     Adapter m_Adapter;
     251             :     OggVorbis_File m_VorbisFile;
     252             :     vorbis_info* m_Info;
     253             :     bool m_FileEOF;
     254             : };
     255             : 
     256             : 
     257             : //-----------------------------------------------------------------------------
     258             : 
     259           0 : Status OpenOggNonstream(const PIVFS& vfs, const VfsPath& pathname, OggStreamPtr& stream)
     260             : {
     261           0 :     std::shared_ptr<std::uint8_t> contents;
     262           0 :     size_t size;
     263           0 :     RETURN_STATUS_IF_ERR(vfs->LoadFile(pathname, contents, size));
     264             : 
     265           0 :     try
     266             :     {
     267           0 :         std::shared_ptr<OggStreamImpl<VorbisBufferAdapter>> tmp{std::make_shared<OggStreamImpl<VorbisBufferAdapter>>(VorbisBufferAdapter(contents, size))};
     268           0 :         stream = tmp;
     269           0 :         return INFO::OK;
     270           0 :     }
     271           0 :     catch(const std::runtime_error& e)
     272             :     {
     273           0 :         LOGERROR("{}", e.what());
     274           0 :         return ERR::IO;
     275           0 :     }
     276           0 : }
     277             : 
     278             : #endif  // CONFIG2_AUDIO
     279             : 

Generated by: LCOV version 1.16