]> git.sesse.net Git - ffmpeg/blob - libavcodec/rawdec.c
Merge commit 'd3810c47fe8c9509c28c65c0244e743c1d353daf'
[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 "imgconvert.h"
29 #include "raw.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"
35
36 typedef struct RawVideoContext {
37     AVClass *av_class;
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 */
41     int flip;
42     AVFrame pic;             ///< AVCodecContext.coded_frame
43     int tff;
44 } RawVideoContext;
45
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},
48 {NULL}
49 };
50
51 static const AVClass class = {
52     .class_name = "rawdec",
53     .option     = options,
54     .version    = LIBAVUTIL_VERSION_INT,
55 };
56
57 static const PixelFormatTag pix_fmt_bps_avi[] = {
58     { PIX_FMT_MONOWHITE, 1 },
59     { PIX_FMT_PAL8,    2 },
60     { PIX_FMT_PAL8,    4 },
61     { PIX_FMT_PAL8,    8 },
62     { PIX_FMT_RGB444, 12 },
63     { PIX_FMT_RGB555, 15 },
64     { PIX_FMT_RGB555, 16 },
65     { PIX_FMT_BGR24,  24 },
66     { PIX_FMT_BGRA,   32 },
67     { PIX_FMT_NONE, 0 },
68 };
69
70 static const PixelFormatTag pix_fmt_bps_mov[] = {
71     { PIX_FMT_MONOWHITE, 1 },
72     { PIX_FMT_PAL8,      2 },
73     { PIX_FMT_PAL8,      4 },
74     { PIX_FMT_PAL8,      8 },
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 },
79     { PIX_FMT_ARGB,     32 },
80     { PIX_FMT_MONOWHITE,33 },
81     { PIX_FMT_NONE, 0 },
82 };
83
84 enum PixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
85 {
86     while (tags->pix_fmt >= 0) {
87         if (tags->fourcc == fourcc)
88             return tags->pix_fmt;
89         tags++;
90     }
91     return PIX_FMT_YUV420P;
92 }
93
94 static av_cold int raw_init_decoder(AVCodecContext *avctx)
95 {
96     RawVideoContext *context = avctx->priv_data;
97
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);
106
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);
110     }
111
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);
120     } else {
121         context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
122     }
123     context->pic.pict_type = AV_PICTURE_TYPE_I;
124     context->pic.key_frame = 1;
125
126     avctx->coded_frame= &context->pic;
127
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'))
131         context->flip=1;
132
133     return 0;
134 }
135
136 static void flip(AVCodecContext *avctx, AVPicture * picture){
137     picture->data[0] += picture->linesize[0] * (avctx->height-1);
138     picture->linesize[0] *= -1;
139 }
140
141 static int raw_decode(AVCodecContext *avctx,
142                             void *data, int *data_size,
143                             AVPacket *avpkt)
144 {
145     const uint8_t *buf = avpkt->data;
146     int buf_size = avpkt->size;
147     int linesize_align = 4;
148     RawVideoContext *context = avctx->priv_data;
149     int res, len;
150
151     AVFrame   *frame   = data;
152     AVPicture *picture = data;
153
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;
161
162     if(context->tff>=0){
163         frame->interlaced_frame = 1;
164         frame->top_field_first  = context->tff;
165     }
166
167     if (avctx->width <= 0 || avctx->height <= 0) {
168         av_log(avctx, AV_LOG_ERROR, "w/h is invalid\n");
169         return AVERROR(EINVAL);
170     }
171
172     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
173     if (context->buffer) {
174         int i;
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;
181             }
182             linesize_align = 8;
183         } else {
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;
190             }
191             linesize_align = 16;
192         }
193         buf= dst;
194     }
195
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;
199
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);
204     }
205
206     if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
207                               avctx->width, avctx->height)) < 0)
208         return res;
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;
212     }
213     if (avctx->pix_fmt == PIX_FMT_PAL8) {
214         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
215
216         if (pal) {
217             memcpy(frame->data[1], pal, AVPALETTE_SIZE);
218             frame->palette_has_changed = 1;
219         }
220     }
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);
230
231     if(context->flip)
232         flip(avctx, picture);
233
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]);
239
240     if(avctx->codec_tag == AV_RL32("yuv2") &&
241        avctx->pix_fmt   == PIX_FMT_YUYV422) {
242         int x, y;
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];
248         }
249     }
250     if(avctx->codec_tag == AV_RL32("YVYU") &&
251        avctx->pix_fmt   == PIX_FMT_YUYV422) {
252         int x, y;
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];
258         }
259     }
260
261     *data_size = sizeof(AVPicture);
262     return buf_size;
263 }
264
265 static av_cold int raw_close_decoder(AVCodecContext *avctx)
266 {
267     RawVideoContext *context = avctx->priv_data;
268
269     av_freep(&context->buffer);
270     return 0;
271 }
272
273 AVCodec ff_rawvideo_decoder = {
274     .name           = "rawvideo",
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,
283 };