home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / jpeg / huffdecode.s next >
Encoding:
Text File  |  1995-05-03  |  4.9 KB  |  267 lines

  1. /
  2. / Copyright (C) 1992-1993 Michael Davidson.
  3. / All rights reserved.
  4. /
  5. / Permission to use, copy, modify, and distribute this software
  6. / and its documentation for any purpose and without fee is hereby
  7. / granted, provided that the above copyright notice appear in all
  8. / copies and that both that copyright notice and this permission
  9. / notice appear in supporting documentation.
  10. /
  11. / This software is provided "as is" without express or implied warranty.
  12. /
  13.     .data
  14.  
  15. zigzag:    .byte     0,  1,  8, 16,  9,  2,  3, 10
  16.     .byte    17, 24, 32, 25, 18, 11,  4,  5
  17.     .byte    12, 19, 26, 33, 40, 48, 41, 34
  18.     .byte    27, 20, 13,  6,  7, 14, 21, 28
  19.     .byte    35, 42, 49, 56, 57, 50, 43, 36
  20.     .byte    29, 22, 15, 23, 30, 37, 44, 51
  21.     .byte    58, 59, 52, 45, 38, 31, 39, 46
  22.     .byte    53, 60, 61, 54, 47, 55, 62, 63
  23.      .byte     0,  0,  0,  0,  0,  0,  0,  0
  24.      .byte     0,  0,  0,  0,  0,  0,  0,  0
  25.  
  26.     .text
  27. /
  28. / jpegHuffDecode(fp, bptr, dc_tbl, ac_tbl, dc_pred, quant_tbl)
  29. /
  30. FP        =    8
  31. DBLOCK        =    12
  32. DC_TBL        =    16
  33. AC_TBL        =    20
  34. DC_PRED        =    24
  35. QUANT_TBL     =    28
  36.  
  37. RET_CODE    =    -4
  38. QUANT_0        =    -8
  39. DB_PTR        =    -12
  40.     .globl    jpegHuffDecode
  41.  
  42. jpegHuffDecode:
  43.     pushl  %ebp
  44.     movl   %esp, %ebp
  45.     subl   $12, %esp
  46.     pushl  %ebx
  47.     pushl  %edi
  48.     pushl  %esi
  49.  
  50.     xorl   %eax, %eax
  51.     movl   %eax, RET_CODE(%ebp)
  52. /
  53. / Save the first entry in the quant table and set the table
  54. / entry to 1. This allows us to fix up the DC coefficient later
  55. /
  56.     movl   QUANT_TBL(%ebp), %esi
  57.     movb   $0x1, %al
  58.     xchgl  (%esi), %eax
  59.     movl   %eax, QUANT_0(%ebp)
  60. /
  61. / load bits_left and get_buffer into registers and make a local copy of db_ptr
  62. /
  63.     movl   db_ptr, %eax
  64.     movb   bits_left, %bl
  65.     movl   get_buffer, %esi
  66.     movl   %eax, DB_PTR(%ebp)
  67. /
  68. / for (k = 0; hc = dc; k < DCTSIZE2; k++, hc = ac)
  69. /
  70.     xorb   %bh, %bh
  71.     movl   DC_TBL(%ebp), %edi
  72.     xchgl  %ebx,%ebx
  73.     .align    4
  74. L05:
  75. /
  76. /    if (bits_left < 16)
  77. /    MORE_BITS(fp);
  78. /
  79.     cmpb   $16, %bl
  80.     jge    L25
  81.     call   more_bits
  82.  
  83. /    for (code = 0; code > hc->maxcode; hc++)
  84. /    {
  85. /    code        += code + (get_buffer >> 31);
  86. /    get_buffer    <<= 1;
  87. /    --bits_left;
  88. /    }
  89.  
  90. L25:
  91.     xorl   %eax, %eax
  92.     .align    8
  93. L30:
  94.     addl   %esi, %esi
  95.     leal   8(%edi), %edi
  96.     adcl   %eax, %eax
  97.     decb   %bl
  98.     cmpl   (%edi), %eax
  99.     jg     L30
  100.  
  101. /    if (hc->codeptr == NULL || bits_left < 0)
  102. /    JPEGError("Corrupted data in JPEG file");
  103. /
  104. /    r = hc->codeptr[code];
  105. /    s = r & 15;
  106. /    r = r >> 4;
  107. /    if (s)
  108. /    {
  109. /    k += r;
  110. /    if (get_buffer & 0x80000000)
  111. /        s = get_buffer >> (32 - s);
  112. /    else
  113. /        s = (get_buffer >> (32 - s)) + (-1 << s) + 1;
  114. /    get_buffer    <<= s;
  115. /    bits_left    -= s;
  116. /    (*block)[ZAG[k]] = (JCOEF) (((JCOEF) s) * quanttbl[k]);
  117. /    }
  118.  
  119.     movl   4(%edi), %edi
  120.     orl    %edi, %edi
  121.     je     corrupt_file
  122.  
  123.     orb    %bl, %bl
  124.     jl     corrupt_file
  125.  
  126.     movb   (%edi,%eax,1), %ch
  127.     movb   %ch, %cl
  128.     shrb   $4, %ch
  129.     andb   $0xf, %cl
  130.     je     L35
  131.  
  132.     addb   %ch, %bh        / k += r
  133.     cmpb   %bl, %cl
  134.     jle    L32
  135.  
  136.     pushl  %ecx            / save %cl over call
  137.     call   more_bits
  138.     popl   %ecx
  139.  
  140. L32:
  141.     subb   %cl, %bl
  142.     cmpl   $0x80000000, %esi
  143.     sbbl   %eax, %eax
  144.     shldl  %esi, %eax
  145. /    adcl   $0, %eax
  146.     .byte    0x83, 0xd0, 0x00    / temp kludge to match masm output
  147.     shll   %cl, %esi
  148.  
  149.     xorl   %edx,%edx
  150.     xorl   %ecx,%ecx
  151.  
  152.     movb   %bh, %dl            / edx = k
  153.     movb   zigzag(%edx), %cl
  154.     leal   (,%ecx,4), %ecx
  155.     addl   DBLOCK(%ebp), %ecx
  156.  
  157.     movl   QUANT_TBL(%ebp),%edi
  158.     imull  (%edi,%edx,4), %eax
  159.  
  160.     movl   %eax, (%ecx)
  161.  
  162.     incb   %bh
  163.     movl   AC_TBL(%ebp), %edi    / hc = ac
  164.     cmpb   $64, %bh
  165.     jl     L05
  166.  
  167. done:
  168. /
  169. / store updated values in bits_left, get_buffer and db_ptr
  170. /
  171.     movb   %bl, bits_left
  172.     movl   %esi, get_buffer
  173.     movl   DB_PTR(%ebp), %eax
  174.     movl   %eax, db_ptr
  175. /
  176. / fix quant table and DC coefficient
  177. /
  178.     movl   QUANT_0(%ebp), %eax
  179.     movl   QUANT_TBL(%ebp), %edi
  180.     movl   DBLOCK(%ebp), %ecx
  181.     movl   DC_PRED(%ebp), %edx
  182.     movl   %eax, (%edi)        / quanttbl[0] = q0
  183.     movl   (%ecx), %edi        / s = (*block)[0]
  184.     addl   (%edx), %edi
  185.     movl   %edi, (%edx)
  186.     imull  %edi, %eax
  187.     movl   %eax, (%ecx)
  188.  
  189.     movl   RET_CODE(%ebp), %eax
  190.  
  191.     popl   %esi
  192.     popl   %edi
  193.     popl   %ebx
  194.     movl   %ebp, %esp
  195.     popl   %ebp
  196.     ret    
  197.  
  198. /    else if (k != 0)
  199. /    {
  200. /        if (r != 15)
  201. /        break;
  202. /        k += 15;
  203. /    }
  204. L35:
  205.     orb    %bh, %bh
  206.     je     L36
  207.     cmpb   $15, %ch
  208.     jne    done
  209.     addb   $16, %bh
  210.     movl   AC_TBL(%ebp), %edi        / hc = ac
  211.     cmpb   $64, %bh
  212.     jl     L05
  213.     jmp    done
  214.  
  215. L36:
  216.     incb   %bh
  217.     movl   AC_TBL(%ebp), %edi        / hc = ac
  218.     jmp    L05
  219.  
  220. corrupt_file:
  221.     movl   $-1, RET_CODE(%ebp)
  222.     jmp    done
  223.  
  224. /
  225. / need to refill buffer
  226. /
  227.     .align    8
  228. refill:
  229.     pushl  FP(%ebp)
  230.     call   fill_decode_buf
  231.     addl   $0x4, %esp
  232.     movl   db_ptr, %edx
  233.     movl   %edx, DB_PTR(%ebp)
  234.  
  235.     .align 8
  236. more_bits:
  237.     movl   DB_PTR(%ebp), %edx
  238. .M10:
  239.     xorl   %eax, %eax
  240.     movb   (%edx), %al
  241.     incl   %edx
  242.     cmpb   $0xff, %al        / check marker
  243.     je     .M40
  244. .M20:
  245.     addb   $8, %bl
  246.     movb   $32, %cl
  247.     subb   %bl, %cl
  248.     shll   %cl, %eax
  249.     orl    %eax, %esi
  250.     cmpb   $24, %bl
  251.     jle    .M10
  252.     movl   %edx, DB_PTR(%ebp)
  253. .M30:
  254.     ret    
  255.  
  256. .M40:
  257.     movb   (%edx), %ah
  258.     incl   %edx
  259.     orb    %ah, %ah            / if next byte is 0 it's not a marker
  260.     je     .M20
  261. /
  262. / got some kind of marker
  263. /
  264.     cmpl   db_end, %edx        / if we are at the end of the buffer
  265.     je     refill            / try to refill it
  266.     ret                / otherwise bail out
  267.