1/* 2 * Audio Interleaving functions 3 * 4 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> 5 * 6 * This file is part of FFmpeg. 7 * 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. 12 * 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. 17 * 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 21 */ 22 23#include "libavutil/fifo.h" 24#include "avformat.h" 25#include "audiointerleave.h" 26#include "internal.h" 27 28void ff_audio_interleave_close(AVFormatContext *s) 29{ 30 int i; 31 for (i = 0; i < s->nb_streams; i++) { 32 AVStream *st = s->streams[i]; 33 AudioInterleaveContext *aic = st->priv_data; 34 35 if (st->codec->codec_type == CODEC_TYPE_AUDIO) 36 av_fifo_free(&aic->fifo); 37 } 38} 39 40int ff_audio_interleave_init(AVFormatContext *s, 41 const int *samples_per_frame, 42 AVRational time_base) 43{ 44 int i; 45 46 if (!samples_per_frame) 47 return -1; 48 49 for (i = 0; i < s->nb_streams; i++) { 50 AVStream *st = s->streams[i]; 51 AudioInterleaveContext *aic = st->priv_data; 52 53 if (st->codec->codec_type == CODEC_TYPE_AUDIO) { 54 aic->sample_size = (st->codec->channels * 55 av_get_bits_per_sample(st->codec->codec_id)) / 8; 56 if (!aic->sample_size) { 57 av_log(s, AV_LOG_ERROR, "could not compute sample size\n"); 58 return -1; 59 } 60 aic->samples_per_frame = samples_per_frame; 61 aic->samples = aic->samples_per_frame; 62 aic->time_base = time_base; 63 64 aic->fifo_size = 100* *aic->samples; 65 av_fifo_init(&aic->fifo, 100 * *aic->samples); 66 } 67 } 68 69 return 0; 70} 71 72static int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt, 73 int stream_index, int flush) 74{ 75 AVStream *st = s->streams[stream_index]; 76 AudioInterleaveContext *aic = st->priv_data; 77 78 int size = FFMIN(av_fifo_size(&aic->fifo), *aic->samples * aic->sample_size); 79 if (!size || (!flush && size == av_fifo_size(&aic->fifo))) 80 return 0; 81 82 av_new_packet(pkt, size); 83 av_fifo_read(&aic->fifo, pkt->data, size); 84 85 pkt->dts = pkt->pts = aic->dts; 86 pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base); 87 pkt->stream_index = stream_index; 88 aic->dts += pkt->duration; 89 90 aic->samples++; 91 if (!*aic->samples) 92 aic->samples = aic->samples_per_frame; 93 94 return size; 95} 96 97int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, 98 int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), 99 int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *)) 100{ 101 int i; 102 103 if (pkt) { 104 AVStream *st = s->streams[pkt->stream_index]; 105 AudioInterleaveContext *aic = st->priv_data; 106 if (st->codec->codec_type == CODEC_TYPE_AUDIO) { 107 unsigned new_size = av_fifo_size(&aic->fifo) + pkt->size; 108 if (new_size > aic->fifo_size) { 109 if (av_fifo_realloc2(&aic->fifo, new_size) < 0) 110 return -1; 111 aic->fifo_size = new_size; 112 } 113 av_fifo_generic_write(&aic->fifo, pkt->data, pkt->size, NULL); 114 } else { 115 // rewrite pts and dts to be decoded time line position 116 pkt->pts = pkt->dts = aic->dts; 117 aic->dts += pkt->duration; 118 ff_interleave_add_packet(s, pkt, compare_ts); 119 } 120 pkt = NULL; 121 } 122 123 for (i = 0; i < s->nb_streams; i++) { 124 AVStream *st = s->streams[i]; 125 if (st->codec->codec_type == CODEC_TYPE_AUDIO) { 126 AVPacket new_pkt; 127 while (ff_interleave_new_audio_packet(s, &new_pkt, i, flush)) 128 ff_interleave_add_packet(s, &new_pkt, compare_ts); 129 } 130 } 131 132 return get_packet(s, out, pkt, flush); 133} 134