2003-05-28 18:44:52 +00:00
|
|
|
/*
|
|
|
|
* H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
|
|
|
|
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2003-05-28 18:44:52 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 15:30:46 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2003-05-28 18:44:52 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2003-05-28 18:44:52 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 15:30:46 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 22:43:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-05-28 18:44:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2003-05-28 18:44:52 +00:00
|
|
|
* Context Adaptive Binary Arithmetic Coder.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-05-09 11:56:36 +00:00
|
|
|
#include "libavutil/common.h"
|
2014-01-26 00:39:33 +00:00
|
|
|
#include "libavutil/timer.h"
|
2009-04-13 16:20:26 +00:00
|
|
|
#include "get_bits.h"
|
2003-05-28 18:44:52 +00:00
|
|
|
#include "cabac.h"
|
2012-01-12 20:56:02 +00:00
|
|
|
#include "cabac_functions.h"
|
2003-05-28 18:44:52 +00:00
|
|
|
|
2014-08-31 10:42:51 +00:00
|
|
|
#include "cabac_tablegen.h"
|
2004-10-26 03:12:21 +00:00
|
|
|
|
2003-05-28 18:44:52 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param buf_size size of buf in bits
|
|
|
|
*/
|
|
|
|
void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){
|
2003-10-12 21:25:00 +00:00
|
|
|
init_put_bits(&c->pb, buf, buf_size);
|
2003-05-28 18:44:52 +00:00
|
|
|
|
|
|
|
c->low= 0;
|
|
|
|
c->range= 0x1FE;
|
|
|
|
c->outstanding_count= 0;
|
|
|
|
c->pb.bit_left++; //avoids firstBitFlag
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param buf_size size of buf in bits
|
|
|
|
*/
|
2004-05-18 17:09:46 +00:00
|
|
|
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
|
2005-12-17 18:14:38 +00:00
|
|
|
c->bytestream_start=
|
2003-05-28 18:44:52 +00:00
|
|
|
c->bytestream= buf;
|
2004-07-08 00:53:21 +00:00
|
|
|
c->bytestream_end= buf + buf_size;
|
2003-05-28 18:44:52 +00:00
|
|
|
|
2004-10-26 03:12:21 +00:00
|
|
|
#if CABAC_BITS == 16
|
|
|
|
c->low = (*c->bytestream++)<<18;
|
|
|
|
c->low+= (*c->bytestream++)<<10;
|
|
|
|
#else
|
|
|
|
c->low = (*c->bytestream++)<<10;
|
|
|
|
#endif
|
|
|
|
c->low+= ((*c->bytestream++)<<2) + 2;
|
2006-10-15 20:40:50 +00:00
|
|
|
c->range= 0x1FE;
|
2003-05-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
|
2012-08-26 03:44:46 +00:00
|
|
|
void ff_init_cabac_states(void)
|
|
|
|
{
|
2013-08-11 10:14:44 +00:00
|
|
|
static int initialized = 0;
|
|
|
|
|
|
|
|
if (initialized)
|
|
|
|
return;
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2014-08-31 10:42:51 +00:00
|
|
|
cabac_tableinit();
|
2013-08-11 10:14:44 +00:00
|
|
|
|
|
|
|
initialized = 1;
|
2003-05-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 23:53:51 +00:00
|
|
|
#ifdef TEST
|
2003-05-28 18:44:52 +00:00
|
|
|
#define SIZE 10240
|
2005-01-11 03:18:08 +00:00
|
|
|
|
2009-03-20 11:48:27 +00:00
|
|
|
#include "libavutil/lfg.h"
|
2005-01-11 03:18:08 +00:00
|
|
|
#include "avcodec.h"
|
|
|
|
|
2012-01-07 19:46:09 +00:00
|
|
|
static inline void put_cabac_bit(CABACContext *c, int b){
|
|
|
|
put_bits(&c->pb, 1, b);
|
|
|
|
for(;c->outstanding_count; c->outstanding_count--){
|
|
|
|
put_bits(&c->pb, 1, 1-b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void renorm_cabac_encoder(CABACContext *c){
|
|
|
|
while(c->range < 0x100){
|
|
|
|
//FIXME optimize
|
|
|
|
if(c->low<0x100){
|
|
|
|
put_cabac_bit(c, 0);
|
|
|
|
}else if(c->low<0x200){
|
|
|
|
c->outstanding_count++;
|
|
|
|
c->low -= 0x100;
|
|
|
|
}else{
|
|
|
|
put_cabac_bit(c, 1);
|
|
|
|
c->low -= 0x200;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->range+= c->range;
|
|
|
|
c->low += c->low;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 21:16:56 +00:00
|
|
|
static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
|
|
|
|
int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
|
|
|
|
|
|
|
|
if(bit == ((*state)&1)){
|
|
|
|
c->range -= RangeLPS;
|
2013-09-25 08:02:09 +00:00
|
|
|
*state = ff_h264_mlps_state[128 + *state];
|
2011-07-07 21:16:56 +00:00
|
|
|
}else{
|
|
|
|
c->low += c->range - RangeLPS;
|
|
|
|
c->range = RangeLPS;
|
2013-09-25 14:30:25 +00:00
|
|
|
*state= ff_h264_mlps_state[127 - *state];
|
2011-07-07 21:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renorm_cabac_encoder(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bit 0 -> write zero bit, !=0 write one bit
|
|
|
|
*/
|
|
|
|
static void put_cabac_bypass(CABACContext *c, int bit){
|
|
|
|
c->low += c->low;
|
|
|
|
|
|
|
|
if(bit){
|
|
|
|
c->low += c->range;
|
|
|
|
}
|
|
|
|
//FIXME optimize
|
|
|
|
if(c->low<0x200){
|
|
|
|
put_cabac_bit(c, 0);
|
|
|
|
}else if(c->low<0x400){
|
|
|
|
c->outstanding_count++;
|
|
|
|
c->low -= 0x200;
|
|
|
|
}else{
|
|
|
|
put_cabac_bit(c, 1);
|
|
|
|
c->low -= 0x400;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return the number of bytes written
|
|
|
|
*/
|
|
|
|
static int put_cabac_terminate(CABACContext *c, int bit){
|
|
|
|
c->range -= 2;
|
|
|
|
|
|
|
|
if(!bit){
|
|
|
|
renorm_cabac_encoder(c);
|
|
|
|
}else{
|
|
|
|
c->low += c->range;
|
|
|
|
c->range= 2;
|
|
|
|
|
|
|
|
renorm_cabac_encoder(c);
|
|
|
|
|
2012-07-07 10:33:51 +00:00
|
|
|
av_assert0(c->low <= 0x1FF);
|
2011-07-07 21:16:56 +00:00
|
|
|
put_cabac_bit(c, c->low>>9);
|
|
|
|
put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
|
|
|
|
|
|
|
|
flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
|
|
|
|
}
|
|
|
|
|
|
|
|
return (put_bits_count(&c->pb)+7)>>3;
|
|
|
|
}
|
|
|
|
|
2007-11-23 00:52:56 +00:00
|
|
|
int main(void){
|
2003-05-28 18:44:52 +00:00
|
|
|
CABACContext c;
|
|
|
|
uint8_t b[9*SIZE];
|
2003-05-30 01:05:48 +00:00
|
|
|
uint8_t r[9*SIZE];
|
2015-04-10 17:27:34 +00:00
|
|
|
int i, ret = 0;
|
2003-05-30 01:05:48 +00:00
|
|
|
uint8_t state[10]= {0};
|
2009-04-10 17:23:38 +00:00
|
|
|
AVLFG prng;
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2009-04-10 17:23:38 +00:00
|
|
|
av_lfg_init(&prng, 1);
|
2003-05-28 18:44:52 +00:00
|
|
|
ff_init_cabac_encoder(&c, b, SIZE);
|
2012-08-26 03:44:46 +00:00
|
|
|
ff_init_cabac_states();
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-05-28 18:44:52 +00:00
|
|
|
for(i=0; i<SIZE; i++){
|
2011-09-10 17:22:39 +00:00
|
|
|
if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
|
|
|
|
else r[i] = (i>>8)&1;
|
2003-05-28 18:44:52 +00:00
|
|
|
}
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-05-28 18:44:52 +00:00
|
|
|
for(i=0; i<SIZE; i++){
|
2003-05-30 01:05:48 +00:00
|
|
|
put_cabac_bypass(&c, r[i]&1);
|
2003-05-28 18:44:52 +00:00
|
|
|
}
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-05-28 18:44:52 +00:00
|
|
|
for(i=0; i<SIZE; i++){
|
2003-05-30 01:05:48 +00:00
|
|
|
put_cabac(&c, state, r[i]&1);
|
2003-05-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
put_cabac_terminate(&c, 1);
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-05-28 18:44:52 +00:00
|
|
|
ff_init_cabac_decoder(&c, b, SIZE);
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-05-30 01:05:48 +00:00
|
|
|
memset(state, 0, sizeof(state));
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-05-28 18:44:52 +00:00
|
|
|
for(i=0; i<SIZE; i++){
|
2015-04-10 17:27:34 +00:00
|
|
|
if( (r[i]&1) != get_cabac_bypass(&c) ) {
|
2005-01-11 03:18:08 +00:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
|
2015-04-10 17:27:34 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
2003-05-28 18:44:52 +00:00
|
|
|
}
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2003-05-28 18:44:52 +00:00
|
|
|
for(i=0; i<SIZE; i++){
|
2015-04-10 17:27:34 +00:00
|
|
|
if( (r[i]&1) != get_cabac_noinline(&c, state) ) {
|
2005-01-11 03:18:08 +00:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
|
2015-04-10 17:27:34 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
2003-05-28 18:44:52 +00:00
|
|
|
}
|
2015-04-10 17:27:34 +00:00
|
|
|
if(!get_cabac_terminate(&c)) {
|
2005-01-11 03:18:08 +00:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
|
2015-04-10 17:27:34 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2015-04-10 17:27:34 +00:00
|
|
|
return ret;
|
2003-05-28 18:44:52 +00:00
|
|
|
}
|
|
|
|
|
2008-01-20 23:53:51 +00:00
|
|
|
#endif /* TEST */
|