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.

474 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/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. if (width != avctx->width || height != avctx->height) {
  225. av_frame_unref(s->frame);
  226. avcodec_set_dimensions(avctx, width, height);
  227. if ((ret = ff_get_buffer(avctx, s->frame,
  228. AV_GET_BUFFER_FLAG_REF)) < 0)
  229. return ret;
  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. avpriv_request_sample(avctx, "Unsupported rendition parameter");
  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. avpriv_request_sample(avctx, "Unknown escape code");
  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. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  329. return ret;
  330. if (!avctx->frame_number) {
  331. for (i=0; i<avctx->height; i++)
  332. memset(s->frame->data[0]+ i*s->frame->linesize[0], 0, avctx->width);
  333. memset(s->frame->data[1], 0, AVPALETTE_SIZE);
  334. }
  335. s->frame->pict_type = AV_PICTURE_TYPE_I;
  336. s->frame->palette_has_changed = 1;
  337. set_palette((uint32_t *)s->frame->data[1]);
  338. if (!s->first_frame) {
  339. erase_screen(avctx);
  340. s->first_frame = 1;
  341. }
  342. while(buf < buf_end) {
  343. switch(s->state) {
  344. case STATE_NORMAL:
  345. switch (buf[0]) {
  346. case 0x00: //NUL
  347. case 0x07: //BEL
  348. case 0x1A: //SUB
  349. /* ignore */
  350. break;
  351. case 0x08: //BS
  352. s->x = FFMAX(s->x - 1, 0);
  353. break;
  354. case 0x09: //HT
  355. i = s->x / FONT_WIDTH;
  356. count = ((i + 8) & ~7) - i;
  357. for (i = 0; i < count; i++)
  358. draw_char(avctx, ' ');
  359. break;
  360. case 0x0A: //LF
  361. hscroll(avctx);
  362. case 0x0D: //CR
  363. s->x = 0;
  364. break;
  365. case 0x0C: //FF
  366. erase_screen(avctx);
  367. break;
  368. case 0x1B: //ESC
  369. s->state = STATE_ESCAPE;
  370. break;
  371. default:
  372. draw_char(avctx, buf[0]);
  373. }
  374. break;
  375. case STATE_ESCAPE:
  376. if (buf[0] == '[') {
  377. s->state = STATE_CODE;
  378. s->nb_args = 0;
  379. s->args[0] = -1;
  380. } else {
  381. s->state = STATE_NORMAL;
  382. draw_char(avctx, 0x1B);
  383. continue;
  384. }
  385. break;
  386. case STATE_CODE:
  387. switch(buf[0]) {
  388. case '0': case '1': case '2': case '3': case '4':
  389. case '5': case '6': case '7': case '8': case '9':
  390. if (s->nb_args < MAX_NB_ARGS)
  391. s->args[s->nb_args] = FFMAX(s->args[s->nb_args], 0) * 10 + buf[0] - '0';
  392. break;
  393. case ';':
  394. s->nb_args++;
  395. if (s->nb_args < MAX_NB_ARGS)
  396. s->args[s->nb_args] = 0;
  397. break;
  398. case 'M':
  399. s->state = STATE_MUSIC_PREAMBLE;
  400. break;
  401. case '=': case '?':
  402. /* ignore */
  403. break;
  404. default:
  405. if (s->nb_args > MAX_NB_ARGS)
  406. av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
  407. if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] >= 0)
  408. s->nb_args++;
  409. if ((ret = execute_code(avctx, buf[0])) < 0)
  410. return ret;
  411. s->state = STATE_NORMAL;
  412. }
  413. break;
  414. case STATE_MUSIC_PREAMBLE:
  415. if (buf[0] == 0x0E || buf[0] == 0x1B)
  416. s->state = STATE_NORMAL;
  417. /* ignore music data */
  418. break;
  419. }
  420. buf++;
  421. }
  422. *got_frame = 1;
  423. if ((ret = av_frame_ref(data, s->frame)) < 0)
  424. return ret;
  425. return buf_size;
  426. }
  427. static av_cold int decode_close(AVCodecContext *avctx)
  428. {
  429. AnsiContext *s = avctx->priv_data;
  430. av_frame_free(&s->frame);
  431. return 0;
  432. }
  433. AVCodec ff_ansi_decoder = {
  434. .name = "ansi",
  435. .type = AVMEDIA_TYPE_VIDEO,
  436. .id = AV_CODEC_ID_ANSI,
  437. .priv_data_size = sizeof(AnsiContext),
  438. .init = decode_init,
  439. .close = decode_close,
  440. .decode = decode_frame,
  441. .capabilities = CODEC_CAP_DR1,
  442. .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
  443. };