1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec/cabac: Move encoder related stuff to libavcodec/tests/cabac.c

(This is actually the second time the encoder stuff is removed;
the first was in 8b4119187b62d6932e07aded11d33d3b24e1b42f.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
This commit is contained in:
Andreas Rheinhardt 2021-01-29 03:44:34 +01:00
parent 455d3a7684
commit 8c01eb0a31
3 changed files with 59 additions and 51 deletions

View File

@ -158,18 +158,6 @@ DECLARE_ASM_ALIGNED(1, const uint8_t, ff_h264_cabac_tables)[512 + 4*2*64 + 4*64
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
}; };
/**
* @param buf_size size of buf in bits
*/
void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){
init_put_bits(&c->pb, buf, buf_size);
c->low= 0;
c->range= 0x1FE;
c->outstanding_count= 0;
c->pb.bit_left++; //avoids firstBitFlag
}
/** /**
* *
* @param buf_size size of buf in bits * @param buf_size size of buf in bits

View File

@ -29,8 +29,6 @@
#include <stdint.h> #include <stdint.h>
#include "put_bits.h"
extern const uint8_t ff_h264_cabac_tables[512 + 4*2*64 + 4*64 + 63]; extern const uint8_t ff_h264_cabac_tables[512 + 4*2*64 + 4*64 + 63];
#define H264_NORM_SHIFT_OFFSET 0 #define H264_NORM_SHIFT_OFFSET 0
#define H264_LPS_RANGE_OFFSET 512 #define H264_LPS_RANGE_OFFSET 512
@ -43,14 +41,11 @@ extern const uint8_t ff_h264_cabac_tables[512 + 4*2*64 + 4*64 + 63];
typedef struct CABACContext{ typedef struct CABACContext{
int low; int low;
int range; int range;
int outstanding_count;
const uint8_t *bytestream_start; const uint8_t *bytestream_start;
const uint8_t *bytestream; const uint8_t *bytestream;
const uint8_t *bytestream_end; const uint8_t *bytestream_end;
PutBitContext pb;
}CABACContext; }CABACContext;
void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size); int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
#endif /* AVCODEC_CABAC_H */ #endif /* AVCODEC_CABAC_H */

View File

@ -24,41 +24,51 @@
#include "libavutil/lfg.h" #include "libavutil/lfg.h"
#include "libavcodec/avcodec.h" #include "libavcodec/avcodec.h"
#include "libavcodec/put_bits.h"
static inline void put_cabac_bit(CABACContext *c, int b){ typedef struct CABACTestContext {
CABACContext dec;
int outstanding_count;
PutBitContext pb;
} CABACTestContext;
static inline void put_cabac_bit(CABACTestContext *c, int b)
{
put_bits(&c->pb, 1, b); put_bits(&c->pb, 1, b);
for(;c->outstanding_count; c->outstanding_count--){ for(;c->outstanding_count; c->outstanding_count--){
put_bits(&c->pb, 1, 1-b); put_bits(&c->pb, 1, 1-b);
} }
} }
static inline void renorm_cabac_encoder(CABACContext *c){ static inline void renorm_cabac_encoder(CABACTestContext *c)
while(c->range < 0x100){ {
while (c->dec.range < 0x100) {
//FIXME optimize //FIXME optimize
if(c->low<0x100){ if (c->dec.low < 0x100) {
put_cabac_bit(c, 0); put_cabac_bit(c, 0);
}else if(c->low<0x200){ } else if (c->dec.low < 0x200) {
c->outstanding_count++; c->outstanding_count++;
c->low -= 0x100; c->dec.low -= 0x100;
}else{ }else{
put_cabac_bit(c, 1); put_cabac_bit(c, 1);
c->low -= 0x200; c->dec.low -= 0x200;
} }
c->range+= c->range; c->dec.range += c->dec.range;
c->low += c->low; c->dec.low += c->dec.low;
} }
} }
static void put_cabac(CABACContext *c, uint8_t * const state, int bit){ static void put_cabac(CABACTestContext *c, uint8_t * const state, int bit)
int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state]; {
int RangeLPS = ff_h264_lps_range[2 * (c->dec.range & 0xC0) + *state];
if(bit == ((*state)&1)){ if(bit == ((*state)&1)){
c->range -= RangeLPS; c->dec.range -= RangeLPS;
*state = ff_h264_mlps_state[128 + *state]; *state = ff_h264_mlps_state[128 + *state];
}else{ }else{
c->low += c->range - RangeLPS; c->dec.low += c->dec.range - RangeLPS;
c->range = RangeLPS; c->dec.range = RangeLPS;
*state= ff_h264_mlps_state[127 - *state]; *state= ff_h264_mlps_state[127 - *state];
} }
@ -68,21 +78,22 @@ static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
/** /**
* @param bit 0 -> write zero bit, !=0 write one bit * @param bit 0 -> write zero bit, !=0 write one bit
*/ */
static void put_cabac_bypass(CABACContext *c, int bit){ static void put_cabac_bypass(CABACTestContext *c, int bit)
c->low += c->low; {
c->dec.low += c->dec.low;
if(bit){ if(bit){
c->low += c->range; c->dec.low += c->dec.range;
} }
//FIXME optimize //FIXME optimize
if(c->low<0x200){ if (c->dec.low < 0x200) {
put_cabac_bit(c, 0); put_cabac_bit(c, 0);
}else if(c->low<0x400){ } else if (c->dec.low < 0x400) {
c->outstanding_count++; c->outstanding_count++;
c->low -= 0x200; c->dec.low -= 0x200;
}else{ }else{
put_cabac_bit(c, 1); put_cabac_bit(c, 1);
c->low -= 0x400; c->dec.low -= 0x400;
} }
} }
@ -90,20 +101,21 @@ static void put_cabac_bypass(CABACContext *c, int bit){
* *
* @return the number of bytes written * @return the number of bytes written
*/ */
static int put_cabac_terminate(CABACContext *c, int bit){ static int put_cabac_terminate(CABACTestContext *c, int bit)
c->range -= 2; {
c->dec.range -= 2;
if(!bit){ if(!bit){
renorm_cabac_encoder(c); renorm_cabac_encoder(c);
}else{ }else{
c->low += c->range; c->dec.low += c->dec.range;
c->range= 2; c->dec.range = 2;
renorm_cabac_encoder(c); renorm_cabac_encoder(c);
av_assert0(c->low <= 0x1FF); av_assert0(c->dec.low <= 0x1FF);
put_cabac_bit(c, c->low>>9); put_cabac_bit(c, c->dec.low >> 9);
put_bits(&c->pb, 2, ((c->low>>7)&3)|1); put_bits(&c->pb, 2, ((c->dec.low >> 7) & 3) | 1);
flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
} }
@ -111,8 +123,21 @@ static int put_cabac_terminate(CABACContext *c, int bit){
return (put_bits_count(&c->pb)+7)>>3; return (put_bits_count(&c->pb)+7)>>3;
} }
/**
* @param buf_size size of buf in bits
*/
static void init_cabac_encoder(CABACTestContext *c, uint8_t *buf, int buf_size)
{
init_put_bits(&c->pb, buf, buf_size);
c->dec.low = 0;
c->dec.range = 0x1FE;
c->outstanding_count = 0;
c->pb.bit_left++; //avoids firstBitFlag
}
int main(void){ int main(void){
CABACContext c; CABACTestContext c;
uint8_t b[9*SIZE]; uint8_t b[9*SIZE];
uint8_t r[9*SIZE]; uint8_t r[9*SIZE];
int i, ret = 0; int i, ret = 0;
@ -120,7 +145,7 @@ int main(void){
AVLFG prng; AVLFG prng;
av_lfg_init(&prng, 1); av_lfg_init(&prng, 1);
ff_init_cabac_encoder(&c, b, SIZE); init_cabac_encoder(&c, b, SIZE);
for(i=0; i<SIZE; i++){ for(i=0; i<SIZE; i++){
if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7; if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
@ -139,24 +164,24 @@ int main(void){
b[i++] = av_lfg_get(&prng); b[i++] = av_lfg_get(&prng);
b[i ] = av_lfg_get(&prng); b[i ] = av_lfg_get(&prng);
ff_init_cabac_decoder(&c, b, SIZE); ff_init_cabac_decoder(&c.dec, b, SIZE);
memset(state, 0, sizeof(state)); memset(state, 0, sizeof(state));
for(i=0; i<SIZE; i++){ for(i=0; i<SIZE; i++){
if( (r[i]&1) != get_cabac_bypass(&c) ) { if ((r[i] & 1) != get_cabac_bypass(&c.dec)) {
av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i); av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
ret = 1; ret = 1;
} }
} }
for(i=0; i<SIZE; i++){ for(i=0; i<SIZE; i++){
if( (r[i]&1) != get_cabac_noinline(&c, state) ) { if ((r[i] & 1) != get_cabac_noinline(&c.dec, state)) {
av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i); av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
ret = 1; ret = 1;
} }
} }
if(!get_cabac_terminate(&c)) { if (!get_cabac_terminate(&c.dec)) {
av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n"); av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
ret = 1; ret = 1;
} }