OSDN Git Service

extract audio interleaving code from mxf muxer, will be used by gxf and dv
[coroid/ffmpeg_saccubus.git] / libavformat / audiointerleave.c
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
27 void ff_audio_interleave_close(AVFormatContext *s)
28 {
29     int i;
30     for (i = 0; i < s->nb_streams; i++) {
31         AVStream *st = s->streams[i];
32         AudioInterleaveContext *aic = st->priv_data;
33
34         if (st->codec->codec_type == CODEC_TYPE_AUDIO)
35             av_fifo_free(&aic->fifo);
36     }
37 }
38
39 int ff_audio_interleave_init(AVFormatContext *s,
40                              const int *samples_per_frame,
41                              AVRational time_base)
42 {
43     int i;
44
45     if (!samples_per_frame)
46         return -1;
47
48     for (i = 0; i < s->nb_streams; i++) {
49         AVStream *st = s->streams[i];
50         AudioInterleaveContext *aic = st->priv_data;
51
52         if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
53             aic->sample_size = (st->codec->channels *
54                                 av_get_bits_per_sample(st->codec->codec_id)) / 8;
55             if (!aic->sample_size) {
56                 av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
57                 return -1;
58             }
59             aic->samples_per_frame = samples_per_frame;
60             aic->samples = aic->samples_per_frame;
61             aic->time_base = time_base;
62
63             av_fifo_init(&aic->fifo, 100 * *aic->samples);
64         }
65     }
66
67     return 0;
68 }
69
70 int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
71                                    int stream_index, int flush)
72 {
73     AVStream *st = s->streams[stream_index];
74     AudioInterleaveContext *aic = st->priv_data;
75
76     int size = FFMIN(av_fifo_size(&aic->fifo), *aic->samples * aic->sample_size);
77     if (!size || (!flush && size == av_fifo_size(&aic->fifo)))
78         return 0;
79
80     av_new_packet(pkt, size);
81     av_fifo_read(&aic->fifo, pkt->data, size);
82
83     pkt->dts = pkt->pts = aic->dts;
84     pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base);
85     pkt->stream_index = stream_index;
86     aic->dts += pkt->duration;
87
88     aic->samples++;
89     if (!*aic->samples)
90         aic->samples = aic->samples_per_frame;
91
92     return size;
93 }
94
95 int ff_audio_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
96                         int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
97                         int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *))
98 {
99     int i;
100
101     if (pkt) {
102         AVStream *st = s->streams[pkt->stream_index];
103         AudioInterleaveContext *aic = st->priv_data;
104         if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
105             av_fifo_generic_write(&aic->fifo, pkt->data, pkt->size, NULL);
106         } else {
107             // rewrite pts and dts to be decoded time line position
108             pkt->dts = aic->dts;
109             aic->dts += pkt->duration;
110             ff_interleave_add_packet(s, pkt, compare_ts);
111         }
112         pkt = NULL;
113     }
114
115     for (i = 0; i < s->nb_streams; i++) {
116         AVStream *st = s->streams[i];
117         if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
118             AVPacket new_pkt;
119             while (ff_interleave_new_audio_packet(s, &new_pkt, i, flush))
120                 ff_interleave_add_packet(s, &new_pkt, compare_ts);
121         }
122     }
123
124     return get_packet(s, out, pkt, flush);
125 }