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.

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