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>
49 static int OpenGzip (vlc_object_t *);
50 static int OpenBzip2 (vlc_object_t *);
51 static int OpenXZ (vlc_object_t *);
52 static void Close (vlc_object_t *);
55 set_category (CAT_INPUT)
56 set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
57 set_capability ("stream_filter", 20)
59 set_description (N_("LZMA decompression"))
60 set_callbacks (OpenXZ, Close)
63 set_description (N_("Burrows-Wheeler decompression"))
64 set_callbacks (OpenBzip2, Close)
65 /* TODO: access shortnames for stream_UrlNew() */
68 set_description (N_("gzip decompression"))
69 set_callbacks (OpenGzip, Close)
94 extern char **environ;
96 static const size_t bufsize = 65536;
98 static void cleanup_mmap (void *addr)
100 munmap (addr, bufsize);
104 static void *Thread (void *data)
106 stream_t *stream = data;
107 stream_sys_t *p_sys = stream->p_sys;
109 const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
111 int fd = p_sys->write_fd;
117 int canc = vlc_savecancel ();
119 unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
120 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
121 if (unlikely(buf == MAP_FAILED))
123 vlc_cleanup_push (cleanup_mmap, buf);
125 unsigned char *buf = malloc (bufsize);
126 if (unlikely(buf == NULL))
128 vlc_cleanup_push (free, buf);
131 vlc_mutex_lock (&p_sys->lock);
132 while (p_sys->paused) /* practically always false, but... */
133 vlc_cond_wait (&p_sys->wait, &p_sys->lock);
134 len = stream_Read (stream->p_source, buf, bufsize);
135 vlc_mutex_unlock (&p_sys->lock);
137 vlc_restorecancel (canc);
140 for (ssize_t i = 0, j; i < len; i += j)
143 if ((len - i) <= page_mask) /* incomplete last page */
144 j = write (fd, buf + i, len - i);
147 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
148 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
150 if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
152 j = write (fd, buf + i, len - i);
157 msg_Err (stream, "cannot write data (%m)");
162 vlc_cleanup_run (); /* free (buf) */
166 msg_Dbg (stream, "compressed stream at EOF");
167 /* Let child process know about EOF */
168 p_sys->write_fd = -1;
174 static int Peek (stream_t *, const uint8_t **, unsigned int);
176 #define MIN_BLOCK (1 << 10)
177 #define MAX_BLOCK (1 << 20)
179 * Reads decompressed from the decompression program
180 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
182 static int Read (stream_t *stream, void *buf, unsigned int buflen)
184 stream_sys_t *p_sys = stream->p_sys;
188 if (buf == NULL) /* caller skips data, get big enough peek buffer */
189 buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
191 if ((peeked = p_sys->peeked) != NULL)
192 { /* dequeue peeked data */
193 length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
196 memcpy (buf, peeked->p_buffer, length);
197 buf = ((char *)buf) + length;
200 peeked->p_buffer += length;
201 peeked->i_buffer -= length;
202 if (peeked->i_buffer == 0)
204 block_Release (peeked);
205 p_sys->peeked = NULL;
207 p_sys->offset += length;
210 length += Read (stream, ((char *)buf) + length, buflen - length);
213 assert ((buf != NULL) || (buflen == 0));
215 length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
218 p_sys->offset += length;
225 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
227 stream_sys_t *p_sys = stream->p_sys;
228 block_t *peeked = p_sys->peeked;
230 int fd = p_sys->read_fd;
233 peeked = block_Alloc (len);
234 else if ((curlen = peeked->i_buffer) < len)
235 peeked = block_Realloc (peeked, 0, len);
237 if ((p_sys->peeked = peeked) == NULL)
242 ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
247 peeked->i_buffer = curlen;
250 *pbuf = peeked->p_buffer;
257 static int Control (stream_t *stream, int query, va_list args)
259 stream_sys_t *p_sys = stream->p_sys;
263 case STREAM_CAN_SEEK:
264 case STREAM_CAN_FASTSEEK:
265 *(va_arg (args, bool *)) = false;
267 case STREAM_CAN_PAUSE:
268 *(va_arg (args, bool *)) = p_sys->can_pause;
270 case STREAM_CAN_CONTROL_PACE:
271 *(va_arg (args, bool *)) = p_sys->can_pace;
273 case STREAM_GET_POSITION:
274 *(va_arg (args, uint64_t *)) = p_sys->offset;
276 case STREAM_GET_SIZE:
277 *(va_arg (args, uint64_t *)) = 0;
279 case STREAM_SET_PAUSE_STATE:
281 bool paused = va_arg (args, unsigned);
283 vlc_mutex_lock (&p_sys->lock);
284 stream_Control (stream->p_source, STREAM_SET_PAUSE_STATE, paused);
285 p_sys->paused = paused;
286 vlc_cond_signal (&p_sys->wait);
287 vlc_mutex_unlock (&p_sys->lock);
297 * Pipe data through an external executable.
298 * @param stream the stream filter object.
299 * @param path path to the executable.
301 static int Open (stream_t *stream, const char *path)
303 stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
307 stream->pf_read = Read;
308 stream->pf_peek = Peek;
309 stream->pf_control = Control;
311 vlc_cond_init (&p_sys->wait);
312 vlc_mutex_init (&p_sys->lock);
313 p_sys->paused = false;
316 p_sys->peeked = NULL;
317 stream_Control (stream->p_source, STREAM_CAN_PAUSE, &p_sys->can_pause);
318 stream_Control (stream->p_source, STREAM_CAN_CONTROL_PACE,
321 /* I am not a big fan of the pyramid style, but I cannot think of anything
322 * better here. There are too many failure cases. */
323 int ret = VLC_EGENERIC;
326 /* We use two pipes rather than one stream socket pair, so that we can
327 * use vmsplice() on Linux. */
328 if (vlc_pipe (comp) == 0)
330 p_sys->write_fd = comp[1];
333 if (vlc_pipe (uncomp) == 0)
335 p_sys->read_fd = uncomp[0];
337 #if (_POSIX_SPAWN >= 0)
338 posix_spawn_file_actions_t actions;
339 if (posix_spawn_file_actions_init (&actions) == 0)
341 char *const argv[] = { (char *)path, NULL };
343 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
344 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
345 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
348 if (vlc_clone (&p_sys->thread, Thread, stream,
349 VLC_THREAD_PRIORITY_INPUT) == 0)
354 msg_Err (stream, "Cannot execute %s", path);
357 posix_spawn_file_actions_destroy (&actions);
359 #else /* _POSIX_SPAWN */
360 switch (p_sys->pid = fork ())
363 msg_Err (stream, "Cannot fork (%m)");
368 execlp (path, path, (char *)NULL);
369 exit (1); /* if we get, execlp() failed! */
371 if (vlc_clone (&p_sys->thread, Thread, stream,
372 VLC_THREAD_PRIORITY_INPUT) == 0)
375 #endif /* _POSIX_SPAWN < 0 */
377 if (ret != VLC_SUCCESS)
381 if (ret != VLC_SUCCESS)
385 if (ret == VLC_SUCCESS)
388 if (p_sys->pid != -1)
389 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
390 vlc_mutex_destroy (&p_sys->lock);
391 vlc_cond_destroy (&p_sys->wait);
398 * Releases allocate resources.
400 static void Close (vlc_object_t *obj)
402 stream_t *stream = (stream_t *)obj;
403 stream_sys_t *p_sys = stream->p_sys;
406 vlc_cancel (p_sys->thread);
407 close (p_sys->read_fd);
408 vlc_join (p_sys->thread, NULL);
409 if (p_sys->write_fd != -1)
410 /* Killed before EOF? */
411 close (p_sys->write_fd);
413 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
414 while (waitpid (p_sys->pid, &status, 0) == -1);
415 msg_Dbg (obj, "exit status %d", status);
418 block_Release (p_sys->peeked);
419 vlc_mutex_destroy (&p_sys->lock);
420 vlc_cond_destroy (&p_sys->wait);
426 * Detects gzip file format
428 static int OpenGzip (vlc_object_t *obj)
430 stream_t *stream = (stream_t *)obj;
433 if (stream_Peek (stream->p_source, &peek, 3) < 3)
436 if (memcmp (peek, "\x1f\x8b\x08", 3))
439 msg_Dbg (obj, "detected gzip compressed stream");
440 return Open (stream, "zcat");
445 * Detects bzip2 file format
447 static int OpenBzip2 (vlc_object_t *obj)
449 stream_t *stream = (stream_t *)obj;
452 /* (Try to) parse the bzip2 header */
453 if (stream_Peek (stream->p_source, &peek, 10) < 10)
456 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
457 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
460 msg_Dbg (obj, "detected bzip2 compressed stream");
461 return Open (stream, "bzcat");
465 * Detects xz file format
467 static int OpenXZ (vlc_object_t *obj)
469 stream_t *stream = (stream_t *)obj;
472 /* (Try to) parse the xz stream header */
473 if (stream_Peek (stream->p_source, &peek, 8) < 8)
476 if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
479 msg_Dbg (obj, "detected xz compressed stream");
480 return Open (stream, "xzcat");