writeavi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): Robert Wenzlaff
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. *
  27. * Functions for writing avi-format files.
  28. * Added interface for generic movie support (ton)
  29. */
  30. /** \file blender/blenkernel/intern/writeavi.c
  31. * \ingroup bke
  32. */
  33. #include <string.h>
  34. #include "MEM_guardedalloc.h"
  35. #include "DNA_scene_types.h"
  36. #include "BLI_blenlib.h"
  37. #include "BLI_utildefines.h"
  38. #include "BKE_global.h"
  39. #include "BKE_main.h"
  40. #include "BKE_report.h"
  41. #include "BKE_writeavi.h"
  42. /* ********************** general blender movie support ***************************** */
  43. static int start_stub(void *UNUSED(context_v), Scene *UNUSED(scene), RenderData *UNUSED(rd), int UNUSED(rectx), int UNUSED(recty),
  44. ReportList *UNUSED(reports), bool UNUSED(preview), const char *UNUSED(suffix))
  45. { return 0; }
  46. static void end_stub(void *UNUSED(context_v))
  47. {}
  48. static int append_stub(void *UNUSED(context_v), RenderData *UNUSED(rd), int UNUSED(start_frame), int UNUSED(frame), int *UNUSED(pixels),
  49. int UNUSED(rectx), int UNUSED(recty), const char *UNUSED(suffix), ReportList *UNUSED(reports))
  50. { return 0; }
  51. static void *context_create_stub(void)
  52. { return NULL; }
  53. static void context_free_stub(void *UNUSED(context_v))
  54. {}
  55. #ifdef WITH_AVI
  56. # include "AVI_avi.h"
  57. /* callbacks */
  58. static int start_avi(void *context_v, Scene *scene, RenderData *rd, int rectx, int recty, ReportList *reports, bool preview, const char *suffix);
  59. static void end_avi(void *context_v);
  60. static int append_avi(void *context_v, RenderData *rd, int start_frame, int frame, int *pixels,
  61. int rectx, int recty, const char *suffix, ReportList *reports);
  62. static void filepath_avi(char *string, RenderData *rd, bool preview, const char *suffix);
  63. static void *context_create_avi(void);
  64. static void context_free_avi(void *context_v);
  65. #endif /* WITH_AVI */
  66. #ifdef WITH_QUICKTIME
  67. # include "quicktime_export.h"
  68. #endif
  69. #ifdef WITH_FFMPEG
  70. # include "BKE_writeffmpeg.h"
  71. #endif
  72. #ifdef WITH_FRAMESERVER
  73. # include "BKE_writeframeserver.h"
  74. #endif
  75. bMovieHandle *BKE_movie_handle_get(const char imtype)
  76. {
  77. static bMovieHandle mh = {NULL};
  78. /* stub callbacks in case none of the movie formats is supported */
  79. mh.start_movie = start_stub;
  80. mh.append_movie = append_stub;
  81. mh.end_movie = end_stub;
  82. mh.get_next_frame = NULL;
  83. mh.get_movie_path = NULL;
  84. mh.context_create = context_create_stub;
  85. mh.context_free = context_free_stub;
  86. /* set the default handle, as builtin */
  87. #ifdef WITH_AVI
  88. mh.start_movie = start_avi;
  89. mh.append_movie = append_avi;
  90. mh.end_movie = end_avi;
  91. mh.get_movie_path = filepath_avi;
  92. mh.context_create = context_create_avi;
  93. mh.context_free = context_free_avi;
  94. #endif
  95. /* do the platform specific handles */
  96. #ifdef WITH_QUICKTIME
  97. if (imtype == R_IMF_IMTYPE_QUICKTIME) {
  98. mh.start_movie = start_qt;
  99. mh.append_movie = append_qt;
  100. mh.end_movie = end_qt;
  101. mh.get_movie_path = filepath_qt;
  102. mh.context_create = context_create_qt;
  103. mh.context_free = context_free_qt;
  104. }
  105. #endif
  106. #ifdef WITH_FFMPEG
  107. if (ELEM(imtype, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_XVID, R_IMF_IMTYPE_THEORA)) {
  108. mh.start_movie = BKE_ffmpeg_start;
  109. mh.append_movie = BKE_ffmpeg_append;
  110. mh.end_movie = BKE_ffmpeg_end;
  111. mh.get_movie_path = BKE_ffmpeg_filepath_get;
  112. mh.context_create = BKE_ffmpeg_context_create;
  113. mh.context_free = BKE_ffmpeg_context_free;
  114. }
  115. #endif
  116. #ifdef WITH_FRAMESERVER
  117. if (imtype == R_IMF_IMTYPE_FRAMESERVER) {
  118. mh.start_movie = BKE_frameserver_start;
  119. mh.append_movie = BKE_frameserver_append;
  120. mh.end_movie = BKE_frameserver_end;
  121. mh.get_next_frame = BKE_frameserver_loop;
  122. mh.context_create = BKE_frameserver_context_create;
  123. mh.context_free = BKE_frameserver_context_free;
  124. }
  125. #endif
  126. /* in case all above are disabled */
  127. (void)imtype;
  128. return (mh.append_movie != append_stub) ? &mh : NULL;
  129. }
  130. /* ****************************************************************** */
  131. #ifdef WITH_AVI
  132. static void filepath_avi(char *string, RenderData *rd, bool preview, const char *suffix)
  133. {
  134. int sfra, efra;
  135. if (string == NULL) return;
  136. if (preview) {
  137. sfra = rd->psfra;
  138. efra = rd->pefra;
  139. }
  140. else {
  141. sfra = rd->sfra;
  142. efra = rd->efra;
  143. }
  144. strcpy(string, rd->pic);
  145. BLI_path_abs(string, G.main->name);
  146. BLI_make_existing_file(string);
  147. if (rd->scemode & R_EXTENSION) {
  148. if (!BLI_testextensie(string, ".avi")) {
  149. BLI_path_frame_range(string, sfra, efra, 4);
  150. strcat(string, ".avi");
  151. }
  152. }
  153. else {
  154. if (BLI_path_frame_check_chars(string)) {
  155. BLI_path_frame_range(string, sfra, efra, 4);
  156. }
  157. }
  158. BLI_path_suffix(string, FILE_MAX, suffix, "");
  159. }
  160. static int start_avi(void *context_v, Scene *UNUSED(scene), RenderData *rd, int rectx, int recty,
  161. ReportList *reports, bool preview, const char *suffix)
  162. {
  163. int x, y;
  164. char name[256];
  165. AviFormat format;
  166. int quality;
  167. double framerate;
  168. AviMovie *avi = context_v;
  169. filepath_avi(name, rd, preview, suffix);
  170. x = rectx;
  171. y = recty;
  172. quality = rd->im_format.quality;
  173. framerate = (double) rd->frs_sec / (double) rd->frs_sec_base;
  174. if (rd->im_format.imtype != R_IMF_IMTYPE_AVIJPEG) format = AVI_FORMAT_AVI_RGB;
  175. else format = AVI_FORMAT_MJPEG;
  176. if (AVI_open_compress(name, avi, 1, format) != AVI_ERROR_NONE) {
  177. BKE_report(reports, RPT_ERROR, "Cannot open or start AVI movie file");
  178. return 0;
  179. }
  180. AVI_set_compress_option(avi, AVI_OPTION_TYPE_MAIN, 0, AVI_OPTION_WIDTH, &x);
  181. AVI_set_compress_option(avi, AVI_OPTION_TYPE_MAIN, 0, AVI_OPTION_HEIGHT, &y);
  182. AVI_set_compress_option(avi, AVI_OPTION_TYPE_MAIN, 0, AVI_OPTION_QUALITY, &quality);
  183. AVI_set_compress_option(avi, AVI_OPTION_TYPE_MAIN, 0, AVI_OPTION_FRAMERATE, &framerate);
  184. avi->interlace = 0;
  185. avi->odd_fields = 0;
  186. /* avi->interlace = rd->mode & R_FIELDS; */
  187. /* avi->odd_fields = (rd->mode & R_ODDFIELD) ? 1 : 0; */
  188. printf("Created avi: %s\n", name);
  189. return 1;
  190. }
  191. static int append_avi(void *context_v, RenderData *UNUSED(rd), int start_frame, int frame, int *pixels,
  192. int rectx, int recty, const char *UNUSED(suffix), ReportList *UNUSED(reports))
  193. {
  194. unsigned int *rt1, *rt2, *rectot;
  195. int x, y;
  196. char *cp, rt;
  197. AviMovie *avi = context_v;
  198. if (avi == NULL)
  199. return 0;
  200. /* note that libavi free's the buffer... stupid interface - zr */
  201. rectot = MEM_mallocN(rectx * recty * sizeof(int), "rectot");
  202. rt1 = rectot;
  203. rt2 = (unsigned int *)pixels + (recty - 1) * rectx;
  204. /* flip y and convert to abgr */
  205. for (y = 0; y < recty; y++, rt1 += rectx, rt2 -= rectx) {
  206. memcpy(rt1, rt2, rectx * sizeof(int));
  207. cp = (char *)rt1;
  208. for (x = rectx; x > 0; x--) {
  209. rt = cp[0];
  210. cp[0] = cp[3];
  211. cp[3] = rt;
  212. rt = cp[1];
  213. cp[1] = cp[2];
  214. cp[2] = rt;
  215. cp += 4;
  216. }
  217. }
  218. AVI_write_frame(avi, (frame - start_frame), AVI_FORMAT_RGB32, rectot, rectx * recty * 4);
  219. // printf("added frame %3d (frame %3d in avi): ", frame, frame-start_frame);
  220. return 1;
  221. }
  222. static void end_avi(void *context_v)
  223. {
  224. AviMovie *avi = context_v;
  225. if (avi == NULL) return;
  226. AVI_close_compress(avi);
  227. }
  228. static void *context_create_avi(void)
  229. {
  230. AviMovie *avi = MEM_mallocN(sizeof(AviMovie), "avimovie");
  231. return avi;
  232. }
  233. static void context_free_avi(void *context_v)
  234. {
  235. AviMovie *avi = context_v;
  236. if (avi) {
  237. MEM_freeN(avi);
  238. }
  239. }
  240. #endif /* WITH_AVI */
  241. /* similar to BKE_image_path_from_imformat() */
  242. void BKE_movie_filepath_get(char *string, RenderData *rd, bool preview, const char *suffix)
  243. {
  244. bMovieHandle *mh = BKE_movie_handle_get(rd->im_format.imtype);
  245. if (mh && mh->get_movie_path) {
  246. mh->get_movie_path(string, rd, preview, suffix);
  247. }
  248. else {
  249. string[0] = '\0';
  250. }
  251. }