FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
27 #include "libavutil/error.h"
28 
29 /* AVIO Writer */
30 
31 # define WRITER_NAME "aviowriter"
32 
33 typedef struct IOWriterContext {
34  const AVClass *class;
38 
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 
71  .name = WRITER_NAME,
72  .priv_size = sizeof(IOWriterContext),
76  .writer_w8 = io_w8
77 };
78 
80 {
82  int ret;
83 
84 
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) {
93  "Failed to open output '%s' with error: %s\n", output_filename, av_err2str(ret));
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 {
107  int ret;
108 
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 }
io_w8
static void io_w8(AVTextWriterContext *wctx, int b)
Definition: tw_avio.c:47
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:53
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
WRITER_NAME
#define WRITER_NAME
Definition: tw_avio.c:31
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:36
IOWriterContext
Definition: tw_avio.c:33
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:104
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
error.h
writer_w8
#define writer_w8(wctx_, b_)
Definition: tf_compact.c:33
iowriter_uninit
static av_cold void iowriter_uninit(AVTextWriterContext *wctx)
Definition: tw_avio.c:39
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
avtextwriter_context_open
int avtextwriter_context_open(AVTextWriterContext **pwctx, const AVTextWriter *writer)
Definition: avtextformat.c:603
AVTextWriter
Definition: avtextwriters.h:35
avtextwriter_create_file
int avtextwriter_create_file(AVTextWriterContext **pwctx, const char *output_filename)
Definition: tw_avio.c:79
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
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
avtextwriter_context_close
void avtextwriter_context_close(AVTextWriterContext **pwctx)
Definition: avtextformat.c:585
writer_put_str
#define writer_put_str(wctx_, str_)
Definition: tf_compact.c:34
ret
ret
Definition: filter_design.txt:187
io_printf
static void io_printf(AVTextWriterContext *wctx, const char *fmt,...)
Definition: tw_avio.c:59
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:70
IOWriterContext::avio_context
AVIOContext * avio_context
Definition: tw_avio.c:35
avio_closep
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:649
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
writer_printf
#define writer_printf(wctx_, fmt_,...)
Definition: tf_compact.c:35