| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @file | ||
| 21 | * misc image utilities | ||
| 22 | */ | ||
| 23 | |||
| 24 | #include "avassert.h" | ||
| 25 | #include "common.h" | ||
| 26 | #include "imgutils.h" | ||
| 27 | #include "imgutils_internal.h" | ||
| 28 | #include "intreadwrite.h" | ||
| 29 | #include "log.h" | ||
| 30 | #include "mathematics.h" | ||
| 31 | #include "mem.h" | ||
| 32 | #include "pixdesc.h" | ||
| 33 | #include "rational.h" | ||
| 34 | |||
| 35 | 694461 | void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], | |
| 36 | const AVPixFmtDescriptor *pixdesc) | ||
| 37 | { | ||
| 38 | int i; | ||
| 39 | 694461 | memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0])); | |
| 40 |
2/2✓ Branch 0 taken 693782 times.
✓ Branch 1 taken 679 times.
|
694461 | if (max_pixstep_comps) |
| 41 | 693782 | memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0])); | |
| 42 | |||
| 43 |
2/2✓ Branch 0 taken 2777844 times.
✓ Branch 1 taken 694461 times.
|
3472305 | for (i = 0; i < 4; i++) { |
| 44 | 2777844 | const AVComponentDescriptor *comp = &(pixdesc->comp[i]); | |
| 45 |
2/2✓ Branch 0 taken 1900944 times.
✓ Branch 1 taken 876900 times.
|
2777844 | if (comp->step > max_pixsteps[comp->plane]) { |
| 46 | 1900944 | max_pixsteps[comp->plane] = comp->step; | |
| 47 |
2/2✓ Branch 0 taken 1899319 times.
✓ Branch 1 taken 1625 times.
|
1900944 | if (max_pixstep_comps) |
| 48 | 1899319 | max_pixstep_comps[comp->plane] = i; | |
| 49 | } | ||
| 50 | } | ||
| 51 | 694461 | } | |
| 52 | |||
| 53 | static inline | ||
| 54 | 1963517 | int image_get_linesize(int width, int plane, | |
| 55 | int max_step, int max_step_comp, | ||
| 56 | const AVPixFmtDescriptor *desc) | ||
| 57 | { | ||
| 58 | int s, shifted_w, linesize; | ||
| 59 | |||
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1963517 times.
|
1963517 | if (!desc) |
| 61 | ✗ | return AVERROR(EINVAL); | |
| 62 | |||
| 63 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1963517 times.
|
1963517 | if (width < 0) |
| 64 | ✗ | return AVERROR(EINVAL); | |
| 65 |
4/4✓ Branch 0 taken 1525050 times.
✓ Branch 1 taken 438467 times.
✓ Branch 2 taken 406069 times.
✓ Branch 3 taken 1118981 times.
|
1963517 | s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0; |
| 66 | 1963517 | shifted_w = ((width + (1 << s) - 1)) >> s; | |
| 67 |
3/4✓ Branch 0 taken 1963493 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1963493 times.
|
1963517 | if (shifted_w && max_step > INT_MAX / shifted_w) |
| 68 | ✗ | return AVERROR(EINVAL); | |
| 69 | 1963517 | linesize = max_step * shifted_w; | |
| 70 | |||
| 71 |
2/2✓ Branch 0 taken 5986 times.
✓ Branch 1 taken 1957531 times.
|
1963517 | if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) |
| 72 | 5986 | linesize = (linesize + 7) >> 3; | |
| 73 | 1963517 | return linesize; | |
| 74 | } | ||
| 75 | |||
| 76 | 897843 | int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane) | |
| 77 | { | ||
| 78 | 897843 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 79 | int max_step [4]; /* max pixel step for each plane */ | ||
| 80 | int max_step_comp[4]; /* the component for each plane which has the max pixel step */ | ||
| 81 | |||
| 82 |
3/4✓ Branch 0 taken 270537 times.
✓ Branch 1 taken 627306 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 270537 times.
|
897843 | if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL) |
| 83 | 627306 | return AVERROR(EINVAL); | |
| 84 | |||
| 85 | 270537 | av_image_fill_max_pixsteps(max_step, max_step_comp, desc); | |
| 86 | 270537 | return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc); | |
| 87 | } | ||
| 88 | |||
| 89 | 423245 | int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width) | |
| 90 | { | ||
| 91 | int i, ret; | ||
| 92 | 423245 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 93 | int max_step [4]; /* max pixel step for each plane */ | ||
| 94 | int max_step_comp[4]; /* the component for each plane which has the max pixel step */ | ||
| 95 | |||
| 96 | 423245 | memset(linesizes, 0, 4*sizeof(linesizes[0])); | |
| 97 | |||
| 98 |
2/4✓ Branch 0 taken 423245 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 423245 times.
|
423245 | if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL) |
| 99 | ✗ | return AVERROR(EINVAL); | |
| 100 | |||
| 101 | 423245 | av_image_fill_max_pixsteps(max_step, max_step_comp, desc); | |
| 102 |
2/2✓ Branch 0 taken 1692980 times.
✓ Branch 1 taken 423245 times.
|
2116225 | for (i = 0; i < 4; i++) { |
| 103 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1692980 times.
|
1692980 | if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0) |
| 104 | ✗ | return ret; | |
| 105 | 1692980 | linesizes[i] = ret; | |
| 106 | } | ||
| 107 | |||
| 108 | 423245 | return 0; | |
| 109 | } | ||
| 110 | |||
| 111 | 300570 | int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt, | |
| 112 | int height, const ptrdiff_t linesizes[4]) | ||
| 113 | { | ||
| 114 | 300570 | int i, has_plane[4] = { 0 }; | |
| 115 | |||
| 116 | 300570 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 117 | 300570 | memset(sizes , 0, sizeof(sizes[0])*4); | |
| 118 | |||
| 119 |
2/4✓ Branch 0 taken 300570 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 300570 times.
|
300570 | if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL) |
| 120 | ✗ | return AVERROR(EINVAL); | |
| 121 | |||
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 300570 times.
|
300570 | if (linesizes[0] > SIZE_MAX / height) |
| 123 | ✗ | return AVERROR(EINVAL); | |
| 124 | 300570 | sizes[0] = linesizes[0] * (size_t)height; | |
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 1059 times.
✓ Branch 1 taken 299511 times.
|
300570 | if (desc->flags & AV_PIX_FMT_FLAG_PAL) { |
| 127 | 1059 | sizes[1] = 256 * 4; /* palette is stored here as 256 32 bits words */ | |
| 128 | 1059 | return 0; | |
| 129 | } | ||
| 130 | |||
| 131 |
2/2✓ Branch 0 taken 1198044 times.
✓ Branch 1 taken 299511 times.
|
1497555 | for (i = 0; i < 4; i++) |
| 132 | 1198044 | has_plane[desc->comp[i].plane] = 1; | |
| 133 | |||
| 134 |
4/4✓ Branch 0 taken 789583 times.
✓ Branch 1 taken 3823 times.
✓ Branch 2 taken 493895 times.
✓ Branch 3 taken 295688 times.
|
793406 | for (i = 1; i < 4 && has_plane[i]; i++) { |
| 135 |
4/4✓ Branch 0 taken 240012 times.
✓ Branch 1 taken 253883 times.
✓ Branch 2 taken 236189 times.
✓ Branch 3 taken 3823 times.
|
493895 | int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0; |
| 136 | 493895 | h = (height + (1 << s) - 1) >> s; | |
| 137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 493895 times.
|
493895 | if (linesizes[i] > SIZE_MAX / h) |
| 138 | ✗ | return AVERROR(EINVAL); | |
| 139 | 493895 | sizes[i] = (size_t)h * linesizes[i]; | |
| 140 | } | ||
| 141 | |||
| 142 | 299511 | return 0; | |
| 143 | } | ||
| 144 | |||
| 145 | 25916 | int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, | |
| 146 | uint8_t *ptr, const int linesizes[4]) | ||
| 147 | { | ||
| 148 | int i, ret; | ||
| 149 | ptrdiff_t linesizes1[4]; | ||
| 150 | size_t sizes[4]; | ||
| 151 | |||
| 152 | 25916 | memset(data , 0, sizeof(data[0])*4); | |
| 153 | |||
| 154 |
2/2✓ Branch 0 taken 103664 times.
✓ Branch 1 taken 25916 times.
|
129580 | for (i = 0; i < 4; i++) |
| 155 | 103664 | linesizes1[i] = linesizes[i]; | |
| 156 | |||
| 157 | 25916 | ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, linesizes1); | |
| 158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25916 times.
|
25916 | if (ret < 0) |
| 159 | ✗ | return ret; | |
| 160 | |||
| 161 | 25916 | ret = 0; | |
| 162 |
2/2✓ Branch 0 taken 103664 times.
✓ Branch 1 taken 25916 times.
|
129580 | for (i = 0; i < 4; i++) { |
| 163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103664 times.
|
103664 | if (sizes[i] > INT_MAX - ret) |
| 164 | ✗ | return AVERROR(EINVAL); | |
| 165 | 103664 | ret += sizes[i]; | |
| 166 | } | ||
| 167 | |||
| 168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25916 times.
|
25916 | if (!ptr) |
| 169 | ✗ | return ret; | |
| 170 | |||
| 171 | 25916 | data[0] = ptr; | |
| 172 |
4/4✓ Branch 0 taken 72845 times.
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 47021 times.
✓ Branch 3 taken 25824 times.
|
72937 | for (i = 1; i < 4 && sizes[i]; i++) |
| 173 | 47021 | data[i] = data[i - 1] + sizes[i - 1]; | |
| 174 | |||
| 175 | 25916 | return ret; | |
| 176 | } | ||
| 177 | |||
| 178 | 1603 | int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt) | |
| 179 | { | ||
| 180 | int i; | ||
| 181 | |||
| 182 |
2/2✓ Branch 0 taken 386908 times.
✓ Branch 1 taken 1511 times.
|
388419 | for (i = 0; i < 256; i++) { |
| 183 | int r, g, b; | ||
| 184 | |||
| 185 |
6/6✓ Branch 0 taken 3840 times.
✓ Branch 1 taken 375552 times.
✓ Branch 2 taken 3584 times.
✓ Branch 3 taken 256 times.
✓ Branch 4 taken 3584 times.
✓ Branch 5 taken 92 times.
|
386908 | switch (pix_fmt) { |
| 186 | 3840 | case AV_PIX_FMT_RGB8: | |
| 187 | 3840 | r = (i>>5 )*36; | |
| 188 | 3840 | g = ((i>>2)&7)*36; | |
| 189 | 3840 | b = (i&3 )*85; | |
| 190 | 3840 | break; | |
| 191 | 375552 | case AV_PIX_FMT_BGR8: | |
| 192 | 375552 | b = (i>>6 )*85; | |
| 193 | 375552 | g = ((i>>3)&7)*36; | |
| 194 | 375552 | r = (i&7 )*36; | |
| 195 | 375552 | break; | |
| 196 | 3584 | case AV_PIX_FMT_RGB4_BYTE: | |
| 197 | 3584 | r = (i>>3 )*255; | |
| 198 | 3584 | g = ((i>>1)&3)*85; | |
| 199 | 3584 | b = (i&1 )*255; | |
| 200 | 3584 | break; | |
| 201 | 256 | case AV_PIX_FMT_BGR4_BYTE: | |
| 202 | 256 | b = (i>>3 )*255; | |
| 203 | 256 | g = ((i>>1)&3)*85; | |
| 204 | 256 | r = (i&1 )*255; | |
| 205 | 256 | break; | |
| 206 | 3584 | case AV_PIX_FMT_GRAY8: | |
| 207 | 3584 | r = b = g = i; | |
| 208 | 3584 | break; | |
| 209 | 92 | default: | |
| 210 | 92 | return AVERROR(EINVAL); | |
| 211 | } | ||
| 212 | 386816 | pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24); | |
| 213 | } | ||
| 214 | |||
| 215 | 1511 | return 0; | |
| 216 | } | ||
| 217 | |||
| 218 | 394 | int av_image_alloc(uint8_t *pointers[4], int linesizes[4], | |
| 219 | int w, int h, enum AVPixelFormat pix_fmt, int align) | ||
| 220 | { | ||
| 221 | 394 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 222 | int i, ret; | ||
| 223 | ptrdiff_t linesizes1[4]; | ||
| 224 | size_t total_size, sizes[4]; | ||
| 225 | uint8_t *buf; | ||
| 226 | |||
| 227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 394 times.
|
394 | if (!desc) |
| 228 | ✗ | return AVERROR(EINVAL); | |
| 229 | |||
| 230 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 394 times.
|
394 | if ((ret = av_image_check_size(w, h, 0, NULL)) < 0) |
| 231 | ✗ | return ret; | |
| 232 |
3/4✓ Branch 0 taken 126 times.
✓ Branch 1 taken 268 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 394 times.
|
394 | if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0) |
| 233 | ✗ | return ret; | |
| 234 | |||
| 235 |
2/2✓ Branch 0 taken 1576 times.
✓ Branch 1 taken 394 times.
|
1970 | for (i = 0; i < 4; i++) { |
| 236 | 1576 | linesizes[i] = FFALIGN(linesizes[i], align); | |
| 237 | 1576 | linesizes1[i] = linesizes[i]; | |
| 238 | } | ||
| 239 | |||
| 240 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 394 times.
|
394 | if ((ret = av_image_fill_plane_sizes(sizes, pix_fmt, h, linesizes1)) < 0) |
| 241 | ✗ | return ret; | |
| 242 | 394 | total_size = align; | |
| 243 |
2/2✓ Branch 0 taken 1576 times.
✓ Branch 1 taken 394 times.
|
1970 | for (i = 0; i < 4; i++) { |
| 244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1576 times.
|
1576 | if (total_size > SIZE_MAX - sizes[i]) |
| 245 | ✗ | return AVERROR(EINVAL); | |
| 246 | 1576 | total_size += sizes[i]; | |
| 247 | } | ||
| 248 | 394 | buf = av_malloc(total_size); | |
| 249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 394 times.
|
394 | if (!buf) |
| 250 | ✗ | return AVERROR(ENOMEM); | |
| 251 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 394 times.
|
394 | if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) { |
| 252 | ✗ | av_free(buf); | |
| 253 | ✗ | return ret; | |
| 254 | } | ||
| 255 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 393 times.
|
394 | if (desc->flags & AV_PIX_FMT_FLAG_PAL) { |
| 256 | 1 | avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt); | |
| 257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (align < 4) { |
| 258 | ✗ | av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n"); | |
| 259 | ✗ | av_free(buf); | |
| 260 | ✗ | return AVERROR(EINVAL); | |
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 393 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
394 | if (desc->flags & AV_PIX_FMT_FLAG_PAL && pointers[1] && |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | pointers[1] - pointers[0] > linesizes[0] * h) { |
| 266 | /* zero-initialize the padding before the palette */ | ||
| 267 | ✗ | memset(pointers[0] + linesizes[0] * h, 0, | |
| 268 | ✗ | pointers[1] - pointers[0] - linesizes[0] * h); | |
| 269 | } | ||
| 270 | |||
| 271 | 394 | return ret; | |
| 272 | } | ||
| 273 | |||
| 274 | typedef struct ImgUtils { | ||
| 275 | const AVClass *class; | ||
| 276 | int log_offset; | ||
| 277 | void *log_ctx; | ||
| 278 | } ImgUtils; | ||
| 279 | |||
| 280 | static const AVClass imgutils_class = { | ||
| 281 | .class_name = "IMGUTILS", | ||
| 282 | .item_name = av_default_item_name, | ||
| 283 | .option = NULL, | ||
| 284 | .version = LIBAVUTIL_VERSION_INT, | ||
| 285 | .log_level_offset_offset = offsetof(ImgUtils, log_offset), | ||
| 286 | .parent_log_context_offset = offsetof(ImgUtils, log_ctx), | ||
| 287 | }; | ||
| 288 | |||
| 289 | 668429 | int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx) | |
| 290 | { | ||
| 291 | 668429 | ImgUtils imgutils = { | |
| 292 | .class = &imgutils_class, | ||
| 293 | .log_offset = log_offset, | ||
| 294 | .log_ctx = log_ctx, | ||
| 295 | }; | ||
| 296 | 668429 | int64_t stride = av_image_get_linesize(pix_fmt, w, 0); | |
| 297 |
2/2✓ Branch 0 taken 627306 times.
✓ Branch 1 taken 41123 times.
|
668429 | if (stride <= 0) |
| 298 | 627306 | stride = 8LL*w; | |
| 299 | 668429 | stride += 128*8; | |
| 300 | |||
| 301 |
12/12✓ Branch 0 taken 668374 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 668320 times.
✓ Branch 3 taken 54 times.
✓ Branch 4 taken 668157 times.
✓ Branch 5 taken 163 times.
✓ Branch 6 taken 668004 times.
✓ Branch 7 taken 153 times.
✓ Branch 8 taken 667749 times.
✓ Branch 9 taken 255 times.
✓ Branch 10 taken 1480 times.
✓ Branch 11 taken 666269 times.
|
668429 | if (w==0 || h==0 || w > INT32_MAX || h > INT32_MAX || stride >= INT_MAX || stride*(h + 128ULL) >= INT_MAX) { |
| 302 | 2160 | av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h); | |
| 303 | 2160 | return AVERROR(EINVAL); | |
| 304 | } | ||
| 305 | |||
| 306 |
2/2✓ Branch 0 taken 267087 times.
✓ Branch 1 taken 399182 times.
|
666269 | if (max_pixels < INT64_MAX) { |
| 307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 267087 times.
|
267087 | if (w*(int64_t)h > max_pixels) { |
| 308 | ✗ | av_log(&imgutils, AV_LOG_ERROR, | |
| 309 | "Picture size %ux%u exceeds specified max pixel count %"PRId64", see the documentation if you wish to increase it\n", | ||
| 310 | w, h, max_pixels); | ||
| 311 | ✗ | return AVERROR(EINVAL); | |
| 312 | } | ||
| 313 | } | ||
| 314 | |||
| 315 | 666269 | return 0; | |
| 316 | } | ||
| 317 | |||
| 318 | 360410 | int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx) | |
| 319 | { | ||
| 320 | 360410 | return av_image_check_size2(w, h, INT64_MAX, AV_PIX_FMT_NONE, log_offset, log_ctx); | |
| 321 | } | ||
| 322 | |||
| 323 | 154846 | int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar) | |
| 324 | { | ||
| 325 | int64_t scaled_dim; | ||
| 326 | |||
| 327 |
3/4✓ Branch 0 taken 154841 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 154841 times.
|
154846 | if (sar.den <= 0 || sar.num < 0) |
| 328 | 5 | return AVERROR(EINVAL); | |
| 329 | |||
| 330 |
4/4✓ Branch 0 taken 31535 times.
✓ Branch 1 taken 123306 times.
✓ Branch 2 taken 22547 times.
✓ Branch 3 taken 8988 times.
|
154841 | if (!sar.num || sar.num == sar.den) |
| 331 | 145853 | return 0; | |
| 332 | |||
| 333 |
2/2✓ Branch 0 taken 2570 times.
✓ Branch 1 taken 6418 times.
|
8988 | if (sar.num < sar.den) |
| 334 | 2570 | scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO); | |
| 335 | else | ||
| 336 | 6418 | scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO); | |
| 337 | |||
| 338 |
1/2✓ Branch 0 taken 8988 times.
✗ Branch 1 not taken.
|
8988 | if (scaled_dim > 0) |
| 339 | 8988 | return 0; | |
| 340 | |||
| 341 | ✗ | return AVERROR(EINVAL); | |
| 342 | } | ||
| 343 | |||
| 344 | 2117937 | static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize, | |
| 345 | const uint8_t *src, ptrdiff_t src_linesize, | ||
| 346 | ptrdiff_t bytewidth, int height) | ||
| 347 | { | ||
| 348 |
2/4✓ Branch 0 taken 2117937 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2117937 times.
|
2117937 | if (!dst || !src) |
| 349 | ✗ | return; | |
| 350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2117937 times.
|
2117937 | av_assert0(FFABS(src_linesize) >= bytewidth); |
| 351 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2117937 times.
|
2117937 | av_assert0(FFABS(dst_linesize) >= bytewidth); |
| 352 |
2/2✓ Branch 0 taken 87294654 times.
✓ Branch 1 taken 2117937 times.
|
89412591 | for (;height > 0; height--) { |
| 353 | 87294654 | memcpy(dst, src, bytewidth); | |
| 354 | 87294654 | dst += dst_linesize; | |
| 355 | 87294654 | src += src_linesize; | |
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | ✗ | void av_image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize, | |
| 360 | const uint8_t *src, ptrdiff_t src_linesize, | ||
| 361 | ptrdiff_t bytewidth, int height) | ||
| 362 | { | ||
| 363 | ✗ | int ret = -1; | |
| 364 | |||
| 365 | #if ARCH_X86 | ||
| 366 | ✗ | ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize, | |
| 367 | bytewidth, height); | ||
| 368 | #endif | ||
| 369 | |||
| 370 | ✗ | if (ret < 0) | |
| 371 | ✗ | image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height); | |
| 372 | ✗ | } | |
| 373 | |||
| 374 | 1887997 | void av_image_copy_plane(uint8_t *dst, int dst_linesize, | |
| 375 | const uint8_t *src, int src_linesize, | ||
| 376 | int bytewidth, int height) | ||
| 377 | { | ||
| 378 | 1887997 | image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height); | |
| 379 | 1887997 | } | |
| 380 | |||
| 381 | 81188 | static void image_copy(uint8_t *const dst_data[4], const ptrdiff_t dst_linesizes[4], | |
| 382 | const uint8_t *const src_data[4], const ptrdiff_t src_linesizes[4], | ||
| 383 | enum AVPixelFormat pix_fmt, int width, int height, | ||
| 384 | void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *, | ||
| 385 | ptrdiff_t, ptrdiff_t, int)) | ||
| 386 | { | ||
| 387 | 81188 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 388 | |||
| 389 |
2/4✓ Branch 0 taken 81188 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 81188 times.
|
81188 | if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL) |
| 390 | ✗ | return; | |
| 391 | |||
| 392 |
2/2✓ Branch 0 taken 2273 times.
✓ Branch 1 taken 78915 times.
|
81188 | if (desc->flags & AV_PIX_FMT_FLAG_PAL) { |
| 393 | 2273 | copy_plane(dst_data[0], dst_linesizes[0], | |
| 394 | src_data[0], src_linesizes[0], | ||
| 395 | width, height); | ||
| 396 | /* copy the palette */ | ||
| 397 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 2273 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2273 | if ((desc->flags & AV_PIX_FMT_FLAG_PAL) || (dst_data[1] && src_data[1])) |
| 398 | 2273 | memcpy(dst_data[1], src_data[1], 4*256); | |
| 399 | } else { | ||
| 400 | 78915 | int i, planes_nb = 0; | |
| 401 | |||
| 402 |
2/2✓ Branch 0 taken 238609 times.
✓ Branch 1 taken 78915 times.
|
317524 | for (i = 0; i < desc->nb_components; i++) |
| 403 | 238609 | planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1); | |
| 404 | |||
| 405 |
2/2✓ Branch 0 taken 227667 times.
✓ Branch 1 taken 78915 times.
|
306582 | for (i = 0; i < planes_nb; i++) { |
| 406 | 227667 | int h = height; | |
| 407 | 227667 | ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i); | |
| 408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 227667 times.
|
227667 | if (bwidth < 0) { |
| 409 | ✗ | av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n"); | |
| 410 | ✗ | return; | |
| 411 | } | ||
| 412 |
4/4✓ Branch 0 taken 153381 times.
✓ Branch 1 taken 74286 times.
✓ Branch 2 taken 74136 times.
✓ Branch 3 taken 79245 times.
|
227667 | if (i == 1 || i == 2) { |
| 413 | 148422 | h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h); | |
| 414 | } | ||
| 415 | 227667 | copy_plane(dst_data[i], dst_linesizes[i], | |
| 416 | 227667 | src_data[i], src_linesizes[i], | |
| 417 | bwidth, h); | ||
| 418 | } | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | 81188 | void av_image_copy(uint8_t *const dst_data[4], const int dst_linesizes[4], | |
| 423 | const uint8_t * const src_data[4], const int src_linesizes[4], | ||
| 424 | enum AVPixelFormat pix_fmt, int width, int height) | ||
| 425 | { | ||
| 426 | ptrdiff_t dst_linesizes1[4], src_linesizes1[4]; | ||
| 427 | int i; | ||
| 428 | |||
| 429 |
2/2✓ Branch 0 taken 324752 times.
✓ Branch 1 taken 81188 times.
|
405940 | for (i = 0; i < 4; i++) { |
| 430 | 324752 | dst_linesizes1[i] = dst_linesizes[i]; | |
| 431 | 324752 | src_linesizes1[i] = src_linesizes[i]; | |
| 432 | } | ||
| 433 | |||
| 434 | 81188 | image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt, | |
| 435 | width, height, image_copy_plane); | ||
| 436 | 81188 | } | |
| 437 | |||
| 438 | ✗ | void av_image_copy_uc_from(uint8_t * const dst_data[4], const ptrdiff_t dst_linesizes[4], | |
| 439 | const uint8_t * const src_data[4], const ptrdiff_t src_linesizes[4], | ||
| 440 | enum AVPixelFormat pix_fmt, int width, int height) | ||
| 441 | { | ||
| 442 | ✗ | image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt, | |
| 443 | width, height, av_image_copy_plane_uc_from); | ||
| 444 | ✗ | } | |
| 445 | |||
| 446 | 24953 | int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4], | |
| 447 | const uint8_t *src, enum AVPixelFormat pix_fmt, | ||
| 448 | int width, int height, int align) | ||
| 449 | { | ||
| 450 | int ret, i; | ||
| 451 | |||
| 452 | 24953 | ret = av_image_check_size(width, height, 0, NULL); | |
| 453 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24953 times.
|
24953 | if (ret < 0) |
| 454 | ✗ | return ret; | |
| 455 | |||
| 456 | 24953 | ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width); | |
| 457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24953 times.
|
24953 | if (ret < 0) |
| 458 | ✗ | return ret; | |
| 459 | |||
| 460 |
2/2✓ Branch 0 taken 99812 times.
✓ Branch 1 taken 24953 times.
|
124765 | for (i = 0; i < 4; i++) |
| 461 | 99812 | dst_linesize[i] = FFALIGN(dst_linesize[i], align); | |
| 462 | |||
| 463 | 24953 | return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize); | |
| 464 | } | ||
| 465 | |||
| 466 | 257014 | int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, | |
| 467 | int width, int height, int align) | ||
| 468 | { | ||
| 469 | int ret, i; | ||
| 470 | int linesize[4]; | ||
| 471 | ptrdiff_t aligned_linesize[4]; | ||
| 472 | size_t sizes[4]; | ||
| 473 | 257014 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 474 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 257014 times.
|
257014 | if (!desc) |
| 475 | ✗ | return AVERROR(EINVAL); | |
| 476 | |||
| 477 | 257014 | ret = av_image_check_size(width, height, 0, NULL); | |
| 478 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 257014 times.
|
257014 | if (ret < 0) |
| 479 | ✗ | return ret; | |
| 480 | |||
| 481 | 257014 | ret = av_image_fill_linesizes(linesize, pix_fmt, width); | |
| 482 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 257014 times.
|
257014 | if (ret < 0) |
| 483 | ✗ | return ret; | |
| 484 | |||
| 485 |
2/2✓ Branch 0 taken 1028056 times.
✓ Branch 1 taken 257014 times.
|
1285070 | for (i = 0; i < 4; i++) |
| 486 | 1028056 | aligned_linesize[i] = FFALIGN(linesize[i], align); | |
| 487 | |||
| 488 | 257014 | ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, aligned_linesize); | |
| 489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 257014 times.
|
257014 | if (ret < 0) |
| 490 | ✗ | return ret; | |
| 491 | |||
| 492 | 257014 | ret = 0; | |
| 493 |
2/2✓ Branch 0 taken 1028056 times.
✓ Branch 1 taken 257014 times.
|
1285070 | for (i = 0; i < 4; i++) { |
| 494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1028056 times.
|
1028056 | if (sizes[i] > INT_MAX - ret) |
| 495 | ✗ | return AVERROR(EINVAL); | |
| 496 | 1028056 | ret += sizes[i]; | |
| 497 | } | ||
| 498 | 257014 | return ret; | |
| 499 | } | ||
| 500 | |||
| 501 | 115400 | int av_image_copy_to_buffer(uint8_t *dst, int dst_size, | |
| 502 | const uint8_t * const src_data[4], | ||
| 503 | const int src_linesize[4], | ||
| 504 | enum AVPixelFormat pix_fmt, | ||
| 505 | int width, int height, int align) | ||
| 506 | { | ||
| 507 | 115400 | int i, j, nb_planes = 0, linesize[4]; | |
| 508 | 115400 | int size = av_image_get_buffer_size(pix_fmt, width, height, align); | |
| 509 | 115400 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 510 | int ret; | ||
| 511 | |||
| 512 |
3/6✓ Branch 0 taken 115400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115400 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 115400 times.
|
115400 | if (size > dst_size || size < 0 || !desc) |
| 513 | ✗ | return AVERROR(EINVAL); | |
| 514 | |||
| 515 |
2/2✓ Branch 0 taken 347059 times.
✓ Branch 1 taken 115400 times.
|
462459 | for (i = 0; i < desc->nb_components; i++) |
| 516 | 347059 | nb_planes = FFMAX(desc->comp[i].plane, nb_planes); | |
| 517 | |||
| 518 | 115400 | nb_planes++; | |
| 519 | |||
| 520 | 115400 | ret = av_image_fill_linesizes(linesize, pix_fmt, width); | |
| 521 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 115400 times.
|
115400 | av_assert0(ret >= 0); // was checked previously |
| 522 | |||
| 523 |
2/2✓ Branch 0 taken 302192 times.
✓ Branch 1 taken 115400 times.
|
417592 | for (i = 0; i < nb_planes; i++) { |
| 524 |
4/4✓ Branch 0 taken 205391 times.
✓ Branch 1 taken 96801 times.
✓ Branch 2 taken 88664 times.
✓ Branch 3 taken 116727 times.
|
302192 | int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0; |
| 525 | 302192 | const uint8_t *src = src_data[i]; | |
| 526 | 302192 | h = (height + (1 << shift) - 1) >> shift; | |
| 527 | |||
| 528 |
2/2✓ Branch 0 taken 71101758 times.
✓ Branch 1 taken 302192 times.
|
71403950 | for (j = 0; j < h; j++) { |
| 529 | 71101758 | memcpy(dst, src, linesize[i]); | |
| 530 | 71101758 | dst += FFALIGN(linesize[i], align); | |
| 531 | 71101758 | src += src_linesize[i]; | |
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 |
2/2✓ Branch 0 taken 250 times.
✓ Branch 1 taken 115150 times.
|
115400 | if (desc->flags & AV_PIX_FMT_FLAG_PAL) { |
| 536 | 250 | uint32_t *d32 = (uint32_t *)dst; | |
| 537 | |||
| 538 |
2/2✓ Branch 0 taken 64000 times.
✓ Branch 1 taken 250 times.
|
64250 | for (i = 0; i<256; i++) |
| 539 | 64000 | AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i)); | |
| 540 | } | ||
| 541 | |||
| 542 | 115400 | return size; | |
| 543 | } | ||
| 544 | |||
| 545 | // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear | ||
| 546 | // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e. | ||
| 547 | // dst_size%clear_size!=0), the remaining data will be filled with the beginning | ||
| 548 | // of the clear data only. | ||
| 549 | 75672 | static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear, | |
| 550 | size_t clear_size) | ||
| 551 | { | ||
| 552 | 75672 | int same = 1; | |
| 553 | int i; | ||
| 554 | |||
| 555 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 75672 times.
|
75672 | if (!clear_size) |
| 556 | ✗ | return; | |
| 557 | |||
| 558 | // Reduce to memset() if possible. | ||
| 559 |
2/2✓ Branch 0 taken 175848 times.
✓ Branch 1 taken 36360 times.
|
212208 | for (i = 0; i < clear_size; i++) { |
| 560 |
2/2✓ Branch 0 taken 39312 times.
✓ Branch 1 taken 136536 times.
|
175848 | if (clear[i] != clear[0]) { |
| 561 | 39312 | same = 0; | |
| 562 | 39312 | break; | |
| 563 | } | ||
| 564 | } | ||
| 565 |
2/2✓ Branch 0 taken 36360 times.
✓ Branch 1 taken 39312 times.
|
75672 | if (same) |
| 566 | 36360 | clear_size = 1; | |
| 567 | |||
| 568 |
2/2✓ Branch 0 taken 36360 times.
✓ Branch 1 taken 39312 times.
|
75672 | if (clear_size == 1) { |
| 569 | 36360 | memset(dst, clear[0], dst_size); | |
| 570 | } else { | ||
| 571 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39312 times.
|
39312 | if (clear_size > dst_size) |
| 572 | ✗ | clear_size = dst_size; | |
| 573 | 39312 | memcpy(dst, clear, clear_size); | |
| 574 | 39312 | av_memcpy_backptr(dst + clear_size, clear_size, dst_size - clear_size); | |
| 575 | } | ||
| 576 | } | ||
| 577 | |||
| 578 | // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels | ||
| 579 | // if it's a subsampled packed format). | ||
| 580 | #define MAX_BLOCK_SIZE 32 | ||
| 581 | |||
| 582 | 789 | int av_image_fill_color(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4], | |
| 583 | enum AVPixelFormat pix_fmt, const uint32_t color[4], | ||
| 584 | int width, int height, int flags) | ||
| 585 | { | ||
| 586 | 789 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 587 | 789 | int nb_planes = av_pix_fmt_count_planes(pix_fmt); | |
| 588 | // A pixel or a group of pixels on each plane, with a value that represents the color. | ||
| 589 | // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases. | ||
| 590 | 789 | uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0 | |
| 591 | 789 | int clear_block_size[4] = {0}; | |
| 592 | 789 | ptrdiff_t plane_line_bytes[4] = {0}; | |
| 593 | int bitstream; | ||
| 594 | int plane, c; | ||
| 595 | |||
| 596 |
4/8✓ Branch 0 taken 789 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 789 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 789 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 789 times.
|
789 | if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL) |
| 597 | ✗ | return AVERROR(EINVAL); | |
| 598 | |||
| 599 | 789 | bitstream = !!(desc->flags & AV_PIX_FMT_FLAG_BITSTREAM); | |
| 600 | |||
| 601 |
2/2✓ Branch 0 taken 2379 times.
✓ Branch 1 taken 789 times.
|
3168 | for (c = 0; c < desc->nb_components; c++) { |
| 602 | 2379 | const AVComponentDescriptor comp = desc->comp[c]; | |
| 603 | |||
| 604 | // We try to operate on entire non-subsampled pixel groups (for | ||
| 605 | // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels). | ||
| 606 | 2379 | clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step); | |
| 607 | |||
| 608 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2379 times.
|
2379 | if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE) |
| 609 | ✗ | return AVERROR(EINVAL); | |
| 610 | } | ||
| 611 | |||
| 612 | // Create a byte array for clearing 1 pixel (sometimes several pixels). | ||
| 613 |
2/2✓ Branch 0 taken 2379 times.
✓ Branch 1 taken 789 times.
|
3168 | for (c = 0; c < desc->nb_components; c++) { |
| 614 | 2379 | const AVComponentDescriptor comp = desc->comp[c]; | |
| 615 | // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.) | ||
| 616 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2337 times.
|
2379 | int w = (bitstream ? 8 : 1) * clear_block_size[comp.plane] / comp.step; |
| 617 | uint8_t *c_data[4]; | ||
| 618 | 2379 | const int c_linesize[4] = {0}; | |
| 619 | uint32_t src_array[MAX_BLOCK_SIZE]; | ||
| 620 | int x; | ||
| 621 | |||
| 622 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2379 times.
|
2379 | if (comp.depth > 32) |
| 623 | ✗ | return AVERROR(EINVAL); | |
| 624 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2379 times.
|
2379 | if (w < 1) |
| 625 | ✗ | return AVERROR(EINVAL); | |
| 626 | |||
| 627 |
2/2✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 2379 times.
|
5079 | for (x = 0; x < w; x++) |
| 628 | 2700 | src_array[x] = color[c]; | |
| 629 | |||
| 630 |
2/2✓ Branch 0 taken 9516 times.
✓ Branch 1 taken 2379 times.
|
11895 | for (x = 0; x < 4; x++) |
| 631 | 9516 | c_data[x] = &clear_block[x][0]; | |
| 632 | |||
| 633 | 2379 | av_write_image_line2(src_array, c_data, c_linesize, desc, 0, 0, c, w, 4); | |
| 634 | } | ||
| 635 | |||
| 636 |
2/2✓ Branch 0 taken 1680 times.
✓ Branch 1 taken 789 times.
|
2469 | for (plane = 0; plane < nb_planes; plane++) { |
| 637 | 1680 | plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane); | |
| 638 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1680 times.
|
1680 | if (plane_line_bytes[plane] < 0) |
| 639 | ✗ | return AVERROR(EINVAL); | |
| 640 | } | ||
| 641 | |||
| 642 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 789 times.
|
789 | if (!dst_data) |
| 643 | ✗ | return 0; | |
| 644 | |||
| 645 |
2/2✓ Branch 0 taken 1680 times.
✓ Branch 1 taken 789 times.
|
2469 | for (plane = 0; plane < nb_planes; plane++) { |
| 646 | 1680 | size_t bytewidth = plane_line_bytes[plane]; | |
| 647 | 1680 | uint8_t *data = dst_data[plane]; | |
| 648 |
4/4✓ Branch 0 taken 1257 times.
✓ Branch 1 taken 423 times.
✓ Branch 2 taken 348 times.
✓ Branch 3 taken 909 times.
|
1680 | int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0; |
| 649 | 1680 | int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div; | |
| 650 | |||
| 651 |
2/2✓ Branch 0 taken 75672 times.
✓ Branch 1 taken 1680 times.
|
77352 | for (; plane_h > 0; plane_h--) { |
| 652 | 75672 | memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]); | |
| 653 | 75672 | data += dst_linesize[plane]; | |
| 654 | } | ||
| 655 | } | ||
| 656 | |||
| 657 | 789 | return 0; | |
| 658 | } | ||
| 659 | |||
| 660 | 789 | int av_image_fill_black(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4], | |
| 661 | enum AVPixelFormat pix_fmt, enum AVColorRange range, | ||
| 662 | int width, int height) | ||
| 663 | { | ||
| 664 | 789 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 665 | 789 | int nb_planes = av_pix_fmt_count_planes(pix_fmt); | |
| 666 | int rgb, xyz, pal, limited, alpha, fltp; | ||
| 667 | 789 | uint32_t colors[4] = {0}; | |
| 668 | |||
| 669 |
4/8✓ Branch 0 taken 789 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 789 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 789 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 789 times.
|
789 | if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL) |
| 670 | ✗ | return AVERROR(EINVAL); | |
| 671 | |||
| 672 | 789 | rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB); | |
| 673 | 789 | xyz = !!(desc->flags & AV_PIX_FMT_FLAG_XYZ); | |
| 674 | 789 | pal = !!(desc->flags & AV_PIX_FMT_FLAG_PAL); | |
| 675 |
8/8✓ Branch 0 taken 495 times.
✓ Branch 1 taken 294 times.
✓ Branch 2 taken 489 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 486 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 324 times.
✓ Branch 7 taken 162 times.
|
789 | limited = !rgb && !xyz && !pal && range != AVCOL_RANGE_JPEG; |
| 676 |
4/4✓ Branch 0 taken 786 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 198 times.
✓ Branch 3 taken 588 times.
|
789 | alpha = !pal && !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA); |
| 677 | 789 | fltp = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT); | |
| 678 | |||
| 679 |
2/2✓ Branch 0 taken 2379 times.
✓ Branch 1 taken 789 times.
|
3168 | for (int c = 0; c < desc->nb_components; c++) { |
| 680 | 2379 | const AVComponentDescriptor comp = desc->comp[c]; | |
| 681 | 2379 | uint32_t color = 0; | |
| 682 | |||
| 683 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2379 times.
|
2379 | if (comp.depth > 32) |
| 684 | ✗ | return AVERROR(EINVAL); | |
| 685 | |||
| 686 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2376 times.
|
2379 | if (pix_fmt == AV_PIX_FMT_MONOWHITE) { |
| 687 | 3 | color = 1; | |
| 688 |
4/4✓ Branch 0 taken 786 times.
✓ Branch 1 taken 1590 times.
✓ Branch 2 taken 198 times.
✓ Branch 3 taken 588 times.
|
2376 | } else if (c + 1 == desc->nb_components && alpha) { |
| 689 | // (Assume even limited YUV uses full range alpha.) | ||
| 690 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 162 times.
|
198 | if (fltp) { |
| 691 |
3/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
|
36 | if (comp.depth != 16 && comp.depth != 32) |
| 692 | ✗ | return AVERROR(EINVAL); | |
| 693 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | color = (comp.depth == 16 ? 0x3C00 : 0x3F800000); // 1.0 |
| 694 | } else { | ||
| 695 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 12 times.
|
162 | color = (comp.depth == 32 ? 0 : (1 << comp.depth)) - 1; |
| 696 | } | ||
| 697 |
6/6✓ Branch 0 taken 786 times.
✓ Branch 1 taken 1392 times.
✓ Branch 2 taken 322 times.
✓ Branch 3 taken 464 times.
✓ Branch 4 taken 320 times.
✓ Branch 5 taken 2 times.
|
2178 | } else if (c == 0 && limited && comp.depth > 1) { |
| 698 |
6/8✓ Branch 0 taken 320 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 304 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8 times.
|
320 | if (comp.depth < 8 || (fltp && comp.depth != 16 && comp.depth != 32)) |
| 699 | ✗ | return AVERROR(EINVAL); | |
| 700 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 304 times.
|
320 | if (fltp) |
| 701 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | color = (comp.depth == 16 ? 0x2C00 : 0x3D800000); // 0.0625 |
| 702 | else | ||
| 703 | 304 | color = 16 << (comp.depth - 8); | |
| 704 |
8/8✓ Branch 0 taken 1162 times.
✓ Branch 1 taken 696 times.
✓ Branch 2 taken 696 times.
✓ Branch 3 taken 466 times.
✓ Branch 4 taken 804 times.
✓ Branch 5 taken 588 times.
✓ Branch 6 taken 792 times.
✓ Branch 7 taken 12 times.
|
1858 | } else if ((c == 1 || c == 2) && !rgb && !xyz) { |
| 705 |
2/8✓ Branch 0 taken 792 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 792 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
792 | if (comp.depth < 8 || fltp && comp.depth != 16 && comp.depth != 32) |
| 706 | ✗ | return AVERROR(EINVAL); | |
| 707 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 792 times.
|
792 | if (fltp) |
| 708 | ✗ | color = (comp.depth == 16 ? 0x3800 : 0x3F000000); // 0.5 | |
| 709 | else | ||
| 710 | 792 | color = 128 << (comp.depth - 8); | |
| 711 | } | ||
| 712 | |||
| 713 | 2379 | colors[c] = color; | |
| 714 | } | ||
| 715 | |||
| 716 | 789 | return av_image_fill_color(dst_data, dst_linesize, pix_fmt, colors, width, height, 0); | |
| 717 | } | ||
| 718 |