fftools/sync_queue.c
| 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 <stdint.h> | ||
| 20 | #include <string.h> | ||
| 21 | |||
| 22 | #include "libavutil/avassert.h" | ||
| 23 | #include "libavutil/container_fifo.h" | ||
| 24 | #include "libavutil/channel_layout.h" | ||
| 25 | #include "libavutil/cpu.h" | ||
| 26 | #include "libavutil/error.h" | ||
| 27 | #include "libavutil/mathematics.h" | ||
| 28 | #include "libavutil/mem.h" | ||
| 29 | #include "libavutil/samplefmt.h" | ||
| 30 | #include "libavutil/timestamp.h" | ||
| 31 | |||
| 32 | #include "sync_queue.h" | ||
| 33 | |||
| 34 | /* | ||
| 35 | * How this works: | ||
| 36 | * -------------- | ||
| 37 | * time: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 | ||
| 38 | * ------------------------------------------------------------------- | ||
| 39 | * | | | | | | | | | | | | | | | ||
| 40 | * | ┌───┐┌────────┐┌───┐┌─────────────┐ | ||
| 41 | * stream 0| │d=1││ d=2 ││d=1││ d=3 │ | ||
| 42 | * | └───┘└────────┘└───┘└─────────────┘ | ||
| 43 | * ┌───┐ ┌───────────────────────┐ | ||
| 44 | * stream 1│d=1│ │ d=5 │ | ||
| 45 | * └───┘ └───────────────────────┘ | ||
| 46 | * | ┌───┐┌───┐┌───┐┌───┐ | ||
| 47 | * stream 2| │d=1││d=1││d=1││d=1│ <- stream 2 is the head stream of the queue | ||
| 48 | * | └───┘└───┘└───┘└───┘ | ||
| 49 | * ^ ^ | ||
| 50 | * [stream 2 tail] [stream 2 head] | ||
| 51 | * | ||
| 52 | * We have N streams (N=3 in the diagram), each stream is a FIFO. The *tail* of | ||
| 53 | * each FIFO is the frame with smallest end time, the *head* is the frame with | ||
| 54 | * the largest end time. Frames submitted to the queue with sq_send() are placed | ||
| 55 | * after the head, frames returned to the caller with sq_receive() are taken | ||
| 56 | * from the tail. | ||
| 57 | * | ||
| 58 | * The head stream of the whole queue (SyncQueue.head_stream) is the limiting | ||
| 59 | * stream with the *smallest* head timestamp, i.e. the stream whose source lags | ||
| 60 | * furthest behind all other streams. It determines which frames can be output | ||
| 61 | * from the queue. | ||
| 62 | * | ||
| 63 | * In the diagram, the head stream is 2, because it head time is t=5, while | ||
| 64 | * streams 0 and 1 end at t=8 and t=9 respectively. All frames that _end_ at | ||
| 65 | * or before t=5 can be output, i.e. the first 3 frames from stream 0, first | ||
| 66 | * frame from stream 1, and all 4 frames from stream 2. | ||
| 67 | */ | ||
| 68 | |||
| 69 | #define SQPTR(sq, frame) ((sq->type == SYNC_QUEUE_FRAMES) ? \ | ||
| 70 | (void*)frame.f : (void*)frame.p) | ||
| 71 | |||
| 72 | typedef struct SyncQueueStream { | ||
| 73 | AVContainerFifo *fifo; | ||
| 74 | AVRational tb; | ||
| 75 | |||
| 76 | /* number of audio samples in fifo */ | ||
| 77 | uint64_t samples_queued; | ||
| 78 | /* stream head: largest timestamp seen */ | ||
| 79 | int64_t head_ts; | ||
| 80 | int limiting; | ||
| 81 | /* no more frames will be sent for this stream */ | ||
| 82 | int finished; | ||
| 83 | |||
| 84 | uint64_t frames_sent; | ||
| 85 | uint64_t samples_sent; | ||
| 86 | uint64_t frames_max; | ||
| 87 | int frame_samples; | ||
| 88 | /* sample rate for audio that can be cut by a frame queue, or zero */ | ||
| 89 | int cut_sample_rate; | ||
| 90 | } SyncQueueStream; | ||
| 91 | |||
| 92 | struct SyncQueue { | ||
| 93 | enum SyncQueueType type; | ||
| 94 | |||
| 95 | void *logctx; | ||
| 96 | |||
| 97 | /* no more frames will be sent for any stream */ | ||
| 98 | int finished; | ||
| 99 | /* sync head: the stream with the _smallest_ head timestamp | ||
| 100 | * this stream determines which frames can be output */ | ||
| 101 | int head_stream; | ||
| 102 | /* the finished stream with the smallest finish timestamp or -1 */ | ||
| 103 | int head_finished_stream; | ||
| 104 | |||
| 105 | // maximum buffering duration in microseconds | ||
| 106 | int64_t buf_size_us; | ||
| 107 | |||
| 108 | SyncQueueStream *streams; | ||
| 109 | unsigned int nb_streams; | ||
| 110 | |||
| 111 | int have_limiting; | ||
| 112 | |||
| 113 | uintptr_t align_mask; | ||
| 114 | }; | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Compute the end timestamp of a frame. If nb_samples is provided, consider | ||
| 118 | * the frame to have this number of audio samples, otherwise use frame duration. | ||
| 119 | */ | ||
| 120 | 820429 | static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples) | |
| 121 | { | ||
| 122 | 820429 | if (nb_samples) { | |
| 123 | 230768 | int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate}, | |
| 124 | 230768 | frame.f->time_base); | |
| 125 | 230768 | return frame.f->pts + d; | |
| 126 | } | ||
| 127 | |||
| 128 | 589661 | return (sq->type == SYNC_QUEUE_PACKETS) ? | |
| 129 | 589661 | frame.p->pts + frame.p->duration : | |
| 130 | 581591 | frame.f->pts + frame.f->duration; | |
| 131 | } | ||
| 132 | |||
| 133 | 473026 | static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame) | |
| 134 | { | ||
| 135 | 473026 | return (sq->type == SYNC_QUEUE_PACKETS) ? 0 : frame.f->nb_samples; | |
| 136 | } | ||
| 137 | |||
| 138 | 424141 | static int frame_null(const SyncQueue *sq, SyncQueueFrame frame) | |
| 139 | { | ||
| 140 | 424141 | return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL); | |
| 141 | } | ||
| 142 | |||
| 143 | 416554 | static void tb_update(const SyncQueue *sq, SyncQueueStream *st, | |
| 144 | const SyncQueueFrame frame) | ||
| 145 | { | ||
| 146 | 833108 | AVRational tb = (sq->type == SYNC_QUEUE_PACKETS) ? | |
| 147 | 416554 | frame.p->time_base : frame.f->time_base; | |
| 148 | |||
| 149 | 416554 | av_assert0(tb.num > 0 && tb.den > 0); | |
| 150 | |||
| 151 | 416554 | if (tb.num == st->tb.num && tb.den == st->tb.den) | |
| 152 | 412907 | return; | |
| 153 | |||
| 154 | // timebase should not change after the first frame | ||
| 155 | 3647 | av_assert0(!av_container_fifo_can_read(st->fifo)); | |
| 156 | |||
| 157 | 3647 | if (st->head_ts != AV_NOPTS_VALUE) | |
| 158 | 1 | st->head_ts = av_rescale_q(st->head_ts, st->tb, tb); | |
| 159 | |||
| 160 | 3647 | st->tb = tb; | |
| 161 | } | ||
| 162 | |||
| 163 | 10839 | static void finish_stream(SyncQueue *sq, unsigned int stream_idx) | |
| 164 | { | ||
| 165 | 10839 | SyncQueueStream *st = &sq->streams[stream_idx]; | |
| 166 | |||
| 167 | 10839 | if (!st->finished) | |
| 168 | 3734 | av_log(sq->logctx, AV_LOG_DEBUG, | |
| 169 | "sq: finish %u; head ts %s\n", stream_idx, | ||
| 170 | 3734 | av_ts2timestr(st->head_ts, &st->tb)); | |
| 171 | |||
| 172 | 10839 | st->finished = 1; | |
| 173 | |||
| 174 | 10839 | if (st->limiting && st->head_ts != AV_NOPTS_VALUE) { | |
| 175 | /* check if this stream is the new finished head */ | ||
| 176 | 16170 | if (sq->head_finished_stream < 0 || | |
| 177 | 6455 | av_compare_ts(st->head_ts, st->tb, | |
| 178 | 6455 | sq->streams[sq->head_finished_stream].head_ts, | |
| 179 | 6455 | sq->streams[sq->head_finished_stream].tb) < 0) { | |
| 180 | 3261 | sq->head_finished_stream = stream_idx; | |
| 181 | } | ||
| 182 | |||
| 183 | /* mark as finished all streams that should no longer receive new frames, | ||
| 184 | * due to them being ahead of some finished stream */ | ||
| 185 | 9715 | st = &sq->streams[sq->head_finished_stream]; | |
| 186 | 19484 | for (unsigned int i = 0; i < sq->nb_streams; i++) { | |
| 187 | 9769 | SyncQueueStream *st1 = &sq->streams[i]; | |
| 188 | 9812 | if (st != st1 && st1->head_ts != AV_NOPTS_VALUE && | |
| 189 | 43 | av_compare_ts(st->head_ts, st->tb, st1->head_ts, st1->tb) <= 0) { | |
| 190 | 34 | if (!st1->finished) | |
| 191 | 6 | av_log(sq->logctx, AV_LOG_DEBUG, | |
| 192 | "sq: finish secondary %u; head ts %s\n", i, | ||
| 193 | 6 | av_ts2timestr(st1->head_ts, &st1->tb)); | |
| 194 | |||
| 195 | 34 | st1->finished = 1; | |
| 196 | } | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | /* mark the whole queue as finished if all streams are finished */ | ||
| 201 | 37537 | for (unsigned int i = 0; i < sq->nb_streams; i++) { | |
| 202 | 26906 | if (!sq->streams[i].finished) | |
| 203 | 208 | return; | |
| 204 | } | ||
| 205 | 10631 | sq->finished = 1; | |
| 206 | |||
| 207 | 10631 | av_log(sq->logctx, AV_LOG_DEBUG, "sq: finish queue\n"); | |
| 208 | } | ||
| 209 | |||
| 210 | 314997 | static void queue_head_update(SyncQueue *sq) | |
| 211 | { | ||
| 212 | 314997 | av_assert0(sq->have_limiting); | |
| 213 | |||
| 214 | 314997 | if (sq->head_stream < 0) { | |
| 215 | 3325 | unsigned first_limiting = UINT_MAX; | |
| 216 | |||
| 217 | /* wait for one timestamp in each stream before determining | ||
| 218 | * the queue head */ | ||
| 219 | 6651 | for (unsigned int i = 0; i < sq->nb_streams; i++) { | |
| 220 | 3390 | SyncQueueStream *st = &sq->streams[i]; | |
| 221 | 3390 | if (!st->limiting) | |
| 222 | 13 | continue; | |
| 223 | 3377 | if (st->head_ts == AV_NOPTS_VALUE) | |
| 224 | 64 | return; | |
| 225 | 3313 | if (first_limiting == UINT_MAX) | |
| 226 | 3307 | first_limiting = i; | |
| 227 | } | ||
| 228 | |||
| 229 | // placeholder value, correct one will be found below | ||
| 230 | 3261 | av_assert0(first_limiting < UINT_MAX); | |
| 231 | 3261 | sq->head_stream = first_limiting; | |
| 232 | } | ||
| 233 | |||
| 234 | 924985 | for (unsigned int i = 0; i < sq->nb_streams; i++) { | |
| 235 | 610052 | SyncQueueStream *st_head = &sq->streams[sq->head_stream]; | |
| 236 | 610052 | SyncQueueStream *st_other = &sq->streams[i]; | |
| 237 | 1220023 | if (st_other->limiting && st_other->head_ts != AV_NOPTS_VALUE && | |
| 238 | 609971 | av_compare_ts(st_other->head_ts, st_other->tb, | |
| 239 | st_head->head_ts, st_head->tb) < 0) | ||
| 240 | 97 | sq->head_stream = i; | |
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | /* update this stream's head timestamp */ | ||
| 245 | 416692 | static void stream_update_ts(SyncQueue *sq, unsigned int stream_idx, int64_t ts) | |
| 246 | { | ||
| 247 | 416692 | SyncQueueStream *st = &sq->streams[stream_idx]; | |
| 248 | |||
| 249 | 416692 | if (ts == AV_NOPTS_VALUE || | |
| 250 | 416689 | (st->head_ts != AV_NOPTS_VALUE && st->head_ts >= ts)) | |
| 251 | 8136 | return; | |
| 252 | |||
| 253 | 408556 | st->head_ts = ts; | |
| 254 | |||
| 255 | /* if this stream is now ahead of some finished stream, then | ||
| 256 | * this stream is also finished */ | ||
| 257 | 408679 | if (sq->head_finished_stream >= 0 && | |
| 258 | 123 | av_compare_ts(sq->streams[sq->head_finished_stream].head_ts, | |
| 259 | 123 | sq->streams[sq->head_finished_stream].tb, | |
| 260 | ts, st->tb) <= 0) | ||
| 261 | 11 | finish_stream(sq, stream_idx); | |
| 262 | |||
| 263 | /* update the overall head timestamp if it could have changed */ | ||
| 264 | 408556 | if (st->limiting && | |
| 265 | 372834 | (sq->head_stream < 0 || sq->head_stream == stream_idx)) | |
| 266 | 314997 | queue_head_update(sq); | |
| 267 | } | ||
| 268 | |||
| 269 | /* If the queue for the given stream (or all streams when stream_idx=-1) | ||
| 270 | * is overflowing, trigger a fake heartbeat on lagging streams. | ||
| 271 | * | ||
| 272 | * @return 1 if heartbeat triggered, 0 otherwise | ||
| 273 | */ | ||
| 274 | 413639 | static int overflow_heartbeat(SyncQueue *sq, int stream_idx) | |
| 275 | { | ||
| 276 | SyncQueueStream *st; | ||
| 277 | SyncQueueFrame frame; | ||
| 278 | 413639 | int64_t tail_ts = AV_NOPTS_VALUE; | |
| 279 | |||
| 280 | /* if no stream specified, pick the one that is most ahead */ | ||
| 281 | 413639 | if (stream_idx < 0) { | |
| 282 | 413639 | int64_t ts = AV_NOPTS_VALUE; | |
| 283 | |||
| 284 | 1520301 | for (int i = 0; i < sq->nb_streams; i++) { | |
| 285 | 1106662 | st = &sq->streams[i]; | |
| 286 | 1106662 | if (st->head_ts != AV_NOPTS_VALUE && | |
| 287 | 687579 | (ts == AV_NOPTS_VALUE || | |
| 288 | 687579 | av_compare_ts(ts, sq->streams[stream_idx].tb, | |
| 289 | st->head_ts, st->tb) < 0)) { | ||
| 290 | 471587 | ts = st->head_ts; | |
| 291 | 471587 | stream_idx = i; | |
| 292 | } | ||
| 293 | } | ||
| 294 | /* no stream has a timestamp yet -> nothing to do */ | ||
| 295 | 413639 | if (stream_idx < 0) | |
| 296 | 3 | return 0; | |
| 297 | } | ||
| 298 | |||
| 299 | 413636 | st = &sq->streams[stream_idx]; | |
| 300 | |||
| 301 | /* get the chosen stream's tail timestamp */ | ||
| 302 | 919391 | for (size_t i = 0; tail_ts == AV_NOPTS_VALUE && | |
| 303 | 505755 | av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 0; i++) { | |
| 304 | 92119 | int nb_samples = 0; | |
| 305 | |||
| 306 | /* Use the chunk end from receive_for_stream() only when the queued | ||
| 307 | * frame is shorter than the encoder frame size; otherwise keep the | ||
| 308 | * frame's own end timestamp. */ | ||
| 309 | 92119 | if (sq->type == SYNC_QUEUE_FRAMES && | |
| 310 | 91295 | frame.f->nb_samples < st->frame_samples) | |
| 311 | 83161 | nb_samples = st->frame_samples; | |
| 312 | |||
| 313 | 92119 | tail_ts = frame_end(sq, frame, nb_samples); | |
| 314 | } | ||
| 315 | |||
| 316 | /* overflow triggers when the tail is over specified duration behind the head */ | ||
| 317 | 413636 | if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts || | |
| 318 | 35297 | av_rescale_q(st->head_ts - tail_ts, st->tb, AV_TIME_BASE_Q) < sq->buf_size_us) | |
| 319 | 413498 | return 0; | |
| 320 | |||
| 321 | /* signal a fake timestamp for all streams that prevent tail_ts from being output */ | ||
| 322 | 138 | tail_ts++; | |
| 323 | 414 | for (unsigned int i = 0; i < sq->nb_streams; i++) { | |
| 324 | 276 | const SyncQueueStream *st1 = &sq->streams[i]; | |
| 325 | int64_t ts; | ||
| 326 | |||
| 327 | 276 | if (st == st1 || st1->finished || | |
| 328 | 275 | (st1->head_ts != AV_NOPTS_VALUE && | |
| 329 | 137 | av_compare_ts(tail_ts, st->tb, st1->head_ts, st1->tb) <= 0)) | |
| 330 | 138 | continue; | |
| 331 | |||
| 332 | 138 | ts = av_rescale_q(tail_ts, st->tb, st1->tb); | |
| 333 | 138 | if (st1->head_ts != AV_NOPTS_VALUE) | |
| 334 | 137 | ts = FFMAX(st1->head_ts + 1, ts); | |
| 335 | |||
| 336 | 138 | av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u overflow heardbeat %s -> %s\n", | |
| 337 | 138 | i, av_ts2timestr(st1->head_ts, &st1->tb), av_ts2timestr(ts, &st1->tb)); | |
| 338 | |||
| 339 | 138 | stream_update_ts(sq, i, ts); | |
| 340 | } | ||
| 341 | |||
| 342 | 138 | return 1; | |
| 343 | } | ||
| 344 | |||
| 345 | 424141 | int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) | |
| 346 | { | ||
| 347 | SyncQueueStream *st; | ||
| 348 | int64_t ts; | ||
| 349 | int ret, nb_samples; | ||
| 350 | |||
| 351 | 424141 | av_assert0(stream_idx < sq->nb_streams); | |
| 352 | 424141 | st = &sq->streams[stream_idx]; | |
| 353 | |||
| 354 | 424141 | if (frame_null(sq, frame)) { | |
| 355 | 7515 | av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u EOF\n", stream_idx); | |
| 356 | 7515 | finish_stream(sq, stream_idx); | |
| 357 | 7515 | return 0; | |
| 358 | } | ||
| 359 | 416626 | if (st->finished) | |
| 360 | 72 | return AVERROR_EOF; | |
| 361 | |||
| 362 | 416554 | tb_update(sq, st, frame); | |
| 363 | |||
| 364 | 416554 | nb_samples = frame_samples(sq, frame); | |
| 365 | // make sure frame duration is consistent with sample count | ||
| 366 | 416554 | if (nb_samples) { | |
| 367 | 399711 | av_assert0(frame.f->sample_rate > 0); | |
| 368 | 399711 | frame.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate }, | |
| 369 | 399711 | frame.f->time_base); | |
| 370 | } | ||
| 371 | |||
| 372 | 416554 | ts = frame_end(sq, frame, 0); | |
| 373 | |||
| 374 | 416554 | av_log(sq->logctx, AV_LOG_DEBUG, "sq: send %u ts %s\n", stream_idx, | |
| 375 | 416554 | av_ts2timestr(ts, &st->tb)); | |
| 376 | |||
| 377 | 416554 | ret = av_container_fifo_write(st->fifo, SQPTR(sq, frame), 0); | |
| 378 | 416554 | if (ret < 0) | |
| 379 | ✗ | return ret; | |
| 380 | |||
| 381 | 416554 | stream_update_ts(sq, stream_idx, ts); | |
| 382 | |||
| 383 | 416554 | st->samples_queued += nb_samples; | |
| 384 | 416554 | st->samples_sent += nb_samples; | |
| 385 | |||
| 386 | 416554 | if (st->frame_samples) | |
| 387 | 395593 | st->frames_sent = st->samples_sent / st->frame_samples; | |
| 388 | else | ||
| 389 | 20961 | st->frames_sent++; | |
| 390 | |||
| 391 | 416554 | if (st->frames_sent >= st->frames_max) { | |
| 392 | 3250 | av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u frames_max %"PRIu64" reached\n", | |
| 393 | stream_idx, st->frames_max); | ||
| 394 | |||
| 395 | 3250 | finish_stream(sq, stream_idx); | |
| 396 | } | ||
| 397 | |||
| 398 | 416554 | return 0; | |
| 399 | } | ||
| 400 | |||
| 401 | 102734 | static void offset_audio(AVFrame *f, int nb_samples) | |
| 402 | { | ||
| 403 | 102734 | const int planar = av_sample_fmt_is_planar(f->format); | |
| 404 | 102734 | const int planes = planar ? f->ch_layout.nb_channels : 1; | |
| 405 | 102734 | const int bps = av_get_bytes_per_sample(f->format); | |
| 406 | 102734 | const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels); | |
| 407 | |||
| 408 | 102734 | av_assert0(bps > 0); | |
| 409 | 102734 | av_assert0(nb_samples < f->nb_samples); | |
| 410 | |||
| 411 | 229006 | for (int i = 0; i < planes; i++) { | |
| 412 | 126272 | f->extended_data[i] += offset; | |
| 413 | 126272 | if (i < FF_ARRAY_ELEMS(f->data)) | |
| 414 | 126056 | f->data[i] = f->extended_data[i]; | |
| 415 | } | ||
| 416 | 102734 | f->linesize[0] -= offset; | |
| 417 | 102734 | f->nb_samples -= nb_samples; | |
| 418 | 102734 | f->duration = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate }, | |
| 419 | f->time_base); | ||
| 420 | 102734 | f->pts += av_rescale_q(nb_samples, (AVRational){ 1, f->sample_rate }, | |
| 421 | f->time_base); | ||
| 422 | 102734 | } | |
| 423 | |||
| 424 | 87874 | static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame) | |
| 425 | { | ||
| 426 | // only checks linesize[0], so only works for audio | ||
| 427 | 87874 | av_assert0(frame->nb_samples > 0); | |
| 428 | 87874 | av_assert0(sq->align_mask); | |
| 429 | |||
| 430 | // only check data[0], because we always offset all data pointers | ||
| 431 | // by the same offset, so if one is aligned, all are | ||
| 432 | 87874 | if (!((uintptr_t)frame->data[0] & sq->align_mask) && | |
| 433 | 86995 | !(frame->linesize[0] & sq->align_mask) && | |
| 434 | 86995 | frame->linesize[0] > sq->align_mask) | |
| 435 | 86995 | return 1; | |
| 436 | |||
| 437 | 879 | return 0; | |
| 438 | } | ||
| 439 | |||
| 440 | 105817 | static int receive_samples(SyncQueue *sq, SyncQueueStream *st, | |
| 441 | AVFrame *dst, int nb_samples) | ||
| 442 | { | ||
| 443 | SyncQueueFrame src; | ||
| 444 | int ret; | ||
| 445 | |||
| 446 | 105817 | av_assert0(st->samples_queued >= nb_samples); | |
| 447 | |||
| 448 | 105817 | ret = av_container_fifo_peek(st->fifo, (void**)&src, 0); | |
| 449 | 105817 | av_assert0(ret >= 0); | |
| 450 | |||
| 451 | // peeked frame has enough samples and its data is aligned | ||
| 452 | // -> we can just make a reference and limit its sample count | ||
| 453 | 105817 | if (src.f->nb_samples > nb_samples && frame_is_aligned(sq, src.f)) { | |
| 454 | 79678 | ret = av_frame_ref(dst, src.f); | |
| 455 | 79678 | if (ret < 0) | |
| 456 | ✗ | return ret; | |
| 457 | |||
| 458 | 79678 | dst->nb_samples = nb_samples; | |
| 459 | 79678 | offset_audio(src.f, nb_samples); | |
| 460 | 79678 | st->samples_queued -= nb_samples; | |
| 461 | |||
| 462 | 79678 | goto finish; | |
| 463 | } | ||
| 464 | |||
| 465 | // otherwise allocate a new frame and copy the data | ||
| 466 | 26139 | ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout); | |
| 467 | 26139 | if (ret < 0) | |
| 468 | ✗ | return ret; | |
| 469 | |||
| 470 | 26139 | dst->format = src.f->format; | |
| 471 | 26139 | dst->nb_samples = nb_samples; | |
| 472 | |||
| 473 | 26139 | ret = av_frame_get_buffer(dst, 0); | |
| 474 | 26139 | if (ret < 0) | |
| 475 | ✗ | goto fail; | |
| 476 | |||
| 477 | 26139 | ret = av_frame_copy_props(dst, src.f); | |
| 478 | 26139 | if (ret < 0) | |
| 479 | ✗ | goto fail; | |
| 480 | |||
| 481 | 26139 | dst->nb_samples = 0; | |
| 482 | 437469 | while (dst->nb_samples < nb_samples) { | |
| 483 | int to_copy; | ||
| 484 | |||
| 485 | 411330 | ret = av_container_fifo_peek(st->fifo, (void**)&src, 0); | |
| 486 | 411330 | av_assert0(ret >= 0); | |
| 487 | |||
| 488 | 411330 | to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples); | |
| 489 | |||
| 490 | 411330 | av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples, | |
| 491 | 411330 | 0, to_copy, dst->ch_layout.nb_channels, dst->format); | |
| 492 | |||
| 493 | 411330 | if (to_copy < src.f->nb_samples) | |
| 494 | 23056 | offset_audio(src.f, to_copy); | |
| 495 | else | ||
| 496 | 388274 | av_container_fifo_drain(st->fifo, 1); | |
| 497 | |||
| 498 | 411330 | st->samples_queued -= to_copy; | |
| 499 | |||
| 500 | 411330 | dst->nb_samples += to_copy; | |
| 501 | } | ||
| 502 | |||
| 503 | 26139 | finish: | |
| 504 | 105817 | dst->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst->sample_rate }, | |
| 505 | dst->time_base); | ||
| 506 | |||
| 507 | 105817 | return 0; | |
| 508 | |||
| 509 | ✗ | fail: | |
| 510 | ✗ | av_frame_unref(dst); | |
| 511 | ✗ | return ret; | |
| 512 | } | ||
| 513 | |||
| 514 | 1403893 | static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, | |
| 515 | SyncQueueFrame frame) | ||
| 516 | { | ||
| 517 | 633283 | const SyncQueueStream *st_head = sq->head_stream >= 0 ? | |
| 518 | 1403893 | &sq->streams[sq->head_stream] : NULL; | |
| 519 | SyncQueueStream *st; | ||
| 520 | |||
| 521 | 1403893 | av_assert0(stream_idx < sq->nb_streams); | |
| 522 | 1403893 | st = &sq->streams[stream_idx]; | |
| 523 | |||
| 524 | 1403893 | if (av_container_fifo_can_read(st->fifo) && | |
| 525 | 935463 | (st->frame_samples <= st->samples_queued || st->finished)) { | |
| 526 | 177710 | int nb_samples = st->frame_samples; | |
| 527 | SyncQueueFrame peek; | ||
| 528 | 177710 | int64_t ts, end = 0, cut = 0; | |
| 529 | 177710 | int cmp = 1; | |
| 530 | |||
| 531 | 177710 | if (st->finished) | |
| 532 | 3931 | nb_samples = FFMIN(nb_samples, st->samples_queued); | |
| 533 | |||
| 534 | 177710 | av_container_fifo_peek(st->fifo, (void**)&peek, 0); | |
| 535 | 177710 | ts = frame_end(sq, peek, nb_samples); | |
| 536 | |||
| 537 | /* check if this stream's tail timestamp does not overtake | ||
| 538 | * the overall queue head */ | ||
| 539 | 177710 | if (ts != AV_NOPTS_VALUE && st_head) { | |
| 540 | 64895 | cmp = av_compare_ts(ts, st->tb, st_head->head_ts, st_head->tb); | |
| 541 | |||
| 542 | /* A frame ending after the final queue head is cut there when | ||
| 543 | * the encoder takes any number of samples, so that the stream | ||
| 544 | * does not end up to a frame before the limiting one. */ | ||
| 545 | 64909 | if (cmp > 0 && st_head->finished && !st->frame_samples && | |
| 546 | 14 | frame_samples(sq, peek)) { | |
| 547 | 7 | AVRational sample_tb = { 1, peek.f->sample_rate }; | |
| 548 | |||
| 549 | 7 | end = av_rescale_q(st_head->head_ts, st_head->tb, st->tb); | |
| 550 | 7 | cut = av_rescale_q(st_head->head_ts, st_head->tb, sample_tb) - | |
| 551 | 7 | av_rescale_q(peek.f->pts, st->tb, sample_tb); | |
| 552 | 7 | cut = FFMIN(cut, peek.f->nb_samples); | |
| 553 | } | ||
| 554 | |||
| 555 | /* Allow the nearest tick for cut audio if that tick is no longer | ||
| 556 | * than a sample. A coarse packet time base can otherwise admit a | ||
| 557 | * whole packet that the frame queue released without cutting. */ | ||
| 558 | 64896 | if (cmp > 0 && st_head->finished && st->cut_sample_rate && | |
| 559 | 1 | av_cmp_q(st->tb, (AVRational){ 1, st->cut_sample_rate }) <= 0) | |
| 560 | 1 | cmp = ts > av_rescale_q(st_head->head_ts, st_head->tb, st->tb); | |
| 561 | } | ||
| 562 | |||
| 563 | /* We can release frames that do not end after the queue head. | ||
| 564 | * Frames with no timestamps are just passed through with no conditions. | ||
| 565 | * Frames are also passed through when there are no limiting streams. | ||
| 566 | */ | ||
| 567 | 177710 | if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting || cut > 0) { | |
| 568 | 134046 | if (nb_samples && | |
| 569 | 218951 | (nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) { | |
| 570 | 105817 | int ret = receive_samples(sq, st, frame.f, nb_samples); | |
| 571 | 105817 | if (ret < 0) | |
| 572 | ✗ | return ret; | |
| 573 | } else { | ||
| 574 | 28229 | int ret = av_container_fifo_read(st->fifo, SQPTR(sq, frame), 0); | |
| 575 | 28229 | av_assert0(ret >= 0); | |
| 576 | |||
| 577 | 28229 | av_assert0(st->samples_queued >= frame_samples(sq, frame)); | |
| 578 | 28229 | st->samples_queued -= frame_samples(sq, frame); | |
| 579 | |||
| 580 | 28229 | if (cut > 0) { | |
| 581 | 7 | frame.f->nb_samples = cut; | |
| 582 | 7 | frame.f->duration = end - frame.f->pts; | |
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | 246802 | av_log(sq->logctx, AV_LOG_DEBUG, | |
| 587 | "sq: receive %u ts %s queue head %d ts %s\n", stream_idx, | ||
| 588 | 134046 | av_ts2timestr(frame_end(sq, frame, 0), &st->tb), | |
| 589 | sq->head_stream, | ||
| 590 | 134046 | st_head ? av_ts2timestr(st_head->head_ts, &st_head->tb) : "N/A"); | |
| 591 | |||
| 592 | 134046 | return 0; | |
| 593 | } | ||
| 594 | } | ||
| 595 | |||
| 596 | 1248483 | return (sq->finished || (st->finished && !av_container_fifo_can_read(st->fifo))) ? | |
| 597 | 2518330 | AVERROR_EOF : AVERROR(EAGAIN); | |
| 598 | } | ||
| 599 | |||
| 600 | 558228 | static int receive_internal(SyncQueue *sq, int stream_idx, SyncQueueFrame frame) | |
| 601 | { | ||
| 602 | 558228 | int nb_eof = 0; | |
| 603 | int ret; | ||
| 604 | |||
| 605 | /* read a frame for a specific stream */ | ||
| 606 | 558228 | if (stream_idx >= 0) { | |
| 607 | ✗ | ret = receive_for_stream(sq, stream_idx, frame); | |
| 608 | ✗ | return (ret < 0) ? ret : stream_idx; | |
| 609 | } | ||
| 610 | |||
| 611 | /* read a frame for any stream with available output */ | ||
| 612 | 1828075 | for (unsigned int i = 0; i < sq->nb_streams; i++) { | |
| 613 | 1403893 | ret = receive_for_stream(sq, i, frame); | |
| 614 | 1403893 | if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) { | |
| 615 | 1269847 | nb_eof += (ret == AVERROR_EOF); | |
| 616 | 1269847 | continue; | |
| 617 | } | ||
| 618 | 134046 | return (ret < 0) ? ret : i; | |
| 619 | } | ||
| 620 | |||
| 621 | 424182 | return (nb_eof == sq->nb_streams) ? AVERROR_EOF : AVERROR(EAGAIN); | |
| 622 | } | ||
| 623 | |||
| 624 | 558090 | int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame) | |
| 625 | { | ||
| 626 | 558090 | int ret = receive_internal(sq, stream_idx, frame); | |
| 627 | |||
| 628 | /* try again if the queue overflowed and triggered a fake heartbeat | ||
| 629 | * for lagging streams */ | ||
| 630 | 558090 | if (ret == AVERROR(EAGAIN) && overflow_heartbeat(sq, stream_idx)) | |
| 631 | 138 | ret = receive_internal(sq, stream_idx, frame); | |
| 632 | |||
| 633 | 558090 | return ret; | |
| 634 | } | ||
| 635 | |||
| 636 | 3742 | int sq_add_stream(SyncQueue *sq, int limiting) | |
| 637 | { | ||
| 638 | SyncQueueStream *tmp, *st; | ||
| 639 | |||
| 640 | 3742 | tmp = av_realloc_array(sq->streams, sq->nb_streams + 1, sizeof(*sq->streams)); | |
| 641 | 3742 | if (!tmp) | |
| 642 | ✗ | return AVERROR(ENOMEM); | |
| 643 | 3742 | sq->streams = tmp; | |
| 644 | |||
| 645 | 3742 | st = &sq->streams[sq->nb_streams]; | |
| 646 | 3742 | memset(st, 0, sizeof(*st)); | |
| 647 | |||
| 648 | 7484 | st->fifo = (sq->type == SYNC_QUEUE_FRAMES) ? | |
| 649 | 3742 | av_container_fifo_alloc_avframe(0) : av_container_fifo_alloc_avpacket(0); | |
| 650 | 3742 | if (!st->fifo) | |
| 651 | ✗ | return AVERROR(ENOMEM); | |
| 652 | |||
| 653 | /* we set a valid default, so that a pathological stream that never | ||
| 654 | * receives even a real timebase (and no frames) won't stall all other | ||
| 655 | * streams forever; cf. overflow_heartbeat() */ | ||
| 656 | 3742 | st->tb = (AVRational){ 1, 1 }; | |
| 657 | 3742 | st->head_ts = AV_NOPTS_VALUE; | |
| 658 | 3742 | st->frames_max = UINT64_MAX; | |
| 659 | 3742 | st->limiting = limiting; | |
| 660 | |||
| 661 | 3742 | sq->have_limiting |= limiting; | |
| 662 | |||
| 663 | 3742 | return sq->nb_streams++; | |
| 664 | } | ||
| 665 | |||
| 666 | 3320 | void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames) | |
| 667 | { | ||
| 668 | SyncQueueStream *st; | ||
| 669 | |||
| 670 | 3320 | av_assert0(stream_idx < sq->nb_streams); | |
| 671 | 3320 | st = &sq->streams[stream_idx]; | |
| 672 | |||
| 673 | 3320 | st->frames_max = frames; | |
| 674 | 3320 | if (st->frames_sent >= st->frames_max) | |
| 675 | 63 | finish_stream(sq, stream_idx); | |
| 676 | 3320 | } | |
| 677 | |||
| 678 | 1 | void sq_cut_upstream(SyncQueue *sq, unsigned int stream_idx, int sample_rate) | |
| 679 | { | ||
| 680 | 1 | av_assert0(sq->type == SYNC_QUEUE_PACKETS); | |
| 681 | 1 | av_assert0(stream_idx < sq->nb_streams); | |
| 682 | 1 | av_assert0(sample_rate > 0); | |
| 683 | 1 | sq->streams[stream_idx].cut_sample_rate = sample_rate; | |
| 684 | 1 | } | |
| 685 | |||
| 686 | 390 | void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx, | |
| 687 | int frame_samples) | ||
| 688 | { | ||
| 689 | SyncQueueStream *st; | ||
| 690 | |||
| 691 | 390 | av_assert0(sq->type == SYNC_QUEUE_FRAMES); | |
| 692 | 390 | av_assert0(stream_idx < sq->nb_streams); | |
| 693 | 390 | st = &sq->streams[stream_idx]; | |
| 694 | |||
| 695 | 390 | st->frame_samples = frame_samples; | |
| 696 | |||
| 697 | 390 | sq->align_mask = av_cpu_max_align() - 1; | |
| 698 | 390 | } | |
| 699 | |||
| 700 | 3499 | SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx) | |
| 701 | { | ||
| 702 | 3499 | SyncQueue *sq = av_mallocz(sizeof(*sq)); | |
| 703 | |||
| 704 | 3499 | if (!sq) | |
| 705 | ✗ | return NULL; | |
| 706 | |||
| 707 | 3499 | sq->type = type; | |
| 708 | 3499 | sq->buf_size_us = buf_size_us; | |
| 709 | 3499 | sq->logctx = logctx; | |
| 710 | |||
| 711 | 3499 | sq->head_stream = -1; | |
| 712 | 3499 | sq->head_finished_stream = -1; | |
| 713 | |||
| 714 | 3499 | return sq; | |
| 715 | } | ||
| 716 | |||
| 717 | 12878 | void sq_free(SyncQueue **psq) | |
| 718 | { | ||
| 719 | 12878 | SyncQueue *sq = *psq; | |
| 720 | |||
| 721 | 12878 | if (!sq) | |
| 722 | 9379 | return; | |
| 723 | |||
| 724 | 7241 | for (unsigned int i = 0; i < sq->nb_streams; i++) | |
| 725 | 3742 | av_container_fifo_free(&sq->streams[i].fifo); | |
| 726 | |||
| 727 | 3499 | av_freep(&sq->streams); | |
| 728 | |||
| 729 | 3499 | av_freep(psq); | |
| 730 | } | ||
| 731 |