├── .gitignore
├── LICENSE
├── README.md
├── RTL
└── jls_encoder.v
└── SIM
├── JPEGLSdec.exe
├── images
├── test001.pgm
├── test002.pgm
├── test003.pgm
├── test004.pgm
├── test005.pgm
├── test006.pgm
├── test007.pgm
├── test008.pgm
├── test009.pgm
├── test010.pgm
├── test011.pgm
├── test012.pgm
├── test013.pgm
├── test014.pgm
├── test015.pgm
├── test016.pgm
├── test017.pgm
├── test018.pgm
├── test019.pgm
├── test020.pgm
├── test021.pgm
└── test022.pgm
├── tb_jls_encoder.v
└── tb_jls_encoder_run_iverilog.bat
/.gitignore:
--------------------------------------------------------------------------------
1 | **/vivado
2 | **/quartus
3 | FPGA_jls_encoder_test
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | -9A90FD.svg)   
2 |
3 | [English](#en) | [中文](#cn)
4 |
5 |
6 |
7 | FPGA JPEG-LS image compressor
8 | ===========================
9 |
10 | **FPGA** based streaming **JPEG-LS** image compressor, features:
11 |
12 | * Pure Verilog design, compatible with various FPGA platforms.
13 | * For compressing **8bit** grayscale images.
14 | * Support **lossless mode**, i.e. NEAR=0 .
15 | * Support **lossy mode**, NEAR=1~7 adjustable.
16 | * The value range of image width is [5,16384], and the value range of height is [1,16384].
17 | * Simple streaming input and output.
18 |
19 |
20 |
21 | # Background
22 |
23 | **JPEG-LS** (**JLS**) is a lossless/lossy image compression algorithm which has the best lossless compression ratio compared to PNG, Lossless-JPEG2000, Lossless-WEBP, Lossless-HEIF, etc.
24 |
25 | **JPEG-LS** uses the maximum difference between the pixels before and after compression (**NEAR** value) to control distortion, **NEAR=0** is the lossless mode; **NEAR>0** is the lossy mode, the larger the **NEAR**, the greater the distortion and the greater the compression ratio.
26 |
27 | The file suffix name for **JPEG-LS** compressed image is .**jls** .
28 |
29 | For JPEG-LS standard specification, see ITU-T T.87.
30 |
31 | If you are interested in the software implementation of JPEG-LS baseline encoder, see https://github.com/WangXuan95/ImCvt (C++ language)
32 |
33 |
34 |
35 | # Module Usage
36 |
37 | [**jls_encoder.v**](./RTL/jls_encoder.v) in the [RTL](./RTL) directory is a JPEG-LS compression module that can be call by the FPGA users, which inputs image raw pixels and outputs a JPEG-LS compressed stream.
38 |
39 | ## Module parameter
40 |
41 | **jls_encoder** has a parameter:
42 |
43 | ```verilog
44 | parameter [2:0] NEAR
45 | ```
46 |
47 | which determines the NEAR value of JPEG-LS algorithm. When the value is 3'd0, it works in lossless mode; when the value is 3'd1~3'd7, it works in lossy mode.
48 |
49 | ## Module Interface
50 |
51 | The input and output signals of **jls_encoder** are described in the following table.
52 |
53 | | Signal | Name | direction | width | description |
54 | | :----: | :------------: | :-------: | :---: | :----------------------------------------------------------- |
55 | | rstn | reset | in | 1bit | When the clock rises, if rstn=0, the module is reset, and rstn=1 in normal use. |
56 | | clk | clock | in | 1bit | All signals should be aligned on the rising edge of clk. |
57 | | i_sof | start of frame | in | 1bit | When a new image needs to be input, keep i_sof=1 for at least 368 clock cycles. |
58 | | i_w | width-1 | in | 14bit | For example, if the image width is 1920, i_w should be set to 14'd1919. Needs to remain valid when i_sof=1. |
59 | | i_h | height-1 | in | 14bit | For example, if the image width is 1080, i_h should be set to 14'd1079. Needs to remain valid when i_sof=1. |
60 | | i_e | input valid | in | 1bit | i_e=1 indicates a valid input pixel is on i_x |
61 | | i_x | input pixel | in | 8bit | The pixel value range is 8'd0 ~ 8'd255 . |
62 | | o_e | output valid | out | 1bit | o_e=1 indicates a valid data is on o_data. |
63 | | o_data | output data | out | 16bit | Little endian, odata[7:0] before, odata[15:8] after. |
64 | | o_last | output last | out | 1bit | o_last=1, indicate that this is the last data of the output stream of an image. |
65 |
66 | > Note:i_w cannot less than 14'd4 。
67 |
68 | ## Input pixels
69 |
70 | The operation flow of **jls_encoder** module is:
71 |
72 | 1. **Reset** (optional): Set `rstn=0` for at least **1 cycle** to reset, and then keep `rstn=1` during normal operation. In fact, it is not necessary to reset.
73 | 2. **Start**: keep `i_sof=1` **at least 368 cycles**, while inputting the width and height of the image on the `i_w` and `i_h` signals, `i_w` and `i_h` should remain valid during` i_sof=1`.
74 | 3. **Input**: Control `i_e` and `i_x`, input all the pixels of the image from left to right, top to bottom. When `i_e=1`, `i_x` is input as a pixel.
75 | 4. **Idle between images**: After all pixel input ends, it needs to be idle for at least 32 cycles without any action (i.e. `i_sof=0`, `i_e=0`). Then you can skip to step 2 and start the next image.
76 |
77 | Between `i_sof=1` and `i_e=1`; and between `i_e=1` each can insert any number of free bubbles (ie, `i_sof=0`, `i_e=0`), which means that we can input pixels intermittently (of course, without inserting any bubbles for maximum performance).
78 |
79 | The following figure shows the input timing diagram of compressing 2 images (//represents omitting several cycles, X represents don't care). where image 1 has 1 bubble inserted after the first pixel is entered; while image 2 has 1 bubble inserted after i_sof=1. Note **Inter-image idle** must be at least **32 cycles**.
80 |
81 | __ __// __ __ __ __ //_ __ // __ __// __ __ __ // __
82 | clk \__/ \__/ //_/ \__/ \__/ \__/ \__// \__/ \__///\__/ \__/ //_/ \__/ \__/ \__///\__/ \_
83 | _______//________ // // _______//________ //
84 | i_sof ____/ // \________________//___________//____/ // \___________//________
85 | _______//________ // // _______//________ //
86 | i_w XXXXX_______//________XXXXXXXXXXXXXXXXX//XXXXXXXXXXX//XXXXX_______//________XXXXXXXXXXXX//XXXXXXXX
87 | _______//________ // // _______//________ //
88 | i_h XXXXX_______//________XXXXXXXXXXXXXXXXX//XXXXXXXXXXX//XXXXX_______//________XXXXXXXXXXXX//XXXXXXXX
89 | // _____ ____//_____ // // _____//____
90 | i_e ____________//________/ \_____/ // \_____//____________//______________/ // \___
91 | // _____ ____//_____ // // _____//____
92 | i_x XXXXXXXXXXXX//XXXXXXXXX_____XXXXXXX____//_____XXXXXX//XXXXXXXXXXXX//XXXXXXXXXXXXXXX_____//____XXXX
93 |
94 | 阶段: | 开始图像1 | 输入图像1 | 图像间空闲 | 开始图像2 | 输入图像2
95 |
96 | ## Output JLS stream
97 |
98 | During the input, **jls_encoder** will also output a compressed **JPEG-LS stream**, which constitutes the content of the complete .jls file (including the file header and trailer). When `o_e=1`, `o_data` is a valid output data. Among them, `o_data` follows the little endian order, that is, `o_data[7:0]` is at the front of the stream, and `o_data[15:8]` is at the back of the stream. `o_last=1` indicates the end of the compressed stream for an image when the output stream for each image encounters the last data.
99 |
100 |
101 |
102 | # RTL Simulation
103 |
104 | Simulation related files are in the [SIM](./SIM) directory, including:
105 |
106 | * [tb_jls_encoder.v](./SIM) is a testbench for jls_encoder. The behavior is: batch uncompressed images in .pgm format in the specified folder into jls_encoder for compression, and then save the output of jls_encoder to a .jls file.
107 | * [tb_jls_encoder_run_iverilog.bat](./SIM) is a command script for iverilog simulation.
108 | * The [images](./SIM) folder contains several image files in .pgm format. The .pgm format stores an uncompressed (that is, raw pixel) 8bit grayscale image, which can be opened with photoshop software or a Linux image viewer (Windows image viewer cannot view it).
109 |
110 | > The .pgm file format is very simple, with only a header to indicate the length and width of the image, followed by all the raw pixels of the image. So I choose .pgm file as the input file for the simulation, because it only needs to write some code in the testbench to parse the .pgm file, and take out the pixels and send it to jls_encoder . However, you can ignore the format of the pgm file, because the work of jls_encoder has nothing to do with the pgm format, it only needs to accept the raw pixels of the image as input. You only need to focus on the simulated waveform and how the image pixels are fed into the jls_encoder.
111 |
112 | Before using iverilog for simulation, you need to install iverilog , see: [iverilog_usage](https://github.com/WangXuan95/WangXuan95/blob/main/iverilog_usage/iverilog_usage.md)
113 |
114 | Then double-click tb_jls_encoder_run_iverilog.bat to run the simulation, which takes more than 10 minutes to run.
115 |
116 | After the simulation is over, you can see that several .jls files are generated in the folder, which are compressed image files. In addition, the simulation also produces a waveform file dump.vcd, you can open dump.vcd with gtkwave to view the waveform.
117 |
118 | In addition, you can also modify some simulation parameters:
119 |
120 | - Modify the macro **NEAR** in tb_jls_encoder.v to change the compression ratio.
121 | - Modify the macro **BUBBLE_CONTROL** in tb_jls_encoder.v to determine how many bubbles to insert between adjacent input pixels:
122 | - When **BUBBLE_CONTROL=0**, no bubbles are inserted.
123 | - When **BUBBLE_CONTROL>0**, insert **BUBBLE_CONTROL ** bubbles.
124 | - When **BUBBLE_CONTROL<0**, insert random **0~(-BUBBLE_CONTROL)** bubbles each time.
125 |
126 |
127 |
128 | ## View compressed JLS file
129 |
130 | Because **JPEG-LS** is niche and professional, most image viewing software cannot view .jls files.
131 |
132 | You can try [this site](https://filext.com/file-extension/JLS) to view .jls files (though this site doesn't work sometimes).
133 |
134 | If the website doesn't work, you can use the decompressor [JPEGLSdec.exe](./SIM) I provided to decompress it back to a .pgm file and view it again. Please run the command with CMD in the [SIM](./SIM) directory:
135 |
136 | ```powershell
137 | JPEGLSdec.exe -o
138 | ```
139 |
140 | For example:
141 |
142 | ```powershell
143 | JPEGLSdec.exe test001.jls -otmp.pgm
144 | ```
145 |
146 | > Note: JPEGLSdec.exe is compiled from the C language source code provided by UBC : http://www.stat.columbia.edu/~jakulin/jpeg-ls/mirror.htm
147 |
148 |
149 |
150 | # FPGA Deployment
151 |
152 | On Xilinx Artix-7 xc7a35tcsg324-2, the synthesized and implemented results are as follows.
153 |
154 | | LUT | FF | BRAM | Max Clock freq. |
155 | | :--------: | :------: | :----------------------------: | :-------------: |
156 | | 2347 (11%) | 932 (2%) | 9 x RAMB18 (9%), total 144Kbit | 35 MHz |
157 |
158 | At 35MHz, the image compression performance is 35 Mpixel/s, which means the compression frame rate for 1920x1080 images is 16.8fps.
159 |
160 |
161 |
162 | # Reference
163 |
164 | - ITU-T T.87 : Information technology – Lossless and near-lossless compression of continuous-tone still images – Baseline : https://www.itu.int/rec/T-REC-T.87/en
165 | - UBC's JPEG-LS baseline Public Domain Code : http://www.stat.columbia.edu/~jakulin/jpeg-ls/mirror.htm
166 | - CharLS, a C++ JPEG-LS library implementation : https://github.com/team-charls/charls
167 | - A Simple JPEG-LS baseline encoder in C language : https://github.com/WangXuan95/ImCvt
168 | - Another Ultra High Performance FPGA-based JPEG-LS encoder : https://github.com/WangXuan95/UH-JLS
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 | FPGA JPEG-LS image compressor
177 | ===========================
178 |
179 | 基于 **FPGA** 的流式的 **JPEG-LS** 图像压缩器,特点是:
180 |
181 | * 纯 Verilog 设计,可在各种FPGA型号上部署
182 | * 用于压缩 **8bit** 的灰度图像。
183 | * 可选**无损模式**,即 NEAR=0 。
184 | * 可选**有损模式**,NEAR=1~7 可调。
185 | * 图像宽度取值范围为 [5,16384],高度取值范围为 [1,16383]。
186 | * 极简流式输入输出。
187 |
188 |
189 |
190 | # 背景知识
191 |
192 | **JPEG-LS** (简称**JLS**)是一种无损/有损的图像压缩算法,其无损模式的压缩率相当优异,优于 PNG、Lossless-JPEG2000、Lossless-WEBP、Lossless-HEIF 等。
193 |
194 | **JPEG-LS** 用压缩前后的像素的最大差值(**NEAR**值)来控制失真,无损模式下 **NEAR=0**;有损模式下**NEAR>0**,**NEAR** 越大,失真越大,压缩率也越大。
195 |
196 | **JPEG-LS** 压缩图像的文件后缀是 .**jls** 。
197 |
198 | JPEG-LS 的标准文档详见 ITU-T T.87
199 |
200 | 如果你对软件版本的 JPEG-LS baseline encoder 感兴趣,可以看 https://github.com/WangXuan95/ImCvt (C++实现)
201 |
202 |
203 |
204 | # 使用方法
205 |
206 | RTL 目录中的 [**jls_encoder.v**](./RTL/jls_encoder.v) 是用户可以调用的 JPEG-LS 压缩模块,它输入图像原始像素,输出 JPEG-LS 压缩流。
207 |
208 | ## 模块参数
209 |
210 | **jls_encoder** 只有一个参数:
211 |
212 | ```verilog
213 | parameter [2:0] NEAR
214 | ```
215 |
216 | 决定了 **NEAR** 值,取值为 3'd0 时,工作在无损模式;取值为 3'd1~3'd7 时,工作在有损模式。
217 |
218 | ## 模块信号
219 |
220 | **jls_encoder** 的输入输出信号描述如下表。
221 |
222 | | 信号名称 | 全称 | 方向 | 宽度 | 描述 |
223 | | :---: | :---: | :---: | :---: | :--- |
224 | | rstn | 同步复位 | input | 1bit | 当时钟上升沿时若 rstn=0,模块复位,正常使用时 rstn=1 |
225 | | clk | 时钟 | input | 1bit | 时钟,所有信号都应该于 clk 上升沿对齐。 |
226 | | i_sof | 图像开始 | input | 1bit | 当需要输入一个新的图像时,保持至少368个时钟周期的 i_sof=1 |
227 | | i_w | 图像宽度-1 | input | 14bit | 例如图像宽度为 1920,则 i_w 应该置为 14‘d1919。需要在 i_sof=1 时保持有效。 |
228 | | i_h | 图像高度-1 | input | 14bit | 例如图像宽度为 1080,则 i_h 应该置为 14‘d1079。需要在 i_sof=1 时保持有效。 |
229 | | i_e | 输入像素有效 | input | 1bit | 当 i_e=1 时,一个像素需要被输入到 i_x 上。 |
230 | | i_x | 输入像素 | input | 8bit | 像素取值范围为 8'd0 ~ 8'd255 。 |
231 | | o_e | 输出有效 | output | 1bit | 当 o_e=1 时,输出流数据产生在 o_data 上。 |
232 | | o_data | 输出流数据 | output | 16bit | 小端序,o_data[7:0] 在先;o_data[15:8] 在后。 |
233 | | o_last | 输出流末尾 | output | 1bit | 当 o_e=1 时若 o_last=1 ,说明这是一张图像的输出流的最后一个数据。 |
234 |
235 | > 注:i_w 不能小于 14'd4 。
236 |
237 | ## 输入图片
238 |
239 | **jls_encoder 模块**的操作的流程是:
240 |
241 | 1. **复位**(可选):令 rstn=0 至少 **1 个周期**进行复位,之后正常工作时都保持 rstn=1。实际上也可以不复位(即让 rstn 恒为1)。
242 | 2. **开始**:保持 i_sof=1 **至少 368 个周期**,同时在 i_w 和 i_h 信号上输入图像的宽度和高度,i_sof=1 期间 i_w 和 i_h 要一直保持有效。
243 | 3. **输入**:控制 i_e 和 i_x,从左到右,从上到下地输入该图像的所有像素。当 i_e=1 时,i_x 作为一个像素被输入。
244 | 4. **图像间空闲**:所有像素输入结束后,需要空闲**至少 32 个周期**不做任何动作(即 i_sof=0,i_e=0)。然后才能跳到第2步,开始下一个图像。
245 |
246 | i_sof=1 和 i_e=1 之间;以及 i_e=1 各自之间可以插入任意个空闲气泡(即, i_sof=0,i_e=0),这意味着我们可以断断续续地输入像素(当然,不插入任何气泡才能达到最高性能)。
247 |
248 | 下图展示了压缩 2 张图像的输入时序图(//代表省略若干周期,X代表don't care)。其中图像 1 在输入第一个像素后插入了 1 个气泡;而图像 2 在 i_sof=1 后插入了 1 个气泡。注意**图像间空闲**必须至少 **32 个周期**。
249 |
250 | __ __// __ __ __ __ //_ __ // __ __// __ __ __ // __
251 | clk \__/ \__/ //_/ \__/ \__/ \__/ \__// \__/ \__///\__/ \__/ //_/ \__/ \__/ \__///\__/ \_
252 | _______//________ // // _______//________ //
253 | i_sof ____/ // \________________//___________//____/ // \___________//________
254 | _______//________ // // _______//________ //
255 | i_w XXXXX_______//________XXXXXXXXXXXXXXXXX//XXXXXXXXXXX//XXXXX_______//________XXXXXXXXXXXX//XXXXXXXX
256 | _______//________ // // _______//________ //
257 | i_h XXXXX_______//________XXXXXXXXXXXXXXXXX//XXXXXXXXXXX//XXXXX_______//________XXXXXXXXXXXX//XXXXXXXX
258 | // _____ ____//_____ // // _____//____
259 | i_e ____________//________/ \_____/ // \_____//____________//______________/ // \___
260 | // _____ ____//_____ // // _____//____
261 | i_x XXXXXXXXXXXX//XXXXXXXXX_____XXXXXXX____//_____XXXXXX//XXXXXXXXXXXX//XXXXXXXXXXXXXXX_____//____XXXX
262 |
263 | 阶段: | 开始图像1 | 输入图像1 | 图像间空闲 | 开始图像2 | 输入图像2
264 |
265 | ## 输出压缩流
266 |
267 | 在输入过程中,**jls_encoder** 同时会输出压缩好的 **JPEG-LS流**,该流构成了完整的 .jls 文件的内容(包括文件头部和尾部)。o_e=1 时,o_data 是一个有效输出数据。其中,o_data 遵循小端序,即 o_data[7:0] 在流中的位置靠前,o_data[15:8] 在流中的位置靠后。在每个图像的输出流遇到最后一个数据时,o_last=1 指示一张图像的压缩流结束。
268 |
269 |
270 |
271 | # 仿真
272 |
273 | 仿真相关文件都在 SIM 目录里,包括:
274 |
275 | * tb_jls_encoder.v 是针对 jls_encoder 的 testbench。行为是:将指定文件夹里的 .pgm 格式的未压缩图像批量送入 jls_encoder 进行压缩,然后将 jls_encoder 的输出结果保存到 .jls 文件里。
276 | * tb_jls_encoder_run_iverilog.bat 包含了执行 iverilog 仿真的命令。
277 | * images 文件夹包含几张 .pgm 格式的图像文件。 .pgm 格式存储的是未压缩(也就是存储原始像素)的 8bit 灰度图像,可以使用 photoshop 软件或 Linux 图像查看器就能打开它(Windows图像查看器查看不了它)。
278 |
279 | > .pgm 文件格式非常简单,只有一个文件头来指示图像的长宽,然后紧接着就存放图像的所有原始像素。因此我选用 .pgm 文件作为仿真的输入文件,因为只需要在 testbench 中简单地编写一些代码就能解析 .pgm 文件,并把其中的像素取出发给 jls_encoder 。不过,你可以不关注 pgm 文件的格式,因为 jls_encoder 的工作与 pgm 格式并没有关系,它只需要接受图像的原始像素作为输入即可。你只需关注仿真的波形,关注图像像素是如何被送入 jls_encoder 中即可。
280 |
281 | 使用 iverilog 进行仿真前,需要安装 iverilog ,见:[iverilog_usage](https://github.com/WangXuan95/WangXuan95/blob/main/iverilog_usage/iverilog_usage.md)
282 |
283 | 然后双击 tb_jls_encoder_run_iverilog.bat 就可以运行仿真,该仿真需要运行十几分钟。
284 |
285 | 仿真结束后,你可以看到文件夹中产生了几个 .jls 文件,它们就是压缩得到的图像文件。另外,仿真还产生了波形文件 dump.vcd ,你可以用 gtkwave 打开 dump.vcd 来查看波形。
286 |
287 | 另外,你还可以修改一些仿真参数来进行:
288 |
289 | - 修改 tb_jls_encoder.v 里的宏名 **NEAR** 来改变压缩率。
290 | - 修改 tb_jls_encoder.v 里的宏名 **BUBBLE_CONTROL** 来决定输入相邻的像素间插入多少个气泡:
291 | - **BUBBLE_CONTROL=0** 时,不插入任何气泡。
292 | - **BUBBLE_CONTROL>0** 时,插入 **BUBBLE_CONTROL **个气泡。
293 | - **BUBBLE_CONTROL<0** 时,每次插入随机的 **0~(-BUBBLE_CONTROL)** 个气泡
294 |
295 | > 在不同 NEAR 值和 BUBBLE_CONTROL 值下,本库已经经过了几百张照片的结果对比验证,充分保证无bug。(这部分自动化验证代码就没放上来了)
296 |
297 | ## 查看压缩结果
298 |
299 | 因为 **JPEG-LS** 比较小众和专业,大多数图片查看软件无法查看 .jls 文件。
300 |
301 | 你可以试试用[该网站](https://filext.com/file-extension/JLS)来查看 .jls 文件(不过这个网站时常失效)。
302 |
303 | 如果该网站失效,可以用我提供的解压器 JPEGLSdec.exe 来把它解压回 .pgm 文件再查看。请在 SIM 目录下用 CMD 运行命令:
304 |
305 | ```powershell
306 | JPEGLSdec.exe -o
307 | ```
308 |
309 | 例如:
310 |
311 | ```powershell
312 | JPEGLSdec.exe test001.jls -otmp.pgm
313 | ```
314 |
315 | > 注:JPEGLSdec.exe 编译自 UBC 提供的 C 语言源码: http://www.stat.columbia.edu/~jakulin/jpeg-ls/mirror.htm
316 |
317 |
318 |
319 | # FPGA 部署
320 |
321 | 在 Xilinx Artix-7 xc7a35tcsg324-2 上,综合和实现的结果如下。
322 |
323 | | LUT | FF | BRAM | 最高时钟频率 |
324 | | :--------: | :------: | :----------------------------: | :----------: |
325 | | 2347 (11%) | 932 (2%) | 9个RAMB18 (9%),等效于 144Kbit | 35 MHz |
326 |
327 | 35MHz 下,图像压缩的性能为 35 Mpixel/s ,对 1920x1080 图像的压缩帧率是 16.8fps 。
328 |
329 |
330 |
331 | # 相关链接
332 |
333 | - ITU-T T.87 : Information technology – Lossless and near-lossless compression of continuous-tone still images – Baseline : https://www.itu.int/rec/T-REC-T.87/en
334 | - UBC's JPEG-LS baseline Public Domain Code : http://www.stat.columbia.edu/~jakulin/jpeg-ls/mirror.htm
335 | - CharLS, a C++ JPEG-LS library implementation : https://github.com/team-charls/charls
336 | - 精简的 JPEG-LS baseline 编码器 (C语言) : https://github.com/WangXuan95/ImCvt
337 | - 另一个高性能的 FPGA-based JPEG-LS encoder : https://github.com/WangXuan95/UH-JLS
338 |
--------------------------------------------------------------------------------
/RTL/jls_encoder.v:
--------------------------------------------------------------------------------
1 |
2 | //--------------------------------------------------------------------------------------------------------
3 | // Module : jls_encoder
4 | // Type : synthesizable, IP's top
5 | // Standard: Verilog 2001 (IEEE1364-2001)
6 | // Function: JPEG-LS image compressor
7 | //--------------------------------------------------------------------------------------------------------
8 |
9 | module jls_encoder #(
10 | parameter [ 2:0] NEAR = 3'd0
11 | ) (
12 | input wire rstn,
13 | input wire clk,
14 | input wire i_sof, // start of image
15 | input wire [13:0] i_w, // image_width-1 , range: 4~16383, that is, image_width range: 5~16384
16 | input wire [13:0] i_h, // image_height-1, range: 0~16382, that is, image_height range: 1~16383
17 | input wire i_e, // input pixel enable
18 | input wire [ 7:0] i_x, // input pixel
19 | output wire o_e, // output data enable
20 | output wire o_last, // indicate the last output data of a image
21 | output wire [15:0] o_data // output data
22 | );
23 |
24 |
25 |
26 | //---------------------------------------------------------------------------------------------------------------------------
27 | // local parameters
28 | //---------------------------------------------------------------------------------------------------------------------------
29 | wire [3:0] P_QBPPS [0:7];
30 | assign P_QBPPS[0] = 4'd8;
31 | assign P_QBPPS[1] = 4'd7;
32 | assign P_QBPPS[2] = 4'd6;
33 | assign P_QBPPS[3] = 4'd6;
34 | assign P_QBPPS[4] = 4'd5;
35 | assign P_QBPPS[5] = 4'd5;
36 | assign P_QBPPS[6] = 4'd5;
37 | assign P_QBPPS[7] = 4'd5;
38 |
39 | localparam P_LOSSY = (NEAR != 3'd0);
40 | localparam signed [8:0] P_NEAR = $signed({6'd0, NEAR});
41 | localparam signed [8:0] P_T1 = $signed(9'd3) + $signed(9'd3) * P_NEAR;
42 | localparam signed [8:0] P_T2 = $signed(9'd7) + $signed(9'd5) * P_NEAR;
43 | localparam signed [8:0] P_T3 = $signed(9'd21)+ $signed(9'd7) * P_NEAR;
44 | localparam signed [9:0] P_QUANT = {P_NEAR, 1'b1};
45 | localparam signed [9:0] P_QBETA = $signed(10'd256 + {5'd0,NEAR,2'd0}) / P_QUANT;
46 | localparam signed [9:0] P_QBETAHALF = (P_QBETA+$signed(10'd1)) / $signed(10'd2);
47 | wire [3:0] P_QBPP = P_QBPPS[NEAR];
48 | wire [4:0] P_LIMIT = 5'd31 - {1'b0, P_QBPP};
49 | localparam [12:0] P_AINIT = (NEAR == 3'd0) ? 13'd4 : 13'd2;
50 |
51 | wire [3:0] J [0:31];
52 | assign J[ 0] = 4'd0;
53 | assign J[ 1] = 4'd0;
54 | assign J[ 2] = 4'd0;
55 | assign J[ 3] = 4'd0;
56 | assign J[ 4] = 4'd1;
57 | assign J[ 5] = 4'd1;
58 | assign J[ 6] = 4'd1;
59 | assign J[ 7] = 4'd1;
60 | assign J[ 8] = 4'd2;
61 | assign J[ 9] = 4'd2;
62 | assign J[10] = 4'd2;
63 | assign J[11] = 4'd2;
64 | assign J[12] = 4'd3;
65 | assign J[13] = 4'd3;
66 | assign J[14] = 4'd3;
67 | assign J[15] = 4'd3;
68 | assign J[16] = 4'd4;
69 | assign J[17] = 4'd4;
70 | assign J[18] = 4'd5;
71 | assign J[19] = 4'd5;
72 | assign J[20] = 4'd6;
73 | assign J[21] = 4'd6;
74 | assign J[22] = 4'd7;
75 | assign J[23] = 4'd7;
76 | assign J[24] = 4'd8;
77 | assign J[25] = 4'd9;
78 | assign J[26] = 4'd10;
79 | assign J[27] = 4'd11;
80 | assign J[28] = 4'd12;
81 | assign J[29] = 4'd13;
82 | assign J[30] = 4'd14;
83 | assign J[31] = 4'd15;
84 |
85 |
86 |
87 | //---------------------------------------------------------------------------------------------------------------------------
88 | // function: is_near
89 | //---------------------------------------------------------------------------------------------------------------------------
90 | function [0:0] func_is_near;
91 | input [7:0] x1, x2;
92 | reg signed [8:0] ex1, ex2;
93 | begin
94 | ex1 = $signed({1'b0,x1});
95 | ex2 = $signed({1'b0,x2});
96 | func_is_near = ((ex1 - ex2 <= P_NEAR) && (ex2 - ex1 <= P_NEAR));
97 | end
98 | endfunction
99 |
100 |
101 | //---------------------------------------------------------------------------------------------------------------------------
102 | // function: predictor (get_px)
103 | //---------------------------------------------------------------------------------------------------------------------------
104 | function [7:0] func_predictor;
105 | input [7:0] a, b, c;
106 | begin
107 | if( c>=a && c>=b )
108 | func_predictor = (a>b) ? b : a;
109 | else if( c<=a && c<=b )
110 | func_predictor = (a>b) ? a : b;
111 | else
112 | func_predictor = a - c + b;
113 | end
114 | endfunction
115 |
116 |
117 | //---------------------------------------------------------------------------------------------------------------------------
118 | // function: q_quantize
119 | //---------------------------------------------------------------------------------------------------------------------------
120 | function signed [3:0] func_q_quantize;
121 | input [7:0] x1, x2;
122 | reg signed [8:0] delta;
123 | begin
124 | delta = $signed({1'b0,x1}) - $signed({1'b0,x2});
125 | if (delta <= -P_T3 )
126 | func_q_quantize = -$signed(4'd4);
127 | else if(delta <= -P_T2 )
128 | func_q_quantize = -$signed(4'd3);
129 | else if(delta <= -P_T1 )
130 | func_q_quantize = -$signed(4'd2);
131 | else if(delta < -P_NEAR )
132 | func_q_quantize = -$signed(4'd1);
133 | else if(delta <= P_NEAR )
134 | func_q_quantize = $signed(4'd0);
135 | else if(delta < P_T1 )
136 | func_q_quantize = $signed(4'd1);
137 | else if(delta < P_T2 )
138 | func_q_quantize = $signed(4'd2);
139 | else if(delta < P_T3 )
140 | func_q_quantize = $signed(4'd3);
141 | else
142 | func_q_quantize = $signed(4'd4);
143 | end
144 | endfunction
145 |
146 |
147 | //---------------------------------------------------------------------------------------------------------------------------
148 | // function: get_q (part 1), qp1 = 81*Q(d-b) + 9*Q(b-c)
149 | //---------------------------------------------------------------------------------------------------------------------------
150 | function signed [9:0] func_get_qp1;
151 | input [7:0] c, b, d;
152 | begin
153 | func_get_qp1 = $signed(10'd81) * func_q_quantize(d,b) + $signed(10'd9) * func_q_quantize(b,c);
154 | end
155 | endfunction
156 |
157 |
158 | //---------------------------------------------------------------------------------------------------------------------------
159 | // function: get_q (part 2), get sign(qs) and abs(qs), where qs = qp1 + Q(c-a)
160 | //---------------------------------------------------------------------------------------------------------------------------
161 | function [9:0] func_get_q;
162 | input signed [9:0] qp1;
163 | input [7:0] c, a;
164 | reg signed [9:0] qs;
165 | reg s;
166 | reg [8:0] q;
167 | begin
168 | qs = qp1 + func_q_quantize(c,a);
169 | s = qs[9];
170 | q = s ? (~qs[8:0]+9'd1) : qs[8:0];
171 | func_get_q = {s, q};
172 | end
173 | endfunction
174 |
175 |
176 | //---------------------------------------------------------------------------------------------------------------------------
177 | // function: clip
178 | //---------------------------------------------------------------------------------------------------------------------------
179 | function [7:0] func_clip;
180 | input signed [9:0] val;
181 | begin
182 | if( val > $signed(10'd255) )
183 | func_clip = 8'd255;
184 | else if( val < $signed(10'd0) )
185 | func_clip = 8'd0;
186 | else
187 | func_clip = val[7:0];
188 | end
189 | endfunction
190 |
191 |
192 | //---------------------------------------------------------------------------------------------------------------------------
193 | // function: errval_quantize
194 | //---------------------------------------------------------------------------------------------------------------------------
195 | function signed [9:0] func_errval_quantize;
196 | input signed [9:0] err;
197 | begin
198 | if(err[9])
199 | func_errval_quantize = -( (P_NEAR - err) / P_QUANT );
200 | else
201 | func_errval_quantize = (P_NEAR + err) / P_QUANT;
202 | end
203 | endfunction
204 |
205 |
206 | //---------------------------------------------------------------------------------------------------------------------------
207 | // function: modrange
208 | //---------------------------------------------------------------------------------------------------------------------------
209 | function signed [9:0] func_modrange;
210 | input signed [9:0] val;
211 | begin
212 | func_modrange = val;
213 | if( func_modrange[9] )
214 | func_modrange = func_modrange + P_QBETA;
215 | if( func_modrange >= P_QBETAHALF )
216 | func_modrange = func_modrange - P_QBETA;
217 | end
218 | endfunction
219 |
220 |
221 | //---------------------------------------------------------------------------------------------------------------------------
222 | // function: get k
223 | //---------------------------------------------------------------------------------------------------------------------------
224 | function [ 3:0] func_get_k;
225 | input [12:0] A;
226 | input [ 6:0] N;
227 | input rt;
228 | reg [18:0] Nt, At;
229 | reg [ 3:0] ii;
230 | begin
231 | Nt = {12'h0, N};
232 | At = { 6'h0, A};
233 | func_get_k = 4'd0;
234 | if (rt)
235 | At = At + {13'd0, N[6:1]};
236 | for (ii=4'd0; ii<4'd13; ii=ii+4'd1)
237 | if((Nt<>> 1;
256 | end
257 | endfunction
258 |
259 |
260 | //---------------------------------------------------------------------------------------------------------------------------
261 | // function: C, B update for regular mode
262 | //---------------------------------------------------------------------------------------------------------------------------
263 | function [14:0] C_B_update;
264 | input reset;
265 | input [6:0] N;
266 | input signed [7:0] C;
267 | input signed [6:0] B;
268 | input signed [9:0] err;
269 | reg signed [9:0] Bt;
270 | reg signed [7:0] Ct;
271 | begin
272 | Bt = B;
273 | Ct = C;
274 | Bt = Bt + (err * P_QUANT);
275 | if(reset)
276 | Bt = Bt >>> 1;
277 | if( Bt <= -$signed({3'd0,N}) ) begin
278 | Bt = Bt + $signed({3'd0,N});
279 | if( Bt <= -$signed({3'd0,N}) )
280 | Bt = -$signed({3'd0,N}-10'd1);
281 | if( Ct != $signed(8'd128) )
282 | Ct = Ct - 8'd1;
283 | end else if( Bt > $signed(10'd0) ) begin
284 | Bt = Bt - $signed({3'd0,N});
285 | if( Bt > $signed(10'd0) )
286 | Bt = $signed(10'd0);
287 | if( Ct != $signed(8'd127) )
288 | Ct = Ct + 8'd1;
289 | end
290 | C_B_update = {Ct, Bt[6:0]};
291 | end
292 | endfunction
293 |
294 |
295 | //---------------------------------------------------------------------------------------------------------------------------
296 | // function: A update
297 | //---------------------------------------------------------------------------------------------------------------------------
298 | function [12:0] A_update;
299 | input reset;
300 | input [12:0] A;
301 | input [ 9:0] inc;
302 | begin
303 | A_update = A + {3'd0, inc};
304 | if(reset)
305 | A_update = A_update >>> 1;
306 | end
307 | endfunction
308 |
309 |
310 | //-------------------------------------------------------------------------------------------------------------------
311 | // context memorys
312 | //-------------------------------------------------------------------------------------------------------------------
313 | reg [ 5:0] Nram [0:365];
314 | reg [12:0] Aram [0:365];
315 | reg signed [ 6:0] Bram [0:365];
316 | reg signed [ 7:0] Cram [1:364];
317 |
318 |
319 | //-------------------------------------------------------------------------------------------------------------------
320 | // pipeline stage a: generate ii, jj
321 | //-------------------------------------------------------------------------------------------------------------------
322 | reg a_sof;
323 | reg a_e;
324 | reg [ 7:0] a_x;
325 | reg [13:0] a_w;
326 | reg [13:0] a_h;
327 | reg [13:0] a_wl;
328 | reg [13:0] a_hl;
329 | reg [13:0] a_ii;
330 | reg [14:0] a_jj;
331 |
332 | always @ (posedge clk)
333 | if(~rstn) begin
334 | {a_sof, a_e, a_x, a_w, a_h, a_wl, a_hl, a_ii, a_jj} <= 0;
335 | end else begin
336 | a_sof <= i_sof;
337 | a_e <= i_e;
338 | a_x <= i_x;
339 | a_w <= i_w;
340 | a_h <= i_h;
341 | if(a_sof) begin
342 | a_wl <= (a_w<14'd4 ? 14'd4 : a_w);
343 | a_hl <= a_h;
344 | a_ii <= 14'd0;
345 | a_jj <= 15'd0;
346 | end else if(a_e) begin
347 | if(a_ii < a_wl)
348 | a_ii <= a_ii + 14'd1;
349 | else begin
350 | a_ii <= 14'd0;
351 | if(a_jj <= {1'b0,a_hl})
352 | a_jj <= a_jj + 15'd1;
353 | end
354 | end
355 | end
356 |
357 |
358 | //-------------------------------------------------------------------------------------------------------------------
359 | // pipeline stage b: generate fc, lc, nfr
360 | //-------------------------------------------------------------------------------------------------------------------
361 | reg b_sof;
362 | reg b_e;
363 | reg b_fc;
364 | reg b_lc;
365 | reg b_fr;
366 | reg b_eof;
367 | reg [13:0] b_ii;
368 | reg [ 7:0] b_x;
369 |
370 | always @ (posedge clk) begin
371 | b_sof <= a_sof & rstn;
372 | if(~rstn | a_sof) begin
373 | {b_e, b_fc, b_lc, b_fr, b_eof, b_ii, b_x} <= 0;
374 | end else begin
375 | b_e <= a_e & (a_jj <= {1'b0,a_hl});
376 | b_fc <= a_e & (a_ii == 14'd0);
377 | b_lc <= a_e & (a_ii == a_wl);
378 | b_fr <= a_e & (a_jj == 15'd0);
379 | b_eof <= a_jj > {1'b0,a_hl};
380 | b_ii <= a_ii;
381 | b_x <= a_x;
382 | end
383 | end
384 |
385 |
386 | //-------------------------------------------------------------------------------------------------------------------
387 | // pipeline stage c: maintain linebuffer, generate context pixels: b, c, d , where d is not valid in case of fr and lc
388 | //-------------------------------------------------------------------------------------------------------------------
389 | reg c_sof;
390 | reg c_e;
391 | reg c_fc;
392 | reg c_lc;
393 | reg c_fr;
394 | reg c_eof;
395 | reg [13:0] c_ii;
396 | reg [ 7:0] c_x;
397 | reg [ 7:0] c_b;
398 | reg [ 7:0] c_bt;
399 | reg [ 7:0] c_c;
400 | reg [ 7:0] c_d;
401 |
402 | always @ (posedge clk) begin
403 | c_sof <= b_sof & rstn;
404 | if(~rstn | b_sof) begin
405 | {c_e,c_fc,c_lc,c_fr,c_eof,c_ii,c_x,c_b,c_bt,c_c} <= 0;
406 | end else begin
407 | c_e <= b_e;
408 | c_fc <= b_fc;
409 | c_lc <= b_lc;
410 | c_fr <= b_fr;
411 | c_eof <= b_eof;
412 | c_ii <= b_ii;
413 | if(b_e) begin
414 | c_x <= b_x;
415 | c_b <= b_fr ? 8'd0 : c_d;
416 | if(b_fr) begin
417 | c_bt <= 8'd0;
418 | c_c <= 8'd0;
419 | end else if(b_fc) begin
420 | c_bt <= c_d;
421 | c_c <= c_bt;
422 | end else
423 | c_c <= c_b;
424 | end
425 | end
426 | end
427 |
428 |
429 | //-------------------------------------------------------------------------------------------------------------------
430 | // pipeline stage d: fix context pixel d (locally) in case fr and lc, get q part-1 (qp1)
431 | //-------------------------------------------------------------------------------------------------------------------
432 | reg d_sof;
433 | reg d_e;
434 | reg d_fc;
435 | reg d_lc;
436 | reg d_eof;
437 | reg [13:0] d_ii;
438 | reg [ 7:0] d_x;
439 | reg [ 7:0] d_b;
440 | reg [ 7:0] d_c;
441 | reg signed [9:0] d_qp1;
442 |
443 | wire [7:0] c_w_d = c_fr ? 8'd0 : (c_lc ? c_b : c_d);
444 |
445 | always @ (posedge clk) begin
446 | d_sof <= c_sof & rstn;
447 | if(~rstn | c_sof) begin
448 | {d_e, d_fc, d_lc, d_eof, d_ii, d_x, d_b, d_c, d_qp1} <= 0;
449 | end else begin
450 | d_e <= c_e;
451 | d_fc <= c_fc;
452 | d_lc <= c_lc;
453 | d_eof <= c_eof;
454 | d_ii <= c_ii;
455 | d_x <= c_x;
456 | d_b <= c_b;
457 | d_c <= c_c;
458 | d_qp1 <= func_get_qp1(c_c, c_b, c_w_d);
459 | end
460 | end
461 |
462 |
463 | //-------------------------------------------------------------------------------------------------------------------
464 | // pipeline stage e: get errval, Rx reconstruct loop, N, B, C update
465 | //-------------------------------------------------------------------------------------------------------------------
466 | reg e_sof;
467 | reg e_e;
468 | reg e_fc;
469 | reg e_lc;
470 | reg e_eof;
471 | reg [13:0] e_ii;
472 | reg e_runi;
473 | reg e_rune;
474 | reg e_2BleN;
475 | reg [7:0] e_x;
476 | reg [8:0] e_q;
477 | reg e_rt;
478 | reg signed [9:0] e_err;
479 | reg [6:0] e_No;
480 | reg e_write_C, e_write_en;
481 | reg [5:0] e_Nn;
482 | reg signed [7:0] e_Cn;
483 | reg signed [6:0] e_Bn;
484 |
485 | wire [7:0] d_w_a = d_fc ? d_b : e_x;
486 |
487 | reg s; // not real register
488 | reg [8:0] q; // not real register
489 | reg rt; // not real register
490 | reg runi; // not real register
491 | reg rune; // not real register
492 | reg signed [7:0] Co; // not real register
493 | reg [6:0] No, Nn; // not real register
494 | reg signed [6:0] Bo; // not real register
495 | reg signed [9:0] px; // not real register
496 | reg signed [9:0] err; // not real register
497 |
498 | always @ (posedge clk) begin
499 | e_sof <= d_sof & rstn;
500 | e_2BleN <= 1'b0;
501 | {e_write_C, e_Cn, e_write_en, e_Bn, e_Nn} <= 0;
502 | if(~rstn | d_sof) begin
503 | {e_e, e_fc, e_lc, e_eof, e_ii, e_runi, e_rune, e_x, e_q, e_rt, e_err, e_No} <= 0;
504 | end else begin
505 | rt = 1'b0;
506 | rune = 1'b0;
507 | No = 0;
508 | err = 0;
509 | {s, q} = func_get_q(d_qp1, d_c, d_w_a);
510 | Co = (e_write_C & e_q==q) ? e_Cn : Cram[q];
511 | runi = ~d_fc & e_runi | (q == 9'd0);
512 | if(runi) begin
513 | runi = func_is_near(d_x, d_w_a);
514 | rune = ~runi;
515 | end
516 | if(d_e) begin
517 | if(runi) begin
518 | e_x <= P_LOSSY ? d_w_a : d_x;
519 | end else begin
520 | if(rune) begin
521 | rt = func_is_near(d_b, d_w_a);
522 | s = {1'b0,d_w_a} > ({1'b0,d_b} + {6'd0,NEAR}) ? 1'b1 : 1'b0;
523 | q = rt ? 9'd365 : 9'd0;
524 | px = rt ? d_w_a : d_b;
525 | end else begin
526 | px[9:8] = 2'b00;
527 | px[7:0] = func_clip( $signed({2'h0, func_predictor(d_w_a, d_b, d_c)}) + ( s ? -$signed({Co[7],Co[7],Co}) : $signed({Co[7],Co[7],Co}) ) );
528 | end
529 | err = s ? px - $signed({2'd0, d_x}) : $signed({2'd0, d_x}) - px;
530 | err = func_errval_quantize(err);
531 | e_x <= P_LOSSY ? func_clip( px + ( s ? -(P_QUANT*err) : P_QUANT*err ) ) : d_x;
532 | err = func_modrange(err);
533 | No = ((e_write_en & e_q==q) ? e_Nn : Nram[q]) + 7'd1;
534 | Nn = No;
535 | if(No[6]) Nn = Nn >>> 1;
536 | e_Nn <= Nn[5:0];
537 | Bo = (e_write_en & e_q==q) ? e_Bn : Bram[q];
538 | e_write_en <= 1'b1;
539 | if(rune) begin
540 | e_Bn <= B_update(No[6], Bo, err<$signed(10'd0));
541 | e_2BleN <= $signed({Bo,1'b0}) < $signed({1'b0,No});
542 | end else begin
543 | e_write_C <= 1'b1;
544 | {e_Cn, e_Bn} <= C_B_update(No[6], Nn+7'd1, Co, Bo, err);
545 | e_2BleN <= $signed({Bo,1'b0}) <= -$signed({1'b0,No});
546 | end
547 | end
548 | e_runi <= runi;
549 | end
550 | e_e <= d_e;
551 | e_fc <= d_fc;
552 | e_lc <= d_lc;
553 | e_eof <= d_eof;
554 | e_ii <= d_ii;
555 | e_rune <= d_e & rune;
556 | e_q <= q;
557 | e_rt <= rt;
558 | e_err <= err;
559 | e_No <= No;
560 | end
561 | end
562 |
563 |
564 | //-------------------------------------------------------------------------------------------------------------------
565 | // pipeline stage f: write Cram, Bram, Nram
566 | //-------------------------------------------------------------------------------------------------------------------
567 | reg [8:0] NBC_init_addr;
568 |
569 | always @ (posedge clk)
570 | NBC_init_addr <= e_sof ? NBC_init_addr + (NBC_init_addr < 9'd366 ? 9'd1 : 9'd0) : 9'd0;
571 |
572 | always @ (posedge clk)
573 | if(e_sof | e_write_en) begin
574 | Nram[e_write_en ? e_q : NBC_init_addr] <= e_Nn;
575 | Bram[e_write_en ? e_q : NBC_init_addr] <= e_Bn;
576 | end
577 |
578 | always @ (posedge clk)
579 | if(e_sof | e_write_C) begin
580 | Cram[e_write_C ? e_q : NBC_init_addr] <= e_Cn;
581 | end
582 |
583 |
584 |
585 |
586 | //-------------------------------------------------------------------------------------------------------------------
587 | // pipeline stage ef: read Aram, buffer registers
588 | //-------------------------------------------------------------------------------------------------------------------
589 | reg ef_sof;
590 | reg ef_e;
591 | reg ef_fc;
592 | reg ef_lc;
593 | reg ef_eof;
594 | reg ef_runi;
595 | reg ef_rune;
596 | reg ef_2BleN;
597 | reg [8:0] ef_q;
598 | reg ef_rt;
599 | reg signed [9:0] ef_err;
600 | reg [6:0] ef_No;
601 | reg [12:0] ef_Ao;
602 | reg ef_write_en;
603 |
604 | always @ (posedge clk) begin
605 | ef_sof <= e_sof & rstn;
606 | if(~rstn | e_sof) begin
607 | {ef_e, ef_fc, ef_lc, ef_eof, ef_runi, ef_rune, ef_2BleN, ef_q, ef_rt, ef_err, ef_No, ef_write_en} <= 0;
608 | end else begin
609 | ef_e <= e_e;
610 | ef_fc <= e_fc;
611 | ef_lc <= e_lc;
612 | ef_eof <= e_eof;
613 | ef_runi <= e_runi & e_e;
614 | ef_rune <= e_rune;
615 | ef_2BleN <= e_2BleN;
616 | ef_q <= e_q;
617 | ef_rt <= e_rt;
618 | ef_err <= e_err;
619 | ef_No <= e_No;
620 | ef_write_en <= e_write_en;
621 | end
622 | end
623 |
624 | always @ (posedge clk)
625 | ef_Ao <= Aram[e_q];
626 |
627 |
628 |
629 | //-------------------------------------------------------------------------------------------------------------------
630 | // pipeline stage f: process run, calcuate merrval and k, A update
631 | //-------------------------------------------------------------------------------------------------------------------
632 | reg f_sof;
633 | reg f_e;
634 | reg f_eof;
635 | reg f_runi;
636 | reg f_rune;
637 | reg [ 9:0] f_merr;
638 | reg [ 3:0] f_k;
639 | reg [15:0] f_rc;
640 | reg [ 4:0] f_ri;
641 | reg [ 1:0] f_on;
642 | reg [15:0] f_cb;
643 | reg [ 4:0] f_cn; // in range of 0~16
644 | reg [ 4:0] f_limit;
645 | reg [8:0] f_q;
646 | reg f_write_en;
647 | reg [12:0] f_An;
648 |
649 | reg [8:0] g_q;
650 | reg g_write_en;
651 | reg [12:0] g_An;
652 | always @ (posedge clk)
653 | if(~rstn | f_sof)
654 | {g_q, g_write_en, g_An} <= 0;
655 | else
656 | {g_q, g_write_en, g_An} <= {f_q, f_write_en, f_An};
657 |
658 | reg [ 1:0] on; // not real register
659 | reg [15:0] rc; // not real register
660 | reg [ 4:0] ri; // not real register
661 | reg [12:0] Ao; // not real register
662 | reg [ 3:0] k; // not real register
663 | reg [ 9:0] abserr; // not real register
664 | reg [ 9:0] merr, Ainc; // not real register
665 | reg map; // not real register
666 |
667 | always @ (posedge clk) begin
668 | f_sof <= ef_sof & rstn;
669 | f_limit <= P_LIMIT;
670 | f_An <= P_AINIT;
671 | if(~rstn | ef_sof) begin
672 | {f_e, f_eof, f_runi, f_rune, f_merr, f_k, f_rc, f_ri, f_on, f_cb, f_cn, f_q, f_write_en} <= 0;
673 | end else begin
674 | on = 2'd0;
675 | rc = (ef_fc|~ef_runi) ? 16'd0 : f_rc;
676 | ri = f_ri;
677 | Ao = (f_write_en & f_q==ef_q) ? f_An : (g_write_en & g_q==ef_q) ? g_An : ef_Ao;
678 | abserr = ef_err<$signed(10'd0) ? $unsigned(-ef_err) : $unsigned(ef_err);
679 | merr = 10'd0;
680 | Ainc = 10'd0;
681 | f_write_en <= ef_write_en;
682 | k = func_get_k(Ao, ef_No, ef_rt);
683 | f_cb <= ef_fc ? 16'd0 : f_rc;
684 | f_cn <= {1'b0,J[ri]} + 5'd1;
685 | if(ef_runi) begin
686 | rc = rc + 16'd1;
687 | if(rc >= (16'd1< 16'd0))
693 | on = on + 2'd1;
694 | end else if(ef_rune) begin
695 | f_limit <= P_LIMIT - 5'd1 - {1'b0,J[ri]};
696 | if (ri > 5'd0) ri = ri - 5'd1;
697 | map = ~( (ef_err==10'd0) | ( (ef_err>$signed(10'd0)) ^ (k==4'd0 & ef_2BleN) ) );
698 | merr = (abserr<<1) - {9'd0,ef_rt} - {9'd0,map};
699 | Ainc = ((merr + {9'd0,~ef_rt}) >> 1);
700 | end else begin
701 | map = (~P_LOSSY) & (k==4'd0) & ef_2BleN;
702 | if(ef_err < $signed(10'd0))
703 | merr = (abserr<<1) - 10'd1 - {9'd0,map};
704 | else
705 | merr = (abserr<<1) + {9'd0,map};
706 | Ainc = (ef_err < $signed(10'd0)) ? $unsigned(-ef_err) : $unsigned(ef_err);
707 | end
708 | if(ef_e) begin
709 | f_rc <= rc;
710 | f_ri <= ri;
711 | end
712 | f_An <= A_update(ef_No[6], Ao, Ainc);
713 | f_e <= ef_e;
714 | f_eof <= ef_eof;
715 | f_runi <= ef_runi;
716 | f_rune <= ef_rune;
717 | f_merr <= merr;
718 | f_k <= k;
719 | f_on <= on;
720 | f_q <= ef_q;
721 | end
722 | end
723 |
724 |
725 | //-------------------------------------------------------------------------------------------------------------------
726 | // pipeline stage g: write Aram
727 | //-------------------------------------------------------------------------------------------------------------------
728 | reg [8:0] A_init_addr;
729 | always @ (posedge clk)
730 | A_init_addr <= f_sof ? A_init_addr + (A_init_addr < 9'd366 ? 9'd1 : 9'd0) : 9'd0;
731 |
732 | always @ (posedge clk)
733 | if(f_sof | f_write_en)
734 | Aram[f_write_en ? f_q : A_init_addr] <= f_An;
735 |
736 |
737 | //-------------------------------------------------------------------------------------------------------------------
738 | // pipeline stage g: golomb coding parser
739 | //-------------------------------------------------------------------------------------------------------------------
740 | reg g_sof;
741 | reg g_e;
742 | reg g_eof;
743 | reg g_runi;
744 | reg [ 1:0] g_on; // in range of 0~2
745 | reg [15:0] g_cb;
746 | reg [ 4:0] g_cn; // in range of 0~16
747 | reg [ 4:0] g_zn; // in range of 0~27
748 | reg [ 9:0] g_db;
749 | reg [ 3:0] g_dn; // in range of 0~13
750 |
751 | wire [ 9:0] f_w_merr_sk = (f_merr >> f_k);
752 |
753 | always @ (posedge clk) begin
754 | g_sof <= f_sof & rstn;
755 | if(~rstn | f_sof) begin
756 | {g_e, g_eof, g_runi, g_on, g_cb, g_cn, g_zn, g_db, g_dn} <= 0;
757 | end else begin
758 | g_e <= f_e;
759 | g_eof <= f_eof;
760 | g_runi <= f_runi;
761 | g_on <= f_on;
762 | g_cb <= f_rune ? f_cb : 16'd0;
763 | g_cn <= f_rune ? f_cn : 5'd0;
764 | if(f_w_merr_sk < f_limit) begin
765 | g_zn <= f_w_merr_sk[4:0];
766 | g_db <= f_merr & ~(10'h3ff<> j_bcnt);
836 | bcnt = j_bcnt + {2'd0,h_bn};
837 | if(bcnt >= 8'd16) begin
838 | j_e <= 1'b1;
839 | j_data[15:8] <= bbuf[247:240];
840 | if(bbuf[247:240] == 8'hFF) begin
841 | bbuf = {1'h0, bbuf[239:0], 7'h0};
842 | bcnt = bcnt - 8'd7;
843 | end else begin
844 | bbuf = { bbuf[239:0], 8'h0};
845 | bcnt = bcnt - 8'd8;
846 | end
847 | j_data[ 7:0] <= bbuf[247:240];
848 | if(bbuf[247:240] == 8'hFF) begin
849 | bbuf = {1'h0, bbuf[239:0], 7'h0};
850 | bcnt = bcnt - 8'd7;
851 | end else begin
852 | bbuf = { bbuf[239:0], 8'h0};
853 | bcnt = bcnt - 8'd8;
854 | end
855 | end else if(h_eof) begin
856 | if (bcnt > 8'd0) begin
857 | j_e <= 1'b1;
858 | j_data[15:8] <= bbuf[247:240];
859 | if (bbuf[247:240] == 8'hFF) begin
860 | j_data[ 7:0] <= {1'b0,bbuf[239:233]};
861 | end else if (bcnt > 8'd8) begin
862 | j_data[ 7:0] <= bbuf[239:232];
863 | end else begin
864 | j_data[ 7:0] <= jls_footer[15:8];
865 | state_footer <= 2'd1;
866 | end
867 | end else if (state_footer < 2'd2) begin
868 | j_e <= 1'b1;
869 | j_last <= 1'b1;
870 | j_data <= (state_footer==2'd0) ? jls_footer : {jls_footer[7:0], 8'd0};
871 | state_footer <= 2'd2;
872 | end
873 | bbuf = 248'd0;
874 | bcnt = 8'd0;
875 | end
876 | j_bbuf <= bbuf;
877 | j_bcnt <= bcnt;
878 | end
879 | end
880 |
881 |
882 | //-------------------------------------------------------------------------------------------------------------------
883 | // make .jls file header and footer
884 | //-------------------------------------------------------------------------------------------------------------------
885 | reg [15:0] jls_wl, jls_hl;
886 | wire[15:0] jls_header [0:11];
887 | assign jls_header[0] = 16'hFFD8;
888 | assign jls_header[1] = 16'hFFF7;
889 | assign jls_header[2] = 16'h000B;
890 | assign jls_header[3] = {8'h08 , jls_hl[15:8]};
891 | assign jls_header[4] = {jls_hl[7:0], jls_wl[15:8]};
892 | assign jls_header[5] = {jls_wl[7:0], 8'h01};
893 | assign jls_header[6] = 16'h0111;
894 | assign jls_header[7] = 16'h00FF;
895 | assign jls_header[8] = 16'hDA00;
896 | assign jls_header[9] = 16'h0801;
897 | assign jls_header[10]= 16'h0100;
898 | assign jls_header[11]= {5'b0,NEAR, 8'h0};
899 |
900 | always @ (posedge clk)
901 | if(~rstn) begin
902 | jls_wl <= 16'd0;
903 | jls_hl <= 16'd0;
904 | end else begin
905 | jls_wl <= {2'd0,a_wl} + 16'd1;
906 | jls_hl <= {2'd0,a_hl} + 16'd1;
907 | end
908 |
909 |
910 | //-------------------------------------------------------------------------------------------------------------------
911 | // pipeline stage k: add .jls file header and footer
912 | //-------------------------------------------------------------------------------------------------------------------
913 | reg [3:0] k_header_i;
914 | reg k_e;
915 | reg k_last;
916 | reg [15:0] k_data;
917 |
918 | always @ (posedge clk) begin
919 | k_e <= 1'b0;
920 | k_last <= 1'b0;
921 | k_data <= 16'd0;
922 | if(j_sof) begin
923 | if(k_header_i < 4'd12) begin
924 | k_e <= 1'b1;
925 | k_data <= jls_header[k_header_i];
926 | k_header_i <= k_header_i + 4'd1;
927 | end
928 | end else if(j_e) begin
929 | k_header_i <= 4'd0;
930 | k_e <= 1'b1;
931 | k_last <= j_last;
932 | k_data <= j_data;
933 | end else begin
934 | k_header_i <= 4'd0;
935 | end
936 | end
937 |
938 |
939 | //-------------------------------------------------------------------------------------------------------------------
940 | // linebuffer for context pixels
941 | //-------------------------------------------------------------------------------------------------------------------
942 | reg [7:0] linebuffer [0:(1<<14)-1];
943 | always @ (posedge clk) // line buffer read
944 | c_d <= linebuffer[a_ii];
945 | always @ (posedge clk) // line buffer write
946 | if(e_e) linebuffer[e_ii] <= e_x;
947 |
948 |
949 | //-------------------------------------------------------------------------------------------------------------------
950 | // output signal
951 | //-------------------------------------------------------------------------------------------------------------------
952 | assign o_e = k_e;
953 | assign o_last = k_last;
954 | assign o_data = {k_data[7:0], k_data[15:8]};
955 |
956 |
957 | endmodule
958 |
959 |
--------------------------------------------------------------------------------
/SIM/JPEGLSdec.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/JPEGLSdec.exe
--------------------------------------------------------------------------------
/SIM/images/test001.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test001.pgm
--------------------------------------------------------------------------------
/SIM/images/test002.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test002.pgm
--------------------------------------------------------------------------------
/SIM/images/test003.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test003.pgm
--------------------------------------------------------------------------------
/SIM/images/test004.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test004.pgm
--------------------------------------------------------------------------------
/SIM/images/test005.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test005.pgm
--------------------------------------------------------------------------------
/SIM/images/test006.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test006.pgm
--------------------------------------------------------------------------------
/SIM/images/test007.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test007.pgm
--------------------------------------------------------------------------------
/SIM/images/test008.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test008.pgm
--------------------------------------------------------------------------------
/SIM/images/test009.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test009.pgm
--------------------------------------------------------------------------------
/SIM/images/test010.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test010.pgm
--------------------------------------------------------------------------------
/SIM/images/test011.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test011.pgm
--------------------------------------------------------------------------------
/SIM/images/test012.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test012.pgm
--------------------------------------------------------------------------------
/SIM/images/test013.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test013.pgm
--------------------------------------------------------------------------------
/SIM/images/test014.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test014.pgm
--------------------------------------------------------------------------------
/SIM/images/test015.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test015.pgm
--------------------------------------------------------------------------------
/SIM/images/test016.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test016.pgm
--------------------------------------------------------------------------------
/SIM/images/test017.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test017.pgm
--------------------------------------------------------------------------------
/SIM/images/test018.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test018.pgm
--------------------------------------------------------------------------------
/SIM/images/test019.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test019.pgm
--------------------------------------------------------------------------------
/SIM/images/test020.pgm:
--------------------------------------------------------------------------------
1 | P5
2 | 16384 2
3 | 255
4 | SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS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TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
--------------------------------------------------------------------------------
/SIM/images/test021.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test021.pgm
--------------------------------------------------------------------------------
/SIM/images/test022.pgm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WangXuan95/FPGA-JPEG-LS-encoder/6bce7c5c21bd7c17bc2a0796171c1ac28a09d959/SIM/images/test022.pgm
--------------------------------------------------------------------------------
/SIM/tb_jls_encoder.v:
--------------------------------------------------------------------------------
1 |
2 | //--------------------------------------------------------------------------------------------------------
3 | // Module : tb_jls_encoder
4 | // Type : simulation, top
5 | // Standard: Verilog 2001 (IEEE1364-2001)
6 | // Function: testbench for jls_encoder,
7 | // load some .pgm files (uncompressed image file), and push them to jls_encoder.
8 | // get output JPEG-LS stream from jls_encoder, and write them to .jls files (JPEG-LS image file)
9 | //--------------------------------------------------------------------------------------------------------
10 |
11 | `timescale 1ps/1ps
12 |
13 | `define NEAR 1 // NEAR can be 0~7
14 |
15 | `define FILE_NO_FIRST 1 // first input file number
16 | `define FILE_NO_FINAL 22 // final input file number
17 |
18 |
19 | // bubble numbers that insert between pixels
20 | // when = 0, do not insert bubble
21 | // when > 0, insert BUBBLE_CONTROL bubbles
22 | // when < 0, insert random 0~(-BUBBLE_CONTROL) bubbles
23 | `define BUBBLE_CONTROL (-1)
24 |
25 |
26 | // the input and output file names' format
27 | `define FILE_NAME_FORMAT "test%03d"
28 |
29 | // input file (uncompressed .pgm file) directory
30 | `define INPUT_PGM_DIR "./images"
31 |
32 | // output file (compressed .jls file) directory
33 | `define OUTPUT_JLS_DIR "."
34 |
35 |
36 | module tb_jls_encoder ();
37 |
38 | //initial $dumpvars(1, tb_jls_encoder);
39 |
40 | // -------------------------------------------------------------------------------------------------------------------
41 | // generate clock and reset
42 | // -------------------------------------------------------------------------------------------------------------------
43 | reg rstn = 1'b0;
44 | reg clk = 1'b0;
45 | always #50000 clk = ~clk; // 10MHz
46 | initial begin repeat(4) @(posedge clk); rstn<=1'b1; end
47 |
48 |
49 | // -------------------------------------------------------------------------------------------------------------------
50 | // signals for jls_encoder_i module
51 | // -------------------------------------------------------------------------------------------------------------------
52 | reg i_sof = 0;
53 | reg [13:0] i_w = 0;
54 | reg [13:0] i_h = 0;
55 | reg i_e = 0;
56 | reg [ 7:0] i_x = 0;
57 | wire o_e;
58 | wire o_last;
59 | wire[15:0] o_data;
60 |
61 |
62 |
63 | reg [7:0] img [8192*8192-1:0];
64 | integer w = 0, h = 0;
65 |
66 | task load_img;
67 | input [256*8:1] fname;
68 | reg [256*8-1:0] line;
69 | integer linelen, depth, scanf_num, fp, i;
70 | begin
71 | depth = 0;
72 | fp = $fopen(fname, "rb");
73 | if (fp==0) begin
74 | $display("*** error: could not open file %s", fname);
75 | $finish;
76 | end
77 | linelen = $fgets(line, fp);
78 | if (line[8*(linelen-2)+:16] != 16'h5035) begin
79 | $display("*** error: the first line must be P5");
80 | $fclose(fp);
81 | $finish;
82 | end
83 | scanf_num = $fgets(line, fp);
84 | scanf_num = $sscanf(line, "%d%d", w, h);
85 | if(scanf_num == 1) begin
86 | scanf_num = $fgets(line, fp);
87 | scanf_num = $sscanf(line, "%d", h);
88 | end
89 | scanf_num = $fgets(line, fp);
90 | scanf_num = $sscanf(line, "%d", depth);
91 | if (depth!=255) begin
92 | $display("*** error: images depth must be 255");
93 | $fclose(fp);
94 | $finish;
95 | end
96 | for (i=0; i 0, insert bubble_control bubbles
112 | // when < 0, insert random 0~bubble_control bubbles
113 | // -------------------------------------------------------------------------------------------------------------------
114 | task feed_img;
115 | input integer bubble_control;
116 | integer num_bubble, i;
117 | begin
118 | // start feeding a image by assert i_sof for 368 cycles
119 | repeat(368) begin
120 | @(posedge clk)
121 | i_sof <= 1'b1;
122 | i_w <= w - 1;
123 | i_h <= h - 1;
124 | {i_e, i_x} <= 0;
125 | end
126 |
127 | // for all pixels of the image
128 | for(i=0; i 16384 || h < 1 || h > 16384 ) // image size not supported
194 | $display(" *** image size not supported ***");
195 | else
196 | feed_img(`BUBBLE_CONTROL);
197 | end
198 |
199 | repeat(100) @(posedge clk);
200 |
201 | $finish;
202 | end
203 |
204 |
205 | // -------------------------------------------------------------------------------------------------------------------
206 | // write output stream to .jls files
207 | // -------------------------------------------------------------------------------------------------------------------
208 | reg [256*8:1] output_file_format;
209 | initial $sformat(output_file_format, "%s\\%s.jls", `OUTPUT_JLS_DIR, `FILE_NAME_FORMAT);
210 | reg [256*8:1] output_file_name;
211 | integer opened = 0;
212 | integer jls_file = 0;
213 |
214 | always @ (posedge clk)
215 | if(o_e) begin
216 | // the first data of an output stream, open a new file.
217 | if(opened == 0) begin
218 | opened = 1;
219 | $sformat(output_file_name, output_file_format, file_no);
220 | jls_file = $fopen(output_file_name , "wb");
221 | end
222 |
223 | // write data to file.
224 | if(opened != 0 && jls_file != 0)
225 | $fwrite(jls_file, "%c%c", o_data[7:0], o_data[15:8]);
226 |
227 | // if it is the last data of an output stream, close the file.
228 | if(o_last) begin
229 | opened = 0;
230 | $fclose(jls_file);
231 | end
232 | end
233 |
234 | endmodule
235 |
--------------------------------------------------------------------------------
/SIM/tb_jls_encoder_run_iverilog.bat:
--------------------------------------------------------------------------------
1 | del sim.out dump.vcd
2 | iverilog -g2001 -o sim.out tb_jls_encoder.v ../RTL/jls_encoder.v
3 | vvp -n sim.out
4 | del sim.out
5 | pause
--------------------------------------------------------------------------------