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.

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