FFmpeg
Loading...
Searching...
No Matches
caca.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Paul B Mahol
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 <caca.h>
22#include "libavutil/mem.h"
23#include "libavutil/opt.h"
24#include "libavutil/pixdesc.h"
25#include "libavformat/mux.h"
26#include "avdevice.h"
27
28enum {
31 LIST_CHARSETS = 1 << 2,
32 LIST_COLORS = 1 << 3,
33};
34
35typedef struct CACAContext {
36 AVClass *class;
40
41 caca_canvas_t *canvas;
42 caca_display_t *display;
43 caca_dither_t *dither;
44
46 char *charset, *color;
47 char *driver;
48
52
54{
55 CACAContext *c = s->priv_data;
56
57 if (c->display) {
58 caca_free_display(c->display);
59 c->display = NULL;
60 }
61 if (c->dither) {
62 caca_free_dither(c->dither);
63 c->dither = NULL;
64 }
65 if (c->canvas) {
66 caca_free_canvas(c->canvas);
67 c->canvas = NULL;
68 }
69}
70
72{
73 const char *const *drivers = caca_get_display_driver_list();
74 int i;
75
76 av_log(c->ctx, AV_LOG_INFO, "Available drivers:\n");
77 for (i = 0; drivers[i]; i += 2)
78 av_log(c->ctx, AV_LOG_INFO, "%s: %s\n", drivers[i], drivers[i + 1]);
79}
80
81#define DEFINE_LIST_DITHER(thing, thing_str) \
82static void list_dither_## thing(CACAContext *c) \
83{ \
84 const char *const *thing = caca_get_dither_## thing ##_list(c->dither); \
85 int i; \
86 \
87 av_log(c->ctx, AV_LOG_INFO, "Available %s:\n", thing_str); \
88 for (i = 0; thing[i]; i += 2) \
89 av_log(c->ctx, AV_LOG_INFO, "%s: %s\n", thing[i], thing[i + 1]); \
90}
91
93DEFINE_LIST_DITHER(charset, "charsets");
94DEFINE_LIST_DITHER(algorithm, "algorithms");
95DEFINE_LIST_DITHER(antialias, "antialias");
96
98{
99 CACAContext *c = s->priv_data;
100 AVStream *st = s->streams[0];
101 AVCodecParameters *encctx = st->codecpar;
102 int ret, bpp;
103
104 c->ctx = s;
105 if (c->list_drivers) {
107 return AVERROR_EXIT;
108 }
109 if (c->list_dither) {
110 if (c->list_dither & LIST_COLORS)
111 list_dither_color(c);
112 if (c->list_dither & LIST_CHARSETS)
113 list_dither_charset(c);
114 if (c->list_dither & LIST_ALGORITHMS)
115 list_dither_algorithm(c);
116 if (c->list_dither & LIST_ANTIALIASES)
117 list_dither_antialias(c);
118 return AVERROR_EXIT;
119 }
120
121 if ( s->nb_streams > 1
122 || encctx->codec_type != AVMEDIA_TYPE_VIDEO
123 || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
124 av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
125 return AVERROR(EINVAL);
126 }
127
128 if (encctx->format != AV_PIX_FMT_RGB24) {
130 "Unsupported pixel format '%s', choose rgb24\n",
131 av_get_pix_fmt_name(encctx->format));
132 return AVERROR(EINVAL);
133 }
134
135 c->canvas = caca_create_canvas(c->window_width, c->window_height);
136 if (!c->canvas) {
137 ret = AVERROR(errno);
138 av_log(s, AV_LOG_ERROR, "Failed to create canvas\n");
139 return ret;
140 }
141
143 c->dither = caca_create_dither(bpp, encctx->width, encctx->height,
144 bpp / 8 * encctx->width,
145 0x0000ff, 0x00ff00, 0xff0000, 0);
146 if (!c->dither) {
147 ret = AVERROR(errno);
148 av_log(s, AV_LOG_ERROR, "Failed to create dither\n");
149 return ret;
150 }
151
152#define CHECK_DITHER_OPT(opt) do { \
153 if (caca_set_dither_##opt(c->dither, c->opt) < 0) { \
154 ret = AVERROR(errno); \
155 av_log(s, AV_LOG_ERROR, "Failed to set value '%s' for option '%s'\n", \
156 c->opt, #opt); \
157 return ret; \
158 } \
159} while (0)
160
161 CHECK_DITHER_OPT(algorithm);
162 CHECK_DITHER_OPT(antialias);
163 CHECK_DITHER_OPT(charset);
165
166 c->display = caca_create_display_with_driver(c->canvas, c->driver);
167 if (!c->display) {
168 ret = AVERROR(errno);
169 av_log(s, AV_LOG_ERROR, "Failed to create display\n");
171 return ret;
172 }
173
174 if (!c->window_width || !c->window_height) {
175 c->window_width = caca_get_canvas_width(c->canvas);
176 c->window_height = caca_get_canvas_height(c->canvas);
177 }
178
179 if (!c->window_title)
180 c->window_title = av_strdup(s->url);
181 caca_set_display_title(c->display, c->window_title);
182 caca_set_display_time(c->display, av_rescale_q(1, st->time_base, AV_TIME_BASE_Q));
183
184 return 0;
185}
186
188{
189 CACAContext *c = s->priv_data;
190
191 caca_dither_bitmap(c->canvas, 0, 0, c->window_width, c->window_height, c->dither, pkt->data);
192 caca_refresh_display(c->display);
193
194 return 0;
195}
196
197#define OFFSET(x) offsetof(CACAContext,x)
198#define ENC AV_OPT_FLAG_ENCODING_PARAM
199
200static const AVOption options[] = {
201 { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL }, 0, 0, ENC},
202 { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, ENC },
203 { "driver", "set display driver", OFFSET(driver), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, ENC },
204 { "algorithm", "set dithering algorithm", OFFSET(algorithm), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
205 { "antialias", "set antialias method", OFFSET(antialias), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
206 { "charset", "set charset used to render output", OFFSET(charset), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
207 { "color", "set color used to render output", OFFSET(color), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
208 { "list_drivers", "list available drivers", OFFSET(list_drivers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, ENC },
209 { "list_dither", "list available dither options", OFFSET(list_dither), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, ENC, .unit = "list_dither" },
210 { "algorithms", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = LIST_ALGORITHMS }, 0, 0, ENC, .unit = "list_dither" },
211 { "antialiases", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = LIST_ANTIALIASES }, 0, 0, ENC, .unit = "list_dither" },
212 { "charsets", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = LIST_CHARSETS }, 0, 0, ENC, .unit = "list_dither" },
213 { "colors", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = LIST_COLORS }, 0, 0, ENC, .unit = "list_dither" },
214 { NULL },
215};
216
217static const AVClass caca_class = {
218 .class_name = "caca outdev",
219 .item_name = av_default_item_name,
220 .option = options,
221 .version = LIBAVUTIL_VERSION_INT,
223};
224
226 .p.name = "caca",
227 .p.long_name = NULL_IF_CONFIG_SMALL("caca (color ASCII art) output device"),
228 .priv_data_size = sizeof(CACAContext),
229 .p.audio_codec = AV_CODEC_ID_NONE,
230 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
231 .write_header = caca_write_header,
232 .write_packet = caca_write_packet,
233 .deinit = caca_deinit,
234 .p.flags = AVFMT_NOFILE,
235 .p.priv_class = &caca_class,
236};
const FFOutputFormat ff_caca_muxer
Definition caca.c:225
Main libavdevice API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:488
static void list_drivers(CACAContext *c)
Definition caca.c:71
static void caca_deinit(AVFormatContext *s)
Definition caca.c:53
static const AVClass caca_class
Definition caca.c:217
#define DEFINE_LIST_DITHER(thing, thing_str)
Definition caca.c:81
#define CHECK_DITHER_OPT(opt)
static int caca_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition caca.c:187
#define ENC
Definition caca.c:198
static int caca_write_header(AVFormatContext *s)
Definition caca.c:97
#define OFFSET(x)
Definition caca.c:197
@ LIST_COLORS
Definition caca.c:32
@ LIST_CHARSETS
Definition caca.c:31
@ LIST_ALGORITHMS
Definition caca.c:29
@ LIST_ANTIALIASES
Definition caca.c:30
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static const char * window_title
Definition ffplay.c:310
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT
Definition log.h:41
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
AVOptions.
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition pixdesc.c:3412
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
char * antialias
Definition caca.c:45
char * window_title
Definition caca.c:38
char * charset
Definition caca.c:46
int window_width
Definition caca.c:39
caca_dither_t * dither
Definition caca.c:43
caca_display_t * display
Definition caca.c:42
int list_drivers
Definition caca.c:50
AVFormatContext * ctx
Definition caca.c:37
char * driver
Definition caca.c:47
char * algorithm
Definition caca.c:45
int list_dither
Definition caca.c:49
caca_canvas_t * canvas
Definition caca.c:41
char * color
Definition caca.c:46
int window_height
Definition caca.c:39
#define av_log(a,...)
static double c[64]