]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/raw.c
Add YV12 support, patch by Steve Lhomme % steve P lhomme A free P fr %
[frescor/ffmpeg.git] / libavcodec / raw.c
1 /*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 /**
21  * @file raw.c
22  * Raw Video Codec
23  */
24
25 #include "avcodec.h"
26
27 typedef struct RawVideoContext {
28     unsigned char * buffer;  /* block of memory for holding one frame */
29     int             length;  /* number of bytes in buffer */
30     AVFrame pic;             ///< AVCodecContext.coded_frame
31 } RawVideoContext;
32
33 typedef struct PixelFormatTag {
34     int pix_fmt;
35     unsigned int fourcc;
36 } PixelFormatTag;
37
38 const PixelFormatTag pixelFormatTags[] = {
39     { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, /* Planar formats */
40     { PIX_FMT_YUV420P, MKTAG('I', 'Y', 'U', 'V') },
41     { PIX_FMT_YUV420P, MKTAG('Y', 'V', '1', '2') },
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', 'U', 'Y', '2') }, /* Packed formats */
50     { PIX_FMT_YUV422,  MKTAG('Y', '4', '2', '2') },
51     { PIX_FMT_UYVY422, MKTAG('U', 'Y', 'V', 'Y') },
52     { PIX_FMT_GRAY8,   MKTAG('G', 'R', 'E', 'Y') },
53
54     /* quicktime */
55     { PIX_FMT_UYVY422, MKTAG('2', 'v', 'u', 'y') },
56
57     { -1, 0 },
58 };
59
60 static int findPixelFormat(unsigned int fourcc)
61 {
62     const PixelFormatTag * tags = pixelFormatTags;
63     while (tags->pix_fmt >= 0) {
64         if (tags->fourcc == fourcc)
65             return tags->pix_fmt;
66         tags++;
67     }
68     return PIX_FMT_YUV420P;
69 }
70
71 unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
72 {
73     const PixelFormatTag * tags = pixelFormatTags;
74     while (tags->pix_fmt >= 0) {
75         if (tags->pix_fmt == fmt)
76             return tags->fourcc;
77         tags++;
78     }
79     return 0;
80 }
81
82 /* RAW Decoder Implementation */
83
84 static int raw_init_decoder(AVCodecContext *avctx)
85 {
86     RawVideoContext *context = avctx->priv_data;
87
88     if (avctx->codec_tag)
89         avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
90     else if (avctx->bits_per_sample){
91         switch(avctx->bits_per_sample){
92         case  8: avctx->pix_fmt= PIX_FMT_PAL8  ; break;
93         case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
94         case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
95         case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
96         case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
97         }
98     }
99
100     context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
101     context->buffer = av_malloc(context->length);
102     context->pic.pict_type = FF_I_TYPE;
103     context->pic.key_frame = 1;
104
105     avctx->coded_frame= &context->pic;
106
107     if (!context->buffer)
108         return -1;
109
110     return 0;
111 }
112
113 static void flip(AVCodecContext *avctx, AVPicture * picture){
114     if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[2]==0){
115         picture->data[0] += picture->linesize[0] * (avctx->height-1);
116         picture->linesize[0] *= -1;
117     }
118 }
119
120 static int raw_decode(AVCodecContext *avctx,
121                             void *data, int *data_size,
122                             uint8_t *buf, int buf_size)
123 {
124     RawVideoContext *context = avctx->priv_data;
125
126     AVFrame * frame = (AVFrame *) data;
127     AVPicture * picture = (AVPicture *) data;
128
129     frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
130     frame->top_field_first = avctx->coded_frame->top_field_first;
131
132     if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
133         return -1;
134
135     avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
136     if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){
137         frame->data[1]= context->buffer;
138     }
139     if (avctx->palctrl && avctx->palctrl->palette_changed) {
140         memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
141         avctx->palctrl->palette_changed = 0;
142     }
143
144     flip(avctx, picture);
145
146     if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2'))
147     {
148         // swap fields
149         unsigned char *tmp = picture->data[1];
150         picture->data[1] = picture->data[2];
151         picture->data[2] = tmp;
152     }
153
154     *data_size = sizeof(AVPicture);
155     return buf_size;
156 }
157
158 static int raw_close_decoder(AVCodecContext *avctx)
159 {
160     RawVideoContext *context = avctx->priv_data;
161
162     av_freep(&context->buffer);
163     return 0;
164 }
165
166 /* RAW Encoder Implementation */
167
168 static int raw_init_encoder(AVCodecContext *avctx)
169 {
170     avctx->coded_frame = (AVFrame *)avctx->priv_data;
171     avctx->coded_frame->pict_type = FF_I_TYPE;
172     avctx->coded_frame->key_frame = 1;
173     if(!avctx->codec_tag)
174         avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
175     return 0;
176 }
177
178 static int raw_encode(AVCodecContext *avctx,
179                             unsigned char *frame, int buf_size, void *data)
180 {
181     return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
182                                                avctx->height, frame, buf_size);
183 }
184
185 #ifdef CONFIG_RAWVIDEO_ENCODER
186 AVCodec rawvideo_encoder = {
187     "rawvideo",
188     CODEC_TYPE_VIDEO,
189     CODEC_ID_RAWVIDEO,
190     sizeof(AVFrame),
191     raw_init_encoder,
192     raw_encode,
193 };
194 #endif // CONFIG_RAWVIDEO_ENCODER
195
196 AVCodec rawvideo_decoder = {
197     "rawvideo",
198     CODEC_TYPE_VIDEO,
199     CODEC_ID_RAWVIDEO,
200     sizeof(RawVideoContext),
201     raw_init_decoder,
202     NULL,
203     raw_close_decoder,
204     raw_decode,
205 };