raw.c 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Raw Video Codec
 * Copyright (c) 2001 Fabrice Bellard.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
/**
 * @file raw.c
 * Raw Video Codec
 */
 
#include "avcodec.h"

27 28 29 30 31 32
typedef struct RawVideoContext {
    unsigned char * buffer;  /* block of memory for holding one frame */
    unsigned char * p;       /* current position in buffer */
    int             length;  /* number of bytes in buffer */
    AVFrame pic;             ///< AVCodecContext.coded_frame
} RawVideoContext;
33 34 35 36 37 38 39

typedef struct PixleFormatTag {
    int pix_fmt;
    unsigned int fourcc;
} PixelFormatTag;

const PixelFormatTag pixelFormatTags[] = {
40 41 42 43 44 45 46 47 48 49
    { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, /* Planar formats */
    { PIX_FMT_YUV420P, MKTAG('I', 'Y', 'U', 'V') },
    { PIX_FMT_YUV410P, MKTAG('Y', 'U', 'V', '9') },
    { PIX_FMT_YUV411P, MKTAG('Y', '4', '1', 'B') },
    { PIX_FMT_YUV422P, MKTAG('Y', '4', '2', 'B') },
    { PIX_FMT_GRAY8,   MKTAG('Y', '8', '0', '0') },
    { PIX_FMT_GRAY8,   MKTAG(' ', ' ', 'Y', '8') },


    { PIX_FMT_YUV422,  MKTAG('Y', '4', '2', '2') }, /* Packed formats */
50
    { PIX_FMT_UYVY422, MKTAG('U', 'Y', 'V', 'Y') },
51 52
    { PIX_FMT_GRAY8,   MKTAG('G', 'R', 'E', 'Y') },

53 54 55 56 57 58 59 60 61 62 63 64 65 66
    { -1, 0 },
};

static int findPixelFormat(unsigned int fourcc)
{
    const PixelFormatTag * tags = pixelFormatTags;
    while (tags->pix_fmt >= 0) {
        if (tags->fourcc == fourcc)
            return tags->pix_fmt;
        tags++;
    }
    return PIX_FMT_YUV420P;
}

M
Michael Niedermayer 已提交
67
unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
68 69 70 71 72 73 74 75 76
{
    const PixelFormatTag * tags = pixelFormatTags;
    while (tags->pix_fmt >= 0) {
        if (tags->pix_fmt == fmt)
	    return tags->fourcc;
	tags++; 
    }
    return 0;
}
77

78
/* RAW Decoder Implementation */
79

80
static int raw_init_decoder(AVCodecContext *avctx)
81 82 83
{
    RawVideoContext *context = avctx->priv_data;

84 85
    if (avctx->codec_tag)
        avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
M
Michael Niedermayer 已提交
86 87 88 89 90 91 92 93
    else if (avctx->bits_per_sample){
        switch(avctx->bits_per_sample){
        case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
        case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
        case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
        case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
        }
    }
94 95 96 97 98 99 100
    
    context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
    context->buffer = av_malloc(context->length);
    context->p      = context->buffer;
    context->pic.pict_type = FF_I_TYPE;
    context->pic.key_frame = 1;
    
M
Michael Niedermayer 已提交
101 102
    avctx->coded_frame= &context->pic;
    
103
    if (!context->buffer)
104
        return -1;
105
   
106 107 108
    return 0;
}

M
Michael Niedermayer 已提交
109 110 111 112 113 114 115
static void flip(AVCodecContext *avctx, AVPicture * picture){
    if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[1]==0){
        picture->data[0] += picture->linesize[0] * (avctx->height-1);
        picture->linesize[0] *= -1;
    }
}

116 117 118 119 120
static int raw_decode(AVCodecContext *avctx,
			    void *data, int *data_size,
			    uint8_t *buf, int buf_size)
{
    RawVideoContext *context = avctx->priv_data;
M
10l  
Michael Niedermayer 已提交
121
    int bytesNeeded;
122 123 124 125 126 127

    AVPicture * picture = (AVPicture *) data;

    /* Early out without copy if packet size == frame size */
    if (buf_size == context->length  &&  context->p == context->buffer) {
        avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
M
Michael Niedermayer 已提交
128
        flip(avctx, picture);        
129 130 131 132
        *data_size = sizeof(AVPicture);
        return buf_size;
    }

M
10l  
Michael Niedermayer 已提交
133
    bytesNeeded = context->length - (context->p - context->buffer);
134 135 136 137 138 139 140 141 142
    if (buf_size < bytesNeeded) {
        memcpy(context->p, buf, buf_size);
        context->p += buf_size;
        return buf_size;
    }

    memcpy(context->p, buf, bytesNeeded);
    context->p = context->buffer;
    avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
M
Michael Niedermayer 已提交
143
    flip(avctx, picture);        
144 145 146 147
    *data_size = sizeof(AVPicture);
    return bytesNeeded;
}

148
static int raw_close_decoder(AVCodecContext *avctx)
149 150
{
    RawVideoContext *context = avctx->priv_data;
151 152 153 154
    
    av_freep(&context->buffer);
    return 0;
}
155

156
/* RAW Encoder Implementation */
157

158 159
static int raw_init_encoder(AVCodecContext *avctx)
{
F
Falk Hüffner 已提交
160
    avctx->coded_frame = (AVFrame *)avctx->priv_data;
161 162
    avctx->coded_frame->pict_type = FF_I_TYPE;
    avctx->coded_frame->key_frame = 1;
163
    if(!avctx->codec_tag)
M
Michael Niedermayer 已提交
164
        avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
165 166 167 168 169 170
    return 0;
}

static int raw_encode(AVCodecContext *avctx,
			    unsigned char *frame, int buf_size, void *data)
{
171 172
    return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
                                               avctx->height, frame, buf_size);
173 174
}

175 176 177 178 179 180 181 182
AVCodec rawvideo_encoder = {
    "rawvideo",
    CODEC_TYPE_VIDEO,
    CODEC_ID_RAWVIDEO,
    sizeof(AVFrame),
    raw_init_encoder,
    raw_encode,
};
183

184
AVCodec rawvideo_decoder = {
185 186 187 188
    "rawvideo",
    CODEC_TYPE_VIDEO,
    CODEC_ID_RAWVIDEO,
    sizeof(RawVideoContext),
189 190 191
    raw_init_decoder,
    NULL,
    raw_close_decoder,
192 193
    raw_decode,
};