| 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 | 12704470 | static void get_frame_defaults(AVFrame *frame) | |
| 32 | { | ||
| 33 | 12704470 | memset(frame, 0, sizeof(*frame)); | |
| 34 | |||
| 35 | 12704470 | frame->pts = | |
| 36 | 12704470 | frame->pkt_dts = AV_NOPTS_VALUE; | |
| 37 | 12704470 | frame->best_effort_timestamp = AV_NOPTS_VALUE; | |
| 38 | 12704470 | frame->duration = 0; | |
| 39 | 12704470 | frame->time_base = (AVRational){ 0, 1 }; | |
| 40 | 12704470 | frame->sample_aspect_ratio = (AVRational){ 0, 1 }; | |
| 41 | 12704470 | frame->format = -1; /* unknown */ | |
| 42 | 12704470 | frame->extended_data = frame->data; | |
| 43 | 12704470 | frame->color_primaries = AVCOL_PRI_UNSPECIFIED; | |
| 44 | 12704470 | frame->color_trc = AVCOL_TRC_UNSPECIFIED; | |
| 45 | 12704470 | frame->colorspace = AVCOL_SPC_UNSPECIFIED; | |
| 46 | 12704470 | frame->color_range = AVCOL_RANGE_UNSPECIFIED; | |
| 47 | 12704470 | frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED; | |
| 48 | 12704470 | frame->alpha_mode = AVALPHA_MODE_UNSPECIFIED; | |
| 49 | 12704470 | frame->flags = 0; | |
| 50 | 12704470 | } | |
| 51 | |||
| 52 | 1464755 | AVFrame *av_frame_alloc(void) | |
| 53 | { | ||
| 54 | 1464755 | AVFrame *frame = av_malloc(sizeof(*frame)); | |
| 55 | |||
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1464755 times.
|
1464755 | if (!frame) |
| 57 | ✗ | return NULL; | |
| 58 | |||
| 59 | 1464755 | get_frame_defaults(frame); | |
| 60 | |||
| 61 | 1464755 | return frame; | |
| 62 | } | ||
| 63 | |||
| 64 | 1780724 | void av_frame_free(AVFrame **frame) | |
| 65 | { | ||
| 66 |
3/4✓ Branch 0 taken 1780724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 315395 times.
✓ Branch 3 taken 1465329 times.
|
1780724 | if (!frame || !*frame) |
| 67 | 315395 | return; | |
| 68 | |||
| 69 | 1465329 | av_frame_unref(*frame); | |
| 70 | 1465329 | av_freep(frame); | |
| 71 | } | ||
| 72 | |||
| 73 | #define ALIGN (HAVE_SIMD_ALIGN_64 ? 64 : 32) | ||
| 74 | |||
| 75 | 50573 | static int get_video_buffer(AVFrame *frame, int align) | |
| 76 | { | ||
| 77 | 50573 | 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 50573 times.
|
50573 | if (!desc) |
| 84 | ✗ | return AVERROR(EINVAL); | |
| 85 | |||
| 86 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50573 times.
|
50573 | if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0) |
| 87 | ✗ | return ret; | |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 50548 times.
✓ Branch 1 taken 25 times.
|
50573 | if (align <= 0) |
| 90 | 50548 | align = ALIGN; | |
| 91 | 50573 | plane_padding = FFMAX(ALIGN, align); | |
| 92 | |||
| 93 |
1/2✓ Branch 0 taken 50573 times.
✗ Branch 1 not taken.
|
50573 | if (!frame->linesize[0]) { |
| 94 |
2/2✓ Branch 0 taken 177000 times.
✓ Branch 1 taken 518 times.
|
177518 | for (int i = 1; i <= align; i += i) { |
| 95 | 177000 | ret = av_image_fill_linesizes(frame->linesize, frame->format, | |
| 96 | 177000 | FFALIGN(frame->width, i)); | |
| 97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 177000 times.
|
177000 | if (ret < 0) |
| 98 | ✗ | return ret; | |
| 99 |
2/2✓ Branch 0 taken 50055 times.
✓ Branch 1 taken 126945 times.
|
177000 | if (!(frame->linesize[0] & (align-1))) |
| 100 | 50055 | break; | |
| 101 | } | ||
| 102 | |||
| 103 |
4/4✓ Branch 0 taken 163916 times.
✓ Branch 1 taken 21821 times.
✓ Branch 2 taken 135164 times.
✓ Branch 3 taken 28752 times.
|
185737 | for (int i = 0; i < 4 && frame->linesize[i]; i++) |
| 104 | 135164 | frame->linesize[i] = FFALIGN(frame->linesize[i], align); | |
| 105 | } | ||
| 106 | |||
| 107 |
2/2✓ Branch 0 taken 202292 times.
✓ Branch 1 taken 50573 times.
|
252865 | for (int i = 0; i < 4; i++) |
| 108 | 202292 | linesizes[i] = frame->linesize[i]; | |
| 109 | |||
| 110 | 50573 | padded_height = FFALIGN(frame->height, 32); | |
| 111 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50573 times.
|
50573 | if ((ret = av_image_fill_plane_sizes(sizes, frame->format, |
| 112 | padded_height, linesizes)) < 0) | ||
| 113 | ✗ | return ret; | |
| 114 | |||
| 115 | 50573 | total_size = 4 * plane_padding + 4 * align; | |
| 116 |
2/2✓ Branch 0 taken 202292 times.
✓ Branch 1 taken 50573 times.
|
252865 | for (int i = 0; i < 4; i++) { |
| 117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 202292 times.
|
202292 | if (sizes[i] > SIZE_MAX - total_size) |
| 118 | ✗ | return AVERROR(EINVAL); | |
| 119 | 202292 | total_size += sizes[i]; | |
| 120 | } | ||
| 121 | |||
| 122 | 50573 | frame->buf[0] = av_buffer_alloc(total_size); | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50573 times.
|
50573 | if (!frame->buf[0]) { |
| 124 | ✗ | ret = AVERROR(ENOMEM); | |
| 125 | ✗ | goto fail; | |
| 126 | } | ||
| 127 | |||
| 128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50573 times.
|
50573 | if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height, |
| 129 | 50573 | frame->buf[0]->data, frame->linesize)) < 0) | |
| 130 | ✗ | goto fail; | |
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 151719 times.
✓ Branch 1 taken 50573 times.
|
202292 | for (int i = 1; i < 4; i++) { |
| 133 |
2/2✓ Branch 0 taken 84591 times.
✓ Branch 1 taken 67128 times.
|
151719 | if (frame->data[i]) |
| 134 | 84591 | frame->data[i] += i * plane_padding; | |
| 135 | 151719 | frame->data[i] = (uint8_t *)FFALIGN((uintptr_t)frame->data[i], align); | |
| 136 | } | ||
| 137 | |||
| 138 | 50573 | frame->extended_data = frame->data; | |
| 139 | |||
| 140 | 50573 | return 0; | |
| 141 | ✗ | fail: | |
| 142 | ✗ | av_frame_unref(frame); | |
| 143 | ✗ | return ret; | |
| 144 | } | ||
| 145 | |||
| 146 | 14442 | static int get_audio_buffer(AVFrame *frame, int align) | |
| 147 | { | ||
| 148 | 14442 | int planar = av_sample_fmt_is_planar(frame->format); | |
| 149 | int channels, planes; | ||
| 150 | size_t size; | ||
| 151 | int ret; | ||
| 152 | |||
| 153 | 14442 | channels = frame->ch_layout.nb_channels; | |
| 154 |
2/2✓ Branch 0 taken 1159 times.
✓ Branch 1 taken 13283 times.
|
14442 | planes = planar ? channels : 1; |
| 155 |
1/2✓ Branch 0 taken 14442 times.
✗ Branch 1 not taken.
|
14442 | if (!frame->linesize[0]) { |
| 156 | 14442 | ret = av_samples_get_buffer_size(&frame->linesize[0], channels, | |
| 157 | 14442 | frame->nb_samples, frame->format, | |
| 158 | align); | ||
| 159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14442 times.
|
14442 | if (ret < 0) |
| 160 | ✗ | return ret; | |
| 161 | } | ||
| 162 | |||
| 163 |
2/2✓ Branch 0 taken 14413 times.
✓ Branch 1 taken 29 times.
|
14442 | if (align <= 0) |
| 164 | 14413 | align = ALIGN; | |
| 165 | |||
| 166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14442 times.
|
14442 | 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 | 14442 | frame->extended_data = frame->data; | |
| 179 | |||
| 180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14442 times.
|
14442 | if (frame->linesize[0] > SIZE_MAX - align) |
| 181 | ✗ | return AVERROR(EINVAL); | |
| 182 | 14442 | size = frame->linesize[0] + (size_t)align; | |
| 183 | |||
| 184 |
2/2✓ Branch 0 taken 15247 times.
✓ Branch 1 taken 14442 times.
|
29689 | for (int i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) { |
| 185 | 15247 | frame->buf[i] = av_buffer_alloc(size); | |
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15247 times.
|
15247 | if (!frame->buf[i]) { |
| 187 | ✗ | av_frame_unref(frame); | |
| 188 | ✗ | return AVERROR(ENOMEM); | |
| 189 | } | ||
| 190 | 15247 | frame->extended_data[i] = frame->data[i] = | |
| 191 | 15247 | (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, align); | |
| 192 | } | ||
| 193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14442 times.
|
14442 | 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 | 14442 | return 0; | |
| 203 | |||
| 204 | } | ||
| 205 | |||
| 206 | 65015 | int av_frame_get_buffer(AVFrame *frame, int align) | |
| 207 | { | ||
| 208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65015 times.
|
65015 | if (frame->format < 0) |
| 209 | ✗ | return AVERROR(EINVAL); | |
| 210 | |||
| 211 |
3/4✓ Branch 0 taken 50573 times.
✓ Branch 1 taken 14442 times.
✓ Branch 2 taken 50573 times.
✗ Branch 3 not taken.
|
65015 | if (frame->width > 0 && frame->height > 0) |
| 212 | 50573 | return get_video_buffer(frame, align); | |
| 213 |
2/4✓ Branch 0 taken 14442 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14442 times.
✗ Branch 3 not taken.
|
28884 | else if (frame->nb_samples > 0 && |
| 214 | 14442 | (av_channel_layout_check(&frame->ch_layout))) | |
| 215 | 14442 | return get_audio_buffer(frame, align); | |
| 216 | |||
| 217 | ✗ | return AVERROR(EINVAL); | |
| 218 | } | ||
| 219 | |||
| 220 | 1303160 | static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy) | |
| 221 | { | ||
| 222 | 1303160 | dst->pict_type = src->pict_type; | |
| 223 | 1303160 | dst->sample_aspect_ratio = src->sample_aspect_ratio; | |
| 224 | 1303160 | dst->crop_top = src->crop_top; | |
| 225 | 1303160 | dst->crop_bottom = src->crop_bottom; | |
| 226 | 1303160 | dst->crop_left = src->crop_left; | |
| 227 | 1303160 | dst->crop_right = src->crop_right; | |
| 228 | 1303160 | dst->pts = src->pts; | |
| 229 | 1303160 | dst->duration = src->duration; | |
| 230 | 1303160 | dst->repeat_pict = src->repeat_pict; | |
| 231 | 1303160 | dst->sample_rate = src->sample_rate; | |
| 232 | 1303160 | dst->opaque = src->opaque; | |
| 233 | 1303160 | dst->pkt_dts = src->pkt_dts; | |
| 234 | 1303160 | dst->time_base = src->time_base; | |
| 235 | 1303160 | dst->quality = src->quality; | |
| 236 | 1303160 | dst->best_effort_timestamp = src->best_effort_timestamp; | |
| 237 | 1303160 | dst->flags = src->flags; | |
| 238 | 1303160 | dst->decode_error_flags = src->decode_error_flags; | |
| 239 | 1303160 | dst->color_primaries = src->color_primaries; | |
| 240 | 1303160 | dst->color_trc = src->color_trc; | |
| 241 | 1303160 | dst->colorspace = src->colorspace; | |
| 242 | 1303160 | dst->color_range = src->color_range; | |
| 243 | 1303160 | dst->chroma_location = src->chroma_location; | |
| 244 | 1303160 | dst->alpha_mode = src->alpha_mode; | |
| 245 | |||
| 246 | 1303160 | av_dict_copy(&dst->metadata, src->metadata, 0); | |
| 247 | |||
| 248 |
2/2✓ Branch 0 taken 38316 times.
✓ Branch 1 taken 1303160 times.
|
1341476 | for (int i = 0; i < src->nb_side_data; i++) { |
| 249 | 38316 | const AVFrameSideData *sd_src = src->side_data[i]; | |
| 250 | AVFrameSideData *sd_dst; | ||
| 251 |
2/2✓ Branch 0 taken 14016 times.
✓ Branch 1 taken 24300 times.
|
38316 | if ( sd_src->type == AV_FRAME_DATA_PANSCAN |
| 252 |
2/4✓ Branch 0 taken 14016 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14016 times.
|
14016 | && (src->width != dst->width || src->height != dst->height)) |
| 253 | ✗ | continue; | |
| 254 |
2/2✓ Branch 0 taken 4771 times.
✓ Branch 1 taken 33545 times.
|
38316 | if (force_copy) { |
| 255 | 4771 | sd_dst = av_frame_new_side_data(dst, sd_src->type, | |
| 256 | 4771 | sd_src->size); | |
| 257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4771 times.
|
4771 | if (!sd_dst) { |
| 258 | ✗ | av_frame_side_data_free(&dst->side_data, &dst->nb_side_data); | |
| 259 | ✗ | return AVERROR(ENOMEM); | |
| 260 | } | ||
| 261 | 4771 | memcpy(sd_dst->data, sd_src->data, sd_src->size); | |
| 262 | } else { | ||
| 263 | 33545 | AVBufferRef *ref = av_buffer_ref(sd_src->buf); | |
| 264 | 33545 | sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref); | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33545 times.
|
33545 | 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 | 38316 | av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0); | |
| 272 | } | ||
| 273 | |||
| 274 | 1303160 | av_refstruct_replace(&dst->private_ref, src->private_ref); | |
| 275 | 1303160 | return av_buffer_replace(&dst->opaque_ref, src->opaque_ref); | |
| 276 | } | ||
| 277 | |||
| 278 | 864825 | int av_frame_ref(AVFrame *dst, const AVFrame *src) | |
| 279 | { | ||
| 280 | 864825 | 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 | 864825 | dst->format = src->format; | |
| 287 | 864825 | dst->width = src->width; | |
| 288 | 864825 | dst->height = src->height; | |
| 289 | 864825 | dst->nb_samples = src->nb_samples; | |
| 290 | |||
| 291 | 864825 | ret = frame_copy_props(dst, src, 0); | |
| 292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 864825 times.
|
864825 | if (ret < 0) |
| 293 | ✗ | goto fail; | |
| 294 | |||
| 295 | 864825 | ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout); | |
| 296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 864825 times.
|
864825 | 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 864825 times.
|
864825 | 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 6918600 times.
✓ Branch 1 taken 864825 times.
|
7783425 | for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) { |
| 314 |
2/2✓ Branch 0 taken 5273189 times.
✓ Branch 1 taken 1645411 times.
|
6918600 | if (!src->buf[i]) |
| 315 | 5273189 | continue; | |
| 316 | 1645411 | dst->buf[i] = av_buffer_ref(src->buf[i]); | |
| 317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1645411 times.
|
1645411 | 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 864759 times.
|
864825 | 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 864825 times.
|
864825 | 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 864759 times.
|
864825 | 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 | 864759 | dst->extended_data = dst->data; | |
| 365 | |||
| 366 | 864825 | memcpy(dst->data, src->data, sizeof(src->data)); | |
| 367 | 864825 | memcpy(dst->linesize, src->linesize, sizeof(src->linesize)); | |
| 368 | |||
| 369 | 864825 | return 0; | |
| 370 | |||
| 371 | ✗ | fail: | |
| 372 | ✗ | av_frame_unref(dst); | |
| 373 | ✗ | return ret; | |
| 374 | } | ||
| 375 | |||
| 376 | 4379 | int av_frame_replace(AVFrame *dst, const AVFrame *src) | |
| 377 | { | ||
| 378 | 4379 | int ret = 0; | |
| 379 | |||
| 380 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4379 times.
|
4379 | if (dst == src) |
| 381 | ✗ | return AVERROR(EINVAL); | |
| 382 | |||
| 383 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4379 times.
|
4379 | 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 | 4379 | dst->format = src->format; | |
| 397 | 4379 | dst->width = src->width; | |
| 398 | 4379 | dst->height = src->height; | |
| 399 | 4379 | dst->nb_samples = src->nb_samples; | |
| 400 | |||
| 401 | 4379 | ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout); | |
| 402 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4379 times.
|
4379 | if (ret < 0) |
| 403 | ✗ | goto fail; | |
| 404 | |||
| 405 | 4379 | av_frame_side_data_free(&dst->side_data, &dst->nb_side_data); | |
| 406 | 4379 | av_dict_free(&dst->metadata); | |
| 407 | 4379 | ret = frame_copy_props(dst, src, 0); | |
| 408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4379 times.
|
4379 | if (ret < 0) |
| 409 | ✗ | goto fail; | |
| 410 | |||
| 411 | /* replace the buffers */ | ||
| 412 |
2/2✓ Branch 0 taken 35032 times.
✓ Branch 1 taken 4379 times.
|
39411 | for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) { |
| 413 | 35032 | ret = av_buffer_replace(&dst->buf[i], src->buf[i]); | |
| 414 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35032 times.
|
35032 | if (ret < 0) |
| 415 | ✗ | goto fail; | |
| 416 | } | ||
| 417 | |||
| 418 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4379 times.
|
4379 | 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 4379 times.
|
4379 | } 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 | 4379 | ret = av_buffer_replace(&dst->hw_frames_ctx, src->hw_frames_ctx); | |
| 451 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4379 times.
|
4379 | if (ret < 0) |
| 452 | ✗ | goto fail; | |
| 453 | |||
| 454 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4379 times.
|
4379 | if (dst->extended_data != dst->data) |
| 455 | ✗ | av_freep(&dst->extended_data); | |
| 456 | |||
| 457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4379 times.
|
4379 | 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 | 4379 | dst->extended_data = dst->data; | |
| 472 | |||
| 473 | 4379 | memcpy(dst->data, src->data, sizeof(src->data)); | |
| 474 | 4379 | memcpy(dst->linesize, src->linesize, sizeof(src->linesize)); | |
| 475 | |||
| 476 | 4379 | return 0; | |
| 477 | |||
| 478 | ✗ | fail: | |
| 479 | ✗ | av_frame_unref(dst); | |
| 480 | ✗ | return ret; | |
| 481 | } | ||
| 482 | |||
| 483 | 50332 | AVFrame *av_frame_clone(const AVFrame *src) | |
| 484 | { | ||
| 485 | 50332 | AVFrame *ret = av_frame_alloc(); | |
| 486 | |||
| 487 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50332 times.
|
50332 | if (!ret) |
| 488 | ✗ | return NULL; | |
| 489 | |||
| 490 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50332 times.
|
50332 | if (av_frame_ref(ret, src) < 0) |
| 491 | ✗ | av_frame_free(&ret); | |
| 492 | |||
| 493 | 50332 | return ret; | |
| 494 | } | ||
| 495 | |||
| 496 | 7666857 | void av_frame_unref(AVFrame *frame) | |
| 497 | { | ||
| 498 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7666851 times.
|
7666857 | if (!frame) |
| 499 | 6 | return; | |
| 500 | |||
| 501 | 7666851 | av_frame_side_data_free(&frame->side_data, &frame->nb_side_data); | |
| 502 | |||
| 503 |
2/2✓ Branch 0 taken 61334808 times.
✓ Branch 1 taken 7666851 times.
|
69001659 | for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++) |
| 504 | 61334808 | av_buffer_unref(&frame->buf[i]); | |
| 505 |
2/2✓ Branch 0 taken 5720 times.
✓ Branch 1 taken 7666851 times.
|
7672571 | for (int i = 0; i < frame->nb_extended_buf; i++) |
| 506 | 5720 | av_buffer_unref(&frame->extended_buf[i]); | |
| 507 | 7666851 | av_freep(&frame->extended_buf); | |
| 508 | 7666851 | av_dict_free(&frame->metadata); | |
| 509 | |||
| 510 | 7666851 | av_buffer_unref(&frame->hw_frames_ctx); | |
| 511 | |||
| 512 | 7666851 | av_buffer_unref(&frame->opaque_ref); | |
| 513 | 7666851 | av_refstruct_unref(&frame->private_ref); | |
| 514 | |||
| 515 |
2/2✓ Branch 0 taken 584 times.
✓ Branch 1 taken 7666267 times.
|
7666851 | if (frame->extended_data != frame->data) |
| 516 | 584 | av_freep(&frame->extended_data); | |
| 517 | |||
| 518 | 7666851 | av_channel_layout_uninit(&frame->ch_layout); | |
| 519 | |||
| 520 | 7666851 | get_frame_defaults(frame); | |
| 521 | } | ||
| 522 | |||
| 523 | 3572864 | 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 | 3572864 | *dst = *src; | |
| 530 |
1/2✓ Branch 0 taken 3572864 times.
✗ Branch 1 not taken.
|
3572864 | if (src->extended_data == src->data) |
| 531 | 3572864 | dst->extended_data = dst->data; | |
| 532 | 3572864 | get_frame_defaults(src); | |
| 533 | 3572864 | } | |
| 534 | |||
| 535 | 18502 | int av_frame_is_writable(AVFrame *frame) | |
| 536 | { | ||
| 537 | 18502 | int ret = 1; | |
| 538 | |||
| 539 | /* assume non-refcounted frames are not writable */ | ||
| 540 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18502 times.
|
18502 | if (!frame->buf[0]) |
| 541 | ✗ | return 0; | |
| 542 | |||
| 543 |
2/2✓ Branch 0 taken 148016 times.
✓ Branch 1 taken 18502 times.
|
166518 | for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++) |
| 544 |
2/2✓ Branch 0 taken 37663 times.
✓ Branch 1 taken 110353 times.
|
148016 | if (frame->buf[i]) |
| 545 | 37663 | ret &= !!av_buffer_is_writable(frame->buf[i]); | |
| 546 |
2/2✓ Branch 0 taken 1834 times.
✓ Branch 1 taken 18502 times.
|
20336 | for (int i = 0; i < frame->nb_extended_buf; i++) |
| 547 | 1834 | ret &= !!av_buffer_is_writable(frame->extended_buf[i]); | |
| 548 | |||
| 549 | 18502 | 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 | 433956 | int av_frame_copy_props(AVFrame *dst, const AVFrame *src) | |
| 600 | { | ||
| 601 | 433956 | 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 12920 times.
✓ Branch 1 taken 1897 times.
✓ Branch 2 taken 6385 times.
✓ Branch 3 taken 6535 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 | 50325 | AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame, | |
| 639 | enum AVFrameSideDataType type, | ||
| 640 | AVBufferRef *buf) | ||
| 641 | { | ||
| 642 | return | ||
| 643 | 50325 | ff_frame_side_data_add_from_buf( | |
| 644 | &frame->side_data, &frame->nb_side_data, type, buf); | ||
| 645 | } | ||
| 646 | |||
| 647 | 16750 | AVFrameSideData *av_frame_new_side_data(AVFrame *frame, | |
| 648 | enum AVFrameSideDataType type, | ||
| 649 | size_t size) | ||
| 650 | { | ||
| 651 | AVFrameSideData *ret; | ||
| 652 | 16750 | AVBufferRef *buf = av_buffer_alloc(size); | |
| 653 | 16750 | ret = av_frame_new_side_data_from_buf(frame, type, buf); | |
| 654 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16750 times.
|
16750 | if (!ret) |
| 655 | ✗ | av_buffer_unref(&buf); | |
| 656 | 16750 | return ret; | |
| 657 | } | ||
| 658 | |||
| 659 | 2507864 | AVFrameSideData *av_frame_get_side_data(const AVFrame *frame, | |
| 660 | enum AVFrameSideDataType type) | ||
| 661 | { | ||
| 662 | 5015728 | return (AVFrameSideData *)av_frame_side_data_get( | |
| 663 | 2507864 | frame->side_data, frame->nb_side_data, | |
| 664 | type | ||
| 665 | ); | ||
| 666 | } | ||
| 667 | |||
| 668 | 9828 | static int frame_copy_video(AVFrame *dst, const AVFrame *src) | |
| 669 | { | ||
| 670 | int planes; | ||
| 671 | |||
| 672 |
1/2✓ Branch 0 taken 9828 times.
✗ Branch 1 not taken.
|
9828 | if (dst->width < src->width || |
| 673 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9828 times.
|
9828 | dst->height < src->height) |
| 674 | ✗ | return AVERROR(EINVAL); | |
| 675 | |||
| 676 |
2/4✓ Branch 0 taken 9828 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9828 times.
|
9828 | if (src->hw_frames_ctx || dst->hw_frames_ctx) |
| 677 | ✗ | return av_hwframe_transfer_data(dst, src, 0); | |
| 678 | |||
| 679 | 9828 | planes = av_pix_fmt_count_planes(dst->format); | |
| 680 |
2/2✓ Branch 0 taken 16128 times.
✓ Branch 1 taken 9828 times.
|
25956 | for (int i = 0; i < planes; i++) |
| 681 |
2/4✓ Branch 0 taken 16128 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16128 times.
|
16128 | if (!dst->data[i] || !src->data[i]) |
| 682 | ✗ | return AVERROR(EINVAL); | |
| 683 | |||
| 684 | 9828 | av_image_copy2(dst->data, dst->linesize, | |
| 685 | 9828 | src->data, src->linesize, | |
| 686 | 9828 | dst->format, src->width, src->height); | |
| 687 | |||
| 688 | 9828 | 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 | 9828 | int av_frame_copy(AVFrame *dst, const AVFrame *src) | |
| 712 | { | ||
| 713 |
2/4✓ Branch 0 taken 9828 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9828 times.
|
9828 | if (dst->format != src->format || dst->format < 0) |
| 714 | ✗ | return AVERROR(EINVAL); | |
| 715 | |||
| 716 |
2/4✓ Branch 0 taken 9828 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9828 times.
✗ Branch 3 not taken.
|
9828 | if (dst->width > 0 && dst->height > 0) |
| 717 | 9828 | 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 | 419551 | void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type) | |
| 726 | { | ||
| 727 | 419551 | av_frame_side_data_remove(&frame->side_data, &frame->nb_side_data, type); | |
| 728 | 419551 | } | |
| 729 | |||
| 730 | 133149 | static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame, | |
| 731 | const AVPixFmtDescriptor *desc) | ||
| 732 | { | ||
| 733 |
2/2✓ Branch 0 taken 373363 times.
✓ Branch 1 taken 128448 times.
|
501811 | for (int i = 0; frame->data[i]; i++) { |
| 734 | 373363 | const AVComponentDescriptor *comp = NULL; | |
| 735 |
4/4✓ Branch 0 taken 251273 times.
✓ Branch 1 taken 122090 times.
✓ Branch 2 taken 117386 times.
✓ Branch 3 taken 133887 times.
|
373363 | int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0; |
| 736 |
4/4✓ Branch 0 taken 251273 times.
✓ Branch 1 taken 122090 times.
✓ Branch 2 taken 117386 times.
✓ Branch 3 taken 133887 times.
|
373363 | int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0; |
| 737 | |||
| 738 |
4/4✓ Branch 0 taken 9402 times.
✓ Branch 1 taken 363961 times.
✓ Branch 2 taken 4701 times.
✓ Branch 3 taken 4701 times.
|
373363 | if (desc->flags & AV_PIX_FMT_FLAG_PAL && i == 1) { |
| 739 | 4701 | offsets[i] = 0; | |
| 740 | 4701 | break; | |
| 741 | } | ||
| 742 | |||
| 743 | /* find any component descriptor for this plane */ | ||
| 744 |
1/2✓ Branch 0 taken 723037 times.
✗ Branch 1 not taken.
|
723037 | for (int j = 0; j < desc->nb_components; j++) { |
| 745 |
2/2✓ Branch 0 taken 368662 times.
✓ Branch 1 taken 354375 times.
|
723037 | if (desc->comp[j].plane == i) { |
| 746 | 368662 | comp = &desc->comp[j]; | |
| 747 | 368662 | break; | |
| 748 | } | ||
| 749 | } | ||
| 750 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368662 times.
|
368662 | if (!comp) |
| 751 | ✗ | return AVERROR_BUG; | |
| 752 | |||
| 753 | 368662 | offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] + | |
| 754 | 368662 | (frame->crop_left >> shift_x) * comp->step; | |
| 755 | } | ||
| 756 | |||
| 757 | 133149 | return 0; | |
| 758 | } | ||
| 759 | |||
| 760 | 133453 | 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 133453 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 133453 times.
|
133453 | if (!(frame->width > 0 && frame->height > 0)) |
| 767 | ✗ | return AVERROR(EINVAL); | |
| 768 | |||
| 769 |
1/2✓ Branch 0 taken 133453 times.
✗ Branch 1 not taken.
|
133453 | if (frame->crop_left >= INT_MAX - frame->crop_right || |
| 770 |
1/2✓ Branch 0 taken 133453 times.
✗ Branch 1 not taken.
|
133453 | frame->crop_top >= INT_MAX - frame->crop_bottom || |
| 771 |
1/2✓ Branch 0 taken 133453 times.
✗ Branch 1 not taken.
|
133453 | (frame->crop_left + frame->crop_right) >= frame->width || |
| 772 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 133453 times.
|
133453 | (frame->crop_top + frame->crop_bottom) >= frame->height) |
| 773 | ✗ | return AVERROR(ERANGE); | |
| 774 | |||
| 775 | 133453 | desc = av_pix_fmt_desc_get(frame->format); | |
| 776 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 133453 times.
|
133453 | 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 133135 times.
|
133453 | 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 | 133135 | ret = calc_cropping_offsets(offsets, frame, desc); | |
| 793 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 133135 times.
|
133135 | if (ret < 0) |
| 794 | ✗ | return ret; | |
| 795 | |||
| 796 | /* adjust the offsets to avoid breaking alignment */ | ||
| 797 |
2/2✓ Branch 0 taken 7476 times.
✓ Branch 1 taken 125659 times.
|
133135 | if (!(flags & AV_FRAME_CROP_UNALIGNED)) { |
| 798 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7462 times.
|
7476 | int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX; |
| 799 | 7476 | int min_log2_align = INT_MAX; | |
| 800 | |||
| 801 |
2/2✓ Branch 0 taken 21948 times.
✓ Branch 1 taken 7476 times.
|
29424 | for (int i = 0; frame->data[i]; i++) { |
| 802 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21906 times.
|
21948 | int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX; |
| 803 | 21948 | 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 7476 times.
|
7476 | if (log2_crop_align < min_log2_align) |
| 809 | ✗ | return AVERROR_BUG; | |
| 810 | |||
| 811 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7462 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
7476 | 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 373321 times.
✓ Branch 1 taken 133135 times.
|
506456 | for (int i = 0; frame->data[i]; i++) |
| 820 | 373321 | frame->data[i] += offsets[i]; | |
| 821 | |||
| 822 | 133135 | frame->width -= (frame->crop_left + frame->crop_right); | |
| 823 | 133135 | frame->height -= (frame->crop_top + frame->crop_bottom); | |
| 824 | 133135 | frame->crop_left = 0; | |
| 825 | 133135 | frame->crop_right = 0; | |
| 826 | 133135 | frame->crop_top = 0; | |
| 827 | 133135 | frame->crop_bottom = 0; | |
| 828 | |||
| 829 | 133135 | return 0; | |
| 830 | } | ||
| 831 |