Mercurial > mplayer.hg
annotate libmpdemux/demux_nut.c @ 21032:ddc587b1fd8b
Support multiple subs in mov
author | reimar |
---|---|
date | Sun, 19 Nov 2006 11:55:03 +0000 |
parents | f5180197d7ad |
children | a804f969af97 |
rev | line source |
---|---|
19861 | 1 #include <stdio.h> |
20815 | 2 #include <stdlib.h> |
19861 | 3 |
4 #include "config.h" | |
5 #include "mp_msg.h" | |
6 | |
7 #include "stream.h" | |
8 #include "demuxer.h" | |
9 #include "stheader.h" | |
10 | |
11 #define USE_LIBNUT | |
12 #ifdef USE_LIBNUT | |
13 | |
20893 | 14 #include <libnut.h> |
19861 | 15 |
16 typedef struct { | |
17 int last_pts; // FIXME | |
18 nut_context_t * nut; | |
19 nut_stream_header_t * s; | |
20 } nut_priv_t; | |
21 | |
22 static size_t mp_read(void * h, size_t len, uint8_t * buf) { | |
23 stream_t * stream = (stream_t*)h; | |
24 | |
25 if(stream_eof(stream)) return 0; | |
21022 | 26 //len = MIN(len, 5); |
19861 | 27 |
28 return stream_read(stream, buf, len); | |
29 } | |
30 | |
21022 | 31 static int mp_eof(void * h) { |
32 stream_t * stream = (stream_t*)h; | |
33 if(stream_eof(stream)) return 1; | |
34 return 0; | |
35 } | |
36 | |
19861 | 37 static off_t mp_seek(void * h, long long pos, int whence) { |
38 stream_t * stream = (stream_t*)h; | |
39 | |
40 if (stream->end_pos < stream_tell(stream)) | |
41 stream->end_pos = stream_tell(stream); | |
42 | |
43 if (whence == SEEK_CUR) pos += stream_tell(stream); | |
44 else if (whence == SEEK_END) pos += stream->end_pos; | |
45 else if (whence != SEEK_SET) return -1; | |
46 | |
47 if (pos < stream->end_pos && stream->eof) stream_reset(stream); | |
48 if (stream_seek(stream, pos) == 0) return -1; | |
49 | |
50 return pos; | |
51 } | |
52 | |
53 #define ID_STRING "nut/multimedia container" | |
54 #define ID_LENGTH (strlen(ID_STRING) + 1) | |
55 | |
56 static int nut_check_file(demuxer_t * demuxer) { | |
57 uint8_t buf[ID_LENGTH]; | |
58 | |
59 if (stream_read(demuxer->stream, buf, ID_LENGTH) != ID_LENGTH) return 0; | |
60 | |
61 if (memcmp(buf, ID_STRING, ID_LENGTH)) return 0; | |
62 | |
63 stream_seek(demuxer->stream, 0); | |
64 return DEMUXER_TYPE_NUT; | |
65 } | |
66 | |
67 static demuxer_t * demux_open_nut(demuxer_t * demuxer) { | |
68 extern int index_mode; | |
69 nut_demuxer_opts_t dopts = { | |
70 .input = { | |
71 .priv = demuxer->stream, | |
72 .seek = mp_seek, | |
73 .read = mp_read, | |
21022 | 74 .eof = mp_eof, |
19861 | 75 .file_pos = stream_tell(demuxer->stream), |
76 }, | |
19957 | 77 .alloc = { .malloc = NULL }, |
20909 | 78 .read_index = index_mode, |
79 .cache_syncpoints = 1, | |
19861 | 80 }; |
21011
b3fbda23e570
move demux_nut priv calloc to init() instead of check_file()
ods15
parents:
20971
diff
changeset
|
81 nut_priv_t * priv = demuxer->priv = calloc(1, sizeof(nut_priv_t)); |
19861 | 82 nut_context_t * nut = priv->nut = nut_demuxer_init(&dopts); |
83 nut_stream_header_t * s; | |
84 int ret; | |
85 int i; | |
86 | |
21022 | 87 while ((ret = nut_read_headers(nut, &s, NULL)) == NUT_ERR_EAGAIN); |
88 if (ret) { | |
20938 | 89 mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n", nut_error(ret)); |
19861 | 90 nut_demuxer_uninit(nut); |
91 free(priv); | |
92 return NULL; | |
93 } | |
94 | |
95 priv->s = s; | |
96 | |
97 for (i = 0; s[i].type != -1 && i < 2; i++) switch(s[i].type) { | |
98 case NUT_AUDIO_CLASS: { | |
99 WAVEFORMATEX *wf = | |
100 calloc(sizeof(WAVEFORMATEX) + | |
101 s[i].codec_specific_len, 1); | |
102 sh_audio_t* sh_audio = new_sh_audio(demuxer, i); | |
103 int j; | |
104 | |
105 sh_audio->wf= wf; sh_audio->ds = demuxer->audio; | |
106 sh_audio->audio.dwSampleSize = 0; // FIXME | |
107 sh_audio->audio.dwScale = s[i].time_base.nom; | |
108 sh_audio->audio.dwRate = s[i].time_base.den; | |
109 sh_audio->format = 0; | |
110 for (j = 0; j < s[i].fourcc_len && j < 4; j++) | |
111 sh_audio->format |= s[i].fourcc[j]<<(j*8); | |
112 sh_audio->channels = s[i].channel_count; | |
113 sh_audio->samplerate = | |
114 s[i].samplerate_nom / s[i].samplerate_denom; | |
115 sh_audio->i_bps = 0; // FIXME | |
116 | |
117 wf->wFormatTag = sh_audio->format; | |
118 wf->nChannels = s[i].channel_count; | |
119 wf->nSamplesPerSec = | |
120 s[i].samplerate_nom / s[i].samplerate_denom; | |
121 wf->nAvgBytesPerSec = 0; // FIXME | |
122 wf->nBlockAlign = 0; // FIXME | |
123 wf->wBitsPerSample = 0; // FIXME | |
124 wf->cbSize = s[i].codec_specific_len; | |
125 if (s[i].codec_specific_len) | |
126 memcpy(wf + 1, s[i].codec_specific, | |
127 s[i].codec_specific_len); | |
128 | |
129 demuxer->audio->id = i; | |
130 demuxer->audio->sh= demuxer->a_streams[i]; | |
131 break; | |
132 } | |
133 case NUT_VIDEO_CLASS: { | |
134 BITMAPINFOHEADER * bih = | |
135 calloc(sizeof(BITMAPINFOHEADER) + | |
136 s[i].codec_specific_len, 1); | |
137 sh_video_t * sh_video = new_sh_video(demuxer, i); | |
138 int j; | |
139 | |
140 sh_video->bih = bih; | |
141 sh_video->ds = demuxer->video; | |
142 sh_video->disp_w = s[i].width; | |
143 sh_video->disp_h = s[i].height; | |
144 sh_video->video.dwScale = s[i].time_base.nom; | |
145 sh_video->video.dwRate = s[i].time_base.den; | |
146 | |
147 sh_video->fps = sh_video->video.dwRate/ | |
148 (float)sh_video->video.dwScale; | |
149 sh_video->frametime = 1./sh_video->fps; | |
150 sh_video->format = 0; | |
151 for (j = 0; j < s[i].fourcc_len && j < 4; j++) | |
152 sh_video->format |= s[i].fourcc[j]<<(j*8); | |
153 if (!s[i].sample_height) sh_video->aspect = 0; | |
154 else sh_video->aspect = | |
155 s[i].sample_width / (float)s[i].sample_height; | |
156 sh_video->i_bps = 0; // FIXME | |
157 | |
158 bih->biSize = sizeof(BITMAPINFOHEADER) + | |
159 s[i].codec_specific_len; | |
160 bih->biWidth = s[i].width; | |
161 bih->biHeight = s[i].height; | |
162 bih->biBitCount = 0; // FIXME | |
163 bih->biSizeImage = 0; // FIXME | |
164 bih->biCompression = sh_video->format; | |
165 | |
166 if (s[i].codec_specific_len) | |
167 memcpy(bih + 1, s[i].codec_specific, | |
168 s[i].codec_specific_len); | |
169 | |
170 demuxer->video->id = i; | |
171 demuxer->video->sh = demuxer->v_streams[i]; | |
172 break; | |
173 } | |
174 } | |
175 | |
176 return demuxer; | |
177 } | |
178 | |
179 static int demux_nut_fill_buffer(demuxer_t * demuxer, demux_stream_t * dsds) { | |
180 nut_priv_t * priv = demuxer->priv; | |
181 nut_context_t * nut = priv->nut; | |
182 demux_packet_t *dp; | |
183 demux_stream_t *ds; | |
184 nut_packet_t pd; | |
185 int ret; | |
186 double pts; | |
187 | |
188 demuxer->filepos = stream_tell(demuxer->stream); | |
189 if (stream_eof(demuxer->stream)) return 0; | |
190 | |
21022 | 191 while ((ret = nut_read_next_packet(nut, &pd)) == NUT_ERR_EAGAIN); |
20938 | 192 if (ret) { |
193 if (ret != NUT_ERR_EOF) | |
194 mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n", | |
195 nut_error(ret)); | |
196 return 0; // fatal error | |
19861 | 197 } |
198 | |
199 pts = (double)pd.pts * priv->s[pd.stream].time_base.nom / | |
200 priv->s[pd.stream].time_base.den; | |
201 | |
202 if (pd.stream == demuxer->audio->id) { | |
203 ds = demuxer->audio; | |
204 if (!demuxer->video->sh) { | |
205 ((sh_audio_t*)ds->sh)->delay = pts; | |
206 } | |
207 } | |
208 else if (pd.stream == demuxer->video->id) { | |
209 ds = demuxer->video; | |
210 } | |
211 else { | |
20926 | 212 uint8_t buf[pd.len]; |
21022 | 213 while ((ret = nut_read_frame(nut, &pd.len, buf)) == NUT_ERR_EAGAIN); |
20938 | 214 if (ret) { |
19958 | 215 mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n", |
20938 | 216 nut_error(ret)); |
217 return 0; // fatal error | |
19861 | 218 } |
219 return 1; | |
220 } | |
221 | |
222 if (pd.stream == 0) priv->last_pts = pd.pts; | |
223 | |
224 dp = new_demux_packet(pd.len); | |
225 | |
226 dp->pts = pts; | |
227 | |
228 dp->pos = demuxer->filepos; | |
229 dp->flags= (pd.flags & NUT_FLAG_KEY) ? 0x10 : 0; | |
230 | |
21022 | 231 {int len = pd.len; |
232 while ((ret = nut_read_frame(nut, &len, dp->buffer + pd.len-len)) == NUT_ERR_EAGAIN); | |
233 } | |
20938 | 234 if (ret) { |
19958 | 235 mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n", |
20938 | 236 nut_error(ret)); |
237 return 0; // fatal error | |
19861 | 238 } |
19958 | 239 |
19861 | 240 ds_add_packet(ds, dp); // append packet to DS stream |
241 return 1; | |
242 } | |
243 | |
244 static void demux_seek_nut(demuxer_t * demuxer, float time_pos, float audio_delay, int flags) { | |
245 nut_context_t * nut = ((nut_priv_t*)demuxer->priv)->nut; | |
246 nut_priv_t * priv = demuxer->priv; | |
247 sh_audio_t * sh_audio = demuxer->audio->sh; | |
248 int nutflags = 0; | |
249 int ret; | |
250 const int tmp[] = { 0, -1 }; | |
251 | |
252 if (!(flags & 1)) { | |
253 nutflags |= 1; // relative | |
254 if (time_pos > 0) nutflags |= 2; // forwards | |
255 } | |
256 | |
257 if (flags & 2) // percent | |
258 time_pos *= priv->s[0].max_pts * | |
259 (double)priv->s[0].time_base.nom / | |
260 priv->s[0].time_base.den; | |
261 | |
21022 | 262 while ((ret = nut_seek(nut, time_pos, nutflags, tmp)) == NUT_ERR_EAGAIN); |
21023
f5180197d7ad
fix demux_nut to give proper (estimate) of percent position after a seek
ods15
parents:
21022
diff
changeset
|
263 priv->last_pts = -1; |
20971
e243a3de18c2
missed piece in update to libnut API - non negative errors
ods15
parents:
20938
diff
changeset
|
264 if (ret) mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n", nut_error(ret)); |
19861 | 265 if (sh_audio) resync_audio_stream(sh_audio); |
21023
f5180197d7ad
fix demux_nut to give proper (estimate) of percent position after a seek
ods15
parents:
21022
diff
changeset
|
266 demuxer->filepos = stream_tell(demuxer->stream); |
19861 | 267 } |
268 | |
269 static int demux_control_nut(demuxer_t * demuxer, int cmd, void * arg) { | |
270 nut_priv_t * priv = demuxer->priv; | |
271 switch (cmd) { | |
272 case DEMUXER_CTRL_GET_TIME_LENGTH: | |
273 *((double *)arg) = priv->s[0].max_pts * | |
274 (double)priv->s[0].time_base.nom / | |
275 priv->s[0].time_base.den; | |
276 return DEMUXER_CTRL_OK; | |
277 case DEMUXER_CTRL_GET_PERCENT_POS: | |
21023
f5180197d7ad
fix demux_nut to give proper (estimate) of percent position after a seek
ods15
parents:
21022
diff
changeset
|
278 if (priv->s[0].max_pts == 0 || priv->last_pts == -1) |
19861 | 279 return DEMUXER_CTRL_DONTKNOW; |
280 *((int *)arg) = priv->last_pts * 100 / | |
281 (double)priv->s[0].max_pts; | |
282 return DEMUXER_CTRL_OK; | |
283 default: | |
284 return DEMUXER_CTRL_NOTIMPL; | |
285 } | |
286 } | |
287 | |
288 static void demux_close_nut(demuxer_t *demuxer) { | |
20925 | 289 nut_priv_t * priv = demuxer->priv; |
21011
b3fbda23e570
move demux_nut priv calloc to init() instead of check_file()
ods15
parents:
20971
diff
changeset
|
290 if (!priv) return; |
20925 | 291 nut_demuxer_uninit(priv->nut); |
19861 | 292 free(demuxer->priv); |
293 demuxer->priv = NULL; | |
294 } | |
295 | |
296 | |
297 demuxer_desc_t demuxer_desc_nut = { | |
298 "NUT demuxer", | |
299 "nut", | |
300 "libnut", | |
301 "Oded Shimon (ods15)", | |
302 "NUT demuxer, requires libnut", | |
303 DEMUXER_TYPE_NUT, | |
304 1, // safe check demuxer | |
305 nut_check_file, | |
306 demux_nut_fill_buffer, | |
307 demux_open_nut, | |
308 demux_close_nut, | |
309 demux_seek_nut, | |
310 demux_control_nut | |
311 }; | |
312 | |
313 #endif // USE_LIBNUT | |
314 |