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.

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