| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include "../mednafen.h" |
| 19 | #include "audioreader.h" |
| 20 | #include "audioreader_opus.h" |
| 21 | |
| 22 | // OPUS SUPPORT NOT DONE YET!!! |
| 23 | /* |
| 24 | |
| 25 | (int64)op_pcm_total() * 44100 / 48000 |
| 26 | |
| 27 | resampling vs seek, filter delay, etc. to consider |
| 28 | */ |
| 29 | |
| 30 | static size_t iop_read_func(void *ptr, size_t size, size_t nmemb, void *user_data) |
| 31 | { |
| 32 | Stream *fw = (Stream*)user_data; |
| 33 | |
| 34 | if(!size) |
| 35 | return(0); |
| 36 | |
| 37 | try |
| 38 | { |
| 39 | return fw->read(ptr, size * nmemb, false) / size; |
| 40 | } |
| 41 | catch(...) |
| 42 | { |
| 43 | return(0); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static int iop_seek_func(void *user_data, opus_int64 offset, int whence) |
| 48 | { |
| 49 | Stream *fw = (Stream*)user_data; |
| 50 | |
| 51 | try |
| 52 | { |
| 53 | fw->seek(offset, whence); |
| 54 | return(0); |
| 55 | } |
| 56 | catch(...) |
| 57 | { |
| 58 | return(-1); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static int iop_close_func(void *user_data) |
| 63 | { |
| 64 | Stream *fw = (Stream*)user_data; |
| 65 | |
| 66 | try |
| 67 | { |
| 68 | fw->close(); |
| 69 | return(0); |
| 70 | } |
| 71 | catch(...) |
| 72 | { |
| 73 | return EOF; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static opus_int64 iop_tell_func(void *user_data) |
| 78 | { |
| 79 | Stream *fw = (Stream*)user_data; |
| 80 | |
| 81 | try |
| 82 | { |
| 83 | return fw->tell(); |
| 84 | } |
| 85 | catch(...) |
| 86 | { |
| 87 | return(-1); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* Error strings copied from libopusfile header file comments. */ |
| 92 | static const char *op_errstring(int error) |
| 93 | { |
| 94 | static const struct |
| 95 | { |
| 96 | int code; |
| 97 | const char *str; |
| 98 | } error_table[] = |
| 99 | { |
| 100 | { OP_EREAD, gettext_noop("OP_EREAD: An underlying read, seek, or tell operation failed when it should have succeeded.") }, |
| 101 | { OP_EFAULT, gettext_noop("OP_EFAULT: A NULL pointer was passed where one was unexpected, or an internal memory allocation failed, or an internal library error was encountered.") }, |
| 102 | { OP_EIMPL, gettext_noop("OP_EIMPL: The stream used a feature that is not implemented, such as an unsupported channel family.") }, |
| 103 | { OP_EINVAL, gettext_noop("OP_EINVAL: One or more parameters to a function were invalid.") }, |
| 104 | { OP_ENOTFORMAT, gettext_noop("OP_ENOTFORMAT: A purported Ogg Opus stream did not begin with an Ogg page, or a purported header packet did not start with one of the required strings, \"OpusHead\" or \"OpusTags\".") }, |
| 105 | { OP_EBADHEADER, gettext_noop("OP_EBADHEADER: A required header packet was not properly formatted, contained illegal values, or was missing altogether.") }, |
| 106 | { OP_EVERSION, gettext_noop("OP_EVERSION: The ID header contained an unrecognized version number.") }, |
| 107 | { OP_EBADPACKET, gettext_noop("OP_EBADPACKET: An audio packet failed to decode properly.") }, |
| 108 | { OP_EBADLINK, gettext_noop("OP_EBADLINK: We failed to find data we had seen before, or the bitstream structure was sufficiently malformed that seeking to the target destination was impossible.") }, |
| 109 | { OP_ENOSEEK, gettext_noop("OP_ENOSEEK: An operation that requires seeking was requested on an unseekable stream.") }, |
| 110 | { OP_EBADTIMESTAMP, gettext_noop("OP_EBADTIMESTAMP: The first or last granule position of a link failed basic validity checks.") }, |
| 111 | }; |
| 112 | |
| 113 | for(unsigned i = 0; i < sizeof(error_table) / sizeof(error_table[0]); i++) |
| 114 | { |
| 115 | if(error_table[i].code == error) |
| 116 | { |
| 117 | return _(error_table[i].str); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return _("Unknown"); |
| 122 | } |
| 123 | |
| 124 | OggOpusReader::OggOpusReader(Stream *fp) : fw(fp) |
| 125 | { |
| 126 | OpusFileCallbacks cb; |
| 127 | int error = 0; |
| 128 | |
| 129 | memset(&cb, 0, sizeof(cb)); |
| 130 | cb.read_func = iop_read_func; |
| 131 | cb.seek_func = iop_seek_func; |
| 132 | cb.close_func = iop_close_func; |
| 133 | cb.tell_func = iop_tell_func; |
| 134 | |
| 135 | fp->seek(0, SEEK_SET); |
| 136 | |
| 137 | if(!(opfile = op_open_callbacks((void*)fp, &cb, NULL, 0, &error))) |
| 138 | { |
| 139 | switch(error) |
| 140 | { |
| 141 | default: |
| 142 | throw MDFN_Error(0, _("opusfile: error code: %d(%s)", error, op_errstring(error))); |
| 143 | break; |
| 144 | |
| 145 | case OP_ENOTFORMAT: |
| 146 | throw(0); |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | OggOpusReader::~OggOpusReader() |
| 153 | { |
| 154 | op_free(opfile); |
| 155 | } |
| 156 | |
| 157 | int64 OggOpusReader::Read_(int16 *buffer, int64 frames) |
| 158 | { |
| 159 | int16 *tr_buffer = buffer; |
| 160 | int64 tr_count = frames * 2; |
| 161 | |
| 162 | while(tr_count > 0) |
| 163 | { |
| 164 | int64 didread = op_read(opfile, tr_buffer, tr_count, NULL); |
| 165 | |
| 166 | if(didread == 0) |
| 167 | break; |
| 168 | |
| 169 | tr_buffer += didread * 2; |
| 170 | tr_count -= didread * 2; |
| 171 | } |
| 172 | |
| 173 | return(frames - (tr_count / 2)); |
| 174 | } |
| 175 | |
| 176 | bool OggOpusReader::Seek_(int64 frame_offset) |
| 177 | { |
| 178 | op_pcm_seek(opfile, frame_offset); |
| 179 | return(true); |
| 180 | } |
| 181 | |
| 182 | int64 OggOpusReader::FrameCount(void) |
| 183 | { |
| 184 | return(op_pcm_total(pvfile, -1)); |
| 185 | } |