]> git.sesse.net Git - ffmpeg/blob - libavcodec/rawdec.c
Merge remote-tracking branch 'qatar/master'
[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/intreadwrite.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/opt.h"
33
34 typedef struct RawVideoContext {
35     AVClass *av_class;
36     uint32_t palette[AVPALETTE_COUNT];
37     unsigned char * buffer;  /* block of memory for holding one frame */
38     int             length;  /* number of bytes in buffer */
39     int flip;
40     AVFrame pic;             ///< AVCodecContext.coded_frame
41     int tff;
42 } RawVideoContext;
43
44 static const AVOption options[]={
45 {"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},
46 {NULL}
47 };
48
49 static const AVClass class = {
50     .class_name = "rawdec",
51     .option     = options,
52     .version    = LIBAVUTIL_VERSION_INT,
53 };
54
55 static const PixelFormatTag pix_fmt_bps_avi[] = {
56     { PIX_FMT_MONOWHITE, 1 },
57     { PIX_FMT_PAL8,    2 },
58     { PIX_FMT_PAL8,    4 },
59     { PIX_FMT_PAL8,    8 },
60     { PIX_FMT_RGB444, 12 },
61     { PIX_FMT_RGB555, 15 },
62     { PIX_FMT_RGB555, 16 },
63     { PIX_FMT_BGR24,  24 },
64     { PIX_FMT_BGRA,   32 },
65     { PIX_FMT_NONE, 0 },
66 };
67
68 static const PixelFormatTag pix_fmt_bps_mov[] = {
69     { PIX_FMT_MONOWHITE, 1 },
70     { PIX_FMT_PAL8,      2 },
71     { PIX_FMT_PAL8,      4 },
72     { PIX_FMT_PAL8,      8 },
73     // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
74     // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
75     { PIX_FMT_RGB555BE, 16 },
76     { PIX_FMT_RGB24,    24 },
77     { PIX_FMT_ARGB,     32 },
78     { PIX_FMT_MONOWHITE,33 },
79     { PIX_FMT_NONE, 0 },
80 };
81
82 enum PixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
83 {
84     while (tags->pix_fmt >= 0) {
85         if (tags->fourcc == fourcc)
86             return tags->pix_fmt;
87         tags++;
88     }
89     return PIX_FMT_YUV420P;
90 }
91
92 static av_cold int raw_init_decoder(AVCodecContext *avctx)
93 {
94     RawVideoContext *context = avctx->priv_data;
95
96     if (avctx->codec_tag == MKTAG('r','a','w',' '))
97         avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_mov, avctx->bits_per_coded_sample);
98     else if (avctx->codec_tag == MKTAG('W','R','A','W'))
99         avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
100     else if (avctx->codec_tag)
101         avctx->pix_fmt = ff_find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
102     else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample)
103         avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
104
105     if (avctx->pix_fmt == PIX_FMT_NONE) {
106         av_log(avctx, AV_LOG_ERROR, "Pixel format was not specified and cannot be detected\n");
107         return AVERROR(EINVAL);
108     }
109
110     ff_set_systematic_pal2(context->palette, avctx->pix_fmt);
111     if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
112        avctx->pix_fmt==PIX_FMT_PAL8 &&
113        (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
114         context->length = avpicture_get_size(avctx->pix_fmt, FFALIGN(avctx->width, 16), avctx->height);
115         context->buffer = av_malloc(context->length);
116         if (!context->buffer)
117             return AVERROR(ENOMEM);
118     } else {
119         context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
120     }
121     context->pic.pict_type = AV_PICTURE_TYPE_I;
122     context->pic.key_frame = 1;
123
124     avctx->coded_frame= &context->pic;
125
126     if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
127         avctx->codec_tag == MKTAG('c','y','u','v') ||
128         avctx->codec_tag == MKTAG(3, 0, 0, 0) || avctx->codec_tag == MKTAG('W','R','A','W'))
129         context->flip=1;
130
131     return 0;
132 }
133
134 static void flip(AVCodecContext *avctx, AVPicture * picture){
135     picture->data[0] += picture->linesize[0] * (avctx->height-1);
136     picture->linesize[0] *= -1;
137 }
138
139 static int raw_decode(AVCodecContext *avctx,
140                             void *data, int *data_size,
141                             AVPacket *avpkt)
142 {
143     const uint8_t *buf = avpkt->data;
144     int buf_size = avpkt->size;
145     int linesize_align = 4;
146     RawVideoContext *context = avctx->priv_data;
147     int res, len;
148
149     AVFrame   *frame   = data;
150     AVPicture *picture = data;
151
152     frame->pict_type        = avctx->coded_frame->pict_type;
153     frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
154     frame->top_field_first = avctx->coded_frame->top_field_first;
155     frame->reordered_opaque = avctx->reordered_opaque;
156     frame->pkt_pts          = avctx->pkt->pts;
157     frame->pkt_pos          = avctx->pkt->pos;
158     frame->pkt_duration     = avctx->pkt->duration;
159
160     if(context->tff>=0){
161         frame->interlaced_frame = 1;
162         frame->top_field_first  = context->tff;
163     }
164
165     if (avctx->width <= 0 || avctx->height <= 0) {
166         av_log(avctx, AV_LOG_ERROR, "w/h is invalid\n");
167         return AVERROR(EINVAL);
168     }
169
170     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
171     if (context->buffer) {
172         int i;
173         uint8_t *dst = context->buffer;
174         buf_size = context->length - 256*4;
175         if (avctx->bits_per_coded_sample == 4){
176             for(i=0; 2*i+1 < buf_size && i<avpkt->size; i++){
177                 dst[2*i+0]= buf[i]>>4;
178                 dst[2*i+1]= buf[i]&15;
179             }
180             linesize_align = 8;
181         } else {
182             for(i=0; 4*i+3 < buf_size && i<avpkt->size; i++){
183                 dst[4*i+0]= buf[i]>>6;
184                 dst[4*i+1]= buf[i]>>4&3;
185                 dst[4*i+2]= buf[i]>>2&3;
186                 dst[4*i+3]= buf[i]   &3;
187             }
188             linesize_align = 16;
189         }
190         buf= dst;
191     }
192
193     if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
194        avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
195         buf += buf_size - context->length;
196
197     len = context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0);
198     if (buf_size < len) {
199         av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected length %d\n", buf_size, len);
200         return AVERROR(EINVAL);
201     }
202
203     if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
204                               avctx->width, avctx->height)) < 0)
205         return res;
206     if((avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length) ||
207        (av_pix_fmt_descriptors[avctx->pix_fmt].flags & PIX_FMT_PSEUDOPAL)) {
208         frame->data[1]= context->palette;
209     }
210     if (avctx->pix_fmt == PIX_FMT_PAL8) {
211         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
212
213         if (pal) {
214             memcpy(frame->data[1], pal, AVPALETTE_SIZE);
215             frame->palette_has_changed = 1;
216         }
217     }
218     if((avctx->pix_fmt==PIX_FMT_BGR24    ||
219         avctx->pix_fmt==PIX_FMT_GRAY8    ||
220         avctx->pix_fmt==PIX_FMT_RGB555LE ||
221         avctx->pix_fmt==PIX_FMT_RGB555BE ||
222         avctx->pix_fmt==PIX_FMT_RGB565LE ||
223         avctx->pix_fmt==PIX_FMT_MONOWHITE ||
224         avctx->pix_fmt==PIX_FMT_PAL8) &&
225         FFALIGN(frame->linesize[0], linesize_align)*avctx->height <= buf_size)
226         frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
227
228     if(context->flip)
229         flip(avctx, picture);
230
231     if (   avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
232         || avctx->codec_tag == MKTAG('Y', 'V', '1', '6')
233         || avctx->codec_tag == MKTAG('Y', 'V', '2', '4')
234         || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
235         FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
236
237     if(avctx->codec_tag == AV_RL32("yuv2") &&
238        avctx->pix_fmt   == PIX_FMT_YUYV422) {
239         int x, y;
240         uint8_t *line = picture->data[0];
241         for(y = 0; y < avctx->height; y++) {
242             for(x = 0; x < avctx->width; x++)
243                 line[2*x + 1] ^= 0x80;
244             line += picture->linesize[0];
245         }
246     }
247     if(avctx->codec_tag == AV_RL32("YVYU") &&
248        avctx->pix_fmt   == PIX_FMT_YUYV422) {
249         int x, y;
250         uint8_t *line = picture->data[0];
251         for(y = 0; y < avctx->height; y++) {
252             for(x = 0; x < avctx->width - 1; x += 2)
253                 FFSWAP(uint8_t, line[2*x + 1], line[2*x + 3]);
254             line += picture->linesize[0];
255         }
256     }
257
258     *data_size = sizeof(AVPicture);
259     return buf_size;
260 }
261
262 static av_cold int raw_close_decoder(AVCodecContext *avctx)
263 {
264     RawVideoContext *context = avctx->priv_data;
265
266     av_freep(&context->buffer);
267     return 0;
268 }
269
270 AVCodec ff_rawvideo_decoder = {
271     .name           = "rawvideo",
272     .type           = AVMEDIA_TYPE_VIDEO,
273     .id             = CODEC_ID_RAWVIDEO,
274     .priv_data_size = sizeof(RawVideoContext),
275     .init           = raw_init_decoder,
276     .close          = raw_close_decoder,
277     .decode         = raw_decode,
278     .long_name      = NULL_IF_CONFIG_SMALL("raw video"),
279     .priv_class     = &class,
280 };