caca.c 8.9 KB

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