jpeg_dqt.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // (C) 2020 Ultraembedded
  2. // SPDX-License-Identifier: Apache-2.0
  3. // https://github.com/ultraembedded/core_jpeg
  4. #ifndef JPEG_DQT_H
  5. #define JPEG_DQT_H
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. // Zigzag table
  12. static const int m_zigzag_table[]=
  13. {
  14. 0, 1, 8, 16,9, 2, 3,10,
  15. 17,24,32,25,18,11, 4, 5,
  16. 12,19,26,33,40,48,41,34,
  17. 27,20,13, 6, 7,14,21,28,
  18. 35,42,49,56,57,50,43,36,
  19. 29,22,15,23,30,37,44,51,
  20. 58,59,52,45,38,31,39,46,
  21. 53,60,61,54,47,55,62,63,
  22. 0
  23. };
  24. #ifndef dprintf
  25. #define dprintf(...)
  26. #endif
  27. //-----------------------------------------------------------------------------
  28. // jpeg_dqt:
  29. //-----------------------------------------------------------------------------
  30. class jpeg_dqt
  31. {
  32. public:
  33. jpeg_dqt() { reset(); }
  34. //-------------------------------------------------------------------------
  35. // reset: Reset DQT tables
  36. //-------------------------------------------------------------------------
  37. void reset(void)
  38. {
  39. memset(&m_table_dqt[0], 0, 64 * 4);
  40. }
  41. //-------------------------------------------------------------------------
  42. // process: Store DQT table from input stream
  43. //-------------------------------------------------------------------------
  44. int process(uint8_t *data, int len)
  45. {
  46. uint8_t *buf = data;
  47. // Table number
  48. uint8_t table_num = (*buf++) & 0x3;
  49. dprintf(" DQT: Table %d\n", table_num);
  50. for (int x=0;x<64;x++)
  51. {
  52. // 8-bit
  53. uint8_t qv = *buf++;
  54. dprintf(" %d: %x\n", x, qv);
  55. m_table_dqt[table_num][x] = qv;
  56. }
  57. return buf - data;
  58. }
  59. //-------------------------------------------------------------------------
  60. // lookup: DQT table entry lookup
  61. //-------------------------------------------------------------------------
  62. uint8_t lookup(int table_num, int position)
  63. {
  64. return m_table_dqt[table_num][position];
  65. }
  66. //-------------------------------------------------------------------------
  67. // process_samples: Multiply out samples and de-zigzag ready for IDCT
  68. // samples: (idx, value)
  69. //-------------------------------------------------------------------------
  70. void process_samples(int quant_table, int *sample_in, int *block_out, int count)
  71. {
  72. // Apply quantisation and zigzag
  73. memset(block_out, 0, sizeof(block_out[0])*64);
  74. for (int i=0;i<count;i++)
  75. {
  76. int16_t smpl = (int16_t)(sample_in[i] & 0xFFFF);
  77. int block_idx = (sample_in[i] >> 16);
  78. dprintf("DEQ: %d: %d * %d -> %d @ %d\n", block_idx, smpl, lookup(quant_table,block_idx), smpl * lookup(quant_table,block_idx), m_zigzag_table[block_idx]);
  79. block_out[m_zigzag_table[block_idx]] = smpl * lookup(quant_table,block_idx);
  80. }
  81. }
  82. private:
  83. uint8_t m_table_dqt[4][64];
  84. };
  85. #endif