]> git.sesse.net Git - ffmpeg/blob - libavcodec/rawdec.c
avcodec/mpeg4videodec: Check for bitstream overread in decode_vol_header()
[ffmpeg] / libavcodec / rawdec.c
1 /*
2  * Raw Video Decoder
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Raw Video Decoder
25  */
26
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "raw.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/buffer.h"
32 #include "libavutil/common.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/opt.h"
36
37 typedef struct RawVideoContext {
38     AVClass *av_class;
39     AVBufferRef *palette;
40     int frame_size;  /* size of the frame in bytes */
41     int flip;
42     int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
43     int is_yuv2;
44     int tff;
45 } RawVideoContext;
46
47 static const AVOption options[]={
48 {"top", "top field first", offsetof(RawVideoContext, tff), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM},
49 {NULL}
50 };
51
52 static const AVClass rawdec_class = {
53     .class_name = "rawdec",
54     .option     = options,
55     .version    = LIBAVUTIL_VERSION_INT,
56 };
57
58 static const PixelFormatTag pix_fmt_bps_avi[] = {
59     { AV_PIX_FMT_MONOWHITE, 1 },
60     { AV_PIX_FMT_PAL8,    2 },
61     { AV_PIX_FMT_PAL8,    4 },
62     { AV_PIX_FMT_PAL8,    8 },
63     { AV_PIX_FMT_RGB444LE, 12 },
64     { AV_PIX_FMT_RGB555LE, 15 },
65     { AV_PIX_FMT_RGB555LE, 16 },
66     { AV_PIX_FMT_BGR24,  24 },
67     { AV_PIX_FMT_BGRA,   32 },
68     { AV_PIX_FMT_NONE,    0 },
69 };
70
71 static const PixelFormatTag pix_fmt_bps_mov[] = {
72     { AV_PIX_FMT_MONOWHITE, 1 },
73     { AV_PIX_FMT_PAL8,      2 },
74     { AV_PIX_FMT_PAL8,      4 },
75     { AV_PIX_FMT_PAL8,      8 },
76     // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
77     // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
78     { AV_PIX_FMT_RGB555BE, 16 },
79     { AV_PIX_FMT_RGB24,    24 },
80     { AV_PIX_FMT_ARGB,     32 },
81     { AV_PIX_FMT_MONOWHITE,33 },
82     { AV_PIX_FMT_NONE,      0 },
83 };
84
85 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
86                                        unsigned int fourcc)
87 {
88     while (tags->pix_fmt >= 0) {
89         if (tags->fourcc == fourcc)
90             return tags->pix_fmt;
91         tags++;
92     }
93     return AV_PIX_FMT_NONE;
94 }
95
96 #if LIBAVCODEC_VERSION_MAJOR < 55
97 enum AVPixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
98 {
99     return avpriv_find_pix_fmt(tags, fourcc);
100 }
101 #endif
102
103 static av_cold int raw_init_decoder(AVCodecContext *avctx)
104 {
105     RawVideoContext *context = avctx->priv_data;
106     const AVPixFmtDescriptor *desc;
107
108     if (   avctx->codec_tag == MKTAG('r','a','w',' ')
109         || avctx->codec_tag == MKTAG('N','O','1','6'))
110         avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_mov,
111                                       avctx->bits_per_coded_sample & 0x1f);
112     else if (avctx->codec_tag == MKTAG('W', 'R', 'A', 'W'))
113         avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
114                                       avctx->bits_per_coded_sample);
115     else if (avctx->codec_tag)
116         avctx->pix_fmt = avpriv_find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
117     else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
118         avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
119                                       avctx->bits_per_coded_sample);
120
121     desc = av_pix_fmt_desc_get(avctx->pix_fmt);
122     if (!desc) {
123         av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
124         return AVERROR(EINVAL);
125     }
126
127     if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL)) {
128         context->palette = av_buffer_alloc(AVPALETTE_SIZE);
129         if (!context->palette)
130             return AVERROR(ENOMEM);
131         if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
132             avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
133         else
134             memset(context->palette->data, 0, AVPALETTE_SIZE);
135     }
136
137     if (((avctx->bits_per_coded_sample & 0x1f) == 4 || (avctx->bits_per_coded_sample & 0x1f) == 2) &&
138         avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
139        (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))) {
140         context->is_2_4_bpp = 1;
141         context->frame_size = avpicture_get_size(avctx->pix_fmt,
142                                                  FFALIGN(avctx->width, 16),
143                                                  avctx->height);
144     } else {
145         context->frame_size = avpicture_get_size(avctx->pix_fmt, avctx->width,
146                                                  avctx->height);
147     }
148
149     if ((avctx->extradata_size >= 9 &&
150          !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
151         avctx->codec_tag == MKTAG('c','y','u','v') ||
152         avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
153         avctx->codec_tag == MKTAG('W','R','A','W'))
154         context->flip = 1;
155
156     if (avctx->codec_tag == AV_RL32("yuv2") &&
157         avctx->pix_fmt   == AV_PIX_FMT_YUYV422)
158         context->is_yuv2 = 1;
159
160     return 0;
161 }
162
163 static void flip(AVCodecContext *avctx, AVPicture *picture)
164 {
165     picture->data[0]     += picture->linesize[0] * (avctx->height - 1);
166     picture->linesize[0] *= -1;
167 }
168
169 static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
170                       AVPacket *avpkt)
171 {
172     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
173     RawVideoContext *context       = avctx->priv_data;
174     const uint8_t *buf             = avpkt->data;
175     int buf_size                   = avpkt->size;
176     int linesize_align             = 4;
177     int res, len;
178     int need_copy                  = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2;
179
180     AVFrame   *frame   = data;
181     AVPicture *picture = data;
182
183     frame->pict_type        = AV_PICTURE_TYPE_I;
184     frame->key_frame        = 1;
185     frame->reordered_opaque = avctx->reordered_opaque;
186     frame->pkt_pts          = avctx->internal->pkt->pts;
187     av_frame_set_pkt_pos     (frame, avctx->internal->pkt->pos);
188     av_frame_set_pkt_duration(frame, avctx->internal->pkt->duration);
189
190     if (context->tff >= 0) {
191         frame->interlaced_frame = 1;
192         frame->top_field_first  = context->tff;
193     }
194
195     if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
196         return res;
197
198     if (need_copy)
199         frame->buf[0] = av_buffer_alloc(FFMAX(context->frame_size, buf_size));
200     else
201         frame->buf[0] = av_buffer_ref(avpkt->buf);
202     if (!frame->buf[0])
203         return AVERROR(ENOMEM);
204
205     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
206     if (context->is_2_4_bpp) {
207         int i;
208         uint8_t *dst = frame->buf[0]->data;
209         buf_size = context->frame_size - AVPALETTE_SIZE;
210         if ((avctx->bits_per_coded_sample & 0x1f) == 4) {
211             for (i = 0; 2 * i + 1 < buf_size && i<avpkt->size; i++) {
212                 dst[2 * i + 0] = buf[i] >> 4;
213                 dst[2 * i + 1] = buf[i] & 15;
214             }
215             linesize_align = 8;
216         } else {
217             av_assert0((avctx->bits_per_coded_sample & 0x1f) == 2);
218             for (i = 0; 4 * i + 3 < buf_size && i<avpkt->size; i++) {
219                 dst[4 * i + 0] = buf[i] >> 6;
220                 dst[4 * i + 1] = buf[i] >> 4 & 3;
221                 dst[4 * i + 2] = buf[i] >> 2 & 3;
222                 dst[4 * i + 3] = buf[i]      & 3;
223             }
224             linesize_align = 16;
225         }
226         buf = dst;
227     } else if (need_copy) {
228         memcpy(frame->buf[0]->data, buf, buf_size);
229         buf = frame->buf[0]->data;
230     }
231
232     if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
233         avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
234         buf += buf_size - context->frame_size;
235
236     len = context->frame_size - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
237     if (buf_size < len) {
238         av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected frame_size %d\n", buf_size, len);
239         av_buffer_unref(&frame->buf[0]);
240         return AVERROR(EINVAL);
241     }
242
243     if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
244                               avctx->width, avctx->height)) < 0) {
245         av_buffer_unref(&frame->buf[0]);
246         return res;
247     }
248
249     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
250         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
251                                                      NULL);
252
253         if (pal) {
254             av_buffer_unref(&context->palette);
255             context->palette = av_buffer_alloc(AVPALETTE_SIZE);
256             if (!context->palette) {
257                 av_buffer_unref(&frame->buf[0]);
258                 return AVERROR(ENOMEM);
259             }
260             memcpy(context->palette->data, pal, AVPALETTE_SIZE);
261             frame->palette_has_changed = 1;
262         }
263     }
264
265     if ((avctx->pix_fmt==AV_PIX_FMT_BGR24    ||
266         avctx->pix_fmt==AV_PIX_FMT_GRAY8    ||
267         avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
268         avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
269         avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
270         avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
271         avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
272         FFALIGN(frame->linesize[0], linesize_align) * avctx->height <= buf_size)
273         frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
274
275     if (avctx->pix_fmt == AV_PIX_FMT_NV12 && avctx->codec_tag == MKTAG('N', 'V', '1', '2') &&
276         FFALIGN(frame->linesize[0], linesize_align) * avctx->height +
277         FFALIGN(frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
278         int la0 = FFALIGN(frame->linesize[0], linesize_align);
279         frame->data[1] += (la0 - frame->linesize[0]) * avctx->height;
280         frame->linesize[0] = la0;
281         frame->linesize[1] = FFALIGN(frame->linesize[1], linesize_align);
282     }
283
284     if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
285         (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
286         frame->buf[1]  = av_buffer_ref(context->palette);
287         if (!frame->buf[1]) {
288             av_buffer_unref(&frame->buf[0]);
289             return AVERROR(ENOMEM);
290         }
291         frame->data[1] = frame->buf[1]->data;
292     }
293
294     if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
295         ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
296         frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
297
298     if (context->flip)
299         flip(avctx, picture);
300
301     if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
302         avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
303         avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
304         avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
305         FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
306
307     if (avctx->codec_tag == AV_RL32("I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
308         picture->data[1] = picture->data[1] +  (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
309         picture->data[2] = picture->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
310     }
311
312     if (avctx->codec_tag == AV_RL32("yuv2") &&
313         avctx->pix_fmt   == AV_PIX_FMT_YUYV422) {
314         int x, y;
315         uint8_t *line = picture->data[0];
316         for (y = 0; y < avctx->height; y++) {
317             for (x = 0; x < avctx->width; x++)
318                 line[2 * x + 1] ^= 0x80;
319             line += picture->linesize[0];
320         }
321     }
322     if (avctx->codec_tag == AV_RL32("YVYU") &&
323         avctx->pix_fmt   == AV_PIX_FMT_YUYV422) {
324         int x, y;
325         uint8_t *line = picture->data[0];
326         for(y = 0; y < avctx->height; y++) {
327             for(x = 0; x < avctx->width - 1; x += 2)
328                 FFSWAP(uint8_t, line[2*x + 1], line[2*x + 3]);
329             line += picture->linesize[0];
330         }
331     }
332
333     if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /* we have interlaced material flagged in container */
334         frame->interlaced_frame = 1;
335         if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
336             frame->top_field_first = 1;
337     }
338
339     *got_frame = 1;
340     return buf_size;
341 }
342
343 static av_cold int raw_close_decoder(AVCodecContext *avctx)
344 {
345     RawVideoContext *context = avctx->priv_data;
346
347     av_buffer_unref(&context->palette);
348     return 0;
349 }
350
351 AVCodec ff_rawvideo_decoder = {
352     .name           = "rawvideo",
353     .long_name      = NULL_IF_CONFIG_SMALL("raw video"),
354     .type           = AVMEDIA_TYPE_VIDEO,
355     .id             = AV_CODEC_ID_RAWVIDEO,
356     .priv_data_size = sizeof(RawVideoContext),
357     .init           = raw_init_decoder,
358     .close          = raw_close_decoder,
359     .decode         = raw_decode,
360     .priv_class     = &rawdec_class,
361 };