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