FFmpeg
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 
24 #include "avtextwriters.h"
25 #include "libavutil/opt.h"
26 
27 /* AVIO Writer */
28 
29 # define WRITER_NAME "aviowriter"
30 
31 typedef struct IOWriterContext {
32  const AVClass *class;
36 
37 static const char *iowriter_get_name(void *ctx)
38 {
39  return WRITER_NAME;
40 }
41 
42 static const AVClass iowriter_class = {
44  .item_name = iowriter_get_name,
45 };
46 
48 {
49  IOWriterContext *ctx = wctx->priv;
50 
51  if (ctx->close_on_uninit && ctx->avio_context) {
52  avio_flush(ctx->avio_context);
53  avio_close(ctx->avio_context);
54  }
55 }
56 
57 static void io_w8(AVTextWriterContext *wctx, int b)
58 {
59  IOWriterContext *ctx = wctx->priv;
60  avio_w8(ctx->avio_context, b);
61 }
62 
63 static void io_put_str(AVTextWriterContext *wctx, const char *str)
64 {
65  IOWriterContext *ctx = wctx->priv;
66  avio_write(ctx->avio_context, str, strlen(str));
67 }
68 
69 static void io_printf(AVTextWriterContext *wctx, const char *fmt, ...)
70 {
71  IOWriterContext *ctx = wctx->priv;
72  va_list ap;
73 
74  va_start(ap, fmt);
75  avio_vprintf(ctx->avio_context, fmt, ap);
76  va_end(ap);
77 }
78 
79 
81  .name = WRITER_NAME,
82  .priv_size = sizeof(IOWriterContext),
84  .priv_class = &iowriter_class,
87  .writer_w8 = io_w8
88 };
89 
90 int avtextwriter_create_file(AVTextWriterContext **pwctx, const char *output_filename, int close_on_uninit)
91 {
93  int ret;
94 
95 
97  if (ret < 0)
98  return ret;
99 
100  ctx = (*pwctx)->priv;
101 
102  if ((ret = avio_open(&ctx->avio_context, output_filename, AVIO_FLAG_WRITE)) < 0) {
104  "Failed to open output '%s' with error: %s\n", output_filename, av_err2str(ret));
106  return ret;
107  }
108 
109  ctx->close_on_uninit = close_on_uninit;
110 
111  return ret;
112 }
113 
114 
115 int avtextwriter_create_avio(AVTextWriterContext **pwctx, AVIOContext *avio_ctx, int close_on_uninit)
116 {
118  int ret;
119 
121  if (ret < 0)
122  return ret;
123 
124  ctx = (*pwctx)->priv;
125  ctx->avio_context = avio_ctx;
126  ctx->close_on_uninit = close_on_uninit;
127 
128  return ret;
129 }
opt.h
io_w8
static void io_w8(AVTextWriterContext *wctx, int b)
Definition: tw_avio.c:57
b
#define b
Definition: input.c:42
avio_open
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:497
AVTextWriterContext
Definition: avtextwriters.h:47
io_put_str
static void io_put_str(AVTextWriterContext *wctx, const char *str)
Definition: tw_avio.c:63
AVTextWriterContext::priv
void * priv
private data for use by the writer
Definition: avtextwriters.h:51
output_filename
static const char * output_filename
Definition: ffprobe.c:329
iowriter_get_name
static const char * iowriter_get_name(void *ctx)
Definition: tw_avio.c:37
WRITER_NAME
#define WRITER_NAME
Definition: tw_avio.c:29
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
IOWriterContext::close_on_uninit
int close_on_uninit
Definition: tw_avio.c:34
avtextwriter_context_close
int avtextwriter_context_close(AVTextWriterContext **pwctx)
Definition: avtextformat.c:586
avtextwriter_create_file
int avtextwriter_create_file(AVTextWriterContext **pwctx, const char *output_filename, int close_on_uninit)
Definition: tw_avio.c:90
IOWriterContext
Definition: tw_avio.c:31
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:618
ctx
AVFormatContext * ctx
Definition: movenc.c:49
limits.h
avtextwriter_create_avio
int avtextwriter_create_avio(AVTextWriterContext **pwctx, AVIOContext *avio_ctx, int close_on_uninit)
Definition: tw_avio.c:115
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:223
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
AVTextWriter::name
const char * name
Definition: avtextwriters.h:38
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:179
writer_w8
#define writer_w8(wctx_, b_)
Definition: tf_compact.c:36
iowriter_uninit
static av_cold void iowriter_uninit(AVTextWriterContext *wctx)
Definition: tw_avio.c:47
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
avtextwriter_context_open
int avtextwriter_context_open(AVTextWriterContext **pwctx, const AVTextWriter *writer)
Definition: avtextformat.c:604
AVTextWriter
Definition: avtextwriters.h:35
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
iowriter_class
static const AVClass iowriter_class
Definition: tw_avio.c:42
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
writer_put_str
#define writer_put_str(wctx_, str_)
Definition: tf_compact.c:37
ret
ret
Definition: filter_design.txt:187
io_printf
static void io_printf(AVTextWriterContext *wctx, const char *fmt,...)
Definition: tw_avio.c:69
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
avio_vprintf
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:1191
avtextwriters.h
avtextwriter_avio
const AVTextWriter avtextwriter_avio
Definition: tw_avio.c:80
IOWriterContext::avio_context
AVIOContext * avio_context
Definition: tw_avio.c:33
avio_close
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: avio.c:616
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
writer_printf
#define writer_printf(wctx_, fmt_,...)
Definition: tf_compact.c:38