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         res = !!fread(probe_data, 1, probe_sz, f);
86         fclose(f);
87         if (!res) {
88             free(probe_data);
89             fprintf(stderr, "Failed to read probe data\n");
90             return errno ? DAV1D_ERR(errno) : DAV1D_ERR(EIO);
91         }
92 
93         for (i = 0; demuxers[i]; i++) {
94             if (demuxers[i]->probe(probe_data)) {
95                 impl = demuxers[i];
96                 break;
97             }
98         }
99         free(probe_data);
100         if (!demuxers[i]) {
101             fprintf(stderr,
102                     "Failed to probe demuxer for file %s\n",
103                     filename);
104             return DAV1D_ERR(ENOPROTOOPT);
105         }
106     }
107 
108     if (!(c = calloc(1, sizeof(DemuxerContext) + impl->priv_data_size))) {
109         fprintf(stderr, "Failed to allocate memory\n");
110         return DAV1D_ERR(ENOMEM);
111     }
112     c->impl = impl;
113     c->data = (DemuxerPriv *) &c[1];
114     if ((res = impl->open(c->data, filename, fps, num_frames, timebase)) < 0) {
115         free(c);
116         return res;
117     }
118     *c_out = c;
119 
120     return 0;
121 }
122 
input_read(DemuxerContext * const ctx,Dav1dData * const data)123 int input_read(DemuxerContext *const ctx, Dav1dData *const data) {
124     return ctx->impl->read(ctx->data, data);
125 }
126 
input_close(DemuxerContext * const ctx)127 void input_close(DemuxerContext *const ctx) {
128     ctx->impl->close(ctx->data);
129     free(ctx);
130 }
131