| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2015 Matthieu Bouron <matthieu.bouron stupeflix.com> | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "framepool.h" | ||
| 22 | #include "libavutil/avassert.h" | ||
| 23 | #include "libavutil/avutil.h" | ||
| 24 | #include "libavutil/buffer.h" | ||
| 25 | #include "libavutil/frame.h" | ||
| 26 | #include "libavutil/imgutils.h" | ||
| 27 | #include "libavutil/imgutils_internal.h" | ||
| 28 | #include "libavutil/mem.h" | ||
| 29 | #include "libavutil/pixfmt.h" | ||
| 30 | |||
| 31 | 32592 | static av_cold int frame_pool_video_init(int width, int height, | |
| 32 | enum AVPixelFormat format, | ||
| 33 | int align, FFFramePool *pool) | ||
| 34 | { | ||
| 35 | int ret; | ||
| 36 | |||
| 37 | 32592 | *pool = (FFFramePool) { | |
| 38 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 39 | .width = width, | ||
| 40 | .height = height, | ||
| 41 | .pix_fmt = format, | ||
| 42 | .align = align, | ||
| 43 | }; | ||
| 44 | |||
| 45 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 32592 times.
|
32592 | if ((ret = av_image_check_size2(width, height, INT64_MAX, format, 0, NULL)) < 0) |
| 46 | ✗ | goto fail; | |
| 47 | |||
| 48 | 32592 | ret = av_image_fill_linesizes(pool->linesize, pool->pix_fmt, | |
| 49 | 32592 | FFALIGN(pool->width, align)); | |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32592 times.
|
32592 | if (ret < 0) |
| 51 | ✗ | goto fail; | |
| 52 | |||
| 53 |
4/4✓ Branch 0 taken 99646 times.
✓ Branch 1 taken 4063 times.
✓ Branch 2 taken 71117 times.
✓ Branch 3 taken 28529 times.
|
103709 | for (int i = 0; i < 4 && pool->linesize[i]; i++) |
| 54 | 71117 | pool->linesize[i] = FFALIGN(pool->linesize[i], pool->align); | |
| 55 | |||
| 56 | ptrdiff_t linesizes[4]; | ||
| 57 |
2/2✓ Branch 0 taken 130368 times.
✓ Branch 1 taken 32592 times.
|
162960 | for (int i = 0; i < 4; i++) |
| 58 | 130368 | linesizes[i] = pool->linesize[i]; | |
| 59 | |||
| 60 | size_t sizes[4]; | ||
| 61 | 32592 | ret = av_image_fill_plane_sizes(sizes, pool->pix_fmt, | |
| 62 | 32592 | FFALIGN(pool->height, align), | |
| 63 | linesizes); | ||
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32592 times.
|
32592 | if (ret < 0) |
| 65 | ✗ | goto fail; | |
| 66 | |||
| 67 |
4/4✓ Branch 0 taken 99669 times.
✓ Branch 1 taken 4063 times.
✓ Branch 2 taken 71140 times.
✓ Branch 3 taken 28529 times.
|
103732 | for (int i = 0; i < 4 && sizes[i]; i++) { |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71140 times.
|
71140 | if (sizes[i] > SIZE_MAX - align) |
| 69 | ✗ | goto fail; | |
| 70 | 71140 | pool->pools[i] = av_buffer_pool_init(sizes[i] + align, | |
| 71 | CONFIG_MEMORY_POISONING | ||
| 72 | ? NULL | ||
| 73 | : av_buffer_allocz); | ||
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71140 times.
|
71140 | if (!pool->pools[i]) { |
| 75 | ✗ | ret = AVERROR(ENOMEM); | |
| 76 | ✗ | goto fail; | |
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | 32592 | return 0; | |
| 81 | |||
| 82 | ✗ | fail: | |
| 83 | ✗ | ff_frame_pool_uninit(pool); | |
| 84 | ✗ | return ret; | |
| 85 | } | ||
| 86 | |||
| 87 | 13763 | static av_cold int frame_pool_audio_init(int channels, int nb_samples, | |
| 88 | enum AVSampleFormat format, | ||
| 89 | int align, FFFramePool *pool) | ||
| 90 | { | ||
| 91 | int ret; | ||
| 92 | |||
| 93 | 13763 | int planar = av_sample_fmt_is_planar(format); | |
| 94 | |||
| 95 | 13763 | *pool = (FFFramePool) { | |
| 96 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 97 |
2/2✓ Branch 0 taken 2077 times.
✓ Branch 1 taken 11686 times.
|
13763 | .planes = planar ? channels : 1, |
| 98 | .channels = channels, | ||
| 99 | .nb_samples = nb_samples, | ||
| 100 | .sample_fmt = format, | ||
| 101 | .align = align, | ||
| 102 | }; | ||
| 103 | |||
| 104 | 13763 | ret = av_samples_get_buffer_size(&pool->linesize[0], channels, | |
| 105 | nb_samples, format, 0); | ||
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13763 times.
|
13763 | if (ret < 0) |
| 107 | ✗ | goto fail; | |
| 108 | |||
| 109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13763 times.
|
13763 | if (pool->linesize[0] > SIZE_MAX - align) { |
| 110 | ✗ | ret = AVERROR(EINVAL); | |
| 111 | ✗ | goto fail; | |
| 112 | } | ||
| 113 | |||
| 114 | 13763 | pool->pools[0] = av_buffer_pool_init(pool->linesize[0] + align, | |
| 115 | av_buffer_allocz); | ||
| 116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13763 times.
|
13763 | if (!pool->pools[0]) { |
| 117 | ✗ | ret = AVERROR(ENOMEM); | |
| 118 | ✗ | goto fail; | |
| 119 | } | ||
| 120 | |||
| 121 | 13763 | return 0; | |
| 122 | |||
| 123 | ✗ | fail: | |
| 124 | ✗ | ff_frame_pool_uninit(pool); | |
| 125 | ✗ | return ret; | |
| 126 | } | ||
| 127 | |||
| 128 | 403540 | AVFrame *ff_frame_pool_get(FFFramePool *pool) | |
| 129 | { | ||
| 130 | const AVPixFmtDescriptor *desc; | ||
| 131 | |||
| 132 | 403540 | AVFrame *frame = av_frame_alloc(); | |
| 133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 403540 times.
|
403540 | if (!frame) |
| 134 | ✗ | return NULL; | |
| 135 | |||
| 136 |
2/3✓ Branch 0 taken 126162 times.
✓ Branch 1 taken 277378 times.
✗ Branch 2 not taken.
|
403540 | switch(pool->type) { |
| 137 | 126162 | case AVMEDIA_TYPE_VIDEO: | |
| 138 | 126162 | desc = av_pix_fmt_desc_get(pool->pix_fmt); | |
| 139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126162 times.
|
126162 | if (!desc) |
| 140 | ✗ | goto fail; | |
| 141 | |||
| 142 | 126162 | frame->width = pool->width; | |
| 143 | 126162 | frame->height = pool->height; | |
| 144 | 126162 | frame->format = pool->pix_fmt; | |
| 145 | |||
| 146 |
2/2✓ Branch 0 taken 418117 times.
✓ Branch 1 taken 3459 times.
|
421576 | for (int i = 0; i < 4; i++) { |
| 147 | 418117 | frame->linesize[i] = pool->linesize[i]; | |
| 148 |
2/2✓ Branch 0 taken 122703 times.
✓ Branch 1 taken 295414 times.
|
418117 | if (!pool->pools[i]) |
| 149 | 122703 | break; | |
| 150 | |||
| 151 | 295414 | frame->buf[i] = av_buffer_pool_get(pool->pools[i]); | |
| 152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 295414 times.
|
295414 | if (!frame->buf[i]) |
| 153 | ✗ | goto fail; | |
| 154 | |||
| 155 | 295414 | frame->data[i] = (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, pool->align); | |
| 156 | } | ||
| 157 | |||
| 158 |
2/2✓ Branch 0 taken 877 times.
✓ Branch 1 taken 125285 times.
|
126162 | if (desc->flags & AV_PIX_FMT_FLAG_PAL) { |
| 159 | 877 | enum AVPixelFormat format = pool->pix_fmt; | |
| 160 |
1/2✓ Branch 0 taken 877 times.
✗ Branch 1 not taken.
|
877 | if (format == AV_PIX_FMT_PAL8) |
| 161 | 877 | format = AV_PIX_FMT_BGR8; | |
| 162 | |||
| 163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
|
877 | av_assert0(frame->data[1] != NULL); |
| 164 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 877 times.
|
877 | if (avpriv_set_systematic_pal2((uint32_t *)frame->data[1], format) < 0) |
| 165 | ✗ | goto fail; | |
| 166 | } | ||
| 167 | |||
| 168 | 126162 | frame->extended_data = frame->data; | |
| 169 | 126162 | break; | |
| 170 | 277378 | case AVMEDIA_TYPE_AUDIO: | |
| 171 | 277378 | frame->nb_samples = pool->nb_samples; | |
| 172 | 277378 | frame->ch_layout.nb_channels = pool->channels; | |
| 173 | 277378 | frame->format = pool->sample_fmt; | |
| 174 | 277378 | frame->linesize[0] = pool->linesize[0]; | |
| 175 | |||
| 176 |
2/2✓ Branch 0 taken 516 times.
✓ Branch 1 taken 276862 times.
|
277378 | if (pool->planes > AV_NUM_DATA_POINTERS) { |
| 177 | 516 | frame->extended_data = av_calloc(pool->planes, | |
| 178 | sizeof(*frame->extended_data)); | ||
| 179 | 516 | frame->nb_extended_buf = pool->planes - AV_NUM_DATA_POINTERS; | |
| 180 | 516 | frame->extended_buf = av_calloc(frame->nb_extended_buf, | |
| 181 | sizeof(*frame->extended_buf)); | ||
| 182 |
2/4✓ Branch 0 taken 516 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 516 times.
|
516 | if (!frame->extended_data || !frame->extended_buf) |
| 183 | ✗ | goto fail; | |
| 184 | } else { | ||
| 185 | 276862 | frame->extended_data = frame->data; | |
| 186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 276862 times.
|
276862 | av_assert0(frame->nb_extended_buf == 0); |
| 187 | } | ||
| 188 | |||
| 189 |
2/2✓ Branch 0 taken 286859 times.
✓ Branch 1 taken 277378 times.
|
564237 | for (int i = 0; i < FFMIN(pool->planes, AV_NUM_DATA_POINTERS); i++) { |
| 190 | 286859 | frame->buf[i] = av_buffer_pool_get(pool->pools[0]); | |
| 191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 286859 times.
|
286859 | if (!frame->buf[i]) |
| 192 | ✗ | goto fail; | |
| 193 | 286859 | frame->extended_data[i] = frame->data[i] = | |
| 194 | 286859 | (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, pool->align); | |
| 195 | } | ||
| 196 |
2/2✓ Branch 0 taken 1776 times.
✓ Branch 1 taken 277378 times.
|
279154 | for (int i = 0; i < frame->nb_extended_buf; i++) { |
| 197 | 1776 | frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]); | |
| 198 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1776 times.
|
1776 | if (!frame->extended_buf[i]) |
| 199 | ✗ | goto fail; | |
| 200 | 1776 | frame->extended_data[i + AV_NUM_DATA_POINTERS] = | |
| 201 | 1776 | (uint8_t *)FFALIGN((uintptr_t)frame->extended_buf[i]->data, pool->align); | |
| 202 | } | ||
| 203 | |||
| 204 | 277378 | break; | |
| 205 | ✗ | default: | |
| 206 | ✗ | av_unreachable("only audio and video frame pools exist"); | |
| 207 | } | ||
| 208 | |||
| 209 | 403540 | return frame; | |
| 210 | ✗ | fail: | |
| 211 | ✗ | av_frame_free(&frame); | |
| 212 | ✗ | return NULL; | |
| 213 | } | ||
| 214 | |||
| 215 | 170212 | av_cold void ff_frame_pool_uninit(FFFramePool *pool) | |
| 216 | { | ||
| 217 |
2/2✓ Branch 0 taken 680848 times.
✓ Branch 1 taken 170212 times.
|
851060 | for (int i = 0; i < 4; i++) |
| 218 | 680848 | av_buffer_pool_uninit(&pool->pools[i]); | |
| 219 | |||
| 220 | 170212 | memset(pool, 0, sizeof(*pool)); | |
| 221 | 170212 | } | |
| 222 | |||
| 223 | 282504 | int ff_frame_pool_video_reinit(FFFramePool *pool, | |
| 224 | int width, | ||
| 225 | int height, | ||
| 226 | enum AVPixelFormat format, | ||
| 227 | int align) | ||
| 228 | { | ||
| 229 |
1/2✓ Branch 0 taken 282504 times.
✗ Branch 1 not taken.
|
282504 | if (pool->type == AVMEDIA_TYPE_VIDEO && |
| 230 |
2/2✓ Branch 0 taken 251019 times.
✓ Branch 1 taken 31485 times.
|
282504 | pool->pix_fmt == format && |
| 231 |
2/2✓ Branch 0 taken 249915 times.
✓ Branch 1 taken 1104 times.
|
251019 | FFALIGN(pool->width, pool->align) == FFALIGN(width, align) && |
| 232 |
2/2✓ Branch 0 taken 249912 times.
✓ Branch 1 taken 3 times.
|
249915 | FFALIGN(pool->height, pool->align) == FFALIGN(height, align) && |
| 233 |
1/2✓ Branch 0 taken 249912 times.
✗ Branch 1 not taken.
|
249912 | pool->align == align) |
| 234 | { | ||
| 235 | 249912 | pool->width = width; | |
| 236 | 249912 | pool->height = height; | |
| 237 | 249912 | return 0; | |
| 238 | } | ||
| 239 | |||
| 240 | 32592 | ff_frame_pool_uninit(pool); | |
| 241 | 32592 | return frame_pool_video_init(width, height, format, align, pool); | |
| 242 | } | ||
| 243 | |||
| 244 | 277378 | int ff_frame_pool_audio_reinit(FFFramePool *pool, | |
| 245 | int channels, | ||
| 246 | int nb_samples, | ||
| 247 | enum AVSampleFormat format, | ||
| 248 | int align) | ||
| 249 | { | ||
| 250 |
2/2✓ Branch 0 taken 275632 times.
✓ Branch 1 taken 1746 times.
|
277378 | if (pool->type == AVMEDIA_TYPE_AUDIO && |
| 251 |
1/2✓ Branch 0 taken 275632 times.
✗ Branch 1 not taken.
|
275632 | pool->sample_fmt == format && |
| 252 |
1/2✓ Branch 0 taken 275632 times.
✗ Branch 1 not taken.
|
275632 | pool->channels == channels && |
| 253 |
2/2✓ Branch 0 taken 263615 times.
✓ Branch 1 taken 12017 times.
|
275632 | pool->nb_samples == nb_samples && |
| 254 |
1/2✓ Branch 0 taken 263615 times.
✗ Branch 1 not taken.
|
263615 | pool->align == align) |
| 255 | { | ||
| 256 | 263615 | return 0; | |
| 257 | } | ||
| 258 | |||
| 259 | 13763 | ff_frame_pool_uninit(pool); | |
| 260 | 13763 | return frame_pool_audio_init(channels, nb_samples, format, align, pool); | |
| 261 | } | ||
| 262 |