]> git.sesse.net Git - ffmpeg/blob - libavcodec/frwu.c
libopenjpeg: support YUV and deep RGB pixel formats
[ffmpeg] / libavcodec / frwu.c
1 /*
2  * Forward Uncompressed
3  *
4  * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "avcodec.h"
24 #include "bytestream.h"
25
26 static av_cold int decode_init(AVCodecContext *avctx)
27 {
28     if (avctx->width & 1) {
29         av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
30         return AVERROR(EINVAL);
31     }
32     avctx->pix_fmt = PIX_FMT_UYVY422;
33
34     avctx->coded_frame = avcodec_alloc_frame();
35     if (!avctx->coded_frame)
36         return AVERROR(ENOMEM);
37
38     return 0;
39 }
40
41 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
42                         AVPacket *avpkt)
43 {
44     int field, ret;
45     AVFrame *pic = avctx->coded_frame;
46     const uint8_t *buf = avpkt->data;
47     const uint8_t *buf_end = buf + avpkt->size;
48
49     if (pic->data[0])
50         avctx->release_buffer(avctx, pic);
51
52     if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
53         av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
54         return AVERROR_INVALIDDATA;
55     }
56     if (bytestream_get_le32(&buf) != MKTAG('F', 'R', 'W', '1')) {
57         av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
58         return AVERROR_INVALIDDATA;
59     }
60
61     pic->reference = 0;
62     if ((ret = avctx->get_buffer(avctx, pic)) < 0) {
63         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
64         return ret;
65     }
66
67     pic->pict_type = AV_PICTURE_TYPE_I;
68     pic->key_frame = 1;
69     pic->interlaced_frame = 1;
70     pic->top_field_first = 1;
71
72     for (field = 0; field < 2; field++) {
73         int i;
74         int field_h = (avctx->height + !field) >> 1;
75         int field_size, min_field_size = avctx->width * 2 * field_h;
76         uint8_t *dst = pic->data[0];
77         if (buf_end - buf < 8)
78             return AVERROR_INVALIDDATA;
79         buf += 4; // flags? 0x80 == bottom field maybe?
80         field_size = bytestream_get_le32(&buf);
81         if (field_size < min_field_size) {
82             av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
83             return AVERROR_INVALIDDATA;
84         }
85         if (buf_end - buf < field_size) {
86             av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
87             return AVERROR_INVALIDDATA;
88         }
89         if (field)
90             dst += pic->linesize[0];
91         for (i = 0; i < field_h; i++) {
92             memcpy(dst, buf, avctx->width * 2);
93             buf += avctx->width * 2;
94             dst += pic->linesize[0] << 1;
95         }
96         buf += field_size - min_field_size;
97     }
98
99     *data_size = sizeof(AVFrame);
100     *(AVFrame*)data = *pic;
101
102     return avpkt->size;
103 }
104
105 static av_cold int decode_close(AVCodecContext *avctx)
106 {
107     AVFrame *pic = avctx->coded_frame;
108     if (pic->data[0])
109         avctx->release_buffer(avctx, pic);
110     av_freep(&avctx->coded_frame);
111
112     return 0;
113 }
114
115 AVCodec ff_frwu_decoder = {
116     .name           = "frwu",
117     .type           = AVMEDIA_TYPE_VIDEO,
118     .id             = CODEC_ID_FRWU,
119     .init           = decode_init,
120     .close          = decode_close,
121     .decode         = decode_frame,
122     .capabilities   = CODEC_CAP_DR1,
123     .long_name      = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
124 };