Remove the grayscale and 4:2:2 RTjpeg code, it is neither used nor is there
[mplayer/glamo.git] / libmpdemux / muxer.c
blobd7bda17a61573cb2cb8c4547b1087720a4ea968f
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <inttypes.h>
6 #include <unistd.h>
8 #include "config.h"
9 #include "aviheader.h"
10 #include "ms_hdr.h"
12 #include "stream/stream.h"
13 #include "muxer.h"
14 #include "demuxer.h"
15 #include "mp_msg.h"
16 #include "help_mp.h"
17 #include "stheader.h"
19 muxer_t *muxer_new_muxer(int type,stream_t *stream){
20 muxer_t* muxer=calloc(1,sizeof(muxer_t));
21 if(!muxer)
22 return NULL;
23 muxer->stream = stream;
24 switch (type) {
25 case MUXER_TYPE_MPEG:
26 if(! muxer_init_muxer_mpeg(muxer))
27 goto fail;
28 break;
29 case MUXER_TYPE_RAWVIDEO:
30 if(! muxer_init_muxer_rawvideo(muxer))
31 goto fail;
32 break;
33 case MUXER_TYPE_RAWAUDIO:
34 if(! muxer_init_muxer_rawaudio(muxer))
35 goto fail;
36 break;
37 #ifdef CONFIG_LIBAVFORMAT
38 case MUXER_TYPE_LAVF:
39 if(! muxer_init_muxer_lavf(muxer))
40 goto fail;
41 break;
42 #endif
43 case MUXER_TYPE_AVI:
44 default:
45 if(! muxer_init_muxer_avi(muxer))
46 goto fail;
48 return muxer;
50 fail:
51 free(muxer);
52 return NULL;
55 /* buffer frames until we either:
56 * (a) have at least one frame from each stream
57 * (b) run out of memory */
58 void muxer_write_chunk(muxer_stream_t *s, size_t len, unsigned int flags, double dts, double pts) {
59 if(dts == MP_NOPTS_VALUE) dts= s->timer;
60 if(pts == MP_NOPTS_VALUE) pts= s->timer; // this is wrong
62 if (s->muxer->muxbuf_skip_buffer) {
63 s->muxer->cont_write_chunk(s, len, flags, dts, pts);
65 else {
66 int num = s->muxer->muxbuf_num++;
67 muxbuf_t *buf, *tmp;
69 tmp = realloc_struct(s->muxer->muxbuf, (num+1), sizeof(muxbuf_t));
70 if(!tmp) {
71 mp_msg(MSGT_MUXER, MSGL_FATAL, MSGTR_MuxbufReallocErr);
72 return;
74 s->muxer->muxbuf = tmp;
75 buf = s->muxer->muxbuf + num;
77 /* buffer this frame */
78 buf->stream = s;
79 buf->dts= dts;
80 buf->pts= pts;
81 buf->len = len;
82 buf->flags = flags;
83 buf->buffer = malloc(len);
84 if (!buf->buffer) {
85 mp_msg(MSGT_MUXER, MSGL_FATAL, MSGTR_MuxbufMallocErr);
86 return;
88 memcpy(buf->buffer, s->buffer, buf->len);
89 s->muxbuf_seen = 1;
91 /* see if we need to keep buffering */
92 s->muxer->muxbuf_skip_buffer = 1;
93 for (num = 0; s->muxer->streams[num]; ++num)
94 if (!s->muxer->streams[num]->muxbuf_seen)
95 s->muxer->muxbuf_skip_buffer = 0;
97 /* see if we can flush buffer now */
98 if (s->muxer->muxbuf_skip_buffer) {
99 mp_msg(MSGT_MUXER, MSGL_V, MSGTR_MuxbufSending, s->muxer->muxbuf_num);
101 /* fix parameters for all streams */
102 for (num = 0; s->muxer->streams[num]; ++num) {
103 muxer_stream_t *str = s->muxer->streams[num];
104 if(str->muxer->fix_stream_parameters)
105 muxer_stream_fix_parameters(str->muxer, str);
108 /* write header */
109 if (s->muxer->cont_write_header)
110 muxer_write_header(s->muxer);
112 /* send all buffered frames to muxer */
113 for (num = 0; num < s->muxer->muxbuf_num; ++num) {
114 muxbuf_t tmp_buf;
115 buf = s->muxer->muxbuf + num;
116 s = buf->stream;
118 /* 1. save timer and buffer (might have changed by now) */
119 tmp_buf.dts = s->timer;
120 tmp_buf.buffer = s->buffer;
122 /* 2. move stored timer and buffer into stream and mux it */
123 s->timer = buf->dts;
124 s->buffer = buf->buffer;
125 s->muxer->cont_write_chunk(s, buf->len, buf->flags, buf->dts, buf->pts);
127 /* 3. restore saved timer and buffer */
128 s->timer = tmp_buf.dts;
129 s->buffer = tmp_buf.buffer;
132 free(s->muxer->muxbuf);
133 s->muxer->muxbuf_num = 0;
137 /* this code moved directly from muxer_avi.c */
138 // alter counters:
139 if(s->h.dwSampleSize){
140 // CBR
141 s->h.dwLength+=len/s->h.dwSampleSize;
142 if(len%s->h.dwSampleSize) mp_msg(MSGT_MUXER, MSGL_WARN, MSGTR_WarningLenIsntDivisible);
143 } else {
144 // VBR
145 s->h.dwLength++;
147 s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
148 s->size+=len;
150 return;