1 /*
2 * JPEG2000 parser
3 * Copyright (c) 2020 Gautam Ramakrishnan
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * JPEG2000 parser.
25 */
26
28
29 /* Whether frame is jp2 file or codestream
30 */
34 };
35
40 uint32_t
skip_bytes;
// skip bytes inside codestream data
48
50 {
52
62 }
63
64 /* Returns 1 if marker has any data which can be skipped
65 */
67 {
68 static const uint8_t lut[256] = {
69 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
70 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
71 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
72 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
73 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 0xFF4F
74 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
75 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
76 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
77 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
78 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xFF90 0xFF92 0xFF93
79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
81 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
82 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, // 0xFFD9
83 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
84 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
85 };
86 return marker < 0xFF00 ? 0 : lut[marker & 0xFF];
87 }
88
89 /**
90 * Find the end of the current frame in the bitstream.
91 * @return the position of the first byte of the next frame, or -1
92 */
94 {
99
100 if (buf_size == 0) {
101 return 0;
102 }
103
104 for (
i = 0;
i < buf_size;
i++) {
105 state64 = state64 << 8 | buf[
i];
106 bytes_read++;
108 // handle long skips
110 // need -9 else buf_size - i == 8 ==> i == buf_size after this,
111 // and thus i == buf_size + 1 after the loop
117 }
118 }
120 continue;
121 }
122 if (m->
read_tp) {
// Find out how many bytes inside Tile part codestream to skip.
124 m->
skip_bytes = (state64 & 0xFFFFFFFF) - 9 > 0?
125 (state64 & 0xFFFFFFFF) - 9 : 0;
126 }
128 continue;
129 }
132 if (state64 == 0x6A5020200D0A870A) { // JP2 signature box value.
137 } else {
140 }
141 }
142 }
144 }
145 if ((state64 & 0xFFFFFFFF) == 0x0000000C && bytes_read >= 3) { // Indicates start of JP2 file. Check signature next.
147 } else if ((state64 & 0xFFFF) == 0xFF4F) {
155 }
156 } else if ((state64 & 0xFFFF) == 0xFFD9) {
161 return i + 1;
// End of frame detected, return frame size.
162 }
165 if ((state64 & 0xFFFF) == 0xFF90) { // Are we in tile part header?
168 // Calculate number of bytes to skip to get to end of the next marker.
170
171 // If the next marker is an info marker, skip to the end of of the marker length.
175 // Skip an additional 2 bytes to get to the end of the marker length.
177 }
178 }
179 }
180 }
181 }
182
186 }
187
190 const uint8_t **poutbuf, int *poutbuf_size,
191 const uint8_t *buf, int buf_size)
192 {
195 int next;
196
198 next= buf_size;
199 } else {
201
204 *poutbuf_size = 0;
205 return buf_size;
206 }
207 }
208
209 *poutbuf = buf;
210 *poutbuf_size = buf_size;
211 return next;
212 }
213
219 };