hxxx_nal: use vlc_alloc helper
[vlc.git] / modules / stream_filter / decomp.c
blobb253c022194df7d86c4a202748583f97ac6e9a96
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 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
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 #include <vlc_interrupt.h>
50 #include <signal.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 *);
57 vlc_module_begin ()
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)
65 add_submodule ()
66 set_description (N_("Burrows-Wheeler decompression"))
67 set_callbacks (OpenBzip2, Close)
68 /* TODO: access shortnames for vlc_stream_NewURL() */
70 add_submodule ()
71 set_description (N_("gzip decompression"))
72 set_callbacks (OpenGzip, Close)
73 vlc_module_end ()
75 struct stream_sys_t
77 /* Thread data */
78 int write_fd;
80 /* Shared data */
81 vlc_cond_t wait;
82 vlc_mutex_t lock;
83 bool paused;
85 /* Caller data */
86 vlc_thread_t thread;
87 pid_t pid;
89 int read_fd;
90 bool can_pace;
91 bool can_pause;
92 int64_t pts_delay;
95 extern char **environ;
97 static const size_t bufsize = 65536;
98 #ifdef HAVE_VMSPLICE
99 static void cleanup_mmap (void *addr)
101 munmap (addr, bufsize);
103 #endif
105 static void *Thread (void *data)
107 stream_t *stream = data;
108 stream_sys_t *p_sys = stream->p_sys;
109 #ifdef HAVE_VMSPLICE
110 const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
111 #endif
112 int fd = p_sys->write_fd;
113 bool error = false;
114 sigset_t set;
116 sigemptyset(&set);
117 sigaddset(&set, SIGPIPE);
118 pthread_sigmask(SIG_BLOCK, &set, NULL);
122 ssize_t len;
123 int canc = vlc_savecancel ();
124 #ifdef HAVE_VMSPLICE
125 unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
126 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
127 if (unlikely(buf == MAP_FAILED))
128 break;
129 vlc_cleanup_push (cleanup_mmap, buf);
130 #else
131 unsigned char *buf = malloc (bufsize);
132 if (unlikely(buf == NULL))
133 break;
134 vlc_cleanup_push (free, buf);
135 #endif
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);
144 error = len <= 0;
146 for (ssize_t i = 0, j; i < len; i += j)
148 #ifdef HAVE_VMSPLICE
149 if ((len - i) <= page_mask) /* incomplete last page */
150 j = write (fd, buf + i, len - i);
151 else
153 struct iovec iov = {
154 .iov_base = buf + 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 */
160 #endif
161 j = write (fd, buf + i, len - i);
162 if (j <= 0)
164 if (j == 0)
165 errno = EPIPE;
166 msg_Err (stream, "cannot write data: %s",
167 vlc_strerror_c(errno));
168 error = true;
169 break;
172 vlc_cleanup_pop ();
173 #ifdef HAVE_VMSPLICE
174 munmap (buf, bufsize);
175 #else
176 free (buf);
177 #endif
179 while (!error);
181 msg_Dbg (stream, "compressed stream at EOF");
182 /* Let child process know about EOF */
183 p_sys->write_fd = -1;
184 vlc_close (fd);
185 return NULL;
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;
209 switch (query)
211 case STREAM_CAN_SEEK:
212 case STREAM_CAN_FASTSEEK:
213 *(va_arg (args, bool *)) = false;
214 break;
215 case STREAM_CAN_PAUSE:
216 *(va_arg (args, bool *)) = p_sys->can_pause;
217 break;
218 case STREAM_CAN_CONTROL_PACE:
219 *(va_arg (args, bool *)) = p_sys->can_pace;
220 break;
221 case STREAM_GET_SIZE:
222 *(va_arg (args, uint64_t *)) = 0;
223 break;
224 case STREAM_GET_PTS_DELAY:
225 *va_arg (args, int64_t *) = p_sys->pts_delay;
226 break;
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,
233 paused);
234 p_sys->paused = paused;
235 vlc_cond_signal (&p_sys->wait);
236 vlc_mutex_unlock (&p_sys->lock);
237 break;
239 default:
240 return VLC_EGENERIC;
242 return VLC_SUCCESS;
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));
253 if (p_sys == NULL)
254 return VLC_ENOMEM;
256 vlc_cond_init (&p_sys->wait);
257 vlc_mutex_init (&p_sys->lock);
258 p_sys->paused = false;
259 p_sys->pid = -1;
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,
262 &p_sys->can_pace);
263 vlc_stream_Control(stream->p_source, STREAM_GET_PTS_DELAY,
264 &p_sys->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;
269 int comp[2];
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];
277 int uncomp[2];
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,
291 environ))
293 if (vlc_clone (&p_sys->thread, Thread, stream,
294 VLC_THREAD_PRIORITY_INPUT) == 0)
295 ret = VLC_SUCCESS;
297 else
299 msg_Err (stream, "cannot execute %s", path);
300 p_sys->pid = -1;
302 posix_spawn_file_actions_destroy (&actions);
304 #else /* _POSIX_SPAWN */
305 switch (p_sys->pid = fork ())
307 case -1:
308 msg_Err (stream, "cannot fork: %s", vlc_strerror_c(errno));
309 break;
310 case 0:
311 dup2 (comp[0], 0);
312 dup2 (uncomp[1], 1);
313 execlp (path, path, (const char *)NULL);
314 exit (1); /* if we get, execlp() failed! */
315 default:
316 if (vlc_clone (&p_sys->thread, Thread, stream,
317 VLC_THREAD_PRIORITY_INPUT) == 0)
318 ret = VLC_SUCCESS;
320 #endif /* _POSIX_SPAWN < 0 */
321 vlc_close (uncomp[1]);
322 if (ret != VLC_SUCCESS)
323 vlc_close (uncomp[0]);
325 vlc_close (comp[0]);
326 if (ret != VLC_SUCCESS)
327 vlc_close (comp[1]);
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);
336 free (p_sys);
337 return ret;
340 stream->pf_read = Read;
341 stream->pf_seek = NULL;
342 stream->pf_control = Control;
343 return VLC_SUCCESS;
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;
354 int status;
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);
369 free (p_sys);
374 * Detects gzip file format
376 static int OpenGzip (vlc_object_t *obj)
378 stream_t *stream = (stream_t *)obj;
379 const uint8_t *peek;
381 if (vlc_stream_Peek (stream->p_source, &peek, 3) < 3)
382 return VLC_EGENERIC;
384 if (memcmp (peek, "\x1f\x8b\x08", 3))
385 return VLC_EGENERIC;
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;
398 const uint8_t *peek;
400 /* (Try to) parse the bzip2 header */
401 if (vlc_stream_Peek (stream->p_source, &peek, 10) < 10)
402 return VLC_EGENERIC;
404 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
405 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
406 return VLC_EGENERIC;
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;
418 const uint8_t *peek;
420 /* (Try to) parse the xz stream header */
421 if (vlc_stream_Peek (stream->p_source, &peek, 8) < 8)
422 return VLC_EGENERIC;
424 if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
425 return VLC_EGENERIC;
427 msg_Dbg (obj, "detected xz compressed stream");
428 return Open (stream, "xzcat");