Main Page | Data Structures | File List | Data Fields | Globals

bs.c

Go to the documentation of this file.
00001 /* 
00002  * h264bitstream - a library for reading and writing H.264 video
00003  * Copyright (C) 2005-2006 Auroras Entertainment, LLC
00004  * 
00005  * Written by Alex Izvorski <aizvorski@gmail.com>
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #include <stdint.h>
00023 
00024 #include "bs.h"
00025 
00026 /****** bitstream operations *******/
00027 
00028 void     bs_init(bs_t* b, uint8_t* buf, int size)
00029 {
00030         b->start = buf;
00031         b->p = buf;
00032         b->end = buf + size;
00033         b->bits_left = 8;
00034 }
00035 
00036 uint32_t bs_byte_aligned(bs_t* b) { if (b->bits_left == 8) { return 1; } else { return 0; } }
00037 
00038 uint32_t bs_eof(bs_t* b) { if (b->p >= b->end) { return 1; } else { return 0; } }
00039 
00040 int bs_pos(bs_t* b) { return (b->p - b->start); }
00041 
00042 uint32_t bs_read_u1(bs_t* b)
00043 {
00044         uint32_t r = 0;
00045         if (bs_eof(b)) { return 0; }
00046         
00047         b->bits_left--;
00048         r = ((*(b->p)) >> b->bits_left) & 0x01;
00049 
00050         if (b->bits_left == 0) { b->p ++; b->bits_left = 8; }
00051 
00052         return r;
00053 }
00054 
00055 uint32_t bs_read_u(bs_t* b, int n)
00056 {
00057         uint32_t r = 0;
00058         int i;
00059         for (i = 0; i < n; i++)
00060         {
00061                 r |= ( bs_read_u1(b) << ( n - i - 1 ) );
00062         }
00063         return r;
00064 }
00065 
00066 uint32_t bs_read_f(bs_t* b, int n) { return bs_read_u(b, n); }
00067 
00068 uint32_t bs_read_u8(bs_t* b) { return bs_read_u(b, 8); }
00069 
00070 uint32_t bs_read_ue(bs_t* b)
00071 {
00072         int32_t r = 0;
00073         int i = 0;
00074 
00075     while( bs_read_u1(b) == 0 && i < 32 && !bs_eof(b) )
00076     {
00077         i++;
00078     }
00079         r = bs_read_u(b, i);
00080         r += (1 << i) - 1;
00081         return r;
00082 }
00083 
00084 int32_t bs_read_se(bs_t* b) 
00085 {
00086         int32_t r = bs_read_ue(b);
00087         if (r & 0x01)
00088         {
00089                 r = (r+1)/2;
00090         }
00091         else
00092         {
00093                 r = -(r/2);
00094         }
00095         return r;
00096 }
00097 
00098 
00099 void bs_write_u1(bs_t* b, uint32_t v)
00100 {
00101         if (bs_eof(b)) { return; }
00102         
00103         b->bits_left--;
00104         (*(b->p)) |= ((v & 0x01) << b->bits_left);
00105 
00106         if (b->bits_left == 0) { b->p ++; b->bits_left = 8; }
00107 }
00108 
00109 void bs_write_u(bs_t* b, int n, uint32_t v)
00110 {
00111         int i;
00112         for (i = 0; i < n; i++)
00113         {
00114                 bs_write_u1(b, (v >> ( n - i - 1 ))&0x01 );
00115         }
00116 }
00117 
00118 void bs_write_f(bs_t* b, int n, uint32_t v) { bs_write_u(b, n, v); }
00119 
00120 void bs_write_u8(bs_t* b, uint32_t v) { bs_write_u(b, 8, v); }
00121 
00122 void bs_write_ue(bs_t* b, uint32_t v)
00123 {
00124     static const int len_table[256] =
00125     {
00126         1,
00127                 1,
00128                 2,2,
00129                 3,3,3,3,
00130                 4,4,4,4,4,4,4,4,
00131                 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
00132         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00133                 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
00134         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00135                 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00136         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00137                 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
00138         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
00139                 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
00140         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
00141                 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
00142         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
00143                 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
00144         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
00145                 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
00146     };
00147 
00148     int len;
00149 
00150     if (v == 0)
00151     {
00152         bs_write_u1(b, 1);
00153     }
00154     else
00155     {
00156                 v++;
00157 
00158                 if (v >= 0x01000000)
00159                 {
00160                         len = 24 + len_table[ v >> 24 ];
00161                 }
00162         else if(v >= 0x00010000)
00163         {
00164             len = 16 + len_table[ v >> 16 ];
00165         }
00166         else if(v >= 0x00000100)
00167         {
00168             len =  8 + len_table[ v >>  8 ];
00169         }
00170         else 
00171         {
00172             len = len_table[ v ];
00173         }
00174 
00175         bs_write_u(b, 2*len-1, v);
00176     }
00177 }
00178 
00179 void bs_write_se(bs_t* b, int32_t v)
00180 {
00181         if (v <= 0)
00182         {
00183                 bs_write_ue(b, -v*2);
00184         }
00185         else
00186         {
00187                 bs_write_ue(b, v*2 - 1);
00188         }
00189 }
00190 

Generated on Sat Jul 22 22:05:00 2006 by  doxygen 1.4.4