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.

468 lines
15KB

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