| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // A source object for AMR audio files (as defined in RFC 4867, section 5) |
| 19 | // Implementation |
| 20 | |
| 21 | #include "AMRAudioFileSource.hh" |
| 22 | #include "InputFile.hh" |
| 23 | #include "GroupsockHelper.hh" |
| 24 | |
| 25 | ////////// AMRAudioFileSource ////////// |
| 26 | |
| 27 | AMRAudioFileSource* |
| 28 | AMRAudioFileSource::createNew(UsageEnvironment& env, char const* fileName) { |
| 29 | FILE* fid = NULL; |
| 30 | Boolean magicNumberOK = True; |
| 31 | do { |
| 32 | |
| 33 | fid = OpenInputFile(env, fileName); |
| 34 | if (fid == NULL) break; |
| 35 | |
| 36 | // Now, having opened the input file, read the first few bytes, to |
| 37 | // check the required 'magic number': |
| 38 | magicNumberOK = False; // until we learn otherwise |
| 39 | Boolean isWideband = False; // by default |
| 40 | unsigned numChannels = 1; // by default |
| 41 | char buf[100]; |
| 42 | // Start with the first 6 bytes (the first 5 of which must be "#!AMR"): |
| 43 | if (fread(buf, 1, 6, fid) < 6) break; |
| 44 | if (strncmp(buf, "#!AMR" , 5) != 0) break; // bad magic # |
| 45 | unsigned bytesRead = 6; |
| 46 | |
| 47 | // The next bytes must be "\n", "-WB\n", "_MC1.0\n", or "-WB_MC1.0\n" |
| 48 | if (buf[5] == '-') { |
| 49 | // The next bytes must be "WB\n" or "WB_MC1.0\n" |
| 50 | if (fread(&buf[bytesRead], 1, 3, fid) < 3) break; |
| 51 | if (strncmp(&buf[bytesRead], "WB" , 2) != 0) break; // bad magic # |
| 52 | isWideband = True; |
| 53 | bytesRead += 3; |
| 54 | } |
| 55 | if (buf[bytesRead-1] == '_') { |
| 56 | // The next bytes must be "MC1.0\n" |
| 57 | if (fread(&buf[bytesRead], 1, 6, fid) < 6) break; |
| 58 | if (strncmp(&buf[bytesRead], "MC1.0\n" , 6) != 0) break; // bad magic # |
| 59 | bytesRead += 6; |
| 60 | |
| 61 | // The next 4 bytes contain the number of channels: |
| 62 | char channelDesc[4]; |
| 63 | if (fread(channelDesc, 1, 4, fid) < 4) break; |
| 64 | numChannels = channelDesc[3]&0xF; |
| 65 | } else if (buf[bytesRead-1] != '\n') { |
| 66 | break; // bad magic # |
| 67 | } |
| 68 | |
| 69 | // If we get here, the magic number was OK: |
| 70 | magicNumberOK = True; |
| 71 | |
| 72 | #ifdef DEBUG |
| 73 | fprintf(stderr, "isWideband: %d, numChannels: %d\n" , |
| 74 | isWideband, numChannels); |
| 75 | #endif |
| 76 | return new AMRAudioFileSource(env, fid, isWideband, numChannels); |
| 77 | } while (0); |
| 78 | |
| 79 | // An error occurred: |
| 80 | CloseInputFile(fid); |
| 81 | if (!magicNumberOK) { |
| 82 | env.setResultMsg("Bad (or nonexistent) AMR file header" ); |
| 83 | } |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | AMRAudioFileSource |
| 88 | ::AMRAudioFileSource(UsageEnvironment& env, FILE* fid, |
| 89 | Boolean isWideband, unsigned numChannels) |
| 90 | : AMRAudioSource(env, isWideband, numChannels), |
| 91 | fFid(fid) { |
| 92 | } |
| 93 | |
| 94 | AMRAudioFileSource::~AMRAudioFileSource() { |
| 95 | CloseInputFile(fFid); |
| 96 | } |
| 97 | |
| 98 | // The mapping from the "FT" field to frame size. |
| 99 | // Values of 65535 are invalid. |
| 100 | #define FT_INVALID 65535 |
| 101 | static unsigned short const frameSize[16] = { |
| 102 | 12, 13, 15, 17, |
| 103 | 19, 20, 26, 31, |
| 104 | 5, FT_INVALID, FT_INVALID, FT_INVALID, |
| 105 | FT_INVALID, FT_INVALID, FT_INVALID, 0 |
| 106 | }; |
| 107 | static unsigned short const frameSizeWideband[16] = { |
| 108 | 17, 23, 32, 36, |
| 109 | 40, 46, 50, 58, |
| 110 | 60, 5, FT_INVALID, FT_INVALID, |
| 111 | FT_INVALID, FT_INVALID, 0, 0 |
| 112 | }; |
| 113 | |
| 114 | // Note: We should change the following to use asynchronous file reading, ##### |
| 115 | // as we now do with ByteStreamFileSource. ##### |
| 116 | void AMRAudioFileSource::doGetNextFrame() { |
| 117 | if (feof(fFid) || ferror(fFid)) { |
| 118 | handleClosure(); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // Begin by reading the 1-byte frame header (and checking it for validity) |
| 123 | while (1) { |
| 124 | if (fread(&fLastFrameHeader, 1, 1, fFid) < 1) { |
| 125 | handleClosure(); |
| 126 | return; |
| 127 | } |
| 128 | if ((fLastFrameHeader&0x83) != 0) { |
| 129 | #ifdef DEBUG |
| 130 | fprintf(stderr, "Invalid frame header 0x%02x (padding bits (0x83) are not zero)\n" , fLastFrameHeader); |
| 131 | #endif |
| 132 | } else { |
| 133 | unsigned char ft = (fLastFrameHeader&0x78)>>3; |
| 134 | fFrameSize = fIsWideband ? frameSizeWideband[ft] : frameSize[ft]; |
| 135 | if (fFrameSize == FT_INVALID) { |
| 136 | #ifdef DEBUG |
| 137 | fprintf(stderr, "Invalid FT field %d (from frame header 0x%02x)\n" , |
| 138 | ft, fLastFrameHeader); |
| 139 | #endif |
| 140 | } else { |
| 141 | // The frame header is OK |
| 142 | #ifdef DEBUG |
| 143 | fprintf(stderr, "Valid frame header 0x%02x -> ft %d -> frame size %d\n" , fLastFrameHeader, ft, fFrameSize); |
| 144 | #endif |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Next, read the frame-block into the buffer provided: |
| 151 | fFrameSize *= fNumChannels; // because multiple channels make up a frame-block |
| 152 | if (fFrameSize > fMaxSize) { |
| 153 | fNumTruncatedBytes = fFrameSize - fMaxSize; |
| 154 | fFrameSize = fMaxSize; |
| 155 | } |
| 156 | fFrameSize = fread(fTo, 1, fFrameSize, fFid); |
| 157 | |
| 158 | // Set the 'presentation time': |
| 159 | if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) { |
| 160 | // This is the first frame, so use the current time: |
| 161 | gettimeofday(&fPresentationTime, NULL); |
| 162 | } else { |
| 163 | // Increment by the play time of the previous frame (20 ms) |
| 164 | unsigned uSeconds = fPresentationTime.tv_usec + 20000; |
| 165 | fPresentationTime.tv_sec += uSeconds/1000000; |
| 166 | fPresentationTime.tv_usec = uSeconds%1000000; |
| 167 | } |
| 168 | |
| 169 | fDurationInMicroseconds = 20000; // each frame is 20 ms |
| 170 | |
| 171 | // Switch to another task, and inform the reader that he has data: |
| 172 | nextTask() = envir().taskScheduler().scheduleDelayedTask(0, |
| 173 | (TaskFunc*)FramedSource::afterGetting, this); |
| 174 | } |
| 175 | |