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.

496 lines
16KB

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