]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeg2000_parser.c
avcodec: Constify all the AVCodecParsers
[ffmpeg] / libavcodec / jpeg2000_parser.c
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 codestream_frame_end;
46     uint8_t read_tp;
47     uint8_t in_codestream;
48 } JPEG2000ParserContext;
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
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)
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
185 const 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,
190 };