annotate ffmpeg/libavcodec/xbmdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * XBM image format
yading@10 3 *
yading@10 4 * Copyright (c) 2012 Paul B Mahol
yading@10 5 *
yading@10 6 * This file is part of FFmpeg.
yading@10 7 *
yading@10 8 * FFmpeg is free software; you can redistribute it and/or
yading@10 9 * modify it under the terms of the GNU Lesser General Public
yading@10 10 * License as published by the Free Software Foundation; either
yading@10 11 * version 2.1 of the License, or (at your option) any later version.
yading@10 12 *
yading@10 13 * FFmpeg is distributed in the hope that it will be useful,
yading@10 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 16 * Lesser General Public License for more details.
yading@10 17 *
yading@10 18 * You should have received a copy of the GNU Lesser General Public
yading@10 19 * License along with FFmpeg; if not, write to the Free Software
yading@10 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 21 */
yading@10 22
yading@10 23 #include "avcodec.h"
yading@10 24 #include "internal.h"
yading@10 25 #include "mathops.h"
yading@10 26 #include "libavutil/avstring.h"
yading@10 27
yading@10 28 static av_cold int xbm_decode_init(AVCodecContext *avctx)
yading@10 29 {
yading@10 30 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
yading@10 31
yading@10 32 return 0;
yading@10 33 }
yading@10 34
yading@10 35 static int convert(uint8_t x)
yading@10 36 {
yading@10 37 if (x >= 'a')
yading@10 38 x -= 87;
yading@10 39 else if (x >= 'A')
yading@10 40 x -= 55;
yading@10 41 else
yading@10 42 x -= '0';
yading@10 43 return x;
yading@10 44 }
yading@10 45
yading@10 46 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
yading@10 47 int *got_frame, AVPacket *avpkt)
yading@10 48 {
yading@10 49 AVFrame *p = data;
yading@10 50 const uint8_t *end, *ptr = avpkt->data;
yading@10 51 uint8_t *dst;
yading@10 52 int ret, linesize, i, j;
yading@10 53
yading@10 54 end = avpkt->data + avpkt->size;
yading@10 55 while (!avctx->width || !avctx->height) {
yading@10 56 char name[256];
yading@10 57 int number, len;
yading@10 58
yading@10 59 ptr += strcspn(ptr, "#");
yading@10 60 if (sscanf(ptr, "#define %256s %u", name, &number) != 2) {
yading@10 61 av_log(avctx, AV_LOG_ERROR, "Unexpected preprocessor directive\n");
yading@10 62 return AVERROR_INVALIDDATA;
yading@10 63 }
yading@10 64
yading@10 65 len = strlen(name);
yading@10 66 if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
yading@10 67 avctx->height = number;
yading@10 68 } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
yading@10 69 avctx->width = number;
yading@10 70 } else {
yading@10 71 av_log(avctx, AV_LOG_ERROR, "Unknown define '%s'\n", name);
yading@10 72 return AVERROR_INVALIDDATA;
yading@10 73 }
yading@10 74 ptr += strcspn(ptr, "\n\r") + 1;
yading@10 75 }
yading@10 76
yading@10 77 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
yading@10 78 return ret;
yading@10 79
yading@10 80 // goto start of image data
yading@10 81 ptr += strcspn(ptr, "{") + 1;
yading@10 82
yading@10 83 linesize = (avctx->width + 7) / 8;
yading@10 84 for (i = 0; i < avctx->height; i++) {
yading@10 85 dst = p->data[0] + i * p->linesize[0];
yading@10 86 for (j = 0; j < linesize; j++) {
yading@10 87 uint8_t val;
yading@10 88
yading@10 89 ptr += strcspn(ptr, "x") + 1;
yading@10 90 if (ptr < end && av_isxdigit(*ptr)) {
yading@10 91 val = convert(*ptr);
yading@10 92 ptr++;
yading@10 93 if (av_isxdigit(*ptr))
yading@10 94 val = (val << 4) + convert(*ptr);
yading@10 95 *dst++ = ff_reverse[val];
yading@10 96 } else {
yading@10 97 av_log(avctx, AV_LOG_ERROR, "Unexpected data at '%.8s'\n", ptr);
yading@10 98 return AVERROR_INVALIDDATA;
yading@10 99 }
yading@10 100 }
yading@10 101 }
yading@10 102
yading@10 103 p->key_frame = 1;
yading@10 104 p->pict_type = AV_PICTURE_TYPE_I;
yading@10 105
yading@10 106 *got_frame = 1;
yading@10 107
yading@10 108 return avpkt->size;
yading@10 109 }
yading@10 110
yading@10 111 AVCodec ff_xbm_decoder = {
yading@10 112 .name = "xbm",
yading@10 113 .type = AVMEDIA_TYPE_VIDEO,
yading@10 114 .id = AV_CODEC_ID_XBM,
yading@10 115 .init = xbm_decode_init,
yading@10 116 .decode = xbm_decode_frame,
yading@10 117 .capabilities = CODEC_CAP_DR1,
yading@10 118 .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
yading@10 119 };