/src/mpv/video/mp_image.c
Line | Count | Source |
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 | | #include <limits.h> |
19 | | #include <assert.h> |
20 | | |
21 | | #include <libavutil/mem.h> |
22 | | #include <libavutil/common.h> |
23 | | #include <libavutil/display.h> |
24 | | #include <libavutil/dovi_meta.h> |
25 | | #include <libavutil/bswap.h> |
26 | | #include <libavutil/hwcontext.h> |
27 | | #include <libavutil/intreadwrite.h> |
28 | | #include <libavutil/rational.h> |
29 | | #include <libavutil/stereo3d.h> |
30 | | #include <libavcodec/avcodec.h> |
31 | | #include <libavutil/mastering_display_metadata.h> |
32 | | #include <libplacebo/utils/libav.h> |
33 | | |
34 | | #include "mpv_talloc.h" |
35 | | |
36 | | #include "common/av_common.h" |
37 | | #include "common/common.h" |
38 | | #include "fmt-conversion.h" |
39 | | #include "hwdec.h" |
40 | | #include "mp_image.h" |
41 | | #include "osdep/threads.h" |
42 | | #include "sws_utils.h" |
43 | | #include "out/placebo/utils.h" |
44 | | |
45 | | // Determine strides, plane sizes, and total required size for an image |
46 | | // allocation. Returns total size on success, <0 on error. Unused planes |
47 | | // have out_stride/out_plane_size to 0, and out_plane_offset set to -1 up |
48 | | // until MP_MAX_PLANES-1. |
49 | | static int mp_image_layout(int imgfmt, int w, int h, int stride_align, |
50 | | int out_stride[MP_MAX_PLANES], |
51 | | int out_plane_offset[MP_MAX_PLANES], |
52 | | int out_plane_size[MP_MAX_PLANES]) |
53 | 0 | { |
54 | 0 | struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(imgfmt); |
55 | |
|
56 | 0 | struct mp_image_params params = {.imgfmt = imgfmt, .w = w, .h = h}; |
57 | |
|
58 | 0 | if (!mp_image_params_valid(¶ms) || desc.flags & MP_IMGFLAG_HWACCEL) |
59 | 0 | return -1; |
60 | | |
61 | 0 | w = MP_ALIGN_UP(w, desc.align_x); |
62 | 0 | h = MP_ALIGN_UP(h, desc.align_y); |
63 | | |
64 | | // Note: for non-mod-2 4:2:0 YUV frames, we have to allocate an additional |
65 | | // top/right border. This is needed for correct handling of such |
66 | | // images in filter and VO code (e.g. vo_vdpau or vo_gpu). |
67 | |
|
68 | 0 | for (int n = 0; n < MP_MAX_PLANES; n++) { |
69 | 0 | int alloc_w = mp_chroma_div_up(w, desc.xs[n]); |
70 | 0 | int alloc_h = MP_ALIGN_UP(h, 32) >> desc.ys[n]; |
71 | 0 | int line_bytes = (alloc_w * desc.bpp[n] + 7) / 8; |
72 | 0 | int align = mp_lcm(stride_align, (desc.bpp[n] && desc.bpp[n] % 8 == 0) ? desc.bpp[n] / 8 : 1); |
73 | 0 | out_stride[n] = MP_ALIGN_NPOT(line_bytes, align); |
74 | 0 | int64_t plane_size = (int64_t)out_stride[n] * alloc_h; |
75 | 0 | if (plane_size < 0 || plane_size > INT_MAX) |
76 | 0 | return -1; |
77 | 0 | out_plane_size[n] = plane_size; |
78 | 0 | } |
79 | 0 | if (desc.flags & MP_IMGFLAG_PAL) |
80 | 0 | out_plane_size[1] = AVPALETTE_SIZE; |
81 | | |
82 | | // Leave room for the stride_align bytes the callers add to the size. |
83 | 0 | int sum = 0; |
84 | 0 | for (int n = 0; n < MP_MAX_PLANES; n++) { |
85 | 0 | if (out_plane_size[n] > INT_MAX - stride_align - sum) |
86 | 0 | return -1; |
87 | 0 | out_plane_offset[n] = out_plane_size[n] ? sum : -1; |
88 | 0 | sum += out_plane_size[n]; |
89 | 0 | } |
90 | | |
91 | 0 | return sum; |
92 | 0 | } |
93 | | |
94 | | // Return the total size needed for an image allocation of the given |
95 | | // configuration (imgfmt, w, h must be set). Returns -1 on error. |
96 | | // Assumes the allocation is already aligned on stride_align (otherwise you |
97 | | // need to add padding yourself). |
98 | | int mp_image_get_alloc_size(int imgfmt, int w, int h, int stride_align) |
99 | 0 | { |
100 | 0 | int stride[MP_MAX_PLANES]; |
101 | 0 | int plane_offset[MP_MAX_PLANES]; |
102 | 0 | int plane_size[MP_MAX_PLANES]; |
103 | 0 | return mp_image_layout(imgfmt, w, h, stride_align, stride, plane_offset, |
104 | 0 | plane_size); |
105 | 0 | } |
106 | | |
107 | | // Fill the mpi->planes and mpi->stride fields of the given mpi with data |
108 | | // from buffer according to the mpi's w/h/imgfmt fields. See mp_image_from_buffer |
109 | | // aboud remarks how to allocate/use buffer/buffer_size. |
110 | | // This does not free the data. You are expected to setup refcounting by |
111 | | // setting mp_image.bufs before or after this function is called. |
112 | | // Returns true on success, false on failure. |
113 | | static bool mp_image_fill_alloc(struct mp_image *mpi, int stride_align, |
114 | | void *buffer, int buffer_size) |
115 | 0 | { |
116 | 0 | int stride[MP_MAX_PLANES]; |
117 | 0 | int plane_offset[MP_MAX_PLANES]; |
118 | 0 | int plane_size[MP_MAX_PLANES]; |
119 | 0 | int size = mp_image_layout(mpi->imgfmt, mpi->w, mpi->h, stride_align, |
120 | 0 | stride, plane_offset, plane_size); |
121 | 0 | if (size < 0 || size > buffer_size) |
122 | 0 | return false; |
123 | | |
124 | 0 | int align = MP_ALIGN_UP((uintptr_t)buffer, stride_align) - (uintptr_t)buffer; |
125 | 0 | if (buffer_size - size < align) |
126 | 0 | return false; |
127 | 0 | uint8_t *s = buffer; |
128 | 0 | s += align; |
129 | |
|
130 | 0 | for (int n = 0; n < MP_MAX_PLANES; n++) { |
131 | 0 | mpi->planes[n] = plane_offset[n] >= 0 ? s + plane_offset[n] : NULL; |
132 | 0 | mpi->stride[n] = stride[n]; |
133 | 0 | } |
134 | |
|
135 | 0 | return true; |
136 | 0 | } |
137 | | |
138 | | // Create a mp_image from the provided buffer. The mp_image is filled according |
139 | | // to the imgfmt/w/h parameters, and respecting the stride_align parameter to |
140 | | // align the plane start pointers and strides. Once the last reference to the |
141 | | // returned image is destroyed, free(free_opaque, buffer) is called. (Be aware |
142 | | // that this can happen from any thread.) |
143 | | // The allocated size of buffer must be given by buffer_size. buffer_size should |
144 | | // be at least the value returned by mp_image_get_alloc_size(). If buffer is not |
145 | | // already aligned to stride_align, the function will attempt to align the |
146 | | // pointer itself by incrementing the buffer pointer until their alignment is |
147 | | // achieved (if buffer_size is not large enough to allow aligning the buffer |
148 | | // safely, the function fails). To be safe, you may want to overallocate the |
149 | | // buffer by stride_align bytes, and include the overallocation in buffer_size. |
150 | | // Returns NULL on failure. On failure, the free() callback is not called. |
151 | | struct mp_image *mp_image_from_buffer(int imgfmt, int w, int h, int stride_align, |
152 | | uint8_t *buffer, int buffer_size, |
153 | | void *free_opaque, |
154 | | void (*free)(void *opaque, uint8_t *data)) |
155 | 0 | { |
156 | 0 | struct mp_image *mpi = mp_image_new_dummy_ref(NULL); |
157 | 0 | mp_image_setfmt(mpi, imgfmt); |
158 | 0 | mp_image_set_size(mpi, w, h); |
159 | |
|
160 | 0 | if (!mp_image_fill_alloc(mpi, stride_align, buffer, buffer_size)) |
161 | 0 | goto fail; |
162 | | |
163 | 0 | mpi->bufs[0] = av_buffer_create(buffer, buffer_size, free, free_opaque, 0); |
164 | 0 | if (!mpi->bufs[0]) |
165 | 0 | goto fail; |
166 | | |
167 | 0 | return mpi; |
168 | | |
169 | 0 | fail: |
170 | 0 | talloc_free(mpi); |
171 | 0 | return NULL; |
172 | 0 | } |
173 | | |
174 | | static bool mp_image_alloc_planes(struct mp_image *mpi) |
175 | 0 | { |
176 | 0 | mp_assert(!mpi->planes[0]); |
177 | 0 | mp_assert(!mpi->bufs[0]); |
178 | | |
179 | 0 | int align = MP_IMAGE_BYTE_ALIGN; |
180 | |
|
181 | 0 | int size = mp_image_get_alloc_size(mpi->imgfmt, mpi->w, mpi->h, align); |
182 | 0 | if (size < 0) |
183 | 0 | return false; |
184 | | |
185 | | // Note: mp_image_pool assumes this creates only 1 AVBufferRef. |
186 | 0 | mpi->bufs[0] = av_buffer_alloc(size + align); |
187 | 0 | if (!mpi->bufs[0]) |
188 | 0 | return false; |
189 | | |
190 | 0 | if (!mp_image_fill_alloc(mpi, align, mpi->bufs[0]->data, mpi->bufs[0]->size)) { |
191 | 0 | av_buffer_unref(&mpi->bufs[0]); |
192 | 0 | return false; |
193 | 0 | } |
194 | | |
195 | 0 | return true; |
196 | 0 | } |
197 | | |
198 | | void mp_image_sethwfmt(struct mp_image *mpi, enum mp_imgfmt hw_fmt, enum mp_imgfmt sw_fmt) |
199 | 74.3k | { |
200 | 74.3k | struct mp_imgfmt_desc fmt = mp_imgfmt_get_desc(sw_fmt ? sw_fmt : hw_fmt); |
201 | 74.3k | mpi->params.imgfmt = hw_fmt; |
202 | 74.3k | mpi->params.hw_subfmt = sw_fmt; |
203 | 74.3k | mpi->fmt = fmt; |
204 | 74.3k | mpi->imgfmt = hw_fmt; |
205 | 74.3k | mpi->num_planes = fmt.num_planes; |
206 | 74.3k | mpi->params.repr.alpha = (fmt.flags & MP_IMGFLAG_ALPHA) ? PL_ALPHA_INDEPENDENT |
207 | 74.3k | : PL_ALPHA_NONE; |
208 | | // Calculate bit encoding from all components (excluding alpha) |
209 | 74.3k | struct pl_bit_encoding bits = {0}; |
210 | 74.3k | const int num_comps = mp_imgfmt_desc_get_num_comps(&fmt); |
211 | 287k | for (int c = 0; c < MPMIN(num_comps, 3); c++) { |
212 | 213k | struct pl_bit_encoding cbits = { |
213 | 213k | .sample_depth = fmt.comps[c].size, |
214 | 213k | .color_depth = fmt.comps[c].size - abs(fmt.comps[c].pad), |
215 | 213k | .bit_shift = MPMAX(fmt.comps[c].pad, 0), |
216 | 213k | }; |
217 | | |
218 | 213k | if (bits.sample_depth && !pl_bit_encoding_equal(&bits, &cbits)) { |
219 | | // Bit encoding differs between components, cannot handle this |
220 | 0 | bits = (struct pl_bit_encoding) {0}; |
221 | 0 | break; |
222 | 0 | } |
223 | | |
224 | 213k | bits = cbits; |
225 | 213k | } |
226 | 74.3k | mpi->params.repr.bits = bits; |
227 | 74.3k | } |
228 | | |
229 | | void mp_image_setfmt(struct mp_image *mpi, enum mp_imgfmt fmt) |
230 | 0 | { |
231 | 0 | mp_image_sethwfmt(mpi, fmt, IMGFMT_NONE); |
232 | 0 | } |
233 | | |
234 | | static void mp_image_destructor(void *ptr) |
235 | 162k | { |
236 | 162k | mp_image_t *mpi = ptr; |
237 | 810k | for (int p = 0; p < MP_MAX_PLANES; p++) |
238 | 648k | av_buffer_unref(&mpi->bufs[p]); |
239 | 162k | av_buffer_unref(&mpi->hwctx); |
240 | 162k | av_buffer_unref(&mpi->icc_profile); |
241 | 162k | av_buffer_unref(&mpi->a53_cc); |
242 | 162k | av_buffer_unref(&mpi->dovi); |
243 | 162k | av_buffer_unref(&mpi->film_grain); |
244 | 200k | for (int n = 0; n < mpi->num_ff_side_data; n++) |
245 | 38.4k | av_buffer_unref(&mpi->ff_side_data[n].buf); |
246 | 162k | talloc_free(mpi->ff_side_data); |
247 | 162k | mp_image_unrefp(&mpi->enhancement_layer); |
248 | 162k | } |
249 | | |
250 | | int mp_chroma_div_up(int size, int shift) |
251 | 0 | { |
252 | 0 | return (size + (1 << shift) - 1) >> shift; |
253 | 0 | } |
254 | | |
255 | | // Return the storage width in pixels of the given plane. |
256 | | int mp_image_plane_w(struct mp_image *mpi, int plane) |
257 | 0 | { |
258 | 0 | return mp_chroma_div_up(mpi->w, mpi->fmt.xs[plane]); |
259 | 0 | } |
260 | | |
261 | | // Return the storage height in pixels of the given plane. |
262 | | int mp_image_plane_h(struct mp_image *mpi, int plane) |
263 | 0 | { |
264 | 0 | return mp_chroma_div_up(mpi->h, mpi->fmt.ys[plane]); |
265 | 0 | } |
266 | | |
267 | | // Caller has to make sure this doesn't exceed the allocated plane data/strides. |
268 | | void mp_image_set_size(struct mp_image *mpi, int w, int h) |
269 | 74.3k | { |
270 | 74.3k | mp_assert(w >= 0 && h >= 0); |
271 | 74.3k | mpi->w = mpi->params.w = w; |
272 | 74.3k | mpi->h = mpi->params.h = h; |
273 | 74.3k | } |
274 | | |
275 | | void mp_image_set_params(struct mp_image *image, |
276 | | const struct mp_image_params *params) |
277 | 0 | { |
278 | | // possibly initialize other stuff |
279 | 0 | mp_image_setfmt(image, params->imgfmt); |
280 | 0 | mp_image_set_size(image, params->w, params->h); |
281 | 0 | image->params = *params; |
282 | 0 | } |
283 | | |
284 | | struct mp_image *mp_image_alloc(int imgfmt, int w, int h) |
285 | 0 | { |
286 | 0 | struct mp_image *mpi = talloc_zero(NULL, struct mp_image); |
287 | 0 | talloc_set_destructor(mpi, mp_image_destructor); |
288 | |
|
289 | 0 | mp_image_set_size(mpi, w, h); |
290 | 0 | mp_image_setfmt(mpi, imgfmt); |
291 | 0 | if (!mp_image_alloc_planes(mpi)) { |
292 | 0 | talloc_free(mpi); |
293 | 0 | return NULL; |
294 | 0 | } |
295 | 0 | return mpi; |
296 | 0 | } |
297 | | |
298 | | int mp_image_approx_byte_size(struct mp_image *img) |
299 | 0 | { |
300 | 0 | int total = sizeof(*img); |
301 | |
|
302 | 0 | for (int n = 0; n < MP_MAX_PLANES; n++) { |
303 | 0 | struct AVBufferRef *buf = img->bufs[n]; |
304 | 0 | if (buf) |
305 | 0 | total += buf->size; |
306 | 0 | } |
307 | |
|
308 | 0 | return total; |
309 | 0 | } |
310 | | |
311 | | struct mp_image *mp_image_new_copy(struct mp_image *img) |
312 | 0 | { |
313 | 0 | struct mp_image *new = mp_image_alloc(img->imgfmt, img->w, img->h); |
314 | 0 | if (!new) |
315 | 0 | return NULL; |
316 | 0 | mp_image_copy(new, img); |
317 | 0 | mp_image_copy_attributes(new, img); |
318 | 0 | return new; |
319 | 0 | } |
320 | | |
321 | | // Make dst take over the image data of src, and free src. |
322 | | // This is basically a safe version of *dst = *src; free(src); |
323 | | // Only works with ref-counted images, and can't change image size/format. |
324 | | void mp_image_steal_data(struct mp_image *dst, struct mp_image *src) |
325 | 0 | { |
326 | 0 | mp_assert(dst->imgfmt == src->imgfmt && dst->w == src->w && dst->h == src->h); |
327 | 0 | mp_assert(dst->bufs[0] && src->bufs[0]); |
328 | | |
329 | 0 | mp_image_destructor(dst); // unref old |
330 | 0 | talloc_free_children(dst); |
331 | |
|
332 | 0 | *dst = *src; |
333 | |
|
334 | 0 | *src = (struct mp_image){0}; |
335 | 0 | talloc_free(src); |
336 | 0 | } |
337 | | |
338 | | // Unref most data buffer (and clear the data array), but leave other fields |
339 | | // allocated. In particular, mp_image.hwctx is preserved. |
340 | | void mp_image_unref_data(struct mp_image *img) |
341 | 0 | { |
342 | 0 | for (int n = 0; n < MP_MAX_PLANES; n++) { |
343 | 0 | img->planes[n] = NULL; |
344 | 0 | img->stride[n] = 0; |
345 | 0 | av_buffer_unref(&img->bufs[n]); |
346 | 0 | } |
347 | 0 | } |
348 | | |
349 | | static void ref_buffer(AVBufferRef **dst) |
350 | 1.49M | { |
351 | 1.49M | if (*dst) { |
352 | 392k | *dst = av_buffer_ref(*dst); |
353 | 392k | MP_HANDLE_OOM(*dst); |
354 | 392k | } |
355 | 1.49M | } |
356 | | |
357 | | // Return a new reference to img. The returned reference is owned by the caller, |
358 | | // while img is left untouched. |
359 | | struct mp_image *mp_image_new_ref(struct mp_image *img) |
360 | 162k | { |
361 | 162k | if (!img) |
362 | 0 | return NULL; |
363 | | |
364 | 162k | if (!img->bufs[0]) |
365 | 0 | return mp_image_new_copy(img); |
366 | | |
367 | 162k | struct mp_image *new = talloc_ptrtype(NULL, new); |
368 | 162k | talloc_set_destructor(new, mp_image_destructor); |
369 | 162k | *new = *img; |
370 | | |
371 | 810k | for (int p = 0; p < MP_MAX_PLANES; p++) |
372 | 648k | ref_buffer(&new->bufs[p]); |
373 | | |
374 | 162k | ref_buffer(&new->hwctx); |
375 | 162k | ref_buffer(&new->icc_profile); |
376 | 162k | ref_buffer(&new->a53_cc); |
377 | 162k | ref_buffer(&new->dovi); |
378 | 162k | ref_buffer(&new->film_grain); |
379 | | |
380 | 162k | new->ff_side_data = talloc_memdup(NULL, new->ff_side_data, |
381 | 162k | new->num_ff_side_data * sizeof(new->ff_side_data[0])); |
382 | 200k | for (int n = 0; n < new->num_ff_side_data; n++) |
383 | 38.4k | ref_buffer(&new->ff_side_data[n].buf); |
384 | | |
385 | 162k | new->enhancement_layer = img->enhancement_layer |
386 | 162k | ? mp_image_new_ref(img->enhancement_layer) : NULL; |
387 | | |
388 | 162k | return new; |
389 | 162k | } |
390 | | |
391 | | struct free_args { |
392 | | void *arg; |
393 | | void (*free)(void *arg); |
394 | | }; |
395 | | |
396 | | static void call_free(void *opaque, uint8_t *data) |
397 | 0 | { |
398 | 0 | struct free_args *args = opaque; |
399 | 0 | args->free(args->arg); |
400 | 0 | talloc_free(args); |
401 | 0 | } |
402 | | |
403 | | // Create a new mp_image based on img, but don't set any buffers. |
404 | | // Using this is only valid until the original img is unreferenced (including |
405 | | // implicit unreferencing of the data by mp_image_make_writeable()), unless |
406 | | // a new reference is set. |
407 | | struct mp_image *mp_image_new_dummy_ref(struct mp_image *img) |
408 | 0 | { |
409 | 0 | struct mp_image *new = talloc_ptrtype(NULL, new); |
410 | 0 | talloc_set_destructor(new, mp_image_destructor); |
411 | 0 | *new = img ? *img : (struct mp_image){0}; |
412 | 0 | for (int p = 0; p < MP_MAX_PLANES; p++) |
413 | 0 | new->bufs[p] = NULL; |
414 | 0 | new->hwctx = NULL; |
415 | 0 | new->icc_profile = NULL; |
416 | 0 | new->a53_cc = NULL; |
417 | 0 | new->dovi = NULL; |
418 | 0 | new->film_grain = NULL; |
419 | 0 | new->num_ff_side_data = 0; |
420 | 0 | new->ff_side_data = NULL; |
421 | 0 | new->enhancement_layer = NULL; |
422 | 0 | return new; |
423 | 0 | } |
424 | | |
425 | | // Return a reference counted reference to img. If the reference count reaches |
426 | | // 0, call free(free_arg). The data passed by img must not be free'd before |
427 | | // that. The new reference will be writeable. |
428 | | // On allocation failure, unref the frame and return NULL. |
429 | | // This is only used for hw decoding; this is important, because libav* expects |
430 | | // all plane data to be accounted for by AVBufferRefs. |
431 | | struct mp_image *mp_image_new_custom_ref(struct mp_image *img, void *free_arg, |
432 | | void (*free)(void *arg)) |
433 | 0 | { |
434 | 0 | struct mp_image *new = mp_image_new_dummy_ref(img); |
435 | |
|
436 | 0 | struct free_args *args = talloc_ptrtype(NULL, args); |
437 | 0 | *args = (struct free_args){free_arg, free}; |
438 | 0 | new->bufs[0] = av_buffer_create(NULL, 0, call_free, args, |
439 | 0 | AV_BUFFER_FLAG_READONLY); |
440 | 0 | if (new->bufs[0]) |
441 | 0 | return new; |
442 | 0 | talloc_free(new); |
443 | 0 | return NULL; |
444 | 0 | } |
445 | | |
446 | | bool mp_image_is_writeable(struct mp_image *img) |
447 | 0 | { |
448 | 0 | if (!img->bufs[0]) |
449 | 0 | return true; // not ref-counted => always considered writeable |
450 | 0 | for (int p = 0; p < MP_MAX_PLANES; p++) { |
451 | 0 | if (!img->bufs[p]) |
452 | 0 | break; |
453 | 0 | if (!av_buffer_is_writable(img->bufs[p])) |
454 | 0 | return false; |
455 | 0 | } |
456 | 0 | return true; |
457 | 0 | } |
458 | | |
459 | | // Make the image data referenced by img writeable. This allocates new data |
460 | | // if the data wasn't already writeable, and img->planes[] and img->stride[] |
461 | | // will be set to the copy. |
462 | | // Returns success; if false is returned, the image could not be made writeable. |
463 | | bool mp_image_make_writeable(struct mp_image *img) |
464 | 0 | { |
465 | 0 | if (mp_image_is_writeable(img)) |
466 | 0 | return true; |
467 | | |
468 | 0 | struct mp_image *new = mp_image_new_copy(img); |
469 | 0 | if (!new) |
470 | 0 | return false; |
471 | 0 | mp_image_steal_data(img, new); |
472 | 0 | mp_assert(mp_image_is_writeable(img)); |
473 | 0 | return true; |
474 | 0 | } |
475 | | |
476 | | // Helper function: unrefs *p_img, and sets *p_img to a new ref of new_value. |
477 | | // Only unrefs *p_img and sets it to NULL if out of memory. |
478 | | void mp_image_setrefp(struct mp_image **p_img, struct mp_image *new_value) |
479 | 0 | { |
480 | 0 | if (*p_img != new_value) { |
481 | 0 | talloc_free(*p_img); |
482 | 0 | *p_img = new_value ? mp_image_new_ref(new_value) : NULL; |
483 | 0 | } |
484 | 0 | } |
485 | | |
486 | | // Mere helper function (mp_image can be directly free'd with talloc_free) |
487 | | void mp_image_unrefp(struct mp_image **p_img) |
488 | 473k | { |
489 | 473k | talloc_free(*p_img); |
490 | 473k | *p_img = NULL; |
491 | 473k | } |
492 | | |
493 | | void memcpy_pic(void *dst, const void *src, int bytesPerLine, int height, |
494 | | int dstStride, int srcStride) |
495 | 0 | { |
496 | 0 | if (bytesPerLine == dstStride && dstStride == srcStride && height) { |
497 | 0 | if (srcStride < 0) { |
498 | 0 | src = (uint8_t*)src + (height - 1) * srcStride; |
499 | 0 | dst = (uint8_t*)dst + (height - 1) * dstStride; |
500 | 0 | srcStride = -srcStride; |
501 | 0 | } |
502 | |
|
503 | 0 | memcpy(dst, src, srcStride * (height - 1) + bytesPerLine); |
504 | 0 | } else { |
505 | 0 | for (int i = 0; i < height; i++) { |
506 | 0 | memcpy(dst, src, bytesPerLine); |
507 | 0 | src = (uint8_t*)src + srcStride; |
508 | 0 | dst = (uint8_t*)dst + dstStride; |
509 | 0 | } |
510 | 0 | } |
511 | 0 | } |
512 | | |
513 | | void mp_image_copy(struct mp_image *dst, struct mp_image *src) |
514 | 0 | { |
515 | 0 | mp_assert(dst->imgfmt == src->imgfmt); |
516 | 0 | mp_assert(dst->w == src->w && dst->h == src->h); |
517 | 0 | mp_assert(mp_image_is_writeable(dst)); |
518 | 0 | for (int n = 0; n < dst->num_planes; n++) { |
519 | 0 | int line_bytes = (mp_image_plane_w(dst, n) * dst->fmt.bpp[n] + 7) / 8; |
520 | 0 | int plane_h = mp_image_plane_h(dst, n); |
521 | 0 | memcpy_pic(dst->planes[n], src->planes[n], line_bytes, plane_h, |
522 | 0 | dst->stride[n], src->stride[n]); |
523 | 0 | } |
524 | 0 | if (dst->fmt.flags & MP_IMGFLAG_PAL) |
525 | 0 | memcpy(dst->planes[1], src->planes[1], AVPALETTE_SIZE); |
526 | 0 | } |
527 | | |
528 | | static enum pl_color_system mp_image_params_get_forced_csp(struct mp_image_params *params) |
529 | 3.10k | { |
530 | 3.10k | int imgfmt = params->hw_subfmt ? params->hw_subfmt : params->imgfmt; |
531 | 3.10k | enum pl_color_system csp = mp_imgfmt_get_forced_csp(imgfmt); |
532 | | |
533 | 3.10k | if (csp == PL_COLOR_SYSTEM_RGB && params->repr.sys == PL_COLOR_SYSTEM_XYZ) |
534 | 0 | csp = PL_COLOR_SYSTEM_XYZ; |
535 | | |
536 | 3.10k | return csp; |
537 | 3.10k | } |
538 | | |
539 | | static void assign_bufref(AVBufferRef **dst, AVBufferRef *new) |
540 | 0 | { |
541 | 0 | av_buffer_unref(dst); |
542 | 0 | if (new) { |
543 | 0 | *dst = av_buffer_ref(new); |
544 | 0 | MP_HANDLE_OOM(*dst); |
545 | 0 | } |
546 | 0 | } |
547 | | |
548 | | void mp_image_copy_attributes(struct mp_image *dst, struct mp_image *src) |
549 | 0 | { |
550 | 0 | mp_assert(dst != src); |
551 | | |
552 | 0 | dst->pict_type = src->pict_type; |
553 | 0 | dst->fields = src->fields; |
554 | 0 | dst->pts = src->pts; |
555 | 0 | dst->dts = src->dts; |
556 | 0 | dst->pkt_duration = src->pkt_duration; |
557 | 0 | dst->params.vflip = src->params.vflip; |
558 | 0 | dst->params.rotate = src->params.rotate; |
559 | 0 | dst->params.stereo3d = src->params.stereo3d; |
560 | 0 | dst->params.p_w = src->params.p_w; |
561 | 0 | dst->params.p_h = src->params.p_h; |
562 | 0 | dst->params.color = src->params.color; |
563 | 0 | dst->params.repr = src->params.repr; |
564 | 0 | dst->params.light = src->params.light; |
565 | 0 | dst->params.chroma_location = src->params.chroma_location; |
566 | 0 | dst->params.crop = src->params.crop; |
567 | 0 | dst->nominal_fps = src->nominal_fps; |
568 | 0 | dst->params.primaries_orig = src->params.primaries_orig; |
569 | 0 | dst->params.transfer_orig = src->params.transfer_orig; |
570 | 0 | dst->params.sys_orig = src->params.sys_orig; |
571 | 0 | dst->params.no_dovi = src->params.no_dovi; |
572 | 0 | dst->params.no_enhancement_layer = src->params.no_enhancement_layer; |
573 | | |
574 | | // ensure colorspace consistency |
575 | 0 | enum pl_color_system dst_forced_csp = mp_image_params_get_forced_csp(&dst->params); |
576 | 0 | if (mp_image_params_get_forced_csp(&src->params) != dst_forced_csp) { |
577 | 0 | dst->params.repr.sys = dst_forced_csp != PL_COLOR_SYSTEM_UNKNOWN ? |
578 | 0 | dst_forced_csp : |
579 | 0 | mp_csp_guess_colorspace(src->w, src->h); |
580 | 0 | } |
581 | |
|
582 | 0 | if ((dst->fmt.flags & MP_IMGFLAG_PAL) && (src->fmt.flags & MP_IMGFLAG_PAL)) { |
583 | 0 | if (dst->planes[1] && src->planes[1]) { |
584 | 0 | if (mp_image_make_writeable(dst)) |
585 | 0 | memcpy(dst->planes[1], src->planes[1], AVPALETTE_SIZE); |
586 | 0 | } |
587 | 0 | } |
588 | 0 | assign_bufref(&dst->icc_profile, src->icc_profile); |
589 | 0 | assign_bufref(&dst->dovi, src->dovi); |
590 | 0 | assign_bufref(&dst->film_grain, src->film_grain); |
591 | 0 | assign_bufref(&dst->a53_cc, src->a53_cc); |
592 | |
|
593 | 0 | for (int n = 0; n < dst->num_ff_side_data; n++) |
594 | 0 | av_buffer_unref(&dst->ff_side_data[n].buf); |
595 | |
|
596 | 0 | MP_RESIZE_ARRAY(NULL, dst->ff_side_data, src->num_ff_side_data); |
597 | 0 | dst->num_ff_side_data = src->num_ff_side_data; |
598 | |
|
599 | 0 | for (int n = 0; n < dst->num_ff_side_data; n++) { |
600 | 0 | dst->ff_side_data[n].type = src->ff_side_data[n].type; |
601 | 0 | dst->ff_side_data[n].buf = av_buffer_ref(src->ff_side_data[n].buf); |
602 | 0 | MP_HANDLE_OOM(dst->ff_side_data[n].buf); |
603 | 0 | } |
604 | | |
605 | 0 | mp_image_setrefp(&dst->enhancement_layer, src->enhancement_layer); |
606 | 0 | } |
607 | | |
608 | | // Crop the given image to (x0, y0)-(x1, y1) (bottom/right border exclusive) |
609 | | // x0/y0 must be naturally aligned. |
610 | | void mp_image_crop(struct mp_image *img, int x0, int y0, int x1, int y1) |
611 | 0 | { |
612 | 0 | mp_assert(x0 >= 0 && y0 >= 0); |
613 | 0 | mp_assert(x0 <= x1 && y0 <= y1); |
614 | 0 | mp_assert(x1 <= img->w && y1 <= img->h); |
615 | 0 | mp_assert(!(x0 & (img->fmt.align_x - 1))); |
616 | 0 | mp_assert(!(y0 & (img->fmt.align_y - 1))); |
617 | | |
618 | 0 | for (int p = 0; p < img->num_planes; ++p) { |
619 | 0 | img->planes[p] += (y0 >> img->fmt.ys[p]) * img->stride[p] + |
620 | 0 | (x0 >> img->fmt.xs[p]) * img->fmt.bpp[p] / 8; |
621 | 0 | } |
622 | 0 | mp_image_set_size(img, x1 - x0, y1 - y0); |
623 | 0 | } |
624 | | |
625 | | void mp_image_crop_rc(struct mp_image *img, struct mp_rect rc) |
626 | 0 | { |
627 | 0 | mp_image_crop(img, rc.x0, rc.y0, rc.x1, rc.y1); |
628 | 0 | } |
629 | | |
630 | | // Repeatedly write count patterns of src[0..src_size] to p. |
631 | | static void memset_pattern(void *p, size_t count, uint8_t *src, size_t src_size) |
632 | 0 | { |
633 | 0 | mp_assert(src_size >= 1); |
634 | | |
635 | 0 | if (src_size == 1) { |
636 | 0 | memset(p, src[0], count); |
637 | 0 | } else if (src_size == 2) { // >8 bit YUV => common, be slightly less naive |
638 | 0 | uint16_t val; |
639 | 0 | memcpy(&val, src, 2); |
640 | 0 | uint16_t *p16 = p; |
641 | 0 | while (count--) |
642 | 0 | *p16++ = val; |
643 | 0 | } else { |
644 | 0 | while (count--) { |
645 | 0 | memcpy(p, src, src_size); |
646 | 0 | p = (char *)p + src_size; |
647 | 0 | } |
648 | 0 | } |
649 | 0 | } |
650 | | |
651 | | static bool endian_swap_bytes(void *d, size_t bytes, size_t word_size) |
652 | 0 | { |
653 | 0 | if (word_size != 2 && word_size != 4) |
654 | 0 | return false; |
655 | | |
656 | 0 | size_t num_words = bytes / word_size; |
657 | 0 | uint8_t *ud = d; |
658 | |
|
659 | 0 | switch (word_size) { |
660 | 0 | case 2: |
661 | 0 | for (size_t x = 0; x < num_words; x++) |
662 | 0 | AV_WL16(ud + x * 2, AV_RB16(ud + x * 2)); |
663 | 0 | break; |
664 | 0 | case 4: |
665 | 0 | for (size_t x = 0; x < num_words; x++) |
666 | 0 | AV_WL32(ud + x * 2, AV_RB32(ud + x * 2)); |
667 | 0 | break; |
668 | 0 | default: |
669 | 0 | MP_ASSERT_UNREACHABLE(); |
670 | 0 | } |
671 | | |
672 | 0 | return true; |
673 | 0 | } |
674 | | |
675 | | // Bottom/right border is allowed not to be aligned, but it might implicitly |
676 | | // overwrite pixel data until the alignment (align_x/align_y) is reached. |
677 | | // Alpha is cleared to 0 (fully transparent). |
678 | | void mp_image_clear(struct mp_image *img, int x0, int y0, int x1, int y1) |
679 | 0 | { |
680 | 0 | mp_assert(x0 >= 0 && y0 >= 0); |
681 | 0 | mp_assert(x0 <= x1 && y0 <= y1); |
682 | 0 | mp_assert(x1 <= img->w && y1 <= img->h); |
683 | 0 | mp_assert(!(x0 & (img->fmt.align_x - 1))); |
684 | 0 | mp_assert(!(y0 & (img->fmt.align_y - 1))); |
685 | | |
686 | 0 | struct mp_image area = *img; |
687 | 0 | struct mp_imgfmt_desc *fmt = &area.fmt; |
688 | 0 | mp_image_crop(&area, x0, y0, x1, y1); |
689 | | |
690 | | // "Black" color for each plane. |
691 | 0 | uint8_t plane_clear[MP_MAX_PLANES][8] = {0}; |
692 | 0 | int plane_size[MP_MAX_PLANES] = {0}; |
693 | 0 | int misery = 1; // pixel group width |
694 | | |
695 | | // YUV integer chroma needs special consideration, and technically luma is |
696 | | // usually not 0 either. |
697 | 0 | if ((fmt->flags & (MP_IMGFLAG_HAS_COMPS | MP_IMGFLAG_PACKED_SS_YUV)) && |
698 | 0 | (fmt->flags & MP_IMGFLAG_TYPE_MASK) == MP_IMGFLAG_TYPE_UINT && |
699 | 0 | (fmt->flags & MP_IMGFLAG_COLOR_MASK) == MP_IMGFLAG_COLOR_YUV) |
700 | 0 | { |
701 | 0 | uint64_t plane_clear_i[MP_MAX_PLANES] = {0}; |
702 | | |
703 | | // Need to handle "multiple" pixels with packed YUV. |
704 | 0 | uint8_t luma_offsets[4] = {0}; |
705 | 0 | if (fmt->flags & MP_IMGFLAG_PACKED_SS_YUV) { |
706 | 0 | misery = fmt->align_x; |
707 | 0 | if (misery <= MP_ARRAY_SIZE(luma_offsets)) // ignore if out of bounds |
708 | 0 | mp_imgfmt_get_packed_yuv_locations(fmt->id, luma_offsets); |
709 | 0 | } |
710 | |
|
711 | 0 | for (int c = 0; c < 4; c++) { |
712 | 0 | struct mp_imgfmt_comp_desc *cd = &fmt->comps[c]; |
713 | 0 | int plane_bits = fmt->bpp[cd->plane] * misery; |
714 | 0 | if (plane_bits <= 64 && plane_bits % 8u == 0 && cd->size) { |
715 | 0 | plane_size[cd->plane] = plane_bits / 8u; |
716 | 0 | int depth = cd->size + MPMIN(cd->pad, 0); |
717 | 0 | double m, o; |
718 | 0 | mp_get_csp_uint_mul(area.params.repr.sys, |
719 | 0 | area.params.repr.levels, |
720 | 0 | depth, c + 1, &m, &o); |
721 | 0 | uint64_t val = MPCLAMP(lrint((0 - o) / m), 0, 1ull << depth); |
722 | 0 | plane_clear_i[cd->plane] |= val << cd->offset; |
723 | 0 | for (int x = 1; x < (c ? 0 : misery); x++) |
724 | 0 | plane_clear_i[cd->plane] |= val << luma_offsets[x]; |
725 | 0 | } |
726 | 0 | } |
727 | |
|
728 | 0 | for (int p = 0; p < MP_MAX_PLANES; p++) { |
729 | 0 | if (!plane_clear_i[p]) |
730 | 0 | plane_size[p] = 0; |
731 | 0 | memcpy(&plane_clear[p][0], &plane_clear_i[p], 8); // endian dependent |
732 | |
|
733 | 0 | if (fmt->endian_shift) { |
734 | 0 | endian_swap_bytes(&plane_clear[p][0], plane_size[p], |
735 | 0 | 1 << fmt->endian_shift); |
736 | 0 | } |
737 | 0 | } |
738 | 0 | } |
739 | |
|
740 | 0 | for (int p = 0; p < area.num_planes; p++) { |
741 | 0 | int p_h = mp_image_plane_h(&area, p); |
742 | 0 | int p_w = mp_image_plane_w(&area, p); |
743 | 0 | for (int y = 0; y < p_h; y++) { |
744 | 0 | void *ptr = area.planes[p] + (ptrdiff_t)area.stride[p] * y; |
745 | 0 | if (plane_size[p]) { |
746 | 0 | memset_pattern(ptr, p_w / misery, plane_clear[p], plane_size[p]); |
747 | 0 | } else { |
748 | 0 | memset(ptr, 0, mp_image_plane_bytes(&area, p, 0, area.w)); |
749 | 0 | } |
750 | 0 | } |
751 | 0 | } |
752 | 0 | } |
753 | | |
754 | | void mp_image_clear_rc(struct mp_image *mpi, struct mp_rect rc) |
755 | 0 | { |
756 | 0 | mp_image_clear(mpi, rc.x0, rc.y0, rc.x1, rc.y1); |
757 | 0 | } |
758 | | |
759 | | // Clear the are of the image _not_ covered by rc. |
760 | | void mp_image_clear_rc_inv(struct mp_image *mpi, struct mp_rect rc) |
761 | 0 | { |
762 | 0 | struct mp_rect clr[4]; |
763 | 0 | int cnt = mp_rect_subtract(&(struct mp_rect){0, 0, mpi->w, mpi->h}, &rc, clr); |
764 | 0 | for (int n = 0; n < cnt; n++) |
765 | 0 | mp_image_clear_rc(mpi, clr[n]); |
766 | 0 | } |
767 | | |
768 | | void mp_image_vflip(struct mp_image *img) |
769 | 0 | { |
770 | 0 | for (int p = 0; p < img->num_planes; p++) { |
771 | 0 | int plane_h = mp_image_plane_h(img, p); |
772 | 0 | img->planes[p] = img->planes[p] + img->stride[p] * (plane_h - 1); |
773 | 0 | img->stride[p] = -img->stride[p]; |
774 | 0 | } |
775 | 0 | } |
776 | | |
777 | | bool mp_image_crop_valid(const struct mp_image_params *p) |
778 | 71.8k | { |
779 | 71.8k | return p->crop.x1 > p->crop.x0 && p->crop.y1 > p->crop.y0 && |
780 | 71.8k | p->crop.x0 >= 0 && p->crop.y0 >= 0 && |
781 | 71.8k | p->crop.x1 <= p->w && p->crop.y1 <= p->h; |
782 | 71.8k | } |
783 | | |
784 | | // Display size derived from image size and pixel aspect ratio. |
785 | | void mp_image_params_get_dsize(const struct mp_image_params *p, |
786 | | int *d_w, int *d_h) |
787 | 3.90k | { |
788 | 3.90k | if (mp_image_crop_valid(p)) |
789 | 3.90k | { |
790 | 3.90k | *d_w = mp_rect_w(p->crop); |
791 | 3.90k | *d_h = mp_rect_h(p->crop); |
792 | 3.90k | } else { |
793 | 0 | *d_w = p->w; |
794 | 0 | *d_h = p->h; |
795 | 0 | } |
796 | | |
797 | 3.90k | if (p->p_w > p->p_h && p->p_h >= 1) |
798 | 754 | *d_w = MPCLAMP(*d_w * (int64_t)p->p_w / p->p_h, 1, INT_MAX); |
799 | 3.90k | if (p->p_h > p->p_w && p->p_w >= 1) |
800 | 1.11k | *d_h = MPCLAMP(*d_h * (int64_t)p->p_h / p->p_w, 1, INT_MAX); |
801 | 3.90k | } |
802 | | |
803 | | void mp_image_params_set_dsize(struct mp_image_params *p, int d_w, int d_h) |
804 | 0 | { |
805 | 0 | AVRational ds = av_div_q((AVRational){d_w, d_h}, (AVRational){p->w, p->h}); |
806 | 0 | p->p_w = ds.num; |
807 | 0 | p->p_h = ds.den; |
808 | 0 | } |
809 | | |
810 | | char *mp_image_params_to_str_buf(char *b, size_t bs, |
811 | | const struct mp_image_params *p) |
812 | 23.8k | { |
813 | 23.8k | if (p && p->imgfmt) { |
814 | 23.8k | snprintf(b, bs, "%dx%d", p->w, p->h); |
815 | 23.8k | if (p->p_w != p->p_h || !p->p_w) |
816 | 9.53k | mp_snprintf_cat(b, bs, " [%d:%d]", p->p_w, p->p_h); |
817 | 23.8k | mp_snprintf_cat(b, bs, " %s", mp_imgfmt_to_name(p->imgfmt)); |
818 | 23.8k | if (p->hw_subfmt) |
819 | 0 | mp_snprintf_cat(b, bs, "[%s]", mp_imgfmt_to_name(p->hw_subfmt)); |
820 | 23.8k | mp_snprintf_cat(b, bs, " %s/%s/%s/%s/%s", |
821 | 23.8k | m_opt_choice_str(pl_csp_names, p->repr.sys), |
822 | 23.8k | m_opt_choice_str(pl_csp_prim_names, p->color.primaries), |
823 | 23.8k | m_opt_choice_str(pl_csp_trc_names, p->color.transfer), |
824 | 23.8k | m_opt_choice_str(pl_csp_levels_names, p->repr.levels), |
825 | 23.8k | m_opt_choice_str(mp_csp_light_names, p->light)); |
826 | 23.8k | mp_snprintf_cat(b, bs, " CL=%s", |
827 | 23.8k | m_opt_choice_str(pl_chroma_names, p->chroma_location)); |
828 | 23.8k | if (mp_image_crop_valid(p)) { |
829 | 23.8k | mp_snprintf_cat(b, bs, " crop=%dx%d+%d+%d", mp_rect_w(p->crop), |
830 | 23.8k | mp_rect_h(p->crop), p->crop.x0, p->crop.y0); |
831 | 23.8k | } |
832 | 23.8k | if (p->rotate) |
833 | 0 | mp_snprintf_cat(b, bs, " rot=%d", p->rotate); |
834 | 23.8k | if (p->stereo3d > 0) { |
835 | 0 | mp_snprintf_cat(b, bs, " stereo=%s", |
836 | 0 | MP_STEREO3D_NAME_DEF(p->stereo3d, "?")); |
837 | 0 | } |
838 | 23.8k | if (p->repr.alpha) { |
839 | 20.7k | mp_snprintf_cat(b, bs, " A=%s", |
840 | 20.7k | m_opt_choice_str(pl_alpha_names, p->repr.alpha)); |
841 | 20.7k | } |
842 | 23.8k | } else { |
843 | 0 | snprintf(b, bs, "???"); |
844 | 0 | } |
845 | 23.8k | return b; |
846 | 23.8k | } |
847 | | |
848 | | // Return whether the image parameters are valid. |
849 | | // Some non-essential fields are allowed to be unset (like colorspace flags). |
850 | | bool mp_image_params_valid(const struct mp_image_params *p) |
851 | 0 | { |
852 | | // av_image_check_size has similar checks and triggers around 16000*16000 |
853 | | // It's mostly needed to deal with the fact that offsets are sometimes |
854 | | // ints. We also should (for now) do the same as FFmpeg, to be sure large |
855 | | // images don't crash with libswscale or when wrapping with AVFrame and |
856 | | // passing the result to filters. |
857 | 0 | if (p->w <= 0 || p->h <= 0 || (p->w + 128LL) * (p->h + 128LL) >= INT_MAX / 8) |
858 | 0 | return false; |
859 | | |
860 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
861 | 0 | if (p->w > 16384 || p->h > 16384 || p->w * p->h > 2048 * 2048) |
862 | 0 | return false; |
863 | 0 | #endif |
864 | | |
865 | 0 | if (p->p_w < 0 || p->p_h < 0) |
866 | 0 | return false; |
867 | | |
868 | 0 | if (p->rotate < 0 || p->rotate >= 360) |
869 | 0 | return false; |
870 | | |
871 | 0 | struct mp_imgfmt_desc desc = mp_imgfmt_get_desc(p->imgfmt); |
872 | 0 | if (!desc.id) |
873 | 0 | return false; |
874 | | |
875 | 0 | if (p->hw_subfmt && !(desc.flags & MP_IMGFLAG_HWACCEL)) |
876 | 0 | return false; |
877 | | |
878 | 0 | return true; |
879 | 0 | } |
880 | | |
881 | | bool mp_image_params_equal(const struct mp_image_params *p1, |
882 | | const struct mp_image_params *p2) |
883 | 369k | { |
884 | 369k | return p1->imgfmt == p2->imgfmt && |
885 | 337k | p1->hw_subfmt == p2->hw_subfmt && |
886 | 337k | p1->w == p2->w && p1->h == p2->h && |
887 | 330k | p1->p_w == p2->p_w && p1->p_h == p2->p_h && |
888 | 326k | p1->force_window == p2->force_window && |
889 | 326k | pl_color_space_equal(&p1->color, &p2->color) && |
890 | 326k | pl_color_repr_equal(&p1->repr, &p2->repr) && |
891 | 326k | p1->light == p2->light && |
892 | 326k | p1->chroma_location == p2->chroma_location && |
893 | 326k | p1->vflip == p2->vflip && |
894 | 326k | p1->rotate == p2->rotate && |
895 | 326k | p1->stereo3d == p2->stereo3d && |
896 | 326k | mp_rect_equals(&p1->crop, &p2->crop); |
897 | 369k | } |
898 | | |
899 | | bool mp_image_params_static_equal(const struct mp_image_params *p1, |
900 | | const struct mp_image_params *p2) |
901 | 62.8k | { |
902 | | // Compare only static video parameters, excluding dynamic metadata. |
903 | 62.8k | struct mp_image_params a = *p1; |
904 | 62.8k | struct mp_image_params b = *p2; |
905 | 62.8k | a.repr.dovi = b.repr.dovi = NULL; |
906 | 62.8k | a.color.hdr = b.color.hdr = (struct pl_hdr_metadata){0}; |
907 | 62.8k | return mp_image_params_equal(&a, &b); |
908 | 62.8k | } |
909 | | |
910 | | void mp_image_params_update_dynamic(struct mp_image_params *dst, |
911 | | const struct mp_image_params *src, |
912 | | bool has_peak_detect_values) |
913 | 41.0k | { |
914 | 41.0k | dst->repr.dovi = src->repr.dovi; |
915 | | // Don't overwrite peak-detected HDR metadata if available. |
916 | 41.0k | struct pl_hdr_metadata *hdr = &dst->color.hdr; |
917 | 41.0k | const struct pl_hdr_metadata prev = *hdr; |
918 | 41.0k | *hdr = src->color.hdr; |
919 | 41.0k | if (has_peak_detect_values) { |
920 | 0 | hdr->max_pq_y = prev.max_pq_y; |
921 | 0 | hdr->avg_pq_y = prev.avg_pq_y; |
922 | 0 | hdr->scene_avg = prev.scene_avg; |
923 | 0 | memcpy(hdr->scene_max, prev.scene_max, sizeof(hdr->scene_max)); |
924 | 0 | } |
925 | 41.0k | } |
926 | | |
927 | | // Restore color system, transfer, and primaries to their original values |
928 | | // before dovi mapping. |
929 | | void mp_image_params_restore_dovi_mapping(struct mp_image_params *params) |
930 | 0 | { |
931 | 0 | if (params->repr.sys != PL_COLOR_SYSTEM_DOLBYVISION) |
932 | 0 | return; |
933 | 0 | params->color.primaries = params->primaries_orig; |
934 | 0 | params->color.transfer = params->transfer_orig; |
935 | 0 | params->repr.sys = params->sys_orig; |
936 | 0 | if (!pl_color_transfer_is_hdr(params->transfer_orig)) |
937 | 0 | params->color.hdr = (struct pl_hdr_metadata){0}; |
938 | 0 | if (params->transfer_orig != PL_COLOR_TRC_PQ) |
939 | 0 | params->color.hdr.max_pq_y = params->color.hdr.avg_pq_y = 0; |
940 | 0 | } |
941 | | |
942 | | // Set most image parameters, but not image format or size. |
943 | | // Display size is used to set the PAR. |
944 | | void mp_image_set_attributes(struct mp_image *image, |
945 | | const struct mp_image_params *params) |
946 | 0 | { |
947 | 0 | struct mp_image_params nparams = *params; |
948 | 0 | nparams.imgfmt = image->imgfmt; |
949 | 0 | nparams.w = image->w; |
950 | 0 | nparams.h = image->h; |
951 | 0 | if (nparams.imgfmt != params->imgfmt) { |
952 | 0 | nparams.repr = (struct pl_color_repr){0}; |
953 | 0 | nparams.color = (struct pl_color_space){0}; |
954 | 0 | } |
955 | 0 | mp_image_set_params(image, &nparams); |
956 | 0 | } |
957 | | |
958 | | static enum pl_color_levels infer_levels(enum mp_imgfmt imgfmt) |
959 | 724 | { |
960 | 724 | switch (imgfmt2pixfmt(imgfmt)) { |
961 | 0 | case AV_PIX_FMT_YUVJ420P: |
962 | 0 | case AV_PIX_FMT_YUVJ411P: |
963 | 0 | case AV_PIX_FMT_YUVJ422P: |
964 | 0 | case AV_PIX_FMT_YUVJ444P: |
965 | 0 | case AV_PIX_FMT_YUVJ440P: |
966 | 330 | case AV_PIX_FMT_GRAY8: |
967 | 330 | case AV_PIX_FMT_YA8: |
968 | 330 | case AV_PIX_FMT_GRAY9LE: |
969 | 330 | case AV_PIX_FMT_GRAY9BE: |
970 | 330 | case AV_PIX_FMT_GRAY10LE: |
971 | 330 | case AV_PIX_FMT_GRAY10BE: |
972 | 330 | case AV_PIX_FMT_GRAY12LE: |
973 | 330 | case AV_PIX_FMT_GRAY12BE: |
974 | 330 | case AV_PIX_FMT_GRAY14LE: |
975 | 330 | case AV_PIX_FMT_GRAY14BE: |
976 | 373 | case AV_PIX_FMT_GRAY16LE: |
977 | 373 | case AV_PIX_FMT_GRAY16BE: |
978 | 373 | case AV_PIX_FMT_YA16BE: |
979 | 373 | case AV_PIX_FMT_YA16LE: |
980 | 373 | return PL_COLOR_LEVELS_FULL; |
981 | 351 | default: |
982 | 351 | return PL_COLOR_LEVELS_LIMITED; |
983 | 724 | } |
984 | 724 | } |
985 | | |
986 | | // If details like params->colorspace/colorlevels are missing, guess them from |
987 | | // the other settings. Also, even if they are set, make them consistent with |
988 | | // the colorspace as implied by the pixel format. |
989 | | void mp_image_params_guess_csp(struct mp_image_params *params) |
990 | 3.10k | { |
991 | 3.10k | enum pl_color_system forced_csp = mp_image_params_get_forced_csp(params); |
992 | 3.10k | if (forced_csp == PL_COLOR_SYSTEM_UNKNOWN) { // YUV/other |
993 | 2.03k | if (params->repr.sys != PL_COLOR_SYSTEM_BT_601 && |
994 | 2.03k | params->repr.sys != PL_COLOR_SYSTEM_BT_709 && |
995 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_BT_2020_NC && |
996 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_BT_2020_C && |
997 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_BT_2100_PQ && |
998 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_BT_2100_HLG && |
999 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_DOLBYVISION && |
1000 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_SMPTE_240M && |
1001 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_YCGCO && |
1002 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_YCGCO_RE && |
1003 | 1.98k | params->repr.sys != PL_COLOR_SYSTEM_YCGCO_RO |
1004 | 2.03k | ) { |
1005 | | // Makes no sense, so guess instead |
1006 | | // YCGCO should be separate, but libavcodec disagrees |
1007 | 1.98k | params->repr.sys = PL_COLOR_SYSTEM_UNKNOWN; |
1008 | 1.98k | } |
1009 | 2.03k | if (params->repr.sys == PL_COLOR_SYSTEM_UNKNOWN) |
1010 | 1.98k | params->repr.sys = mp_csp_guess_colorspace(params->w, params->h); |
1011 | 2.03k | if (params->repr.levels == PL_COLOR_LEVELS_UNKNOWN) { |
1012 | 724 | if (params->color.transfer == PL_COLOR_TRC_V_LOG) { |
1013 | 0 | params->repr.levels = PL_COLOR_LEVELS_FULL; |
1014 | 724 | } else { |
1015 | 724 | params->repr.levels = infer_levels(params->imgfmt); |
1016 | 724 | } |
1017 | 724 | } |
1018 | 2.03k | if (params->color.primaries == PL_COLOR_PRIM_UNKNOWN) { |
1019 | | // Guess based on the colormatrix as a first priority |
1020 | 1.98k | if (params->repr.sys == PL_COLOR_SYSTEM_BT_2020_NC || |
1021 | 1.98k | params->repr.sys == PL_COLOR_SYSTEM_BT_2020_C) { |
1022 | 0 | params->color.primaries = PL_COLOR_PRIM_BT_2020; |
1023 | 1.98k | } else if (params->repr.sys == PL_COLOR_SYSTEM_BT_709) { |
1024 | 1.12k | params->color.primaries = PL_COLOR_PRIM_BT_709; |
1025 | 1.12k | } else { |
1026 | | // Ambiguous colormatrix for BT.601, guess based on res |
1027 | 853 | params->color.primaries = mp_csp_guess_primaries(params->w, params->h); |
1028 | 853 | } |
1029 | 1.98k | } |
1030 | 2.03k | if (params->color.transfer == PL_COLOR_TRC_UNKNOWN) |
1031 | 1.93k | params->color.transfer = params->repr.levels == PL_COLOR_LEVELS_LIMITED ? |
1032 | 1.55k | PL_COLOR_TRC_BT_1886 : PL_COLOR_TRC_SRGB; |
1033 | 2.03k | } else if (forced_csp == PL_COLOR_SYSTEM_RGB) { |
1034 | 1.07k | params->repr.sys = PL_COLOR_SYSTEM_RGB; |
1035 | 1.07k | params->repr.levels = PL_COLOR_LEVELS_FULL; |
1036 | | |
1037 | | // The majority of RGB content is either sRGB or (rarely) some other |
1038 | | // color space which we don't even handle, like AdobeRGB or |
1039 | | // ProPhotoRGB. The only reasonable thing we can do is assume it's |
1040 | | // sRGB and hope for the best, which should usually just work out fine. |
1041 | | // Note: sRGB primaries = BT.709 primaries |
1042 | 1.07k | if (params->color.primaries == PL_COLOR_PRIM_UNKNOWN) |
1043 | 1.07k | params->color.primaries = PL_COLOR_PRIM_BT_709; |
1044 | 1.07k | if (params->color.transfer == PL_COLOR_TRC_UNKNOWN) |
1045 | 1.07k | params->color.transfer = PL_COLOR_TRC_SRGB; |
1046 | 1.07k | } else if (forced_csp == PL_COLOR_SYSTEM_XYZ) { |
1047 | 0 | params->repr.sys = PL_COLOR_SYSTEM_XYZ; |
1048 | 0 | params->repr.levels = PL_COLOR_LEVELS_FULL; |
1049 | 0 | if (params->color.transfer == PL_COLOR_TRC_LINEAR) { |
1050 | | // Linear light XYZ is decoded into the tagged primaries |
1051 | 0 | if (params->color.primaries == PL_COLOR_PRIM_UNKNOWN) |
1052 | 0 | params->color.primaries = PL_COLOR_PRIM_BT_2020; |
1053 | 0 | } else { |
1054 | | // Anything else is DCDM X'Y'Z' (ST 428-1) by default. |
1055 | 0 | if (params->color.transfer == PL_COLOR_TRC_UNKNOWN) |
1056 | 0 | params->color.transfer = PL_COLOR_TRC_ST428; |
1057 | 0 | if (params->color.primaries == PL_COLOR_PRIM_UNKNOWN) |
1058 | 0 | params->color.primaries = PL_COLOR_PRIM_DCI_P3; |
1059 | 0 | } |
1060 | 0 | } else { |
1061 | | // We have no clue. |
1062 | 0 | params->repr.sys = PL_COLOR_SYSTEM_UNKNOWN; |
1063 | 0 | params->repr.levels = PL_COLOR_LEVELS_UNKNOWN; |
1064 | 0 | params->color.primaries = PL_COLOR_PRIM_UNKNOWN; |
1065 | 0 | params->color.transfer = PL_COLOR_TRC_UNKNOWN; |
1066 | 0 | } |
1067 | | |
1068 | | // If the signal peak is unknown, we're forced to pick the TRC's |
1069 | | // nominal range as the signal peak to prevent clipping |
1070 | 3.10k | pl_color_space_infer(¶ms->color); |
1071 | | |
1072 | 3.10k | if (!pl_color_space_is_hdr(¶ms->color)) { |
1073 | | // Some clips have leftover HDR metadata after conversion to SDR, so to |
1074 | | // avoid blowing up the tone mapping code, strip/sanitize it |
1075 | 3.09k | params->color.hdr = pl_hdr_metadata_empty; |
1076 | 3.09k | } |
1077 | | |
1078 | 3.10k | if (mp_imgfmt_is_subsampled(params->hw_subfmt ? params->hw_subfmt : params->imgfmt)) { |
1079 | 1.55k | if (params->chroma_location == PL_CHROMA_UNKNOWN) { |
1080 | 242 | if (params->repr.levels == PL_COLOR_LEVELS_LIMITED) |
1081 | 192 | params->chroma_location = PL_CHROMA_LEFT; |
1082 | 242 | if (params->repr.levels == PL_COLOR_LEVELS_FULL) |
1083 | 50 | params->chroma_location = PL_CHROMA_CENTER; |
1084 | 242 | } |
1085 | 1.55k | } else { |
1086 | | // Set to center for non-subsampled formats. |
1087 | 1.54k | params->chroma_location = PL_CHROMA_CENTER; |
1088 | 1.54k | } |
1089 | | |
1090 | 3.10k | if (params->light == MP_CSP_LIGHT_AUTO) { |
1091 | | // HLG is always scene-referred (using its own OOTF), everything else |
1092 | | // we assume is display-referred by default. |
1093 | 3.10k | if (params->color.transfer == PL_COLOR_TRC_HLG) { |
1094 | 0 | params->light = MP_CSP_LIGHT_SCENE_HLG; |
1095 | 3.10k | } else { |
1096 | 3.10k | params->light = MP_CSP_LIGHT_DISPLAY; |
1097 | 3.10k | } |
1098 | 3.10k | } |
1099 | 3.10k | } |
1100 | | |
1101 | | // Create a new mp_image reference to av_frame. |
1102 | | struct mp_image *mp_image_from_av_frame(struct AVFrame *src) |
1103 | 74.3k | { |
1104 | 74.3k | struct mp_image *dst = &(struct mp_image){0}; |
1105 | 74.3k | AVFrameSideData *sd; |
1106 | | |
1107 | 371k | for (int p = 0; p < MP_MAX_PLANES; p++) |
1108 | 297k | dst->bufs[p] = src->buf[p]; |
1109 | | |
1110 | 74.3k | dst->hwctx = src->hw_frames_ctx; |
1111 | | |
1112 | 74.3k | if (dst->hwctx) { |
1113 | 0 | AVHWFramesContext *fctx = (void *)dst->hwctx->data; |
1114 | 0 | dst->params.hw_subfmt = pixfmt2imgfmt(fctx->sw_format); |
1115 | 0 | } |
1116 | | |
1117 | 74.3k | mp_image_sethwfmt(dst, pixfmt2imgfmt(src->format), dst->params.hw_subfmt); |
1118 | 74.3k | mp_image_set_size(dst, src->width, src->height); |
1119 | | |
1120 | 74.3k | dst->params.p_w = src->sample_aspect_ratio.num; |
1121 | 74.3k | dst->params.p_h = src->sample_aspect_ratio.den; |
1122 | | |
1123 | 371k | for (int i = 0; i < 4; i++) { |
1124 | 297k | dst->planes[i] = src->data[i]; |
1125 | 297k | dst->stride[i] = src->linesize[i]; |
1126 | 297k | } |
1127 | | |
1128 | 74.3k | dst->pict_type = src->pict_type; |
1129 | | |
1130 | 74.3k | dst->params.crop.x0 = src->crop_left; |
1131 | 74.3k | dst->params.crop.y0 = src->crop_top; |
1132 | 74.3k | dst->params.crop.x1 = src->width - src->crop_right; |
1133 | 74.3k | dst->params.crop.y1 = src->height - src->crop_bottom; |
1134 | | |
1135 | 74.3k | dst->fields = 0; |
1136 | 74.3k | if (src->flags & AV_FRAME_FLAG_INTERLACED) |
1137 | 4.05k | dst->fields |= MP_IMGFIELD_INTERLACED; |
1138 | 74.3k | if (src->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) |
1139 | 4.85k | dst->fields |= MP_IMGFIELD_TOP_FIRST; |
1140 | 74.3k | if (src->repeat_pict == 1) |
1141 | 109 | dst->fields |= MP_IMGFIELD_REPEAT_FIRST; |
1142 | | |
1143 | 74.3k | dst->params.repr.sys = pl_system_from_av(src->colorspace); |
1144 | 74.3k | dst->params.repr.levels = pl_levels_from_av(src->color_range); |
1145 | | |
1146 | 74.3k | dst->params.color = (struct pl_color_space){ |
1147 | 74.3k | .primaries = pl_primaries_from_av(src->color_primaries), |
1148 | 74.3k | .transfer = pl_transfer_from_av(src->color_trc), |
1149 | 74.3k | }; |
1150 | | |
1151 | 74.3k | dst->params.chroma_location = pl_chroma_from_av(src->chroma_location); |
1152 | | |
1153 | 74.3k | sd = av_frame_get_side_data(src, AV_FRAME_DATA_STEREO3D); |
1154 | 74.3k | if (sd) |
1155 | 0 | dst->params.stereo3d = mp_stereo3d_from_av((const AVStereo3D *)sd->data); |
1156 | | |
1157 | 74.3k | if (src->opaque_ref) { |
1158 | 0 | struct mp_image_params *p = (void *)src->opaque_ref->data; |
1159 | 0 | dst->params.stereo3d = p->stereo3d; |
1160 | | // Might be incorrect if colorspace changes. |
1161 | 0 | dst->params.light = p->light; |
1162 | 0 | dst->params.no_dovi = p->no_dovi; |
1163 | 0 | dst->params.no_enhancement_layer = p->no_enhancement_layer; |
1164 | | #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(60, 11, 100) |
1165 | | dst->params.repr.alpha = p->repr.alpha; |
1166 | | #endif |
1167 | 0 | } |
1168 | | |
1169 | 74.3k | #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(60, 11, 100) |
1170 | | // mp_image_setfmt sets to PL_ALPHA_INDEPENDENT, if format has alpha. |
1171 | 74.3k | if (dst->params.repr.alpha == PL_ALPHA_INDEPENDENT) |
1172 | 32.9k | dst->params.repr.alpha = pl_alpha_from_av(src->alpha_mode); |
1173 | 74.3k | #endif |
1174 | | |
1175 | 74.3k | sd = av_frame_get_side_data(src, AV_FRAME_DATA_DISPLAYMATRIX); |
1176 | 74.3k | if (sd) { |
1177 | 0 | int32_t *matrix = (int32_t *) sd->data; |
1178 | | // determinant |
1179 | 0 | int vflip = ((int64_t)matrix[0] * (int64_t)matrix[4] |
1180 | 0 | - (int64_t)matrix[1] * (int64_t)matrix[3]) < 0; |
1181 | 0 | double r = av_display_rotation_get(matrix); |
1182 | 0 | if (!isnan(r)) { |
1183 | 0 | dst->params.rotate = (((int)(-r) % 360) + 360) % 360; |
1184 | 0 | dst->params.vflip = vflip; |
1185 | 0 | } |
1186 | 0 | } |
1187 | | |
1188 | 74.3k | sd = av_frame_get_side_data(src, AV_FRAME_DATA_ICC_PROFILE); |
1189 | 74.3k | if (sd) |
1190 | 0 | dst->icc_profile = sd->buf; |
1191 | | |
1192 | 74.3k | AVFrameSideData *mdm = av_frame_get_side_data(src, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA); |
1193 | 74.3k | AVFrameSideData *clm = av_frame_get_side_data(src, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL); |
1194 | 74.3k | AVFrameSideData *dhp = av_frame_get_side_data(src, AV_FRAME_DATA_DYNAMIC_HDR_PLUS); |
1195 | 74.3k | pl_map_hdr_metadata(&dst->params.color.hdr, &(struct pl_av_hdr_metadata) { |
1196 | 74.3k | .mdm = (void *)(mdm ? mdm->data : NULL), |
1197 | 74.3k | .clm = (void *)(clm ? clm->data : NULL), |
1198 | 74.3k | .dhp = (void *)(dhp ? dhp->data : NULL), |
1199 | 74.3k | }); |
1200 | | |
1201 | 74.3k | sd = av_frame_get_side_data(src, AV_FRAME_DATA_A53_CC); |
1202 | 74.3k | if (sd) |
1203 | 0 | dst->a53_cc = sd->buf; |
1204 | | |
1205 | 74.3k | dst->params.primaries_orig = dst->params.color.primaries; |
1206 | 74.3k | dst->params.transfer_orig = dst->params.color.transfer; |
1207 | 74.3k | dst->params.sys_orig = dst->params.repr.sys; |
1208 | 74.3k | AVBufferRef *dovi = NULL; |
1209 | 74.3k | sd = av_frame_get_side_data(src, AV_FRAME_DATA_DOVI_METADATA); |
1210 | 74.3k | if (sd) { |
1211 | 0 | #ifdef PL_HAVE_LAV_DOLBY_VISION |
1212 | 0 | const AVDOVIMetadata *metadata = (const AVDOVIMetadata *)sd->buf->data; |
1213 | 0 | dst->dovi = dovi = av_buffer_alloc(sizeof(struct pl_dovi_metadata)); |
1214 | 0 | MP_HANDLE_OOM(dovi); |
1215 | 0 | pl_map_avdovi_metadata(&dst->params.color, &dst->params.repr, |
1216 | 0 | (void *)dst->dovi->data, metadata); |
1217 | 0 | #endif |
1218 | 0 | } |
1219 | | |
1220 | 74.3k | sd = av_frame_get_side_data(src, AV_FRAME_DATA_DOVI_RPU_BUFFER); |
1221 | 74.3k | if (sd) { |
1222 | 0 | pl_hdr_metadata_from_dovi_rpu(&dst->params.color.hdr, sd->buf->data, |
1223 | 0 | sd->buf->size); |
1224 | 0 | } |
1225 | | |
1226 | 74.3k | sd = av_frame_get_side_data(src, AV_FRAME_DATA_FILM_GRAIN_PARAMS); |
1227 | 74.3k | if (sd) |
1228 | 0 | dst->film_grain = sd->buf; |
1229 | | |
1230 | 102k | for (int n = 0; n < src->nb_side_data; n++) { |
1231 | 28.4k | sd = src->side_data[n]; |
1232 | 28.4k | struct mp_ff_side_data mpsd = { |
1233 | 28.4k | .type = sd->type, |
1234 | 28.4k | .buf = sd->buf, |
1235 | 28.4k | }; |
1236 | 28.4k | MP_TARRAY_APPEND(NULL, dst->ff_side_data, dst->num_ff_side_data, mpsd); |
1237 | 28.4k | } |
1238 | | |
1239 | 74.3k | struct mp_image *res = mp_image_new_ref(dst); |
1240 | | |
1241 | | // Allocated, but non-refcounted data. |
1242 | 74.3k | talloc_free(dst->ff_side_data); |
1243 | 74.3k | av_buffer_unref(&dovi); |
1244 | | |
1245 | 74.3k | return res; |
1246 | 74.3k | } |
1247 | | |
1248 | | |
1249 | | // Convert the mp_image reference to a AVFrame reference. |
1250 | | struct AVFrame *mp_image_to_av_frame(struct mp_image *src) |
1251 | 0 | { |
1252 | 0 | struct mp_image *new_ref = mp_image_new_ref(src); |
1253 | 0 | AVFrame *dst = av_frame_alloc(); |
1254 | 0 | if (!dst || !new_ref) { |
1255 | 0 | talloc_free(new_ref); |
1256 | 0 | av_frame_free(&dst); |
1257 | 0 | return NULL; |
1258 | 0 | } |
1259 | | |
1260 | 0 | for (int p = 0; p < MP_MAX_PLANES; p++) { |
1261 | 0 | dst->buf[p] = new_ref->bufs[p]; |
1262 | 0 | new_ref->bufs[p] = NULL; |
1263 | 0 | } |
1264 | |
|
1265 | 0 | dst->hw_frames_ctx = new_ref->hwctx; |
1266 | 0 | new_ref->hwctx = NULL; |
1267 | |
|
1268 | 0 | dst->format = imgfmt2pixfmt(src->imgfmt); |
1269 | 0 | dst->width = src->w; |
1270 | 0 | dst->height = src->h; |
1271 | |
|
1272 | 0 | dst->crop_left = src->params.crop.x0; |
1273 | 0 | dst->crop_top = src->params.crop.y0; |
1274 | 0 | dst->crop_right = dst->width - src->params.crop.x1; |
1275 | 0 | dst->crop_bottom = dst->height - src->params.crop.y1; |
1276 | |
|
1277 | 0 | dst->sample_aspect_ratio.num = src->params.p_w; |
1278 | 0 | dst->sample_aspect_ratio.den = src->params.p_h; |
1279 | |
|
1280 | 0 | for (int i = 0; i < 4; i++) { |
1281 | 0 | dst->data[i] = src->planes[i]; |
1282 | 0 | dst->linesize[i] = src->stride[i]; |
1283 | 0 | } |
1284 | 0 | dst->extended_data = dst->data; |
1285 | |
|
1286 | 0 | dst->pict_type = src->pict_type; |
1287 | 0 | if (src->fields & MP_IMGFIELD_INTERLACED) |
1288 | 0 | dst->flags |= AV_FRAME_FLAG_INTERLACED; |
1289 | 0 | if (src->fields & MP_IMGFIELD_TOP_FIRST) |
1290 | 0 | dst->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; |
1291 | 0 | if (src->fields & MP_IMGFIELD_REPEAT_FIRST) |
1292 | 0 | dst->repeat_pict = 1; |
1293 | | |
1294 | | // Image params without dovi mapped; should be passed as side data instead |
1295 | 0 | struct mp_image_params params = src->params; |
1296 | 0 | mp_image_params_restore_dovi_mapping(¶ms); |
1297 | 0 | pl_avframe_set_repr(dst, params.repr); |
1298 | |
|
1299 | 0 | dst->chroma_location = pl_chroma_to_av(params.chroma_location); |
1300 | |
|
1301 | 0 | dst->opaque_ref = av_buffer_alloc(sizeof(struct mp_image_params)); |
1302 | 0 | MP_HANDLE_OOM(dst->opaque_ref); |
1303 | 0 | *(struct mp_image_params *)dst->opaque_ref->data = params; |
1304 | |
|
1305 | 0 | if (src->icc_profile) { |
1306 | 0 | AVFrameSideData *sd = |
1307 | 0 | av_frame_new_side_data_from_buf(dst, AV_FRAME_DATA_ICC_PROFILE, |
1308 | 0 | new_ref->icc_profile); |
1309 | 0 | MP_HANDLE_OOM(sd); |
1310 | 0 | new_ref->icc_profile = NULL; |
1311 | 0 | } |
1312 | | |
1313 | 0 | pl_avframe_set_color(dst, params.color); |
1314 | |
|
1315 | 0 | { |
1316 | 0 | AVFrameSideData *sd = av_frame_new_side_data(dst, |
1317 | 0 | AV_FRAME_DATA_DISPLAYMATRIX, |
1318 | 0 | sizeof(int32_t) * 9); |
1319 | 0 | MP_HANDLE_OOM(sd); |
1320 | 0 | av_display_rotation_set((int32_t *)sd->data, params.rotate); |
1321 | 0 | } |
1322 | | |
1323 | | // Add back side data, but only for types which are not specially handled |
1324 | | // above. Keep in mind that the types above will be out of sync anyway. |
1325 | 0 | for (int n = 0; n < new_ref->num_ff_side_data; n++) { |
1326 | 0 | struct mp_ff_side_data *mpsd = &new_ref->ff_side_data[n]; |
1327 | 0 | if (!av_frame_get_side_data(dst, mpsd->type)) { |
1328 | 0 | AVFrameSideData *sd = av_frame_new_side_data_from_buf(dst, mpsd->type, |
1329 | 0 | mpsd->buf); |
1330 | 0 | MP_HANDLE_OOM(sd); |
1331 | 0 | mpsd->buf = NULL; |
1332 | 0 | } |
1333 | 0 | } |
1334 | | |
1335 | 0 | talloc_free(new_ref); |
1336 | |
|
1337 | 0 | if (dst->format == AV_PIX_FMT_NONE) |
1338 | 0 | av_frame_free(&dst); |
1339 | 0 | return dst; |
1340 | 0 | } |
1341 | | |
1342 | | // Same as mp_image_to_av_frame(), but unref img. (It does so even on failure.) |
1343 | | struct AVFrame *mp_image_to_av_frame_and_unref(struct mp_image *img) |
1344 | 0 | { |
1345 | 0 | AVFrame *frame = mp_image_to_av_frame(img); |
1346 | 0 | talloc_free(img); |
1347 | 0 | return frame; |
1348 | 0 | } |
1349 | | |
1350 | | void memset_pic(void *dst, int fill, int bytesPerLine, int height, int stride) |
1351 | 0 | { |
1352 | 0 | if (bytesPerLine == stride && height) { |
1353 | 0 | memset(dst, fill, stride * (height - 1) + bytesPerLine); |
1354 | 0 | } else { |
1355 | 0 | for (int i = 0; i < height; i++) { |
1356 | 0 | memset(dst, fill, bytesPerLine); |
1357 | 0 | dst = (uint8_t *)dst + stride; |
1358 | 0 | } |
1359 | 0 | } |
1360 | 0 | } |
1361 | | |
1362 | | void memset16_pic(void *dst, int fill, int unitsPerLine, int height, int stride) |
1363 | 0 | { |
1364 | 0 | if (fill == 0) { |
1365 | 0 | memset_pic(dst, 0, unitsPerLine * 2, height, stride); |
1366 | 0 | } else { |
1367 | 0 | for (int i = 0; i < height; i++) { |
1368 | 0 | uint16_t *line = dst; |
1369 | 0 | uint16_t *end = line + unitsPerLine; |
1370 | 0 | while (line < end) |
1371 | 0 | *line++ = fill; |
1372 | 0 | dst = (uint8_t *)dst + stride; |
1373 | 0 | } |
1374 | 0 | } |
1375 | 0 | } |
1376 | | |
1377 | | // Pixel at the given luma position on the given plane. x/y always refer to |
1378 | | // non-subsampled coordinates (even if plane is chroma). |
1379 | | // The coordinates must be aligned to mp_imgfmt_desc.align_x/y (these are byte |
1380 | | // and chroma boundaries). |
1381 | | // You cannot access e.g. individual luma pixels on the luma plane with yuv420p. |
1382 | | void *mp_image_pixel_ptr(struct mp_image *img, int plane, int x, int y) |
1383 | 0 | { |
1384 | 0 | mp_assert(MP_IS_ALIGNED(x, img->fmt.align_x)); |
1385 | 0 | mp_assert(MP_IS_ALIGNED(y, img->fmt.align_y)); |
1386 | 0 | return mp_image_pixel_ptr_ny(img, plane, x, y); |
1387 | 0 | } |
1388 | | |
1389 | | // Like mp_image_pixel_ptr(), but do not require alignment on Y coordinates if |
1390 | | // the plane does not require it. Use with care. |
1391 | | // Useful for addressing luma rows. |
1392 | | void *mp_image_pixel_ptr_ny(struct mp_image *img, int plane, int x, int y) |
1393 | 0 | { |
1394 | 0 | mp_assert(MP_IS_ALIGNED(x, img->fmt.align_x)); |
1395 | 0 | mp_assert(MP_IS_ALIGNED(y, 1 << img->fmt.ys[plane])); |
1396 | 0 | return img->planes[plane] + |
1397 | 0 | img->stride[plane] * (ptrdiff_t)(y >> img->fmt.ys[plane]) + |
1398 | 0 | (x >> img->fmt.xs[plane]) * (size_t)img->fmt.bpp[plane] / 8; |
1399 | 0 | } |
1400 | | |
1401 | | // Return size of pixels [x0, x0+w-1] in bytes. The coordinates refer to non- |
1402 | | // subsampled pixels (basically plane 0), and the size is rounded to chroma |
1403 | | // and byte alignment boundaries for the entire image, even if plane!=0. |
1404 | | // x0!=0 is useful for rounding (e.g. 8 bpp, x0=7, w=7 => 0..15 => 2 bytes). |
1405 | | size_t mp_image_plane_bytes(struct mp_image *img, int plane, int x0, int w) |
1406 | 0 | { |
1407 | 0 | int x1 = MP_ALIGN_UP(x0 + w, img->fmt.align_x); |
1408 | 0 | x0 = MP_ALIGN_DOWN(x0, img->fmt.align_x); |
1409 | 0 | size_t bpp = img->fmt.bpp[plane]; |
1410 | 0 | int xs = img->fmt.xs[plane]; |
1411 | 0 | return (x1 >> xs) * bpp / 8 - (x0 >> xs) * bpp / 8; |
1412 | 0 | } |