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