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