00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avcodec.h"
00024 #include "internal.h"
00025 #include "libavutil/common.h"
00026
00027 static av_cold int xbm_encode_init(AVCodecContext *avctx)
00028 {
00029 avctx->coded_frame = avcodec_alloc_frame();
00030 if (!avctx->coded_frame)
00031 return AVERROR(ENOMEM);
00032 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00033
00034 return 0;
00035 }
00036
00037 static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00038 const AVFrame *p, int *got_packet)
00039 {
00040 int i, j, ret, size, linesize;
00041 uint8_t *ptr, *buf;
00042
00043 linesize = (avctx->width + 7) / 8;
00044 size = avctx->height * (linesize * 7 + 2) + 110;
00045 if ((ret = ff_alloc_packet2(avctx, pkt, size)) < 0)
00046 return ret;
00047
00048 buf = pkt->data;
00049 ptr = p->data[0];
00050
00051 buf += snprintf(buf, 32, "#define image_width %u\n", avctx->width);
00052 buf += snprintf(buf, 33, "#define image_height %u\n", avctx->height);
00053 buf += snprintf(buf, 40, "static unsigned char image_bits[] = {\n");
00054 for (i = 0; i < avctx->height; i++) {
00055 for (j = 0; j < linesize; j++)
00056 buf += snprintf(buf, 7, " 0x%02X,", av_reverse[*ptr++]);
00057 ptr += p->linesize[0] - linesize;
00058 buf += snprintf(buf, 2, "\n");
00059 }
00060 buf += snprintf(buf, 5, " };\n");
00061
00062 pkt->size = buf - pkt->data;
00063 pkt->flags |= AV_PKT_FLAG_KEY;
00064 *got_packet = 1;
00065 return 0;
00066 }
00067
00068 static av_cold int xbm_encode_close(AVCodecContext *avctx)
00069 {
00070 av_freep(&avctx->coded_frame);
00071
00072 return 0;
00073 }
00074
00075 AVCodec ff_xbm_encoder = {
00076 .name = "xbm",
00077 .type = AVMEDIA_TYPE_VIDEO,
00078 .id = AV_CODEC_ID_XBM,
00079 .init = xbm_encode_init,
00080 .encode2 = xbm_encode_frame,
00081 .close = xbm_encode_close,
00082 .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_MONOWHITE,
00083 PIX_FMT_NONE },
00084 .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
00085 };