You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

436 lines
14KB

  1. /*
  2. * ASCII/ANSI art decoder
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * ASCII/ANSI art decoder
  24. */
  25. #include "libavutil/lfg.h"
  26. #include "avcodec.h"
  27. #include "cga_data.h"
  28. #define ATTR_BOLD 0x01 /**< Bold/Bright-foreground (mode 1) */
  29. #define ATTR_FAINT 0x02 /**< Faint (mode 2) */
  30. #define ATTR_UNDERLINE 0x08 /**< Underline (mode 4) */
  31. #define ATTR_BLINK 0x10 /**< Blink/Bright-background (mode 5) */
  32. #define ATTR_REVERSE 0x40 /**< Reverse (mode 7) */
  33. #define ATTR_CONCEALED 0x80 /**< Concealed (mode 8) */
  34. #define DEFAULT_FG_COLOR 7 /**< CGA color index */
  35. #define DEFAULT_BG_COLOR 0
  36. #define DEFAULT_SCREEN_MODE 3 /**< 80x25 */
  37. #define FONT_WIDTH 8 /**< Font width */
  38. /** map ansi color index to cga palette index */
  39. static const uint8_t ansi_to_cga[16] = {
  40. 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
  41. };
  42. typedef struct {
  43. AVFrame frame;
  44. int x; /**< x cursor position (pixels) */
  45. int y; /**< y cursor position (pixels) */
  46. int sx; /**< saved x cursor position (pixels) */
  47. int sy; /**< saved y cursor position (pixels) */
  48. const uint8_t* font; /**< font */
  49. int font_height; /**< font height */
  50. int attributes; /**< attribute flags */
  51. int fg; /**< foreground color */
  52. int bg; /**< background color */
  53. /* ansi parser state machine */
  54. enum {
  55. STATE_NORMAL = 0,
  56. STATE_ESCAPE,
  57. STATE_CODE,
  58. STATE_MUSIC_PREAMBLE
  59. } state;
  60. #define MAX_NB_ARGS 4
  61. int args[MAX_NB_ARGS];
  62. int nb_args; /**< number of arguments (may exceed MAX_NB_ARGS) */
  63. } AnsiContext;
  64. static av_cold int decode_init(AVCodecContext *avctx)
  65. {
  66. AnsiContext *s = avctx->priv_data;
  67. avctx->pix_fmt = PIX_FMT_PAL8;
  68. /* defaults */
  69. s->font = ff_vga16_font;
  70. s->font_height = 16;
  71. s->fg = DEFAULT_FG_COLOR;
  72. s->bg = DEFAULT_BG_COLOR;
  73. if (!avctx->width || !avctx->height)
  74. avcodec_set_dimensions(avctx, 80<<3, 25<<4);
  75. return 0;
  76. }
  77. static void hscroll(AVCodecContext *avctx)
  78. {
  79. AnsiContext *s = avctx->priv_data;
  80. int i;
  81. if (s->y < avctx->height - s->font_height) {
  82. s->y += s->font_height;
  83. return;
  84. }
  85. i = 0;
  86. for (; i < avctx->height - s->font_height; i++)
  87. memcpy(s->frame.data[0] + i * s->frame.linesize[0],
  88. s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
  89. avctx->width);
  90. for (; i < avctx->height; i++)
  91. memset(s->frame.data[0] + i * s->frame.linesize[0],
  92. DEFAULT_BG_COLOR, avctx->width);
  93. }
  94. static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
  95. {
  96. AnsiContext *s = avctx->priv_data;
  97. int i;
  98. for (i = 0; i < s->font_height; i++)
  99. memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
  100. DEFAULT_BG_COLOR, xlength);
  101. }
  102. static void erase_screen(AVCodecContext *avctx)
  103. {
  104. AnsiContext *s = avctx->priv_data;
  105. int i;
  106. for (i = 0; i < avctx->height; i++)
  107. memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
  108. s->x = s->y = 0;
  109. }
  110. /**
  111. * Draw character to screen
  112. */
  113. static void draw_char(AVCodecContext *avctx, int c)
  114. {
  115. AnsiContext *s = avctx->priv_data;
  116. int fg = s->fg;
  117. int bg = s->bg;
  118. if ((s->attributes & ATTR_BOLD))
  119. fg += 8;
  120. if ((s->attributes & ATTR_BLINK))
  121. bg += 8;
  122. if ((s->attributes & ATTR_REVERSE))
  123. FFSWAP(int, fg, bg);
  124. if ((s->attributes & ATTR_CONCEALED))
  125. fg = bg;
  126. ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
  127. s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
  128. s->x += FONT_WIDTH;
  129. if (s->x >= avctx->width) {
  130. s->x = 0;
  131. hscroll(avctx);
  132. }
  133. }
  134. /**
  135. * Execute ANSI escape code
  136. * @return 0 on success, negative on error
  137. */
  138. static int execute_code(AVCodecContext * avctx, int c)
  139. {
  140. AnsiContext *s = avctx->priv_data;
  141. int ret, i, width, height;
  142. switch(c) {
  143. case 'A': //Cursor Up
  144. s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
  145. break;
  146. case 'B': //Cursor Down
  147. s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
  148. break;
  149. case 'C': //Cursor Right
  150. s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
  151. break;
  152. case 'D': //Cursor Left
  153. s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
  154. break;
  155. case 'H': //Cursor Position
  156. case 'f': //Horizontal and Vertical Position
  157. s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
  158. s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
  159. break;
  160. case 'h': //set creen mode
  161. case 'l': //reset screen mode
  162. if (s->nb_args < 2)
  163. s->args[0] = DEFAULT_SCREEN_MODE;
  164. switch(s->args[0]) {
  165. case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
  166. s->font = ff_cga_font;
  167. s->font_height = 8;
  168. width = 40<<3;
  169. height = 25<<3;
  170. break;
  171. case 2: case 3: //640x400 (25 rows)
  172. s->font = ff_vga16_font;
  173. s->font_height = 16;
  174. width = 80<<3;
  175. height = 25<<4;
  176. break;
  177. case 6: case 14: //640x200 (25 rows)
  178. s->font = ff_cga_font;
  179. s->font_height = 8;
  180. width = 80<<3;
  181. height = 25<<3;
  182. break;
  183. case 7: //set line wrapping
  184. break;
  185. case 15: case 16: //640x350 (43 rows)
  186. s->font = ff_cga_font;
  187. s->font_height = 8;
  188. width = 80<<3;
  189. height = 43<<3;
  190. break;
  191. case 17: case 18: //640x480 (60 rows)
  192. s->font = ff_cga_font;
  193. s->font_height = 8;
  194. width = 80<<3;
  195. height = 60<<4;
  196. break;
  197. default:
  198. av_log_ask_for_sample(avctx, "unsupported screen mode\n");
  199. }
  200. if (width != avctx->width || height != avctx->height) {
  201. if (s->frame.data[0])
  202. avctx->release_buffer(avctx, &s->frame);
  203. avcodec_set_dimensions(avctx, width, height);
  204. ret = avctx->get_buffer(avctx, &s->frame);
  205. if (ret < 0) {
  206. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  207. return ret;
  208. }
  209. s->frame.pict_type = AV_PICTURE_TYPE_I;
  210. s->frame.palette_has_changed = 1;
  211. memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
  212. erase_screen(avctx);
  213. } else if (c == 'l') {
  214. erase_screen(avctx);
  215. }
  216. break;
  217. case 'J': //Erase in Page
  218. switch (s->args[0]) {
  219. case 0:
  220. erase_line(avctx, s->x, avctx->width - s->x);
  221. if (s->y < avctx->height - s->font_height)
  222. memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
  223. DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
  224. break;
  225. case 1:
  226. erase_line(avctx, 0, s->x);
  227. if (s->y > 0)
  228. memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
  229. break;
  230. case 2:
  231. erase_screen(avctx);
  232. }
  233. break;
  234. case 'K': //Erase in Line
  235. switch(s->args[0]) {
  236. case 0:
  237. erase_line(avctx, s->x, avctx->width - s->x);
  238. break;
  239. case 1:
  240. erase_line(avctx, 0, s->x);
  241. break;
  242. case 2:
  243. erase_line(avctx, 0, avctx->width);
  244. }
  245. break;
  246. case 'm': //Select Graphics Rendition
  247. if (s->nb_args == 0) {
  248. s->nb_args = 1;
  249. s->args[0] = 0;
  250. }
  251. for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
  252. int m = s->args[i];
  253. if (m == 0) {
  254. s->attributes = 0;
  255. s->fg = DEFAULT_FG_COLOR;
  256. s->bg = DEFAULT_BG_COLOR;
  257. } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
  258. s->attributes |= 1 << (m - 1);
  259. } else if (m >= 30 && m <= 38) {
  260. s->fg = ansi_to_cga[m - 30];
  261. } else if (m == 39) {
  262. s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
  263. } else if (m >= 40 && m <= 47) {
  264. s->bg = ansi_to_cga[m - 40];
  265. } else if (m == 49) {
  266. s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
  267. } else {
  268. av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
  269. }
  270. }
  271. break;
  272. case 'n': //Device Status Report
  273. case 'R': //report current line and column
  274. /* ignore */
  275. break;
  276. case 's': //Save Cursor Position
  277. s->sx = s->x;
  278. s->sy = s->y;
  279. break;
  280. case 'u': //Restore Cursor Position
  281. s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
  282. s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
  283. break;
  284. default:
  285. av_log_ask_for_sample(avctx, "unsupported escape code\n");
  286. break;
  287. }
  288. return 0;
  289. }
  290. static int decode_frame(AVCodecContext *avctx,
  291. void *data, int *data_size,
  292. AVPacket *avpkt)
  293. {
  294. AnsiContext *s = avctx->priv_data;
  295. uint8_t *buf = avpkt->data;
  296. int buf_size = avpkt->size;
  297. const uint8_t *buf_end = buf+buf_size;
  298. int ret, i, count;
  299. ret = avctx->reget_buffer(avctx, &s->frame);
  300. if (ret < 0){
  301. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  302. return ret;
  303. }
  304. s->frame.pict_type = AV_PICTURE_TYPE_I;
  305. s->frame.palette_has_changed = 1;
  306. memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
  307. while(buf < buf_end) {
  308. switch(s->state) {
  309. case STATE_NORMAL:
  310. switch (buf[0]) {
  311. case 0x00: //NUL
  312. case 0x07: //BEL
  313. case 0x1A: //SUB
  314. /* ignore */
  315. break;
  316. case 0x08: //BS
  317. s->x = FFMAX(s->x - 1, 0);
  318. break;
  319. case 0x09: //HT
  320. i = s->x / FONT_WIDTH;
  321. count = ((i + 8) & ~7) - i;
  322. for (i = 0; i < count; i++)
  323. draw_char(avctx, ' ');
  324. break;
  325. case 0x0A: //LF
  326. hscroll(avctx);
  327. case 0x0D: //CR
  328. s->x = 0;
  329. break;
  330. case 0x0C: //FF
  331. erase_screen(avctx);
  332. break;
  333. case 0x1B: //ESC
  334. s->state = STATE_ESCAPE;
  335. break;
  336. default:
  337. draw_char(avctx, buf[0]);
  338. }
  339. break;
  340. case STATE_ESCAPE:
  341. if (buf[0] == '[') {
  342. s->state = STATE_CODE;
  343. s->nb_args = 0;
  344. s->args[0] = 0;
  345. } else {
  346. s->state = STATE_NORMAL;
  347. draw_char(avctx, 0x1B);
  348. return -1;
  349. continue;
  350. }
  351. break;
  352. case STATE_CODE:
  353. switch(buf[0]) {
  354. case '0': case '1': case '2': case '3': case '4':
  355. case '5': case '6': case '7': case '8': case '9':
  356. if (s->nb_args < MAX_NB_ARGS)
  357. s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
  358. break;
  359. case ';':
  360. s->nb_args++;
  361. if (s->nb_args < MAX_NB_ARGS)
  362. s->args[s->nb_args] = 0;
  363. break;
  364. case 'M':
  365. s->state = STATE_MUSIC_PREAMBLE;
  366. break;
  367. case '=': case '?':
  368. /* ignore */
  369. break;
  370. default:
  371. if (s->nb_args > MAX_NB_ARGS)
  372. av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
  373. if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
  374. s->nb_args++;
  375. if (execute_code(avctx, buf[0]) < 0)
  376. return -1;
  377. s->state = STATE_NORMAL;
  378. }
  379. break;
  380. case STATE_MUSIC_PREAMBLE:
  381. if (buf[0] == 0x0E || buf[0] == 0x1B)
  382. s->state = STATE_NORMAL;
  383. /* ignore music data */
  384. break;
  385. }
  386. buf++;
  387. }
  388. *data_size = sizeof(AVFrame);
  389. *(AVFrame*)data = s->frame;
  390. return buf_size;
  391. }
  392. static av_cold int decode_close(AVCodecContext *avctx)
  393. {
  394. AnsiContext *s = avctx->priv_data;
  395. if (s->frame.data[0])
  396. avctx->release_buffer(avctx, &s->frame);
  397. return 0;
  398. }
  399. AVCodec ff_ansi_decoder = {
  400. .name = "ansi",
  401. .type = AVMEDIA_TYPE_VIDEO,
  402. .id = CODEC_ID_ANSI,
  403. .priv_data_size = sizeof(AnsiContext),
  404. .init = decode_init,
  405. .close = decode_close,
  406. .decode = decode_frame,
  407. .capabilities = CODEC_CAP_DR1,
  408. .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
  409. };