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
|
|
|
*/
|
|
|
|
|
2013-12-20 20:10:24 +01:00
|
|
|
#ifndef VP9_ENCODER_VP9_WRITER_H_
|
|
|
|
#define VP9_ENCODER_VP9_WRITER_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
|
|
|
|
2013-12-16 21:53:09 +01:00
|
|
|
#include "vp9/common/vp9_prob.h"
|
|
|
|
|
2014-01-18 21:16:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
typedef struct {
|
|
|
|
unsigned int lowvalue;
|
|
|
|
unsigned int range;
|
|
|
|
unsigned int value;
|
|
|
|
int count;
|
|
|
|
unsigned int pos;
|
2013-05-01 01:39:07 +02:00
|
|
|
uint8_t *buffer;
|
2012-07-14 00:21:29 +02:00
|
|
|
|
|
|
|
// Variables used to track bit costs without outputing to the bitstream
|
|
|
|
unsigned int measure_cost;
|
2013-12-20 20:10:24 +01:00
|
|
|
uint64_t bit_counter;
|
2013-05-08 03:19:50 +02:00
|
|
|
} vp9_writer;
|
2011-07-19 15:17:25 +02:00
|
|
|
|
2012-10-31 01:12:12 +01:00
|
|
|
extern const unsigned int vp9_prob_cost[256];
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2013-05-01 01:39:07 +02:00
|
|
|
void vp9_start_encode(vp9_writer *bc, uint8_t *buffer);
|
|
|
|
void vp9_stop_encode(vp9_writer *bc);
|
|
|
|
|
2013-05-08 03:19:50 +02:00
|
|
|
static void vp9_write(vp9_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
|
|
|
|
2012-10-31 01:12:12 +01:00
|
|
|
shift = vp9_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
|
|
|
}
|
|
|
|
|
2013-05-08 03:19:50 +02:00
|
|
|
static void vp9_write_bit(vp9_writer *w, int bit) {
|
|
|
|
vp9_write(w, bit, 128); // vp9_prob_half
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vp9_write_literal(vp9_writer *w, int data, int bits) {
|
|
|
|
int bit;
|
|
|
|
|
|
|
|
for (bit = bits - 1; bit >= 0; bit--)
|
|
|
|
vp9_write_bit(w, 1 & (data >> bit));
|
|
|
|
}
|
|
|
|
|
2013-12-11 03:13:29 +01:00
|
|
|
#define vp9_write_prob(w, v) vp9_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
|
|
|
|
|
2013-12-20 20:10:24 +01:00
|
|
|
#endif // VP9_ENCODER_VP9_WRITER_H_
|