• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/audiointerleave.c

Go to the documentation of this file.
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 "libavutil/mathematics.h"
00025 #include "avformat.h"
00026 #include "audiointerleave.h"
00027 #include "internal.h"
00028 
00029 void ff_audio_interleave_close(AVFormatContext *s)
00030 {
00031     int i;
00032     for (i = 0; i < s->nb_streams; i++) {
00033         AVStream *st = s->streams[i];
00034         AudioInterleaveContext *aic = st->priv_data;
00035 
00036         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
00037             av_fifo_free(aic->fifo);
00038     }
00039 }
00040 
00041 int ff_audio_interleave_init(AVFormatContext *s,
00042                              const int *samples_per_frame,
00043                              AVRational time_base)
00044 {
00045     int i;
00046 
00047     if (!samples_per_frame)
00048         return -1;
00049 
00050     for (i = 0; i < s->nb_streams; i++) {
00051         AVStream *st = s->streams[i];
00052         AudioInterleaveContext *aic = st->priv_data;
00053 
00054         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00055             aic->sample_size = (st->codec->channels *
00056                                 av_get_bits_per_sample(st->codec->codec_id)) / 8;
00057             if (!aic->sample_size) {
00058                 av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
00059                 return -1;
00060             }
00061             aic->samples_per_frame = samples_per_frame;
00062             aic->samples = aic->samples_per_frame;
00063             aic->time_base = time_base;
00064 
00065             aic->fifo_size = 100* *aic->samples;
00066             aic->fifo= av_fifo_alloc(100 * *aic->samples);
00067         }
00068     }
00069 
00070     return 0;
00071 }
00072 
00073 static int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
00074                                    int stream_index, int flush)
00075 {
00076     AVStream *st = s->streams[stream_index];
00077     AudioInterleaveContext *aic = st->priv_data;
00078 
00079     int size = FFMIN(av_fifo_size(aic->fifo), *aic->samples * aic->sample_size);
00080     if (!size || (!flush && size == av_fifo_size(aic->fifo)))
00081         return 0;
00082 
00083     av_new_packet(pkt, size);
00084     av_fifo_generic_read(aic->fifo, pkt->data, size, NULL);
00085 
00086     pkt->dts = pkt->pts = aic->dts;
00087     pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
00088     pkt->stream_index = stream_index;
00089     aic->dts += pkt->duration;
00090 
00091     aic->samples++;
00092     if (!*aic->samples)
00093         aic->samples = aic->samples_per_frame;
00094 
00095     return size;
00096 }
00097 
00098 int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
00099                         int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
00100                         int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
00101 {
00102     int i;
00103 
00104     if (pkt) {
00105         AVStream *st = s->streams[pkt->stream_index];
00106         AudioInterleaveContext *aic = st->priv_data;
00107         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00108             unsigned new_size = av_fifo_size(aic->fifo) + pkt->size;
00109             if (new_size > aic->fifo_size) {
00110                 if (av_fifo_realloc2(aic->fifo, new_size) < 0)
00111                     return -1;
00112                 aic->fifo_size = new_size;
00113             }
00114             av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL);
00115         } else {
00116             int ret;
00117             // rewrite pts and dts to be decoded time line position
00118             pkt->pts = pkt->dts = aic->dts;
00119             aic->dts += pkt->duration;
00120             ret = ff_interleave_add_packet(s, pkt, compare_ts);
00121             if (ret < 0)
00122                 return ret;
00123         }
00124         pkt = NULL;
00125     }
00126 
00127     for (i = 0; i < s->nb_streams; i++) {
00128         AVStream *st = s->streams[i];
00129         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00130             AVPacket new_pkt;
00131             int ret;
00132             while (ff_interleave_new_audio_packet(s, &new_pkt, i, flush)) {
00133                 ret = ff_interleave_add_packet(s, &new_pkt, compare_ts);
00134                 if (ret < 0)
00135                     return ret;
00136             }
00137         }
00138     }
00139 
00140     return get_packet(s, out, pkt, flush);
00141 }
Generated on Fri Feb 1 2013 14:34:50 for FFmpeg by doxygen 1.7.1