00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "audio_frame_queue.h"
00023 #include "internal.h"
00024 #include "libavutil/avassert.h"
00025
00026 void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
00027 {
00028 afq->avctx = avctx;
00029 afq->remaining_delay = avctx->delay;
00030 afq->remaining_samples = avctx->delay;
00031 afq->frame_count = 0;
00032 }
00033
00034 void ff_af_queue_close(AudioFrameQueue *afq)
00035 {
00036 if(afq->frame_count)
00037 av_log(afq->avctx, AV_LOG_WARNING, "%d frames left in que on closing\n", afq->frame_count);
00038 av_freep(&afq->frames);
00039 memset(afq, 0, sizeof(*afq));
00040 }
00041
00042 int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
00043 {
00044 AudioFrame *new = av_fast_realloc(afq->frames, &afq->frame_alloc, sizeof(*afq->frames)*(afq->frame_count+1));
00045 if(!new)
00046 return AVERROR(ENOMEM);
00047 afq->frames = new;
00048 new += afq->frame_count;
00049
00050
00051 new->duration = f->nb_samples;
00052 new->duration += afq->remaining_delay;
00053 if (f->pts != AV_NOPTS_VALUE) {
00054 new->pts = av_rescale_q(f->pts,
00055 afq->avctx->time_base,
00056 (AVRational){ 1, afq->avctx->sample_rate });
00057 new->pts -= afq->remaining_delay;
00058 if(afq->frame_count && new[-1].pts >= new->pts)
00059 av_log(afq->avctx, AV_LOG_WARNING, "Que input is backward in time\n");
00060 } else {
00061 new->pts = AV_NOPTS_VALUE;
00062 }
00063 afq->remaining_delay = 0;
00064
00065
00066 afq->remaining_samples += f->nb_samples;
00067
00068 afq->frame_count++;
00069
00070 return 0;
00071 }
00072
00073 void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts,
00074 int *duration)
00075 {
00076 int64_t out_pts = AV_NOPTS_VALUE;
00077 int removed_samples = 0;
00078 int i;
00079
00080 if (afq->frame_count || afq->frame_alloc) {
00081 if (afq->frames->pts != AV_NOPTS_VALUE)
00082 out_pts = afq->frames->pts;
00083 }
00084 if(!afq->frame_count)
00085 av_log(afq->avctx, AV_LOG_WARNING, "Trying to remove %d samples, but que empty\n", nb_samples);
00086 if (pts)
00087 *pts = ff_samples_to_time_base(afq->avctx, out_pts);
00088
00089 for(i=0; nb_samples && i<afq->frame_count; i++){
00090 int n= FFMIN(afq->frames[i].duration, nb_samples);
00091 afq->frames[i].duration -= n;
00092 nb_samples -= n;
00093 removed_samples += n;
00094 if(afq->frames[i].pts != AV_NOPTS_VALUE)
00095 afq->frames[i].pts += n;
00096 }
00097 i -= i && afq->frames[i-1].duration;
00098 memmove(afq->frames, afq->frames + i, sizeof(*afq->frames) * (afq->frame_count - i));
00099 afq->frame_count -= i;
00100
00101 if(nb_samples){
00102 av_assert0(!afq->frame_count);
00103 if(afq->frames && afq->frames[0].pts != AV_NOPTS_VALUE)
00104 afq->frames[0].pts += nb_samples;
00105 av_log(afq->avctx, AV_LOG_DEBUG, "Trying to remove %d more samples than are in the que\n", nb_samples);
00106 }
00107 if (duration)
00108 *duration = ff_samples_to_time_base(afq->avctx, removed_samples);
00109 }
00110