1 /* 2 * This file is part of mpv. 3 * 4 * mpv 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 * mpv 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 12 * GNU 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 mpv. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef MPLAYER_MP_IMAGE_H 19 #define MPLAYER_MP_IMAGE_H 20 21 #include <stdbool.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <inttypes.h> 26 #include "common/common.h" 27 #include "common/msg.h" 28 #include "csputils.h" 29 #include "video/img_format.h" 30 31 // Assumed minimum align needed for image allocation. It's notable that FFmpeg's 32 // libraries except libavcodec don't really know what alignment they want. 33 // Things will randomly crash or get slower if the alignment is not satisfied. 34 // Whatever. This value should be pretty safe with current CPU architectures. 35 #define MP_IMAGE_BYTE_ALIGN 64 36 37 #define MP_IMGFIELD_TOP_FIRST 0x02 38 #define MP_IMGFIELD_REPEAT_FIRST 0x04 39 #define MP_IMGFIELD_INTERLACED 0x20 40 41 // Describes image parameters that usually stay constant. 42 // New fields can be added in the future. Code changing the parameters should 43 // usually copy the whole struct, so that fields added later will be preserved. 44 struct mp_image_params { 45 enum mp_imgfmt imgfmt; // pixel format 46 enum mp_imgfmt hw_subfmt; // underlying format for some hwaccel pixfmts 47 int w, h; // image dimensions 48 int p_w, p_h; // define pixel aspect ratio (undefined: 0/0) 49 struct mp_colorspace color; 50 enum mp_chroma_location chroma_location; 51 // The image should be rotated clockwise (0-359 degrees). 52 int rotate; 53 enum mp_stereo3d_mode stereo3d; // image is encoded with this mode 54 enum mp_alpha_type alpha; // usually auto; only set if explicitly known 55 }; 56 57 /* Memory management: 58 * - mp_image is a light-weight reference to the actual image data (pixels). 59 * The actual image data is reference counted and can outlive mp_image 60 * allocations. mp_image references can be created with mp_image_new_ref() 61 * and free'd with talloc_free() (the helpers mp_image_setrefp() and 62 * mp_image_unrefp() can also be used). The actual image data is free'd when 63 * the last mp_image reference to it is free'd. 64 * - Each mp_image has a clear owner. The owner can do anything with it, such 65 * as changing mp_image fields. Instead of making ownership ambiguous by 66 * sharing a mp_image reference, new references should be created. 67 * - Write access to the actual image data is allowed only after calling 68 * mp_image_make_writeable(), or if mp_image_is_writeable() returns true. 69 * Conceptually, images can be changed by their owner only, and copy-on-write 70 * is used to ensure that other references do not see any changes to the 71 * image data. mp_image_make_writeable() will do that copy if required. 72 */ 73 typedef struct mp_image { 74 int w, h; // visible dimensions (redundant with params.w/h) 75 76 struct mp_image_params params; 77 78 // fields redundant to params.imgfmt, for convenience or compatibility 79 struct mp_imgfmt_desc fmt; 80 enum mp_imgfmt imgfmt; 81 int num_planes; 82 83 uint8_t *planes[MP_MAX_PLANES]; 84 int stride[MP_MAX_PLANES]; 85 86 int pict_type; // 0->unknown, 1->I, 2->P, 3->B 87 int fields; 88 89 /* only inside filter chain */ 90 double pts; 91 /* only after decoder */ 92 double dts, pkt_duration; 93 /* container reported FPS; can be incorrect, or 0 if unknown */ 94 double nominal_fps; 95 /* for private use */ 96 void* priv; 97 98 // Reference-counted data references. 99 // These do not necessarily map directly to planes[]. They can have 100 // different order or count. There shouldn't be more buffers than planes. 101 // If bufs[n] is NULL, bufs[n+1] must also be NULL. 102 // All mp_* functions manage this automatically; do not mess with it. 103 // (See also AVFrame.buf.) 104 struct AVBufferRef *bufs[MP_MAX_PLANES]; 105 // Points to AVHWFramesContext* (same as AVFrame.hw_frames_ctx) 106 struct AVBufferRef *hwctx; 107 // Embedded ICC profile, if any 108 struct AVBufferRef *icc_profile; 109 // Closed captions packet, if any (only after decoder) 110 struct AVBufferRef *a53_cc; 111 // Other side data we don't care about. 112 struct mp_ff_side_data *ff_side_data; 113 int num_ff_side_data; 114 } mp_image_t; 115 116 struct mp_ff_side_data { 117 int type; 118 struct AVBufferRef *buf; 119 }; 120 121 int mp_chroma_div_up(int size, int shift); 122 123 int mp_image_get_alloc_size(int imgfmt, int w, int h, int stride_align); 124 struct mp_image *mp_image_from_buffer(int imgfmt, int w, int h, int stride_align, 125 uint8_t *buffer, int buffer_size, 126 void *free_opaque, 127 void (*free)(void *opaque, uint8_t *data)); 128 129 struct mp_image *mp_image_alloc(int fmt, int w, int h); 130 void mp_image_copy(struct mp_image *dmpi, struct mp_image *mpi); 131 void mp_image_copy_attributes(struct mp_image *dmpi, struct mp_image *mpi); 132 struct mp_image *mp_image_new_copy(struct mp_image *img); 133 struct mp_image *mp_image_new_ref(struct mp_image *img); 134 bool mp_image_is_writeable(struct mp_image *img); 135 bool mp_image_make_writeable(struct mp_image *img); 136 void mp_image_setrefp(struct mp_image **p_img, struct mp_image *new_value); 137 void mp_image_unrefp(struct mp_image **p_img); 138 139 void mp_image_clear(struct mp_image *mpi, int x0, int y0, int x1, int y1); 140 void mp_image_clear_rc(struct mp_image *mpi, struct mp_rect rc); 141 void mp_image_clear_rc_inv(struct mp_image *mpi, struct mp_rect rc); 142 void mp_image_crop(struct mp_image *img, int x0, int y0, int x1, int y1); 143 void mp_image_crop_rc(struct mp_image *img, struct mp_rect rc); 144 void mp_image_vflip(struct mp_image *img); 145 146 void mp_image_set_size(struct mp_image *mpi, int w, int h); 147 int mp_image_plane_w(struct mp_image *mpi, int plane); 148 int mp_image_plane_h(struct mp_image *mpi, int plane); 149 150 void mp_image_setfmt(mp_image_t* mpi, int out_fmt); 151 void mp_image_steal_data(struct mp_image *dst, struct mp_image *src); 152 void mp_image_unref_data(struct mp_image *img); 153 154 int mp_image_approx_byte_size(struct mp_image *img); 155 156 struct mp_image *mp_image_new_dummy_ref(struct mp_image *img); 157 struct mp_image *mp_image_new_custom_ref(struct mp_image *img, void *arg, 158 void (*free)(void *arg)); 159 160 void mp_image_params_guess_csp(struct mp_image_params *params); 161 162 char *mp_image_params_to_str_buf(char *b, size_t bs, 163 const struct mp_image_params *p); 164 #define mp_image_params_to_str(p) mp_image_params_to_str_buf((char[256]){0}, 256, p) 165 166 bool mp_image_params_valid(const struct mp_image_params *p); 167 bool mp_image_params_equal(const struct mp_image_params *p1, 168 const struct mp_image_params *p2); 169 170 void mp_image_params_get_dsize(const struct mp_image_params *p, 171 int *d_w, int *d_h); 172 void mp_image_params_set_dsize(struct mp_image_params *p, int d_w, int d_h); 173 174 void mp_image_set_params(struct mp_image *image, 175 const struct mp_image_params *params); 176 177 void mp_image_set_attributes(struct mp_image *image, 178 const struct mp_image_params *params); 179 180 struct AVFrame; 181 struct mp_image *mp_image_from_av_frame(struct AVFrame *av_frame); 182 struct AVFrame *mp_image_to_av_frame(struct mp_image *img); 183 struct AVFrame *mp_image_to_av_frame_and_unref(struct mp_image *img); 184 185 void memcpy_pic(void *dst, const void *src, int bytesPerLine, int height, 186 int dstStride, int srcStride); 187 void memset_pic(void *dst, int fill, int bytesPerLine, int height, int stride); 188 void memset16_pic(void *dst, int fill, int unitsPerLine, int height, int stride); 189 190 void *mp_image_pixel_ptr(struct mp_image *img, int plane, int x, int y); 191 void *mp_image_pixel_ptr_ny(struct mp_image *img, int plane, int x, int y); 192 size_t mp_image_plane_bytes(struct mp_image *img, int plane, int x0, int w); 193 194 #endif /* MPLAYER_MP_IMAGE_H */ 195