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
34
37 {
38 int i;
39 memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
40 if (max_pixstep_comps)
41 memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
42
43 for (i = 0; i < 4; i++) {
45 if (comp->
step > max_pixsteps[comp->
plane]) {
47 if (max_pixstep_comps)
48 max_pixstep_comps[comp->
plane] = i;
49 }
50 }
51 }
52
53 static inline
55 int max_step, int max_step_comp,
57 {
58 int s, shifted_w, linesize;
59
60 if (!desc)
62
63 if (width < 0)
65 s = (max_step_comp == 1 || max_step_comp == 2) ? desc->
log2_chroma_w : 0;
66 shifted_w = ((width + (1 << s) - 1)) >> s;
67 if (shifted_w && max_step > INT_MAX / shifted_w)
69 linesize = max_step * shifted_w;
70
72 linesize = (linesize + 7) >> 3;
73 return linesize;
74 }
75
77 {
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
84
87 }
88
90 {
91 int i, ret;
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 memset(linesizes, 0, 4*sizeof(linesizes[0]));
97
100
102 for (i = 0; i < 4; i++) {
104 return ret;
105 linesizes[i] = ret;
106 }
107
108 return 0;
109 }
110
112 uint8_t *ptr,
const int linesizes[4])
113 {
114 int i, total_size,
size[4] = { 0 }, has_plane[4] = { 0 };
115
117 memset(data , 0, sizeof(data[0])*4);
118
121
122 data[0] = ptr;
123 if (linesizes[0] > (INT_MAX - 1024) / height)
125 size[0] = linesizes[0] *
height;
126
129 data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
130 return size[0] + 256 * 4;
131 }
132
133 for (i = 0; i < 4; i++)
135
136 total_size = size[0];
137 for (i = 1; i < 4 && has_plane[i]; i++) {
139 data[i] = data[i-1] + size[i-1];
140 h = (height + (1 << s) - 1) >> s;
141 if (linesizes[i] > INT_MAX / h)
143 size[i] = h * linesizes[i];
144 if (total_size > INT_MAX - size[i])
146 total_size += size[i];
147 }
148
149 return total_size;
150 }
151
153 {
154 int i;
155
156 for (i = 0; i < 256; i++) {
158
159 switch (pix_fmt) {
161 r = (i>>5 )*36;
162 g = ((i>>2)&7)*36;
163 b = (i&3 )*85;
164 break;
166 b = (i>>6 )*85;
167 g = ((i>>3)&7)*36;
168 r = (i&7 )*36;
169 break;
171 r = (i>>3 )*255;
172 g = ((i>>1)&3)*85;
173 b = (i&1 )*255;
174 break;
176 b = (i>>3 )*255;
177 g = ((i>>1)&3)*85;
178 r = (i&1 )*255;
179 break;
181 r = b = g = i;
182 break;
183 default:
185 }
186 pal[i] = b + (g << 8) + (r << 16) + (0xFF
U << 24);
187 }
188
189 return 0;
190 }
191
194 {
196 int i, ret;
198
199 if (!desc)
201
203 return ret;
205 return ret;
206
207 for (i = 0; i < 4; i++)
208 linesizes[i] =
FFALIGN(linesizes[i], align);
209
211 return ret;
213 if (!buf)
217 return ret;
218 }
221 if (align < 4) {
224 }
225 }
226
229 pointers[1] - pointers[0] > linesizes[0] * h) {
230 /* zero-initialize the padding before the palette */
231 memset(pointers[0] + linesizes[0] * h, 0,
232 pointers[1] - pointers[0] - linesizes[0] * h);
233 }
234
235 return ret;
236 }
237
243
249 .log_level_offset_offset = offsetof(
ImgUtils, log_offset),
250 .parent_log_context_offset = offsetof(
ImgUtils, log_ctx),
251 };
252
254 {
257 .log_offset = log_offset,
258 .log_ctx = log_ctx,
259 };
261 if (stride <= 0)
263 stride += 128*8;
264
265 if ((int)w<=0 || (int)h<=0 || stride >= INT_MAX || stride*(uint64_t)(h+128) >= INT_MAX) {
268 }
269
270 if (max_pixels < INT64_MAX) {
271 if (w*(int64_t)h > max_pixels) {
273 "Picture size %ux%u exceeds specified max pixel count %"PRId64", see the documentation if you wish to increase it\n",
274 w, h, max_pixels);
276 }
277 }
278
279 return 0;
280 }
281
283 {
285 }
286
288 {
289 int64_t scaled_dim;
290
291 if (sar.
den <= 0 || sar.
num < 0)
293
295 return 0;
296
299 else
301
302 if (scaled_dim > 0)
303 return 0;
304
306 }
307
310 ptrdiff_t bytewidth,
int height)
311 {
312 if (!dst || !src)
313 return;
316 for (;height > 0; height--) {
317 memcpy(dst, src, bytewidth);
318 dst += dst_linesize;
319 src += src_linesize;
320 }
321 }
322
325 ptrdiff_t bytewidth,
int height)
326 {
327 int ret = -1;
328
329 #if ARCH_X86
331 bytewidth, height);
332 #endif
333
334 if (ret < 0)
336 }
337
340 int bytewidth,
int height)
341 {
343 }
344
346 const uint8_t *src_data[4],
const ptrdiff_t src_linesizes[4],
349 ptrdiff_t, ptrdiff_t, int))
350 {
352
354 return;
355
358 copy_plane(dst_data[0], dst_linesizes[0],
359 src_data[0], src_linesizes[0],
360 width, height);
361 /* copy the palette */
363 memcpy(dst_data[1], src_data[1], 4*256);
364 } else {
365 int i, planes_nb = 0;
366
369
370 for (i = 0; i < planes_nb; i++) {
373 if (bwidth < 0) {
375 return;
376 }
377 if (i == 1 || i == 2) {
379 }
380 copy_plane(dst_data[i], dst_linesizes[i],
381 src_data[i], src_linesizes[i],
382 bwidth, h);
383 }
384 }
385 }
386
388 const uint8_t *src_data[4],
const int src_linesizes[4],
390 {
391 ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
392 int i;
393
394 for (i = 0; i < 4; i++) {
395 dst_linesizes1[i] = dst_linesizes[i];
396 src_linesizes1[i] = src_linesizes[i];
397 }
398
399 image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
401 }
402
404 const uint8_t *src_data[4],
const ptrdiff_t src_linesizes[4],
406 {
407 image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
409 }
410
413 int width,
int height,
int align)
414 {
415 int ret, i;
416
418 if (ret < 0)
419 return ret;
420
422 if (ret < 0)
423 return ret;
424
425 for (i = 0; i < 4; i++)
426 dst_linesize[i] =
FFALIGN(dst_linesize[i], align);
427
429 }
430
432 int width,
int height,
int align)
433 {
435 int linesize[4];
436 int ret;
438 if (!desc)
440
442 if (ret < 0)
443 return ret;
444
445 // do not include palette for these pseudo-paletted formats
448
450 width, height, align);
451 }
452
454 const uint8_t *
const src_data[4],
455 const int src_linesize[4],
457 int width,
int height,
int align)
458 {
459 int i, j, nb_planes = 0, linesize[4];
462 int ret;
463
464 if (size > dst_size || size < 0 || !desc)
466
469
470 nb_planes++;
471
473 av_assert0(ret >= 0);
// was checked previously
474
475 for (i = 0; i < nb_planes; i++) {
478 h = (height + (1 << shift) - 1) >> shift;
479
480 for (j = 0; j <
h; j++) {
481 memcpy(dst,
src, linesize[i]);
482 dst +=
FFALIGN(linesize[i], align);
483 src += src_linesize[i];
484 }
485 }
486
488 uint32_t *
d32 = (uint32_t *)dst;
489
490 for (i = 0; i<256; i++)
492 }
493
495 }
496
497 // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
498 // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
499 // dst_size%clear_size!=0), the remaining data will be filled with the beginning
500 // of the clear data only.
502 size_t clear_size)
503 {
504 size_t pos = 0;
505 int same = 1;
506 int i;
507
508 if (!clear_size)
509 return;
510
511 // Reduce to memset() if possible.
512 for (i = 0; i < clear_size; i++) {
513 if (clear[i] != clear[0]) {
514 same = 0;
515 break;
516 }
517 }
518 if (same)
519 clear_size = 1;
520
521 if (clear_size == 1) {
522 memset(dst, clear[0], dst_size);
523 dst_size = 0;
524 } else if (clear_size == 2) {
526 for (; dst_size >= 2; dst_size -= 2) {
528 dst += 2;
529 }
530 } else if (clear_size == 4) {
532 for (; dst_size >= 4; dst_size -= 4) {
534 dst += 4;
535 }
536 } else if (clear_size == 8) {
538 for (; dst_size >= 8; dst_size -= 8) {
540 dst += 8;
541 }
542 }
543
544 for (; dst_size; dst_size--)
545 *dst++ = clear[pos++ % clear_size];
546 }
547
548 // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
549 // if it's a subsampled packed format).
550 #define MAX_BLOCK_SIZE 32
551
554 int width, int height)
555 {
558 // A pixel or a group of pixels on each plane, with a value that represents black.
559 // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
561 int clear_block_size[4] = {0};
562 ptrdiff_t plane_line_bytes[4] = {0};
563 int rgb, limited;
565
568
571
577 if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
579
580 if (!dst_data)
581 return 0;
582
583 data = dst_data[0];
584
585 // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
586 for (;height > 0; height--) {
587 memset(data, fill, bytewidth);
588 data += dst_linesize[0];
589 }
590 return 0;
591 }
592
595
596 // We try to operate on entire non-subsampled pixel groups (for
597 // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
599
602 }
603
604 // Create a byte array for clearing 1 pixel (sometimes several pixels).
607 // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
608 int w = clear_block_size[comp.
plane] / comp.
step;
610 const int c_linesize[4] = {0};
613 int x;
614
617 if (!rgb && comp.
depth < 8)
619 if (w < 1)
621
622 if (c == 0 && limited) {
623 src = 16 << (comp.
depth - 8);
624 } else if ((c == 1 || c == 2) && !rgb) {
625 src = 128 << (comp.
depth - 8);
626 } else if (c == 3) {
627 // (Assume even limited YUV uses full range alpha.)
628 src = (1 << comp.
depth) - 1;
629 }
630
631 for (x = 0; x <
w; x++)
632 src_array[x] = src;
633
634 for (x = 0; x < 4; x++)
635 c_data[x] = &clear_block[x][0];
636
638 }
639
640 for (plane = 0; plane < nb_planes; plane++) {
642 if (plane_line_bytes[plane] < 0)
644 }
645
646 if (!dst_data)
647 return 0;
648
649 for (plane = 0; plane < nb_planes; plane++) {
650 size_t bytewidth = plane_line_bytes[
plane];
652 int chroma_div = plane == 1 || plane == 2 ? desc->
log2_chroma_h : 0;
653 int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
654
655 for (; plane_h > 0; plane_h--) {
656 memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
657 data += dst_linesize[
plane];
658 }
659 }
660
661 return 0;
662 }
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
int plane
Which of the 4 planes contains the component.
const char const char void * val
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane...
static enum AVPixelFormat pix_fmt
static int shift(int a, int b)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
ptrdiff_t const GLvoid * data
int av_image_copy_to_buffer(uint8_t *dst, int dst_size, const uint8_t *const src_data[4], const int src_linesize[4], enum AVPixelFormat pix_fmt, int width, int height, int align)
Copy image data from an image into a buffer.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
#define LIBAVUTIL_VERSION_INT
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
static const AVClass imgutils_class
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4], const uint8_t *src, enum AVPixelFormat pix_fmt, int width, int height, int align)
Setup the data pointers and linesizes based on the specified image parameters and the provided array...
const char * av_default_item_name(void *ptr)
Return the context name.
static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear, size_t clear_size)
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
void av_write_image_line(const uint16_t *src, uint8_t *data[4], const int linesize[4], const AVPixFmtDescriptor *desc, int x, int y, int c, int w)
Write the values from src to the pixel format component c of an image line.
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
AVColorRange
MPEG vs JPEG YUV range.
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, ptrdiff_t bytewidth, int height)
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
simple assert() macros that are a bit more flexible than ISO C assert().
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
common internal API header
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
uint8_t nb_components
The number of components each pixel has, (1-4)
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
static int image_get_linesize(int width, int plane, int max_step, int max_step_comp, const AVPixFmtDescriptor *desc)
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
the normal 2^n-1 "JPEG" YUV ranges
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
const AVS_VideoInfo int align
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, uint8_t *ptr, const int linesizes[4])
Fill plane data pointers for an image with pixel format pix_fmt and height height.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Describe the class of an AVClass context structure.
static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4], const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height, void(*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *, ptrdiff_t, ptrdiff_t, int))
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Rational number (pair of numerator and denominator).
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
GLint GLenum GLboolean GLsizei stride
common internal and external API header
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Utilties for rational number calculation.
void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4], const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image data located in uncacheable (e.g.
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
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)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4], enum AVPixelFormat pix_fmt, enum AVColorRange range, int width, int height)
Overwrite the image data with black.
static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, ptrdiff_t bytewidth, int height)
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
int ff_image_copy_plane_uc_from_x86(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, ptrdiff_t bytewidth, int height)
int depth
Number of bits in the component.
AVPixelFormat
Pixel format.
int step
Number of elements between 2 horizontally consecutive pixels.
#define AV_CEIL_RSHIFT(a, b)