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') },
38 static int findPixelFormat(unsigned int fourcc)
40 const PixelFormatTag * tags = pixelFormatTags;
41 while (tags->pix_fmt >= 0) {
42 if (tags->fourcc == fourcc)
46 return PIX_FMT_YUV420P;
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 */
57 static int raw_init(AVCodecContext *avctx)
59 RawVideoContext *context = avctx->priv_data;
61 if (avctx->codec_tag) {
62 avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
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;
69 if (! context->buffer) {
76 static int raw_decode(AVCodecContext *avctx,
77 void *data, int *data_size,
78 uint8_t *buf, int buf_size)
80 RawVideoContext *context = avctx->priv_data;
82 AVPicture * picture = (AVPicture *) data;
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);
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;
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);
106 static int raw_close(AVCodecContext *avctx)
108 RawVideoContext *context = avctx->priv_data;
110 av_freep(& context->buffer);
115 static int raw_encode(AVCodecContext *avctx,
116 unsigned char *frame, int buf_size, void *data)
118 AVPicture * picture = data;
121 unsigned char *dest = frame;
124 int w = avctx->width;
125 int h = avctx->height;
126 int size = avpicture_get_size(avctx->pix_fmt, w, h);
128 if (size > buf_size) {
132 switch(avctx->pix_fmt) {
133 case PIX_FMT_YUV420P:
139 src = picture->data[i];
141 memcpy(dest, src, w);
143 src += picture->linesize[i];
147 case PIX_FMT_YUV422P:
152 src = picture->data[i];
154 memcpy(dest, src, w);
156 src += picture->linesize[i];
160 case PIX_FMT_YUV444P:
162 src = picture->data[i];
164 memcpy(dest, src, w);
166 src += picture->linesize[i];
171 src = picture->data[0];
173 memcpy(dest, src, w * 2);
175 src += picture->linesize[0];
180 src = picture->data[0];
182 memcpy(dest, src, w * 3);
184 src += picture->linesize[0];
195 AVCodec rawvideo_codec = {
199 sizeof(RawVideoContext),