3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "imgconvert.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/opt.h"
36 typedef struct RawVideoContext {
38 uint32_t palette[AVPALETTE_COUNT];
39 unsigned char * buffer; /* block of memory for holding one frame */
40 int length; /* number of bytes in buffer */
42 AVFrame pic; ///< AVCodecContext.coded_frame
46 static const AVOption options[]={
47 {"top", "top field first", offsetof(RawVideoContext, tff), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM},
51 static const AVClass class = {
52 .class_name = "rawdec",
54 .version = LIBAVUTIL_VERSION_INT,
57 static const PixelFormatTag pix_fmt_bps_avi[] = {
58 { PIX_FMT_MONOWHITE, 1 },
62 { PIX_FMT_RGB444, 12 },
63 { PIX_FMT_RGB555, 15 },
64 { PIX_FMT_RGB555, 16 },
65 { PIX_FMT_BGR24, 24 },
70 static const PixelFormatTag pix_fmt_bps_mov[] = {
71 { PIX_FMT_MONOWHITE, 1 },
75 // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
76 // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
77 { PIX_FMT_RGB555BE, 16 },
78 { PIX_FMT_RGB24, 24 },
80 { PIX_FMT_MONOWHITE,33 },
84 enum PixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
86 while (tags->pix_fmt >= 0) {
87 if (tags->fourcc == fourcc)
91 return PIX_FMT_YUV420P;
94 static av_cold int raw_init_decoder(AVCodecContext *avctx)
96 RawVideoContext *context = avctx->priv_data;
98 if (avctx->codec_tag == MKTAG('r','a','w',' ') || avctx->codec_tag == MKTAG('N','O','1','6'))
99 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_mov, avctx->bits_per_coded_sample);
100 else if (avctx->codec_tag == MKTAG('W','R','A','W'))
101 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
102 else if (avctx->codec_tag)
103 avctx->pix_fmt = ff_find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
104 else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample)
105 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
107 if (avctx->pix_fmt == PIX_FMT_NONE) {
108 av_log(avctx, AV_LOG_ERROR, "Pixel format was not specified and cannot be detected\n");
109 return AVERROR(EINVAL);
112 ff_set_systematic_pal2(context->palette, avctx->pix_fmt);
113 if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
114 avctx->pix_fmt==PIX_FMT_PAL8 &&
115 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
116 context->length = avpicture_get_size(avctx->pix_fmt, FFALIGN(avctx->width, 16), avctx->height);
117 context->buffer = av_malloc(context->length);
118 if (!context->buffer)
119 return AVERROR(ENOMEM);
121 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
123 context->pic.pict_type = AV_PICTURE_TYPE_I;
124 context->pic.key_frame = 1;
126 avctx->coded_frame= &context->pic;
128 if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
129 avctx->codec_tag == MKTAG('c','y','u','v') ||
130 avctx->codec_tag == MKTAG(3, 0, 0, 0) || avctx->codec_tag == MKTAG('W','R','A','W'))
136 static void flip(AVCodecContext *avctx, AVPicture * picture){
137 picture->data[0] += picture->linesize[0] * (avctx->height-1);
138 picture->linesize[0] *= -1;
141 static int raw_decode(AVCodecContext *avctx,
142 void *data, int *data_size,
145 const uint8_t *buf = avpkt->data;
146 int buf_size = avpkt->size;
147 int linesize_align = 4;
148 RawVideoContext *context = avctx->priv_data;
151 AVFrame *frame = data;
152 AVPicture *picture = data;
154 frame->pict_type = avctx->coded_frame->pict_type;
155 frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
156 frame->top_field_first = avctx->coded_frame->top_field_first;
157 frame->reordered_opaque = avctx->reordered_opaque;
158 frame->pkt_pts = avctx->pkt->pts;
159 frame->pkt_pos = avctx->pkt->pos;
160 frame->pkt_duration = avctx->pkt->duration;
163 frame->interlaced_frame = 1;
164 frame->top_field_first = context->tff;
167 if (avctx->width <= 0 || avctx->height <= 0) {
168 av_log(avctx, AV_LOG_ERROR, "w/h is invalid\n");
169 return AVERROR(EINVAL);
172 //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
173 if (context->buffer) {
175 uint8_t *dst = context->buffer;
176 buf_size = context->length - AVPALETTE_SIZE;
177 if (avctx->bits_per_coded_sample == 4){
178 for(i=0; 2*i+1 < buf_size && i<avpkt->size; i++){
179 dst[2*i+0]= buf[i]>>4;
180 dst[2*i+1]= buf[i]&15;
184 av_assert0(avctx->bits_per_coded_sample == 2);
185 for(i=0; 4*i+3 < buf_size && i<avpkt->size; i++){
186 dst[4*i+0]= buf[i]>>6;
187 dst[4*i+1]= buf[i]>>4&3;
188 dst[4*i+2]= buf[i]>>2&3;
189 dst[4*i+3]= buf[i] &3;
196 if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
197 avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
198 buf += buf_size - context->length;
200 len = context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
201 if (buf_size < len) {
202 av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected length %d\n", buf_size, len);
203 return AVERROR(EINVAL);
206 if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
207 avctx->width, avctx->height)) < 0)
209 if((avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length) ||
210 (av_pix_fmt_descriptors[avctx->pix_fmt].flags & PIX_FMT_PSEUDOPAL)) {
211 frame->data[1]= context->palette;
213 if (avctx->pix_fmt == PIX_FMT_PAL8) {
214 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
217 memcpy(frame->data[1], pal, AVPALETTE_SIZE);
218 frame->palette_has_changed = 1;
221 if((avctx->pix_fmt==PIX_FMT_BGR24 ||
222 avctx->pix_fmt==PIX_FMT_GRAY8 ||
223 avctx->pix_fmt==PIX_FMT_RGB555LE ||
224 avctx->pix_fmt==PIX_FMT_RGB555BE ||
225 avctx->pix_fmt==PIX_FMT_RGB565LE ||
226 avctx->pix_fmt==PIX_FMT_MONOWHITE ||
227 avctx->pix_fmt==PIX_FMT_PAL8) &&
228 FFALIGN(frame->linesize[0], linesize_align)*avctx->height <= buf_size)
229 frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
232 flip(avctx, picture);
234 if ( avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
235 || avctx->codec_tag == MKTAG('Y', 'V', '1', '6')
236 || avctx->codec_tag == MKTAG('Y', 'V', '2', '4')
237 || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
238 FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
240 if(avctx->codec_tag == AV_RL32("yuv2") &&
241 avctx->pix_fmt == PIX_FMT_YUYV422) {
243 uint8_t *line = picture->data[0];
244 for(y = 0; y < avctx->height; y++) {
245 for(x = 0; x < avctx->width; x++)
246 line[2*x + 1] ^= 0x80;
247 line += picture->linesize[0];
250 if(avctx->codec_tag == AV_RL32("YVYU") &&
251 avctx->pix_fmt == PIX_FMT_YUYV422) {
253 uint8_t *line = picture->data[0];
254 for(y = 0; y < avctx->height; y++) {
255 for(x = 0; x < avctx->width - 1; x += 2)
256 FFSWAP(uint8_t, line[2*x + 1], line[2*x + 3]);
257 line += picture->linesize[0];
261 *data_size = sizeof(AVPicture);
265 static av_cold int raw_close_decoder(AVCodecContext *avctx)
267 RawVideoContext *context = avctx->priv_data;
269 av_freep(&context->buffer);
273 AVCodec ff_rawvideo_decoder = {
275 .type = AVMEDIA_TYPE_VIDEO,
276 .id = AV_CODEC_ID_RAWVIDEO,
277 .priv_data_size = sizeof(RawVideoContext),
278 .init = raw_init_decoder,
279 .close = raw_close_decoder,
280 .decode = raw_decode,
281 .long_name = NULL_IF_CONFIG_SMALL("raw video"),
282 .priv_class = &class,