FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mjpegenc_huffman.c
Go to the documentation of this file.
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 occurences 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
45 
46 /**
47  * Used to store optimal huffman encoding results
48  */
49 typedef struct HuffTable {
50  int code; ///< code is the input value
51  int length; ///< length of the encoding
52 } HuffTable;
53 
54 /**
55  * Comparison function for two PTables by prob
56  *
57  * @param a First PTable to compare
58  * @param b Second PTable to compare
59  * @return < 0 for less than, 0 for equals, > 0 for greater than
60  */
61 static int compare_by_prob(const void *a, const void *b)
62 {
63  PTable a_val = *(PTable *) a;
64  PTable b_val = *(PTable *) b;
65  return a_val.prob - b_val.prob;
66 }
67 
68 /**
69  * Comparison function for two HuffTables by length
70  *
71  * @param a First HuffTable to compare
72  * @param b Second HuffTable to compare
73  * @return < 0 for less than, 0 for equals, > 0 for greater than
74  */
75 static int compare_by_length(const void *a, const void *b)
76 {
77  HuffTable a_val = *(HuffTable *) a;
78  HuffTable b_val = *(HuffTable *) b;
79  return a_val.length - b_val.length;
80 }
81 
82 /**
83  * Computes the length of the Huffman encoding for each distinct input value.
84  * Uses package merge algorithm as follows:
85  * 1. start with an empty list, lets call it list(0), set i = 0
86  * 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
87  * 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
88  * 4. if there is more than 1 symbol left in the current list(i), then goto 3
89  * 5. i++
90  * 6. if i < 16 goto 2
91  * 7. select the n-1 elements in the last list with the lowest score (n = the number of symbols)
92  * 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
93  * Go to guru.multimedia.cx/small-tasks-for-ffmpeg/ for more details
94  *
95  * All probabilities should be positive integers. The output is sorted by code,
96  * not by length.
97  *
98  * @param prob_table input array of a PTable for each distinct input value
99  * @param distincts output array of a HuffTable that will be populated by this function
100  * @param size size of the prob_table array
101  * @param max_length max length of an encoding
102  */
103 static void mjpegenc_huffman_compute_bits(PTable *prob_table, HuffTable *distincts,
104  int size, int max_length)
105 {
106  PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
107 
108  int times, i, j, k;
109 
110  int nbits[257] = {0};
111 
112  int min;
113 
114  av_assert0(max_length > 0);
115 
116  to->nitems = 0;
117  from->nitems = 0;
118  to->item_idx[0] = 0;
119  from->item_idx[0] = 0;
120  AV_QSORT(prob_table, size, PTable, compare_by_prob);
121 
122  for (times = 0; times <= max_length; times++) {
123  to->nitems = 0;
124  to->item_idx[0] = 0;
125 
126  j = 0;
127  k = 0;
128 
129  if (times < max_length) {
130  i = 0;
131  }
132  while (i < size || j + 1 < from->nitems) {
133  to->nitems++;
134  to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
135  if (i < size &&
136  (j + 1 >= from->nitems ||
137  prob_table[i].prob <
138  from->probability[j] + from->probability[j + 1])) {
139  to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
140  to->probability[to->nitems - 1] = prob_table[i].prob;
141  i++;
142  } else {
143  for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
144  to->items[to->item_idx[to->nitems]++] = from->items[k];
145  }
146  to->probability[to->nitems - 1] =
147  from->probability[j] + from->probability[j + 1];
148  j += 2;
149  }
150  }
151  temp = to;
152  to = from;
153  from = temp;
154  }
155 
156  min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
157  for (i = 0; i < from->item_idx[min]; i++) {
158  nbits[from->items[i]]++;
159  }
160  // we don't want to return the 256 bit count (it was just in here to prevent
161  // all 1s encoding)
162  j = 0;
163  for (i = 0; i < 256; i++) {
164  if (nbits[i] > 0) {
165  distincts[j].code = i;
166  distincts[j].length = nbits[i];
167  j++;
168  }
169  }
170 }
171 
173 {
174  memset(s->val_count, 0, sizeof(s->val_count));
175 }
176 
177 /**
178  * Produces a Huffman encoding with a given input
179  *
180  * @param s input to encode
181  * @param bits output array where the ith character represents how many input values have i length encoding
182  * @param val output array of input values sorted by their encoded length
183  * @param max_nval maximum number of distinct input values
184  */
186  uint8_t val[], int max_nval)
187 {
188  PTable val_counts[257];
189  HuffTable distincts[256];
190 
191  av_assert1(max_nval <= FF_ARRAY_ELEMS(val_counts) - 1);
192 
193  int nval = 0;
194  for (int i = 0; i < 256; i++) {
195  if (s->val_count[i]) {
196  val_counts[nval].value = i;
197  val_counts[nval].prob = s->val_count[i];
198  nval++;
199  av_assert2(nval <= max_nval);
200  }
201  }
202  val_counts[nval].value = 256;
203  val_counts[nval].prob = 0;
204  mjpegenc_huffman_compute_bits(val_counts, distincts, nval + 1, 16);
205  AV_QSORT(distincts, nval, HuffTable, compare_by_length);
206 
207  memset(bits, 0, sizeof(bits[0]) * 17);
208  for (int i = 0; i < nval; i++) {
209  val[i] = distincts[i].code;
210  bits[distincts[i].length]++;
211  }
212 }
ff_mjpeg_encode_huffman_close
void ff_mjpeg_encode_huffman_close(MJpegEncHuffmanContext *s, uint8_t bits[17], uint8_t val[], int max_nval)
Produces a Huffman encoding with a given input.
Definition: mjpegenc_huffman.c:185
mjpegenc_huffman_compute_bits
static void mjpegenc_huffman_compute_bits(PTable *prob_table, HuffTable *distincts, int size, int max_length)
Computes the length of the Huffman encoding for each distinct input value.
Definition: mjpegenc_huffman.c:103
b
#define b
Definition: input.c:42
PackageMergerList::item_idx
int item_idx[515]
index range for each item in items 0, 2, 5, 9, 13
Definition: magicyuvenc.c:301
ff_mjpeg_encode_huffman_init
void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s)
Definition: mjpegenc_huffman.c:172
compare_by_prob
static int compare_by_prob(const void *a, const void *b)
Comparison function for two PTables by prob.
Definition: mjpegenc_huffman.c:61
PTable::prob
int prob
number of occurences of this value in input
Definition: mjpegenc_huffman.c:33
val
static double val(void *priv, double ch)
Definition: aeval.c:77
avassert.h
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
s
#define s(width, name)
Definition: cbs_vp9.c:198
compare_by_length
static int compare_by_length(const void *a, const void *b)
Comparison function for two HuffTables by length.
Definition: mjpegenc_huffman.c:75
PackageMergerList::nitems
int nitems
number of items in the list and probability ex. 4
Definition: magicyuvenc.c:300
bits
uint8_t bits
Definition: vp3data.h:128
from
const char * from
Definition: jacosubdec.c:66
to
const char * to
Definition: webvttdec.c:35
MJpegEncHuffmanContext
Definition: mjpegenc_huffman.h:32
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
PTable::prob
int64_t prob
number of occurences of this value in input
Definition: magicyuvenc.c:53
PTable
Used to assign a occurrence count or "probability" to an input value.
Definition: magicyuvenc.c:51
HuffTable
Used to store optimal huffman encoding results.
Definition: mjpegenc_huffman.c:49
HuffTable::code
int code
code is the input value
Definition: mjpegenc_huffman.c:50
qsort.h
PackageMergerList
Used to store intermediate lists in the package merge algorithm.
Definition: magicyuvenc.c:299
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
HuffTable::length
int length
length of the encoding
Definition: mjpegenc_huffman.c:51
size
int size
Definition: twinvq_data.h:10344
mjpegenc_huffman.h
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
PTable::value
int value
input value
Definition: magicyuvenc.c:52
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AV_QSORT
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
Definition: qsort.h:33
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
PackageMergerList::items
int items[257 *16]
chain of all individual values that make up items A, B, A, B, C, A, B, C, D, C, D,...
Definition: magicyuvenc.c:303
PackageMergerList::probability
int probability[514]
probability of each item 3, 8, 18, 46
Definition: magicyuvenc.c:302
temp
else temp
Definition: vf_mcdeint.c:263
min
float min
Definition: vorbis_enc_data.h:429