Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * NuppelVideo decoder | ||
3 | * Copyright (c) 2006 Reimar Doeffinger | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include <stddef.h> | ||
23 | #include <limits.h> | ||
24 | |||
25 | #include "libavutil/common.h" | ||
26 | #include "libavutil/intreadwrite.h" | ||
27 | #include "libavutil/lzo.h" | ||
28 | #include "libavutil/imgutils.h" | ||
29 | #include "libavutil/mem.h" | ||
30 | #include "avcodec.h" | ||
31 | #include "codec_internal.h" | ||
32 | #include "decode.h" | ||
33 | #include "jpegquanttables.h" | ||
34 | #include "rtjpeg.h" | ||
35 | |||
36 | typedef struct NuvContext { | ||
37 | AVFrame *pic; | ||
38 | int codec_frameheader; | ||
39 | int quality; | ||
40 | int width, height; | ||
41 | unsigned int decomp_size; | ||
42 | unsigned char *decomp_buf; | ||
43 | uint32_t lq[64], cq[64]; | ||
44 | RTJpegContext rtj; | ||
45 | } NuvContext; | ||
46 | |||
47 | /** | ||
48 | * @brief copy frame data from buffer to AVFrame, handling stride. | ||
49 | * @param f destination AVFrame | ||
50 | * @param src source buffer, does not use any line-stride | ||
51 | * @param width width of the video frame | ||
52 | * @param height height of the video frame | ||
53 | */ | ||
54 | ✗ | static void copy_frame(AVFrame *f, const uint8_t *src, int width, int height) | |
55 | { | ||
56 | uint8_t *src_data[4]; | ||
57 | int src_linesize[4]; | ||
58 | ✗ | av_image_fill_arrays(src_data, src_linesize, src, | |
59 | ✗ | f->format, width, height, 1); | |
60 | ✗ | av_image_copy2(f->data, f->linesize, src_data, src_linesize, | |
61 | ✗ | f->format, width, height); | |
62 | ✗ | } | |
63 | |||
64 | /** | ||
65 | * @brief extract quantization tables from codec data into our context | ||
66 | */ | ||
67 | 4 | static int get_quant(AVCodecContext *avctx, NuvContext *c, const uint8_t *buf, | |
68 | int size) | ||
69 | { | ||
70 | int i; | ||
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (size < 2 * 64 * 4) { |
72 | ✗ | av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n"); | |
73 | ✗ | return AVERROR_INVALIDDATA; | |
74 | } | ||
75 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 4 times.
|
260 | for (i = 0; i < 64; i++, buf += 4) |
76 | 256 | c->lq[i] = AV_RL32(buf); | |
77 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 4 times.
|
260 | for (i = 0; i < 64; i++, buf += 4) |
78 | 256 | c->cq[i] = AV_RL32(buf); | |
79 | 4 | return 0; | |
80 | } | ||
81 | |||
82 | /** | ||
83 | * @brief set quantization tables from a quality value | ||
84 | */ | ||
85 | 50 | static void get_quant_quality(NuvContext *c, int quality) | |
86 | { | ||
87 | int i; | ||
88 | 50 | quality = FFMAX(quality, 1); | |
89 |
2/2✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 50 times.
|
3250 | for (i = 0; i < 64; i++) { |
90 | 3200 | c->lq[i] = (ff_mjpeg_std_luminance_quant_tbl[i] << 7) / quality; | |
91 | 3200 | c->cq[i] = (ff_mjpeg_std_chrominance_quant_tbl[i] << 7) / quality; | |
92 | } | ||
93 | 50 | } | |
94 | |||
95 | 54 | static int codec_reinit(AVCodecContext *avctx, int width, int height, | |
96 | int quality) | ||
97 | { | ||
98 | 54 | NuvContext *c = avctx->priv_data; | |
99 | int ret; | ||
100 | |||
101 | 54 | width = FFALIGN(width, 2); | |
102 | 54 | height = FFALIGN(height, 2); | |
103 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 4 times.
|
54 | if (quality >= 0) |
104 | 50 | get_quant_quality(c, quality); | |
105 |
3/4✓ Branch 0 taken 50 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
|
54 | if (width != c->width || height != c->height) { |
106 | // also reserve space for a possible additional header | ||
107 | 4 | int64_t buf_size = height * (int64_t)width * 3 / 2 | |
108 | + FFMAX(AV_LZO_OUTPUT_PADDING, AV_INPUT_BUFFER_PADDING_SIZE) | ||
109 | + RTJPEG_HEADER_SIZE; | ||
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (buf_size > INT_MAX/8) |
111 | ✗ | return -1; | |
112 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = ff_set_dimensions(avctx, width, height)) < 0) |
113 | ✗ | return ret; | |
114 | 4 | c->width = width; | |
115 | 4 | c->height = height; | |
116 | 4 | av_fast_malloc(&c->decomp_buf, &c->decomp_size, | |
117 | buf_size); | ||
118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!c->decomp_buf) { |
119 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
120 | "Can't allocate decompression buffer.\n"); | ||
121 | ✗ | return AVERROR(ENOMEM); | |
122 | } | ||
123 | 4 | ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq); | |
124 | 4 | av_frame_unref(c->pic); | |
125 | 4 | return 1; | |
126 |
1/2✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
|
50 | } else if (quality != c->quality) |
127 | 50 | ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq); | |
128 | |||
129 | 50 | return 0; | |
130 | } | ||
131 | |||
132 | 59 | static int decode_frame(AVCodecContext *avctx, AVFrame *picture, | |
133 | int *got_frame, AVPacket *avpkt) | ||
134 | { | ||
135 | 59 | const uint8_t *buf = avpkt->data; | |
136 | 59 | int buf_size = avpkt->size; | |
137 | 59 | NuvContext *c = avctx->priv_data; | |
138 | 59 | int orig_size = buf_size; | |
139 | int keyframe, ret; | ||
140 | 59 | int size_change = 0; | |
141 | 59 | int minsize = 0; | |
142 | 59 | int flags = 0; | |
143 | 59 | int result, init_frame = !avctx->frame_num; | |
144 | enum { | ||
145 | NUV_UNCOMPRESSED = '0', | ||
146 | NUV_RTJPEG = '1', | ||
147 | NUV_RTJPEG_IN_LZO = '2', | ||
148 | NUV_LZO = '3', | ||
149 | NUV_BLACK = 'N', | ||
150 | NUV_COPY_LAST = 'L' | ||
151 | } comptype; | ||
152 | |||
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
|
59 | if (buf_size < 12) { |
154 | ✗ | av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); | |
155 | ✗ | return AVERROR_INVALIDDATA; | |
156 | } | ||
157 | |||
158 | // codec data (rtjpeg quant tables) | ||
159 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
59 | if (buf[0] == 'D' && buf[1] == 'R') { |
160 | int ret; | ||
161 | // Skip the rest of the frame header. | ||
162 | ✗ | buf = &buf[12]; | |
163 | ✗ | buf_size -= 12; | |
164 | ✗ | ret = get_quant(avctx, c, buf, buf_size); | |
165 | ✗ | if (ret < 0) | |
166 | ✗ | return ret; | |
167 | ✗ | ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq); | |
168 | ✗ | return orig_size; | |
169 | } | ||
170 | |||
171 |
2/4✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 59 times.
|
59 | if (buf_size < 12 || buf[0] != 'V') { |
172 | ✗ | av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n"); | |
173 | ✗ | return AVERROR_INVALIDDATA; | |
174 | } | ||
175 | 59 | comptype = buf[1]; | |
176 |
2/3✓ Branch 0 taken 58 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
59 | switch (comptype) { |
177 | 58 | case NUV_RTJPEG_IN_LZO: | |
178 | case NUV_RTJPEG: | ||
179 | 58 | keyframe = !buf[2]; | |
180 |
2/4✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 58 times.
|
58 | if (c->width < 16 || c->height < 16) { |
181 | ✗ | return AVERROR_INVALIDDATA; | |
182 | } | ||
183 | 58 | break; | |
184 | 1 | case NUV_COPY_LAST: | |
185 | 1 | flags |= FF_REGET_BUFFER_FLAG_READONLY; | |
186 | 1 | keyframe = 0; | |
187 | 1 | break; | |
188 | ✗ | default: | |
189 | ✗ | keyframe = 1; | |
190 | ✗ | break; | |
191 | } | ||
192 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
|
59 | switch (comptype) { |
193 | ✗ | case NUV_UNCOMPRESSED: | |
194 | ✗ | minsize = c->width * c->height * 3 / 2; | |
195 | ✗ | break; | |
196 | ✗ | case NUV_RTJPEG: | |
197 | ✗ | minsize = c->width/16 * (c->height/16) * 6; | |
198 | ✗ | break; | |
199 | 59 | case NUV_BLACK: | |
200 | case NUV_COPY_LAST: | ||
201 | case NUV_LZO: | ||
202 | case NUV_RTJPEG_IN_LZO: | ||
203 | 59 | break; | |
204 | ✗ | default: | |
205 | ✗ | av_log(avctx, AV_LOG_ERROR, "unknown compression\n"); | |
206 | ✗ | return AVERROR_INVALIDDATA; | |
207 | } | ||
208 |
1/2✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
|
59 | if (buf_size < minsize / 4) |
209 | ✗ | return AVERROR_INVALIDDATA; | |
210 | 59 | retry: | |
211 | // Skip the rest of the frame header. | ||
212 | 59 | buf = &buf[12]; | |
213 | 59 | buf_size -= 12; | |
214 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
59 | if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO) { |
215 | 58 | int outlen = c->decomp_size - FFMAX(AV_INPUT_BUFFER_PADDING_SIZE, AV_LZO_OUTPUT_PADDING); | |
216 | 58 | int inlen = buf_size; | |
217 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 57 times.
|
58 | if (av_lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen)) { |
218 | 1 | av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); | |
219 | 1 | return AVERROR_INVALIDDATA; | |
220 | } | ||
221 | 57 | buf = c->decomp_buf; | |
222 | 57 | buf_size = c->decomp_size - FFMAX(AV_INPUT_BUFFER_PADDING_SIZE, AV_LZO_OUTPUT_PADDING) - outlen; | |
223 | 57 | memset(c->decomp_buf + buf_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
224 | } | ||
225 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 8 times.
|
58 | if (c->codec_frameheader) { |
226 | int w, h, q; | ||
227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (buf_size < RTJPEG_HEADER_SIZE) { |
228 | ✗ | av_log(avctx, AV_LOG_ERROR, "Too small NUV video frame\n"); | |
229 | ✗ | return AVERROR_INVALIDDATA; | |
230 | } | ||
231 | // There seem to exist two variants of this header: one starts with 'V' | ||
232 | // and 5 bytes unknown, the other matches current MythTV and is 4 bytes size, | ||
233 | // 1 byte header size (== 12), 1 byte version (== 0) | ||
234 |
2/4✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
|
50 | if (buf[0] != 'V' && AV_RL16(&buf[4]) != 0x000c) { |
235 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown secondary frame header (wrong codec_tag?)\n"); | |
236 | ✗ | return AVERROR_INVALIDDATA; | |
237 | } | ||
238 | 50 | w = AV_RL16(&buf[6]); | |
239 | 50 | h = AV_RL16(&buf[8]); | |
240 | 50 | q = buf[10]; | |
241 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
50 | if ((result = codec_reinit(avctx, w, h, q)) < 0) |
242 | ✗ | return result; | |
243 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (result) { |
244 | ✗ | buf = avpkt->data; | |
245 | ✗ | buf_size = avpkt->size; | |
246 | ✗ | size_change = 1; | |
247 | ✗ | goto retry; | |
248 | } | ||
249 | 50 | buf = &buf[RTJPEG_HEADER_SIZE]; | |
250 | 50 | buf_size -= RTJPEG_HEADER_SIZE; | |
251 | } | ||
252 | |||
253 |
3/4✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 55 times.
|
58 | if (size_change || keyframe) { |
254 | 3 | av_frame_unref(c->pic); | |
255 | 3 | init_frame = 1; | |
256 | } | ||
257 | |||
258 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
|
58 | if ((result = ff_reget_buffer(avctx, c->pic, flags)) < 0) |
259 | ✗ | return result; | |
260 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 55 times.
|
58 | if (init_frame) { |
261 | 3 | memset(c->pic->data[0], 0, avctx->height * c->pic->linesize[0]); | |
262 | 3 | memset(c->pic->data[1], 0x80, avctx->height * c->pic->linesize[1] / 2); | |
263 | 3 | memset(c->pic->data[2], 0x80, avctx->height * c->pic->linesize[2] / 2); | |
264 | } | ||
265 | |||
266 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 55 times.
|
58 | c->pic->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; |
267 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 55 times.
|
58 | if (keyframe) |
268 | 3 | c->pic->flags |= AV_FRAME_FLAG_KEY; | |
269 | else | ||
270 | 55 | c->pic->flags &= ~AV_FRAME_FLAG_KEY; | |
271 | // decompress/copy/whatever data | ||
272 |
2/5✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
58 | switch (comptype) { |
273 | ✗ | case NUV_LZO: | |
274 | case NUV_UNCOMPRESSED: { | ||
275 | ✗ | int height = c->height; | |
276 | ✗ | if (buf_size < c->width * height * 3 / 2) { | |
277 | ✗ | av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n"); | |
278 | ✗ | height = buf_size / c->width / 3 * 2; | |
279 | } | ||
280 | ✗ | if(height > 0) | |
281 | ✗ | copy_frame(c->pic, buf, c->width, height); | |
282 | ✗ | break; | |
283 | } | ||
284 | 57 | case NUV_RTJPEG_IN_LZO: | |
285 | case NUV_RTJPEG: | ||
286 | 57 | ret = ff_rtjpeg_decode_frame_yuv420(&c->rtj, c->pic, buf, buf_size); | |
287 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | if (ret < 0) |
288 | ✗ | return ret; | |
289 | 57 | break; | |
290 | ✗ | case NUV_BLACK: | |
291 | ✗ | memset(c->pic->data[0], 0, c->width * c->height); | |
292 | ✗ | memset(c->pic->data[1], 128, c->width * c->height / 4); | |
293 | ✗ | memset(c->pic->data[2], 128, c->width * c->height / 4); | |
294 | ✗ | break; | |
295 | 1 | case NUV_COPY_LAST: | |
296 | /* nothing more to do here */ | ||
297 | 1 | break; | |
298 | } | ||
299 | |||
300 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
|
58 | if ((result = av_frame_ref(picture, c->pic)) < 0) |
301 | ✗ | return result; | |
302 | |||
303 | 58 | *got_frame = 1; | |
304 | 58 | return orig_size; | |
305 | } | ||
306 | |||
307 | 4 | static av_cold int decode_init(AVCodecContext *avctx) | |
308 | { | ||
309 | 4 | NuvContext *c = avctx->priv_data; | |
310 | int ret; | ||
311 | |||
312 | 4 | c->pic = av_frame_alloc(); | |
313 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!c->pic) |
314 | ✗ | return AVERROR(ENOMEM); | |
315 | |||
316 | 4 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; | |
317 | 4 | c->decomp_buf = NULL; | |
318 | 4 | c->quality = -1; | |
319 | 4 | c->width = 0; | |
320 | 4 | c->height = 0; | |
321 | |||
322 | 4 | c->codec_frameheader = avctx->codec_tag == MKTAG('R', 'J', 'P', 'G'); | |
323 | |||
324 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (avctx->extradata_size) |
325 | 4 | get_quant(avctx, c, avctx->extradata, avctx->extradata_size); | |
326 | |||
327 | 4 | ff_rtjpeg_init(&c->rtj, avctx); | |
328 | |||
329 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = codec_reinit(avctx, avctx->width, avctx->height, -1)) < 0) |
330 | ✗ | return ret; | |
331 | |||
332 | 4 | return 0; | |
333 | } | ||
334 | |||
335 | 4 | static av_cold int decode_end(AVCodecContext *avctx) | |
336 | { | ||
337 | 4 | NuvContext *c = avctx->priv_data; | |
338 | |||
339 | 4 | av_freep(&c->decomp_buf); | |
340 | 4 | av_frame_free(&c->pic); | |
341 | |||
342 | 4 | return 0; | |
343 | } | ||
344 | |||
345 | const FFCodec ff_nuv_decoder = { | ||
346 | .p.name = "nuv", | ||
347 | CODEC_LONG_NAME("NuppelVideo/RTJPEG"), | ||
348 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
349 | .p.id = AV_CODEC_ID_NUV, | ||
350 | .priv_data_size = sizeof(NuvContext), | ||
351 | .init = decode_init, | ||
352 | .close = decode_end, | ||
353 | FF_CODEC_DECODE_CB(decode_frame), | ||
354 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
355 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
356 | }; | ||
357 |