2
0

mjpegenc_huffman.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * MJPEG encoder
  3. * Copyright (c) 2016 William Ma, Ted Ying, Jerry Jiang
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/error.h"
  27. #include "libavutil/qsort.h"
  28. #include "mjpegenc_huffman.h"
  29. /**
  30. * Comparison function for two PTables by prob
  31. *
  32. * @param a First PTable to compare
  33. * @param b Second PTable to compare
  34. * @return < 0 for less than, 0 for equals, > 0 for greater than
  35. */
  36. static int compare_by_prob(const void *a, const void *b)
  37. {
  38. PTable a_val = *(PTable *) a;
  39. PTable b_val = *(PTable *) b;
  40. return a_val.prob - b_val.prob;
  41. }
  42. /**
  43. * Comparison function for two HuffTables by length
  44. *
  45. * @param a First HuffTable to compare
  46. * @param b Second HuffTable to compare
  47. * @return < 0 for less than, 0 for equals, > 0 for greater than
  48. */
  49. static int compare_by_length(const void *a, const void *b)
  50. {
  51. HuffTable a_val = *(HuffTable *) a;
  52. HuffTable b_val = *(HuffTable *) b;
  53. return a_val.length - b_val.length;
  54. }
  55. /**
  56. * Computes the length of the Huffman encoding for each distinct input value.
  57. * Uses package merge algorithm as follows:
  58. * 1. start with an empty list, lets call it list(0), set i = 0
  59. * 2. add 1 entry to list(i) for each symbol we have and give each a score equal to the probability of the respective symbol
  60. * 3. merge the 2 symbols of least score and put them in list(i+1), and remove them from list(i). The new score will be the sum of the 2 scores
  61. * 4. if there is more than 1 symbol left in the current list(i), then goto 3
  62. * 5. i++
  63. * 6. if i < 16 goto 2
  64. * 7. select the n-1 elements in the last list with the lowest score (n = the number of symbols)
  65. * 8. the length of the huffman code for symbol s will be equal to the number of times the symbol occurs in the select elements
  66. * Go to guru.multimedia.cx/small-tasks-for-ffmpeg/ for more details
  67. *
  68. * All probabilities should be positive integers. The output is sorted by code,
  69. * not by length.
  70. *
  71. * @param prob_table input array of a PTable for each distinct input value
  72. * @param distincts output array of a HuffTable that will be populated by this function
  73. * @param size size of the prob_table array
  74. * @param max_length max length of an encoding
  75. */
  76. void ff_mjpegenc_huffman_compute_bits(PTable *prob_table, HuffTable *distincts, int size, int max_length)
  77. {
  78. PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
  79. int times, i, j, k;
  80. int nbits[257] = {0};
  81. int min;
  82. av_assert0(max_length > 0);
  83. to->nitems = 0;
  84. from->nitems = 0;
  85. to->item_idx[0] = 0;
  86. from->item_idx[0] = 0;
  87. AV_QSORT(prob_table, size, PTable, compare_by_prob);
  88. for (times = 0; times <= max_length; times++) {
  89. to->nitems = 0;
  90. to->item_idx[0] = 0;
  91. j = 0;
  92. k = 0;
  93. if (times < max_length) {
  94. i = 0;
  95. }
  96. while (i < size || j + 1 < from->nitems) {
  97. to->nitems++;
  98. to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
  99. if (i < size &&
  100. (j + 1 >= from->nitems ||
  101. prob_table[i].prob <
  102. from->probability[j] + from->probability[j + 1])) {
  103. to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
  104. to->probability[to->nitems - 1] = prob_table[i].prob;
  105. i++;
  106. } else {
  107. for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
  108. to->items[to->item_idx[to->nitems]++] = from->items[k];
  109. }
  110. to->probability[to->nitems - 1] =
  111. from->probability[j] + from->probability[j + 1];
  112. j += 2;
  113. }
  114. }
  115. temp = to;
  116. to = from;
  117. from = temp;
  118. }
  119. min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
  120. for (i = 0; i < from->item_idx[min]; i++) {
  121. nbits[from->items[i]]++;
  122. }
  123. // we don't want to return the 256 bit count (it was just in here to prevent
  124. // all 1s encoding)
  125. j = 0;
  126. for (i = 0; i < 256; i++) {
  127. if (nbits[i] > 0) {
  128. distincts[j].code = i;
  129. distincts[j].length = nbits[i];
  130. j++;
  131. }
  132. }
  133. }
  134. void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s)
  135. {
  136. memset(s->val_count, 0, sizeof(s->val_count));
  137. }
  138. /**
  139. * Produces a Huffman encoding with a given input
  140. *
  141. * @param s input to encode
  142. * @param bits output array where the ith character represents how many input values have i length encoding
  143. * @param val output array of input values sorted by their encoded length
  144. * @param max_nval maximum number of distinct input values
  145. */
  146. void ff_mjpeg_encode_huffman_close(MJpegEncHuffmanContext *s, uint8_t bits[17],
  147. uint8_t val[], int max_nval)
  148. {
  149. int i, j;
  150. int nval = 0;
  151. PTable val_counts[257];
  152. HuffTable distincts[256];
  153. for (i = 0; i < 256; i++) {
  154. if (s->val_count[i]) nval++;
  155. }
  156. av_assert0 (nval <= max_nval);
  157. j = 0;
  158. for (i = 0; i < 256; i++) {
  159. if (s->val_count[i]) {
  160. val_counts[j].value = i;
  161. val_counts[j].prob = s->val_count[i];
  162. j++;
  163. }
  164. }
  165. val_counts[j].value = 256;
  166. val_counts[j].prob = 0;
  167. ff_mjpegenc_huffman_compute_bits(val_counts, distincts, nval + 1, 16);
  168. AV_QSORT(distincts, nval, HuffTable, compare_by_length);
  169. memset(bits, 0, sizeof(bits[0]) * 17);
  170. for (i = 0; i < nval; i++) {
  171. val[i] = distincts[i].code;
  172. bits[distincts[i].length]++;
  173. }
  174. }