GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mjpegenc_common.c Lines: 289 336 86.0 %
Date: 2019-11-20 04:07:19 Branches: 107 148 72.3 %

Line Branch Exec Source
1
/*
2
 * lossless JPEG shared bits
3
 * Copyright (c) 2000, 2001 Fabrice Bellard
4
 * Copyright (c) 2003 Alex Beregszaszi
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#include <stdint.h>
24
#include <string.h>
25
26
#include "libavutil/common.h"
27
#include "libavutil/pixdesc.h"
28
#include "libavutil/pixfmt.h"
29
30
#include "avcodec.h"
31
#include "idctdsp.h"
32
#include "jpegtables.h"
33
#include "put_bits.h"
34
#include "mjpegenc.h"
35
#include "mjpegenc_common.h"
36
#include "mjpegenc_huffman.h"
37
#include "mjpeg.h"
38
39
2536
av_cold void ff_init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
40
{
41
    int i;
42
43
327144
    for (i = 0; i < 128; i++) {
44
324608
        int level = i - 64;
45
        int run;
46
324608
        if (!level)
47
2536
            continue;
48
20934680
        for (run = 0; run < 64; run++) {
49
            int len, code, nbits;
50
20612608
            int alevel = FFABS(level);
51
52
20612608
            len = (run >> 4) * huff_size_ac[0xf0];
53
54
20612608
            nbits= av_log2_16bit(alevel) + 1;
55
20612608
            code = ((15&run) << 4) | nbits;
56
57
20612608
            len += huff_size_ac[code] + nbits;
58
59
20612608
            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
2536
}
64
65
/* table_class: 0 = DC coef, 1 = AC coefs */
66
5752
static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
67
                             const uint8_t *bits_table, const uint8_t *value_table)
68
{
69
    int n, i;
70
71
5752
    put_bits(p, 4, table_class);
72
5752
    put_bits(p, 4, table_id);
73
74
5752
    n = 0;
75
97784
    for(i=1;i<=16;i++) {
76
92032
        n += bits_table[i];
77
92032
        put_bits(p, 8, bits_table[i]);
78
    }
79
80
184935
    for(i=0;i<n;i++)
81
179183
        put_bits(p, 8, value_table[i]);
82
83
5752
    return n + 17;
84
}
85
86
1438
static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
87
                              ScanTable *intra_scantable,
88
                              uint16_t luma_intra_matrix[64],
89
                              uint16_t chroma_intra_matrix[64],
90
                              int hsample[3])
91
{
92
    int i, j, size;
93
    uint8_t *ptr;
94
1438
    MpegEncContext *s = NULL;
95
96
    /* Since avctx->priv_data will point to LJpegEncContext in this case */
97
1438
    if (avctx->codec_id != AV_CODEC_ID_LJPEG)
98
1238
        s = avctx->priv_data;
99
100
1438
    if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
101
1238
        int matrix_count = 1 + !!memcmp(luma_intra_matrix,
102
                                        chroma_intra_matrix,
103
                                        sizeof(luma_intra_matrix[0]) * 64);
104

1238
    if (s && s->force_duplicated_matrix)
105
        matrix_count = 2;
106
    /* quant matrixes */
107
1238
    put_marker(p, DQT);
108
1238
    put_bits(p, 16, 2 + matrix_count * (1 + 64));
109
1238
    put_bits(p, 4, 0); /* 8 bit precision */
110
1238
    put_bits(p, 4, 0); /* table 0 */
111
80470
    for(i=0;i<64;i++) {
112
79232
        j = intra_scantable->permutated[i];
113
79232
        put_bits(p, 8, luma_intra_matrix[j]);
114
    }
115
116
1238
        if (matrix_count > 1) {
117
            put_bits(p, 4, 0); /* 8 bit precision */
118
            put_bits(p, 4, 1); /* table 1 */
119
            for(i=0;i<64;i++) {
120
                j = intra_scantable->permutated[i];
121
                put_bits(p, 8, chroma_intra_matrix[j]);
122
            }
123
        }
124
    }
125
126
1438
    if(avctx->active_thread_type & FF_THREAD_SLICE){
127
        put_marker(p, DRI);
128
        put_bits(p, 16, 4);
129
        put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
130
    }
131
132
    /* huffman table */
133
1438
    put_marker(p, DHT);
134
1438
    flush_put_bits(p);
135
1438
    ptr = put_bits_ptr(p);
136
1438
    put_bits(p, 16, 0); /* patched later */
137
1438
    size = 2;
138
139
    // Only MJPEG can have a variable Huffman variable. All other
140
    // formats use the default Huffman table.
141

1438
    if (s && s->huffman == HUFFMAN_TABLE_OPTIMAL) {
142
2476
        size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
143
1238
                                  s->mjpeg_ctx->val_dc_luminance);
144
2476
        size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
145
1238
                                  s->mjpeg_ctx->val_dc_chrominance);
146
147
2476
        size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
148
1238
                                  s->mjpeg_ctx->val_ac_luminance);
149
2476
        size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
150
1238
                                  s->mjpeg_ctx->val_ac_chrominance);
151
    } else {
152
200
        size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
153
                                  avpriv_mjpeg_val_dc);
154
200
        size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
155
                                  avpriv_mjpeg_val_dc);
156
157
200
        size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
158
                                  avpriv_mjpeg_val_ac_luminance);
159
200
        size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
160
                                  avpriv_mjpeg_val_ac_chrominance);
161
    }
162
1438
    AV_WB16(ptr, size);
163
1438
}
164
165
1438
static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
166
{
167
    int size;
168
    uint8_t *ptr;
169
170

1438
    if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
171
        AVRational sar = avctx->sample_aspect_ratio;
172
173
        if (sar.num > 65535 || sar.den > 65535) {
174
            if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
175
                av_log(avctx, AV_LOG_WARNING,
176
                    "Cannot store exact aspect ratio %d:%d\n",
177
                    avctx->sample_aspect_ratio.num,
178
                    avctx->sample_aspect_ratio.den);
179
        }
180
181
        /* JFIF header */
182
        put_marker(p, APP0);
183
        put_bits(p, 16, 16);
184
        avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
185
        /* The most significant byte is used for major revisions, the least
186
         * significant byte for minor revisions. Version 1.02 is the current
187
         * released revision. */
188
        put_bits(p, 16, 0x0102);
189
        put_bits(p,  8, 0);              /* units type: 0 - aspect ratio */
190
        put_bits(p, 16, sar.num);
191
        put_bits(p, 16, sar.den);
192
        put_bits(p, 8, 0); /* thumbnail width */
193
        put_bits(p, 8, 0); /* thumbnail height */
194
    }
195
196
    /* comment */
197
1438
    if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
198
        put_marker(p, COM);
199
        flush_put_bits(p);
200
        ptr = put_bits_ptr(p);
201
        put_bits(p, 16, 0); /* patched later */
202
        avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
203
        size = strlen(LIBAVCODEC_IDENT)+3;
204
        AV_WB16(ptr, size);
205
    }
206
207
1438
    if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
208
1238
          avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
209

1438
          avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
210
1238
        || avctx->color_range == AVCOL_RANGE_MPEG) {
211
200
        put_marker(p, COM);
212
200
        flush_put_bits(p);
213
200
        ptr = put_bits_ptr(p);
214
200
        put_bits(p, 16, 0); /* patched later */
215
200
        avpriv_put_string(p, "CS=ITU601", 1);
216
200
        size = strlen("CS=ITU601")+3;
217
200
        AV_WB16(ptr, size);
218
    }
219
1438
}
220
221
1642
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
222
{
223
    int chroma_h_shift, chroma_v_shift;
224
225
1642
    av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
226
                                     &chroma_v_shift);
227
1642
    if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
228
204
        (   avctx->pix_fmt == AV_PIX_FMT_BGR0
229
204
         || avctx->pix_fmt == AV_PIX_FMT_BGRA
230
204
         || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
231
        vsample[0] = hsample[0] =
232
        vsample[1] = hsample[1] =
233
        vsample[2] = hsample[2] =
234
        vsample[3] = hsample[3] = 1;
235

1642
    } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
236
200
        vsample[0] = vsample[1] = vsample[2] = 2;
237
200
        hsample[0] = hsample[1] = hsample[2] = 1;
238
    } else {
239
1442
        vsample[0] = 2;
240
1442
        vsample[1] = 2 >> chroma_v_shift;
241
1442
        vsample[2] = 2 >> chroma_v_shift;
242
1442
        hsample[0] = 2;
243
1442
        hsample[1] = 2 >> chroma_h_shift;
244
1442
        hsample[2] = 2 >> chroma_h_shift;
245
    }
246
1642
}
247
248
1638
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
249
                                    ScanTable *intra_scantable, int pred,
250
                                    uint16_t luma_intra_matrix[64],
251
                                    uint16_t chroma_intra_matrix[64])
252
{
253

1638
    const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
254
    int hsample[4], vsample[4];
255
    int i;
256
1638
    int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
257
1638
    int chroma_matrix = !!memcmp(luma_intra_matrix,
258
                                 chroma_intra_matrix,
259
                                 sizeof(luma_intra_matrix[0])*64);
260
261
1638
    ff_mjpeg_init_hvsample(avctx, hsample, vsample);
262
263
1638
    put_marker(pb, SOI);
264
265
    // hack for AMV mjpeg format
266
1638
    if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
267
268
1438
    jpeg_put_comments(avctx, pb);
269
270
1438
    jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
271
272
1438
    switch (avctx->codec_id) {
273
1238
    case AV_CODEC_ID_MJPEG:  put_marker(pb, SOF0 ); break;
274
200
    case AV_CODEC_ID_LJPEG:  put_marker(pb, SOF3 ); break;
275
    default: av_assert0(0);
276
    }
277
278
1438
    put_bits(pb, 16, 17);
279

1438
    if (lossless && (  avctx->pix_fmt == AV_PIX_FMT_BGR0
280
200
                    || avctx->pix_fmt == AV_PIX_FMT_BGRA
281
200
                    || avctx->pix_fmt == AV_PIX_FMT_BGR24))
282
        put_bits(pb, 8, 9); /* 9 bits/component RCT */
283
    else
284
1438
        put_bits(pb, 8, 8); /* 8 bits/component */
285
1438
    put_bits(pb, 16, avctx->height);
286
1438
    put_bits(pb, 16, avctx->width);
287
1438
    put_bits(pb, 8, components); /* 3 or 4 components */
288
289
    /* Y component */
290
1438
    put_bits(pb, 8, 1); /* component number */
291
1438
    put_bits(pb, 4, hsample[0]); /* H factor */
292
1438
    put_bits(pb, 4, vsample[0]); /* V factor */
293
1438
    put_bits(pb, 8, 0); /* select matrix */
294
295
    /* Cb component */
296
1438
    put_bits(pb, 8, 2); /* component number */
297
1438
    put_bits(pb, 4, hsample[1]); /* H factor */
298
1438
    put_bits(pb, 4, vsample[1]); /* V factor */
299
1438
    put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
300
301
    /* Cr component */
302
1438
    put_bits(pb, 8, 3); /* component number */
303
1438
    put_bits(pb, 4, hsample[2]); /* H factor */
304
1438
    put_bits(pb, 4, vsample[2]); /* V factor */
305
1438
    put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
306
307
1438
    if (components == 4) {
308
        put_bits(pb, 8, 4); /* component number */
309
        put_bits(pb, 4, hsample[3]); /* H factor */
310
        put_bits(pb, 4, vsample[3]); /* V factor */
311
        put_bits(pb, 8, 0); /* select matrix */
312
    }
313
314
    /* scan header */
315
1438
    put_marker(pb, SOS);
316
1438
    put_bits(pb, 16, 6 + 2*components); /* length */
317
1438
    put_bits(pb, 8, components); /* 3 components */
318
319
    /* Y component */
320
1438
    put_bits(pb, 8, 1); /* index */
321
1438
    put_bits(pb, 4, 0); /* DC huffman table index */
322
1438
    put_bits(pb, 4, 0); /* AC huffman table index */
323
324
    /* Cb component */
325
1438
    put_bits(pb, 8, 2); /* index */
326
1438
    put_bits(pb, 4, 1); /* DC huffman table index */
327
1438
    put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
328
329
    /* Cr component */
330
1438
    put_bits(pb, 8, 3); /* index */
331
1438
    put_bits(pb, 4, 1); /* DC huffman table index */
332
1438
    put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
333
334
1438
    if (components == 4) {
335
        /* Alpha component */
336
        put_bits(pb, 8, 4); /* index */
337
        put_bits(pb, 4, 0); /* DC huffman table index */
338
        put_bits(pb, 4, 0); /* AC huffman table index */
339
    }
340
341
1438
    put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
342
343
1438
    switch (avctx->codec_id) {
344
1238
    case AV_CODEC_ID_MJPEG:  put_bits(pb, 8, 63); break; /* Se (not used) */
345
200
    case AV_CODEC_ID_LJPEG:  put_bits(pb, 8,  0); break; /* not used */
346
    default: av_assert0(0);
347
    }
348
349
1438
    put_bits(pb, 8, 0); /* Ah/Al (not used) */
350
351
1638
end:
352
1638
    if (!lossless) {
353
1438
        MpegEncContext *s = avctx->priv_data;
354
1438
        av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
355
356
1438
        s->esc_pos = put_bits_count(pb) >> 3;
357
1438
        for(i=1; i<s->slice_context_count; i++)
358
            s->thread_context[i]->esc_pos = 0;
359
    }
360
1638
}
361
362
/**
363
 * Encodes and outputs the entire frame in the JPEG format.
364
 *
365
 * @param s The MpegEncContext.
366
 */
367
1238
void ff_mjpeg_encode_picture_frame(MpegEncContext *s)
368
{
369
    int i, nbits, code, table_id;
370
1238
    MJpegContext *m = s->mjpeg_ctx;
371
1238
    uint8_t *huff_size[4] = {m->huff_size_dc_luminance,
372
1238
                             m->huff_size_dc_chrominance,
373
1238
                             m->huff_size_ac_luminance,
374
1238
                             m->huff_size_ac_chrominance};
375
1238
    uint16_t *huff_code[4] = {m->huff_code_dc_luminance,
376
1238
                              m->huff_code_dc_chrominance,
377
1238
                              m->huff_code_ac_luminance,
378
1238
                              m->huff_code_ac_chrominance};
379
1238
    size_t total_bits = 0;
380
    size_t bytes_needed;
381
382
1238
    s->header_bits = get_bits_diff(s);
383
    // Estimate the total size first
384
28812345
    for (i = 0; i < m->huff_ncode; i++) {
385
28811107
        table_id = m->huff_buffer[i].table_id;
386
28811107
        code = m->huff_buffer[i].code;
387
28811107
        nbits = code & 0xf;
388
389
28811107
        total_bits += huff_size[table_id][code] + nbits;
390
    }
391
392
1238
    bytes_needed = (total_bits + 7) / 8;
393
1238
    ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
394
395
28812345
    for (i = 0; i < m->huff_ncode; i++) {
396
28811107
        table_id = m->huff_buffer[i].table_id;
397
28811107
        code = m->huff_buffer[i].code;
398
28811107
        nbits = code & 0xf;
399
400
28811107
        put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
401
28811107
        if (nbits != 0) {
402
26007642
            put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
403
        }
404
    }
405
406
1238
    m->huff_ncode = 0;
407
1238
    s->i_tex_bits = get_bits_diff(s);
408
1238
}
409
410
1638
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
411
{
412
    int size;
413
    int i, ff_count;
414
1638
    uint8_t *buf = pb->buf + start;
415
1638
    int align= (-(size_t)(buf))&3;
416
1638
    int pad = (-put_bits_count(pb))&7;
417
418
1638
    if (pad)
419
1419
        put_bits(pb, pad, (1<<pad)-1);
420
421
1638
    flush_put_bits(pb);
422
1638
    size = put_bits_count(pb) - start * 8;
423
424
    av_assert1((size&7) == 0);
425
1638
    size >>= 3;
426
427
1638
    ff_count=0;
428

4487
    for(i=0; i<size && i<align; i++){
429
2849
        if(buf[i]==0xFF) ff_count++;
430
    }
431
2331275
    for(; i<size-15; i+=16){
432
        int acc, v;
433
434
2329637
        v= *(uint32_t*)(&buf[i]);
435
2329637
        acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
436
2329637
        v= *(uint32_t*)(&buf[i+4]);
437
2329637
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
438
2329637
        v= *(uint32_t*)(&buf[i+8]);
439
2329637
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
440
2329637
        v= *(uint32_t*)(&buf[i+12]);
441
2329637
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
442
443
2329637
        acc>>=4;
444
2329637
        acc+= (acc>>16);
445
2329637
        acc+= (acc>>8);
446
2329637
        ff_count+= acc&0xFF;
447
    }
448
13684
    for(; i<size; i++){
449
12046
        if(buf[i]==0xFF) ff_count++;
450
    }
451
452
1638
    if(ff_count==0) return;
453
454
1607
    flush_put_bits(pb);
455
1607
    skip_put_bytes(pb, ff_count);
456
457
36521623
    for(i=size-1; ff_count; i--){
458
36520016
        int v= buf[i];
459
460
36520016
        if(v==0xFF){
461
138316
            buf[i+ff_count]= 0;
462
138316
            ff_count--;
463
        }
464
465
36520016
        buf[i+ff_count]= v;
466
    }
467
}
468
469
/**
470
 * Builds all 4 optimal Huffman tables.
471
 *
472
 * Uses the data stored in the JPEG buffer to compute the tables.
473
 * Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
474
 *
475
 * @param m MJpegContext containing the JPEG buffer.
476
 */
477
1238
static void ff_mjpeg_build_optimal_huffman(MJpegContext *m)
478
{
479
    int i, table_id, code;
480
481
    MJpegEncHuffmanContext dc_luminance_ctx;
482
    MJpegEncHuffmanContext dc_chrominance_ctx;
483
    MJpegEncHuffmanContext ac_luminance_ctx;
484
    MJpegEncHuffmanContext ac_chrominance_ctx;
485
1238
    MJpegEncHuffmanContext *ctx[4] = {&dc_luminance_ctx,
486
                                      &dc_chrominance_ctx,
487
                                      &ac_luminance_ctx,
488
                                      &ac_chrominance_ctx};
489
6190
    for (i = 0; i < 4; i++) {
490
4952
        ff_mjpeg_encode_huffman_init(ctx[i]);
491
    }
492
28812345
    for (i = 0; i < m->huff_ncode; i++) {
493
28811107
        table_id = m->huff_buffer[i].table_id;
494
28811107
        code = m->huff_buffer[i].code;
495
496
28811107
        ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
497
    }
498
499
1238
    ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
500
1238
                                  m->bits_dc_luminance,
501
1238
                                  m->val_dc_luminance, 12);
502
1238
    ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
503
1238
                                  m->bits_dc_chrominance,
504
1238
                                  m->val_dc_chrominance, 12);
505
1238
    ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
506
1238
                                  m->bits_ac_luminance,
507
1238
                                  m->val_ac_luminance, 256);
508
1238
    ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
509
1238
                                  m->bits_ac_chrominance,
510
1238
                                  m->val_ac_chrominance, 256);
511
512
1238
    ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
513
1238
                                 m->huff_code_dc_luminance,
514
1238
                                 m->bits_dc_luminance,
515
1238
                                 m->val_dc_luminance);
516
1238
    ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
517
1238
                                 m->huff_code_dc_chrominance,
518
1238
                                 m->bits_dc_chrominance,
519
1238
                                 m->val_dc_chrominance);
520
1238
    ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
521
1238
                                 m->huff_code_ac_luminance,
522
1238
                                 m->bits_ac_luminance,
523
1238
                                 m->val_ac_luminance);
524
1238
    ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
525
1238
                                 m->huff_code_ac_chrominance,
526
1238
                                 m->bits_ac_chrominance,
527
1238
                                 m->val_ac_chrominance);
528
1238
}
529
530
/**
531
 * Writes the complete JPEG frame when optimal huffman tables are enabled,
532
 * otherwise writes the stuffing.
533
 *
534
 * Header + values + stuffing.
535
 *
536
 * @param s The MpegEncContext.
537
 * @return int Error code, 0 if successful.
538
 */
539
1438
int ff_mjpeg_encode_stuffing(MpegEncContext *s)
540
{
541
    int i;
542
1438
    PutBitContext *pbc = &s->pb;
543
1438
    int mb_y = s->mb_y - !s->mb_x;
544
    int ret;
545
    MJpegContext *m;
546
547
1438
    m = s->mjpeg_ctx;
548
549
1438
    if (s->huffman == HUFFMAN_TABLE_OPTIMAL) {
550
1238
        ff_mjpeg_build_optimal_huffman(m);
551
552
        // Replace the VLCs with the optimal ones.
553
        // The default ones may be used for trellis during quantization.
554
1238
        ff_init_uni_ac_vlc(m->huff_size_ac_luminance,   m->uni_ac_vlc_len);
555
1238
        ff_init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
556
1238
        s->intra_ac_vlc_length      =
557
1238
        s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
558
1238
        s->intra_chroma_ac_vlc_length      =
559
1238
        s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
560
561
1238
        ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
562
1238
                                       s->pred, s->intra_matrix, s->chroma_intra_matrix);
563
1238
        ff_mjpeg_encode_picture_frame(s);
564
    }
565
566
1438
    ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
567
1438
                                            put_bits_count(&s->pb) / 4 + 1000);
568
569
1438
    if (ret < 0) {
570
        av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
571
        goto fail;
572
    }
573
574
1438
    ff_mjpeg_escape_FF(pbc, s->esc_pos);
575
576

1438
    if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
577
        put_marker(pbc, RST0 + (mb_y&7));
578
1438
    s->esc_pos = put_bits_count(pbc) >> 3;
579
1438
fail:
580
581
5752
    for(i=0; i<3; i++)
582
4314
        s->last_dc[i] = 128 << s->intra_dc_precision;
583
584
1438
    return ret;
585
}
586
587
1638
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
588
{
589
    av_assert1((header_bits & 7) == 0);
590
591
1638
    put_marker(pb, EOI);
592
1638
}
593
594
23255400
void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
595
                        uint8_t *huff_size, uint16_t *huff_code)
596
{
597
    int mant, nbits;
598
599
23255400
    if (val == 0) {
600
2701595
        put_bits(pb, huff_size[0], huff_code[0]);
601
    } else {
602
20553805
        mant = val;
603
20553805
        if (val < 0) {
604
8922845
            val = -val;
605
8922845
            mant--;
606
        }
607
608
20553805
        nbits= av_log2_16bit(val) + 1;
609
610
20553805
        put_bits(pb, huff_size[nbits], huff_code[nbits]);
611
612
20553805
        put_sbits(pb, nbits, mant);
613
    }
614
23255400
}