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.

439 lines
14KB

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