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