Libav
|
00001 /* 00002 * Audio Interleaving functions 00003 * 00004 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "libavutil/fifo.h" 00024 #include "avformat.h" 00025 #include "audiointerleave.h" 00026 #include "internal.h" 00027 00028 void ff_audio_interleave_close(AVFormatContext *s) 00029 { 00030 int i; 00031 for (i = 0; i < s->nb_streams; i++) { 00032 AVStream *st = s->streams[i]; 00033 AudioInterleaveContext *aic = st->priv_data; 00034 00035 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) 00036 av_fifo_free(aic->fifo); 00037 } 00038 } 00039 00040 int ff_audio_interleave_init(AVFormatContext *s, 00041 const int *samples_per_frame, 00042 AVRational time_base) 00043 { 00044 int i; 00045 00046 if (!samples_per_frame) 00047 return -1; 00048 00049 for (i = 0; i < s->nb_streams; i++) { 00050 AVStream *st = s->streams[i]; 00051 AudioInterleaveContext *aic = st->priv_data; 00052 00053 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { 00054 aic->sample_size = (st->codec->channels * 00055 av_get_bits_per_sample(st->codec->codec_id)) / 8; 00056 if (!aic->sample_size) { 00057 av_log(s, AV_LOG_ERROR, "could not compute sample size\n"); 00058 return -1; 00059 } 00060 aic->samples_per_frame = samples_per_frame; 00061 aic->samples = aic->samples_per_frame; 00062 aic->time_base = time_base; 00063 00064 aic->fifo_size = 100* *aic->samples; 00065 aic->fifo= av_fifo_alloc(100 * *aic->samples); 00066 } 00067 } 00068 00069 return 0; 00070 } 00071 00072 static int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt, 00073 int stream_index, int flush) 00074 { 00075 AVStream *st = s->streams[stream_index]; 00076 AudioInterleaveContext *aic = st->priv_data; 00077 00078 int size = FFMIN(av_fifo_size(aic->fifo), *aic->samples * aic->sample_size); 00079 if (!size || (!flush && size == av_fifo_size(aic->fifo))) 00080 return 0; 00081 00082 av_new_packet(pkt, size); 00083 av_fifo_generic_read(aic->fifo, pkt->data, size, NULL); 00084 00085 pkt->dts = pkt->pts = aic->dts; 00086 pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base); 00087 pkt->stream_index = stream_index; 00088 aic->dts += pkt->duration; 00089 00090 aic->samples++; 00091 if (!*aic->samples) 00092 aic->samples = aic->samples_per_frame; 00093 00094 return size; 00095 } 00096 00097 int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, 00098 int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), 00099 int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *)) 00100 { 00101 int i; 00102 00103 if (pkt) { 00104 AVStream *st = s->streams[pkt->stream_index]; 00105 AudioInterleaveContext *aic = st->priv_data; 00106 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { 00107 unsigned new_size = av_fifo_size(aic->fifo) + pkt->size; 00108 if (new_size > aic->fifo_size) { 00109 if (av_fifo_realloc2(aic->fifo, new_size) < 0) 00110 return -1; 00111 aic->fifo_size = new_size; 00112 } 00113 av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL); 00114 } else { 00115 // rewrite pts and dts to be decoded time line position 00116 pkt->pts = pkt->dts = aic->dts; 00117 aic->dts += pkt->duration; 00118 ff_interleave_add_packet(s, pkt, compare_ts); 00119 } 00120 pkt = NULL; 00121 } 00122 00123 for (i = 0; i < s->nb_streams; i++) { 00124 AVStream *st = s->streams[i]; 00125 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { 00126 AVPacket new_pkt; 00127 while (ff_interleave_new_audio_packet(s, &new_pkt, i, flush)) 00128 ff_interleave_add_packet(s, &new_pkt, compare_ts); 00129 } 00130 } 00131 00132 return get_packet(s, out, pkt, flush); 00133 }