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
27 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
34 typedef struct PixleFormatTag {
39 const PixelFormatTag pixelFormatTags[] = {
40 { 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') },
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') },
56 static int findPixelFormat(unsigned int fourcc)
58 const PixelFormatTag * tags = pixelFormatTags;
59 while (tags->pix_fmt >= 0) {
60 if (tags->fourcc == fourcc)
64 return PIX_FMT_YUV420P;
67 static unsigned int findFourCC(int fmt)
69 const PixelFormatTag * tags = pixelFormatTags;
70 while (tags->pix_fmt >= 0) {
71 if (tags->pix_fmt == fmt)
78 /* RAW Decoder Implementation */
80 static int raw_init_decoder(AVCodecContext *avctx)
82 RawVideoContext *context = avctx->priv_data;
85 avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
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;
93 avctx->coded_frame= &context->pic;
101 static int raw_decode(AVCodecContext *avctx,
102 void *data, int *data_size,
103 uint8_t *buf, int buf_size)
105 RawVideoContext *context = avctx->priv_data;
108 AVPicture * picture = (AVPicture *) data;
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);
117 bytesNeeded = context->length - (context->p - context->buffer);
118 if (buf_size < bytesNeeded) {
119 memcpy(context->p, buf, buf_size);
120 context->p += buf_size;
125 memcpy(context->p, buf, bytesNeeded);
126 context->p = context->buffer;
127 avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
128 *data_size = sizeof(AVPicture);
132 static int raw_close_decoder(AVCodecContext *avctx)
134 RawVideoContext *context = avctx->priv_data;
136 av_freep(&context->buffer);
140 /* RAW Encoder Implementation */
142 static int raw_init_encoder(AVCodecContext *avctx)
144 avctx->coded_frame = (AVPicture*)avctx->priv_data;
145 avctx->coded_frame->pict_type = FF_I_TYPE;
146 avctx->coded_frame->key_frame = 1;
147 avctx->codec_tag = findFourCC(avctx->pix_fmt);
151 static int raw_encode(AVCodecContext *avctx,
152 unsigned char *frame, int buf_size, void *data)
154 return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
155 avctx->height, frame, buf_size);
158 AVCodec rawvideo_encoder = {
167 AVCodec rawvideo_decoder = {
171 sizeof(RawVideoContext),