caca.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "libavformat/mux.h"
  24. #include "avdevice.h"
  25. typedef struct CACAContext {
  26. AVClass *class;
  27. AVFormatContext *ctx;
  28. char *window_title;
  29. int window_width, window_height;
  30. caca_canvas_t *canvas;
  31. caca_display_t *display;
  32. caca_dither_t *dither;
  33. char *algorithm, *antialias;
  34. char *charset, *color;
  35. char *driver;
  36. char *list_dither;
  37. int list_drivers;
  38. } CACAContext;
  39. static void caca_deinit(AVFormatContext *s)
  40. {
  41. CACAContext *c = s->priv_data;
  42. if (c->display) {
  43. caca_free_display(c->display);
  44. c->display = NULL;
  45. }
  46. if (c->dither) {
  47. caca_free_dither(c->dither);
  48. c->dither = NULL;
  49. }
  50. if (c->canvas) {
  51. caca_free_canvas(c->canvas);
  52. c->canvas = NULL;
  53. }
  54. }
  55. static void list_drivers(CACAContext *c)
  56. {
  57. const char *const *drivers = caca_get_display_driver_list();
  58. int i;
  59. av_log(c->ctx, AV_LOG_INFO, "Available drivers:\n");
  60. for (i = 0; drivers[i]; i += 2)
  61. av_log(c->ctx, AV_LOG_INFO, "%s: %s\n", drivers[i], drivers[i + 1]);
  62. }
  63. #define DEFINE_LIST_DITHER(thing, thing_str) \
  64. static void list_dither_## thing(CACAContext *c) \
  65. { \
  66. const char *const *thing = caca_get_dither_## thing ##_list(c->dither); \
  67. int i; \
  68. \
  69. av_log(c->ctx, AV_LOG_INFO, "Available %s:\n", thing_str); \
  70. for (i = 0; thing[i]; i += 2) \
  71. av_log(c->ctx, AV_LOG_INFO, "%s: %s\n", thing[i], thing[i + 1]); \
  72. }
  73. DEFINE_LIST_DITHER(color, "colors");
  74. DEFINE_LIST_DITHER(charset, "charsets");
  75. DEFINE_LIST_DITHER(algorithm, "algorithms");
  76. DEFINE_LIST_DITHER(antialias, "antialias");
  77. static int caca_write_header(AVFormatContext *s)
  78. {
  79. CACAContext *c = s->priv_data;
  80. AVStream *st = s->streams[0];
  81. AVCodecParameters *encctx = st->codecpar;
  82. int ret, bpp;
  83. c->ctx = s;
  84. if (c->list_drivers) {
  85. list_drivers(c);
  86. return AVERROR_EXIT;
  87. }
  88. if (c->list_dither) {
  89. if (!strcmp(c->list_dither, "colors")) {
  90. list_dither_color(c);
  91. } else if (!strcmp(c->list_dither, "charsets")) {
  92. list_dither_charset(c);
  93. } else if (!strcmp(c->list_dither, "algorithms")) {
  94. list_dither_algorithm(c);
  95. } else if (!strcmp(c->list_dither, "antialiases")) {
  96. list_dither_antialias(c);
  97. } else {
  98. av_log(s, AV_LOG_ERROR,
  99. "Invalid argument '%s', for 'list_dither' option\n"
  100. "Argument must be one of 'algorithms, 'antialiases', 'charsets', 'colors'\n",
  101. c->list_dither);
  102. return AVERROR(EINVAL);
  103. }
  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_STRING, {.str=NULL}, 0, 1, ENC, "list_dither" },
  181. { "algorithms", NULL, 0, AV_OPT_TYPE_CONST, {.str = "algorithms"}, 0, 0, ENC, "list_dither" },
  182. { "antialiases", NULL, 0, AV_OPT_TYPE_CONST, {.str = "antialiases"},0, 0, ENC, "list_dither" },
  183. { "charsets", NULL, 0, AV_OPT_TYPE_CONST, {.str = "charsets"}, 0, 0, ENC, "list_dither" },
  184. { "colors", NULL, 0, AV_OPT_TYPE_CONST, {.str = "colors"}, 0, 0, ENC, "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. };