FFmpeg
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/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avdevice.h"
25 
26 typedef struct CACAContext {
27  AVClass *class;
29  char *window_title;
31 
32  caca_canvas_t *canvas;
33  caca_display_t *display;
34  caca_dither_t *dither;
35 
37  char *charset, *color;
38  char *driver;
39 
40  char *list_dither;
42 } CACAContext;
43 
45 {
46  CACAContext *c = s->priv_data;
47 
49 
50  if (c->display) {
51  caca_free_display(c->display);
52  c->display = NULL;
53  }
54  if (c->dither) {
55  caca_free_dither(c->dither);
56  c->dither = NULL;
57  }
58  if (c->canvas) {
59  caca_free_canvas(c->canvas);
60  c->canvas = NULL;
61  }
62  return 0;
63 }
64 
65 static void list_drivers(CACAContext *c)
66 {
67  const char *const *drivers = caca_get_display_driver_list();
68  int i;
69 
70  av_log(c->ctx, AV_LOG_INFO, "Available drivers:\n");
71  for (i = 0; drivers[i]; i += 2)
72  av_log(c->ctx, AV_LOG_INFO, "%s: %s\n", drivers[i], drivers[i + 1]);
73 }
74 
75 #define DEFINE_LIST_DITHER(thing, thing_str) \
76 static void list_dither_## thing(CACAContext *c) \
77 { \
78  const char *const *thing = caca_get_dither_## thing ##_list(c->dither); \
79  int i; \
80  \
81  av_log(c->ctx, AV_LOG_INFO, "Available %s:\n", thing_str); \
82  for (i = 0; thing[i]; i += 2) \
83  av_log(c->ctx, AV_LOG_INFO, "%s: %s\n", thing[i], thing[i + 1]); \
84 }
85 
86 DEFINE_LIST_DITHER(color, "colors");
87 DEFINE_LIST_DITHER(charset, "charsets");
88 DEFINE_LIST_DITHER(algorithm, "algorithms");
89 DEFINE_LIST_DITHER(antialias, "antialias");
90 
92 {
93  CACAContext *c = s->priv_data;
94  AVStream *st = s->streams[0];
95  AVCodecParameters *encctx = st->codecpar;
96  int ret, bpp;
97 
98  c->ctx = s;
99  if (c->list_drivers) {
100  list_drivers(c);
101  return AVERROR_EXIT;
102  }
103  if (c->list_dither) {
104  if (!strcmp(c->list_dither, "colors")) {
105  list_dither_color(c);
106  } else if (!strcmp(c->list_dither, "charsets")) {
107  list_dither_charset(c);
108  } else if (!strcmp(c->list_dither, "algorithms")) {
109  list_dither_algorithm(c);
110  } else if (!strcmp(c->list_dither, "antialiases")) {
111  list_dither_antialias(c);
112  } else {
113  av_log(s, AV_LOG_ERROR,
114  "Invalid argument '%s', for 'list_dither' option\n"
115  "Argument must be one of 'algorithms, 'antialiases', 'charsets', 'colors'\n",
116  c->list_dither);
117  return AVERROR(EINVAL);
118  }
119  return AVERROR_EXIT;
120  }
121 
122  if ( s->nb_streams > 1
123  || encctx->codec_type != AVMEDIA_TYPE_VIDEO
124  || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
125  av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
126  return AVERROR(EINVAL);
127  }
128 
129  if (encctx->format != AV_PIX_FMT_RGB24) {
130  av_log(s, AV_LOG_ERROR,
131  "Unsupported pixel format '%s', choose rgb24\n",
132  av_get_pix_fmt_name(encctx->format));
133  return AVERROR(EINVAL);
134  }
135 
136  c->canvas = caca_create_canvas(c->window_width, c->window_height);
137  if (!c->canvas) {
138  ret = AVERROR(errno);
139  av_log(s, AV_LOG_ERROR, "Failed to create canvas\n");
140  goto fail;
141  }
142 
144  c->dither = caca_create_dither(bpp, encctx->width, encctx->height,
145  bpp / 8 * encctx->width,
146  0x0000ff, 0x00ff00, 0xff0000, 0);
147  if (!c->dither) {
148  ret = AVERROR(errno);
149  av_log(s, AV_LOG_ERROR, "Failed to create dither\n");
150  goto fail;
151  }
152 
153 #define CHECK_DITHER_OPT(opt) do { \
154  if (caca_set_dither_##opt(c->dither, c->opt) < 0) { \
155  ret = AVERROR(errno); \
156  av_log(s, AV_LOG_ERROR, "Failed to set value '%s' for option '%s'\n", \
157  c->opt, #opt); \
158  goto fail; \
159  } \
160 } while (0)
161 
166 
167  c->display = caca_create_display_with_driver(c->canvas, c->driver);
168  if (!c->display) {
169  ret = AVERROR(errno);
170  av_log(s, AV_LOG_ERROR, "Failed to create display\n");
171  list_drivers(c);
172  goto fail;
173  }
174 
175  if (!c->window_width || !c->window_height) {
176  c->window_width = caca_get_canvas_width(c->canvas);
177  c->window_height = caca_get_canvas_height(c->canvas);
178  }
179 
180  if (!c->window_title)
181  c->window_title = av_strdup(s->url);
182  caca_set_display_title(c->display, c->window_title);
183  caca_set_display_time(c->display, av_rescale_q(1, st->codec->time_base, AV_TIME_BASE_Q));
184 
185  return 0;
186 
187 fail:
189  return ret;
190 }
191 
193 {
194  CACAContext *c = s->priv_data;
195 
196  caca_dither_bitmap(c->canvas, 0, 0, c->window_width, c->window_height, c->dither, pkt->data);
197  caca_refresh_display(c->display);
198 
199  return 0;
200 }
201 
202 #define OFFSET(x) offsetof(CACAContext,x)
203 #define ENC AV_OPT_FLAG_ENCODING_PARAM
204 
205 static const AVOption options[] = {
206  { "window_size", "set window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL }, 0, 0, ENC},
207  { "window_title", "set window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, ENC },
208  { "driver", "set display driver", OFFSET(driver), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, ENC },
209  { "algorithm", "set dithering algorithm", OFFSET(algorithm), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
210  { "antialias", "set antialias method", OFFSET(antialias), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
211  { "charset", "set charset used to render output", OFFSET(charset), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
212  { "color", "set color used to render output", OFFSET(color), AV_OPT_TYPE_STRING, {.str = "default" }, 0, 0, ENC },
213  { "list_drivers", "list available drivers", OFFSET(list_drivers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, ENC },
214  { "list_dither", "list available dither options", OFFSET(list_dither), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, ENC, "list_dither" },
215  { "algorithms", NULL, 0, AV_OPT_TYPE_CONST, {.str = "algorithms"}, 0, 0, ENC, "list_dither" },
216  { "antialiases", NULL, 0, AV_OPT_TYPE_CONST, {.str = "antialiases"},0, 0, ENC, "list_dither" },
217  { "charsets", NULL, 0, AV_OPT_TYPE_CONST, {.str = "charsets"}, 0, 0, ENC, "list_dither" },
218  { "colors", NULL, 0, AV_OPT_TYPE_CONST, {.str = "colors"}, 0, 0, ENC, "list_dither" },
219  { NULL },
220 };
221 
222 static const AVClass caca_class = {
223  .class_name = "caca_outdev",
224  .item_name = av_default_item_name,
225  .option = options,
226  .version = LIBAVUTIL_VERSION_INT,
228 };
229 
231  .name = "caca",
232  .long_name = NULL_IF_CONFIG_SMALL("caca (color ASCII art) output device"),
233  .priv_data_size = sizeof(CACAContext),
234  .audio_codec = AV_CODEC_ID_NONE,
235  .video_codec = AV_CODEC_ID_RAWVIDEO,
239  .flags = AVFMT_NOFILE,
240  .priv_class = &caca_class,
241 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:689
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
AVOption.
Definition: opt.h:246
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
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:2474
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3953
caca_canvas_t * canvas
Definition: caca.c:32
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3945
Format I/O context.
Definition: avformat.h:1358
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:72
caca_dither_t * dither
Definition: caca.c:34
static int caca_write_trailer(AVFormatContext *s)
Definition: caca.c:44
int width
Video only.
Definition: avcodec.h:4019
AVOptions.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
uint8_t * data
Definition: avcodec.h:1477
#define av_log(a,...)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
Main libavdevice API header.
char * window_title
Definition: caca.c:29
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:260
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
char * url
input or output URL.
Definition: avformat.h:1454
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3949
int window_width
Definition: caca.c:30
#define fail()
Definition: checkasm.h:120
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1414
int list_drivers
Definition: caca.c:41
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
AVOutputFormat ff_caca_muxer
Definition: caca.c:230
caca_display_t * display
Definition: caca.c:33
const char * name
Definition: avformat.h:505
char * algorithm
Definition: caca.c:36
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVOption options[]
Definition: caca.c:205
int window_height
Definition: caca.c:30
#define CHECK_DITHER_OPT(opt)
char * color
Definition: caca.c:37
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
char * antialias
Definition: caca.c:36
Stream structure.
Definition: avformat.h:881
#define DEFINE_LIST_DITHER(thing, thing_str)
Definition: caca.c:75
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
#define ENC
Definition: caca.c:203
char * charset
Definition: caca.c:37
Describe the class of an AVClass context structure.
Definition: log.h:67
offset must point to two consecutive integers
Definition: opt.h:233
static int caca_write_header(AVFormatContext *s)
Definition: caca.c:91
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVFormatContext * ctx
Definition: caca.c:28
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:463
static int caca_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: caca.c:192
#define OFFSET(x)
Definition: caca.c:202
void * priv_data
Format private data.
Definition: avformat.h:1386
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
char * list_dither
Definition: caca.c:40
#define av_freep(p)
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
char * driver
Definition: caca.c:38
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:2438
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:910
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
static const AVClass caca_class
Definition: caca.c:222
This structure stores compressed data.
Definition: avcodec.h:1454