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.

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