| 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 | // Descriptor preceding frames of 'ADU' MP3 streams (for improved loss-tolerance) |
| 19 | // Implementation |
| 20 | |
| 21 | #include "MP3ADUdescriptor.hh" |
| 22 | |
| 23 | ////////// ADUdescriptor ////////// |
| 24 | |
| 25 | //##### NOTE: For now, ignore fragmentation. Fix this later! ##### |
| 26 | |
| 27 | #define TWO_BYTE_DESCR_FLAG 0x40 |
| 28 | |
| 29 | unsigned ADUdescriptor::generateDescriptor(unsigned char*& toPtr, |
| 30 | unsigned remainingFrameSize) { |
| 31 | unsigned descriptorSize = ADUdescriptor::computeSize(remainingFrameSize); |
| 32 | switch (descriptorSize) { |
| 33 | case 1: { |
| 34 | *toPtr++ = (unsigned char)remainingFrameSize; |
| 35 | break; |
| 36 | } |
| 37 | case 2: { |
| 38 | generateTwoByteDescriptor(toPtr, remainingFrameSize); |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return descriptorSize; |
| 44 | } |
| 45 | |
| 46 | void ADUdescriptor::generateTwoByteDescriptor(unsigned char*& toPtr, |
| 47 | unsigned remainingFrameSize) { |
| 48 | *toPtr++ = (TWO_BYTE_DESCR_FLAG|(unsigned char)(remainingFrameSize>>8)); |
| 49 | *toPtr++ = (unsigned char)(remainingFrameSize&0xFF); |
| 50 | } |
| 51 | |
| 52 | unsigned ADUdescriptor::getRemainingFrameSize(unsigned char*& fromPtr) { |
| 53 | unsigned char firstByte = *fromPtr++; |
| 54 | |
| 55 | if (firstByte&TWO_BYTE_DESCR_FLAG) { |
| 56 | // This is a 2-byte descriptor |
| 57 | unsigned char secondByte = *fromPtr++; |
| 58 | |
| 59 | return ((firstByte&0x3F)<<8) | secondByte; |
| 60 | } else { |
| 61 | // This is a 1-byte descriptor |
| 62 | return (firstByte&0x3F); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |