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.

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