GCC Code Coverage Report


Directory: src/gate/
File: src/gate/io/videosources_writer.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 90 103 87.4%
Functions: 9 9 100.0%
Branches: 19 32 59.4%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28 #include "gate/io/videosources.h"
29 #include "gate/results.h"
30 #include "gate/files.h"
31 #include "gate/graphics/pngimages.h"
32 #include "gate/graphics/jpegimages.h"
33
34 typedef struct gate_videowriter_impl_class
35 {
36 GATE_INTERFACE_VTBL(gate_video_writer) const* vtbl;
37
38 gate_atomic_int_t ref_counter;
39 gate_enumint_t filetype;
40 gate_video_format_t format;
41 gate_string_t path;
42 gate_string_t file_prefix;
43 gate_string_t file_extension;
44 gate_filestream_t* stream;
45 gate_timecounter_t start_time;
46 gate_timecounter_t last_frame_time;
47 gate_uint64_t frame_id;
48 } gate_videowriter_impl_t;
49
50 2 static char const* videowriter_get_interface_name(void* thisptr)
51 {
52 GATE_UNUSED_ARG(thisptr);
53 2 return GATE_INTERFACE_NAME_VIDEO_WRITER;
54 }
55 4 static void videowriter_release(void* thisptr)
56 {
57 4 gate_videowriter_impl_t* const self = (gate_videowriter_impl_t*)thisptr;
58
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
4 if (gate_atomic_int_dec(&self->ref_counter) == 0)
59 {
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (self->stream)
61 {
62 ✗ gate_object_release(self->stream);
63 }
64 2 gate_string_release(&self->path);
65 2 gate_string_release(&self->file_prefix);
66 2 gate_string_release(&self->file_extension);
67 }
68 4 }
69 2 static int videowriter_retain(void* thisptr)
70 {
71 2 gate_videowriter_impl_t* const self = (gate_videowriter_impl_t*)thisptr;
72 2 return gate_atomic_int_inc(&self->ref_counter);
73 }
74
75 6 static gate_string_t* generate_next_frame_file_name(gate_videowriter_impl_t* self, gate_string_t* file_name)
76 {
77 6 gate_string_t* ret = NULL;
78 6 gate_uint64_t frame_id = ++self->frame_id;
79 gate_strbuilder_t builder;
80
81 6 gate_strbuilder_create(&builder, gate_string_length(&self->path) + 32);
82 6 gate_strbuilder_append_string(&builder, &self->path);
83 6 gate_strbuilder_append_string(&builder, &self->file_prefix);
84 6 gate_strbuilder_append_cstr(&builder, "__");
85 6 gate_strbuilder_append_uint64(&builder, frame_id);
86 6 gate_strbuilder_append_cstr(&builder, "-");
87 6 gate_strbuilder_append_uint64(&builder, 0);
88 6 gate_strbuilder_append_string(&builder, &self->file_extension);
89
90 6 ret = gate_strbuilder_to_string(&builder, file_name);
91 6 gate_strbuilder_release(&builder);
92 6 return ret;
93 }
94
95 3 static gate_result_t write_png_frame(gate_videowriter_impl_t* self, gate_video_frame_t const* frame)
96 {
97 3 gate_result_t ret = GATE_RESULT_FAILED;
98 3 gate_string_t file_path = GATE_STRING_INIT_EMPTY;
99 3 gate_filestream_t* stream = NULL;
100
101 do
102 {
103
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (NULL == generate_next_frame_file_name(self, &file_path))
104 {
105 ✗ ret = GATE_RESULT_OUTOFMEMORY;
106 ✗ break;
107 }
108
109 3 ret = gate_file_openstream(&file_path, GATE_STREAM_OPEN_WRITE, &stream);
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
111
112 3 ret = gate_pngimage_save(&frame->image, (gate_stream_t*)stream, 0);
113 } while (0);
114
115 3 gate_string_release(&file_path);
116
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (stream)
117 {
118 3 gate_object_release(stream);
119 }
120
121 3 return ret;
122 }
123
124 3 static gate_result_t write_jpeg_frame(gate_videowriter_impl_t* self, gate_video_frame_t const* frame)
125 {
126 3 gate_result_t ret = GATE_RESULT_FAILED;
127 3 gate_string_t file_path = GATE_STRING_INIT_EMPTY;
128 3 gate_filestream_t* stream = NULL;
129
130 do
131 {
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (NULL == generate_next_frame_file_name(self, &file_path))
133 {
134 ✗ ret = GATE_RESULT_OUTOFMEMORY;
135 ✗ break;
136 }
137
138 3 ret = gate_file_openstream(&file_path, GATE_STREAM_OPEN_WRITE, &stream);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
140
141 3 ret = gate_jpegimage_save(&frame->image, (gate_stream_t*)stream, 0);
142 } while (0);
143
144 3 gate_string_release(&file_path);
145
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (stream)
146 {
147 3 gate_object_release(stream);
148 }
149
150 3 return ret;
151 }
152
153 6 static gate_result_t videowriter_write(void* thisptr, gate_video_frame_t const* frame)
154 {
155 6 gate_videowriter_impl_t* const self = (gate_videowriter_impl_t*)thisptr;
156
2/3
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 switch (self->filetype)
157 {
158 3 case GATE_VIDEO_FILEWRITER_TYPE_PNG:
159 {
160 3 return write_png_frame(self, frame);
161 }
162 3 case GATE_VIDEO_FILEWRITER_TYPE_JPEG:
163 {
164 3 return write_jpeg_frame(self, frame);
165 }
166 ✗ default:
167 {
168 ✗ return GATE_RESULT_NOTSUPPORTED;
169 }
170 }
171 }
172
173 2 static GATE_INTERFACE_VTBL(gate_video_writer) const* init_videowriter_vtbl()
174 {
175 static GATE_INTERFACE_VTBL(gate_video_writer) global_vtbl;
176
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (global_vtbl.get_interface_name == NULL)
177 {
178 GATE_INTERFACE_VTBL(gate_video_writer) const local_vtbl =
179 {
180 &videowriter_get_interface_name,
181 &videowriter_release,
182 &videowriter_retain,
183 &videowriter_write
184 };
185 1 global_vtbl = local_vtbl;
186 }
187 2 return &global_vtbl;
188 }
189
190
191 2 gate_result_t gate_video_filewriter_create(
192 gate_enumint_t filetype,
193 gate_string_t const* path,
194 gate_video_format_t const* ptr_format,
195 gate_video_writer_t** ptr_writer)
196 {
197 static gate_string_t const file_ext_png = GATE_STRING_INIT_STATIC(".png");
198 static gate_string_t const file_ext_jpg = GATE_STRING_INIT_STATIC(".jpg");
199 2 gate_result_t ret = GATE_RESULT_FAILED;
200 2 gate_videowriter_impl_t* impl = NULL;
201 do
202 {
203 2 impl = (gate_videowriter_impl_t*)gate_mem_alloc(sizeof(gate_videowriter_impl_t));
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (impl == NULL)
205 {
206 ✗ ret = GATE_RESULT_OUTOFMEMORY;
207 ✗ break;
208 }
209 2 gate_mem_clear(impl, sizeof(gate_videowriter_impl_t));
210 2 impl->vtbl = init_videowriter_vtbl();
211 2 gate_atomic_int_init(&impl->ref_counter, 1);
212
213 2 gate_mem_copy(&impl->format, ptr_format, sizeof(impl->format));
214 2 impl->filetype = filetype;
215
216
2/3
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 switch (filetype)
217 {
218 1 case GATE_VIDEO_FILEWRITER_TYPE_PNG:
219 {
220 1 gate_file_split_path(path, &impl->path, &impl->file_prefix);
221 1 gate_string_duplicate(&impl->file_extension, &file_ext_png);
222 1 ret = GATE_RESULT_OK;
223 1 break;
224 }
225 1 case GATE_VIDEO_FILEWRITER_TYPE_JPEG:
226 {
227 1 gate_file_split_path(path, &impl->path, &impl->file_prefix);
228 1 gate_string_duplicate(&impl->file_extension, &file_ext_jpg);
229 1 ret = GATE_RESULT_OK;
230 1 break;
231 }
232 ✗ default:
233 {
234 ✗ ret = GATE_RESULT_NOTSUPPORTED;
235 ✗ break;
236 }
237 }
238
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
240
241
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptr_writer)
242 {
243 2 *ptr_writer = (gate_video_writer_t*)impl;
244 2 impl = NULL;
245 }
246 2 ret = GATE_RESULT_OK;
247 } while (0);
248
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (impl)
250 {
251 ✗ videowriter_release(impl);
252 }
253 2 return ret;
254 }
255