forked from gagle/raspberrypi-openmax-jpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpeg.c
1097 lines (968 loc) · 36.6 KB
/
jpeg.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
/*
For the sake of simplicity, this example exits on error.
Very quick OpenMAX IL explanation:
- There are components. Each component performs an action. For example, the
OMX.broadcom.camera module captures images and videos and the
OMX.broadcom.image_encoder module encodes raw data from an image into multiple
formats. Each component has input and output ports and receives and sends
buffers with data. The main goal is to join these components to form a
pipeline and do complex tasks.
- There are two ways to connect components: with tunnels or manually. The
non-tunneled ports need to manually allocate the buffers with
OMX_AllocateBuffer() and free them with OMX_FreeBuffer().
- The components have states.
- There are at least two threads: the thread that uses the application (CPU) and
the thread that is used internally by OMX to execute the components (GPU).
- There are two types of functions: blocking and non-blocking. The blocking
functions are synchronous and the non-blocking are asynchronous. Being
asynchronous means that the function returns immediately but the result is
returned in a later time, so you need to wait until you receive an event. This
example uses two non-blocking functions: OMX_SendCommand and
OMX_FillThisBuffer.
Note: The camera component has two video ports: "preview" and "video". The
"preview" port must be enabled even if you're not using it (tunnel it to the
null_sink component) because it is used to run AGC (automatic gain control) and
AWB (auto white balance) algorithms.
*/
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <bcm_host.h>
#include <interface/vcos/vcos.h>
#include <IL/OMX_Broadcom.h>
#include "dump.h"
#define OMX_INIT_STRUCTURE(a) \
memset (&(a), 0, sizeof (a)); \
(a).nSize = sizeof (a); \
(a).nVersion.nVersion = OMX_VERSION; \
(a).nVersion.s.nVersionMajor = OMX_VERSION_MAJOR; \
(a).nVersion.s.nVersionMinor = OMX_VERSION_MINOR; \
(a).nVersion.s.nRevision = OMX_VERSION_REVISION; \
(a).nVersion.s.nStep = OMX_VERSION_STEP
#define FILENAME "still.jpg"
#define JPEG_QUALITY 75 //1 .. 100
#define JPEG_EXIF_DISABLE OMX_FALSE
#define JPEG_IJG_ENABLE OMX_FALSE
#define JPEG_THUMBNAIL_ENABLE OMX_TRUE
#define JPEG_THUMBNAIL_WIDTH 64 //0 .. 1024
#define JPEG_THUMBNAIL_HEIGHT 48 //0 .. 1024
#define JPEG_PREVIEW OMX_FALSE
#define RAW_BAYER OMX_FALSE
//Some settings doesn't work well
#define CAM_WIDTH 2592
#define CAM_HEIGHT 1944
#define CAM_SHARPNESS 0 //-100 .. 100
#define CAM_CONTRAST 0 //-100 .. 100
#define CAM_BRIGHTNESS 50 //0 .. 100
#define CAM_SATURATION 0 //-100 .. 100
#define CAM_SHUTTER_SPEED_AUTO OMX_TRUE
//In microseconds, (1/8)*1e6
#define CAM_SHUTTER_SPEED 125000 //1 ..
#define CAM_ISO_AUTO OMX_TRUE
#define CAM_ISO 100 //100 .. 800
#define CAM_EXPOSURE OMX_ExposureControlAuto
#define CAM_EXPOSURE_COMPENSATION 0 //-24 .. 24
#define CAM_MIRROR OMX_MirrorNone
#define CAM_ROTATION 0 //0 90 180 270
#define CAM_COLOR_ENABLE OMX_FALSE
#define CAM_COLOR_U 128 //0 .. 255
#define CAM_COLOR_V 128 //0 .. 255
#define CAM_NOISE_REDUCTION OMX_TRUE
#define CAM_FRAME_STABILIZATION OMX_FALSE
#define CAM_METERING OMX_MeteringModeAverage
#define CAM_WHITE_BALANCE OMX_WhiteBalControlAuto
//The gains are used if the white balance is set to off
#define CAM_WHITE_BALANCE_RED_GAIN 1000 //0 ..
#define CAM_WHITE_BALANCE_BLUE_GAIN 1000 //0 ..
#define CAM_IMAGE_FILTER OMX_ImageFilterNone
#define CAM_ROI_TOP 0 //0 .. 100
#define CAM_ROI_LEFT 0 //0 .. 100
#define CAM_ROI_WIDTH 100 //0 .. 100
#define CAM_ROI_HEIGHT 100 //0 .. 100
#define CAM_DRC OMX_DynRangeExpOff
/*
Possible values:
CAM_EXPOSURE
OMX_ExposureControlOff
OMX_ExposureControlAuto
OMX_ExposureControlNight
OMX_ExposureControlBackLight
OMX_ExposureControlSpotlight
OMX_ExposureControlSports
OMX_ExposureControlSnow
OMX_ExposureControlBeach
OMX_ExposureControlLargeAperture
OMX_ExposureControlSmallAperture
OMX_ExposureControlVeryLong
OMX_ExposureControlFixedFps
OMX_ExposureControlNightWithPreview
OMX_ExposureControlAntishake
OMX_ExposureControlFireworks
CAM_IMAGE_FILTER
OMX_ImageFilterNone
OMX_ImageFilterEmboss
OMX_ImageFilterNegative
OMX_ImageFilterSketch
OMX_ImageFilterOilPaint
OMX_ImageFilterHatch
OMX_ImageFilterGpen
OMX_ImageFilterSolarize
OMX_ImageFilterWatercolor
OMX_ImageFilterPastel
OMX_ImageFilterFilm
OMX_ImageFilterBlur
OMX_ImageFilterColourSwap
OMX_ImageFilterWashedOut
OMX_ImageFilterColourPoint
OMX_ImageFilterPosterise
OMX_ImageFilterColourBalance
OMX_ImageFilterCartoon
CAM_METERING
OMX_MeteringModeAverage
OMX_MeteringModeSpot
OMX_MeteringModeMatrix
CAM_MIRROR
OMX_MirrorNone
OMX_MirrorHorizontal
OMX_MirrorVertical
OMX_MirrorBoth
CAM_WHITE_BALANCE
OMX_WhiteBalControlOff
OMX_WhiteBalControlAuto
OMX_WhiteBalControlSunLight
OMX_WhiteBalControlCloudy
OMX_WhiteBalControlShade
OMX_WhiteBalControlTungsten
OMX_WhiteBalControlFluorescent
OMX_WhiteBalControlIncandescent
OMX_WhiteBalControlFlash
OMX_WhiteBalControlHorizon
CAM_DRC
OMX_DynRangeExpOff
OMX_DynRangeExpLow
OMX_DynRangeExpMedium
OMX_DynRangeExpHigh
*/
//Data of each component
typedef struct {
//The handle is obtained with OMX_GetHandle() and is used on every function
//that needs to manipulate a component. It is released with OMX_FreeHandle()
OMX_HANDLETYPE handle;
//Bitwise OR of flags. Used for blocking the current thread and waiting an
//event. Used with vcos_event_flags_get() and vcos_event_flags_set()
VCOS_EVENT_FLAGS_T flags;
//The fullname of the component
OMX_STRING name;
} component_t;
//Events used with vcos_event_flags_get() and vcos_event_flags_set()
typedef enum {
EVENT_ERROR = 0x1,
EVENT_PORT_ENABLE = 0x2,
EVENT_PORT_DISABLE = 0x4,
EVENT_STATE_SET = 0x8,
EVENT_FLUSH = 0x10,
EVENT_MARK_BUFFER = 0x20,
EVENT_MARK = 0x40,
EVENT_PORT_SETTINGS_CHANGED = 0x80,
EVENT_PARAM_OR_CONFIG_CHANGED = 0x100,
EVENT_BUFFER_FLAG = 0x200,
EVENT_RESOURCES_ACQUIRED = 0x400,
EVENT_DYNAMIC_RESOURCES_AVAILABLE = 0x800,
EVENT_FILL_BUFFER_DONE = 0x1000,
EVENT_EMPTY_BUFFER_DONE = 0x2000,
} component_event;
//Prototypes
OMX_ERRORTYPE EventHandler (
OMX_IN OMX_HANDLETYPE hComponent,
OMX_IN OMX_PTR pAppData,
OMX_IN OMX_EVENTTYPE eEvent,
OMX_IN OMX_U32 nData1,
OMX_IN OMX_U32 nData2,
OMX_IN OMX_PTR pEventData);
OMX_ERRORTYPE FillBufferDone (
OMX_IN OMX_HANDLETYPE hComponent,
OMX_IN OMX_PTR pAppData,
OMX_IN OMX_BUFFERHEADERTYPE* pBuffer);
void wake (component_t* component, VCOS_UNSIGNED event);
void wait (
component_t* component,
VCOS_UNSIGNED events,
VCOS_UNSIGNED* retrieves_events);
void init_component (component_t* component);
void deinit_component (component_t* component);
void load_camera_drivers (component_t* component);
void change_state (component_t* component, OMX_STATETYPE state);
void enable_port (component_t* component, OMX_U32 port);
void disable_port (component_t* component, OMX_U32 port);
void enable_encoder_output_port (
component_t* encoder,
OMX_BUFFERHEADERTYPE** encoder_output_buffer);
void disable_encoder_output_port (
component_t* encoder,
OMX_BUFFERHEADERTYPE* encoder_output_buffer);
void set_camera_settings (component_t* camera);
void set_jpeg_settings (component_t* encoder);
//Function that is called when a component receives an event from a secondary
//thread
OMX_ERRORTYPE event_handler (
OMX_IN OMX_HANDLETYPE comp,
OMX_IN OMX_PTR app_data,
OMX_IN OMX_EVENTTYPE event,
OMX_IN OMX_U32 data1,
OMX_IN OMX_U32 data2,
OMX_IN OMX_PTR event_data){
component_t* component = (component_t*)app_data;
switch (event){
case OMX_EventCmdComplete:
switch (data1){
case OMX_CommandStateSet:
printf ("event: %s, OMX_CommandStateSet, state: %s\n",
component->name, dump_OMX_STATETYPE (data2));
wake (component, EVENT_STATE_SET);
break;
case OMX_CommandPortDisable:
printf ("event: %s, OMX_CommandPortDisable, port: %d\n",
component->name, data2);
wake (component, EVENT_PORT_DISABLE);
break;
case OMX_CommandPortEnable:
printf ("event: %s, OMX_CommandPortEnable, port: %d\n",
component->name, data2);
wake (component, EVENT_PORT_ENABLE);
break;
case OMX_CommandFlush:
printf ("event: %s, OMX_CommandFlush, port: %d\n",
component->name, data2);
wake (component, EVENT_FLUSH);
break;
case OMX_CommandMarkBuffer:
printf ("event: %s, OMX_CommandMarkBuffer, port: %d\n",
component->name, data2);
wake (component, EVENT_MARK_BUFFER);
break;
}
break;
case OMX_EventError:
printf ("event: %s, %s\n", component->name, dump_OMX_ERRORTYPE (data1));
wake (component, EVENT_ERROR);
break;
case OMX_EventMark:
printf ("event: %s, OMX_EventMark\n", component->name);
wake (component, EVENT_MARK);
break;
case OMX_EventPortSettingsChanged:
printf ("event: %s, OMX_EventPortSettingsChanged, port: %d\n",
component->name, data1);
wake (component, EVENT_PORT_SETTINGS_CHANGED);
break;
case OMX_EventParamOrConfigChanged:
printf ("event: %s, OMX_EventParamOrConfigChanged, data1: %d, data2: "
"%X\n", component->name, data1, data2);
wake (component, EVENT_PARAM_OR_CONFIG_CHANGED);
break;
case OMX_EventBufferFlag:
printf ("event: %s, OMX_EventBufferFlag, port: %d\n",
component->name, data1);
wake (component, EVENT_BUFFER_FLAG);
break;
case OMX_EventResourcesAcquired:
printf ("event: %s, OMX_EventResourcesAcquired\n", component->name);
wake (component, EVENT_RESOURCES_ACQUIRED);
break;
case OMX_EventDynamicResourcesAvailable:
printf ("event: %s, OMX_EventDynamicResourcesAvailable\n",
component->name);
wake (component, EVENT_DYNAMIC_RESOURCES_AVAILABLE);
break;
default:
//This should never execute, just ignore
printf ("event: unknown (%X)\n", event);
break;
}
return OMX_ErrorNone;
}
//Function that is called when a component fills a buffer with data
OMX_ERRORTYPE fill_buffer_done (
OMX_IN OMX_HANDLETYPE comp,
OMX_IN OMX_PTR app_data,
OMX_IN OMX_BUFFERHEADERTYPE* buffer){
component_t* component = (component_t*)app_data;
printf ("event: %s, fill_buffer_done\n", component->name);
wake (component, EVENT_FILL_BUFFER_DONE);
return OMX_ErrorNone;
}
void wake (component_t* component, VCOS_UNSIGNED event){
vcos_event_flags_set (&component->flags, event, VCOS_OR);
}
void wait (
component_t* component,
VCOS_UNSIGNED events,
VCOS_UNSIGNED* retrieved_events){
VCOS_UNSIGNED set;
if (vcos_event_flags_get (&component->flags, events | EVENT_ERROR,
VCOS_OR_CONSUME, VCOS_SUSPEND, &set)){
fprintf (stderr, "error: vcos_event_flags_get\n");
exit (1);
}
if (set == EVENT_ERROR){
exit (1);
}
if (retrieved_events){
*retrieved_events = set;
}
}
void init_component (component_t* component){
printf ("initializing component '%s'\n", component->name);
OMX_ERRORTYPE error;
//Create the event flags
if (vcos_event_flags_create (&component->flags, "component")){
fprintf (stderr, "error: vcos_event_flags_create\n");
exit (1);
}
//Each component has an event_handler and fill_buffer_done functions
OMX_CALLBACKTYPE callbacks_st;
callbacks_st.EventHandler = event_handler;
callbacks_st.FillBufferDone = fill_buffer_done;
//Get the handle
if ((error = OMX_GetHandle (&component->handle, component->name, component,
&callbacks_st))){
fprintf (stderr, "error: OMX_GetHandle: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Disable all the ports
OMX_INDEXTYPE types[] = {
OMX_IndexParamAudioInit,
OMX_IndexParamVideoInit,
OMX_IndexParamImageInit,
OMX_IndexParamOtherInit
};
OMX_PORT_PARAM_TYPE ports_st;
OMX_INIT_STRUCTURE (ports_st);
int i;
for (i=0; i<4; i++){
if ((error = OMX_GetParameter (component->handle, types[i], &ports_st))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
OMX_U32 port;
for (port=ports_st.nStartPortNumber;
port<ports_st.nStartPortNumber + ports_st.nPorts; port++){
//Disable the port
disable_port (component, port);
//Wait to the event
wait (component, EVENT_PORT_DISABLE, 0);
}
}
}
void deinit_component (component_t* component){
printf ("deinitializing component '%s'\n", component->name);
OMX_ERRORTYPE error;
vcos_event_flags_delete (&component->flags);
if ((error = OMX_FreeHandle (component->handle))){
fprintf (stderr, "error: OMX_FreeHandle: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void load_camera_drivers (component_t* component){
/*
This is a specific behaviour of the Broadcom's Raspberry Pi OpenMAX IL
implementation module because the OMX_SetConfig() and OMX_SetParameter() are
blocking functions but the drivers are loaded asynchronously, that is, an
event is fired to signal the completion. Basically, what you're saying is:
"When the parameter with index OMX_IndexParamCameraDeviceNumber is set, load
the camera drivers and emit an OMX_EventParamOrConfigChanged event"
The red LED of the camera will be turned on after this call.
*/
printf ("loading '%s' drivers\n", component->name);
OMX_ERRORTYPE error;
OMX_CONFIG_REQUESTCALLBACKTYPE cbs_st;
OMX_INIT_STRUCTURE (cbs_st);
cbs_st.nPortIndex = OMX_ALL;
cbs_st.nIndex = OMX_IndexParamCameraDeviceNumber;
cbs_st.bEnable = OMX_TRUE;
if ((error = OMX_SetConfig (component->handle, OMX_IndexConfigRequestCallback,
&cbs_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
OMX_PARAM_U32TYPE dev_st;
OMX_INIT_STRUCTURE (dev_st);
dev_st.nPortIndex = OMX_ALL;
//ID for the camera device
dev_st.nU32 = 0;
if ((error = OMX_SetParameter (component->handle,
OMX_IndexParamCameraDeviceNumber, &dev_st))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
wait (component, EVENT_PARAM_OR_CONFIG_CHANGED, 0);
}
void change_state (component_t* component, OMX_STATETYPE state){
printf ("changing '%s' state to %s\n", component->name,
dump_OMX_STATETYPE (state));
OMX_ERRORTYPE error;
if ((error = OMX_SendCommand (component->handle, OMX_CommandStateSet, state,
0))){
fprintf (stderr, "error: OMX_SendCommand: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void enable_port (component_t* component, OMX_U32 port){
printf ("enabling port %d ('%s')\n", port, component->name);
OMX_ERRORTYPE error;
if ((error = OMX_SendCommand (component->handle, OMX_CommandPortEnable,
port, 0))){
fprintf (stderr, "error: OMX_SendCommand: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void disable_port (component_t* component, OMX_U32 port){
printf ("disabling port %d ('%s')\n", port, component->name);
OMX_ERRORTYPE error;
if ((error = OMX_SendCommand (component->handle, OMX_CommandPortDisable,
port, 0))){
fprintf (stderr, "error: OMX_SendCommand: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void enable_encoder_output_port (
component_t* encoder,
OMX_BUFFERHEADERTYPE** encoder_output_buffer){
//The port is not enabled until the buffer is allocated
OMX_ERRORTYPE error;
enable_port (encoder, 341);
OMX_PARAM_PORTDEFINITIONTYPE def_st;
OMX_INIT_STRUCTURE (def_st);
def_st.nPortIndex = 341;
if ((error = OMX_GetParameter (encoder->handle, OMX_IndexParamPortDefinition,
&def_st))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
printf ("allocating %s output buffer\n", encoder->name);
if ((error = OMX_AllocateBuffer (encoder->handle, encoder_output_buffer, 341,
0, def_st.nBufferSize))){
fprintf (stderr, "error: OMX_AllocateBuffer: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
wait (encoder, EVENT_PORT_ENABLE, 0);
}
void disable_encoder_output_port (
component_t* encoder,
OMX_BUFFERHEADERTYPE* encoder_output_buffer){
//The port is not disabled until the buffer is released
OMX_ERRORTYPE error;
disable_port (encoder, 341);
//Free encoder output buffer
printf ("releasing '%s' output buffer\n", encoder->name);
if ((error = OMX_FreeBuffer (encoder->handle, 341, encoder_output_buffer))){
fprintf (stderr, "error: OMX_FreeBuffer: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
wait (encoder, EVENT_PORT_DISABLE, 0);
}
void set_camera_settings (component_t* camera){
printf ("configuring '%s' settings\n", camera->name);
OMX_ERRORTYPE error;
//Sharpness
OMX_CONFIG_SHARPNESSTYPE sharpness_st;
OMX_INIT_STRUCTURE (sharpness_st);
sharpness_st.nPortIndex = OMX_ALL;
sharpness_st.nSharpness = CAM_SHARPNESS;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonSharpness,
&sharpness_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Contrast
OMX_CONFIG_CONTRASTTYPE contrast_st;
OMX_INIT_STRUCTURE (contrast_st);
contrast_st.nPortIndex = OMX_ALL;
contrast_st.nContrast = CAM_CONTRAST;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonContrast,
&contrast_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Saturation
OMX_CONFIG_SATURATIONTYPE saturation_st;
OMX_INIT_STRUCTURE (saturation_st);
saturation_st.nPortIndex = OMX_ALL;
saturation_st.nSaturation = CAM_SATURATION;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonSaturation,
&saturation_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Brightness
OMX_CONFIG_BRIGHTNESSTYPE brightness_st;
OMX_INIT_STRUCTURE (brightness_st);
brightness_st.nPortIndex = OMX_ALL;
brightness_st.nBrightness = CAM_BRIGHTNESS;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonBrightness,
&brightness_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Exposure value
OMX_CONFIG_EXPOSUREVALUETYPE exposure_value_st;
OMX_INIT_STRUCTURE (exposure_value_st);
exposure_value_st.nPortIndex = OMX_ALL;
exposure_value_st.eMetering = CAM_METERING;
exposure_value_st.xEVCompensation = (CAM_EXPOSURE_COMPENSATION << 16)/6;
exposure_value_st.nShutterSpeedMsec = CAM_SHUTTER_SPEED;
exposure_value_st.bAutoShutterSpeed = CAM_SHUTTER_SPEED_AUTO;
exposure_value_st.nSensitivity = CAM_ISO;
exposure_value_st.bAutoSensitivity = CAM_ISO_AUTO;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigCommonExposureValue, &exposure_value_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Exposure control
OMX_CONFIG_EXPOSURECONTROLTYPE exposure_control_st;
OMX_INIT_STRUCTURE (exposure_control_st);
exposure_control_st.nPortIndex = OMX_ALL;
exposure_control_st.eExposureControl = CAM_EXPOSURE;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonExposure,
&exposure_control_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Frame stabilisation
OMX_CONFIG_FRAMESTABTYPE frame_stabilisation_st;
OMX_INIT_STRUCTURE (frame_stabilisation_st);
frame_stabilisation_st.nPortIndex = OMX_ALL;
frame_stabilisation_st.bStab = CAM_FRAME_STABILIZATION;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigCommonFrameStabilisation, &frame_stabilisation_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//White balance
OMX_CONFIG_WHITEBALCONTROLTYPE white_balance_st;
OMX_INIT_STRUCTURE (white_balance_st);
white_balance_st.nPortIndex = OMX_ALL;
white_balance_st.eWhiteBalControl = CAM_WHITE_BALANCE;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonWhiteBalance,
&white_balance_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//White balance gains (if white balance is set to off)
if (!CAM_WHITE_BALANCE){
OMX_CONFIG_CUSTOMAWBGAINSTYPE white_balance_gains_st;
OMX_INIT_STRUCTURE (white_balance_gains_st);
white_balance_gains_st.xGainR = (CAM_WHITE_BALANCE_RED_GAIN << 16)/1000;
white_balance_gains_st.xGainB = (CAM_WHITE_BALANCE_BLUE_GAIN << 16)/1000;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCustomAwbGains,
&white_balance_gains_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
}
//Image filter
OMX_CONFIG_IMAGEFILTERTYPE image_filter_st;
OMX_INIT_STRUCTURE (image_filter_st);
image_filter_st.nPortIndex = OMX_ALL;
image_filter_st.eImageFilter = CAM_IMAGE_FILTER;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonImageFilter,
&image_filter_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Mirror
OMX_CONFIG_MIRRORTYPE mirror_st;
OMX_INIT_STRUCTURE (mirror_st);
mirror_st.nPortIndex = 72;
mirror_st.eMirror = CAM_MIRROR;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonMirror,
&mirror_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Rotation
OMX_CONFIG_ROTATIONTYPE rotation_st;
OMX_INIT_STRUCTURE (rotation_st);
rotation_st.nPortIndex = 72;
rotation_st.nRotation = CAM_ROTATION;
if ((error = OMX_SetConfig (camera->handle, OMX_IndexConfigCommonRotate,
&rotation_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Color enhancement
OMX_CONFIG_COLORENHANCEMENTTYPE color_enhancement_st;
OMX_INIT_STRUCTURE (color_enhancement_st);
color_enhancement_st.nPortIndex = OMX_ALL;
color_enhancement_st.bColorEnhancement = CAM_COLOR_ENABLE;
color_enhancement_st.nCustomizedU = CAM_COLOR_U;
color_enhancement_st.nCustomizedV = CAM_COLOR_V;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigCommonColorEnhancement, &color_enhancement_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Denoise
OMX_CONFIG_BOOLEANTYPE denoise_st;
OMX_INIT_STRUCTURE (denoise_st);
denoise_st.bEnabled = CAM_NOISE_REDUCTION;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigStillColourDenoiseEnable, &denoise_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//ROI
OMX_CONFIG_INPUTCROPTYPE roi_st;
OMX_INIT_STRUCTURE (roi_st);
roi_st.nPortIndex = OMX_ALL;
roi_st.xLeft = (CAM_ROI_LEFT << 16)/100;
roi_st.xTop = (CAM_ROI_TOP << 16)/100;
roi_st.xWidth = (CAM_ROI_WIDTH << 16)/100;
roi_st.xHeight = (CAM_ROI_HEIGHT << 16)/100;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigInputCropPercentages, &roi_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//DRC
OMX_CONFIG_DYNAMICRANGEEXPANSIONTYPE drc_st;
OMX_INIT_STRUCTURE (drc_st);
drc_st.eMode = CAM_DRC;
if ((error = OMX_SetConfig (camera->handle,
OMX_IndexConfigDynamicRangeExpansion, &drc_st))){
fprintf (stderr, "error: OMX_SetConfig: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
}
void set_jpeg_settings (component_t* encoder){
printf ("configuring '%s' settings\n", encoder->name);
OMX_ERRORTYPE error;
//Quality
OMX_IMAGE_PARAM_QFACTORTYPE quality;
OMX_INIT_STRUCTURE (quality);
quality.nPortIndex = 341;
quality.nQFactor = JPEG_QUALITY;
if ((error = OMX_SetParameter (encoder->handle, OMX_IndexParamQFactor,
&quality))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Disable EXIF tags
OMX_CONFIG_BOOLEANTYPE exif;
OMX_INIT_STRUCTURE (exif);
exif.bEnabled = JPEG_EXIF_DISABLE;
if ((error = OMX_SetParameter (encoder->handle, OMX_IndexParamBrcmDisableEXIF,
&exif))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Enable IJG table
OMX_PARAM_IJGSCALINGTYPE ijg;
OMX_INIT_STRUCTURE (ijg);
ijg.nPortIndex = 341;
ijg.bEnabled = JPEG_IJG_ENABLE;
if ((error = OMX_SetParameter (encoder->handle,
OMX_IndexParamBrcmEnableIJGTableScaling, &ijg))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Thumbnail
OMX_PARAM_BRCMTHUMBNAILTYPE thumbnail;
OMX_INIT_STRUCTURE (thumbnail);
thumbnail.bEnable = JPEG_THUMBNAIL_ENABLE;
thumbnail.bUsePreview = JPEG_PREVIEW;
thumbnail.nWidth = JPEG_THUMBNAIL_WIDTH;
thumbnail.nHeight = JPEG_THUMBNAIL_HEIGHT;
if ((error = OMX_SetParameter (encoder->handle, OMX_IndexParamBrcmThumbnail,
&thumbnail))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//EXIF tags
//See firmware/documentation/ilcomponents/image_decode.html for valid keys
char key[] = "IFD0.Make";
char value[] = "Raspberry Pi";
int key_length = strlen (key);
int value_length = strlen (value);
struct {
//These two fields need to be together
OMX_CONFIG_METADATAITEMTYPE metadata_st;
char metadata_padding[value_length];
} item;
OMX_INIT_STRUCTURE (item.metadata_st);
item.metadata_st.nSize = sizeof (item);
item.metadata_st.eScopeMode = OMX_MetadataScopePortLevel;
item.metadata_st.nScopeSpecifier = 341;
item.metadata_st.eKeyCharset = OMX_MetadataCharsetASCII;
item.metadata_st.nKeySizeUsed = key_length;
memcpy (item.metadata_st.nKey, key, key_length);
item.metadata_st.eValueCharset = OMX_MetadataCharsetASCII;
item.metadata_st.nValueMaxSize = sizeof (item.metadata_padding);
item.metadata_st.nValueSizeUsed = value_length;
memcpy (item.metadata_st.nValue, value, value_length);
if ((error = OMX_SetConfig (encoder->handle,
OMX_IndexConfigMetadataItem, &item))){
fprintf (stderr, "OMX_SetConfig: %s", dump_OMX_ERRORTYPE (error));
exit (1);
}
}
int round_up (int value, int divisor){
return (divisor + value - 1) & ~(divisor - 1);
}
int main (){
OMX_ERRORTYPE error;
OMX_BUFFERHEADERTYPE* encoder_output_buffer;
component_t camera;
component_t null_sink;
component_t encoder;
camera.name = "OMX.broadcom.camera";
null_sink.name = "OMX.broadcom.null_sink";
encoder.name = "OMX.broadcom.image_encode";
//Open the file
int fd = open (FILENAME, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0666);
if (fd == -1){
fprintf (stderr, "error: open\n");
exit (1);
}
//Initialize Broadcom's VideoCore APIs
bcm_host_init ();
//Initialize OpenMAX IL
if ((error = OMX_Init ())){
fprintf (stderr, "error: OMX_Init: %s\n", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Initialize components
init_component (&camera);
init_component (&null_sink);
init_component (&encoder);
//Initialize camera drivers
load_camera_drivers (&camera);
//Configure camera sensor
printf ("configuring '%s' sensor\n", camera.name);
OMX_PARAM_SENSORMODETYPE sensor;
OMX_INIT_STRUCTURE (sensor);
sensor.nPortIndex = OMX_ALL;
OMX_INIT_STRUCTURE (sensor.sFrameSize);
sensor.sFrameSize.nPortIndex = OMX_ALL;
if ((error = OMX_GetParameter (camera.handle, OMX_IndexParamCommonSensorMode,
&sensor))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
sensor.bOneShot = OMX_TRUE;
sensor.sFrameSize.nWidth = CAM_WIDTH;
sensor.sFrameSize.nHeight = CAM_HEIGHT;
if ((error = OMX_SetParameter (camera.handle, OMX_IndexParamCommonSensorMode,
&sensor))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Configure camera port definition
printf ("configuring '%s' port definition\n", camera.name);
OMX_PARAM_PORTDEFINITIONTYPE port_def;
OMX_INIT_STRUCTURE (port_def);
port_def.nPortIndex = 72;
if ((error = OMX_GetParameter (camera.handle, OMX_IndexParamPortDefinition,
&port_def))){
fprintf (stderr, "error: OMX_GetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
port_def.format.image.nFrameWidth = CAM_WIDTH;
port_def.format.image.nFrameHeight = CAM_HEIGHT;
port_def.format.image.eCompressionFormat = OMX_IMAGE_CodingUnused;
port_def.format.image.eColorFormat = OMX_COLOR_FormatYUV420PackedPlanar;
//Stride is byte-per-pixel*width, YUV has 1 byte per pixel, so the stride is
//the width (rounded up to the nearest multiple of 16).
//See mmal/util/mmal_util.c, mmal_encoding_width_to_stride()
port_def.format.image.nStride = round_up (CAM_WIDTH, 16);
if ((error = OMX_SetParameter (camera.handle, OMX_IndexParamPortDefinition,
&port_def))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Configure preview port
//In theory the fastest resolution and framerate are 1920x1080 @30fps because
//these are the default settings for the preview port, so the frames don't
//need to be resized. In practice, this is not true. The fastest way to
//produce stills is setting the lowest resolution, that is, 640x480 @30fps.
//The difference between 1920x1080 @30fps and 640x480 @30fps is a speed boost
//of ~4%, from ~1083ms to ~1039ms
port_def.nPortIndex = 70;
port_def.format.video.nFrameWidth = 640;
port_def.format.video.nFrameHeight = 480;
port_def.format.video.eCompressionFormat = OMX_IMAGE_CodingUnused;
port_def.format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedPlanar;
//Setting the framerate to 0 unblocks the shutter speed from 66ms to 772ms
//The higher the speed, the higher the capture time
port_def.format.video.xFramerate = 0;
port_def.format.video.nStride = 640;
if ((error = OMX_SetParameter (camera.handle, OMX_IndexParamPortDefinition,
&port_def))){
fprintf (stderr, "error: OMX_SetParameter - "
"OMX_IndexParamPortDefinition: %s", dump_OMX_ERRORTYPE (error));
exit (1);
}
//Configure camera settings
set_camera_settings (&camera);
//Configure encoder port definition
printf ("configuring '%s' port definition\n", encoder.name);
OMX_INIT_STRUCTURE (port_def);
port_def.nPortIndex = 341;
if ((error = OMX_GetParameter (encoder.handle, OMX_IndexParamPortDefinition,
&port_def))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
port_def.format.image.nFrameWidth = CAM_WIDTH;
port_def.format.image.nFrameHeight = CAM_HEIGHT;
port_def.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;
port_def.format.image.eColorFormat = OMX_COLOR_FormatUnused;
if ((error = OMX_SetParameter (encoder.handle, OMX_IndexParamPortDefinition,
&port_def))){
fprintf (stderr, "error: OMX_SetParameter: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Configure JPEG settings
set_jpeg_settings (&encoder);
//Setup tunnels: camera (still) -> image_encode, camera (preview) -> null_sink
printf ("configuring tunnels\n");
if ((error = OMX_SetupTunnel (camera.handle, 72, encoder.handle, 340))){
fprintf (stderr, "error: OMX_SetupTunnel: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
if ((error = OMX_SetupTunnel (camera.handle, 70, null_sink.handle, 240))){
fprintf (stderr, "error: OMX_SetupTunnel: %s\n",
dump_OMX_ERRORTYPE (error));
exit (1);
}
//Change state to IDLE
change_state (&camera, OMX_StateIdle);
wait (&camera, EVENT_STATE_SET, 0);
change_state (&null_sink, OMX_StateIdle);
wait (&null_sink, EVENT_STATE_SET, 0);
change_state (&encoder, OMX_StateIdle);
wait (&encoder, EVENT_STATE_SET, 0);
//Enable the tunnel ports
enable_port (&camera, 72);
enable_port (&camera, 70);
enable_port (&null_sink, 240);
enable_port (&encoder, 340);
enable_encoder_output_port (&encoder, &encoder_output_buffer);
//Change state to EXECUTING
change_state (&camera, OMX_StateExecuting);
wait (&camera, EVENT_STATE_SET, 0);
change_state (&null_sink, OMX_StateExecuting);
wait (&null_sink, EVENT_STATE_SET, 0);
change_state (&encoder, OMX_StateExecuting);
wait (&encoder, EVENT_STATE_SET, 0);
//Enable camera capture port. This basically says that the port 72 will be
//used to get data from the camera. If you're capturing video, the port 71
//must be used
printf ("enabling '%s' capture port\n", camera.name);
OMX_CONFIG_PORTBOOLEANTYPE cameraCapturePort;
OMX_INIT_STRUCTURE (cameraCapturePort);
cameraCapturePort.nPortIndex = 72;
cameraCapturePort.bEnabled = OMX_TRUE;
if ((error = OMX_SetConfig (camera.handle, OMX_IndexConfigPortCapturing,