Statistics
| Branch: | Revision:

ffmpeg / libavcodec / raw.c @ 20646267

History | View | Annotate | Download (4.88 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
 
20
/**
21
 * @file raw.c
22
 * Raw Video Codec
23
 */
24
 
25
#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
    { PIX_FMT_YUV422,  MKTAG('Y', '4', '2', '2') }, /* Packed formats */
50
    { PIX_FMT_YUV422,  MKTAG('U', 'Y', 'V', 'Y') },
51
    { PIX_FMT_GRAY8,   MKTAG('G', 'R', 'E', 'Y') },
52
53 e8750b00 Fred Rothganger
    { -1, 0 },
54
};
55
56
static int findPixelFormat(unsigned int fourcc)
57
{
58
    const PixelFormatTag * tags = pixelFormatTags;
59
    while (tags->pix_fmt >= 0) {
60
        if (tags->fourcc == fourcc)
61
            return tags->pix_fmt;
62
        tags++;
63
    }
64
    return PIX_FMT_YUV420P;
65
}
66
67 63167088 Roman Shaposhnik
static unsigned int findFourCC(int fmt)
68
{
69
    const PixelFormatTag * tags = pixelFormatTags;
70
    while (tags->pix_fmt >= 0) {
71
        if (tags->pix_fmt == fmt)
72
            return tags->fourcc;
73
        tags++; 
74
    }
75
    return 0;
76
}
77 e8750b00 Fred Rothganger
78 63167088 Roman Shaposhnik
/* RAW Decoder Implementation */
79 e8750b00 Fred Rothganger
80 63167088 Roman Shaposhnik
static int raw_init_decoder(AVCodecContext *avctx)
81 e8750b00 Fred Rothganger
{
82
    RawVideoContext *context = avctx->priv_data;
83
84 63167088 Roman Shaposhnik
    if (avctx->codec_tag)
85
        avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
86
    
87
    context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
88
    context->buffer = av_malloc(context->length);
89
    context->p      = context->buffer;
90
    context->pic.pict_type = FF_I_TYPE;
91
    context->pic.key_frame = 1;
92
    
93 95342235 Michael Niedermayer
    avctx->coded_frame= &context->pic;
94
    
95 63167088 Roman Shaposhnik
    if (!context->buffer)
96 e8750b00 Fred Rothganger
        return -1;
97 63167088 Roman Shaposhnik
   
98 e8750b00 Fred Rothganger
    return 0;
99
}
100
101
static int raw_decode(AVCodecContext *avctx,
102
                            void *data, int *data_size,
103
                            uint8_t *buf, int buf_size)
104
{
105
    RawVideoContext *context = avctx->priv_data;
106 6c949d12 Michael Niedermayer
    int bytesNeeded;
107 e8750b00 Fred Rothganger
108
    AVPicture * picture = (AVPicture *) data;
109
110
    /* Early out without copy if packet size == frame size */
111
    if (buf_size == context->length  &&  context->p == context->buffer) {
112
        avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
113
        *data_size = sizeof(AVPicture);
114
        return buf_size;
115
    }
116
117 6c949d12 Michael Niedermayer
    bytesNeeded = context->length - (context->p - context->buffer);
118 e8750b00 Fred Rothganger
    if (buf_size < bytesNeeded) {
119
        memcpy(context->p, buf, buf_size);
120
        context->p += buf_size;
121
        return buf_size;
122
    }
123
124
    memcpy(context->p, buf, bytesNeeded);
125
    context->p = context->buffer;
126
    avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
127
    *data_size = sizeof(AVPicture);
128
    return bytesNeeded;
129
}
130
131 63167088 Roman Shaposhnik
static int raw_close_decoder(AVCodecContext *avctx)
132 e8750b00 Fred Rothganger
{
133
    RawVideoContext *context = avctx->priv_data;
134 63167088 Roman Shaposhnik
    
135
    av_freep(&context->buffer);
136
    return 0;
137
}
138 e8750b00 Fred Rothganger
139 63167088 Roman Shaposhnik
/* RAW Encoder Implementation */
140 e8750b00 Fred Rothganger
141 63167088 Roman Shaposhnik
static int raw_init_encoder(AVCodecContext *avctx)
142
{
143 3db320ea Falk H├╝ffner
    avctx->coded_frame = (AVFrame *)avctx->priv_data;
144 63167088 Roman Shaposhnik
    avctx->coded_frame->pict_type = FF_I_TYPE;
145
    avctx->coded_frame->key_frame = 1;
146
    avctx->codec_tag = findFourCC(avctx->pix_fmt);
147 e8750b00 Fred Rothganger
    return 0;
148
}
149
150
static int raw_encode(AVCodecContext *avctx,
151
                            unsigned char *frame, int buf_size, void *data)
152
{
153 63167088 Roman Shaposhnik
    return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
154
                                               avctx->height, frame, buf_size);
155 e8750b00 Fred Rothganger
}
156
157 63167088 Roman Shaposhnik
AVCodec rawvideo_encoder = {
158
    "rawvideo",
159
    CODEC_TYPE_VIDEO,
160
    CODEC_ID_RAWVIDEO,
161
    sizeof(AVFrame),
162
    raw_init_encoder,
163
    raw_encode,
164
};
165 e8750b00 Fred Rothganger
166 63167088 Roman Shaposhnik
AVCodec rawvideo_decoder = {
167 e8750b00 Fred Rothganger
    "rawvideo",
168
    CODEC_TYPE_VIDEO,
169
    CODEC_ID_RAWVIDEO,
170
    sizeof(RawVideoContext),
171 63167088 Roman Shaposhnik
    raw_init_decoder,
172
    NULL,
173
    raw_close_decoder,
174 e8750b00 Fred Rothganger
    raw_decode,
175
};