xref: /third_party/ffmpeg/libavdevice/caca.c (revision cabdff1a)
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/opt.h"
23#include "libavutil/pixdesc.h"
24#include "avdevice.h"
25
26typedef struct CACAContext {
27    AVClass         *class;
28    AVFormatContext *ctx;
29    char            *window_title;
30    int             window_width,  window_height;
31
32    caca_canvas_t   *canvas;
33    caca_display_t  *display;
34    caca_dither_t   *dither;
35
36    char            *algorithm, *antialias;
37    char            *charset, *color;
38    char            *driver;
39
40    char            *list_dither;
41    int             list_drivers;
42} CACAContext;
43
44static void caca_deinit(AVFormatContext *s)
45{
46    CACAContext *c = s->priv_data;
47
48    if (c->display) {
49        caca_free_display(c->display);
50        c->display = NULL;
51    }
52    if (c->dither) {
53        caca_free_dither(c->dither);
54        c->dither = NULL;
55    }
56    if (c->canvas) {
57        caca_free_canvas(c->canvas);
58        c->canvas = NULL;
59    }
60}
61
62static void list_drivers(CACAContext *c)
63{
64    const char *const *drivers = caca_get_display_driver_list();
65    int i;
66
67    av_log(c->ctx, AV_LOG_INFO, "Available drivers:\n");
68    for (i = 0; drivers[i]; i += 2)
69        av_log(c->ctx, AV_LOG_INFO, "%s: %s\n", drivers[i], drivers[i + 1]);
70}
71
72#define DEFINE_LIST_DITHER(thing, thing_str)                                 \
73static void list_dither_## thing(CACAContext *c)                             \
74{                                                                            \
75    const char *const *thing = caca_get_dither_## thing ##_list(c->dither);  \
76    int i;                                                                   \
77                                                                             \
78    av_log(c->ctx, AV_LOG_INFO, "Available %s:\n", thing_str);               \
79    for (i = 0; thing[i]; i += 2)                                            \
80        av_log(c->ctx, AV_LOG_INFO, "%s: %s\n", thing[i], thing[i + 1]);     \
81}
82
83DEFINE_LIST_DITHER(color, "colors");
84DEFINE_LIST_DITHER(charset, "charsets");
85DEFINE_LIST_DITHER(algorithm, "algorithms");
86DEFINE_LIST_DITHER(antialias, "antialias");
87
88static int caca_write_header(AVFormatContext *s)
89{
90    CACAContext *c = s->priv_data;
91    AVStream *st = s->streams[0];
92    AVCodecParameters *encctx = st->codecpar;
93    int ret, bpp;
94
95    c->ctx = s;
96    if (c->list_drivers) {
97        list_drivers(c);
98        return AVERROR_EXIT;
99    }
100    if (c->list_dither) {
101        if (!strcmp(c->list_dither, "colors")) {
102            list_dither_color(c);
103        } else if (!strcmp(c->list_dither, "charsets")) {
104            list_dither_charset(c);
105        } else if (!strcmp(c->list_dither, "algorithms")) {
106            list_dither_algorithm(c);
107        } else if (!strcmp(c->list_dither, "antialiases")) {
108            list_dither_antialias(c);
109        } else {
110            av_log(s, AV_LOG_ERROR,
111                   "Invalid argument '%s', for 'list_dither' option\n"
112                   "Argument must be one of 'algorithms, 'antialiases', 'charsets', 'colors'\n",
113                   c->list_dither);
114            return AVERROR(EINVAL);
115        }
116        return AVERROR_EXIT;
117    }
118
119    if (   s->nb_streams > 1
120        || encctx->codec_type != AVMEDIA_TYPE_VIDEO
121        || encctx->codec_id   != AV_CODEC_ID_RAWVIDEO) {
122        av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
123        return AVERROR(EINVAL);
124    }
125
126    if (encctx->format != AV_PIX_FMT_RGB24) {
127        av_log(s, AV_LOG_ERROR,
128               "Unsupported pixel format '%s', choose rgb24\n",
129               av_get_pix_fmt_name(encctx->format));
130        return AVERROR(EINVAL);
131    }
132
133    c->canvas = caca_create_canvas(c->window_width, c->window_height);
134    if (!c->canvas) {
135        ret = AVERROR(errno);
136        av_log(s, AV_LOG_ERROR, "Failed to create canvas\n");
137        return ret;
138    }
139
140    bpp = av_get_bits_per_pixel(av_pix_fmt_desc_get(encctx->format));
141    c->dither = caca_create_dither(bpp, encctx->width, encctx->height,
142                                   bpp / 8 * encctx->width,
143                                   0x0000ff, 0x00ff00, 0xff0000, 0);
144    if (!c->dither) {
145        ret =  AVERROR(errno);
146        av_log(s, AV_LOG_ERROR, "Failed to create dither\n");
147        return ret;
148    }
149
150#define CHECK_DITHER_OPT(opt) do {                                              \
151    if (caca_set_dither_##opt(c->dither, c->opt) < 0)  {                        \
152        ret = AVERROR(errno);                                                   \
153        av_log(s, AV_LOG_ERROR, "Failed to set value '%s' for option '%s'\n",   \
154               c->opt, #opt);                                                   \
155        return ret;                                                             \
156    }                                                                           \
157} while (0)
158
159    CHECK_DITHER_OPT(algorithm);
160    CHECK_DITHER_OPT(antialias);
161    CHECK_DITHER_OPT(charset);
162    CHECK_DITHER_OPT(color);
163
164    c->display = caca_create_display_with_driver(c->canvas, c->driver);
165    if (!c->display) {
166        ret = AVERROR(errno);
167        av_log(s, AV_LOG_ERROR, "Failed to create display\n");
168        list_drivers(c);
169        return ret;
170    }
171
172    if (!c->window_width || !c->window_height) {
173        c->window_width  = caca_get_canvas_width(c->canvas);
174        c->window_height = caca_get_canvas_height(c->canvas);
175    }
176
177    if (!c->window_title)
178        c->window_title = av_strdup(s->url);
179    caca_set_display_title(c->display, c->window_title);
180    caca_set_display_time(c->display, av_rescale_q(1, st->time_base, AV_TIME_BASE_Q));
181
182    return 0;
183}
184
185static int caca_write_packet(AVFormatContext *s, AVPacket *pkt)
186{
187    CACAContext *c = s->priv_data;
188
189    caca_dither_bitmap(c->canvas, 0, 0, c->window_width, c->window_height, c->dither, pkt->data);
190    caca_refresh_display(c->display);
191
192    return 0;
193}
194
195#define OFFSET(x) offsetof(CACAContext,x)
196#define ENC AV_OPT_FLAG_ENCODING_PARAM
197
198static const AVOption options[] = {
199    { "window_size",  "set window forced size",  OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL }, 0, 0, ENC},
200    { "window_title", "set window title",        OFFSET(window_title), AV_OPT_TYPE_STRING,     {.str = NULL }, 0, 0, ENC },
201    { "driver",       "set display driver",      OFFSET(driver),    AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, ENC },
202    { "algorithm",    "set dithering algorithm", OFFSET(algorithm), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
203    { "antialias",    "set antialias method",    OFFSET(antialias), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
204    { "charset",      "set charset used to render output", OFFSET(charset), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
205    { "color",        "set color used to render output",   OFFSET(color),   AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
206    { "list_drivers", "list available drivers",  OFFSET(list_drivers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, ENC },
207    { "list_dither", "list available dither options", OFFSET(list_dither), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, ENC, "list_dither" },
208    { "algorithms",   NULL, 0, AV_OPT_TYPE_CONST, {.str = "algorithms"}, 0, 0, ENC, "list_dither" },
209    { "antialiases",  NULL, 0, AV_OPT_TYPE_CONST, {.str = "antialiases"},0, 0, ENC, "list_dither" },
210    { "charsets",     NULL, 0, AV_OPT_TYPE_CONST, {.str = "charsets"},   0, 0, ENC, "list_dither" },
211    { "colors",       NULL, 0, AV_OPT_TYPE_CONST, {.str = "colors"},     0, 0, ENC, "list_dither" },
212    { NULL },
213};
214
215static const AVClass caca_class = {
216    .class_name = "caca outdev",
217    .item_name  = av_default_item_name,
218    .option     = options,
219    .version    = LIBAVUTIL_VERSION_INT,
220    .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
221};
222
223const AVOutputFormat ff_caca_muxer = {
224    .name           = "caca",
225    .long_name      = NULL_IF_CONFIG_SMALL("caca (color ASCII art) output device"),
226    .priv_data_size = sizeof(CACAContext),
227    .audio_codec    = AV_CODEC_ID_NONE,
228    .video_codec    = AV_CODEC_ID_RAWVIDEO,
229    .write_header   = caca_write_header,
230    .write_packet   = caca_write_packet,
231    .deinit         = caca_deinit,
232    .flags          = AVFMT_NOFILE,
233    .priv_class     = &caca_class,
234};
235