/src/ffmpeg/libavcodec/mqcdec.c
Line | Count | Source |
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 | | static void bytein(MqcState *mqc) |
31 | 2.08M | { |
32 | 2.08M | if (*mqc->bp == 0xff) { |
33 | 1.76M | if (*(mqc->bp + 1) > 0x8f) |
34 | 1.75M | mqc->c++; |
35 | 5.74k | else { |
36 | 5.74k | mqc->bp++; |
37 | 5.74k | mqc->c += 2 + 0xfe00 - (*mqc->bp << 9); |
38 | 5.74k | } |
39 | 1.76M | } else { |
40 | 317k | mqc->bp++; |
41 | 317k | mqc->c += 1 + 0xff00 - (*mqc->bp << 8); |
42 | 317k | } |
43 | 2.08M | } |
44 | | |
45 | | static int exchange(MqcState *mqc, uint8_t *cxstate, int lps) |
46 | 7.01M | { |
47 | 7.01M | int d; |
48 | 7.01M | if ((mqc->a < ff_mqc_qe[*cxstate]) ^ (!lps)) { |
49 | 3.71M | if (lps) |
50 | 726k | mqc->a = ff_mqc_qe[*cxstate]; |
51 | 3.71M | d = *cxstate & 1; |
52 | 3.71M | *cxstate = ff_mqc_nmps[*cxstate]; |
53 | 3.71M | } else { |
54 | 3.30M | if (lps) |
55 | 2.63M | mqc->a = ff_mqc_qe[*cxstate]; |
56 | 3.30M | d = 1 - (*cxstate & 1); |
57 | 3.30M | *cxstate = ff_mqc_nlps[*cxstate]; |
58 | 3.30M | } |
59 | | // do RENORMD: see ISO/IEC 15444-1:2002 §C.3.3 |
60 | 9.21M | do { |
61 | 9.21M | if (!(mqc->c & 0xff)) { |
62 | 1.17M | mqc->c -= 0x100; |
63 | 1.17M | bytein(mqc); |
64 | 1.17M | } |
65 | 9.21M | mqc->a += mqc->a; |
66 | 9.21M | mqc->c += mqc->c; |
67 | 9.21M | } while (!(mqc->a & 0x8000)); |
68 | 7.01M | return d; |
69 | 7.01M | } |
70 | | |
71 | | void ff_mqc_initdec(MqcState *mqc, uint8_t *bp, int raw, int reset) |
72 | 96.7k | { |
73 | 96.7k | mqc->raw = raw; |
74 | 96.7k | if (reset) |
75 | 19.4k | ff_mqc_init_contexts(mqc); |
76 | 96.7k | mqc->bp = bp; |
77 | 96.7k | mqc->c = (*mqc->bp ^ 0xff) << 16; |
78 | 96.7k | bytein(mqc); |
79 | 96.7k | mqc->c = mqc->c << 7; |
80 | 96.7k | mqc->a = 0x8000; |
81 | 96.7k | } |
82 | | |
83 | 6.48M | static int mqc_decode_bypass(MqcState *mqc) { |
84 | 6.48M | int bit = !(mqc->c & 0x40000000); |
85 | 6.48M | if (!(mqc->c & 0xff)) { |
86 | 813k | mqc->c -= 0x100; |
87 | 813k | bytein(mqc); |
88 | 813k | } |
89 | 6.48M | mqc->c += mqc->c; |
90 | 6.48M | return bit; |
91 | 6.48M | } |
92 | | |
93 | | int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate) |
94 | 22.4M | { |
95 | 22.4M | if (mqc->raw) |
96 | 6.48M | return mqc_decode_bypass(mqc); |
97 | 15.9M | mqc->a -= ff_mqc_qe[*cxstate]; |
98 | 15.9M | if ((mqc->c >> 16) < mqc->a) { |
99 | 12.5M | if (mqc->a & 0x8000) |
100 | 8.91M | return *cxstate & 1; |
101 | 3.65M | else |
102 | 3.65M | return exchange(mqc, cxstate, 0); |
103 | 12.5M | } else { |
104 | 3.36M | mqc->c -= mqc->a << 16; |
105 | 3.36M | return exchange(mqc, cxstate, 1); |
106 | 3.36M | } |
107 | 15.9M | } |