]> git.sesse.net Git - ffmpeg/blob - libavcodec/audio_frame_queue.c
Don't include common.h from avutil.h
[ffmpeg] / libavcodec / audio_frame_queue.c
1 /*
2  * Audio Frame Queue
3  * Copyright (c) 2012 Justin Ruggles
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/common.h"
23 #include "libavutil/mathematics.h"
24 #include "internal.h"
25 #include "audio_frame_queue.h"
26
27 void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
28 {
29     afq->avctx             = avctx;
30     afq->next_pts          = AV_NOPTS_VALUE;
31     afq->remaining_delay   = avctx->delay;
32     afq->remaining_samples = avctx->delay;
33     afq->frame_queue       = NULL;
34 }
35
36 static void delete_next_frame(AudioFrameQueue *afq)
37 {
38     AudioFrame *f = afq->frame_queue;
39     if (f) {
40         afq->frame_queue = f->next;
41         f->next = NULL;
42         av_freep(&f);
43     }
44 }
45
46 void ff_af_queue_close(AudioFrameQueue *afq)
47 {
48     /* remove/free any remaining frames */
49     while (afq->frame_queue)
50         delete_next_frame(afq);
51     memset(afq, 0, sizeof(*afq));
52 }
53
54 int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
55 {
56     AudioFrame *new_frame;
57     AudioFrame *queue_end = afq->frame_queue;
58
59     /* find the end of the queue */
60     while (queue_end && queue_end->next)
61         queue_end = queue_end->next;
62
63     /* allocate new frame queue entry */
64     if (!(new_frame = av_malloc(sizeof(*new_frame))))
65         return AVERROR(ENOMEM);
66
67     /* get frame parameters */
68     new_frame->next = NULL;
69     new_frame->duration = f->nb_samples;
70     if (f->pts != AV_NOPTS_VALUE) {
71         new_frame->pts = av_rescale_q(f->pts,
72                                       afq->avctx->time_base,
73                                       (AVRational){ 1, afq->avctx->sample_rate });
74         afq->next_pts = new_frame->pts + new_frame->duration;
75     } else {
76         new_frame->pts = AV_NOPTS_VALUE;
77         afq->next_pts  = AV_NOPTS_VALUE;
78     }
79
80     /* add new frame to the end of the queue */
81     if (!queue_end)
82         afq->frame_queue = new_frame;
83     else
84         queue_end->next = new_frame;
85
86     /* add frame sample count */
87     afq->remaining_samples += f->nb_samples;
88
89 #ifdef DEBUG
90     ff_af_queue_log_state(afq);
91 #endif
92
93     return 0;
94 }
95
96 void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts,
97                         int *duration)
98 {
99     int64_t out_pts = AV_NOPTS_VALUE;
100     int removed_samples = 0;
101
102 #ifdef DEBUG
103     ff_af_queue_log_state(afq);
104 #endif
105
106     /* get output pts from the next frame or generated pts */
107     if (afq->frame_queue) {
108         if (afq->frame_queue->pts != AV_NOPTS_VALUE)
109             out_pts = afq->frame_queue->pts - afq->remaining_delay;
110     } else {
111         if (afq->next_pts != AV_NOPTS_VALUE)
112             out_pts = afq->next_pts - afq->remaining_delay;
113     }
114     if (pts) {
115         if (out_pts != AV_NOPTS_VALUE)
116             *pts = ff_samples_to_time_base(afq->avctx, out_pts);
117         else
118             *pts = AV_NOPTS_VALUE;
119     }
120
121     /* if the delay is larger than the packet duration, we use up delay samples
122        for the output packet and leave all frames in the queue */
123     if (afq->remaining_delay >= nb_samples) {
124         removed_samples      += nb_samples;
125         afq->remaining_delay -= nb_samples;
126     }
127     /* remove frames from the queue until we have enough to cover the
128        requested number of samples or until the queue is empty */
129     while (removed_samples < nb_samples && afq->frame_queue) {
130         removed_samples += afq->frame_queue->duration;
131         delete_next_frame(afq);
132     }
133     afq->remaining_samples -= removed_samples;
134
135     /* if there are no frames left and we have room for more samples, use
136        any remaining delay samples */
137     if (removed_samples < nb_samples && afq->remaining_samples > 0) {
138         int add_samples = FFMIN(afq->remaining_samples,
139                                 nb_samples - removed_samples);
140         removed_samples        += add_samples;
141         afq->remaining_samples -= add_samples;
142     }
143     if (removed_samples > nb_samples)
144         av_log(afq->avctx, AV_LOG_WARNING, "frame_size is too large\n");
145     if (duration)
146         *duration = ff_samples_to_time_base(afq->avctx, removed_samples);
147 }
148
149 void ff_af_queue_log_state(AudioFrameQueue *afq)
150 {
151     AudioFrame *f;
152     av_log(afq->avctx, AV_LOG_DEBUG, "remaining delay   = %d\n",
153            afq->remaining_delay);
154     av_log(afq->avctx, AV_LOG_DEBUG, "remaining samples = %d\n",
155            afq->remaining_samples);
156     av_log(afq->avctx, AV_LOG_DEBUG, "frames:\n");
157     f = afq->frame_queue;
158     while (f) {
159         av_log(afq->avctx, AV_LOG_DEBUG, "  [ pts=%9"PRId64" duration=%d ]\n",
160                f->pts, f->duration);
161         f = f->next;
162     }
163 }