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
28 #include "libavutil/avassert.h"
31 static void byteout(MqcState *mqc)
34 if (*mqc->bp == 0xff){
36 *mqc->bp = mqc->c >> 20;
39 } else if ((mqc->c & 0x8000000)){
45 *mqc->bp = mqc->c >> 19;
51 static void renorme(MqcState *mqc)
58 } while (!(mqc->a & 0x8000));
61 static void setbits(MqcState *mqc)
63 int tmp = mqc->c + mqc->a;
69 void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
71 ff_mqc_init_contexts(mqc);
76 mqc->ct = 12 + (*mqc->bp == 0xff);
79 void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
83 qe = ff_mqc_qe[*cxstate];
85 if ((*cxstate & 1) == d){
86 if (!(mqc->a & 0x8000)){
91 *cxstate = ff_mqc_nmps[*cxstate];
100 *cxstate = ff_mqc_nlps[*cxstate];
105 int ff_mqc_length(MqcState *mqc)
107 return mqc->bp - mqc->bpstart;
110 int ff_mqc_flush(MqcState *mqc)
113 mqc->c = mqc->c << mqc->ct;
115 mqc->c = mqc->c << mqc->ct;
117 if (*mqc->bp != 0xff)
119 return mqc->bp - mqc->bpstart;
122 int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
124 MqcState mqc2 = *mqc;
129 *dst_len = mqc2.bp - dst;
130 if (mqc->bp < mqc->bpstart) {
131 av_assert1(mqc->bpstart - mqc->bp == 1);
132 av_assert1(*dst_len > 0);
133 av_assert1(mqc->bp[0] == 0 && dst[0] == 0);
135 memmove(dst, dst+1, *dst_len);
136 return mqc->bp - mqc->bpstart + 1 + *dst_len;
138 return mqc->bp - mqc->bpstart + *dst_len;