process_pipeline.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include "process_pipeline.h"
  2. #include "pipeline.h"
  3. #include "main.h"
  4. #include "config.h"
  5. #include "quickpreview.h"
  6. #include <tiffio.h>
  7. #include <assert.h>
  8. #include <math.h>
  9. #include <wordexp.h>
  10. #include <gtk/gtk.h>
  11. #define TIFFTAG_FORWARDMATRIX1 50964
  12. static const float colormatrix_srgb[] = {
  13. 3.2409, -1.5373, -0.4986,
  14. -0.9692, 1.8759, 0.0415,
  15. 0.0556, -0.2039, 1.0569
  16. };
  17. static MPPipeline *pipeline;
  18. static char burst_dir[23];
  19. static char processing_script[512];
  20. static volatile bool is_capturing = false;
  21. static volatile int frames_processed = 0;
  22. static volatile int frames_received = 0;
  23. static const struct mp_camera_config *camera;
  24. static MPCameraMode mode;
  25. static int burst_length;
  26. static int captures_remaining = 0;
  27. static int preview_width;
  28. static int preview_height;
  29. // static bool gain_is_manual;
  30. static int gain;
  31. static int gain_max;
  32. static bool exposure_is_manual;
  33. static int exposure;
  34. static char capture_fname[255];
  35. static void
  36. register_custom_tiff_tags(TIFF *tif)
  37. {
  38. static const TIFFFieldInfo custom_fields[] = {
  39. {TIFFTAG_FORWARDMATRIX1, -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 1, 1, "ForwardMatrix1"},
  40. };
  41. // Add missing dng fields
  42. TIFFMergeFieldInfo(tif, custom_fields, sizeof(custom_fields) / sizeof(custom_fields[0]));
  43. }
  44. static bool
  45. find_processor(char *script)
  46. {
  47. char *xdg_config_home;
  48. char filename[] = "postprocess.sh";
  49. wordexp_t exp_result;
  50. // Resolve XDG stuff
  51. if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
  52. xdg_config_home = "~/.config";
  53. }
  54. wordexp(xdg_config_home, &exp_result, 0);
  55. xdg_config_home = strdup(exp_result.we_wordv[0]);
  56. wordfree(&exp_result);
  57. // Check postprocess.h in the current working directory
  58. sprintf(script, "%s", filename);
  59. if(access(script, F_OK) != -1) {
  60. sprintf(script, "./%s", filename);
  61. printf("Found postprocessor script at %s\n", script);
  62. return true;
  63. }
  64. // Check for a script in XDG_CONFIG_HOME
  65. sprintf(script, "%s/megapixels/%s", xdg_config_home, filename);
  66. if(access(script, F_OK) != -1) {
  67. printf("Found postprocessor script at %s\n", script);
  68. return true;
  69. }
  70. // Check user overridden /etc/megapixels/postprocessor.sh
  71. sprintf(script, "%s/megapixels/%s", SYSCONFDIR, filename);
  72. if(access(script, F_OK) != -1) {
  73. printf("Found postprocessor script at %s\n", script);
  74. return true;
  75. }
  76. // Check packaged /usr/share/megapixels/postprocessor.sh
  77. sprintf(script, "%s/megapixels/%s", DATADIR, filename);
  78. if(access(script, F_OK) != -1) {
  79. printf("Found postprocessor script at %s\n", script);
  80. return true;
  81. }
  82. return false;
  83. }
  84. static void setup(MPPipeline *pipeline, const void *data)
  85. {
  86. TIFFSetTagExtender(register_custom_tiff_tags);
  87. if (!find_processor(processing_script)) {
  88. g_printerr("Could not find any post-process script\n");
  89. exit(1);
  90. }
  91. }
  92. void mp_process_pipeline_start()
  93. {
  94. pipeline = mp_pipeline_new();
  95. mp_pipeline_invoke(pipeline, setup, NULL, 0);
  96. }
  97. void mp_process_pipeline_stop()
  98. {
  99. mp_pipeline_free(pipeline);
  100. }
  101. static void
  102. process_image_for_preview(const MPImage *image)
  103. {
  104. uint32_t surface_width, surface_height, skip;
  105. quick_preview_size(
  106. &surface_width,
  107. &surface_height,
  108. &skip,
  109. preview_width,
  110. preview_height,
  111. image->width,
  112. image->height,
  113. image->pixel_format,
  114. camera->rotate);
  115. cairo_surface_t *surface = cairo_image_surface_create(
  116. CAIRO_FORMAT_RGB24,
  117. surface_width,
  118. surface_height);
  119. uint8_t *pixels = cairo_image_surface_get_data(surface);
  120. quick_preview(
  121. (uint32_t *)pixels,
  122. surface_width,
  123. surface_height,
  124. image->data,
  125. image->width,
  126. image->height,
  127. image->pixel_format,
  128. camera->rotate,
  129. camera->mirrored,
  130. camera->colormatrix[0] == 0 ? NULL : camera->colormatrix,
  131. camera->blacklevel,
  132. skip);
  133. mp_main_set_preview(surface);
  134. }
  135. static void
  136. process_image_for_capture(const MPImage *image, int count)
  137. {
  138. time_t rawtime;
  139. time(&rawtime);
  140. struct tm tim = *(localtime(&rawtime));
  141. char datetime[20] = {0};
  142. strftime(datetime, 20, "%Y:%m:%d %H:%M:%S", &tim);
  143. char fname[255];
  144. sprintf(fname, "%s/%d.dng", burst_dir, count);
  145. TIFF *tif = TIFFOpen(fname, "w");
  146. if(!tif) {
  147. printf("Could not open tiff\n");
  148. }
  149. // Define TIFF thumbnail
  150. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 1);
  151. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, image->width >> 4);
  152. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, image->height >> 4);
  153. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  154. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  155. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  156. TIFFSetField(tif, TIFFTAG_MAKE, mp_get_device_make());
  157. TIFFSetField(tif, TIFFTAG_MODEL, mp_get_device_model());
  158. uint16_t orientation;
  159. if (camera->rotate == 0) {
  160. orientation = camera->mirrored ? ORIENTATION_TOPRIGHT : ORIENTATION_TOPLEFT;
  161. } else if (camera->rotate == 90) {
  162. orientation = camera->mirrored ? ORIENTATION_RIGHTBOT : ORIENTATION_LEFTBOT;
  163. } else if (camera->rotate == 180) {
  164. orientation = camera->mirrored ? ORIENTATION_BOTLEFT : ORIENTATION_BOTRIGHT;
  165. } else {
  166. orientation = camera->mirrored ? ORIENTATION_LEFTTOP : ORIENTATION_RIGHTTOP;
  167. }
  168. TIFFSetField(tif, TIFFTAG_ORIENTATION, orientation);
  169. TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
  170. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
  171. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  172. TIFFSetField(tif, TIFFTAG_SOFTWARE, "Megapixels");
  173. long sub_offset = 0;
  174. TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &sub_offset);
  175. TIFFSetField(tif, TIFFTAG_DNGVERSION, "\001\001\0\0");
  176. TIFFSetField(tif, TIFFTAG_DNGBACKWARDVERSION, "\001\0\0\0");
  177. char uniquecameramodel[255];
  178. sprintf(uniquecameramodel, "%s %s", mp_get_device_make(), mp_get_device_model());
  179. TIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, uniquecameramodel);
  180. if(camera->colormatrix[0]) {
  181. TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, camera->colormatrix);
  182. } else {
  183. TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, colormatrix_srgb);
  184. }
  185. if(camera->forwardmatrix[0]) {
  186. TIFFSetField(tif, TIFFTAG_FORWARDMATRIX1, 9, camera->forwardmatrix);
  187. }
  188. static const float neutral[] = {1.0, 1.0, 1.0};
  189. TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral);
  190. TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT1, 21);
  191. // Write black thumbnail, only windows uses this
  192. {
  193. unsigned char *buf = (unsigned char *)calloc(1, (int)image->width >> 4);
  194. for (int row = 0; row < image->height>>4; row++) {
  195. TIFFWriteScanline(tif, buf, row, 0);
  196. }
  197. free(buf);
  198. }
  199. TIFFWriteDirectory(tif);
  200. // Define main photo
  201. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
  202. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, image->width);
  203. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, image->height);
  204. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  205. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA);
  206. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  207. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  208. static const short cfapatterndim[] = {2, 2};
  209. TIFFSetField(tif, TIFFTAG_CFAREPEATPATTERNDIM, cfapatterndim);
  210. TIFFSetField(tif, TIFFTAG_CFAPATTERN, "\002\001\001\000"); // BGGR
  211. if(camera->whitelevel) {
  212. TIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &camera->whitelevel);
  213. }
  214. if(camera->blacklevel) {
  215. TIFFSetField(tif, TIFFTAG_BLACKLEVEL, 1, &camera->blacklevel);
  216. }
  217. TIFFCheckpointDirectory(tif);
  218. printf("Writing frame to %s\n", fname);
  219. unsigned char *pLine = (unsigned char*)malloc(image->width);
  220. for(int row = 0; row < image->height; row++){
  221. TIFFWriteScanline(tif, image->data + (row * image->width), row, 0);
  222. }
  223. free(pLine);
  224. TIFFWriteDirectory(tif);
  225. // Add an EXIF block to the tiff
  226. TIFFCreateEXIFDirectory(tif);
  227. // 1 = manual, 2 = full auto, 3 = aperture priority, 4 = shutter priority
  228. if (!exposure_is_manual) {
  229. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 2);
  230. } else {
  231. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 1);
  232. }
  233. TIFFSetField(tif, EXIFTAG_EXPOSURETIME, (mode.frame_interval.numerator / (float)mode.frame_interval.denominator) / ((float)image->height / (float)exposure));
  234. uint16_t isospeed[1];
  235. isospeed[0] = (uint16_t)remap(gain - 1, 0, gain_max, camera->iso_min, camera->iso_max);
  236. TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, isospeed);
  237. TIFFSetField(tif, EXIFTAG_FLASH, 0);
  238. TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, datetime);
  239. TIFFSetField(tif, EXIFTAG_DATETIMEDIGITIZED, datetime);
  240. if(camera->fnumber) {
  241. TIFFSetField(tif, EXIFTAG_FNUMBER, camera->fnumber);
  242. }
  243. if(camera->focallength) {
  244. TIFFSetField(tif, EXIFTAG_FOCALLENGTH, camera->focallength);
  245. }
  246. if(camera->focallength && camera->cropfactor) {
  247. TIFFSetField(tif, EXIFTAG_FOCALLENGTHIN35MMFILM, (short)(camera->focallength * camera->cropfactor));
  248. }
  249. uint64_t exif_offset = 0;
  250. TIFFWriteCustomDirectory(tif, &exif_offset);
  251. TIFFFreeDirectory(tif);
  252. // Update exif pointer
  253. TIFFSetDirectory(tif, 0);
  254. TIFFSetField(tif, TIFFTAG_EXIFIFD, exif_offset);
  255. TIFFRewriteDirectory(tif);
  256. TIFFClose(tif);
  257. }
  258. static void
  259. process_capture_burst()
  260. {
  261. time_t rawtime;
  262. time(&rawtime);
  263. struct tm tim = *(localtime(&rawtime));
  264. char timestamp[30];
  265. strftime(timestamp, 30, "%Y%m%d%H%M%S", &tim);
  266. sprintf(capture_fname, "%s/Pictures/IMG%s", getenv("HOME"), timestamp);
  267. // Start post-processing the captured burst
  268. g_print("Post process %s to %s.ext\n", burst_dir, capture_fname);
  269. char command[1024];
  270. sprintf(command, "%s %s %s &", processing_script, burst_dir, capture_fname);
  271. system(command);
  272. }
  273. static void
  274. process_image(MPPipeline *pipeline, const MPImage *image)
  275. {
  276. assert(image->width == mode.width && image->height == mode.height);
  277. process_image_for_preview(image);
  278. if (captures_remaining > 0) {
  279. int count = burst_length - captures_remaining;
  280. --captures_remaining;
  281. process_image_for_capture(image, count);
  282. if (captures_remaining == 0) {
  283. process_capture_burst();
  284. mp_main_capture_completed(capture_fname);
  285. }
  286. }
  287. free(image->data);
  288. ++frames_processed;
  289. if (captures_remaining == 0) {
  290. is_capturing = false;
  291. }
  292. }
  293. void mp_process_pipeline_process_image(MPImage image)
  294. {
  295. // If we haven't processed the previous frame yet, drop this one
  296. if (frames_received != frames_processed && !is_capturing) {
  297. printf("Dropped frame at capture %d %d\n", frames_received, frames_processed);
  298. return;
  299. }
  300. ++frames_received;
  301. mp_pipeline_invoke(pipeline, (MPPipelineCallback)process_image, &image, sizeof(MPImage));
  302. }
  303. static void capture()
  304. {
  305. char template[] = "/tmp/megapixels.XXXXXX";
  306. char *tempdir;
  307. tempdir = mkdtemp(template);
  308. if (tempdir == NULL) {
  309. g_printerr("Could not make capture directory %s\n", template);
  310. exit (EXIT_FAILURE);
  311. }
  312. strcpy(burst_dir, tempdir);
  313. captures_remaining = burst_length;
  314. }
  315. void mp_process_pipeline_capture()
  316. {
  317. is_capturing = true;
  318. mp_pipeline_invoke(pipeline, capture, NULL, 0);
  319. }
  320. static void
  321. update_state(MPPipeline *pipeline, const struct mp_process_pipeline_state *state)
  322. {
  323. camera = state->camera;
  324. mode = state->mode;
  325. burst_length = state->burst_length;
  326. preview_width = state->preview_width;
  327. preview_height = state->preview_height;
  328. // gain_is_manual = state->gain_is_manual;
  329. gain = state->gain;
  330. gain_max = state->gain_max;
  331. exposure_is_manual = state->exposure_is_manual;
  332. exposure = state->exposure;
  333. struct mp_main_state main_state = {
  334. .camera = camera,
  335. .mode = mode,
  336. .gain_is_manual = state->gain_is_manual,
  337. .gain = gain,
  338. .gain_max = gain_max,
  339. .exposure_is_manual = exposure_is_manual,
  340. .exposure = exposure,
  341. .has_auto_focus_continuous = state->has_auto_focus_continuous,
  342. .has_auto_focus_start = state->has_auto_focus_start,
  343. };
  344. mp_main_update_state(&main_state);
  345. }
  346. void mp_process_pipeline_update_state(const struct mp_process_pipeline_state *new_state)
  347. {
  348. mp_pipeline_invoke(pipeline, (MPPipelineCallback)update_state, new_state, sizeof(struct mp_process_pipeline_state));
  349. }