process_pipeline.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. TIFFSetField(tif, TIFFTAG_CFAPATTERN, "\002\001\001\000"); // BGGR
  202. if (camera->whitelevel) {
  203. TIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &camera->whitelevel);
  204. }
  205. if (camera->blacklevel) {
  206. TIFFSetField(tif, TIFFTAG_BLACKLEVEL, 1, &camera->blacklevel);
  207. }
  208. TIFFCheckpointDirectory(tif);
  209. printf("Writing frame to %s\n", fname);
  210. unsigned char *pLine = (unsigned char *)malloc(image->width);
  211. for (int row = 0; row < image->height; row++) {
  212. TIFFWriteScanline(tif, image->data + (row * image->width), row, 0);
  213. }
  214. free(pLine);
  215. TIFFWriteDirectory(tif);
  216. // Add an EXIF block to the tiff
  217. TIFFCreateEXIFDirectory(tif);
  218. // 1 = manual, 2 = full auto, 3 = aperture priority, 4 = shutter priority
  219. if (!exposure_is_manual) {
  220. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 2);
  221. } else {
  222. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 1);
  223. }
  224. TIFFSetField(tif, EXIFTAG_EXPOSURETIME,
  225. (mode.frame_interval.numerator /
  226. (float)mode.frame_interval.denominator) /
  227. ((float)image->height / (float)exposure));
  228. uint16_t isospeed[1];
  229. isospeed[0] = (uint16_t)remap(gain - 1, 0, gain_max, camera->iso_min,
  230. camera->iso_max);
  231. TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, isospeed);
  232. TIFFSetField(tif, EXIFTAG_FLASH, 0);
  233. TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, datetime);
  234. TIFFSetField(tif, EXIFTAG_DATETIMEDIGITIZED, datetime);
  235. if (camera->fnumber) {
  236. TIFFSetField(tif, EXIFTAG_FNUMBER, camera->fnumber);
  237. }
  238. if (camera->focallength) {
  239. TIFFSetField(tif, EXIFTAG_FOCALLENGTH, camera->focallength);
  240. }
  241. if (camera->focallength && camera->cropfactor) {
  242. TIFFSetField(tif, EXIFTAG_FOCALLENGTHIN35MMFILM,
  243. (short)(camera->focallength * camera->cropfactor));
  244. }
  245. uint64_t exif_offset = 0;
  246. TIFFWriteCustomDirectory(tif, &exif_offset);
  247. TIFFFreeDirectory(tif);
  248. // Update exif pointer
  249. TIFFSetDirectory(tif, 0);
  250. TIFFSetField(tif, TIFFTAG_EXIFIFD, exif_offset);
  251. TIFFRewriteDirectory(tif);
  252. TIFFClose(tif);
  253. }
  254. static void
  255. process_capture_burst()
  256. {
  257. time_t rawtime;
  258. time(&rawtime);
  259. struct tm tim = *(localtime(&rawtime));
  260. char timestamp[30];
  261. strftime(timestamp, 30, "%Y%m%d%H%M%S", &tim);
  262. sprintf(capture_fname, "%s/Pictures/IMG%s", getenv("HOME"), timestamp);
  263. // Start post-processing the captured burst
  264. g_print("Post process %s to %s.ext\n", burst_dir, capture_fname);
  265. char command[1024];
  266. sprintf(command, "%s %s %s &", processing_script, burst_dir, capture_fname);
  267. system(command);
  268. }
  269. static void
  270. process_image(MPPipeline *pipeline, const MPImage *image)
  271. {
  272. assert(image->width == mode.width && image->height == mode.height);
  273. process_image_for_preview(image);
  274. if (captures_remaining > 0) {
  275. int count = burst_length - captures_remaining;
  276. --captures_remaining;
  277. process_image_for_capture(image, count);
  278. if (captures_remaining == 0) {
  279. process_capture_burst();
  280. mp_main_capture_completed(capture_fname);
  281. }
  282. }
  283. free(image->data);
  284. ++frames_processed;
  285. if (captures_remaining == 0) {
  286. is_capturing = false;
  287. }
  288. }
  289. void
  290. mp_process_pipeline_process_image(MPImage image)
  291. {
  292. // If we haven't processed the previous frame yet, drop this one
  293. if (frames_received != frames_processed && !is_capturing) {
  294. printf("Dropped frame at capture\n");
  295. return;
  296. }
  297. ++frames_received;
  298. mp_pipeline_invoke(pipeline, (MPPipelineCallback)process_image, &image,
  299. sizeof(MPImage));
  300. }
  301. static void
  302. capture()
  303. {
  304. char template[] = "/tmp/megapixels.XXXXXX";
  305. char *tempdir;
  306. tempdir = mkdtemp(template);
  307. if (tempdir == NULL) {
  308. g_printerr("Could not make capture directory %s\n", template);
  309. exit(EXIT_FAILURE);
  310. }
  311. strcpy(burst_dir, tempdir);
  312. captures_remaining = burst_length;
  313. }
  314. void
  315. 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
  347. mp_process_pipeline_update_state(const struct mp_process_pipeline_state *new_state)
  348. {
  349. mp_pipeline_invoke(pipeline, (MPPipelineCallback)update_state, new_state,
  350. sizeof(struct mp_process_pipeline_state));
  351. }