Merge branch 'mplayer1_changes'
[mplayer/greg.git] / libmpdemux / demux_demuxers.c
blob54fe5c1b0195a39035705740d43418b0411e931d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
20 #include "mp_msg.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "stream/stream.h"
25 #include "demuxer.h"
26 #include "stheader.h"
27 #include "talloc.h"
29 typedef struct dd_priv {
30 demuxer_t* vd;
31 demuxer_t* ad;
32 demuxer_t* sd;
33 } dd_priv_t;
35 extern const demuxer_desc_t demuxer_desc_demuxers;
37 demuxer_t* new_demuxers_demuxer(demuxer_t* vd, demuxer_t* ad, demuxer_t* sd) {
38 demuxer_t* ret;
39 dd_priv_t* priv;
41 ret = talloc_zero(NULL, struct demuxer);
43 priv = malloc(sizeof(dd_priv_t));
44 priv->vd = vd;
45 priv->ad = ad;
46 priv->sd = sd;
47 ret->priv = priv;
49 ret->type = ret->file_format = DEMUXER_TYPE_DEMUXERS;
50 // Video is the most important :-)
51 ret->stream = vd->stream;
52 ret->seekable = vd->seekable && ad->seekable && sd->seekable;
53 ret->stream_pts = MP_NOPTS_VALUE;
55 ret->video = vd->video;
56 ret->audio = ad->audio;
57 ret->sub = sd->sub;
58 if (sd && sd != vd && sd != ad) sd->sub->non_interleaved = 1;
60 // without these, demux_demuxers_fill_buffer will never be called,
61 // but they break the demuxer-specific code in video.c
62 #if 0
63 if (vd) vd->video->demuxer = ret;
64 if (ad) ad->audio->demuxer = ret;
65 if (sd) sd->sub->demuxer = ret;
67 // HACK?, necessary for subtitle (and audio and video when implemented) switching
68 memcpy(ret->v_streams, vd->v_streams, sizeof(ret->v_streams));
69 memcpy(ret->a_streams, ad->a_streams, sizeof(ret->a_streams));
70 memcpy(ret->s_streams, sd->s_streams, sizeof(ret->s_streams));
71 #endif
73 ret->desc = &demuxer_desc_demuxers;
75 return ret;
78 static int demux_demuxers_fill_buffer(demuxer_t *demux,demux_stream_t *ds) {
79 dd_priv_t* priv;
81 priv=demux->priv;
83 // HACK: make sure the subtitles get properly interleaved if with -subfile
84 if (priv->sd && priv->sd->sub != ds &&
85 priv->sd != priv->vd && priv->sd != priv->ad)
86 ds_get_next_pts(priv->sd->sub);
87 if(priv->vd && priv->vd->video == ds)
88 return demux_fill_buffer(priv->vd,ds);
89 else if(priv->ad && priv->ad->audio == ds)
90 return demux_fill_buffer(priv->ad,ds);
91 else if(priv->sd && priv->sd->sub == ds)
92 return demux_fill_buffer(priv->sd,ds);
94 mp_tmsg(MSGT_DEMUX,MSGL_WARN,"fill_buffer error: bad demuxer: not vd, ad or sd.\n");
95 return 0;
98 static void demux_demuxers_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags) {
99 dd_priv_t* priv;
100 float pos;
101 priv=demuxer->priv;
103 priv->ad->stream->eof = 0;
104 priv->sd->stream->eof = 0;
106 // Seek video
107 demux_seek(priv->vd,rel_seek_secs,audio_delay,flags);
108 // Get the new pos
109 pos = demuxer->video->pts;
110 if (!pos) {
111 demux_fill_buffer(priv->vd, demuxer->video);
112 if (demuxer->video->first)
113 pos = demuxer->video->first->pts;
116 if(priv->ad != priv->vd && demuxer->audio->sh) {
117 sh_audio_t* sh = demuxer->audio->sh;
118 demux_seek(priv->ad,pos,audio_delay,1);
119 // In case the demuxer don't set pts
120 if(!demuxer->audio->pts)
121 demuxer->audio->pts = pos-((ds_tell_pts(demuxer->audio)-sh->a_in_buffer_len)/(float)sh->i_bps);
124 if(priv->sd != priv->vd)
125 demux_seek(priv->sd,pos,audio_delay,1);
129 static void demux_close_demuxers(demuxer_t* demuxer) {
130 dd_priv_t* priv = demuxer->priv;
131 stream_t *s;
133 if(priv->vd)
134 free_demuxer(priv->vd);
135 if(priv->ad && priv->ad != priv->vd) {
136 // That's a hack to free the audio file stream
137 // It's ok atm but we shouldn't free that here
138 s = priv->ad->stream;
139 free_demuxer(priv->ad);
140 free_stream(s);
141 } if(priv->sd && priv->sd != priv->vd && priv->sd != priv->ad) {
142 s = priv->sd->stream;
143 free_demuxer(priv->sd);
144 free_stream(s);
147 free(priv);
151 static int demux_demuxers_control(demuxer_t *demuxer,int cmd, void *arg){
152 dd_priv_t* priv = demuxer->priv;
153 switch (cmd) {
154 case DEMUXER_CTRL_GET_TIME_LENGTH:
155 case DEMUXER_CTRL_GET_PERCENT_POS:
156 return demux_control(priv->vd, cmd, arg);
158 return DEMUXER_CTRL_NOTIMPL;
161 const demuxer_desc_t demuxer_desc_demuxers = {
162 "Demuxers demuxer",
163 "", // Not selectable
165 "?",
166 "internal use only",
167 DEMUXER_TYPE_DEMUXERS,
168 0, // no autodetect
169 NULL,
170 demux_demuxers_fill_buffer,
171 NULL,
172 demux_close_demuxers,
173 demux_demuxers_seek,
174 demux_demuxers_control