GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mjpegenc_common.c Lines: 200 250 80.0 %
Date: 2021-04-20 04:37:23 Branches: 92 138 66.7 %

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/pixdesc.h"
27
#include "libavutil/pixfmt.h"
28
29
#include "avcodec.h"
30
#include "idctdsp.h"
31
#include "jpegtables.h"
32
#include "put_bits.h"
33
#include "mjpegenc.h"
34
#include "mjpegenc_common.h"
35
#include "mjpeg.h"
36
37
/* table_class: 0 = DC coef, 1 = AC coefs */
38
5752
static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
39
                             const uint8_t *bits_table, const uint8_t *value_table)
40
{
41
    int n, i;
42
43
5752
    put_bits(p, 4, table_class);
44
5752
    put_bits(p, 4, table_id);
45
46
5752
    n = 0;
47
97784
    for(i=1;i<=16;i++) {
48
92032
        n += bits_table[i];
49
92032
        put_bits(p, 8, bits_table[i]);
50
    }
51
52
184935
    for(i=0;i<n;i++)
53
179183
        put_bits(p, 8, value_table[i]);
54
55
5752
    return n + 17;
56
}
57
58
1438
static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
59
                              ScanTable *intra_scantable,
60
                              uint16_t luma_intra_matrix[64],
61
                              uint16_t chroma_intra_matrix[64],
62
                              int hsample[3])
63
{
64
    int i, j, size;
65
    uint8_t *ptr;
66
1438
    MpegEncContext *s = NULL;
67
68
    /* Since avctx->priv_data will point to LJpegEncContext in this case */
69
1438
    if (avctx->codec_id != AV_CODEC_ID_LJPEG)
70
1238
        s = avctx->priv_data;
71
72
1438
    if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
73
1238
        int matrix_count = 1 + !!memcmp(luma_intra_matrix,
74
                                        chroma_intra_matrix,
75
                                        sizeof(luma_intra_matrix[0]) * 64);
76

1238
    if (s && s->force_duplicated_matrix)
77
        matrix_count = 2;
78
    /* quant matrixes */
79
1238
    put_marker(p, DQT);
80
1238
    put_bits(p, 16, 2 + matrix_count * (1 + 64));
81
1238
    put_bits(p, 4, 0); /* 8 bit precision */
82
1238
    put_bits(p, 4, 0); /* table 0 */
83
80470
    for(i=0;i<64;i++) {
84
79232
        j = intra_scantable->permutated[i];
85
79232
        put_bits(p, 8, luma_intra_matrix[j]);
86
    }
87
88
1238
        if (matrix_count > 1) {
89
            put_bits(p, 4, 0); /* 8 bit precision */
90
            put_bits(p, 4, 1); /* table 1 */
91
            for(i=0;i<64;i++) {
92
                j = intra_scantable->permutated[i];
93
                put_bits(p, 8, chroma_intra_matrix[j]);
94
            }
95
        }
96
    }
97
98
1438
    if(avctx->active_thread_type & FF_THREAD_SLICE){
99
        put_marker(p, DRI);
100
        put_bits(p, 16, 4);
101
        put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
102
    }
103
104
    /* huffman table */
105
1438
    put_marker(p, DHT);
106
1438
    flush_put_bits(p);
107
1438
    ptr = put_bits_ptr(p);
108
1438
    put_bits(p, 16, 0); /* patched later */
109
1438
    size = 2;
110
111
    // Only MJPEG can have a variable Huffman variable. All other
112
    // formats use the default Huffman table.
113

1438
    if (s && s->huffman == HUFFMAN_TABLE_OPTIMAL) {
114
2476
        size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
115
1238
                                  s->mjpeg_ctx->val_dc_luminance);
116
2476
        size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
117
1238
                                  s->mjpeg_ctx->val_dc_chrominance);
118
119
2476
        size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
120
1238
                                  s->mjpeg_ctx->val_ac_luminance);
121
2476
        size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
122
1238
                                  s->mjpeg_ctx->val_ac_chrominance);
123
    } else {
124
200
        size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
125
                                  avpriv_mjpeg_val_dc);
126
200
        size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
127
                                  avpriv_mjpeg_val_dc);
128
129
200
        size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
130
                                  avpriv_mjpeg_val_ac_luminance);
131
200
        size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
132
                                  avpriv_mjpeg_val_ac_chrominance);
133
    }
134
1438
    AV_WB16(ptr, size);
135
1438
}
136
137
1438
static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
138
{
139
    int size;
140
    uint8_t *ptr;
141
142

1438
    if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
143
        AVRational sar = avctx->sample_aspect_ratio;
144
145
        if (sar.num > 65535 || sar.den > 65535) {
146
            if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
147
                av_log(avctx, AV_LOG_WARNING,
148
                    "Cannot store exact aspect ratio %d:%d\n",
149
                    avctx->sample_aspect_ratio.num,
150
                    avctx->sample_aspect_ratio.den);
151
        }
152
153
        /* JFIF header */
154
        put_marker(p, APP0);
155
        put_bits(p, 16, 16);
156
        ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
157
        /* The most significant byte is used for major revisions, the least
158
         * significant byte for minor revisions. Version 1.02 is the current
159
         * released revision. */
160
        put_bits(p, 16, 0x0102);
161
        put_bits(p,  8, 0);              /* units type: 0 - aspect ratio */
162
        put_bits(p, 16, sar.num);
163
        put_bits(p, 16, sar.den);
164
        put_bits(p, 8, 0); /* thumbnail width */
165
        put_bits(p, 8, 0); /* thumbnail height */
166
    }
167
168
    /* comment */
169
1438
    if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
170
        put_marker(p, COM);
171
        flush_put_bits(p);
172
        ptr = put_bits_ptr(p);
173
        put_bits(p, 16, 0); /* patched later */
174
        ff_put_string(p, LIBAVCODEC_IDENT, 1);
175
        size = strlen(LIBAVCODEC_IDENT)+3;
176
        AV_WB16(ptr, size);
177
    }
178
179
1438
    if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
180
1238
          avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
181

1438
          avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
182
1238
        || avctx->color_range == AVCOL_RANGE_MPEG) {
183
200
        put_marker(p, COM);
184
200
        flush_put_bits(p);
185
200
        ptr = put_bits_ptr(p);
186
200
        put_bits(p, 16, 0); /* patched later */
187
200
        ff_put_string(p, "CS=ITU601", 1);
188
200
        size = strlen("CS=ITU601")+3;
189
200
        AV_WB16(ptr, size);
190
    }
191
1438
}
192
193
1642
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
194
{
195
    int chroma_h_shift, chroma_v_shift;
196
197
1642
    av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
198
                                     &chroma_v_shift);
199
1642
    if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
200
204
        (   avctx->pix_fmt == AV_PIX_FMT_BGR0
201
204
         || avctx->pix_fmt == AV_PIX_FMT_BGRA
202
204
         || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
203
        vsample[0] = hsample[0] =
204
        vsample[1] = hsample[1] =
205
        vsample[2] = hsample[2] =
206
        vsample[3] = hsample[3] = 1;
207

1642
    } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
208
200
        vsample[0] = vsample[1] = vsample[2] = 2;
209
200
        hsample[0] = hsample[1] = hsample[2] = 1;
210
    } else {
211
1442
        vsample[0] = 2;
212
1442
        vsample[1] = 2 >> chroma_v_shift;
213
1442
        vsample[2] = 2 >> chroma_v_shift;
214
1442
        hsample[0] = 2;
215
1442
        hsample[1] = 2 >> chroma_h_shift;
216
1442
        hsample[2] = 2 >> chroma_h_shift;
217
    }
218
1642
}
219
220
1638
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
221
                                    ScanTable *intra_scantable, int pred,
222
                                    uint16_t luma_intra_matrix[64],
223
                                    uint16_t chroma_intra_matrix[64])
224
{
225

1638
    const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
226
    int hsample[4], vsample[4];
227
    int i;
228
1638
    int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
229
1638
    int chroma_matrix = !!memcmp(luma_intra_matrix,
230
                                 chroma_intra_matrix,
231
                                 sizeof(luma_intra_matrix[0])*64);
232
233
1638
    ff_mjpeg_init_hvsample(avctx, hsample, vsample);
234
235
1638
    put_marker(pb, SOI);
236
237
    // hack for AMV mjpeg format
238
1638
    if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
239
240
1438
    jpeg_put_comments(avctx, pb);
241
242
1438
    jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
243
244
1438
    switch (avctx->codec_id) {
245
1238
    case AV_CODEC_ID_MJPEG:  put_marker(pb, SOF0 ); break;
246
200
    case AV_CODEC_ID_LJPEG:  put_marker(pb, SOF3 ); break;
247
    default: av_assert0(0);
248
    }
249
250
1438
    put_bits(pb, 16, 17);
251

1438
    if (lossless && (  avctx->pix_fmt == AV_PIX_FMT_BGR0
252
200
                    || avctx->pix_fmt == AV_PIX_FMT_BGRA
253
200
                    || avctx->pix_fmt == AV_PIX_FMT_BGR24))
254
        put_bits(pb, 8, 9); /* 9 bits/component RCT */
255
    else
256
1438
        put_bits(pb, 8, 8); /* 8 bits/component */
257
1438
    put_bits(pb, 16, avctx->height);
258
1438
    put_bits(pb, 16, avctx->width);
259
1438
    put_bits(pb, 8, components); /* 3 or 4 components */
260
261
    /* Y component */
262
1438
    put_bits(pb, 8, 1); /* component number */
263
1438
    put_bits(pb, 4, hsample[0]); /* H factor */
264
1438
    put_bits(pb, 4, vsample[0]); /* V factor */
265
1438
    put_bits(pb, 8, 0); /* select matrix */
266
267
    /* Cb component */
268
1438
    put_bits(pb, 8, 2); /* component number */
269
1438
    put_bits(pb, 4, hsample[1]); /* H factor */
270
1438
    put_bits(pb, 4, vsample[1]); /* V factor */
271
1438
    put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
272
273
    /* Cr component */
274
1438
    put_bits(pb, 8, 3); /* component number */
275
1438
    put_bits(pb, 4, hsample[2]); /* H factor */
276
1438
    put_bits(pb, 4, vsample[2]); /* V factor */
277
1438
    put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
278
279
1438
    if (components == 4) {
280
        put_bits(pb, 8, 4); /* component number */
281
        put_bits(pb, 4, hsample[3]); /* H factor */
282
        put_bits(pb, 4, vsample[3]); /* V factor */
283
        put_bits(pb, 8, 0); /* select matrix */
284
    }
285
286
    /* scan header */
287
1438
    put_marker(pb, SOS);
288
1438
    put_bits(pb, 16, 6 + 2*components); /* length */
289
1438
    put_bits(pb, 8, components); /* 3 components */
290
291
    /* Y component */
292
1438
    put_bits(pb, 8, 1); /* index */
293
1438
    put_bits(pb, 4, 0); /* DC huffman table index */
294
1438
    put_bits(pb, 4, 0); /* AC huffman table index */
295
296
    /* Cb component */
297
1438
    put_bits(pb, 8, 2); /* index */
298
1438
    put_bits(pb, 4, 1); /* DC huffman table index */
299
1438
    put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
300
301
    /* Cr component */
302
1438
    put_bits(pb, 8, 3); /* index */
303
1438
    put_bits(pb, 4, 1); /* DC huffman table index */
304
1438
    put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
305
306
1438
    if (components == 4) {
307
        /* Alpha component */
308
        put_bits(pb, 8, 4); /* index */
309
        put_bits(pb, 4, 0); /* DC huffman table index */
310
        put_bits(pb, 4, 0); /* AC huffman table index */
311
    }
312
313
1438
    put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
314
315
1438
    switch (avctx->codec_id) {
316
1238
    case AV_CODEC_ID_MJPEG:  put_bits(pb, 8, 63); break; /* Se (not used) */
317
200
    case AV_CODEC_ID_LJPEG:  put_bits(pb, 8,  0); break; /* not used */
318
    default: av_assert0(0);
319
    }
320
321
1438
    put_bits(pb, 8, 0); /* Ah/Al (not used) */
322
323
1638
end:
324
1638
    if (!lossless) {
325
1438
        MpegEncContext *s = avctx->priv_data;
326
1438
        av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
327
328
1438
        s->esc_pos = put_bytes_count(pb, 0);
329
1438
        for(i=1; i<s->slice_context_count; i++)
330
            s->thread_context[i]->esc_pos = 0;
331
    }
332
1638
}
333
334
1638
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
335
{
336
    int size;
337
    int i, ff_count;
338
1638
    uint8_t *buf = pb->buf + start;
339
1638
    int align= (-(size_t)(buf))&3;
340
1638
    int pad = (-put_bits_count(pb))&7;
341
342
1638
    if (pad)
343
1419
        put_bits(pb, pad, (1<<pad)-1);
344
345
1638
    flush_put_bits(pb);
346
1638
    size = put_bytes_output(pb) - start;
347
348
1638
    ff_count=0;
349

4487
    for(i=0; i<size && i<align; i++){
350
2849
        if(buf[i]==0xFF) ff_count++;
351
    }
352
2331275
    for(; i<size-15; i+=16){
353
        int acc, v;
354
355
2329637
        v= *(uint32_t*)(&buf[i]);
356
2329637
        acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
357
2329637
        v= *(uint32_t*)(&buf[i+4]);
358
2329637
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
359
2329637
        v= *(uint32_t*)(&buf[i+8]);
360
2329637
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
361
2329637
        v= *(uint32_t*)(&buf[i+12]);
362
2329637
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
363
364
2329637
        acc>>=4;
365
2329637
        acc+= (acc>>16);
366
2329637
        acc+= (acc>>8);
367
2329637
        ff_count+= acc&0xFF;
368
    }
369
13684
    for(; i<size; i++){
370
12046
        if(buf[i]==0xFF) ff_count++;
371
    }
372
373
1638
    if(ff_count==0) return;
374
375
1607
    flush_put_bits(pb);
376
1607
    skip_put_bytes(pb, ff_count);
377
378
36521623
    for(i=size-1; ff_count; i--){
379
36520016
        int v= buf[i];
380
381
36520016
        if(v==0xFF){
382
138316
            buf[i+ff_count]= 0;
383
138316
            ff_count--;
384
        }
385
386
36520016
        buf[i+ff_count]= v;
387
    }
388
}
389
390
/* isn't this function nicer than the one in the libjpeg ? */
391
5080
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
392
                                  const uint8_t *bits_table,
393
                                  const uint8_t *val_table)
394
{
395
    int k, code;
396
397
5080
    k = 0;
398
5080
    code = 0;
399
86360
    for (int i = 1; i <= 16; i++) {
400
81280
        int nb = bits_table[i];
401
201399
        for (int j = 0; j < nb; j++) {
402
120119
            int sym = val_table[k++];
403
120119
            huff_size[sym] = i;
404
120119
            huff_code[sym] = code;
405
120119
            code++;
406
        }
407
81280
        code <<= 1;
408
    }
409
5080
}
410
411
1638
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
412
{
413
    av_assert1((header_bits & 7) == 0);
414
415
1638
    put_marker(pb, EOI);
416
1638
}
417
418
23255400
void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
419
                        uint8_t *huff_size, uint16_t *huff_code)
420
{
421
    int mant, nbits;
422
423
23255400
    if (val == 0) {
424
2701595
        put_bits(pb, huff_size[0], huff_code[0]);
425
    } else {
426
20553805
        mant = val;
427
20553805
        if (val < 0) {
428
8922845
            val = -val;
429
8922845
            mant--;
430
        }
431
432
20553805
        nbits= av_log2_16bit(val) + 1;
433
434
20553805
        put_bits(pb, huff_size[nbits], huff_code[nbits]);
435
436
20553805
        put_sbits(pb, nbits, mant);
437
    }
438
23255400
}
439
440
34
int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx)
441
{
442
34
    if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
443
26
        avctx->color_range != AVCOL_RANGE_JPEG &&
444
        (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
445
         avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
446
         avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
447
         avctx->color_range == AVCOL_RANGE_MPEG)) {
448
        av_log(avctx, AV_LOG_ERROR,
449
               "Non full-range YUV is non-standard, set strict_std_compliance "
450
               "to at most unofficial to use it.\n");
451
        return AVERROR(EINVAL);
452
    }
453
34
    return 0;
454
}