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", 320)
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
->s
, 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
->s
, STREAM_SET_PAUSE_STATE
, paused
);
233 p_sys
->paused
= paused
;
234 vlc_cond_signal (&p_sys
->wait
);
235 vlc_mutex_unlock (&p_sys
->lock
);
245 * Pipe data through an external executable.
246 * @param stream the stream filter object.
247 * @param path path to the executable.
249 static int Open (stream_t
*stream
, const char *path
)
251 stream_sys_t
*p_sys
= stream
->p_sys
= malloc (sizeof (*p_sys
));
255 vlc_cond_init (&p_sys
->wait
);
256 vlc_mutex_init (&p_sys
->lock
);
257 p_sys
->paused
= false;
259 vlc_stream_Control(stream
->s
, STREAM_CAN_PAUSE
, &p_sys
->can_pause
);
260 vlc_stream_Control(stream
->s
, STREAM_CAN_CONTROL_PACE
, &p_sys
->can_pace
);
261 vlc_stream_Control(stream
->s
, STREAM_GET_PTS_DELAY
, &p_sys
->pts_delay
);
263 /* I am not a big fan of the pyramid style, but I cannot think of anything
264 * better here. There are too many failure cases. */
265 int ret
= VLC_EGENERIC
;
268 /* We use two pipes rather than one stream socket pair, so that we can
269 * use vmsplice() on Linux. */
270 if (vlc_pipe (comp
) == 0)
272 p_sys
->write_fd
= comp
[1];
275 if (vlc_pipe (uncomp
) == 0)
277 p_sys
->read_fd
= uncomp
[0];
279 #if (_POSIX_SPAWN >= 0)
280 posix_spawn_file_actions_t actions
;
281 if (posix_spawn_file_actions_init (&actions
) == 0)
283 char *const argv
[] = { (char *)path
, NULL
};
285 if (!posix_spawn_file_actions_adddup2 (&actions
, comp
[0], 0)
286 && !posix_spawn_file_actions_adddup2 (&actions
, uncomp
[1], 1)
287 && !posix_spawnp (&p_sys
->pid
, path
, &actions
, NULL
, argv
,
290 if (vlc_clone (&p_sys
->thread
, Thread
, stream
,
291 VLC_THREAD_PRIORITY_INPUT
) == 0)
296 msg_Err (stream
, "cannot execute %s", path
);
299 posix_spawn_file_actions_destroy (&actions
);
301 #else /* _POSIX_SPAWN */
302 switch (p_sys
->pid
= fork ())
305 msg_Err (stream
, "cannot fork: %s", vlc_strerror_c(errno
));
310 execlp (path
, path
, (const char *)NULL
);
311 exit (1); /* if we get, execlp() failed! */
313 if (vlc_clone (&p_sys
->thread
, Thread
, stream
,
314 VLC_THREAD_PRIORITY_INPUT
) == 0)
317 #endif /* _POSIX_SPAWN < 0 */
318 vlc_close (uncomp
[1]);
319 if (ret
!= VLC_SUCCESS
)
320 vlc_close (uncomp
[0]);
323 if (ret
!= VLC_SUCCESS
)
327 if (ret
!= VLC_SUCCESS
)
329 if (p_sys
->pid
!= -1)
330 while (waitpid (p_sys
->pid
, &(int){ 0 }, 0) == -1);
331 vlc_mutex_destroy (&p_sys
->lock
);
332 vlc_cond_destroy (&p_sys
->wait
);
337 stream
->pf_read
= Read
;
338 stream
->pf_seek
= NULL
;
339 stream
->pf_control
= Control
;
345 * Releases allocate resources.
347 static void Close (vlc_object_t
*obj
)
349 stream_t
*stream
= (stream_t
*)obj
;
350 stream_sys_t
*p_sys
= stream
->p_sys
;
353 vlc_cancel (p_sys
->thread
);
354 vlc_close (p_sys
->read_fd
);
355 vlc_join (p_sys
->thread
, NULL
);
356 if (p_sys
->write_fd
!= -1)
357 /* Killed before EOF? */
358 vlc_close (p_sys
->write_fd
);
360 msg_Dbg (obj
, "waiting for PID %u", (unsigned)p_sys
->pid
);
361 while (waitpid (p_sys
->pid
, &status
, 0) == -1);
362 msg_Dbg (obj
, "exit status %d", status
);
364 vlc_mutex_destroy (&p_sys
->lock
);
365 vlc_cond_destroy (&p_sys
->wait
);
371 * Detects gzip file format
373 static int OpenGzip (vlc_object_t
*obj
)
375 stream_t
*stream
= (stream_t
*)obj
;
378 if (vlc_stream_Peek (stream
->s
, &peek
, 3) < 3)
381 if (memcmp (peek
, "\x1f\x8b\x08", 3))
384 msg_Dbg (obj
, "detected gzip compressed stream");
385 return Open (stream
, "zcat");
390 * Detects bzip2 file format
392 static int OpenBzip2 (vlc_object_t
*obj
)
394 stream_t
*stream
= (stream_t
*)obj
;
397 /* (Try to) parse the bzip2 header */
398 if (vlc_stream_Peek (stream
->s
, &peek
, 10) < 10)
401 if (memcmp (peek
, "BZh", 3) || (peek
[3] < '1') || (peek
[3] > '9')
402 || memcmp (peek
+ 4, "\x31\x41\x59\x26\x53\x59", 6))
405 msg_Dbg (obj
, "detected bzip2 compressed stream");
406 return Open (stream
, "bzcat");
410 * Detects xz file format
412 static int OpenXZ (vlc_object_t
*obj
)
414 stream_t
*stream
= (stream_t
*)obj
;
417 /* (Try to) parse the xz stream header */
418 if (vlc_stream_Peek (stream
->s
, &peek
, 8) < 8)
421 if (memcmp (peek
, "\xfd\x37\x7a\x58\x5a", 6))
424 msg_Dbg (obj
, "detected xz compressed stream");
425 return Open (stream
, "xzcat");