caca.c 8.8 KB

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