annotate ffmpeg/libavcodec/sgirledec.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 * SGI RLE 8-bit decoder
yading@10 3 * Copyright (c) 2012 Peter Ross
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * SGI RLE 8-bit decoder
yading@10 25 */
yading@10 26
yading@10 27 #include "avcodec.h"
yading@10 28 #include "internal.h"
yading@10 29
yading@10 30 typedef struct SGIRLEContext {
yading@10 31 AVFrame *frame;
yading@10 32 } SGIRLEContext;
yading@10 33
yading@10 34 static av_cold int sgirle_decode_init(AVCodecContext *avctx)
yading@10 35 {
yading@10 36 SGIRLEContext *s = avctx->priv_data;
yading@10 37 avctx->pix_fmt = AV_PIX_FMT_BGR8;
yading@10 38 s->frame = av_frame_alloc();
yading@10 39 if (!s->frame)
yading@10 40 return AVERROR(ENOMEM);
yading@10 41 return 0;
yading@10 42 }
yading@10 43
yading@10 44 /**
yading@10 45 * Convert SGI RGB332 pixel into PIX_FMT_BGR8
yading@10 46 * SGI RGB332 is packed RGB 3:3:2, 8bpp, (msb)3R 2B 3G(lsb)
yading@10 47 */
yading@10 48 #define RGB332_TO_BGR8(x) (((x << 3) & 0xC0) | ((x << 3) & 0x38) | ((x >> 5) & 7))
yading@10 49
yading@10 50 static av_always_inline void memcpy_rgb332_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
yading@10 51 {
yading@10 52 int i;
yading@10 53 for (i = 0; i < size; i++)
yading@10 54 dst[i] = RGB332_TO_BGR8(src[i]);
yading@10 55 }
yading@10 56
yading@10 57 /**
yading@10 58 * @param[out] dst Destination buffer
yading@10 59 * @param[in] src Source buffer
yading@10 60 * @param src_size Source buffer size (bytes)
yading@10 61 * @param width Width of destination buffer (pixels)
yading@10 62 * @param height Height of destination buffer (pixels)
yading@10 63 * @param linesize Line size of destination buffer (bytes)
yading@10 64 * @return <0 on error
yading@10 65 */
yading@10 66 static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
yading@10 67 {
yading@10 68 const uint8_t *src_end = src + src_size;
yading@10 69 int x = 0, y = 0;
yading@10 70
yading@10 71 #define INC_XY(n) \
yading@10 72 x += n; \
yading@10 73 if (x >= width) { \
yading@10 74 y++; \
yading@10 75 if (y >= height) \
yading@10 76 return 0; \
yading@10 77 x = 0; \
yading@10 78 }
yading@10 79
yading@10 80 while (src_end - src >= 2) {
yading@10 81 uint8_t v = *src++;
yading@10 82 if (v > 0 && v < 0xC0) {
yading@10 83 do {
yading@10 84 int length = FFMIN(v, width - x);
yading@10 85 memset(dst + y*linesize + x, RGB332_TO_BGR8(*src), length);
yading@10 86 INC_XY(length);
yading@10 87 v -= length;
yading@10 88 } while (v > 0);
yading@10 89 src++;
yading@10 90 } else if (v >= 0xC1) {
yading@10 91 v -= 0xC0;
yading@10 92 do {
yading@10 93 int length = FFMIN3(v, width - x, src_end - src);
yading@10 94 if (src_end - src < length)
yading@10 95 break;
yading@10 96 memcpy_rgb332_to_bgr8(dst + y*linesize + x, src, length);
yading@10 97 INC_XY(length);
yading@10 98 src += length;
yading@10 99 v -= length;
yading@10 100 } while (v > 0);
yading@10 101 } else {
yading@10 102 avpriv_request_sample(avctx, "opcode %d", v);
yading@10 103 return AVERROR_PATCHWELCOME;
yading@10 104 }
yading@10 105 }
yading@10 106 return 0;
yading@10 107 }
yading@10 108
yading@10 109 static int sgirle_decode_frame(AVCodecContext *avctx,
yading@10 110 void *data, int *got_frame,
yading@10 111 AVPacket *avpkt)
yading@10 112 {
yading@10 113 SGIRLEContext *s = avctx->priv_data;
yading@10 114 int ret;
yading@10 115
yading@10 116 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
yading@10 117 return ret;
yading@10 118
yading@10 119 ret = decode_sgirle8(avctx, s->frame->data[0], avpkt->data, avpkt->size, avctx->width, avctx->height, s->frame->linesize[0]);
yading@10 120 if (ret < 0)
yading@10 121 return ret;
yading@10 122
yading@10 123 *got_frame = 1;
yading@10 124 if ((ret = av_frame_ref(data, s->frame)) < 0)
yading@10 125 return ret;
yading@10 126
yading@10 127 return avpkt->size;
yading@10 128 }
yading@10 129
yading@10 130 static av_cold int sgirle_decode_end(AVCodecContext *avctx)
yading@10 131 {
yading@10 132 SGIRLEContext *s = avctx->priv_data;
yading@10 133
yading@10 134 av_frame_free(&s->frame);
yading@10 135
yading@10 136 return 0;
yading@10 137 }
yading@10 138
yading@10 139 AVCodec ff_sgirle_decoder = {
yading@10 140 .name = "sgirle",
yading@10 141 .type = AVMEDIA_TYPE_VIDEO,
yading@10 142 .id = AV_CODEC_ID_SGIRLE,
yading@10 143 .priv_data_size = sizeof(SGIRLEContext),
yading@10 144 .init = sgirle_decode_init,
yading@10 145 .close = sgirle_decode_end,
yading@10 146 .decode = sgirle_decode_frame,
yading@10 147 .capabilities = CODEC_CAP_DR1,
yading@10 148 .long_name = NULL_IF_CONFIG_SMALL("SGI RLE 8-bit"),
yading@10 149 };