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.

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