/src/ffmpeg/libavcodec/mjpegenc_huffman.c
Line | Count | Source |
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 | | |
22 | | #include <string.h> |
23 | | #include <stdint.h> |
24 | | #include "libavutil/avassert.h" |
25 | | #include "libavutil/qsort.h" |
26 | | #include "mjpegenc_huffman.h" |
27 | | |
28 | | /** |
29 | | * Used to assign a occurrence count or "probability" to an input value |
30 | | */ |
31 | | typedef struct PTable { |
32 | | int value; ///< input value |
33 | | int prob; ///< number of occurrences of this value in input |
34 | | } PTable; |
35 | | |
36 | | /** |
37 | | * Used to store intermediate lists in the package merge algorithm |
38 | | */ |
39 | | typedef struct PackageMergerList { |
40 | | int nitems; ///< number of items in the list and probability ex. 4 |
41 | | int item_idx[515]; ///< index range for each item in items 0, 2, 5, 9, 13 |
42 | | int probability[514]; ///< probability of each item 3, 8, 18, 46 |
43 | | int items[257 * 16]; ///< chain of all individual values that make up items A, B, A, B, C, A, B, C, D, C, D, D, E |
44 | | } PackageMergerList; |
45 | | |
46 | | /** |
47 | | * Comparison function for two PTables by prob |
48 | | * |
49 | | * @param a First PTable to compare |
50 | | * @param b Second PTable to compare |
51 | | * @return < 0 for less than, 0 for equals, > 0 for greater than |
52 | | */ |
53 | | static int compare_by_prob(const void *a, const void *b) |
54 | 130k | { |
55 | 130k | PTable a_val = *(PTable *) a; |
56 | 130k | PTable b_val = *(PTable *) b; |
57 | 130k | return a_val.prob - b_val.prob; |
58 | 130k | } |
59 | | |
60 | | /** |
61 | | * Computes the length of the Huffman encoding for each distinct input value. |
62 | | * Uses package merge algorithm as follows: |
63 | | * 1. start with an empty list, lets call it list(0), set i = 0 |
64 | | * 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 |
65 | | * 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 |
66 | | * 4. if there is more than 1 symbol left in the current list(i), then goto 3 |
67 | | * 5. i++ |
68 | | * 6. if i < 16 goto 2 |
69 | | * 7. select the n-1 elements in the last list with the lowest score (n = the number of symbols) |
70 | | * 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 |
71 | | * Go to guru.multimedia.cx/small-tasks-for-ffmpeg/ for more details |
72 | | * |
73 | | * All probabilities should be nonnegative integers. |
74 | | * |
75 | | * @param prob_table[in,out] array of a PTable for each distinct input value, |
76 | | * will be sorted according to ascending probability |
77 | | * @param counts[out] the number of values of a given length |
78 | | * @param size number of elements of the prob_table array |
79 | | * @param max_length max length of a code |
80 | | */ |
81 | | static void mjpegenc_huffman_compute_bits(PTable *prob_table, |
82 | | uint8_t counts[/* max_length + 1 */], |
83 | | int size, int max_length) |
84 | 2.20k | { |
85 | 2.20k | PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp; |
86 | | |
87 | 2.20k | int times, i, j, k; |
88 | | |
89 | 2.20k | int nbits[257] = {0}; |
90 | | |
91 | 2.20k | int min; |
92 | | |
93 | 2.20k | av_assert0(max_length > 0); |
94 | | |
95 | 2.20k | to->nitems = 0; |
96 | 2.20k | from->nitems = 0; |
97 | 2.20k | to->item_idx[0] = 0; |
98 | 2.20k | from->item_idx[0] = 0; |
99 | 2.20k | AV_QSORT(prob_table, size, PTable, compare_by_prob); |
100 | | |
101 | 39.6k | for (times = 0; times <= max_length; times++) { |
102 | 37.4k | to->nitems = 0; |
103 | 37.4k | to->item_idx[0] = 0; |
104 | | |
105 | 37.4k | j = 0; |
106 | 37.4k | k = 0; |
107 | | |
108 | 37.4k | if (times < max_length) { |
109 | 35.2k | i = 0; |
110 | 35.2k | } |
111 | 1.02M | while (i < size || j + 1 < from->nitems) { |
112 | 992k | to->nitems++; |
113 | 992k | to->item_idx[to->nitems] = to->item_idx[to->nitems - 1]; |
114 | 992k | if (i < size && |
115 | 905k | (j + 1 >= from->nitems || |
116 | 864k | prob_table[i].prob < |
117 | 864k | from->probability[j] + from->probability[j + 1])) { |
118 | 527k | to->items[to->item_idx[to->nitems]++] = prob_table[i].value; |
119 | 527k | to->probability[to->nitems - 1] = prob_table[i].prob; |
120 | 527k | i++; |
121 | 527k | } else { |
122 | 3.54M | for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) { |
123 | 3.08M | to->items[to->item_idx[to->nitems]++] = from->items[k]; |
124 | 3.08M | } |
125 | 465k | to->probability[to->nitems - 1] = |
126 | 465k | from->probability[j] + from->probability[j + 1]; |
127 | 465k | j += 2; |
128 | 465k | } |
129 | 992k | } |
130 | 37.4k | temp = to; |
131 | 37.4k | to = from; |
132 | 37.4k | from = temp; |
133 | 37.4k | } |
134 | | |
135 | 2.20k | min = (size - 1 < from->nitems) ? size - 1 : from->nitems; |
136 | 280k | for (i = 0; i < from->item_idx[min]; i++) { |
137 | 278k | nbits[from->items[i]]++; |
138 | 278k | } |
139 | | // we don't want to return the 256 bit count (it was just in here to prevent |
140 | | // all 1s encoding) |
141 | 2.20k | memset(counts, 0, sizeof(counts[0]) * (max_length + 1)); |
142 | 566k | for (int i = 0; i < 256; ++i) |
143 | 564k | counts[nbits[i]]++; |
144 | 2.20k | } |
145 | | |
146 | | void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s) |
147 | 2.20k | { |
148 | 2.20k | memset(s->val_count, 0, sizeof(s->val_count)); |
149 | 2.20k | } |
150 | | |
151 | | /** |
152 | | * Produces a Huffman encoding with a given input |
153 | | * |
154 | | * @param s input to encode |
155 | | * @param bits output array where the ith character represents how many input values have i length encoding |
156 | | * @param val output array of input values sorted by their encoded length |
157 | | * @param max_nval maximum number of distinct input values |
158 | | */ |
159 | | void ff_mjpeg_encode_huffman_close(MJpegEncHuffmanContext *s, uint8_t bits[17], |
160 | | uint8_t val[], int max_nval) |
161 | 2.20k | { |
162 | 2.20k | PTable val_counts[257]; |
163 | | |
164 | 2.20k | av_assert1(max_nval <= FF_ARRAY_ELEMS(val_counts) - 1); |
165 | | |
166 | 2.20k | int nval = 0; |
167 | 566k | for (int i = 0; i < 256; i++) { |
168 | 564k | if (s->val_count[i]) { |
169 | 30.7k | val_counts[nval].value = i; |
170 | 30.7k | val_counts[nval].prob = s->val_count[i]; |
171 | 30.7k | nval++; |
172 | 30.7k | av_assert2(nval <= max_nval); |
173 | 30.7k | } |
174 | 564k | } |
175 | 2.20k | val_counts[nval].value = 256; |
176 | 2.20k | val_counts[nval].prob = 0; |
177 | | |
178 | 2.20k | mjpegenc_huffman_compute_bits(val_counts, bits, nval + 1, 16); |
179 | | |
180 | | // val_counts[0] is the fake element we added earlier. |
181 | 2.20k | av_assert1(val_counts[0].prob == 0 && val_counts[0].value == 256); |
182 | | // The following loop puts the values with higher occurrence first, |
183 | | // ensuring that they get the shorter codes. |
184 | 32.9k | for (int i = 0; i < nval; ++i) |
185 | 30.7k | val[i] = val_counts[nval - i].value; |
186 | 2.20k | } |