annotate ffmpeg/libavformat/data_uri.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Copyright (c) 2012 Nicolas George
yading@11 3 *
yading@11 4 * This file is part of FFmpeg.
yading@11 5 *
yading@11 6 * FFmpeg is free software; you can redistribute it and/or
yading@11 7 * modify it under the terms of the GNU Lesser General Public License
yading@11 8 * as published by the Free Software Foundation; either
yading@11 9 * version 2.1 of the License, or (at your option) any later version.
yading@11 10 *
yading@11 11 * FFmpeg is distributed in the hope that it will be useful,
yading@11 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
yading@11 14 * GNU Lesser General Public License for more details.
yading@11 15 *
yading@11 16 * You should have received a copy of the GNU Lesser General Public License
yading@11 17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
yading@11 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 19 */
yading@11 20
yading@11 21 #include <string.h>
yading@11 22 #include "libavutil/avstring.h"
yading@11 23 #include "libavutil/base64.h"
yading@11 24 #include "url.h"
yading@11 25
yading@11 26 typedef struct {
yading@11 27 const uint8_t *data;
yading@11 28 void *tofree;
yading@11 29 size_t size;
yading@11 30 size_t pos;
yading@11 31 } DataContext;
yading@11 32
yading@11 33 static av_cold int data_open(URLContext *h, const char *uri, int flags)
yading@11 34 {
yading@11 35 DataContext *dc = h->priv_data;
yading@11 36 const char *data, *opt, *next;
yading@11 37 char *ddata;
yading@11 38 int ret, base64 = 0;
yading@11 39 size_t in_size;
yading@11 40
yading@11 41 /* data:content/type[;base64],payload */
yading@11 42
yading@11 43 av_strstart(uri, "data:", &uri);
yading@11 44 data = strchr(uri, ',');
yading@11 45 if (!data) {
yading@11 46 av_log(h, AV_LOG_ERROR, "No ',' delimiter in URI\n");
yading@11 47 return AVERROR(EINVAL);
yading@11 48 }
yading@11 49 opt = uri;
yading@11 50 while (opt < data) {
yading@11 51 next = av_x_if_null(memchr(opt, ';', data - opt), data);
yading@11 52 if (opt == uri) {
yading@11 53 if (!memchr(opt, '/', next - opt)) { /* basic validity check */
yading@11 54 av_log(h, AV_LOG_ERROR, "Invalid content-type '%.*s'\n",
yading@11 55 (int)(next - opt), opt);
yading@11 56 return AVERROR(EINVAL);
yading@11 57 }
yading@11 58 av_log(h, AV_LOG_VERBOSE, "Content-type: %.*s\n",
yading@11 59 (int)(next - opt), opt);
yading@11 60 } else {
yading@11 61 if (!av_strncasecmp(opt, "base64", next - opt)) {
yading@11 62 base64 = 1;
yading@11 63 } else {
yading@11 64 av_log(h, AV_LOG_VERBOSE, "Ignoring option '%.*s'\n",
yading@11 65 (int)(next - opt), opt);
yading@11 66 }
yading@11 67 }
yading@11 68 opt = next + 1;
yading@11 69 }
yading@11 70
yading@11 71 data++;
yading@11 72 in_size = strlen(data);
yading@11 73 if (base64) {
yading@11 74 size_t out_size = 3 * (in_size / 4) + 1;
yading@11 75
yading@11 76 if (out_size > INT_MAX || !(ddata = av_malloc(out_size)))
yading@11 77 return AVERROR(ENOMEM);
yading@11 78 if ((ret = av_base64_decode(ddata, data, out_size)) < 0) {
yading@11 79 av_free(ddata);
yading@11 80 av_log(h, AV_LOG_ERROR, "Invalid base64 in URI\n");
yading@11 81 return ret;
yading@11 82 }
yading@11 83 dc->data = dc->tofree = ddata;
yading@11 84 dc->size = ret;
yading@11 85 } else {
yading@11 86 dc->data = data;
yading@11 87 dc->size = in_size;
yading@11 88 }
yading@11 89 return 0;
yading@11 90 }
yading@11 91
yading@11 92 static av_cold int data_close(URLContext *h)
yading@11 93 {
yading@11 94 DataContext *dc = h->priv_data;
yading@11 95
yading@11 96 av_freep(&dc->tofree);
yading@11 97 return 0;
yading@11 98 }
yading@11 99
yading@11 100 static int data_read(URLContext *h, unsigned char *buf, int size)
yading@11 101 {
yading@11 102 DataContext *dc = h->priv_data;
yading@11 103
yading@11 104 if (dc->pos >= dc->size)
yading@11 105 return AVERROR_EOF;
yading@11 106 size = FFMIN(size, dc->size - dc->pos);
yading@11 107 memcpy(buf, dc->data + dc->pos, size);
yading@11 108 dc->pos += size;
yading@11 109 return size;
yading@11 110 }
yading@11 111
yading@11 112 URLProtocol ff_data_protocol = {
yading@11 113 .name = "data",
yading@11 114 .url_open = data_open,
yading@11 115 .url_close = data_close,
yading@11 116 .url_read = data_read,
yading@11 117 .priv_data_size = sizeof(DataContext),
yading@11 118 };