• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef AVFILTER_DRAWUTILS_H
20 #define AVFILTER_DRAWUTILS_H
21 
22 /**
23  * @file
24  * misc drawing utilities
25  */
26 
27 #include <stdint.h>
28 #include "avfilter.h"
29 #include "libavutil/pixfmt.h"
30 
31 int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt);
32 
33 #define MAX_PLANES 4
34 
35 typedef struct FFDrawContext {
36     const struct AVPixFmtDescriptor *desc;
37     enum AVPixelFormat format;
38     unsigned nb_planes;
39     int pixelstep[MAX_PLANES]; /*< offset between pixels */
40     uint8_t hsub[MAX_PLANES];  /*< horizontal subsampling */
41     uint8_t vsub[MAX_PLANES];  /*< vertical subsampling */
42     uint8_t hsub_max;
43     uint8_t vsub_max;
44     enum AVColorRange range;
45     unsigned flags;
46     enum AVColorSpace csp;
47     double rgb2yuv[3][3];
48 } FFDrawContext;
49 
50 typedef struct FFDrawColor {
51     uint8_t rgba[4];
52     union {
53         uint32_t u32[4];
54         uint16_t u16[8];
55         uint8_t  u8[16];
56     } comp[MAX_PLANES];
57 } FFDrawColor;
58 
59 /**
60   * Process alpha pixel component.
61   */
62 #define FF_DRAW_PROCESS_ALPHA 1
63 
64 /**
65  * Init a draw context.
66  *
67  * Only a limited number of pixel formats are supported, if format is not
68  * supported the function will return an error.
69  * @param format  pixel format of the frames that will be drawn onto
70  * @param csp     color space of the frames that will be drawn onto,
71  *                defaulting to BT601 or RGB depending on the specified format
72  *                when AVCOL_SPC_UNSPECIFIED is passed.
73  * @param range   sample value range of the frames that will be drawn onto,
74  *                defaulting to TV-range unless using a legacy J format
75  *                when AVCOL_RANGE_UNSPECIFIED is passed.
76  * @param flags   combination of FF_DRAW_* flags.
77  * @return        0 for success, < 0 for error
78  */
79 int ff_draw_init2(FFDrawContext *draw, enum AVPixelFormat format, enum AVColorSpace csp,
80                   enum AVColorRange range, unsigned flags);
81 
82 /*
83  * Legacy wrapper for ff_draw_init2.
84  */
85 int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags);
86 
87 
88 
89 /**
90  * Prepare a color. The rgba value passed is always 8-bit full-range in the RGB space
91  * corresponding to the space set at initialization.
92  */
93 void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4]);
94 
95 /**
96  * Copy a rectangle from an image to another.
97  *
98  * The coordinates must be as even as the subsampling requires.
99  */
100 void ff_copy_rectangle2(FFDrawContext *draw,
101                         uint8_t *dst[], int dst_linesize[],
102                         uint8_t *src[], int src_linesize[],
103                         int dst_x, int dst_y, int src_x, int src_y,
104                         int w, int h);
105 
106 /**
107  * Fill a rectangle with an uniform color.
108  *
109  * The coordinates must be as even as the subsampling requires.
110  * The color needs to be inited with ff_draw_color.
111  */
112 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
113                        uint8_t *dst[], int dst_linesize[],
114                        int dst_x, int dst_y, int w, int h);
115 
116 /**
117  * Blend a rectangle with an uniform color.
118  */
119 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
120                         uint8_t *dst[], int dst_linesize[],
121                         int dst_w, int dst_h,
122                         int x0, int y0, int w, int h);
123 
124 /**
125  * Blend an alpha mask with an uniform color.
126  *
127  * @param draw           draw context
128  * @param color          color for the overlay;
129  * @param dst            destination image
130  * @param dst_linesize   line stride of the destination
131  * @param dst_w          width of the destination image
132  * @param dst_h          height of the destination image
133  * @param mask           mask
134  * @param mask_linesize  line stride of the mask
135  * @param mask_w         width of the mask
136  * @param mask_h         height of the mask
137  * @param l2depth        log2 of depth of the mask (0 for 1bpp, 3 for 8bpp)
138  * @param endianness     bit order of the mask (0: MSB to the left)
139  * @param x0             horizontal position of the overlay
140  * @param y0             vertical position of the overlay
141  */
142 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
143                    uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
144                    const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
145                    int l2depth, unsigned endianness, int x0, int y0);
146 
147 /**
148  * Round a dimension according to subsampling.
149  *
150  * @param draw       draw context
151  * @param sub_dir    0 for horizontal, 1 for vertical
152  * @param round_dir  0 nearest, -1 round down, +1 round up
153  * @param value      value to round
154  * @return  the rounded value
155  */
156 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
157                          int value);
158 
159 /**
160  * Return the list of pixel formats supported by the draw functions.
161  *
162  * The flags are the same as ff_draw_init, i.e., none currently.
163  */
164 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags);
165 
166 #endif /* AVFILTER_DRAWUTILS_H */
167