1 /*
2 * This file is part of MPlayer.
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <unistd.h>
24
25 #include "config.h"
26 #include "aviheader.h"
27 #include "ms_hdr.h"
28
29 #include "stream/stream.h"
30 #include "muxer.h"
31 #include "demuxer.h"
32 #include "mp_msg.h"
33 #include "help_mp.h"
34 #include "stheader.h"
35
muxer_new_muxer(int type,stream_t * stream)36 muxer_t *muxer_new_muxer(int type,stream_t *stream){
37 muxer_t* muxer=calloc(1,sizeof(muxer_t));
38 if(!muxer)
39 return NULL;
40 muxer->stream = stream;
41 switch (type) {
42 case MUXER_TYPE_MPEG:
43 if(! muxer_init_muxer_mpeg(muxer))
44 goto fail;
45 break;
46 case MUXER_TYPE_RAWVIDEO:
47 if(! muxer_init_muxer_rawvideo(muxer))
48 goto fail;
49 break;
50 case MUXER_TYPE_RAWAUDIO:
51 if(! muxer_init_muxer_rawaudio(muxer))
52 goto fail;
53 break;
54 #ifdef CONFIG_FFMPEG
55 case MUXER_TYPE_LAVF:
56 if(! muxer_init_muxer_lavf(muxer))
57 goto fail;
58 break;
59 #endif
60 case MUXER_TYPE_AVI:
61 default:
62 if(! muxer_init_muxer_avi(muxer))
63 goto fail;
64 }
65 return muxer;
66
67 fail:
68 free(muxer);
69 return NULL;
70 }
71
72 /* Flush the internal muxer buffer.
73 * This is normally called from muxer_write_chunk() once all streams
74 * have seen frames. */
muxer_flush(muxer_t * m)75 void muxer_flush(muxer_t *m) {
76 int num;
77
78 if (!m->muxbuf)
79 return;
80
81 mp_msg(MSGT_MUXER, MSGL_V, "Muxer frame buffer sending %d frame(s) to the muxer.\n", m->muxbuf_num);
82
83 /* fix parameters for all streams */
84 for (num = 0; m->streams[num]; ++num) {
85 muxer_stream_t *str = m->streams[num];
86 if(str->muxer->fix_stream_parameters)
87 muxer_stream_fix_parameters(str->muxer, str);
88 }
89
90 /* write header */
91 if (m->cont_write_header)
92 muxer_write_header(m);
93
94 /* send all buffered frames to muxer */
95 for (num = 0; num < m->muxbuf_num; ++num) {
96 muxbuf_t tmp_buf;
97 muxbuf_t *buf;
98 muxer_stream_t *s;
99 buf = m->muxbuf + num;
100 s = buf->stream;
101
102 /* 1. save timer and buffer (might have changed by now) */
103 tmp_buf.dts = s->timer;
104 tmp_buf.buffer = s->buffer;
105
106 /* 2. move stored timer and buffer into stream and mux it */
107 s->timer = buf->dts;
108 s->buffer = buf->buffer;
109 m->cont_write_chunk(s, buf->len, buf->flags, buf->dts, buf->pts);
110 free(buf->buffer);
111 buf->buffer = NULL;
112
113 /* 3. restore saved timer and buffer */
114 s->timer = tmp_buf.dts;
115 s->buffer = tmp_buf.buffer;
116 }
117
118 free(m->muxbuf);
119 m->muxbuf = NULL;
120 m->muxbuf_num = 0;
121 }
122
123 /* buffer frames until we either:
124 * (a) have at least one non-empty frame from each stream
125 * (b) run out of memory */
muxer_write_chunk(muxer_stream_t * s,size_t len,unsigned int flags,double dts,double pts)126 void muxer_write_chunk(muxer_stream_t *s, size_t len, unsigned int flags, double dts, double pts) {
127 if(dts == MP_NOPTS_VALUE) dts= s->timer;
128 if(pts == MP_NOPTS_VALUE) pts= s->timer; // this is wrong
129
130 if (s->muxer->muxbuf_skip_buffer) {
131 s->muxer->cont_write_chunk(s, len, flags, dts, pts);
132 }
133 else {
134 int num = s->muxer->muxbuf_num++;
135 muxbuf_t *buf;
136
137 s->muxer->muxbuf = realloc_struct(s->muxer->muxbuf, (num+1), sizeof(muxbuf_t));
138 if(!s->muxer->muxbuf) {
139 s->muxer->muxbuf_num = 0;
140 mp_msg(MSGT_MUXER, MSGL_FATAL, MSGTR_MuxbufReallocErr);
141 return;
142 }
143 buf = s->muxer->muxbuf + num;
144
145 /* buffer this frame */
146 buf->stream = s;
147 buf->dts= dts;
148 buf->pts= pts;
149 buf->len = len;
150 buf->flags = flags;
151 buf->buffer = malloc(len);
152 if (!buf->buffer) {
153 mp_msg(MSGT_MUXER, MSGL_FATAL, MSGTR_MuxbufMallocErr);
154 return;
155 }
156 memcpy(buf->buffer, s->buffer, buf->len);
157
158 /* If mencoder inserts "repeat last frame" chunks with len == 0
159 * before the encoder is configured and first real frame is output
160 * or a broken file starts a stream with such frames, then muxer
161 * won't find needed info for writing initial header.
162 * Wait until the first real frame is seen. */
163 if (len > 0)
164 s->muxbuf_seen = 1;
165
166 /* see if we need to keep buffering */
167 s->muxer->muxbuf_skip_buffer = 1;
168 for (num = 0; s->muxer->streams[num]; ++num)
169 if (!s->muxer->streams[num]->muxbuf_seen)
170 s->muxer->muxbuf_skip_buffer = 0;
171
172 /* see if we can flush buffer now */
173 if (s->muxer->muxbuf_skip_buffer)
174 muxer_flush(s->muxer);
175 }
176
177 /* this code moved directly from muxer_avi.c */
178 // alter counters:
179 if(s->h.dwSampleSize){
180 // CBR
181 s->h.dwLength+=len/s->h.dwSampleSize;
182 if(len%s->h.dwSampleSize) mp_msg(MSGT_MUXER, MSGL_WARN, MSGTR_WarningLenIsntDivisible);
183 } else {
184 // VBR
185 s->h.dwLength++;
186 }
187 s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
188 s->size+=len;
189
190 return;
191 }
192