ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:54:45 2017 +0800
Revision:
47:9e361da97763
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 47:9e361da97763 1 #ifndef RDA_MP4_H
TMBOY 47:9e361da97763 2 #define RDA_MP4_H
TMBOY 47:9e361da97763 3
TMBOY 47:9e361da97763 4 #ifdef __cplusplus
TMBOY 47:9e361da97763 5 extern "C" {
TMBOY 47:9e361da97763 6 #endif /* __cplusplus */
TMBOY 47:9e361da97763 7
TMBOY 47:9e361da97763 8 #include "mp4ff_int_types.h"
TMBOY 47:9e361da97763 9 #include <stdlib.h>
TMBOY 47:9e361da97763 10 #include "rda_log.h"
TMBOY 47:9e361da97763 11
TMBOY 47:9e361da97763 12 #define INLINE __inline
TMBOY 47:9e361da97763 13 //#define NULL ((void *) 0)
TMBOY 47:9e361da97763 14
TMBOY 47:9e361da97763 15 #define SBR_DEC
TMBOY 47:9e361da97763 16 //#define PS_DEC
TMBOY 47:9e361da97763 17
TMBOY 47:9e361da97763 18 #define MAIN 1
TMBOY 47:9e361da97763 19 #define LC 2
TMBOY 47:9e361da97763 20 #define SSR 3
TMBOY 47:9e361da97763 21 #define LTP 4
TMBOY 47:9e361da97763 22 #define HE_AAC 5
TMBOY 47:9e361da97763 23 #define LD 23
TMBOY 47:9e361da97763 24 #define ER_LC 17
TMBOY 47:9e361da97763 25 #define ER_LTP 19
TMBOY 47:9e361da97763 26 #define DRM_ER_LC 27 /* special object type for DRM */
TMBOY 47:9e361da97763 27
TMBOY 47:9e361da97763 28 /* header types */
TMBOY 47:9e361da97763 29 #define RAW 0
TMBOY 47:9e361da97763 30 #define ADIF 1
TMBOY 47:9e361da97763 31 #define ADTS 2
TMBOY 47:9e361da97763 32 #define LATM 3
TMBOY 47:9e361da97763 33
TMBOY 47:9e361da97763 34 /* SBR signalling */
TMBOY 47:9e361da97763 35 #define NO_SBR 0
TMBOY 47:9e361da97763 36 #define SBR_UPSAMPLED 1
TMBOY 47:9e361da97763 37 #define SBR_DOWNSAMPLED 2
TMBOY 47:9e361da97763 38 #define NO_SBR_UPSAMPLED 3
TMBOY 47:9e361da97763 39
TMBOY 47:9e361da97763 40 /* First object type that has ER */
TMBOY 47:9e361da97763 41 #define ER_OBJECT_START 17
TMBOY 47:9e361da97763 42
TMBOY 47:9e361da97763 43 typedef struct _bitfile
TMBOY 47:9e361da97763 44 {
TMBOY 47:9e361da97763 45 /* bit input */
TMBOY 47:9e361da97763 46 uint32_t bufa;
TMBOY 47:9e361da97763 47 uint32_t bufb;
TMBOY 47:9e361da97763 48 uint32_t bits_left;
TMBOY 47:9e361da97763 49 uint32_t buffer_size; /* size of the buffer in bytes */
TMBOY 47:9e361da97763 50 uint32_t bytes_left;
TMBOY 47:9e361da97763 51 uint8_t error;
TMBOY 47:9e361da97763 52 uint32_t *tail;
TMBOY 47:9e361da97763 53 uint32_t *start;
TMBOY 47:9e361da97763 54 const void *buffer;
TMBOY 47:9e361da97763 55 } bitfile;
TMBOY 47:9e361da97763 56
TMBOY 47:9e361da97763 57 typedef struct mp4AudioSpecificConfig
TMBOY 47:9e361da97763 58 {
TMBOY 47:9e361da97763 59 /* Audio Specific Info */
TMBOY 47:9e361da97763 60 unsigned char objectTypeIndex;
TMBOY 47:9e361da97763 61 unsigned char samplingFrequencyIndex;
TMBOY 47:9e361da97763 62 unsigned long samplingFrequency;
TMBOY 47:9e361da97763 63 unsigned char channelsConfiguration;
TMBOY 47:9e361da97763 64
TMBOY 47:9e361da97763 65 /* GA Specific Info */
TMBOY 47:9e361da97763 66 unsigned char frameLengthFlag;
TMBOY 47:9e361da97763 67 unsigned char dependsOnCoreCoder;
TMBOY 47:9e361da97763 68 unsigned short coreCoderDelay;
TMBOY 47:9e361da97763 69 unsigned char extensionFlag;
TMBOY 47:9e361da97763 70 unsigned char aacSectionDataResilienceFlag;
TMBOY 47:9e361da97763 71 unsigned char aacScalefactorDataResilienceFlag;
TMBOY 47:9e361da97763 72 unsigned char aacSpectralDataResilienceFlag;
TMBOY 47:9e361da97763 73 unsigned char epConfig;
TMBOY 47:9e361da97763 74
TMBOY 47:9e361da97763 75 char sbr_present_flag;
TMBOY 47:9e361da97763 76 char ps_present_flag;
TMBOY 47:9e361da97763 77 char forceUpSampling;
TMBOY 47:9e361da97763 78 char downSampledSBR;
TMBOY 47:9e361da97763 79 } mp4AudioSpecificConfig;
TMBOY 47:9e361da97763 80
TMBOY 47:9e361da97763 81 /* circumvent memory alignment errors on ARM */
TMBOY 47:9e361da97763 82 static INLINE uint32_t getdword(void *mem)
TMBOY 47:9e361da97763 83 {
TMBOY 47:9e361da97763 84 uint32_t tmp;
TMBOY 47:9e361da97763 85 #ifndef ARCH_IS_BIG_ENDIAN
TMBOY 47:9e361da97763 86 ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[3];
TMBOY 47:9e361da97763 87 ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[2];
TMBOY 47:9e361da97763 88 ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[1];
TMBOY 47:9e361da97763 89 ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[0];
TMBOY 47:9e361da97763 90 #else
TMBOY 47:9e361da97763 91 ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[0];
TMBOY 47:9e361da97763 92 ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[1];
TMBOY 47:9e361da97763 93 ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[2];
TMBOY 47:9e361da97763 94 ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[3];
TMBOY 47:9e361da97763 95 #endif
TMBOY 47:9e361da97763 96
TMBOY 47:9e361da97763 97 return tmp;
TMBOY 47:9e361da97763 98 }
TMBOY 47:9e361da97763 99
TMBOY 47:9e361da97763 100 /* reads only n bytes from the stream instead of the standard 4 */
TMBOY 47:9e361da97763 101 static /*INLINE*/ uint32_t getdword_n(void *mem, int n)
TMBOY 47:9e361da97763 102 {
TMBOY 47:9e361da97763 103 uint32_t tmp = 0;
TMBOY 47:9e361da97763 104 #ifndef ARCH_IS_BIG_ENDIAN
TMBOY 47:9e361da97763 105 switch (n)
TMBOY 47:9e361da97763 106 {
TMBOY 47:9e361da97763 107 case 3:
TMBOY 47:9e361da97763 108 ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[2];
TMBOY 47:9e361da97763 109 case 2:
TMBOY 47:9e361da97763 110 ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[1];
TMBOY 47:9e361da97763 111 case 1:
TMBOY 47:9e361da97763 112 ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[0];
TMBOY 47:9e361da97763 113 default:
TMBOY 47:9e361da97763 114 break;
TMBOY 47:9e361da97763 115 }
TMBOY 47:9e361da97763 116 #else
TMBOY 47:9e361da97763 117 switch (n)
TMBOY 47:9e361da97763 118 {
TMBOY 47:9e361da97763 119 case 3:
TMBOY 47:9e361da97763 120 ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[2];
TMBOY 47:9e361da97763 121 case 2:
TMBOY 47:9e361da97763 122 ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[1];
TMBOY 47:9e361da97763 123 case 1:
TMBOY 47:9e361da97763 124 ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[0];
TMBOY 47:9e361da97763 125 default:
TMBOY 47:9e361da97763 126 break;
TMBOY 47:9e361da97763 127 }
TMBOY 47:9e361da97763 128 #endif
TMBOY 47:9e361da97763 129
TMBOY 47:9e361da97763 130 return tmp;
TMBOY 47:9e361da97763 131 }
TMBOY 47:9e361da97763 132
TMBOY 47:9e361da97763 133 static INLINE uint32_t faad_showbits(bitfile *ld, uint32_t bits)
TMBOY 47:9e361da97763 134 {
TMBOY 47:9e361da97763 135 if (bits <= ld->bits_left)
TMBOY 47:9e361da97763 136 {
TMBOY 47:9e361da97763 137 //return (ld->bufa >> (ld->bits_left - bits)) & bitmask[bits];
TMBOY 47:9e361da97763 138 return (ld->bufa << (32 - ld->bits_left)) >> (32 - bits);
TMBOY 47:9e361da97763 139 }
TMBOY 47:9e361da97763 140
TMBOY 47:9e361da97763 141 bits -= ld->bits_left;
TMBOY 47:9e361da97763 142 //return ((ld->bufa & bitmask[ld->bits_left]) << bits) | (ld->bufb >> (32 - bits));
TMBOY 47:9e361da97763 143 return ((ld->bufa & ((1<<ld->bits_left)-1)) << bits) | (ld->bufb >> (32 - bits));
TMBOY 47:9e361da97763 144 }
TMBOY 47:9e361da97763 145
TMBOY 47:9e361da97763 146 static /*INLINE*/ void faad_flushbits_ex(bitfile *ld, uint32_t bits)
TMBOY 47:9e361da97763 147 {
TMBOY 47:9e361da97763 148 uint32_t tmp;
TMBOY 47:9e361da97763 149
TMBOY 47:9e361da97763 150 ld->bufa = ld->bufb;
TMBOY 47:9e361da97763 151 if (ld->bytes_left >= 4)
TMBOY 47:9e361da97763 152 {
TMBOY 47:9e361da97763 153 tmp = getdword(ld->tail);
TMBOY 47:9e361da97763 154 ld->bytes_left -= 4;
TMBOY 47:9e361da97763 155 } else {
TMBOY 47:9e361da97763 156 tmp = getdword_n(ld->tail, ld->bytes_left);
TMBOY 47:9e361da97763 157 ld->bytes_left = 0;
TMBOY 47:9e361da97763 158 }
TMBOY 47:9e361da97763 159 ld->bufb = tmp;
TMBOY 47:9e361da97763 160 ld->tail++;
TMBOY 47:9e361da97763 161 ld->bits_left += (32 - bits);
TMBOY 47:9e361da97763 162 }
TMBOY 47:9e361da97763 163
TMBOY 47:9e361da97763 164 static INLINE void faad_flushbits(bitfile *ld, uint32_t bits)
TMBOY 47:9e361da97763 165 {
TMBOY 47:9e361da97763 166 /* do nothing if error */
TMBOY 47:9e361da97763 167 if (ld->error != 0)
TMBOY 47:9e361da97763 168 return;
TMBOY 47:9e361da97763 169
TMBOY 47:9e361da97763 170 if (bits < ld->bits_left)
TMBOY 47:9e361da97763 171 {
TMBOY 47:9e361da97763 172 ld->bits_left -= bits;
TMBOY 47:9e361da97763 173 } else {
TMBOY 47:9e361da97763 174 faad_flushbits_ex(ld, bits);
TMBOY 47:9e361da97763 175 }
TMBOY 47:9e361da97763 176 }
TMBOY 47:9e361da97763 177
TMBOY 47:9e361da97763 178 /* return next n bits (right adjusted) */
TMBOY 47:9e361da97763 179 static /*INLINE*/ uint32_t faad_getbits(bitfile *ld, uint32_t n)
TMBOY 47:9e361da97763 180 {
TMBOY 47:9e361da97763 181 uint32_t ret;
TMBOY 47:9e361da97763 182
TMBOY 47:9e361da97763 183 if (n == 0)
TMBOY 47:9e361da97763 184 return 0;
TMBOY 47:9e361da97763 185
TMBOY 47:9e361da97763 186 ret = faad_showbits(ld, n);
TMBOY 47:9e361da97763 187 faad_flushbits(ld, n);
TMBOY 47:9e361da97763 188
TMBOY 47:9e361da97763 189 return ret;
TMBOY 47:9e361da97763 190 }
TMBOY 47:9e361da97763 191
TMBOY 47:9e361da97763 192 static INLINE uint8_t faad_get1bit(bitfile *ld)
TMBOY 47:9e361da97763 193 {
TMBOY 47:9e361da97763 194 uint8_t r;
TMBOY 47:9e361da97763 195
TMBOY 47:9e361da97763 196 if (ld->bits_left > 0)
TMBOY 47:9e361da97763 197 {
TMBOY 47:9e361da97763 198 ld->bits_left--;
TMBOY 47:9e361da97763 199 r = (uint8_t)((ld->bufa >> ld->bits_left) & 1);
TMBOY 47:9e361da97763 200 return r;
TMBOY 47:9e361da97763 201 }
TMBOY 47:9e361da97763 202
TMBOY 47:9e361da97763 203 /* bits_left == 0 */
TMBOY 47:9e361da97763 204 #if 0
TMBOY 47:9e361da97763 205 r = (uint8_t)(ld->bufb >> 31);
TMBOY 47:9e361da97763 206 faad_flushbits_ex(ld, 1);
TMBOY 47:9e361da97763 207 #else
TMBOY 47:9e361da97763 208 r = (uint8_t)faad_getbits(ld, 1);
TMBOY 47:9e361da97763 209 #endif
TMBOY 47:9e361da97763 210 return r;
TMBOY 47:9e361da97763 211 }
TMBOY 47:9e361da97763 212
TMBOY 47:9e361da97763 213 void faad_initbits(bitfile *ld, const void *_buffer, const uint32_t buffer_size);
TMBOY 47:9e361da97763 214 uint8_t faad_byte_align(bitfile *ld);
TMBOY 47:9e361da97763 215 void faad_endbits(bitfile *ld);
TMBOY 47:9e361da97763 216 uint32_t faad_get_processed_bits(bitfile *ld);
TMBOY 47:9e361da97763 217 uint32_t get_sample_rate(const uint8_t sr_index);
TMBOY 47:9e361da97763 218 int8_t rda_GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC);
TMBOY 47:9e361da97763 219 int8_t rda_AudioSpecificConfigFromBitfile(bitfile *ld, mp4AudioSpecificConfig *mp4ASC, uint32_t buffer_size, uint8_t short_form);
TMBOY 47:9e361da97763 220 int8_t rda_AudioSpecificConfig2(uint8_t *pBuffer, uint32_t buffer_size, mp4AudioSpecificConfig *mp4ASC, uint8_t short_form);
TMBOY 47:9e361da97763 221 int32_t rda_MakeAdtsHeader(unsigned char *data, int size, mp4AudioSpecificConfig *mp4ASC);
TMBOY 47:9e361da97763 222 uint32_t read_callback(void *user_data, void *buffer, uint32_t length);
TMBOY 47:9e361da97763 223 uint32_t seek_callback(void *user_data, uint64_t position);
TMBOY 47:9e361da97763 224
TMBOY 47:9e361da97763 225 #ifdef __cplusplus
TMBOY 47:9e361da97763 226 }
TMBOY 47:9e361da97763 227 #endif /* __cplusplus */
TMBOY 47:9e361da97763 228
TMBOY 47:9e361da97763 229 #endif
TMBOY 47:9e361da97763 230