]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
hwcontext_vulkan: remove plane size alignment checks when host importing
[ffmpeg] / libavformat / avio.c
1 /*
2  * unbuffered I/O
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/avstring.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 #include "libavutil/avassert.h"
27 #include "os_support.h"
28 #include "avformat.h"
29 #include "internal.h"
30 #if CONFIG_NETWORK
31 #include "network.h"
32 #endif
33 #include "url.h"
34
35 /** @name Logging context. */
36 /*@{*/
37 static const char *urlcontext_to_name(void *ptr)
38 {
39     URLContext *h = (URLContext *)ptr;
40     if (h->prot)
41         return h->prot->name;
42     else
43         return "NULL";
44 }
45
46 static void *urlcontext_child_next(void *obj, void *prev)
47 {
48     URLContext *h = obj;
49     if (!prev && h->priv_data && h->prot->priv_data_class)
50         return h->priv_data;
51     return NULL;
52 }
53
54 #define OFFSET(x) offsetof(URLContext,x)
55 #define E AV_OPT_FLAG_ENCODING_PARAM
56 #define D AV_OPT_FLAG_DECODING_PARAM
57 static const AVOption options[] = {
58     {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL },  0, 0, D },
59     {"protocol_blacklist", "List of protocols that are not allowed to be used", OFFSET(protocol_blacklist), AV_OPT_TYPE_STRING, { .str = NULL },  0, 0, D },
60     {"rw_timeout", "Timeout for IO operations (in microseconds)", offsetof(URLContext, rw_timeout), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_DECODING_PARAM },
61     { NULL }
62 };
63
64 const AVClass ffurl_context_class = {
65     .class_name       = "URLContext",
66     .item_name        = urlcontext_to_name,
67     .option           = options,
68     .version          = LIBAVUTIL_VERSION_INT,
69     .child_next       = urlcontext_child_next,
70 #if FF_API_CHILD_CLASS_NEXT
71     .child_class_next = ff_urlcontext_child_class_next,
72 #endif
73     .child_class_iterate = ff_urlcontext_child_class_iterate,
74 };
75 /*@}*/
76
77 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
78                                   const char *filename, int flags,
79                                   const AVIOInterruptCB *int_cb)
80 {
81     URLContext *uc;
82     int err;
83
84 #if CONFIG_NETWORK
85     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
86         return AVERROR(EIO);
87 #endif
88     if ((flags & AVIO_FLAG_READ) && !up->url_read) {
89         av_log(NULL, AV_LOG_ERROR,
90                "Impossible to open the '%s' protocol for reading\n", up->name);
91         return AVERROR(EIO);
92     }
93     if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
94         av_log(NULL, AV_LOG_ERROR,
95                "Impossible to open the '%s' protocol for writing\n", up->name);
96         return AVERROR(EIO);
97     }
98     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
99     if (!uc) {
100         err = AVERROR(ENOMEM);
101         goto fail;
102     }
103     uc->av_class = &ffurl_context_class;
104     uc->filename = (char *)&uc[1];
105     strcpy(uc->filename, filename);
106     uc->prot            = up;
107     uc->flags           = flags;
108     uc->is_streamed     = 0; /* default = not streamed */
109     uc->max_packet_size = 0; /* default: stream file */
110     if (up->priv_data_size) {
111         uc->priv_data = av_mallocz(up->priv_data_size);
112         if (!uc->priv_data) {
113             err = AVERROR(ENOMEM);
114             goto fail;
115         }
116         if (up->priv_data_class) {
117             int proto_len= strlen(up->name);
118             char *start = strchr(uc->filename, ',');
119             *(const AVClass **)uc->priv_data = up->priv_data_class;
120             av_opt_set_defaults(uc->priv_data);
121             if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
122                 int ret= 0;
123                 char *p= start;
124                 char sep= *++p;
125                 char *key, *val;
126                 p++;
127
128                 if (strcmp(up->name, "subfile"))
129                     ret = AVERROR(EINVAL);
130
131                 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
132                     *val= *key= 0;
133                     if (strcmp(p, "start") && strcmp(p, "end")) {
134                         ret = AVERROR_OPTION_NOT_FOUND;
135                     } else
136                         ret= av_opt_set(uc->priv_data, p, key+1, 0);
137                     if (ret == AVERROR_OPTION_NOT_FOUND)
138                         av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
139                     *val= *key= sep;
140                     p= val+1;
141                 }
142                 if(ret<0 || p!=key){
143                     av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
144                     av_freep(&uc->priv_data);
145                     av_freep(&uc);
146                     err = AVERROR(EINVAL);
147                     goto fail;
148                 }
149                 memmove(start, key+1, strlen(key));
150             }
151         }
152     }
153     if (int_cb)
154         uc->interrupt_callback = *int_cb;
155
156     *puc = uc;
157     return 0;
158 fail:
159     *puc = NULL;
160     if (uc)
161         av_freep(&uc->priv_data);
162     av_freep(&uc);
163 #if CONFIG_NETWORK
164     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
165         ff_network_close();
166 #endif
167     return err;
168 }
169
170 int ffurl_connect(URLContext *uc, AVDictionary **options)
171 {
172     int err;
173     AVDictionary *tmp_opts = NULL;
174     AVDictionaryEntry *e;
175
176     if (!options)
177         options = &tmp_opts;
178
179     // Check that URLContext was initialized correctly and lists are matching if set
180     av_assert0(!(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
181                (uc->protocol_whitelist && !strcmp(uc->protocol_whitelist, e->value)));
182     av_assert0(!(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
183                (uc->protocol_blacklist && !strcmp(uc->protocol_blacklist, e->value)));
184
185     if (uc->protocol_whitelist && av_match_list(uc->prot->name, uc->protocol_whitelist, ',') <= 0) {
186         av_log(uc, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", uc->prot->name, uc->protocol_whitelist);
187         return AVERROR(EINVAL);
188     }
189
190     if (uc->protocol_blacklist && av_match_list(uc->prot->name, uc->protocol_blacklist, ',') > 0) {
191         av_log(uc, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", uc->prot->name, uc->protocol_blacklist);
192         return AVERROR(EINVAL);
193     }
194
195     if (!uc->protocol_whitelist && uc->prot->default_whitelist) {
196         av_log(uc, AV_LOG_DEBUG, "Setting default whitelist '%s'\n", uc->prot->default_whitelist);
197         uc->protocol_whitelist = av_strdup(uc->prot->default_whitelist);
198         if (!uc->protocol_whitelist) {
199             return AVERROR(ENOMEM);
200         }
201     } else if (!uc->protocol_whitelist)
202         av_log(uc, AV_LOG_DEBUG, "No default whitelist set\n"); // This should be an error once all declare a default whitelist
203
204     if ((err = av_dict_set(options, "protocol_whitelist", uc->protocol_whitelist, 0)) < 0)
205         return err;
206     if ((err = av_dict_set(options, "protocol_blacklist", uc->protocol_blacklist, 0)) < 0)
207         return err;
208
209     err =
210         uc->prot->url_open2 ? uc->prot->url_open2(uc,
211                                                   uc->filename,
212                                                   uc->flags,
213                                                   options) :
214         uc->prot->url_open(uc, uc->filename, uc->flags);
215
216     av_dict_set(options, "protocol_whitelist", NULL, 0);
217     av_dict_set(options, "protocol_blacklist", NULL, 0);
218
219     if (err)
220         return err;
221     uc->is_connected = 1;
222     /* We must be careful here as ffurl_seek() could be slow,
223      * for example for http */
224     if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
225         if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
226             uc->is_streamed = 1;
227     return 0;
228 }
229
230 int ffurl_accept(URLContext *s, URLContext **c)
231 {
232     av_assert0(!*c);
233     if (s->prot->url_accept)
234         return s->prot->url_accept(s, c);
235     return AVERROR(EBADF);
236 }
237
238 int ffurl_handshake(URLContext *c)
239 {
240     int ret;
241     if (c->prot->url_handshake) {
242         ret = c->prot->url_handshake(c);
243         if (ret)
244             return ret;
245     }
246     c->is_connected = 1;
247     return 0;
248 }
249
250 #define URL_SCHEME_CHARS                        \
251     "abcdefghijklmnopqrstuvwxyz"                \
252     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
253     "0123456789+-."
254
255 static const struct URLProtocol *url_find_protocol(const char *filename)
256 {
257     const URLProtocol **protocols;
258     char proto_str[128], proto_nested[128], *ptr;
259     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
260     int i;
261
262     if (filename[proto_len] != ':' &&
263         (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
264         is_dos_path(filename))
265         strcpy(proto_str, "file");
266     else
267         av_strlcpy(proto_str, filename,
268                    FFMIN(proto_len + 1, sizeof(proto_str)));
269
270     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
271     if ((ptr = strchr(proto_nested, '+')))
272         *ptr = '\0';
273
274     protocols = ffurl_get_protocols(NULL, NULL);
275     if (!protocols)
276         return NULL;
277     for (i = 0; protocols[i]; i++) {
278             const URLProtocol *up = protocols[i];
279         if (!strcmp(proto_str, up->name)) {
280             av_freep(&protocols);
281             return up;
282         }
283         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
284             !strcmp(proto_nested, up->name)) {
285             av_freep(&protocols);
286             return up;
287         }
288     }
289     av_freep(&protocols);
290     if (av_strstart(filename, "https:", NULL) || av_strstart(filename, "tls:", NULL))
291         av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile FFmpeg with "
292                                      "openssl, gnutls or securetransport enabled.\n");
293
294     return NULL;
295 }
296
297 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
298                 const AVIOInterruptCB *int_cb)
299 {
300     const URLProtocol *p = NULL;
301
302     p = url_find_protocol(filename);
303     if (p)
304        return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
305
306     *puc = NULL;
307     return AVERROR_PROTOCOL_NOT_FOUND;
308 }
309
310 int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
311                          const AVIOInterruptCB *int_cb, AVDictionary **options,
312                          const char *whitelist, const char* blacklist,
313                          URLContext *parent)
314 {
315     AVDictionary *tmp_opts = NULL;
316     AVDictionaryEntry *e;
317     int ret = ffurl_alloc(puc, filename, flags, int_cb);
318     if (ret < 0)
319         return ret;
320     if (parent)
321         av_opt_copy(*puc, parent);
322     if (options &&
323         (ret = av_opt_set_dict(*puc, options)) < 0)
324         goto fail;
325     if (options && (*puc)->prot->priv_data_class &&
326         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
327         goto fail;
328
329     if (!options)
330         options = &tmp_opts;
331
332     av_assert0(!whitelist ||
333                !(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
334                !strcmp(whitelist, e->value));
335     av_assert0(!blacklist ||
336                !(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
337                !strcmp(blacklist, e->value));
338
339     if ((ret = av_dict_set(options, "protocol_whitelist", whitelist, 0)) < 0)
340         goto fail;
341
342     if ((ret = av_dict_set(options, "protocol_blacklist", blacklist, 0)) < 0)
343         goto fail;
344
345     if ((ret = av_opt_set_dict(*puc, options)) < 0)
346         goto fail;
347
348     ret = ffurl_connect(*puc, options);
349
350     if (!ret)
351         return 0;
352 fail:
353     ffurl_closep(puc);
354     return ret;
355 }
356
357 int ffurl_open(URLContext **puc, const char *filename, int flags,
358                const AVIOInterruptCB *int_cb, AVDictionary **options)
359 {
360     return ffurl_open_whitelist(puc, filename, flags,
361                                 int_cb, options, NULL, NULL, NULL);
362 }
363
364 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
365                                          int size, int size_min,
366                                          int (*transfer_func)(URLContext *h,
367                                                               uint8_t *buf,
368                                                               int size))
369 {
370     int ret, len;
371     int fast_retries = 5;
372     int64_t wait_since = 0;
373
374     len = 0;
375     while (len < size_min) {
376         if (ff_check_interrupt(&h->interrupt_callback))
377             return AVERROR_EXIT;
378         ret = transfer_func(h, buf + len, size - len);
379         if (ret == AVERROR(EINTR))
380             continue;
381         if (h->flags & AVIO_FLAG_NONBLOCK)
382             return ret;
383         if (ret == AVERROR(EAGAIN)) {
384             ret = 0;
385             if (fast_retries) {
386                 fast_retries--;
387             } else {
388                 if (h->rw_timeout) {
389                     if (!wait_since)
390                         wait_since = av_gettime_relative();
391                     else if (av_gettime_relative() > wait_since + h->rw_timeout)
392                         return AVERROR(EIO);
393                 }
394                 av_usleep(1000);
395             }
396         } else if (ret == AVERROR_EOF)
397             return (len > 0) ? len : AVERROR_EOF;
398         else if (ret < 0)
399             return ret;
400         if (ret) {
401             fast_retries = FFMAX(fast_retries, 2);
402             wait_since = 0;
403         }
404         len += ret;
405     }
406     return len;
407 }
408
409 int ffurl_read(URLContext *h, unsigned char *buf, int size)
410 {
411     if (!(h->flags & AVIO_FLAG_READ))
412         return AVERROR(EIO);
413     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
414 }
415
416 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
417 {
418     if (!(h->flags & AVIO_FLAG_READ))
419         return AVERROR(EIO);
420     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
421 }
422
423 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
424 {
425     if (!(h->flags & AVIO_FLAG_WRITE))
426         return AVERROR(EIO);
427     /* avoid sending too big packets */
428     if (h->max_packet_size && size > h->max_packet_size)
429         return AVERROR(EIO);
430
431     return retry_transfer_wrapper(h, (unsigned char *)buf, size, size,
432                                   (int (*)(struct URLContext *, uint8_t *, int))
433                                   h->prot->url_write);
434 }
435
436 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
437 {
438     int64_t ret;
439
440     if (!h->prot->url_seek)
441         return AVERROR(ENOSYS);
442     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
443     return ret;
444 }
445
446 int ffurl_closep(URLContext **hh)
447 {
448     URLContext *h= *hh;
449     int ret = 0;
450     if (!h)
451         return 0;     /* can happen when ffurl_open fails */
452
453     if (h->is_connected && h->prot->url_close)
454         ret = h->prot->url_close(h);
455 #if CONFIG_NETWORK
456     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
457         ff_network_close();
458 #endif
459     if (h->prot->priv_data_size) {
460         if (h->prot->priv_data_class)
461             av_opt_free(h->priv_data);
462         av_freep(&h->priv_data);
463     }
464     av_opt_free(h);
465     av_freep(hh);
466     return ret;
467 }
468
469 int ffurl_close(URLContext *h)
470 {
471     return ffurl_closep(&h);
472 }
473
474
475 const char *avio_find_protocol_name(const char *url)
476 {
477     const URLProtocol *p = url_find_protocol(url);
478
479     return p ? p->name : NULL;
480 }
481
482 int avio_check(const char *url, int flags)
483 {
484     URLContext *h;
485     int ret = ffurl_alloc(&h, url, flags, NULL);
486     if (ret < 0)
487         return ret;
488
489     if (h->prot->url_check) {
490         ret = h->prot->url_check(h, flags);
491     } else {
492         ret = ffurl_connect(h, NULL);
493         if (ret >= 0)
494             ret = flags;
495     }
496
497     ffurl_close(h);
498     return ret;
499 }
500
501 int avpriv_io_move(const char *url_src, const char *url_dst)
502 {
503     URLContext *h_src, *h_dst;
504     int ret = ffurl_alloc(&h_src, url_src, AVIO_FLAG_READ_WRITE, NULL);
505     if (ret < 0)
506         return ret;
507     ret = ffurl_alloc(&h_dst, url_dst, AVIO_FLAG_WRITE, NULL);
508     if (ret < 0) {
509         ffurl_close(h_src);
510         return ret;
511     }
512
513     if (h_src->prot == h_dst->prot && h_src->prot->url_move)
514         ret = h_src->prot->url_move(h_src, h_dst);
515     else
516         ret = AVERROR(ENOSYS);
517
518     ffurl_close(h_src);
519     ffurl_close(h_dst);
520     return ret;
521 }
522
523 int avpriv_io_delete(const char *url)
524 {
525     URLContext *h;
526     int ret = ffurl_alloc(&h, url, AVIO_FLAG_WRITE, NULL);
527     if (ret < 0)
528         return ret;
529
530     if (h->prot->url_delete)
531         ret = h->prot->url_delete(h);
532     else
533         ret = AVERROR(ENOSYS);
534
535     ffurl_close(h);
536     return ret;
537 }
538
539 int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
540 {
541     URLContext *h = NULL;
542     AVIODirContext *ctx = NULL;
543     int ret;
544     av_assert0(s);
545
546     ctx = av_mallocz(sizeof(*ctx));
547     if (!ctx) {
548         ret = AVERROR(ENOMEM);
549         goto fail;
550     }
551
552     if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
553         goto fail;
554
555     if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
556         if (options && h->prot->priv_data_class &&
557             (ret = av_opt_set_dict(h->priv_data, options)) < 0)
558             goto fail;
559         ret = h->prot->url_open_dir(h);
560     } else
561         ret = AVERROR(ENOSYS);
562     if (ret < 0)
563         goto fail;
564
565     h->is_connected = 1;
566     ctx->url_context = h;
567     *s = ctx;
568     return 0;
569
570   fail:
571     av_free(ctx);
572     *s = NULL;
573     ffurl_close(h);
574     return ret;
575 }
576
577 int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
578 {
579     URLContext *h;
580     int ret;
581
582     if (!s || !s->url_context)
583         return AVERROR(EINVAL);
584     h = s->url_context;
585     if ((ret = h->prot->url_read_dir(h, next)) < 0)
586         avio_free_directory_entry(next);
587     return ret;
588 }
589
590 int avio_close_dir(AVIODirContext **s)
591 {
592     URLContext *h;
593
594     av_assert0(s);
595     if (!(*s) || !(*s)->url_context)
596         return AVERROR(EINVAL);
597     h = (*s)->url_context;
598     h->prot->url_close_dir(h);
599     ffurl_close(h);
600     av_freep(s);
601     *s = NULL;
602     return 0;
603 }
604
605 void avio_free_directory_entry(AVIODirEntry **entry)
606 {
607     if (!entry || !*entry)
608         return;
609     av_free((*entry)->name);
610     av_freep(entry);
611 }
612
613 int64_t ffurl_size(URLContext *h)
614 {
615     int64_t pos, size;
616
617     size = ffurl_seek(h, 0, AVSEEK_SIZE);
618     if (size < 0) {
619         pos = ffurl_seek(h, 0, SEEK_CUR);
620         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
621             return size;
622         size++;
623         ffurl_seek(h, pos, SEEK_SET);
624     }
625     return size;
626 }
627
628 int ffurl_get_file_handle(URLContext *h)
629 {
630     if (!h || !h->prot || !h->prot->url_get_file_handle)
631         return -1;
632     return h->prot->url_get_file_handle(h);
633 }
634
635 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
636 {
637     if (!h || !h->prot)
638         return AVERROR(ENOSYS);
639     if (!h->prot->url_get_multi_file_handle) {
640         if (!h->prot->url_get_file_handle)
641             return AVERROR(ENOSYS);
642         *handles = av_malloc(sizeof(**handles));
643         if (!*handles)
644             return AVERROR(ENOMEM);
645         *numhandles = 1;
646         *handles[0] = h->prot->url_get_file_handle(h);
647         return 0;
648     }
649     return h->prot->url_get_multi_file_handle(h, handles, numhandles);
650 }
651
652 int ffurl_get_short_seek(URLContext *h)
653 {
654     if (!h || !h->prot || !h->prot->url_get_short_seek)
655         return AVERROR(ENOSYS);
656     return h->prot->url_get_short_seek(h);
657 }
658
659 int ffurl_shutdown(URLContext *h, int flags)
660 {
661     if (!h || !h->prot || !h->prot->url_shutdown)
662         return AVERROR(ENOSYS);
663     return h->prot->url_shutdown(h, flags);
664 }
665
666 int ff_check_interrupt(AVIOInterruptCB *cb)
667 {
668     if (cb && cb->callback)
669         return cb->callback(cb->opaque);
670     return 0;
671 }
672
673 int ff_rename(const char *url_src, const char *url_dst, void *logctx)
674 {
675     int ret = avpriv_io_move(url_src, url_dst);
676     if (ret < 0)
677         av_log(logctx, AV_LOG_ERROR, "failed to rename file %s to %s: %s\n", url_src, url_dst, av_err2str(ret));
678     return ret;
679 }