rtp_utils.c

Go to the documentation of this file.
00001 /*
00002  *
00003  * This file is part of FFmpeg.
00004  *
00005  * FFmpeg is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * FFmpeg is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with FFmpeg; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00018  */
00019 
00025 #include <stdlib.h>
00026 #include <stdint.h>
00027 #include <string.h>
00028 
00029 static const uint8_t map2[] =
00030 {
00031     0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
00032     0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
00033     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
00034     0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
00035     0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
00036     0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
00037     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b,
00038     0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
00039     0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
00040     0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33
00041 };
00042 
00049 int nms_base64_decode(uint8_t * out, const char *in, int out_length)
00050 {
00051     int i, v;
00052     uint8_t *dst = out;
00053 
00054     v = 0;
00055     for (i = 0; in[i] && in[i] != '='; i++) {
00056         unsigned int index= in[i]-43;
00057         if (index>=(sizeof(map2)/sizeof(map2[0])) || map2[index] == 0xff)
00058             return -1;
00059         v = (v << 6) + map2[index];
00060         if (i & 3) {
00061             if (dst - out < out_length) {
00062                 *dst++ = v >> (6 - 2 * (i & 3));
00063             }
00064         } 
00065     }
00066 
00067     return (dst - out);
00068 }
00069 
00075 int nms_hex_decode(uint8_t * out, const char *in, int out_length)
00076 {
00077     int i, c = 0;
00078     uint8_t *dst = out;
00079 
00080     for (i = 0; in[i]; i++) {
00081         if (in[i] >= '0' && in[i] <= '9')
00082             c += in[i] - '0';
00083         else if (in[i] >= 'a' && in[i] <= 'f')
00084             c += in[i] - 'a' + 10;
00085         else if (in[i] >= 'A' && in[i] <= 'F')
00086             c += in[i] - 'A' + 10;
00087         else
00088             return -1;
00089 
00090         if (i % 2 && (dst - out < out_length)) {
00091             *dst++ = c;
00092         }
00093         c = c << 4 & 0xFF;
00094     }
00095 
00096     return (dst - out);
00097 }
00098 
00099 /*
00100  * xiph/mkv variable length encoder
00101  * @param s destination
00102  * @param v value
00103  * @return the number of byte written
00104  */
00105 unsigned int nms_xiphlacing(unsigned char *s, unsigned int v)
00106 {
00107     unsigned int n = 0;
00108 
00109     while(v >= 0xff) {
00110         *s++ = 0xff;
00111         v -= 0xff;
00112         n++;
00113     }
00114     *s = v;
00115     n++;
00116     return n;
00117 }
00118 
00119 uint64_t nms_consume_BE(uint8_t ** buff, uint8_t n_bytes)
00120 {
00121     uint64_t v = 0;
00122     uint8_t left = n_bytes;
00123 
00124     while (left)
00125         v |= *((uint8_t*)(*buff)++) << ((--left)*8);
00126 
00127     return v;    
00128 }
00129 
00130 uint32_t nms_consume_BE4(uint8_t ** buff)
00131 {
00132     uint32_t v = 0;
00133     uint8_t * buff_p = *buff;
00134    
00135     v = (buff_p[3])|(buff_p[2] << 8)|(buff_p[1] << 16)|(buff_p[0] << 24);
00136     *buff += 4;
00137 
00138     return v;
00139 }
00140 
00141 uint32_t nms_consume_BE3(uint8_t ** buff)
00142 {
00143     uint32_t v = 0;
00144     uint8_t * buff_p = *buff;
00145    
00146     v = (buff_p[2])|(buff_p[1] << 8)|(buff_p[0] << 16);
00147     *buff += 3;
00148 
00149     return v;
00150 }
00151 
00152 
00153 uint16_t nms_consume_BE2(uint8_t ** buff)
00154 {
00155     uint16_t v = 0;
00156     uint8_t * buff_p = *buff;
00157    
00158     v = (buff_p[1])|(buff_p[0] << 8);
00159     *buff += 2;
00160 
00161     return v;
00162 }
00163 
00169 int nms_get_attr_value(char *attr, const char *param, char *v, int v_len )
00170 {
00171     char *value, *tmp;
00172     int len;
00173     if ((value = strstr(attr, param)) &&
00174         (*(value += strlen(param)) == '=')) {
00175             value++;
00176             strncpy(v, value, v_len - 1);
00177             v[v_len - 1] = '\0';
00178             if ((tmp = strstr(v,";"))) {
00179                 len = tmp - v;
00180                 v[len] = '\0';
00181             } else {
00182                 len = strlen(v);
00183             }
00184         return len;
00185     }
00186     return 0;
00187 }
00188 
00192 int nms_alloc_data(uint8_t **buf, long *cur_len, long new_len) {
00193     if (buf && cur_len && *cur_len < new_len) {
00194         if (!(*buf = realloc(*buf, new_len))) {
00195             return -1;
00196         }
00197         *cur_len = new_len;
00198     }
00199     return 0;
00200 }
00201 
00202 void nms_append_data(uint8_t *dst, long offset, uint8_t *src, long len) {
00203     memcpy(dst + offset, src, len);
00204 }
00205 
00206 void nms_append_incr(uint8_t *dst, long *offset, uint8_t *src, long len) {
00207     nms_append_data(dst, *offset, src, len);
00208     *offset += len;
00209 }

Generated on Tue Feb 3 03:10:02 2009 for libnemesi by  doxygen 1.5.4