3 * Copyright (c) 2020 Gautam Ramakrishnan
5 * This file is part of FFmpeg.
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.
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.
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
29 /* Whether frame is jp2 file or codestream
36 typedef struct JPEG2000ParserContext {
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 codestream_frame_end;
47 uint8_t in_codestream;
48 } JPEG2000ParserContext;
50 static inline void reset_context(JPEG2000ParserContext *m)
52 ParseContext *pc = &m->pc;
54 pc->frame_start_found= 0;
58 m->skipped_codestream = 0;
60 m->codestream_frame_end = 0;
66 /* Returns 1 if marker has any data which can be skipped
68 static uint8_t info_marker(uint16_t marker)
70 if (marker == 0xFF92 || marker == 0xFF4F ||
71 marker == 0xFF90 || marker == 0xFF93 ||
75 if (marker > 0xFF00) return 1;
80 * Find the end of the current frame in the bitstream.
81 * @return the position of the first byte of the next frame, or -1
83 static int find_frame_end(JPEG2000ParserContext *m, const uint8_t *buf, int buf_size)
85 ParseContext *pc= &m->pc;
90 state64 = pc->state64;
95 for (i = 0; i < buf_size; i++) {
96 state = state << 8 | buf[i];
97 state64 = state64 << 8 | buf[i];
103 if (m->codestream_frame_end) {
107 if (m->read_tp) { // Find out how many bytes inside Tile part codestream to skip.
108 if (m->read_tp == 1) {
109 m->skip_bytes = (state64 & 0xFFFFFFFF) - 10 > 0?
110 (state64 & 0xFFFFFFFF) - 10 : 0;
114 if (m->fheader_read) {
115 if (m->fheader_read == 1) {
116 if (state64 == 0x6A5020200D0A870A) { // JP2 signature box value.
117 if (pc->frame_start_found) {
118 pc->frame_start_found = 0;
122 pc->frame_start_found = 1;
129 if (state == 0x0000000C && m->bytes_read >= 3) { // Indicates start of JP2 file. Check signature next.
131 } else if ((state & 0xFFFF) == 0xFF4F) {
132 m->in_codestream = 1;
133 if (!pc->frame_start_found) {
134 pc->frame_start_found = 1;
136 } else if (pc->frame_start_found && m->ft == jp2_file && m->skipped_codestream) {
140 } else if ((state & 0xFFFF) == 0xFFD9) {
141 if (pc->frame_start_found && m->ft == jp2_file) {
142 m->skipped_codestream = 1;
143 } else if (pc->frame_start_found && m->ft == j2k_cstream) {
144 m->codestream_frame_end = 1;
146 m->in_codestream = 0;
147 } else if (m->in_codestream && (state & 0xFFFF) == 0xFF90) { // Are we in tile part header?
149 } else if (pc->frame_start_found && info_marker((state & 0xFFFF0000)>>16) && m->in_codestream) {
150 m->skip_bytes = (state & 0xFFFF) - 2;
155 pc->state64 = state64;
156 return END_NOT_FOUND;
159 static int jpeg2000_parse(AVCodecParserContext *s,
160 AVCodecContext *avctx,
161 const uint8_t **poutbuf, int *poutbuf_size,
162 const uint8_t *buf, int buf_size)
164 JPEG2000ParserContext *m = s->priv_data;
165 ParseContext *pc = &m->pc;
168 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
171 next= find_frame_end(m, buf, buf_size);
173 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
181 *poutbuf_size = buf_size;
185 AVCodecParser ff_jpeg2000_parser = {
186 .codec_ids = { AV_CODEC_ID_JPEG2000 },
187 .priv_data_size = sizeof(JPEG2000ParserContext),
188 .parser_parse = jpeg2000_parse,
189 .parser_close = ff_parse_close,