FFmpeg  4.3
cabac_functions.h
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3  * Copyright (c) 2003 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  * Context Adaptive Binary Arithmetic Coder inline functions
25  */
26 
27 #ifndef AVCODEC_CABAC_FUNCTIONS_H
28 #define AVCODEC_CABAC_FUNCTIONS_H
29 
30 #include <stdint.h>
31 
32 #include "cabac.h"
33 #include "config.h"
34 
35 #ifndef UNCHECKED_BITSTREAM_READER
36 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
37 #endif
38 
39 #if ARCH_AARCH64
40 # include "aarch64/cabac.h"
41 #endif
42 #if ARCH_ARM
43 # include "arm/cabac.h"
44 #endif
45 #if ARCH_X86
46 # include "x86/cabac.h"
47 #endif
48 #if ARCH_MIPS
49 # include "mips/cabac.h"
50 #endif
51 
56 
57 #if !defined(get_cabac_bypass) || !defined(get_cabac_terminate)
58 static void refill(CABACContext *c){
59 #if CABAC_BITS == 16
60  c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
61 #else
62  c->low+= c->bytestream[0]<<1;
63 #endif
64  c->low -= CABAC_MASK;
65 #if !UNCHECKED_BITSTREAM_READER
66  if (c->bytestream < c->bytestream_end)
67 #endif
68  c->bytestream += CABAC_BITS / 8;
69 }
70 #endif
71 
72 #ifndef get_cabac_terminate
74  int shift= (uint32_t)(c->range - 0x100)>>31;
75  c->range<<= shift;
76  c->low <<= shift;
77  if(!(c->low & CABAC_MASK))
78  refill(c);
79 }
80 #endif
81 
82 #ifndef get_cabac_inline
83 static void refill2(CABACContext *c){
84  int i;
85  unsigned x;
86 #if !HAVE_FAST_CLZ
87  x= c->low ^ (c->low-1);
88  i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
89 #else
90  i = ff_ctz(c->low) - CABAC_BITS;
91 #endif
92 
93  x= -CABAC_MASK;
94 
95 #if CABAC_BITS == 16
96  x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
97 #else
98  x+= c->bytestream[0]<<1;
99 #endif
100 
101  c->low += x<<i;
102 #if !UNCHECKED_BITSTREAM_READER
103  if (c->bytestream < c->bytestream_end)
104 #endif
105  c->bytestream += CABAC_BITS/8;
106 }
107 #endif
108 
109 #ifndef get_cabac_inline
111  int s = *state;
112  int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
113  int bit, lps_mask;
114 
115  c->range -= RangeLPS;
116  lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
117 
118  c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
119  c->range += (RangeLPS - c->range) & lps_mask;
120 
121  s^=lps_mask;
122  *state= (ff_h264_mlps_state+128)[s];
123  bit= s&1;
124 
125  lps_mask= ff_h264_norm_shift[c->range];
126  c->range<<= lps_mask;
127  c->low <<= lps_mask;
128  if(!(c->low & CABAC_MASK))
129  refill2(c);
130  return bit;
131 }
132 #endif
133 
135  return get_cabac_inline(c,state);
136 }
137 
139  return get_cabac_inline(c,state);
140 }
141 
142 #ifndef get_cabac_bypass
144  int range;
145  c->low += c->low;
146 
147  if(!(c->low & CABAC_MASK))
148  refill(c);
149 
150  range= c->range<<(CABAC_BITS+1);
151  if(c->low < range){
152  return 0;
153  }else{
154  c->low -= range;
155  return 1;
156  }
157 }
158 #endif
159 
160 #ifndef get_cabac_bypass_sign
162  int range, mask;
163  c->low += c->low;
164 
165  if(!(c->low & CABAC_MASK))
166  refill(c);
167 
168  range= c->range<<(CABAC_BITS+1);
169  c->low -= range;
170  mask= c->low >> 31;
171  range &= mask;
172  c->low += range;
173  return (val^mask)-mask;
174 }
175 #endif
176 
177 /**
178  * @return the number of bytes read or 0 if no end
179  */
180 #ifndef get_cabac_terminate
182  c->range -= 2;
183  if(c->low < c->range<<(CABAC_BITS+1)){
185  return 0;
186  }else{
187  return c->bytestream - c->bytestream_start;
188  }
189 }
190 #endif
191 
192 /**
193  * Skip @p n bytes and reset the decoder.
194  * @return the address of the first skipped byte or NULL if there's less than @p n bytes left
195  */
196 #ifndef skip_bytes
197 static av_unused const uint8_t* skip_bytes(CABACContext *c, int n) {
198  const uint8_t *ptr = c->bytestream;
199 
200  if (c->low & 0x1)
201  ptr--;
202 #if CABAC_BITS == 16
203  if (c->low & 0x1FF)
204  ptr--;
205 #endif
206  if ((int) (c->bytestream_end - ptr) < n)
207  return NULL;
208  if (ff_init_cabac_decoder(c, ptr + n, c->bytestream_end - ptr - n) < 0)
209  return NULL;
210 
211  return ptr;
212 }
213 #endif
214 
215 #endif /* AVCODEC_CABAC_FUNCTIONS_H */
cabac.h
ff_ctz
#define ff_ctz
Definition: intmath.h:106
renorm_cabac_decoder_once
static void renorm_cabac_decoder_once(CABACContext *c)
Definition: cabac_functions.h:73
av_unused
#define av_unused
Definition: attributes.h:131
cabac.h
get_cabac_inline
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t *const state)
Definition: cabac_functions.h:110
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
get_cabac
static int av_unused get_cabac(CABACContext *c, uint8_t *const state)
Definition: cabac_functions.h:138
ff_h264_last_coeff_flag_offset_8x8
static const uint8_t *const ff_h264_last_coeff_flag_offset_8x8
Definition: cabac_functions.h:55
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
val
static double val(void *priv, double ch)
Definition: aeval.c:76
ff_h264_cabac_tables
const uint8_t ff_h264_cabac_tables[512+4 *2 *64+4 *64+63]
av_noinline
#define av_noinline
Definition: attributes.h:72
CABAC_MASK
#define CABAC_MASK
Definition: cabac.h:41
ff_h264_norm_shift
static const uint8_t *const ff_h264_norm_shift
Definition: cabac_functions.h:52
mask
static const uint16_t mask[17]
Definition: lzw.c:38
s
#define s(width, name)
Definition: cbs_vp9.c:257
cabac.h
refill2
static void refill2(CABACContext *c)
Definition: cabac_functions.h:83
NULL
#define NULL
Definition: coverity.c:32
ff_h264_mlps_state
static const uint8_t *const ff_h264_mlps_state
Definition: cabac_functions.h:54
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
get_cabac_noinline
static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t *const state)
Definition: cabac_functions.h:134
state
static struct @314 state
H264_LPS_RANGE_OFFSET
#define H264_LPS_RANGE_OFFSET
Definition: cabac.h:36
get_cabac_bypass_sign
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val)
Definition: cabac_functions.h:161
get_cabac_terminate
static int av_unused get_cabac_terminate(CABACContext *c)
Definition: cabac_functions.h:181
ff_h264_lps_range
static const uint8_t *const ff_h264_lps_range
Definition: cabac_functions.h:53
ff_init_cabac_decoder
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:176
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
get_cabac_bypass
static int av_unused get_cabac_bypass(CABACContext *c)
Definition: cabac_functions.h:143
av_always_inline
#define av_always_inline
Definition: attributes.h:49
cabac.h
uint8_t
uint8_t
Definition: audio_convert.c:194
H264_NORM_SHIFT_OFFSET
#define H264_NORM_SHIFT_OFFSET
Definition: cabac.h:35
cabac.h
H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
#define H264_LAST_COEFF_FLAG_OFFSET_8x8_OFFSET
Definition: cabac.h:38
refill
static void refill(CABACContext *c)
Definition: cabac_functions.h:58
config.h
shift
static int shift(int a, int b)
Definition: sonic.c:82
skip_bytes
static const av_unused uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
Definition: cabac_functions.h:197
CABAC_BITS
#define CABAC_BITS
Definition: cabac.h:40
H264_MLPS_STATE_OFFSET
#define H264_MLPS_STATE_OFFSET
Definition: cabac.h:37
CABACContext
Definition: cabac.h:43