1 /*
2  * Copyright © 2019, VideoLAN and dav1d authors
3  * Copyright © 2019, Two Orioles, LLC
4  * Copyright © 2019, James Almer <jamrial@gmail.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 
37 #include "dav1d/headers.h"
38 
39 #include "input/demuxer.h"
40 #include "input/parse.h"
41 
42 #define PROBE_SIZE 1024
43 
section5_probe(const uint8_t * data)44 static int section5_probe(const uint8_t *data) {
45     int ret, cnt = 0;
46 
47     // Check that the first OBU is a Temporal Delimiter.
48     size_t obu_size;
49     enum Dav1dObuType type;
50     ret = parse_obu_header(data + cnt, PROBE_SIZE - cnt,
51                            &obu_size, &type, 0);
52     if (ret < 0 || type != DAV1D_OBU_TD || obu_size > 0)
53         return 0;
54     cnt += ret;
55 
56     // look for first frame and accompanying sequence header
57     int seq = 0;
58     while (cnt < PROBE_SIZE) {
59         ret = parse_obu_header(data + cnt, PROBE_SIZE - cnt,
60                                &obu_size, &type, 0);
61         if (ret < 0)
62             return 0;
63         cnt += ret;
64 
65         switch (type) {
66         case DAV1D_OBU_SEQ_HDR:
67             seq = 1;
68             break;
69         case DAV1D_OBU_FRAME:
70         case DAV1D_OBU_FRAME_HDR:
71             return seq;
72         case DAV1D_OBU_TD:
73         case DAV1D_OBU_TILE_GRP:
74             return 0;
75         default:
76             break;
77         }
78     }
79 
80     return 0;
81 }
82 
83 typedef struct DemuxerPriv {
84     FILE *f;
85 } Section5InputContext;
86 
section5_open(Section5InputContext * const c,const char * const file,unsigned fps[2],unsigned * const num_frames,unsigned timebase[2])87 static int section5_open(Section5InputContext *const c, const char *const file,
88                          unsigned fps[2], unsigned *const num_frames, unsigned timebase[2])
89 {
90     if (!(c->f = fopen(file, "rb"))) {
91         fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
92         return -1;
93     }
94 
95     // TODO: Parse sequence header and read timing info if any.
96     fps[0] = 25;
97     fps[1] = 1;
98     timebase[0] = 25;
99     timebase[1] = 1;
100     *num_frames = 0;
101     for (;;) {
102         uint8_t byte[2];
103 
104         if (fread(&byte[0], 1, 1, c->f) < 1)
105             break;
106         const enum Dav1dObuType obu_type = (byte[0] >> 3) & 0xf;
107         if (obu_type == DAV1D_OBU_TD)
108             (*num_frames)++;
109         const int has_length_field = byte[0] & 0x2;
110         if (!has_length_field)
111             return -1;
112         const int has_extension = byte[0] & 0x4;
113         if (has_extension && fread(&byte[1], 1, 1, c->f) < 1)
114             return -1;
115         size_t len;
116         const int res = leb128(c->f, &len);
117         if (res < 0)
118             return -1;
119         fseeko(c->f, len, SEEK_CUR); // skip packet
120     }
121     fseeko(c->f, 0, SEEK_SET);
122 
123     return 0;
124 }
125 
section5_read(Section5InputContext * const c,Dav1dData * const data)126 static int section5_read(Section5InputContext *const c, Dav1dData *const data) {
127     size_t total_bytes = 0;
128 
129     for (int first = 1;; first = 0) {
130         uint8_t byte[2];
131 
132         if (fread(&byte[0], 1, 1, c->f) < 1) {
133             if (!first && feof(c->f)) break;
134             return -1;
135         }
136         const enum Dav1dObuType obu_type = (byte[0] >> 3) & 0xf;
137         if (first) {
138             if (obu_type != DAV1D_OBU_TD)
139                 return -1;
140         } else {
141             if (obu_type == DAV1D_OBU_TD) {
142                 // include TD in next packet
143                 fseeko(c->f, -1, SEEK_CUR);
144                 break;
145             }
146         }
147         const int has_length_field = byte[0] & 0x2;
148         if (!has_length_field)
149             return -1;
150         const int has_extension = !!(byte[0] & 0x4);
151         if (has_extension && fread(&byte[1], 1, 1, c->f) < 1)
152             return -1;
153         size_t len;
154         const int res = leb128(c->f, &len);
155         if (res < 0)
156             return -1;
157         total_bytes += 1 + has_extension + res + len;
158         fseeko(c->f, len, SEEK_CUR); // skip packet, we'll read it below
159     }
160 
161     fseeko(c->f, -(off_t)total_bytes, SEEK_CUR);
162     uint8_t *ptr = dav1d_data_create(data, total_bytes);
163     if (!ptr) return -1;
164     if (fread(ptr, total_bytes, 1, c->f) != 1) {
165         fprintf(stderr, "Failed to read frame data: %s\n", strerror(errno));
166         dav1d_data_unref(data);
167         return -1;
168     }
169 
170     return 0;
171 }
172 
section5_close(Section5InputContext * const c)173 static void section5_close(Section5InputContext *const c) {
174     fclose(c->f);
175 }
176 
177 const Demuxer section5_demuxer = {
178     .priv_data_size = sizeof(Section5InputContext),
179     .name = "section5",
180     .probe = section5_probe,
181     .probe_sz = PROBE_SIZE,
182     .open = section5_open,
183     .read = section5_read,
184     .close = section5_close,
185 };
186