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.

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