1 /*
2 * lossless JPEG shared bits
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdint.h>
24 #include <string.h>
25
28
36
37 /* table_class: 0 = DC coef, 1 = AC coefs */
39 const uint8_t *bits_table, const uint8_t *value_table)
40 {
42
45
46 n = 0;
50 }
51
54
55 return n + 17;
56 }
57
60 uint16_t luma_intra_matrix[64],
61 uint16_t chroma_intra_matrix[64],
62 int hsample[3])
63 {
65 uint8_t *ptr;
67
68 /* Since avctx->priv_data will point to LJpegEncContext in this case */
71
73 int matrix_count = 1 + !!memcmp(luma_intra_matrix,
74 chroma_intra_matrix,
75 sizeof(luma_intra_matrix[0]) * 64);
76 if (
s &&
s->force_duplicated_matrix)
77 matrix_count = 2;
78 /* quant matrixes */
80 put_bits(p, 16, 2 + matrix_count * (1 + 64));
81 put_bits(p, 4, 0);
/* 8 bit precision */
85 put_bits(p, 8, luma_intra_matrix[j]);
86 }
87
88 if (matrix_count > 1) {
89 put_bits(p, 4, 0);
/* 8 bit precision */
93 put_bits(p, 8, chroma_intra_matrix[j]);
94 }
95 }
96 }
97
102 }
103
104 /* huffman table */
108 put_bits(p, 16, 0);
/* patched later */
110
111 // Only MJPEG can have a variable Huffman variable. All other
112 // formats use the default Huffman table.
115 s->mjpeg_ctx->val_dc_luminance);
117 s->mjpeg_ctx->val_dc_chrominance);
118
120 s->mjpeg_ctx->val_ac_luminance);
122 s->mjpeg_ctx->val_ac_chrominance);
123 } else {
128
133 }
135 }
136
138 {
140 uint8_t *ptr;
141
144
145 if (sar.
num > 65535 || sar.
den > 65535) {
148 "Cannot store exact aspect ratio %d:%d\n",
151 }
152
153 /* JFIF header */
156 ff_put_string(p,
"JFIF", 1);
/* this puts the trailing zero-byte too */
157 /* The most significant byte is used for major revisions, the least
158 * significant byte for minor revisions. Version 1.02 is the current
159 * released revision. */
161 put_bits(p, 8, 0);
/* units type: 0 - aspect ratio */
164 put_bits(p, 8, 0);
/* thumbnail width */
165 put_bits(p, 8, 0);
/* thumbnail height */
166 }
167
168 /* comment */
173 put_bits(p, 16, 0);
/* patched later */
177 }
178
186 put_bits(p, 16, 0);
/* patched later */
188 size = strlen(
"CS=ITU601")+3;
190 }
191 }
192
194 {
195 int chroma_h_shift, chroma_v_shift;
196
198 &chroma_v_shift);
203 vsample[0] = hsample[0] =
204 vsample[1] = hsample[1] =
205 vsample[2] = hsample[2] =
206 vsample[3] = hsample[3] = 1;
208 vsample[0] = vsample[1] = vsample[2] = 2;
209 hsample[0] = hsample[1] = hsample[2] = 1;
210 } else {
211 vsample[0] = 2;
212 vsample[1] = 2 >> chroma_v_shift;
213 vsample[2] = 2 >> chroma_v_shift;
214 hsample[0] = 2;
215 hsample[1] = 2 >> chroma_h_shift;
216 hsample[2] = 2 >> chroma_h_shift;
217 }
218 }
219
222 uint16_t luma_intra_matrix[64],
223 uint16_t chroma_intra_matrix[64])
224 {
226 int hsample[4], vsample[4];
229 int chroma_matrix = !!memcmp(luma_intra_matrix,
230 chroma_intra_matrix,
231 sizeof(luma_intra_matrix[0])*64);
232
234
236
237 // hack for AMV mjpeg format
239
241
242 jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
243
248 }
249
254 put_bits(pb, 8, 9);
/* 9 bits/component RCT */
255 else
256 put_bits(pb, 8, 8);
/* 8 bits/component */
259 put_bits(pb, 8, components);
/* 3 or 4 components */
260
261 /* Y component */
262 put_bits(pb, 8, 1);
/* component number */
263 put_bits(pb, 4, hsample[0]);
/* H factor */
264 put_bits(pb, 4, vsample[0]);
/* V factor */
265 put_bits(pb, 8, 0);
/* select matrix */
266
267 /* Cb component */
268 put_bits(pb, 8, 2);
/* component number */
269 put_bits(pb, 4, hsample[1]);
/* H factor */
270 put_bits(pb, 4, vsample[1]);
/* V factor */
271 put_bits(pb, 8, lossless ? 0 : chroma_matrix);
/* select matrix */
272
273 /* Cr component */
274 put_bits(pb, 8, 3);
/* component number */
275 put_bits(pb, 4, hsample[2]);
/* H factor */
276 put_bits(pb, 4, vsample[2]);
/* V factor */
277 put_bits(pb, 8, lossless ? 0 : chroma_matrix);
/* select matrix */
278
279 if (components == 4) {
280 put_bits(pb, 8, 4);
/* component number */
281 put_bits(pb, 4, hsample[3]);
/* H factor */
282 put_bits(pb, 4, vsample[3]);
/* V factor */
283 put_bits(pb, 8, 0);
/* select matrix */
284 }
285
286 /* scan header */
288 put_bits(pb, 16, 6 + 2*components);
/* length */
289 put_bits(pb, 8, components);
/* 3 components */
290
291 /* Y component */
293 put_bits(pb, 4, 0);
/* DC huffman table index */
294 put_bits(pb, 4, 0);
/* AC huffman table index */
295
296 /* Cb component */
298 put_bits(pb, 4, 1);
/* DC huffman table index */
299 put_bits(pb, 4, lossless ? 0 : 1);
/* AC huffman table index */
300
301 /* Cr component */
303 put_bits(pb, 4, 1);
/* DC huffman table index */
304 put_bits(pb, 4, lossless ? 0 : 1);
/* AC huffman table index */
305
306 if (components == 4) {
307 /* Alpha component */
309 put_bits(pb, 4, 0);
/* DC huffman table index */
310 put_bits(pb, 4, 0);
/* AC huffman table index */
311 }
312
313 put_bits(pb, 8, lossless ?
pred : 0);
/* Ss (not used) */
314
319 }
320
321 put_bits(pb, 8, 0);
/* Ah/Al (not used) */
322
323 end:
324 if (!lossless) {
327
329 for(
i=1;
i<
s->slice_context_count;
i++)
330 s->thread_context[
i]->esc_pos = 0;
331 }
332 }
333
335 {
338 uint8_t *buf = pb->
buf + start;
339 int align= (-(size_t)(buf))&3;
341
342 if (pad)
344
347
348 ff_count=0;
350 if(buf[
i]==0xFF) ff_count++;
351 }
354
355 v= *(uint32_t*)(&buf[
i]);
356 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
357 v= *(uint32_t*)(&buf[
i+4]);
358 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
359 v= *(uint32_t*)(&buf[
i+8]);
360 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
361 v= *(uint32_t*)(&buf[
i+12]);
362 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
363
368 }
370 if(buf[
i]==0xFF) ff_count++;
371 }
372
373 if(ff_count==0) return;
374
377
378 for(
i=
size-1; ff_count;
i--){
380
381 if(v==0xFF){
383 ff_count--;
384 }
385
387 }
388 }
389
390 /* isn't this function nicer than the one in the libjpeg ? */
392 const uint8_t *bits_table,
393 const uint8_t *val_table)
394 {
396
397 k = 0;
399 for (
int i = 1;
i <= 16;
i++) {
400 int nb = bits_table[
i];
401 for (int j = 0; j < nb; j++) {
402 int sym = val_table[k++];
404 huff_code[sym] =
code;
406 }
408 }
409 }
410
412 {
414
416 }
417
419 uint8_t *huff_size, uint16_t *huff_code)
420 {
421 int mant, nbits;
422
424 put_bits(pb, huff_size[0], huff_code[0]);
425 } else {
429 mant--;
430 }
431
433
434 put_bits(pb, huff_size[nbits], huff_code[nbits]);
435
437 }
438 }
439
441 {
449 "Non full-range YUV is non-standard, set strict_std_compliance "
450 "to at most unofficial to use it.\n");
452 }
453 return 0;
454 }