3 * Copyright (c) 2001 Fabrice Bellard.
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.
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.
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
28 typedef struct PixleFormatTag {
33 const PixelFormatTag pixelFormatTags[] = {
34 { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') },
35 { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') },
39 static int findPixelFormat(unsigned int fourcc)
41 const PixelFormatTag * tags = pixelFormatTags;
42 while (tags->pix_fmt >= 0) {
43 if (tags->fourcc == fourcc)
47 return PIX_FMT_YUV420P;
51 typedef struct RawVideoContext {
52 unsigned char * buffer; /* block of memory for holding one frame */
53 unsigned char * p; /* current position in buffer */
54 int length; /* number of bytes in buffer */
58 static int raw_init(AVCodecContext *avctx)
60 RawVideoContext *context = avctx->priv_data;
62 if (avctx->codec_tag) {
63 avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
66 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
67 context->buffer = av_malloc(context->length);
68 context->p = context->buffer;
70 if (! context->buffer) {
77 static int raw_decode(AVCodecContext *avctx,
78 void *data, int *data_size,
79 uint8_t *buf, int buf_size)
81 RawVideoContext *context = avctx->priv_data;
84 AVPicture * picture = (AVPicture *) data;
86 /* Early out without copy if packet size == frame size */
87 if (buf_size == context->length && context->p == context->buffer) {
88 avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
89 *data_size = sizeof(AVPicture);
93 bytesNeeded = context->length - (context->p - context->buffer);
94 if (buf_size < bytesNeeded) {
95 memcpy(context->p, buf, buf_size);
96 context->p += buf_size;
101 memcpy(context->p, buf, bytesNeeded);
102 context->p = context->buffer;
103 avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
104 *data_size = sizeof(AVPicture);
108 static int raw_close(AVCodecContext *avctx)
110 RawVideoContext *context = avctx->priv_data;
112 av_freep(& context->buffer);
117 static int raw_encode(AVCodecContext *avctx,
118 unsigned char *frame, int buf_size, void *data)
120 AVPicture * picture = data;
123 unsigned char *dest = frame;
126 int w = avctx->width;
127 int h = avctx->height;
128 int size = avpicture_get_size(avctx->pix_fmt, w, h);
130 if (size > buf_size) {
134 switch(avctx->pix_fmt) {
135 case PIX_FMT_YUV420P:
141 src = picture->data[i];
143 memcpy(dest, src, w);
145 src += picture->linesize[i];
149 case PIX_FMT_YUV422P:
154 src = picture->data[i];
156 memcpy(dest, src, w);
158 src += picture->linesize[i];
162 case PIX_FMT_YUV444P:
164 src = picture->data[i];
166 memcpy(dest, src, w);
168 src += picture->linesize[i];
173 src = picture->data[0];
175 memcpy(dest, src, w * 2);
177 src += picture->linesize[0];
182 src = picture->data[0];
184 memcpy(dest, src, w * 3);
186 src += picture->linesize[0];
197 AVCodec rawvideo_codec = {
201 sizeof(RawVideoContext),