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
37
38 /* table_class: 0 = DC coef, 1 = AC coefs */
40 const uint8_t *bits_table, const uint8_t *value_table)
41 {
43
46
47 n = 0;
51 }
52
55
56 return n + 17;
57 }
58
62 uint16_t luma_intra_matrix[64],
63 uint16_t chroma_intra_matrix[64],
64 int hsample[3], int use_slices)
65 {
67 uint8_t *ptr;
68
69 if (m) {
70 int matrix_count = 1 + !!memcmp(luma_intra_matrix,
71 chroma_intra_matrix,
72 sizeof(luma_intra_matrix[0]) * 64);
74 matrix_count = 2;
75 /* quant matrixes */
77 put_bits(p, 16, 2 + matrix_count * (1 + 64));
78 put_bits(p, 4, 0);
/* 8 bit precision */
80 for (
int i = 0;
i < 64;
i++) {
82 put_bits(p, 8, luma_intra_matrix[j]);
83 }
84
85 if (matrix_count > 1) {
86 put_bits(p, 4, 0);
/* 8 bit precision */
90 put_bits(p, 8, chroma_intra_matrix[j]);
91 }
92 }
93 }
94
95 if (use_slices) {
99 }
100
101 /* huffman table */
105 put_bits(p, 16, 0);
/* patched later */
107
108 // Only MJPEG can have a variable Huffman variable. All other
109 // formats use the default Huffman table.
115
120 } else {
125
130 }
132 }
133
134 enum {
138 };
139
141 size_t *max_pkt_size)
142 {
144 size_t new_pkt_size;
145 int nb_chunks;
147 if (!sd || !sd->
size)
148 return 0;
149
152 "profile: too large for JPEG\n",
155 }
156
158 new_pkt_size = *max_pkt_size + nb_chunks * (UINT16_MAX + 2 /* APP2 marker */);
159 if (new_pkt_size < *max_pkt_size) /* overflow */
161 *max_pkt_size = new_pkt_size;
162 return 0;
163 }
164
167 {
170 uint8_t *ptr;
171
174
175 if (sar.
num > 65535 || sar.
den > 65535) {
178 "Cannot store exact aspect ratio %d:%d\n",
181 }
182
183 /* JFIF header */
186 ff_put_string(p,
"JFIF", 1);
/* this puts the trailing zero-byte too */
187 /* The most significant byte is used for major revisions, the least
188 * significant byte for minor revisions. Version 1.02 is the current
189 * released revision. */
191 put_bits(p, 8, 0);
/* units type: 0 - aspect ratio */
194 put_bits(p, 8, 0);
/* thumbnail width */
195 put_bits(p, 8, 0);
/* thumbnail height */
196 }
197
198 /* ICC profile */
200 if (sd && sd->
size) {
203 size_t remaining = sd->
size;
204 /* must already be checked by the packat allocation code */
207 for (
int i = 0;
i < nb_chunks;
i++) {
211 ptr[0] = 0xff; /* chunk marker, not part of ICC_HDR_SIZE */
218 ptr[17] = nb_chunks;
223 }
225 }
226
227 /* comment */
232 put_bits(p, 16, 0);
/* patched later */
236 }
237
245 put_bits(p, 16, 0);
/* patched later */
247 size = strlen(
"CS=ITU601")+3;
249 }
250 }
251
253 {
258 vsample[0] = hsample[0] =
259 vsample[1] = hsample[1] =
260 vsample[2] = hsample[2] =
261 vsample[3] = hsample[3] = 1;
263 vsample[0] = vsample[1] = vsample[2] = 2;
264 hsample[0] = hsample[1] = hsample[2] = 1;
265 } else {
266 int chroma_h_shift, chroma_v_shift;
268 &chroma_v_shift);
269 vsample[0] = 2;
270 vsample[1] = 2 >> chroma_v_shift;
271 vsample[2] = 2 >> chroma_v_shift;
272 hsample[0] = 2;
273 hsample[1] = 2 >> chroma_h_shift;
274 hsample[2] = 2 >> chroma_h_shift;
275 }
276 }
277
281 uint16_t luma_intra_matrix[64],
282 uint16_t chroma_intra_matrix[64],
283 int use_slices)
284 {
285 const int lossless = !m;
286 int hsample[4], vsample[4];
288 int chroma_matrix = !!memcmp(luma_intra_matrix,
289 chroma_intra_matrix,
290 sizeof(luma_intra_matrix[0])*64);
291
293
295
296 // hack for AMV mjpeg format
298 return;
299
301
303 luma_intra_matrix, chroma_intra_matrix, hsample,
304 use_slices);
305
310 }
311
316 put_bits(pb, 8, 9);
/* 9 bits/component RCT */
317 else
318 put_bits(pb, 8, 8);
/* 8 bits/component */
321 put_bits(pb, 8, components);
/* 3 or 4 components */
322
323 /* Y component */
324 put_bits(pb, 8, 1);
/* component number */
325 put_bits(pb, 4, hsample[0]);
/* H factor */
326 put_bits(pb, 4, vsample[0]);
/* V factor */
327 put_bits(pb, 8, 0);
/* select matrix */
328
329 /* Cb component */
330 put_bits(pb, 8, 2);
/* component number */
331 put_bits(pb, 4, hsample[1]);
/* H factor */
332 put_bits(pb, 4, vsample[1]);
/* V factor */
333 put_bits(pb, 8, lossless ? 0 : chroma_matrix);
/* select matrix */
334
335 /* Cr component */
336 put_bits(pb, 8, 3);
/* component number */
337 put_bits(pb, 4, hsample[2]);
/* H factor */
338 put_bits(pb, 4, vsample[2]);
/* V factor */
339 put_bits(pb, 8, lossless ? 0 : chroma_matrix);
/* select matrix */
340
341 if (components == 4) {
342 put_bits(pb, 8, 4);
/* component number */
343 put_bits(pb, 4, hsample[3]);
/* H factor */
344 put_bits(pb, 4, vsample[3]);
/* V factor */
345 put_bits(pb, 8, 0);
/* select matrix */
346 }
347
348 /* scan header */
350 put_bits(pb, 16, 6 + 2*components);
/* length */
351 put_bits(pb, 8, components);
/* 3 components */
352
353 /* Y component */
355 put_bits(pb, 4, 0);
/* DC huffman table index */
356 put_bits(pb, 4, 0);
/* AC huffman table index */
357
358 /* Cb component */
360 put_bits(pb, 4, 1);
/* DC huffman table index */
361 put_bits(pb, 4, lossless ? 0 : 1);
/* AC huffman table index */
362
363 /* Cr component */
365 put_bits(pb, 4, 1);
/* DC huffman table index */
366 put_bits(pb, 4, lossless ? 0 : 1);
/* AC huffman table index */
367
368 if (components == 4) {
369 /* Alpha component */
371 put_bits(pb, 4, 0);
/* DC huffman table index */
372 put_bits(pb, 4, 0);
/* AC huffman table index */
373 }
374
375 put_bits(pb, 8,
pred);
/* Ss (not used); pred only nonzero for LJPEG */
376
381 }
382
383 put_bits(pb, 8, 0);
/* Ah/Al (not used) */
384 }
385
387 {
390 uint8_t *buf = pb->
buf + start;
391 int align= (-(size_t)(buf))&3;
393
394 if (pad)
396
399
400 ff_count=0;
402 if(buf[
i]==0xFF) ff_count++;
403 }
406
407 v= *(uint32_t*)(&buf[
i]);
408 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
409 v= *(uint32_t*)(&buf[
i+4]);
410 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
411 v= *(uint32_t*)(&buf[
i+8]);
412 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
413 v= *(uint32_t*)(&buf[
i+12]);
414 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
415
420 }
422 if(buf[
i]==0xFF) ff_count++;
423 }
424
425 if(ff_count==0) return;
426
429
430 for(
i=
size-1; ff_count;
i--){
432
433 if(v==0xFF){
435 ff_count--;
436 }
437
439 }
440 }
441
442 /* isn't this function nicer than the one in the libjpeg ? */
444 const uint8_t *bits_table,
445 const uint8_t *val_table)
446 {
448
449 k = 0;
451 for (
int i = 1;
i <= 16;
i++) {
452 int nb = bits_table[
i];
453 for (int j = 0; j < nb; j++) {
454 int sym = val_table[k++];
456 huff_code[sym] =
code;
458 }
460 }
461 }
462
464 {
466
468 }
469
471 uint8_t *huff_size, uint16_t *huff_code)
472 {
473 int mant, nbits;
474
476 put_bits(pb, huff_size[0], huff_code[0]);
477 } else {
481 mant--;
482 }
483
485
486 put_bits(pb, huff_size[nbits], huff_code[nbits]);
487
489 }
490 }
491
493 {
501 "Non full-range YUV is non-standard, set strict_std_compliance "
502 "to at most unofficial to use it.\n");
504 }
505 return 0;
506 }