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.

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