FFmpeg
dirac_arith.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
3  * Copyright (C) 2009 David Conrad
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  * Arithmetic decoder for Dirac
25  * @author Marco Gerards <marco@gnu.org>
26  */
27 
28 #include "dirac_arith.h"
29 
30 
31 static const uint16_t dirac_prob[256] = {
32  0, 2, 5, 8, 11, 15, 20, 24,
33  29, 35, 41, 47, 53, 60, 67, 74,
34  82, 89, 97, 106, 114, 123, 132, 141,
35  150, 160, 170, 180, 190, 201, 211, 222,
36  233, 244, 256, 267, 279, 291, 303, 315,
37  327, 340, 353, 366, 379, 392, 405, 419,
38  433, 447, 461, 475, 489, 504, 518, 533,
39  548, 563, 578, 593, 609, 624, 640, 656,
40  672, 688, 705, 721, 738, 754, 771, 788,
41  805, 822, 840, 857, 875, 892, 910, 928,
42  946, 964, 983, 1001, 1020, 1038, 1057, 1076,
43  1095, 1114, 1133, 1153, 1172, 1192, 1211, 1231,
44  1251, 1271, 1291, 1311, 1332, 1352, 1373, 1393,
45  1414, 1435, 1456, 1477, 1498, 1520, 1541, 1562,
46  1584, 1606, 1628, 1649, 1671, 1694, 1716, 1738,
47  1760, 1783, 1806, 1828, 1851, 1874, 1897, 1920,
48  1935, 1942, 1949, 1955, 1961, 1968, 1974, 1980,
49  1985, 1991, 1996, 2001, 2006, 2011, 2016, 2021,
50  2025, 2029, 2033, 2037, 2040, 2044, 2047, 2050,
51  2053, 2056, 2058, 2061, 2063, 2065, 2066, 2068,
52  2069, 2070, 2071, 2072, 2072, 2072, 2072, 2072,
53  2072, 2071, 2070, 2069, 2068, 2066, 2065, 2063,
54  2060, 2058, 2055, 2052, 2049, 2045, 2042, 2038,
55  2033, 2029, 2024, 2019, 2013, 2008, 2002, 1996,
56  1989, 1982, 1975, 1968, 1960, 1952, 1943, 1934,
57  1925, 1916, 1906, 1896, 1885, 1874, 1863, 1851,
58  1839, 1827, 1814, 1800, 1786, 1772, 1757, 1742,
59  1727, 1710, 1694, 1676, 1659, 1640, 1622, 1602,
60  1582, 1561, 1540, 1518, 1495, 1471, 1447, 1422,
61  1396, 1369, 1341, 1312, 1282, 1251, 1219, 1186,
62  1151, 1114, 1077, 1037, 995, 952, 906, 857,
63  805, 750, 690, 625, 553, 471, 376, 255
64 };
65 
69  [CTX_ZP_F2] = CTX_ZP_F3,
70  [CTX_ZP_F3] = CTX_ZP_F4,
71  [CTX_ZP_F4] = CTX_ZP_F5,
72  [CTX_ZP_F5] = CTX_ZP_F6,
73  [CTX_ZP_F6] = CTX_ZP_F6,
76  [CTX_NP_F2] = CTX_NP_F3,
77  [CTX_NP_F3] = CTX_NP_F4,
78  [CTX_NP_F4] = CTX_NP_F5,
79  [CTX_NP_F5] = CTX_NP_F6,
80  [CTX_NP_F6] = CTX_NP_F6,
82 };
83 
84 int16_t ff_dirac_prob_branchless[256][2];
85 
87 {
88  int i;
89 
90  for (i = 0; i < 256; i++) {
93  }
94 }
95 
97 {
98  int i;
99  align_get_bits(gb);
100 
101  length = FFMIN(length, get_bits_left(gb)/8);
102 
103  c->bytestream = gb->buffer + get_bits_count(gb)/8;
104  c->bytestream_end = c->bytestream + length;
105  skip_bits_long(gb, length*8);
106 
107  c->low = 0;
108  for (i = 0; i < 4; i++) {
109  c->low <<= 8;
110  if (c->bytestream < c->bytestream_end)
111  c->low |= *c->bytestream++;
112  else
113  c->low |= 0xff;
114  }
115 
116  c->counter = -16;
117  c->range = 0xffff;
118  c->error = 0;
119  c->overread= 0;
120 
121  for (i = 0; i < DIRAC_CTX_COUNT; i++)
122  c->contexts[i] = 0x8000;
123 }
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
CTX_ZP_F2
@ CTX_ZP_F2
Definition: dirac_arith.h:44
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
CTX_ZPZN_F1
@ CTX_ZPZN_F1
Definition: dirac_arith.h:40
CTX_ZP_F4
@ CTX_ZP_F4
Definition: dirac_arith.h:46
DiracArith
Definition: dirac_arith.h:79
CTX_ZP_F5
@ CTX_ZP_F5
Definition: dirac_arith.h:47
CTX_ZP_F3
@ CTX_ZP_F3
Definition: dirac_arith.h:45
GetBitContext
Definition: get_bits.h:108
av_cold
#define av_cold
Definition: attributes.h:90
DIRAC_CTX_COUNT
@ DIRAC_CTX_COUNT
Definition: dirac_arith.h:63
CTX_NP_F3
@ CTX_NP_F3
Definition: dirac_arith.h:50
ff_dirac_init_arith_decoder
void ff_dirac_init_arith_decoder(DiracArith *c, GetBitContext *gb, int length)
Definition: dirac_arith.c:96
CTX_DELTA_Q_F
@ CTX_DELTA_Q_F
Definition: dirac_arith.h:59
ff_dirac_prob_branchless
int16_t ff_dirac_prob_branchless[256][2]
Definition: dirac_arith.c:84
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:109
CTX_NP_F6
@ CTX_NP_F6
Definition: dirac_arith.h:53
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
CTX_ZPNN_F1
@ CTX_ZPNN_F1
Definition: dirac_arith.h:41
CTX_NPZN_F1
@ CTX_NPZN_F1
Definition: dirac_arith.h:42
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
CTX_NP_F5
@ CTX_NP_F5
Definition: dirac_arith.h:52
dirac_arith.h
ff_dirac_init_arith_tables
av_cold void ff_dirac_init_arith_tables(void)
Definition: dirac_arith.c:86
CTX_ZP_F6
@ CTX_ZP_F6
Definition: dirac_arith.h:48
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:561
CTX_NP_F2
@ CTX_NP_F2
Definition: dirac_arith.h:49
ff_dirac_next_ctx
const uint8_t ff_dirac_next_ctx[DIRAC_CTX_COUNT]
Definition: dirac_arith.c:66
dirac_prob
static const uint16_t dirac_prob[256]
Definition: dirac_arith.c:31
CTX_NPNN_F1
@ CTX_NPNN_F1
Definition: dirac_arith.h:43
CTX_NP_F4
@ CTX_NP_F4
Definition: dirac_arith.h:51