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.

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