2 * Audio Interleaving functions
4 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/fifo.h"
25 #include "audiointerleave.h"
27 void ff_audio_interleave_close(AVFormatContext *s)
30 for (i = 0; i < s->nb_streams; i++) {
31 AVStream *st = s->streams[i];
32 AudioInterleaveContext *aic = st->priv_data;
34 if (st->codec->codec_type == CODEC_TYPE_AUDIO)
35 av_fifo_free(&aic->fifo);
39 int ff_audio_interleave_init(AVFormatContext *s,
40 const int *samples_per_frame,
45 if (!samples_per_frame)
48 for (i = 0; i < s->nb_streams; i++) {
49 AVStream *st = s->streams[i];
50 AudioInterleaveContext *aic = st->priv_data;
52 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
53 aic->sample_size = (st->codec->channels *
54 av_get_bits_per_sample(st->codec->codec_id)) / 8;
55 if (!aic->sample_size) {
56 av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
59 aic->samples_per_frame = samples_per_frame;
60 aic->samples = aic->samples_per_frame;
61 aic->time_base = time_base;
63 av_fifo_init(&aic->fifo, 100 * *aic->samples);
70 int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
71 int stream_index, int flush)
73 AVStream *st = s->streams[stream_index];
74 AudioInterleaveContext *aic = st->priv_data;
76 int size = FFMIN(av_fifo_size(&aic->fifo), *aic->samples * aic->sample_size);
77 if (!size || (!flush && size == av_fifo_size(&aic->fifo)))
80 av_new_packet(pkt, size);
81 av_fifo_read(&aic->fifo, pkt->data, size);
83 pkt->dts = pkt->pts = aic->dts;
84 pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
85 pkt->stream_index = stream_index;
86 aic->dts += pkt->duration;
90 aic->samples = aic->samples_per_frame;
95 int ff_audio_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
96 int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
97 int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
102 AVStream *st = s->streams[pkt->stream_index];
103 AudioInterleaveContext *aic = st->priv_data;
104 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
105 av_fifo_generic_write(&aic->fifo, pkt->data, pkt->size, NULL);
107 // rewrite pts and dts to be decoded time line position
109 aic->dts += pkt->duration;
110 ff_interleave_add_packet(s, pkt, compare_ts);
115 for (i = 0; i < s->nb_streams; i++) {
116 AVStream *st = s->streams[i];
117 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
119 while (ff_interleave_new_audio_packet(s, &new_pkt, i, flush))
120 ff_interleave_add_packet(s, &new_pkt, compare_ts);
124 return get_packet(s, out, pkt, flush);