]> git.sesse.net Git - ffmpeg/blob - libavcodec/frwu.c
Merge remote-tracking branch 'qatar/master'
[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/intreadwrite.h"
26
27 static av_cold int decode_init(AVCodecContext *avctx)
28 {
29     if (avctx->width & 1) {
30         av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
31         return -1;
32     }
33     avctx->pix_fmt = PIX_FMT_UYVY422;
34
35     avctx->coded_frame = avcodec_alloc_frame();
36     if (!avctx->coded_frame)
37         return AVERROR(ENOMEM);
38
39     return 0;
40 }
41
42 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
43                         AVPacket *avpkt)
44 {
45     int field;
46     AVFrame *pic = avctx->coded_frame;
47     const uint8_t *buf = avpkt->data;
48     const uint8_t *buf_end = buf + avpkt->size;
49
50     if (pic->data[0])
51         avctx->release_buffer(avctx, pic);
52
53     if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
54         av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
55         return -1;
56     }
57     if (bytestream_get_le32(&buf) != AV_RL32("FRW1")) {
58         av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
59         return -1;
60     }
61
62     pic->reference = 0;
63     if (avctx->get_buffer(avctx, pic) < 0)
64         return -1;
65
66     pic->pict_type = AV_PICTURE_TYPE_I;
67     pic->key_frame = 1;
68     pic->interlaced_frame = 1;
69     pic->top_field_first = 1;
70
71     for (field = 0; field < 2; field++) {
72         int i;
73         int field_h = (avctx->height + !field) >> 1;
74         int field_size, min_field_size = avctx->width * 2 * field_h;
75         uint8_t *dst = pic->data[0];
76         if (buf_end - buf < 8)
77             return -1;
78         buf += 4; // flags? 0x80 == bottom field maybe?
79         field_size = bytestream_get_le32(&buf);
80         if (field_size < min_field_size) {
81             av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
82             return -1;
83         }
84         if (buf_end - buf < field_size) {
85             av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
86             return -1;
87         }
88         if (field)
89             dst += pic->linesize[0];
90         for (i = 0; i < field_h; i++) {
91             memcpy(dst, buf, avctx->width * 2);
92             buf += avctx->width * 2;
93             dst += pic->linesize[0] << 1;
94         }
95         buf += field_size - min_field_size;
96     }
97
98     *data_size = sizeof(AVFrame);
99     *(AVFrame*)data = *pic;
100
101     return avpkt->size;
102 }
103
104 static av_cold int decode_close(AVCodecContext *avctx)
105 {
106     AVFrame *pic = avctx->coded_frame;
107     if (pic->data[0])
108         avctx->release_buffer(avctx, pic);
109     av_freep(&avctx->coded_frame);
110
111     return 0;
112 }
113
114 AVCodec ff_frwu_decoder = {
115     .name           = "frwu",
116     .type           = AVMEDIA_TYPE_VIDEO,
117     .id             = CODEC_ID_FRWU,
118     .init           = decode_init,
119     .close          = decode_close,
120     .decode         = decode_frame,
121     .capabilities   = CODEC_CAP_DR1,
122     .long_name = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
123 };