| 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 | #include "libavutil/avassert.h" | ||
| 27 | |||
| 28 | #include "libavutil/error.h" | ||
| 29 | |||
| 30 | /* AVIO Writer */ | ||
| 31 | |||
| 32 | # define WRITER_NAME "aviowriter" | ||
| 33 | |||
| 34 | typedef struct IOWriterContext { | ||
| 35 | const AVClass *class; | ||
| 36 | AVIOContext *avio_context; | ||
| 37 | int close_on_uninit; | ||
| 38 | } IOWriterContext; | ||
| 39 | |||
| 40 | ✗ | static av_cold int iowriter_uninit(AVTextWriterContext *wctx) | |
| 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 | |||
| 50 | ✗ | static void io_w8(AVTextWriterContext *wctx, int b) | |
| 51 | { | ||
| 52 | ✗ | IOWriterContext *ctx = wctx->priv; | |
| 53 | ✗ | avio_w8(ctx->avio_context, b); | |
| 54 | ✗ | } | |
| 55 | |||
| 56 | ✗ | static 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 | |||
| 62 | ✗ | static 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 | |||
| 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_vprintf = io_vprintf, | ||
| 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 | ✗ | 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 | |||
| 89 | ✗ | ret = avtextwriter_context_open(pwctx, &avtextwriter_avio); | |
| 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) { | |
| 96 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
| 97 | ✗ | "Failed to open output '%s' with error: %s\n", output_filename, av_err2str(ret)); | |
| 98 | ✗ | avtextwriter_context_close(pwctx); | |
| 99 | ✗ | return ret; | |
| 100 | } | ||
| 101 | |||
| 102 | ✗ | ctx->close_on_uninit = 1; | |
| 103 | |||
| 104 | ✗ | return ret; | |
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | ✗ | int avtextwriter_create_avio(AVTextWriterContext **pwctx, AVIOContext *avio_ctx, int close_on_uninit) | |
| 109 | { | ||
| 110 | IOWriterContext *ctx; | ||
| 111 | int ret; | ||
| 112 | |||
| 113 | ✗ | av_assert0(avio_ctx); | |
| 114 | |||
| 115 | ✗ | ret = avtextwriter_context_open(pwctx, &avtextwriter_avio); | |
| 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 | } | ||
| 125 |