1 /*****************************************************************************
2 * decomp.c : Decompression module for vlc
3 *****************************************************************************
4 * Copyright © 2008-2009 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_stream.h>
28 #include <vlc_network.h>
34 # define _POSIX_SPAWN (-1)
37 #if (_POSIX_SPAWN >= 0)
41 #include <sys/ioctl.h>
42 #if defined (__linux__) && defined (HAVE_VMSPLICE)
44 # include <sys/mman.h>
48 #include <vlc_interrupt.h>
52 static int OpenGzip (vlc_object_t
*);
53 static int OpenBzip2 (vlc_object_t
*);
54 static int OpenXZ (vlc_object_t
*);
55 static void Close (vlc_object_t
*);
58 set_category (CAT_INPUT
)
59 set_subcategory (SUBCAT_INPUT_STREAM_FILTER
)
60 set_capability ("stream_filter", 20)
62 set_description (N_("LZMA decompression"))
63 set_callbacks (OpenXZ
, Close
)
66 set_description (N_("Burrows-Wheeler decompression"))
67 set_callbacks (OpenBzip2
, Close
)
68 /* TODO: access shortnames for vlc_stream_NewURL() */
71 set_description (N_("gzip decompression"))
72 set_callbacks (OpenGzip
, Close
)
95 extern char **environ
;
97 static const size_t bufsize
= 65536;
99 static void cleanup_mmap (void *addr
)
101 munmap (addr
, bufsize
);
105 static void *Thread (void *data
)
107 stream_t
*stream
= data
;
108 stream_sys_t
*p_sys
= stream
->p_sys
;
110 const ssize_t page_mask
= sysconf (_SC_PAGE_SIZE
) - 1;
112 int fd
= p_sys
->write_fd
;
117 sigaddset(&set
, SIGPIPE
);
118 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
123 int canc
= vlc_savecancel ();
125 unsigned char *buf
= mmap (NULL
, bufsize
, PROT_READ
|PROT_WRITE
,
126 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
127 if (unlikely(buf
== MAP_FAILED
))
129 vlc_cleanup_push (cleanup_mmap
, buf
);
131 unsigned char *buf
= malloc (bufsize
);
132 if (unlikely(buf
== NULL
))
134 vlc_cleanup_push (free
, buf
);
137 vlc_mutex_lock (&p_sys
->lock
);
138 while (p_sys
->paused
) /* practically always false, but... */
139 vlc_cond_wait (&p_sys
->wait
, &p_sys
->lock
);
140 len
= vlc_stream_Read (stream
->p_source
, buf
, bufsize
);
141 vlc_mutex_unlock (&p_sys
->lock
);
143 vlc_restorecancel (canc
);
146 for (ssize_t i
= 0, j
; i
< len
; i
+= j
)
149 if ((len
- i
) <= page_mask
) /* incomplete last page */
150 j
= write (fd
, buf
+ i
, len
- i
);
155 .iov_len
= (len
- i
) & ~page_mask
};
157 j
= vmsplice (fd
, &iov
, 1, SPLICE_F_GIFT
);
159 if (j
== -1 && errno
== ENOSYS
) /* vmsplice() not supported */
161 j
= write (fd
, buf
+ i
, len
- i
);
166 msg_Err (stream
, "cannot write data: %s",
167 vlc_strerror_c(errno
));
174 munmap (buf
, bufsize
);
181 msg_Dbg (stream
, "compressed stream at EOF");
182 /* Let child process know about EOF */
183 p_sys
->write_fd
= -1;
189 #define MIN_BLOCK (1 << 10)
190 #define MAX_BLOCK (1 << 20)
192 * Reads decompressed from the decompression program
193 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
195 static ssize_t
Read (stream_t
*stream
, void *buf
, size_t buflen
)
197 stream_sys_t
*sys
= stream
->p_sys
;
198 ssize_t val
= vlc_read_i11e (sys
->read_fd
, buf
, buflen
);
199 return (val
>= 0) ? val
: 0;
205 static int Control (stream_t
*stream
, int query
, va_list args
)
207 stream_sys_t
*p_sys
= stream
->p_sys
;
211 case STREAM_CAN_SEEK
:
212 case STREAM_CAN_FASTSEEK
:
213 *(va_arg (args
, bool *)) = false;
215 case STREAM_CAN_PAUSE
:
216 *(va_arg (args
, bool *)) = p_sys
->can_pause
;
218 case STREAM_CAN_CONTROL_PACE
:
219 *(va_arg (args
, bool *)) = p_sys
->can_pace
;
221 case STREAM_GET_SIZE
:
222 *(va_arg (args
, uint64_t *)) = 0;
224 case STREAM_GET_PTS_DELAY
:
225 *va_arg (args
, int64_t *) = p_sys
->pts_delay
;
227 case STREAM_SET_PAUSE_STATE
:
229 bool paused
= va_arg (args
, unsigned);
231 vlc_mutex_lock (&p_sys
->lock
);
232 vlc_stream_Control(stream
->p_source
, STREAM_SET_PAUSE_STATE
,
234 p_sys
->paused
= paused
;
235 vlc_cond_signal (&p_sys
->wait
);
236 vlc_mutex_unlock (&p_sys
->lock
);
246 * Pipe data through an external executable.
247 * @param stream the stream filter object.
248 * @param path path to the executable.
250 static int Open (stream_t
*stream
, const char *path
)
252 stream_sys_t
*p_sys
= stream
->p_sys
= malloc (sizeof (*p_sys
));
256 vlc_cond_init (&p_sys
->wait
);
257 vlc_mutex_init (&p_sys
->lock
);
258 p_sys
->paused
= false;
260 vlc_stream_Control(stream
->p_source
, STREAM_CAN_PAUSE
, &p_sys
->can_pause
);
261 vlc_stream_Control(stream
->p_source
, STREAM_CAN_CONTROL_PACE
,
263 vlc_stream_Control(stream
->p_source
, STREAM_GET_PTS_DELAY
,
266 /* I am not a big fan of the pyramid style, but I cannot think of anything
267 * better here. There are too many failure cases. */
268 int ret
= VLC_EGENERIC
;
271 /* We use two pipes rather than one stream socket pair, so that we can
272 * use vmsplice() on Linux. */
273 if (vlc_pipe (comp
) == 0)
275 p_sys
->write_fd
= comp
[1];
278 if (vlc_pipe (uncomp
) == 0)
280 p_sys
->read_fd
= uncomp
[0];
282 #if (_POSIX_SPAWN >= 0)
283 posix_spawn_file_actions_t actions
;
284 if (posix_spawn_file_actions_init (&actions
) == 0)
286 char *const argv
[] = { (char *)path
, NULL
};
288 if (!posix_spawn_file_actions_adddup2 (&actions
, comp
[0], 0)
289 && !posix_spawn_file_actions_adddup2 (&actions
, uncomp
[1], 1)
290 && !posix_spawnp (&p_sys
->pid
, path
, &actions
, NULL
, argv
,
293 if (vlc_clone (&p_sys
->thread
, Thread
, stream
,
294 VLC_THREAD_PRIORITY_INPUT
) == 0)
299 msg_Err (stream
, "cannot execute %s", path
);
302 posix_spawn_file_actions_destroy (&actions
);
304 #else /* _POSIX_SPAWN */
305 switch (p_sys
->pid
= fork ())
308 msg_Err (stream
, "cannot fork: %s", vlc_strerror_c(errno
));
313 execlp (path
, path
, (const char *)NULL
);
314 exit (1); /* if we get, execlp() failed! */
316 if (vlc_clone (&p_sys
->thread
, Thread
, stream
,
317 VLC_THREAD_PRIORITY_INPUT
) == 0)
320 #endif /* _POSIX_SPAWN < 0 */
321 vlc_close (uncomp
[1]);
322 if (ret
!= VLC_SUCCESS
)
323 vlc_close (uncomp
[0]);
326 if (ret
!= VLC_SUCCESS
)
330 if (ret
!= VLC_SUCCESS
)
332 if (p_sys
->pid
!= -1)
333 while (waitpid (p_sys
->pid
, &(int){ 0 }, 0) == -1);
334 vlc_mutex_destroy (&p_sys
->lock
);
335 vlc_cond_destroy (&p_sys
->wait
);
340 stream
->pf_read
= Read
;
341 stream
->pf_seek
= NULL
;
342 stream
->pf_control
= Control
;
348 * Releases allocate resources.
350 static void Close (vlc_object_t
*obj
)
352 stream_t
*stream
= (stream_t
*)obj
;
353 stream_sys_t
*p_sys
= stream
->p_sys
;
356 vlc_cancel (p_sys
->thread
);
357 vlc_close (p_sys
->read_fd
);
358 vlc_join (p_sys
->thread
, NULL
);
359 if (p_sys
->write_fd
!= -1)
360 /* Killed before EOF? */
361 vlc_close (p_sys
->write_fd
);
363 msg_Dbg (obj
, "waiting for PID %u", (unsigned)p_sys
->pid
);
364 while (waitpid (p_sys
->pid
, &status
, 0) == -1);
365 msg_Dbg (obj
, "exit status %d", status
);
367 vlc_mutex_destroy (&p_sys
->lock
);
368 vlc_cond_destroy (&p_sys
->wait
);
374 * Detects gzip file format
376 static int OpenGzip (vlc_object_t
*obj
)
378 stream_t
*stream
= (stream_t
*)obj
;
381 if (vlc_stream_Peek (stream
->p_source
, &peek
, 3) < 3)
384 if (memcmp (peek
, "\x1f\x8b\x08", 3))
387 msg_Dbg (obj
, "detected gzip compressed stream");
388 return Open (stream
, "zcat");
393 * Detects bzip2 file format
395 static int OpenBzip2 (vlc_object_t
*obj
)
397 stream_t
*stream
= (stream_t
*)obj
;
400 /* (Try to) parse the bzip2 header */
401 if (vlc_stream_Peek (stream
->p_source
, &peek
, 10) < 10)
404 if (memcmp (peek
, "BZh", 3) || (peek
[3] < '1') || (peek
[3] > '9')
405 || memcmp (peek
+ 4, "\x31\x41\x59\x26\x53\x59", 6))
408 msg_Dbg (obj
, "detected bzip2 compressed stream");
409 return Open (stream
, "bzcat");
413 * Detects xz file format
415 static int OpenXZ (vlc_object_t
*obj
)
417 stream_t
*stream
= (stream_t
*)obj
;
420 /* (Try to) parse the xz stream header */
421 if (vlc_stream_Peek (stream
->p_source
, &peek
, 8) < 8)
424 if (memcmp (peek
, "\xfd\x37\x7a\x58\x5a", 6))
427 msg_Dbg (obj
, "detected xz compressed stream");
428 return Open (stream
, "xzcat");