]> git.sesse.net Git - ffmpeg/blob - libavcodec/rawdec.c
avcodec: Add av_cold attributes to init functions missing them
[ffmpeg] / libavcodec / rawdec.c
1 /*
2  * Raw Video Decoder
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Raw Video Decoder
25  */
26
27 #include "avcodec.h"
28 #include "raw.h"
29 #include "libavutil/buffer.h"
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/imgutils.h"
33
34 typedef struct RawVideoContext {
35     AVBufferRef *palette;
36     int frame_size;  /* size of the frame in bytes */
37     int flip;
38     int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
39     int is_yuv2;
40 } RawVideoContext;
41
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 },
51 };
52
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 },
65 };
66
67 static enum AVPixelFormat find_pix_fmt(const PixelFormatTag *tags,
68                                        unsigned int fourcc)
69 {
70     while (tags->pix_fmt >= 0) {
71         if (tags->fourcc == fourcc)
72             return tags->pix_fmt;
73         tags++;
74     }
75     return AV_PIX_FMT_YUV420P;
76 }
77
78 static av_cold int raw_init_decoder(AVCodecContext *avctx)
79 {
80     RawVideoContext *context = avctx->priv_data;
81     const AVPixFmtDescriptor *desc;
82
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);
94
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
101     if (desc->flags & (PIX_FMT_PAL | PIX_FMT_PSEUDOPAL)) {
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);
109     }
110
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;
117
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;
123
124     if (avctx->codec_tag == AV_RL32("yuv2") &&
125         avctx->pix_fmt   == AV_PIX_FMT_YUYV422)
126         context->is_yuv2 = 1;
127
128     return 0;
129 }
130
131 static void flip(AVCodecContext *avctx, AVPicture *picture)
132 {
133     picture->data[0]     += picture->linesize[0] * (avctx->height - 1);
134     picture->linesize[0] *= -1;
135 }
136
137 static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
138                       AVPacket *avpkt)
139 {
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;
145     int res;
146
147     AVFrame   *frame   = data;
148     AVPicture *picture = data;
149
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;
154
155     if (buf_size < context->frame_size - (avctx->pix_fmt == AV_PIX_FMT_PAL8 ?
156                                           AVPALETTE_SIZE : 0))
157         return -1;
158
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
166     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
167     if (context->is_2_4_bpp) {
168         int i;
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;
175             }
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;
182             }
183         }
184         buf = dst;
185     } else if (need_copy) {
186         memcpy(frame->buf[0]->data, buf, FFMIN(buf_size, context->frame_size));
187         buf = frame->buf[0]->data;
188     }
189
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;
193
194     if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
195                               avctx->width, avctx->height)) < 0)
196         return res;
197
198     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
199         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
200                                                      NULL);
201
202         if (pal) {
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;
209         }
210     }
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     }
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;
222
223     if (context->flip)
224         flip(avctx, picture);
225
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]);
231
232     if (avctx->codec_tag == AV_RL32("yuv2") &&
233         avctx->pix_fmt   == AV_PIX_FMT_YUYV422) {
234         int x, y;
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];
240         }
241     }
242
243     *got_frame = 1;
244     return buf_size;
245 }
246
247 static av_cold int raw_close_decoder(AVCodecContext *avctx)
248 {
249     RawVideoContext *context = avctx->priv_data;
250
251     av_buffer_unref(&context->palette);
252     return 0;
253 }
254
255 AVCodec ff_rawvideo_decoder = {
256     .name           = "rawvideo",
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"),
264 };