2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
|
|
|
|
2015-07-20 20:18:57 +02:00
|
|
|
#ifndef VPX_DSP_BITWRITER_H_
|
|
|
|
#define VPX_DSP_BITWRITER_H_
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2011-07-19 15:17:25 +02:00
|
|
|
#include "vpx_ports/mem.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2015-07-17 23:09:05 +02:00
|
|
|
#include "vpx_dsp/prob.h"
|
2013-12-16 21:53:09 +01:00
|
|
|
|
2014-01-18 21:16:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
typedef struct vpx_writer {
|
2012-07-14 00:21:29 +02:00
|
|
|
unsigned int lowvalue;
|
|
|
|
unsigned int range;
|
|
|
|
int count;
|
|
|
|
unsigned int pos;
|
2013-05-01 01:39:07 +02:00
|
|
|
uint8_t *buffer;
|
2015-07-20 23:13:38 +02:00
|
|
|
} vpx_writer;
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
void vpx_start_encode(vpx_writer *bc, uint8_t *buffer);
|
|
|
|
void vpx_stop_encode(vpx_writer *bc);
|
2013-05-01 01:39:07 +02:00
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
static INLINE void vpx_write(vpx_writer *br, int bit, int probability) {
|
2012-07-14 00:21:29 +02:00
|
|
|
unsigned int split;
|
|
|
|
int count = br->count;
|
|
|
|
unsigned int range = br->range;
|
|
|
|
unsigned int lowvalue = br->lowvalue;
|
|
|
|
register unsigned int shift;
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
split = 1 + (((range - 1) * probability) >> 8);
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
range = split;
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (bit) {
|
|
|
|
lowvalue += split;
|
|
|
|
range = br->range - split;
|
|
|
|
}
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2015-07-20 23:04:21 +02:00
|
|
|
shift = vpx_norm[range];
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
range <<= shift;
|
|
|
|
count += shift;
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if (count >= 0) {
|
|
|
|
int offset = shift - count;
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
if ((lowvalue << (offset - 1)) & 0x80000000) {
|
|
|
|
int x = br->pos - 1;
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
while (x >= 0 && br->buffer[x] == 0xff) {
|
2013-05-01 01:39:07 +02:00
|
|
|
br->buffer[x] = 0;
|
2012-07-14 00:21:29 +02:00
|
|
|
x--;
|
|
|
|
}
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
br->buffer[x] += 1;
|
2011-07-19 15:17:25 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
br->buffer[br->pos++] = (lowvalue >> (24 - offset));
|
|
|
|
lowvalue <<= offset;
|
|
|
|
shift = count;
|
|
|
|
lowvalue &= 0xffffff;
|
|
|
|
count -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
lowvalue <<= shift;
|
|
|
|
br->count = count;
|
|
|
|
br->lowvalue = lowvalue;
|
|
|
|
br->range = range;
|
2011-07-19 15:17:25 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
static INLINE void vpx_write_bit(vpx_writer *w, int bit) {
|
|
|
|
vpx_write(w, bit, 128); // vpx_prob_half
|
2013-05-08 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
static INLINE void vpx_write_literal(vpx_writer *w, int data, int bits) {
|
2013-05-08 03:19:50 +02:00
|
|
|
int bit;
|
|
|
|
|
|
|
|
for (bit = bits - 1; bit >= 0; bit--)
|
2015-07-20 23:13:38 +02:00
|
|
|
vpx_write_bit(w, 1 & (data >> bit));
|
2013-05-08 03:19:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
#define vpx_write_prob(w, v) vpx_write_literal((w), (v), 8)
|
2013-05-08 03:19:50 +02:00
|
|
|
|
2014-01-18 21:16:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2015-07-20 20:18:57 +02:00
|
|
|
#endif // VPX_DSP_BITWRITER_H_
|