]> git.sesse.net Git - ffmpeg/blob - libavutil/imgutils.c
avutil/pixdesc: Remove deprecated AV_PIX_FMT_FLAG_PSEUDOPAL
[ffmpeg] / libavutil / imgutils.c
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 "internal.h"
29 #include "intreadwrite.h"
30 #include "log.h"
31 #include "mathematics.h"
32 #include "pixdesc.h"
33 #include "rational.h"
34
35 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
36                                 const AVPixFmtDescriptor *pixdesc)
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++) {
44         const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
45         if (comp->step > max_pixsteps[comp->plane]) {
46             max_pixsteps[comp->plane] = comp->step;
47             if (max_pixstep_comps)
48                 max_pixstep_comps[comp->plane] = i;
49         }
50     }
51 }
52
53 static inline
54 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     if (!desc)
61         return AVERROR(EINVAL);
62
63     if (width < 0)
64         return AVERROR(EINVAL);
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)
68         return AVERROR(EINVAL);
69     linesize = max_step * shifted_w;
70
71     if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
72         linesize = (linesize + 7) >> 3;
73     return linesize;
74 }
75
76 int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
77 {
78     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     if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
83         return AVERROR(EINVAL);
84
85     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
86     return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
87 }
88
89 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
90 {
91     int i, ret;
92     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     memset(linesizes, 0, 4*sizeof(linesizes[0]));
97
98     if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
99         return AVERROR(EINVAL);
100
101     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
102     for (i = 0; i < 4; i++) {
103         if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
104             return ret;
105         linesizes[i] = ret;
106     }
107
108     return 0;
109 }
110
111 int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt,
112                               int height, const ptrdiff_t linesizes[4])
113 {
114     int i, has_plane[4] = { 0 };
115
116     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
117     memset(sizes    , 0, sizeof(sizes[0])*4);
118
119     if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
120         return AVERROR(EINVAL);
121
122     if (linesizes[0] > SIZE_MAX / height)
123         return AVERROR(EINVAL);
124     sizes[0] = linesizes[0] * (size_t)height;
125
126     if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
127         sizes[1] = 256 * 4; /* palette is stored here as 256 32 bits words */
128         return 0;
129     }
130
131     for (i = 0; i < 4; i++)
132         has_plane[desc->comp[i].plane] = 1;
133
134     for (i = 1; i < 4 && has_plane[i]; i++) {
135         int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
136         h = (height + (1 << s) - 1) >> s;
137         if (linesizes[i] > SIZE_MAX / h)
138             return AVERROR(EINVAL);
139         sizes[i] = (size_t)h * linesizes[i];
140     }
141
142     return 0;
143 }
144
145 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     memset(data     , 0, sizeof(data[0])*4);
153
154     for (i = 0; i < 4; i++)
155         linesizes1[i] = linesizes[i];
156
157     ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, linesizes1);
158     if (ret < 0)
159         return ret;
160
161     ret = 0;
162     for (i = 0; i < 4; i++) {
163         if (sizes[i] > INT_MAX - ret)
164             return AVERROR(EINVAL);
165         ret += sizes[i];
166     }
167
168     data[0] = ptr;
169     for (i = 1; i < 4 && sizes[i]; i++)
170         data[i] = data[i - 1] + sizes[i - 1];
171
172     return ret;
173 }
174
175 int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
176 {
177     int i;
178
179     for (i = 0; i < 256; i++) {
180         int r, g, b;
181
182         switch (pix_fmt) {
183         case AV_PIX_FMT_RGB8:
184             r = (i>>5    )*36;
185             g = ((i>>2)&7)*36;
186             b = (i&3     )*85;
187             break;
188         case AV_PIX_FMT_BGR8:
189             b = (i>>6    )*85;
190             g = ((i>>3)&7)*36;
191             r = (i&7     )*36;
192             break;
193         case AV_PIX_FMT_RGB4_BYTE:
194             r = (i>>3    )*255;
195             g = ((i>>1)&3)*85;
196             b = (i&1     )*255;
197             break;
198         case AV_PIX_FMT_BGR4_BYTE:
199             b = (i>>3    )*255;
200             g = ((i>>1)&3)*85;
201             r = (i&1     )*255;
202             break;
203         case AV_PIX_FMT_GRAY8:
204             r = b = g = i;
205             break;
206         default:
207             return AVERROR(EINVAL);
208         }
209         pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
210     }
211
212     return 0;
213 }
214
215 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
216                    int w, int h, enum AVPixelFormat pix_fmt, int align)
217 {
218     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
219     int i, ret;
220     ptrdiff_t linesizes1[4];
221     size_t total_size, sizes[4];
222     uint8_t *buf;
223
224     if (!desc)
225         return AVERROR(EINVAL);
226
227     if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
228         return ret;
229     if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
230         return ret;
231
232     for (i = 0; i < 4; i++) {
233         linesizes[i] = FFALIGN(linesizes[i], align);
234         linesizes1[i] = linesizes[i];
235     }
236
237     if ((ret = av_image_fill_plane_sizes(sizes, pix_fmt, h, linesizes1)) < 0)
238         return ret;
239     total_size = align;
240     for (i = 0; i < 4; i++) {
241         if (total_size > SIZE_MAX - sizes[i])
242             return AVERROR(EINVAL);
243         total_size += sizes[i];
244     }
245     buf = av_malloc(total_size);
246     if (!buf)
247         return AVERROR(ENOMEM);
248     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
249         av_free(buf);
250         return ret;
251     }
252     if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
253         avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
254         if (align < 4) {
255             av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n");
256             av_free(buf);
257             return AVERROR(EINVAL);
258         }
259     }
260
261     if (desc->flags & AV_PIX_FMT_FLAG_PAL && pointers[1] &&
262         pointers[1] - pointers[0] > linesizes[0] * h) {
263         /* zero-initialize the padding before the palette */
264         memset(pointers[0] + linesizes[0] * h, 0,
265                pointers[1] - pointers[0] - linesizes[0] * h);
266     }
267
268     return ret;
269 }
270
271 typedef struct ImgUtils {
272     const AVClass *class;
273     int   log_offset;
274     void *log_ctx;
275 } ImgUtils;
276
277 static const AVClass imgutils_class = {
278     .class_name                = "IMGUTILS",
279     .item_name                 = av_default_item_name,
280     .option                    = NULL,
281     .version                   = LIBAVUTIL_VERSION_INT,
282     .log_level_offset_offset   = offsetof(ImgUtils, log_offset),
283     .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
284 };
285
286 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)
287 {
288     ImgUtils imgutils = {
289         .class      = &imgutils_class,
290         .log_offset = log_offset,
291         .log_ctx    = log_ctx,
292     };
293     int64_t stride = av_image_get_linesize(pix_fmt, w, 0);
294     if (stride <= 0)
295         stride = 8LL*w;
296     stride += 128*8;
297
298     if ((int)w<=0 || (int)h<=0 || stride >= INT_MAX || stride*(uint64_t)(h+128) >= INT_MAX) {
299         av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
300         return AVERROR(EINVAL);
301     }
302
303     if (max_pixels < INT64_MAX) {
304         if (w*(int64_t)h > max_pixels) {
305             av_log(&imgutils, AV_LOG_ERROR,
306                     "Picture size %ux%u exceeds specified max pixel count %"PRId64", see the documentation if you wish to increase it\n",
307                     w, h, max_pixels);
308             return AVERROR(EINVAL);
309         }
310     }
311
312     return 0;
313 }
314
315 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
316 {
317     return av_image_check_size2(w, h, INT64_MAX, AV_PIX_FMT_NONE, log_offset, log_ctx);
318 }
319
320 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
321 {
322     int64_t scaled_dim;
323
324     if (sar.den <= 0 || sar.num < 0)
325         return AVERROR(EINVAL);
326
327     if (!sar.num || sar.num == sar.den)
328         return 0;
329
330     if (sar.num < sar.den)
331         scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
332     else
333         scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
334
335     if (scaled_dim > 0)
336         return 0;
337
338     return AVERROR(EINVAL);
339 }
340
341 static void image_copy_plane(uint8_t       *dst, ptrdiff_t dst_linesize,
342                              const uint8_t *src, ptrdiff_t src_linesize,
343                              ptrdiff_t bytewidth, int height)
344 {
345     if (!dst || !src)
346         return;
347     av_assert0(FFABS(src_linesize) >= bytewidth);
348     av_assert0(FFABS(dst_linesize) >= bytewidth);
349     for (;height > 0; height--) {
350         memcpy(dst, src, bytewidth);
351         dst += dst_linesize;
352         src += src_linesize;
353     }
354 }
355
356 static void image_copy_plane_uc_from(uint8_t       *dst, ptrdiff_t dst_linesize,
357                                      const uint8_t *src, ptrdiff_t src_linesize,
358                                      ptrdiff_t bytewidth, int height)
359 {
360     int ret = -1;
361
362 #if ARCH_X86
363     ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
364                                           bytewidth, height);
365 #endif
366
367     if (ret < 0)
368         image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
369 }
370
371 void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
372                          const uint8_t *src, int src_linesize,
373                          int bytewidth, int height)
374 {
375     image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
376 }
377
378 static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
379                        const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
380                        enum AVPixelFormat pix_fmt, int width, int height,
381                        void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
382                                           ptrdiff_t, ptrdiff_t, int))
383 {
384     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
385
386     if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
387         return;
388
389     if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
390         copy_plane(dst_data[0], dst_linesizes[0],
391                    src_data[0], src_linesizes[0],
392                    width, height);
393         /* copy the palette */
394         if ((desc->flags & AV_PIX_FMT_FLAG_PAL) || (dst_data[1] && src_data[1]))
395             memcpy(dst_data[1], src_data[1], 4*256);
396     } else {
397         int i, planes_nb = 0;
398
399         for (i = 0; i < desc->nb_components; i++)
400             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
401
402         for (i = 0; i < planes_nb; i++) {
403             int h = height;
404             ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
405             if (bwidth < 0) {
406                 av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
407                 return;
408             }
409             if (i == 1 || i == 2) {
410                 h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
411             }
412             copy_plane(dst_data[i], dst_linesizes[i],
413                        src_data[i], src_linesizes[i],
414                        bwidth, h);
415         }
416     }
417 }
418
419 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
420                    const uint8_t *src_data[4], const int src_linesizes[4],
421                    enum AVPixelFormat pix_fmt, int width, int height)
422 {
423     ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
424     int i;
425
426     for (i = 0; i < 4; i++) {
427         dst_linesizes1[i] = dst_linesizes[i];
428         src_linesizes1[i] = src_linesizes[i];
429     }
430
431     image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
432                width, height, image_copy_plane);
433 }
434
435 void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
436                            const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
437                            enum AVPixelFormat pix_fmt, int width, int height)
438 {
439     image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
440                width, height, image_copy_plane_uc_from);
441 }
442
443 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
444                          const uint8_t *src, enum AVPixelFormat pix_fmt,
445                          int width, int height, int align)
446 {
447     int ret, i;
448
449     ret = av_image_check_size(width, height, 0, NULL);
450     if (ret < 0)
451         return ret;
452
453     ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
454     if (ret < 0)
455         return ret;
456
457     for (i = 0; i < 4; i++)
458         dst_linesize[i] = FFALIGN(dst_linesize[i], align);
459
460     return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
461 }
462
463 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
464                              int width, int height, int align)
465 {
466     int ret, i;
467     int linesize[4];
468     ptrdiff_t aligned_linesize[4];
469     size_t sizes[4];
470     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
471     if (!desc)
472         return AVERROR(EINVAL);
473
474     ret = av_image_check_size(width, height, 0, NULL);
475     if (ret < 0)
476         return ret;
477
478     ret = av_image_fill_linesizes(linesize, pix_fmt, width);
479     if (ret < 0)
480         return ret;
481
482     for (i = 0; i < 4; i++)
483         aligned_linesize[i] = FFALIGN(linesize[i], align);
484
485     ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, aligned_linesize);
486     if (ret < 0)
487         return ret;
488
489     ret = 0;
490     for (i = 0; i < 4; i++) {
491         if (sizes[i] > INT_MAX - ret)
492             return AVERROR(EINVAL);
493         ret += sizes[i];
494     }
495     return ret;
496 }
497
498 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
499                             const uint8_t * const src_data[4],
500                             const int src_linesize[4],
501                             enum AVPixelFormat pix_fmt,
502                             int width, int height, int align)
503 {
504     int i, j, nb_planes = 0, linesize[4];
505     int size = av_image_get_buffer_size(pix_fmt, width, height, align);
506     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
507     int ret;
508
509     if (size > dst_size || size < 0 || !desc)
510         return AVERROR(EINVAL);
511
512     for (i = 0; i < desc->nb_components; i++)
513         nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
514
515     nb_planes++;
516
517     ret = av_image_fill_linesizes(linesize, pix_fmt, width);
518     av_assert0(ret >= 0); // was checked previously
519
520     for (i = 0; i < nb_planes; i++) {
521         int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
522         const uint8_t *src = src_data[i];
523         h = (height + (1 << shift) - 1) >> shift;
524
525         for (j = 0; j < h; j++) {
526             memcpy(dst, src, linesize[i]);
527             dst += FFALIGN(linesize[i], align);
528             src += src_linesize[i];
529         }
530     }
531
532     if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
533         uint32_t *d32 = (uint32_t *)dst;
534
535         for (i = 0; i<256; i++)
536             AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
537     }
538
539     return size;
540 }
541
542 // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
543 // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
544 // dst_size%clear_size!=0), the remaining data will be filled with the beginning
545 // of the clear data only.
546 static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
547                          size_t clear_size)
548 {
549     int same = 1;
550     int i;
551
552     if (!clear_size)
553         return;
554
555     // Reduce to memset() if possible.
556     for (i = 0; i < clear_size; i++) {
557         if (clear[i] != clear[0]) {
558             same = 0;
559             break;
560         }
561     }
562     if (same)
563         clear_size = 1;
564
565     if (clear_size == 1) {
566         memset(dst, clear[0], dst_size);
567     } else {
568         if (clear_size > dst_size)
569             clear_size = dst_size;
570         memcpy(dst, clear, clear_size);
571         av_memcpy_backptr(dst + clear_size, clear_size, dst_size - clear_size);
572     }
573 }
574
575 // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
576 // if it's a subsampled packed format).
577 #define MAX_BLOCK_SIZE 32
578
579 int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
580                         enum AVPixelFormat pix_fmt, enum AVColorRange range,
581                         int width, int height)
582 {
583     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
584     int nb_planes = av_pix_fmt_count_planes(pix_fmt);
585     // A pixel or a group of pixels on each plane, with a value that represents black.
586     // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
587     uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0
588     int clear_block_size[4] = {0};
589     ptrdiff_t plane_line_bytes[4] = {0};
590     int rgb, limited;
591     int plane, c;
592
593     if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
594         return AVERROR(EINVAL);
595
596     rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
597     limited = !rgb && range != AVCOL_RANGE_JPEG;
598
599     if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
600         ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
601         uint8_t *data;
602         int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
603         int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
604         if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
605             return AVERROR(EINVAL);
606
607         if (!dst_data)
608             return 0;
609
610         data = dst_data[0];
611
612         // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
613         for (;height > 0; height--) {
614             memset(data, fill, bytewidth);
615             data += dst_linesize[0];
616         }
617         return 0;
618     }
619
620     for (c = 0; c < desc->nb_components; c++) {
621         const AVComponentDescriptor comp = desc->comp[c];
622
623         // We try to operate on entire non-subsampled pixel groups (for
624         // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
625         clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
626
627         if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
628             return AVERROR(EINVAL);
629     }
630
631     // Create a byte array for clearing 1 pixel (sometimes several pixels).
632     for (c = 0; c < desc->nb_components; c++) {
633         const AVComponentDescriptor comp = desc->comp[c];
634         // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
635         int w = clear_block_size[comp.plane] / comp.step;
636         uint8_t *c_data[4];
637         const int c_linesize[4] = {0};
638         uint16_t src_array[MAX_BLOCK_SIZE];
639         uint16_t src = 0;
640         int x;
641
642         if (comp.depth > 16)
643             return AVERROR(EINVAL);
644         if (!rgb && comp.depth < 8)
645             return AVERROR(EINVAL);
646         if (w < 1)
647             return AVERROR(EINVAL);
648
649         if (c == 0 && limited) {
650             src = 16 << (comp.depth - 8);
651         } else if ((c == 1 || c == 2) && !rgb) {
652             src = 128 << (comp.depth - 8);
653         } else if (c == 3) {
654             // (Assume even limited YUV uses full range alpha.)
655             src = (1 << comp.depth) - 1;
656         }
657
658         for (x = 0; x < w; x++)
659             src_array[x] = src;
660
661         for (x = 0; x < 4; x++)
662             c_data[x] = &clear_block[x][0];
663
664         av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
665     }
666
667     for (plane = 0; plane < nb_planes; plane++) {
668         plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
669         if (plane_line_bytes[plane] < 0)
670             return AVERROR(EINVAL);
671     }
672
673     if (!dst_data)
674         return 0;
675
676     for (plane = 0; plane < nb_planes; plane++) {
677         size_t bytewidth = plane_line_bytes[plane];
678         uint8_t *data = dst_data[plane];
679         int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
680         int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
681
682         for (; plane_h > 0; plane_h--) {
683             memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
684             data += dst_linesize[plane];
685         }
686     }
687
688     return 0;
689 }