FFmpeg
Loading...
Searching...
No Matches
xbmenc.c
Go to the documentation of this file.
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#define ANSI_MIN_READLINE 509
28
30 const AVFrame *p, int *got_packet)
31{
32 int i, j, l, commas, ret, size, linesize, lineout, rowsout;
33 const uint8_t *ptr;
34 uint8_t *buf;
35
36 linesize = lineout = (avctx->width + 7) / 8;
37 commas = avctx->height * linesize;
38
39 /* ANSI worst case minimum readline is 509 chars. */
40 rowsout = avctx->height;
41 if (lineout > (ANSI_MIN_READLINE / 6)) {
42 lineout = ANSI_MIN_READLINE / 6;
43 rowsout = (commas + lineout - 1) / lineout;
44 }
45
46 size = rowsout * (lineout * 6 + 1) + 106;
47 if ((ret = ff_alloc_packet(avctx, pkt, size)) < 0)
48 return ret;
49
50 buf = pkt->data;
51 ptr = p->data[0];
52
53 buf += snprintf(buf, 32, "#define image_width %u\n", avctx->width);
54 buf += snprintf(buf, 33, "#define image_height %u\n", avctx->height);
55 buf += snprintf(buf, 39, "static unsigned char image_bits[] = {\n");
56 for (i = 0, l = lineout; i < avctx->height; i++) {
57 for (j = 0; j < linesize; j++) {
58 // 0..15 bitreversed as chars
59 static const char lut[] = {
60 '0', '8', '4', 'C', '2', 'A', '6', 'E',
61 '1', '9', '5', 'D', '3', 'B', '7', 'F'
62 };
63 buf[0] = ' ';
64 buf[1] = '0';
65 buf[2] = 'x';
66 buf[3] = lut[*ptr & 0xF];
67 buf[4] = lut[*ptr >> 4];
68 buf += 5;
69 ptr++;
70 if (--commas <= 0) {
71 *buf++ = '\n';
72 break;
73 }
74 *buf++ = ',';
75 if (--l <= 0) {
76 *buf++ = '\n';
77 l = lineout;
78 }
79 }
80 ptr += p->linesize[0] - linesize;
81 }
82 buf += snprintf(buf, 5, " };\n");
83
84 pkt->size = buf - pkt->data;
85 *got_packet = 1;
86 return 0;
87}
88
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,
97};
const FFCodec ff_xbm_encoder
Definition xbmenc.c:89
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
static AVPacket * pkt
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_XBM
Definition codec_id.h:209
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:82
#define snprintf
Definition snprintf.h:34
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition xbmenc.c:29
#define ANSI_MIN_READLINE
Definition xbmenc.c:27