FFmpeg  4.3
rangecoder.h
Go to the documentation of this file.
1 /*
2  * Range coder
3  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Range coder.
25  */
26 
27 #ifndef AVCODEC_RANGECODER_H
28 #define AVCODEC_RANGECODER_H
29 
30 #include <stdint.h>
31 
32 #include "libavutil/common.h"
33 #include "libavutil/avassert.h"
34 
35 typedef struct RangeCoder {
36  int low;
37  int range;
45  int overread;
46 #define MAX_OVERREAD 2
47 } RangeCoder;
48 
49 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
50 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
51 
52 /**
53  * Terminates the range coder
54  * @param version version 0 requires the decoder to know the data size in bytes
55  * version 1 needs about 1 bit more space but does not need to
56  * carry the size from encoder to decoder
57  */
59 
60 /**
61  * Check if at the current position there is a valid looking termination
62  * @param version version 0 requires the decoder to know the data size in bytes
63  * version 1 needs about 1 bit more space but does not need to
64  * carry the size from encoder to decoder
65  * @returns negative AVERROR code on error or non negative.
66  */
68 
69 void ff_build_rac_states(RangeCoder *c, int factor, int max_p);
70 
71 static inline void renorm_encoder(RangeCoder *c)
72 {
73  // FIXME: optimize
74  while (c->range < 0x100) {
75  if (c->outstanding_byte < 0) {
76  c->outstanding_byte = c->low >> 8;
77  } else if (c->low <= 0xFF00) {
78  *c->bytestream++ = c->outstanding_byte;
79  for (; c->outstanding_count; c->outstanding_count--)
80  *c->bytestream++ = 0xFF;
81  c->outstanding_byte = c->low >> 8;
82  } else if (c->low >= 0x10000) {
83  *c->bytestream++ = c->outstanding_byte + 1;
84  for (; c->outstanding_count; c->outstanding_count--)
85  *c->bytestream++ = 0x00;
86  c->outstanding_byte = (c->low >> 8) & 0xFF;
87  } else {
88  c->outstanding_count++;
89  }
90 
91  c->low = (c->low & 0xFF) << 8;
92  c->range <<= 8;
93  }
94 }
95 
96 static inline int get_rac_count(RangeCoder *c)
97 {
98  int x = c->bytestream - c->bytestream_start + c->outstanding_count;
99  if (c->outstanding_byte >= 0)
100  x++;
101  return 8 * x - av_log2(c->range);
102 }
103 
104 static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit)
105 {
106  int range1 = (c->range * (*state)) >> 8;
107 
108  av_assert2(*state);
109  av_assert2(range1 < c->range);
110  av_assert2(range1 > 0);
111  if (!bit) {
112  c->range -= range1;
113  *state = c->zero_state[*state];
114  } else {
115  c->low += c->range - range1;
116  c->range = range1;
117  *state = c->one_state[*state];
118  }
119 
120  renorm_encoder(c);
121 }
122 
123 static inline void refill(RangeCoder *c)
124 {
125  if (c->range < 0x100) {
126  c->range <<= 8;
127  c->low <<= 8;
128  if (c->bytestream < c->bytestream_end) {
129  c->low += c->bytestream[0];
130  c->bytestream++;
131  } else
132  c->overread ++;
133  }
134 }
135 
136 static inline int get_rac(RangeCoder *c, uint8_t *const state)
137 {
138  int range1 = (c->range * (*state)) >> 8;
139 
140  c->range -= range1;
141  if (c->low < c->range) {
142  *state = c->zero_state[*state];
143  refill(c);
144  return 0;
145  } else {
146  c->low -= c->range;
147  *state = c->one_state[*state];
148  c->range = range1;
149  refill(c);
150  return 1;
151  }
152 }
153 
154 #endif /* AVCODEC_RANGECODER_H */
RangeCoder::bytestream_end
uint8_t * bytestream_end
Definition: rangecoder.h:44
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
refill
static void refill(RangeCoder *c)
Definition: rangecoder.h:123
x
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo x
Definition: fate.txt:150
avassert.h
RangeCoder::one_state
uint8_t one_state[256]
Definition: rangecoder.h:41
ff_build_rac_states
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:68
RangeCoder::range
int range
Definition: rangecoder.h:37
RangeCoder::low
int low
Definition: rangecoder.h:36
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
state
static struct @314 state
RangeCoder::bytestream
uint8_t * bytestream
Definition: rangecoder.h:43
version
version
Definition: libkvazaar.c:292
av_log2
#define av_log2
Definition: intmath.h:83
RangeCoder::outstanding_count
int outstanding_count
Definition: rangecoder.h:38
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
ff_init_range_decoder
void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: rangecoder.c:53
renorm_encoder
static void renorm_encoder(RangeCoder *c)
Definition: rangecoder.h:71
common.h
ff_rac_check_termination
int ff_rac_check_termination(RangeCoder *c, int version)
Check if at the current position there is a valid looking termination.
Definition: rangecoder.c:125
uint8_t
uint8_t
Definition: audio_convert.c:194
get_rac
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:136
RangeCoder::overread
int overread
Definition: rangecoder.h:45
RangeCoder::outstanding_byte
int outstanding_byte
Definition: rangecoder.h:39
RangeCoder::bytestream_start
uint8_t * bytestream_start
Definition: rangecoder.h:42
ff_init_range_encoder
void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
Definition: rangecoder.c:42
get_rac_count
static int get_rac_count(RangeCoder *c)
Definition: rangecoder.h:96
factor
static const int factor[16]
Definition: vf_pp7.c:75
ff_rac_terminate
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
Definition: rangecoder.c:109
put_rac
static void put_rac(RangeCoder *c, uint8_t *const state, int bit)
Definition: rangecoder.h:104
RangeCoder
Definition: mss3.c:61
RangeCoder::zero_state
uint8_t zero_state[256]
Definition: rangecoder.h:40