summaryrefslogtreecommitdiff
path: root/gst/jp2kdecimator/jp2kcodestream.h
blob: 168e7c48e38a07de0f676ca2323c3d35611008a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* GStreamer
 * Copyright (C) 2010 Oblong Industries, Inc.
 * Copyright (C) 2010 Collabora Multimedia
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef __JP2K_CODESTREAM_H__
#define __JP2K_CODESTREAM_H__

#include <gst/gst.h>
#include <gst/base/gstbytereader.h>
#include <gst/base/gstbitreader.h>
#include <gst/base/gstbytewriter.h>

#include "gstjp2kdecimator.h"

/* Used to represent codestream packets */
typedef struct
{
  gboolean sop;
  gboolean eph;
  guint16 seqno;

  const guint8 *data;
  guint length;
} Packet;

/* Used to represent unparsed markers for passthrough */
typedef struct
{
  const guint8 *data;
  guint length;
} Buffer;

typedef struct
{
  guint8 s;                     /* sample precision */
  guint8 xr, yr;                /* resolution */
} ComponentSize;

/* SIZ */
typedef struct
{
  guint16 caps;                 /* capabilities */
  guint32 x, y;                 /* reference grid size */
  guint32 xo, yo;               /* origin */
  ComponentSize *components;
  guint16 n_components;
  guint32 xt, yt;               /* tile sizes */
  guint32 xto, yto;             /* tile origin */
} ImageSize;

/* Progression orders
 * L - layer
 * R - resolution/decomposition level
 * C - component
 * P - position/precinct
 */
typedef enum
{
  PROGRESSION_ORDER_LRCP = 0,
  PROGRESSION_ORDER_RLCP,
  PROGRESSION_ORDER_RPCL,
  PROGRESSION_ORDER_PCRL,
  PROGRESSION_ORDER_CPRL,
  PROGRESSION_ORDER_MAX
} ProgressionOrder;

/* COD */
typedef struct
{
  /* Scod */
  gboolean sop, eph;
  /* SGcod */
  ProgressionOrder progression_order;
  guint16 n_layers;
  guint8 multi_component_transform;
  /* SPcod */
  guint8 n_decompositions;
  guint8 xcb, ycb;              /* code block dimensions */
  guint8 code_block_style;
  guint8 transformation;
  guint8 *PPx, *PPy;            /* precinct sizes (default:15,
                                 * otherwise n_decompositions+1 elements) */
} CodingStyleDefault;

/* SOT */
typedef struct
{
  guint16 tile_index;
  guint32 tile_part_size;
  guint8 tile_part_index, n_tile_parts;
} StartOfTile;

/* PLT */
typedef struct
{
  guint8 index;
  GArray *packet_lengths;       /* array of guint32 */
} PacketLengthTilePart;

typedef struct
{
  StartOfTile sot;
  CodingStyleDefault *cod;

  Buffer *qcd;
  GList *qcc;                   /* list of Buffer */

  GList *plt;                   /* list of PacketLengthTilePart */

  GList *com;                   /* list of Buffer */

  GList *packets;               /* list of Packet, codestream */

  /* TODO: COC, PPT */

  /* Calculated value */
  gint tile_x, tile_y;
  gint tx0, tx1, ty0, ty1;      /* tile dimensions */
} Tile;

typedef struct
{
  /* Parsed values */
  ImageSize siz;
  CodingStyleDefault cod;

  Buffer qcd;
  GList *qcc;                   /* list of Buffer */
  GList *crg, *com;             /* lists of Buffer */

  /* TODO: COC, PPM, TLM, PLM */

  guint n_tiles_x, n_tiles_y, n_tiles;  /* calculated */
  Tile *tiles;
} MainHeader;

typedef struct _PacketIterator PacketIterator;
struct _PacketIterator
{
  gboolean (*next) (PacketIterator * it);
  const MainHeader *header;
  const Tile *tile;

  gboolean first;

  gint cur_layer;
  gint cur_resolution;
  gint cur_component;
  gint cur_precinct;
  gint cur_x, cur_y;

  gint n_layers;
  gint n_resolutions;
  gint n_components;
  gint n_precincts, n_precincts_w, n_precincts_h;

  gint tx0, tx1, ty0, ty1;
  gint x_step, y_step;

  /* cached calculated values */
  /* depends on resolution and component */
  gint tcx0, tcx1, tcy0, tcy1;
  gint trx0, trx1, try0, try1;
  gint tpx0, tpx1, tpy0, tpy1;
  gint yr, xr;
  gint two_nl_r, two_ppx, two_ppy;

  gint cur_packet;
};

GstFlowReturn parse_main_header (GstJP2kDecimator * self, GstByteReader * reader, MainHeader * header);
guint sizeof_main_header (GstJP2kDecimator * self, const MainHeader * header);
void reset_main_header (GstJP2kDecimator * self, MainHeader * header);
GstFlowReturn write_main_header (GstJP2kDecimator * self, GstByteWriter * writer, const MainHeader * header);
GstFlowReturn decimate_main_header (GstJP2kDecimator * self, MainHeader * header);

#endif /* __JP2K_CODESTREAM_H__ */