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)
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;
118 int canc = vlc_savecancel ();
120 unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
121 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
122 if (unlikely(buf == MAP_FAILED))
124 vlc_cleanup_push (cleanup_mmap, buf);
126 unsigned char *buf = malloc (bufsize);
127 if (unlikely(buf == NULL))
129 vlc_cleanup_push (free, buf);
132 vlc_mutex_lock (&p_sys->lock);
133 while (p_sys->paused) /* practically always false, but... */
134 vlc_cond_wait (&p_sys->wait, &p_sys->lock);
135 len = stream_Read (stream->p_source, buf, bufsize);
136 vlc_mutex_unlock (&p_sys->lock);
138 vlc_restorecancel (canc);
141 for (ssize_t i = 0, j; i < len; i += j)
144 if ((len - i) <= page_mask) /* incomplete last page */
145 j = write (fd, buf + i, len - i);
148 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
149 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
151 if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
153 j = write (fd, buf + i, len - i);
158 msg_Err (stream, "cannot write data: %s",
159 vlc_strerror_c(errno));
164 vlc_cleanup_run (); /* free (buf) */
168 msg_Dbg (stream, "compressed stream at EOF");
169 /* Let child process know about EOF */
170 p_sys->write_fd = -1;
176 static int Peek (stream_t *, const uint8_t **, unsigned int);
178 #define MIN_BLOCK (1 << 10)
179 #define MAX_BLOCK (1 << 20)
181 * Reads decompressed from the decompression program
182 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
184 static int Read (stream_t *stream, void *buf, unsigned int buflen)
186 stream_sys_t *p_sys = stream->p_sys;
190 if (buf == NULL) /* caller skips data, get big enough peek buffer */
191 buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
193 if ((peeked = p_sys->peeked) != NULL)
194 { /* dequeue peeked data */
195 length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
198 memcpy (buf, peeked->p_buffer, length);
199 buf = ((char *)buf) + length;
202 peeked->p_buffer += length;
203 peeked->i_buffer -= length;
204 if (peeked->i_buffer == 0)
206 block_Release (peeked);
207 p_sys->peeked = NULL;
209 p_sys->offset += length;
212 length += Read (stream, ((char *)buf) + length, buflen - length);
215 assert ((buf != NULL) || (buflen == 0));
217 length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
220 p_sys->offset += length;
227 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
229 stream_sys_t *p_sys = stream->p_sys;
230 block_t *peeked = p_sys->peeked;
232 int fd = p_sys->read_fd;
235 peeked = block_Alloc (len);
236 else if ((curlen = peeked->i_buffer) < len)
237 peeked = block_Realloc (peeked, 0, len);
239 if ((p_sys->peeked = peeked) == NULL)
244 ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
245 len - curlen, false);
249 peeked->i_buffer = curlen;
251 *pbuf = peeked->p_buffer;
258 static int Control (stream_t *stream, int query, va_list args)
260 stream_sys_t *p_sys = stream->p_sys;
264 case STREAM_CAN_SEEK:
265 case STREAM_CAN_FASTSEEK:
266 *(va_arg (args, bool *)) = false;
268 case STREAM_CAN_PAUSE:
269 *(va_arg (args, bool *)) = p_sys->can_pause;
271 case STREAM_CAN_CONTROL_PACE:
272 *(va_arg (args, bool *)) = p_sys->can_pace;
274 case STREAM_GET_POSITION:
275 *(va_arg (args, uint64_t *)) = p_sys->offset;
277 case STREAM_GET_SIZE:
278 *(va_arg (args, uint64_t *)) = 0;
280 case STREAM_GET_PTS_DELAY:
281 *va_arg (args, int64_t *) = p_sys->pts_delay;
283 case STREAM_SET_PAUSE_STATE:
285 bool paused = va_arg (args, unsigned);
287 vlc_mutex_lock (&p_sys->lock);
288 stream_Control (stream->p_source, STREAM_SET_PAUSE_STATE, paused);
289 p_sys->paused = paused;
290 vlc_cond_signal (&p_sys->wait);
291 vlc_mutex_unlock (&p_sys->lock);
301 * Pipe data through an external executable.
302 * @param stream the stream filter object.
303 * @param path path to the executable.
305 static int Open (stream_t *stream, const char *path)
307 stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
311 stream->pf_read = Read;
312 stream->pf_peek = Peek;
313 stream->pf_control = Control;
315 vlc_cond_init (&p_sys->wait);
316 vlc_mutex_init (&p_sys->lock);
317 p_sys->paused = false;
320 p_sys->peeked = NULL;
321 stream_Control (stream->p_source, STREAM_CAN_PAUSE, &p_sys->can_pause);
322 stream_Control (stream->p_source, STREAM_CAN_CONTROL_PACE,
324 stream_Control (stream->p_source, STREAM_GET_PTS_DELAY, &p_sys->pts_delay);
326 /* I am not a big fan of the pyramid style, but I cannot think of anything
327 * better here. There are too many failure cases. */
328 int ret = VLC_EGENERIC;
331 /* We use two pipes rather than one stream socket pair, so that we can
332 * use vmsplice() on Linux. */
333 if (vlc_pipe (comp) == 0)
335 p_sys->write_fd = comp[1];
338 if (vlc_pipe (uncomp) == 0)
340 p_sys->read_fd = uncomp[0];
342 #if (_POSIX_SPAWN >= 0)
343 posix_spawn_file_actions_t actions;
344 if (posix_spawn_file_actions_init (&actions) == 0)
346 char *const argv[] = { (char *)path, NULL };
348 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
349 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
350 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
353 if (vlc_clone (&p_sys->thread, Thread, stream,
354 VLC_THREAD_PRIORITY_INPUT) == 0)
359 msg_Err (stream, "cannot execute %s", path);
362 posix_spawn_file_actions_destroy (&actions);
364 #else /* _POSIX_SPAWN */
365 switch (p_sys->pid = fork ())
368 msg_Err (stream, "cannot fork: %s", vlc_strerror_c(errno));
373 execlp (path, path, (char *)NULL);
374 exit (1); /* if we get, execlp() failed! */
376 if (vlc_clone (&p_sys->thread, Thread, stream,
377 VLC_THREAD_PRIORITY_INPUT) == 0)
380 #endif /* _POSIX_SPAWN < 0 */
382 if (ret != VLC_SUCCESS)
386 if (ret != VLC_SUCCESS)
390 if (ret == VLC_SUCCESS)
393 if (p_sys->pid != -1)
394 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
395 vlc_mutex_destroy (&p_sys->lock);
396 vlc_cond_destroy (&p_sys->wait);
403 * Releases allocate resources.
405 static void Close (vlc_object_t *obj)
407 stream_t *stream = (stream_t *)obj;
408 stream_sys_t *p_sys = stream->p_sys;
411 vlc_cancel (p_sys->thread);
412 close (p_sys->read_fd);
413 vlc_join (p_sys->thread, NULL);
414 if (p_sys->write_fd != -1)
415 /* Killed before EOF? */
416 close (p_sys->write_fd);
418 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
419 while (waitpid (p_sys->pid, &status, 0) == -1);
420 msg_Dbg (obj, "exit status %d", status);
423 block_Release (p_sys->peeked);
424 vlc_mutex_destroy (&p_sys->lock);
425 vlc_cond_destroy (&p_sys->wait);
431 * Detects gzip file format
433 static int OpenGzip (vlc_object_t *obj)
435 stream_t *stream = (stream_t *)obj;
438 if (stream_Peek (stream->p_source, &peek, 3) < 3)
441 if (memcmp (peek, "\x1f\x8b\x08", 3))
444 msg_Dbg (obj, "detected gzip compressed stream");
445 return Open (stream, "zcat");
450 * Detects bzip2 file format
452 static int OpenBzip2 (vlc_object_t *obj)
454 stream_t *stream = (stream_t *)obj;
457 /* (Try to) parse the bzip2 header */
458 if (stream_Peek (stream->p_source, &peek, 10) < 10)
461 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
462 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
465 msg_Dbg (obj, "detected bzip2 compressed stream");
466 return Open (stream, "bzcat");
470 * Detects xz file format
472 static int OpenXZ (vlc_object_t *obj)
474 stream_t *stream = (stream_t *)obj;
477 /* (Try to) parse the xz stream header */
478 if (stream_Peek (stream->p_source, &peek, 8) < 8)
481 if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
484 msg_Dbg (obj, "detected xz compressed stream");
485 return Open (stream, "xzcat");