]> git.sesse.net Git - vlc/blob - modules/stream_filter/decomp.c
Qt: OpenFile: Use QGroupBox for subs.
[vlc] / modules / stream_filter / decomp.c
1 /*****************************************************************************
2  * decomp.c : Decompression module for vlc
3  *****************************************************************************
4  * Copyright © 2008-2009 Rémi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_stream.h>
28 #include <vlc_network.h>
29 #include <vlc_fs.h>
30 #include <assert.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #ifndef _POSIX_SPAWN
34 # define _POSIX_SPAWN (-1)
35 #endif
36 #include <fcntl.h>
37 #if (_POSIX_SPAWN >= 0)
38 # include <spawn.h>
39 #endif
40 #include <sys/wait.h>
41 #include <sys/ioctl.h>
42 #if defined (__linux__) && defined (HAVE_VMSPLICE)
43 # include <sys/uio.h>
44 # include <sys/mman.h>
45 #else
46 # undef HAVE_VMSPLICE
47 #endif
48
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 *);
53
54 vlc_module_begin ()
55     set_category (CAT_INPUT)
56     set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
57     set_capability ("stream_filter", 20)
58
59     set_description (N_("LZMA decompression"))
60     set_callbacks (OpenXZ, Close)
61
62     add_submodule ()
63     set_description (N_("Burrows-Wheeler decompression"))
64     set_callbacks (OpenBzip2, Close)
65     /* TODO: access shortnames for stream_UrlNew() */
66
67     add_submodule ()
68     set_description (N_("gzip decompression"))
69     set_callbacks (OpenGzip, Close)
70 vlc_module_end ()
71
72 struct stream_sys_t
73 {
74     /* Thread data */
75     int          write_fd;
76
77     /* Shared data */
78     vlc_cond_t   wait;
79     vlc_mutex_t  lock;
80     bool         paused;
81
82     /* Caller data */
83     vlc_thread_t thread;
84     pid_t        pid;
85
86     uint64_t     offset;
87     block_t      *peeked;
88
89     int          read_fd;
90     bool         can_pace;
91     bool         can_pause;
92 };
93
94 extern char **environ;
95
96 static const size_t bufsize = 65536;
97 #ifdef HAVE_VMSPLICE
98 static void cleanup_mmap (void *addr)
99 {
100     munmap (addr, bufsize);
101 }
102 #endif
103
104 static void *Thread (void *data)
105 {
106     stream_t *stream = data;
107     stream_sys_t *p_sys = stream->p_sys;
108 #ifdef HAVE_VMSPLICE
109     const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
110 #endif
111     int fd = p_sys->write_fd;
112     bool error = false;
113
114     do
115     {
116         ssize_t len;
117         int canc = vlc_savecancel ();
118 #ifdef HAVE_VMSPLICE
119         unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
120                                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
121         if (unlikely(buf == MAP_FAILED))
122             break;
123         vlc_cleanup_push (cleanup_mmap, buf);
124 #else
125         unsigned char *buf = malloc (bufsize);
126         if (unlikely(buf == NULL))
127             break;
128         vlc_cleanup_push (free, buf);
129 #endif
130
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);
136
137         vlc_restorecancel (canc);
138         error = len <= 0;
139
140         for (ssize_t i = 0, j; i < len; i += j)
141         {
142 #ifdef HAVE_VMSPLICE
143             if ((len - i) <= page_mask) /* incomplete last page */
144                 j = write (fd, buf + i, len - i);
145             else
146             {
147                 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
148                 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
149             }
150             if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
151 #endif
152             j = write (fd, buf + i, len - i);
153             if (j <= 0)
154             {
155                 if (j == 0)
156                     errno = EPIPE;
157                 msg_Err (stream, "cannot write data (%m)");
158                 error = true;
159                 break;
160             }
161         }
162         vlc_cleanup_run (); /* free (buf) */
163     }
164     while (!error);
165
166     msg_Dbg (stream, "compressed stream at EOF");
167     /* Let child process know about EOF */
168     p_sys->write_fd = -1;
169     close (fd);
170     return NULL;
171 }
172
173
174 static int Peek (stream_t *, const uint8_t **, unsigned int);
175
176 #define MIN_BLOCK (1 << 10)
177 #define MAX_BLOCK (1 << 20)
178 /**
179  * Reads decompressed from the decompression program
180  * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
181  */
182 static int Read (stream_t *stream, void *buf, unsigned int buflen)
183 {
184     stream_sys_t *p_sys = stream->p_sys;
185     block_t *peeked;
186     ssize_t length;
187
188     if (buf == NULL) /* caller skips data, get big enough peek buffer */
189         buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
190
191     if ((peeked = p_sys->peeked) != NULL)
192     {   /* dequeue peeked data */
193         length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
194         if (buf != NULL)
195         {
196             memcpy (buf, peeked->p_buffer, length);
197             buf = ((char *)buf) + length;
198         }
199         buflen -= length;
200         peeked->p_buffer += length;
201         peeked->i_buffer -= length;
202         if (peeked->i_buffer == 0)
203         {
204             block_Release (peeked);
205             p_sys->peeked = NULL;
206         }
207         p_sys->offset += length;
208
209         if (buflen > 0)
210             length += Read (stream, ((char *)buf) + length, buflen - length);
211         return length;
212     }
213     assert ((buf != NULL) || (buflen == 0));
214
215     length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
216     if (length < 0)
217         return 0;
218     p_sys->offset += length;
219     return length;
220 }
221
222 /**
223  *
224  */
225 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
226 {
227     stream_sys_t *p_sys = stream->p_sys;
228     block_t *peeked = p_sys->peeked;
229     size_t curlen = 0;
230     int fd = p_sys->read_fd;
231
232     if (peeked == NULL)
233         peeked = block_Alloc (len);
234     else if ((curlen = peeked->i_buffer) < len)
235         peeked = block_Realloc (peeked, 0, len);
236
237     if ((p_sys->peeked = peeked) == NULL)
238         return 0;
239
240     if (curlen < len)
241     {
242         ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
243                                 len - curlen, true);
244         if (val >= 0)
245         {
246             curlen += val;
247             peeked->i_buffer = curlen;
248         }
249     }
250     *pbuf = peeked->p_buffer;
251     return curlen;
252 }
253
254 /**
255  *
256  */
257 static int Control (stream_t *stream, int query, va_list args)
258 {
259     stream_sys_t *p_sys = stream->p_sys;
260
261     switch (query)
262     {
263         case STREAM_CAN_SEEK:
264         case STREAM_CAN_FASTSEEK:
265             *(va_arg (args, bool *)) = false;
266             break;
267         case STREAM_CAN_PAUSE:
268              *(va_arg (args, bool *)) = p_sys->can_pause;
269             break;
270         case STREAM_CAN_CONTROL_PACE:
271             *(va_arg (args, bool *)) = p_sys->can_pace;
272             break;
273         case STREAM_GET_POSITION:
274             *(va_arg (args, uint64_t *)) = p_sys->offset;
275             break;
276         case STREAM_GET_SIZE:
277             *(va_arg (args, uint64_t *)) = 0;
278             break;
279         case STREAM_SET_PAUSE_STATE:
280         {
281             bool paused = va_arg (args, unsigned);
282
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);
288             break;
289         }
290         default:
291             return VLC_EGENERIC;
292     }
293     return VLC_SUCCESS;
294 }
295
296 /**
297  * Pipe data through an external executable.
298  * @param stream the stream filter object.
299  * @param path path to the executable.
300  */
301 static int Open (stream_t *stream, const char *path)
302 {
303     stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
304     if (p_sys == NULL)
305         return VLC_ENOMEM;
306
307     stream->pf_read = Read;
308     stream->pf_peek = Peek;
309     stream->pf_control = Control;
310
311     vlc_cond_init (&p_sys->wait);
312     vlc_mutex_init (&p_sys->lock);
313     p_sys->paused = false;
314     p_sys->pid = -1;
315     p_sys->offset = 0;
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,
319                     &p_sys->can_pace);
320
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;
324     int comp[2];
325
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)
329     {
330         p_sys->write_fd = comp[1];
331
332         int uncomp[2];
333         if (vlc_pipe (uncomp) == 0)
334         {
335             p_sys->read_fd = uncomp[0];
336
337 #if (_POSIX_SPAWN >= 0)
338             posix_spawn_file_actions_t actions;
339             if (posix_spawn_file_actions_init (&actions) == 0)
340             {
341                 char *const argv[] = { (char *)path, NULL };
342
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,
346                                    environ))
347                 {
348                     if (vlc_clone (&p_sys->thread, Thread, stream,
349                                    VLC_THREAD_PRIORITY_INPUT) == 0)
350                         ret = VLC_SUCCESS;
351                 }
352                 else
353                 {
354                     msg_Err (stream, "Cannot execute %s", path);
355                     p_sys->pid = -1;
356                 }
357                 posix_spawn_file_actions_destroy (&actions);
358             }
359 #else /* _POSIX_SPAWN */
360             switch (p_sys->pid = fork ())
361             {
362                 case -1:
363                     msg_Err (stream, "Cannot fork (%m)");
364                     break;
365                 case 0:
366                     dup2 (comp[0], 0);
367                     dup2 (uncomp[1], 1);
368                     execlp (path, path, (char *)NULL);
369                     exit (1); /* if we get, execlp() failed! */
370                 default:
371                     if (vlc_clone (&p_sys->thread, Thread, stream,
372                                    VLC_THREAD_PRIORITY_INPUT) == 0)
373                         ret = VLC_SUCCESS;
374             }
375 #endif /* _POSIX_SPAWN < 0 */
376             close (uncomp[1]);
377             if (ret != VLC_SUCCESS)
378                 close (uncomp[0]);
379         }
380         close (comp[0]);
381         if (ret != VLC_SUCCESS)
382             close (comp[1]);
383     }
384
385     if (ret == VLC_SUCCESS)
386         return VLC_SUCCESS;
387
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);
392     free (p_sys);
393     return ret;
394 }
395
396
397 /**
398  * Releases allocate resources.
399  */
400 static void Close (vlc_object_t *obj)
401 {
402     stream_t *stream = (stream_t *)obj;
403     stream_sys_t *p_sys = stream->p_sys;
404     int status;
405
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);
412
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);
416
417     if (p_sys->peeked)
418         block_Release (p_sys->peeked);
419     vlc_mutex_destroy (&p_sys->lock);
420     vlc_cond_destroy (&p_sys->wait);
421     free (p_sys);
422 }
423
424
425 /**
426  * Detects gzip file format
427  */
428 static int OpenGzip (vlc_object_t *obj)
429 {
430     stream_t      *stream = (stream_t *)obj;
431     const uint8_t *peek;
432
433     if (stream_Peek (stream->p_source, &peek, 3) < 3)
434         return VLC_EGENERIC;
435
436     if (memcmp (peek, "\x1f\x8b\x08", 3))
437         return VLC_EGENERIC;
438
439     msg_Dbg (obj, "detected gzip compressed stream");
440     return Open (stream, "zcat");
441 }
442
443
444 /**
445  * Detects bzip2 file format
446  */
447 static int OpenBzip2 (vlc_object_t *obj)
448 {
449     stream_t      *stream = (stream_t *)obj;
450     const uint8_t *peek;
451
452     /* (Try to) parse the bzip2 header */
453     if (stream_Peek (stream->p_source, &peek, 10) < 10)
454         return VLC_EGENERIC;
455
456     if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
457      || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
458         return VLC_EGENERIC;
459
460     msg_Dbg (obj, "detected bzip2 compressed stream");
461     return Open (stream, "bzcat");
462 }
463
464 /**
465  * Detects xz file format
466  */
467 static int OpenXZ (vlc_object_t *obj)
468 {
469     stream_t      *stream = (stream_t *)obj;
470     const uint8_t *peek;
471
472     /* (Try to) parse the xz stream header */
473     if (stream_Peek (stream->p_source, &peek, 8) < 8)
474         return VLC_EGENERIC;
475
476     if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
477         return VLC_EGENERIC;
478
479     msg_Dbg (obj, "detected xz compressed stream");
480     return Open (stream, "xzcat");
481 }