00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "dmx.h"
00012 #include <stdio.h>
00013
00014 DMX::DMX(
00015 Id n,
00016 In<Bits>& jpegBits,
00017 Out<Bits>& quantTableBits,
00018 Out<Bits>& huffTableBits,
00019 Out<Bits>& frameHeaderBits,
00020 Out<Bits>& scanHeaderBits,
00021 Out<Bits>& MCUBits,
00022 Out<Bits>& restartIntervalBitsOut,
00023 Out<Marker>& DHTmarkerOut,
00024 Out<Marker>& DQTmarkerOut)
00025 :
00026 Process(n),
00027 jpegIn( id("jpegIn"), jpegBits),
00028 quantizationTableOut( id("quantizationTableOut"), quantTableBits),
00029 huffmanTableOut( id("huffmanTableOut"), huffTableBits),
00030 frameHeaderOut( id("frameHeaderOut"), frameHeaderBits),
00031 scanHeaderOut( id("scanHeaderOut"), scanHeaderBits),
00032 MCUOut( id("MCUOut"), MCUBits),
00033 restartIntervalBitsOutP(id("restartIntervalBitsOutP"),restartIntervalBitsOut),
00034 DHTmarkerOutP( id("DHTmarkerOutP"), DHTmarkerOut),
00035 DQTmarkerOutP( id("DQTmarkerOutP"), DQTmarkerOut)
00036 {
00037 }
00038
00039 void DMX::abort()
00040 {
00041 printf("\n");
00042 printf("%s:\n", fullName());
00043 printf("\tERROR:\tAbnormal end of decompression process!\n");
00044 exit(1);
00045 }
00046
00047 void DMX::main()
00048 {
00049 Bits bits;
00050 Bits bits1;
00051 Marker marker;
00052
00053
00054 get_marker(marker);
00055 if (marker == SOI_MK)
00056 {
00057 #ifdef VERBOSE
00058 printf("%s:\n\tFound the SOI marker!\n", fullName());
00059 #endif
00060 } else {
00061 #ifdef VERBOSE
00062 printf("%s:\n\tCould not find the SOI marker!\n", fullName());
00063 #endif
00064 abort();
00065 }
00066
00067
00068 get_marker(marker);
00069 while (marker != EOI_MK)
00070 {
00071 Bits firstc;
00072 Bits nextc;
00073 unsigned int size;
00074
00075 switch (marker)
00076 {
00077 case COM_MK:
00078 #ifdef VERBOSE
00079 printf("%s:\n\tSkipping comments\n", fullName());
00080 #endif
00081 get_marker(marker);
00082 break;
00083
00084 case DQT_MK:
00085 #ifdef VERBOSE
00086 printf("%s:\n\tDefining Quantization Tables\n", fullName());
00087 #endif
00088 write(DQTmarkerOutP, marker);
00089 while (get_bits(bits,marker))
00090 {
00091 write(quantizationTableOut, bits);
00092 }
00093 break;
00094
00095 case SOF_MK:
00096 #ifdef VERBOSE
00097 printf("%s:\n\tFound the SOF marker!\n",fullName());
00098 #endif
00099 while (get_bits(bits,marker))
00100 {
00101 write(frameHeaderOut, bits);
00102 }
00103 break;
00104
00105 case DHT_MK:
00106 #ifdef VERBOSE
00107 printf("%s:\n\tDefining Huffman Tables\n",fullName());
00108 #endif
00109 write(DHTmarkerOutP, marker);
00110 while (get_bits(bits,marker))
00111 {
00112 write(huffmanTableOut, bits);
00113 }
00114 break;
00115
00116 case DRI_MK:
00117 {
00118 #ifdef VERBOSE
00119 printf("%s:\n\tDefining Restart Interval\n",fullName());
00120 #endif
00121 write(DHTmarkerOutP, marker);
00122
00123 read(jpegIn, firstc);
00124 size = firstc;
00125 read(jpegIn, nextc);
00126 size = (size<<8) | nextc;
00127
00128 for(unsigned int i=0; i<size-2; i++)
00129 {
00130 read(jpegIn, bits);
00131 write(restartIntervalBitsOutP, bits);
00132 }
00133
00134 get_marker(marker);
00135
00136 break;
00137 }
00138 case SOS_MK:
00139 {
00140 #ifdef VERBOSE
00141 printf("%s:\n\tFound the SOS marker!\n",fullName());
00142 #endif
00143 write(DHTmarkerOutP, marker);
00144 write(DQTmarkerOutP, marker);
00145
00146 read(jpegIn, firstc);
00147 write(scanHeaderOut, firstc);
00148 size = firstc;
00149 read(jpegIn, nextc);
00150 write(scanHeaderOut, nextc);
00151 size = (size<<8) | nextc;
00152
00153 for(unsigned int i=0; i<size-2; i++)
00154 {
00155 read(jpegIn, bits);
00156 write(scanHeaderOut, bits);
00157 }
00158
00159 bool newmarker;
00160 newmarker = false;
00161 while (!newmarker)
00162 {
00163 read(jpegIn, bits);
00164 if (bits != 0xFF)
00165 {
00166 write(MCUOut, bits);
00167 } else {
00168 read(jpegIn, bits1);
00169 if (bits1 == 0x00 || RST_MK(0xFF00|bits1))
00170 {
00171 write(MCUOut, bits);
00172 write(MCUOut, bits1);
00173 } else {
00174 marker = (0xFF00 | bits1);
00175 newmarker = true;
00176 }
00177 }
00178 }
00179 break;
00180 }
00181 case EOF:
00182 #ifdef VERBOSE
00183 printf("%s:\n\tRan out of input data!\n",fullName());
00184 #endif
00185 abort();
00186
00187 default:
00188 {
00189 if ((marker&MK_MSK) == APP_MK)
00190 {
00191 Bits firstc;
00192 Bits nextc;
00193 unsigned int size;
00194
00195 read(jpegIn, firstc);
00196 size = firstc;
00197 read(jpegIn, nextc);
00198 size = (size<<8) | nextc;
00199
00200 #ifdef VERBOSE
00201 printf("%s:\n\tSkipping application data\n",fullName());
00202 #endif
00203 for(unsigned int i=0; i<size-2; i++)
00204 {
00205 read(jpegIn, bits);
00206 }
00207
00208 get_marker(marker);
00209 } else {
00210 #ifdef VERBOSE
00211 printf("%s:\n\tWARNING:\tLost sync outside scan, %x!\n",fullName(),marker);
00212 #endif
00213 abort();
00214 }
00215 break;
00216 }
00217 }
00218 }
00219 #ifdef VERBOSE
00220 printf("%s:\n\tFound the EOI marker!\n",fullName());
00221 #endif
00222 }
00223
00224 bool DMX::get_bits(Bits& bits, Marker& marker)
00225 {
00226 read(jpegIn, bits);
00227 if (bits == 0xFF)
00228 {
00229 read(jpegIn, bits);
00230 if (bits == 0x00)
00231 {
00232 bits = 0xFF;
00233 return true;
00234 } else {
00235 marker = (0xFF00 | bits);
00236 return false;
00237 }
00238 }
00239 return true;
00240 }
00241
00242 void DMX::get_marker(Marker& marker)
00243 {
00244 Bits bits;
00245 while (get_bits(bits,marker));
00246 }