/src/ffmpeg/libavcodec/cpia.c
Line | Count | Source |
1 | | /* |
2 | | * CPiA video decoder. |
3 | | * Copyright (c) 2010 Hans de Goede <hdegoede@redhat.com> |
4 | | * |
5 | | * This decoder is based on the LGPL code available at |
6 | | * https://v4l4j.googlecode.com/svn/v4l4j/trunk/libvideo/libv4lconvert/cpia1.c |
7 | | * |
8 | | * This file is part of FFmpeg. |
9 | | * |
10 | | * FFmpeg is free software; you can redistribute it and/or |
11 | | * modify it under the terms of the GNU Lesser General Public |
12 | | * License as published by the Free Software Foundation; either |
13 | | * version 2.1 of the License, or (at your option) any later version. |
14 | | * |
15 | | * FFmpeg is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | | * Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public |
21 | | * License along with FFmpeg; if not, write to the Free Software |
22 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | | */ |
24 | | |
25 | | #include "libavutil/intreadwrite.h" |
26 | | #include "avcodec.h" |
27 | | #include "codec_internal.h" |
28 | | #include "decode.h" |
29 | | |
30 | | |
31 | 518k | #define FRAME_HEADER_SIZE 64 |
32 | 412k | #define MAGIC_0 0x19 /**< First header byte */ |
33 | 411k | #define MAGIC_1 0x68 /**< Second header byte */ |
34 | 226k | #define SUBSAMPLE_420 0 |
35 | 110k | #define SUBSAMPLE_422 1 |
36 | 221k | #define YUVORDER_YUYV 0 |
37 | 110k | #define YUVORDER_UYVY 1 |
38 | 330k | #define NOT_COMPRESSED 0 |
39 | 25.8k | #define COMPRESSED 1 |
40 | 220k | #define NO_DECIMATION 0 |
41 | 109k | #define DECIMATION_ENAB 1 |
42 | 97.9k | #define EOL 0xfd /**< End Of Line marker */ |
43 | | #define EOI 0xff /**< End Of Image marker */ |
44 | | |
45 | | |
46 | | typedef struct { |
47 | | AVFrame *frame; |
48 | | } CpiaContext; |
49 | | |
50 | | |
51 | | static int cpia_decode_frame(AVCodecContext *avctx, AVFrame *rframe, |
52 | | int *got_frame, AVPacket* avpkt) |
53 | 300k | { |
54 | 300k | CpiaContext* const cpia = avctx->priv_data; |
55 | 300k | int i,j,ret; |
56 | | |
57 | 300k | const uint8_t *const header = avpkt->data; |
58 | 300k | const uint8_t *src; |
59 | 300k | int src_size; |
60 | 300k | uint16_t linelength; |
61 | 300k | uint8_t skip; |
62 | | |
63 | 300k | AVFrame *frame = cpia->frame; |
64 | 300k | uint8_t *y, *u, *v, *y_end, *u_end, *v_end; |
65 | | |
66 | | // Check header |
67 | 300k | if ( avpkt->size < FRAME_HEADER_SIZE + avctx->height * 3 |
68 | 112k | || header[0] != MAGIC_0 || header[1] != MAGIC_1 |
69 | 111k | || (header[17] != SUBSAMPLE_420 && header[17] != SUBSAMPLE_422) |
70 | 110k | || (header[18] != YUVORDER_YUYV && header[18] != YUVORDER_UYVY) |
71 | 110k | || (header[28] != NOT_COMPRESSED && header[28] != COMPRESSED) |
72 | 110k | || (header[29] != NO_DECIMATION && header[29] != DECIMATION_ENAB) |
73 | 300k | ) { |
74 | 190k | av_log(avctx, AV_LOG_ERROR, "Invalid header!\n"); |
75 | 190k | return AVERROR_INVALIDDATA; |
76 | 190k | } |
77 | | |
78 | | // currently unsupported properties |
79 | 109k | if (header[17] == SUBSAMPLE_422) { |
80 | 328 | avpriv_report_missing_feature(avctx, "4:2:2 subsampling"); |
81 | 328 | return AVERROR_PATCHWELCOME; |
82 | 328 | } |
83 | 109k | if (header[18] == YUVORDER_UYVY) { |
84 | 217 | avpriv_report_missing_feature(avctx, "YUV byte order UYVY"); |
85 | 217 | return AVERROR_PATCHWELCOME; |
86 | 217 | } |
87 | 109k | if (header[29] == DECIMATION_ENAB) { |
88 | 219 | avpriv_report_missing_feature(avctx, "Decimation"); |
89 | 219 | return AVERROR_PATCHWELCOME; |
90 | 219 | } |
91 | | |
92 | 109k | src = header + FRAME_HEADER_SIZE; |
93 | 109k | src_size = avpkt->size - FRAME_HEADER_SIZE; |
94 | | |
95 | 109k | if (header[28] == NOT_COMPRESSED) { |
96 | 106k | frame->pict_type = AV_PICTURE_TYPE_I; |
97 | 106k | frame->flags |= AV_FRAME_FLAG_KEY; |
98 | 106k | } else { |
99 | 2.91k | frame->pict_type = AV_PICTURE_TYPE_P; |
100 | 2.91k | frame->flags &= ~AV_FRAME_FLAG_KEY; |
101 | 2.91k | } |
102 | | |
103 | | // Get buffer filled with previous frame |
104 | 109k | if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0) |
105 | 1.37k | return ret; |
106 | | |
107 | | |
108 | 107k | for ( i = 0; |
109 | 112k | i < frame->height; |
110 | 107k | i++, src += linelength, src_size -= linelength |
111 | 110k | ) { |
112 | | // Read line length, two byte little endian |
113 | 110k | linelength = AV_RL16(src); |
114 | 110k | src += 2; |
115 | 110k | src_size -= 2; |
116 | | |
117 | 110k | if (src_size < linelength) { |
118 | 12.8k | frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM; |
119 | 12.8k | av_log(avctx, AV_LOG_WARNING, "Frame ended unexpectedly!\n"); |
120 | 12.8k | break; |
121 | 12.8k | } |
122 | 97.9k | if (src[linelength - 1] != EOL) { |
123 | 93.5k | frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM; |
124 | 93.5k | av_log(avctx, AV_LOG_WARNING, "Wrong line length %d or line not terminated properly (found 0x%02x)!\n", linelength, src[linelength - 1]); |
125 | 93.5k | break; |
126 | 93.5k | } |
127 | | |
128 | | /* Update the data pointers. Y data is on every line. |
129 | | * U and V data on every second line |
130 | | */ |
131 | 4.38k | y = &frame->data[0][i * frame->linesize[0]]; |
132 | 4.38k | u = &frame->data[1][(i >> 1) * frame->linesize[1]]; |
133 | 4.38k | v = &frame->data[2][(i >> 1) * frame->linesize[2]]; |
134 | 4.38k | y_end = y + frame->linesize[0] - 1; |
135 | 4.38k | u_end = u + frame->linesize[1] - 1; |
136 | 4.38k | v_end = v + frame->linesize[2] - 1; |
137 | | |
138 | 4.38k | if ((i & 1) && header[17] == SUBSAMPLE_420) { |
139 | | /* We are on an odd line and 420 subsample is used. |
140 | | * On this line only Y values are specified, one per pixel. |
141 | | */ |
142 | 21.1k | for (j = 0; j < linelength - 1; j++) { |
143 | 20.0k | if (y > y_end) { |
144 | 359 | frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM; |
145 | 359 | av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n"); |
146 | 359 | break; |
147 | 359 | } |
148 | 19.6k | if ((src[j] & 1) && header[28] == COMPRESSED) { |
149 | | /* It seems that odd lines are always uncompressed, but |
150 | | * we do it according to specification anyways. |
151 | | */ |
152 | 5.72k | skip = src[j] >> 1; |
153 | 5.72k | y += skip; |
154 | 13.9k | } else { |
155 | 13.9k | *(y++) = src[j]; |
156 | 13.9k | } |
157 | 19.6k | } |
158 | 2.91k | } else if (header[17] == SUBSAMPLE_420) { |
159 | | /* We are on an even line and 420 subsample is used. |
160 | | * On this line each pair of pixels is described by four bytes. |
161 | | */ |
162 | 20.3k | for (j = 0; j < linelength - 4; ) { |
163 | 17.8k | if (y + 1 > y_end || u > u_end || v > v_end) { |
164 | 418 | frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM; |
165 | 418 | av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n"); |
166 | 418 | break; |
167 | 418 | } |
168 | 17.3k | if ((src[j] & 1) && header[28] == COMPRESSED) { |
169 | | // Skip amount of pixels and move forward one byte |
170 | 7.11k | skip = src[j] >> 1; |
171 | 7.11k | y += skip; |
172 | 7.11k | u += skip >> 1; |
173 | 7.11k | v += skip >> 1; |
174 | 7.11k | j++; |
175 | 10.2k | } else { |
176 | | // Set image data as specified and move forward 4 bytes |
177 | 10.2k | *(y++) = src[j]; |
178 | 10.2k | *(u++) = src[j+1]; |
179 | 10.2k | *(y++) = src[j+2]; |
180 | 10.2k | *(v++) = src[j+3]; |
181 | 10.2k | j += 4; |
182 | 10.2k | } |
183 | 17.3k | } |
184 | 2.91k | } |
185 | 4.38k | } |
186 | | |
187 | 107k | *got_frame = 1; |
188 | 107k | if ((ret = av_frame_ref(rframe, cpia->frame)) < 0) |
189 | 0 | return ret; |
190 | | |
191 | 107k | return avpkt->size; |
192 | 107k | } |
193 | | |
194 | | static av_cold int cpia_decode_init(AVCodecContext *avctx) |
195 | 975 | { |
196 | 975 | CpiaContext *s = avctx->priv_data; |
197 | | |
198 | | // output pixel format |
199 | 975 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
200 | | |
201 | 975 | s->frame = av_frame_alloc(); |
202 | 975 | if (!s->frame) |
203 | 0 | return AVERROR(ENOMEM); |
204 | | |
205 | 975 | return 0; |
206 | 975 | } |
207 | | |
208 | | static av_cold int cpia_decode_end(AVCodecContext *avctx) |
209 | 975 | { |
210 | 975 | CpiaContext *s = avctx->priv_data; |
211 | | |
212 | 975 | av_frame_free(&s->frame); |
213 | | |
214 | 975 | return 0; |
215 | 975 | } |
216 | | |
217 | | const FFCodec ff_cpia_decoder = { |
218 | | .p.name = "cpia", |
219 | | CODEC_LONG_NAME("CPiA video format"), |
220 | | .p.type = AVMEDIA_TYPE_VIDEO, |
221 | | .p.id = AV_CODEC_ID_CPIA, |
222 | | .priv_data_size = sizeof(CpiaContext), |
223 | | .init = cpia_decode_init, |
224 | | .close = cpia_decode_end, |
225 | | FF_CODEC_DECODE_CB(cpia_decode_frame), |
226 | | .p.capabilities = AV_CODEC_CAP_DR1, |
227 | | }; |