3 * Copyright (c) 2012 Justin Ruggles
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/mathematics.h"
24 #include "audio_frame_queue.h"
26 void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
29 afq->next_pts = AV_NOPTS_VALUE;
30 afq->remaining_delay = avctx->delay;
31 afq->remaining_samples = avctx->delay;
32 afq->frame_queue = NULL;
35 static void delete_next_frame(AudioFrameQueue *afq)
37 AudioFrame *f = afq->frame_queue;
39 afq->frame_queue = f->next;
45 void ff_af_queue_close(AudioFrameQueue *afq)
47 /* remove/free any remaining frames */
48 while (afq->frame_queue)
49 delete_next_frame(afq);
50 memset(afq, 0, sizeof(*afq));
53 int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
55 AudioFrame *new_frame;
56 AudioFrame *queue_end = afq->frame_queue;
58 /* find the end of the queue */
59 while (queue_end && queue_end->next)
60 queue_end = queue_end->next;
62 /* allocate new frame queue entry */
63 if (!(new_frame = av_malloc(sizeof(*new_frame))))
64 return AVERROR(ENOMEM);
66 /* get frame parameters */
67 new_frame->next = NULL;
68 new_frame->duration = f->nb_samples;
69 if (f->pts != AV_NOPTS_VALUE) {
70 new_frame->pts = av_rescale_q(f->pts,
71 afq->avctx->time_base,
72 (AVRational){ 1, afq->avctx->sample_rate });
73 afq->next_pts = new_frame->pts + new_frame->duration;
75 new_frame->pts = AV_NOPTS_VALUE;
76 afq->next_pts = AV_NOPTS_VALUE;
79 /* add new frame to the end of the queue */
81 afq->frame_queue = new_frame;
83 queue_end->next = new_frame;
85 /* add frame sample count */
86 afq->remaining_samples += f->nb_samples;
89 ff_af_queue_log_state(afq);
95 void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts,
98 int64_t out_pts = AV_NOPTS_VALUE;
99 int removed_samples = 0;
102 ff_af_queue_log_state(afq);
105 /* get output pts from the next frame or generated pts */
106 if (afq->frame_queue) {
107 if (afq->frame_queue->pts != AV_NOPTS_VALUE)
108 out_pts = afq->frame_queue->pts - afq->remaining_delay;
110 if (afq->next_pts != AV_NOPTS_VALUE)
111 out_pts = afq->next_pts - afq->remaining_delay;
114 if (out_pts != AV_NOPTS_VALUE)
115 *pts = ff_samples_to_time_base(afq->avctx, out_pts);
117 *pts = AV_NOPTS_VALUE;
120 /* if the delay is larger than the packet duration, we use up delay samples
121 for the output packet and leave all frames in the queue */
122 if (afq->remaining_delay >= nb_samples) {
123 removed_samples += nb_samples;
124 afq->remaining_delay -= nb_samples;
126 /* remove frames from the queue until we have enough to cover the
127 requested number of samples or until the queue is empty */
128 while (removed_samples < nb_samples && afq->frame_queue) {
129 removed_samples += afq->frame_queue->duration;
130 delete_next_frame(afq);
132 afq->remaining_samples -= removed_samples;
134 /* if there are no frames left and we have room for more samples, use
135 any remaining delay samples */
136 if (removed_samples < nb_samples && afq->remaining_samples > 0) {
137 int add_samples = FFMIN(afq->remaining_samples,
138 nb_samples - removed_samples);
139 removed_samples += add_samples;
140 afq->remaining_samples -= add_samples;
142 if (removed_samples > nb_samples)
143 av_log(afq->avctx, AV_LOG_WARNING, "frame_size is too large\n");
145 *duration = ff_samples_to_time_base(afq->avctx, removed_samples);
148 void ff_af_queue_log_state(AudioFrameQueue *afq)
151 av_log(afq->avctx, AV_LOG_DEBUG, "remaining delay = %d\n",
152 afq->remaining_delay);
153 av_log(afq->avctx, AV_LOG_DEBUG, "remaining samples = %d\n",
154 afq->remaining_samples);
155 av_log(afq->avctx, AV_LOG_DEBUG, "frames:\n");
156 f = afq->frame_queue;
158 av_log(afq->avctx, AV_LOG_DEBUG, " [ pts=%9"PRId64" duration=%d ]\n",
159 f->pts, f->duration);