Mercurial > mplayer.hg
annotate libmpdemux/muxer.c @ 33215:4c7b564719e0
Simplify colormap generation code, avoid some mallocs and add frees
for one error case.
author | reimar |
---|---|
date | Fri, 22 Apr 2011 16:53:01 +0000 |
parents | 801dc49c6f33 |
children | 4c3bee76ef3b |
rev | line source |
---|---|
29238
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
1 /* |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
2 * This file is part of MPlayer. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
3 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
4 * MPlayer is free software; you can redistribute it and/or modify |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
7 * (at your option) any later version. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
8 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
9 * MPlayer is distributed in the hope that it will be useful, |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
12 * GNU General Public License for more details. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
13 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
14 * You should have received a copy of the GNU General Public License along |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
27341
diff
changeset
|
17 */ |
8585 | 18 |
19 #include <stdio.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 #include <inttypes.h> | |
8591 | 23 #include <unistd.h> |
8585 | 24 |
25 #include "config.h" | |
12341
0db4a3a5b01d
removed loader/ dependancy, imported some files from g2, also used patches from Dominik Mierzejewski
alex
parents:
12016
diff
changeset
|
26 #include "aviheader.h" |
0db4a3a5b01d
removed loader/ dependancy, imported some files from g2, also used patches from Dominik Mierzejewski
alex
parents:
12016
diff
changeset
|
27 #include "ms_hdr.h" |
8585 | 28 |
22605
4d81dbdf46b9
Add explicit location for headers from the stream/ directory.
diego
parents:
21660
diff
changeset
|
29 #include "stream/stream.h" |
8585 | 30 #include "muxer.h" |
14753
70c446099f40
new mpeg muxer compatible with dvd/[s]vcd; small changes in the muxer layer (sanity checks in the muxer_init functions)
nicodvb
parents:
12341
diff
changeset
|
31 #include "demuxer.h" |
70c446099f40
new mpeg muxer compatible with dvd/[s]vcd; small changes in the muxer layer (sanity checks in the muxer_init functions)
nicodvb
parents:
12341
diff
changeset
|
32 #include "mp_msg.h" |
70c446099f40
new mpeg muxer compatible with dvd/[s]vcd; small changes in the muxer layer (sanity checks in the muxer_init functions)
nicodvb
parents:
12341
diff
changeset
|
33 #include "help_mp.h" |
70c446099f40
new mpeg muxer compatible with dvd/[s]vcd; small changes in the muxer layer (sanity checks in the muxer_init functions)
nicodvb
parents:
12341
diff
changeset
|
34 #include "stheader.h" |
8585 | 35 |
21660
ca9da45d13e9
muxers now write to output muxer->stream rather than to muxer->file
nicodvb
parents:
18558
diff
changeset
|
36 muxer_t *muxer_new_muxer(int type,stream_t *stream){ |
17839 | 37 muxer_t* muxer=calloc(1,sizeof(muxer_t)); |
17831
f36966ddbf77
exit if calloc() fails; free(muxer) before returning NULL if muxer_init() fails (to avoid memleak). Fixes cid 173
nicodvb
parents:
17487
diff
changeset
|
38 if(!muxer) |
f36966ddbf77
exit if calloc() fails; free(muxer) before returning NULL if muxer_init() fails (to avoid memleak). Fixes cid 173
nicodvb
parents:
17487
diff
changeset
|
39 return NULL; |
21660
ca9da45d13e9
muxers now write to output muxer->stream rather than to muxer->file
nicodvb
parents:
18558
diff
changeset
|
40 muxer->stream = stream; |
8585 | 41 switch (type) { |
42 case MUXER_TYPE_MPEG: | |
14753
70c446099f40
new mpeg muxer compatible with dvd/[s]vcd; small changes in the muxer layer (sanity checks in the muxer_init functions)
nicodvb
parents:
12341
diff
changeset
|
43 if(! muxer_init_muxer_mpeg(muxer)) |
17839 | 44 goto fail; |
8585 | 45 break; |
12016 | 46 case MUXER_TYPE_RAWVIDEO: |
14753
70c446099f40
new mpeg muxer compatible with dvd/[s]vcd; small changes in the muxer layer (sanity checks in the muxer_init functions)
nicodvb
parents:
12341
diff
changeset
|
47 if(! muxer_init_muxer_rawvideo(muxer)) |
17839 | 48 goto fail; |
12016 | 49 break; |
15794
59e8dcea6d9e
messed up ordering of cases, special delivery of Cola to Tobias
henry
parents:
15754
diff
changeset
|
50 case MUXER_TYPE_RAWAUDIO: |
59e8dcea6d9e
messed up ordering of cases, special delivery of Cola to Tobias
henry
parents:
15754
diff
changeset
|
51 if(! muxer_init_muxer_rawaudio(muxer)) |
17839 | 52 goto fail; |
15794
59e8dcea6d9e
messed up ordering of cases, special delivery of Cola to Tobias
henry
parents:
15754
diff
changeset
|
53 break; |
32142
4614728cab25
build system: Merge all FFmpeg library checks into a single FFmpeg check.
diego
parents:
30633
diff
changeset
|
54 #ifdef CONFIG_FFMPEG |
14757
7a2adc5e8928
initial, extremely experimental, libavformat muxer; don't expect anything to work yet
nicodvb
parents:
14753
diff
changeset
|
55 case MUXER_TYPE_LAVF: |
7a2adc5e8928
initial, extremely experimental, libavformat muxer; don't expect anything to work yet
nicodvb
parents:
14753
diff
changeset
|
56 if(! muxer_init_muxer_lavf(muxer)) |
17839 | 57 goto fail; |
14757
7a2adc5e8928
initial, extremely experimental, libavformat muxer; don't expect anything to work yet
nicodvb
parents:
14753
diff
changeset
|
58 break; |
7a2adc5e8928
initial, extremely experimental, libavformat muxer; don't expect anything to work yet
nicodvb
parents:
14753
diff
changeset
|
59 #endif |
8585 | 60 case MUXER_TYPE_AVI: |
61 default: | |
14753
70c446099f40
new mpeg muxer compatible with dvd/[s]vcd; small changes in the muxer layer (sanity checks in the muxer_init functions)
nicodvb
parents:
12341
diff
changeset
|
62 if(! muxer_init_muxer_avi(muxer)) |
17839 | 63 goto fail; |
8585 | 64 } |
65 return muxer; | |
17839 | 66 |
67 fail: | |
68 free(muxer); | |
69 return NULL; | |
8585 | 70 } |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
71 |
32795 | 72 /* Flush the internal muxer buffer. |
73 * This is normally called from muxer_write_chunk() once all streams | |
74 * have seen frames. */ | |
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, MSGTR_MuxbufSending, 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 | |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
123 /* buffer frames until we either: |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
124 * (a) have at least one frame from each stream |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
125 * (b) run out of memory */ |
17487
fa17424b4c7b
change muxer_write_chunk() so that pts/dts _could_ be passed from encoder to muxer
michael
parents:
17354
diff
changeset
|
126 void muxer_write_chunk(muxer_stream_t *s, size_t len, unsigned int flags, double dts, double pts) { |
fa17424b4c7b
change muxer_write_chunk() so that pts/dts _could_ be passed from encoder to muxer
michael
parents:
17354
diff
changeset
|
127 if(dts == MP_NOPTS_VALUE) dts= s->timer; |
fa17424b4c7b
change muxer_write_chunk() so that pts/dts _could_ be passed from encoder to muxer
michael
parents:
17354
diff
changeset
|
128 if(pts == MP_NOPTS_VALUE) pts= s->timer; // this is wrong |
fa17424b4c7b
change muxer_write_chunk() so that pts/dts _could_ be passed from encoder to muxer
michael
parents:
17354
diff
changeset
|
129 |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
130 if (s->muxer->muxbuf_skip_buffer) { |
17487
fa17424b4c7b
change muxer_write_chunk() so that pts/dts _could_ be passed from encoder to muxer
michael
parents:
17354
diff
changeset
|
131 s->muxer->cont_write_chunk(s, len, flags, dts, pts); |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
132 } |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
133 else { |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
134 int num = s->muxer->muxbuf_num++; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
135 muxbuf_t *buf, *tmp; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
136 |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18366
diff
changeset
|
137 tmp = realloc_struct(s->muxer->muxbuf, (num+1), sizeof(muxbuf_t)); |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
138 if(!tmp) { |
17065
cf6bfdf41143
Clean up some muxer messages, patch by Corey Hickey bugfood-ml AT -fatooh/org- , small fixes by me
reynaldo
parents:
17023
diff
changeset
|
139 mp_msg(MSGT_MUXER, MSGL_FATAL, MSGTR_MuxbufReallocErr); |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
140 return; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
141 } |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
142 s->muxer->muxbuf = tmp; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
143 buf = s->muxer->muxbuf + num; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
144 |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
145 /* buffer this frame */ |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
146 buf->stream = s; |
17487
fa17424b4c7b
change muxer_write_chunk() so that pts/dts _could_ be passed from encoder to muxer
michael
parents:
17354
diff
changeset
|
147 buf->dts= dts; |
fa17424b4c7b
change muxer_write_chunk() so that pts/dts _could_ be passed from encoder to muxer
michael
parents:
17354
diff
changeset
|
148 buf->pts= pts; |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
149 buf->len = len; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
150 buf->flags = flags; |
18366 | 151 buf->buffer = malloc(len); |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
152 if (!buf->buffer) { |
17065
cf6bfdf41143
Clean up some muxer messages, patch by Corey Hickey bugfood-ml AT -fatooh/org- , small fixes by me
reynaldo
parents:
17023
diff
changeset
|
153 mp_msg(MSGT_MUXER, MSGL_FATAL, MSGTR_MuxbufMallocErr); |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
154 return; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
155 } |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
156 memcpy(buf->buffer, s->buffer, buf->len); |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
157 s->muxbuf_seen = 1; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
158 |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
159 /* see if we need to keep buffering */ |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
160 s->muxer->muxbuf_skip_buffer = 1; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
161 for (num = 0; s->muxer->streams[num]; ++num) |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
162 if (!s->muxer->streams[num]->muxbuf_seen) |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
163 s->muxer->muxbuf_skip_buffer = 0; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
164 |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
165 /* see if we can flush buffer now */ |
32795 | 166 if (s->muxer->muxbuf_skip_buffer) |
167 muxer_flush(s->muxer); | |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
168 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
169 |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
170 /* this code moved directly from muxer_avi.c */ |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
171 // alter counters: |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
172 if(s->h.dwSampleSize){ |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
173 // CBR |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
174 s->h.dwLength+=len/s->h.dwSampleSize; |
17065
cf6bfdf41143
Clean up some muxer messages, patch by Corey Hickey bugfood-ml AT -fatooh/org- , small fixes by me
reynaldo
parents:
17023
diff
changeset
|
175 if(len%s->h.dwSampleSize) mp_msg(MSGT_MUXER, MSGL_WARN, MSGTR_WarningLenIsntDivisible); |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
176 } else { |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
177 // VBR |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
178 s->h.dwLength++; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
179 } |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
180 s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
181 s->size+=len; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
182 |
17023
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
183 return; |
dd5be8f8d16d
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me
nicodvb
parents:
17012
diff
changeset
|
184 } |