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 };
45 
46 extern const Demuxer ivf_demuxer;
47 extern const Demuxer annexb_demuxer;
48 extern const Demuxer section5_demuxer;
49 static const Demuxer *const demuxers[] = {
50     &ivf_demuxer,
51     &annexb_demuxer,
52     &section5_demuxer,
53     NULL
54 };
55 
input_open(DemuxerContext ** const c_out,const char * const name,const char * const filename,unsigned fps[2],unsigned * const num_frames,unsigned timebase[2])56 int input_open(DemuxerContext **const c_out,
57                const char *const name, const char *const filename,
58                unsigned fps[2], unsigned *const num_frames, unsigned timebase[2])
59 {
60     const Demuxer *impl;
61     DemuxerContext *c;
62     int res, i;
63 
64     if (name) {
65         for (i = 0; demuxers[i]; i++) {
66             if (!strcmp(demuxers[i]->name, name)) {
67                 impl = demuxers[i];
68                 break;
69             }
70         }
71         if (!demuxers[i]) {
72             fprintf(stderr, "Failed to find demuxer named \"%s\"\n", name);
73             return DAV1D_ERR(ENOPROTOOPT);
74         }
75     } else {
76         int probe_sz = 0;
77         for (i = 0; demuxers[i]; i++)
78             probe_sz = imax(probe_sz, demuxers[i]->probe_sz);
79         uint8_t *const probe_data = malloc(probe_sz);
80         if (!probe_data) {
81             fprintf(stderr, "Failed to allocate memory\n");
82             return DAV1D_ERR(ENOMEM);
83         }
84         FILE *f = fopen(filename, "rb");
85         if (!f) {
86             fprintf(stderr, "Failed to open input file %s: %s\n", filename, strerror(errno));
87             return errno ? DAV1D_ERR(errno) : DAV1D_ERR(EIO);
88         }
89         res = !!fread(probe_data, 1, probe_sz, f);
90         fclose(f);
91         if (!res) {
92             free(probe_data);
93             fprintf(stderr, "Failed to read probe data\n");
94             return errno ? DAV1D_ERR(errno) : DAV1D_ERR(EIO);
95         }
96 
97         for (i = 0; demuxers[i]; i++) {
98             if (demuxers[i]->probe(probe_data)) {
99                 impl = demuxers[i];
100                 break;
101             }
102         }
103         free(probe_data);
104         if (!demuxers[i]) {
105             fprintf(stderr,
106                     "Failed to probe demuxer for file %s\n",
107                     filename);
108             return DAV1D_ERR(ENOPROTOOPT);
109         }
110     }
111 
112     if (!(c = calloc(1, sizeof(DemuxerContext) + impl->priv_data_size))) {
113         fprintf(stderr, "Failed to allocate memory\n");
114         return DAV1D_ERR(ENOMEM);
115     }
116     c->impl = impl;
117     c->data = (DemuxerPriv *) &c[1];
118     if ((res = impl->open(c->data, filename, fps, num_frames, timebase)) < 0) {
119         free(c);
120         return res;
121     }
122     *c_out = c;
123 
124     return 0;
125 }
126 
input_read(DemuxerContext * const ctx,Dav1dData * const data)127 int input_read(DemuxerContext *const ctx, Dav1dData *const data) {
128     return ctx->impl->read(ctx->data, data);
129 }
130 
input_close(DemuxerContext * const ctx)131 void input_close(DemuxerContext *const ctx) {
132     ctx->impl->close(ctx->data);
133     free(ctx);
134 }
135