FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/textformat/tw_avio.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 0 39 0.0%
Functions: 0 6 0.0%
Branches: 0 8 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) The FFmpeg developers
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <limits.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "avtextwriters.h"
26
27 #include "libavutil/error.h"
28
29 /* AVIO Writer */
30
31 # define WRITER_NAME "aviowriter"
32
33 typedef struct IOWriterContext {
34 const AVClass *class;
35 AVIOContext *avio_context;
36 int close_on_uninit;
37 } IOWriterContext;
38
39 static av_cold void iowriter_uninit(AVTextWriterContext *wctx)
40 {
41 IOWriterContext *ctx = wctx->priv;
42
43 if (ctx->close_on_uninit)
44 avio_closep(&ctx->avio_context);
45 }
46
47 static void io_w8(AVTextWriterContext *wctx, int b)
48 {
49 IOWriterContext *ctx = wctx->priv;
50 avio_w8(ctx->avio_context, b);
51 }
52
53 static void io_put_str(AVTextWriterContext *wctx, const char *str)
54 {
55 IOWriterContext *ctx = wctx->priv;
56 avio_write(ctx->avio_context, str, strlen(str));
57 }
58
59 static void io_printf(AVTextWriterContext *wctx, const char *fmt, ...)
60 {
61 IOWriterContext *ctx = wctx->priv;
62 va_list ap;
63
64 va_start(ap, fmt);
65 avio_vprintf(ctx->avio_context, fmt, ap);
66 va_end(ap);
67 }
68
69
70 const AVTextWriter avtextwriter_avio = {
71 .name = WRITER_NAME,
72 .priv_size = sizeof(IOWriterContext),
73 .uninit = iowriter_uninit,
74 .writer_put_str = io_put_str,
75 .writer_printf = io_printf,
76 .writer_w8 = io_w8
77 };
78
79 int avtextwriter_create_file(AVTextWriterContext **pwctx, const char *output_filename)
80 {
81 IOWriterContext *ctx;
82 int ret;
83
84
85 ret = avtextwriter_context_open(pwctx, &avtextwriter_avio);
86 if (ret < 0)
87 return ret;
88
89 ctx = (*pwctx)->priv;
90
91 if ((ret = avio_open(&ctx->avio_context, output_filename, AVIO_FLAG_WRITE)) < 0) {
92 av_log(ctx, AV_LOG_ERROR,
93 "Failed to open output '%s' with error: %s\n", output_filename, av_err2str(ret));
94 avtextwriter_context_close(pwctx);
95 return ret;
96 }
97
98 ctx->close_on_uninit = 1;
99
100 return ret;
101 }
102
103
104 int avtextwriter_create_avio(AVTextWriterContext **pwctx, AVIOContext *avio_ctx, int close_on_uninit)
105 {
106 IOWriterContext *ctx;
107 int ret;
108
109 ret = avtextwriter_context_open(pwctx, &avtextwriter_avio);
110 if (ret < 0)
111 return ret;
112
113 ctx = (*pwctx)->priv;
114 ctx->avio_context = avio_ctx;
115 ctx->close_on_uninit = close_on_uninit;
116
117 return ret;
118 }
119