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 av_new_packet(pkt, size);
00088 av_fifo_generic_read(aic->fifo, pkt->data, size, NULL);
00089
00090 pkt->dts = pkt->pts = aic->dts;
00091 pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
00092 pkt->stream_index = stream_index;
00093 aic->dts += pkt->duration;
00094
00095 aic->samples++;
00096 if (!*aic->samples)
00097 aic->samples = aic->samples_per_frame;
00098
00099 return size;
00100 }
00101
00102 int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
00103 int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
00104 int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
00105 {
00106 int i;
00107
00108 if (pkt) {
00109 AVStream *st = s->streams[pkt->stream_index];
00110 AudioInterleaveContext *aic = st->priv_data;
00111 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00112 unsigned new_size = av_fifo_size(aic->fifo) + pkt->size;
00113 if (new_size > aic->fifo_size) {
00114 if (av_fifo_realloc2(aic->fifo, new_size) < 0)
00115 return -1;
00116 aic->fifo_size = new_size;
00117 }
00118 av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL);
00119 } else {
00120 int ret;
00121
00122 pkt->pts = pkt->dts = aic->dts;
00123 aic->dts += pkt->duration;
00124 ret = ff_interleave_add_packet(s, pkt, compare_ts);
00125 if (ret < 0)
00126 return ret;
00127 }
00128 pkt = NULL;
00129 }
00130
00131 for (i = 0; i < s->nb_streams; i++) {
00132 AVStream *st = s->streams[i];
00133 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00134 AVPacket new_pkt;
00135 int ret;
00136 while (ff_interleave_new_audio_packet(s, &new_pkt, i, flush)) {
00137 ret = ff_interleave_add_packet(s, &new_pkt, compare_ts);
00138 if (ret < 0)
00139 return ret;
00140 }
00141 }
00142 }
00143
00144 return get_packet(s, out, pkt, flush);
00145 }