3030
3131#include <ctype.h>
3232#include <inttypes.h>
33- #include <string.h>
3433#include <stdbool.h>
34+ #include <stdio.h> // for fprintf, stderr
35+ #include <string.h> // for memcmp, strlen, memcpy
3536
3637#include "../libgpujpeg/gpujpeg_decoder.h"
3738#include "gpujpeg_decoder_internal.h"
@@ -117,7 +118,7 @@ struct gpujpeg_reader
117118 * @return marker code or -1 if failed
118119 */
119120static int
120- gpujpeg_reader_read_marker (uint8_t * * image , const uint8_t * image_end )
121+ gpujpeg_reader_read_marker (uint8_t * * image , const uint8_t * image_end , int verbose )
121122{
122123 if (image_end - * image < 2 ) {
123124 fprintf (stderr , "[GPUJPEG] [Error] Failed to read marker from JPEG data (end of data)\n" );
@@ -130,6 +131,7 @@ gpujpeg_reader_read_marker(uint8_t** image, const uint8_t* image_end)
130131 return -1 ;
131132 }
132133 int marker = gpujpeg_reader_read_byte (* image );
134+ DEBUG2_MSG (verbose , "Read marker %s\n" , gpujpeg_marker_name (marker ));
133135 return marker ;
134136}
135137
@@ -418,7 +420,7 @@ gpujpeg_reader_read_spiff_directory(uint8_t** image, const uint8_t* image_end, i
418420 uint32_t tag = gpujpeg_reader_read_4byte (* image );
419421 DEBUG2_MSG (verbose , "Read SPIFF tag 0x%x with length %d.\n" , tag , length + 2 );
420422 if (tag == SPIFF_ENTRY_TAG_EOD && length == SPIFF_ENTRY_TAG_EOD_LENGHT - 2 ) {
421- int marker_soi = gpujpeg_reader_read_marker (image , image_end );
423+ int marker_soi = gpujpeg_reader_read_marker (image , image_end , verbose );
422424 if ( marker_soi != GPUJPEG_MARKER_SOI ) {
423425 VERBOSE_MSG (verbose , "SPIFF entry 0x1 should be followed directly with SOI.\n" );
424426 return -1 ;
@@ -606,6 +608,19 @@ gpujpeg_reader_read_com(uint8_t** image, const uint8_t* image_end, bool ff_cs_it
606608 return 0 ;
607609}
608610
611+ static void
612+ quant_table_dump (unsigned index , const struct gpujpeg_table_quantization * table )
613+ {
614+ printf ("quantization table 0x%02x:" , index );
615+ for ( int i = 0 ; i < 64 ; ++ i ) {
616+ if ( i % 8 == 0 ) {
617+ printf ("\n" );
618+ }
619+ printf ("%hu\t" , table -> table [i ]);
620+ }
621+ printf ("\n\n" );
622+ }
623+
609624/**
610625 * Read quantization table definition block from image
611626 *
@@ -656,6 +671,10 @@ gpujpeg_reader_read_dqt(struct gpujpeg_decoder* decoder, uint8_t** image, const
656671
657672 // Prepare quantization table for read raw table
658673 gpujpeg_table_quantization_decoder_compute (table );
674+
675+ if (decoder -> coder .param .verbose >= LL_DEBUG2 ) {
676+ quant_table_dump (index , table );
677+ }
659678 }
660679 return 0 ;
661680}
@@ -788,6 +807,32 @@ gpujpeg_reader_read_sof0(struct gpujpeg_parameters * param, struct gpujpeg_image
788807 return 0 ;
789808}
790809
810+ static void
811+ huff_table_dump (int Th , int Tc , const struct gpujpeg_table_huffman_decoder * table )
812+ {
813+ const char * comp_type = "(unknown)" ;
814+ switch ( Th ) {
815+ case 0 :
816+ comp_type = "lum" ;
817+ break ;
818+ case 1 :
819+ comp_type = "chr" ;
820+ break ;
821+ }
822+ printf ("table index 0x%02x (Tc: %d /%s/, Th: %d /%s/):\n" , Th | (Tc << 4 ), Tc , Tc == 0 ? "DC" : "AC" , Th ,
823+ comp_type );
824+ int hi = 0 ;
825+ for ( unsigned i = 1 ; i < sizeof table -> bits / sizeof table -> bits [0 ]; ++ i ) {
826+ printf ("values per %2u bits - count: %3hhu, list:" , i , table -> bits [i ]);
827+ for ( int j = hi ; j < hi + table -> bits [i ]; ++ j ) {
828+ printf (" %3hhu" , table -> huffval [j ]);
829+ }
830+ hi += table -> bits [i ];
831+ printf ("\n" );
832+ }
833+ printf ("total: %d\n\n" , hi );
834+ }
835+
791836/**
792837 * Read huffman table definition block from image
793838 *
@@ -852,6 +897,10 @@ gpujpeg_reader_read_dht(struct gpujpeg_decoder* decoder, uint8_t** image, const
852897 // Compute huffman table for read values
853898 gpujpeg_table_huffman_decoder_compute (table );
854899
900+ if (decoder -> coder .param .verbose >= LL_DEBUG2 ) {
901+ huff_table_dump (Th , Tc , table );
902+ }
903+
855904 // Copy table to device memory
856905 cudaMemcpyAsync (d_table , table , sizeof (struct gpujpeg_table_huffman_decoder ), cudaMemcpyHostToDevice , decoder -> stream );
857906 gpujpeg_cuda_check_error ("Decoder copy huffman table " , return - 1 );
@@ -1416,7 +1465,7 @@ gpujpeg_reader_read_image(struct gpujpeg_decoder* decoder, uint8_t* image, size_
14161465 uint8_t * image_end = image + image_size ;
14171466
14181467 // Check first SOI marker
1419- int marker_soi = gpujpeg_reader_read_marker (& image , image_end );
1468+ int marker_soi = gpujpeg_reader_read_marker (& image , image_end , decoder -> coder . param . verbose );
14201469 if ( marker_soi != GPUJPEG_MARKER_SOI ) {
14211470 fprintf (stderr , "[GPUJPEG] [Error] JPEG data should begin with SOI marker, but marker %s was found!\n" , gpujpeg_marker_name ((enum gpujpeg_marker_code )marker_soi ));
14221471 return -1 ;
@@ -1426,7 +1475,7 @@ gpujpeg_reader_read_image(struct gpujpeg_decoder* decoder, uint8_t* image, size_
14261475 _Bool in_spiff = 0 ;
14271476 while ( eoi_presented == 0 ) {
14281477 // Read marker
1429- int marker = gpujpeg_reader_read_marker (& image , image_end );
1478+ int marker = gpujpeg_reader_read_marker (& image , image_end , decoder -> coder . param . verbose );
14301479 if ( marker == -1 ) {
14311480 return -1 ;
14321481 }
@@ -1544,7 +1593,7 @@ gpujpeg_reader_get_image_info(uint8_t *image, size_t image_size, struct gpujpeg_
15441593 param -> restart_interval = 0 ;
15451594
15461595 // Check first SOI marker
1547- int marker_soi = gpujpeg_reader_read_marker (& image , image_end );
1596+ int marker_soi = gpujpeg_reader_read_marker (& image , image_end , param -> verbose );
15481597 if (marker_soi != GPUJPEG_MARKER_SOI ) {
15491598 fprintf (stderr , "[GPUJPEG] [Error] JPEG data should begin with SOI marker, but marker %s was found!\n" , gpujpeg_marker_name ((enum gpujpeg_marker_code )marker_soi ));
15501599 return -1 ;
@@ -1554,7 +1603,7 @@ gpujpeg_reader_get_image_info(uint8_t *image, size_t image_size, struct gpujpeg_
15541603 _Bool in_spiff = 0 ;
15551604 while (eoi_presented == 0 ) {
15561605 // Read marker
1557- int marker = gpujpeg_reader_read_marker (& image , image_end );
1606+ int marker = gpujpeg_reader_read_marker (& image , image_end , param -> verbose );
15581607 if (marker == -1 ) {
15591608 return -1 ;
15601609 }
0 commit comments