]> git.sesse.net Git - ffmpeg/blob - libavcodec/rawdec.c
lavc: Drop deprecated public symbols
[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 "internal.h"
29 #include "raw.h"
30 #include "libavutil/buffer.h"
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/imgutils.h"
34
35 typedef struct RawVideoContext {
36     AVBufferRef *palette;
37     int frame_size;  /* size of the frame in bytes */
38     int flip;
39     int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
40     int is_yuv2;
41 } RawVideoContext;
42
43 static const PixelFormatTag pix_fmt_bps_avi[] = {
44     { AV_PIX_FMT_PAL8,    4 },
45     { AV_PIX_FMT_PAL8,    8 },
46     { AV_PIX_FMT_RGB444, 12 },
47     { AV_PIX_FMT_RGB555, 15 },
48     { AV_PIX_FMT_RGB555, 16 },
49     { AV_PIX_FMT_BGR24,  24 },
50     { AV_PIX_FMT_RGB32,  32 },
51     { AV_PIX_FMT_NONE,    0 },
52 };
53
54 static const PixelFormatTag pix_fmt_bps_mov[] = {
55     { AV_PIX_FMT_MONOWHITE, 1 },
56     { AV_PIX_FMT_PAL8,      2 },
57     { AV_PIX_FMT_PAL8,      4 },
58     { AV_PIX_FMT_PAL8,      8 },
59     // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
60     // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
61     { AV_PIX_FMT_RGB555BE, 16 },
62     { AV_PIX_FMT_RGB24,    24 },
63     { AV_PIX_FMT_ARGB,     32 },
64     { AV_PIX_FMT_MONOWHITE,33 },
65     { AV_PIX_FMT_NONE,      0 },
66 };
67
68 static enum AVPixelFormat find_pix_fmt(const PixelFormatTag *tags,
69                                        unsigned int fourcc)
70 {
71     while (tags->pix_fmt >= 0) {
72         if (tags->fourcc == fourcc)
73             return tags->pix_fmt;
74         tags++;
75     }
76     return AV_PIX_FMT_YUV420P;
77 }
78
79 static av_cold int raw_init_decoder(AVCodecContext *avctx)
80 {
81     RawVideoContext *context = avctx->priv_data;
82     const AVPixFmtDescriptor *desc;
83
84     if (avctx->codec_tag == MKTAG('r', 'a', 'w', ' '))
85         avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_mov,
86                                       avctx->bits_per_coded_sample);
87     else if (avctx->codec_tag == MKTAG('W', 'R', 'A', 'W'))
88         avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi,
89                                       avctx->bits_per_coded_sample);
90     else if (avctx->codec_tag)
91         avctx->pix_fmt = find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
92     else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
93         avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi,
94                                       avctx->bits_per_coded_sample);
95
96     desc = av_pix_fmt_desc_get(avctx->pix_fmt);
97     if (!desc) {
98         av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
99         return AVERROR(EINVAL);
100     }
101
102     if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL)) {
103         context->palette = av_buffer_alloc(AVPALETTE_SIZE);
104         if (!context->palette)
105             return AVERROR(ENOMEM);
106         if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
107             avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
108         else
109             memset(context->palette->data, 0, AVPALETTE_SIZE);
110     }
111
112     context->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
113                                                    avctx->width,
114                                                    avctx->height, 1);
115
116     if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
117         avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
118        (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' ')))
119         context->is_2_4_bpp = 1;
120
121     if ((avctx->extradata_size >= 9 &&
122          !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
123         avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
124         avctx->codec_tag == MKTAG('W','R','A','W'))
125         context->flip = 1;
126
127     if (avctx->codec_tag == AV_RL32("yuv2") &&
128         avctx->pix_fmt   == AV_PIX_FMT_YUYV422)
129         context->is_yuv2 = 1;
130
131     return 0;
132 }
133
134 static void flip(AVCodecContext *avctx, AVFrame *frame)
135 {
136     frame->data[0]     += frame->linesize[0] * (avctx->height - 1);
137     frame->linesize[0] *= -1;
138 }
139
140 static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
141                       AVPacket *avpkt)
142 {
143     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
144     RawVideoContext *context       = avctx->priv_data;
145     const uint8_t *buf             = avpkt->data;
146     int buf_size                   = avpkt->size;
147     int need_copy                  = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2;
148     int res;
149
150     AVFrame   *frame   = data;
151
152     frame->pict_type        = AV_PICTURE_TYPE_I;
153     frame->key_frame        = 1;
154
155     res = ff_decode_frame_props(avctx, frame);
156     if (res < 0)
157         return res;
158
159     if (buf_size < context->frame_size - (avctx->pix_fmt == AV_PIX_FMT_PAL8 ?
160                                           AVPALETTE_SIZE : 0))
161         return -1;
162
163     if (need_copy)
164         frame->buf[0] = av_buffer_alloc(context->frame_size);
165     else
166         frame->buf[0] = av_buffer_ref(avpkt->buf);
167     if (!frame->buf[0])
168         return AVERROR(ENOMEM);
169
170     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
171     if (context->is_2_4_bpp) {
172         int i;
173         uint8_t *dst = frame->buf[0]->data;
174         buf_size = context->frame_size - AVPALETTE_SIZE;
175         if (avctx->bits_per_coded_sample == 4) {
176             for (i = 0; 2 * i + 1 < buf_size; i++) {
177                 dst[2 * i + 0] = buf[i] >> 4;
178                 dst[2 * i + 1] = buf[i] & 15;
179             }
180         } else {
181             for (i = 0; 4 * i + 3 < buf_size; i++) {
182                 dst[4 * i + 0] = buf[i] >> 6;
183                 dst[4 * i + 1] = buf[i] >> 4 & 3;
184                 dst[4 * i + 2] = buf[i] >> 2 & 3;
185                 dst[4 * i + 3] = buf[i]      & 3;
186             }
187         }
188         buf = dst;
189     } else if (need_copy) {
190         memcpy(frame->buf[0]->data, buf, FFMIN(buf_size, context->frame_size));
191         buf = frame->buf[0]->data;
192     }
193
194     if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
195         avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
196         buf += buf_size - context->frame_size;
197
198     if ((res = av_image_fill_arrays(frame->data, frame->linesize,
199                                     buf, avctx->pix_fmt,
200                                     avctx->width, avctx->height, 1)) < 0)
201         return res;
202
203     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
204         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
205                                                      NULL);
206
207         if (pal) {
208             av_buffer_unref(&context->palette);
209             context->palette = av_buffer_alloc(AVPALETTE_SIZE);
210             if (!context->palette)
211                 return AVERROR(ENOMEM);
212             memcpy(context->palette->data, pal, AVPALETTE_SIZE);
213             frame->palette_has_changed = 1;
214         }
215     }
216
217     if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
218         (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
219         frame->buf[1]  = av_buffer_ref(context->palette);
220         if (!frame->buf[1])
221             return AVERROR(ENOMEM);
222         frame->data[1] = frame->buf[1]->data;
223     }
224     if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
225         ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
226         frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
227
228     if (context->flip)
229         flip(avctx, frame);
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 *, frame->data[1], frame->data[2]);
236
237     if (avctx->codec_tag == AV_RL32("yuv2") &&
238         avctx->pix_fmt   == AV_PIX_FMT_YUYV422) {
239         int x, y;
240         uint8_t *line = frame->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 += frame->linesize[0];
245         }
246     }
247
248     *got_frame = 1;
249     return buf_size;
250 }
251
252 static av_cold int raw_close_decoder(AVCodecContext *avctx)
253 {
254     RawVideoContext *context = avctx->priv_data;
255
256     av_buffer_unref(&context->palette);
257     return 0;
258 }
259
260 AVCodec ff_rawvideo_decoder = {
261     .name           = "rawvideo",
262     .long_name      = NULL_IF_CONFIG_SMALL("raw video"),
263     .type           = AVMEDIA_TYPE_VIDEO,
264     .id             = AV_CODEC_ID_RAWVIDEO,
265     .priv_data_size = sizeof(RawVideoContext),
266     .init           = raw_init_decoder,
267     .close          = raw_close_decoder,
268     .decode         = raw_decode,
269 };