FFmpeg
jpeg2000_parser.c
Go to the documentation of this file.
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,
34 };
35 
36 typedef struct JPEG2000ParserContext {
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
49 
50 static inline void reset_context(JPEG2000ParserContext *m)
51 {
52  ParseContext *pc = &m->pc;
53 
54  pc->frame_start_found= 0;
55  pc->state = 0;
56  m->bytes_read = 0;
57  m->ft = 0;
58  m->skipped_codestream = 0;
59  m->fheader_read = 0;
60  m->codestream_frame_end = 0;
61  m->skip_bytes = 0;
62  m->read_tp = 0;
63  m->in_codestream = 0;
64 }
65 
66 /* Returns 1 if marker has any data which can be skipped
67 */
68 static uint8_t info_marker(uint16_t marker)
69 {
70  if (marker == 0xFF92 || marker == 0xFF4F ||
71  marker == 0xFF90 || marker == 0xFF93 ||
72  marker == 0xFFD9)
73  return 0;
74  else
75  if (marker > 0xFF00) return 1;
76  return 0;
77 }
78 
79 /**
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
82  */
83 static int find_frame_end(JPEG2000ParserContext *m, const uint8_t *buf, int buf_size)
84 {
85  ParseContext *pc= &m->pc;
86  int i;
87  uint32_t state;
88  uint64_t state64;
89  state= pc->state;
90  state64 = pc->state64;
91  if (buf_size == 0) {
92  return 0;
93  }
94 
95  for (i = 0; i < buf_size; i++) {
96  state = state << 8 | buf[i];
97  state64 = state64 << 8 | buf[i];
98  m->bytes_read++;
99  if (m->skip_bytes) {
100  m->skip_bytes--;
101  continue;
102  }
103  if (m->codestream_frame_end) {
104  reset_context(m);
105  return i;
106  }
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;
111  }
112  m->read_tp--;
113  }
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;
119  reset_context(m);
120  return i - 11;
121  } else {
122  pc->frame_start_found = 1;
123  m->ft = jp2_file;
124  }
125  }
126  }
127  m->fheader_read--;
128  }
129  if (state == 0x0000000C && m->bytes_read >= 3) { // Indicates start of JP2 file. Check signature next.
130  m->fheader_read = 8;
131  } else if ((state & 0xFFFF) == 0xFF4F) {
132  m->in_codestream = 1;
133  if (!pc->frame_start_found) {
134  pc->frame_start_found = 1;
135  m->ft = j2k_cstream;
136  } else if (pc->frame_start_found && m->ft == jp2_file && m->skipped_codestream) {
137  reset_context(m);
138  return i - 1;
139  }
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;
145  }
146  m->in_codestream = 0;
147  } else if (m->in_codestream && (state & 0xFFFF) == 0xFF90) { // Are we in tile part header?
148  m->read_tp = 8;
149  } else if (pc->frame_start_found && info_marker((state & 0xFFFF0000)>>16) && m->in_codestream) {
150  m->skip_bytes = (state & 0xFFFF) - 2;
151  }
152  }
153 
154  pc->state = state;
155  pc->state64 = state64;
156  return END_NOT_FOUND;
157 }
158 
160  AVCodecContext *avctx,
161  const uint8_t **poutbuf, int *poutbuf_size,
162  const uint8_t *buf, int buf_size)
163 {
164  JPEG2000ParserContext *m = s->priv_data;
165  ParseContext *pc = &m->pc;
166  int next;
167 
168  if(s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
169  next= buf_size;
170  } else {
171  next= find_frame_end(m, buf, buf_size);
172 
173  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
174  *poutbuf = NULL;
175  *poutbuf_size = 0;
176  return buf_size;
177  }
178  }
179 
180  *poutbuf = buf;
181  *poutbuf_size = buf_size;
182  return next;
183 }
184 
187  .priv_data_size = sizeof(JPEG2000ParserContext),
188  .parser_parse = jpeg2000_parse,
189  .parser_close = ff_parse_close,
190 };
JPEG2000ParserContext::codestream_frame_end
uint8_t codestream_frame_end
Definition: jpeg2000_parser.c:45
j2k_cstream
@ j2k_cstream
Definition: jpeg2000_parser.c:33
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:319
JPEG2000ParserContext::in_codestream
uint8_t in_codestream
Definition: jpeg2000_parser.c:47
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
ParseContext
Definition: parser.h:28
jpeg2000_parse
static int jpeg2000_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: jpeg2000_parser.c:159
s
#define s(width, name)
Definition: cbs_vp9.c:257
JPEG2000ParserContext::reading_file_header
uint8_t reading_file_header
Definition: jpeg2000_parser.c:43
JPEG2000ParserContext::pc
ParseContext pc
Definition: jpeg2000_parser.c:37
ff_jpeg2000_parser
AVCodecParser ff_jpeg2000_parser
Definition: jpeg2000_parser.c:185
NULL
#define NULL
Definition: coverity.c:32
AVCodecParser::codec_ids
int codec_ids[5]
Definition: avcodec.h:3521
JPEG2000ParserContext::read_tp
uint8_t read_tp
Definition: jpeg2000_parser.c:46
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
find_frame_end
static int find_frame_end(JPEG2000ParserContext *m, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
Definition: jpeg2000_parser.c:83
JPEG2000ParserContext::fheader_read
uint8_t fheader_read
Definition: jpeg2000_parser.c:42
state
static struct @314 state
jp2_file
@ jp2_file
Definition: jpeg2000_parser.c:32
ff_combine_frame
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:234
reset_context
static void reset_context(JPEG2000ParserContext *m)
Definition: jpeg2000_parser.c:50
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3387
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
frame_type
frame_type
Definition: jpeg2000_parser.c:31
JPEG2000ParserContext
Definition: jpeg2000_parser.c:36
uint8_t
uint8_t
Definition: audio_convert.c:194
parser.h
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:137
AVCodecParserContext
Definition: avcodec.h:3353
JPEG2000ParserContext::skip_bytes
uint32_t skip_bytes
Definition: jpeg2000_parser.c:40
JPEG2000ParserContext::skipped_codestream
uint8_t skipped_codestream
Definition: jpeg2000_parser.c:44
AVCodecContext
main external API structure.
Definition: avcodec.h:526
info_marker
static uint8_t info_marker(uint16_t marker)
Definition: jpeg2000_parser.c:68
JPEG2000ParserContext::fheader_state
uint64_t fheader_state
Definition: jpeg2000_parser.c:39
ParseContext::state64
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
JPEG2000ParserContext::ft
enum frame_type ft
Definition: jpeg2000_parser.c:41
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:3520
JPEG2000ParserContext::bytes_read
uint64_t bytes_read
Definition: jpeg2000_parser.c:38