implementing jpeg with tms320c2xx assembly language software
TRANSCRIPT
Application ReportSPRA615 - January 2000
1
Implementing JPEG with TMS320C2xxAssembly Language Software
Vivian Shao Digital Signal Processing Solutions
ABSTRACT
JPEG compression/decompression technique is widely used for still image applications.There are two major ways to implement JPEG algorithm – hardware and software. Thehardware approach is good for speed but it has less flexibility and it costs more. The softwareapproach, on the other hand, is less expensive and more flexible. Implementing JPEG withTMS320C2xx DSP software is a good solution for consumer image products, such as digitalstill camera and color printer. This application report introduces the basic concepts of JPEGand the TMS320C2xx software is attached in the Appendices.
Contents1 Introduction 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 JPEG Algorithm Overview 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 JPEG Encoding 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1 DCT 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.1 ZIGZAG 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.2 Huffman Encoding 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.3 Encoding DC coefficients 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.4 Encoding AC Coefficients 6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 JPEG Decoding 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.1 Huffman Decoding 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.2 Dequantization 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.3 IDCT 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2.4 Image Reconstruction 8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 Conclusion 8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Appendix A JPEG Encoding Software 9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Appendix B JPEG Decoding Software 25. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Appendix C JPEG Tables 40. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Appendix D Huffman AC Coefficient Tables 47. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
List of Figures
Figure 1. JPEG Encoding Block Diagram 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Figure 2. JPEG Decoding Block Diagram 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Figure 3. Zig-Zag Order 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Figure 4. Difference Magnitude Categories for DC Coding 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Figure 5. Table for Luminance DC Difference 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Figure 6. Table for Chrominance DC Difference 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Figure 7. Categories Assigned to Coefficient Values 6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SPRA615
2 Implementing JPEG with TMS320C2xx Assembly Language Software
1 Introduction
This application report includes the following sections: JPEG Algorithm Overview, JPEG Encoding,JPEG Decoding, and Conclusion. In the JPEG compression and JPEG decompression sections, thedetailed algorithms, such as DCT/IDCT, quantization, ZIGZAG, Huffman encode/decode, will beintroduced and some of implementation tricks will be described as well. The TMS320C2xx JPEGsource code is attached in the Appendices.
2 JPEG Algorithm Overview
The JPEG (Joint Photographic Experts Group) technical specification is applicable tocontinuous-tone-grayscale or color digital still image data. It is applicable to a wide range ofapplications that require use of compressed images. This specification:
• Specifies processes for converting source image data to compressed image data
• Specifies processes for converting compressed image data to reconstructed image data
• Gives guidance on how to implement these processes in practice
• Specifies coded representations for compressed image data
Figure 1 shows the main procedure for JPEG encoding process based on the DCT (discretecosine transform). The original image should be grouped into 8x8 blocks first. Each 8x8 block istransferred by forward DCT into a set of 64 values referred to as DCT coefficients. Each of the64 coefficients is then quantized using a quantization table with 64 corresponding values. BeforeHuffman encoding, the entropy encoding, the two-dimensional image data is converted toone-dimensional zigzag sequences.
DCT QUANT. ZIGZAG HUFFMANENCODE
8x8 block
Original ImageCompressed Data
QUANT.TABLE HUFFMAN
TABLE8x8 block
Figure 1. JPEG Encoding Block Diagram
Figure 2 shows the main procedure for JPEG decoding process, which is the reversed processof encoding. The detailed algorithms and implementation techniques will be discussed in thefollowing sections.
IDCTDeQUANT.8x8 blockreconstruction
HUFFMANDECODE
Recovered Image
Compressed Data
DEQUANT.TABLEHUFFMAN
DECODETABLE
Figure 2. JPEG Decoding Block Diagram
SPRA615
3 Implementing JPEG with TMS320C2xx Assembly Language Software
3 JPEG Encoding
3.1 DCT
The first step in JPEG encoding is level shifting of image by 128, i.e., image data minus 128.After which the forward DCT is applied onto the image data to transform the originaltwo-dimensional data into a set of coefficients. One of these values is referred as the DCcoefficient and the other 63 as the AC coefficients. The forward DCT equation is
Fv, u � 14
cu cv �7
x�0
�7y�0
fy, x cos(2x � 1)u�
16cos
(2y � 1)v�16
It can be written in matrix multiplication form as
[Y] � [A] � [X] � [A]T
where
[X] is the 8 x 8 input data matrix
[A] is the 8 x 8 DCT coefficient matrix
[Y] is the result of DCT transformation
The coefficient matrix [A] for forward DCT is
[A] �
���������������
�
�
12�
cos �
16
cos 2�16
cos 3�16
cos 4�16
cos 5�16
cos 6�16
cos 7�16
12�
cos 3�16
cos 6�16
cos 9�16
cos 12�16
cos 15�16
cos 18�16
cos 21�16
12�
cos 5�16
cos 10�16
cos 15�16
cos 20�16
cos 25�16
cos 30�16
cos 35�16
12�
cos 7�16
cos 14�16
cos 21�16
cos 28�16
cos 35�16
cos 42�16
cos 49�16
12�
� cos 7�16
cos 14�16
� cos 21�16
cos 28�16
� cos 35�16
cos 42�16
� cos 49�16
12�
� cos 5�16
cos 10�16
� cos 15�16
cos 20�16
� cos 25�16
cos 30�16
� cos 35�16
12�
� cos 3�16
cos 6�16
� cos 9�16
cos 12�16
� cos 15�16
cos 18�16
� cos 21�16
12�
� cos �
16
cos 2�16
� cos 3�16
cos 4�16
� cos 5�16
cos 6�16
� cos 7�16
���������������
DSP supports powerful instruction “MAC” for multiply-and-accumulate operations, which can beused to code DCT very efficiently. The DCT subroutine is shown in Appendix A.
Each of the 64 coefficients is then quantized using one of 64 corresponding values from aquantization table. No default values for quantization tables are specified in the JPEGspecification; applications may specify values which customize picture quality for their particularimage characteristics, display devices, and viewing conditions.
The quantization table is declared in “tables.asm” file, which is attached in Appendix C.
(1)
(2)
(3)
SPRA615
4 Implementing JPEG with TMS320C2xx Assembly Language Software
3.1.1 ZIGZAG
After quantization, the DC coefficient and the 63 AC coefficients are prepared for entropy, which isHuffman encoding. The previous quantized DC coefficient is used to predict the current quantizedDC coefficient, and the difference is encoded. The 63 quantized AC coefficients undergo no suchdifferential encoding, but are converted into a one-dimensional zig-zag sequence, as shown inFigure 3, where the numbers are the zig-zag orders, notified as ZZ(0), ZZ(1),...,ZZ(63).
ÉÉÉÉ
0ÉÉÉÉ
1ÉÉÉÉ
5ÉÉÉÉÉÉ
6ÉÉÉÉ
14ÉÉÉÉ
15ÉÉÉÉÉÉ
27ÉÉÉÉ
28ÉÉÉÉ
2ÉÉÉÉ
4ÉÉÉÉ
7ÉÉÉÉÉÉ
13ÉÉÉÉ
16ÉÉÉÉ
26ÉÉÉÉÉÉ
29ÉÉÉÉ
42ÉÉÉÉ
3ÉÉÉÉ
8ÉÉÉÉ
12ÉÉÉÉÉÉ
17ÉÉÉÉ
25ÉÉÉÉ
30ÉÉÉÉÉÉ
41ÉÉÉÉ
43ÉÉÉÉ
9ÉÉÉÉ
11ÉÉÉÉ
18ÉÉÉÉÉÉ
24ÉÉÉÉ
31ÉÉÉÉ
40ÉÉÉÉÉÉ
44ÉÉÉÉ
53ÉÉÉÉ
10ÉÉÉÉ
19ÉÉÉÉ
23ÉÉÉÉÉÉ
32ÉÉÉÉ
39ÉÉÉÉ
45ÉÉÉÉÉÉ
52ÉÉÉÉ
54ÉÉÉÉ
20ÉÉÉÉ
22ÉÉÉÉ
33ÉÉÉÉÉÉ
38ÉÉÉÉ
46ÉÉÉÉ
51ÉÉÉÉÉÉ
55ÉÉÉÉ
60ÉÉÉÉ
21ÉÉÉÉ
34ÉÉÉÉ
37ÉÉÉÉÉÉ
47ÉÉÉÉ
50ÉÉÉÉ
56ÉÉÉÉÉÉ
59ÉÉÉÉ
61ÉÉÉÉ
35ÉÉÉÉ
36ÉÉÉÉ
48ÉÉÉÉÉÉ
49ÉÉÉÉ
57ÉÉÉÉ
58ÉÉÉÉÉÉ
62ÉÉÉÉ
63
Figure 3. Zig-Zag Order
3.1.2 Huffman Encoding
Two coding procedures are used, one for the DC coefficient ZZ(0) and the other for the ACcoefficients ZZ(1)…ZZ(63). The coefficients are encoded in the order in which they occur in ZZ,starting with the DC coefficient.
3.1.3 Encoding DC coefficients
The DC coefficients are coded differentially, using a one-dimensional predictor, PRED, which is thequantized DC value from the most recently coded 8x8 block from the same component (Y, U, V).The difference, DIFF, is obtained from
DIFF = ZZ(0) – RPED
At the beginning of the scan and at the beginning of each restart interval, the prediction for theDC coefficient prediction is initialized to 0.
The twos-complement difference magnitudes are grouped into 12 categories, SSSS, as shownin Figure 4. A Huffman code is created for each of the 12 difference magnitude categories byHuffman DC tables, as shown in Figure 5 and Figure 6.
SPRA615
5 Implementing JPEG with TMS320C2xx Assembly Language Software
SSSS DIFF Values0 01 –1,12 –3,–2,2,33 –7,...,–4,4,...74 –15...,–8,8,...,155 –31,...,–16,16,...,316 –63,...,–32,32,...,637 –127,...,–64,64,...,1278 –255,...,–128,128,...2559 –511,...,–256,256,...,51110 –1023,...,–512,512,...102311 –2047,...,–1024,1024,...2047
Figure 4. Difference Magnitude Categories for DC Coding
Category Code Length Code Word0 2 001 3 0102 3 0113 3 1004 3 1015 3 1106 4 11107 5 111108 6 1111109 7 111111010 8 1111111011 9 111111110
Figure 5. Table for Luminance DC Difference
Category Code Length Code Word0 2 001 2 012 2 103 3 1104 4 11105 5 111106 6 1111107 7 11111108 8 111111109 9 11111111010 10 111111111011 11 11111111110
Figure 6. Table for Chrominance DC Difference
SPRA615
6 Implementing JPEG with TMS320C2xx Assembly Language Software
For each category, except SSSS=0, and additional bits field is appended to the code word touniquely identify which difference in that category actually occurred. The number of extra bits isgiven by SSSS; the extra bits are appended to the LSB of ther preceding Huffman code, mostsignificant bit first. When DIFF is positive, the SSSS low order bits of DIFF are transmitted. WhenDIFF is negative, the SSSS low order bits of (DIFF-1) are transmitted. Note that the most significantbit of the appended bit sequence is 0 for negative differences and 1 for positive differences.
3.1.4 Encoding AC Coefficients
Since many coefficients are zero, runs of zeros are identified and coded efficiently. In addition, ifthe last part of ZZ is entirely zero, this is coded explicitly as an end-of-block (EOB).
Each nonezero AC coefficient in ZZ is described by a composite 8-bit value, R/S, of the form
R/S = binary ‘RRRR/SSSS’
The four bits, ‘SSSS’, define a category for the amplitude of the next nonzero coefficient in ZZ. The‘SSSS’ is defined by category table, which is shown in Figure 7. The four bits, ‘RRRR’, give theposition of the coefficient in ZZ relative to the previous nonzero coefficient. The Huffman AC codetable consists of one Huffman code (maximum length of 16 bits) for each possible composite value.The Huffman table for luminance AC coefficients and chrominance AC coefficients are shown inAppendix D.
SSSS DIFF Values1 –1,12 –3,–2,2,33 –7,...,–4,4,...74 –15...,–8,8,...,155 –31,...,–16,16,...,316 –63,...,–32,32,...,637 –127,...,–64,64,...,1278 –255,...,–128,128,...2559 –511,...,–256,256,...,51110 –1023,...,–512,512,...102311 –2047,...,–1024,1024,...2047
Figure 7. Categories Assigned to Coefficient Values
Each Huffman code is followed by additional bits, which specify the sign and exact amplitude ofthe coefficient. The format for the additional bits is the same as in the coding of DC coefficients.ZZ(K) is the Kth coefficient in the zig-zag sequence of coefficients being coded. The value ofSSSS gives the number of additional bits required to specify the sign and precise amplitude ofthe coefficient. The additional bits are either the low-order SSSS bits of ZZ(K) when ZZ(K) ispositive or the low-order SSSS bits of ZZ(K)–1 when ZZ(K) is negative.
The JPEG encoding C2xx software, including subroutines of DCT, QUANT, HUFF_ENCODE, isattached on Appendix A. The JPEG encoding related tables, such as DCT coefficients table,quantization table, zig-zag sequence table, Huffman DC and AC coefficients tables are alldefined and implemented in “tables.asm” file, which is attached in Appendix C.
SPRA615
7 Implementing JPEG with TMS320C2xx Assembly Language Software
3.2 JPEG Decoding
3.2.1 Huffman Decoding
The Huffman decoding table is used to recover the compressed data from bit stream format to64 16-bit data. The Huffman decoding procedure is:
1. Get code word from bit stream
2. Get the information of RRRR/SSSS by looking up Huffman decoding table
3. Get the exact value from extra bits of bit stream
4. Reconstruct the 64 16-bit values before Huffman encoding
3.2.2 Dequantization
The values in Dequantization table are the inverse of the values in quantization table. Thedequantization table used here combines the dequantization value and zig-zag sequence, thatis, the data order after dequantization will be in the order of ZZ(0), ZZ(1), ZZ(5), ZZ(6), ZZ(14),ZZ(15),… as shown in Figure 3.
3.2.3 IDCT
The inverse DCT equation is
fy, x � 14�7u�0
�7y�0
cu cv Fv, u cos(2x � 1)u�
16cos
(2y � 1)v�16
Which can be represented as the form of matrix multiplication as described in DCT section. The coefficient matrix of IDCT is
�A� �
�����������������
�
12�
12�
12�
12�
12�
12�
12�
12�
cos �
16
cos 3�16
cos 5�16
cos 7�16
� cos 7�16
� cos 5�16
� cos 3�16
� cos �
16
cos 2�16
cos 6�16
cos 10�16
cos 14�16
cos 14�16
cos 10�16
cos 6�16
cos 2�16
cos 3�16
cos 9�16
cos 15�16
cos 21�16
� cos 21�16
� cos 15�16
� cos 9�16
� cos 3�16
cos 4�16
cos 12�16
cos 20�16
cos 28�16
cos 28�16
cos 20�16
cos 12�16
cos 4�16
cos 5�16
cos 15�16
cos 25�16
cos 35�16
� cos 35�16
� cos 25�16
� cos 15�16
� cos5pi16
cos 6�16
cos 18�16
cos 30�16
cos 42�16
cos 42�16
cos 30�16
cos 18�16
cos 6�16
cos 7�16
cos 21�16
cos 35�16
cos 49�16
� cos 49�16
� cos 35�16
� cos 21�16
� cos 7�16
�����������������
�
(4)
(5)
SPRA615
8 Implementing JPEG with TMS320C2xx Assembly Language Software
3.2.4 Image Reconstruction
After IDCT, the JPEG decoding of 8x8 block is completed. The last procedure is to reconstructthe 8x8 blocks to the original image. It can be done by moving the data to the original imageaddresses.
The JPEG decoding software is attached on Appendix B and the related tables is in “tables.asm”file, which is attached on Appendix C.
4 Conclusion
This application report has described the JPEG specification and explained the implementationsteps. It also provides the JPEG ’C2xx assembly source code for DSP application engineers.Some simple modification may be needed to be adapted to individual systems; however, itprovides the baisc starting for software engineers and save on development time and efforts.Note that this report only describte the encode/decode process of a basic 8x8 block under thecontext of JPEG standard. It does not include the file parsing routine required to extract headerdata information when decoding a JPEG image file that is compliant to for a JPEG file format.
SPRA615
9 Implementing JPEG With TMS320C2xx Assembly Language Software
Appendix A JPEG Encoding Software
JEPGEN.ASM;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Program Name : JPEGEN.ASM
; Discription : This program includes JPEG encoder
; The modules of JPEG includes
; 1. Frame => 8x8 blocks
; 2. JPEG Coefficients Initialization
; 3. Testing Data Initialization
; Editor : Vivian Shao
; Date : 02/20/1997
; Possessor : Texas Instruments
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
.def JPEG_EN
.def J_STATUS, J_STACK
.ref PICT
.ref IMAGE_LENGTH, IMAGE_WIDTH, NEXT_LINE, NEXT_SECTION
.ref BLOCK_ENCODE, HUFF_EN
.ref DCT_COEF, Y_QUANTIZE_TABLE, ZIGZAG_TABLE0, RDCHUFF
.ref QUANT_TABLE, ZIGZAG_TABLE, DCHUFF
.ref DCTB1, COMPRESS_DATA, RNDOFF
.ref MASKDATA, MASKSIZE, EOB, LAST_DC, LAST_RESULT
.ref DIFF, DIFF1, RZERO, IN_SIZE, COWORD, USER_BIT, LESS_BIT
.ref ABSDIFF, ABSDIFF1, TRANSDIFF, DC_HEAD, DC_TAIL
.ref SIZE, COWORD_SIZE, V_SIZE, NEG_VSIZE, JUDGE_DATA, F_TAB
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; JPEGEN VARIABLES
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
.bss J_STATUS, 2
J_STACK .usect ”.j_stack”, 10h
DCTB1_ADDR .set DCTB1
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; JPEG_EN PROGRAM BEGINS
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
.text
JPEG_EN:
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; CONTEXT SAVING ;
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
SST #0, J_STATUS
SST #1, J_STATUS+1
LDP #J_STACK
SPRA615
10 Implementing JPEG With TMS320C2xx Assembly Language Software
SAR AR1, J_STACK+1 ; AR1 is used in DCT, QUANT & HUFF_EN
SAR AR2, J_STACK+2 ; AR2 is used in DCT, QUANT & HUFF_EN
SAR AR3, J_STACK+3 ; AR3 is used in DCT, QUANT & HUFF_EN
SAR AR4, J_STACK+4 ; AR4 is used for PICT DISASSEMBLE
SAR AR5, J_STACK+5 ; AR5 is used in QUANT & HUFF_EN
POPD J_STACK+8
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; JPEG_EN MAIN ; AR4 CANNOT BE USED FOR OTHER PURPOSE
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
CALL JPEG_EN_INIT
LAR AR7, #IMAGE_WIDTH–1 ; block # of width
blocken_loop0:
LAR AR6, #IMAGE_LENGTH–1 ; block # of length
blocken_loop:
CALL BLOCK_INIT ; initialize 8x8 block data
CALL BLOCK_ENCODE ; compressed data –> COMPRESS_DATA
MAR *, AR6
BANZ blocken_loop, *–
LAR AR0, #NEXT_SECTION
MAR *, AR4
MAR *0+, AR7
BANZ blocken_loop0, *–
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; CONTEXT RESTORE
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
LDP #J_STACK
PSHD J_STACK+8
LAR AR5, J_STACK+5
LAR AR4, J_STACK+4
LAR AR3, J_STACK+3
LAR AR2, J_STACK+2
LAR AR1, J_STACK+1
LDP #0
LST #1, J_STATUS+1
LST #0, J_STATUS
RET ; retutn from JPEG_EN
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Inintialize Block Data
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
BLOCK_INIT:
LAR AR0, #NEXT_LINE
MAR *, AR4
RPT #7
SPRA615
11 Implementing JPEG With TMS320C2xx Assembly Language Software
BLDD *+, #DCTB1
MAR *0+
RPT #7
BLDD *+, #DCTB1+8
MAR *0+
RPT #7
BLDD *+, #DCTB1+16
MAR *0+
RPT #7
BLDD *+, #DCTB1+24
MAR *0+
RPT #7
BLDD *+, #DCTB1+32
MAR *0+
RPT #7
BLDD *+, #DCTB1+40
MAR *0+
RPT #7
BLDD *+, #DCTB1+48
MAR *0+
RPT #7
BLDD *+, #DCTB1+56
LAR AR0, #NEXT_SECTION
MAR *0–, AR2
LAR AR2, #DCTB1 ; data – 128
LAR AR3, #63
ji_loop:
LACC *
SUB #128
SACL *+, 0, AR3
BANZ ji_loop, *–, AR2
LDP #RNDOFF ; initialize RNDOFF value
SPLK #8000h, RNDOFF
RET ; return from BLOCK_INIT
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
;
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
JPEG_EN_INIT:
dct_init:
SPRA615
12 Implementing JPEG With TMS320C2xx Assembly Language Software
CLRC CNF ; B0 is mapped to data memory
SETC SXM ; SXM=1
SETC OVM ; OVM=1
SPM 0 ; PM=0
MAR *, AR5
LAR AR5, #0200h ; LOAD DCT COEF to B0
RPT #63
BLPD #DCT_COEF, *+
SETC CNF ; B0 is mapped to program memory
quant_init:
MAR *, AR5
LAR AR5, #QUANT_TABLE ; Initialize Quant table
RPT #63
BLPD #Y_QUANTIZE_TABLE, *+
LAR AR5, #ZIGZAG_TABLE ; Initialize ZigZag table
RPT #63
BLPD #ZIGZAG_TABLE0, *+
huffen_init:
LAR AR1, #DCHUFF
MAR *, AR1
RPT #220
BLPD #RDCHUFF, *+
LAR AR1, #COMPRESS_DATA
LACL #0
RPT #255
SACL *+
; RPT #255
; SACL *+
; RPT #255
; SACL *+
LAR AR5, #COMPRESS_DATA
LAR AR4, #PICT
LDP #DIFF
; SPLK #0, DIFF
; SPLK #0, DIFF1
; SPLK #0, ABSDIFF
; SPLK #0, ABSDIFF1
SPRA615
13 Implementing JPEG With TMS320C2xx Assembly Language Software
; SPLK #0, TRANSDIFF
; SPLK #0, RZERO
; SPLK #0, IN_SIZE
; SPLK #0, COWORD
SPLK #0FFF0h,MASKDATA
SPLK #0F000h,MASKSIZE
SPLK #0Ah, EOB ; Luminance
; SPLK #00h, EOB ; Chrominance
SPLK #0h, LAST_DC
SPLK #0h, LAST_RESULT
SPLK #010h, USER_BIT
SPLK #0h, LESS_BIT
; LDP #SIZE
; SPLK #0, SIZE
; SPLK #0, COWORD_SIZE
; SPLK #0, V_SIZE
; SPLK #0, NEG_VSIZE
; SPLK #0, JUDGE_DATA
; SPLK #0, F_TAB
RET ; retrun from JPEG_EN_INIT
BLOCKEN.ASM;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Program Name : BLOCKEN.ASM
; Discription : This program includes JPEG 8x8 block encoder
; The modules of BLOCKEN include
; 1. DCT
; 2. QUANT
; 3. HUFF
; Editor : Vivian Shao
; Date : 02/20/1997
; Possessor : Texas Instruments
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
.def BLOCK_ENCODE, IDCT, HUFF_EN
.def RNDOFF, BLK2_ADDR, DCTB1, EOB_SIZE
.def QUANT_TABLE, ZIGZAG_TABLE, DCHUFF
.def B_STATUS, B_STACK
.ref DCT_COEF, Y_QUANTIZE_TABLE, ZIGZAG_TABLE0
.ref MAX_VALUE, MIN_VALUE
.def DIFF, DIFF1, ABSDIFF, ABSDIFF1, TRANSDIFF
.def RZERO, IN_SIZE, COWORD, EOB
.def MASKDATA, MASKSIZE, LAST_DC, LAST_RESULT, USER_BIT, LESS_BIT
.def SRC, DST, DC_TAIL, DC_HEAD, DATA
SPRA615
14 Implementing JPEG With TMS320C2xx Assembly Language Software
.def ACHUFF, DCHUFF, LMASK
.def DIFFSIZE
.ref RDCHUFF
.ref COMPRESS_DATA
B_STACK .usect ”.b_stack”, 10h
RNDOFF .usect ”.dct_var”, 1
DCTB1 .usect ”.dct_blk”, 64
DCTB2 .usect ”.dct_blk”, 64
QZBI .usect ”.qz_blk”, 64
QZBR .usect ”.qz_blk”, 64
QUANT_TABLE .usect ”.qz_tbl”, 64
ZIGZAG_TABLE .usect ”.qz_tbl”, 64
DIFF .usect ”.huf_var”, 1
DIFF1 .usect ”.huf_var”, 1
ABSDIFF .usect ”.huf_var”, 1
ABSDIFF1 .usect ”.huf_var”, 1
RZERO .usect ”.huf_var”, 1
IN_SIZE .usect ”.huf_var”, 1
COWORD .usect ”.huf_var”, 1
TRANSDIFF .usect ”.huf_var”, 1
MASKDATA .usect ”.huf_var”, 1
MASKSIZE .usect ”.huf_var”, 1
EOB .usect ”.huf_var”, 1
LAST_DC .usect ”.huf_var”, 1
LAST_RESULT .usect ”.huf_var”, 1
USER_BIT .usect ”.huf_var”, 1
LESS_BIT .usect ”.huf_var”, 1
DC_HEAD .usect ”.huf_var”, 1
DC_TAIL .usect ”.huf_var”, 1
BLOCK_NUMBER .usect ”.huf_var”, 1
SRC .usect ”.huf_var”, 1
DST .usect ”.huf_var”, 1
LENGTH .usect ”.huf_var”, 1
DATA .usect ”.huf_var”, 1
SPRA615
15 Implementing JPEG With TMS320C2xx Assembly Language Software
DCHUFF .usect ”huftable”, 12
DIFFSIZE .usect ”huftable”, 16
LMASK .usect ”huftable”, 17
ACHUFF .usect ”huftable”,176
.bss B_STATUS, 2
EOB_SIZE .set 4
BLK2_ADDR .set QZBR
.text
BLOCK_ENCODE:
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; CONTEXT SAVING ; AR5 cannot be assigned to other usage!!
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
SST #0, B_STATUS
SST #1, B_STATUS+1
LDP #B_STACK
SAR AR1, B_STACK ; AR1 is used in DCT, QUANT & HUFF_EN
SAR AR2, B_STACK+1 ; AR2 is used in DCT, QUANT & HUFF_EN
SAR AR3, B_STACK+2 ; AR3 is used in DCT, QUANT & HUFF_EN
SAR AR6, B_STACK+3 ; AR6 is used in QUANT & HUFF_EN
POPD B_STACK+4
CALL DCT
CALL QUANT
CALL HUFF_EN
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; CONTEXT RESTORE
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
LDP #B_STACK
PSHD B_STACK+4
LAR AR6, B_STACK+3
LAR AR3, B_STACK+2
LAR AR2, B_STACK+1
LAR AR1, B_STACK
LDP #0
LST #1, B_STATUS+1
LST #0, B_STATUS
RET
SPRA615
16 Implementing JPEG With TMS320C2xx Assembly Language Software
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Subroutine : DCT
; Function : A 2D 8x8 Discrete Cosine Transform
; Disciption : This program is based on the matrix
; formulation.
; T T T
; 8X8 DCT = [c] [d] [c] = [c] { [c] [d] } }
;
; Notes: 1. [c] is cosine matrix for DCT
; 2. [d] is 8x8 input data matrix
; 3. T is the symbol for matrix transpose
;
; Input matrix is stored in B1, and the results
; are stored in the input matrix.
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; DCT Main
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
IDCT:
DCT:
; SOVM ; SET OVM=1
CNFP ; set B0 is PROGRAM MEMORY
LAR AR1,#DCTB1
LAR AR2,#DCTB2
CALL DCT1D
LAR AR1,#DCTB2
LAR AR2,#DCTB1
CALL DCT1D
CNFD
RET
;––––––––––––––––––––––––––––––––––––––
; DCT1D
; T
; 8X8 DCT1D = [c] [d]
;––––––––––––––––––––––––––––––––––––––
DCT1D:
LAR AR0, #64 ; BLOCKSIZE
LDP #RNDOFF
SPRA615
17 Implementing JPEG With TMS320C2xx Assembly Language Software
;––––––––––––––––––––––––––––––––––––––
; DCT1D Main Program
;––––––––––––––––––––––––––––––––––––––
MAR *, AR1 ; ACTIVE AR1
LAR AR3, #7 ; COUNT FOR 8 1–D DCTs
ROW1 ZAC ; ACC=0
MPY #0 ; Preg=0
RPT #7
MAC 0ff00h,*+ ; ACC = X0*Y0 + X1*Y1 + ... + X6*Y6,
; Preg = X7*Y7
LTA *,AR2 ; ACC+ = X7*Y7, ARP=2
ADDS RNDOFF ; ROUND OFF
SACH *+,AR3 ; dma = ACCH, ARP=3
BANZ ROW1,*–,AR1 ; if AR3!=0 GOTO ROW1,AR3––,ARP=1
MAR *0– ; AR1– = AR0
LAR AR3, #7 ; COUNT FOR 8 1–D DCTs
ROW2 ZAC ; ACC=0 ,Preg=0
MPY #0 ; Preg=0
RPT #7
MAC 0ff08h,*+ ; ACC=X0*Y0+X1*Y1+...+X6*Y6,
; Preg=X7*Y7
LTA *,AR2 ; ACC+=X7*Y7,ARP=2
ADDS RNDOFF ; ROUND OFF
SACH *+,AR3 ; dma=ACCH,ARP=3
BANZ ROW2,*–,AR1 ; if AR3!=0 GOTO ROW2,AR3––,ARP=1
MAR *0– ; AR1–=AR0
LARK AR3, 7 ; COUNT FOR 8 1–D DCTs
ROW3 ZAC ; ACC=0 ,Preg=0
MPYK 0 ; Preg=0
RPTK 7
MAC 0ff10h,*+ ; ACC=X0*Y0+X1*Y1+...+X6*Y6,
; Preg=X7*Y7
LTA *,AR2 ; ACC+=X7*Y7,ARP=2
ADDS RNDOFF ; ROUND OFF
SACH *+,AR3 ; dma=ACCH,ARP=3
BANZ ROW3,*–,AR1 ; if AR3!=0 GOTO ROW3,AR3––,ARP=1
MAR *0– ; AR1–=AR0
LARK AR3, 7 ; COUNT FOR 8 1–D DCTs
ROW4 ZAC ; ACC=0 ,Preg=0
MPYK 0 ; Preg=0
RPTK 7
MAC 0ff18h,*+ ; ACC=X0*Y0+X1*Y1+...+X6*Y6,
; Preg=X7*Y7
SPRA615
18 Implementing JPEG With TMS320C2xx Assembly Language Software
LTA *,AR2 ; ACC+=X7*Y7,ARP=2
ADDS RNDOFF ; ROUND OFF
SACH *+,AR3 ; dma=ACCH,ARP=3
BANZ ROW4,*–,AR1 ; if AR3!=0 GOTO ROW4,AR3––,ARP=1
MAR *0– ; AR1–=AR0
LARK AR3, 7 ; COUNT FOR 8 1–D DCTs
ROW5 ZAC ; ACC=0 ,Preg=0
MPYK 0 ; Preg=0
RPTK 7
MAC 0ff20h,*+ ; ACC=X0*Y0+X1*Y1+...+X6*Y6,
; Preg=X7*Y7
LTA *,AR2 ; ACC+=X7*Y7,ARP=2
ADDS RNDOFF ; ROUND OFF
SACH *+,AR3 ; dma=ACCH,ARP=3
BANZ ROW5,*–,AR1 ; if AR3!=0 GOTO ROW5,AR3––,ARP=1
MAR *0– ; AR1–=AR0
LARK AR3, 7 ; COUNT FOR 8 1–D DCTs
ROW6 ZAC ; ACC=0 ,Preg=0
MPYK 0 ; Preg=0
RPTK 7
MAC 0ff28h,*+ ; ACC=X0*Y0+X1*Y1+...+X6*Y6,
; Preg=X7*Y7
LTA *,AR2 ; ACC+=X7*Y7,ARP=2
ADDS RNDOFF ; ROUND OFF
SACH *+,AR3 ; dma=ACCH,ARP=3
BANZ ROW6,*–,AR1 ; if AR3!=0 GOTO ROW6,AR3––,ARP=1
MAR *0– ; AR1–=AR0
LARK AR3, 7 ; COUNT FOR 8 1–D DCTs
ROW7 ZAC ; ACC=0 ,Preg=0
MPYK 0 ; Preg=0
RPTK 7
MAC 0ff30h,*+ ; ACC=X0*Y0+X1*Y1+...+X6*Y6,
; Preg=X7*Y7
LTA *,AR2 ; ACC+=X7*Y7,ARP=2
ADDS RNDOFF ; ROUND OFF
SACH *+,AR3 ; dma=ACCH,ARP=3
BANZ ROW7,*–,AR1 ; if AR3!=0 GOTO ROW7,AR3––,ARP=1
MAR *0– ; AR1–=AR0
LARK AR3, 7 ; COUNT FOR 8 1–D DCTs
ROW8 ZAC ; ACC=0 ,Preg=0
MPYK 0 ; Preg=0
RPTK 7
SPRA615
19 Implementing JPEG With TMS320C2xx Assembly Language Software
MAC 0ff38h,*+ ; ACC=X0*Y0+X1*Y1+...+X6*Y6,
; Preg=X7*Y7
LTA *,AR2 ; ACC+=X7*Y7,ARP=2
ADDS RNDOFF ; ROUND OFF
SACH *+,AR3 ; dma=ACCH,ARP=3
BANZ ROW8,*–,AR1 ; if AR3!=0 GOTO ROW8, AR3––,ARP=1
RET
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Subroutine : QUANT
; Function : QUANTIZATION and ZIGZAG for JPEG Encoding
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
QUANT:
;–––––––––––––––––––––––––––––––––
; QUANT+ZIGZAG Main
;–––––––––––––––––––––––––––––––––
; SPM 2 ; LEFT_SHIFTED 4 PLACES
SPM 1
LAR AR0, #63 ; BLOCKSIZE–1
LAR AR1, #QZBI ; SOURCE ADDRESS
LAR AR2, #ZIGZAG_TABLE ; ZIGZAG TABLE REGISTER
LAR AR6, #QUANT_TABLE ; QUANTIZE TABLE REGISTER
MAR *, AR2 ; ACTIVE AR2
Q_loop: LAR AR3, *+, AR1 ; ASSIGN (DST+ZIGZAG) TO AR3
LT *+, AR6 ; (dma)––>Treg
MPY *+, AR3 ; (Treg)*(Quantize Cofficient)
PAC ; (Preg)––>ACC
ADDS RNDOFF ; ACC+RNDOFF
SACH *, AR0 ; Put the result to DST
BANZ Q_loop, *–, AR2 ; Repeat 64 Times
SPM 0 ; NO SHIFT
RET
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Subroutine : HUFF_EN
; Discription : Huffman Encoding
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
HUFF_EN:
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Huffman Encode Main Program
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
LDP #DIFF ;
ZAC ; ACC=0
SACL DIFF ; DIFF=0
SPRA615
20 Implementing JPEG With TMS320C2xx Assembly Language Software
SACL IN_SIZE ; IN_SIZE=0
SACL RZERO ; RZERO(ZERO RUNS)=0
DCENHUFF:
LARP AR1
LAR AR1, #QZBR
LACC * ; JPEGB(COEFF)–>ACC,AR0=>ABSDIFF
SUB LAST_DC ; ACC=COEFF–LAST_DC
CALL GSIZE, *, 2 ; CALL GETSIZE FOR GET REAL SIZE
LARP AR3
LAR AR3, #DCHUFF
LAR AR0, ABSDIFF
MAR *0+
LAC *0–, 12, 5 ;FOR EXAMPLE 0063,006 IS COWARD, 3 IS SIZE
CALL GCOWORD, *, 5
ACENHUFF:
LAR AR1, #QZBR
LARK AR6,3EH ;AR6=BLOCK_SIZE–2
LARP 1
LAC *+
SACL LAST_DC
CHECKZERO:
LAC *+,0,6
BZ CKZERO
CALL GSIZE,*,2
CONCHECK:
LAC RZERO
SUBK 10H
BLZ NEXT1
DMOV ABSDIFF
DMOV DIFF
SACL RZERO
LALK 07F9BH,12
SACL ABSDIFF,4
SACL DIFF,4
CALL GCOWORD
LAC ABSDIFF1
SACL ABSDIFF
LAC DIFF1
SACL DIFF
B CONCHECK
SPRA615
21 Implementing JPEG With TMS320C2xx Assembly Language Software
NEXT1:
RSXM
LAC RZERO
ADD ABSDIFF,4
SACL TRANSDIFF
LARP 3
LRLK AR3,ACHUFF
LAR AR0,TRANSDIFF
MAR *0+
BIT *,0H,3
BBZ nouser_16
BIT *,8H,3
BBZ nouser_16
user_16:
ZALH *0–,5
b conte
nouser_16:
LAC *0–,12,5
conte:
CALL GCOWORD,*,5
ZAC
SACL RZERO
LARP 6
BANZ CHECKZERO,*–,1
B INSERT_EOB
CKZERO:
LAC RZERO
ADDK 1H
SACL RZERO
BANZ CHECKZERO,*–,1
INSERT_EOB:
LARP 3
LRLK AR3,ACHUFF
LAC *,12
SACL ABSDIFF,4
SACL DIFF,4 ;EOB–>0A4h
CALL GCOWORD
RET
SPRA615
22 Implementing JPEG With TMS320C2xx Assembly Language Software
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; GET THE REAL SIZE SUBROUTION
; HUFFDCENCODE HUFFACENCODE
; DIFF = DC(I)=DC(I–1) DIFF = COEFF
; THE INICIAL STATE OF THIS SUBROUTINE
; ARP=AR0(ABSDIFF)
; AR2=THE HEADER OF DIFFSIZE TABLE
; THE OUTPUT STATE OF THIS SUBROUTINE
; ARP=AR0(DIFFSIZE)
; DSIZE = REAL SIZE
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
GSIZE:
SACL DIFF ;ACC–>DIFF
BIT DIFF,0H ;CHECK MSB OF DIFF
BBZ POSITIVE ;IF MSB=1 THEN DIFF IS NEGATIVE
NEGATIVE:
ABS
CMPL ;GET COMPLEMENT OF DIFF
SACL DIFF
CMPL ;GET OLD DIFF
POSITIVE:
SACL ABSDIFF ;ABS(COEFF–LAST_DC)–>AR0(DIFF),AR2(DIFFSIZE)
AND MASKDATA ;MASKDATA = 0XFFF0H
; BZ GETSIZE,*,AR2 ;IF ACC&0XFFF0H =0 THEN SIZE <4 BIT
MAR *, AR2
BZ GETSIZE
LAC ABSDIFF,12 ;ELSE SIZE >4 BIT
SACH ABSDIFF ;SHIFT RIGHT 4 BIT
LACL IN_SIZE ;IN_SIZE = IN_SIZE+4
ADDK 04H
SACL IN_SIZE
LACL ABSDIFF
B POSITIVE
GETSIZE:
LARP 2
LRLK AR2,DIFFSIZE
LAR AR0,ABSDIFF
MAR *0+
LAC * ;ARP=AR2+AR0–>ACC
ADD IN_SIZE
SACL ABSDIFF
SACH IN_SIZE
LRLK AR2,LMASK
SPRA615
23 Implementing JPEG With TMS320C2xx Assembly Language Software
LAR AR0,ABSDIFF
MAR *0+
LAC *
AND DIFF
SACL DIFF
RET
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; FIND THE COWORD AND COWORD_SIZE
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
GCOWORD:
LARP 5
SACH COWORD ;COWRD=6 , AR5–>RESULT
AND MASKSIZE
BNZ not_16
ADLK 10H,12
not_16
SUB USER_BIT,0CH ;USER_BIT=16 => 10000
NEG ; 3000
SACH USER_BIT,4 ; = 0D =D0000
BGEZ SHIFT_LT
NEWORD1:
LAC USER_BIT
ADDK 10H
SACL USER_BIT
LT USER_BIT
ZALH *
ADDT COWORD
SACH *+
SACL *
B CONDIFF
SHIFT_LT:
LT USER_BIT ;LOAD USER_BIT TO T REGISTER
LAC * ;RESULT–>ACC
ADDT COWORD
SACL *
CONDIFF:
LAC USER_BIT
SUB ABSDIFF
SACL USER_BIT
BGEZ NORMALS
SPRA615
24 Implementing JPEG With TMS320C2xx Assembly Language Software
NEWORD2:
ADDK 10H
SACL USER_BIT
LT USER_BIT
ZALH *
ADDT DIFF
SACH *+
SACL *
B ENDSUB
NORMALS:
LT USER_BIT
LAC *
ADDT DIFF
SACL *
ENDSUB:
SSXM
RET
SPRA615
25 Implementing JPEG With TMS320C2xx Assembly Language Software
Appendix B JPEG Decoding Software
JPEGDE.ASM;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Program Name : JPEGDE.ASM
; Discription : This program includes JPEG decode
; The modules of JPEG includes
; 1. JPEG Coefficients Initialization
; 2. CALL BLOCK_DECODE
; 3. 8x8 block –> image frame
; Editor : Vivian Shao
; Date : 11/26/1997
; Possessor : Texas Instruments
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
.def JPEG_DEC
.ref PICT
.ref IMAGE_LENGTH, IMAGE_WIDTH, NEXT_LINE, NEXT_SECTION
.ref BLOCK_DECODE, DCTB1
.ref J_STATUS, J_STACK
.ref IDCT_COEF, Y_DQUANTIZE_TABLE, ZIGZAG_TABLE1
.ref RD48TAB, RD12TAB, LOWMASK
.ref HDCDECODE_TABLE, DCDETAB ; ADDED on 11/26/97
.ref DQZBR, DQZBI, DQUANT_TABLE, DZIGZAG_TABLE, DETABLE, D12TAB
.ref EMASK, COMPRESS_DATA, DST, DC_TAIL, DATA
.ref MASKDATA, MASKSIZE, EOB, LAST_DC, LAST_RESULT, USER_BIT, LESS_BIT
BLOCK_RESULT .set DCTB1
.text
JPEG_DEC:
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; CONTEXT SAVING ;
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
SST #0, J_STATUS
SST #1, J_STATUS+1
LDP #J_STACK
SAR AR1, J_STACK+1 ; AR1 is used in DCT, QUANT & HUFF_EN
SAR AR2, J_STACK+2 ; AR2 is used in DCT, QUANT & HUFF_EN
SAR AR3, J_STACK+3 ; AR3 is used in DCT, QUANT & HUFF_EN
SAR AR4, J_STACK+4
SAR AR5, J_STACK+5 ; AR5 is used in QUANT & HUFF_EN
SAR AR6, J_STACK+6 ; AR6 is used as counter
SAR AR7, J_STACK+7 ; AR7 is used as counter
POPD J_STACK+8
SPRA615
26 Implementing JPEG With TMS320C2xx Assembly Language Software
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; JPEG_DE MAIN ;
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
CALL JPEG_DEC_INIT
LAR AR7, #IMAGE_WIDTH–1 ; block # of width
blockde_loop0:
LAR AR6, #IMAGE_LENGTH–1 ; block # of length
blockde_loop:
CALL BLOCK_DECODE ; decompressed data –> PICT
CALL BLOCK_ASSEMBLE
MAR *, AR6
BANZ blockde_loop, *–
LAR AR0, #NEXT_SECTION
MAR *, AR5
MAR *0+, AR7
; MAR *, AR7
BANZ blockde_loop0, *–
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; OUTPUT DATA TO FILE ”jpegout.dat”
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
JPEGOUT:
MAR *, AR4
LAR AR4, #PICT
LAR AR7, #(160*120/8)–1
JPEGOUT_LOOP:
OUT *+, 0
OUT *+, 0
OUT *+, 0
OUT *+, 0
OUT *+, 0
OUT *+, 0
OUT *+, 0
OUT *+, 0, AR7
BANZ JPEGOUT_LOOP, *–, AR4
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; CONTEXT RESTORE
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
LDP #J_STACK
PSHD J_STACK+8
SAR AR7, J_STACK+7 ; AR6 is used as counter
SAR AR6, J_STACK+6 ; AR7 is used as counter
LAR AR5, J_STACK+5
SPRA615
27 Implementing JPEG With TMS320C2xx Assembly Language Software
LAR AR4, J_STACK+4
LAR AR3, J_STACK+3
LAR AR2, J_STACK+2
LAR AR1, J_STACK+1
LDP #0
LST #1, J_STATUS+1
LST #0, J_STATUS
RET
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Assemble Blockes of Data to Image Frame
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
BLOCK_ASSEMBLE:
LAR AR0, #NEXT_LINE
MAR *, AR5
RPT #7
BLDD #BLOCK_RESULT, *+
MAR *0+
RPT #7
BLDD #BLOCK_RESULT+8, *+
MAR *0+
RPT #7
BLDD #BLOCK_RESULT+16, *+
MAR *0+
RPT #7
BLDD #BLOCK_RESULT+24, *+
MAR *0+
RPT #7
BLDD #BLOCK_RESULT+32, *+
MAR *0+
RPT #7
BLDD #BLOCK_RESULT+40, *+
MAR *0+
RPT #7
BLDD #BLOCK_RESULT+48, *+
MAR *0+
RPT #7
BLDD #BLOCK_RESULT+56, *+
LAR AR0, #NEXT_SECTION
MAR *0–
RET ; return from BLOCK_INIT
SPRA615
28 Implementing JPEG With TMS320C2xx Assembly Language Software
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
;
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
JPEG_DEC_INIT:
idct_init:
CLRC CNF ; B0 is mapped to data memory
SSXM ; SXM=1
SOVM ; OVM=1
SPM 0 ; PM=0
MAR *, AR5
LAR AR5, #0200h ; LOAD DCT COEF to B0
RPT #63
BLPD #IDCT_COEF, *+
SETC CNF ; B0 is mapped to program memory
dequant_init:
MAR *, AR5
LAR AR5, #DQUANT_TABLE ; Initialize DeQuant table
RPT #63
BLPD #Y_DQUANTIZE_TABLE, *+
LAR AR5, #DZIGZAG_TABLE ; Initialize ZigZag table
RPT #63
BLPD #ZIGZAG_TABLE1, *+
dehuff_init:
;–––––––––––––––––––––––––––––––––––––––––––––––
; Huffman Decode Initialization
;–––––––––––––––––––––––––––––––––––––––––––––––
MAR *, AR4
LAR AR4, #DETABLE
RPT #250
BLPD #RD48TAB, *+
LAR AR4, #D12TAB
RPT #87
BLPD #RD12TAB, *+
RPT #47 ; Added by V.S. 11/26/97
BLPD #HDCDECODE_TABLE, *+ ; Added by V.S. 11/26/97
RPT #16
BLPD #LOWMASK, *+
SPRA615
29 Implementing JPEG With TMS320C2xx Assembly Language Software
LDP #MASKDATA
SPLK #0fff0h, MASKDATA
SPLK #0f000h, MASKSIZE
SPLK #0Ah, EOB ; Luminance
; SPLK #00h, EOB ; Chrominance
SPLK #0h, LAST_DC
SPLK #0h, LAST_RESULT
SPLK #10h, USER_BIT
SPLK #0h, LESS_BIT
LAR AR4, #COMPRESS_DATA
LACC *+
SAR AR4, DC_TAIL
SACL DATA
LDP #DC_TAIL
LAR AR4, DC_TAIL
LAR AR5, #PICT
SAR AR5, DST
RET
BLOCKDE.ASM
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Program Name : BLOCKDE.ASM
; Discription : This program includes JPEG encoder and decoder
; The modules of JPEG includes
; 1. IDCT
; 2. DEQUANT
; 3. HUFF Decode
; Editor : Vivian Shao
; Date : 03/25/1997
; Possessor : Texas Instruments
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
.def BLOCK_DECODE
.def BLK1_ADDR, DQZBI, DQZBR
.def EMASK, DETABLE
.def DQUANT_TABLE, DZIGZAG_TABLE, D12TAB
.def DCDETAB
.def F_TAB, SIZE, COWORD_SIZE, V_SIZE, NEG_VSIZE, JUDGE_DATA
SPRA615
30 Implementing JPEG With TMS320C2xx Assembly Language Software
.ref B_STATUS, B_STACK
.ref SRC, LAST_RESULT, LESS_BIT, EOB, DC_TAIL, DATA, LAST_DC
.ref IDCT
.ref IDCT_COEF, Y_DQUANTIZE_TABLE, ZIGZAG_TABLE1
.ref RD48TAB, RD12TAB, LOWMASK
.ref MAX_VALUE, MIN_VALUE
.ref COWORD, DCTB1, USER_BIT, DIFF, RZERO, ABSDIFF, EOB_SIZE, COMPRESS_DATA
DQZBR .usect ”.dq_blk”, 64
DQZBI .usect ”.dq_blk”, 64
DQUANT_TABLE .usect ”.dq_tbl”, 64
DZIGZAG_TABLE .usect ”.dq_tbl”, 64
DETABLE .usect ”dehuftab”,251
D12TAB .usect ”dehuftab”, 88
DCDETAB .usect ”dehuftab”, 48
EMASK .usect ”dehuftab”, 17
F_TAB .usect ”.huf_var”, 1
SIZE .usect ”.huf_var”, 1
COWORD_SIZE .usect ”.huf_var”, 1
V_SIZE .usect ”.huf_var”, 1
NEG_VSIZE .usect ”.huf_var”, 1
JUDGE_DATA .usect ”.huf_var”, 1
CHECK_DC .usect ”.huf_var”, 1
BLK1_ADDR .set DQZBI
.text
BLOCK_DECODE:
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; CONTEXT SAVING ; AR4 cannot be assigned to other usage!!
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
SST #0, B_STATUS
SST #1, B_STATUS+1
LDP #B_STACK
SAR AR1, B_STACK ; AR1 is used in DCT, QUANT & HUFF_DE
SAR AR2, B_STACK+1 ; AR2 is used in DCT, QUANT & HUFF_DE
SAR AR3, B_STACK+2 ; AR3 is used in DCT, QUANT & HUFF_DE
SAR AR6, B_STACK+3 ; AR6 is used in QUANT & HUFF_DE
SPRA615
31 Implementing JPEG With TMS320C2xx Assembly Language Software
; SAR AR4, B_STACK+4
; SAR AR5, B_STACK+5
POPD B_STACK+4
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; BLOCK_DECODE MAIN
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
CALL HUFFDECODE
CALL DQUANT
CALL IDCT
CALL JPEG_FINAL
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; CONTEXT RESTORE
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
LDP #B_STACK
PSHD B_STACK+4
; LAR AR5, B_STACK+5
; LAR AR4, B_STACK+4
LAR AR6, B_STACK+3
LAR AR3, B_STACK+2
LAR AR2, B_STACK+1
LAR AR1, B_STACK
LDP #0
LST #1, B_STATUS+1
LST #0, B_STATUS
RET
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Subroutine: JPEG_FINAL
; Functions: 1. Limit the range of data to {MAX_VALUE, MIN_VALUE}
; 2. SUB 128
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
JPEG_FINAL:
LAR AR2, #DCTB1
LAR AR3, #63
LARP AR2
BOUND: LACC *
BCND neg_BOUND, LT
SUB #MAX_VALUE
BCND end_BOUND, LT
LACC #MAX_VALUE
SACL *
B end_BOUND
SPRA615
32 Implementing JPEG With TMS320C2xx Assembly Language Software
neg_BOUND:
SUB #MIN_VALUE
BCND end_BOUND, GT
LACC #MIN_VALUE
SACL *
B end_BOUND
end_BOUND
MAR *+, AR0
BANZ BOUND, *–, AR2
ADD_128:
LARP AR2
LAR AR2, #DCTB1
LAR AR3, #63
jf_loop:
LACC * ; +128
ADD #128
SACL *+, 0, AR3
BANZ jf_loop, *–, AR2
RET
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; DEQUANT+ZIGZAG Main
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
DQUANT:
LAR AR0, #63 ; BLOCKSIZE–1
LAR AR2, #DQZBR ; DESTINATION ADDRESS
LAR AR3, #DZIGZAG_TABLE ; ZIGZAG TABLE REGISTER
LAR AR6, #DQUANT_TABLE ; QUANTIZE TABLE REGISTER
LARP AR3 ; ACTIVE AR3
DQ_loop:
LAR AR1, *+, AR1 ; LOAD SOURCE ADDRESS
LT *+, AR6 ; (dma)––>Treg
MPY *+, AR2 ; (dma)*Treg––>Preg
SPL *+, AR0 ; Preg(15..0)––>(dma)
BANZ DQ_loop, *–, AR3 ; REPETS 64 Times
RET
SPRA615
33 Implementing JPEG With TMS320C2xx Assembly Language Software
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Subroutine : HUFFDECODE
; Function : Huffman decode
; Date : 02/14/97
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
;––––––––––––––––––––––––––––––––––––––––––––––––
; Adress point EXPLANATION
;
; AR1 EMASK
; AR2 DETABLE
; AR3 DQZBI
; AR4 COMPRESS_DAT
; AR6 COUNTER
;––––––––––––––––––––––––––––––––––––––––––––––––
HUFFDECODE:
;–––––––––––––––––––––––––––––––––––––––––––––––––
; Huffman Decode Main
;–––––––––––––––––––––––––––––––––––––––––––––––––
RSXM ;SXM=0
;assign 0 to the result
ZAC
LARP 3
LAR AR3, #DQZBI
RPTK 63
SACL *+
DC_DECODE:
LAR AR3, #DQZBI
LARP 4
LDP #LESS_BIT
SPLK #0FFFFh, CHECK_DC ; DC decode flag, 12/9/97
LT LESS_BIT
ZALH DATA
ADDT *
; LDP #JUDGE_DATA
SACH JUDGE_DATA
LAC JUDGE_DATA,4
SACH ABSDIFF
LAC ABSDIFF ;if ABSDIFF<15
SUBK 0FH
BLZ GDCANS ;goto GDCANS
LAC JUDGE_DATA,8
SACH ABSDIFF
LAC ABSDIFF
SPRA615
34 Implementing JPEG With TMS320C2xx Assembly Language Software
SUBK 0FFh ;––––––––––––––––––––––––––––
BLZ DCCWLT9B ; added on 11/27/97
LAC JUDGE_DATA,12 ; by Vivian Shao
ANDK 0Fh ;
ADDK 20h ; to check DC codeword > 9 bits
SACL ABSDIFF ;
B GDCANS ;–––––––––––––––––––––––––––––
DCCWLT9B:
LAC ABSDIFF ;else
ANDK 0FH ;ABSDIFF=ABSDIFF mod 16
ADDK 10H ;ABSDIFF+=16
SACL ABSDIFF
GDCANS: ;get DC answer
LARP 2
; LRLK AR2,DETABLE ; marked on 11/26/97
LRLK AR2, DCDETAB ; added on 11/26/97
LAR AR0,ABSDIFF
MAR *0+
LAC *0–,8
ANDK 00FFH,8
SACL F_TAB
CALL GET_VEN
LARP AR3
MAR *–
LAC *
ADD LAST_DC
SACL *+,AR4
SACL LAST_DC
LRLK AR6,63
AC_DECODE:
SPLK #0, CHECK_DC ; DC decode flag, 12/9/97
LRLK AR2, DETABLE ; added on 11/26/97
LT LESS_BIT
ZALH DATA
ADDT *
SACH JUDGE_DATA
LAC JUDGE_DATA,8
SACH ABSDIFF
LAC ABSDIFF ; if JUDGE_DATA <= 0bfffh
SPRA615
35 Implementing JPEG With TMS320C2xx Assembly Language Software
;Luminance
SUBK 0BFH
;Chrominance
; SUBK 0AFH
BLEZ NOZERO ; goto NOZERO
;Luminance
SUBK 03BH ; elseif JUDGE_DATA <= 0faffh
;Chrominance
; SUBK 04AH ; elseif JUDGE_DATA <= 0faffh
BLEZ WITHZERO ; goto WITHZERO
LAC JUDGE_DATA
;Luminance
SUBK 0FF81H ; elseif JIDGE_DATA < 0ff81h
;Chrominance
; SUBK 0FF83H ; elseif JIDGE_DATA < 0ff81h
BLZ BIT12 ; goto BIT12
; else goto BIT16
BIT16: ; COWORD= 16 BITS
; Luminance
ADDK 21H
; Chrominance
; ADDK 23H
SACL ABSDIFF
LT LESS_BIT
ZALH DATA
ADDT *+
SACL DATA
SAR 4,DC_TAIL
; LAC DC_TAIL ; Marked by V.S., 11/26/97
; ANDK 11FFH ; WRONG CODE => Need to be deleted
; SACL DC_TAIL ; Marked by V.S., 11/26/97
; LAR 4,DC_TAIL ; Marked by V.S., 11/26/97
CALL JUMPZERO,*,AR2
LAC F_TAB,4
ANDK 0F00H
SACL F_TAB
SPRA615
36 Implementing JPEG With TMS320C2xx Assembly Language Software
SPLK #0FFFFh, CHECK_DC ; 12/09/97, not checking EOB
CALL GET_VEN
END_ACDECODE:
BANZ AC_DECODE,*,AR4
LAC COWORD
SUB EOB
BZ END_DECODE
LACK EOB_SIZE ; the last entry is not zero
CALL GET_COWORD
SACH COWORD
END_DECODE:
SSXM ; SSXM=1
RET
BIT12: ; COWORD = 12 bits
LARP 2
LAC JUDGE_DATA,12
SACH ABSDIFF
LAC ABSDIFF
;––––––– Changed on 11/17 –––––––––––––––
; SBLK 0FB0H–(0FAH+1H) ;original
SBLK 0FA0H–(0FAH+1H)
;––––––––––––––––––––––––––––––––––––––––
SACL ABSDIFF
WITHZERO:
CALL JUMPZERO,*,AR2
LAC F_TAB
ANDK 0FFH,8
SACL F_TAB
CALL GET_VEN
B END_ACDECODE
NOZERO:
LARP 2
LRLK AR2,DETABLE
LAR AR0,ABSDIFF
MAR *0+
LAC *0–
ANDK 0FFH,8
SACL F_TAB
CALL GET_VEN
B END_ACDECODE
SPRA615
37 Implementing JPEG With TMS320C2xx Assembly Language Software
;
; JUMP RZERO
;
JUMPZERO:
; LARP 2
; LRLK AR2,DETABLE
LAR AR0,ABSDIFF
MAR *0+
LAC *0–
; LDP #F_TAB
SACL F_TAB
ANDK 0FH
; LDP #RZERO
SACL RZERO
LAR AR0,RZERO
LARP 3
MAR *0+,AR6
MAR *0–
RET
;
; GET THE V VALUE
;
GET_VEN:
LAC F_TAB,4
SACH COWORD_SIZE
ANDK 0F000H
SACH V_SIZE,4
LAC COWORD_SIZE
CALL GET_COWORD,*,AR4
SACH COWORD
LACC CHECK_DC ; Distinct EOB(00B) and DC SSSS=0(00B)
BNZ SKIP_EOB_CHECKING ; 12/9/97
LAC COWORD
SUB EOB
MAR *, AR6
BZ FIND_EOB
SKIP_EOB_CHECKING:
LAC V_SIZE
CALL GET_COWORD,*,AR4
SACH DIFF
SPRA615
38 Implementing JPEG With TMS320C2xx Assembly Language Software
LACL #16 ; modifid from next 3 original code
SUB V_SIZE
SACL NEG_VSIZE
; LAC V_SIZE ; original code
; NEG ; original code
; SACL NEG_VSIZE ; original code
LAC DIFF
LT NEG_VSIZE
BITT DIFF
BBNZ DPOSITIVE
LARP 1
LRLK AR1,EMASK
LAR AR0,V_SIZE
MAR *0+
LAC *0–
CMPL
OR DIFF
ADDK 1H
;
DPOSITIVE:
LARP 3
SACL *+,AR6
MAR *–
RET
FIND_EOB:
LRLK AR6,0 ; AR6 reseted if EOB met
RET
;
; GET THE COWORD
;
GET_COWORD:
; LARP 4
SACL SIZE
SUB USER_BIT
SACL LESS_BIT
NEG
LT SIZE
BLEZ GET_NEWWORD ; if user_bit < 0 goto GET_NEWWORD
SACL USER_BIT ; else coword shift size bits
LACT DATA
SACL DATA
B END_GCOWORD
SPRA615
39 Implementing JPEG With TMS320C2xx Assembly Language Software
GET_NEWWORD: ; get new coword
ADDK 10H ; user_bit += 16
SACL USER_BIT
LACT DATA ; coword shift size bits
LT LESS_BIT ; new coword shift less bits
ADDT *+
SACH DIFF
SACL DATA
SAR 4,DC_TAIL
LAC DC_TAIL
; ANDK 11FFH
SACL DC_TAIL
LAR 4,DC_TAIL
ZALH DIFF
END_GCOWORD:
RET
SPRA615
40 Implementing JPEG With TMS320C2xx Assembly Language Software
Appendix C JPEG TablesTABLES.ASM;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Program Name : TABLES.ASM
; Discription : This program includes the tables JPEG encoder/decoder
; needed:
; 1. DCT/IDCT coefficient tables
; 2. QUANT/DEQUANT tables
; 3. HUFFMAN ENCODE/DECODE tables
; Editor : Vivian Shao
; Date : 11/27/1997
; Possessor : Texas Instruments
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
.def MAX_VALUE, MIN_VALUE
.def DCT_COEF, Y_QUANTIZE_TABLE, ZIGZAG_TABLE0
.def IDCT_COEF, Y_DQUANTIZE_TABLE, ZIGZAG_TABLE1
.def RDCHUFF, RACHUFF
.def RD48TAB, RD12TAB, LOWMASK
.def RDCHUFF
.def HDCDECODE_TABLE
.def MAX_VALUE, MIN_VALUE
.def IMAGE_LENGTH, IMAGE_WIDTH, NEXT_LINE, NEXT_SECTION
.def COMPRESS_DATA
.ref BLK1_ADDR, BLK2_ADDR
.ref PICT
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; DEFINE IMAGE SIZE:
; NEED TO BE MODIFIED FOR REAL CASE !!
; example shown below: 32=pixels/line; 24=lines/frame
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
IMAGE_LENGTH .set 160/8 ; block # of image length
IMAGE_WIDTH .set 120/8 ; block # of image width
NEXT_LINE .set 8*(160/8–1)
;NEXT_SECTION .set 8*7*(120/8)
NEXT_SECTION .set 7*160
COMPRESS_DATA .usect ”.jpgen”, 800h ; size 400h can be changed
;PICT .usect ”.picture”, 8000h ; size 8000h can be changed
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
MAX_VALUE .set 255/2 ; color resolution
MIN_VALUE .set –255/2–1 ; color resolution
.sect ”jpeg_tbl”
SPRA615
41 Implementing JPEG With TMS320C2xx Assembly Language Software
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; DCT cofficients x 65536
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
DCT_COEF:
.word 23170, 23170, 23170, 23170, 23170, 23170, 23170, 23170
.word 32138, 27246, 18205, 6393, –6393,–18205,–27246,–32138
.word 30274, 12540,–12540,–30274,–30274,–12540, 12540, 30274
.word 27246, –6393,–32138,–18205, 18205, 32138, 6393,–27246
.word 23170,–23170,–23170, 23170, 23170,–23170,–23170, 23170
.word 18205,–32138, 6393, 27246,–27246, –6393, 32138,–18205
.word 12540,–30274, 30274,–12540,–12540, 30274,–30274, 12540
.word 6393,–18205, 27246,–32138, 32138,–27246, 18205, –6393
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; Inverse DCT cofficients x 65536
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
IDCT_COEF:
.word 23170, 32138, 30274, 27246, 23170, 18205, 12540, 6393
.word 23170, 27246, 12540, –6393,–23170,–32138,–30274,–18205
.word 23170, 18205,–12540,–32138,–23170, 6393, 30274, 27246
.word 23170, 6393,–30274,–18205, 23170, 27246,–12540,–32138
.word 23170, –6393,–30274, 18205, 23170,–27246,–12540, 32138
.word 23170,–18205,–12540, 32138,–23170, –6393, 30274,–27246
.word 23170,–27246, 12540, 6393,–23170, 32138,–30274, 18205
.word 23170,–32138, 30274,–27246, 23170,–18205, 12540, –6393
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; QUANTIZATION table
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Y_QUANTIZE_TABLE:
;–––––––––––––––––Quantization table 1–––––––––––––––––––––––––––––
.word 2048, 2979, 3277, 2048, 1365, 819, 643, 537
.word 2731, 2731, 2341, 1725, 1260, 565, 546, 596
.word 2341, 2521, 2048, 1365, 819, 575, 475, 585
.word 2341, 1928, 1489, 1130, 643, 377, 410, 529
.word 1820, 1489, 886, 585, 482, 301, 318, 426
.word 1365, 936, 596, 512, 405, 315, 290, 356
.word 669, 512, 420, 377, 318, 271, 273, 324
.word 455, 356, 345, 334, 293, 328, 318, 331
;;–––––––––––––––––Quantization table 2–––––––––––––––––––––––––––––
; .word 2048*2, 2979*2, 3277*2, 2048*2, 1365*2, 819*2, 643*2, 537*2
; .word 2731*2, 2731*2, 2341*2, 1725*2, 1260*2, 565*2, 546*2, 596*2
; .word 2341*2, 2521*2, 2048*2, 1365*2, 819*2, 575*2, 475*2, 585*2
; .word 2341*2, 1928*2, 1489*2, 1130*2, 643*2, 377*2, 410*2, 529*2
; .word 1820*2, 1489*2, 886*2, 585*2, 482*2, 301*2, 318*2, 426*2
SPRA615
42 Implementing JPEG With TMS320C2xx Assembly Language Software
; .word 1365*2, 936*2, 596*2, 512*2, 405*2, 315*2, 290*2, 356*2
; .word 669*2, 512*2, 420*2, 377*2, 318*2, 271*2, 273*2, 324*2
; .word 455*2, 356*2, 345*2, 334*2, 293*2, 328*2, 318*2, 331*2
UV_QUANTIZE_TABLE:
.word 1928, 1820, 1365, 697, 331, 331, 331, 331
.word 1820, 1560, 1260, 496, 331, 331, 331, 331
.word 1365, 1260, 585, 331, 331, 331, 331, 331
.word 697, 496, 331, 331, 331, 331, 331, 331
.word 331, 331, 331, 331, 331, 331, 331, 331
.word 331, 331, 331, 331, 331, 331, 331, 331
.word 331, 331, 331, 331, 331, 331, 331, 331
.word 331, 331, 331, 331, 331, 331, 331, 331
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; QUANTIZATION table
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Y_DQUANTIZE_TABLE:
;–––––––––––––––––De Quantization table 1–––––––––––––––––––––––––––––
.word 16, 11, 10, 16, 24, 40, 51, 61
.word 12, 12, 14, 19, 26, 58, 60, 55
.word 14, 13, 16, 24, 40, 57, 69, 56
.word 14, 17, 22, 29, 51, 87, 80, 62
.word 18, 22, 37, 56, 68, 109, 103, 77
.word 24, 35, 55, 64, 81, 104, 113, 92
.word 49, 64, 78, 87, 103, 121, 120, 101
.word 72, 92, 95, 98, 112, 100, 103, 99
;;–––––––––––––––––De Quantization table 2–––––––––––––––––––––––––––––
; .word 8, 6, 5, 8,12,20,26,31
; .word 6, 6, 7,10,13,29,30,28
; .word 7, 7, 8,12,20,29,35,28
; .word 7, 9,11,15,26,44,40,31
; .word 9,11,19,28,34,55,52,39
; .word 12,18,28,32,41,52,57,46
; .word 25,32,39,44,52,61,60,51
; .word 36,46,48,49,56,50,52,50
UV_DQUANTIZE_TABLE:
.word 17, 18, 24, 47, 99, 99, 99, 99
.word 18, 21, 26, 66, 99, 99, 99, 99
.word 24, 26, 56, 99, 99, 99, 99, 99
.word 47, 66, 99, 99, 99, 99, 99, 99
.word 99, 99, 99, 99, 99, 99, 99, 99
.word 99, 99, 99, 99, 99, 99, 99, 99
.word 99, 99, 99, 99, 99, 99, 99, 99
.word 99, 99, 99, 99, 99, 99, 99, 99
SPRA615
43 Implementing JPEG With TMS320C2xx Assembly Language Software
;–––––––––––––––––––––––––––––––––––––––––––––
; ZIGZAG Cofficient
;–––––––––––––––––––––––––––––––––––––––––––––
ZIGZAG_TABLE0:
.word 0+BLK2_ADDR, 1+BLK2_ADDR, 5+BLK2_ADDR,6+BLK2_ADDR,14+BLK2_ADDR,15+BLK2_ADDR,27+BLK2_ADDR,28+BLK2_ADDR
.word 2+BLK2_ADDR, 4+BLK2_ADDR,7+BLK2_ADDR,13+BLK2_ADDR,16+BLK2_ADDR,26+BLK2_ADDR,29+BLK2_ADDR,42+BLK2_ADDR
.word 3+BLK2_ADDR,8+BLK2_ADDR,12+BLK2_ADDR,17+BLK2_ADDR,25+BLK2_ADDR,30+BLK2_ADDR,41+BLK2_ADDR,43+BLK2_ADDR
.word9+BLK2_ADDR,11+BLK2_ADDR,18+BLK2_ADDR,24+BLK2_ADDR,31+BLK2_ADDR,40+BLK2_ADDR,44+BLK2_ADDR,53+BLK2_ADDR
.word10+BLK2_ADDR,19+BLK2_ADDR,23+BLK2_ADDR,32+BLK2_ADDR,39+BLK2_ADDR,45+BLK2_ADDR,52+BLK2_ADDR,54+BLK2_ADDR
.word20+BLK2_ADDR,22+BLK2_ADDR,33+BLK2_ADDR,38+BLK2_ADDR,46+BLK2_ADDR,51+BLK2_ADDR,55+BLK2_ADDR,60+BLK2_ADDR
.word21+BLK2_ADDR,34+BLK2_ADDR,37+BLK2_ADDR,47+BLK2_ADDR,50+BLK2_ADDR,56+BLK2_ADDR,59+BLK2_ADDR,61+BLK2_ADDR
.word35+BLK2_ADDR,36+BLK2_ADDR,48+BLK2_ADDR,49+BLK2_ADDR,57+BLK2_ADDR,58+BLK2_ADDR,62+BLK2_ADDR,63+BLK2_ADDR
;–––––––––––––––––––––––––––––––––––––––––––––
; ZIGZAG Cofficient
;–––––––––––––––––––––––––––––––––––––––––––––
ZIGZAG_TABLE1:
.word 0+BLK1_ADDR, 1+BLK1_ADDR, 5+BLK1_ADDR,6+BLK1_ADDR,14+BLK1_ADDR,15+BLK1_ADDR,27+BLK1_ADDR,28+BLK1_ADDR
.word 2+BLK1_ADDR, 4+BLK1_ADDR,7+BLK1_ADDR,13+BLK1_ADDR,16+BLK1_ADDR,26+BLK1_ADDR,29+BLK1_ADDR,42+BLK1_ADDR
.word 3+BLK1_ADDR,8+BLK1_ADDR,12+BLK1_ADDR,17+BLK1_ADDR,25+BLK1_ADDR,30+BLK1_ADDR,41+BLK1_ADDR,43+BLK1_ADDR
.word9+BLK1_ADDR,11+BLK1_ADDR,18+BLK1_ADDR,24+BLK1_ADDR,31+BLK1_ADDR,40+BLK1_ADDR,44+BLK1_ADDR,53+BLK1_ADDR
.word10+BLK1_ADDR,19+BLK1_ADDR,23+BLK1_ADDR,32+BLK1_ADDR,39+BLK1_ADDR,45+BLK1_ADDR,52+BLK1_ADDR,54+BLK1_ADDR
.word20+BLK1_ADDR,22+BLK1_ADDR,33+BLK1_ADDR,38+BLK1_ADDR,46+BLK1_ADDR,51+BLK1_ADDR,55+BLK1_ADDR,60+BLK1_ADDR
.word21+BLK1_ADDR,34+BLK1_ADDR,37+BLK1_ADDR,47+BLK1_ADDR,50+BLK1_ADDR,56+BLK1_ADDR,59+BLK1_ADDR,61+BLK1_ADDR
.word35+BLK1_ADDR,36+BLK1_ADDR,48+BLK1_ADDR,49+BLK1_ADDR,57+BLK1_ADDR,58+BLK1_ADDR,62+BLK1_ADDR,63+BLK1_ADDR
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; DC Huffman Coding Table
; The High byte means code word size
; Low byte means code word
; Bit position 15 14 13 12 11 10 9 8 7 6 4 5 3 2 1 0
; \ \ | | | | | | | | / / \ \ / /
; code word size
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
SPRA615
44 Implementing JPEG With TMS320C2xx Assembly Language Software
RDCHUFF: ; Luminance DC table
.word 0002h,0023h,0033h,0043h,0053h,0063h
.word 00e4h,01e5h,03e6h,07e7h,0fe8h,1fe9h
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; From DIFF to Get the real size of Diff
; DIFF 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
; SSSS 0 1 2 2 3 3 3 3 4 4 4 4 4 4 4 4
;––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
DIFFSIZE:
.word 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
LOWMASK
.word 0000H,0001h,0003h,0007h,000fh,001fh,003fh,007fh
.word 00ffh,01ffh,03ffh,07ffh,0fffh,1fffh,3fffh,7fffh
.word 0ffffh
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; AC Huffman Coding Table
; Bit position 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
; \ | | | . . . . . . ./.. / \ . . /
; Codeword Size
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
RACHUFF: ; Luminance
.word 000a4h,00000h,00000h,00000h,00000h,00000h,00000h,00000h ;0/0
.word 00000h,00000h,00000h,00000h,00000h,00000h,00000h,07f9bh ;8/0
.word 00002h,000c4h,001c5h,003a6h,003b6h,007a7h,007b7h,00fa8h ;0/1
.word 01f89h,01f99h,01fa9h,03f9ah,03faah,07f8bh,0ffebh,0fff5h ;8/1
.word 00012h,001b5h,00f98h,01f79h,03f8ah,07f7bh,0ff6ch,0ff7ch ;0/2
.word 0ff81h,0ffbeh,0ffc7h,0ffd0h,0ffd9h,0ffe2h,0ffech,0fff6h ;8/2
.word 00043h,00797h,03f7ah,0ff5ch,0ff96h,0ff9eh,0ffa6h,0ffaeh ;0/3
.word 0ffb6h,0ffbfh,0ffc8h,0ffd1h,0ffdah,0ffe3h,0ffedh,0fff7h ;8/3
.word 000b4h,01f69h,0ff4ch,0ff8fh,0ff97h,0ff9fh,0ffa7h,0ffafh ;0/4
.word 0ffb7h,0ffc0h,0ffc9h,0ffd2h,0ffdbh,0ffe4h,0ffeeh,0fff8h ;8/4
.word 001a5h,07f6bh,0ff89h,0ff90h,0ff98h,0ffa0h,0ffa8h,0ffb0h ;0/5
.word 0ffb8h,0ffc1h,0ffcah,0ffd3h,0ffdch,0ffe5h,0ffefh,0fff9h ;8/5
.word 00787h,0ff84h,0ff8ah,0ff91h,0ff99h,0ffa1h,0ffa9h,0ffb1h ;0/6
.word 0ffb9h,0ffc2h,0ffcbh,0ffd4h,0ffddh,0ffe6h,0fff0h,0fffah ;8/6
.word 00f88h,0ff85h,0ff8bh,0ff92h,0ff9ah,0ffa2h,0ffaah,0ffb2h ;0/7
.word 0ffbah,0ffc3h,0ffcch,0ffd5h,0ffdeh,0ffe7h,0fff1h,0fffbh ;8/7
.word 03f6ah,0ff86h,0ff8ch,0ff93h,0ff9bh,0ffa3h,0ffabh,0ffb3h ;0/8
.word 0ffbbh,0ffc4h,0ffcdh,0ffd6h,0ffdfh,0ffe8h,0fff2h,0fffch ;8/8
.word 0ff82h,0ff87h,0ff8dh,0ff94h,0ff9ch,0ffa4h,0ffach,0ffb4h ;0/9
.word 0ffbch,0ffc5h,0ffceh,0ffd7h,0ffe0h,0ffe9h,0fff3h,0fffdh ;8/9
.word 0ff83h,0ff88h,0ff8eh,0ff95h,0ff9dh,0ffa5h,0ffadh,0ffb5h ;0/A
.word 0ffbdh,0ffc6h,0ffcfh,0ffd8h,0ffe1h,0ffeah,0fff4h,0fffeh ;8/A
SPRA615
45 Implementing JPEG With TMS320C2xx Assembly Language Software
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; decoder table
; for coword_size < 4 bit | for coword _size = 16 bit
; put in high byte | put in low byte
; number of zeros is always 0 | codeword size is always 16
; bit position
; 15 14 13 12 11 10 09 08 | 07 06 05 04 03 02 01 00
; \ | | / \ | | / \ | | / \ | | /
; codeword size v_len size v_length number of zeros
; byte positon 0 1 2 3 4 5 6 7 8 9 10 1112 13 14 15
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
RD48TAB: ; Luminance
.word02120h,02120h,02120h,02120h,02131h,02131h,02132h,02132h,02133h,02133h,02134h,02134h,02135h,02135h,02146h,02100h
.word02157h,02157h,02157h,02157h,02157h,02157h,02157h,02157h,02168h,02168h,02168h,02168h,02179h,02179h,0218ah,0219bh
.word02128h,02128h,02190h,021a0h,02161h,02171h,02181h,02191h,021a1h,02152h,02162h,02172h,02182h,02192h,021a2h,02143h
.word02153h,02163h,02173h,02183h,02193h,021a3h,02134h,02144h,02154h,02164h,02174h,02184h,02194h,021a4h,02135h,02145h
.word02255h,02265h,02275h,02285h,02295h,022a5h,02236h,02246h,02256h,02266h,02276h,02286h,02296h,022a6h,02237h,02247h
.word02257h,02267h,02277h,02287h,02297h,022a7h,02238h,02248h,02258h,02268h,02278h,02288h,02298h,022a8h,02229h,02239h
.word02249h,02259h,02269h,02279h,02289h,02299h,022a9h,0222ah,0223ah,0224ah,0225ah,0226ah,0227ah,0228ah,0229ah,022aah
.word0222bh,0223bh,0224bh,0225bh,0226bh,0227bh,0228bh,0229bh,022abh,0222ch,0223ch,0224ch,0225ch,0226ch,0227ch,0228ch
.word0339ch,033ach,0332dh,0333dh,0334dh,0335dh,0336dh,0337dh,0338dh,0339dh,033adh,0331eh,0332eh,0333eh,0334eh,0335eh
.word0336eh,0337eh,0338eh,0339eh,033aeh,0331fh,0332fh,0333fh,0334fh,0335fh,0336fh,0337fh,0338fh,0339fh,033afh,03300h
;–––––––––––––––––––––––––––––––––––––
; Code Word : 4–8 bits
;–––––––––––––––––––––––––––––––––––––
.word04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h,04000h
.word04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h,04400h
.word04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h,04101h
.word05500h,05500h,05500h,05500h,05500h,05500h,05500h,05500h,05201h,05201h,05201h,05201h,05201h,05201h,05201h,05201h
.word05102h,05102h,05102h,05102h,05102h,05102h,05102h,05102h,06103h,06103h,06103h,06103h,06104h,06104h,06104h,06104h
.word 07600h,07600h,07301h,07301h,07105h,07105h,07106h,07106h,08700h,08202h,08107h
SPRA615
46 Implementing JPEG With TMS320C2xx Assembly Language Software
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
; decoder table
; for 12bit cowrod
; bit position
; 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
; \ | | / \ | | / \ | / / \ | | /
; coword size v_lengyh don’t care number of zeros
; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
;–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
RD12TAB ; Luminance
.word00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h,00000h
.word09401h,09401h,09401h,09401h,09401h,09401h,09401h,09401h,09203h,09203h,09203h,09203h,09203h,09203h,09203h,09203h
.word09108h,09108h,09108h,09108h,09108h,09108h,09108h,09108h,09109h,09109h,09109h,09109h,09109h,09109h,09109h,09109h
.word0910ah,0910ah,0910ah,0910ah,0910ah,0910ah,0910ah,0910ah,0a800h,0a800h,0a800h,0a800h,0a302h,0a302h,0a302h,0a302h
.word0a204h,0a204h,0a204h,0a204h,0a10bh,0a10bh,0a10bh,0a10bh,0a10ch,0a10ch,0a10ch,0a10ch,0b501h,0b501h,0b205h,0b205h
.word 0b10dh,0b10dh,0b00fh,0b00fh,0c402h,0c303h,0c206h,0c207h
HDCDECODE_TABLE:
.word 0020h,0020h,0020h,0020h,0031h,0031h,0032h,0032h,0033h,0033h,0034h,0034h,0035h,0035h,0046h,0000h
.word 0057h,0057h,0057h,0057h,0057h,0057h,0057h,0057h,0068h,0068h,0068h,0068h,0079h,0079h,008ah,009bh
.word 0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h,0000h
SPRA615
47 Implementing JPEG With TMS320C2xx Assembly Language Software
Appendix D Huffman AC Coefficient Tables
Table for Luminance AC Coefficients
RRRR/SSSS Code length Code word0/0(EOB) 4 10100/1 2 000/2 2 010/3 3 1000/4 4 10110/5 5 110100/6 7 11110000/7 8 111110000/8 10 1111110110
Table for Chrominance AC Coefficients
RRRR/SSSS Code length Code word0/0(EOB) 2 000/1 2 010/2 3 1000/3 4 10100/4 5 110000/5 6 11001
IMPORTANT NOTICE
Texas Instruments and its subsidiaries (TI) reserve the right to make changes to their products or to discontinueany product or service without notice, and advise customers to obtain the latest version of relevant informationto verify, before placing orders, that information being relied on is current and complete. All products are soldsubject to the terms and conditions of sale supplied at the time of order acknowledgement, including thosepertaining to warranty, patent infringement, and limitation of liability.
TI warrants performance of its semiconductor products to the specifications applicable at the time of sale inaccordance with TI’s standard warranty. Testing and other quality control techniques are utilized to the extentTI deems necessary to support this warranty. Specific testing of all parameters of each device is not necessarilyperformed, except those mandated by government requirements.
CERTAIN APPLICATIONS USING SEMICONDUCTOR PRODUCTS MAY INVOLVE POTENTIAL RISKS OFDEATH, PERSONAL INJURY, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE (“CRITICALAPPLICATIONS”). TI SEMICONDUCTOR PRODUCTS ARE NOT DESIGNED, AUTHORIZED, ORWARRANTED TO BE SUITABLE FOR USE IN LIFE-SUPPORT DEVICES OR SYSTEMS OR OTHERCRITICAL APPLICATIONS. INCLUSION OF TI PRODUCTS IN SUCH APPLICATIONS IS UNDERSTOOD TOBE FULLY AT THE CUSTOMER’S RISK.
In order to minimize risks associated with the customer’s applications, adequate design and operatingsafeguards must be provided by the customer to minimize inherent or procedural hazards.
TI assumes no liability for applications assistance or customer product design. TI does not warrant or representthat any license, either express or implied, is granted under any patent right, copyright, mask work right, or otherintellectual property right of TI covering or relating to any combination, machine, or process in which suchsemiconductor products or services might be or are used. TI’s publication of information regarding any thirdparty’s products or services does not constitute TI’s approval, warranty or endorsement thereof.
Copyright 2000, Texas Instruments Incorporated