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.

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