process_pipeline.c 12 KB

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