• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*  GStreamer JPEG parser
2  *  Copyright (C) 2011-2012 Intel Corporation
3  *  Copyright (C) 2015 Tim-Philipp Müller <tim@centricular.com>
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free
17  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  *  Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef GST_JPEG_PARSER_H
22 #define GST_JPEG_PARSER_H
23 
24 #ifndef OHOS_EXT_FUNC
25 // ohos.ext.func.0013
26 #ifndef GST_USE_UNSTABLE_API
27 #  warning "The JPEG parsing library is unstable API and may change in future."
28 #  warning "You can define GST_USE_UNSTABLE_API to avoid this warning."
29 #endif
30 #endif
31 
32 #include <gst/gst.h>
33 #include <gst/codecparsers/codecparsers-prelude.h>
34 
35 G_BEGIN_DECLS
36 
37 /**
38  * GST_JPEG_MAX_FRAME_COMPONENTS:
39  *
40  * Maximum number of image components in a frame (Nf).
41  *
42  * Since: 1.6
43  */
44 #define GST_JPEG_MAX_FRAME_COMPONENTS   256
45 
46 /**
47  * GST_JPEG_MAX_SCAN_COMPONENTS:
48  *
49  * Maximum number of image components in a scan (Ns).
50  *
51  * Since: 1.6
52  */
53 #define GST_JPEG_MAX_SCAN_COMPONENTS    4
54 
55 /**
56  * GST_JPEG_MAX_QUANT_ELEMENTS:
57  *
58  * Number of elements in the quantization table.
59  *
60  * Since: 1.6
61  */
62 #define GST_JPEG_MAX_QUANT_ELEMENTS     64
63 
64 typedef struct _GstJpegQuantTable       GstJpegQuantTable;
65 typedef struct _GstJpegQuantTables      GstJpegQuantTables;
66 typedef struct _GstJpegHuffmanTable     GstJpegHuffmanTable;
67 typedef struct _GstJpegHuffmanTables    GstJpegHuffmanTables;
68 typedef struct _GstJpegScanComponent    GstJpegScanComponent;
69 typedef struct _GstJpegScanHdr          GstJpegScanHdr;
70 typedef struct _GstJpegFrameComponent   GstJpegFrameComponent;
71 typedef struct _GstJpegFrameHdr         GstJpegFrameHdr;
72 typedef struct _GstJpegSegment          GstJpegSegment;
73 
74 /**
75  * GstJpegMarker:
76  * @GST_JPEG_MARKER_SOF0: Start of frame marker code (Baseline)
77  * @GST_JPEG_MARKER_SOF1: Start of frame marker code (Extended Sequential, Huffman)
78  * @GST_JPEG_MARKER_SOF2: Start of frame marker code (Progressive, Huffman)
79  * @GST_JPEG_MARKER_SOF3: Start of frame marker code (Lossless, Huffman)
80  * @GST_JPEG_MARKER_SOF5: Start of frame marker code (Differential Sequential, Huffman)
81  * @GST_JPEG_MARKER_SOF6: Start of frame marker code (Differential Progressive, Huffman)
82  * @GST_JPEG_MARKER_SOF7: Start of frame marker code (Differential Lossless, Huffman)
83  * @GST_JPEG_MARKER_SOF9: Start of frame marker code (Extended Sequential, Arithmetic)
84  * @GST_JPEG_MARKER_SOF10: Start of frame marker code (Progressive, Arithmetic)
85  * @GST_JPEG_MARKER_SOF11: Start of frame marker code (Lossless, Arithmetic)
86  * @GST_JPEG_MARKER_SOF13: Start of frame marker code (Differential Sequential, Arithmetic)
87  * @GST_JPEG_MARKER_SOF14: Start of frame marker code (Differential Progressive, Arithmetic)
88  * @GST_JPEG_MARKER_SOF15: Start of frame marker code (Differential Lossless, Arithmetic)
89  * @GST_JPEG_MARKER_DHT: Huffman table marker code
90  * @GST_JPEG_MARKER_DAC: Arithmetic coding marker code
91  * @GST_JPEG_MARKER_RST_MIN: Restart interval min marker code
92  * @GST_JPEG_MARKER_RST_MAX: Restart interval max marker code
93  * @GST_JPEG_MARKER_SOI: Start of image marker code
94  * @GST_JPEG_MARKER_EOI: End of image marker code
95  * @GST_JPEG_MARKER_SOS: Start of scan marker code
96  * @GST_JPEG_MARKER_DQT: Define quantization table marker code
97  * @GST_JPEG_MARKER_DNL: Define number of lines marker code
98  * @GST_JPEG_MARKER_DRI: Define restart interval marker code
99  * @GST_JPEG_MARKER_APP0: Application segment 0 marker code
100  * @GST_JPEG_MARKER_APP1: Application segment 1 marker code
101  * @GST_JPEG_MARKER_APP2: Application segment 2 marker code
102  * @GST_JPEG_MARKER_APP3: Application segment 3 marker code
103  * @GST_JPEG_MARKER_APP4: Application segment 4 marker code
104  * @GST_JPEG_MARKER_APP5: Application segment 5 marker code
105  * @GST_JPEG_MARKER_APP6: Application segment 6 marker code
106  * @GST_JPEG_MARKER_APP7: Application segment 7 marker code
107  * @GST_JPEG_MARKER_APP8: Application segment 8 marker code
108  * @GST_JPEG_MARKER_APP9: Application segment 9 marker code
109  * @GST_JPEG_MARKER_APP10: Application segment 10 marker code
110  * @GST_JPEG_MARKER_APP11: Application segment 11 marker code
111  * @GST_JPEG_MARKER_APP12: Application segment 12 marker code
112  * @GST_JPEG_MARKER_APP13: Application segment 13 marker code
113  * @GST_JPEG_MARKER_APP14: Application segment 14 marker code
114  * @GST_JPEG_MARKER_APP15: Application segment 15 marker code
115  * @GST_JPEG_MARKER_COM: Comment marker code
116  *
117  * Indicates the type of JPEG segment.
118  *
119  * Since: 1.6
120  */
121 typedef enum {
122   GST_JPEG_MARKER_SOF0          = 0xC0,
123   GST_JPEG_MARKER_SOF1          = 0xC1,
124   GST_JPEG_MARKER_SOF2          = 0xC2,
125   GST_JPEG_MARKER_SOF3          = 0xC3,
126   /* 0xC4 = DHT see below */
127   GST_JPEG_MARKER_SOF5          = 0xC5,
128   GST_JPEG_MARKER_SOF6          = 0xC6,
129   GST_JPEG_MARKER_SOF7          = 0xC7,
130   /* 0xC8 = reserved */
131   GST_JPEG_MARKER_SOF9          = 0xC9,
132   GST_JPEG_MARKER_SOF10         = 0xCA,
133   GST_JPEG_MARKER_SOF11         = 0xCB,
134   /* 0xCC = DAC see below */
135   GST_JPEG_MARKER_SOF13         = 0xCD,
136   GST_JPEG_MARKER_SOF14         = 0xCE,
137   GST_JPEG_MARKER_SOF15         = 0xCF,
138   GST_JPEG_MARKER_DHT           = 0xC4,
139   GST_JPEG_MARKER_DAC           = 0xCC,
140   GST_JPEG_MARKER_RST0          = 0xD0,
141   GST_JPEG_MARKER_RST1          = 0xD1,
142   GST_JPEG_MARKER_RST2          = 0xD2,
143   GST_JPEG_MARKER_RST3          = 0xD3,
144   GST_JPEG_MARKER_RST4          = 0xD4,
145   GST_JPEG_MARKER_RST5          = 0xD5,
146   GST_JPEG_MARKER_RST6          = 0xD6,
147   GST_JPEG_MARKER_RST7          = 0xD7,
148   GST_JPEG_MARKER_SOI           = 0xD8,
149   GST_JPEG_MARKER_EOI           = 0xD9,
150   GST_JPEG_MARKER_SOS           = 0xDA,
151   GST_JPEG_MARKER_DQT           = 0xDB,
152   GST_JPEG_MARKER_DNL           = 0xDC,
153   GST_JPEG_MARKER_DRI           = 0xDD,
154   GST_JPEG_MARKER_APP0          = 0xE0,
155   GST_JPEG_MARKER_APP1          = 0xE1,
156   GST_JPEG_MARKER_APP2          = 0xE2,
157   GST_JPEG_MARKER_APP3          = 0xE3,
158   GST_JPEG_MARKER_APP4          = 0xE4,
159   GST_JPEG_MARKER_APP5          = 0xE5,
160   GST_JPEG_MARKER_APP6          = 0xE6,
161   GST_JPEG_MARKER_APP7          = 0xE7,
162   GST_JPEG_MARKER_APP8          = 0xE8,
163   GST_JPEG_MARKER_APP9          = 0xE9,
164   GST_JPEG_MARKER_APP10         = 0xEA,
165   GST_JPEG_MARKER_APP11         = 0xEB,
166   GST_JPEG_MARKER_APP12         = 0xEC,
167   GST_JPEG_MARKER_APP13         = 0xED,
168   GST_JPEG_MARKER_APP14         = 0xEE,
169   GST_JPEG_MARKER_APP15         = 0xEF,
170   GST_JPEG_MARKER_COM           = 0xFE,
171 } GstJpegMarker;
172 
173 #define GST_JPEG_MARKER_SOF_MIN GST_JPEG_MARKER_SOF0
174 #define GST_JPEG_MARKER_SOF_MAX GST_JPEG_MARKER_SOF15
175 
176 #define GST_JPEG_MARKER_APP_MIN GST_JPEG_MARKER_APP0
177 #define GST_JPEG_MARKER_APP_MAX GST_JPEG_MARKER_APP15
178 
179 #define GST_JPEG_MARKER_RST_MIN GST_JPEG_MARKER_RST0
180 #define GST_JPEG_MARKER_RST_MAX GST_JPEG_MARKER_RST7
181 
182 /**
183  * GstJpegProfile:
184  * @GST_JPEG_PROFILE_BASELINE: Baseline DCT
185  * @GST_JPEG_PROFILE_EXTENDED: Extended sequential DCT
186  * @GST_JPEG_PROFILE_PROGRESSIVE: Progressive DCT
187  * @GST_JPEG_PROFILE_LOSSLESS: Lossless (sequential)
188  *
189  * JPEG encoding processes.
190  *
191  * Since: 1.6
192  */
193 typedef enum {
194   GST_JPEG_PROFILE_BASELINE     = 0x00,
195   GST_JPEG_PROFILE_EXTENDED     = 0x01,
196   GST_JPEG_PROFILE_PROGRESSIVE  = 0x02,
197   GST_JPEG_PROFILE_LOSSLESS     = 0x03,
198 } GstJpegProfile;
199 
200 /**
201  * GstJpegEntropyCodingMode:
202  * @GST_JPEG_ENTROPY_CODING_HUFFMAN: Huffman coding
203  * @GST_JPEG_ENTROPY_CODING_ARITHMETIC: arithmetic coding
204  *
205  * JPEG entropy coding mode.
206  *
207  * Since: 1.6
208  */
209 typedef enum {
210   GST_JPEG_ENTROPY_CODING_HUFFMAN       = 0x00,
211   GST_JPEG_ENTROPY_CODING_ARITHMETIC    = 0x08
212 } GstJpegEntropyCodingMode;
213 
214 /**
215  * GstJpegQuantTable:
216  * @quant_precision: Quantization table element precision (Pq)
217  * @quant_table: Quantization table elements (Qk)
218  * @valid: If the quantization table is valid, which means it has
219  *   already been parsed
220  *
221  * Quantization table.
222  *
223  * Since: 1.6
224  */
225 struct _GstJpegQuantTable
226 {
227   guint8 quant_precision;
228   guint16 quant_table[GST_JPEG_MAX_QUANT_ELEMENTS];
229   gboolean valid;
230 };
231 
232 /**
233  * GstJpegQuantTables:
234  * @quant_tables: All quantization tables
235  *
236  * Helper data structure that holds all quantization tables used to
237  * decode an image.
238  *
239  * Since: 1.6
240  */
241 struct _GstJpegQuantTables
242 {
243   GstJpegQuantTable quant_tables[GST_JPEG_MAX_SCAN_COMPONENTS];
244 };
245 
246 /**
247  * GstJpegHuffmanTable:
248  * @huf_bits: Number of Huffman codes of length i + 1 (Li)
249  * @huf_vales: Value associated with each Huffman code (Vij)
250  * @valid: If the Huffman table is valid, which means it has already
251  *   been parsed
252  *
253  * Huffman table.
254  *
255  * Since: 1.6
256  */
257 struct _GstJpegHuffmanTable
258 {
259   guint8 huf_bits[16];
260   guint8 huf_values[256];
261   gboolean valid;
262 };
263 
264 /**
265  * GstJpegHuffmanTables:
266  * @dc_tables: DC Huffman tables
267  * @ac_tables: AC Huffman tables
268  *
269  * Helper data structure that holds all AC/DC Huffman tables used to
270  * decode an image.
271  *
272  * Since: 1.6
273  */
274 struct _GstJpegHuffmanTables
275 {
276   GstJpegHuffmanTable dc_tables[GST_JPEG_MAX_SCAN_COMPONENTS];
277   GstJpegHuffmanTable ac_tables[GST_JPEG_MAX_SCAN_COMPONENTS];
278 };
279 
280 /**
281  * GstJpegScanComponent:
282  * @component_selector: Scan component selector (Csj)
283  * @dc_selector: DC entropy coding table destination selector (Tdj)
284  * @ac_selector: AC entropy coding table destination selector (Taj)
285 
286  * Component-specification parameters.
287  *
288  * Since: 1.6
289  */
290 struct _GstJpegScanComponent
291 {
292     guint8 component_selector;          /* 0 .. 255     */
293     guint8 dc_selector;                 /* 0 .. 3       */
294     guint8 ac_selector;                 /* 0 .. 3       */
295 };
296 
297 /**
298  * GstJpegScanHdr:
299  * @num_components: Number of image components in scan (Ns)
300  * @components: Image components
301  *
302  * Scan header.
303  *
304  * Since: 1.6
305  */
306 struct _GstJpegScanHdr
307 {
308   guint8 num_components;                /* 1 .. 4       */
309   GstJpegScanComponent components[GST_JPEG_MAX_SCAN_COMPONENTS];
310 
311   /*< private >*/
312   guint8 _reserved1; /* Ss */
313   guint8 _reserved2; /* Se */
314   guint8 _reserved3; /* Al */
315   guint8 _reserved4; /* Ah */
316 };
317 
318 /**
319  * GstJpegFrameComponent:
320  * @identifier: Component identifier (Ci)
321  * @horizontal_factor: Horizontal sampling factor (Hi)
322  * @vertical_factor: Vertical sampling factor (Vi)
323  * @quant_table_selector: Quantization table destination selector (Tqi)
324  *
325  * Component-specification parameters.
326  *
327  * Since: 1.6
328  */
329 struct _GstJpegFrameComponent
330 {
331   guint8 identifier;                    /* 0 .. 255     */
332   guint8 horizontal_factor;             /* 1 .. 4       */
333   guint8 vertical_factor;               /* 1 .. 4       */
334   guint8 quant_table_selector;          /* 0 .. 3       */
335 };
336 
337 /**
338  * GstJpegFrameHdr:
339  * @sample_precision: Sample precision (P)
340  * @height: Number of lines (Y)
341  * @width: Number of samples per line (X)
342  * @num_components: Number of image components in frame (Nf)
343  * @components: Image components
344  * @restart_interval: Number of MCU in the restart interval (Ri)
345  *
346  * Frame header.
347  *
348  * Since: 1.6
349  */
350 struct _GstJpegFrameHdr
351 {
352   guint8 sample_precision;              /* 2 .. 16      */
353   guint16 width;                        /* 1 .. 65535   */
354   guint16 height;                       /* 0 .. 65535   */
355   guint8 num_components;                /* 1 .. 255     */
356   GstJpegFrameComponent components[GST_JPEG_MAX_FRAME_COMPONENTS];
357 };
358 
359 /**
360  * GstJpegSegment:
361  * @marker: The type of the segment that starts at @offset
362  * @data: the data containing the jpeg segment starting at @offset
363  * @offset: The offset to the segment start in bytes. This is the
364  *   exact start of the segment, no marker code included
365  * @size: The size of the segment in bytes, or -1 if the end was not
366  *   found. It is the exact size of the segment, without the sync byte and
367  *   marker code but including any length bytes.
368  *
369  * A structure that contains the type of a segment, its offset and its size.
370  *
371  * Since: 1.6
372  */
373 struct _GstJpegSegment
374 {
375   GstJpegMarker marker;
376   const guint8 *data;
377   guint offset;
378   gssize size;
379 };
380 
381 GST_CODEC_PARSERS_API
382 gboolean  gst_jpeg_parse (GstJpegSegment * seg,
383                           const guint8   * data,
384                           gsize            size,
385                           guint            offset);
386 
387 GST_CODEC_PARSERS_API
388 gboolean  gst_jpeg_segment_parse_frame_header  (const GstJpegSegment  * segment,
389                                                 GstJpegFrameHdr       * frame_hdr);
390 
391 GST_CODEC_PARSERS_API
392 gboolean  gst_jpeg_segment_parse_scan_header   (const GstJpegSegment * segment,
393                                                 GstJpegScanHdr       * scan_hdr);
394 
395 GST_CODEC_PARSERS_API
396 gboolean  gst_jpeg_segment_parse_huffman_table (const GstJpegSegment * segment,
397                                                 GstJpegHuffmanTables * huff_tables);
398 
399 GST_CODEC_PARSERS_API
400 gboolean  gst_jpeg_segment_parse_restart_interval (const GstJpegSegment * segment,
401                                                    guint                * interval);
402 
403 GST_CODEC_PARSERS_API
404 gboolean  gst_jpeg_segment_parse_quantization_table (const GstJpegSegment * segment,
405                                                      GstJpegQuantTables   * quant_tables);
406 
407 GST_CODEC_PARSERS_API
408 void      gst_jpeg_get_default_quantization_tables (GstJpegQuantTables * quant_tables);
409 
410 GST_CODEC_PARSERS_API
411 void      gst_jpeg_get_default_huffman_tables (GstJpegHuffmanTables * huff_tables);
412 
413 G_END_DECLS
414 
415 #endif /* GST_JPEG_PARSER_H */
416