Browse code

Implement optimal huffman encoding for (M)JPEG.

> seems to break
> make fate-vsynth1-mjpeg-444

Fixed.

Jerry Jiang authored on 2017/02/02 16:23:04
Showing 23 changed files
... ...
@@ -21,6 +21,7 @@ version <next>:
21 21
 - Scenarist Closed Captions demuxer and muxer
22 22
 - threshold filter
23 23
 - midequalizer filter
24
+- Optimal Huffman tables for (M)JPEG encoding
24 25
 
25 26
 version 3.2:
26 27
 - libopenmpt demuxer
... ...
@@ -1200,6 +1200,27 @@ Same as @samp{3}, but with extra processing enabled.
1200 1200
 @end table
1201 1201
 @end table
1202 1202
 
1203
+ at anchor{mjpegenc}
1204
+ at section mjpeg
1205
+
1206
+Motion JPEG encoder.
1207
+
1208
+ at subsection Options
1209
+
1210
+ at table @option
1211
+ at item huffman
1212
+Set the huffman encoding strategy. Possible values:
1213
+
1214
+ at table @samp
1215
+ at item default
1216
+Use the default huffman tables. This is the default strategy.
1217
+
1218
+ at item optimal
1219
+Compute and use optimal huffman tables.
1220
+
1221
+ at end table
1222
+ at end table
1223
+
1203 1224
 @anchor{wavpackenc}
1204 1225
 @section wavpack
1205 1226
 
... ...
@@ -39,6 +39,7 @@ OBJS = allcodecs.o                                                      \
39 39
        mediacodec.o                                                     \
40 40
        mpeg12framerate.o                                                \
41 41
        options.o                                                        \
42
+       mjpegenc_huffman.o                                               \
42 43
        parser.o                                                         \
43 44
        profiles.o                                                       \
44 45
        qsv_api.o                                                        \
... ...
@@ -175,7 +176,8 @@ OBJS-$(CONFIG_AMRWB_DECODER)           += amrwbdec.o celp_filters.o   \
175 175
                                           celp_math.o acelp_filters.o \
176 176
                                           acelp_vectors.o             \
177 177
                                           acelp_pitch_delay.o
178
-OBJS-$(CONFIG_AMV_ENCODER)             += mjpegenc.o mjpegenc_common.o
178
+OBJS-$(CONFIG_AMV_ENCODER)             += mjpegenc.o mjpegenc_common.o \
179
+                                          mjpegenc_huffman.o
179 180
 OBJS-$(CONFIG_ANM_DECODER)             += anm.o
180 181
 OBJS-$(CONFIG_ANSI_DECODER)            += ansi.o cga_data.o
181 182
 OBJS-$(CONFIG_APE_DECODER)             += apedec.o
... ...
@@ -377,7 +379,8 @@ OBJS-$(CONFIG_METASOUND_DECODER)       += metasound.o metasound_data.o \
377 377
 OBJS-$(CONFIG_MICRODVD_DECODER)        += microdvddec.o ass.o
378 378
 OBJS-$(CONFIG_MIMIC_DECODER)           += mimic.o
379 379
 OBJS-$(CONFIG_MJPEG_DECODER)           += mjpegdec.o
380
-OBJS-$(CONFIG_MJPEG_ENCODER)           += mjpegenc.o mjpegenc_common.o
380
+OBJS-$(CONFIG_MJPEG_ENCODER)           += mjpegenc.o mjpegenc_common.o \
381
+                                          mjpegenc_huffman.o
381 382
 OBJS-$(CONFIG_MJPEGB_DECODER)          += mjpegbdec.o
382 383
 OBJS-$(CONFIG_MJPEG_VAAPI_ENCODER)     += vaapi_encode_mjpeg.o
383 384
 OBJS-$(CONFIG_MLP_DECODER)             += mlpdec.o mlpdsp.o
... ...
@@ -1014,6 +1017,7 @@ TESTPROGS = avpacket                                                    \
1014 1014
             jpeg2000dwt                                                 \
1015 1015
             mathops                                                    \
1016 1016
             options                                                     \
1017
+            mjpegenc_huffman                                            \
1017 1018
             utils                                                       \
1018 1019
 
1019 1020
 TESTPROGS-$(CONFIG_CABAC)                 += cabac
... ...
@@ -39,39 +39,15 @@
39 39
 #include "mjpeg.h"
40 40
 #include "mjpegenc.h"
41 41
 
42
-static uint8_t uni_ac_vlc_len[64 * 64 * 2];
43
-static uint8_t uni_chroma_ac_vlc_len[64 * 64 * 2];
44
-
45
-static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
46
-{
47
-    int i;
48
-
49
-    for (i = 0; i < 128; i++) {
50
-        int level = i - 64;
51
-        int run;
52
-        if (!level)
53
-            continue;
54
-        for (run = 0; run < 64; run++) {
55
-            int len, code, nbits;
56
-            int alevel = FFABS(level);
57
-
58
-            len = (run >> 4) * huff_size_ac[0xf0];
59
-
60
-            nbits= av_log2_16bit(alevel) + 1;
61
-            code = ((15&run) << 4) | nbits;
62
-
63
-            len += huff_size_ac[code] + nbits;
64
-
65
-            uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
66
-            // We ignore EOB as its just a constant which does not change generally
67
-        }
68
-    }
69
-}
42
+// Don't know, but let's guess 16 bits per code
43
+#define MJPEG_HUFFMAN_EST_BITS_PER_CODE 16
70 44
 
71 45
 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
72 46
 {
73 47
     MJpegContext *m;
74 48
 
49
+    av_assert0(s->slice_context_count == 1);
50
+
75 51
     if (s->width > 65500 || s->height > 65500) {
76 52
         av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
77 53
         return AVERROR(EINVAL);
... ...
@@ -84,7 +60,9 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
84 84
     s->min_qcoeff=-1023;
85 85
     s->max_qcoeff= 1023;
86 86
 
87
-    /* build all the huffman tables */
87
+    // Build default Huffman tables.
88
+    // These may be overwritten later with more optimal Huffman tables, but
89
+    // they are needed at least right now for some processes like trellis.
88 90
     ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
89 91
                                  m->huff_code_dc_luminance,
90 92
                                  avpriv_mjpeg_bits_dc_luminance,
... ...
@@ -102,12 +80,18 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
102 102
                                  avpriv_mjpeg_bits_ac_chrominance,
103 103
                                  avpriv_mjpeg_val_ac_chrominance);
104 104
 
105
-    init_uni_ac_vlc(m->huff_size_ac_luminance,   uni_ac_vlc_len);
106
-    init_uni_ac_vlc(m->huff_size_ac_chrominance, uni_chroma_ac_vlc_len);
105
+    init_uni_ac_vlc(m->huff_size_ac_luminance,   m->uni_ac_vlc_len);
106
+    init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
107 107
     s->intra_ac_vlc_length      =
108
-    s->intra_ac_vlc_last_length = uni_ac_vlc_len;
108
+    s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
109 109
     s->intra_chroma_ac_vlc_length      =
110
-    s->intra_chroma_ac_vlc_last_length = uni_chroma_ac_vlc_len;
110
+    s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
111
+
112
+    // Buffers start out empty.
113
+    m->huff_buffer = NULL;
114
+    m->huff_ncode = 0;
115
+    m->huff_capacity = 0;
116
+    m->error = 0;
111 117
 
112 118
     s->mjpeg_ctx = m;
113 119
     return 0;
... ...
@@ -115,71 +99,193 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
115 115
 
116 116
 av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
117 117
 {
118
+    av_freep(&s->mjpeg_ctx->huff_buffer);
118 119
     av_freep(&s->mjpeg_ctx);
119 120
 }
120 121
 
122
+/**
123
+ * Encodes and outputs the entire frame in the JPEG format.
124
+ *
125
+ * @param s The MpegEncContext.
126
+ */
127
+void ff_mjpeg_encode_picture_frame(MpegEncContext *s)
128
+{
129
+    int i, nbits, code, table_id;
130
+    MJpegContext *m = s->mjpeg_ctx;
131
+    uint8_t *huff_size[4] = {m->huff_size_dc_luminance,
132
+                             m->huff_size_dc_chrominance,
133
+                             m->huff_size_ac_luminance,
134
+                             m->huff_size_ac_chrominance};
135
+    uint16_t *huff_code[4] = {m->huff_code_dc_luminance,
136
+                              m->huff_code_dc_chrominance,
137
+                              m->huff_code_ac_luminance,
138
+                              m->huff_code_ac_chrominance};
139
+    size_t total_bits = 0;
140
+    size_t bytes_needed;
141
+
142
+    // Estimate the total size first
143
+    for (i = 0; i < m->huff_ncode; i++) {
144
+        table_id = m->huff_buffer[i].table_id;
145
+        code = m->huff_buffer[i].code;
146
+        nbits = code & 0xf;
147
+
148
+        total_bits += huff_size[table_id][code] + nbits;
149
+    }
150
+
151
+    bytes_needed = (total_bits + 7) / 8;
152
+    ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
153
+
154
+    for (i = 0; i < m->huff_ncode; i++) {
155
+        table_id = m->huff_buffer[i].table_id;
156
+        code = m->huff_buffer[i].code;
157
+        nbits = code & 0xf;
158
+
159
+        put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
160
+        if (nbits != 0) {
161
+            put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
162
+        }
163
+    }
164
+
165
+    m->huff_ncode = 0;
166
+}
167
+
168
+/**
169
+ * Add code and table_id to the JPEG buffer.
170
+ *
171
+ * @param s The MJpegContext which contains the JPEG buffer.
172
+ * @param table_id Which Huffman table the code belongs to.
173
+ * @param code The encoded exponent of the coefficients and the run-bits.
174
+ */
175
+static inline void ff_mjpeg_encode_code(MJpegContext *s, uint8_t table_id, int code)
176
+{
177
+    MJpegHuffmanCode *c = &s->huff_buffer[s->huff_ncode++];
178
+    av_assert0(s->huff_ncode < s->huff_capacity);
179
+    c->table_id = table_id;
180
+    c->code = code;
181
+}
182
+
183
+/**
184
+ * Add the coefficient's data to the JPEG buffer.
185
+ *
186
+ * @param s The MJpegContext which contains the JPEG buffer.
187
+ * @param table_id Which Huffman table the code belongs to.
188
+ * @param val The coefficient.
189
+ * @param run The run-bits.
190
+ */
191
+static void ff_mjpeg_encode_coef(MJpegContext *s, uint8_t table_id, int val, int run)
192
+{
193
+    int mant, code;
194
+
195
+    if (val == 0) {
196
+        av_assert0(run == 0);
197
+        ff_mjpeg_encode_code(s, table_id, 0);
198
+    } else {
199
+        mant = val;
200
+        if (val < 0) {
201
+            val = -val;
202
+            mant--;
203
+        }
204
+
205
+        code = (run << 4) | (av_log2_16bit(val) + 1);
206
+
207
+        s->huff_buffer[s->huff_ncode].mant = mant;
208
+        ff_mjpeg_encode_code(s, table_id, code);
209
+    }
210
+}
211
+
212
+/**
213
+ * Add the block's data into the JPEG buffer.
214
+ *
215
+ * @param s The MJpegEncContext that contains the JPEG buffer.
216
+ * @param block The block.
217
+ * @param n The block's index or number.
218
+ */
121 219
 static void encode_block(MpegEncContext *s, int16_t *block, int n)
122 220
 {
123
-    int mant, nbits, code, i, j;
124
-    int component, dc, run, last_index, val;
221
+    int i, j, table_id;
222
+    int component, dc, last_index, val, run;
125 223
     MJpegContext *m = s->mjpeg_ctx;
126
-    uint8_t *huff_size_ac;
127
-    uint16_t *huff_code_ac;
224
+
225
+    if (m->error) return;
226
+
227
+    av_assert0(m->huff_capacity >= m->huff_ncode + 64);
128 228
 
129 229
     /* DC coef */
130 230
     component = (n <= 3 ? 0 : (n&1) + 1);
231
+    table_id = (n <= 3 ? 0 : 1);
131 232
     dc = block[0]; /* overflow is impossible */
132 233
     val = dc - s->last_dc[component];
133
-    if (n < 4) {
134
-        ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
135
-        huff_size_ac = m->huff_size_ac_luminance;
136
-        huff_code_ac = m->huff_code_ac_luminance;
137
-    } else {
138
-        ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
139
-        huff_size_ac = m->huff_size_ac_chrominance;
140
-        huff_code_ac = m->huff_code_ac_chrominance;
141
-    }
234
+
235
+    ff_mjpeg_encode_coef(m, table_id, val, 0);
236
+
142 237
     s->last_dc[component] = dc;
143 238
 
144 239
     /* AC coefs */
145 240
 
146 241
     run = 0;
147 242
     last_index = s->block_last_index[n];
243
+    table_id |= 2;
244
+
148 245
     for(i=1;i<=last_index;i++) {
149 246
         j = s->intra_scantable.permutated[i];
150 247
         val = block[j];
248
+
151 249
         if (val == 0) {
152 250
             run++;
153 251
         } else {
154 252
             while (run >= 16) {
155
-                put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
253
+                ff_mjpeg_encode_code(m, table_id, 0xf0);
156 254
                 run -= 16;
157 255
             }
158
-            mant = val;
159
-            if (val < 0) {
160
-                val = -val;
161
-                mant--;
162
-            }
163
-
164
-            nbits= av_log2_16bit(val) + 1;
165
-            code = (run << 4) | nbits;
166
-
167
-            put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
168
-
169
-            put_sbits(&s->pb, nbits, mant);
256
+            ff_mjpeg_encode_coef(m, table_id, val, run);
170 257
             run = 0;
171 258
         }
172 259
     }
173 260
 
174 261
     /* output EOB only if not already 64 values */
175 262
     if (last_index < 63 || run != 0)
176
-        put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
263
+        ff_mjpeg_encode_code(m, table_id, 0);
177 264
 }
178 265
 
179
-void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
266
+// Possibly reallocate the huffman code buffer, assuming blocks_per_mb.
267
+// Set s->mjpeg_ctx->error on ENOMEM.
268
+static void realloc_huffman(MpegEncContext *s, int blocks_per_mb)
180 269
 {
181
-    int i;
270
+    MJpegContext *m = s->mjpeg_ctx;
271
+    size_t num_mbs, num_blocks, num_codes;
272
+    MJpegHuffmanCode *new_buf;
273
+    if (m->error) return;
274
+    // Make sure we have enough space to hold this frame.
275
+    num_mbs = s->mb_width * s->mb_height;
276
+    num_blocks = num_mbs * blocks_per_mb;
277
+    av_assert0(m->huff_ncode <=
278
+               (s->mb_y * s->mb_width + s->mb_x) * blocks_per_mb * 64);
279
+    num_codes = num_blocks * 64;
280
+
281
+    new_buf = av_fast_realloc(m->huff_buffer, &m->huff_capacity,
282
+                              num_codes * sizeof(MJpegHuffmanCode));
283
+    if (!new_buf) {
284
+        m->error = AVERROR(ENOMEM);
285
+    } else {
286
+        m->huff_buffer = new_buf;
287
+    }
288
+}
289
+
290
+int ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
291
+{
292
+    int i, is_chroma_420;
293
+
294
+    // Number of bits used depends on future data.
295
+    // So, nothing that relies on encoding many times and taking the
296
+    // one with the fewest bits will work properly here.
297
+    if (s->i_tex_bits != MJPEG_HUFFMAN_EST_BITS_PER_CODE *
298
+        s->mjpeg_ctx->huff_ncode) {
299
+        av_log(NULL, AV_LOG_ERROR, "Unsupported encoding method\n");
300
+        return AVERROR(EINVAL);
301
+    }
302
+
182 303
     if (s->chroma_format == CHROMA_444) {
304
+        realloc_huffman(s, 12);
183 305
         encode_block(s, block[0], 0);
184 306
         encode_block(s, block[2], 2);
185 307
         encode_block(s, block[4], 4);
... ...
@@ -196,10 +302,12 @@ void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
196 196
             encode_block(s, block[11], 11);
197 197
         }
198 198
     } else {
199
+        is_chroma_420 = (s->chroma_format == CHROMA_420);
200
+        realloc_huffman(s, 5 + (is_chroma_420 ? 1 : 3));
199 201
         for(i=0;i<5;i++) {
200 202
             encode_block(s, block[i], i);
201 203
         }
202
-        if (s->chroma_format == CHROMA_420) {
204
+        if (is_chroma_420) {
203 205
             encode_block(s, block[5], 5);
204 206
         } else {
205 207
             encode_block(s, block[6], 6);
... ...
@@ -207,8 +315,11 @@ void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
207 207
             encode_block(s, block[7], 7);
208 208
         }
209 209
     }
210
+    if (s->mjpeg_ctx->error)
211
+        return s->mjpeg_ctx->error;
210 212
 
211
-    s->i_tex_bits += get_bits_diff(s);
213
+    s->i_tex_bits = MJPEG_HUFFMAN_EST_BITS_PER_CODE * s->mjpeg_ctx->huff_ncode;
214
+    return 0;
212 215
 }
213 216
 
214 217
 // maximum over s->mjpeg_vsample[i]
... ...
@@ -261,7 +372,9 @@ FF_MPV_COMMON_OPTS
261 261
     { "left",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
262 262
     { "plane",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
263 263
     { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "pred" },
264
-
264
+{ "huffman", "Huffman table strategy", OFFSET(huffman), AV_OPT_TYPE_INT, { .i64 = HUFFMAN_TABLE_DEFAULT }, 0, NB_HUFFMAN_TABLE_OPTION - 1, VE, "huffman" },
265
+    { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_DEFAULT }, INT_MIN, INT_MAX, VE, "huffman" },
266
+    { "optimal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_OPTIMAL }, INT_MIN, INT_MAX, VE, "huffman" },
265 267
 { NULL},
266 268
 };
267 269
 
... ...
@@ -39,18 +39,67 @@
39 39
 #include "mpegvideo.h"
40 40
 #include "put_bits.h"
41 41
 
42
+/**
43
+ * Buffer of JPEG frame data.
44
+ *
45
+ * Optimal Huffman table generation requires the frame data to be loaded into
46
+ * a buffer so that the tables can be computed.
47
+ * There are at most mb_width*mb_height*12*64 of these per frame.
48
+ */
49
+typedef struct MJpegHuffmanCode {
50
+    // 0=DC lum, 1=DC chrom, 2=AC lum, 3=AC chrom
51
+    uint8_t table_id; ///< The Huffman table id associated with the data.
52
+    uint8_t code;     ///< The exponent.
53
+    uint16_t mant;    ///< The mantissa.
54
+} MJpegHuffmanCode;
55
+
56
+/**
57
+ * Holds JPEG frame data and Huffman table data.
58
+ */
42 59
 typedef struct MJpegContext {
43
-    uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chroma, for easier addressing
44
-    uint16_t huff_code_dc_luminance[12];
45
-    uint8_t huff_size_dc_chrominance[12];
46
-    uint16_t huff_code_dc_chrominance[12];
60
+    //FIXME use array [3] instead of lumi / chroma, for easier addressing
61
+    uint8_t huff_size_dc_luminance[12];     ///< DC luminance Huffman table size.
62
+    uint16_t huff_code_dc_luminance[12];    ///< DC luminance Huffman table codes.
63
+    uint8_t huff_size_dc_chrominance[12];   ///< DC chrominance Huffman table size.
64
+    uint16_t huff_code_dc_chrominance[12];  ///< DC chrominance Huffman table codes.
65
+
66
+    uint8_t huff_size_ac_luminance[256];    ///< AC luminance Huffman table size.
67
+    uint16_t huff_code_ac_luminance[256];   ///< AC luminance Huffman table codes.
68
+    uint8_t huff_size_ac_chrominance[256];  ///< AC chrominance Huffman table size.
69
+    uint16_t huff_code_ac_chrominance[256]; ///< AC chrominance Huffman table codes.
47 70
 
48
-    uint8_t huff_size_ac_luminance[256];
49
-    uint16_t huff_code_ac_luminance[256];
50
-    uint8_t huff_size_ac_chrominance[256];
51
-    uint16_t huff_code_ac_chrominance[256];
71
+    /** Storage for AC luminance VLC (in MpegEncContext) */
72
+    uint8_t uni_ac_vlc_len[64 * 64 * 2];
73
+    /** Storage for AC chrominance VLC (in MpegEncContext) */
74
+    uint8_t uni_chroma_ac_vlc_len[64 * 64 * 2];
75
+
76
+    // Default DC tables have exactly 12 values
77
+    uint8_t bits_dc_luminance[17];   ///< DC luminance Huffman bits.
78
+    uint8_t val_dc_luminance[12];    ///< DC luminance Huffman values.
79
+    uint8_t bits_dc_chrominance[17]; ///< DC chrominance Huffman bits.
80
+    uint8_t val_dc_chrominance[12];  ///< DC chrominance Huffman values.
81
+
82
+    // 8-bit JPEG has max 256 values
83
+    uint8_t bits_ac_luminance[17];   ///< AC luminance Huffman bits.
84
+    uint8_t val_ac_luminance[256];   ///< AC luminance Huffman values.
85
+    uint8_t bits_ac_chrominance[17]; ///< AC chrominance Huffman bits.
86
+    uint8_t val_ac_chrominance[256]; ///< AC chrominance Huffman values.
87
+
88
+    unsigned int huff_capacity;      ///< Size of the buffer, in entries.
89
+    size_t huff_ncode;               ///< Number of current entries in the buffer.
90
+    MJpegHuffmanCode *huff_buffer;   ///< Buffer for Huffman code values.
91
+    int error;                       ///< Error code.
52 92
 } MJpegContext;
53 93
 
94
+/**
95
+ * Enum for the Huffman encoding strategy.
96
+ */
97
+enum HuffmanTableOption {
98
+    HUFFMAN_TABLE_DEFAULT = 0, ///< Use the default Huffman tables.
99
+    HUFFMAN_TABLE_OPTIMAL = 1, ///< Compute and use optimal Huffman tables.
100
+    NB_HUFFMAN_TABLE_OPTION = 2
101
+};
102
+
54 103
 static inline void put_marker(PutBitContext *p, enum JpegMarker code)
55 104
 {
56 105
     put_bits(p, 8, 0xff);
... ...
@@ -58,7 +107,8 @@ static inline void put_marker(PutBitContext *p, enum JpegMarker code)
58 58
 }
59 59
 
60 60
 int  ff_mjpeg_encode_init(MpegEncContext *s);
61
+void ff_mjpeg_encode_picture_frame(MpegEncContext *s);
61 62
 void ff_mjpeg_encode_close(MpegEncContext *s);
62
-void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64]);
63
+int ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64]);
63 64
 
64 65
 #endif /* AVCODEC_MJPEGENC_H */
... ...
@@ -33,8 +33,35 @@
33 33
 #include "put_bits.h"
34 34
 #include "mjpegenc.h"
35 35
 #include "mjpegenc_common.h"
36
+#include "mjpegenc_huffman.h"
36 37
 #include "mjpeg.h"
37 38
 
39
+av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
40
+{
41
+    int i;
42
+
43
+    for (i = 0; i < 128; i++) {
44
+        int level = i - 64;
45
+        int run;
46
+        if (!level)
47
+            continue;
48
+        for (run = 0; run < 64; run++) {
49
+            int len, code, nbits;
50
+            int alevel = FFABS(level);
51
+
52
+            len = (run >> 4) * huff_size_ac[0xf0];
53
+
54
+            nbits= av_log2_16bit(alevel) + 1;
55
+            code = ((15&run) << 4) | nbits;
56
+
57
+            len += huff_size_ac[code] + nbits;
58
+
59
+            uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
60
+            // We ignore EOB as its just a constant which does not change generally
61
+        }
62
+    }
63
+}
64
+
38 65
 /* table_class: 0 = DC coef, 1 = AC coefs */
39 66
 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
40 67
                              const uint8_t *bits_table, const uint8_t *value_table)
... ...
@@ -104,15 +131,30 @@ static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
104 104
     ptr = put_bits_ptr(p);
105 105
     put_bits(p, 16, 0); /* patched later */
106 106
     size = 2;
107
-    size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
108
-                              avpriv_mjpeg_val_dc);
109
-    size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
110
-                              avpriv_mjpeg_val_dc);
111
-
112
-    size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
113
-                              avpriv_mjpeg_val_ac_luminance);
114
-    size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
115
-                              avpriv_mjpeg_val_ac_chrominance);
107
+
108
+    // Only MJPEG can have a variable Huffman variable. All other
109
+    // formats use the default Huffman table.
110
+    if (s->out_format == FMT_MJPEG && s->huffman == HUFFMAN_TABLE_OPTIMAL) {
111
+        size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
112
+                                  s->mjpeg_ctx->val_dc_luminance);
113
+        size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
114
+                                  s->mjpeg_ctx->val_dc_chrominance);
115
+
116
+        size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
117
+                                  s->mjpeg_ctx->val_ac_luminance);
118
+        size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
119
+                                  s->mjpeg_ctx->val_ac_chrominance);
120
+    } else {
121
+        size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
122
+                                  avpriv_mjpeg_val_dc);
123
+        size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
124
+                                  avpriv_mjpeg_val_dc);
125
+
126
+        size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
127
+                                  avpriv_mjpeg_val_ac_luminance);
128
+        size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
129
+                                  avpriv_mjpeg_val_ac_chrominance);
130
+    }
116 131
     AV_WB16(ptr, size);
117 132
 }
118 133
 
... ...
@@ -372,14 +414,116 @@ void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
372 372
     }
373 373
 }
374 374
 
375
+/**
376
+ * Builds all 4 optimal Huffman tables.
377
+ *
378
+ * Uses the data stored in the JPEG buffer to compute the tables.
379
+ * Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
380
+ *
381
+ * @param m MJpegContext containing the JPEG buffer.
382
+ */
383
+static void ff_mjpeg_build_optimal_huffman(MJpegContext *m)
384
+{
385
+    int i, ret, table_id, code;
386
+
387
+    MJpegEncHuffmanContext dc_luminance_ctx;
388
+    MJpegEncHuffmanContext dc_chrominance_ctx;
389
+    MJpegEncHuffmanContext ac_luminance_ctx;
390
+    MJpegEncHuffmanContext ac_chrominance_ctx;
391
+    MJpegEncHuffmanContext *ctx[4] = {&dc_luminance_ctx,
392
+                                      &dc_chrominance_ctx,
393
+                                      &ac_luminance_ctx,
394
+                                      &ac_chrominance_ctx};
395
+    for (i = 0; i < 4; i++) {
396
+        ff_mjpeg_encode_huffman_init(ctx[i]);
397
+    }
398
+    for (i = 0; i < m->huff_ncode; i++) {
399
+        table_id = m->huff_buffer[i].table_id;
400
+        code = m->huff_buffer[i].code;
401
+
402
+        ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
403
+    }
404
+
405
+    ret = ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
406
+                                        m->bits_dc_luminance,
407
+                                        m->val_dc_luminance, 12);
408
+    av_assert0(!ret);
409
+    ret = ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
410
+                                        m->bits_dc_chrominance,
411
+                                        m->val_dc_chrominance, 12);
412
+    av_assert0(!ret);
413
+    ret = ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
414
+                                        m->bits_ac_luminance,
415
+                                        m->val_ac_luminance, 256);
416
+    av_assert0(!ret);
417
+    ret = ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
418
+                                        m->bits_ac_chrominance,
419
+                                        m->val_ac_chrominance, 256);
420
+    av_assert0(!ret);
421
+
422
+    ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
423
+                                 m->huff_code_dc_luminance,
424
+                                 m->bits_dc_luminance,
425
+                                 m->val_dc_luminance);
426
+    ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
427
+                                 m->huff_code_dc_chrominance,
428
+                                 m->bits_dc_chrominance,
429
+                                 m->val_dc_chrominance);
430
+    ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
431
+                                 m->huff_code_ac_luminance,
432
+                                 m->bits_ac_luminance,
433
+                                 m->val_ac_luminance);
434
+    ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
435
+                                 m->huff_code_ac_chrominance,
436
+                                 m->bits_ac_chrominance,
437
+                                 m->val_ac_chrominance);
438
+}
439
+
440
+/**
441
+ * Writes the complete JPEG frame.
442
+ *
443
+ * Header + values + stuffing.
444
+ *
445
+ * @param s The MpegEncContext.
446
+ * @return int Error code, 0 if successful.
447
+ */
375 448
 int ff_mjpeg_encode_stuffing(MpegEncContext *s)
376 449
 {
377 450
     int i;
378 451
     PutBitContext *pbc = &s->pb;
379 452
     int mb_y = s->mb_y - !s->mb_x;
453
+    int ret;
454
+    MJpegContext *m;
455
+
456
+    m = s->mjpeg_ctx;
457
+
458
+    if (m->error)
459
+        return m->error;
460
+
461
+    if (s->huffman == HUFFMAN_TABLE_OPTIMAL) {
462
+        ff_mjpeg_build_optimal_huffman(m);
463
+
464
+        // Replace the VLCs with the optimal ones.
465
+        // The default ones may be used for trellis during quantization.
466
+        init_uni_ac_vlc(m->huff_size_ac_luminance,   m->uni_ac_vlc_len);
467
+        init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
468
+        s->intra_ac_vlc_length      =
469
+        s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
470
+        s->intra_chroma_ac_vlc_length      =
471
+        s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
472
+    }
473
+
474
+    ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
475
+                                   s->pred, s->intra_matrix, s->chroma_intra_matrix);
476
+    ff_mjpeg_encode_picture_frame(s);
477
+    if (m->error < 0) {
478
+        ret = m->error;
479
+        return ret;
480
+    }
481
+
482
+    ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
483
+                                            put_bits_count(&s->pb) / 4 + 1000);
380 484
 
381
-    int ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
382
-                                                put_bits_count(&s->pb) / 4 + 1000);
383 485
     if (ret < 0) {
384 486
         av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
385 487
         goto fail;
... ...
@@ -40,4 +40,6 @@ void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4
40 40
 void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
41 41
                         uint8_t *huff_size, uint16_t *huff_code);
42 42
 
43
+av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len);
44
+
43 45
 #endif /* AVCODEC_MJPEGENC_COMMON_H */
44 46
new file mode 100644
... ...
@@ -0,0 +1,195 @@
0
+/*
1
+ * MJPEG encoder
2
+ * Copyright (c) 2016 William Ma, Ted Ying, Jerry Jiang
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <string.h>
22
+#include <stdint.h>
23
+#include <assert.h>
24
+#include <stdlib.h>
25
+#include "libavutil/common.h"
26
+#include "libavutil/error.h"
27
+#include "libavutil/qsort.h"
28
+#include "mjpegenc_huffman.h"
29
+
30
+/**
31
+ * Comparison function for two PTables by prob
32
+ *
33
+ * @param a First PTable to compare
34
+ * @param b Second PTable to compare
35
+ * @return < 0 for less than, 0 for equals, > 0 for greater than
36
+ */
37
+static int compare_by_prob(const void *a, const void *b)
38
+{
39
+    PTable a_val = *(PTable *) a;
40
+    PTable b_val = *(PTable *) b;
41
+    return a_val.prob - b_val.prob;
42
+}
43
+
44
+/**
45
+ * Comparison function for two HuffTables by length
46
+ *
47
+ * @param a First HuffTable to compare
48
+ * @param b Second HuffTable to compare
49
+ * @return < 0 for less than, 0 for equals, > 0 for greater than
50
+ */
51
+static int compare_by_length(const void *a, const void *b)
52
+{
53
+    HuffTable a_val = *(HuffTable *) a;
54
+    HuffTable b_val = *(HuffTable *) b;
55
+    return a_val.length - b_val.length;
56
+}
57
+
58
+/**
59
+ * Computes the length of the Huffman encoding for each distinct input value.
60
+ * Uses package merge algorithm as follows:
61
+ * 1. start with an empty list, lets call it list(0), set i = 0
62
+ * 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
63
+ * 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
64
+ * 4. if there is more than 1 symbol left in the current list(i), then goto 3
65
+ * 5. i++
66
+ * 6. if i < 16 goto 2
67
+ * 7. select the n-1 elements in the last list with the lowest score (n = the number of symbols)
68
+ * 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
69
+ * Go to guru.multimedia.cx/small-tasks-for-ffmpeg/ for more details
70
+ *
71
+ * All probabilities should be positive integers. The output is sorted by code,
72
+ * not by length.
73
+ *
74
+ * @param prob_table input array of a PTable for each distinct input value
75
+ * @param distincts  output array of a HuffTable that will be populated by this function
76
+ * @param size       size of the prob_table array
77
+ * @param max_length max length of an encoding
78
+ */
79
+void ff_mjpegenc_huffman_compute_bits(PTable *prob_table, HuffTable *distincts, int size, int max_length)
80
+{
81
+    PackageMergerList list_a, list_b, *to = &list_a, *from = &list_b, *temp;
82
+
83
+    int times, i, j, k;
84
+
85
+    int nbits[257] = {0};
86
+
87
+    int min;
88
+
89
+    to->nitems = 0;
90
+    from->nitems = 0;
91
+    to->item_idx[0] = 0;
92
+    from->item_idx[0] = 0;
93
+    AV_QSORT(prob_table, size, PTable, compare_by_prob);
94
+
95
+    for (times = 0; times <= max_length; times++) {
96
+        to->nitems = 0;
97
+        to->item_idx[0] = 0;
98
+
99
+        j = 0;
100
+        k = 0;
101
+
102
+        if (times < max_length) {
103
+            i = 0;
104
+        }
105
+        while (i < size || j + 1 < from->nitems) {
106
+            to->nitems++;
107
+            to->item_idx[to->nitems] = to->item_idx[to->nitems - 1];
108
+            if (i < size &&
109
+                (j + 1 >= from->nitems ||
110
+                 prob_table[i].prob <
111
+                     from->probability[j] + from->probability[j + 1])) {
112
+                to->items[to->item_idx[to->nitems]++] = prob_table[i].value;
113
+                to->probability[to->nitems - 1] = prob_table[i].prob;
114
+                i++;
115
+            } else {
116
+                for (k = from->item_idx[j]; k < from->item_idx[j + 2]; k++) {
117
+                    to->items[to->item_idx[to->nitems]++] = from->items[k];
118
+                }
119
+                to->probability[to->nitems - 1] =
120
+                    from->probability[j] + from->probability[j + 1];
121
+                j += 2;
122
+            }
123
+        }
124
+        temp = to;
125
+        to = from;
126
+        from = temp;
127
+    }
128
+
129
+    min = (size - 1 < from->nitems) ? size - 1 : from->nitems;
130
+    for (i = 0; i < from->item_idx[min]; i++) {
131
+        nbits[from->items[i]]++;
132
+    }
133
+    // we don't want to return the 256 bit count (it was just in here to prevent
134
+    // all 1s encoding)
135
+    j = 0;
136
+    for (i = 0; i < 256; i++) {
137
+        if (nbits[i] > 0) {
138
+            distincts[j].code = i;
139
+            distincts[j].length = nbits[i];
140
+            j++;
141
+        }
142
+    }
143
+}
144
+
145
+void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s)
146
+{
147
+    memset(s->val_count, 0, sizeof(s->val_count));
148
+}
149
+
150
+/**
151
+ * Produces a Huffman encoding with a given input
152
+ *
153
+ * @param s         input to encode
154
+ * @param bits      output array where the ith character represents how many input values have i length encoding
155
+ * @param val       output array of input values sorted by their encoded length
156
+ * @param max_nval  maximum number of distinct input values
157
+ * @return int      Return code, 0 if succeeded.
158
+ */
159
+int 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
+    if (nval > max_nval) {
171
+        return AVERROR(EINVAL);
172
+    }
173
+
174
+    j = 0;
175
+    for (i = 0; i < 256; i++) {
176
+        if (s->val_count[i]) {
177
+            val_counts[j].value = i;
178
+            val_counts[j].prob = s->val_count[i];
179
+            j++;
180
+        }
181
+    }
182
+    val_counts[j].value = 256;
183
+    val_counts[j].prob = 0;
184
+    ff_mjpegenc_huffman_compute_bits(val_counts, distincts, nval + 1, 16);
185
+    AV_QSORT(distincts, nval, HuffTable, compare_by_length);
186
+
187
+    memset(bits, 0, sizeof(bits[0]) * 17);
188
+    for (i = 0; i < nval; i++) {
189
+        val[i] = distincts[i].code;
190
+        bits[distincts[i].length]++;
191
+    }
192
+
193
+    return 0;
194
+}
0 195
new file mode 100644
... ...
@@ -0,0 +1,74 @@
0
+/*
1
+ * MJPEG encoder
2
+ * Copyright (c) 2016 William Ma, Ted Ying, Jerry Jiang
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * Huffman table generation for MJPEG encoder.
24
+ */
25
+
26
+#ifndef AVCODEC_MJPEGENC_HUFFMAN_H
27
+#define AVCODEC_MJPEGENC_HUFFMAN_H
28
+
29
+typedef struct MJpegEncHuffmanContext {
30
+    int val_count[256];
31
+} MJpegEncHuffmanContext;
32
+
33
+// Uses the package merge algorithm to compute the Huffman table.
34
+void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s);
35
+static inline void ff_mjpeg_encode_huffman_increment(MJpegEncHuffmanContext *s,
36
+                                                     uint8_t val)
37
+{
38
+    s->val_count[val]++;
39
+}
40
+int ff_mjpeg_encode_huffman_close(MJpegEncHuffmanContext *s,
41
+                                  uint8_t bits[17], uint8_t val[],
42
+                                  int max_nval);
43
+
44
+
45
+/**
46
+ * Used to assign a occurrence count or "probability" to an input value
47
+ */
48
+typedef struct PTable {
49
+    int value;  ///< input value
50
+    int prob;   ///< number of occurences of this value in input
51
+} PTable;
52
+
53
+/**
54
+ * Used to store intermediate lists in the package merge algorithm
55
+ */
56
+typedef struct PackageMergerList {
57
+    int nitems;             ///< number of items in the list and probability      ex. 4
58
+    int item_idx[515];      ///< index range for each item in items                   0, 2, 5, 9, 13
59
+    int probability[514];   ///< probability of each item                             3, 8, 18, 46
60
+    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
61
+} PackageMergerList;
62
+
63
+/**
64
+ * Used to store optimal huffman encoding results
65
+ */
66
+typedef struct HuffTable {
67
+    int code;       ///< code is the input value
68
+    int length;     ///< length of the encoding
69
+} HuffTable;
70
+
71
+void ff_mjpegenc_huffman_compute_bits(PTable *prob_table, HuffTable *distincts,
72
+                                      int size, int max_length);
73
+#endif /* AVCODEC_MJPEGENC_HUFFMAN_H */
... ...
@@ -422,6 +422,7 @@ typedef struct MpegEncContext {
422 422
     struct MJpegContext *mjpeg_ctx;
423 423
     int esc_pos;
424 424
     int pred;
425
+    int huffman;
425 426
 
426 427
     /* MSMPEG4 specific */
427 428
     int mv_table_index;
... ...
@@ -266,7 +266,8 @@ static void mpv_encode_defaults(MpegEncContext *s)
266 266
     s->picture_in_gop_number = 0;
267 267
 }
268 268
 
269
-av_cold int ff_dct_encode_init(MpegEncContext *s) {
269
+av_cold int ff_dct_encode_init(MpegEncContext *s)
270
+{
270 271
     if (ARCH_X86)
271 272
         ff_dct_encode_init_x86(s);
272 273
 
... ...
@@ -642,6 +643,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
642 642
         return -1;
643 643
     }
644 644
 
645
+    if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
646
+            (s->codec_id == AV_CODEC_ID_AMV ||
647
+             s->codec_id == AV_CODEC_ID_MJPEG)) {
648
+        // Used to produce garbage with MJPEG.
649
+        av_log(avctx, AV_LOG_ERROR,
650
+               "QP RD is no longer compatible with MJPEG or AMV\n");
651
+        return -1;
652
+    }
653
+
645 654
 #if FF_API_PRIVATE_OPT
646 655
 FF_DISABLE_DEPRECATION_WARNINGS
647 656
     if (avctx->scenechange_threshold)
... ...
@@ -3932,9 +3942,8 @@ static int encode_picture(MpegEncContext *s, int picture_number)
3932 3932
     s->last_bits= put_bits_count(&s->pb);
3933 3933
     switch(s->out_format) {
3934 3934
     case FMT_MJPEG:
3935
-        if (CONFIG_MJPEG_ENCODER)
3936
-            ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
3937
-                                           s->pred, s->intra_matrix, s->chroma_intra_matrix);
3935
+        /* The MJPEG headers are printed after the initial encoding so that the
3936
+         * optimal huffman encoding can be found. */
3938 3937
         break;
3939 3938
     case FMT_H261:
3940 3939
         if (CONFIG_H261_ENCODER)
... ...
@@ -10,6 +10,7 @@
10 10
 /imgconvert
11 11
 /jpeg2000dwt
12 12
 /mathops
13
+/mjpegenc_huffman
13 14
 /motion
14 15
 /options
15 16
 /rangecoder
16 17
new file mode 100644
... ...
@@ -0,0 +1,163 @@
0
+/*
1
+ * Copyright (c) 2016 William Ma, Sofia Kim, Dustin Woo
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Optimal Huffman Encoding tests.
23
+ */
24
+
25
+#include "libavcodec/avcodec.h"
26
+#include <stdlib.h>
27
+#include "libavcodec/mjpegenc.h"
28
+#include "libavcodec/mjpegenc_huffman.h"
29
+#include "libavcodec/mjpegenc_common.h"
30
+#include "libavcodec/mpegvideo.h"
31
+
32
+// Validate the computed lengths satisfy the JPEG restrictions and is optimal.
33
+static int check_lengths(int L, int expected_length,
34
+                         const int *probs, int nprobs)
35
+{
36
+    HuffTable lengths[256];
37
+    PTable val_counts[256];
38
+    int actual_length = 0, i, j, k, prob, length;
39
+    int ret = 0;
40
+    double cantor_measure = 0;
41
+    assert(nprobs <= 256);
42
+
43
+    for (i = 0; i < nprobs; i++) {
44
+        val_counts[i] = (PTable){.value = i, .prob = probs[i]};
45
+    }
46
+
47
+    ff_mjpegenc_huffman_compute_bits(val_counts, lengths, nprobs, L);
48
+
49
+    for (i = 0; i < nprobs; i++) {
50
+        // Find the value's prob and length
51
+        for (j = 0; j < nprobs; j++)
52
+            if (val_counts[j].value == i) break;
53
+        for (k = 0; k < nprobs; k++)
54
+            if (lengths[k].code == i) break;
55
+        if (!(j < nprobs && k < nprobs)) return 1;
56
+        prob = val_counts[j].prob;
57
+        length = lengths[k].length;
58
+
59
+        if (prob) {
60
+            actual_length += prob * length;
61
+            cantor_measure += 1. / (1 << length);
62
+        }
63
+
64
+        if (length > L || length < 1) return 1;
65
+    }
66
+    // Check that the codes can be prefix-free.
67
+    if (cantor_measure > 1) ret = 1;
68
+    // Check that the total length is optimal
69
+    if (actual_length != expected_length) ret = 1;
70
+
71
+    if (ret == 1) {
72
+      fprintf(stderr,
73
+              "Cantor measure: %f\n"
74
+              "Actual length: %d\n"
75
+              "Expected length: %d\n",
76
+              cantor_measure, actual_length, expected_length);
77
+    }
78
+
79
+    return ret;
80
+}
81
+
82
+static const int probs_zeroes[] = {6, 6, 0, 0, 0};
83
+static const int probs_skewed[] = {2, 0, 0, 0, 0, 1, 0, 0, 20, 0, 2,
84
+    0, 10, 5, 1, 1, 9, 1, 1, 6, 0, 5, 0, 1, 0, 7, 6, 1, 1, 5, 0, 0, 0, 0,
85
+    11, 0, 0, 0, 51, 1, 0, 20, 0, 1, 0, 0, 0, 0, 6, 106, 1, 0, 1, 0, 2, 1,
86
+    16, 0, 0, 5, 0, 0, 0, 4, 3, 15, 4, 4, 0, 0, 0, 3, 0, 0, 1, 0, 3, 0, 3,
87
+    2, 2, 0, 0, 4, 3, 40, 1, 2, 0, 22, 0, 0, 0, 9, 0, 0, 0, 0, 1, 1, 0, 1,
88
+    6, 11, 4, 10, 28, 6, 1, 0, 0, 9, 9, 4, 0, 0, 0, 0, 8, 33844, 2, 0, 2,
89
+    1, 1, 5, 0, 0, 1, 9, 1, 0, 4, 14, 4, 0, 0, 3, 8, 0, 51, 9, 6, 1, 1, 2,
90
+    2, 3, 1, 5, 5, 29, 0, 0, 0, 0, 14, 29, 6, 4, 13, 12, 2, 3, 1, 0, 5, 4,
91
+    1, 1, 0, 0, 29, 1, 0, 0, 0, 0, 4, 0, 0, 1, 0, 1, 7, 0, 42, 0, 0, 0, 0,
92
+    0, 2, 0, 3, 9, 0, 0, 0, 2, 1, 0, 0, 6, 5, 6, 1, 2, 3, 0, 0, 0, 3, 0, 0,
93
+    28, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 23, 0, 0, 0, 0,
94
+    0, 21, 1, 0, 3, 24, 2, 0, 0, 7, 0, 0, 1, 5, 1, 2, 0, 5};
95
+static const int probs_sat[] = {74, 8, 14, 7, 9345, 40, 0, 2014, 2, 1,
96
+    115, 0, 2, 1, 194, 388, 20, 0, 0, 2, 1, 121, 1, 1583, 0, 16, 21, 2, 132,
97
+    2, 15, 9, 13, 1, 0, 2293, 2, 8, 5, 2, 30, 0, 0, 4, 54, 783, 4, 1, 2, 4,
98
+    0, 22, 93, 1, 143, 19, 0, 36, 32, 4, 6, 33, 3, 45, 0, 8, 1, 0, 18, 17, 1,
99
+    0, 1, 0, 0, 1, 1004, 38, 3, 8, 90, 23, 0, 2819, 3, 0, 970, 158, 9, 6, 4,
100
+    48, 4, 0, 1, 0, 0, 60, 3, 62, 0, 2, 2, 2, 279, 66, 16, 1, 20, 0, 7, 9,
101
+    32, 1411, 6, 3, 27, 1, 5, 49, 0, 0, 0, 0, 0, 2, 10, 1, 1, 2, 3, 801, 3,
102
+    25, 5, 1, 1, 0, 632, 0, 14, 18, 5, 8, 200, 4, 4, 22, 12, 0, 4, 1, 0, 2,
103
+    4, 9, 3, 16, 7, 2, 2, 213, 0, 2, 620, 39303, 0, 1, 0, 2, 1, 183781, 1,
104
+    0, 0, 0, 94, 7, 3, 4, 0, 4, 306, 43, 352, 76, 34, 13, 11, 0, 51, 1, 13,
105
+    19, 0, 26, 0, 7276, 4, 207, 31, 1, 2, 4, 6, 19, 8, 17, 4, 6, 0, 1085, 0,
106
+    0, 0, 3, 489, 36, 1, 0, 1, 9420, 294, 28, 0, 57, 5, 0, 9, 2, 0, 1, 2, 2,
107
+    0, 0, 9, 2, 29, 2, 2, 7, 0, 5, 490, 0, 7, 5, 0, 1, 8, 0, 0, 23255, 0, 1};
108
+
109
+// Test the example given on @see
110
+// http://guru.multimedia.cx/small-tasks-for-ffmpeg/
111
+int main(int argc, char **argv)
112
+{
113
+    int i, ret = 0;
114
+    // Probabilities of symbols 0..4
115
+    static PTable val_counts[] = {
116
+        {.value = 0, .prob = 1},
117
+        {.value = 1, .prob = 2},
118
+        {.value = 2, .prob = 5},
119
+        {.value = 3, .prob = 10},
120
+        {.value = 4, .prob = 21},
121
+    };
122
+    // Expected code lengths for each symbol
123
+    static const HuffTable expected[] = {
124
+        {.code = 0, .length = 3},
125
+        {.code = 1, .length = 3},
126
+        {.code = 2, .length = 3},
127
+        {.code = 3, .length = 3},
128
+        {.code = 4, .length = 1},
129
+    };
130
+    // Actual code lengths
131
+    HuffTable distincts[5];
132
+
133
+    // Build optimal huffman tree using an internal function, to allow for
134
+    // smaller-than-normal test cases. This mutates val_counts by sorting.
135
+    ff_mjpegenc_huffman_compute_bits(val_counts, distincts,
136
+                                     FF_ARRAY_ELEMS(distincts), 3);
137
+
138
+    for (i = 0; i < FF_ARRAY_ELEMS(distincts); i++) {
139
+        if (distincts[i].code != expected[i].code ||
140
+            distincts[i].length != expected[i].length) {
141
+            fprintf(stderr,
142
+                    "Built huffman does not equal expectations. "
143
+                    "Expected: code %d probability %d, "
144
+                    "Actual: code %d probability %d\n",
145
+                    expected[i].code, expected[i].length,
146
+                    distincts[i].code, distincts[i].length);
147
+            ret = 1;
148
+        }
149
+    }
150
+
151
+    // Check handling of zero probabilities
152
+    if (check_lengths(16, 18, probs_zeroes, FF_ARRAY_ELEMS(probs_zeroes)))
153
+        ret = 1;
154
+    // Check skewed distribution over 256 without saturated lengths
155
+    if (check_lengths(16, 41282, probs_skewed, FF_ARRAY_ELEMS(probs_skewed)))
156
+        ret = 1;
157
+    // Check skewed distribution over 256 with saturated lengths
158
+    if (check_lengths(16, 669904, probs_sat, FF_ARRAY_ELEMS(probs_sat)))
159
+        ret = 1;
160
+
161
+    return ret;
162
+}
... ...
@@ -49,5 +49,11 @@ fate-libavcodec-utils: CMD = run libavcodec/tests/utils
49 49
 fate-libavcodec-utils: CMP = null
50 50
 fate-libavcodec-utils: REF = /dev/null
51 51
 
52
+FATE_LIBAVCODEC-yes += fate-libavcodec-huffman
53
+fate-libavcodec-huffman: libavcodec/tests/mjpegenc_huffman$(EXESUF)
54
+fate-libavcodec-huffman: CMD = run libavcodec/tests/mjpegenc_huffman
55
+fate-libavcodec-huffman: CMP = null
56
+fate-libavcodec-huffman: REF = /dev/null
57
+
52 58
 FATE-$(CONFIG_AVCODEC) += $(FATE_LIBAVCODEC-yes)
53 59
 fate-libavcodec: $(FATE_LIBAVCODEC-yes)
... ...
@@ -213,11 +213,13 @@ fate-vsynth%-jpeg2000-97:             DECINOPTS = -vcodec jpeg2000
213 213
 FATE_VCODEC-$(call ENCDEC, LJPEG MJPEG, AVI) += ljpeg
214 214
 fate-vsynth%-ljpeg:              ENCOPTS = -strict -1
215 215
 
216
-FATE_VCODEC-$(call ENCDEC, MJPEG, AVI)  += mjpeg mjpeg-422 mjpeg-444 mjpeg-trell
217
-fate-vsynth%-mjpeg:              ENCOPTS = -qscale 9 -pix_fmt yuvj420p
218
-fate-vsynth%-mjpeg-422:          ENCOPTS = -qscale 9 -pix_fmt yuvj422p
219
-fate-vsynth%-mjpeg-444:          ENCOPTS = -qscale 9 -pix_fmt yuvj444p
220
-fate-vsynth%-mjpeg-trell:        ENCOPTS = -qscale 9 -pix_fmt yuvj420p -trellis 1
216
+FATE_VCODEC-$(call ENCDEC, MJPEG, AVI)  += mjpeg mjpeg-422 mjpeg-444 mjpeg-trell mjpeg-huffman mjpeg-trell-huffman
217
+fate-vsynth%-mjpeg:                   ENCOPTS = -qscale 9 -pix_fmt yuvj420p
218
+fate-vsynth%-mjpeg-422:               ENCOPTS = -qscale 9 -pix_fmt yuvj422p
219
+fate-vsynth%-mjpeg-444:               ENCOPTS = -qscale 9 -pix_fmt yuvj444p
220
+fate-vsynth%-mjpeg-trell:             ENCOPTS = -qscale 9 -pix_fmt yuvj420p -trellis 1
221
+fate-vsynth%-mjpeg-huffman:           ENCOPTS = -qscale 9 -pix_fmt yuvj420p -huffman optimal
222
+fate-vsynth%-mjpeg-trell-huffman:     ENCOPTS = -qscale 9 -pix_fmt yuvj420p -trellis 1 -huffman optimal
221 223
 
222 224
 FATE_VCODEC-$(call ENCDEC, MPEG1VIDEO, MPEG1VIDEO MPEGVIDEO) += mpeg1 mpeg1b
223 225
 fate-vsynth%-mpeg1:              FMT     = mpeg1video
224 226
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+63ea9bd494e16bad8f3a0c8dbb3dc11e *tests/data/fate/vsynth1-mjpeg-huffman.avi
1
+1391380 tests/data/fate/vsynth1-mjpeg-huffman.avi
2
+9a3b8169c251d19044f7087a95458c55 *tests/data/fate/vsynth1-mjpeg-huffman.out.rawvideo
3
+stddev:    7.87 PSNR: 30.21 MAXDIFF:   63 bytes:  7603200/  7603200
0 4
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+d9410fa80c07edbd2a2b44ceb06086ca *tests/data/fate/vsynth1-mjpeg-trell-huffman.avi
1
+1360456 tests/data/fate/vsynth1-mjpeg-trell-huffman.avi
2
+0266b223bdd7928426a951164bb4a366 *tests/data/fate/vsynth1-mjpeg-trell-huffman.out.rawvideo
3
+stddev:    7.68 PSNR: 30.42 MAXDIFF:   62 bytes:  7603200/  7603200
0 4
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+9bf00cd3188b7395b798bb10df376243 *tests/data/fate/vsynth2-mjpeg-huffman.avi
1
+792742 tests/data/fate/vsynth2-mjpeg-huffman.avi
2
+2b8c59c59e33d6ca7c85d31c5eeab7be *tests/data/fate/vsynth2-mjpeg-huffman.out.rawvideo
3
+stddev:    4.87 PSNR: 34.37 MAXDIFF:   55 bytes:  7603200/  7603200
0 4
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+a59d99d31d24875161504820d4266e4d *tests/data/fate/vsynth2-mjpeg-trell-huffman.avi
1
+734728 tests/data/fate/vsynth2-mjpeg-trell-huffman.avi
2
+42376126213c73c86b408882e24ba015 *tests/data/fate/vsynth2-mjpeg-trell-huffman.out.rawvideo
3
+stddev:    5.03 PSNR: 34.09 MAXDIFF:   67 bytes:  7603200/  7603200
0 4
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+eec435352485fec167179a63405505be *tests/data/fate/vsynth3-mjpeg-huffman.avi
1
+48156 tests/data/fate/vsynth3-mjpeg-huffman.avi
2
+c4fe7a2669afbd96c640748693fc4e30 *tests/data/fate/vsynth3-mjpeg-huffman.out.rawvideo
3
+stddev:    8.60 PSNR: 29.43 MAXDIFF:   58 bytes:    86700/    86700
0 4
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+484fa337b71c06a0206243814c4894b0 *tests/data/fate/vsynth3-mjpeg-trell-huffman.avi
1
+47816 tests/data/fate/vsynth3-mjpeg-trell-huffman.avi
2
+f0ccfe4584d193fd6d690a85a70db188 *tests/data/fate/vsynth3-mjpeg-trell-huffman.out.rawvideo
3
+stddev:    8.27 PSNR: 29.78 MAXDIFF:   55 bytes:    86700/    86700
0 4
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+007c989af621445dc7c9bd248b9df3b4 *tests/data/fate/vsynth_lena-mjpeg-huffman.avi
1
+635498 tests/data/fate/vsynth_lena-mjpeg-huffman.avi
2
+9d4bd90e9abfa18192383b4adc23c8d4 *tests/data/fate/vsynth_lena-mjpeg-huffman.out.rawvideo
3
+stddev:    4.32 PSNR: 35.40 MAXDIFF:   49 bytes:  7603200/  7603200
0 4
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+6eb36ab28a082f496f1f3bc165704a68 *tests/data/fate/vsynth_lena-mjpeg-trell-huffman.avi
1
+582534 tests/data/fate/vsynth_lena-mjpeg-trell-huffman.avi
2
+dcb183a6a5fa06e7234d46dd97ceb8ec *tests/data/fate/vsynth_lena-mjpeg-trell-huffman.out.rawvideo
3
+stddev:    4.51 PSNR: 35.05 MAXDIFF:   60 bytes:  7603200/  7603200