/src/mpv/video/out/vo_image.c
Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv 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 Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <stdio.h> |
19 | | #include <stdlib.h> |
20 | | #include <string.h> |
21 | | #include <math.h> |
22 | | #include <stdbool.h> |
23 | | #include <sys/stat.h> |
24 | | |
25 | | #include <libswscale/swscale.h> |
26 | | |
27 | | #include "misc/bstr.h" |
28 | | #include "osdep/io.h" |
29 | | #include "options/m_config.h" |
30 | | #include "options/path.h" |
31 | | #include "mpv_talloc.h" |
32 | | #include "common/common.h" |
33 | | #include "common/msg.h" |
34 | | #include "video/out/vo.h" |
35 | | #include "video/csputils.h" |
36 | | #include "video/mp_image.h" |
37 | | #include "video/fmt-conversion.h" |
38 | | #include "video/image_writer.h" |
39 | | #include "video/sws_utils.h" |
40 | | #include "sub/osd.h" |
41 | | #include "options/m_option.h" |
42 | | |
43 | | static const struct m_sub_options image_writer_conf = { |
44 | | .opts = image_writer_opts, |
45 | | .size = sizeof(struct image_writer_opts), |
46 | | .defaults = &image_writer_opts_defaults, |
47 | | }; |
48 | | |
49 | | struct vo_image_opts { |
50 | | struct image_writer_opts *opts; |
51 | | char *outdir; |
52 | | }; |
53 | | |
54 | | #define OPT_BASE_STRUCT struct vo_image_opts |
55 | | |
56 | | static const struct m_sub_options vo_image_conf = { |
57 | | .opts = (const struct m_option[]) { |
58 | | {"vo-image", OPT_SUBSTRUCT(opts, image_writer_conf)}, |
59 | | {"vo-image-outdir", OPT_STRING(outdir), .flags = M_OPT_FILE}, |
60 | | {0}, |
61 | | }, |
62 | | .size = sizeof(struct vo_image_opts), |
63 | | }; |
64 | | |
65 | | struct priv { |
66 | | struct vo_image_opts *opts; |
67 | | |
68 | | struct mp_image *current; |
69 | | char *dir; |
70 | | int frame; |
71 | | }; |
72 | | |
73 | | static bool checked_mkdir(struct vo *vo, const char *buf) |
74 | 0 | { |
75 | 0 | struct priv *p = vo->priv; |
76 | 0 | p->dir = mp_get_user_path(vo, vo->global, buf); |
77 | 0 | MP_INFO(vo, "Creating output directory '%s'...\n", p->dir); |
78 | 0 | if (mkdir(p->dir, 0755) < 0) { |
79 | 0 | char *errstr = mp_strerror(errno); |
80 | 0 | if (errno == EEXIST) { |
81 | 0 | struct stat stat_p; |
82 | 0 | if (stat(buf, &stat_p ) == 0 && S_ISDIR(stat_p.st_mode)) |
83 | 0 | return true; |
84 | 0 | } |
85 | 0 | MP_ERR(vo, "Error creating output directory: %s\n", errstr); |
86 | 0 | return false; |
87 | 0 | } |
88 | 0 | return true; |
89 | 0 | } |
90 | | |
91 | | static int reconfig(struct vo *vo, struct mp_image_params *params) |
92 | 0 | { |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | | static bool draw_frame(struct vo *vo, struct vo_frame *frame) |
97 | 0 | { |
98 | 0 | struct priv *p = vo->priv; |
99 | 0 | if (!frame->current) |
100 | 0 | goto done; |
101 | | |
102 | 0 | p->current = frame->current; |
103 | |
|
104 | 0 | struct mp_osd_res dim = osd_res_from_image_params(vo->params); |
105 | 0 | osd_draw_on_image(vo->osd, dim, frame->current->pts, OSD_DRAW_SUB_ONLY, p->current); |
106 | |
|
107 | 0 | done: |
108 | 0 | return VO_TRUE; |
109 | 0 | } |
110 | | |
111 | | static void flip_page(struct vo *vo) |
112 | 0 | { |
113 | 0 | struct priv *p = vo->priv; |
114 | 0 | if (!p->current) |
115 | 0 | return; |
116 | | |
117 | 0 | (p->frame)++; |
118 | |
|
119 | 0 | void *t = talloc_new(NULL); |
120 | 0 | char *filename = talloc_asprintf(t, "%08d.%s", p->frame, |
121 | 0 | image_writer_file_ext(p->opts->opts)); |
122 | |
|
123 | 0 | if (p->dir && strlen(p->dir)) |
124 | 0 | filename = mp_path_join(t, p->dir, filename); |
125 | |
|
126 | 0 | MP_INFO(vo, "Saving %s\n", filename); |
127 | 0 | write_image(p->current, p->opts->opts, filename, vo->global, vo->log, true); |
128 | |
|
129 | 0 | talloc_free(t); |
130 | 0 | } |
131 | | |
132 | | static int query_format(struct vo *vo, int fmt) |
133 | 0 | { |
134 | 0 | if (mp_sws_supported_format(fmt)) |
135 | 0 | return 1; |
136 | 0 | return 0; |
137 | 0 | } |
138 | | |
139 | | static void uninit(struct vo *vo) |
140 | 0 | { |
141 | 0 | } |
142 | | |
143 | | static int preinit(struct vo *vo) |
144 | 0 | { |
145 | 0 | struct priv *p = vo->priv; |
146 | 0 | p->opts = mp_get_config_group(vo, vo->global, &vo_image_conf); |
147 | 0 | if (p->opts->outdir && !checked_mkdir(vo, p->opts->outdir)) |
148 | 0 | return -1; |
149 | 0 | return 0; |
150 | 0 | } |
151 | | |
152 | | static int control(struct vo *vo, uint32_t request, void *data) |
153 | 0 | { |
154 | 0 | return VO_NOTIMPL; |
155 | 0 | } |
156 | | |
157 | | const struct vo_driver video_out_image = |
158 | | { |
159 | | .description = "Write video frames to image files", |
160 | | .name = "image", |
161 | | .caps = VO_CAP_UNTIMED, |
162 | | .priv_size = sizeof(struct priv), |
163 | | .preinit = preinit, |
164 | | .query_format = query_format, |
165 | | .reconfig = reconfig, |
166 | | .control = control, |
167 | | .draw_frame = draw_frame, |
168 | | .flip_page = flip_page, |
169 | | .uninit = uninit, |
170 | | .global_opts = &vo_image_conf, |
171 | | }; |