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 
69 void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
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 
105 static int 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 
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  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 }
mqc.h
MQ-coder.
mqc_flush
static int mqc_flush(MqcState *mqc)
Definition: mqcenc.c:105
ff_mqc_flush_to
int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
flush the encoder [returns number of bytes encoded]
Definition: mqcenc.c:117
setbits
static void setbits(MqcState *mqc)
Definition: mqcenc.c:61
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
ff_mqc_qe
const uint16_t ff_mqc_qe[2 *47]
Definition: mqc.c:33
ff_mqc_nmps
const uint8_t ff_mqc_nmps[2 *47]
Definition: mqc.c:55
MqcState::bpstart
uint8_t * bpstart
Definition: mqc.h:41
avassert.h
MqcState::bp
uint8_t * bp
Definition: mqc.h:41
renorme
static void renorme(MqcState *mqc)
Definition: mqcenc.c:51
ff_mqc_initenc
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition: mqcenc.c:69
MqcState::ct
unsigned int ct
Definition: mqc.h:44
ff_mqc_nlps
const uint8_t ff_mqc_nlps[2 *47]
Definition: mqc.c:47
ff_mqc_encode
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition: mqcenc.c:79
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
MqcState::c
unsigned int c
Definition: mqc.h:43
MqcState::a
unsigned int a
Definition: mqc.h:42
d
d
Definition: ffmpeg_filter.c:153
MqcState
Definition: mqc.h:40
ff_mqc_init_contexts
void ff_mqc_init_contexts(MqcState *mqc)
MQ-coder context initialisations.
Definition: mqc.c:64
byteout
static void byteout(MqcState *mqc)
Definition: mqcenc.c:31