Line | Branch | Exec | Source |
---|---|---|---|
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 "common.h" | ||
23 | #include "cpu.h" | ||
24 | #include "dict.h" | ||
25 | #include "frame.h" | ||
26 | #include "imgutils.h" | ||
27 | #include "mem.h" | ||
28 | #include "samplefmt.h" | ||
29 | #include "hwcontext.h" | ||
30 | |||
31 | static const AVSideDataDescriptor sd_props[] = { | ||
32 | [AV_FRAME_DATA_PANSCAN] = { "AVPanScan" }, | ||
33 | [AV_FRAME_DATA_A53_CC] = { "ATSC A53 Part 4 Closed Captions" }, | ||
34 | [AV_FRAME_DATA_MATRIXENCODING] = { "AVMatrixEncoding" }, | ||
35 | [AV_FRAME_DATA_DOWNMIX_INFO] = { "Metadata relevant to a downmix procedure" }, | ||
36 | [AV_FRAME_DATA_AFD] = { "Active format description" }, | ||
37 | [AV_FRAME_DATA_MOTION_VECTORS] = { "Motion vectors" }, | ||
38 | [AV_FRAME_DATA_SKIP_SAMPLES] = { "Skip samples" }, | ||
39 | [AV_FRAME_DATA_GOP_TIMECODE] = { "GOP timecode" }, | ||
40 | [AV_FRAME_DATA_S12M_TIMECODE] = { "SMPTE 12-1 timecode" }, | ||
41 | [AV_FRAME_DATA_DYNAMIC_HDR_PLUS] = { "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)" }, | ||
42 | [AV_FRAME_DATA_DYNAMIC_HDR_VIVID] = { "HDR Dynamic Metadata CUVA 005.1 2021 (Vivid)" }, | ||
43 | [AV_FRAME_DATA_REGIONS_OF_INTEREST] = { "Regions Of Interest" }, | ||
44 | [AV_FRAME_DATA_VIDEO_ENC_PARAMS] = { "Video encoding parameters" }, | ||
45 | [AV_FRAME_DATA_FILM_GRAIN_PARAMS] = { "Film grain parameters" }, | ||
46 | [AV_FRAME_DATA_DETECTION_BBOXES] = { "Bounding boxes for object detection and classification" }, | ||
47 | [AV_FRAME_DATA_DOVI_RPU_BUFFER] = { "Dolby Vision RPU Data" }, | ||
48 | [AV_FRAME_DATA_DOVI_METADATA] = { "Dolby Vision Metadata" }, | ||
49 | [AV_FRAME_DATA_LCEVC] = { "LCEVC NAL data" }, | ||
50 | [AV_FRAME_DATA_VIEW_ID] = { "View ID" }, | ||
51 | [AV_FRAME_DATA_STEREO3D] = { "Stereo 3D", AV_SIDE_DATA_PROP_GLOBAL }, | ||
52 | [AV_FRAME_DATA_REPLAYGAIN] = { "AVReplayGain", AV_SIDE_DATA_PROP_GLOBAL }, | ||
53 | [AV_FRAME_DATA_DISPLAYMATRIX] = { "3x3 displaymatrix", AV_SIDE_DATA_PROP_GLOBAL }, | ||
54 | [AV_FRAME_DATA_AUDIO_SERVICE_TYPE] = { "Audio service type", AV_SIDE_DATA_PROP_GLOBAL }, | ||
55 | [AV_FRAME_DATA_MASTERING_DISPLAY_METADATA] = { "Mastering display metadata", AV_SIDE_DATA_PROP_GLOBAL }, | ||
56 | [AV_FRAME_DATA_CONTENT_LIGHT_LEVEL] = { "Content light level metadata", AV_SIDE_DATA_PROP_GLOBAL }, | ||
57 | [AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT] = { "Ambient viewing environment", AV_SIDE_DATA_PROP_GLOBAL }, | ||
58 | [AV_FRAME_DATA_SPHERICAL] = { "Spherical Mapping", AV_SIDE_DATA_PROP_GLOBAL }, | ||
59 | [AV_FRAME_DATA_ICC_PROFILE] = { "ICC profile", AV_SIDE_DATA_PROP_GLOBAL }, | ||
60 | [AV_FRAME_DATA_SEI_UNREGISTERED] = { "H.26[45] User Data Unregistered SEI message", AV_SIDE_DATA_PROP_MULTI }, | ||
61 | }; | ||
62 | |||
63 | 12257832 | static void get_frame_defaults(AVFrame *frame) | |
64 | { | ||
65 | 12257832 | memset(frame, 0, sizeof(*frame)); | |
66 | |||
67 | 12257832 | frame->pts = | |
68 | 12257832 | frame->pkt_dts = AV_NOPTS_VALUE; | |
69 | 12257832 | frame->best_effort_timestamp = AV_NOPTS_VALUE; | |
70 | 12257832 | frame->duration = 0; | |
71 | #if FF_API_FRAME_PKT | ||
72 | FF_DISABLE_DEPRECATION_WARNINGS | ||
73 | 12257832 | frame->pkt_pos = -1; | |
74 | 12257832 | frame->pkt_size = -1; | |
75 | FF_ENABLE_DEPRECATION_WARNINGS | ||
76 | #endif | ||
77 | 12257832 | frame->time_base = (AVRational){ 0, 1 }; | |
78 | 12257832 | frame->sample_aspect_ratio = (AVRational){ 0, 1 }; | |
79 | 12257832 | frame->format = -1; /* unknown */ | |
80 | 12257832 | frame->extended_data = frame->data; | |
81 | 12257832 | frame->color_primaries = AVCOL_PRI_UNSPECIFIED; | |
82 | 12257832 | frame->color_trc = AVCOL_TRC_UNSPECIFIED; | |
83 | 12257832 | frame->colorspace = AVCOL_SPC_UNSPECIFIED; | |
84 | 12257832 | frame->color_range = AVCOL_RANGE_UNSPECIFIED; | |
85 | 12257832 | frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED; | |
86 | 12257832 | frame->flags = 0; | |
87 | 12257832 | } | |
88 | |||
89 | 61234 | static void free_side_data(AVFrameSideData **ptr_sd) | |
90 | { | ||
91 | 61234 | AVFrameSideData *sd = *ptr_sd; | |
92 | |||
93 | 61234 | av_buffer_unref(&sd->buf); | |
94 | 61234 | av_dict_free(&sd->metadata); | |
95 | 61234 | av_freep(ptr_sd); | |
96 | 61234 | } | |
97 | |||
98 | 7582988 | static void wipe_side_data(AVFrameSideData ***sd, int *nb_side_data) | |
99 | { | ||
100 |
2/2✓ Branch 0 taken 60997 times.
✓ Branch 1 taken 7582988 times.
|
7643985 | for (int i = 0; i < *nb_side_data; i++) { |
101 | 60997 | free_side_data(&((*sd)[i])); | |
102 | } | ||
103 | 7582988 | *nb_side_data = 0; | |
104 | |||
105 | 7582988 | av_freep(sd); | |
106 | 7582988 | } | |
107 | |||
108 | 7513723 | static void frame_side_data_wipe(AVFrame *frame) | |
109 | { | ||
110 | 7513723 | wipe_side_data(&frame->side_data, &frame->nb_side_data); | |
111 | 7513723 | } | |
112 | |||
113 | 69265 | void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd) | |
114 | { | ||
115 | 69265 | wipe_side_data(sd, nb_sd); | |
116 | 69265 | } | |
117 | |||
118 | 407002 | static void remove_side_data(AVFrameSideData ***sd, int *nb_side_data, | |
119 | const enum AVFrameSideDataType type) | ||
120 | { | ||
121 |
2/2✓ Branch 0 taken 16434 times.
✓ Branch 1 taken 407002 times.
|
423436 | for (int i = *nb_side_data - 1; i >= 0; i--) { |
122 | 16434 | AVFrameSideData *entry = ((*sd)[i]); | |
123 |
2/2✓ Branch 0 taken 16197 times.
✓ Branch 1 taken 237 times.
|
16434 | if (entry->type != type) |
124 | 16197 | continue; | |
125 | |||
126 | 237 | free_side_data(&entry); | |
127 | |||
128 | 237 | ((*sd)[i]) = ((*sd)[*nb_side_data - 1]); | |
129 | 237 | (*nb_side_data)--; | |
130 | } | ||
131 | 407002 | } | |
132 | |||
133 | ✗ | static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd, | |
134 | const AVFrameSideData *target) | ||
135 | { | ||
136 | ✗ | for (int i = *nb_sd - 1; i >= 0; i--) { | |
137 | ✗ | AVFrameSideData *entry = ((*sd)[i]); | |
138 | ✗ | if (entry != target) | |
139 | ✗ | continue; | |
140 | |||
141 | ✗ | free_side_data(&entry); | |
142 | |||
143 | ✗ | ((*sd)[i]) = ((*sd)[*nb_sd - 1]); | |
144 | ✗ | (*nb_sd)--; | |
145 | |||
146 | ✗ | return; | |
147 | } | ||
148 | } | ||
149 | |||
150 | 1282641 | AVFrame *av_frame_alloc(void) | |
151 | { | ||
152 | 1282641 | AVFrame *frame = av_malloc(sizeof(*frame)); | |
153 | |||
154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1282641 times.
|
1282641 | if (!frame) |
155 | ✗ | return NULL; | |
156 | |||
157 | 1282641 | get_frame_defaults(frame); | |
158 | |||
159 | 1282641 | return frame; | |
160 | } | ||
161 | |||
162 | 1541577 | void av_frame_free(AVFrame **frame) | |
163 | { | ||
164 |
3/4✓ Branch 0 taken 1541577 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 258862 times.
✓ Branch 3 taken 1282715 times.
|
1541577 | if (!frame || !*frame) |
165 | 258862 | return; | |
166 | |||
167 | 1282715 | av_frame_unref(*frame); | |
168 | 1282715 | av_freep(frame); | |
169 | } | ||
170 | |||
171 | #define ALIGN (HAVE_SIMD_ALIGN_64 ? 64 : 32) | ||
172 | |||
173 | 407 | static int get_video_buffer(AVFrame *frame, int align) | |
174 | { | ||
175 | 407 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); | |
176 | int ret, padded_height, total_size; | ||
177 | 407 | int plane_padding = FFMAX(16 + 16/*STRIDE_ALIGN*/, align); | |
178 | ptrdiff_t linesizes[4]; | ||
179 | size_t sizes[4]; | ||
180 | |||
181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
|
407 | if (!desc) |
182 | ✗ | return AVERROR(EINVAL); | |
183 | |||
184 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 407 times.
|
407 | if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0) |
185 | ✗ | return ret; | |
186 | |||
187 |
1/2✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
|
407 | if (!frame->linesize[0]) { |
188 |
2/2✓ Branch 0 taken 383 times.
✓ Branch 1 taken 24 times.
|
407 | if (align <= 0) |
189 | 383 | align = ALIGN; | |
190 | |||
191 |
1/2✓ Branch 0 taken 1088 times.
✗ Branch 1 not taken.
|
1088 | for (int i = 1; i <= align; i += i) { |
192 | 1088 | ret = av_image_fill_linesizes(frame->linesize, frame->format, | |
193 | 1088 | FFALIGN(frame->width, i)); | |
194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1088 times.
|
1088 | if (ret < 0) |
195 | ✗ | return ret; | |
196 |
2/2✓ Branch 0 taken 407 times.
✓ Branch 1 taken 681 times.
|
1088 | if (!(frame->linesize[0] & (align-1))) |
197 | 407 | break; | |
198 | } | ||
199 | |||
200 |
3/4✓ Branch 0 taken 1016 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 609 times.
✓ Branch 3 taken 407 times.
|
1016 | for (int i = 0; i < 4 && frame->linesize[i]; i++) |
201 | 609 | frame->linesize[i] = FFALIGN(frame->linesize[i], align); | |
202 | } | ||
203 | |||
204 |
2/2✓ Branch 0 taken 1628 times.
✓ Branch 1 taken 407 times.
|
2035 | for (int i = 0; i < 4; i++) |
205 | 1628 | linesizes[i] = frame->linesize[i]; | |
206 | |||
207 | 407 | padded_height = FFALIGN(frame->height, 32); | |
208 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 407 times.
|
407 | if ((ret = av_image_fill_plane_sizes(sizes, frame->format, |
209 | padded_height, linesizes)) < 0) | ||
210 | ✗ | return ret; | |
211 | |||
212 | 407 | total_size = 4*plane_padding; | |
213 |
2/2✓ Branch 0 taken 1628 times.
✓ Branch 1 taken 407 times.
|
2035 | for (int i = 0; i < 4; i++) { |
214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1628 times.
|
1628 | if (sizes[i] > INT_MAX - total_size) |
215 | ✗ | return AVERROR(EINVAL); | |
216 | 1628 | total_size += sizes[i]; | |
217 | } | ||
218 | |||
219 | 407 | frame->buf[0] = av_buffer_alloc(total_size); | |
220 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
|
407 | if (!frame->buf[0]) { |
221 | ✗ | ret = AVERROR(ENOMEM); | |
222 | ✗ | goto fail; | |
223 | } | ||
224 | |||
225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
|
407 | if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height, |
226 | 407 | frame->buf[0]->data, frame->linesize)) < 0) | |
227 | ✗ | goto fail; | |
228 | |||
229 |
2/2✓ Branch 0 taken 1221 times.
✓ Branch 1 taken 407 times.
|
1628 | for (int i = 1; i < 4; i++) { |
230 |
2/2✓ Branch 0 taken 202 times.
✓ Branch 1 taken 1019 times.
|
1221 | if (frame->data[i]) |
231 | 202 | frame->data[i] += i * plane_padding; | |
232 | } | ||
233 | |||
234 | 407 | frame->extended_data = frame->data; | |
235 | |||
236 | 407 | return 0; | |
237 | ✗ | fail: | |
238 | ✗ | av_frame_unref(frame); | |
239 | ✗ | return ret; | |
240 | } | ||
241 | |||
242 | 14361 | static int get_audio_buffer(AVFrame *frame, int align) | |
243 | { | ||
244 | 14361 | int planar = av_sample_fmt_is_planar(frame->format); | |
245 | int channels, planes; | ||
246 | int ret; | ||
247 | |||
248 | 14361 | channels = frame->ch_layout.nb_channels; | |
249 |
2/2✓ Branch 0 taken 1169 times.
✓ Branch 1 taken 13192 times.
|
14361 | planes = planar ? channels : 1; |
250 |
1/2✓ Branch 0 taken 14361 times.
✗ Branch 1 not taken.
|
14361 | if (!frame->linesize[0]) { |
251 | 14361 | ret = av_samples_get_buffer_size(&frame->linesize[0], channels, | |
252 | 14361 | frame->nb_samples, frame->format, | |
253 | align); | ||
254 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
|
14361 | if (ret < 0) |
255 | ✗ | return ret; | |
256 | } | ||
257 | |||
258 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
|
14361 | if (planes > AV_NUM_DATA_POINTERS) { |
259 | ✗ | frame->extended_data = av_calloc(planes, | |
260 | sizeof(*frame->extended_data)); | ||
261 | ✗ | frame->extended_buf = av_calloc(planes - AV_NUM_DATA_POINTERS, | |
262 | sizeof(*frame->extended_buf)); | ||
263 | ✗ | if (!frame->extended_data || !frame->extended_buf) { | |
264 | ✗ | av_freep(&frame->extended_data); | |
265 | ✗ | av_freep(&frame->extended_buf); | |
266 | ✗ | return AVERROR(ENOMEM); | |
267 | } | ||
268 | ✗ | frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS; | |
269 | } else | ||
270 | 14361 | frame->extended_data = frame->data; | |
271 | |||
272 |
2/2✓ Branch 0 taken 15156 times.
✓ Branch 1 taken 14361 times.
|
29517 | for (int i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) { |
273 | 15156 | frame->buf[i] = av_buffer_alloc(frame->linesize[0]); | |
274 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15156 times.
|
15156 | if (!frame->buf[i]) { |
275 | ✗ | av_frame_unref(frame); | |
276 | ✗ | return AVERROR(ENOMEM); | |
277 | } | ||
278 | 15156 | frame->extended_data[i] = frame->data[i] = frame->buf[i]->data; | |
279 | } | ||
280 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14361 times.
|
14361 | for (int i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) { |
281 | ✗ | frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]); | |
282 | ✗ | if (!frame->extended_buf[i]) { | |
283 | ✗ | av_frame_unref(frame); | |
284 | ✗ | return AVERROR(ENOMEM); | |
285 | } | ||
286 | ✗ | frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data; | |
287 | } | ||
288 | 14361 | return 0; | |
289 | |||
290 | } | ||
291 | |||
292 | 14768 | int av_frame_get_buffer(AVFrame *frame, int align) | |
293 | { | ||
294 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14768 times.
|
14768 | if (frame->format < 0) |
295 | ✗ | return AVERROR(EINVAL); | |
296 | |||
297 |
3/4✓ Branch 0 taken 407 times.
✓ Branch 1 taken 14361 times.
✓ Branch 2 taken 407 times.
✗ Branch 3 not taken.
|
14768 | if (frame->width > 0 && frame->height > 0) |
298 | 407 | return get_video_buffer(frame, align); | |
299 |
2/4✓ Branch 0 taken 14361 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14361 times.
✗ Branch 3 not taken.
|
28722 | else if (frame->nb_samples > 0 && |
300 | 14361 | (av_channel_layout_check(&frame->ch_layout))) | |
301 | 14361 | return get_audio_buffer(frame, align); | |
302 | |||
303 | ✗ | return AVERROR(EINVAL); | |
304 | } | ||
305 | |||
306 | 1303663 | static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy) | |
307 | { | ||
308 | int ret; | ||
309 | |||
310 | #if FF_API_FRAME_KEY | ||
311 | FF_DISABLE_DEPRECATION_WARNINGS | ||
312 | 1303663 | dst->key_frame = src->key_frame; | |
313 | FF_ENABLE_DEPRECATION_WARNINGS | ||
314 | #endif | ||
315 | 1303663 | dst->pict_type = src->pict_type; | |
316 | 1303663 | dst->sample_aspect_ratio = src->sample_aspect_ratio; | |
317 | 1303663 | dst->crop_top = src->crop_top; | |
318 | 1303663 | dst->crop_bottom = src->crop_bottom; | |
319 | 1303663 | dst->crop_left = src->crop_left; | |
320 | 1303663 | dst->crop_right = src->crop_right; | |
321 | 1303663 | dst->pts = src->pts; | |
322 | 1303663 | dst->duration = src->duration; | |
323 | 1303663 | dst->repeat_pict = src->repeat_pict; | |
324 | #if FF_API_INTERLACED_FRAME | ||
325 | FF_DISABLE_DEPRECATION_WARNINGS | ||
326 | 1303663 | dst->interlaced_frame = src->interlaced_frame; | |
327 | 1303663 | dst->top_field_first = src->top_field_first; | |
328 | FF_ENABLE_DEPRECATION_WARNINGS | ||
329 | #endif | ||
330 | #if FF_API_PALETTE_HAS_CHANGED | ||
331 | FF_DISABLE_DEPRECATION_WARNINGS | ||
332 | 1303663 | dst->palette_has_changed = src->palette_has_changed; | |
333 | FF_ENABLE_DEPRECATION_WARNINGS | ||
334 | #endif | ||
335 | 1303663 | dst->sample_rate = src->sample_rate; | |
336 | 1303663 | dst->opaque = src->opaque; | |
337 | 1303663 | dst->pkt_dts = src->pkt_dts; | |
338 | #if FF_API_FRAME_PKT | ||
339 | FF_DISABLE_DEPRECATION_WARNINGS | ||
340 | 1303663 | dst->pkt_pos = src->pkt_pos; | |
341 | 1303663 | dst->pkt_size = src->pkt_size; | |
342 | FF_ENABLE_DEPRECATION_WARNINGS | ||
343 | #endif | ||
344 | 1303663 | dst->time_base = src->time_base; | |
345 | 1303663 | dst->quality = src->quality; | |
346 | 1303663 | dst->best_effort_timestamp = src->best_effort_timestamp; | |
347 | 1303663 | dst->flags = src->flags; | |
348 | 1303663 | dst->decode_error_flags = src->decode_error_flags; | |
349 | 1303663 | dst->color_primaries = src->color_primaries; | |
350 | 1303663 | dst->color_trc = src->color_trc; | |
351 | 1303663 | dst->colorspace = src->colorspace; | |
352 | 1303663 | dst->color_range = src->color_range; | |
353 | 1303663 | dst->chroma_location = src->chroma_location; | |
354 | |||
355 | 1303663 | av_dict_copy(&dst->metadata, src->metadata, 0); | |
356 | |||
357 |
2/2✓ Branch 0 taken 44029 times.
✓ Branch 1 taken 1303663 times.
|
1347692 | for (int i = 0; i < src->nb_side_data; i++) { |
358 | 44029 | const AVFrameSideData *sd_src = src->side_data[i]; | |
359 | AVFrameSideData *sd_dst; | ||
360 |
2/2✓ Branch 0 taken 13894 times.
✓ Branch 1 taken 30135 times.
|
44029 | if ( sd_src->type == AV_FRAME_DATA_PANSCAN |
361 |
2/4✓ Branch 0 taken 13894 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13894 times.
|
13894 | && (src->width != dst->width || src->height != dst->height)) |
362 | ✗ | continue; | |
363 |
2/2✓ Branch 0 taken 8245 times.
✓ Branch 1 taken 35784 times.
|
44029 | if (force_copy) { |
364 | 8245 | sd_dst = av_frame_new_side_data(dst, sd_src->type, | |
365 | 8245 | sd_src->size); | |
366 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8245 times.
|
8245 | if (!sd_dst) { |
367 | ✗ | frame_side_data_wipe(dst); | |
368 | ✗ | return AVERROR(ENOMEM); | |
369 | } | ||
370 | 8245 | memcpy(sd_dst->data, sd_src->data, sd_src->size); | |
371 | } else { | ||
372 | 35784 | AVBufferRef *ref = av_buffer_ref(sd_src->buf); | |
373 | 35784 | sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref); | |
374 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35784 times.
|
35784 | if (!sd_dst) { |
375 | ✗ | av_buffer_unref(&ref); | |
376 | ✗ | frame_side_data_wipe(dst); | |
377 | ✗ | return AVERROR(ENOMEM); | |
378 | } | ||
379 | } | ||
380 | 44029 | av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0); | |
381 | } | ||
382 | |||
383 | 1303663 | ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref); | |
384 | 1303663 | ret |= av_buffer_replace(&dst->private_ref, src->private_ref); | |
385 | 1303663 | return ret; | |
386 | } | ||
387 | |||
388 | 952821 | int av_frame_ref(AVFrame *dst, const AVFrame *src) | |
389 | { | ||
390 | 952821 | int ret = 0; | |
391 | |||
392 | av_assert1(dst->width == 0 && dst->height == 0); | ||
393 | av_assert1(dst->ch_layout.nb_channels == 0 && | ||
394 | dst->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC); | ||
395 | |||
396 | 952821 | dst->format = src->format; | |
397 | 952821 | dst->width = src->width; | |
398 | 952821 | dst->height = src->height; | |
399 | 952821 | dst->nb_samples = src->nb_samples; | |
400 | |||
401 | 952821 | ret = frame_copy_props(dst, src, 0); | |
402 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 952821 times.
|
952821 | if (ret < 0) |
403 | ✗ | goto fail; | |
404 | |||
405 | 952821 | ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout); | |
406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 952821 times.
|
952821 | if (ret < 0) |
407 | ✗ | goto fail; | |
408 | |||
409 | /* duplicate the frame data if it's not refcounted */ | ||
410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 952821 times.
|
952821 | if (!src->buf[0]) { |
411 | ✗ | ret = av_frame_get_buffer(dst, 0); | |
412 | ✗ | if (ret < 0) | |
413 | ✗ | goto fail; | |
414 | |||
415 | ✗ | ret = av_frame_copy(dst, src); | |
416 | ✗ | if (ret < 0) | |
417 | ✗ | goto fail; | |
418 | |||
419 | ✗ | return 0; | |
420 | } | ||
421 | |||
422 | /* ref the buffers */ | ||
423 |
2/2✓ Branch 0 taken 7622568 times.
✓ Branch 1 taken 952821 times.
|
8575389 | for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) { |
424 |
2/2✓ Branch 0 taken 5739152 times.
✓ Branch 1 taken 1883416 times.
|
7622568 | if (!src->buf[i]) |
425 | 5739152 | continue; | |
426 | 1883416 | dst->buf[i] = av_buffer_ref(src->buf[i]); | |
427 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1883416 times.
|
1883416 | if (!dst->buf[i]) { |
428 | ✗ | ret = AVERROR(ENOMEM); | |
429 | ✗ | goto fail; | |
430 | } | ||
431 | } | ||
432 | |||
433 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 952755 times.
|
952821 | if (src->extended_buf) { |
434 | 66 | dst->extended_buf = av_calloc(src->nb_extended_buf, | |
435 | sizeof(*dst->extended_buf)); | ||
436 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (!dst->extended_buf) { |
437 | ✗ | ret = AVERROR(ENOMEM); | |
438 | ✗ | goto fail; | |
439 | } | ||
440 | 66 | dst->nb_extended_buf = src->nb_extended_buf; | |
441 | |||
442 |
2/2✓ Branch 0 taken 3828 times.
✓ Branch 1 taken 66 times.
|
3894 | for (int i = 0; i < src->nb_extended_buf; i++) { |
443 | 3828 | dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]); | |
444 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3828 times.
|
3828 | if (!dst->extended_buf[i]) { |
445 | ✗ | ret = AVERROR(ENOMEM); | |
446 | ✗ | goto fail; | |
447 | } | ||
448 | } | ||
449 | } | ||
450 | |||
451 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 952821 times.
|
952821 | if (src->hw_frames_ctx) { |
452 | ✗ | dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx); | |
453 | ✗ | if (!dst->hw_frames_ctx) { | |
454 | ✗ | ret = AVERROR(ENOMEM); | |
455 | ✗ | goto fail; | |
456 | } | ||
457 | } | ||
458 | |||
459 | /* duplicate extended data */ | ||
460 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 952755 times.
|
952821 | if (src->extended_data != src->data) { |
461 | 66 | int ch = dst->ch_layout.nb_channels; | |
462 | |||
463 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (!ch) { |
464 | ✗ | ret = AVERROR(EINVAL); | |
465 | ✗ | goto fail; | |
466 | } | ||
467 | |||
468 | 66 | dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch); | |
469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (!dst->extended_data) { |
470 | ✗ | ret = AVERROR(ENOMEM); | |
471 | ✗ | goto fail; | |
472 | } | ||
473 | 66 | memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch); | |
474 | } else | ||
475 | 952755 | dst->extended_data = dst->data; | |
476 | |||
477 | 952821 | memcpy(dst->data, src->data, sizeof(src->data)); | |
478 | 952821 | memcpy(dst->linesize, src->linesize, sizeof(src->linesize)); | |
479 | |||
480 | 952821 | return 0; | |
481 | |||
482 | ✗ | fail: | |
483 | ✗ | av_frame_unref(dst); | |
484 | ✗ | return ret; | |
485 | } | ||
486 | |||
487 | 4349 | int av_frame_replace(AVFrame *dst, const AVFrame *src) | |
488 | { | ||
489 | 4349 | int ret = 0; | |
490 | |||
491 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | if (dst == src) |
492 | ✗ | return AVERROR(EINVAL); | |
493 | |||
494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | if (!src->buf[0]) { |
495 | ✗ | av_frame_unref(dst); | |
496 | |||
497 | /* duplicate the frame data if it's not refcounted */ | ||
498 | ✗ | if ( src->data[0] || src->data[1] | |
499 | ✗ | || src->data[2] || src->data[3]) | |
500 | ✗ | return av_frame_ref(dst, src); | |
501 | |||
502 | ✗ | ret = frame_copy_props(dst, src, 0); | |
503 | ✗ | if (ret < 0) | |
504 | ✗ | goto fail; | |
505 | } | ||
506 | |||
507 | 4349 | dst->format = src->format; | |
508 | 4349 | dst->width = src->width; | |
509 | 4349 | dst->height = src->height; | |
510 | 4349 | dst->nb_samples = src->nb_samples; | |
511 | |||
512 | 4349 | ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout); | |
513 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | if (ret < 0) |
514 | ✗ | goto fail; | |
515 | |||
516 | 4349 | frame_side_data_wipe(dst); | |
517 | 4349 | av_dict_free(&dst->metadata); | |
518 | 4349 | ret = frame_copy_props(dst, src, 0); | |
519 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | if (ret < 0) |
520 | ✗ | goto fail; | |
521 | |||
522 | /* replace the buffers */ | ||
523 |
2/2✓ Branch 0 taken 34792 times.
✓ Branch 1 taken 4349 times.
|
39141 | for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) { |
524 | 34792 | ret = av_buffer_replace(&dst->buf[i], src->buf[i]); | |
525 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34792 times.
|
34792 | if (ret < 0) |
526 | ✗ | goto fail; | |
527 | } | ||
528 | |||
529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | if (src->extended_buf) { |
530 | ✗ | if (dst->nb_extended_buf != src->nb_extended_buf) { | |
531 | ✗ | int nb_extended_buf = FFMIN(dst->nb_extended_buf, src->nb_extended_buf); | |
532 | void *tmp; | ||
533 | |||
534 | ✗ | for (int i = nb_extended_buf; i < dst->nb_extended_buf; i++) | |
535 | ✗ | av_buffer_unref(&dst->extended_buf[i]); | |
536 | |||
537 | ✗ | tmp = av_realloc_array(dst->extended_buf, sizeof(*dst->extended_buf), | |
538 | ✗ | src->nb_extended_buf); | |
539 | ✗ | if (!tmp) { | |
540 | ✗ | ret = AVERROR(ENOMEM); | |
541 | ✗ | goto fail; | |
542 | } | ||
543 | ✗ | dst->extended_buf = tmp; | |
544 | ✗ | dst->nb_extended_buf = src->nb_extended_buf; | |
545 | |||
546 | ✗ | memset(&dst->extended_buf[nb_extended_buf], 0, | |
547 | ✗ | (src->nb_extended_buf - nb_extended_buf) * sizeof(*dst->extended_buf)); | |
548 | } | ||
549 | |||
550 | ✗ | for (int i = 0; i < src->nb_extended_buf; i++) { | |
551 | ✗ | ret = av_buffer_replace(&dst->extended_buf[i], src->extended_buf[i]); | |
552 | ✗ | if (ret < 0) | |
553 | ✗ | goto fail; | |
554 | } | ||
555 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | } else if (dst->extended_buf) { |
556 | ✗ | for (int i = 0; i < dst->nb_extended_buf; i++) | |
557 | ✗ | av_buffer_unref(&dst->extended_buf[i]); | |
558 | ✗ | av_freep(&dst->extended_buf); | |
559 | } | ||
560 | |||
561 | 4349 | ret = av_buffer_replace(&dst->hw_frames_ctx, src->hw_frames_ctx); | |
562 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | if (ret < 0) |
563 | ✗ | goto fail; | |
564 | |||
565 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | if (dst->extended_data != dst->data) |
566 | ✗ | av_freep(&dst->extended_data); | |
567 | |||
568 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4349 times.
|
4349 | if (src->extended_data != src->data) { |
569 | ✗ | int ch = dst->ch_layout.nb_channels; | |
570 | |||
571 | ✗ | if (!ch) { | |
572 | ✗ | ret = AVERROR(EINVAL); | |
573 | ✗ | goto fail; | |
574 | } | ||
575 | |||
576 | ✗ | if (ch > SIZE_MAX / sizeof(*dst->extended_data)) | |
577 | ✗ | goto fail; | |
578 | |||
579 | ✗ | dst->extended_data = av_memdup(src->extended_data, sizeof(*dst->extended_data) * ch); | |
580 | ✗ | if (!dst->extended_data) { | |
581 | ✗ | ret = AVERROR(ENOMEM); | |
582 | ✗ | goto fail; | |
583 | } | ||
584 | } else | ||
585 | 4349 | dst->extended_data = dst->data; | |
586 | |||
587 | 4349 | memcpy(dst->data, src->data, sizeof(src->data)); | |
588 | 4349 | memcpy(dst->linesize, src->linesize, sizeof(src->linesize)); | |
589 | |||
590 | 4349 | return 0; | |
591 | |||
592 | ✗ | fail: | |
593 | ✗ | av_frame_unref(dst); | |
594 | ✗ | return ret; | |
595 | } | ||
596 | |||
597 | 39077 | AVFrame *av_frame_clone(const AVFrame *src) | |
598 | { | ||
599 | 39077 | AVFrame *ret = av_frame_alloc(); | |
600 | |||
601 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39077 times.
|
39077 | if (!ret) |
602 | ✗ | return NULL; | |
603 | |||
604 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39077 times.
|
39077 | if (av_frame_ref(ret, src) < 0) |
605 | ✗ | av_frame_free(&ret); | |
606 | |||
607 | 39077 | return ret; | |
608 | } | ||
609 | |||
610 | 7509378 | void av_frame_unref(AVFrame *frame) | |
611 | { | ||
612 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7509374 times.
|
7509378 | if (!frame) |
613 | 4 | return; | |
614 | |||
615 | 7509374 | frame_side_data_wipe(frame); | |
616 | |||
617 |
2/2✓ Branch 0 taken 60074992 times.
✓ Branch 1 taken 7509374 times.
|
67584366 | for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++) |
618 | 60074992 | av_buffer_unref(&frame->buf[i]); | |
619 |
2/2✓ Branch 0 taken 5240 times.
✓ Branch 1 taken 7509374 times.
|
7514614 | for (int i = 0; i < frame->nb_extended_buf; i++) |
620 | 5240 | av_buffer_unref(&frame->extended_buf[i]); | |
621 | 7509374 | av_freep(&frame->extended_buf); | |
622 | 7509374 | av_dict_free(&frame->metadata); | |
623 | |||
624 | 7509374 | av_buffer_unref(&frame->hw_frames_ctx); | |
625 | |||
626 | 7509374 | av_buffer_unref(&frame->opaque_ref); | |
627 | 7509374 | av_buffer_unref(&frame->private_ref); | |
628 | |||
629 |
2/2✓ Branch 0 taken 464 times.
✓ Branch 1 taken 7508910 times.
|
7509374 | if (frame->extended_data != frame->data) |
630 | 464 | av_freep(&frame->extended_data); | |
631 | |||
632 | 7509374 | av_channel_layout_uninit(&frame->ch_layout); | |
633 | |||
634 | 7509374 | get_frame_defaults(frame); | |
635 | } | ||
636 | |||
637 | 3465817 | void av_frame_move_ref(AVFrame *dst, AVFrame *src) | |
638 | { | ||
639 | av_assert1(dst->width == 0 && dst->height == 0); | ||
640 | av_assert1(dst->ch_layout.nb_channels == 0 && | ||
641 | dst->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC); | ||
642 | |||
643 | 3465817 | *dst = *src; | |
644 |
1/2✓ Branch 0 taken 3465817 times.
✗ Branch 1 not taken.
|
3465817 | if (src->extended_data == src->data) |
645 | 3465817 | dst->extended_data = dst->data; | |
646 | 3465817 | get_frame_defaults(src); | |
647 | 3465817 | } | |
648 | |||
649 | 18019 | int av_frame_is_writable(AVFrame *frame) | |
650 | { | ||
651 | 18019 | int ret = 1; | |
652 | |||
653 | /* assume non-refcounted frames are not writable */ | ||
654 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18019 times.
|
18019 | if (!frame->buf[0]) |
655 | ✗ | return 0; | |
656 | |||
657 |
2/2✓ Branch 0 taken 144152 times.
✓ Branch 1 taken 18019 times.
|
162171 | for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++) |
658 |
2/2✓ Branch 0 taken 35453 times.
✓ Branch 1 taken 108699 times.
|
144152 | if (frame->buf[i]) |
659 | 35453 | ret &= !!av_buffer_is_writable(frame->buf[i]); | |
660 |
2/2✓ Branch 0 taken 1354 times.
✓ Branch 1 taken 18019 times.
|
19373 | for (int i = 0; i < frame->nb_extended_buf; i++) |
661 | 1354 | ret &= !!av_buffer_is_writable(frame->extended_buf[i]); | |
662 | |||
663 | 18019 | return ret; | |
664 | } | ||
665 | |||
666 | 3200 | int av_frame_make_writable(AVFrame *frame) | |
667 | { | ||
668 | AVFrame tmp; | ||
669 | int ret; | ||
670 | |||
671 |
1/2✓ Branch 1 taken 3200 times.
✗ Branch 2 not taken.
|
3200 | if (av_frame_is_writable(frame)) |
672 | 3200 | return 0; | |
673 | |||
674 | ✗ | memset(&tmp, 0, sizeof(tmp)); | |
675 | ✗ | tmp.format = frame->format; | |
676 | ✗ | tmp.width = frame->width; | |
677 | ✗ | tmp.height = frame->height; | |
678 | ✗ | tmp.nb_samples = frame->nb_samples; | |
679 | ✗ | ret = av_channel_layout_copy(&tmp.ch_layout, &frame->ch_layout); | |
680 | ✗ | if (ret < 0) { | |
681 | ✗ | av_frame_unref(&tmp); | |
682 | ✗ | return ret; | |
683 | } | ||
684 | |||
685 | ✗ | if (frame->hw_frames_ctx) | |
686 | ✗ | ret = av_hwframe_get_buffer(frame->hw_frames_ctx, &tmp, 0); | |
687 | else | ||
688 | ✗ | ret = av_frame_get_buffer(&tmp, 0); | |
689 | ✗ | if (ret < 0) | |
690 | ✗ | return ret; | |
691 | |||
692 | ✗ | ret = av_frame_copy(&tmp, frame); | |
693 | ✗ | if (ret < 0) { | |
694 | ✗ | av_frame_unref(&tmp); | |
695 | ✗ | return ret; | |
696 | } | ||
697 | |||
698 | ✗ | ret = av_frame_copy_props(&tmp, frame); | |
699 | ✗ | if (ret < 0) { | |
700 | ✗ | av_frame_unref(&tmp); | |
701 | ✗ | return ret; | |
702 | } | ||
703 | |||
704 | ✗ | av_frame_unref(frame); | |
705 | |||
706 | ✗ | *frame = tmp; | |
707 | ✗ | if (tmp.data == tmp.extended_data) | |
708 | ✗ | frame->extended_data = frame->data; | |
709 | |||
710 | ✗ | return 0; | |
711 | } | ||
712 | |||
713 | 346493 | int av_frame_copy_props(AVFrame *dst, const AVFrame *src) | |
714 | { | ||
715 | 346493 | return frame_copy_props(dst, src, 1); | |
716 | } | ||
717 | |||
718 | 5540 | AVBufferRef *av_frame_get_plane_buffer(const AVFrame *frame, int plane) | |
719 | { | ||
720 | uint8_t *data; | ||
721 | int planes; | ||
722 | |||
723 |
2/2✓ Branch 0 taken 457 times.
✓ Branch 1 taken 5083 times.
|
5540 | if (frame->nb_samples) { |
724 | 457 | int channels = frame->ch_layout.nb_channels; | |
725 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 457 times.
|
457 | if (!channels) |
726 | ✗ | return NULL; | |
727 |
1/2✓ Branch 1 taken 457 times.
✗ Branch 2 not taken.
|
457 | planes = av_sample_fmt_is_planar(frame->format) ? channels : 1; |
728 | } else | ||
729 | 5083 | planes = 4; | |
730 | |||
731 |
3/6✓ Branch 0 taken 5540 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5540 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5540 times.
|
5540 | if (plane < 0 || plane >= planes || !frame->extended_data[plane]) |
732 | ✗ | return NULL; | |
733 | 5540 | data = frame->extended_data[plane]; | |
734 | |||
735 |
2/4✓ Branch 0 taken 12932 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12932 times.
✗ Branch 3 not taken.
|
12932 | for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) { |
736 | 12932 | AVBufferRef *buf = frame->buf[i]; | |
737 |
4/4✓ Branch 0 taken 11947 times.
✓ Branch 1 taken 985 times.
✓ Branch 2 taken 5540 times.
✓ Branch 3 taken 6407 times.
|
12932 | if (data >= buf->data && data < buf->data + buf->size) |
738 | 5540 | return buf; | |
739 | } | ||
740 | ✗ | for (int i = 0; i < frame->nb_extended_buf; i++) { | |
741 | ✗ | AVBufferRef *buf = frame->extended_buf[i]; | |
742 | ✗ | if (data >= buf->data && data < buf->data + buf->size) | |
743 | ✗ | return buf; | |
744 | } | ||
745 | ✗ | return NULL; | |
746 | } | ||
747 | |||
748 | 61234 | static AVFrameSideData *add_side_data_from_buf_ext(AVFrameSideData ***sd, | |
749 | int *nb_sd, | ||
750 | enum AVFrameSideDataType type, | ||
751 | AVBufferRef *buf, uint8_t *data, | ||
752 | size_t size) | ||
753 | { | ||
754 | AVFrameSideData *ret, **tmp; | ||
755 | |||
756 | // *nb_sd + 1 needs to fit into an int and a size_t. | ||
757 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61234 times.
|
61234 | if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX)) |
758 | ✗ | return NULL; | |
759 | |||
760 | 61234 | tmp = av_realloc_array(*sd, sizeof(**sd), *nb_sd + 1); | |
761 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61234 times.
|
61234 | if (!tmp) |
762 | ✗ | return NULL; | |
763 | 61234 | *sd = tmp; | |
764 | |||
765 | 61234 | ret = av_mallocz(sizeof(*ret)); | |
766 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61234 times.
|
61234 | if (!ret) |
767 | ✗ | return NULL; | |
768 | |||
769 | 61234 | ret->buf = buf; | |
770 | 61234 | ret->data = data; | |
771 | 61234 | ret->size = size; | |
772 | 61234 | ret->type = type; | |
773 | |||
774 | 61234 | (*sd)[(*nb_sd)++] = ret; | |
775 | |||
776 | 61234 | return ret; | |
777 | } | ||
778 | |||
779 | 61002 | static AVFrameSideData *add_side_data_from_buf(AVFrameSideData ***sd, | |
780 | int *nb_sd, | ||
781 | enum AVFrameSideDataType type, | ||
782 | AVBufferRef *buf) | ||
783 | { | ||
784 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61002 times.
|
61002 | if (!buf) |
785 | ✗ | return NULL; | |
786 | |||
787 | 61002 | return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size); | |
788 | } | ||
789 | |||
790 | 59351 | AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame, | |
791 | enum AVFrameSideDataType type, | ||
792 | AVBufferRef *buf) | ||
793 | { | ||
794 | return | ||
795 | 59351 | add_side_data_from_buf( | |
796 | &frame->side_data, &frame->nb_side_data, type, buf); | ||
797 | } | ||
798 | |||
799 | 23502 | AVFrameSideData *av_frame_new_side_data(AVFrame *frame, | |
800 | enum AVFrameSideDataType type, | ||
801 | size_t size) | ||
802 | { | ||
803 | AVFrameSideData *ret; | ||
804 | 23502 | AVBufferRef *buf = av_buffer_alloc(size); | |
805 | 23502 | ret = av_frame_new_side_data_from_buf(frame, type, buf); | |
806 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23502 times.
|
23502 | if (!ret) |
807 | ✗ | av_buffer_unref(&buf); | |
808 | 23502 | return ret; | |
809 | } | ||
810 | |||
811 | 2 | static AVFrameSideData *replace_side_data_from_buf(AVFrameSideData *dst, | |
812 | AVBufferRef *buf, int flags) | ||
813 | { | ||
814 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE)) |
815 | ✗ | return NULL; | |
816 | |||
817 | 2 | av_dict_free(&dst->metadata); | |
818 | 2 | av_buffer_unref(&dst->buf); | |
819 | 2 | dst->buf = buf; | |
820 | 2 | dst->data = buf->data; | |
821 | 2 | dst->size = buf->size; | |
822 | 2 | return dst; | |
823 | } | ||
824 | |||
825 | 570 | AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd, | |
826 | enum AVFrameSideDataType type, | ||
827 | size_t size, unsigned int flags) | ||
828 | { | ||
829 | 570 | const AVSideDataDescriptor *desc = av_frame_side_data_desc(type); | |
830 | 570 | AVBufferRef *buf = av_buffer_alloc(size); | |
831 | 570 | AVFrameSideData *ret = NULL; | |
832 | |||
833 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 569 times.
|
570 | if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE) |
834 | 1 | remove_side_data(sd, nb_sd, type); | |
835 |
5/6✓ Branch 0 taken 570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 563 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 561 times.
|
1133 | if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) && |
836 | 563 | (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) { | |
837 | 2 | ret = replace_side_data_from_buf(ret, buf, flags); | |
838 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!ret) |
839 | ✗ | av_buffer_unref(&buf); | |
840 | 2 | return ret; | |
841 | } | ||
842 | |||
843 | 568 | ret = add_side_data_from_buf(sd, nb_sd, type, buf); | |
844 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 568 times.
|
568 | if (!ret) |
845 | ✗ | av_buffer_unref(&buf); | |
846 | |||
847 | 568 | return ret; | |
848 | } | ||
849 | |||
850 | 1083 | AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd, | |
851 | enum AVFrameSideDataType type, | ||
852 | AVBufferRef **pbuf, unsigned int flags) | ||
853 | { | ||
854 | 1083 | const AVSideDataDescriptor *desc = av_frame_side_data_desc(type); | |
855 | 1083 | AVFrameSideData *sd_dst = NULL; | |
856 | 1083 | AVBufferRef *buf = *pbuf; | |
857 | |||
858 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
|
1083 | if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE) |
859 | ✗ | remove_side_data(sd, nb_sd, type); | |
860 |
4/6✓ Branch 0 taken 1083 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 785 times.
✓ Branch 3 taken 298 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 785 times.
|
1868 | if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) && |
861 | 785 | (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) { | |
862 | ✗ | sd_dst = replace_side_data_from_buf(sd_dst, buf, flags); | |
863 | ✗ | if (sd_dst) | |
864 | ✗ | *pbuf = NULL; | |
865 | ✗ | return sd_dst; | |
866 | } | ||
867 | |||
868 | 1083 | sd_dst = add_side_data_from_buf(sd, nb_sd, type, buf); | |
869 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
|
1083 | if (!sd_dst) |
870 | ✗ | return NULL; | |
871 | |||
872 | 1083 | *pbuf = NULL; | |
873 | 1083 | return sd_dst; | |
874 | } | ||
875 | |||
876 | 232 | int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd, | |
877 | const AVFrameSideData *src, unsigned int flags) | ||
878 | { | ||
879 | const AVSideDataDescriptor *desc; | ||
880 | 232 | AVBufferRef *buf = NULL; | |
881 | 232 | AVFrameSideData *sd_dst = NULL; | |
882 | 232 | int ret = AVERROR_BUG; | |
883 | |||
884 |
6/10✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 232 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 227 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 5 times.
|
232 | if (!sd || !src || !nb_sd || (*nb_sd && !*sd)) |
885 | ✗ | return AVERROR(EINVAL); | |
886 | |||
887 | 232 | desc = av_frame_side_data_desc(src->type); | |
888 |
1/2✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
|
232 | if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE) |
889 | 232 | remove_side_data(sd, nb_sd, src->type); | |
890 |
3/6✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 232 times.
|
464 | if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) && |
891 | 232 | (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) { | |
892 | ✗ | AVDictionary *dict = NULL; | |
893 | |||
894 | ✗ | if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE)) | |
895 | ✗ | return AVERROR(EEXIST); | |
896 | |||
897 | ✗ | ret = av_dict_copy(&dict, src->metadata, 0); | |
898 | ✗ | if (ret < 0) | |
899 | ✗ | return ret; | |
900 | |||
901 | ✗ | ret = av_buffer_replace(&sd_dst->buf, src->buf); | |
902 | ✗ | if (ret < 0) { | |
903 | ✗ | av_dict_free(&dict); | |
904 | ✗ | return ret; | |
905 | } | ||
906 | |||
907 | ✗ | av_dict_free(&sd_dst->metadata); | |
908 | ✗ | sd_dst->metadata = dict; | |
909 | ✗ | sd_dst->data = src->data; | |
910 | ✗ | sd_dst->size = src->size; | |
911 | ✗ | return 0; | |
912 | } | ||
913 | |||
914 | 232 | buf = av_buffer_ref(src->buf); | |
915 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
|
232 | if (!buf) |
916 | ✗ | return AVERROR(ENOMEM); | |
917 | |||
918 | 232 | sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf, | |
919 | 232 | src->data, src->size); | |
920 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
|
232 | if (!sd_dst) { |
921 | ✗ | av_buffer_unref(&buf); | |
922 | ✗ | return AVERROR(ENOMEM); | |
923 | } | ||
924 | |||
925 | 232 | ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0); | |
926 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
|
232 | if (ret < 0) { |
927 | ✗ | remove_side_data_by_entry(sd, nb_sd, sd_dst); | |
928 | ✗ | return ret; | |
929 | } | ||
930 | |||
931 | 232 | return 0; | |
932 | } | ||
933 | |||
934 | 1158387 | const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd, | |
935 | const int nb_sd, | ||
936 | enum AVFrameSideDataType type) | ||
937 | { | ||
938 |
2/2✓ Branch 0 taken 38368 times.
✓ Branch 1 taken 1156369 times.
|
1194737 | for (int i = 0; i < nb_sd; i++) { |
939 |
2/2✓ Branch 0 taken 2018 times.
✓ Branch 1 taken 36350 times.
|
38368 | if (sd[i]->type == type) |
940 | 2018 | return sd[i]; | |
941 | } | ||
942 | 1156369 | return NULL; | |
943 | } | ||
944 | |||
945 | 10 | void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd, | |
946 | enum AVFrameSideDataType type) | ||
947 | { | ||
948 | 10 | remove_side_data(sd, nb_sd, type); | |
949 | 10 | } | |
950 | |||
951 | 962906 | AVFrameSideData *av_frame_get_side_data(const AVFrame *frame, | |
952 | enum AVFrameSideDataType type) | ||
953 | { | ||
954 | 1925812 | return (AVFrameSideData *)av_frame_side_data_get( | |
955 | 962906 | frame->side_data, frame->nb_side_data, | |
956 | type | ||
957 | ); | ||
958 | } | ||
959 | |||
960 | 10013 | static int frame_copy_video(AVFrame *dst, const AVFrame *src) | |
961 | { | ||
962 | int planes; | ||
963 | |||
964 |
1/2✓ Branch 0 taken 10013 times.
✗ Branch 1 not taken.
|
10013 | if (dst->width < src->width || |
965 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10013 times.
|
10013 | dst->height < src->height) |
966 | ✗ | return AVERROR(EINVAL); | |
967 | |||
968 |
2/4✓ Branch 0 taken 10013 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10013 times.
|
10013 | if (src->hw_frames_ctx || dst->hw_frames_ctx) |
969 | ✗ | return av_hwframe_transfer_data(dst, src, 0); | |
970 | |||
971 | 10013 | planes = av_pix_fmt_count_planes(dst->format); | |
972 |
2/2✓ Branch 0 taken 16176 times.
✓ Branch 1 taken 10013 times.
|
26189 | for (int i = 0; i < planes; i++) |
973 |
2/4✓ Branch 0 taken 16176 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16176 times.
|
16176 | if (!dst->data[i] || !src->data[i]) |
974 | ✗ | return AVERROR(EINVAL); | |
975 | |||
976 | 10013 | av_image_copy2(dst->data, dst->linesize, | |
977 | 10013 | src->data, src->linesize, | |
978 | 10013 | dst->format, src->width, src->height); | |
979 | |||
980 | 10013 | return 0; | |
981 | } | ||
982 | |||
983 | ✗ | static int frame_copy_audio(AVFrame *dst, const AVFrame *src) | |
984 | { | ||
985 | ✗ | int planar = av_sample_fmt_is_planar(dst->format); | |
986 | ✗ | int channels = dst->ch_layout.nb_channels; | |
987 | ✗ | int planes = planar ? channels : 1; | |
988 | |||
989 | ✗ | if (dst->nb_samples != src->nb_samples || | |
990 | ✗ | av_channel_layout_compare(&dst->ch_layout, &src->ch_layout)) | |
991 | ✗ | return AVERROR(EINVAL); | |
992 | |||
993 | ✗ | for (int i = 0; i < planes; i++) | |
994 | ✗ | if (!dst->extended_data[i] || !src->extended_data[i]) | |
995 | ✗ | return AVERROR(EINVAL); | |
996 | |||
997 | ✗ | av_samples_copy(dst->extended_data, src->extended_data, 0, 0, | |
998 | ✗ | dst->nb_samples, channels, dst->format); | |
999 | |||
1000 | ✗ | return 0; | |
1001 | } | ||
1002 | |||
1003 | 10013 | int av_frame_copy(AVFrame *dst, const AVFrame *src) | |
1004 | { | ||
1005 |
2/4✓ Branch 0 taken 10013 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10013 times.
|
10013 | if (dst->format != src->format || dst->format < 0) |
1006 | ✗ | return AVERROR(EINVAL); | |
1007 | |||
1008 |
2/4✓ Branch 0 taken 10013 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10013 times.
✗ Branch 3 not taken.
|
10013 | if (dst->width > 0 && dst->height > 0) |
1009 | 10013 | return frame_copy_video(dst, src); | |
1010 | ✗ | else if (dst->nb_samples > 0 && | |
1011 | ✗ | (av_channel_layout_check(&dst->ch_layout))) | |
1012 | ✗ | return frame_copy_audio(dst, src); | |
1013 | |||
1014 | ✗ | return AVERROR(EINVAL); | |
1015 | } | ||
1016 | |||
1017 | 406759 | void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type) | |
1018 | { | ||
1019 | 406759 | remove_side_data(&frame->side_data, &frame->nb_side_data, type); | |
1020 | 406759 | } | |
1021 | |||
1022 | 2880 | const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type) | |
1023 | { | ||
1024 | 2880 | unsigned t = type; | |
1025 |
2/4✓ Branch 0 taken 2880 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2880 times.
✗ Branch 3 not taken.
|
2880 | if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name) |
1026 | 2880 | return &sd_props[t]; | |
1027 | ✗ | return NULL; | |
1028 | } | ||
1029 | |||
1030 | 483 | const char *av_frame_side_data_name(enum AVFrameSideDataType type) | |
1031 | { | ||
1032 | 483 | const AVSideDataDescriptor *desc = av_frame_side_data_desc(type); | |
1033 |
1/2✓ Branch 0 taken 483 times.
✗ Branch 1 not taken.
|
483 | return desc ? desc->name : NULL; |
1034 | } | ||
1035 | |||
1036 | 144777 | static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame, | |
1037 | const AVPixFmtDescriptor *desc) | ||
1038 | { | ||
1039 |
2/2✓ Branch 0 taken 408147 times.
✓ Branch 1 taken 140035 times.
|
548182 | for (int i = 0; frame->data[i]; i++) { |
1040 | 408147 | const AVComponentDescriptor *comp = NULL; | |
1041 |
4/4✓ Branch 0 taken 274435 times.
✓ Branch 1 taken 133712 times.
✓ Branch 2 taken 128967 times.
✓ Branch 3 taken 145468 times.
|
408147 | int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0; |
1042 |
4/4✓ Branch 0 taken 274435 times.
✓ Branch 1 taken 133712 times.
✓ Branch 2 taken 128967 times.
✓ Branch 3 taken 145468 times.
|
408147 | int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0; |
1043 | |||
1044 |
4/4✓ Branch 0 taken 9484 times.
✓ Branch 1 taken 398663 times.
✓ Branch 2 taken 4742 times.
✓ Branch 3 taken 4742 times.
|
408147 | if (desc->flags & AV_PIX_FMT_FLAG_PAL && i == 1) { |
1045 | 4742 | offsets[i] = 0; | |
1046 | 4742 | break; | |
1047 | } | ||
1048 | |||
1049 | /* find any component descriptor for this plane */ | ||
1050 |
1/2✓ Branch 0 taken 792382 times.
✗ Branch 1 not taken.
|
792382 | for (int j = 0; j < desc->nb_components; j++) { |
1051 |
2/2✓ Branch 0 taken 403405 times.
✓ Branch 1 taken 388977 times.
|
792382 | if (desc->comp[j].plane == i) { |
1052 | 403405 | comp = &desc->comp[j]; | |
1053 | 403405 | break; | |
1054 | } | ||
1055 | } | ||
1056 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 403405 times.
|
403405 | if (!comp) |
1057 | ✗ | return AVERROR_BUG; | |
1058 | |||
1059 | 403405 | offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] + | |
1060 | 403405 | (frame->crop_left >> shift_x) * comp->step; | |
1061 | } | ||
1062 | |||
1063 | 144777 | return 0; | |
1064 | } | ||
1065 | |||
1066 | 145081 | int av_frame_apply_cropping(AVFrame *frame, int flags) | |
1067 | { | ||
1068 | const AVPixFmtDescriptor *desc; | ||
1069 | size_t offsets[4]; | ||
1070 | |||
1071 |
2/4✓ Branch 0 taken 145081 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 145081 times.
|
145081 | if (!(frame->width > 0 && frame->height > 0)) |
1072 | ✗ | return AVERROR(EINVAL); | |
1073 | |||
1074 |
1/2✓ Branch 0 taken 145081 times.
✗ Branch 1 not taken.
|
145081 | if (frame->crop_left >= INT_MAX - frame->crop_right || |
1075 |
1/2✓ Branch 0 taken 145081 times.
✗ Branch 1 not taken.
|
145081 | frame->crop_top >= INT_MAX - frame->crop_bottom || |
1076 |
1/2✓ Branch 0 taken 145081 times.
✗ Branch 1 not taken.
|
145081 | (frame->crop_left + frame->crop_right) >= frame->width || |
1077 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 145081 times.
|
145081 | (frame->crop_top + frame->crop_bottom) >= frame->height) |
1078 | ✗ | return AVERROR(ERANGE); | |
1079 | |||
1080 | 145081 | desc = av_pix_fmt_desc_get(frame->format); | |
1081 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 145081 times.
|
145081 | if (!desc) |
1082 | ✗ | return AVERROR_BUG; | |
1083 | |||
1084 | /* Apply just the right/bottom cropping for hwaccel formats. Bitstream | ||
1085 | * formats cannot be easily handled here either (and corresponding decoders | ||
1086 | * should not export any cropping anyway), so do the same for those as well. | ||
1087 | * */ | ||
1088 |
2/2✓ Branch 0 taken 318 times.
✓ Branch 1 taken 144763 times.
|
145081 | if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) { |
1089 | 318 | frame->width -= frame->crop_right; | |
1090 | 318 | frame->height -= frame->crop_bottom; | |
1091 | 318 | frame->crop_right = 0; | |
1092 | 318 | frame->crop_bottom = 0; | |
1093 | 318 | return 0; | |
1094 | } | ||
1095 | |||
1096 | /* calculate the offsets for each plane */ | ||
1097 | 144763 | calc_cropping_offsets(offsets, frame, desc); | |
1098 | |||
1099 | /* adjust the offsets to avoid breaking alignment */ | ||
1100 |
2/2✓ Branch 0 taken 7335 times.
✓ Branch 1 taken 137428 times.
|
144763 | if (!(flags & AV_FRAME_CROP_UNALIGNED)) { |
1101 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7321 times.
|
7335 | int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX; |
1102 | 7335 | int min_log2_align = INT_MAX; | |
1103 | |||
1104 |
2/2✓ Branch 0 taken 21523 times.
✓ Branch 1 taken 7335 times.
|
28858 | for (int i = 0; frame->data[i]; i++) { |
1105 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21481 times.
|
21523 | int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX; |
1106 | 21523 | min_log2_align = FFMIN(log2_align, min_log2_align); | |
1107 | } | ||
1108 | |||
1109 | /* we assume, and it should always be true, that the data alignment is | ||
1110 | * related to the cropping alignment by a constant power-of-2 factor */ | ||
1111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7335 times.
|
7335 | if (log2_crop_align < min_log2_align) |
1112 | ✗ | return AVERROR_BUG; | |
1113 | |||
1114 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7321 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
7335 | if (min_log2_align < 5 && log2_crop_align != INT_MAX) { |
1115 | 14 | frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1); | |
1116 | 14 | calc_cropping_offsets(offsets, frame, desc); | |
1117 | } | ||
1118 | } | ||
1119 | |||
1120 |
2/2✓ Branch 0 taken 408105 times.
✓ Branch 1 taken 144763 times.
|
552868 | for (int i = 0; frame->data[i]; i++) |
1121 | 408105 | frame->data[i] += offsets[i]; | |
1122 | |||
1123 | 144763 | frame->width -= (frame->crop_left + frame->crop_right); | |
1124 | 144763 | frame->height -= (frame->crop_top + frame->crop_bottom); | |
1125 | 144763 | frame->crop_left = 0; | |
1126 | 144763 | frame->crop_right = 0; | |
1127 | 144763 | frame->crop_top = 0; | |
1128 | 144763 | frame->crop_bottom = 0; | |
1129 | |||
1130 | 144763 | return 0; | |
1131 | } | ||
1132 |