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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 PixelFormatTag {
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', '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') },
57 static int findPixelFormat(unsigned int fourcc)
59 const PixelFormatTag * tags = pixelFormatTags;
60 while (tags->pix_fmt >= 0) {
61 if (tags->fourcc == fourcc)
65 return PIX_FMT_YUV420P;
68 unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
70 const PixelFormatTag * tags = pixelFormatTags;
71 while (tags->pix_fmt >= 0) {
72 if (tags->pix_fmt == fmt)
79 /* RAW Decoder Implementation */
81 static int raw_init_decoder(AVCodecContext *avctx)
83 RawVideoContext *context = avctx->priv_data;
86 avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
87 else if (avctx->bits_per_sample){
88 switch(avctx->bits_per_sample){
89 case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
90 case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
91 case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
92 case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
96 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
97 context->buffer = av_malloc(context->length);
98 context->p = context->buffer;
99 context->pic.pict_type = FF_I_TYPE;
100 context->pic.key_frame = 1;
102 avctx->coded_frame= &context->pic;
104 if (!context->buffer)
110 static void flip(AVCodecContext *avctx, AVPicture * picture){
111 if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[1]==0){
112 picture->data[0] += picture->linesize[0] * (avctx->height-1);
113 picture->linesize[0] *= -1;
117 static int raw_decode(AVCodecContext *avctx,
118 void *data, int *data_size,
119 uint8_t *buf, int buf_size)
121 RawVideoContext *context = avctx->priv_data;
124 AVFrame * frame = (AVFrame *) data;
125 AVPicture * picture = (AVPicture *) data;
127 frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
128 frame->top_field_first = avctx->coded_frame->top_field_first;
130 /* Early out without copy if packet size == frame size */
131 if (buf_size == context->length && context->p == context->buffer) {
132 avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
133 flip(avctx, picture);
134 *data_size = sizeof(AVPicture);
138 bytesNeeded = context->length - (context->p - context->buffer);
139 if (buf_size < bytesNeeded) {
140 memcpy(context->p, buf, buf_size);
141 context->p += buf_size;
145 memcpy(context->p, buf, bytesNeeded);
146 context->p = context->buffer;
147 avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
148 flip(avctx, picture);
149 *data_size = sizeof(AVPicture);
153 static int raw_close_decoder(AVCodecContext *avctx)
155 RawVideoContext *context = avctx->priv_data;
157 av_freep(&context->buffer);
161 /* RAW Encoder Implementation */
163 static int raw_init_encoder(AVCodecContext *avctx)
165 avctx->coded_frame = (AVFrame *)avctx->priv_data;
166 avctx->coded_frame->pict_type = FF_I_TYPE;
167 avctx->coded_frame->key_frame = 1;
168 if(!avctx->codec_tag)
169 avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
173 static int raw_encode(AVCodecContext *avctx,
174 unsigned char *frame, int buf_size, void *data)
176 return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
177 avctx->height, frame, buf_size);
180 #ifdef CONFIG_RAWVIDEO_ENCODER
181 AVCodec rawvideo_encoder = {
189 #endif // CONFIG_RAWVIDEO_ENCODER
191 AVCodec rawvideo_decoder = {
195 sizeof(RawVideoContext),