audio_frame_queue.c
Go to the documentation of this file.
1 /*
2  * Audio Frame Queue
3  * Copyright (c) 2012 Justin Ruggles
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "audio_frame_queue.h"
24 #include "internal.h"
25 #include "libavutil/avassert.h"
26 
28 {
29  afq->avctx = avctx;
30  afq->remaining_delay = avctx->delay;
31  afq->remaining_samples = avctx->delay;
32  afq->frame_count = 0;
33 }
34 
36 {
37  if(afq->frame_count)
38  av_log(afq->avctx, AV_LOG_WARNING, "%d frames left in the queue on closing\n", afq->frame_count);
39  av_freep(&afq->frames);
40  memset(afq, 0, sizeof(*afq));
41 }
42 
44 {
45  AudioFrame *new = av_fast_realloc(afq->frames, &afq->frame_alloc, sizeof(*afq->frames)*(afq->frame_count+1));
46  if(!new)
47  return AVERROR(ENOMEM);
48  afq->frames = new;
49  new += afq->frame_count;
50 
51  /* get frame parameters */
52  new->duration = f->nb_samples;
53  new->duration += afq->remaining_delay;
54  if (f->pts != AV_NOPTS_VALUE) {
55  new->pts = av_rescale_q(f->pts,
56  afq->avctx->time_base,
57  (AVRational){ 1, afq->avctx->sample_rate });
58  new->pts -= afq->remaining_delay;
59  if(afq->frame_count && new[-1].pts >= new->pts)
60  av_log(afq->avctx, AV_LOG_WARNING, "Queue input is backward in time\n");
61  } else {
62  new->pts = AV_NOPTS_VALUE;
63  }
64  afq->remaining_delay = 0;
65 
66  /* add frame sample count */
67  afq->remaining_samples += f->nb_samples;
68 
69  afq->frame_count++;
70 
71  return 0;
72 }
73 
74 void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts,
75  int *duration)
76 {
77  int64_t out_pts = AV_NOPTS_VALUE;
78  int removed_samples = 0;
79  int i;
80 
81  if (afq->frame_count || afq->frame_alloc) {
82  if (afq->frames->pts != AV_NOPTS_VALUE)
83  out_pts = afq->frames->pts;
84  }
85  if(!afq->frame_count)
86  av_log(afq->avctx, AV_LOG_WARNING, "Trying to remove %d samples, but the queue is empty\n", nb_samples);
87  if (pts)
88  *pts = ff_samples_to_time_base(afq->avctx, out_pts);
89 
90  for(i=0; nb_samples && i<afq->frame_count; i++){
91  int n= FFMIN(afq->frames[i].duration, nb_samples);
92  afq->frames[i].duration -= n;
93  nb_samples -= n;
94  removed_samples += n;
95  if(afq->frames[i].pts != AV_NOPTS_VALUE)
96  afq->frames[i].pts += n;
97  }
98  afq->remaining_samples -= removed_samples;
99  i -= i && afq->frames[i-1].duration;
100  memmove(afq->frames, afq->frames + i, sizeof(*afq->frames) * (afq->frame_count - i));
101  afq->frame_count -= i;
102 
103  if(nb_samples){
104  av_assert0(!afq->frame_count);
106  if(afq->frames && afq->frames[0].pts != AV_NOPTS_VALUE)
107  afq->frames[0].pts += nb_samples;
108  av_log(afq->avctx, AV_LOG_DEBUG, "Trying to remove %d more samples than there are in the queue\n", nb_samples);
109  }
110  if (duration)
111  *duration = ff_samples_to_time_base(afq->avctx, removed_samples);
112 }
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
AudioFrame * frames
Sinusoidal phase f
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
static int64_t duration
Definition: ffplay.c:294
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
AVCodecContext * avctx
#define FFMIN(a, b)
Definition: common.h:58
int nb_samples
number of samples currently in the FIFO
Definition: audio_fifo.c:37
main external API structure.
synthesis window for stochastic i
rational number numerator/denominator
Definition: rational.h:43
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
common internal and external API header
void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int *duration)
Remove frame(s) from the queue.
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
int delay
Codec delay.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190