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