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