4 * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
6 * This file is part of Libav.
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.
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.
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
24 #include "bytestream.h"
27 static av_cold int decode_init(AVCodecContext *avctx)
29 if (avctx->width & 1) {
30 av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
31 return AVERROR(EINVAL);
33 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
38 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
43 const uint8_t *buf = avpkt->data;
44 const uint8_t *buf_end = buf + avpkt->size;
46 if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
47 av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
48 return AVERROR_INVALIDDATA;
50 if (bytestream_get_le32(&buf) != MKTAG('F', 'R', 'W', '1')) {
51 av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
52 return AVERROR_INVALIDDATA;
55 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0) {
56 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
60 pic->pict_type = AV_PICTURE_TYPE_I;
62 pic->interlaced_frame = 1;
63 pic->top_field_first = 1;
65 for (field = 0; field < 2; field++) {
67 int field_h = (avctx->height + !field) >> 1;
68 int field_size, min_field_size = avctx->width * 2 * field_h;
69 uint8_t *dst = pic->data[0];
70 if (buf_end - buf < 8)
71 return AVERROR_INVALIDDATA;
72 buf += 4; // flags? 0x80 == bottom field maybe?
73 field_size = bytestream_get_le32(&buf);
74 if (field_size < min_field_size) {
75 av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
76 return AVERROR_INVALIDDATA;
78 if (buf_end - buf < field_size) {
79 av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
80 return AVERROR_INVALIDDATA;
83 dst += pic->linesize[0];
84 for (i = 0; i < field_h; i++) {
85 memcpy(dst, buf, avctx->width * 2);
86 buf += avctx->width * 2;
87 dst += pic->linesize[0] << 1;
89 buf += field_size - min_field_size;
97 AVCodec ff_frwu_decoder = {
99 .long_name = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
100 .type = AVMEDIA_TYPE_VIDEO,
101 .id = AV_CODEC_ID_FRWU,
103 .decode = decode_frame,
104 .capabilities = CODEC_CAP_DR1,