]> git.sesse.net Git - ffmpeg/blob - libavcodec/mjpegenc_huffman.c
Merge commit '4fb311c804098d78e5ce5f527f9a9c37536d3a08'
[ffmpeg] / libavcodec / mjpegenc_huffman.c
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 <stdlib.h>
25 #include "libavutil/avassert.h"
26 #include "libavutil/common.h"
27 #include "libavutil/error.h"
28 #include "libavutil/qsort.h"
29 #include "mjpegenc_huffman.h"
30
31 /**
32  * Comparison function for two PTables by prob
33  *
34  * @param a First PTable to compare
35  * @param b Second PTable to compare
36  * @return < 0 for less than, 0 for equals, > 0 for greater than
37  */
38 static int compare_by_prob(const void *a, const void *b)
39 {
40     PTable a_val = *(PTable *) a;
41     PTable b_val = *(PTable *) b;
42     return a_val.prob - b_val.prob;
43 }
44
45 /**
46  * Comparison function for two HuffTables by length
47  *
48  * @param a First HuffTable to compare
49  * @param b Second HuffTable to compare
50  * @return < 0 for less than, 0 for equals, > 0 for greater than
51  */
52 static int compare_by_length(const void *a, const void *b)
53 {
54     HuffTable a_val = *(HuffTable *) a;
55     HuffTable b_val = *(HuffTable *) b;
56     return a_val.length - b_val.length;
57 }
58
59 /**
60  * Computes the length of the Huffman encoding for each distinct input value.
61  * Uses package merge algorithm as follows:
62  * 1. start with an empty list, lets call it list(0), set i = 0
63  * 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
64  * 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
65  * 4. if there is more than 1 symbol left in the current list(i), then goto 3
66  * 5. i++
67  * 6. if i < 16 goto 2
68  * 7. select the n-1 elements in the last list with the lowest score (n = the number of symbols)
69  * 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
70  * Go to guru.multimedia.cx/small-tasks-for-ffmpeg/ for more details
71  *
72  * All probabilities should be positive integers. The output is sorted by code,
73  * not by length.
74  *
75  * @param prob_table input array of a PTable for each distinct input value
76  * @param distincts  output array of a HuffTable that will be populated by this function
77  * @param size       size of the prob_table array
78  * @param max_length max length of an encoding
79  */
80 void ff_mjpegenc_huffman_compute_bits(PTable *prob_table, HuffTable *distincts, int size, int max_length)
81 {
82     PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
83
84     int times, i, j, k;
85
86     int nbits[257] = {0};
87
88     int min;
89
90     to->nitems = 0;
91     from->nitems = 0;
92     to->item_idx[0] = 0;
93     from->item_idx[0] = 0;
94     AV_QSORT(prob_table, size, PTable, compare_by_prob);
95
96     for (times = 0; times <= max_length; times++) {
97         to->nitems = 0;
98         to->item_idx[0] = 0;
99
100         j = 0;
101         k = 0;
102
103         if (times < max_length) {
104             i = 0;
105         }
106         while (i < size || j + 1 < from->nitems) {
107             to->nitems++;
108             to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
109             if (i < size &&
110                 (j + 1 >= from->nitems ||
111                  prob_table[i].prob <
112                      from->probability[j] + from->probability[j + 1])) {
113                 to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
114                 to->probability[to->nitems - 1] = prob_table[i].prob;
115                 i++;
116             } else {
117                 for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
118                     to->items[to->item_idx[to->nitems]++] = from->items[k];
119                 }
120                 to->probability[to->nitems - 1] =
121                     from->probability[j] + from->probability[j + 1];
122                 j += 2;
123             }
124         }
125         temp = to;
126         to = from;
127         from = temp;
128     }
129
130     min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
131     for (i = 0; i < from->item_idx[min]; i++) {
132         nbits[from->items[i]]++;
133     }
134     // we don't want to return the 256 bit count (it was just in here to prevent
135     // all 1s encoding)
136     j = 0;
137     for (i = 0; i < 256; i++) {
138         if (nbits[i] > 0) {
139             distincts[j].code = i;
140             distincts[j].length = nbits[i];
141             j++;
142         }
143     }
144 }
145
146 void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s)
147 {
148     memset(s->val_count, 0, sizeof(s->val_count));
149 }
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 {
162     int i, j;
163     int nval = 0;
164     PTable val_counts[257];
165     HuffTable distincts[256];
166
167     for (i = 0; i < 256; i++) {
168         if (s->val_count[i]) nval++;
169     }
170     av_assert0 (nval <= max_nval);
171
172     j = 0;
173     for (i = 0; i < 256; i++) {
174         if (s->val_count[i]) {
175             val_counts[j].value = i;
176             val_counts[j].prob = s->val_count[i];
177             j++;
178         }
179     }
180     val_counts[j].value = 256;
181     val_counts[j].prob = 0;
182     ff_mjpegenc_huffman_compute_bits(val_counts, distincts, nval + 1, 16);
183     AV_QSORT(distincts, nval, HuffTable, compare_by_length);
184
185     memset(bits, 0, sizeof(bits[0]) * 17);
186     for (i = 0; i < nval; i++) {
187         val[i] = distincts[i].code;
188         bits[distincts[i].length]++;
189     }
190 }