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