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.

455 lines
14KB

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