FFmpeg
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,
96  .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE,
98 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
ANSI_MIN_READLINE
#define ANSI_MIN_READLINE
Definition: xbmenc.c:27
encode.h
AV_PIX_FMT_MONOWHITE
@ 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
FFCodec
Definition: codec_internal.h:127
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#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:159
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
xbm_encode_frame
static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: xbmenc.c:29
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
codec_internal.h
size
int size
Definition: twinvq_data.h:10344
ff_xbm_encoder
const FFCodec ff_xbm_encoder
Definition: xbmenc.c:89
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:624
AV_CODEC_ID_XBM
@ AV_CODEC_ID_XBM
Definition: codec_id.h:214
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
snprintf
#define snprintf
Definition: snprintf.h:34
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:62