3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/buffer.h"
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/imgutils.h"
34 typedef struct RawVideoContext {
36 int frame_size; /* size of the frame in bytes */
38 int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
42 static const PixelFormatTag pix_fmt_bps_avi[] = {
43 { AV_PIX_FMT_PAL8, 4 },
44 { AV_PIX_FMT_PAL8, 8 },
45 { AV_PIX_FMT_RGB444, 12 },
46 { AV_PIX_FMT_RGB555, 15 },
47 { AV_PIX_FMT_RGB555, 16 },
48 { AV_PIX_FMT_BGR24, 24 },
49 { AV_PIX_FMT_RGB32, 32 },
50 { AV_PIX_FMT_NONE, 0 },
53 static const PixelFormatTag pix_fmt_bps_mov[] = {
54 { AV_PIX_FMT_MONOWHITE, 1 },
55 { AV_PIX_FMT_PAL8, 2 },
56 { AV_PIX_FMT_PAL8, 4 },
57 { AV_PIX_FMT_PAL8, 8 },
58 // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
59 // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
60 { AV_PIX_FMT_RGB555BE, 16 },
61 { AV_PIX_FMT_RGB24, 24 },
62 { AV_PIX_FMT_ARGB, 32 },
63 { AV_PIX_FMT_MONOWHITE,33 },
64 { AV_PIX_FMT_NONE, 0 },
67 static enum AVPixelFormat find_pix_fmt(const PixelFormatTag *tags,
70 while (tags->pix_fmt >= 0) {
71 if (tags->fourcc == fourcc)
75 return AV_PIX_FMT_YUV420P;
78 static av_cold int raw_init_decoder(AVCodecContext *avctx)
80 RawVideoContext *context = avctx->priv_data;
81 const AVPixFmtDescriptor *desc;
83 if (avctx->codec_tag == MKTAG('r', 'a', 'w', ' '))
84 avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_mov,
85 avctx->bits_per_coded_sample);
86 else if (avctx->codec_tag == MKTAG('W', 'R', 'A', 'W'))
87 avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi,
88 avctx->bits_per_coded_sample);
89 else if (avctx->codec_tag)
90 avctx->pix_fmt = find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
91 else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
92 avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi,
93 avctx->bits_per_coded_sample);
95 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
97 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
98 return AVERROR(EINVAL);
101 if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL)) {
102 context->palette = av_buffer_alloc(AVPALETTE_SIZE);
103 if (!context->palette)
104 return AVERROR(ENOMEM);
105 if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
106 avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
108 memset(context->palette->data, 0, AVPALETTE_SIZE);
111 context->frame_size = avpicture_get_size(avctx->pix_fmt, avctx->width,
113 if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
114 avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
115 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' ')))
116 context->is_2_4_bpp = 1;
118 if ((avctx->extradata_size >= 9 &&
119 !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
120 avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
121 avctx->codec_tag == MKTAG('W','R','A','W'))
124 if (avctx->codec_tag == AV_RL32("yuv2") &&
125 avctx->pix_fmt == AV_PIX_FMT_YUYV422)
126 context->is_yuv2 = 1;
131 static void flip(AVCodecContext *avctx, AVPicture *picture)
133 picture->data[0] += picture->linesize[0] * (avctx->height - 1);
134 picture->linesize[0] *= -1;
137 static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
140 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
141 RawVideoContext *context = avctx->priv_data;
142 const uint8_t *buf = avpkt->data;
143 int buf_size = avpkt->size;
144 int need_copy = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2;
147 AVFrame *frame = data;
148 AVPicture *picture = data;
150 frame->pict_type = AV_PICTURE_TYPE_I;
151 frame->key_frame = 1;
152 frame->reordered_opaque = avctx->reordered_opaque;
153 frame->pkt_pts = avctx->pkt->pts;
155 if (buf_size < context->frame_size - (avctx->pix_fmt == AV_PIX_FMT_PAL8 ?
160 frame->buf[0] = av_buffer_alloc(context->frame_size);
162 frame->buf[0] = av_buffer_ref(avpkt->buf);
164 return AVERROR(ENOMEM);
166 //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
167 if (context->is_2_4_bpp) {
169 uint8_t *dst = frame->buf[0]->data;
170 buf_size = context->frame_size - AVPALETTE_SIZE;
171 if (avctx->bits_per_coded_sample == 4) {
172 for (i = 0; 2 * i + 1 < buf_size; i++) {
173 dst[2 * i + 0] = buf[i] >> 4;
174 dst[2 * i + 1] = buf[i] & 15;
177 for (i = 0; 4 * i + 3 < buf_size; i++) {
178 dst[4 * i + 0] = buf[i] >> 6;
179 dst[4 * i + 1] = buf[i] >> 4 & 3;
180 dst[4 * i + 2] = buf[i] >> 2 & 3;
181 dst[4 * i + 3] = buf[i] & 3;
185 } else if (need_copy) {
186 memcpy(frame->buf[0]->data, buf, FFMIN(buf_size, context->frame_size));
187 buf = frame->buf[0]->data;
190 if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
191 avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
192 buf += buf_size - context->frame_size;
194 if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
195 avctx->width, avctx->height)) < 0)
198 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
199 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
203 av_buffer_unref(&context->palette);
204 context->palette = av_buffer_alloc(AVPALETTE_SIZE);
205 if (!context->palette)
206 return AVERROR(ENOMEM);
207 memcpy(context->palette->data, pal, AVPALETTE_SIZE);
208 frame->palette_has_changed = 1;
212 if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
213 (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
214 frame->buf[1] = av_buffer_ref(context->palette);
216 return AVERROR(ENOMEM);
217 frame->data[1] = frame->buf[1]->data;
219 if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
220 ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
221 frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
224 flip(avctx, picture);
226 if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
227 avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
228 avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
229 avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
230 FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
232 if (avctx->codec_tag == AV_RL32("yuv2") &&
233 avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
235 uint8_t *line = picture->data[0];
236 for (y = 0; y < avctx->height; y++) {
237 for (x = 0; x < avctx->width; x++)
238 line[2 * x + 1] ^= 0x80;
239 line += picture->linesize[0];
247 static av_cold int raw_close_decoder(AVCodecContext *avctx)
249 RawVideoContext *context = avctx->priv_data;
251 av_buffer_unref(&context->palette);
255 AVCodec ff_rawvideo_decoder = {
257 .type = AVMEDIA_TYPE_VIDEO,
258 .id = AV_CODEC_ID_RAWVIDEO,
259 .priv_data_size = sizeof(RawVideoContext),
260 .init = raw_init_decoder,
261 .close = raw_close_decoder,
262 .decode = raw_decode,
263 .long_name = NULL_IF_CONFIG_SMALL("raw video"),