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
byteout(MqcState * mqc)31 static void byteout(MqcState *mqc)
32 {
33 retry:
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
renorme(MqcState * mqc)51 static 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
setbits(MqcState * mqc)61 static 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
ff_mqc_initenc(MqcState * mqc,uint8_t * bp)69 void 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
ff_mqc_encode(MqcState * mqc,uint8_t * cxstate,int d)79 void 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
ff_mqc_flush(MqcState * mqc)105 int ff_mqc_flush(MqcState *mqc)
106 {
107 setbits(mqc);
108 mqc->c = mqc->c << mqc->ct;
109 byteout(mqc);
110 mqc->c = mqc->c << mqc->ct;
111 byteout(mqc);
112 if (*mqc->bp != 0xff)
113 mqc->bp++;
114 return mqc->bp - mqc->bpstart;
115 }
116
ff_mqc_flush_to(MqcState * mqc,uint8_t * dst,int * dst_len)117 int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
118 {
119 MqcState mqc2 = *mqc;
120 mqc2.bpstart=
121 mqc2.bp = dst;
122 *mqc2.bp = *mqc->bp;
123 ff_mqc_flush(&mqc2);
124 *dst_len = mqc2.bp - dst;
125 if (mqc->bp < mqc->bpstart) {
126 av_assert1(mqc->bpstart - mqc->bp == 1);
127 av_assert1(*dst_len > 0);
128 av_assert1(mqc->bp[0] == 0 && dst[0] == 0);
129 (*dst_len) --;
130 memmove(dst, dst+1, *dst_len);
131 return mqc->bp - mqc->bpstart + 1 + *dst_len;
132 }
133 return mqc->bp - mqc->bpstart + *dst_len;
134 }
135