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