]> git.sesse.net Git - ffmpeg/blob - libavcodec/raw.c
9dc88e842dbe16dcb2c90d942424f1b9ad49095d
[ffmpeg] / 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., 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
28 typedef struct PixleFormatTag {
29     int pix_fmt;
30     unsigned int fourcc;
31 } PixelFormatTag;
32
33 const PixelFormatTag pixelFormatTags[] = {
34     { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') },
35     { -1, 0 },
36 };
37
38 static int findPixelFormat(unsigned int fourcc)
39 {
40     const PixelFormatTag * tags = pixelFormatTags;
41     while (tags->pix_fmt >= 0) {
42         if (tags->fourcc == fourcc)
43             return tags->pix_fmt;
44         tags++;
45     }
46     return PIX_FMT_YUV420P;
47 }
48
49
50 typedef struct RawVideoContext {
51     unsigned char * buffer;  /* block of memory for holding one frame */
52     unsigned char * p;       /* current position in buffer */
53     int             length;  /* number of bytes in buffer */
54 } RawVideoContext;
55
56
57 static int raw_init(AVCodecContext *avctx)
58 {
59     RawVideoContext *context = avctx->priv_data;
60
61     if (avctx->codec_tag) {
62             avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
63     }
64
65         context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
66         context->buffer = av_malloc(context->length);
67         context->p      = context->buffer;
68
69     if (! context->buffer) {
70         return -1;
71     }
72
73     return 0;
74 }
75
76 static int raw_decode(AVCodecContext *avctx,
77                             void *data, int *data_size,
78                             uint8_t *buf, int buf_size)
79 {
80     RawVideoContext *context = avctx->priv_data;
81
82     AVPicture * picture = (AVPicture *) data;
83
84     /* Early out without copy if packet size == frame size */
85     if (buf_size == context->length  &&  context->p == context->buffer) {
86         avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
87         *data_size = sizeof(AVPicture);
88         return buf_size;
89     }
90
91     int bytesNeeded = context->length - (context->p - context->buffer);
92     if (buf_size < bytesNeeded) {
93         memcpy(context->p, buf, buf_size);
94         context->p += buf_size;
95         *data_size = 0;
96         return buf_size;
97     }
98
99     memcpy(context->p, buf, bytesNeeded);
100     context->p = context->buffer;
101     avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
102     *data_size = sizeof(AVPicture);
103     return bytesNeeded;
104 }
105
106 static int raw_close(AVCodecContext *avctx)
107 {
108     RawVideoContext *context = avctx->priv_data;
109
110     av_freep(& context->buffer);
111
112     return 0;
113 }
114
115 static int raw_encode(AVCodecContext *avctx,
116                             unsigned char *frame, int buf_size, void *data)
117 {
118         AVPicture * picture = data;
119
120     unsigned char *src;
121         unsigned char *dest = frame;
122     int i, j;
123
124         int w = avctx->width;
125         int h = avctx->height;
126         int size = avpicture_get_size(avctx->pix_fmt, w, h);
127
128     if (size > buf_size) {
129         return -1;
130     }
131
132     switch(avctx->pix_fmt) {
133     case PIX_FMT_YUV420P:
134         for(i=0;i<3;i++) {
135             if (i == 1) {
136                 w >>= 1;
137                 h >>= 1;
138             }
139             src = picture->data[i];
140             for(j=0;j<h;j++) {
141                 memcpy(dest, src, w);
142                 dest += w;
143                 src += picture->linesize[i];
144             }
145         }
146         break;
147     case PIX_FMT_YUV422P:
148         for(i=0;i<3;i++) {
149             if (i == 1) {
150                 w >>= 1;
151             }
152             src = picture->data[i];
153             for(j=0;j<h;j++) {
154                 memcpy(dest, src, w);
155                 dest += w;
156                 src += picture->linesize[i];
157             }
158         }
159         break;
160     case PIX_FMT_YUV444P:
161         for(i=0;i<3;i++) {
162             src = picture->data[i];
163             for(j=0;j<h;j++) {
164                 memcpy(dest, src, w);
165                 dest += w;
166                 src += picture->linesize[i];
167             }
168         }
169         break;
170     case PIX_FMT_YUV422:
171         src = picture->data[0];
172         for(j=0;j<h;j++) {
173             memcpy(dest, src, w * 2);
174             dest += w * 2;
175             src += picture->linesize[0];
176         }
177         break;
178     case PIX_FMT_RGB24:
179     case PIX_FMT_BGR24:
180         src = picture->data[0];
181         for(j=0;j<h;j++) {
182             memcpy(dest, src, w * 3);
183             dest += w * 3;
184             src += picture->linesize[0];
185         }
186         break;
187     default:
188         return -1;
189     }
190
191     return size;
192 }
193
194
195 AVCodec rawvideo_codec = {
196     "rawvideo",
197     CODEC_TYPE_VIDEO,
198     CODEC_ID_RAWVIDEO,
199     sizeof(RawVideoContext),
200     raw_init,
201     raw_encode,
202     raw_close,
203     raw_decode,
204 };