This will be made useful in following commits.
Unified · Split
| @@ -20,10 +20,13 @@ · 19 unmodified lines | |||
| 20 | #include <string.h> | 20 | #include <string.h> |
| 21 | 21 | ||
| 22 | #include "libavutil/avassert.h" | 22 | #include "libavutil/avassert.h" |
| 23 | #include "libavutil/channel_layout.h" | ||
| 24 | #include "libavutil/cpu.h" | ||
| 23 | #include "libavutil/error.h" | 25 | #include "libavutil/error.h" |
| 24 | #include "libavutil/fifo.h" | 26 | #include "libavutil/fifo.h" |
| 25 | #include "libavutil/mathematics.h" | 27 | #include "libavutil/mathematics.h" |
| 26 | #include "libavutil/mem.h" | 28 | #include "libavutil/mem.h" |
| 29 | #include "libavutil/samplefmt.h" | ||
| 27 | 30 | ||
| 28 | #include "objpool.h" | 31 | #include "objpool.h" |
| 29 | #include "sync_queue.h" | 32 | #include "sync_queue.h" |
| @@ -67,6 +70,8 @@ typedef struct SyncQueueStream { · 37 unmodified lines | |||
| 67 | AVFifo *fifo; | 70 | AVFifo *fifo; |
| 68 | AVRational tb; | 71 | AVRational tb; |
| 69 | 72 | ||
| 73 | /* number of audio samples in fifo */ | ||
| 74 | uint64_t samples_queued; | ||
| 70 | /* stream head: largest timestamp seen */ | 75 | /* stream head: largest timestamp seen */ |
| 71 | int64_t head_ts; | 76 | int64_t head_ts; |
| 72 | int limiting; | 77 | int limiting; |
| @@ -74,7 +79,9 @@ typedef struct SyncQueueStream { · 1 unmodified line | |||
| 74 | int finished; | 79 | int finished; |
| 75 | 80 | ||
| 76 | uint64_t frames_sent; | 81 | uint64_t frames_sent; |
| 82 | uint64_t samples_sent; | ||
| 77 | uint64_t frames_max; | 83 | uint64_t frames_max; |
| 84 | int frame_samples; | ||
| 78 | } SyncQueueStream; | 85 | } SyncQueueStream; |
| 79 | 86 | ||
| 80 | struct SyncQueue { | 87 | struct SyncQueue { |
| @@ -98,6 +105,8 @@ struct SyncQueue { · 17 unmodified lines | |||
| 98 | ObjPool *pool; | 105 | ObjPool *pool; |
| 99 | 106 | ||
| 100 | int have_limiting; | 107 | int have_limiting; |
| 108 | |||
| 109 | uintptr_t align_mask; | ||
| 101 | }; | 110 | }; |
| 102 | 111 | ||
| 103 | static void frame_move(const SyncQueue *sq, SyncQueueFrame dst, | 112 | static void frame_move(const SyncQueue *sq, SyncQueueFrame dst, |
| @@ -109,8 +118,18 @@ static void frame_move(const SyncQueue *sq, SyncQueueFrame dst, · 5 unmodified lines | |||
| 109 | av_frame_move_ref(dst.f, src.f); | 118 | av_frame_move_ref(dst.f, src.f); |
| 110 | } | 119 | } |
| 111 | 120 | ||
| 112 | static int64_t frame_ts(const SyncQueue *sq, SyncQueueFrame frame) | 121 | /** |
| 122 | * Compute the end timestamp of a frame. If nb_samples is provided, consider | ||
| 123 | * the frame to have this number of audio samples, otherwise use frame duration. | ||
| 124 | */ | ||
| 125 | static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples) | ||
| 113 | { | 126 | { |
| 127 | if (nb_samples) { | ||
| 128 | int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate}, | ||
| 129 | frame.f->time_base); | ||
| 130 | return frame.f->pts + d; | ||
| 131 | } | ||
| 132 | |||
| 114 | return (sq->type == SYNC_QUEUE_PACKETS) ? | 133 | return (sq->type == SYNC_QUEUE_PACKETS) ? |
| 115 | frame.p->pts + frame.p->duration : | 134 | frame.p->pts + frame.p->duration : |
| 116 | frame.f->pts + frame.f->duration; | 135 | frame.f->pts + frame.f->duration; |
| @@ -265,7 +284,7 @@ static int overflow_heartbeat(SyncQueue *sq, int stream_idx) · 148 unmodified lines | |||
| 265 | /* get the chosen stream's tail timestamp */ | 284 | /* get the chosen stream's tail timestamp */ |
| 266 | for (size_t i = 0; tail_ts == AV_NOPTS_VALUE && | 285 | for (size_t i = 0; tail_ts == AV_NOPTS_VALUE && |
| 267 | av_fifo_peek(st->fifo, &frame, 1, i) >= 0; i++) | 286 | av_fifo_peek(st->fifo, &frame, 1, i) >= 0; i++) |
| 268 | tail_ts = frame_ts(sq, frame); | 287 | tail_ts = frame_end(sq, frame, 0); |
| 269 | 288 | ||
| 270 | /* overflow triggers when the tail is over specified duration behind the head */ | 289 | /* overflow triggers when the tail is over specified duration behind the head */ |
| 271 | if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts || | 290 | if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts || |
| @@ -326,7 +345,7 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) · 54 unmodified lines | |||
| 326 | dst.f->time_base); | 345 | dst.f->time_base); |
| 327 | } | 346 | } |
| 328 | 347 | ||
| 329 | ts = frame_ts(sq, dst); | 348 | ts = frame_end(sq, dst, 0); |
| 330 | 349 | ||
| 331 | ret = av_fifo_write(st->fifo, &dst, 1); | 350 | ret = av_fifo_write(st->fifo, &dst, 1); |
| 332 | if (ret < 0) { | 351 | if (ret < 0) { |
| @@ -337,13 +356,131 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) · 4 unmodified lines | |||
| 337 | 356 | ||
| 338 | stream_update_ts(sq, stream_idx, ts); | 357 | stream_update_ts(sq, stream_idx, ts); |
| 339 | 358 | ||
| 340 | st->frames_sent++; | 359 | st->samples_queued += nb_samples; |
| 360 | st->samples_sent += nb_samples; | ||
| 361 | |||
| 362 | if (st->frame_samples) | ||
| 363 | st->frames_sent = st->samples_sent / st->frame_samples; | ||
| 364 | else | ||
| 365 | st->frames_sent++; | ||
| 366 | |||
| 341 | if (st->frames_sent >= st->frames_max) | 367 | if (st->frames_sent >= st->frames_max) |
| 342 | finish_stream(sq, stream_idx); | 368 | finish_stream(sq, stream_idx); |
| 343 | 369 | ||
| 344 | return 0; | 370 | return 0; |
| 345 | } | 371 | } |
| 346 | 372 | ||
| 373 | static void offset_audio(AVFrame *f, int nb_samples) | ||
| 374 | { | ||
| 375 | const int planar = av_sample_fmt_is_planar(f->format); | ||
| 376 | const int planes = planar ? f->ch_layout.nb_channels : 1; | ||
| 377 | const int bps = av_get_bytes_per_sample(f->format); | ||
| 378 | const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels); | ||
| 379 | |||
| 380 | av_assert0(bps > 0); | ||
| 381 | av_assert0(nb_samples < f->nb_samples); | ||
| 382 | |||
| 383 | for (int i = 0; i < planes; i++) { | ||
| 384 | f->extended_data[i] += offset; | ||
| 385 | if (i < FF_ARRAY_ELEMS(f->data)) | ||
| 386 | f->data[i] = f->extended_data[i]; | ||
| 387 | } | ||
| 388 | f->linesize[0] -= offset; | ||
| 389 | f->nb_samples -= nb_samples; | ||
| 390 | f->duration = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate }, | ||
| 391 | f->time_base); | ||
| 392 | f->pts += av_rescale_q(nb_samples, (AVRational){ 1, f->sample_rate }, | ||
| 393 | f->time_base); | ||
| 394 | } | ||
| 395 | |||
| 396 | static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame) | ||
| 397 | { | ||
| 398 | // only checks linesize[0], so only works for audio | ||
| 399 | av_assert0(frame->nb_samples > 0); | ||
| 400 | av_assert0(sq->align_mask); | ||
| 401 | |||
| 402 | // only check data[0], because we always offset all data pointers | ||
| 403 | // by the same offset, so if one is aligned, all are | ||
| 404 | if (!((uintptr_t)frame->data[0] & sq->align_mask) && | ||
| 405 | !(frame->linesize[0] & sq->align_mask) && | ||
| 406 | frame->linesize[0] > sq->align_mask) | ||
| 407 | return 1; | ||
| 408 | |||
| 409 | return 0; | ||
| 410 | } | ||
| 411 | |||
| 412 | static int receive_samples(SyncQueue *sq, SyncQueueStream *st, | ||
| 413 | AVFrame *dst, int nb_samples) | ||
| 414 | { | ||
| 415 | SyncQueueFrame src; | ||
| 416 | int ret; | ||
| 417 | |||
| 418 | av_assert0(st->samples_queued >= nb_samples); | ||
| 419 | |||
| 420 | ret = av_fifo_peek(st->fifo, &src, 1, 0); | ||
| 421 | av_assert0(ret >= 0); | ||
| 422 | |||
| 423 | // peeked frame has enough samples and its data is aligned | ||
| 424 | // -> we can just make a reference and limit its sample count | ||
| 425 | if (src.f->nb_samples > nb_samples && frame_is_aligned(sq, src.f)) { | ||
| 426 | ret = av_frame_ref(dst, src.f); | ||
| 427 | if (ret < 0) | ||
| 428 | return ret; | ||
| 429 | |||
| 430 | dst->nb_samples = nb_samples; | ||
| 431 | offset_audio(src.f, nb_samples); | ||
| 432 | st->samples_queued -= nb_samples; | ||
| 433 | |||
| 434 | return 0; | ||
| 435 | } | ||
| 436 | |||
| 437 | // otherwise allocate a new frame and copy the data | ||
| 438 | ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout); | ||
| 439 | if (ret < 0) | ||
| 440 | return ret; | ||
| 441 | |||
| 442 | dst->format = src.f->format; | ||
| 443 | dst->nb_samples = nb_samples; | ||
| 444 | |||
| 445 | ret = av_frame_get_buffer(dst, 0); | ||
| 446 | if (ret < 0) | ||
| 447 | goto fail; | ||
| 448 | |||
| 449 | ret = av_frame_copy_props(dst, src.f); | ||
| 450 | if (ret < 0) | ||
| 451 | goto fail; | ||
| 452 | |||
| 453 | dst->nb_samples = 0; | ||
| 454 | while (dst->nb_samples < nb_samples) { | ||
| 455 | int to_copy; | ||
| 456 | |||
| 457 | ret = av_fifo_peek(st->fifo, &src, 1, 0); | ||
| 458 | av_assert0(ret >= 0); | ||
| 459 | |||
| 460 | to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples); | ||
| 461 | |||
| 462 | av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples, | ||
| 463 | 0, to_copy, dst->ch_layout.nb_channels, dst->format); | ||
| 464 | |||
| 465 | if (to_copy < src.f->nb_samples) | ||
| 466 | offset_audio(src.f, to_copy); | ||
| 467 | else { | ||
| 468 | av_frame_unref(src.f); | ||
| 469 | objpool_release(sq->pool, (void**)&src); | ||
| 470 | av_fifo_drain2(st->fifo, 1); | ||
| 471 | } | ||
| 472 | st->samples_queued -= to_copy; | ||
| 473 | |||
| 474 | dst->nb_samples += to_copy; | ||
| 475 | } | ||
| 476 | |||
| 477 | return 0; | ||
| 478 | |||
| 479 | fail: | ||
| 480 | av_frame_unref(dst); | ||
| 481 | return ret; | ||
| 482 | } | ||
| 483 | |||
| 347 | static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, | 484 | static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, |
| 348 | SyncQueueFrame frame) | 485 | SyncQueueFrame frame) |
| 349 | { | 486 | { |
| @@ -354,13 +491,18 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, · 4 unmodified lines | |||
| 354 | av_assert0(stream_idx < sq->nb_streams); | 491 | av_assert0(stream_idx < sq->nb_streams); |
| 355 | st = &sq->streams[stream_idx]; | 492 | st = &sq->streams[stream_idx]; |
| 356 | 493 | ||
| 357 | if (av_fifo_can_read(st->fifo)) { | 494 | if (av_fifo_can_read(st->fifo) && |
| 495 | (st->frame_samples <= st->samples_queued || st->finished)) { | ||
| 496 | int nb_samples = st->frame_samples; | ||
| 358 | SyncQueueFrame peek; | 497 | SyncQueueFrame peek; |
| 359 | int64_t ts; | 498 | int64_t ts; |
| 360 | int cmp = 1; | 499 | int cmp = 1; |
| 361 | 500 | ||
| 501 | if (st->finished) | ||
| 502 | nb_samples = FFMIN(nb_samples, st->samples_queued); | ||
| 503 | |||
| 362 | av_fifo_peek(st->fifo, &peek, 1, 0); | 504 | av_fifo_peek(st->fifo, &peek, 1, 0); |
| 363 | ts = frame_ts(sq, peek); | 505 | ts = frame_end(sq, peek, nb_samples); |
| 364 | 506 | ||
| 365 | /* check if this stream's tail timestamp does not overtake | 507 | /* check if this stream's tail timestamp does not overtake |
| 366 | * the overall queue head */ | 508 | * the overall queue head */ |
| @@ -372,9 +514,19 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx, · 5 unmodified lines | |||
| 372 | * Frames are also passed through when there are no limiting streams. | 514 | * Frames are also passed through when there are no limiting streams. |
| 373 | */ | 515 | */ |
| 374 | if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) { | 516 | if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) { |
| 375 | frame_move(sq, frame, peek); | 517 | if (nb_samples && |
| 376 | objpool_release(sq->pool, (void**)&peek); | 518 | (nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) { |
| 377 | av_fifo_drain2(st->fifo, 1); | 519 | int ret = receive_samples(sq, st, frame.f, nb_samples); |
| 520 | if (ret < 0) | ||
| 521 | return ret; | ||
| 522 | } else { | ||
| 523 | frame_move(sq, frame, peek); | ||
| 524 | objpool_release(sq->pool, (void**)&peek); | ||
| 525 | av_fifo_drain2(st->fifo, 1); | ||
| 526 | av_assert0(st->samples_queued >= frame_samples(sq, frame)); | ||
| 527 | st->samples_queued -= frame_samples(sq, frame); | ||
| 528 | } | ||
| 529 | |||
| 378 | return 0; | 530 | return 0; |
| 379 | } | 531 | } |
| 380 | } | 532 | } |
| @@ -460,6 +612,20 @@ void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames) · 79 unmodified lines | |||
| 460 | finish_stream(sq, stream_idx); | 612 | finish_stream(sq, stream_idx); |
| 461 | } | 613 | } |
| 462 | 614 | ||
| 615 | void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx, | ||
| 616 | int frame_samples) | ||
| 617 | { | ||
| 618 | SyncQueueStream *st; | ||
| 619 | |||
| 620 | av_assert0(sq->type == SYNC_QUEUE_FRAMES); | ||
| 621 | av_assert0(stream_idx < sq->nb_streams); | ||
| 622 | st = &sq->streams[stream_idx]; | ||
| 623 | |||
| 624 | st->frame_samples = frame_samples; | ||
| 625 | |||
| 626 | sq->align_mask = av_cpu_max_align() - 1; | ||
| 627 | } | ||
| 628 | |||
| 463 | SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us) | 629 | SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us) |
| 464 | { | 630 | { |
| 465 | SyncQueue *sq = av_mallocz(sizeof(*sq)); | 631 | SyncQueue *sq = av_mallocz(sizeof(*sq)); |
| @@ -71,6 +71,16 @@ int sq_add_stream(SyncQueue *sq, int limiting); · 70 unmodified lines | |||
| 71 | void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, | 71 | void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, |
| 72 | uint64_t max_frames); | 72 | uint64_t max_frames); |
| 73 | 73 | ||
| 74 | /** | ||
| 75 | * Set a constant output audio frame size, in samples. Can only be used with | ||
| 76 | * SYNC_QUEUE_FRAMES queues and audio streams. | ||
| 77 | * | ||
| 78 | * All output frames will have exactly frame_samples audio samples, except | ||
| 79 | * possibly for the last one, which may have fewer. | ||
| 80 | */ | ||
| 81 | void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx, | ||
| 82 | int frame_samples); | ||
| 83 | |||
| 74 | /** | 84 | /** |
| 75 | * Submit a frame for the stream with index stream_idx. | 85 | * Submit a frame for the stream with index stream_idx. |
| 76 | * | 86 | * |