00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avcodec.h"
00024 #include "bytestream.h"
00025 #include "internal.h"
00026 #include "libavutil/opt.h"
00027
00028 typedef struct {
00029 AVClass *av_class;
00030 int change_field_order;
00031 } FRWUContext;
00032
00033 static av_cold int decode_init(AVCodecContext *avctx)
00034 {
00035 if (avctx->width & 1) {
00036 av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
00037 return AVERROR(EINVAL);
00038 }
00039 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
00040
00041 avctx->coded_frame = avcodec_alloc_frame();
00042 if (!avctx->coded_frame)
00043 return AVERROR(ENOMEM);
00044
00045 return 0;
00046 }
00047
00048 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
00049 AVPacket *avpkt)
00050 {
00051 FRWUContext *s = avctx->priv_data;
00052 int field, ret;
00053 AVFrame *pic = avctx->coded_frame;
00054 const uint8_t *buf = avpkt->data;
00055 const uint8_t *buf_end = buf + avpkt->size;
00056
00057 if (pic->data[0])
00058 avctx->release_buffer(avctx, pic);
00059
00060 if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
00061 av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
00062 return AVERROR_INVALIDDATA;
00063 }
00064 if (bytestream_get_le32(&buf) != MKTAG('F', 'R', 'W', '1')) {
00065 av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
00066 return AVERROR_INVALIDDATA;
00067 }
00068
00069 pic->reference = 0;
00070 if ((ret = ff_get_buffer(avctx, pic)) < 0) {
00071 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00072 return ret;
00073 }
00074
00075 pic->pict_type = AV_PICTURE_TYPE_I;
00076 pic->key_frame = 1;
00077
00078 for (field = 0; field < 2; field++) {
00079 int i;
00080 int field_h = (avctx->height + !field) >> 1;
00081 int field_size, min_field_size = avctx->width * 2 * field_h;
00082 uint8_t *dst = pic->data[0];
00083 if (buf_end - buf < 8)
00084 return AVERROR_INVALIDDATA;
00085 buf += 4;
00086 field_size = bytestream_get_le32(&buf);
00087 if (field_size < min_field_size) {
00088 av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
00089 return AVERROR_INVALIDDATA;
00090 }
00091 if (buf_end - buf < field_size) {
00092 av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
00093 return AVERROR_INVALIDDATA;
00094 }
00095 if (field ^ s->change_field_order) {
00096 dst += pic->linesize[0];
00097 } else if (s->change_field_order) {
00098 dst += 2 * pic->linesize[0];
00099 }
00100 for (i = 0; i < field_h; i++) {
00101 if (s->change_field_order && field && i == field_h - 1)
00102 dst = pic->data[0];
00103 memcpy(dst, buf, avctx->width * 2);
00104 buf += avctx->width * 2;
00105 dst += pic->linesize[0] << 1;
00106 }
00107 buf += field_size - min_field_size;
00108 }
00109
00110 *got_frame = 1;
00111 *(AVFrame*)data = *pic;
00112
00113 return avpkt->size;
00114 }
00115
00116 static av_cold int decode_close(AVCodecContext *avctx)
00117 {
00118 AVFrame *pic = avctx->coded_frame;
00119 if (pic->data[0])
00120 avctx->release_buffer(avctx, pic);
00121 av_freep(&avctx->coded_frame);
00122
00123 return 0;
00124 }
00125
00126 static const AVOption frwu_options[] = {
00127 {"change_field_order", "Change field order", offsetof(FRWUContext, change_field_order), FF_OPT_TYPE_INT,
00128 {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM},
00129 {NULL}
00130 };
00131
00132 static const AVClass frwu_class = {
00133 .class_name = "frwu Decoder",
00134 .item_name = av_default_item_name,
00135 .option = frwu_options,
00136 .version = LIBAVUTIL_VERSION_INT,
00137 };
00138
00139 AVCodec ff_frwu_decoder = {
00140 .name = "frwu",
00141 .type = AVMEDIA_TYPE_VIDEO,
00142 .id = AV_CODEC_ID_FRWU,
00143 .priv_data_size = sizeof(FRWUContext),
00144 .init = decode_init,
00145 .close = decode_close,
00146 .decode = decode_frame,
00147 .capabilities = CODEC_CAP_DR1,
00148 .long_name = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
00149 .priv_class = &frwu_class,
00150 };