00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 if (!time_base.num) {
00051 av_log(s, AV_LOG_ERROR, "timebase not set for audio interleave\n");
00052 return -1;
00053 }
00054 for (i = 0; i < s->nb_streams; i++) {
00055 AVStream *st = s->streams[i];
00056 AudioInterleaveContext *aic = st->priv_data;
00057
00058 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00059 aic->sample_size = (st->codec->channels *
00060 av_get_bits_per_sample(st->codec->codec_id)) / 8;
00061 if (!aic->sample_size) {
00062 av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
00063 return -1;
00064 }
00065 aic->samples_per_frame = samples_per_frame;
00066 aic->samples = aic->samples_per_frame;
00067 aic->time_base = time_base;
00068
00069 aic->fifo_size = 100* *aic->samples;
00070 aic->fifo= av_fifo_alloc(100 * *aic->samples);
00071 }
00072 }
00073
00074 return 0;
00075 }
00076
00077 static int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
00078 int stream_index, int flush)
00079 {
00080 AVStream *st = s->streams[stream_index];
00081 AudioInterleaveContext *aic = st->priv_data;
00082
00083 int size = FFMIN(av_fifo_size(aic->fifo), *aic->samples * aic->sample_size);
00084 if (!size || (!flush && size == av_fifo_size(aic->fifo)))
00085 return 0;
00086
00087 if (av_new_packet(pkt, size) < 0)
00088 return AVERROR(ENOMEM);
00089 av_fifo_generic_read(aic->fifo, pkt->data, size, NULL);
00090
00091 pkt->dts = pkt->pts = aic->dts;
00092 pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
00093 pkt->stream_index = stream_index;
00094 aic->dts += pkt->duration;
00095
00096 aic->samples++;
00097 if (!*aic->samples)
00098 aic->samples = aic->samples_per_frame;
00099
00100 return size;
00101 }
00102
00103 int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
00104 int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
00105 int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
00106 {
00107 int i;
00108
00109 if (pkt) {
00110 AVStream *st = s->streams[pkt->stream_index];
00111 AudioInterleaveContext *aic = st->priv_data;
00112 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00113 unsigned new_size = av_fifo_size(aic->fifo) + pkt->size;
00114 if (new_size > aic->fifo_size) {
00115 if (av_fifo_realloc2(aic->fifo, new_size) < 0)
00116 return -1;
00117 aic->fifo_size = new_size;
00118 }
00119 av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL);
00120 } else {
00121 int ret;
00122
00123 pkt->pts = pkt->dts = aic->dts;
00124 aic->dts += pkt->duration;
00125 ret = ff_interleave_add_packet(s, pkt, compare_ts);
00126 if (ret < 0)
00127 return ret;
00128 }
00129 pkt = NULL;
00130 }
00131
00132 for (i = 0; i < s->nb_streams; i++) {
00133 AVStream *st = s->streams[i];
00134 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00135 AVPacket new_pkt;
00136 int ret;
00137 while ((ret = ff_interleave_new_audio_packet(s, &new_pkt, i, flush)) > 0) {
00138 ret = ff_interleave_add_packet(s, &new_pkt, compare_ts);
00139 if (ret < 0)
00140 return ret;
00141 }
00142 if (ret < 0)
00143 return ret;
00144 }
00145 }
00146
00147 return get_packet(s, out, NULL, flush);
00148 }