| 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 | #include "version.h" | ||
| 37 | |||
| 38 | /* table_class: 0 = DC coef, 1 = AC coefs */ | ||
| 39 | 5756 | static int put_huffman_table(PutBitContext *p, int table_class, int table_id, | |
| 40 | const uint8_t *bits_table, const uint8_t *value_table) | ||
| 41 | { | ||
| 42 | 5756 | int n = 0; | |
| 43 | |||
| 44 | 5756 | put_bits(p, 4, table_class); | |
| 45 | 5756 | put_bits(p, 4, table_id); | |
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 92096 times.
✓ Branch 1 taken 5756 times.
|
97852 | for (int i = 1; i <= 16; i++) { |
| 48 | 92096 | n += bits_table[i]; | |
| 49 | 92096 | put_bits(p, 8, bits_table[i]); | |
| 50 | } | ||
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 284002 times.
✓ Branch 1 taken 5756 times.
|
289758 | for (int i = 0; i < n; i++) |
| 53 | 284002 | put_bits(p, 8, value_table[i]); | |
| 54 | |||
| 55 | 5756 | return n + 17; | |
| 56 | } | ||
| 57 | |||
| 58 | 1439 | static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p, | |
| 59 | const MJpegContext *m, | ||
| 60 | const uint8_t intra_matrix_permutation[64], | ||
| 61 | const uint16_t luma_intra_matrix[64], | ||
| 62 | const uint16_t chroma_intra_matrix[64], | ||
| 63 | int hsample[3], int use_slices, int matrices_differ) | ||
| 64 | { | ||
| 65 | int size; | ||
| 66 | uint8_t *ptr; | ||
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
|
1439 | if (m) { |
| 69 | 1239 | int matrix_count = 1 + matrices_differ; | |
| 70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
|
1239 | if (m->force_duplicated_matrix) |
| 71 | ✗ | matrix_count = 2; | |
| 72 | /* quant matrixes */ | ||
| 73 | 1239 | put_marker(p, DQT); | |
| 74 | 1239 | put_bits(p, 16, 2 + matrix_count * (1 + 64)); | |
| 75 | 1239 | put_bits(p, 4, 0); /* 8 bit precision */ | |
| 76 | 1239 | put_bits(p, 4, 0); /* table 0 */ | |
| 77 |
2/2✓ Branch 0 taken 79296 times.
✓ Branch 1 taken 1239 times.
|
80535 | for (int i = 0; i < 64; i++) { |
| 78 | 79296 | uint8_t j = intra_matrix_permutation[i]; | |
| 79 | 79296 | put_bits(p, 8, luma_intra_matrix[j]); | |
| 80 | } | ||
| 81 | |||
| 82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
|
1239 | if (matrix_count > 1) { |
| 83 | ✗ | put_bits(p, 4, 0); /* 8 bit precision */ | |
| 84 | ✗ | put_bits(p, 4, 1); /* table 1 */ | |
| 85 | ✗ | for (int i = 0; i < 64; i++) { | |
| 86 | ✗ | uint8_t j = intra_matrix_permutation[i]; | |
| 87 | ✗ | put_bits(p, 8, chroma_intra_matrix[j]); | |
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1239 times.
|
1439 | if (use_slices) { |
| 93 | 200 | put_marker(p, DRI); | |
| 94 | 200 | put_bits(p, 16, 4); | |
| 95 | 200 | put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1); | |
| 96 | } | ||
| 97 | |||
| 98 | /* huffman table */ | ||
| 99 | 1439 | put_marker(p, DHT); | |
| 100 | 1439 | flush_put_bits(p); | |
| 101 | 1439 | ptr = put_bits_ptr(p); | |
| 102 | 1439 | put_bits(p, 16, 0); /* patched later */ | |
| 103 | 1439 | size = 2; | |
| 104 | |||
| 105 | // Only MJPEG can have a variable Huffman variable. All other | ||
| 106 | // formats use the default Huffman table. | ||
| 107 |
4/4✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 839 times.
✓ Branch 3 taken 400 times.
|
1439 | if (m && m->huffman == HUFFMAN_TABLE_OPTIMAL) { |
| 108 | 1678 | size += put_huffman_table(p, 0, 0, m->bits_dc_luminance, | |
| 109 | 839 | m->val_dc_luminance); | |
| 110 | 1678 | size += put_huffman_table(p, 0, 1, m->bits_dc_chrominance, | |
| 111 | 839 | m->val_dc_chrominance); | |
| 112 | |||
| 113 | 1678 | size += put_huffman_table(p, 1, 0, m->bits_ac_luminance, | |
| 114 | 839 | m->val_ac_luminance); | |
| 115 | 1678 | size += put_huffman_table(p, 1, 1, m->bits_ac_chrominance, | |
| 116 | 839 | m->val_ac_chrominance); | |
| 117 | } else { | ||
| 118 | 600 | size += put_huffman_table(p, 0, 0, ff_mjpeg_bits_dc_luminance, | |
| 119 | ff_mjpeg_val_dc); | ||
| 120 | 600 | size += put_huffman_table(p, 0, 1, ff_mjpeg_bits_dc_chrominance, | |
| 121 | ff_mjpeg_val_dc); | ||
| 122 | |||
| 123 | 600 | size += put_huffman_table(p, 1, 0, ff_mjpeg_bits_ac_luminance, | |
| 124 | ff_mjpeg_val_ac_luminance); | ||
| 125 | 600 | size += put_huffman_table(p, 1, 1, ff_mjpeg_bits_ac_chrominance, | |
| 126 | ff_mjpeg_val_ac_chrominance); | ||
| 127 | } | ||
| 128 | 1439 | AV_WB16(ptr, size); | |
| 129 | 1439 | } | |
| 130 | |||
| 131 | enum { | ||
| 132 | ICC_HDR_SIZE = 16, /* ICC_PROFILE\0 tag + 4 bytes */ | ||
| 133 | ICC_CHUNK_SIZE = UINT16_MAX - ICC_HDR_SIZE, | ||
| 134 | ICC_MAX_CHUNKS = UINT8_MAX, | ||
| 135 | }; | ||
| 136 | |||
| 137 | 1439 | int ff_mjpeg_add_icc_profile_size(AVCodecContext *avctx, const AVFrame *frame, | |
| 138 | size_t *max_pkt_size) | ||
| 139 | { | ||
| 140 | const AVFrameSideData *sd; | ||
| 141 | size_t new_pkt_size; | ||
| 142 | int nb_chunks; | ||
| 143 | 1439 | sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE); | |
| 144 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1438 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1439 | if (!sd || !sd->size) |
| 145 | 1438 | return 0; | |
| 146 | |||
| 147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (sd->size > ICC_MAX_CHUNKS * ICC_CHUNK_SIZE) { |
| 148 | ✗ | av_log(avctx, AV_LOG_ERROR, "Cannot store %"SIZE_SPECIFIER" byte ICC " | |
| 149 | "profile: too large for JPEG\n", | ||
| 150 | ✗ | sd->size); | |
| 151 | ✗ | return AVERROR_INVALIDDATA; | |
| 152 | } | ||
| 153 | |||
| 154 | 1 | nb_chunks = (sd->size + ICC_CHUNK_SIZE - 1) / ICC_CHUNK_SIZE; | |
| 155 | 1 | new_pkt_size = *max_pkt_size + nb_chunks * (UINT16_MAX + 2 /* APP2 marker */); | |
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (new_pkt_size < *max_pkt_size) /* overflow */ |
| 157 | ✗ | return AVERROR_INVALIDDATA; | |
| 158 | 1 | *max_pkt_size = new_pkt_size; | |
| 159 | 1 | return 0; | |
| 160 | } | ||
| 161 | |||
| 162 | 1439 | static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p, | |
| 163 | const AVFrame *frame) | ||
| 164 | { | ||
| 165 | 1439 | const AVFrameSideData *sd = NULL; | |
| 166 | int size; | ||
| 167 | uint8_t *ptr; | ||
| 168 | |||
| 169 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1438 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1439 | if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) { |
| 170 | 1 | AVRational sar = avctx->sample_aspect_ratio; | |
| 171 | |||
| 172 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (sar.num > 65535 || sar.den > 65535) { |
| 173 | ✗ | if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535)) | |
| 174 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 175 | "Cannot store exact aspect ratio %d:%d\n", | ||
| 176 | avctx->sample_aspect_ratio.num, | ||
| 177 | avctx->sample_aspect_ratio.den); | ||
| 178 | } | ||
| 179 | |||
| 180 | /* JFIF header */ | ||
| 181 | 1 | put_marker(p, APP0); | |
| 182 | 1 | put_bits(p, 16, 16); | |
| 183 | 1 | ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */ | |
| 184 | /* The most significant byte is used for major revisions, the least | ||
| 185 | * significant byte for minor revisions. Version 1.02 is the current | ||
| 186 | * released revision. */ | ||
| 187 | 1 | put_bits(p, 16, 0x0102); | |
| 188 | 1 | put_bits(p, 8, 0); /* units type: 0 - aspect ratio */ | |
| 189 | 1 | put_bits(p, 16, sar.num); | |
| 190 | 1 | put_bits(p, 16, sar.den); | |
| 191 | 1 | put_bits(p, 8, 0); /* thumbnail width */ | |
| 192 | 1 | put_bits(p, 8, 0); /* thumbnail height */ | |
| 193 | } | ||
| 194 | |||
| 195 | /* ICC profile */ | ||
| 196 | 1439 | sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE); | |
| 197 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1438 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1439 | if (sd && sd->size) { |
| 198 | 1 | const int nb_chunks = (sd->size + ICC_CHUNK_SIZE - 1) / ICC_CHUNK_SIZE; | |
| 199 | 1 | const uint8_t *data = sd->data; | |
| 200 | 1 | size_t remaining = sd->size; | |
| 201 | /* must already be checked by the packat allocation code */ | ||
| 202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(remaining <= ICC_MAX_CHUNKS * ICC_CHUNK_SIZE); |
| 203 | 1 | flush_put_bits(p); | |
| 204 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (int i = 0; i < nb_chunks; i++) { |
| 205 | 1 | size = FFMIN(remaining, ICC_CHUNK_SIZE); | |
| 206 | av_assert1(size > 0); | ||
| 207 | 1 | ptr = put_bits_ptr(p); | |
| 208 | 1 | ptr[0] = 0xff; /* chunk marker, not part of ICC_HDR_SIZE */ | |
| 209 | 1 | ptr[1] = APP2; | |
| 210 | 1 | AV_WB16(ptr+2, size + ICC_HDR_SIZE); | |
| 211 | 1 | AV_WL32(ptr+4, MKTAG('I','C','C','_')); | |
| 212 | 1 | AV_WL32(ptr+8, MKTAG('P','R','O','F')); | |
| 213 | 1 | AV_WL32(ptr+12, MKTAG('I','L','E','\0')); | |
| 214 | 1 | ptr[16] = i+1; | |
| 215 | 1 | ptr[17] = nb_chunks; | |
| 216 | 1 | memcpy(&ptr[18], data, size); | |
| 217 | 1 | skip_put_bytes(p, size + ICC_HDR_SIZE + 2); | |
| 218 | 1 | remaining -= size; | |
| 219 | 1 | data += size; | |
| 220 | } | ||
| 221 | av_assert1(!remaining); | ||
| 222 | } | ||
| 223 | |||
| 224 | /* comment */ | ||
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1439 times.
|
1439 | if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) { |
| 226 | ✗ | put_marker(p, COM); | |
| 227 | ✗ | flush_put_bits(p); | |
| 228 | ✗ | ptr = put_bits_ptr(p); | |
| 229 | ✗ | put_bits(p, 16, 0); /* patched later */ | |
| 230 | ✗ | ff_put_string(p, LIBAVCODEC_IDENT, 1); | |
| 231 | ✗ | size = strlen(LIBAVCODEC_IDENT)+3; | |
| 232 | ✗ | AV_WB16(ptr, size); | |
| 233 | } | ||
| 234 | |||
| 235 |
2/2✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 225 times.
|
1439 | if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P || |
| 236 |
1/2✓ Branch 0 taken 1214 times.
✗ Branch 1 not taken.
|
1214 | avctx->pix_fmt == AV_PIX_FMT_YUV422P || |
| 237 |
4/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1213 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 200 times.
|
1439 | avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG) |
| 238 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
|
1239 | || avctx->color_range == AVCOL_RANGE_MPEG) { |
| 239 | 200 | put_marker(p, COM); | |
| 240 | 200 | flush_put_bits(p); | |
| 241 | 200 | ptr = put_bits_ptr(p); | |
| 242 | 200 | put_bits(p, 16, 0); /* patched later */ | |
| 243 | 200 | ff_put_string(p, "CS=ITU601", 1); | |
| 244 | 200 | size = strlen("CS=ITU601")+3; | |
| 245 | 200 | AV_WB16(ptr, size); | |
| 246 | } | ||
| 247 | 1439 | } | |
| 248 | |||
| 249 | 1643 | void ff_mjpeg_init_hvsample(const AVCodecContext *avctx, int hsample[4], int vsample[4]) | |
| 250 | { | ||
| 251 |
2/2✓ Branch 0 taken 204 times.
✓ Branch 1 taken 1439 times.
|
1643 | if (avctx->codec_id == AV_CODEC_ID_LJPEG && |
| 252 |
1/2✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
|
204 | ( avctx->pix_fmt == AV_PIX_FMT_BGR0 |
| 253 |
1/2✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
|
204 | || avctx->pix_fmt == AV_PIX_FMT_BGRA |
| 254 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
|
204 | || avctx->pix_fmt == AV_PIX_FMT_BGR24)) { |
| 255 | ✗ | vsample[0] = hsample[0] = | |
| 256 | ✗ | vsample[1] = hsample[1] = | |
| 257 | ✗ | vsample[2] = hsample[2] = | |
| 258 | ✗ | vsample[3] = hsample[3] = 1; | |
| 259 |
4/4✓ Branch 0 taken 1642 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 1442 times.
|
1643 | } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) { |
| 260 | 201 | vsample[0] = vsample[1] = vsample[2] = 2; | |
| 261 | 201 | hsample[0] = hsample[1] = hsample[2] = 1; | |
| 262 | } else { | ||
| 263 | int chroma_h_shift, chroma_v_shift; | ||
| 264 | 1442 | av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, | |
| 265 | &chroma_v_shift); | ||
| 266 | 1442 | vsample[0] = 2; | |
| 267 | 1442 | vsample[1] = 2 >> chroma_v_shift; | |
| 268 | 1442 | vsample[2] = 2 >> chroma_v_shift; | |
| 269 | 1442 | hsample[0] = 2; | |
| 270 | 1442 | hsample[1] = 2 >> chroma_h_shift; | |
| 271 | 1442 | hsample[2] = 2 >> chroma_h_shift; | |
| 272 | } | ||
| 273 | 1643 | } | |
| 274 | |||
| 275 | 1639 | void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, | |
| 276 | const AVFrame *frame, const struct MJpegContext *m, | ||
| 277 | const uint8_t intra_matrix_permutation[64], int pred, | ||
| 278 | const uint16_t luma_intra_matrix[64], | ||
| 279 | const uint16_t chroma_intra_matrix[64], | ||
| 280 | int use_slices) | ||
| 281 | { | ||
| 282 | 1639 | const int lossless = !m; | |
| 283 | int hsample[4], vsample[4]; | ||
| 284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1639 times.
|
1639 | int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA); |
| 285 | int chroma_matrix; | ||
| 286 | |||
| 287 | 1639 | ff_mjpeg_init_hvsample(avctx, hsample, vsample); | |
| 288 | |||
| 289 | 1639 | put_marker(pb, SOI); | |
| 290 | |||
| 291 | // hack for AMV mjpeg format | ||
| 292 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1439 times.
|
1639 | if (avctx->codec_id == AV_CODEC_ID_AMV) |
| 293 | 200 | return; | |
| 294 | |||
| 295 | 1439 | jpeg_put_comments(avctx, pb, frame); | |
| 296 | |||
| 297 |
3/4✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1239 times.
|
1439 | chroma_matrix = !lossless && !!memcmp(luma_intra_matrix, |
| 298 | chroma_intra_matrix, | ||
| 299 | sizeof(luma_intra_matrix[0]) * 64); | ||
| 300 | 1439 | jpeg_table_header(avctx, pb, m, intra_matrix_permutation, | |
| 301 | luma_intra_matrix, chroma_intra_matrix, hsample, | ||
| 302 | use_slices, chroma_matrix); | ||
| 303 | |||
| 304 |
2/3✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
|
1439 | switch (avctx->codec_id) { |
| 305 | 1239 | case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break; | |
| 306 | 200 | case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break; | |
| 307 | ✗ | default: av_unreachable("ff_mjpeg_encode_picture_header only called by " | |
| 308 | "AMV, LJPEG, MJPEG and the former has been ruled out"); | ||
| 309 | } | ||
| 310 | |||
| 311 | 1439 | put_bits(pb, 16, 8 + 3 * components); | |
| 312 |
3/4✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1239 times.
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
|
1439 | if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0 |
| 313 |
1/2✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
|
200 | || avctx->pix_fmt == AV_PIX_FMT_BGRA |
| 314 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
|
200 | || avctx->pix_fmt == AV_PIX_FMT_BGR24)) |
| 315 | ✗ | put_bits(pb, 8, 9); /* 9 bits/component RCT */ | |
| 316 | else | ||
| 317 | 1439 | put_bits(pb, 8, 8); /* 8 bits/component */ | |
| 318 | 1439 | put_bits(pb, 16, avctx->height); | |
| 319 | 1439 | put_bits(pb, 16, avctx->width); | |
| 320 | 1439 | put_bits(pb, 8, components); /* 3 or 4 components */ | |
| 321 | |||
| 322 | /* Y component */ | ||
| 323 | 1439 | put_bits(pb, 8, 1); /* component number */ | |
| 324 | 1439 | put_bits(pb, 4, hsample[0]); /* H factor */ | |
| 325 | 1439 | put_bits(pb, 4, vsample[0]); /* V factor */ | |
| 326 | 1439 | put_bits(pb, 8, 0); /* select matrix */ | |
| 327 | |||
| 328 | /* Cb component */ | ||
| 329 | 1439 | put_bits(pb, 8, 2); /* component number */ | |
| 330 | 1439 | put_bits(pb, 4, hsample[1]); /* H factor */ | |
| 331 | 1439 | put_bits(pb, 4, vsample[1]); /* V factor */ | |
| 332 |
2/2✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
|
1439 | put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */ |
| 333 | |||
| 334 | /* Cr component */ | ||
| 335 | 1439 | put_bits(pb, 8, 3); /* component number */ | |
| 336 | 1439 | put_bits(pb, 4, hsample[2]); /* H factor */ | |
| 337 | 1439 | put_bits(pb, 4, vsample[2]); /* V factor */ | |
| 338 |
2/2✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
|
1439 | put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */ |
| 339 | |||
| 340 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1439 times.
|
1439 | if (components == 4) { |
| 341 | ✗ | put_bits(pb, 8, 4); /* component number */ | |
| 342 | ✗ | put_bits(pb, 4, hsample[3]); /* H factor */ | |
| 343 | ✗ | put_bits(pb, 4, vsample[3]); /* V factor */ | |
| 344 | ✗ | put_bits(pb, 8, 0); /* select matrix */ | |
| 345 | } | ||
| 346 | |||
| 347 | /* scan header */ | ||
| 348 | 1439 | put_marker(pb, SOS); | |
| 349 | 1439 | put_bits(pb, 16, 6 + 2*components); /* length */ | |
| 350 | 1439 | put_bits(pb, 8, components); /* 3 components */ | |
| 351 | |||
| 352 | /* Y component */ | ||
| 353 | 1439 | put_bits(pb, 8, 1); /* index */ | |
| 354 | 1439 | put_bits(pb, 4, 0); /* DC huffman table index */ | |
| 355 | 1439 | put_bits(pb, 4, 0); /* AC huffman table index */ | |
| 356 | |||
| 357 | /* Cb component */ | ||
| 358 | 1439 | put_bits(pb, 8, 2); /* index */ | |
| 359 | 1439 | put_bits(pb, 4, 1); /* DC huffman table index */ | |
| 360 | 1439 | put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */ | |
| 361 | |||
| 362 | /* Cr component */ | ||
| 363 | 1439 | put_bits(pb, 8, 3); /* index */ | |
| 364 | 1439 | put_bits(pb, 4, 1); /* DC huffman table index */ | |
| 365 | 1439 | put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */ | |
| 366 | |||
| 367 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1439 times.
|
1439 | if (components == 4) { |
| 368 | /* Alpha component */ | ||
| 369 | ✗ | put_bits(pb, 8, 4); /* index */ | |
| 370 | ✗ | put_bits(pb, 4, 0); /* DC huffman table index */ | |
| 371 | ✗ | put_bits(pb, 4, 0); /* AC huffman table index */ | |
| 372 | } | ||
| 373 | |||
| 374 | 1439 | put_bits(pb, 8, pred); /* Ss (not used); pred only nonzero for LJPEG */ | |
| 375 | |||
| 376 |
2/3✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
|
1439 | switch (avctx->codec_id) { |
| 377 | 1239 | case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */ | |
| 378 | 200 | case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */ | |
| 379 | ✗ | default: av_unreachable("Only LJPEG, MJPEG possible here"); | |
| 380 | } | ||
| 381 | |||
| 382 | 1439 | put_bits(pb, 8, 0); /* Ah/Al (not used) */ | |
| 383 | } | ||
| 384 | |||
| 385 | 4289 | void ff_mjpeg_escape_FF(PutBitContext *pb, int start) | |
| 386 | { | ||
| 387 | int size; | ||
| 388 | int i, ff_count; | ||
| 389 | 4289 | uint8_t *buf = pb->buf + start; | |
| 390 | 4289 | int align= (-(size_t)(buf))&3; | |
| 391 | 4289 | int pad = (-put_bits_count(pb))&7; | |
| 392 | |||
| 393 |
2/2✓ Branch 0 taken 3739 times.
✓ Branch 1 taken 550 times.
|
4289 | if (pad) |
| 394 | 3739 | put_bits(pb, pad, (1<<pad)-1); | |
| 395 | |||
| 396 | 4289 | flush_put_bits(pb); | |
| 397 | 4289 | size = put_bytes_output(pb) - start; | |
| 398 | |||
| 399 | 4289 | ff_count=0; | |
| 400 |
3/4✓ Branch 0 taken 11075 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6786 times.
✓ Branch 3 taken 4289 times.
|
11075 | for(i=0; i<size && i<align; i++){ |
| 401 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 6773 times.
|
6786 | if(buf[i]==0xFF) ff_count++; |
| 402 | } | ||
| 403 |
2/2✓ Branch 0 taken 2344615 times.
✓ Branch 1 taken 4289 times.
|
2348904 | for(; i<size-15; i+=16){ |
| 404 | int acc, v; | ||
| 405 | |||
| 406 | 2344615 | v= *(uint32_t*)(&buf[i]); | |
| 407 | 2344615 | acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; | |
| 408 | 2344615 | v= *(uint32_t*)(&buf[i+4]); | |
| 409 | 2344615 | acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; | |
| 410 | 2344615 | v= *(uint32_t*)(&buf[i+8]); | |
| 411 | 2344615 | acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; | |
| 412 | 2344615 | v= *(uint32_t*)(&buf[i+12]); | |
| 413 | 2344615 | acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; | |
| 414 | |||
| 415 | 2344615 | acc>>=4; | |
| 416 | 2344615 | acc+= (acc>>16); | |
| 417 | 2344615 | acc+= (acc>>8); | |
| 418 | 2344615 | ff_count+= acc&0xFF; | |
| 419 | } | ||
| 420 |
2/2✓ Branch 0 taken 31845 times.
✓ Branch 1 taken 4289 times.
|
36134 | for(; i<size; i++){ |
| 421 |
2/2✓ Branch 0 taken 385 times.
✓ Branch 1 taken 31460 times.
|
31845 | if(buf[i]==0xFF) ff_count++; |
| 422 | } | ||
| 423 | |||
| 424 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 4220 times.
|
4289 | if(ff_count==0) return; |
| 425 | |||
| 426 | 4220 | skip_put_bytes(pb, ff_count); | |
| 427 | |||
| 428 |
2/2✓ Branch 0 taken 36418724 times.
✓ Branch 1 taken 4220 times.
|
36422944 | for(i=size-1; ff_count; i--){ |
| 429 | 36418724 | int v= buf[i]; | |
| 430 | |||
| 431 |
2/2✓ Branch 0 taken 165648 times.
✓ Branch 1 taken 36253076 times.
|
36418724 | if(v==0xFF){ |
| 432 | 165648 | buf[i+ff_count]= 0; | |
| 433 | 165648 | ff_count--; | |
| 434 | } | ||
| 435 | |||
| 436 | 36418724 | buf[i+ff_count]= v; | |
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | /* isn't this function nicer than the one in the libjpeg ? */ | ||
| 441 | 3488 | void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, | |
| 442 | const uint8_t *bits_table, | ||
| 443 | const uint8_t *val_table) | ||
| 444 | { | ||
| 445 | int k, code; | ||
| 446 | |||
| 447 | 3488 | k = 0; | |
| 448 | 3488 | code = 0; | |
| 449 |
2/2✓ Branch 0 taken 55808 times.
✓ Branch 1 taken 3488 times.
|
59296 | for (int i = 1; i <= 16; i++) { |
| 450 | 55808 | int nb = bits_table[i]; | |
| 451 |
2/2✓ Branch 0 taken 86086 times.
✓ Branch 1 taken 55808 times.
|
141894 | for (int j = 0; j < nb; j++) { |
| 452 | 86086 | int sym = val_table[k++]; | |
| 453 | 86086 | huff_size[sym] = i; | |
| 454 | 86086 | huff_code[sym] = code; | |
| 455 | 86086 | code++; | |
| 456 | } | ||
| 457 | 55808 | code <<= 1; | |
| 458 | } | ||
| 459 | 3488 | } | |
| 460 | |||
| 461 | 1639 | void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits) | |
| 462 | { | ||
| 463 | av_assert1((header_bits & 7) == 0); | ||
| 464 | |||
| 465 | 1639 | put_marker(pb, EOI); | |
| 466 | 1639 | } | |
| 467 | |||
| 468 | 23973600 | void ff_mjpeg_encode_dc(PutBitContext *pb, int val, | |
| 469 | const uint8_t huff_size[], const uint16_t huff_code[]) | ||
| 470 | { | ||
| 471 | int mant, nbits; | ||
| 472 | |||
| 473 |
2/2✓ Branch 0 taken 2724153 times.
✓ Branch 1 taken 21249447 times.
|
23973600 | if (val == 0) { |
| 474 | 2724153 | put_bits(pb, huff_size[0], huff_code[0]); | |
| 475 | } else { | ||
| 476 | 21249447 | mant = val; | |
| 477 |
2/2✓ Branch 0 taken 9257064 times.
✓ Branch 1 taken 11992383 times.
|
21249447 | if (val < 0) { |
| 478 | 9257064 | val = -val; | |
| 479 | 9257064 | mant--; | |
| 480 | } | ||
| 481 | |||
| 482 | 21249447 | nbits= av_log2_16bit(val) + 1; | |
| 483 | |||
| 484 | 21249447 | put_bits(pb, huff_size[nbits], huff_code[nbits]); | |
| 485 | |||
| 486 | 21249447 | put_sbits(pb, nbits, mant); | |
| 487 | } | ||
| 488 | 23973600 | } | |
| 489 | |||
| 490 | 35 | int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx) | |
| 491 | { | ||
| 492 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 8 times.
|
35 | if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && |
| 493 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | avctx->color_range != AVCOL_RANGE_JPEG && |
| 494 | ✗ | (avctx->pix_fmt == AV_PIX_FMT_YUV420P || | |
| 495 | ✗ | avctx->pix_fmt == AV_PIX_FMT_YUV422P || | |
| 496 | ✗ | avctx->pix_fmt == AV_PIX_FMT_YUV444P || | |
| 497 | ✗ | avctx->color_range == AVCOL_RANGE_MPEG)) { | |
| 498 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 499 | "Non full-range YUV is non-standard, set strict_std_compliance " | ||
| 500 | "to at most unofficial to use it.\n"); | ||
| 501 | ✗ | return AVERROR(EINVAL); | |
| 502 | } | ||
| 503 | 35 | return 0; | |
| 504 | } | ||
| 505 |