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