annotate ffmpeg/libavformat/audiointerleave.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 f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Audio Interleaving functions
yading@11 3 *
yading@11 4 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
yading@11 5 *
yading@11 6 * This file is part of FFmpeg.
yading@11 7 *
yading@11 8 * FFmpeg is free software; you can redistribute it and/or
yading@11 9 * modify it under the terms of the GNU Lesser General Public
yading@11 10 * License as published by the Free Software Foundation; either
yading@11 11 * version 2.1 of the License, or (at your option) any later version.
yading@11 12 *
yading@11 13 * FFmpeg is distributed in the hope that it will be useful,
yading@11 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 16 * Lesser General Public License for more details.
yading@11 17 *
yading@11 18 * You should have received a copy of the GNU Lesser General Public
yading@11 19 * License along with FFmpeg; if not, write to the Free Software
yading@11 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 21 */
yading@11 22
yading@11 23 #include "libavutil/fifo.h"
yading@11 24 #include "libavutil/mathematics.h"
yading@11 25 #include "avformat.h"
yading@11 26 #include "audiointerleave.h"
yading@11 27 #include "internal.h"
yading@11 28
yading@11 29 void ff_audio_interleave_close(AVFormatContext *s)
yading@11 30 {
yading@11 31 int i;
yading@11 32 for (i = 0; i < s->nb_streams; i++) {
yading@11 33 AVStream *st = s->streams[i];
yading@11 34 AudioInterleaveContext *aic = st->priv_data;
yading@11 35
yading@11 36 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
yading@11 37 av_fifo_free(aic->fifo);
yading@11 38 }
yading@11 39 }
yading@11 40
yading@11 41 int ff_audio_interleave_init(AVFormatContext *s,
yading@11 42 const int *samples_per_frame,
yading@11 43 AVRational time_base)
yading@11 44 {
yading@11 45 int i;
yading@11 46
yading@11 47 if (!samples_per_frame)
yading@11 48 return -1;
yading@11 49
yading@11 50 if (!time_base.num) {
yading@11 51 av_log(s, AV_LOG_ERROR, "timebase not set for audio interleave\n");
yading@11 52 return -1;
yading@11 53 }
yading@11 54 for (i = 0; i < s->nb_streams; i++) {
yading@11 55 AVStream *st = s->streams[i];
yading@11 56 AudioInterleaveContext *aic = st->priv_data;
yading@11 57
yading@11 58 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
yading@11 59 aic->sample_size = (st->codec->channels *
yading@11 60 av_get_bits_per_sample(st->codec->codec_id)) / 8;
yading@11 61 if (!aic->sample_size) {
yading@11 62 av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
yading@11 63 return -1;
yading@11 64 }
yading@11 65 aic->samples_per_frame = samples_per_frame;
yading@11 66 aic->samples = aic->samples_per_frame;
yading@11 67 aic->time_base = time_base;
yading@11 68
yading@11 69 aic->fifo_size = 100* *aic->samples;
yading@11 70 aic->fifo= av_fifo_alloc(100 * *aic->samples);
yading@11 71 }
yading@11 72 }
yading@11 73
yading@11 74 return 0;
yading@11 75 }
yading@11 76
yading@11 77 static int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
yading@11 78 int stream_index, int flush)
yading@11 79 {
yading@11 80 AVStream *st = s->streams[stream_index];
yading@11 81 AudioInterleaveContext *aic = st->priv_data;
yading@11 82
yading@11 83 int size = FFMIN(av_fifo_size(aic->fifo), *aic->samples * aic->sample_size);
yading@11 84 if (!size || (!flush && size == av_fifo_size(aic->fifo)))
yading@11 85 return 0;
yading@11 86
yading@11 87 if (av_new_packet(pkt, size) < 0)
yading@11 88 return AVERROR(ENOMEM);
yading@11 89 av_fifo_generic_read(aic->fifo, pkt->data, size, NULL);
yading@11 90
yading@11 91 pkt->dts = pkt->pts = aic->dts;
yading@11 92 pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
yading@11 93 pkt->stream_index = stream_index;
yading@11 94 aic->dts += pkt->duration;
yading@11 95
yading@11 96 aic->samples++;
yading@11 97 if (!*aic->samples)
yading@11 98 aic->samples = aic->samples_per_frame;
yading@11 99
yading@11 100 return size;
yading@11 101 }
yading@11 102
yading@11 103 int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
yading@11 104 int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
yading@11 105 int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
yading@11 106 {
yading@11 107 int i;
yading@11 108
yading@11 109 if (pkt) {
yading@11 110 AVStream *st = s->streams[pkt->stream_index];
yading@11 111 AudioInterleaveContext *aic = st->priv_data;
yading@11 112 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
yading@11 113 unsigned new_size = av_fifo_size(aic->fifo) + pkt->size;
yading@11 114 if (new_size > aic->fifo_size) {
yading@11 115 if (av_fifo_realloc2(aic->fifo, new_size) < 0)
yading@11 116 return -1;
yading@11 117 aic->fifo_size = new_size;
yading@11 118 }
yading@11 119 av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL);
yading@11 120 } else {
yading@11 121 int ret;
yading@11 122 // rewrite pts and dts to be decoded time line position
yading@11 123 pkt->pts = pkt->dts = aic->dts;
yading@11 124 aic->dts += pkt->duration;
yading@11 125 ret = ff_interleave_add_packet(s, pkt, compare_ts);
yading@11 126 if (ret < 0)
yading@11 127 return ret;
yading@11 128 }
yading@11 129 pkt = NULL;
yading@11 130 }
yading@11 131
yading@11 132 for (i = 0; i < s->nb_streams; i++) {
yading@11 133 AVStream *st = s->streams[i];
yading@11 134 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
yading@11 135 AVPacket new_pkt;
yading@11 136 int ret;
yading@11 137 while ((ret = ff_interleave_new_audio_packet(s, &new_pkt, i, flush)) > 0) {
yading@11 138 ret = ff_interleave_add_packet(s, &new_pkt, compare_ts);
yading@11 139 if (ret < 0)
yading@11 140 return ret;
yading@11 141 }
yading@11 142 if (ret < 0)
yading@11 143 return ret;
yading@11 144 }
yading@11 145 }
yading@11 146
yading@11 147 return get_packet(s, out, NULL, flush);
yading@11 148 }