File: jpeg_decoder.c

package info (click to toggle)
guvcview 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,748 kB
  • sloc: ansic: 26,250; cpp: 3,786; makefile: 28
file content (1491 lines) | stat: -rw-r--r-- 38,851 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
/******************************************************************************#
#           guvcview              http://guvcview.sourceforge.net              #
#                                                                              #
#           Paulo Assis <pj.assis@gmail.com>                                   #
#           Nobuhiro Iwamatsu <iwamatsu@nigauri.org>                           #
#                             Add UYVY color support(Macbook iSight)           #
#                                                                              #
# 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    #
#                                                                              #
*******************************************************************************/

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libavutil/imgutils.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include "colorspaces.h"
#include "frame_decoder.h"
#include "gviewv4l2core.h"
#include "jpeg_decoder.h"

extern int verbosity;

/* default Huffman table*/
#define JPG_HUFFMAN_TABLE_LENGTH 0x01A0

const uint8_t jpeg_huffman_table[JPG_HUFFMAN_TABLE_LENGTH] = {
    // luminance dc - length bits
    0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00,
    // luminance dc - code
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
    // chrominance dc - length bits
    0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x00, 0x00, 0x00, 0x00, 0x00,
    // chrominance dc - code
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
    // luminance ac - number of codes with # bits (ordered by code length 1-16)
    0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
    0x04, 0x00, 0x00, 0x01, 0x7D,
    // luminance ac - run size (ordered by code length)
    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,
    // chrominance ac -number of codes with # bits (ordered by code length 1-16)
    0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04,
    0x04, 0x00, 0x01, 0x02, 0x77,
    // chrominance ac - run size (ordered by code length)
    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};

typedef struct _jpeg_decoder_context_t {
  void *codec_data;

  int width;
  int height;
  int pic_size;

  uint8_t *tmp_frame; // temp frame buffer

} jpeg_decoder_context_t;

static jpeg_decoder_context_t *jpeg_ctx = NULL;

#if MJPG_BUILTIN // use internal jpeg decoder

#define ISHIFT 11

#define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))

#ifndef __P
#define __P(x) x
#endif

/* special markers */
#define M_BADHUFF -1
#define M_EOF 0x80

#undef PREC
#define PREC int

/******** Markers *********/
#define M_SOI 0xd8
#define M_APP0 0xe0
#define M_DQT 0xdb
#define M_SOF0 0xc0
#define M_DHT 0xc4
#define M_DRI 0xdd
#define M_SOS 0xda
#define M_RST0 0xd0
#define M_EOI 0xd9
#define M_COM 0xfe

/*
 * IDCT data
 */
#define IMULT(a, b) (((a) * (b)) >> ISHIFT)
#define ITOINT(a) ((a) >> ISHIFT)

#define S22 ((PREC)IFIX(2 * 0.382683432))
#define C22 ((PREC)IFIX(2 * 0.923879532))
#define IC4 ((PREC)IFIX(1 / 0.707106781))

/*zigzag order used by idct*/
static unsigned char zig2[64] = {
    0,  2,  3,  9,  10, 20, 21, 35, 14, 16, 25, 31, 39, 46, 50, 57,
    5,  7,  12, 18, 23, 33, 37, 48, 27, 29, 41, 44, 52, 55, 59, 62,
    15, 26, 30, 40, 45, 51, 56, 58, 1,  4,  8,  11, 19, 22, 34, 36,
    28, 42, 43, 53, 54, 60, 61, 63, 6,  13, 17, 24, 32, 38, 47, 49};

static uint8_t zig[64] = {0,  1,  5,  6,  14, 15, 27, 28, 2,  4,  7,  13, 16,
                          26, 29, 42, 3,  8,  12, 17, 25, 30, 41, 43, 9,  11,
                          18, 24, 31, 40, 44, 53, 10, 19, 23, 32, 39, 45, 52,
                          54, 20, 22, 33, 38, 46, 51, 55, 60, 21, 34, 37, 47,
                          50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63};

/*coef used in idct*/
static PREC aaidct[8] = {IFIX(0.3535533906), IFIX(0.4903926402),
                         IFIX(0.4619397663), IFIX(0.4157348062),
                         IFIX(0.3535533906), IFIX(0.2777851165),
                         IFIX(0.1913417162), IFIX(0.0975451610)};

/*
 * decoder structs
 */
struct jpeg_decdata {
  int dcts[6 * 64 + 16];
  int out[64 * 6];
  int dquant[3][64];
};

struct in {
  uint8_t *p;
  uint32_t bits;
  int left;
  int marker;
  int(*func) __P((void *));
  void *data;
};

#define LEBI_DCL int le, bi
#define LEBI_GET(in) (le = in->left, bi = in->bits)
#define LEBI_PUT(in) (in->left = le, in->bits = bi)

/*********************************/
#define DECBITS 10 /* seems to be the optimum */

struct dec_hufftbl {
  int maxcode[17];
  int valptr[16];
  uint8_t vals[256];
  uint32_t llvals[1 << DECBITS];
};

// struct enc_hufftbl;

union hufftblp {
  struct dec_hufftbl *dhuff;
  // struct enc_hufftbl *ehuff;
};

struct scan {
  int dc; /* old dc value */

  union hufftblp hudc;
  union hufftblp huac;
  int next; /* when to switch to next scan */

  int cid; /* component id */
  int hv;  /* horiz/vert, copied from comp */
  int tq;  /* quant tbl, copied from comp */
};

#define MAXCOMP 4

struct comp {
  int cid;
  int hv;
  int tq;
};

struct jpginfo {
  int nc;  /* number of components */
  int ns;  /* number of scans */
  int dri; /* restart interval */
  int nm;  /* mcus til next marker */
  int rm;  /* next restart marker */
};

static struct jpginfo info;
static struct comp comps[MAXCOMP];

static struct scan dscans[MAXCOMP];

static uint8_t quant[4][64];

static struct dec_hufftbl dhuff[4];

#define dec_huffdc (dhuff + 0)
#define dec_huffac (dhuff + 2)

/*
 * build huffman data
 * args:
 *    hu - pointer to dec_hufftbl struct
 *    hufflen - pointer to int with code size
 *    huffvals - pointer to uint8_t with huffman values
 *
 * asserts:
 *    hu not null
 *    hufflen not null
 *    huffvals not null
 *
 * returns: error code (0 - OK)
 */
static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen,
                         uint8_t *huffvals) {
  /*assertions*/
  assert(hu != NULL);
  assert(hufflen != NULL);
  assert(huffvals != NULL);

  int code, k, i, j, d, x, c, v;
  for (i = 0; i < (1 << DECBITS); i++)
    hu->llvals[i] = 0;

  /*
   * llvals layout:
   *
   * value v already known, run r, backup u bits:
   *  vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu
   * value unknown, size b bits, run r, backup u bits:
   *  000000000000bbbb 0000 rrrr 0 uuuuuuu
   * value and size unknown:
   *  0000000000000000 0000 0000 0 0000000
   */
  code = 0;
  k = 0;
  for (i = 0; i < 16; i++, code <<= 1) { /* sizes */
    hu->valptr[i] = k;
    for (j = 0; j < hufflen[i]; j++) {
      hu->vals[k] = *huffvals++;
      if (i < DECBITS) {
        c = code << (DECBITS - 1 - i);
        v = hu->vals[k] & 0x0f; /* size */
        for (d = 1 << (DECBITS - 1 - i); --d >= 0;) {
          if (v + i < DECBITS) { /* both fit in table */
            x = d >> (DECBITS - 1 - v - i);
            if (v && x < (1 << (v - 1)))
              x += (-1 << v) + 1;
            x = x << 16 | (hu->vals[k] & 0xf0) << 4 | (DECBITS - (i + 1 + v)) |
                128;
          } else
            x = v << 16 | (hu->vals[k] & 0xf0) << 4 | (DECBITS - (i + 1));
          hu->llvals[c | d] = x;
        }
      }
      code++;
      k++;
    }
    hu->maxcode[i] = code;
  }
  hu->maxcode[16] = 0x20000; /* always terminate decode */
}

/*
 * huffman decoder initialization
 * args:
 *    none
 *
 * asserts:
 *    none
 *
 * returns: error code (0 - OK)
 */
static int huffman_init(void) {
  uint8_t *ptr = (uint8_t *)jpeg_huffman_table;
  int i, j, l;
  l = JPG_HUFFMAN_TABLE_LENGTH;
  while (l > 0) {
    int hufflen[16], k;
    uint8_t huffvals[256];

    int tc = *ptr++;
    int th = tc & 15;
    tc >>= 4;
    int tt = tc * 2 + th;
    if (tc > 1 || th > 1)
      return E_BAD_TABLES_ERR;
    for (i = 0; i < 16; i++)
      hufflen[i] = *ptr++;
    l -= 1 + 16;
    k = 0;
    for (i = 0; i < 16; i++) {
      for (j = 0; j < hufflen[i]; j++)
        huffvals[k++] = *ptr++;
      l -= hufflen[i];
    }
    dec_makehuff(dhuff + tt, hufflen, huffvals);
  }
  return 0;
}

/*
 * fillbits
 * args:
 *    inp - pointer to struct in
 *    le - left
 *    bi - bits
 *
 * asserts:
 *    inp not null
 *
 * returns: error code (0 - OK)
 */
static int fillbits(struct in *inp, int le, unsigned int bi) {
  /*asserts*/
  assert(inp != NULL);

  if (inp->marker) {
    if (le <= 16)
      inp->bits = bi << 16, le += 16;
    return le;
  }
  while (le <= 24) {
    int b = *inp->p++;
    int m = 0;

    if (b == 0xff && (m = *inp->p++) != 0) {
      if (m == M_EOF) {
        if (inp->func && (m = inp->func(inp->data)) == 0)
          continue;
      }
      inp->marker = m;
      if (le <= 16)
        bi = bi << 16, le += 16;
      break;
    }
    bi = bi << 8 | b;
    le += 8;
  }
  inp->bits = bi; /* tmp... 2 return values needed */
  return le;
}

static int dec_rec2 __P((struct in *, struct dec_hufftbl *, int *, int, int));

#define GETBITS(in, n)                                                         \
  ((le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), (le -= (n)),     \
   bi >> le & ((1 << (n)) - 1))

#define UNGETBITS(in, n) (le += (n))

#define DEC_REC(in, hu, r, i)                                                  \
  (r = GETBITS(in, DECBITS), i = hu->llvals[r],                                \
   i & 128 ? (UNGETBITS(in, i & 127), r = i >> 8 & 15, i >> 16)                \
           : (LEBI_PUT(in), i = dec_rec2(in, hu, &r, r, i), LEBI_GET(in), i))

/*
 * mcus decoder
 * args:
 *    inp - pointer to struct in
 *    dct - pointer to int with dct values
 *    n - number of dct values
 *    sc - pointer to struct scan
 *    maxp - pointer to int
 *
 * asserts:
 *    none
 *
 * returns: error code (0 - OK)
 */
static void decode_mcus(struct in *inp, int *dct, int n, struct scan *sc,
                        int *maxp) {
  struct dec_hufftbl *hu;
  int r = 0, t = 0;
  LEBI_DCL;

  memset(dct, 0, n * 64 * sizeof(*dct));
  LEBI_GET(inp);
  while (n-- > 0) {
    hu = sc->hudc.dhuff;
    *dct++ = (sc->dc += DEC_REC(inp, hu, r, t));

    hu = sc->huac.dhuff;
    int i = 63;

    while (i > 0) {
      t = DEC_REC(inp, hu, r, t);
      if (t == 0 && r == 0) {
        dct += i;
        break;
      }
      dct += r;
      *dct++ = t;
      i -= r + 1;
    }
    *maxp++ = 64 - i;
    if (n == sc->next)
      sc++;
  }
  LEBI_PUT(inp);
}

/*
 * readmarker decoder
 * args:
 *    inp - pointer to struct in
 *
 * asserts:
 *    inp not null
 *
 * returns: error code (0 - OK)
 */
static int dec_readmarker(struct in *inp) {
  /*asserts*/
  assert(inp != NULL);

  int m;

  inp->left = fillbits(inp, inp->left, inp->bits);
  if ((m = inp->marker) == 0)
    return 0;
  inp->left = 0;
  inp->marker = 0;
  return m;
}

/*
 * set input
 * args:
 *    inp - pointer to struct in
 *    p - pointer to pixel data
 *
 * asserts:
 *    inp not null
 *
 * returns: error code (0 - OK)
 */
static void setinput(struct in *inp, uint8_t *p) {
  /*asserts*/
  assert(inp != NULL);

  inp->p = p;
  inp->left = 0;
  inp->bits = 0;
  inp->marker = 0;
}

/*
 * IDCT quantization table
 */
static void idctqtab(uint8_t *qin, PREC *qout) {
  int i, j;

  for (i = 0; i < 8; i++)
    for (j = 0; j < 8; j++)
      qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] * IMULT(aaidct[i], aaidct[j]);
}

/****************************************************************/
/**************             idct                  ***************/
/****************************************************************/

/*
 * inverse dct for jpeg decoding
 * args:
 *   in -  pointer to input data ( mcu - after huffman decoding)
 *   out - pointer to data with output of idct (to be filled)
 *   quant - pointer to quantization data tables
 *   off - offset value (128.5 or 0.5)
 *   max - maximum input mcu index?
 *
 * asserts:
 *   none
 *
 * returns: none
 */
inline static void idct(int *inp, int *out, int *quant, long off, int max) {
  long t0, t1, t2, t3, t4, t5, t6, t7; // t ;
  long tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
  long tmp[64], *tmpp;
  int i, j, te;
  uint8_t *zig2p;

  t0 = off;
  if (max == 1) // single color mcu
  {
    t0 += inp[0] * quant[0]; // only DC available
    for (i = 0; i < 64; i++) // fill mcu with DC value
      out[i] = ITOINT(t0);
    return;
  }
  zig2p = zig2;
  tmpp = tmp;
  for (i = 0; i < 8; i++) // apply quantization table in zigzag order
  {
    j = *zig2p++;
    t0 += inp[j] * (long)quant[j];
    j = *zig2p++;
    t5 = inp[j] * (long)quant[j];
    j = *zig2p++;
    t2 = inp[j] * (long)quant[j];
    j = *zig2p++;
    t7 = inp[j] * (long)quant[j];
    j = *zig2p++;
    t1 = inp[j] * (long)quant[j];
    j = *zig2p++;
    t4 = inp[j] * (long)quant[j];
    j = *zig2p++;
    t3 = inp[j] * (long)quant[j];
    j = *zig2p++;
    t6 = inp[j] * (long)quant[j];

    if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0) {
      tmpp[0 * 8] = t0; // DC
      tmpp[1 * 8] = t0;
      tmpp[2 * 8] = t0;
      tmpp[3 * 8] = t0;
      tmpp[4 * 8] = t0;
      tmpp[5 * 8] = t0;
      tmpp[6 * 8] = t0;
      tmpp[7 * 8] = t0;

      tmpp++;
      t0 = 0;
      continue;
    }
    // IDCT;
    tmp0 = t0 + t1;
    t1 = t0 - t1;
    tmp2 = t2 - t3;
    t3 = t2 + t3;
    tmp2 = IMULT(tmp2, IC4) - t3;
    tmp3 = tmp0 + t3;
    t3 = tmp0 - t3;
    tmp1 = t1 + tmp2;
    tmp2 = t1 - tmp2;
    tmp4 = t4 - t7;
    t7 = t4 + t7;
    tmp5 = t5 + t6;
    t6 = t5 - t6;
    tmp6 = tmp5 - t7;
    t7 = tmp5 + t7;
    tmp5 = IMULT(tmp6, IC4);
    tmp6 = IMULT((tmp4 + t6), S22);
    tmp4 = IMULT(tmp4, (C22 - S22)) + tmp6;
    t6 = IMULT(t6, (C22 + S22)) - tmp6;
    t6 = t6 - t7;
    t5 = tmp5 - t6;
    t4 = tmp4 - t5;

    tmpp[0 * 8] = tmp3 + t7; // t0;
    tmpp[1 * 8] = tmp1 + t6; // t1;
    tmpp[2 * 8] = tmp2 + t5; // t2;
    tmpp[3 * 8] = t3 + t4;   // t3;
    tmpp[4 * 8] = t3 - t4;   // t4;
    tmpp[5 * 8] = tmp2 - t5; // t5;
    tmpp[6 * 8] = tmp1 - t6; // t6;
    tmpp[7 * 8] = tmp3 - t7; // t7;
    tmpp++;
    t0 = 0;
  }
  for (i = 0, j = 0; i < 8; i++) {
    t0 = tmp[j + 0];
    t1 = tmp[j + 1];
    t2 = tmp[j + 2];
    t3 = tmp[j + 3];
    t4 = tmp[j + 4];
    t5 = tmp[j + 5];
    t6 = tmp[j + 6];
    t7 = tmp[j + 7];
    if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0) {
      te = ITOINT(t0);
      out[j + 0] = te;
      out[j + 1] = te;
      out[j + 2] = te;
      out[j + 3] = te;
      out[j + 4] = te;
      out[j + 5] = te;
      out[j + 6] = te;
      out[j + 7] = te;
      j += 8;
      continue;
    }
    // IDCT;
    tmp0 = t0 + t1;
    t1 = t0 - t1;
    tmp2 = t2 - t3;
    t3 = t2 + t3;
    tmp2 = IMULT(tmp2, IC4) - t3;
    tmp3 = tmp0 + t3;
    t3 = tmp0 - t3;
    tmp1 = t1 + tmp2;
    tmp2 = t1 - tmp2;
    tmp4 = t4 - t7;
    t7 = t4 + t7;
    tmp5 = t5 + t6;
    t6 = t5 - t6;
    tmp6 = tmp5 - t7;
    t7 = tmp5 + t7;
    tmp5 = IMULT(tmp6, IC4);
    tmp6 = IMULT((tmp4 + t6), S22);
    tmp4 = IMULT(tmp4, (C22 - S22)) + tmp6;
    t6 = IMULT(t6, (C22 + S22)) - tmp6;
    t6 = t6 - t7;
    t5 = tmp5 - t6;
    t4 = tmp4 - t5;

    out[j + 0] = ITOINT(tmp3 + t7);
    out[j + 1] = ITOINT(tmp1 + t6);
    out[j + 2] = ITOINT(tmp2 + t5);
    out[j + 3] = ITOINT(t3 + t4);
    out[j + 4] = ITOINT(t3 - t4);
    out[j + 5] = ITOINT(tmp2 - t5);
    out[j + 6] = ITOINT(tmp1 - t6);
    out[j + 7] = ITOINT(tmp3 - t7);
    j += 8;
  }
}

/*********************************/
// static void col221111 __P((int *, unsigned char *, int));

typedef void (*ftopict)(int *out, uint8_t *pic, int width);

/*********************************/
/*
 * pointer to pixel data
 */
static uint8_t *datap;

/*
 * input structure (in)
 */
static struct in inp;

/*
 * get byte (8 bit) from datap
 */
static int getbyte(void) { return *datap++; }

/*
 * get word (16 bit) from datap
 */
static int getword(void) {
  int c1, c2;
  c1 = *datap++;
  c2 = *datap++;
  return c1 << 8 | c2;
}

/*
 * read jpeg tables (huffman and quantization)
 * args:
 *    till - Marker (frame - SOF0   scan - SOS)
 *    isDHT - flag indicating the presence of huffman tables (if 0 must use
 * default ones - MJPG frame) asserts: none
 *
 * returns: error code (0 - OK)
 */
static int readtables(int till, int *isDHT) {
  int l, i, j, lq, pq, tq;
  int tc, th, tt;

  for (;;) {
    if (getbyte() != 0xff)
      return -1;

    int m = 0;

    if ((m = getbyte()) == till)
      break;

    switch (m) {
    case 0xc2:
      return 0;
    /*read quantization tables (Lqt and Cqt)*/
    case M_DQT:
      lq = getword();
      while (lq > 2) {
        pq = getbyte();
        /*Lqt=0x00   Cqt=0x01*/
        tq = pq & 15;
        if (tq > 3)
          return -1;
        pq >>= 4;
        if (pq != 0)
          return -1;
        for (i = 0; i < 64; i++)
          quant[tq][i] = getbyte();
        lq -= 64 + 1;
      }
      break;
    /*read huffman table*/
    case M_DHT:
      l = getword();
      while (l > 2) {
        int hufflen[16], k;
        uint8_t huffvals[256];

        tc = getbyte();
        th = tc & 15;
        tc >>= 4;
        tt = tc * 2 + th;
        if (tc > 1 || th > 1)
          return -1;

        for (i = 0; i < 16; i++)
          hufflen[i] = getbyte();
        l -= 1 + 16;
        k = 0;
        for (i = 0; i < 16; i++) {
          for (j = 0; j < hufflen[i]; j++)
            huffvals[k++] = getbyte();
          l -= hufflen[i];
        }
        dec_makehuff(dhuff + tt, hufflen, huffvals);
      }
      /* has huffman tables defined (JPEG)*/
      *isDHT = 1;
      break;
    /*restart interval*/
    case M_DRI:
      l = getword();
      info.dri = getword();
      break;

    default:
      l = getword();
      while (l-- > 2)
        getbyte();
      break;
    }
  }
  return 0;
}

/*
 * init dscans
 * args:
 *    none
 *
 * asserts:
 *    none
 *
 * returns: none
 */
static void dec_initscans(void) {
  int i;

  info.nm = info.dri + 1;
  info.rm = M_RST0;
  for (i = 0; i < info.ns; i++)
    dscans[i].dc = 0;
}

/*
 * check markers
 * args:
 *    none
 *
 * asserts:
 *    none
 *
 * returns: error code (0 - OK)
 */
static int dec_checkmarker(void) {
  int i;

  if (dec_readmarker(&inp) != info.rm)
    return -1;
  info.nm = info.dri;
  info.rm = (info.rm + 1) & ~0x08;
  for (i = 0; i < info.ns; i++)
    dscans[i].dc = 0;
  return 0;
}

/*
 * check markers
 * args:
 *    none
 *
 * asserts:
 *    none
 *
 * returns: error code (0 - OK)
 */
static int dec_rec2(struct in *inp, struct dec_hufftbl *hu, int *runp, int c,
                    int i) {
  LEBI_DCL;

  LEBI_GET(inp);
  if (i) {
    UNGETBITS(inp, i & 127);
    *runp = i >> 8 & 15;
    i >>= 16;
  } else {
    for (i = DECBITS; (c = ((c << 1) | GETBITS(inp, 1))) >= (hu->maxcode[i]);
         i++)
      ;
    if (i >= 16) {
      inp->marker = M_BADHUFF;
      return 0;
    }
    i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2];
    *runp = i >> 4;
    i &= 15;
  }
  if (i == 0) { /* sigh, 0xf0 is 11 bit */
    LEBI_PUT(inp);
    return 0;
  }
  /* receive part */
  c = GETBITS(inp, i);
  if (c < (1 << (i - 1)))
    c += (-1 << i) + 1;
  LEBI_PUT(inp);
  return c;
}

/*
 * init (m)jpeg decoder context
 * args:
 *    width - image width
 *    height - image height
 *
 * asserts:
 *    none
 *
 * returns: error code (0 - E_OK)
 */
int jpeg_init_decoder(int width, int height) {
  if (jpeg_ctx != NULL)
    jpeg_close_decoder();

  jpeg_ctx = calloc(1, sizeof(jpeg_decoder_context_t));
  if (jpeg_ctx == NULL) {
    fprintf(
        stderr,
        "V4L2_CORE: FATAL memory allocation failure (jpeg_init_decoder): %s\n",
        strerror(errno));
    exit(-1);
  }

  jpeg_ctx->width = width;
  jpeg_ctx->height = height;
  jpeg_ctx->pic_size = width * height * 2; // yuyv
  jpeg_ctx->codec_data = NULL;

  jpeg_ctx->tmp_frame = calloc(jpeg_ctx->pic_size, sizeof(uint8_t));
  if (jpeg_ctx->tmp_frame == NULL) {
    fprintf(
        stderr,
        "V4L2_CORE: FATAL memory allocation failure (jpeg_init_decoder): %s\n",
        strerror(errno));
    exit(-1);
  }

  return E_OK;
}

/*
 * jpeg decode
 * args:
 *   out_buf -  pointer to picture data ( decoded image - yuyv format)
 *   in_buf -  pointer to input data ( compressed jpeg )
 *   size - picture size
 *
 * asserts:
 *   out_buf not null
 *   in_buf not null
 *
 * returns: error code (0 - OK)
 */
// int jpeg_decode(uint8_t **pic, uint8_t *buf, int width, int height)
int jpeg_decode(uint8_t *out_buf, uint8_t *in_buf, int size) {
  /*asserts*/
  assert(in_buf != NULL);
  assert(out_buf != NULL);

  memcpy(jpeg_ctx->tmp_frame, in_buf, size);

  struct jpeg_decdata *decdata;
  int i = 0, j = 0, m = 0, tac = 0, tdc = 0;
  int intwidth = 0, intheight = 0;
  int mcusx = 0, mcusy = 0, mx = 0, my = 0;
  int ypitch = 0, xpitch = 0, bpp = 0, pitch = 0, x = 0, y = 0;
  int mb = 0;
  int max[6];
  ftopict convert;
  int err = 0;
  int isInitHuffman = 0;
  decdata = calloc(1, sizeof(struct jpeg_decdata));
  if (decdata == NULL) {
    fprintf(stderr,
            "V4L2_CORE: FATAL memory allocation failure (jpeg_decode): %s\n",
            strerror(errno));
    exit(-1);
  }

  for (i = 0; i < 6; i++)
    max[i] = 0;

  if (!decdata) {
    err = E_ALLOC_ERR;
    goto error;
  }

  datap = jpeg_ctx->tmp_frame;
  /*check SOI (0xFFD8)*/
  if (getbyte() != 0xff) {
    err = E_NO_SOI_ERR;
    goto error;
  }
  if (getbyte() != M_SOI) {
    err = E_NO_SOI_ERR;
    goto error;
  }
  /*read tables - if exist, up to start frame marker (0xFFC0)*/
  if (readtables(M_SOF0, &isInitHuffman)) {
    err = E_BAD_TABLES_ERR;
    goto error;
  }
  getword();     /*header lenght*/
  i = getbyte(); /*precision (8 bit)*/
  if (i != 8) {
    err = E_NOT_8BIT_ERR;
    goto error;
  }
  intheight = getword(); /*height*/
  intwidth = getword();  /*width */

  if ((intheight & 7) || (intwidth & 7)) /*must be even*/
  {
    err = E_BAD_WIDTH_OR_HEIGHT_ERR;
    goto error;
  }
  info.nc = getbyte(); /*number of components*/
  if (info.nc > MAXCOMP) {
    err = E_TOO_MANY_COMPPS_ERR;
    goto error;
  }
  /*for each component*/
  for (i = 0; i < info.nc; i++) {
    int h, v;
    comps[i].cid = getbyte(); /*component id*/
    comps[i].hv = getbyte();
    v = comps[i].hv & 15;    /*vertical sampling   */
    h = comps[i].hv >> 4;    /*horizontal sampling */
    comps[i].tq = getbyte(); /*quantization table used*/
    if (h > 3 || v > 3) {
      err = E_ILLEGAL_HV_ERR;
      goto error;
    }
    if (comps[i].tq > 3) {
      err = E_QUANT_TBL_SEL_ERR;
      goto error;
    }
  }
  /*read tables - if exist, up to start of scan marker (0xFFDA)*/
  if (readtables(M_SOS, &isInitHuffman)) {
    err = E_BAD_TABLES_ERR;
    goto error;
  }
  getword();           /* header lenght */
  info.ns = getbyte(); /* number of scans */
  if (!info.ns) {
    printf("V4L2_CORE: (jpeg decoder) info ns %d/n", info.ns);
    err = E_NOT_YCBCR_ERR;
    goto error;
  }
  /*for each scan*/
  for (i = 0; i < info.ns; i++) {
    dscans[i].cid = getbyte(); /*component id*/
    tdc = getbyte();
    tac = tdc & 15; /*ac table*/
    tdc >>= 4;      /*dc table*/
    if (tdc > 1 || tac > 1) {
      err = E_QUANT_TBL_SEL_ERR;
      goto error;
    }
    for (j = 0; j < info.nc; j++)
      if (comps[j].cid == dscans[i].cid)
        break;
    if (j == info.nc) {
      err = E_UNKNOWN_CID_ERR;
      goto error;
    }
    dscans[i].hv = comps[j].hv;
    dscans[i].tq = comps[j].tq;
    dscans[i].hudc.dhuff = dec_huffdc + tdc;
    dscans[i].huac.dhuff = dec_huffac + tac;
  }

  i = getbyte(); /*0 */
  j = getbyte(); /*63*/
  m = getbyte(); /*0 */

  if (i != 0 || j != 63 || m != 0) {
    fprintf(stderr, "V4L2_CORE: (jpeg decoder) FW error,not seq DCT ??\n");
  }

  /*build huffman tables*/
  if (!isInitHuffman) {
    if (huffman_init() < 0)
      return E_BAD_TABLES_ERR;
  }
  /*
  if (dscans[0].cid != 1 || dscans[1].cid != 2 || dscans[2].cid != 3)
  {
          err = ERR_NOT_YCBCR_221111;
          goto error;
  }

  if (dscans[1].hv != 0x11 || dscans[2].hv != 0x11)
  {
          err = ERR_NOT_YCBCR_221111;
          goto error;
  }
  */

  /*
   * if internal width and external are not the same or heigth too
   * and pic not allocated realloc the good size and mark the change
   * need 1 macroblock line more ??
   */
  // if (intwidth != width || intheight != height || pic == NULL)
  //{
  //	width = intwidth;
  //	height = intheight;
  //	// BytesperPixel 2 yuyv , 3 rgb24
  //	*pic = calloc( intwidth * (intheight + 8) * 2, sizeof(uint8_t));
  //	if(*pic == NULL)
  //	{
  //		fprintf(stderr, "V4L2_CORE: FATAL memory allocation failure
  //(alloc_v4l2_frames): %s\n", strerror(errno)); 		exit(-1);
  //	}
  // }

  switch (dscans[0].hv) {
  case 0x22: // 411
    mb = 6;
    mcusx = jpeg_ctx->width >> 4;
    mcusy = jpeg_ctx->height >> 4;
    bpp = 2;
    xpitch = 16 * bpp;
    pitch = jpeg_ctx->width * bpp; // YUYV out
    ypitch = 16 * pitch;
    convert = yuv420pto422; // choose the right conversion function
    break;
  case 0x21: // 422
    mb = 4;
    mcusx = jpeg_ctx->width >> 4;
    mcusy = jpeg_ctx->height >> 3;
    bpp = 2;
    xpitch = 16 * bpp;
    pitch = jpeg_ctx->width * bpp; // YUYV out
    ypitch = 8 * pitch;
    convert = yuv422pto422; // choose the right conversion function
    break;
  case 0x11: // 444
    mcusx = jpeg_ctx->width >> 3;
    mcusy = jpeg_ctx->height >> 3;
    bpp = 2;
    xpitch = 8 * bpp;
    pitch = jpeg_ctx->width * bpp; // YUYV out
    ypitch = 8 * pitch;
    if (info.ns == 1) {
      mb = 1;
      convert = yuv400pto422; // choose the right conversion function
    } else {
      mb = 3;
      convert = yuv444pto422; // choose the right conversion function
    }
    break;
  default:
    err = E_NOT_YCBCR_ERR;
    goto error;
    break;
  }

  idctqtab(quant[dscans[0].tq], decdata->dquant[0]);
  idctqtab(quant[dscans[1].tq], decdata->dquant[1]);
  idctqtab(quant[dscans[2].tq], decdata->dquant[2]);
  setinput(&inp, datap);
  dec_initscans();

  dscans[0].next = 2;
  dscans[1].next = 1;
  dscans[2].next = 0; /* 4xx encoding */
  for (my = 0, y = 0; my < mcusy; my++, y += ypitch) {
    for (mx = 0, x = 0; mx < mcusx; mx++, x += xpitch) {
      if (info.dri && !--info.nm)
        if (dec_checkmarker()) {
          err = E_WRONG_MARKER_ERR;
          goto error;
        }
      switch (mb) {
      case 6:
        decode_mcus(&inp, decdata->dcts, mb, dscans, max);
        idct(decdata->dcts, decdata->out, decdata->dquant[0], IFIX(128.5),
             max[0]);
        idct(decdata->dcts + 64, decdata->out + 64, decdata->dquant[0],
             IFIX(128.5), max[1]);
        idct(decdata->dcts + 128, decdata->out + 128, decdata->dquant[0],
             IFIX(128.5), max[2]);
        idct(decdata->dcts + 192, decdata->out + 192, decdata->dquant[0],
             IFIX(128.5), max[3]);
        idct(decdata->dcts + 256, decdata->out + 256, decdata->dquant[1],
             IFIX(0.5), max[4]);
        idct(decdata->dcts + 320, decdata->out + 320, decdata->dquant[2],
             IFIX(0.5), max[5]);
        break;

      case 4:
        decode_mcus(&inp, decdata->dcts, mb, dscans, max);
        idct(decdata->dcts, decdata->out, decdata->dquant[0], IFIX(128.5),
             max[0]);
        idct(decdata->dcts + 64, decdata->out + 64, decdata->dquant[0],
             IFIX(128.5), max[1]);
        idct(decdata->dcts + 128, decdata->out + 256, decdata->dquant[1],
             IFIX(0.5), max[4]);
        idct(decdata->dcts + 192, decdata->out + 320, decdata->dquant[2],
             IFIX(0.5), max[5]);
        break;

      case 3:
        decode_mcus(&inp, decdata->dcts, mb, dscans, max);
        idct(decdata->dcts, decdata->out, decdata->dquant[0], IFIX(128.5),
             max[0]);
        idct(decdata->dcts + 64, decdata->out + 256, decdata->dquant[1],
             IFIX(0.5), max[4]);
        idct(decdata->dcts + 128, decdata->out + 320, decdata->dquant[2],
             IFIX(0.5), max[5]);
        break;

      case 1:
        decode_mcus(&inp, decdata->dcts, mb, dscans, max);
        idct(decdata->dcts, decdata->out, decdata->dquant[0], IFIX(128.5),
             max[0]);
        break;
      } // switch enc411
      convert(decdata->out, out_buf + y + x, pitch); // convert to 422
    }
  }

  m = dec_readmarker(&inp);
  if (m != M_EOI) {
    err = E_NO_EOI_ERR;
    goto error;
  }
  free(decdata);
  return 0;
error:
  free(decdata);
  return err;
}

/*
 * close (m)jpeg decoder context
 * args:
 *    none
 *
 * asserts:
 *    none
 *
 * returns: none
 */
void jpeg_close_decoder() {
  if (jpeg_ctx == NULL)
    return;

  free(jpeg_ctx->tmp_frame);
  free(jpeg_ctx);

  jpeg_ctx = NULL;
}

#else // use libavcodec to decode mjpeg data

typedef struct _codec_data_t {
  const AVCodec *codec;
  AVCodecContext *context;
  AVFrame *picture;
} codec_data_t;

/*
 * init (m)jpeg decoder context
 * args:
 *    width - image width
 *    height - image height
 *
 * asserts:
 *    none
 *
 * returns: error code (0 - E_OK)
 */
int jpeg_init_decoder(int width, int height) {
#if !LIBAVCODEC_VER_AT_LEAST(53, 34)
  avcodec_init();
#endif
#if !LIBAVCODEC_VER_AT_LEAST(58, 9)
  /*
   * register all the codecs (we can also register only the codec
   * we wish to have smaller code)
   */
  avcodec_register_all();
#endif
  av_log_set_level(AV_LOG_PANIC);

  if (jpeg_ctx != NULL)
    jpeg_close_decoder();

  jpeg_ctx = calloc(1, sizeof(jpeg_decoder_context_t));
  if (jpeg_ctx == NULL) {
    fprintf(
        stderr,
        "V4L2_CORE: FATAL memory allocation failure (jpeg_init_decoder): %s\n",
        strerror(errno));
    exit(-1);
  }

  codec_data_t *codec_data = calloc(1, sizeof(codec_data_t));
  if (codec_data == NULL) {
    fprintf(
        stderr,
        "V4L2_CORE: FATAL memory allocation failure (jpeg_init_decoder): %s\n",
        strerror(errno));
    exit(-1);
  }

  codec_data->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  if (!codec_data->codec) {
    fprintf(stderr, "V4L2_CORE: (mjpeg decoder) codec not found\n");
    free(jpeg_ctx);
    free(codec_data);
    jpeg_ctx = NULL;
    return E_NO_CODEC;
  }

#if LIBAVCODEC_VER_AT_LEAST(57, 107)
  codec_data->context = avcodec_alloc_context3(codec_data->codec);
#elif LIBAVCODEC_VER_AT_LEAST(53, 6)
  codec_data->context = avcodec_alloc_context3(codec_data->codec);
  avcodec_get_context_defaults3(codec_data->context, codec_data->codec);
#else
  codec_data->context = avcodec_alloc_context();
  avcodec_get_context_defaults(codec_data->context);
#endif
  if (codec_data->context == NULL) {
    fprintf(
        stderr,
        "V4L2_CORE: FATAL memory allocation failure (h264_init_decoder): %s\n",
        strerror(errno));
    exit(-1);
  }

  codec_data->context->pix_fmt = AV_PIX_FMT_YUV422P;
  codec_data->context->width = width;
  codec_data->context->height = height;
  // jpeg_ctx->context->dsp_mask = (FF_MM_MMX | FF_MM_MMXEXT | FF_MM_SSE);

#if LIBAVCODEC_VER_AT_LEAST(53, 6)
  if (avcodec_open2(codec_data->context, codec_data->codec, NULL) < 0)
#else
  if (avcodec_open(codec_data->context, codec_data->codec) < 0)
#endif
  {
    fprintf(stderr, "V4L2_CORE: (mjpeg decoder) couldn't open codec\n");

#if LIBAVCODEC_VER_AT_LEAST(61, 3)
    avcodec_free_context(&codec_data->context);
#else
    avcodec_close(codec_data->context);
    free(codec_data->context);
#endif
    free(codec_data);
    free(jpeg_ctx);
    jpeg_ctx = NULL;
    return E_NO_CODEC;
  }

#if LIBAVCODEC_VER_AT_LEAST(55, 28)
  codec_data->picture = av_frame_alloc();
  av_frame_unref(codec_data->picture);
#else
  codec_data->picture = avcodec_alloc_frame();
  avcodec_get_frame_defaults(codec_data->picture);
#endif

  /*alloc temp buffer*/
  jpeg_ctx->tmp_frame = calloc(width * height * 2, sizeof(uint8_t));
  if (jpeg_ctx->tmp_frame == NULL) {
    fprintf(
        stderr,
        "V4L2_CORE: FATAL memory allocation failure (jpeg_init_decoder): %s\n",
        strerror(errno));
    exit(-1);
  }
#if LIBAVUTIL_VER_AT_LEAST(54, 6)
  jpeg_ctx->pic_size =
      av_image_get_buffer_size(codec_data->context->pix_fmt, width, height, 1);
#else
  jpeg_ctx->pic_size =
      avpicture_get_size(codec_data->context->pix_fmt, width, height);
#endif
  jpeg_ctx->width = width;
  jpeg_ctx->height = height;
  jpeg_ctx->codec_data = codec_data;

  return E_OK;
}

/*
 * decode (m)jpeg frame
 * args:
 *    out_buf - pointer to decoded data
 *    in_buf - pointer to h264 data
 *    size - in_buf size
 *
 * asserts:
 *    jpeg_ctx is not null
 *    in_buf is not null
 *    out_buf is not null
 *
 * returns: decoded data size
 */
int jpeg_decode(uint8_t *out_buf, uint8_t *in_buf, int size) {
  /*asserts*/
  assert(jpeg_ctx != NULL);
  assert(in_buf != NULL);
  assert(out_buf != NULL);

  int got_frame = 0;
  codec_data_t *codec_data = (codec_data_t *)jpeg_ctx->codec_data;

#if LIBAVCODEC_VER_AT_LEAST(58, 129)
  AVPacket *avpkt = av_packet_alloc();
  if (!avpkt) {
    fprintf(stderr, "V4L2_CORE uvc_H264: could not allocate av_packet\n");
    return -1;
  }

  avpkt->size = size;
  avpkt->data = in_buf;

  int ret =
      libav_decode(codec_data->context, codec_data->picture, &got_frame, avpkt);

  av_packet_free(&avpkt);
#else
  AVPacket avpkt;
  av_init_packet(&avpkt);
  avpkt.size = size;
  avpkt.data = in_buf;
  int ret = libav_decode(codec_data->context, codec_data->picture, &got_frame,
                         &avpkt);
#endif
  if (ret < 0) {
    fprintf(stderr, "V4L2_CORE: (jpeg decoder) error while decoding frame\n");
    return ret;
  }

  if (got_frame) {
#if LIBAVUTIL_VER_AT_LEAST(54, 6)
    av_image_copy_to_buffer(jpeg_ctx->tmp_frame, jpeg_ctx->pic_size,
                            (const uint8_t *const *)codec_data->picture->data,
                            codec_data->picture->linesize,
                            codec_data->context->pix_fmt, jpeg_ctx->width,
                            jpeg_ctx->height, 1);
#else
    avpicture_layout((AVPicture *)codec_data->picture,
                     codec_data->context->pix_fmt, jpeg_ctx->width,
                     jpeg_ctx->height, jpeg_ctx->tmp_frame, jpeg_ctx->pic_size);
#endif
    /* requested libavcodec output format is yuv422p
     * but apparently for some cameras
     * (https://sourceforge.net/u/shicetu/uos-guvcview/ci/fbdc4b23f0072c5285383d09d2724dbf962d8a7f/)
     * it can turn out be in yuv420p */
    if (codec_data->context->pix_fmt == AV_PIX_FMT_YUV422P ||
        codec_data->context->pix_fmt == AV_PIX_FMT_YUVJ422P) {

      yuv422p_to_yu12(out_buf, jpeg_ctx->tmp_frame, jpeg_ctx->width,
                      jpeg_ctx->height);
      return jpeg_ctx->pic_size;

    } else if (codec_data->context->pix_fmt == AV_PIX_FMT_YUVJ420P ||
               codec_data->context->pix_fmt == AV_PIX_FMT_YUV420P) {

      if (jpeg_ctx->pic_size >
          (size_t)(jpeg_ctx->width * jpeg_ctx->height * 3 / 2))
        jpeg_ctx->pic_size =
            (size_t)(jpeg_ctx->width * jpeg_ctx->height * 3 / 2);

      memcpy(out_buf, jpeg_ctx->tmp_frame, jpeg_ctx->pic_size);
      return jpeg_ctx->pic_size;

    } else {
      fprintf(stderr, "JPEG_DECODER: output pixel format not supported: %i\n",
              codec_data->context->pix_fmt);
    }
  }

  return 0;
}

/*
 * close (m)jpeg decoder context
 * args:
 *    none
 *
 * asserts:
 *    none
 *
 * returns: none
 */
void jpeg_close_decoder() {
  if (jpeg_ctx == NULL)
    return;

  codec_data_t *codec_data = (codec_data_t *)jpeg_ctx->codec_data;

#if LIBAVCODEC_VER_AT_LEAST(61, 3)
  avcodec_free_context(&codec_data->context);
#else
  avcodec_close(codec_data->context);
  free(codec_data->context);
#endif

#if LIBAVCODEC_VER_AT_LEAST(55, 28)
  av_frame_free(&codec_data->picture);
#else
#if LIBAVCODEC_VER_AT_LEAST(54, 28)
  avcodec_free_frame(&codec_data->picture);
#else
  av_freep(&codec_data->picture);
#endif
#endif

  if (jpeg_ctx->tmp_frame)
    free(jpeg_ctx->tmp_frame);

  free(codec_data);
  free(jpeg_ctx);

  jpeg_ctx = NULL;
}

#endif