caca.c 8.7 KB

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