FFmpeg
mqcenc.c
Go to the documentation of this file.
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 
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 
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 
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 
70 {
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 
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 
106 {
107  return mqc->bp - mqc->bpstart;
108 }
109 
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 
122 int 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 }
int ff_mqc_length(MqcState *mqc)
number of encoded bytes
Definition: mqcenc.c:105
void ff_mqc_init_contexts(MqcState *mqc)
MQ-coder context initialisations.
Definition: mqc.c:111
MQ-coder.
uint8_t
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition: mqcenc.c:79
int ff_mqc_flush(MqcState *mqc)
flush the encoder [returns number of bytes encoded]
Definition: mqcenc.c:110
uint16_t ff_mqc_qe[2 *47]
Definition: mqc.c:93
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition: mqcenc.c:69
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
Definition: mqcenc.c:122
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
uint8_t ff_mqc_nlps[2 *47]
Definition: mqc.c:94
unsigned int a
Definition: mqc.h:42
static void setbits(MqcState *mqc)
Definition: mqcenc.c:61
unsigned int ct
Definition: mqc.h:44
uint8_t ff_mqc_nmps[2 *47]
Definition: mqc.c:95
unsigned int c
Definition: mqc.h:43
uint8_t * bpstart
Definition: mqc.h:41
static void byteout(MqcState *mqc)
Definition: mqcenc.c:31
static void renorme(MqcState *mqc)
Definition: mqcenc.c:51
Definition: mqc.h:40
uint8_t * bp
Definition: mqc.h:41
static uint8_t tmp[11]
Definition: aes_ctr.c:26