3 * Copyright (c) 2007 Kamil Nowosad
5 * This file is part of FFmpeg.
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.
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.
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
25 * @author Kamil Nowosad
30 static void bytein(MqcState *mqc)
32 if (*mqc->bp == 0xff) {
33 if (*(mqc->bp + 1) > 0x8f)
37 mqc->c += 2 + 0xfe00 - (*mqc->bp << 9);
41 mqc->c += 1 + 0xff00 - (*mqc->bp << 8);
45 static int exchange(MqcState *mqc, uint8_t *cxstate, int lps)
48 if ((mqc->a < ff_mqc_qe[*cxstate]) ^ (!lps)) {
50 mqc->a = ff_mqc_qe[*cxstate];
52 *cxstate = ff_mqc_nmps[*cxstate];
55 mqc->a = ff_mqc_qe[*cxstate];
56 d = 1 - (*cxstate & 1);
57 *cxstate = ff_mqc_nlps[*cxstate];
59 // do RENORMD: see ISO/IEC 15444-1:2002 §C.3.3
61 if (!(mqc->c & 0xff)) {
67 } while (!(mqc->a & 0x8000));
71 void ff_mqc_initdec(MqcState *mqc, uint8_t *bp)
73 ff_mqc_init_contexts(mqc);
75 mqc->c = (*mqc->bp ^ 0xff) << 16;
81 int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate)
83 mqc->a -= ff_mqc_qe[*cxstate];
84 if ((mqc->c >> 16) < mqc->a) {
88 return exchange(mqc, cxstate, 0);
90 mqc->c -= mqc->a << 16;
91 return exchange(mqc, cxstate, 1);