FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/jpeg2000_parser.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 8 87 9.2%
Branches: 1 76 1.3%

Line Branch Exec Source
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
27 #include "parser.h"
28
29 /* Whether frame is jp2 file or codestream
30 */
31 enum frame_type {
32 jp2_file = 1,
33 j2k_cstream
34 };
35
36 typedef struct JPEG2000ParserContext {
37 ParseContext pc;
38 uint64_t bytes_read;
39 uint64_t fheader_state;
40 uint32_t skip_bytes; // skip bytes inside codestream data
41 enum frame_type ft; // 1 if file, 2 if codestream
42 uint8_t fheader_read; // are we reading
43 uint8_t reading_file_header;
44 uint8_t skipped_codestream;
45 uint8_t read_tp;
46 uint8_t in_codestream;
47 } JPEG2000ParserContext;
48
49 static inline void reset_context(JPEG2000ParserContext *m)
50 {
51 ParseContext *pc = &m->pc;
52
53 pc->frame_start_found= 0;
54 pc->state = 0;
55 m->bytes_read = 0;
56 m->ft = 0;
57 m->skipped_codestream = 0;
58 m->fheader_read = 0;
59 m->skip_bytes = 0;
60 m->read_tp = 0;
61 m->in_codestream = 0;
62 }
63
64 /* Returns 1 if marker has any data which can be skipped
65 */
66 static uint8_t info_marker(uint16_t marker)
67 {
68 if (marker == 0xFF92 || marker == 0xFF4F ||
69 marker == 0xFF90 || marker == 0xFF93 ||
70 marker == 0xFFD9)
71 return 0;
72 else
73 if (marker > 0xFF00) return 1;
74 return 0;
75 }
76
77 /**
78 * Find the end of the current frame in the bitstream.
79 * @return the position of the first byte of the next frame, or -1
80 */
81 static int find_frame_end(JPEG2000ParserContext *m, const uint8_t *buf, int buf_size)
82 {
83 ParseContext *pc= &m->pc;
84 int i;
85 uint32_t state, next_state;
86 uint64_t state64;
87 state= pc->state;
88 state64 = pc->state64;
89 if (buf_size == 0) {
90 return 0;
91 }
92
93 for (i = 0; i < buf_size; i++) {
94 state = state << 8 | buf[i];
95 state64 = state64 << 8 | buf[i];
96 m->bytes_read++;
97 if (m->skip_bytes) {
98 m->skip_bytes--;
99 continue;
100 }
101 if (m->read_tp) { // Find out how many bytes inside Tile part codestream to skip.
102 if (m->read_tp == 1) {
103 m->skip_bytes = (state64 & 0xFFFFFFFF) - 9 > 0?
104 (state64 & 0xFFFFFFFF) - 9 : 0;
105 }
106 m->read_tp--;
107 continue;
108 }
109 if (m->fheader_read) {
110 if (m->fheader_read == 1) {
111 if (state64 == 0x6A5020200D0A870A) { // JP2 signature box value.
112 if (pc->frame_start_found) {
113 pc->frame_start_found = 0;
114 reset_context(m);
115 return i - 11;
116 } else {
117 pc->frame_start_found = 1;
118 m->ft = jp2_file;
119 }
120 }
121 }
122 m->fheader_read--;
123 }
124 if (state == 0x0000000C && m->bytes_read >= 3) { // Indicates start of JP2 file. Check signature next.
125 m->fheader_read = 8;
126 } else if ((state & 0xFFFF) == 0xFF4F) {
127 m->in_codestream = 1;
128 if (!pc->frame_start_found) {
129 pc->frame_start_found = 1;
130 m->ft = j2k_cstream;
131 } else if (pc->frame_start_found && m->ft == jp2_file && m->skipped_codestream) {
132 reset_context(m);
133 return i - 1;
134 }
135 } else if ((state & 0xFFFF) == 0xFFD9) {
136 if (pc->frame_start_found && m->ft == jp2_file) {
137 m->skipped_codestream = 1;
138 } else if (pc->frame_start_found && m->ft == j2k_cstream) {
139 reset_context(m);
140 return i + 1; // End of frame detected, return frame size.
141 }
142 m->in_codestream = 0;
143 } else if (m->in_codestream && (state & 0xFFFF) == 0xFF90) { // Are we in tile part header?
144 m->read_tp = 8;
145 } else if (pc->frame_start_found && info_marker((state & 0xFFFF0000)>>16) && m->in_codestream && (state & 0xFFFF)) {
146 // Calculate number of bytes to skip to get to end of the next marker.
147 m->skip_bytes = (state & 0xFFFF)-1;
148
149 // If the next marker is an info marker, skip to the end of of the marker length.
150 if (i + m->skip_bytes + 1 < buf_size) {
151 next_state = (buf[i + m->skip_bytes] << 8) | buf[i + m->skip_bytes + 1];
152 if (info_marker(next_state)) {
153 // Skip an additional 2 bytes to get to the end of the marker length.
154 m->skip_bytes += 2;
155 }
156 }
157 }
158 }
159
160 pc->state = state;
161 pc->state64 = state64;
162 return END_NOT_FOUND;
163 }
164
165 411 static int jpeg2000_parse(AVCodecParserContext *s,
166 AVCodecContext *avctx,
167 const uint8_t **poutbuf, int *poutbuf_size,
168 const uint8_t *buf, int buf_size)
169 {
170 411 JPEG2000ParserContext *m = s->priv_data;
171 411 ParseContext *pc = &m->pc;
172 int next;
173
174
1/2
✓ Branch 0 taken 411 times.
✗ Branch 1 not taken.
411 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
175 411 next= buf_size;
176 } else {
177 next= find_frame_end(m, buf, buf_size);
178
179 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
180 *poutbuf = NULL;
181 *poutbuf_size = 0;
182 return buf_size;
183 }
184 }
185
186 411 *poutbuf = buf;
187 411 *poutbuf_size = buf_size;
188 411 return next;
189 }
190
191 const AVCodecParser ff_jpeg2000_parser = {
192 .codec_ids = { AV_CODEC_ID_JPEG2000 },
193 .priv_data_size = sizeof(JPEG2000ParserContext),
194 .parser_parse = jpeg2000_parse,
195 .parser_close = ff_parse_close,
196 };
197