/src/ffmpeg/libavcodec/xbmenc.c
Line | Count | Source |
1 | | /* |
2 | | * XBM image format |
3 | | * |
4 | | * Copyright (c) 2012 Paul B Mahol |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * FFmpeg is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | #include "avcodec.h" |
24 | | #include "codec_internal.h" |
25 | | #include "encode.h" |
26 | | |
27 | 8.10k | #define ANSI_MIN_READLINE 509 |
28 | | |
29 | | static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
30 | | const AVFrame *p, int *got_packet) |
31 | 7.75k | { |
32 | 7.75k | int i, j, l, commas, ret, size, linesize, lineout, rowsout; |
33 | 7.75k | const uint8_t *ptr; |
34 | 7.75k | uint8_t *buf; |
35 | | |
36 | 7.75k | linesize = lineout = (avctx->width + 7) / 8; |
37 | 7.75k | commas = avctx->height * linesize; |
38 | | |
39 | | /* ANSI worst case minimum readline is 509 chars. */ |
40 | 7.75k | rowsout = avctx->height; |
41 | 7.75k | if (lineout > (ANSI_MIN_READLINE / 6)) { |
42 | 356 | lineout = ANSI_MIN_READLINE / 6; |
43 | 356 | rowsout = (commas + lineout - 1) / lineout; |
44 | 356 | } |
45 | | |
46 | 7.75k | size = rowsout * (lineout * 6 + 1) + 106; |
47 | 7.75k | if ((ret = ff_alloc_packet(avctx, pkt, size)) < 0) |
48 | 0 | return ret; |
49 | | |
50 | 7.75k | buf = pkt->data; |
51 | 7.75k | ptr = p->data[0]; |
52 | | |
53 | 7.75k | buf += snprintf(buf, 32, "#define image_width %u\n", avctx->width); |
54 | 7.75k | buf += snprintf(buf, 33, "#define image_height %u\n", avctx->height); |
55 | 7.75k | buf += snprintf(buf, 39, "static unsigned char image_bits[] = {\n"); |
56 | 1.96M | for (i = 0, l = lineout; i < avctx->height; i++) { |
57 | 5.68M | for (j = 0; j < linesize; j++) { |
58 | | // 0..15 bitreversed as chars |
59 | 3.73M | static const char lut[] = { |
60 | 3.73M | '0', '8', '4', 'C', '2', 'A', '6', 'E', |
61 | 3.73M | '1', '9', '5', 'D', '3', 'B', '7', 'F' |
62 | 3.73M | }; |
63 | 3.73M | buf[0] = ' '; |
64 | 3.73M | buf[1] = '0'; |
65 | 3.73M | buf[2] = 'x'; |
66 | 3.73M | buf[3] = lut[*ptr & 0xF]; |
67 | 3.73M | buf[4] = lut[*ptr >> 4]; |
68 | 3.73M | buf += 5; |
69 | 3.73M | ptr++; |
70 | 3.73M | if (--commas <= 0) { |
71 | 7.75k | *buf++ = '\n'; |
72 | 7.75k | break; |
73 | 7.75k | } |
74 | 3.72M | *buf++ = ','; |
75 | 3.72M | if (--l <= 0) { |
76 | 1.95M | *buf++ = '\n'; |
77 | 1.95M | l = lineout; |
78 | 1.95M | } |
79 | 3.72M | } |
80 | 1.96M | ptr += p->linesize[0] - linesize; |
81 | 1.96M | } |
82 | 7.75k | buf += snprintf(buf, 5, " };\n"); |
83 | | |
84 | 7.75k | pkt->size = buf - pkt->data; |
85 | 7.75k | *got_packet = 1; |
86 | 7.75k | return 0; |
87 | 7.75k | } |
88 | | |
89 | | const FFCodec ff_xbm_encoder = { |
90 | | .p.name = "xbm", |
91 | | CODEC_LONG_NAME("XBM (X BitMap) image"), |
92 | | .p.type = AVMEDIA_TYPE_VIDEO, |
93 | | .p.id = AV_CODEC_ID_XBM, |
94 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
95 | | FF_CODEC_ENCODE_CB(xbm_encode_frame), |
96 | | CODEC_PIXFMTS(AV_PIX_FMT_MONOWHITE), |
97 | | }; |