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 bytein(MqcState *mqc)
00031 {
00032 if (*mqc->bp == 0xff){
00033 if (*(mqc->bp+1) > 0x8f)
00034 mqc->c++;
00035 else{
00036 mqc->bp++;
00037 mqc->c += 2 + 0xfe00 - (*mqc->bp << 9);
00038 }
00039 } else{
00040 mqc->bp++;
00041 mqc->c += 1 + 0xff00 - (*mqc->bp << 8);
00042 }
00043 }
00044
00045 static int exchange(MqcState *mqc, uint8_t *cxstate, int lps)
00046 {
00047 int d;
00048 if ((mqc->a < ff_mqc_qe[*cxstate]) ^ (!lps)){
00049 if (lps)
00050 mqc->a = ff_mqc_qe[*cxstate];
00051 d = *cxstate & 1;
00052 *cxstate = ff_mqc_nmps[*cxstate];
00053 } else{
00054 if (lps)
00055 mqc->a = ff_mqc_qe[*cxstate];
00056 d = 1 - (*cxstate & 1);
00057 *cxstate = ff_mqc_nlps[*cxstate];
00058 }
00059
00060 do{
00061 if (!(mqc->c & 0xff)){
00062 mqc->c -= 0x100;
00063 bytein(mqc);
00064 }
00065 mqc->a += mqc->a;
00066 mqc->c += mqc->c;
00067 } while (!(mqc->a & 0x8000));
00068 return d;
00069 }
00070
00071 void ff_mqc_initdec(MqcState *mqc, uint8_t *bp)
00072 {
00073 ff_mqc_init_contexts(mqc);
00074 mqc->bp = bp;
00075 mqc->c = (*mqc->bp ^ 0xff) << 16;
00076 bytein(mqc);
00077 mqc->c = mqc->c << 7;
00078 mqc->a = 0x8000;
00079 }
00080
00081 int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate)
00082 {
00083 mqc->a -= ff_mqc_qe[*cxstate];
00084 if ((mqc->c >> 16) < mqc->a){
00085 if (mqc->a & 0x8000)
00086 return *cxstate & 1;
00087 else
00088 return exchange(mqc, cxstate, 0);
00089 } else {
00090 mqc->c -= mqc->a << 16;
00091 return exchange(mqc, cxstate, 1);
00092 }
00093 }