Statistics
| Branch: | Revision:

ffmpeg / libavcodec / raw.c @ 64c968da

History | View | Annotate | Download (5.65 KB)

1 e8750b00 Fred Rothganger
/*
2
 * Raw Video Codec
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19 115329f1 Diego Biurrun
20 e8750b00 Fred Rothganger
/**
21
 * @file raw.c
22
 * Raw Video Codec
23
 */
24 115329f1 Diego Biurrun
25 e8750b00 Fred Rothganger
#include "avcodec.h"
26
27 63167088 Roman Shaposhnik
typedef struct RawVideoContext {
28
    unsigned char * buffer;  /* block of memory for holding one frame */
29
    unsigned char * p;       /* current position in buffer */
30
    int             length;  /* number of bytes in buffer */
31
    AVFrame pic;             ///< AVCodecContext.coded_frame
32
} RawVideoContext;
33 e8750b00 Fred Rothganger
34
typedef struct PixleFormatTag {
35
    int pix_fmt;
36
    unsigned int fourcc;
37
} PixelFormatTag;
38
39
const PixelFormatTag pixelFormatTags[] = {
40 63167088 Roman Shaposhnik
    { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, /* Planar formats */
41
    { PIX_FMT_YUV420P, MKTAG('I', 'Y', 'U', 'V') },
42
    { PIX_FMT_YUV410P, MKTAG('Y', 'U', 'V', '9') },
43
    { PIX_FMT_YUV411P, MKTAG('Y', '4', '1', 'B') },
44
    { PIX_FMT_YUV422P, MKTAG('Y', '4', '2', 'B') },
45
    { PIX_FMT_GRAY8,   MKTAG('Y', '8', '0', '0') },
46
    { PIX_FMT_GRAY8,   MKTAG(' ', ' ', 'Y', '8') },
47
48
49 d57d8cbc Michael Niedermayer
    { PIX_FMT_YUV422,  MKTAG('Y', 'U', 'Y', '2') }, /* Packed formats */
50
    { PIX_FMT_YUV422,  MKTAG('Y', '4', '2', '2') },
51 3dc10d47 Todd Kirby
    { PIX_FMT_UYVY422, MKTAG('U', 'Y', 'V', 'Y') },
52 63167088 Roman Shaposhnik
    { PIX_FMT_GRAY8,   MKTAG('G', 'R', 'E', 'Y') },
53
54 e8750b00 Fred Rothganger
    { -1, 0 },
55
};
56
57
static int findPixelFormat(unsigned int fourcc)
58
{
59
    const PixelFormatTag * tags = pixelFormatTags;
60
    while (tags->pix_fmt >= 0) {
61
        if (tags->fourcc == fourcc)
62
            return tags->pix_fmt;
63
        tags++;
64
    }
65
    return PIX_FMT_YUV420P;
66
}
67
68 d07730dd Michael Niedermayer
unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
69 63167088 Roman Shaposhnik
{
70
    const PixelFormatTag * tags = pixelFormatTags;
71
    while (tags->pix_fmt >= 0) {
72
        if (tags->pix_fmt == fmt)
73
            return tags->fourcc;
74 115329f1 Diego Biurrun
        tags++;
75 63167088 Roman Shaposhnik
    }
76
    return 0;
77
}
78 e8750b00 Fred Rothganger
79 63167088 Roman Shaposhnik
/* RAW Decoder Implementation */
80 e8750b00 Fred Rothganger
81 63167088 Roman Shaposhnik
static int raw_init_decoder(AVCodecContext *avctx)
82 e8750b00 Fred Rothganger
{
83
    RawVideoContext *context = avctx->priv_data;
84
85 63167088 Roman Shaposhnik
    if (avctx->codec_tag)
86
        avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
87 627c50b7 Michael Niedermayer
    else if (avctx->bits_per_sample){
88
        switch(avctx->bits_per_sample){
89
        case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
90
        case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
91
        case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
92
        case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
93
        }
94
    }
95 115329f1 Diego Biurrun
96 63167088 Roman Shaposhnik
    context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
97
    context->buffer = av_malloc(context->length);
98
    context->p      = context->buffer;
99
    context->pic.pict_type = FF_I_TYPE;
100
    context->pic.key_frame = 1;
101 115329f1 Diego Biurrun
102 95342235 Michael Niedermayer
    avctx->coded_frame= &context->pic;
103 115329f1 Diego Biurrun
104 63167088 Roman Shaposhnik
    if (!context->buffer)
105 e8750b00 Fred Rothganger
        return -1;
106 115329f1 Diego Biurrun
107 e8750b00 Fred Rothganger
    return 0;
108
}
109
110 9d2a0695 Michael Niedermayer
static void flip(AVCodecContext *avctx, AVPicture * picture){
111
    if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[1]==0){
112
        picture->data[0] += picture->linesize[0] * (avctx->height-1);
113
        picture->linesize[0] *= -1;
114
    }
115
}
116
117 e8750b00 Fred Rothganger
static int raw_decode(AVCodecContext *avctx,
118
                            void *data, int *data_size,
119
                            uint8_t *buf, int buf_size)
120
{
121
    RawVideoContext *context = avctx->priv_data;
122 6c949d12 Michael Niedermayer
    int bytesNeeded;
123 e8750b00 Fred Rothganger
124
    AVPicture * picture = (AVPicture *) data;
125
126
    /* Early out without copy if packet size == frame size */
127
    if (buf_size == context->length  &&  context->p == context->buffer) {
128
        avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
129 115329f1 Diego Biurrun
        flip(avctx, picture);
130 e8750b00 Fred Rothganger
        *data_size = sizeof(AVPicture);
131
        return buf_size;
132
    }
133
134 6c949d12 Michael Niedermayer
    bytesNeeded = context->length - (context->p - context->buffer);
135 e8750b00 Fred Rothganger
    if (buf_size < bytesNeeded) {
136
        memcpy(context->p, buf, buf_size);
137
        context->p += buf_size;
138
        return buf_size;
139
    }
140
141
    memcpy(context->p, buf, bytesNeeded);
142
    context->p = context->buffer;
143
    avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
144 115329f1 Diego Biurrun
    flip(avctx, picture);
145 e8750b00 Fred Rothganger
    *data_size = sizeof(AVPicture);
146
    return bytesNeeded;
147
}
148
149 63167088 Roman Shaposhnik
static int raw_close_decoder(AVCodecContext *avctx)
150 e8750b00 Fred Rothganger
{
151
    RawVideoContext *context = avctx->priv_data;
152 115329f1 Diego Biurrun
153 63167088 Roman Shaposhnik
    av_freep(&context->buffer);
154
    return 0;
155
}
156 e8750b00 Fred Rothganger
157 63167088 Roman Shaposhnik
/* RAW Encoder Implementation */
158 e8750b00 Fred Rothganger
159 63167088 Roman Shaposhnik
static int raw_init_encoder(AVCodecContext *avctx)
160
{
161 3db320ea Falk H├╝ffner
    avctx->coded_frame = (AVFrame *)avctx->priv_data;
162 63167088 Roman Shaposhnik
    avctx->coded_frame->pict_type = FF_I_TYPE;
163
    avctx->coded_frame->key_frame = 1;
164 6d24231e Michael Niedermayer
    if(!avctx->codec_tag)
165 d07730dd Michael Niedermayer
        avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
166 e8750b00 Fred Rothganger
    return 0;
167
}
168
169
static int raw_encode(AVCodecContext *avctx,
170
                            unsigned char *frame, int buf_size, void *data)
171
{
172 63167088 Roman Shaposhnik
    return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
173
                                               avctx->height, frame, buf_size);
174 e8750b00 Fred Rothganger
}
175
176 983ea0bc Roine Gustafsson
#ifdef CONFIG_RAWVIDEO_ENCODER
177 63167088 Roman Shaposhnik
AVCodec rawvideo_encoder = {
178
    "rawvideo",
179
    CODEC_TYPE_VIDEO,
180
    CODEC_ID_RAWVIDEO,
181
    sizeof(AVFrame),
182
    raw_init_encoder,
183
    raw_encode,
184
};
185 983ea0bc Roine Gustafsson
#endif // CONFIG_RAWVIDEO_ENCODER
186 e8750b00 Fred Rothganger
187 63167088 Roman Shaposhnik
AVCodec rawvideo_decoder = {
188 e8750b00 Fred Rothganger
    "rawvideo",
189
    CODEC_TYPE_VIDEO,
190
    CODEC_ID_RAWVIDEO,
191
    sizeof(RawVideoContext),
192 63167088 Roman Shaposhnik
    raw_init_decoder,
193
    NULL,
194
    raw_close_decoder,
195 e8750b00 Fred Rothganger
    raw_decode,
196
};