2 * This file is part of FFmpeg.
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.
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.
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
19 #include "channel_layout.h"
28 #include "samplefmt.h"
29 #include "hwcontext.h"
31 #if FF_API_OLD_CHANNEL_LAYOUT
32 #define CHECK_CHANNELS_CONSISTENCY(frame) \
33 av_assert2(!(frame)->channel_layout || \
34 (frame)->channels == \
35 av_get_channel_layout_nb_channels((frame)->channel_layout))
38 static void get_frame_defaults(AVFrame
*frame
)
40 memset(frame
, 0, sizeof(*frame
));
43 frame
->pkt_dts
= AV_NOPTS_VALUE
;
44 frame
->best_effort_timestamp
= AV_NOPTS_VALUE
;
46 #if FF_API_PKT_DURATION
47 FF_DISABLE_DEPRECATION_WARNINGS
48 frame
->pkt_duration
= 0;
49 FF_ENABLE_DEPRECATION_WARNINGS
52 FF_DISABLE_DEPRECATION_WARNINGS
55 FF_ENABLE_DEPRECATION_WARNINGS
57 frame
->time_base
= (AVRational
){ 0, 1 };
58 frame
->sample_aspect_ratio
= (AVRational
){ 0, 1 };
59 frame
->format
= -1; /* unknown */
60 frame
->extended_data
= frame
->data
;
61 frame
->color_primaries
= AVCOL_PRI_UNSPECIFIED
;
62 frame
->color_trc
= AVCOL_TRC_UNSPECIFIED
;
63 frame
->colorspace
= AVCOL_SPC_UNSPECIFIED
;
64 frame
->color_range
= AVCOL_RANGE_UNSPECIFIED
;
65 frame
->chroma_location
= AVCHROMA_LOC_UNSPECIFIED
;
69 static void free_side_data(AVFrameSideData
**ptr_sd
)
71 AVFrameSideData
*sd
= *ptr_sd
;
73 av_buffer_unref(&sd
->buf
);
74 av_dict_free(&sd
->metadata
);
78 static void wipe_side_data(AVFrame
*frame
)
80 for (int i
= 0; i
< frame
->nb_side_data
; i
++) {
81 free_side_data(&frame
->side_data
[i
]);
83 frame
->nb_side_data
= 0;
85 av_freep(&frame
->side_data
);
88 AVFrame
*av_frame_alloc(void)
90 AVFrame
*frame
= av_malloc(sizeof(*frame
));
95 get_frame_defaults(frame
);
100 void av_frame_free(AVFrame
**frame
)
102 if (!frame
|| !*frame
)
105 av_frame_unref(*frame
);
109 static int get_video_buffer(AVFrame
*frame
, int align
)
111 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(frame
->format
);
112 int ret
, padded_height
, total_size
;
113 int plane_padding
= FFMAX(16 + 16/*STRIDE_ALIGN*/, align
);
114 ptrdiff_t linesizes
[4];
118 return AVERROR(EINVAL
);
120 if ((ret
= av_image_check_size(frame
->width
, frame
->height
, 0, NULL
)) < 0)
123 if (!frame
->linesize
[0]) {
125 align
= 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
127 for (int i
= 1; i
<= align
; i
+= i
) {
128 ret
= av_image_fill_linesizes(frame
->linesize
, frame
->format
,
129 FFALIGN(frame
->width
, i
));
132 if (!(frame
->linesize
[0] & (align
-1)))
136 for (int i
= 0; i
< 4 && frame
->linesize
[i
]; i
++)
137 frame
->linesize
[i
] = FFALIGN(frame
->linesize
[i
], align
);
140 for (int i
= 0; i
< 4; i
++)
141 linesizes
[i
] = frame
->linesize
[i
];
143 padded_height
= FFALIGN(frame
->height
, 32);
144 if ((ret
= av_image_fill_plane_sizes(sizes
, frame
->format
,
145 padded_height
, linesizes
)) < 0)
148 total_size
= 4*plane_padding
;
149 for (int i
= 0; i
< 4; i
++) {
150 if (sizes
[i
] > INT_MAX
- total_size
)
151 return AVERROR(EINVAL
);
152 total_size
+= sizes
[i
];
155 frame
->buf
[0] = av_buffer_alloc(total_size
);
156 if (!frame
->buf
[0]) {
157 ret
= AVERROR(ENOMEM
);
161 if ((ret
= av_image_fill_pointers(frame
->data
, frame
->format
, padded_height
,
162 frame
->buf
[0]->data
, frame
->linesize
)) < 0)
165 for (int i
= 1; i
< 4; i
++) {
167 frame
->data
[i
] += i
* plane_padding
;
170 frame
->extended_data
= frame
->data
;
174 av_frame_unref(frame
);
178 static int get_audio_buffer(AVFrame
*frame
, int align
)
180 int planar
= av_sample_fmt_is_planar(frame
->format
);
181 int channels
, planes
;
184 #if FF_API_OLD_CHANNEL_LAYOUT
185 FF_DISABLE_DEPRECATION_WARNINGS
186 if (!frame
->ch_layout
.nb_channels
) {
187 if (frame
->channel_layout
) {
188 av_channel_layout_from_mask(&frame
->ch_layout
, frame
->channel_layout
);
190 frame
->ch_layout
.nb_channels
= frame
->channels
;
191 frame
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
194 frame
->channels
= frame
->ch_layout
.nb_channels
;
195 frame
->channel_layout
= frame
->ch_layout
.order
== AV_CHANNEL_ORDER_NATIVE
?
196 frame
->ch_layout
.u
.mask
: 0;
197 FF_ENABLE_DEPRECATION_WARNINGS
199 channels
= frame
->ch_layout
.nb_channels
;
200 planes
= planar
? channels
: 1;
201 if (!frame
->linesize
[0]) {
202 ret
= av_samples_get_buffer_size(&frame
->linesize
[0], channels
,
203 frame
->nb_samples
, frame
->format
,
209 if (planes
> AV_NUM_DATA_POINTERS
) {
210 frame
->extended_data
= av_calloc(planes
,
211 sizeof(*frame
->extended_data
));
212 frame
->extended_buf
= av_calloc(planes
- AV_NUM_DATA_POINTERS
,
213 sizeof(*frame
->extended_buf
));
214 if (!frame
->extended_data
|| !frame
->extended_buf
) {
215 av_freep(&frame
->extended_data
);
216 av_freep(&frame
->extended_buf
);
217 return AVERROR(ENOMEM
);
219 frame
->nb_extended_buf
= planes
- AV_NUM_DATA_POINTERS
;
221 frame
->extended_data
= frame
->data
;
223 for (int i
= 0; i
< FFMIN(planes
, AV_NUM_DATA_POINTERS
); i
++) {
224 frame
->buf
[i
] = av_buffer_alloc(frame
->linesize
[0]);
225 if (!frame
->buf
[i
]) {
226 av_frame_unref(frame
);
227 return AVERROR(ENOMEM
);
229 frame
->extended_data
[i
] = frame
->data
[i
] = frame
->buf
[i
]->data
;
231 for (int i
= 0; i
< planes
- AV_NUM_DATA_POINTERS
; i
++) {
232 frame
->extended_buf
[i
] = av_buffer_alloc(frame
->linesize
[0]);
233 if (!frame
->extended_buf
[i
]) {
234 av_frame_unref(frame
);
235 return AVERROR(ENOMEM
);
237 frame
->extended_data
[i
+ AV_NUM_DATA_POINTERS
] = frame
->extended_buf
[i
]->data
;
243 int av_frame_get_buffer(AVFrame
*frame
, int align
)
245 if (frame
->format
< 0)
246 return AVERROR(EINVAL
);
248 FF_DISABLE_DEPRECATION_WARNINGS
249 if (frame
->width
> 0 && frame
->height
> 0)
250 return get_video_buffer(frame
, align
);
251 else if (frame
->nb_samples
> 0 &&
252 (av_channel_layout_check(&frame
->ch_layout
)
253 #if FF_API_OLD_CHANNEL_LAYOUT
254 || frame
->channel_layout
|| frame
->channels
> 0
257 return get_audio_buffer(frame
, align
);
258 FF_ENABLE_DEPRECATION_WARNINGS
260 return AVERROR(EINVAL
);
263 static int frame_copy_props(AVFrame
*dst
, const AVFrame
*src
, int force_copy
)
268 FF_DISABLE_DEPRECATION_WARNINGS
269 dst
->key_frame
= src
->key_frame
;
270 FF_ENABLE_DEPRECATION_WARNINGS
272 dst
->pict_type
= src
->pict_type
;
273 dst
->sample_aspect_ratio
= src
->sample_aspect_ratio
;
274 dst
->crop_top
= src
->crop_top
;
275 dst
->crop_bottom
= src
->crop_bottom
;
276 dst
->crop_left
= src
->crop_left
;
277 dst
->crop_right
= src
->crop_right
;
279 dst
->duration
= src
->duration
;
280 dst
->repeat_pict
= src
->repeat_pict
;
281 #if FF_API_INTERLACED_FRAME
282 FF_DISABLE_DEPRECATION_WARNINGS
283 dst
->interlaced_frame
= src
->interlaced_frame
;
284 dst
->top_field_first
= src
->top_field_first
;
285 FF_ENABLE_DEPRECATION_WARNINGS
287 #if FF_API_PALETTE_HAS_CHANGED
288 FF_DISABLE_DEPRECATION_WARNINGS
289 dst
->palette_has_changed
= src
->palette_has_changed
;
290 FF_ENABLE_DEPRECATION_WARNINGS
292 dst
->sample_rate
= src
->sample_rate
;
293 dst
->opaque
= src
->opaque
;
294 dst
->pkt_dts
= src
->pkt_dts
;
296 FF_DISABLE_DEPRECATION_WARNINGS
297 dst
->pkt_pos
= src
->pkt_pos
;
298 dst
->pkt_size
= src
->pkt_size
;
299 FF_ENABLE_DEPRECATION_WARNINGS
301 #if FF_API_PKT_DURATION
302 FF_DISABLE_DEPRECATION_WARNINGS
303 dst
->pkt_duration
= src
->pkt_duration
;
304 FF_ENABLE_DEPRECATION_WARNINGS
306 dst
->time_base
= src
->time_base
;
307 #if FF_API_REORDERED_OPAQUE
308 FF_DISABLE_DEPRECATION_WARNINGS
309 dst
->reordered_opaque
= src
->reordered_opaque
;
310 FF_ENABLE_DEPRECATION_WARNINGS
312 dst
->quality
= src
->quality
;
313 dst
->best_effort_timestamp
= src
->best_effort_timestamp
;
314 #if FF_API_FRAME_PICTURE_NUMBER
315 FF_DISABLE_DEPRECATION_WARNINGS
316 dst
->coded_picture_number
= src
->coded_picture_number
;
317 dst
->display_picture_number
= src
->display_picture_number
;
318 FF_ENABLE_DEPRECATION_WARNINGS
320 dst
->flags
= src
->flags
;
321 dst
->decode_error_flags
= src
->decode_error_flags
;
322 dst
->color_primaries
= src
->color_primaries
;
323 dst
->color_trc
= src
->color_trc
;
324 dst
->colorspace
= src
->colorspace
;
325 dst
->color_range
= src
->color_range
;
326 dst
->chroma_location
= src
->chroma_location
;
328 av_dict_copy(&dst
->metadata
, src
->metadata
, 0);
330 for (int i
= 0; i
< src
->nb_side_data
; i
++) {
331 const AVFrameSideData
*sd_src
= src
->side_data
[i
];
332 AVFrameSideData
*sd_dst
;
333 if ( sd_src
->type
== AV_FRAME_DATA_PANSCAN
334 && (src
->width
!= dst
->width
|| src
->height
!= dst
->height
))
337 sd_dst
= av_frame_new_side_data(dst
, sd_src
->type
,
341 return AVERROR(ENOMEM
);
343 memcpy(sd_dst
->data
, sd_src
->data
, sd_src
->size
);
345 AVBufferRef
*ref
= av_buffer_ref(sd_src
->buf
);
346 sd_dst
= av_frame_new_side_data_from_buf(dst
, sd_src
->type
, ref
);
348 av_buffer_unref(&ref
);
350 return AVERROR(ENOMEM
);
353 av_dict_copy(&sd_dst
->metadata
, sd_src
->metadata
, 0);
356 ret
= av_buffer_replace(&dst
->opaque_ref
, src
->opaque_ref
);
357 ret
|= av_buffer_replace(&dst
->private_ref
, src
->private_ref
);
361 int av_frame_ref(AVFrame
*dst
, const AVFrame
*src
)
365 av_assert1(dst
->width
== 0 && dst
->height
== 0);
366 #if FF_API_OLD_CHANNEL_LAYOUT
367 FF_DISABLE_DEPRECATION_WARNINGS
368 av_assert1(dst
->channels
== 0);
369 FF_ENABLE_DEPRECATION_WARNINGS
371 av_assert1(dst
->ch_layout
.nb_channels
== 0 &&
372 dst
->ch_layout
.order
== AV_CHANNEL_ORDER_UNSPEC
);
374 dst
->format
= src
->format
;
375 dst
->width
= src
->width
;
376 dst
->height
= src
->height
;
377 dst
->nb_samples
= src
->nb_samples
;
378 #if FF_API_OLD_CHANNEL_LAYOUT
379 FF_DISABLE_DEPRECATION_WARNINGS
380 dst
->channels
= src
->channels
;
381 dst
->channel_layout
= src
->channel_layout
;
382 if (!av_channel_layout_check(&src
->ch_layout
)) {
383 if (src
->channel_layout
)
384 av_channel_layout_from_mask(&dst
->ch_layout
, src
->channel_layout
);
386 dst
->ch_layout
.nb_channels
= src
->channels
;
387 dst
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
390 FF_ENABLE_DEPRECATION_WARNINGS
393 ret
= frame_copy_props(dst
, src
, 0);
397 // this check is needed only until FF_API_OLD_CHANNEL_LAYOUT is out
398 if (av_channel_layout_check(&src
->ch_layout
)) {
399 ret
= av_channel_layout_copy(&dst
->ch_layout
, &src
->ch_layout
);
404 /* duplicate the frame data if it's not refcounted */
406 ret
= av_frame_get_buffer(dst
, 0);
410 ret
= av_frame_copy(dst
, src
);
417 /* ref the buffers */
418 for (int i
= 0; i
< FF_ARRAY_ELEMS(src
->buf
); i
++) {
421 dst
->buf
[i
] = av_buffer_ref(src
->buf
[i
]);
423 ret
= AVERROR(ENOMEM
);
428 if (src
->extended_buf
) {
429 dst
->extended_buf
= av_calloc(src
->nb_extended_buf
,
430 sizeof(*dst
->extended_buf
));
431 if (!dst
->extended_buf
) {
432 ret
= AVERROR(ENOMEM
);
435 dst
->nb_extended_buf
= src
->nb_extended_buf
;
437 for (int i
= 0; i
< src
->nb_extended_buf
; i
++) {
438 dst
->extended_buf
[i
] = av_buffer_ref(src
->extended_buf
[i
]);
439 if (!dst
->extended_buf
[i
]) {
440 ret
= AVERROR(ENOMEM
);
446 if (src
->hw_frames_ctx
) {
447 dst
->hw_frames_ctx
= av_buffer_ref(src
->hw_frames_ctx
);
448 if (!dst
->hw_frames_ctx
) {
449 ret
= AVERROR(ENOMEM
);
454 /* duplicate extended data */
455 if (src
->extended_data
!= src
->data
) {
456 int ch
= dst
->ch_layout
.nb_channels
;
459 ret
= AVERROR(EINVAL
);
463 dst
->extended_data
= av_malloc_array(sizeof(*dst
->extended_data
), ch
);
464 if (!dst
->extended_data
) {
465 ret
= AVERROR(ENOMEM
);
468 memcpy(dst
->extended_data
, src
->extended_data
, sizeof(*src
->extended_data
) * ch
);
470 dst
->extended_data
= dst
->data
;
472 memcpy(dst
->data
, src
->data
, sizeof(src
->data
));
473 memcpy(dst
->linesize
, src
->linesize
, sizeof(src
->linesize
));
482 int av_frame_replace(AVFrame
*dst
, const AVFrame
*src
)
487 return AVERROR(EINVAL
);
492 /* duplicate the frame data if it's not refcounted */
493 if ( src
->data
[0] || src
->data
[1]
494 || src
->data
[2] || src
->data
[3])
495 return av_frame_ref(dst
, src
);
497 ret
= frame_copy_props(dst
, src
, 0);
502 dst
->format
= src
->format
;
503 dst
->width
= src
->width
;
504 dst
->height
= src
->height
;
505 dst
->nb_samples
= src
->nb_samples
;
506 #if FF_API_OLD_CHANNEL_LAYOUT
507 FF_DISABLE_DEPRECATION_WARNINGS
508 dst
->channels
= src
->channels
;
509 dst
->channel_layout
= src
->channel_layout
;
510 if (!av_channel_layout_check(&src
->ch_layout
)) {
511 av_channel_layout_uninit(&dst
->ch_layout
);
512 if (src
->channel_layout
)
513 av_channel_layout_from_mask(&dst
->ch_layout
, src
->channel_layout
);
515 dst
->ch_layout
.nb_channels
= src
->channels
;
516 dst
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
520 ret
= av_channel_layout_copy(&dst
->ch_layout
, &src
->ch_layout
);
523 #if FF_API_OLD_CHANNEL_LAYOUT
525 FF_ENABLE_DEPRECATION_WARNINGS
529 av_dict_free(&dst
->metadata
);
530 ret
= frame_copy_props(dst
, src
, 0);
534 /* replace the buffers */
535 for (int i
= 0; i
< FF_ARRAY_ELEMS(src
->buf
); i
++) {
536 ret
= av_buffer_replace(&dst
->buf
[i
], src
->buf
[i
]);
541 if (src
->extended_buf
) {
542 if (dst
->nb_extended_buf
!= src
->nb_extended_buf
) {
543 int nb_extended_buf
= FFMIN(dst
->nb_extended_buf
, src
->nb_extended_buf
);
546 for (int i
= nb_extended_buf
; i
< dst
->nb_extended_buf
; i
++)
547 av_buffer_unref(&dst
->extended_buf
[i
]);
549 tmp
= av_realloc_array(dst
->extended_buf
, sizeof(*dst
->extended_buf
),
550 src
->nb_extended_buf
);
552 ret
= AVERROR(ENOMEM
);
555 dst
->extended_buf
= tmp
;
556 dst
->nb_extended_buf
= src
->nb_extended_buf
;
558 memset(&dst
->extended_buf
[nb_extended_buf
], 0,
559 (src
->nb_extended_buf
- nb_extended_buf
) * sizeof(*dst
->extended_buf
));
562 for (int i
= 0; i
< src
->nb_extended_buf
; i
++) {
563 ret
= av_buffer_replace(&dst
->extended_buf
[i
], src
->extended_buf
[i
]);
567 } else if (dst
->extended_buf
) {
568 for (int i
= 0; i
< dst
->nb_extended_buf
; i
++)
569 av_buffer_unref(&dst
->extended_buf
[i
]);
570 av_freep(&dst
->extended_buf
);
573 ret
= av_buffer_replace(&dst
->hw_frames_ctx
, src
->hw_frames_ctx
);
577 if (dst
->extended_data
!= dst
->data
)
578 av_freep(&dst
->extended_data
);
580 if (src
->extended_data
!= src
->data
) {
581 int ch
= dst
->ch_layout
.nb_channels
;
584 ret
= AVERROR(EINVAL
);
588 if (ch
> SIZE_MAX
/ sizeof(*dst
->extended_data
))
591 dst
->extended_data
= av_memdup(src
->extended_data
, sizeof(*dst
->extended_data
) * ch
);
592 if (!dst
->extended_data
) {
593 ret
= AVERROR(ENOMEM
);
597 dst
->extended_data
= dst
->data
;
599 memcpy(dst
->data
, src
->data
, sizeof(src
->data
));
600 memcpy(dst
->linesize
, src
->linesize
, sizeof(src
->linesize
));
609 AVFrame
*av_frame_clone(const AVFrame
*src
)
611 AVFrame
*ret
= av_frame_alloc();
616 if (av_frame_ref(ret
, src
) < 0)
622 void av_frame_unref(AVFrame
*frame
)
627 wipe_side_data(frame
);
629 for (int i
= 0; i
< FF_ARRAY_ELEMS(frame
->buf
); i
++)
630 av_buffer_unref(&frame
->buf
[i
]);
631 for (int i
= 0; i
< frame
->nb_extended_buf
; i
++)
632 av_buffer_unref(&frame
->extended_buf
[i
]);
633 av_freep(&frame
->extended_buf
);
634 av_dict_free(&frame
->metadata
);
636 av_buffer_unref(&frame
->hw_frames_ctx
);
638 av_buffer_unref(&frame
->opaque_ref
);
639 av_buffer_unref(&frame
->private_ref
);
641 if (frame
->extended_data
!= frame
->data
)
642 av_freep(&frame
->extended_data
);
644 av_channel_layout_uninit(&frame
->ch_layout
);
646 get_frame_defaults(frame
);
649 void av_frame_move_ref(AVFrame
*dst
, AVFrame
*src
)
651 av_assert1(dst
->width
== 0 && dst
->height
== 0);
652 #if FF_API_OLD_CHANNEL_LAYOUT
653 FF_DISABLE_DEPRECATION_WARNINGS
654 av_assert1(dst
->channels
== 0);
655 FF_ENABLE_DEPRECATION_WARNINGS
657 av_assert1(dst
->ch_layout
.nb_channels
== 0 &&
658 dst
->ch_layout
.order
== AV_CHANNEL_ORDER_UNSPEC
);
661 if (src
->extended_data
== src
->data
)
662 dst
->extended_data
= dst
->data
;
663 get_frame_defaults(src
);
666 int av_frame_is_writable(AVFrame
*frame
)
670 /* assume non-refcounted frames are not writable */
674 for (int i
= 0; i
< FF_ARRAY_ELEMS(frame
->buf
); i
++)
676 ret
&= !!av_buffer_is_writable(frame
->buf
[i
]);
677 for (int i
= 0; i
< frame
->nb_extended_buf
; i
++)
678 ret
&= !!av_buffer_is_writable(frame
->extended_buf
[i
]);
683 int av_frame_make_writable(AVFrame
*frame
)
688 if (av_frame_is_writable(frame
))
691 memset(&tmp
, 0, sizeof(tmp
));
692 tmp
.format
= frame
->format
;
693 tmp
.width
= frame
->width
;
694 tmp
.height
= frame
->height
;
695 #if FF_API_OLD_CHANNEL_LAYOUT
696 FF_DISABLE_DEPRECATION_WARNINGS
697 tmp
.channels
= frame
->channels
;
698 tmp
.channel_layout
= frame
->channel_layout
;
699 FF_ENABLE_DEPRECATION_WARNINGS
701 tmp
.nb_samples
= frame
->nb_samples
;
702 ret
= av_channel_layout_copy(&tmp
.ch_layout
, &frame
->ch_layout
);
704 av_frame_unref(&tmp
);
708 if (frame
->hw_frames_ctx
)
709 ret
= av_hwframe_get_buffer(frame
->hw_frames_ctx
, &tmp
, 0);
711 ret
= av_frame_get_buffer(&tmp
, 0);
715 ret
= av_frame_copy(&tmp
, frame
);
717 av_frame_unref(&tmp
);
721 ret
= av_frame_copy_props(&tmp
, frame
);
723 av_frame_unref(&tmp
);
727 av_frame_unref(frame
);
730 if (tmp
.data
== tmp
.extended_data
)
731 frame
->extended_data
= frame
->data
;
736 int av_frame_copy_props(AVFrame
*dst
, const AVFrame
*src
)
738 return frame_copy_props(dst
, src
, 1);
741 AVBufferRef
*av_frame_get_plane_buffer(const AVFrame
*frame
, int plane
)
746 if (frame
->nb_samples
) {
747 int channels
= frame
->ch_layout
.nb_channels
;
749 #if FF_API_OLD_CHANNEL_LAYOUT
750 FF_DISABLE_DEPRECATION_WARNINGS
752 channels
= frame
->channels
;
753 CHECK_CHANNELS_CONSISTENCY(frame
);
755 FF_ENABLE_DEPRECATION_WARNINGS
759 planes
= av_sample_fmt_is_planar(frame
->format
) ? channels
: 1;
763 if (plane
< 0 || plane
>= planes
|| !frame
->extended_data
[plane
])
765 data
= frame
->extended_data
[plane
];
767 for (int i
= 0; i
< FF_ARRAY_ELEMS(frame
->buf
) && frame
->buf
[i
]; i
++) {
768 AVBufferRef
*buf
= frame
->buf
[i
];
769 if (data
>= buf
->data
&& data
< buf
->data
+ buf
->size
)
772 for (int i
= 0; i
< frame
->nb_extended_buf
; i
++) {
773 AVBufferRef
*buf
= frame
->extended_buf
[i
];
774 if (data
>= buf
->data
&& data
< buf
->data
+ buf
->size
)
780 AVFrameSideData
*av_frame_new_side_data_from_buf(AVFrame
*frame
,
781 enum AVFrameSideDataType type
,
784 AVFrameSideData
*ret
, **tmp
;
789 if (frame
->nb_side_data
> INT_MAX
/ sizeof(*frame
->side_data
) - 1)
792 tmp
= av_realloc(frame
->side_data
,
793 (frame
->nb_side_data
+ 1) * sizeof(*frame
->side_data
));
796 frame
->side_data
= tmp
;
798 ret
= av_mallocz(sizeof(*ret
));
803 ret
->data
= ret
->buf
->data
;
804 ret
->size
= buf
->size
;
807 frame
->side_data
[frame
->nb_side_data
++] = ret
;
812 AVFrameSideData
*av_frame_new_side_data(AVFrame
*frame
,
813 enum AVFrameSideDataType type
,
816 AVFrameSideData
*ret
;
817 AVBufferRef
*buf
= av_buffer_alloc(size
);
818 ret
= av_frame_new_side_data_from_buf(frame
, type
, buf
);
820 av_buffer_unref(&buf
);
824 AVFrameSideData
*av_frame_get_side_data(const AVFrame
*frame
,
825 enum AVFrameSideDataType type
)
827 for (int i
= 0; i
< frame
->nb_side_data
; i
++) {
828 if (frame
->side_data
[i
]->type
== type
)
829 return frame
->side_data
[i
];
834 static int frame_copy_video(AVFrame
*dst
, const AVFrame
*src
)
838 if (dst
->width
< src
->width
||
839 dst
->height
< src
->height
)
840 return AVERROR(EINVAL
);
842 if (src
->hw_frames_ctx
|| dst
->hw_frames_ctx
)
843 return av_hwframe_transfer_data(dst
, src
, 0);
845 planes
= av_pix_fmt_count_planes(dst
->format
);
846 for (int i
= 0; i
< planes
; i
++)
847 if (!dst
->data
[i
] || !src
->data
[i
])
848 return AVERROR(EINVAL
);
850 av_image_copy2(dst
->data
, dst
->linesize
,
851 src
->data
, src
->linesize
,
852 dst
->format
, src
->width
, src
->height
);
857 static int frame_copy_audio(AVFrame
*dst
, const AVFrame
*src
)
859 int planar
= av_sample_fmt_is_planar(dst
->format
);
860 int channels
= dst
->ch_layout
.nb_channels
;
861 int planes
= planar
? channels
: 1;
863 #if FF_API_OLD_CHANNEL_LAYOUT
864 FF_DISABLE_DEPRECATION_WARNINGS
865 if (!channels
|| !src
->ch_layout
.nb_channels
) {
866 if (dst
->channels
!= src
->channels
||
867 dst
->channel_layout
!= src
->channel_layout
)
868 return AVERROR(EINVAL
);
869 CHECK_CHANNELS_CONSISTENCY(src
);
872 channels
= dst
->channels
;
873 planes
= planar
? channels
: 1;
875 FF_ENABLE_DEPRECATION_WARNINGS
878 if (dst
->nb_samples
!= src
->nb_samples
||
879 #if FF_API_OLD_CHANNEL_LAYOUT
880 (av_channel_layout_check(&dst
->ch_layout
) &&
881 av_channel_layout_check(&src
->ch_layout
) &&
883 av_channel_layout_compare(&dst
->ch_layout
, &src
->ch_layout
))
884 #if FF_API_OLD_CHANNEL_LAYOUT
887 return AVERROR(EINVAL
);
889 for (int i
= 0; i
< planes
; i
++)
890 if (!dst
->extended_data
[i
] || !src
->extended_data
[i
])
891 return AVERROR(EINVAL
);
893 av_samples_copy(dst
->extended_data
, src
->extended_data
, 0, 0,
894 dst
->nb_samples
, channels
, dst
->format
);
899 int av_frame_copy(AVFrame
*dst
, const AVFrame
*src
)
901 if (dst
->format
!= src
->format
|| dst
->format
< 0)
902 return AVERROR(EINVAL
);
904 FF_DISABLE_DEPRECATION_WARNINGS
905 if (dst
->width
> 0 && dst
->height
> 0)
906 return frame_copy_video(dst
, src
);
907 else if (dst
->nb_samples
> 0 &&
908 (av_channel_layout_check(&dst
->ch_layout
)
909 #if FF_API_OLD_CHANNEL_LAYOUT
913 return frame_copy_audio(dst
, src
);
914 FF_ENABLE_DEPRECATION_WARNINGS
916 return AVERROR(EINVAL
);
919 void av_frame_remove_side_data(AVFrame
*frame
, enum AVFrameSideDataType type
)
921 for (int i
= frame
->nb_side_data
- 1; i
>= 0; i
--) {
922 AVFrameSideData
*sd
= frame
->side_data
[i
];
923 if (sd
->type
== type
) {
924 free_side_data(&frame
->side_data
[i
]);
925 frame
->side_data
[i
] = frame
->side_data
[frame
->nb_side_data
- 1];
926 frame
->nb_side_data
--;
931 const char *av_frame_side_data_name(enum AVFrameSideDataType type
)
934 case AV_FRAME_DATA_PANSCAN
: return "AVPanScan";
935 case AV_FRAME_DATA_A53_CC
: return "ATSC A53 Part 4 Closed Captions";
936 case AV_FRAME_DATA_STEREO3D
: return "Stereo 3D";
937 case AV_FRAME_DATA_MATRIXENCODING
: return "AVMatrixEncoding";
938 case AV_FRAME_DATA_DOWNMIX_INFO
: return "Metadata relevant to a downmix procedure";
939 case AV_FRAME_DATA_REPLAYGAIN
: return "AVReplayGain";
940 case AV_FRAME_DATA_DISPLAYMATRIX
: return "3x3 displaymatrix";
941 case AV_FRAME_DATA_AFD
: return "Active format description";
942 case AV_FRAME_DATA_MOTION_VECTORS
: return "Motion vectors";
943 case AV_FRAME_DATA_SKIP_SAMPLES
: return "Skip samples";
944 case AV_FRAME_DATA_AUDIO_SERVICE_TYPE
: return "Audio service type";
945 case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
: return "Mastering display metadata";
946 case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
: return "Content light level metadata";
947 case AV_FRAME_DATA_GOP_TIMECODE
: return "GOP timecode";
948 case AV_FRAME_DATA_S12M_TIMECODE
: return "SMPTE 12-1 timecode";
949 case AV_FRAME_DATA_SPHERICAL
: return "Spherical Mapping";
950 case AV_FRAME_DATA_ICC_PROFILE
: return "ICC profile";
951 case AV_FRAME_DATA_DYNAMIC_HDR_PLUS
: return "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)";
952 case AV_FRAME_DATA_DYNAMIC_HDR_VIVID
: return "HDR Dynamic Metadata CUVA 005.1 2021 (Vivid)";
953 case AV_FRAME_DATA_REGIONS_OF_INTEREST
: return "Regions Of Interest";
954 case AV_FRAME_DATA_VIDEO_ENC_PARAMS
: return "Video encoding parameters";
955 case AV_FRAME_DATA_SEI_UNREGISTERED
: return "H.26[45] User Data Unregistered SEI message";
956 case AV_FRAME_DATA_FILM_GRAIN_PARAMS
: return "Film grain parameters";
957 case AV_FRAME_DATA_DETECTION_BBOXES
: return "Bounding boxes for object detection and classification";
958 case AV_FRAME_DATA_DOVI_RPU_BUFFER
: return "Dolby Vision RPU Data";
959 case AV_FRAME_DATA_DOVI_METADATA
: return "Dolby Vision Metadata";
960 case AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT
: return "Ambient viewing environment";
965 static int calc_cropping_offsets(size_t offsets
[4], const AVFrame
*frame
,
966 const AVPixFmtDescriptor
*desc
)
968 for (int i
= 0; frame
->data
[i
]; i
++) {
969 const AVComponentDescriptor
*comp
= NULL
;
970 int shift_x
= (i
== 1 || i
== 2) ? desc
->log2_chroma_w
: 0;
971 int shift_y
= (i
== 1 || i
== 2) ? desc
->log2_chroma_h
: 0;
973 if (desc
->flags
& AV_PIX_FMT_FLAG_PAL
&& i
== 1) {
978 /* find any component descriptor for this plane */
979 for (int j
= 0; j
< desc
->nb_components
; j
++) {
980 if (desc
->comp
[j
].plane
== i
) {
981 comp
= &desc
->comp
[j
];
988 offsets
[i
] = (frame
->crop_top
>> shift_y
) * frame
->linesize
[i
] +
989 (frame
->crop_left
>> shift_x
) * comp
->step
;
995 int av_frame_apply_cropping(AVFrame
*frame
, int flags
)
997 const AVPixFmtDescriptor
*desc
;
1000 if (!(frame
->width
> 0 && frame
->height
> 0))
1001 return AVERROR(EINVAL
);
1003 if (frame
->crop_left
>= INT_MAX
- frame
->crop_right
||
1004 frame
->crop_top
>= INT_MAX
- frame
->crop_bottom
||
1005 (frame
->crop_left
+ frame
->crop_right
) >= frame
->width
||
1006 (frame
->crop_top
+ frame
->crop_bottom
) >= frame
->height
)
1007 return AVERROR(ERANGE
);
1009 desc
= av_pix_fmt_desc_get(frame
->format
);
1013 /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
1014 * formats cannot be easily handled here either (and corresponding decoders
1015 * should not export any cropping anyway), so do the same for those as well.
1017 if (desc
->flags
& (AV_PIX_FMT_FLAG_BITSTREAM
| AV_PIX_FMT_FLAG_HWACCEL
)) {
1018 frame
->width
-= frame
->crop_right
;
1019 frame
->height
-= frame
->crop_bottom
;
1020 frame
->crop_right
= 0;
1021 frame
->crop_bottom
= 0;
1025 /* calculate the offsets for each plane */
1026 calc_cropping_offsets(offsets
, frame
, desc
);
1028 /* adjust the offsets to avoid breaking alignment */
1029 if (!(flags
& AV_FRAME_CROP_UNALIGNED
)) {
1030 int log2_crop_align
= frame
->crop_left
? ff_ctz(frame
->crop_left
) : INT_MAX
;
1031 int min_log2_align
= INT_MAX
;
1033 for (int i
= 0; frame
->data
[i
]; i
++) {
1034 int log2_align
= offsets
[i
] ? ff_ctz(offsets
[i
]) : INT_MAX
;
1035 min_log2_align
= FFMIN(log2_align
, min_log2_align
);
1038 /* we assume, and it should always be true, that the data alignment is
1039 * related to the cropping alignment by a constant power-of-2 factor */
1040 if (log2_crop_align
< min_log2_align
)
1043 if (min_log2_align
< 5) {
1044 frame
->crop_left
&= ~((1 << (5 + log2_crop_align
- min_log2_align
)) - 1);
1045 calc_cropping_offsets(offsets
, frame
, desc
);
1049 for (int i
= 0; frame
->data
[i
]; i
++)
1050 frame
->data
[i
] += offsets
[i
];
1052 frame
->width
-= (frame
->crop_left
+ frame
->crop_right
);
1053 frame
->height
-= (frame
->crop_top
+ frame
->crop_bottom
);
1054 frame
->crop_left
= 0;
1055 frame
->crop_right
= 0;
1056 frame
->crop_top
= 0;
1057 frame
->crop_bottom
= 0;