annotate ffmpeg/libavcodec/audio_frame_queue.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Audio Frame Queue
yading@10 3 * Copyright (c) 2012 Justin Ruggles
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include "libavutil/common.h"
yading@10 23 #include "audio_frame_queue.h"
yading@10 24 #include "internal.h"
yading@10 25 #include "libavutil/avassert.h"
yading@10 26
yading@10 27 void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
yading@10 28 {
yading@10 29 afq->avctx = avctx;
yading@10 30 afq->remaining_delay = avctx->delay;
yading@10 31 afq->remaining_samples = avctx->delay;
yading@10 32 afq->frame_count = 0;
yading@10 33 }
yading@10 34
yading@10 35 void ff_af_queue_close(AudioFrameQueue *afq)
yading@10 36 {
yading@10 37 if(afq->frame_count)
yading@10 38 av_log(afq->avctx, AV_LOG_WARNING, "%d frames left in the queue on closing\n", afq->frame_count);
yading@10 39 av_freep(&afq->frames);
yading@10 40 memset(afq, 0, sizeof(*afq));
yading@10 41 }
yading@10 42
yading@10 43 int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
yading@10 44 {
yading@10 45 AudioFrame *new = av_fast_realloc(afq->frames, &afq->frame_alloc, sizeof(*afq->frames)*(afq->frame_count+1));
yading@10 46 if(!new)
yading@10 47 return AVERROR(ENOMEM);
yading@10 48 afq->frames = new;
yading@10 49 new += afq->frame_count;
yading@10 50
yading@10 51 /* get frame parameters */
yading@10 52 new->duration = f->nb_samples;
yading@10 53 new->duration += afq->remaining_delay;
yading@10 54 if (f->pts != AV_NOPTS_VALUE) {
yading@10 55 new->pts = av_rescale_q(f->pts,
yading@10 56 afq->avctx->time_base,
yading@10 57 (AVRational){ 1, afq->avctx->sample_rate });
yading@10 58 new->pts -= afq->remaining_delay;
yading@10 59 if(afq->frame_count && new[-1].pts >= new->pts)
yading@10 60 av_log(afq->avctx, AV_LOG_WARNING, "Queue input is backward in time\n");
yading@10 61 } else {
yading@10 62 new->pts = AV_NOPTS_VALUE;
yading@10 63 }
yading@10 64 afq->remaining_delay = 0;
yading@10 65
yading@10 66 /* add frame sample count */
yading@10 67 afq->remaining_samples += f->nb_samples;
yading@10 68
yading@10 69 afq->frame_count++;
yading@10 70
yading@10 71 return 0;
yading@10 72 }
yading@10 73
yading@10 74 void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts,
yading@10 75 int *duration)
yading@10 76 {
yading@10 77 int64_t out_pts = AV_NOPTS_VALUE;
yading@10 78 int removed_samples = 0;
yading@10 79 int i;
yading@10 80
yading@10 81 if (afq->frame_count || afq->frame_alloc) {
yading@10 82 if (afq->frames->pts != AV_NOPTS_VALUE)
yading@10 83 out_pts = afq->frames->pts;
yading@10 84 }
yading@10 85 if(!afq->frame_count)
yading@10 86 av_log(afq->avctx, AV_LOG_WARNING, "Trying to remove %d samples, but the queue is empty\n", nb_samples);
yading@10 87 if (pts)
yading@10 88 *pts = ff_samples_to_time_base(afq->avctx, out_pts);
yading@10 89
yading@10 90 for(i=0; nb_samples && i<afq->frame_count; i++){
yading@10 91 int n= FFMIN(afq->frames[i].duration, nb_samples);
yading@10 92 afq->frames[i].duration -= n;
yading@10 93 nb_samples -= n;
yading@10 94 removed_samples += n;
yading@10 95 if(afq->frames[i].pts != AV_NOPTS_VALUE)
yading@10 96 afq->frames[i].pts += n;
yading@10 97 }
yading@10 98 afq->remaining_samples -= removed_samples;
yading@10 99 i -= i && afq->frames[i-1].duration;
yading@10 100 memmove(afq->frames, afq->frames + i, sizeof(*afq->frames) * (afq->frame_count - i));
yading@10 101 afq->frame_count -= i;
yading@10 102
yading@10 103 if(nb_samples){
yading@10 104 av_assert0(!afq->frame_count);
yading@10 105 av_assert0(afq->remaining_samples == afq->remaining_delay);
yading@10 106 if(afq->frames && afq->frames[0].pts != AV_NOPTS_VALUE)
yading@10 107 afq->frames[0].pts += nb_samples;
yading@10 108 av_log(afq->avctx, AV_LOG_DEBUG, "Trying to remove %d more samples than there are in the queue\n", nb_samples);
yading@10 109 }
yading@10 110 if (duration)
yading@10 111 *duration = ff_samples_to_time_base(afq->avctx, removed_samples);
yading@10 112 }