Mercurial > libavcodec.hg
annotate rangecoder.h @ 5211:413c5e2eff68 libavcodec
move mpeg encoder specific initialization in the encoder specific file
| author | aurel |
|---|---|
| date | Thu, 05 Jul 2007 09:48:29 +0000 |
| parents | 3fd46e281bd8 |
| children | 2b72f9bc4f06 |
| rev | line source |
|---|---|
| 2334 | 1 /* |
| 2 * Range coder | |
| 3 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | |
| 4 * | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
5 * This file is part of FFmpeg. |
|
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
6 * |
|
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
| 2334 | 8 * modify it under the terms of the GNU Lesser General Public |
| 9 * License as published by the Free Software Foundation; either | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
| 2334 | 11 * |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
| 2334 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 * Lesser General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU Lesser General Public | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
|
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 2334 | 20 * |
| 21 */ | |
| 2967 | 22 |
| 2334 | 23 /** |
| 24 * @file rangecoder.h | |
| 25 * Range coder. | |
| 26 */ | |
| 27 | |
| 5163 | 28 #ifndef AVCODEC_RANGECODER_H |
| 29 #define AVCODEC_RANGECODER_H | |
| 30 | |
| 5162 | 31 #include <stdint.h> |
| 32 #include <assert.h> | |
| 33 #include "common.h" | |
| 34 | |
| 2334 | 35 typedef struct RangeCoder{ |
| 36 int low; | |
| 37 int range; | |
| 38 int outstanding_count; | |
| 39 int outstanding_byte; | |
| 40 uint8_t zero_state[256]; | |
| 41 uint8_t one_state[256]; | |
| 42 uint8_t *bytestream_start; | |
| 43 uint8_t *bytestream; | |
| 44 uint8_t *bytestream_end; | |
| 45 }RangeCoder; | |
| 46 | |
| 47 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size); | |
| 48 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size); | |
| 49 int ff_rac_terminate(RangeCoder *c); | |
| 50 void ff_build_rac_states(RangeCoder *c, int factor, int max_p); | |
| 51 | |
| 52 static inline void renorm_encoder(RangeCoder *c){ | |
| 53 //FIXME optimize | |
| 54 while(c->range < 0x100){ | |
| 55 if(c->outstanding_byte < 0){ | |
| 56 c->outstanding_byte= c->low>>8; | |
| 57 }else if(c->low <= 0xFF00){ | |
| 58 *c->bytestream++ = c->outstanding_byte; | |
| 59 for(;c->outstanding_count; c->outstanding_count--) | |
| 60 *c->bytestream++ = 0xFF; | |
| 61 c->outstanding_byte= c->low>>8; | |
| 62 }else if(c->low >= 0x10000){ | |
| 63 *c->bytestream++ = c->outstanding_byte + 1; | |
| 64 for(;c->outstanding_count; c->outstanding_count--) | |
| 65 *c->bytestream++ = 0x00; | |
| 66 c->outstanding_byte= (c->low>>8) & 0xFF; | |
| 67 }else{ | |
| 68 c->outstanding_count++; | |
| 69 } | |
| 2967 | 70 |
| 2334 | 71 c->low = (c->low & 0xFF)<<8; |
| 72 c->range <<= 8; | |
| 73 } | |
| 74 } | |
| 75 | |
| 5082 | 76 static inline int get_rac_count(RangeCoder *c){ |
| 77 int x= c->bytestream - c->bytestream_start + c->outstanding_count; | |
| 78 if(c->outstanding_byte >= 0) | |
| 79 x++; | |
| 80 return 8*x - av_log2(c->range); | |
| 81 } | |
| 82 | |
| 2334 | 83 static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){ |
| 84 int range1= (c->range * (*state)) >> 8; | |
| 85 | |
| 86 assert(*state); | |
| 87 assert(range1 < c->range); | |
| 88 assert(range1 > 0); | |
| 89 if(!bit){ | |
| 90 c->range -= range1; | |
| 91 *state= c->zero_state[*state]; | |
| 92 }else{ | |
| 93 c->low += c->range - range1; | |
| 94 c->range = range1; | |
| 95 *state= c->one_state[*state]; | |
| 96 } | |
| 2967 | 97 |
| 2334 | 98 renorm_encoder(c); |
| 99 } | |
| 100 | |
| 101 static inline void refill(RangeCoder *c){ | |
| 102 if(c->range < 0x100){ | |
| 103 c->range <<= 8; | |
| 104 c->low <<= 8; | |
| 105 if(c->bytestream < c->bytestream_end) | |
| 106 c->low+= c->bytestream[0]; | |
| 107 c->bytestream++; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 static inline int get_rac(RangeCoder *c, uint8_t * const state){ | |
| 112 int range1= (c->range * (*state)) >> 8; | |
|
5083
ce36118abbbb
rename attribute_unused to av_unused and moves its declaration to common.h
benoit
parents:
5082
diff
changeset
|
113 int av_unused one_mask; |
| 2967 | 114 |
| 2334 | 115 c->range -= range1; |
| 116 #if 1 | |
| 117 if(c->low < c->range){ | |
| 118 *state= c->zero_state[*state]; | |
| 119 refill(c); | |
| 120 return 0; | |
| 121 }else{ | |
| 122 c->low -= c->range; | |
| 123 *state= c->one_state[*state]; | |
| 124 c->range = range1; | |
| 125 refill(c); | |
| 126 return 1; | |
| 127 } | |
| 128 #else | |
| 129 one_mask= (c->range - c->low-1)>>31; | |
| 2967 | 130 |
| 2334 | 131 c->low -= c->range & one_mask; |
| 132 c->range += (range1 - c->range) & one_mask; | |
| 2967 | 133 |
| 2334 | 134 *state= c->zero_state[(*state) + (256&one_mask)]; |
| 2967 | 135 |
| 2334 | 136 refill(c); |
| 137 | |
| 138 return one_mask&1; | |
| 139 #endif | |
| 140 } | |
| 141 | |
|
5169
3fd46e281bd8
add a comment to indicate which #endif belong to which #define
gpoirier
parents:
5163
diff
changeset
|
142 #endif // AVCODEC_RANGECODER_H |
