stream/tv: move new_handle() function from header to tv.c
[mplayer.git] / libmpdemux / muxer.c
blob32ac1bef8c4710b40d35d9b829f1f62b64ac887e
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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <unistd.h>
25 #include "config.h"
26 #include "aviheader.h"
27 #include "ms_hdr.h"
29 #include "stream/stream.h"
30 #include "muxer.h"
31 #include "demuxer.h"
32 #include "mp_msg.h"
33 #include "stheader.h"
35 muxer_t *muxer_new_muxer(int type,stream_t *stream){
36 muxer_t* muxer=calloc(1,sizeof(muxer_t));
37 if(!muxer)
38 return NULL;
39 muxer->stream = stream;
40 switch (type) {
41 case MUXER_TYPE_MPEG:
42 if(! muxer_init_muxer_mpeg(muxer))
43 goto fail;
44 break;
45 case MUXER_TYPE_RAWVIDEO:
46 if(! muxer_init_muxer_rawvideo(muxer))
47 goto fail;
48 break;
49 case MUXER_TYPE_RAWAUDIO:
50 if(! muxer_init_muxer_rawaudio(muxer))
51 goto fail;
52 break;
53 #ifdef CONFIG_LIBAVFORMAT
54 case MUXER_TYPE_LAVF:
55 if(! muxer_init_muxer_lavf(muxer))
56 goto fail;
57 break;
58 #endif
59 case MUXER_TYPE_AVI:
60 default:
61 if(! muxer_init_muxer_avi(muxer))
62 goto fail;
64 return muxer;
66 fail:
67 free(muxer);
68 return NULL;
71 /* buffer frames until we either:
72 * (a) have at least one frame from each stream
73 * (b) run out of memory */
74 void muxer_write_chunk(muxer_stream_t *s, size_t len, unsigned int flags, double dts, double pts) {
75 if(dts == MP_NOPTS_VALUE) dts= s->timer;
76 if(pts == MP_NOPTS_VALUE) pts= s->timer; // this is wrong
78 if (s->muxer->muxbuf_skip_buffer) {
79 s->muxer->cont_write_chunk(s, len, flags, dts, pts);
81 else {
82 int num = s->muxer->muxbuf_num++;
83 muxbuf_t *buf, *tmp;
85 tmp = realloc_struct(s->muxer->muxbuf, (num+1), sizeof(muxbuf_t));
86 if(!tmp) {
87 mp_tmsg(MSGT_MUXER, MSGL_FATAL, "Muxer frame buffer cannot reallocate memory!\n");
88 return;
90 s->muxer->muxbuf = tmp;
91 buf = s->muxer->muxbuf + num;
93 /* buffer this frame */
94 buf->stream = s;
95 buf->dts= dts;
96 buf->pts= pts;
97 buf->len = len;
98 buf->flags = flags;
99 buf->buffer = malloc(len);
100 if (!buf->buffer) {
101 mp_tmsg(MSGT_MUXER, MSGL_FATAL, "Muxer frame buffer cannot allocate memory!\n");
102 return;
104 memcpy(buf->buffer, s->buffer, buf->len);
105 s->muxbuf_seen = 1;
107 /* see if we need to keep buffering */
108 s->muxer->muxbuf_skip_buffer = 1;
109 for (num = 0; s->muxer->streams[num]; ++num)
110 if (!s->muxer->streams[num]->muxbuf_seen)
111 s->muxer->muxbuf_skip_buffer = 0;
113 /* see if we can flush buffer now */
114 if (s->muxer->muxbuf_skip_buffer) {
115 mp_tmsg(MSGT_MUXER, MSGL_V, "Muxer frame buffer sending %d frame(s) to the muxer.\n", s->muxer->muxbuf_num);
117 /* fix parameters for all streams */
118 for (num = 0; s->muxer->streams[num]; ++num) {
119 muxer_stream_t *str = s->muxer->streams[num];
120 if(str->muxer->fix_stream_parameters)
121 muxer_stream_fix_parameters(str->muxer, str);
124 /* write header */
125 if (s->muxer->cont_write_header)
126 muxer_write_header(s->muxer);
128 /* send all buffered frames to muxer */
129 for (num = 0; num < s->muxer->muxbuf_num; ++num) {
130 muxbuf_t tmp_buf;
131 buf = s->muxer->muxbuf + num;
132 s = buf->stream;
134 /* 1. save timer and buffer (might have changed by now) */
135 tmp_buf.dts = s->timer;
136 tmp_buf.buffer = s->buffer;
138 /* 2. move stored timer and buffer into stream and mux it */
139 s->timer = buf->dts;
140 s->buffer = buf->buffer;
141 s->muxer->cont_write_chunk(s, buf->len, buf->flags, buf->dts, buf->pts);
143 /* 3. restore saved timer and buffer */
144 s->timer = tmp_buf.dts;
145 s->buffer = tmp_buf.buffer;
148 free(s->muxer->muxbuf);
149 s->muxer->muxbuf_num = 0;
153 /* this code moved directly from muxer_avi.c */
154 // alter counters:
155 if(s->h.dwSampleSize){
156 // CBR
157 s->h.dwLength+=len/s->h.dwSampleSize;
158 if(len%s->h.dwSampleSize) mp_tmsg(MSGT_MUXER, MSGL_WARN, "Warning, len isn't divisible by samplesize!\n");
159 } else {
160 // VBR
161 s->h.dwLength++;
163 s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
164 s->size+=len;
166 return;