summaryrefslogtreecommitdiff
path: root/libavcodec/mqcenc.c (plain)
blob: 7c9e1a0df961bdaa3ce08020bb88eeee39c7467b
1/*
2 * MQ-coder encoder
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 encoder
24 * @file
25 * @author Kamil Nowosad
26 */
27
28#include "libavutil/avassert.h"
29#include "mqc.h"
30
31static void byteout(MqcState *mqc)
32{
33retry:
34 if (*mqc->bp == 0xff){
35 mqc->bp++;
36 *mqc->bp = mqc->c >> 20;
37 mqc->c &= 0xfffff;
38 mqc->ct = 7;
39 } else if ((mqc->c & 0x8000000)){
40 (*mqc->bp)++;
41 mqc->c &= 0x7ffffff;
42 goto retry;
43 } else{
44 mqc->bp++;
45 *mqc->bp = mqc->c >> 19;
46 mqc->c &= 0x7ffff;
47 mqc->ct = 8;
48 }
49}
50
51static void renorme(MqcState *mqc)
52{
53 do{
54 mqc->a += mqc->a;
55 mqc->c += mqc->c;
56 if (!--mqc->ct)
57 byteout(mqc);
58 } while (!(mqc->a & 0x8000));
59}
60
61static void setbits(MqcState *mqc)
62{
63 int tmp = mqc->c + mqc->a;
64 mqc->c |= 0xffff;
65 if (mqc->c >= tmp)
66 mqc->c -= 0x8000;
67}
68
69void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
70{
71 ff_mqc_init_contexts(mqc);
72 mqc->a = 0x8000;
73 mqc->c = 0;
74 mqc->bp = bp-1;
75 mqc->bpstart = bp;
76 mqc->ct = 12 + (*mqc->bp == 0xff);
77}
78
79void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
80{
81 int qe;
82
83 qe = ff_mqc_qe[*cxstate];
84 mqc->a -= qe;
85 if ((*cxstate & 1) == d){
86 if (!(mqc->a & 0x8000)){
87 if (mqc->a < qe)
88 mqc->a = qe;
89 else
90 mqc->c += qe;
91 *cxstate = ff_mqc_nmps[*cxstate];
92 renorme(mqc);
93 } else
94 mqc->c += qe;
95 } else{
96 if (mqc->a < qe)
97 mqc->c += qe;
98 else
99 mqc->a = qe;
100 *cxstate = ff_mqc_nlps[*cxstate];
101 renorme(mqc);
102 }
103}
104
105int ff_mqc_length(MqcState *mqc)
106{
107 return mqc->bp - mqc->bpstart;
108}
109
110int ff_mqc_flush(MqcState *mqc)
111{
112 setbits(mqc);
113 mqc->c = mqc->c << mqc->ct;
114 byteout(mqc);
115 mqc->c = mqc->c << mqc->ct;
116 byteout(mqc);
117 if (*mqc->bp != 0xff)
118 mqc->bp++;
119 return mqc->bp - mqc->bpstart;
120}
121
122int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
123{
124 MqcState mqc2 = *mqc;
125 mqc2.bpstart=
126 mqc2.bp = dst;
127 *mqc2.bp = *mqc->bp;
128 ff_mqc_flush(&mqc2);
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);
134 (*dst_len) --;
135 memmove(dst, dst+1, *dst_len);
136 return mqc->bp - mqc->bpstart + 1 + *dst_len;
137 }
138 return mqc->bp - mqc->bpstart + *dst_len;
139}
140