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