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.

442 lines
14KB

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