1 |
|
|
/* |
2 |
|
|
* MQ-coder decoder |
3 |
|
|
* Copyright (c) 2007 Kamil Nowosad |
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 |
|
|
* MQ-coder decoder |
24 |
|
|
* @file |
25 |
|
|
* @author Kamil Nowosad |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
#include "mqc.h" |
29 |
|
|
|
30 |
|
16161272 |
static void bytein(MqcState *mqc) |
31 |
|
|
{ |
32 |
✓✓ |
16161272 |
if (*mqc->bp == 0xff) { |
33 |
✓✓ |
473157 |
if (*(mqc->bp + 1) > 0x8f) |
34 |
|
407651 |
mqc->c++; |
35 |
|
|
else { |
36 |
|
65506 |
mqc->bp++; |
37 |
|
65506 |
mqc->c += 2 + 0xfe00 - (*mqc->bp << 9); |
38 |
|
|
} |
39 |
|
|
} else { |
40 |
|
15688115 |
mqc->bp++; |
41 |
|
15688115 |
mqc->c += 1 + 0xff00 - (*mqc->bp << 8); |
42 |
|
|
} |
43 |
|
16161272 |
} |
44 |
|
|
|
45 |
|
88937633 |
static int exchange(MqcState *mqc, uint8_t *cxstate, int lps) |
46 |
|
|
{ |
47 |
|
|
int d; |
48 |
✓✓ |
88937633 |
if ((mqc->a < ff_mqc_qe[*cxstate]) ^ (!lps)) { |
49 |
✓✓ |
47134844 |
if (lps) |
50 |
|
9071787 |
mqc->a = ff_mqc_qe[*cxstate]; |
51 |
|
47134844 |
d = *cxstate & 1; |
52 |
|
47134844 |
*cxstate = ff_mqc_nmps[*cxstate]; |
53 |
|
|
} else { |
54 |
✓✓ |
41802789 |
if (lps) |
55 |
|
34488874 |
mqc->a = ff_mqc_qe[*cxstate]; |
56 |
|
41802789 |
d = 1 - (*cxstate & 1); |
57 |
|
41802789 |
*cxstate = ff_mqc_nlps[*cxstate]; |
58 |
|
|
} |
59 |
|
|
// do RENORMD: see ISO/IEC 15444-1:2002 §C.3.3 |
60 |
|
|
do { |
61 |
✓✓ |
125009704 |
if (!(mqc->c & 0xff)) { |
62 |
|
15762913 |
mqc->c -= 0x100; |
63 |
|
15762913 |
bytein(mqc); |
64 |
|
|
} |
65 |
|
125009704 |
mqc->a += mqc->a; |
66 |
|
125009704 |
mqc->c += mqc->c; |
67 |
✓✓ |
125009704 |
} while (!(mqc->a & 0x8000)); |
68 |
|
88937633 |
return d; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
398359 |
void ff_mqc_initdec(MqcState *mqc, uint8_t *bp, int raw, int reset) |
72 |
|
|
{ |
73 |
|
398359 |
mqc->raw = raw; |
74 |
✓✗ |
398359 |
if (reset) |
75 |
|
398359 |
ff_mqc_init_contexts(mqc); |
76 |
|
398359 |
mqc->bp = bp; |
77 |
|
398359 |
mqc->c = (*mqc->bp ^ 0xff) << 16; |
78 |
|
398359 |
bytein(mqc); |
79 |
|
398359 |
mqc->c = mqc->c << 7; |
80 |
|
398359 |
mqc->a = 0x8000; |
81 |
|
398359 |
} |
82 |
|
|
|
83 |
|
|
static int mqc_decode_bypass(MqcState *mqc) { |
84 |
|
|
int bit = !(mqc->c & 0x40000000); |
85 |
|
|
if (!(mqc->c & 0xff)) { |
86 |
|
|
mqc->c -= 0x100; |
87 |
|
|
bytein(mqc); |
88 |
|
|
} |
89 |
|
|
mqc->c += mqc->c; |
90 |
|
|
return bit; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
185774934 |
int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate) |
94 |
|
|
{ |
95 |
✗✓ |
185774934 |
if (mqc->raw) |
96 |
|
|
return mqc_decode_bypass(mqc); |
97 |
|
185774934 |
mqc->a -= ff_mqc_qe[*cxstate]; |
98 |
✓✓ |
185774934 |
if ((mqc->c >> 16) < mqc->a) { |
99 |
✓✓ |
142214273 |
if (mqc->a & 0x8000) |
100 |
|
96837301 |
return *cxstate & 1; |
101 |
|
|
else |
102 |
|
45376972 |
return exchange(mqc, cxstate, 0); |
103 |
|
|
} else { |
104 |
|
43560661 |
mqc->c -= mqc->a << 16; |
105 |
|
43560661 |
return exchange(mqc, cxstate, 1); |
106 |
|
|
} |
107 |
|
|
} |