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