FFmpeg
Loading...
Searching...
No Matches
tw_avio.c
Go to the documentation of this file.
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#include "libavutil/avassert.h"
27
28#include "libavutil/error.h"
29
30/* AVIO Writer */
31
32# define WRITER_NAME "aviowriter"
33
39
41{
42 IOWriterContext *ctx = wctx->priv;
43 int ret = 0;
44
45 if (ctx->close_on_uninit)
46 ret = avio_closep(&ctx->avio_context);
47 return ret;
48}
49
50static void io_w8(AVTextWriterContext *wctx, int b)
51{
52 IOWriterContext *ctx = wctx->priv;
53 avio_w8(ctx->avio_context, b);
54}
55
56static void io_put_str(AVTextWriterContext *wctx, const char *str)
57{
58 IOWriterContext *ctx = wctx->priv;
59 avio_write(ctx->avio_context, (const unsigned char *)str, (int)strlen(str));
60}
61
62static void io_vprintf(AVTextWriterContext *wctx, const char *fmt, va_list vl)
63{
64 IOWriterContext *ctx = wctx->priv;
65
66 avio_vprintf(ctx->avio_context, fmt, vl);
67}
68
69
71 .name = WRITER_NAME,
72 .priv_size = sizeof(IOWriterContext),
75 .writer_vprintf = io_vprintf,
77};
78
80{
82 int ret;
83
84 if (!output_filename || !output_filename[0]) {
85 av_log(NULL, AV_LOG_ERROR, "The output_filename cannot be NULL or empty\n");
86 return AVERROR(EINVAL);
87 }
88
90 if (ret < 0)
91 return ret;
92
93 ctx = (*pwctx)->priv;
94
95 if ((ret = avio_open(&ctx->avio_context, output_filename, AVIO_FLAG_WRITE)) < 0) {
97 "Failed to open output '%s' with error: %s\n", output_filename, av_err2str(ret));
99 return ret;
100 }
101
102 ctx->close_on_uninit = 1;
103
104 return ret;
105}
106
107
108int avtextwriter_create_avio(AVTextWriterContext **pwctx, AVIOContext *avio_ctx, int close_on_uninit)
109{
111 int ret;
112
113 av_assert0(avio_ctx);
114
116 if (ret < 0)
117 return ret;
118
119 ctx = (*pwctx)->priv;
120 ctx->avio_context = avio_ctx;
121 ctx->close_on_uninit = close_on_uninit;
122
123 return ret;
124}
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition avio.c:565
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition avio.c:717
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
int avio_vprintf(AVIOContext *s, const char *fmt, va_list ap)
Writes a formatted string to the context taking a va_list.
Definition aviobuf.c:1194
#define AVIO_FLAG_WRITE
write-only
Definition avio.h:618
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int avtextwriter_context_open(AVTextWriterContext **pwctx, const AVTextWriter *writer)
int avtextwriter_context_close(AVTextWriterContext **pwctx)
#define NULL
Definition coverity.c:32
error code definitions
static const char * output_filename
Definition ffprobe.c:346
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define b
Definition input.c:43
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define av_cold
Definition attributes.h:117
Describe the class of an AVClass context structure.
Definition log.h:76
Bytestream IO Context.
Definition avio.h:160
void * priv
private data for use by the writer
AVIOContext * avio_context
Definition tw_avio.c:36
int close_on_uninit
Definition tw_avio.c:37
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static void writer_w8(AVTextFormatContext *wctx, int b)
Definition tf_internal.h:63
static void writer_put_str(AVTextFormatContext *wctx, const char *str)
Definition tf_internal.h:68
static av_cold int iowriter_uninit(AVTextWriterContext *wctx)
Definition tw_avio.c:40
static void io_w8(AVTextWriterContext *wctx, int b)
Definition tw_avio.c:50
int avtextwriter_create_avio(AVTextWriterContext **pwctx, AVIOContext *avio_ctx, int close_on_uninit)
Definition tw_avio.c:108
const AVTextWriter avtextwriter_avio
Definition tw_avio.c:70
static void io_put_str(AVTextWriterContext *wctx, const char *str)
Definition tw_avio.c:56
int avtextwriter_create_file(AVTextWriterContext **pwctx, const char *output_filename)
Definition tw_avio.c:79
#define WRITER_NAME
Definition tw_avio.c:32
static void io_vprintf(AVTextWriterContext *wctx, const char *fmt, va_list vl)
Definition tw_avio.c:62