annotate ffmpeg/libavcodec/sunrastenc.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 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
yading@10 3 * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
yading@10 4 *
yading@10 5 * This file is part of Libav.
yading@10 6 *
yading@10 7 * Libav 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 * Libav 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 Libav; 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 #include "avcodec.h"
yading@10 23 #include "bytestream.h"
yading@10 24 #include "internal.h"
yading@10 25 #include "sunrast.h"
yading@10 26
yading@10 27 typedef struct SUNRASTContext {
yading@10 28 AVFrame picture;
yading@10 29 PutByteContext p;
yading@10 30 int depth; ///< depth of pixel
yading@10 31 int length; ///< length (bytes) of image
yading@10 32 int type; ///< type of file
yading@10 33 int maptype; ///< type of colormap
yading@10 34 int maplength; ///< length (bytes) of colormap
yading@10 35 int size;
yading@10 36 } SUNRASTContext;
yading@10 37
yading@10 38 static void sunrast_image_write_header(AVCodecContext *avctx)
yading@10 39 {
yading@10 40 SUNRASTContext *s = avctx->priv_data;
yading@10 41
yading@10 42 bytestream2_put_be32u(&s->p, RAS_MAGIC);
yading@10 43 bytestream2_put_be32u(&s->p, avctx->width);
yading@10 44 bytestream2_put_be32u(&s->p, avctx->height);
yading@10 45 bytestream2_put_be32u(&s->p, s->depth);
yading@10 46 bytestream2_put_be32u(&s->p, s->length);
yading@10 47 bytestream2_put_be32u(&s->p, s->type);
yading@10 48 bytestream2_put_be32u(&s->p, s->maptype);
yading@10 49 bytestream2_put_be32u(&s->p, s->maplength);
yading@10 50 }
yading@10 51
yading@10 52 static void sunrast_image_write_image(AVCodecContext *avctx,
yading@10 53 const uint8_t *pixels,
yading@10 54 const uint32_t *palette_data,
yading@10 55 int linesize)
yading@10 56 {
yading@10 57 SUNRASTContext *s = avctx->priv_data;
yading@10 58 const uint8_t *ptr;
yading@10 59 int len, alen, x, y;
yading@10 60
yading@10 61 if (s->maplength) { // palettized
yading@10 62 PutByteContext pb_r, pb_g;
yading@10 63 int len = s->maplength / 3;
yading@10 64
yading@10 65 pb_r = s->p;
yading@10 66 bytestream2_skip_p(&s->p, len);
yading@10 67 pb_g = s->p;
yading@10 68 bytestream2_skip_p(&s->p, len);
yading@10 69
yading@10 70 for (x = 0; x < len; x++) {
yading@10 71 uint32_t pixel = palette_data[x];
yading@10 72
yading@10 73 bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
yading@10 74 bytestream2_put_byteu(&pb_g, (pixel >> 8) & 0xFF);
yading@10 75 bytestream2_put_byteu(&s->p, pixel & 0xFF);
yading@10 76 }
yading@10 77 }
yading@10 78
yading@10 79 len = (s->depth * avctx->width + 7) >> 3;
yading@10 80 alen = len + (len & 1);
yading@10 81 ptr = pixels;
yading@10 82
yading@10 83 if (s->type == RT_BYTE_ENCODED) {
yading@10 84 uint8_t value, value2;
yading@10 85 int run;
yading@10 86
yading@10 87 ptr = pixels;
yading@10 88
yading@10 89 #define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
yading@10 90
yading@10 91 x = 0, y = 0;
yading@10 92 value2 = GET_VALUE;
yading@10 93 while (y < avctx->height) {
yading@10 94 run = 1;
yading@10 95 value = value2;
yading@10 96 x++;
yading@10 97 if (x >= alen) {
yading@10 98 x = 0;
yading@10 99 ptr += linesize, y++;
yading@10 100 }
yading@10 101
yading@10 102 value2 = GET_VALUE;
yading@10 103 while (value2 == value && run < 256 && y < avctx->height) {
yading@10 104 x++;
yading@10 105 run++;
yading@10 106 if (x >= alen) {
yading@10 107 x = 0;
yading@10 108 ptr += linesize, y++;
yading@10 109 }
yading@10 110 value2 = GET_VALUE;
yading@10 111 }
yading@10 112
yading@10 113 if (run > 2 || value == RLE_TRIGGER) {
yading@10 114 bytestream2_put_byteu(&s->p, RLE_TRIGGER);
yading@10 115 bytestream2_put_byteu(&s->p, run - 1);
yading@10 116 if (run > 1)
yading@10 117 bytestream2_put_byteu(&s->p, value);
yading@10 118 } else if (run == 1) {
yading@10 119 bytestream2_put_byteu(&s->p, value);
yading@10 120 } else
yading@10 121 bytestream2_put_be16u(&s->p, (value << 8) | value);
yading@10 122 }
yading@10 123
yading@10 124 // update data length for header
yading@10 125 s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
yading@10 126 } else {
yading@10 127 for (y = 0; y < avctx->height; y++) {
yading@10 128 bytestream2_put_buffer(&s->p, ptr, len);
yading@10 129 if (len < alen)
yading@10 130 bytestream2_put_byteu(&s->p, 0);
yading@10 131 ptr += linesize;
yading@10 132 }
yading@10 133 }
yading@10 134 }
yading@10 135
yading@10 136 static av_cold int sunrast_encode_init(AVCodecContext *avctx)
yading@10 137 {
yading@10 138 SUNRASTContext *s = avctx->priv_data;
yading@10 139
yading@10 140 switch (avctx->coder_type) {
yading@10 141 case FF_CODER_TYPE_RLE:
yading@10 142 s->type = RT_BYTE_ENCODED;
yading@10 143 break;
yading@10 144 case FF_CODER_TYPE_RAW:
yading@10 145 s->type = RT_STANDARD;
yading@10 146 break;
yading@10 147 default:
yading@10 148 av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
yading@10 149 return AVERROR(EINVAL);
yading@10 150 }
yading@10 151
yading@10 152 avctx->coded_frame = &s->picture;
yading@10 153 avctx->coded_frame->key_frame = 1;
yading@10 154 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
yading@10 155 s->maptype = RMT_NONE;
yading@10 156 s->maplength = 0;
yading@10 157
yading@10 158 switch (avctx->pix_fmt) {
yading@10 159 case AV_PIX_FMT_MONOWHITE:
yading@10 160 s->depth = 1;
yading@10 161 break;
yading@10 162 case AV_PIX_FMT_PAL8 :
yading@10 163 s->maptype = RMT_EQUAL_RGB;
yading@10 164 s->maplength = 3 * 256;
yading@10 165 case AV_PIX_FMT_GRAY8:
yading@10 166 s->depth = 8;
yading@10 167 break;
yading@10 168 case AV_PIX_FMT_BGR24:
yading@10 169 s->depth = 24;
yading@10 170 break;
yading@10 171 default:
yading@10 172 return AVERROR_BUG;
yading@10 173 }
yading@10 174 s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
yading@10 175 s->size = 32 + s->maplength +
yading@10 176 s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
yading@10 177
yading@10 178 return 0;
yading@10 179 }
yading@10 180
yading@10 181 static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
yading@10 182 const AVFrame *frame, int *got_packet_ptr)
yading@10 183 {
yading@10 184 SUNRASTContext *s = avctx->priv_data;
yading@10 185 int ret;
yading@10 186
yading@10 187 if ((ret = ff_alloc_packet2(avctx, avpkt, s->size)) < 0)
yading@10 188 return ret;
yading@10 189
yading@10 190 bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
yading@10 191 sunrast_image_write_header(avctx);
yading@10 192 sunrast_image_write_image(avctx, frame->data[0],
yading@10 193 (const uint32_t *)frame->data[1],
yading@10 194 frame->linesize[0]);
yading@10 195 // update data length in header after RLE
yading@10 196 if (s->type == RT_BYTE_ENCODED)
yading@10 197 AV_WB32(&avpkt->data[16], s->length);
yading@10 198
yading@10 199 *got_packet_ptr = 1;
yading@10 200 avpkt->flags |= AV_PKT_FLAG_KEY;
yading@10 201 avpkt->size = bytestream2_tell_p(&s->p);
yading@10 202 return 0;
yading@10 203 }
yading@10 204
yading@10 205 static const AVCodecDefault sunrast_defaults[] = {
yading@10 206 { "coder", "rle" },
yading@10 207 { NULL },
yading@10 208 };
yading@10 209
yading@10 210 AVCodec ff_sunrast_encoder = {
yading@10 211 .name = "sunrast",
yading@10 212 .type = AVMEDIA_TYPE_VIDEO,
yading@10 213 .id = AV_CODEC_ID_SUNRAST,
yading@10 214 .priv_data_size = sizeof(SUNRASTContext),
yading@10 215 .init = sunrast_encode_init,
yading@10 216 .encode2 = sunrast_encode_frame,
yading@10 217 .defaults = sunrast_defaults,
yading@10 218 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
yading@10 219 AV_PIX_FMT_PAL8,
yading@10 220 AV_PIX_FMT_GRAY8,
yading@10 221 AV_PIX_FMT_MONOWHITE,
yading@10 222 AV_PIX_FMT_NONE },
yading@10 223 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
yading@10 224 };