]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs_jpeg_syntax_template.c
Merge commit 'd7f4f5c4a18a0c9e62635cfa6fe8a9302b413c01'
[ffmpeg] / libavcodec / cbs_jpeg_syntax_template.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 static int FUNC(frame_header)(CodedBitstreamContext *ctx, RWContext *rw,
20                               JPEGRawFrameHeader *current)
21 {
22     int err, i;
23
24     HEADER("Frame Header");
25
26     u(16, Lf, 8, 8 + 3 * JPEG_MAX_COMPONENTS);
27
28     u(8,  P,  2, 16);
29     u(16, Y,  0, JPEG_MAX_HEIGHT);
30     u(16, X,  1, JPEG_MAX_WIDTH);
31     u(8,  Nf, 1, JPEG_MAX_COMPONENTS);
32
33     for (i = 0; i < current->Nf; i++) {
34         us(8, C[i],  i, 0, JPEG_MAX_COMPONENTS);
35         us(4, H[i],  i, 1, 4);
36         us(4, V[i],  i, 1, 4);
37         us(8, Tq[i], i, 0, 3);
38     }
39
40     return 0;
41 }
42
43 static int FUNC(quantisation_table)(CodedBitstreamContext *ctx, RWContext *rw,
44                                      JPEGRawQuantisationTable *current)
45 {
46     int err, i;
47
48     u(4, Pq, 0, 1);
49     u(4, Tq, 0, 3);
50
51     if (current->Pq) {
52         for (i = 0; i < 64; i++)
53             us(16, Q[i], i, 1, 255);
54     } else {
55         for (i = 0; i < 64; i++)
56             us(8,  Q[i], i, 1, 255);
57     }
58
59     return 0;
60 }
61
62 static int FUNC(dqt)(CodedBitstreamContext *ctx, RWContext *rw,
63                      JPEGRawQuantisationTableSpecification *current)
64 {
65     int err, i, n;
66
67     HEADER("Quantisation Tables");
68
69     u(16, Lq, 2, 2 + 4 * 65);
70     n = current->Lq / 65;
71
72     for (i = 0; i < n; i++)
73         CHECK(FUNC(quantisation_table)(ctx, rw, &current->table[i]));
74
75     return 0;
76 }
77
78 static int FUNC(huffman_table)(CodedBitstreamContext *ctx, RWContext *rw,
79                                JPEGRawHuffmanTable *current)
80 {
81     int err, i, j, ij;
82
83     u(4, Tc, 0, 1);
84     u(4, Th, 0, 3);
85
86     for (i = 0; i < 16; i++)
87         us(8, L[i], i, 0, 224);
88
89     ij = 0;
90     for (i = 0; i < 16; i++) {
91         for (j = 0; j < current->L[i]; j++) {
92             us(8, V[ij], ij, 0, 255);
93             ++ij;
94         }
95     }
96
97     return 0;
98 }
99
100 static int FUNC(dht)(CodedBitstreamContext *ctx, RWContext *rw,
101                      JPEGRawHuffmanTableSpecification *current)
102 {
103     int err, i, j, n;
104
105     HEADER("Huffman Tables");
106
107     u(16, Lh, 2, 2 + 8 * (1 + 16 + 256));
108
109     n = 2;
110     for (i = 0; n < current->Lh; i++) {
111         CHECK(FUNC(huffman_table)(ctx, rw, &current->table[i]));
112
113         ++n;
114         for (j = 0; j < 16; j++)
115             n += 1 + current->table[i].L[j];
116     }
117
118     return 0;
119 }
120
121 static int FUNC(scan_header)(CodedBitstreamContext *ctx, RWContext *rw,
122                              JPEGRawScanHeader *current)
123 {
124     int err, j;
125
126     HEADER("Scan");
127
128     u(16, Ls, 6, 6 + 2 * JPEG_MAX_COMPONENTS);
129
130     u(8, Ns, 1, 4);
131     for (j = 0; j < current->Ns; j++) {
132         us(8, Cs[j], j, 0, JPEG_MAX_COMPONENTS);
133         us(4, Td[j], j, 0, 3);
134         us(4, Ta[j], j, 0, 3);
135     }
136
137     u(8, Ss, 0, 63);
138     u(8, Se, 0, 63);
139     u(4, Ah, 0, 13);
140     u(4, Al, 0, 15);
141
142     return 0;
143 }
144
145 static int FUNC(application_data)(CodedBitstreamContext *ctx, RWContext *rw,
146                                   JPEGRawApplicationData *current)
147 {
148     int err, i;
149
150     HEADER("Application Data");
151
152     u(16, Lp, 2, 65535);
153
154     if (current->Lp > 2) {
155 #ifdef READ
156         current->Ap_ref = av_buffer_alloc(current->Lp - 2);
157         if (!current->Ap_ref)
158             return AVERROR(ENOMEM);
159         current->Ap = current->Ap_ref->data;
160 #endif
161
162         for (i = 0; i < current->Lp - 2; i++)
163             us(8, Ap[i], i, 0, 255);
164     }
165
166     return 0;
167 }
168
169 static int FUNC(comment)(CodedBitstreamContext *ctx, RWContext *rw,
170                          JPEGRawComment *current)
171 {
172     int err, i;
173
174     HEADER("Comment");
175
176     u(16, Lc, 2, 65535);
177
178     if (current->Lc > 2) {
179 #ifdef READ
180         current->Cm_ref = av_buffer_alloc(current->Lc - 2);
181         if (!current->Cm_ref)
182             return AVERROR(ENOMEM);
183         current->Cm = current->Cm_ref->data;
184 #endif
185
186         for (i = 0; i < current->Lc - 2; i++)
187             us(8, Cm[i], i, 0, 255);
188     }
189
190     return 0;
191 }