raw.c 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
 */
19

20 21 22 23
/**
 * @file raw.c
 * Raw Video Codec
 */
24

25 26
#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

D
Diego Biurrun 已提交
34
typedef struct PixelFormatTag {
35 36 37 38 39
    int pix_fmt;
    unsigned int fourcc;
} PixelFormatTag;

const PixelFormatTag pixelFormatTags[] = {
40 41 42 43 44 45 46 47 48
    { 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') },


M
Michael Niedermayer 已提交
49 50
    { PIX_FMT_YUV422,  MKTAG('Y', 'U', 'Y', '2') }, /* Packed formats */
    { PIX_FMT_YUV422,  MKTAG('Y', '4', '2', '2') },
51
    { PIX_FMT_UYVY422, MKTAG('U', 'Y', 'V', 'Y') },
52 53
    { PIX_FMT_GRAY8,   MKTAG('G', 'R', 'E', 'Y') },

54 55 56 57 58 59 60 61 62 63 64 65 66 67
    { -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 已提交
68
unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
69 70 71 72
{
    const PixelFormatTag * tags = pixelFormatTags;
    while (tags->pix_fmt >= 0) {
        if (tags->pix_fmt == fmt)
73 74
            return tags->fourcc;
        tags++;
75 76 77
    }
    return 0;
}
78

79
/* RAW Decoder Implementation */
80

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

85 86
    if (avctx->codec_tag)
        avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
M
Michael Niedermayer 已提交
87 88 89 90 91 92 93 94
    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;
        }
    }
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;
101

M
Michael Niedermayer 已提交
102
    avctx->coded_frame= &context->pic;
103

104
    if (!context->buffer)
105
        return -1;
106

107 108 109
    return 0;
}

M
Michael Niedermayer 已提交
110 111 112 113 114 115 116
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;
    }
}

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

124
    AVFrame * frame = (AVFrame *) data;
125 126
    AVPicture * picture = (AVPicture *) data;

127 128 129
    frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
    frame->top_field_first = avctx->coded_frame->top_field_first;

130 131 132
    /* 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);
133
        flip(avctx, picture);
134 135 136 137
        *data_size = sizeof(AVPicture);
        return buf_size;
    }

M
10l  
Michael Niedermayer 已提交
138
    bytesNeeded = context->length - (context->p - context->buffer);
139 140 141 142 143 144 145 146 147
    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);
148
    flip(avctx, picture);
149 150 151 152
    *data_size = sizeof(AVPicture);
    return bytesNeeded;
}

153
static int raw_close_decoder(AVCodecContext *avctx)
154 155
{
    RawVideoContext *context = avctx->priv_data;
156

157 158 159
    av_freep(&context->buffer);
    return 0;
}
160

161
/* RAW Encoder Implementation */
162

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

static int raw_encode(AVCodecContext *avctx,
174
                            unsigned char *frame, int buf_size, void *data)
175
{
176 177
    return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
                                               avctx->height, frame, buf_size);
178 179
}

180
#ifdef CONFIG_RAWVIDEO_ENCODER
181 182 183 184 185 186 187 188
AVCodec rawvideo_encoder = {
    "rawvideo",
    CODEC_TYPE_VIDEO,
    CODEC_ID_RAWVIDEO,
    sizeof(AVFrame),
    raw_init_encoder,
    raw_encode,
};
189
#endif // CONFIG_RAWVIDEO_ENCODER
190

191
AVCodec rawvideo_decoder = {
192 193 194 195
    "rawvideo",
    CODEC_TYPE_VIDEO,
    CODEC_ID_RAWVIDEO,
    sizeof(RawVideoContext),
196 197 198
    raw_init_decoder,
    NULL,
    raw_close_decoder,
199 200
    raw_decode,
};