forked from sackmotion/motion
-
Notifications
You must be signed in to change notification settings - Fork 36
/
jpegutils.c
1192 lines (998 loc) · 40.8 KB
/
jpegutils.c
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* jpegutils.c: Some Utility programs for dealing with
* JPEG encoded images
*
* Copyright (C) 1999 Rainer Johanni <[email protected]>
* Copyright (C) 2001 pHilipp Zabel <[email protected]>
* Copyright (C) 2008 Angel Carpintero <[email protected]>
*
* based on jdatasrc.c and jdatadst.c from the Independent
* JPEG Group's software by Thomas G. Lane
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "motion.h"
#include "jpegutils.h"
#include <setjmp.h>
#include <jpeglib.h>
#include <jerror.h>
#include <assert.h>
/*
* jpeg_data: buffer with input / output jpeg
* len: Length of jpeg buffer
* itype: 0: Not interlaced
* 1: Interlaced, Top field first
* 2: Interlaced, Bottom field first
* ctype Chroma format for decompression.
* Currently only Y4M_CHROMA_{420JPEG,422} are available
* raw0 buffer with input / output raw Y channel
* raw1 buffer with input / output raw U/Cb channel
* raw2 buffer with input / output raw V/Cr channel
* width width of Y channel (width of U/V is width/2)
* height height of Y channel (height of U/V is height/2)
*/
static void jpeg_buffer_src(j_decompress_ptr cinfo, unsigned char *buffer,
long num);
static void jpeg_buffer_dest(j_compress_ptr cinfo, unsigned char *buffer,
long len);
static void jpeg_skip_ff(j_decompress_ptr cinfo);
/*******************************************************************
* *
* The following routines define a JPEG Source manager which *
* just reads from a given buffer (instead of a file as in *
* the jpeg library) *
* *
*******************************************************************/
/*
* Initialize source --- called by jpeg_read_header
* before any data is actually read.
*/
static void init_source(j_decompress_ptr cinfo ATTRIBUTE_UNUSED)
{
/* No work necessary here */
}
/*
* Fill the input buffer --- called whenever buffer is emptied.
*
* Should never be called since all data should be allready provided.
* Is nevertheless sometimes called - sets the input buffer to data
* which is the JPEG EOI marker;
*
*/
static uint8_t EOI_data[2] = { 0xFF, 0xD9 };
static boolean fill_input_buffer(j_decompress_ptr cinfo)
{
cinfo->src->next_input_byte = EOI_data;
cinfo->src->bytes_in_buffer = 2;
return TRUE;
}
/*
* Skip data --- used to skip over a potentially large amount of
* uninteresting data (such as an APPn marker).
*
*/
static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
if (num_bytes > 0) {
if (num_bytes > (long) cinfo->src->bytes_in_buffer)
num_bytes = (long) cinfo->src->bytes_in_buffer;
cinfo->src->next_input_byte += (size_t) num_bytes;
cinfo->src->bytes_in_buffer -= (size_t) num_bytes;
}
}
/*
* Terminate source --- called by jpeg_finish_decompress
* after all data has been read. Often a no-op.
*/
static void term_source(j_decompress_ptr cinfo ATTRIBUTE_UNUSED)
{
/* No work necessary here */
}
/*
* Prepare for input from a data buffer.
*/
static void jpeg_buffer_src(j_decompress_ptr cinfo, unsigned char *buffer, long num)
{
/* The source object and input buffer are made permanent so that a series
* of JPEG images can be read from the same buffer by calling jpeg_buffer_src
* only before the first one. (If we discarded the buffer at the end of
* one image, we'd likely lose the start of the next one.)
* This makes it unsafe to use this manager and a different source
* manager serially with the same JPEG object. Caveat programmer.
*/
if (cinfo->src == NULL) { /* First time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof (struct jpeg_source_mgr));
}
cinfo->src->init_source = init_source;
cinfo->src->fill_input_buffer = fill_input_buffer;
cinfo->src->skip_input_data = skip_input_data;
cinfo->src->resync_to_restart = jpeg_resync_to_restart; /* Use default method */
cinfo->src->term_source = term_source;
cinfo->src->bytes_in_buffer = num;
cinfo->src->next_input_byte = (JOCTET *) buffer;
}
/*
* jpeg_skip_ff is not a part of the source manager but it is
* particularly useful when reading several images from the same buffer:
* It should be called to skip padding 0xff bytes beetween images.
*/
static void jpeg_skip_ff(j_decompress_ptr cinfo)
{
while (cinfo->src->bytes_in_buffer > 1
&& cinfo->src->next_input_byte[0] == 0xff
&& cinfo->src->next_input_byte[1] == 0xff) {
cinfo->src->bytes_in_buffer--;
cinfo->src->next_input_byte++;
}
}
/*******************************************************************
* *
* The following routines define a JPEG Destination manager *
* which just reads from a given buffer (instead of a file *
* as in the jpeg library) *
* *
*******************************************************************/
/*
* Initialize destination --- called by jpeg_start_compress
* before any data is actually written.
*/
static void init_destination(j_compress_ptr cinfo ATTRIBUTE_UNUSED)
{
/* No work necessary here */
}
/*
* Empty the output buffer --- called whenever buffer fills up.
*
* Should never be called since all data should be written to the buffer.
* If it gets called, the given jpeg buffer was too small.
*
*/
static boolean empty_output_buffer(j_compress_ptr cinfo)
{
/*FIXME: */
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Given jpeg buffer was too small");
ERREXIT (cinfo, JERR_BUFFER_SIZE); /* Shouldn't be FILE_WRITE but BUFFER_OVERRUN! */
return TRUE;
}
/*
* Terminate destination --- called by jpeg_finish_compress
* after all data has been written. Usually needs to flush buffer.
*
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
* application must deal with any cleanup that should happen even
* for error exit.
*/
static void term_destination(j_compress_ptr cinfo ATTRIBUTE_UNUSED)
{
/* No work necessary here */
}
/*
* Prepare for output to a stdio stream.
* The caller must have already opened the stream, and is responsible
* for closing it after finishing compression.
*/
static void jpeg_buffer_dest(j_compress_ptr cinfo, unsigned char *buf, long len)
{
/*
* The destination object is made permanent so that multiple JPEG images
* can be written to the same file without re-executing jpeg_stdio_dest.
* This makes it dangerous to use this manager and a different destination
* manager serially with the same JPEG object, because their private object
* sizes may be different. Caveat programmer.
*/
if (cinfo->dest == NULL) { /* First time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof (struct jpeg_destination_mgr));
}
cinfo->dest->init_destination = init_destination;
cinfo->dest->empty_output_buffer = empty_output_buffer;
cinfo->dest->term_destination = term_destination;
cinfo->dest->free_in_buffer = len;
cinfo->dest->next_output_byte = (JOCTET *) buf;
}
/*******************************************************************
* *
* decode_jpeg_data: Decode a (possibly interlaced) JPEG frame *
* *
*******************************************************************/
/*
* ERROR HANDLING:
*
* We want in all cases to return to the user.
* The following kind of error handling is from the
* example.c file in the Independent JPEG Group's JPEG software
*/
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* For return to caller */
/* Original emit_message method. */
JMETHOD(void, original_emit_message, (j_common_ptr cinfo, int msg_level));
/* Was a corrupt-data warning seen. */
int warning_seen;
};
static void my_error_exit(j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer. */
struct my_error_mgr *myerr = (struct my_error_mgr *) cinfo->err;
/*
* Always display the message.
* We could postpone this until after returning, if we chose.
*/
(*cinfo->err->output_message) (cinfo);
/* Return control to the setjmp point. */
longjmp (myerr->setjmp_buffer, 1);
}
static void my_emit_message(j_common_ptr cinfo, int msg_level)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer. */
struct my_error_mgr *myerr = (struct my_error_mgr *) cinfo->err;
if (msg_level < 0)
myerr->warning_seen = 1;
/* Call original emit_message() */
(myerr->original_emit_message)(cinfo, msg_level);
}
#define MAX_LUMA_WIDTH 4096
#define MAX_CHROMA_WIDTH 2048
static unsigned char buf0[16][MAX_LUMA_WIDTH];
static unsigned char buf1[8][MAX_CHROMA_WIDTH];
static unsigned char buf2[8][MAX_CHROMA_WIDTH];
static unsigned char chr1[8][MAX_CHROMA_WIDTH];
static unsigned char chr2[8][MAX_CHROMA_WIDTH];
#if 1 /* Generation of 'std' Huffman tables... */
static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr,
const UINT8 *bits, const UINT8 *val)
/* Define a Huffman table */
{
int nsymbols, len;
if (*htblptr == NULL)
*htblptr = jpeg_alloc_huff_table((j_common_ptr) dinfo);
/* Copy the number-of-symbols-of-each-code-length counts. */
memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits));
/*
* Validate the counts. We do this here mainly so we can copy the right
* number of symbols from the val[] array, without risking marching off
* the end of memory. jchuff.c will do a more thorough test later.
*/
nsymbols = 0;
for (len = 1; len <= 16; len++)
nsymbols += bits[len];
if (nsymbols < 1 || nsymbols > 256)
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Given jpeg buffer was too small");
memcpy((*htblptr)->huffval, val, nsymbols * sizeof(UINT8));
}
static void std_huff_tables (j_decompress_ptr dinfo)
/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
/* IMPORTANT: these are only valid for 8-bit data precision! */
{
static const UINT8 bits_dc_luminance[17] =
{ /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
static const UINT8 val_dc_luminance[] =
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
static const UINT8 bits_dc_chrominance[17] =
{ /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
static const UINT8 val_dc_chrominance[] =
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
static const UINT8 bits_ac_luminance[17] =
{ /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
static const UINT8 val_ac_luminance[] =
{ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa };
static const UINT8 bits_ac_chrominance[17] =
{ /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
static const UINT8 val_ac_chrominance[] =
{ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa };
add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[0],
bits_dc_luminance, val_dc_luminance);
add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[0],
bits_ac_luminance, val_ac_luminance);
add_huff_table(dinfo, &dinfo->dc_huff_tbl_ptrs[1],
bits_dc_chrominance, val_dc_chrominance);
add_huff_table(dinfo, &dinfo->ac_huff_tbl_ptrs[1],
bits_ac_chrominance, val_ac_chrominance);
}
static void guarantee_huff_tables(j_decompress_ptr dinfo)
{
if ((dinfo->dc_huff_tbl_ptrs[0] == NULL) &&
(dinfo->dc_huff_tbl_ptrs[1] == NULL) &&
(dinfo->ac_huff_tbl_ptrs[0] == NULL) &&
(dinfo->ac_huff_tbl_ptrs[1] == NULL)) {
std_huff_tables(dinfo);
}
}
#endif /* ...'std' Huffman table generation */
/*
* jpeg_data: Buffer with jpeg data to decode
* len: Length of buffer
* itype: 0: Not interlaced
* 1: Interlaced, Top field first
* 2: Interlaced, Bottom field first
* ctype Chroma format for decompression.
* Currently only Y4M_CHROMA_{420JPEG,422} are available
* returns:
* -1 on fatal error
* 0 on success
* 1 if jpeg lib threw a "corrupt jpeg data" warning.
* in this case, "a damaged output image is likely."
*
*/
int decode_jpeg_raw (unsigned char *jpeg_data, int len,
int itype, int ctype, unsigned int width,
unsigned int height, unsigned char *raw0,
unsigned char *raw1, unsigned char *raw2)
{
int numfields, hsf[3], field, yl, yc;
int i, xsl, xsc, xs, hdown;
unsigned int x, y = 0, vsf[3], xd;
JSAMPROW row0[16] = { buf0[0], buf0[1], buf0[2], buf0[3],
buf0[4], buf0[5], buf0[6], buf0[7],
buf0[8], buf0[9], buf0[10], buf0[11],
buf0[12], buf0[13], buf0[14], buf0[15]};
JSAMPROW row1[8] = { buf1[0], buf1[1], buf1[2], buf1[3],
buf1[4], buf1[5], buf1[6], buf1[7]};
JSAMPROW row2[16] = { buf2[0], buf2[1], buf2[2], buf2[3],
buf2[4], buf2[5], buf2[6], buf2[7]};
JSAMPROW row1_444[16], row2_444[16];
JSAMPARRAY scanarray[3] = { row0, row1, row2};
struct jpeg_decompress_struct dinfo;
struct my_error_mgr jerr;
/* We set up the normal JPEG error routines, then override error_exit. */
dinfo.err = jpeg_std_error (&jerr.pub);
jerr.pub.error_exit = my_error_exit;
/* Also hook the emit_message routine to note corrupt-data warnings. */
jerr.original_emit_message = jerr.pub.emit_message;
jerr.pub.emit_message = my_emit_message;
jerr.warning_seen = 0;
/* Establish the setjmp return context for my_error_exit to use. */
if (setjmp (jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. */
jpeg_destroy_decompress (&dinfo);
return -1;
}
jpeg_create_decompress (&dinfo);
jpeg_buffer_src (&dinfo, jpeg_data, len);
/*
* Read header, make some checks and try to figure out what the
* user really wants.
*/
jpeg_read_header (&dinfo, TRUE);
dinfo.raw_data_out = TRUE;
#if JPEG_LIB_VERSION >= 70
dinfo.do_fancy_upsampling = FALSE;
#endif
dinfo.out_color_space = JCS_YCbCr;
dinfo.dct_method = JDCT_IFAST;
guarantee_huff_tables(&dinfo);
jpeg_start_decompress (&dinfo);
if (dinfo.output_components != 3) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Output components of JPEG image"
" = %d, must be 3", dinfo.output_components);
goto ERR_EXIT;
}
for (i = 0; i < 3; i++) {
hsf[i] = dinfo.comp_info[i].h_samp_factor;
vsf[i] = dinfo.comp_info[i].v_samp_factor;
}
if ((hsf[0] != 2 && hsf[0] != 1) || hsf[1] != 1 || hsf[2] != 1 ||
(vsf[0] != 1 && vsf[0] != 2) || vsf[1] != 1 || vsf[2] != 1) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Unsupported sampling factors,"
" hsf=(%d, %d, %d) vsf=(%d, %d, %d) !", hsf[0], hsf[1],
hsf[2], vsf[0], vsf[1], vsf[2]);
goto ERR_EXIT;
}
if (hsf[0] == 1) {
if (height % 8 != 0) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: YUV 4:4:4 sampling, but image"
" height %d not dividable by 8 !", height);
goto ERR_EXIT;
}
for (y = 0; y < 16; y++) { // Allocate a special buffer for the extra sampling depth.
row1_444[y] = (unsigned char *)malloc(dinfo.output_width * sizeof(char));
row2_444[y] = (unsigned char *)malloc(dinfo.output_width * sizeof(char));
}
scanarray[1] = row1_444;
scanarray[2] = row2_444;
}
/* Height match image height or be exact twice the image height. */
if (dinfo.output_height == height) {
numfields = 1;
} else if (2 * dinfo.output_height == height) {
numfields = 2;
} else {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Read JPEG: requested height = %d, "
"height of image = %d", height, dinfo.output_height);
goto ERR_EXIT;
}
/* Width is more flexible */
if (dinfo.output_width > MAX_LUMA_WIDTH) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Image width of %d exceeds max",
dinfo.output_width);
goto ERR_EXIT;
}
if (width < 2 * dinfo.output_width / 3) {
/* Downsample 2:1 */
hdown = 1;
if (2 * width < dinfo.output_width)
xsl = (dinfo.output_width - 2 * width) / 2;
else
xsl = 0;
} else if (width == 2 * dinfo.output_width / 3) {
/* Special case of 3:2 downsampling */
hdown = 2;
xsl = 0;
} else {
/* No downsampling */
hdown = 0;
if (width < dinfo.output_width)
xsl = (dinfo.output_width - width) / 2;
else
xsl = 0;
}
/* Make xsl even, calculate xsc */
xsl = xsl & ~1;
xsc = xsl / 2;
yl = yc = 0;
for (field = 0; field < numfields; field++) {
if (field > 0) {
jpeg_read_header (&dinfo, TRUE);
dinfo.raw_data_out = TRUE;
#if JPEG_LIB_VERSION >= 70
dinfo.do_fancy_upsampling = FALSE;
#endif
dinfo.out_color_space = JCS_YCbCr;
dinfo.dct_method = JDCT_IFAST;
jpeg_start_decompress (&dinfo);
}
if (numfields == 2) {
switch (itype) {
case Y4M_ILACE_TOP_FIRST:
yl = yc = field;
break;
case Y4M_ILACE_BOTTOM_FIRST:
yl = yc = (1 - field);
break;
default:
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Input is interlaced but"
" no interlacing set");
goto ERR_EXIT;
}
} else {
yl = yc = 0;
}
while (dinfo.output_scanline < dinfo.output_height) {
/* Read raw data */
jpeg_read_raw_data (&dinfo, scanarray, 8 * vsf[0]);
for (y = 0; y < 8 * vsf[0]; yl += numfields, y++) {
xd = yl * width;
xs = xsl;
if (hdown == 0) {
for (x = 0; x < width; x++)
raw0[xd++] = row0[y][xs++];
} else if (hdown == 1) {
for (x = 0; x < width; x++, xs += 2)
raw0[xd++] = (row0[y][xs] + row0[y][xs + 1]) >> 1;
} else {
for (x = 0; x < width / 2; x++, xd += 2, xs += 3) {
raw0[xd] = (2 * row0[y][xs] + row0[y][xs + 1]) / 3;
raw0[xd + 1] = (2 * row0[y][xs + 2] + row0[y][xs + 1]) / 3;
}
}
}
/* Horizontal downsampling of chroma */
for (y = 0; y < 8; y++) {
xs = xsc;
if (hsf[0] == 1)
for (x = 0; x < width / 2; x++, xs++) {
row1[y][xs] = (row1_444[y][2*x] + row1_444[y][2*x + 1]) >> 1;
row2[y][xs] = (row2_444[y][2*x] + row2_444[y][2*x + 1]) >> 1;
}
xs = xsc;
if (hdown == 0) {
for (x = 0; x < width / 2; x++, xs++) {
chr1[y][x] = row1[y][xs];
chr2[y][x] = row2[y][xs];
}
} else if (hdown == 1) {
for (x = 0; x < width / 2; x++, xs += 2) {
chr1[y][x] = (row1[y][xs] + row1[y][xs + 1]) >> 1;
chr2[y][x] = (row2[y][xs] + row2[y][xs + 1]) >> 1;
}
} else {
for (x = 0; x < width / 2; x += 2, xs += 3) {
chr1[y][x] = (2 * row1[y][xs] + row1[y][xs + 1]) / 3;
chr1[y][x + 1] = (2 * row1[y][xs + 2] + row1[y][xs + 1]) / 3;
chr2[y][x] = (2 * row2[y][xs] + row2[y][xs + 1]) / 3;
chr2[y][x + 1] = (2 * row2[y][xs + 2] + row2[y][xs + 1]) / 3;
}
}
}
/* Vertical resampling of chroma */
switch (ctype) {
case Y4M_CHROMA_422:
if (vsf[0] == 1) {
/* Just copy */
for (y = 0; y < 8 /*&& yc < height */; y++, yc += numfields) {
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
raw1[xd] = chr1[y][x];
raw2[xd] = chr2[y][x];
}
}
} else {
/* upsample */
for (y = 0; y < 8 /*&& yc < height */; y++) {
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
raw1[xd] = chr1[y][x];
raw2[xd] = chr2[y][x];
}
yc += numfields;
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
raw1[xd] = chr1[y][x];
raw2[xd] = chr2[y][x];
}
yc += numfields;
}
}
break;
default:
/*
* Should be case Y4M_CHROMA_420JPEG: but use default: for compatibility. Some
* pass things like '420' in with the expectation that anything other than
* Y4M_CHROMA_422 will default to 420JPEG.
*/
if (vsf[0] == 1) {
/* Really downsample */
for (y = 0; y < 8 /*&& yc < height/2*/; y += 2, yc += numfields) {
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
assert(xd < (width * height / 4));
raw1[xd] = (chr1[y][x] + chr1[y + 1][x]) >> 1;
raw2[xd] = (chr2[y][x] + chr2[y + 1][x]) >> 1;
}
}
} else {
/* Just copy */
for (y = 0; y < 8 /* && yc < height / 2 */; y++, yc += numfields) {
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
raw1[xd] = chr1[y][x];
raw2[xd] = chr2[y][x];
}
}
}
break;
}
}
(void) jpeg_finish_decompress (&dinfo);
if (field == 0 && numfields > 1)
jpeg_skip_ff (&dinfo);
}
if (hsf[0] == 1) {
for (y = 0; y < 16; y++) { // Allocate a special buffer for the extra sampling depth.
free(row1_444[y]);
free(row2_444[y]);
}
}
jpeg_destroy_decompress (&dinfo);
if (jerr.warning_seen)
return 1;
else
return 0;
ERR_EXIT:
jpeg_destroy_decompress (&dinfo);
return -1;
}
/*
* jpeg_data: Buffer with jpeg data to decode, must be grayscale mode
* len: Length of buffer
* itype: 0: Not interlaced
* 1: Interlaced, Top field first
* 2: Interlaced, Bottom field first
* ctype Chroma format for decompression.
* Currently only Y4M_CHROMA_{420JPEG,422} are available
*/
int decode_jpeg_gray_raw(unsigned char *jpeg_data, int len,
int itype, int ctype, unsigned int width,
unsigned int height, unsigned char *raw0,
unsigned char *raw1, unsigned char *raw2)
{
int numfields, hsf[3], field, yl, yc, xsl, xsc, xs, xd, hdown;
unsigned int x, y, vsf[3];
JSAMPROW row0[16] = { buf0[0], buf0[1], buf0[2], buf0[3],
buf0[4], buf0[5], buf0[6], buf0[7],
buf0[8], buf0[9], buf0[10], buf0[11],
buf0[12], buf0[13], buf0[14], buf0[15]};
JSAMPARRAY scanarray[3] = { row0 };
struct jpeg_decompress_struct dinfo;
struct my_error_mgr jerr;
/* We set up the normal JPEG error routines, then override error_exit. */
dinfo.err = jpeg_std_error (&jerr.pub);
jerr.pub.error_exit = my_error_exit;
/* Establish the setjmp return context for my_error_exit to use. */
if (setjmp (jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. */
jpeg_destroy_decompress (&dinfo);
return -1;
}
jpeg_create_decompress (&dinfo);
jpeg_buffer_src (&dinfo, jpeg_data, len);
/*
* Read header, make some checks and try to figure out what the
* user really wants.
*/
jpeg_read_header (&dinfo, TRUE);
dinfo.raw_data_out = TRUE;
#if JPEG_LIB_VERSION >= 70
dinfo.do_fancy_upsampling = FALSE;
#endif
dinfo.out_color_space = JCS_GRAYSCALE;
dinfo.dct_method = JDCT_IFAST;
if (dinfo.jpeg_color_space != JCS_GRAYSCALE) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Expected grayscale colorspace"
" for JPEG raw decoding");
goto ERR_EXIT;
}
guarantee_huff_tables(&dinfo);
jpeg_start_decompress (&dinfo);
hsf[0] = 1; hsf[1] = 1; hsf[2] = 1;
vsf[0]= 1; vsf[1] = 1; vsf[2] = 1;
/* Height match image height or be exact twice the image height. */
if (dinfo.output_height == height) {
numfields = 1;
} else if (2 * dinfo.output_height == height) {
numfields = 2;
} else {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Read JPEG: requested height = %d, "
"height of image = %d", height, dinfo.output_height);
goto ERR_EXIT;
}
/* Width is more flexible */
if (dinfo.output_width > MAX_LUMA_WIDTH) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Image width of %d exceeds max",
dinfo.output_width);
goto ERR_EXIT;
}
if (width < 2 * dinfo.output_width / 3) {
/* Downsample 2:1 */
hdown = 1;
if (2 * width < dinfo.output_width)
xsl = (dinfo.output_width - 2 * width) / 2;
else
xsl = 0;
} else if (width == 2 * dinfo.output_width / 3) {
/* special case of 3:2 downsampling */
hdown = 2;
xsl = 0;
} else {
/* No downsampling */
hdown = 0;
if (width < dinfo.output_width)
xsl = (dinfo.output_width - width) / 2;
else
xsl = 0;
}
/* Make xsl even, calculate xsc */
xsl = xsl & ~1;
xsc = xsl / 2;
yl = yc = 0;
for (field = 0; field < numfields; field++) {
if (field > 0) {
jpeg_read_header (&dinfo, TRUE);
dinfo.raw_data_out = TRUE;
#if JPEG_LIB_VERSION >= 70
dinfo.do_fancy_upsampling = FALSE;
#endif
dinfo.out_color_space = JCS_GRAYSCALE;
dinfo.dct_method = JDCT_IFAST;
jpeg_start_decompress (&dinfo);
}
if (numfields == 2) {
switch (itype) {
case Y4M_ILACE_TOP_FIRST:
yl = yc = field;
break;
case Y4M_ILACE_BOTTOM_FIRST:
yl = yc = (1 - field);
break;
default:
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Input is interlaced"
" but no interlacing set");
goto ERR_EXIT;
}
} else {
yl = yc = 0;
}
while (dinfo.output_scanline < dinfo.output_height) {
jpeg_read_raw_data (&dinfo, scanarray, 16);
for (y = 0; y < 8 * vsf[0]; yl += numfields, y++) {
xd = yl * width;
xs = xsl;
if (hdown == 0) { // No horiz downsampling
for (x = 0; x < width; x++)
raw0[xd++] = row0[y][xs++];
} else if (hdown == 1) { // Half the res
for (x = 0; x < width; x++, xs += 2)
raw0[xd++] = (row0[y][xs] + row0[y][xs + 1]) >> 1;
} else { // 2:3 downsampling
for (x = 0; x < width / 2; x++, xd += 2, xs += 3) {
raw0[xd] = (2 * row0[y][xs] + row0[y][xs + 1]) / 3;
raw0[xd + 1] = (2 * row0[y][xs + 2] + row0[y][xs + 1]) / 3;
}
}
}
for (y = 0; y < 8; y++) {
xs = xsc;
if (hdown == 0) {
for (x = 0; x < width / 2; x++, xs++) {
chr1[y][x] = 0; //row1[y][xs];
chr2[y][x] = 0; //row2[y][xs];
}
} else if (hdown == 1) {
for (x = 0; x < width / 2; x++, xs += 2) {
chr1[y][x] = 0; //(row1[y][xs] + row1[y][xs + 1]) >> 1;
chr2[y][x] = 0; //(row2[y][xs] + row2[y][xs + 1]) >> 1;
}
} else {
for (x = 0; x < width / 2; x += 2, xs += 3) {
chr1[y][x] = 0; //(2 * row1[y][xs] + row1[y][xs + 1]) / 3;
chr1[y][x + 1] = 0;
//(2 * row1[y][xs + 2] + row1[y][xs + 1]) / 3;
chr2[y][x] = 0; // (2 * row2[y][xs] + row2[y][xs + 1]) / 3;
chr2[y][x + 1] = 0;
//(2 * row2[y][xs + 2] + row2[y][xs + 1]) / 3;
}
}
}
switch (ctype) {
case Y4M_CHROMA_422:
if (vsf[0] == 1) {
/* Just copy */
for (y = 0; y < 8 /* && yc < height */; y++, yc += numfields) {
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
raw1[xd] = 127; //chr1[y][x];
raw2[xd] = 127; //chr2[y][x];
}
}
} else {
/* upsample */
for (y = 0; y < 8 /* && yc < height */; y++) {
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
raw1[xd] = 127; //chr1[y][x];
raw2[xd] = 127; //chr2[y][x];
}
yc += numfields;
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
raw1[xd] = 127; //chr1[y][x];
raw2[xd] = 127; //chr2[y][x];
}
yc += numfields;
}
}
break;
/*
* Should be case Y4M_CHROMA_420JPEG: but use default: for compatibility. Some
* pass things like '420' in with the expectation that anything other than
* Y4M_CHROMA_422 will default to 420JPEG.
*/
default:
if (vsf[0] == 1) {
/* Really downsample */
for (y = 0; y < 8; y += 2, yc += numfields) {
xd = yc * width / 2;
for (x = 0; x < width / 2; x++, xd++) {
raw1[xd] = 127; //(chr1[y][x] + chr1[y + 1][x]) >> 1;
raw2[xd] = 127; //(chr2[y][x] + chr2[y + 1][x]) >> 1;
}
}
} else {
/* Just copy */
for (y = 0; y < 8; y++, yc += numfields) {