1 /*
2 * Copyright © 2018, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "config.h"
29
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "common/attributes.h"
36 #include "common/intops.h"
37
38 #include "input/input.h"
39 #include "input/demuxer.h"
40
41 struct DemuxerContext {
42 DemuxerPriv *data;
43 const Demuxer *impl;
44 uint64_t priv_data[];
45 };
46
47 extern const Demuxer ivf_demuxer;
48 extern const Demuxer annexb_demuxer;
49 extern const Demuxer section5_demuxer;
50 static const Demuxer *const demuxers[] = {
51 &ivf_demuxer,
52 &annexb_demuxer,
53 §ion5_demuxer,
54 NULL
55 };
56
input_open(DemuxerContext ** const c_out,const char * const name,const char * const filename,unsigned fps[2],unsigned * const num_frames,unsigned timebase[2])57 int input_open(DemuxerContext **const c_out,
58 const char *const name, const char *const filename,
59 unsigned fps[2], unsigned *const num_frames, unsigned timebase[2])
60 {
61 const Demuxer *impl;
62 DemuxerContext *c;
63 int res, i;
64
65 if (name) {
66 for (i = 0; demuxers[i]; i++) {
67 if (!strcmp(demuxers[i]->name, name)) {
68 impl = demuxers[i];
69 break;
70 }
71 }
72 if (!demuxers[i]) {
73 fprintf(stderr, "Failed to find demuxer named \"%s\"\n", name);
74 return DAV1D_ERR(ENOPROTOOPT);
75 }
76 } else {
77 int probe_sz = 0;
78 for (i = 0; demuxers[i]; i++)
79 probe_sz = imax(probe_sz, demuxers[i]->probe_sz);
80 uint8_t *const probe_data = malloc(probe_sz);
81 if (!probe_data) {
82 fprintf(stderr, "Failed to allocate memory\n");
83 return DAV1D_ERR(ENOMEM);
84 }
85 FILE *f = fopen(filename, "rb");
86 if (!f) {
87 fprintf(stderr, "Failed to open input file %s: %s\n", filename, strerror(errno));
88 return errno ? DAV1D_ERR(errno) : DAV1D_ERR(EIO);
89 }
90 res = !!fread(probe_data, 1, probe_sz, f);
91 fclose(f);
92 if (!res) {
93 free(probe_data);
94 fprintf(stderr, "Failed to read probe data\n");
95 return errno ? DAV1D_ERR(errno) : DAV1D_ERR(EIO);
96 }
97
98 for (i = 0; demuxers[i]; i++) {
99 if (demuxers[i]->probe(probe_data)) {
100 impl = demuxers[i];
101 break;
102 }
103 }
104 free(probe_data);
105 if (!demuxers[i]) {
106 fprintf(stderr,
107 "Failed to probe demuxer for file %s\n",
108 filename);
109 return DAV1D_ERR(ENOPROTOOPT);
110 }
111 }
112
113 if (!(c = calloc(1, offsetof(DemuxerContext, priv_data) + impl->priv_data_size))) {
114 fprintf(stderr, "Failed to allocate memory\n");
115 return DAV1D_ERR(ENOMEM);
116 }
117 c->impl = impl;
118 c->data = (DemuxerPriv *) c->priv_data;
119 if ((res = impl->open(c->data, filename, fps, num_frames, timebase)) < 0) {
120 free(c);
121 return res;
122 }
123 *c_out = c;
124
125 return 0;
126 }
127
input_read(DemuxerContext * const ctx,Dav1dData * const data)128 int input_read(DemuxerContext *const ctx, Dav1dData *const data) {
129 return ctx->impl->read(ctx->data, data);
130 }
131
input_seek(DemuxerContext * const ctx,const uint64_t pts)132 int input_seek(DemuxerContext *const ctx, const uint64_t pts) {
133 return ctx->impl->seek ? ctx->impl->seek(ctx->data, pts) : -1;
134 }
135
input_close(DemuxerContext * const ctx)136 void input_close(DemuxerContext *const ctx) {
137 ctx->impl->close(ctx->data);
138 free(ctx);
139 }
140