4 * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
6 * This file is part of FFmpeg.
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.
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.
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
24 #include "bytestream.h"
25 #include "libavutil/opt.h"
29 int change_field_order;
32 static av_cold int decode_init(AVCodecContext *avctx)
34 if (avctx->width & 1) {
35 av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
36 return AVERROR(EINVAL);
38 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
40 avctx->coded_frame = avcodec_alloc_frame();
41 if (!avctx->coded_frame)
42 return AVERROR(ENOMEM);
47 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
50 FRWUContext *s = avctx->priv_data;
52 AVFrame *pic = avctx->coded_frame;
53 const uint8_t *buf = avpkt->data;
54 const uint8_t *buf_end = buf + avpkt->size;
57 avctx->release_buffer(avctx, pic);
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;
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;
69 if ((ret = avctx->get_buffer(avctx, pic)) < 0) {
70 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
74 pic->pict_type = AV_PICTURE_TYPE_I;
77 for (field = 0; field < 2; field++) {
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;
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;
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];
99 for (i = 0; i < field_h; i++) {
100 if (s->change_field_order && field && i == field_h - 1)
102 memcpy(dst, buf, avctx->width * 2);
103 buf += avctx->width * 2;
104 dst += pic->linesize[0] << 1;
106 buf += field_size - min_field_size;
109 *data_size = sizeof(AVFrame);
110 *(AVFrame*)data = *pic;
115 static av_cold int decode_close(AVCodecContext *avctx)
117 AVFrame *pic = avctx->coded_frame;
119 avctx->release_buffer(avctx, pic);
120 av_freep(&avctx->coded_frame);
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},
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,
138 AVCodec ff_frwu_decoder = {
140 .type = AVMEDIA_TYPE_VIDEO,
141 .id = AV_CODEC_ID_FRWU,
142 .priv_data_size = sizeof(FRWUContext),
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,