-
Notifications
You must be signed in to change notification settings - Fork 88
/
raspiraw.c
2447 lines (2189 loc) · 62.5 KB
/
raspiraw.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
/*
Copyright (c) 2015, Raspberry Pi Foundation
Copyright (c) 2015, Dave Stevenson
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define VERSION_STRING "0.0.3"
#define _GNU_SOURCE
#include <ctype.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#define I2C_SLAVE_FORCE 0x0706
#include "bcm_host.h"
#include "interface/vcos/vcos.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_buffer.h"
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/util/mmal_connection.h"
#include "interface/mmal/util/mmal_default_components.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "interface/vcsm/user-vcsm.h"
#include "RaspiCLI.h"
#include <sys/ioctl.h>
#include "raw_header.h"
#define DEFAULT_I2C_DEVICE 10
#define ALT_I2C_DEVICE 0
#define I2C_DEVICE_NAME_LEN 13 // "/dev/i2c-XXX"+NULL
struct brcm_raw_header *brcm_header = NULL;
enum bayer_order
{
// Carefully ordered so that an hflip is ^1,
// and a vflip is ^2.
BAYER_ORDER_BGGR,
BAYER_ORDER_GBRG,
BAYER_ORDER_GRBG,
BAYER_ORDER_RGGB
};
struct sensor_regs
{
uint16_t reg;
uint16_t data;
};
struct mode_def
{
struct sensor_regs *regs;
int num_regs;
int width;
int height;
MMAL_FOURCC_T encoding;
enum bayer_order order;
int native_bit_depth;
uint8_t image_id;
uint8_t data_lanes;
int min_vts;
int line_time_ns;
uint32_t timing[5];
uint32_t term[2];
int black_level;
int binning; /* Binning or skipping factor */
};
struct raspiraw_crop
{
int hinc;
int vinc;
int width;
int height;
int left;
int top;
};
struct sensor_def
{
char *name;
struct sensor_regs *common_init;
int num_common_init;
struct mode_def *modes;
int num_modes;
struct sensor_regs *stop;
int num_stop_regs;
uint8_t i2c_addr; // Device I2C slave address
int i2c_addressing; // Length of register address values
int i2c_data_size; // Length of register data to write
// Detecting the device
int i2c_ident_length; // Length of I2C ID register
uint16_t i2c_ident_reg; // ID register address
uint16_t i2c_ident_value; // ID register value
// Flip configuration
uint16_t vflip_reg; // Register for VFlip
int vflip_reg_bit; // Bit in that register for VFlip
uint16_t hflip_reg; // Register for HFlip
int hflip_reg_bit; // Bit in that register for HFlip
int flips_dont_change_bayer_order; // Some sensors do not change the
// Bayer order by adjusting X/Y
// starts to compensate.
uint16_t exposure_reg;
int exposure_reg_num_bits;
uint16_t vts_reg;
int vts_reg_num_bits;
uint16_t gain_reg;
int gain_reg_num_bits;
int (*set_crop)(const struct sensor_def *, struct mode_def *, const struct raspiraw_crop *cfg);
};
// The process first loads the cleaned up dump of the registers
// than updates the known registers to the proper values
// based on: http://www.seeedstudio.com/wiki/images/3/3c/Ov5647_full.pdf
enum operation
{
EQUAL, // Set bit to value
SET, // Set bit
CLEAR, // Clear bit
XOR // Xor bit
};
void modReg(struct mode_def *mode, uint16_t reg, int startBit, int endBit, int value, enum operation op);
#define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
#include "adv7282m_modes.h"
#include "imx219_modes.h"
#include "imx477_modes.h"
#include "ov5647_modes.h"
const struct sensor_def *sensors[] = { &ov5647, &imx219, &adv7282, &imx477, NULL };
enum
{
CommandHelp,
CommandMode,
CommandHFlip,
CommandVFlip,
CommandExposure,
CommandGain,
CommandOutput,
CommandWriteHeader,
CommandTimeout,
CommandSaveRate,
CommandBitDepth,
CommandCameraNum,
CommandExposureus,
CommandI2cBus,
CommandAwbGains,
CommandRegs,
CommandHinc,
CommandVinc,
CommandFps,
CommandWidth,
CommandHeight,
CommandLeft,
CommandTop,
CommandVts,
CommandLine,
CommandWriteHeader0,
CommandWriteHeaderG,
CommandWriteTimestamps,
CommandWriteEmpty,
CommandDecodeMetadata,
CommandAwb,
CommandNoPreview,
CommandProcessing,
CommandPreview,
CommandFullScreen,
CommandOpacity,
CommandProcessingYUV,
CommandOutputYUV,
};
static COMMAND_LIST cmdline_commands[] = {
// clang-format off
{ CommandHelp, "-help", "?", "This help information", 0 },
{ CommandMode, "-mode", "md", "Set sensor mode <mode>", 1 },
{ CommandHFlip, "-hflip", "hf", "Set horizontal flip", 0 },
{ CommandVFlip, "-vflip", "vf", "Set vertical flip", 0 },
{ CommandExposure, "-ss", "e", "Set the sensor exposure time (not calibrated units)", 0 },
{ CommandGain, "-gain", "g", "Set the sensor gain code (not calibrated units)", 0 },
{ CommandOutput, "-output", "o", "Set the output filename", 0 },
{ CommandWriteHeader, "-header", "hd", "Write the BRCM header to the output file", 0 },
{ CommandTimeout, "-timeout", "t", "Time (in ms) before shutting down (if not specified, set to 5s)", 1 },
{ CommandSaveRate, "-saverate", "sr", "Save every Nth frame", 1 },
{ CommandBitDepth, "-bitdepth", "b", "Set output raw bit depth (8, 10, 12 or 16, if not specified, set to sensor native)", 1 },
{ CommandCameraNum, "-cameranum", "c", "Set camera number to use (0=CAM0, 1=CAM1).", 1 },
{ CommandExposureus, "-expus", "eus", "Set the sensor exposure time in micro seconds.", -1 },
{ CommandI2cBus, "-i2c", "y", "Set the I2C bus to use.", -1 },
{ CommandAwbGains, "-awbgains", "awbg", "Set the AWB gains to use.", 1 },
{ CommandRegs, "-regs", "r", "Change (current mode) regs", 0 },
{ CommandHinc, "-hinc", "hi", "Set horizontal odd/even inc reg", -1 },
{ CommandVinc, "-vinc", "vi", "Set vertical odd/even inc reg", -1 },
{ CommandFps, "-fps", "f", "Set framerate regs", -1 },
{ CommandWidth, "-width", "w", "Set current mode width", -1 },
{ CommandHeight, "-height", "h", "Set current mode height", -1 },
{ CommandLeft, "-left", "lt", "Set current mode left", -1 },
{ CommandTop, "-top", "tp", "Set current mode top", -1 },
{ CommandWriteHeader0, "-header0", "hd0", "Sets filename to write the BRCM header to", 0 },
{ CommandWriteHeaderG, "-headerg", "hdg", "Sets filename to write the .pgm header to", 0 },
{ CommandWriteTimestamps, "-tstamps", "ts", "Sets filename to write timestamps to", 0 },
{ CommandWriteEmpty, "-empty", "emp", "Write empty output files", 0 },
{ CommandDecodeMetadata, "-metadata", "m", "Decode register metadata", 0 },
{ CommandAwb, "-awb", "awb", "Use a simple grey-world AWB algorithm", 0 },
{ CommandNoPreview, "-nopreview", "n", "Do not send the stream to the display", 0 },
{ CommandProcessing, "-processing", "P", "Pass images into an image processing function", 0 },
{ CommandPreview, "-preview", "p", "Preview window settings <'x,y,w,h'>", 1 },
{ CommandFullScreen, "-fullscreen", "fs", "Fullscreen preview mode", 0 },
{ CommandOpacity, "-opacity", "op", "Preview window opacity (0-255)", 1 },
{ CommandProcessingYUV, "-processing_yuv", "PY", "Pass processed YUV images into an image processing function", 0 },
{ CommandOutputYUV, "-output_yuv", "oY", "Set the output filename for YUV data", 0 },
// clang-format on
};
static int cmdline_commands_size = sizeof(cmdline_commands) / sizeof(cmdline_commands[0]);
typedef struct pts_node
{
int idx;
int64_t pts;
struct pts_node *nxt;
} * PTS_NODE_T;
#define DEFAULT_PREVIEW_LAYER 3
typedef struct raspiraw_params
{
struct raspiraw_crop crop;
int mode;
int hflip;
int vflip;
int exposure;
int gain;
char *output;
int capture;
int write_header;
int timeout;
int saverate;
int bit_depth;
int camera_num;
int exposure_us;
int i2c_bus;
double awb_gains_r;
double awb_gains_b;
char *regs;
double fps;
char *write_header0;
char *write_headerg;
char *write_timestamps;
int write_empty;
PTS_NODE_T ptsa;
PTS_NODE_T ptso;
int decodemetadata;
int awb;
int no_preview;
int processing;
int fullscreen; // 0 is use previewRect, non-zero to use full screen
int opacity; // Opacity of window - 0 = transparent, 255 = opaque
MMAL_RECT_T
preview_window; // Destination rectangle for the preview window.
int capture_yuv;
char *output_yuv;
int processing_yuv;
} RASPIRAW_PARAMS_T;
typedef struct
{
RASPIRAW_PARAMS_T *cfg;
MMAL_POOL_T *rawcam_pool;
MMAL_PORT_T *rawcam_output;
MMAL_POOL_T *isp_ip_pool;
MMAL_PORT_T *isp_ip;
MMAL_QUEUE_T *awb_queue;
int awb_thread_quit;
MMAL_PARAMETER_AWB_GAINS_T wb_gains;
MMAL_QUEUE_T *processing_queue;
int processing_thread_quit;
} RASPIRAW_CALLBACK_T;
typedef struct
{
RASPIRAW_PARAMS_T *cfg;
MMAL_POOL_T *isp_op_pool;
MMAL_PORT_T *isp_output;
MMAL_POOL_T *vr_ip_pool;
MMAL_PORT_T *vr_ip;
MMAL_QUEUE_T *processing_yuv_queue;
int processing_yuv_thread_quit;
} RASPIRAW_ISP_CALLBACK_T;
void update_regs(const struct sensor_def *sensor, struct mode_def *mode, int hflip, int vflip, int exposure, int gain);
static int i2c_rd(int fd, uint8_t i2c_addr, uint16_t reg, uint8_t *values, uint32_t n, const struct sensor_def *sensor)
{
int err;
uint8_t buf[2] = { reg >> 8, reg & 0xff };
struct i2c_rdwr_ioctl_data msgset;
struct i2c_msg msgs[2] = {
{
.addr = i2c_addr,
.flags = 0,
.len = 2,
.buf = buf,
},
{
.addr = i2c_addr,
.flags = I2C_M_RD,
.len = n,
.buf = values,
},
};
if (sensor->i2c_addressing == 1)
{
msgs[0].len = 1;
}
msgset.msgs = msgs;
msgset.nmsgs = 2;
err = ioctl(fd, I2C_RDWR, &msgset);
// vcos_log_error("Read i2c addr %02X, reg %04X (len %d), value %02X,
// err %d", i2c_addr, msgs[0].buf[0], msgs[0].len, values[0], err);
if (err != (int)msgset.nmsgs)
return -1;
return 0;
}
const struct sensor_def *probe_sensor(int fd)
{
const struct sensor_def **sensor_list = &sensors[0];
const struct sensor_def *sensor = NULL;
while (*sensor_list != NULL)
{
uint16_t reg = 0;
sensor = *sensor_list;
vcos_log_error("Probing sensor %s on addr %02X", sensor->name, sensor->i2c_addr);
if (sensor->i2c_ident_length <= 2)
{
if (!i2c_rd(fd, sensor->i2c_addr, sensor->i2c_ident_reg, (uint8_t *)®,
sensor->i2c_ident_length, sensor))
{
if (reg == sensor->i2c_ident_value)
{
vcos_log_error("Found sensor %s at address %02X", sensor->name,
sensor->i2c_addr);
break;
}
}
}
sensor_list++;
sensor = NULL;
}
return sensor;
}
void send_regs(int fd, const struct sensor_def *sensor, const struct sensor_regs *regs, int num_regs)
{
int i;
for (i = 0; i < num_regs; i++)
{
if (regs[i].reg == 0xFFFF)
{
if (ioctl(fd, I2C_SLAVE_FORCE, regs[i].data) < 0)
{
vcos_log_error("Failed to set I2C address to %02X", regs[i].data);
}
}
else if (regs[i].reg == 0xFFFE)
{
vcos_sleep(regs[i].data);
}
else
{
if (sensor->i2c_addressing == 1)
{
unsigned char msg[3] = { regs[i].reg, regs[i].data & 0xFF };
int len = 2;
if (sensor->i2c_data_size == 2)
{
msg[1] = (regs[i].data >> 8) & 0xFF;
msg[2] = regs[i].data & 0xFF;
len = 3;
}
if (write(fd, msg, len) != len)
{
vcos_log_error("Failed to write register index %d "
"(%02X val %02X)",
i, regs[i].reg, regs[i].data);
}
}
else
{
unsigned char msg[4] = { regs[i].reg >> 8, regs[i].reg, regs[i].data };
int len = 3;
if (sensor->i2c_data_size == 2)
{
msg[2] = regs[i].data >> 8;
msg[3] = regs[i].data;
len = 4;
}
if (write(fd, msg, len) != len)
{
vcos_log_error("Failed to write register index %d", i);
}
}
}
}
}
void start_camera_streaming(const struct sensor_def *sensor, struct mode_def *mode, int fd)
{
if (ioctl(fd, I2C_SLAVE_FORCE, sensor->i2c_addr) < 0)
{
vcos_log_error("Failed to set I2C address");
return;
}
if (sensor->common_init)
send_regs(fd, sensor, sensor->common_init, sensor->num_common_init);
send_regs(fd, sensor, mode->regs, mode->num_regs);
vcos_log_error("Now streaming...");
}
void stop_camera_streaming(const struct sensor_def *sensor, int fd)
{
if (ioctl(fd, I2C_SLAVE_FORCE, sensor->i2c_addr) < 0)
{
vcos_log_error("Failed to set I2C address");
return;
}
send_regs(fd, sensor, sensor->stop, sensor->num_stop_regs);
}
/**
* Allocates and generates a filename based on the
* user-supplied pattern and the frame number.
* On successful return, finalName and tempName point to malloc()ed strings
* which must be freed externally. (On failure, returns nulls that
* don't need free()ing.)
*
* @param finalName pointer receives an
* @param pattern sprintf pattern with %d to be replaced by frame
* @param frame for timelapse, the frame number
* @return Returns a MMAL_STATUS_T giving result of operation
*/
MMAL_STATUS_T create_filenames(char **finalName, char *pattern, int frame)
{
*finalName = NULL;
if (0 > asprintf(finalName, pattern, frame))
{
return MMAL_ENOMEM; // It may be some other error, but it is not
// worth getting it right
}
return MMAL_SUCCESS;
}
void decodemetadataline(uint8_t *data, int bpp)
{
int c = 1;
uint8_t tag, dta;
uint16_t reg = -1;
if (data[0] == 0x0a)
{
while (data[c] != 0x07)
{
tag = data[c++];
if (bpp == 10 && (c % 5) == 4)
c++;
if (bpp == 12 && (c % 3) == 2)
c++;
dta = data[c++];
if (tag == 0xaa)
reg = (reg & 0x00ff) | (dta << 8);
else if (tag == 0xa5)
reg = (reg & 0xff00) | dta;
else if (tag == 0x5a)
vcos_log_error("Register 0x%04x = 0x%02x", reg++, dta);
else if (tag == 0x55)
vcos_log_error("Skip 0x%04x", reg++);
else
vcos_log_error("Metadata decode failed %x %x %x", reg, tag, dta);
}
}
else
vcos_log_error("Doesn't looks like register set %x!=0x0a", data[0]);
}
int encoding_to_bpp(uint32_t encoding)
{
switch (encoding)
{
case MMAL_ENCODING_BAYER_SBGGR10P:
case MMAL_ENCODING_BAYER_SGBRG10P:
case MMAL_ENCODING_BAYER_SGRBG10P:
case MMAL_ENCODING_BAYER_SRGGB10P:
return 10;
case MMAL_ENCODING_BAYER_SBGGR12P:
case MMAL_ENCODING_BAYER_SGBRG12P:
case MMAL_ENCODING_BAYER_SGRBG12P:
case MMAL_ENCODING_BAYER_SRGGB12P:
return 12;
default:
return 8;
};
}
static void buffers_to_rawcam(RASPIRAW_CALLBACK_T *dev)
{
MMAL_BUFFER_HEADER_T *buffer;
while ((buffer = mmal_queue_get(dev->rawcam_pool->queue)) != NULL)
{
mmal_port_send_buffer(dev->rawcam_output, buffer);
// vcos_log_error("Buffer %p to rawcam\n", buffer);
}
}
static void callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
static int count = 0;
RASPIRAW_CALLBACK_T *dev = (RASPIRAW_CALLBACK_T *)port->userdata;
RASPIRAW_PARAMS_T *cfg = (RASPIRAW_PARAMS_T *)dev->cfg;
MMAL_STATUS_T status;
// vcos_log_error("Buffer %p returned, filled %d, timestamp %llu, flags %04X", buffer, buffer->length,
// buffer->pts, buffer->flags);
if (cfg->capture)
{
if (!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO) && (((count++) % cfg->saverate) == 0))
{
// Save every Nth frame
// SD card access is too slow to do much more.
FILE *file;
char *filename = NULL;
if (create_filenames(&filename, cfg->output, count) == MMAL_SUCCESS)
{
file = fopen(filename, "wb");
if (file)
{
if (cfg->ptso) // make sure previous
// malloc() was
// successful
{
cfg->ptso->idx = count;
cfg->ptso->pts = buffer->pts;
cfg->ptso->nxt = malloc(sizeof(*cfg->ptso->nxt));
cfg->ptso = cfg->ptso->nxt;
}
if (!cfg->write_empty)
{
if (cfg->write_header)
fwrite(brcm_header, BRCM_RAW_HEADER_LENGTH, 1, file);
fwrite(buffer->user_data, buffer->length, 1, file);
}
fclose(file);
}
free(filename);
}
}
}
if (cfg->decodemetadata && (buffer->flags & MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO))
{
int bpp = encoding_to_bpp(port->format->encoding);
int pitch = mmal_encoding_width_to_stride(port->format->encoding, port->format->es->video.width);
vcos_log_error("First metadata line");
decodemetadataline(buffer->user_data, bpp);
vcos_log_error("Second metadata line");
decodemetadataline(buffer->user_data + pitch, bpp);
}
/* Pass the buffers off to any other MMAL sinks. */
if (buffer->length && !(buffer->flags & MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO))
{
if (dev->isp_ip)
{
MMAL_BUFFER_HEADER_T *out = mmal_queue_get(dev->isp_ip_pool->queue);
if (out)
{
// vcos_log_error("replicate buffer %p for isp", buffer);
mmal_buffer_header_replicate(out, buffer);
out->data = buffer->data;
out->alloc_size = buffer->alloc_size;
status = mmal_port_send_buffer(dev->isp_ip, out);
if (status != MMAL_SUCCESS)
vcos_log_error("Failed to send buffer "
"%p to isp - %d",
buffer, status);
}
}
/* Pass to the AWB thread */
if (dev->awb_queue)
{
/* Relying on the AWB thread to release this
* buffer`refcount */
mmal_buffer_header_acquire(buffer);
mmal_queue_put(dev->awb_queue, buffer);
// vcos_log_error("send buffer %p to awb", buffer);
}
/* Pass to the processing thread */
if (dev->processing_queue)
{
/* Relying on the processing thread to release this
* buffer`refcount */
mmal_buffer_header_acquire(buffer);
mmal_queue_put(dev->processing_queue, buffer);
// vcos_log_error("send buffer %p to awb", buffer);
}
}
mmal_buffer_header_release(buffer);
buffers_to_rawcam(dev);
}
static void buffers_to_isp_op(RASPIRAW_ISP_CALLBACK_T *dev)
{
MMAL_BUFFER_HEADER_T *buffer;
while ((buffer = mmal_queue_get(dev->isp_op_pool->queue)) != NULL)
{
mmal_port_send_buffer(dev->isp_output, buffer);
// vcos_log_error("Buffer %p to isp op\n", buffer);
}
}
static void yuv_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
{
static int count = 0;
RASPIRAW_ISP_CALLBACK_T *yuv_cb = (RASPIRAW_ISP_CALLBACK_T *)port->userdata;
RASPIRAW_PARAMS_T *cfg = (RASPIRAW_PARAMS_T *)yuv_cb->cfg;
MMAL_STATUS_T status;
// vcos_log_error("Buffer %p returned, filled %d, timestamp %llu, flags
// %04X", buffer, buffer->length, buffer->pts, buffer->flags);
if (cfg->capture_yuv)
{
if (!(buffer->flags & MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO) && (((count++) % cfg->saverate) == 0))
{
// Save every Nth frame
// SD card access is too slow to do much more.
FILE *file;
char *filename = NULL;
if (create_filenames(&filename, cfg->output_yuv, count) == MMAL_SUCCESS)
{
file = fopen(filename, "wb");
if (file)
{
if (!cfg->write_empty)
{
fwrite(buffer->user_data, buffer->length, 1, file);
}
fclose(file);
}
free(filename);
}
}
}
/* Pass the buffers off to any other MMAL sinks. */
if (buffer->length && !(buffer->flags & MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO))
{
if (yuv_cb->vr_ip)
{
MMAL_BUFFER_HEADER_T *out = mmal_queue_get(yuv_cb->vr_ip_pool->queue);
if (out)
{
// vcos_log_error("replicate buffer %p for vr",
// buffer);
mmal_buffer_header_replicate(out, buffer);
out->data = buffer->data;
out->alloc_size = buffer->alloc_size;
status = mmal_port_send_buffer(yuv_cb->vr_ip, out);
if (status != MMAL_SUCCESS)
vcos_log_error("Failed to send buffer %p to video "
"render - %d",
buffer, status);
}
}
/* Pass to the processing thread */
if (yuv_cb->processing_yuv_queue)
{
/* Relying on the processing thread to release this
* buffer's refcount */
mmal_buffer_header_acquire(buffer);
mmal_queue_put(yuv_cb->processing_yuv_queue, buffer);
// vcos_log_error("send buffer %p to yuv processing",
// buffer);
}
}
mmal_buffer_header_release(buffer);
buffers_to_isp_op(yuv_cb);
}
uint32_t order_and_bit_depth_to_encoding(enum bayer_order order, int bit_depth)
{
// BAYER_ORDER_BGGR,
// BAYER_ORDER_GBRG,
// BAYER_ORDER_GRBG,
// BAYER_ORDER_RGGB
const uint32_t depth8[] = { MMAL_ENCODING_BAYER_SBGGR8, MMAL_ENCODING_BAYER_SGBRG8, MMAL_ENCODING_BAYER_SGRBG8,
MMAL_ENCODING_BAYER_SRGGB8 };
const uint32_t depth10[] = { MMAL_ENCODING_BAYER_SBGGR10P, MMAL_ENCODING_BAYER_SGBRG10P,
MMAL_ENCODING_BAYER_SGRBG10P, MMAL_ENCODING_BAYER_SRGGB10P };
const uint32_t depth12[] = {
MMAL_ENCODING_BAYER_SBGGR12P,
MMAL_ENCODING_BAYER_SGBRG12P,
MMAL_ENCODING_BAYER_SGRBG12P,
MMAL_ENCODING_BAYER_SRGGB12P,
};
const uint32_t depth16[] = {
MMAL_ENCODING_BAYER_SBGGR16,
MMAL_ENCODING_BAYER_SGBRG16,
MMAL_ENCODING_BAYER_SGRBG16,
MMAL_ENCODING_BAYER_SRGGB16,
};
if (order < 0 || order > 3)
{
vcos_log_error("order out of range - %d", order);
return 0;
}
switch (bit_depth)
{
case 8:
return depth8[order];
case 10:
return depth10[order];
case 12:
return depth12[order];
case 16:
return depth16[order];
}
vcos_log_error("%d not one of the handled bit depths", bit_depth);
return 0;
}
/**
* Parse the incoming command line and put resulting parameters in to the state
*
* @param argc Number of arguments in command line
* @param argv Array of pointers to strings from command line
* @param state Pointer to state structure to assign any discovered parameters
* to
* @return non-0 if failed for some reason, 0 otherwise
*/
static int parse_cmdline(int argc, char **argv, RASPIRAW_PARAMS_T *cfg)
{
// Parse the command line arguments.
// We are looking for --<something> or -<abbreviation of something>
int valid = 1;
int i;
for (i = 1; i < argc && valid; i++)
{
int command_id, num_parameters, len;
if (!argv[i])
continue;
if (argv[i][0] != '-')
{
valid = 0;
continue;
}
// Assume parameter is valid until proven otherwise
valid = 1;
command_id =
raspicli_get_command_id(cmdline_commands, cmdline_commands_size, &argv[i][1], &num_parameters);
// If we found a command but are missing a parameter, continue
// (and we will drop out of the loop)
if (command_id != -1 && num_parameters > 0 && (i + 1 >= argc))
continue;
// We are now dealing with a command line option
switch (command_id)
{
case CommandHelp:
raspicli_display_help(cmdline_commands, cmdline_commands_size);
// exit straight away if help requested
return -1;
case CommandMode:
if (sscanf(argv[i + 1], "%d", &cfg->mode) != 1)
valid = 0;
else
i++;
break;
case CommandHFlip:
cfg->hflip = 1;
break;
case CommandVFlip:
cfg->vflip = 1;
break;
case CommandExposure:
if (sscanf(argv[i + 1], "%d", &cfg->exposure) != 1)
valid = 0;
else
i++;
break;
case CommandGain:
if (sscanf(argv[i + 1], "%d", &cfg->gain) != 1)
valid = 0;
else
i++;
break;
case CommandOutput: // output filename
{
len = strlen(argv[i + 1]);
if (len)
{
// We use sprintf to append the frame number for
// timelapse mode Ensure that any %<char> is
// either %% or %d.
const char *percent = argv[i + 1];
while (valid && *percent && (percent = strchr(percent, '%')) != NULL)
{
int digits = 0;
percent++;
while (isdigit(*percent))
{
percent++;
digits++;
}
if (!((*percent == '%' && !digits) || *percent == 'd'))
{
valid = 0;
fprintf(stderr, "Filename contains %% "
"characters, but not "
"%%d or %%%% - sorry, "
"will fail\n");
}
percent++;
}
cfg->output = malloc(len + 10); // leave enough space for any timelapse
// generated changes to filename
if (cfg->output)
{
strncpy(cfg->output, argv[i + 1], len + 1);
i++;
cfg->capture = 1;
}
else
{
fprintf(stderr, "internal error - "
"allocation fail\n");
valid = 0;
}
}
else
{
valid = 0;
}
break;
}
case CommandWriteHeader:
cfg->write_header = 1;
break;
case CommandTimeout: // Time to run for in milliseconds
if (sscanf(argv[i + 1], "%u", &cfg->timeout) == 1)
{
i++;
}
else
valid = 0;
break;
case CommandSaveRate:
if (sscanf(argv[i + 1], "%u", &cfg->saverate) == 1)
{
i++;
}
else
valid = 0;
break;
case CommandBitDepth:
if (sscanf(argv[i + 1], "%u", &cfg->bit_depth) == 1)
{
i++;
}
else
valid = 0;
break;
case CommandCameraNum:
if (sscanf(argv[i + 1], "%u", &cfg->camera_num) == 1)
{
i++;
if (cfg->camera_num != 0 && cfg->camera_num != 1)
{
fprintf(stderr,
"Invalid camera number "
"specified (%d)."
" It should be 0 or 1.\n",
cfg->camera_num);
valid = 0;
}
}
else
valid = 0;
break;
case CommandExposureus:
if (sscanf(argv[i + 1], "%d", &cfg->exposure_us) != 1)
valid = 0;
else
i++;
break;
case CommandI2cBus:
if (sscanf(argv[i + 1], "%d", &cfg->i2c_bus) != 1)
valid = 0;
else
i++;
break;
case CommandAwbGains:
{
double r, b;
int args;
args = sscanf(argv[i + 1], "%lf,%lf", &r, &b);