whitespace cosmetics:
[mplayer/glamo.git] / libmpdemux / demux_nut.c
blob8fd46e6921596bd5931ffdc85b9124121e625ccd
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
5 #include "mp_msg.h"
7 #include "help_mp.h"
9 #include "stream/stream.h"
10 #include "demuxer.h"
11 #include "stheader.h"
13 #include <libnut.h>
15 typedef struct {
16 int last_pts; // FIXME
17 nut_context_tt * nut;
18 nut_stream_header_tt * s;
19 } nut_priv_tt;
21 static size_t mp_read(void * h, size_t len, uint8_t * buf) {
22 stream_t * stream = (stream_t*)h;
24 if(stream_eof(stream)) return 0;
25 //len = MIN(len, 5);
27 return stream_read(stream, buf, len);
30 static int mp_eof(void * h) {
31 stream_t * stream = (stream_t*)h;
32 if(stream_eof(stream)) return 1;
33 return 0;
36 static off_t mp_seek(void * h, long long pos, int whence) {
37 stream_t * stream = (stream_t*)h;
39 if (stream->end_pos < stream_tell(stream))
40 stream->end_pos = stream_tell(stream);
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;
46 if (pos < stream->end_pos && stream->eof) stream_reset(stream);
47 if (stream_seek(stream, pos) == 0) return -1;
49 return pos;
52 #define ID_STRING "nut/multimedia container"
53 #define ID_LENGTH (strlen(ID_STRING) + 1)
55 static int nut_check_file(demuxer_t * demuxer) {
56 uint8_t buf[ID_LENGTH];
58 if (stream_read(demuxer->stream, buf, ID_LENGTH) != ID_LENGTH) return 0;
60 if (memcmp(buf, ID_STRING, ID_LENGTH)) return 0;
62 stream_seek(demuxer->stream, 0);
63 return DEMUXER_TYPE_NUT;
66 static demuxer_t * demux_open_nut(demuxer_t * demuxer) {
67 nut_demuxer_opts_tt dopts = {
68 .input = {
69 .priv = demuxer->stream,
70 .seek = mp_seek,
71 .read = mp_read,
72 .eof = mp_eof,
73 .file_pos = stream_tell(demuxer->stream),
75 .alloc = { .malloc = NULL },
76 .read_index = index_mode,
77 .cache_syncpoints = 1,
79 nut_priv_tt * priv = demuxer->priv = calloc(1, sizeof(nut_priv_tt));
80 nut_context_tt * nut = priv->nut = nut_demuxer_init(&dopts);
81 nut_stream_header_tt * s;
82 int ret;
83 int i;
85 while ((ret = nut_read_headers(nut, &s, NULL)) == NUT_ERR_EAGAIN);
86 if (ret) {
87 mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n", nut_error(ret));
88 return NULL;
91 priv->s = s;
93 for (i = 0; s[i].type != -1 && i < 2; i++) switch(s[i].type) {
94 case NUT_AUDIO_CLASS: {
95 WAVEFORMATEX *wf =
96 calloc(sizeof(WAVEFORMATEX) +
97 s[i].codec_specific_len, 1);
98 sh_audio_t* sh_audio = new_sh_audio(demuxer, i);
99 int j;
100 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "nut", i);
102 sh_audio->wf= wf; sh_audio->ds = demuxer->audio;
103 sh_audio->audio.dwSampleSize = 0; // FIXME
104 sh_audio->audio.dwScale = s[i].time_base.num;
105 sh_audio->audio.dwRate = s[i].time_base.den;
106 sh_audio->format = 0;
107 for (j = 0; j < s[i].fourcc_len && j < 4; j++)
108 sh_audio->format |= s[i].fourcc[j]<<(j*8);
109 sh_audio->channels = s[i].channel_count;
110 sh_audio->samplerate =
111 s[i].samplerate_num / s[i].samplerate_denom;
112 sh_audio->i_bps = 0; // FIXME
114 wf->wFormatTag = sh_audio->format;
115 wf->nChannels = s[i].channel_count;
116 wf->nSamplesPerSec =
117 s[i].samplerate_num / s[i].samplerate_denom;
118 wf->nAvgBytesPerSec = 0; // FIXME
119 wf->nBlockAlign = 0; // FIXME
120 wf->wBitsPerSample = 0; // FIXME
121 wf->cbSize = s[i].codec_specific_len;
122 if (s[i].codec_specific_len)
123 memcpy(wf + 1, s[i].codec_specific,
124 s[i].codec_specific_len);
126 demuxer->audio->id = i;
127 demuxer->audio->sh= demuxer->a_streams[i];
128 break;
130 case NUT_VIDEO_CLASS: {
131 BITMAPINFOHEADER * bih =
132 calloc(sizeof(BITMAPINFOHEADER) +
133 s[i].codec_specific_len, 1);
134 sh_video_t * sh_video = new_sh_video(demuxer, i);
135 int j;
136 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "nut", i);
138 sh_video->bih = bih;
139 sh_video->ds = demuxer->video;
140 sh_video->disp_w = s[i].width;
141 sh_video->disp_h = s[i].height;
142 sh_video->video.dwScale = s[i].time_base.num;
143 sh_video->video.dwRate = s[i].time_base.den;
145 sh_video->fps = sh_video->video.dwRate/
146 (float)sh_video->video.dwScale;
147 sh_video->frametime = 1./sh_video->fps;
148 sh_video->format = 0;
149 for (j = 0; j < s[i].fourcc_len && j < 4; j++)
150 sh_video->format |= s[i].fourcc[j]<<(j*8);
151 if (!s[i].sample_height) sh_video->aspect = 0;
152 else sh_video->aspect =
153 s[i].sample_width / (float)s[i].sample_height;
154 sh_video->i_bps = 0; // FIXME
156 bih->biSize = sizeof(BITMAPINFOHEADER) +
157 s[i].codec_specific_len;
158 bih->biWidth = s[i].width;
159 bih->biHeight = s[i].height;
160 bih->biBitCount = 0; // FIXME
161 bih->biSizeImage = 0; // FIXME
162 bih->biCompression = sh_video->format;
164 if (s[i].codec_specific_len)
165 memcpy(bih + 1, s[i].codec_specific,
166 s[i].codec_specific_len);
168 demuxer->video->id = i;
169 demuxer->video->sh = demuxer->v_streams[i];
170 break;
174 return demuxer;
177 static int demux_nut_fill_buffer(demuxer_t * demuxer, demux_stream_t * dsds) {
178 nut_priv_tt * priv = demuxer->priv;
179 nut_context_tt * nut = priv->nut;
180 demux_packet_t *dp;
181 demux_stream_t *ds;
182 nut_packet_tt pd;
183 int ret;
184 double pts;
186 demuxer->filepos = stream_tell(demuxer->stream);
187 if (stream_eof(demuxer->stream)) return 0;
189 while ((ret = nut_read_next_packet(nut, &pd)) == NUT_ERR_EAGAIN);
190 if (ret) {
191 if (ret != NUT_ERR_EOF)
192 mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n",
193 nut_error(ret));
194 return 0; // fatal error
197 pts = (double)pd.pts * priv->s[pd.stream].time_base.num /
198 priv->s[pd.stream].time_base.den;
200 if (pd.stream == demuxer->audio->id) {
201 ds = demuxer->audio;
203 else if (pd.stream == demuxer->video->id) {
204 ds = demuxer->video;
206 else {
207 uint8_t buf[pd.len];
208 while ((ret = nut_read_frame(nut, &pd.len, buf)) == NUT_ERR_EAGAIN);
209 if (ret) {
210 mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n",
211 nut_error(ret));
212 return 0; // fatal error
214 return 1;
217 if (pd.stream == 0) priv->last_pts = pd.pts;
219 dp = new_demux_packet(pd.len);
221 dp->pts = pts;
223 dp->pos = demuxer->filepos;
224 dp->flags= (pd.flags & NUT_FLAG_KEY) ? 0x10 : 0;
226 {int len = pd.len;
227 while ((ret = nut_read_frame(nut, &len, dp->buffer + pd.len-len)) == NUT_ERR_EAGAIN);
229 if (ret) {
230 mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n",
231 nut_error(ret));
232 return 0; // fatal error
235 ds_add_packet(ds, dp); // append packet to DS stream
236 return 1;
239 static void demux_seek_nut(demuxer_t * demuxer, float time_pos, float audio_delay, int flags) {
240 nut_context_tt * nut = ((nut_priv_tt*)demuxer->priv)->nut;
241 nut_priv_tt * priv = demuxer->priv;
242 int nutflags = 0;
243 int ret;
244 const int tmp[] = { 0, -1 };
246 if (!(flags & SEEK_ABSOLUTE)) {
247 nutflags |= 1; // relative
248 if (time_pos > 0) nutflags |= 2; // forwards
251 if (flags & SEEK_FACTOR)
252 time_pos *= priv->s[0].max_pts *
253 (double)priv->s[0].time_base.num /
254 priv->s[0].time_base.den;
256 while ((ret = nut_seek(nut, time_pos, nutflags, tmp)) == NUT_ERR_EAGAIN);
257 priv->last_pts = -1;
258 if (ret) mp_msg(MSGT_HEADER, MSGL_ERR, "NUT error: %s\n", nut_error(ret));
259 demuxer->filepos = stream_tell(demuxer->stream);
262 static int demux_control_nut(demuxer_t * demuxer, int cmd, void * arg) {
263 nut_priv_tt * priv = demuxer->priv;
264 switch (cmd) {
265 case DEMUXER_CTRL_GET_TIME_LENGTH:
266 *((double *)arg) = priv->s[0].max_pts *
267 (double)priv->s[0].time_base.num /
268 priv->s[0].time_base.den;
269 return DEMUXER_CTRL_OK;
270 case DEMUXER_CTRL_GET_PERCENT_POS:
271 if (priv->s[0].max_pts == 0 || priv->last_pts == -1)
272 return DEMUXER_CTRL_DONTKNOW;
273 *((int *)arg) = priv->last_pts * 100 /
274 (double)priv->s[0].max_pts;
275 return DEMUXER_CTRL_OK;
276 default:
277 return DEMUXER_CTRL_NOTIMPL;
281 static void demux_close_nut(demuxer_t *demuxer) {
282 nut_priv_tt * priv = demuxer->priv;
283 if (!priv) return;
284 nut_demuxer_uninit(priv->nut);
285 free(demuxer->priv);
286 demuxer->priv = NULL;
290 const demuxer_desc_t demuxer_desc_nut = {
291 "NUT demuxer",
292 "nut",
293 "libnut",
294 "Oded Shimon (ods15)",
295 "NUT demuxer, requires libnut",
296 DEMUXER_TYPE_NUT,
297 1, // safe check demuxer
298 nut_check_file,
299 demux_nut_fill_buffer,
300 demux_open_nut,
301 demux_close_nut,
302 demux_seek_nut,
303 demux_control_nut