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.

488 lines
16KB

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