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 "mathops.h"
00026
00027 static av_cold int xbm_decode_init(AVCodecContext *avctx)
00028 {
00029 avctx->coded_frame = avcodec_alloc_frame();
00030 if (!avctx->coded_frame)
00031 return AVERROR(ENOMEM);
00032
00033 return 0;
00034 }
00035
00036 static int convert(uint8_t x)
00037 {
00038 if (x >= 'a')
00039 x -= 87;
00040 else if (x >= 'A')
00041 x -= 55;
00042 else
00043 x -= '0';
00044 return x;
00045 }
00046
00047 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
00048 int *got_frame, AVPacket *avpkt)
00049 {
00050 AVFrame *p = avctx->coded_frame;
00051 const uint8_t *end, *ptr = avpkt->data;
00052 uint8_t *dst;
00053 int ret, linesize, i, j;
00054
00055 end = avpkt->data + avpkt->size;
00056 while (!avctx->width || !avctx->height) {
00057 char name[256];
00058 int number, len;
00059
00060 ptr += strcspn(ptr, "#");
00061 if (sscanf(ptr, "#define %256s %u", name, &number) != 2) {
00062 av_log(avctx, AV_LOG_ERROR, "Unexpected preprocessor directive\n");
00063 return AVERROR_INVALIDDATA;
00064 }
00065
00066 len = strlen(name);
00067 if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
00068 avctx->height = number;
00069 } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
00070 avctx->width = number;
00071 } else {
00072 av_log(avctx, AV_LOG_ERROR, "Unknown define '%s'\n", name);
00073 return AVERROR_INVALIDDATA;
00074 }
00075 ptr += strcspn(ptr, "\n\r") + 1;
00076 }
00077
00078 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
00079
00080 if (p->data[0])
00081 avctx->release_buffer(avctx, p);
00082
00083 p->reference = 0;
00084 if ((ret = ff_get_buffer(avctx, p)) < 0)
00085 return ret;
00086
00087
00088 ptr += strcspn(ptr, "{") + 1;
00089
00090 linesize = (avctx->width + 7) / 8;
00091 for (i = 0; i < avctx->height; i++) {
00092 dst = p->data[0] + i * p->linesize[0];
00093 for (j = 0; j < linesize; j++) {
00094 uint8_t val;
00095
00096 ptr += strcspn(ptr, "x") + 1;
00097 if (ptr < end && isxdigit(*ptr)) {
00098 val = convert(*ptr);
00099 ptr++;
00100 if (isxdigit(*ptr))
00101 val = (val << 4) + convert(*ptr);
00102 *dst++ = ff_reverse[val];
00103 } else {
00104 av_log(avctx, AV_LOG_ERROR, "Unexpected data at '%.8s'\n", ptr);
00105 return AVERROR_INVALIDDATA;
00106 }
00107 }
00108 }
00109
00110 p->key_frame = 1;
00111 p->pict_type = AV_PICTURE_TYPE_I;
00112
00113 *got_frame = 1;
00114 *(AVFrame *)data = *p;
00115
00116 return avpkt->size;
00117 }
00118
00119 static av_cold int xbm_decode_close(AVCodecContext *avctx)
00120 {
00121 if (avctx->coded_frame->data[0])
00122 avctx->release_buffer(avctx, avctx->coded_frame);
00123
00124 av_freep(&avctx->coded_frame);
00125
00126 return 0;
00127 }
00128
00129 AVCodec ff_xbm_decoder = {
00130 .name = "xbm",
00131 .type = AVMEDIA_TYPE_VIDEO,
00132 .id = AV_CODEC_ID_XBM,
00133 .init = xbm_decode_init,
00134 .close = xbm_decode_close,
00135 .decode = xbm_decode_frame,
00136 .capabilities = CODEC_CAP_DR1,
00137 .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
00138 };