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.

437 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. switch(s->args[0]) {
  166. case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
  167. s->font = ff_cga_font;
  168. s->font_height = 8;
  169. width = 40<<3;
  170. height = 25<<3;
  171. break;
  172. case 2: case 3: //640x400 (25 rows)
  173. s->font = ff_vga16_font;
  174. s->font_height = 16;
  175. width = 80<<3;
  176. height = 25<<4;
  177. break;
  178. case 6: case 14: //640x200 (25 rows)
  179. s->font = ff_cga_font;
  180. s->font_height = 8;
  181. width = 80<<3;
  182. height = 25<<3;
  183. break;
  184. case 7: //set line wrapping
  185. break;
  186. case 15: case 16: //640x350 (43 rows)
  187. s->font = ff_cga_font;
  188. s->font_height = 8;
  189. width = 80<<3;
  190. height = 43<<3;
  191. break;
  192. case 17: case 18: //640x480 (60 rows)
  193. s->font = ff_cga_font;
  194. s->font_height = 8;
  195. width = 80<<3;
  196. height = 60<<4;
  197. break;
  198. default:
  199. av_log_ask_for_sample(avctx, "unsupported screen mode\n");
  200. }
  201. if (width != avctx->width || height != avctx->height) {
  202. if (s->frame.data[0])
  203. avctx->release_buffer(avctx, &s->frame);
  204. avcodec_set_dimensions(avctx, width, height);
  205. ret = avctx->get_buffer(avctx, &s->frame);
  206. if (ret < 0) {
  207. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  208. return ret;
  209. }
  210. s->frame.pict_type = AV_PICTURE_TYPE_I;
  211. s->frame.palette_has_changed = 1;
  212. memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
  213. erase_screen(avctx);
  214. } else if (c == 'l') {
  215. erase_screen(avctx);
  216. }
  217. break;
  218. case 'J': //Erase in Page
  219. switch (s->args[0]) {
  220. case 0:
  221. erase_line(avctx, s->x, avctx->width - s->x);
  222. if (s->y < avctx->height - s->font_height)
  223. memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
  224. DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
  225. break;
  226. case 1:
  227. erase_line(avctx, 0, s->x);
  228. if (s->y > 0)
  229. memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
  230. break;
  231. case 2:
  232. erase_screen(avctx);
  233. }
  234. break;
  235. case 'K': //Erase in Line
  236. switch(s->args[0]) {
  237. case 0:
  238. erase_line(avctx, s->x, avctx->width - s->x);
  239. break;
  240. case 1:
  241. erase_line(avctx, 0, s->x);
  242. break;
  243. case 2:
  244. erase_line(avctx, 0, avctx->width);
  245. }
  246. break;
  247. case 'm': //Select Graphics Rendition
  248. if (s->nb_args == 0) {
  249. s->nb_args = 1;
  250. s->args[0] = 0;
  251. }
  252. for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
  253. int m = s->args[i];
  254. if (m == 0) {
  255. s->attributes = 0;
  256. s->fg = DEFAULT_FG_COLOR;
  257. s->bg = DEFAULT_BG_COLOR;
  258. } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
  259. s->attributes |= 1 << (m - 1);
  260. } else if (m >= 30 && m <= 38) {
  261. s->fg = ansi_to_cga[m - 30];
  262. } else if (m == 39) {
  263. s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
  264. } else if (m >= 40 && m <= 47) {
  265. s->bg = ansi_to_cga[m - 40];
  266. } else if (m == 49) {
  267. s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
  268. } else {
  269. av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
  270. }
  271. }
  272. break;
  273. case 'n': //Device Status Report
  274. case 'R': //report current line and column
  275. /* ignore */
  276. break;
  277. case 's': //Save Cursor Position
  278. s->sx = s->x;
  279. s->sy = s->y;
  280. break;
  281. case 'u': //Restore Cursor Position
  282. s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
  283. s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
  284. break;
  285. default:
  286. av_log_ask_for_sample(avctx, "unsupported escape code\n");
  287. break;
  288. }
  289. return 0;
  290. }
  291. static int decode_frame(AVCodecContext *avctx,
  292. void *data, int *data_size,
  293. AVPacket *avpkt)
  294. {
  295. AnsiContext *s = avctx->priv_data;
  296. uint8_t *buf = avpkt->data;
  297. int buf_size = avpkt->size;
  298. const uint8_t *buf_end = buf+buf_size;
  299. int ret, i, count;
  300. ret = avctx->reget_buffer(avctx, &s->frame);
  301. if (ret < 0){
  302. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  303. return ret;
  304. }
  305. s->frame.pict_type = AV_PICTURE_TYPE_I;
  306. s->frame.palette_has_changed = 1;
  307. memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
  308. while(buf < buf_end) {
  309. switch(s->state) {
  310. case STATE_NORMAL:
  311. switch (buf[0]) {
  312. case 0x00: //NUL
  313. case 0x07: //BEL
  314. case 0x1A: //SUB
  315. /* ignore */
  316. break;
  317. case 0x08: //BS
  318. s->x = FFMAX(s->x - 1, 0);
  319. break;
  320. case 0x09: //HT
  321. i = s->x / FONT_WIDTH;
  322. count = ((i + 8) & ~7) - i;
  323. for (i = 0; i < count; i++)
  324. draw_char(avctx, ' ');
  325. break;
  326. case 0x0A: //LF
  327. hscroll(avctx);
  328. case 0x0D: //CR
  329. s->x = 0;
  330. break;
  331. case 0x0C: //FF
  332. erase_screen(avctx);
  333. break;
  334. case 0x1B: //ESC
  335. s->state = STATE_ESCAPE;
  336. break;
  337. default:
  338. draw_char(avctx, buf[0]);
  339. }
  340. break;
  341. case STATE_ESCAPE:
  342. if (buf[0] == '[') {
  343. s->state = STATE_CODE;
  344. s->nb_args = 0;
  345. s->args[0] = 0;
  346. } else {
  347. s->state = STATE_NORMAL;
  348. draw_char(avctx, 0x1B);
  349. return -1;
  350. continue;
  351. }
  352. break;
  353. case STATE_CODE:
  354. switch(buf[0]) {
  355. case '0': case '1': case '2': case '3': case '4':
  356. case '5': case '6': case '7': case '8': case '9':
  357. if (s->nb_args < MAX_NB_ARGS)
  358. s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
  359. break;
  360. case ';':
  361. s->nb_args++;
  362. if (s->nb_args < MAX_NB_ARGS)
  363. s->args[s->nb_args] = 0;
  364. break;
  365. case 'M':
  366. s->state = STATE_MUSIC_PREAMBLE;
  367. break;
  368. case '=': case '?':
  369. /* ignore */
  370. break;
  371. default:
  372. if (s->nb_args > MAX_NB_ARGS)
  373. av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
  374. if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
  375. s->nb_args++;
  376. if (execute_code(avctx, buf[0]) < 0)
  377. return -1;
  378. s->state = STATE_NORMAL;
  379. }
  380. break;
  381. case STATE_MUSIC_PREAMBLE:
  382. if (buf[0] == 0x0E || buf[0] == 0x1B)
  383. s->state = STATE_NORMAL;
  384. /* ignore music data */
  385. break;
  386. }
  387. buf++;
  388. }
  389. *data_size = sizeof(AVFrame);
  390. *(AVFrame*)data = s->frame;
  391. return buf_size;
  392. }
  393. static av_cold int decode_close(AVCodecContext *avctx)
  394. {
  395. AnsiContext *s = avctx->priv_data;
  396. if (s->frame.data[0])
  397. avctx->release_buffer(avctx, &s->frame);
  398. return 0;
  399. }
  400. AVCodec ff_ansi_decoder = {
  401. .name = "ansi",
  402. .type = AVMEDIA_TYPE_VIDEO,
  403. .id = CODEC_ID_ANSI,
  404. .priv_data_size = sizeof(AnsiContext),
  405. .init = decode_init,
  406. .close = decode_close,
  407. .decode = decode_frame,
  408. .capabilities = CODEC_CAP_DR1,
  409. .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
  410. };