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