rawdec: fix a typo -- || instead of |
[libav.git] / libavcodec / rawdec.c
CommitLineData
3a763f0f
AJ
1/*
2 * Raw Video Decoder
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
3a763f0f 4 *
2912e87a 5 * This file is part of Libav.
3a763f0f 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
3a763f0f
AJ
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.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
3a763f0f
AJ
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
3a763f0f
AJ
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
3a763f0f
AJ
24 * Raw Video Decoder
25 */
26
27#include "avcodec.h"
28#include "raw.h"
759001c5 29#include "libavutil/buffer.h"
1d9c2dc8 30#include "libavutil/common.h"
3dd2f87e 31#include "libavutil/intreadwrite.h"
737eb597 32#include "libavutil/imgutils.h"
3a763f0f
AJ
33
34typedef struct RawVideoContext {
759001c5
AK
35 AVBufferRef *palette;
36 int frame_size; /* size of the frame in bytes */
31f2616d 37 int flip;
759001c5
AK
38 int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
39 int is_yuv2;
3a763f0f
AJ
40} RawVideoContext;
41
6a2c9b8b 42static const PixelFormatTag pix_fmt_bps_avi[] = {
716d413c
AK
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 },
0ce033f8 50 { AV_PIX_FMT_NONE, 0 },
3a763f0f
AJ
51};
52
6a2c9b8b 53static const PixelFormatTag pix_fmt_bps_mov[] = {
716d413c
AK
54 { AV_PIX_FMT_MONOWHITE, 1 },
55 { AV_PIX_FMT_PAL8, 2 },
56 { AV_PIX_FMT_PAL8, 4 },
57 { AV_PIX_FMT_PAL8, 8 },
7574dc4c
BC
58 // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
59 // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
716d413c
AK
60 { AV_PIX_FMT_RGB555BE, 16 },
61 { AV_PIX_FMT_RGB24, 24 },
62 { AV_PIX_FMT_ARGB, 32 },
63 { AV_PIX_FMT_MONOWHITE,33 },
0ce033f8 64 { AV_PIX_FMT_NONE, 0 },
3a763f0f
AJ
65};
66
0ce033f8
AK
67static enum AVPixelFormat find_pix_fmt(const PixelFormatTag *tags,
68 unsigned int fourcc)
3a763f0f
AJ
69{
70 while (tags->pix_fmt >= 0) {
71 if (tags->fourcc == fourcc)
72 return tags->pix_fmt;
73 tags++;
74 }
716d413c 75 return AV_PIX_FMT_YUV420P;
3a763f0f
AJ
76}
77
98a6fff9 78static av_cold int raw_init_decoder(AVCodecContext *avctx)
3a763f0f
AJ
79{
80 RawVideoContext *context = avctx->priv_data;
759001c5 81 const AVPixFmtDescriptor *desc;
3a763f0f 82
0ce033f8
AK
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);
3a763f0f 89 else if (avctx->codec_tag)
1beff414 90 avctx->pix_fmt = find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
716d413c 91 else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
0ce033f8
AK
92 avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi,
93 avctx->bits_per_coded_sample);
3a763f0f 94
759001c5
AK
95 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
96 if (!desc) {
97 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
98 return AVERROR(EINVAL);
99 }
100
be40d6cc 101 if (desc->flags & (PIX_FMT_PAL | PIX_FMT_PSEUDOPAL)) {
759001c5
AK
102 context->palette = av_buffer_alloc(AVPALETTE_SIZE);
103 if (!context->palette)
104 return AVERROR(ENOMEM);
105 if (desc->flags & PIX_FMT_PSEUDOPAL)
106 avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
107 else
108 memset(context->palette->data, 0, AVPALETTE_SIZE);
2efcde73 109 }
3a763f0f 110
759001c5
AK
111 context->frame_size = avpicture_get_size(avctx->pix_fmt, avctx->width,
112 avctx->height);
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;
3a763f0f 117
0ce033f8
AK
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'))
122 context->flip = 1;
31f2616d 123
759001c5
AK
124 if (avctx->codec_tag == AV_RL32("yuv2") &&
125 avctx->pix_fmt == AV_PIX_FMT_YUYV422)
126 context->is_yuv2 = 1;
127
3a763f0f
AJ
128 return 0;
129}
130
0ce033f8
AK
131static void flip(AVCodecContext *avctx, AVPicture *picture)
132{
133 picture->data[0] += picture->linesize[0] * (avctx->height - 1);
e641f320 134 picture->linesize[0] *= -1;
3a763f0f
AJ
135}
136
0ce033f8
AK
137static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
138 AVPacket *avpkt)
3a763f0f 139{
50ba57e0 140 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
0ce033f8
AK
141 RawVideoContext *context = avctx->priv_data;
142 const uint8_t *buf = avpkt->data;
143 int buf_size = avpkt->size;
759001c5 144 int need_copy = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2;
98df2e24 145 int res;
3a763f0f 146
562b6c74
DB
147 AVFrame *frame = data;
148 AVPicture *picture = data;
3a763f0f 149
759001c5
AK
150 frame->pict_type = AV_PICTURE_TYPE_I;
151 frame->key_frame = 1;
1bb56bdb 152 frame->reordered_opaque = avctx->reordered_opaque;
3c802cab 153 frame->pkt_pts = avctx->pkt->pts;
3a763f0f 154
759001c5
AK
155 if (buf_size < context->frame_size - (avctx->pix_fmt == AV_PIX_FMT_PAL8 ?
156 AVPALETTE_SIZE : 0))
cc5dd632
RB
157 return -1;
158
759001c5
AK
159 if (need_copy)
160 frame->buf[0] = av_buffer_alloc(context->frame_size);
161 else
162 frame->buf[0] = av_buffer_ref(avpkt->buf);
163 if (!frame->buf[0])
164 return AVERROR(ENOMEM);
165
6698d139 166 //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
759001c5 167 if (context->is_2_4_bpp) {
611e7bc4 168 int i;
759001c5
AK
169 uint8_t *dst = frame->buf[0]->data;
170 buf_size = context->frame_size - AVPALETTE_SIZE;
0ce033f8
AK
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;
6698d139 175 }
0ce033f8
AK
176 } else {
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;
4235c98e 182 }
0ce033f8
AK
183 }
184 buf = dst;
759001c5
AK
185 } else if (need_copy) {
186 memcpy(frame->buf[0]->data, buf, FFMIN(buf_size, context->frame_size));
187 buf = frame->buf[0]->data;
611e7bc4
MN
188 }
189
0ce033f8
AK
190 if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
191 avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
759001c5 192 buf += buf_size - context->frame_size;
864a0742 193
98df2e24
RB
194 if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
195 avctx->width, avctx->height)) < 0)
196 return res;
759001c5 197
716d413c 198 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
0ce033f8
AK
199 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
200 NULL);
2d8591c2
KS
201
202 if (pal) {
759001c5
AK
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);
2d8591c2
KS
208 frame->palette_has_changed = 1;
209 }
3a763f0f 210 }
759001c5
AK
211
212 if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
213 (desc->flags & PIX_FMT_PSEUDOPAL)) {
214 frame->buf[1] = av_buffer_ref(context->palette);
215 if (!frame->buf[1])
216 return AVERROR(ENOMEM);
217 frame->data[1] = frame->buf[1]->data;
218 }
0ce033f8
AK
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;
3a763f0f 222
0ce033f8 223 if (context->flip)
e641f320 224 flip(avctx, picture);
3a763f0f 225
0ce033f8
AK
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'))
c522b4e9 230 FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
3a763f0f 231
0ce033f8
AK
232 if (avctx->codec_tag == AV_RL32("yuv2") &&
233 avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
3dd2f87e
JM
234 int x, y;
235 uint8_t *line = picture->data[0];
0ce033f8
AK
236 for (y = 0; y < avctx->height; y++) {
237 for (x = 0; x < avctx->width; x++)
238 line[2 * x + 1] ^= 0x80;
3dd2f87e
JM
239 line += picture->linesize[0];
240 }
241 }
242
df9b9567 243 *got_frame = 1;
3a763f0f
AJ
244 return buf_size;
245}
246
98a6fff9 247static av_cold int raw_close_decoder(AVCodecContext *avctx)
3a763f0f
AJ
248{
249 RawVideoContext *context = avctx->priv_data;
250
759001c5 251 av_buffer_unref(&context->palette);
3a763f0f
AJ
252 return 0;
253}
254
d36beb3f 255AVCodec ff_rawvideo_decoder = {
ec6402b7
AK
256 .name = "rawvideo",
257 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 258 .id = AV_CODEC_ID_RAWVIDEO,
ec6402b7
AK
259 .priv_data_size = sizeof(RawVideoContext),
260 .init = raw_init_decoder,
261 .close = raw_close_decoder,
262 .decode = raw_decode,
00c3b67b 263 .long_name = NULL_IF_CONFIG_SMALL("raw video"),
3a763f0f 264};