]> git.sesse.net Git - ffmpeg/blob - libavcodec/frwu.c
a0484d3cbf3c9d93f785aa20fdd7a23bfc9802b7
[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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include "libavutil/opt.h"
26
27 typedef struct {
28     AVClass *av_class;
29     int change_field_order;
30 } FRWUContext;
31
32 static av_cold int decode_init(AVCodecContext *avctx)
33 {
34     if (avctx->width & 1) {
35         av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
36         return AVERROR(EINVAL);
37     }
38     avctx->pix_fmt = AV_PIX_FMT_UYVY422;
39
40     avctx->coded_frame = avcodec_alloc_frame();
41     if (!avctx->coded_frame)
42         return AVERROR(ENOMEM);
43
44     return 0;
45 }
46
47 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
48                         AVPacket *avpkt)
49 {
50     FRWUContext *s = avctx->priv_data;
51     int field, ret;
52     AVFrame *pic = avctx->coded_frame;
53     const uint8_t *buf = avpkt->data;
54     const uint8_t *buf_end = buf + avpkt->size;
55
56     if (pic->data[0])
57         avctx->release_buffer(avctx, pic);
58
59     if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
60         av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
61         return AVERROR_INVALIDDATA;
62     }
63     if (bytestream_get_le32(&buf) != MKTAG('F', 'R', 'W', '1')) {
64         av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
65         return AVERROR_INVALIDDATA;
66     }
67
68     pic->reference = 0;
69     if ((ret = avctx->get_buffer(avctx, pic)) < 0) {
70         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
71         return ret;
72     }
73
74     pic->pict_type = AV_PICTURE_TYPE_I;
75     pic->key_frame = 1;
76
77     for (field = 0; field < 2; field++) {
78         int i;
79         int field_h = (avctx->height + !field) >> 1;
80         int field_size, min_field_size = avctx->width * 2 * field_h;
81         uint8_t *dst = pic->data[0];
82         if (buf_end - buf < 8)
83             return AVERROR_INVALIDDATA;
84         buf += 4; // flags? 0x80 == bottom field maybe?
85         field_size = bytestream_get_le32(&buf);
86         if (field_size < min_field_size) {
87             av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
88             return AVERROR_INVALIDDATA;
89         }
90         if (buf_end - buf < field_size) {
91             av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
92             return AVERROR_INVALIDDATA;
93         }
94         if (field ^ s->change_field_order) {
95             dst += pic->linesize[0];
96         } else if (s->change_field_order) {
97             dst += 2 * pic->linesize[0];
98         }
99         for (i = 0; i < field_h; i++) {
100             if (s->change_field_order && field && i == field_h - 1)
101                 dst = pic->data[0];
102             memcpy(dst, buf, avctx->width * 2);
103             buf += avctx->width * 2;
104             dst += pic->linesize[0] << 1;
105         }
106         buf += field_size - min_field_size;
107     }
108
109     *data_size = sizeof(AVFrame);
110     *(AVFrame*)data = *pic;
111
112     return avpkt->size;
113 }
114
115 static av_cold int decode_close(AVCodecContext *avctx)
116 {
117     AVFrame *pic = avctx->coded_frame;
118     if (pic->data[0])
119         avctx->release_buffer(avctx, pic);
120     av_freep(&avctx->coded_frame);
121
122     return 0;
123 }
124
125 static const AVOption frwu_options[] = {
126     {"change_field_order", "Change field order", offsetof(FRWUContext, change_field_order), FF_OPT_TYPE_INT,
127      {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM},
128     {NULL}
129 };
130
131 static const AVClass frwu_class = {
132     .class_name = "frwu Decoder",
133     .item_name  = av_default_item_name,
134     .option     = frwu_options,
135     .version    = LIBAVUTIL_VERSION_INT,
136 };
137
138 AVCodec ff_frwu_decoder = {
139     .name           = "frwu",
140     .type           = AVMEDIA_TYPE_VIDEO,
141     .id             = AV_CODEC_ID_FRWU,
142     .priv_data_size = sizeof(FRWUContext),
143     .init           = decode_init,
144     .close          = decode_close,
145     .decode         = decode_frame,
146     .capabilities   = CODEC_CAP_DR1,
147     .long_name      = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
148     .priv_class     = &frwu_class,
149 };