FFmpeg
Loading...
Searching...
No Matches
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 */
31typedef 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 */
39typedef 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 * 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 */
53static int compare_by_prob(const void *a, const void *b)
54{
55 PTable a_val = *(PTable *) a;
56 PTable b_val = *(PTable *) b;
57 return a_val.prob - b_val.prob;
58}
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 */
81static void mjpegenc_huffman_compute_bits(PTable *prob_table,
82 uint8_t counts[/* max_length + 1 */],
83 int size, int max_length)
84{
85 PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
86
87 int times, i, j, k;
88
89 int nbits[257] = {0};
90
91 int min;
92
93 av_assert0(max_length > 0);
94
95 to->nitems = 0;
96 from->nitems = 0;
97 to->item_idx[0] = 0;
98 from->item_idx[0] = 0;
99 AV_QSORT(prob_table, size, PTable, compare_by_prob);
100
101 for (times = 0; times <= max_length; times++) {
102 to->nitems = 0;
103 to->item_idx[0] = 0;
104
105 j = 0;
106 k = 0;
107
108 if (times < max_length) {
109 i = 0;
110 }
111 while (i < size || j + 1 < from->nitems) {
112 to->nitems++;
113 to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
114 if (i < size &&
115 (j + 1 >= from->nitems ||
116 prob_table[i].prob <
117 from->probability[j] + from->probability[j + 1])) {
118 to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
119 to->probability[to->nitems - 1] = prob_table[i].prob;
120 i++;
121 } else {
122 for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
123 to->items[to->item_idx[to->nitems]++] = from->items[k];
124 }
125 to->probability[to->nitems - 1] =
126 from->probability[j] + from->probability[j + 1];
127 j += 2;
128 }
129 }
130 temp = to;
131 to = from;
132 from = temp;
133 }
134
135 min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
136 for (i = 0; i < from->item_idx[min]; i++) {
137 nbits[from->items[i]]++;
138 }
139 // we don't want to return the 256 bit count (it was just in here to prevent
140 // all 1s encoding)
141 memset(counts, 0, sizeof(counts[0]) * (max_length + 1));
142 for (int i = 0; i < 256; ++i)
143 counts[nbits[i]]++;
144}
145
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 */
160 uint8_t val[], int max_nval)
161{
162 PTable val_counts[257];
163
164 av_assert1(max_nval <= FF_ARRAY_ELEMS(val_counts) - 1);
165
166 int nval = 0;
167 for (int i = 0; i < 256; i++) {
168 if (s->val_count[i]) {
169 val_counts[nval].value = i;
170 val_counts[nval].prob = s->val_count[i];
171 nval++;
172 av_assert2(nval <= max_nval);
173 }
174 }
175 val_counts[nval].value = 256;
176 val_counts[nval].prob = 0;
177
178 mjpegenc_huffman_compute_bits(val_counts, bits, nval + 1, 16);
179
180 // val_counts[0] is the fake element we added earlier.
181 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 for (int i = 0; i < nval; ++i)
185 val[i] = val_counts[nval - i].value;
186}
static double val(void *priv, double ch)
Definition aeval.c:77
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define prob(name, subs,...)
Definition cbs_vp9.c:252
#define min(a, b)
double value
Definition eval.c:102
static const uint8_t bits[8]
Definition fastaudio.c:100
int a
#define b
Definition input.c:43
const char * from
Definition jacosubdec.c:64
const char * to
Definition webvttdec.c:36
static int compare_by_prob(const void *a, const void *b)
static int compare_by_prob(const void *a, const void *b)
Comparison function for two PTables by prob.
void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s)
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.
static void mjpegenc_huffman_compute_bits(PTable *prob_table, uint8_t counts[], int size, int max_length)
Computes the length of the Huffman encoding for each distinct input value.
Huffman table generation for MJPEG encoder.
#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
#define FF_ARRAY_ELEMS(a)
Used to assign a occurrence count or "probability" to an input value.
Definition magicyuvenc.c:51
int64_t prob
number of occurrences of this value in input
Definition magicyuvenc.c:53
int value
input value
Definition magicyuvenc.c:52
Used to store intermediate lists in the package merge algorithm.
int probability[514]
probability of each item 3, 8, 18, 46
int items[257 *16]
chain of all individual values that make up items A, B, A, B, C, A, B, C, D, C, D,...
int item_idx[515]
index range for each item in items 0, 2, 5, 9, 13
int nitems
number of items in the list and probability ex. 4
int size
else temp
Definition vf_mcdeint.c:275