00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "mqc.h"
00029
00030 static void byteout(MqcState *mqc)
00031 {
00032 retry:
00033 if (*mqc->bp == 0xff){
00034 mqc->bp++;
00035 *mqc->bp = mqc->c >> 20;
00036 mqc->c &= 0xfffff;
00037 mqc->ct = 7;
00038 } else if ((mqc->c & 0x8000000)){
00039 (*mqc->bp)++;
00040 mqc->c &= 0x7ffffff;
00041 goto retry;
00042 } else{
00043 mqc->bp++;
00044 *mqc->bp = mqc->c >> 19;
00045 mqc->c &= 0x7ffff;
00046 mqc->ct = 8;
00047 }
00048 }
00049
00050 static void renorme(MqcState *mqc)
00051 {
00052 do{
00053 mqc->a += mqc->a;
00054 mqc->c += mqc->c;
00055 if (!--mqc->ct)
00056 byteout(mqc);
00057 } while (!(mqc->a & 0x8000));
00058 }
00059
00060 static void setbits(MqcState *mqc)
00061 {
00062 int tmp = mqc->c + mqc->a;
00063 mqc->c |= 0xffff;
00064 if (mqc->c >= tmp)
00065 mqc->c -= 0x8000;
00066 }
00067
00068 void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
00069 {
00070 ff_mqc_init_contexts(mqc);
00071 mqc->a = 0x8000;
00072 mqc->c = 0;
00073 mqc->bp = bp-1;
00074 mqc->bpstart = bp;
00075 mqc->ct = 12 + (*mqc->bp == 0xff);
00076 }
00077
00078 void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
00079 {
00080 int qe;
00081
00082 qe = ff_mqc_qe[*cxstate];
00083 mqc->a -= qe;
00084 if ((*cxstate & 1) == d){
00085 if (!(mqc->a & 0x8000)){
00086 if (mqc->a < qe)
00087 mqc->a = qe;
00088 else
00089 mqc->c += qe;
00090 *cxstate = ff_mqc_nmps[*cxstate];
00091 renorme(mqc);
00092 } else
00093 mqc->c += qe;
00094 } else{
00095 if (mqc->a < qe)
00096 mqc->c += qe;
00097 else
00098 mqc->a = qe;
00099 *cxstate = ff_mqc_nlps[*cxstate];
00100 renorme(mqc);
00101 }
00102 }
00103
00104 int ff_mqc_length(MqcState *mqc)
00105 {
00106 return mqc->bp - mqc->bpstart;
00107 }
00108
00109 int ff_mqc_flush(MqcState *mqc)
00110 {
00111 setbits(mqc);
00112 mqc->c = mqc->c << mqc->ct;
00113 byteout(mqc);
00114 mqc->c = mqc->c << mqc->ct;
00115 byteout(mqc);
00116 if (*mqc->bp != 0xff)
00117 mqc->bp++;
00118 return mqc->bp - mqc->bpstart;
00119 }