avfilter/avfiltergraph: fix constant string comparision
[ffmpeg.git] / libavutil / frame.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 #include "channel_layout.h"
20 #include "avassert.h"
21 #include "buffer.h"
22 #include "dict.h"
23 #include "frame.h"
24 #include "imgutils.h"
25 #include "mem.h"
26 #include "refstruct.h"
27 #include "samplefmt.h"
28 #include "side_data.h"
29 #include "hwcontext.h"
30
31 static void get_frame_defaults(AVFrame *frame)
32 {
33 memset(frame, 0, sizeof(*frame));
34
35 frame->pts =
36 frame->pkt_dts = AV_NOPTS_VALUE;
37 frame->best_effort_timestamp = AV_NOPTS_VALUE;
38 frame->duration = 0;
39 frame->time_base = (AVRational){ 0, 1 };
40 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
41 frame->format = -1; /* unknown */
42 frame->extended_data = frame->data;
43 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
44 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
45 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
46 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
47 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
48 frame->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
49 frame->flags = 0;
50 }
51
52 AVFrame *av_frame_alloc(void)
53 {
54 AVFrame *frame = av_malloc(sizeof(*frame));
55
56 if (!frame)
57 return NULL;
58
59 get_frame_defaults(frame);
60
61 return frame;
62 }
63
64 void av_frame_free(AVFrame **frame)
65 {
66 if (!frame || !*frame)
67 return;
68
69 av_frame_unref(*frame);
70 av_freep(frame);
71 }
72
73 #define ALIGN (HAVE_SIMD_ALIGN_64 ? 64 : 32)
74
75 static int get_video_buffer(AVFrame *frame, int align)
76 {
77 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
78 int ret, padded_height;
79 int plane_padding;
80 ptrdiff_t linesizes[4];
81 size_t total_size, sizes[4];
82
83 if (!desc)
84 return AVERROR(EINVAL);
85
86 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
87 return ret;
88
89 if (align <= 0)
90 align = ALIGN;
91 plane_padding = FFMAX(ALIGN, align);
92
93 if (!frame->linesize[0]) {
94 for (int i = 1; i <= align; i += i) {
95 ret = av_image_fill_linesizes(frame->linesize, frame->format,
96 FFALIGN(frame->width, i));
97 if (ret < 0)
98 return ret;
99 if (!(frame->linesize[0] & (align-1)))
100 break;
101 }
102
103 for (int i = 0; i < 4 && frame->linesize[i]; i++)
104 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
105 }
106
107 for (int i = 0; i < 4; i++)
108 linesizes[i] = frame->linesize[i];
109
110 padded_height = FFALIGN(frame->height, 32);
111 if ((ret = av_image_fill_plane_sizes(sizes, frame->format,
112 padded_height, linesizes)) < 0)
113 return ret;
114
115 total_size = 4 * plane_padding + 4 * align;
116 for (int i = 0; i < 4; i++) {
117 if (sizes[i] > SIZE_MAX - total_size)
118 return AVERROR(EINVAL);
119 total_size += sizes[i];
120 }
121
122 frame->buf[0] = av_buffer_alloc(total_size);
123 if (!frame->buf[0]) {
124 ret = AVERROR(ENOMEM);
125 goto fail;
126 }
127
128 if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height,
129 frame->buf[0]->data, frame->linesize)) < 0)
130 goto fail;
131
132 for (int i = 1; i < 4; i++) {
133 if (frame->data[i])
134 frame->data[i] += i * plane_padding;
135 frame->data[i] = (uint8_t *)FFALIGN((uintptr_t)frame->data[i], align);
136 }
137
138 frame->extended_data = frame->data;
139
140 return 0;
141 fail:
142 av_frame_unref(frame);
143 return ret;
144 }
145
146 static int get_audio_buffer(AVFrame *frame, int align)
147 {
148 int planar = av_sample_fmt_is_planar(frame->format);
149 int channels, planes;
150 size_t size;
151 int ret;
152
153 channels = frame->ch_layout.nb_channels;
154 planes = planar ? channels : 1;
155 if (!frame->linesize[0]) {
156 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
157 frame->nb_samples, frame->format,
158 align);
159 if (ret < 0)
160 return ret;
161 }
162
163 if (align <= 0)
164 align = ALIGN;
165
166 if (planes > AV_NUM_DATA_POINTERS) {
167 frame->extended_data = av_calloc(planes,
168 sizeof(*frame->extended_data));
169 frame->extended_buf = av_calloc(planes - AV_NUM_DATA_POINTERS,
170 sizeof(*frame->extended_buf));
171 if (!frame->extended_data || !frame->extended_buf) {
172 av_freep(&frame->extended_data);
173 av_freep(&frame->extended_buf);
174 return AVERROR(ENOMEM);
175 }
176 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
177 } else
178 frame->extended_data = frame->data;
179
180 if (frame->linesize[0] > SIZE_MAX - align)
181 return AVERROR(EINVAL);
182 size = frame->linesize[0] + (size_t)align;
183
184 for (int i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
185 frame->buf[i] = av_buffer_alloc(size);
186 if (!frame->buf[i]) {
187 av_frame_unref(frame);
188 return AVERROR(ENOMEM);
189 }
190 frame->extended_data[i] = frame->data[i] =
191 (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, align);
192 }
193 for (int i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
194 frame->extended_buf[i] = av_buffer_alloc(size);
195 if (!frame->extended_buf[i]) {
196 av_frame_unref(frame);
197 return AVERROR(ENOMEM);
198 }
199 frame->extended_data[i + AV_NUM_DATA_POINTERS] =
200 (uint8_t *)FFALIGN((uintptr_t)frame->extended_buf[i]->data, align);
201 }
202 return 0;
203
204 }
205
206 int av_frame_get_buffer(AVFrame *frame, int align)
207 {
208 if (frame->format < 0)
209 return AVERROR(EINVAL);
210
211 if (frame->width > 0 && frame->height > 0)
212 return get_video_buffer(frame, align);
213 else if (frame->nb_samples > 0 &&
214 (av_channel_layout_check(&frame->ch_layout)))
215 return get_audio_buffer(frame, align);
216
217 return AVERROR(EINVAL);
218 }
219
220 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
221 {
222 dst->pict_type = src->pict_type;
223 dst->sample_aspect_ratio = src->sample_aspect_ratio;
224 dst->crop_top = src->crop_top;
225 dst->crop_bottom = src->crop_bottom;
226 dst->crop_left = src->crop_left;
227 dst->crop_right = src->crop_right;
228 dst->pts = src->pts;
229 dst->duration = src->duration;
230 dst->repeat_pict = src->repeat_pict;
231 dst->sample_rate = src->sample_rate;
232 dst->opaque = src->opaque;
233 dst->pkt_dts = src->pkt_dts;
234 dst->time_base = src->time_base;
235 dst->quality = src->quality;
236 dst->best_effort_timestamp = src->best_effort_timestamp;
237 dst->flags = src->flags;
238 dst->decode_error_flags = src->decode_error_flags;
239 dst->color_primaries = src->color_primaries;
240 dst->color_trc = src->color_trc;
241 dst->colorspace = src->colorspace;
242 dst->color_range = src->color_range;
243 dst->chroma_location = src->chroma_location;
244 dst->alpha_mode = src->alpha_mode;
245
246 av_dict_copy(&dst->metadata, src->metadata, 0);
247
248 for (int i = 0; i < src->nb_side_data; i++) {
249 const AVFrameSideData *sd_src = src->side_data[i];
250 AVFrameSideData *sd_dst;
251 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
252 && (src->width != dst->width || src->height != dst->height))
253 continue;
254 if (force_copy) {
255 sd_dst = av_frame_new_side_data(dst, sd_src->type,
256 sd_src->size);
257 if (!sd_dst) {
258 av_frame_side_data_free(&dst->side_data, &dst->nb_side_data);
259 return AVERROR(ENOMEM);
260 }
261 memcpy(sd_dst->data, sd_src->data, sd_src->size);
262 } else {
263 AVBufferRef *ref = av_buffer_ref(sd_src->buf);
264 sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
265 if (!sd_dst) {
266 av_buffer_unref(&ref);
267 av_frame_side_data_free(&dst->side_data, &dst->nb_side_data);
268 return AVERROR(ENOMEM);
269 }
270 }
271 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
272 }
273
274 av_refstruct_replace(&dst->private_ref, src->private_ref);
275 return av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
276 }
277
278 int av_frame_ref(AVFrame *dst, const AVFrame *src)
279 {
280 int ret = 0;
281
282 av_assert1(dst->width == 0 && dst->height == 0);
283 av_assert1(dst->ch_layout.nb_channels == 0 &&
284 dst->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
285
286 dst->format = src->format;
287 dst->width = src->width;
288 dst->height = src->height;
289 dst->nb_samples = src->nb_samples;
290
291 ret = frame_copy_props(dst, src, 0);
292 if (ret < 0)
293 goto fail;
294
295 ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
296 if (ret < 0)
297 goto fail;
298
299 /* duplicate the frame data if it's not refcounted */
300 if (!src->buf[0]) {
301 ret = av_frame_get_buffer(dst, 0);
302 if (ret < 0)
303 goto fail;
304
305 ret = av_frame_copy(dst, src);
306 if (ret < 0)
307 goto fail;
308
309 return 0;
310 }
311
312 /* ref the buffers */
313 for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
314 if (!src->buf[i])
315 continue;
316 dst->buf[i] = av_buffer_ref(src->buf[i]);
317 if (!dst->buf[i]) {
318 ret = AVERROR(ENOMEM);
319 goto fail;
320 }
321 }
322
323 if (src->extended_buf) {
324 dst->extended_buf = av_calloc(src->nb_extended_buf,
325 sizeof(*dst->extended_buf));
326 if (!dst->extended_buf) {
327 ret = AVERROR(ENOMEM);
328 goto fail;
329 }
330 dst->nb_extended_buf = src->nb_extended_buf;
331
332 for (int i = 0; i < src->nb_extended_buf; i++) {
333 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
334 if (!dst->extended_buf[i]) {
335 ret = AVERROR(ENOMEM);
336 goto fail;
337 }
338 }
339 }
340
341 if (src->hw_frames_ctx) {
342 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
343 if (!dst->hw_frames_ctx) {
344 ret = AVERROR(ENOMEM);
345 goto fail;
346 }
347 }
348
349 /* duplicate extended data */
350 if (src->extended_data != src->data) {
351 int ch = dst->ch_layout.nb_channels;
352
353 if (ch <= 0 || ch > SIZE_MAX / sizeof(*dst->extended_data)) {
354 ret = AVERROR(EINVAL);
355 goto fail;
356 }
357
358 dst->extended_data = av_memdup(src->extended_data, sizeof(*dst->extended_data) * ch);
359 if (!dst->extended_data) {
360 ret = AVERROR(ENOMEM);
361 goto fail;
362 }
363 } else
364 dst->extended_data = dst->data;
365
366 memcpy(dst->data, src->data, sizeof(src->data));
367 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
368
369 return 0;
370
371 fail:
372 av_frame_unref(dst);
373 return ret;
374 }
375
376 int av_frame_replace(AVFrame *dst, const AVFrame *src)
377 {
378 int ret = 0;
379
380 if (dst == src)
381 return AVERROR(EINVAL);
382
383 if (!src->buf[0]) {
384 av_frame_unref(dst);
385
386 /* duplicate the frame data if it's not refcounted */
387 if ( src->data[0] || src->data[1]
388 || src->data[2] || src->data[3])
389 return av_frame_ref(dst, src);
390
391 ret = frame_copy_props(dst, src, 0);
392 if (ret < 0)
393 goto fail;
394 }
395
396 dst->format = src->format;
397 dst->width = src->width;
398 dst->height = src->height;
399 dst->nb_samples = src->nb_samples;
400
401 ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
402 if (ret < 0)
403 goto fail;
404
405 av_frame_side_data_free(&dst->side_data, &dst->nb_side_data);
406 av_dict_free(&dst->metadata);
407 ret = frame_copy_props(dst, src, 0);
408 if (ret < 0)
409 goto fail;
410
411 /* replace the buffers */
412 for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
413 ret = av_buffer_replace(&dst->buf[i], src->buf[i]);
414 if (ret < 0)
415 goto fail;
416 }
417
418 if (src->extended_buf) {
419 if (dst->nb_extended_buf != src->nb_extended_buf) {
420 int nb_extended_buf = FFMIN(dst->nb_extended_buf, src->nb_extended_buf);
421 void *tmp;
422
423 for (int i = nb_extended_buf; i < dst->nb_extended_buf; i++)
424 av_buffer_unref(&dst->extended_buf[i]);
425
426 tmp = av_realloc_array(dst->extended_buf, src->nb_extended_buf,
427 sizeof(*dst->extended_buf));
428 if (!tmp) {
429 ret = AVERROR(ENOMEM);
430 goto fail;
431 }
432 dst->extended_buf = tmp;
433 dst->nb_extended_buf = src->nb_extended_buf;
434
435 memset(&dst->extended_buf[nb_extended_buf], 0,
436 (src->nb_extended_buf - nb_extended_buf) * sizeof(*dst->extended_buf));
437 }
438
439 for (int i = 0; i < src->nb_extended_buf; i++) {
440 ret = av_buffer_replace(&dst->extended_buf[i], src->extended_buf[i]);
441 if (ret < 0)
442 goto fail;
443 }
444 } else if (dst->extended_buf) {
445 for (int i = 0; i < dst->nb_extended_buf; i++)
446 av_buffer_unref(&dst->extended_buf[i]);
447 av_freep(&dst->extended_buf);
448 }
449
450 ret = av_buffer_replace(&dst->hw_frames_ctx, src->hw_frames_ctx);
451 if (ret < 0)
452 goto fail;
453
454 if (dst->extended_data != dst->data)
455 av_freep(&dst->extended_data);
456
457 if (src->extended_data != src->data) {
458 int ch = dst->ch_layout.nb_channels;
459
460 if (ch <= 0 || ch > SIZE_MAX / sizeof(*dst->extended_data)) {
461 ret = AVERROR(EINVAL);
462 goto fail;
463 }
464
465 dst->extended_data = av_memdup(src->extended_data, sizeof(*dst->extended_data) * ch);
466 if (!dst->extended_data) {
467 ret = AVERROR(ENOMEM);
468 goto fail;
469 }
470 } else
471 dst->extended_data = dst->data;
472
473 memcpy(dst->data, src->data, sizeof(src->data));
474 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
475
476 return 0;
477
478 fail:
479 av_frame_unref(dst);
480 return ret;
481 }
482
483 AVFrame *av_frame_clone(const AVFrame *src)
484 {
485 AVFrame *ret = av_frame_alloc();
486
487 if (!ret)
488 return NULL;
489
490 if (av_frame_ref(ret, src) < 0)
491 av_frame_free(&ret);
492
493 return ret;
494 }
495
496 void av_frame_unref(AVFrame *frame)
497 {
498 if (!frame)
499 return;
500
501 av_frame_side_data_free(&frame->side_data, &frame->nb_side_data);
502
503 for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
504 av_buffer_unref(&frame->buf[i]);
505 for (int i = 0; i < frame->nb_extended_buf; i++)
506 av_buffer_unref(&frame->extended_buf[i]);
507 av_freep(&frame->extended_buf);
508 av_dict_free(&frame->metadata);
509
510 av_buffer_unref(&frame->hw_frames_ctx);
511
512 av_buffer_unref(&frame->opaque_ref);
513 av_refstruct_unref(&frame->private_ref);
514
515 if (frame->extended_data != frame->data)
516 av_freep(&frame->extended_data);
517
518 av_channel_layout_uninit(&frame->ch_layout);
519
520 get_frame_defaults(frame);
521 }
522
523 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
524 {
525 av_assert1(dst->width == 0 && dst->height == 0);
526 av_assert1(dst->ch_layout.nb_channels == 0 &&
527 dst->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
528
529 *dst = *src;
530 if (src->extended_data == src->data)
531 dst->extended_data = dst->data;
532 get_frame_defaults(src);
533 }
534
535 int av_frame_is_writable(AVFrame *frame)
536 {
537 int ret = 1;
538
539 /* assume non-refcounted frames are not writable */
540 if (!frame->buf[0])
541 return 0;
542
543 for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
544 if (frame->buf[i])
545 ret &= !!av_buffer_is_writable(frame->buf[i]);
546 for (int i = 0; i < frame->nb_extended_buf; i++)
547 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
548
549 return ret;
550 }
551
552 int av_frame_make_writable(AVFrame *frame)
553 {
554 AVFrame tmp;
555 int ret;
556
557 if (av_frame_is_writable(frame))
558 return 0;
559
560 memset(&tmp, 0, sizeof(tmp));
561 tmp.format = frame->format;
562 tmp.width = frame->width;
563 tmp.height = frame->height;
564 tmp.nb_samples = frame->nb_samples;
565 ret = av_channel_layout_copy(&tmp.ch_layout, &frame->ch_layout);
566 if (ret < 0) {
567 av_frame_unref(&tmp);
568 return ret;
569 }
570
571 if (frame->hw_frames_ctx)
572 ret = av_hwframe_get_buffer(frame->hw_frames_ctx, &tmp, 0);
573 else
574 ret = av_frame_get_buffer(&tmp, 0);
575 if (ret < 0)
576 return ret;
577
578 ret = av_frame_copy(&tmp, frame);
579 if (ret < 0) {
580 av_frame_unref(&tmp);
581 return ret;
582 }
583
584 ret = av_frame_copy_props(&tmp, frame);
585 if (ret < 0) {
586 av_frame_unref(&tmp);
587 return ret;
588 }
589
590 av_frame_unref(frame);
591
592 *frame = tmp;
593 if (tmp.data == tmp.extended_data)
594 frame->extended_data = frame->data;
595
596 return 0;
597 }
598
599 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
600 {
601 return frame_copy_props(dst, src, 1);
602 }
603
604 AVBufferRef *av_frame_get_plane_buffer(const AVFrame *frame, int plane)
605 {
606 uintptr_t data;
607 int planes;
608
609 if (frame->nb_samples) {
610 int channels = frame->ch_layout.nb_channels;
611 if (!channels)
612 return NULL;
613 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
614 } else
615 planes = 4;
616
617 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
618 return NULL;
619 data = (uintptr_t)frame->extended_data[plane];
620
621 for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
622 AVBufferRef *buf = frame->buf[i];
623 uintptr_t buf_begin = (uintptr_t)buf->data;
624
625 if (data >= buf_begin && data < buf_begin + buf->size)
626 return buf;
627 }
628 for (int i = 0; i < frame->nb_extended_buf; i++) {
629 AVBufferRef *buf = frame->extended_buf[i];
630 uintptr_t buf_begin = (uintptr_t)buf->data;
631
632 if (data >= buf_begin && data < buf_begin + buf->size)
633 return buf;
634 }
635 return NULL;
636 }
637
638 AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
639 enum AVFrameSideDataType type,
640 AVBufferRef *buf)
641 {
642 return
643 ff_frame_side_data_add_from_buf(
644 &frame->side_data, &frame->nb_side_data, type, buf);
645 }
646
647 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
648 enum AVFrameSideDataType type,
649 size_t size)
650 {
651 AVFrameSideData *ret;
652 AVBufferRef *buf = av_buffer_alloc(size);
653 ret = av_frame_new_side_data_from_buf(frame, type, buf);
654 if (!ret)
655 av_buffer_unref(&buf);
656 return ret;
657 }
658
659 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
660 enum AVFrameSideDataType type)
661 {
662 return (AVFrameSideData *)av_frame_side_data_get(
663 frame->side_data, frame->nb_side_data,
664 type
665 );
666 }
667
668 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
669 {
670 int planes;
671
672 if (dst->width < src->width ||
673 dst->height < src->height)
674 return AVERROR(EINVAL);
675
676 if (src->hw_frames_ctx || dst->hw_frames_ctx)
677 return av_hwframe_transfer_data(dst, src, 0);
678
679 planes = av_pix_fmt_count_planes(dst->format);
680 for (int i = 0; i < planes; i++)
681 if (!dst->data[i] || !src->data[i])
682 return AVERROR(EINVAL);
683
684 av_image_copy2(dst->data, dst->linesize,
685 src->data, src->linesize,
686 dst->format, src->width, src->height);
687
688 return 0;
689 }
690
691 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
692 {
693 int planar = av_sample_fmt_is_planar(dst->format);
694 int channels = dst->ch_layout.nb_channels;
695 int planes = planar ? channels : 1;
696
697 if (dst->nb_samples != src->nb_samples ||
698 av_channel_layout_compare(&dst->ch_layout, &src->ch_layout))
699 return AVERROR(EINVAL);
700
701 for (int i = 0; i < planes; i++)
702 if (!dst->extended_data[i] || !src->extended_data[i])
703 return AVERROR(EINVAL);
704
705 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
706 dst->nb_samples, channels, dst->format);
707
708 return 0;
709 }
710
711 int av_frame_copy(AVFrame *dst, const AVFrame *src)
712 {
713 if (dst->format != src->format || dst->format < 0)
714 return AVERROR(EINVAL);
715
716 if (dst->width > 0 && dst->height > 0)
717 return frame_copy_video(dst, src);
718 else if (dst->nb_samples > 0 &&
719 (av_channel_layout_check(&dst->ch_layout)))
720 return frame_copy_audio(dst, src);
721
722 return AVERROR(EINVAL);
723 }
724
725 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
726 {
727 av_frame_side_data_remove(&frame->side_data, &frame->nb_side_data, type);
728 }
729
730 static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
731 const AVPixFmtDescriptor *desc)
732 {
733 for (int i = 0; frame->data[i]; i++) {
734 const AVComponentDescriptor *comp = NULL;
735 int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
736 int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
737
738 if (desc->flags & AV_PIX_FMT_FLAG_PAL && i == 1) {
739 offsets[i] = 0;
740 break;
741 }
742
743 /* find any component descriptor for this plane */
744 for (int j = 0; j < desc->nb_components; j++) {
745 if (desc->comp[j].plane == i) {
746 comp = &desc->comp[j];
747 break;
748 }
749 }
750 if (!comp)
751 return AVERROR_BUG;
752
753 offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
754 (frame->crop_left >> shift_x) * comp->step;
755 }
756
757 return 0;
758 }
759
760 int av_frame_apply_cropping(AVFrame *frame, int flags)
761 {
762 const AVPixFmtDescriptor *desc;
763 size_t offsets[4];
764 int ret;
765
766 if (!(frame->width > 0 && frame->height > 0))
767 return AVERROR(EINVAL);
768
769 if (frame->crop_left >= INT_MAX - frame->crop_right ||
770 frame->crop_top >= INT_MAX - frame->crop_bottom ||
771 (frame->crop_left + frame->crop_right) >= frame->width ||
772 (frame->crop_top + frame->crop_bottom) >= frame->height)
773 return AVERROR(ERANGE);
774
775 desc = av_pix_fmt_desc_get(frame->format);
776 if (!desc)
777 return AVERROR_BUG;
778
779 /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
780 * formats cannot be easily handled here either (and corresponding decoders
781 * should not export any cropping anyway), so do the same for those as well.
782 * */
783 if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
784 frame->width -= frame->crop_right;
785 frame->height -= frame->crop_bottom;
786 frame->crop_right = 0;
787 frame->crop_bottom = 0;
788 return 0;
789 }
790
791 /* calculate the offsets for each plane */
792 ret = calc_cropping_offsets(offsets, frame, desc);
793 if (ret < 0)
794 return ret;
795
796 /* adjust the offsets to avoid breaking alignment */
797 if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
798 int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
799 int min_log2_align = INT_MAX;
800
801 for (int i = 0; frame->data[i]; i++) {
802 int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
803 min_log2_align = FFMIN(log2_align, min_log2_align);
804 }
805
806 /* we assume, and it should always be true, that the data alignment is
807 * related to the cropping alignment by a constant power-of-2 factor */
808 if (log2_crop_align < min_log2_align)
809 return AVERROR_BUG;
810
811 if (min_log2_align < 5 && log2_crop_align != INT_MAX) {
812 frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
813 ret = calc_cropping_offsets(offsets, frame, desc);
814 if (ret < 0)
815 return ret;
816 }
817 }
818
819 for (int i = 0; frame->data[i]; i++)
820 frame->data[i] += offsets[i];
821
822 frame->width -= (frame->crop_left + frame->crop_right);
823 frame->height -= (frame->crop_top + frame->crop_bottom);
824 frame->crop_left = 0;
825 frame->crop_right = 0;
826 frame->crop_top = 0;
827 frame->crop_bottom = 0;
828
829 return 0;
830 }