]> git.sesse.net Git - ffmpeg/blob - libavcodec/nuv.c
convert svn:ignore properties to .gitignore files
[ffmpeg] / libavcodec / nuv.c
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 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "libavutil/bswap.h"
25 #include "libavutil/lzo.h"
26 #include "libavcore/imgutils.h"
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "rtjpeg.h"
30
31 typedef struct {
32     AVFrame pic;
33     int codec_frameheader;
34     int quality;
35     int width, height;
36     unsigned int decomp_size;
37     unsigned char* decomp_buf;
38     uint32_t lq[64], cq[64];
39     RTJpegContext rtj;
40     DSPContext dsp;
41 } NuvContext;
42
43 static const uint8_t fallback_lquant[] = {
44     16,  11,  10,  16,  24,  40,  51,  61,
45     12,  12,  14,  19,  26,  58,  60,  55,
46     14,  13,  16,  24,  40,  57,  69,  56,
47     14,  17,  22,  29,  51,  87,  80,  62,
48     18,  22,  37,  56,  68, 109, 103,  77,
49     24,  35,  55,  64,  81, 104, 113,  92,
50     49,  64,  78,  87, 103, 121, 120, 101,
51     72,  92,  95,  98, 112, 100, 103,  99
52 };
53
54 static const uint8_t fallback_cquant[] = {
55     17, 18, 24, 47, 99, 99, 99, 99,
56     18, 21, 26, 66, 99, 99, 99, 99,
57     24, 26, 56, 99, 99, 99, 99, 99,
58     47, 66, 99, 99, 99, 99, 99, 99,
59     99, 99, 99, 99, 99, 99, 99, 99,
60     99, 99, 99, 99, 99, 99, 99, 99,
61     99, 99, 99, 99, 99, 99, 99, 99,
62     99, 99, 99, 99, 99, 99, 99, 99
63 };
64
65 /**
66  * \brief copy frame data from buffer to AVFrame, handling stride.
67  * \param f destination AVFrame
68  * \param src source buffer, does not use any line-stride
69  * \param width width of the video frame
70  * \param height height of the video frame
71  */
72 static void copy_frame(AVFrame *f, const uint8_t *src,
73                        int width, int height) {
74     AVPicture pic;
75     avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height);
76     av_picture_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height);
77 }
78
79 /**
80  * \brief extract quantization tables from codec data into our context
81  */
82 static int get_quant(AVCodecContext *avctx, NuvContext *c,
83                      const uint8_t *buf, int size) {
84     int i;
85     if (size < 2 * 64 * 4) {
86         av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
87         return -1;
88     }
89     for (i = 0; i < 64; i++, buf += 4)
90         c->lq[i] = AV_RL32(buf);
91     for (i = 0; i < 64; i++, buf += 4)
92         c->cq[i] = AV_RL32(buf);
93     return 0;
94 }
95
96 /**
97  * \brief set quantization tables from a quality value
98  */
99 static void get_quant_quality(NuvContext *c, int quality) {
100     int i;
101     quality = FFMAX(quality, 1);
102     for (i = 0; i < 64; i++) {
103         c->lq[i] = (fallback_lquant[i] << 7) / quality;
104         c->cq[i] = (fallback_cquant[i] << 7) / quality;
105     }
106 }
107
108 static int codec_reinit(AVCodecContext *avctx, int width, int height, int quality) {
109     NuvContext *c = avctx->priv_data;
110     width = (width + 1) & ~1;
111     height = (height + 1) & ~1;
112     if (quality >= 0)
113         get_quant_quality(c, quality);
114     if (width != c->width || height != c->height) {
115         if (av_image_check_size(height, width, 0, avctx) < 0)
116             return 0;
117         avctx->width = c->width = width;
118         avctx->height = c->height = height;
119         c->decomp_size = c->height * c->width * 3 / 2;
120         c->decomp_buf = av_realloc(c->decomp_buf, c->decomp_size + AV_LZO_OUTPUT_PADDING);
121         if (!c->decomp_buf) {
122             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
123             return 0;
124         }
125         rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
126     } else if (quality != c->quality)
127         rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
128     return 1;
129 }
130
131 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
132                         AVPacket *avpkt) {
133     const uint8_t *buf = avpkt->data;
134     int buf_size = avpkt->size;
135     NuvContext *c = avctx->priv_data;
136     AVFrame *picture = data;
137     int orig_size = buf_size;
138     int keyframe;
139     int result;
140     enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1',
141           NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3',
142           NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype;
143
144     if (buf_size < 12) {
145         av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
146         return -1;
147     }
148
149     // codec data (rtjpeg quant tables)
150     if (buf[0] == 'D' && buf[1] == 'R') {
151         int ret;
152         // skip rest of the frameheader.
153         buf = &buf[12];
154         buf_size -= 12;
155         ret = get_quant(avctx, c, buf, buf_size);
156         if (ret < 0)
157             return ret;
158         rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
159         return orig_size;
160     }
161
162     if (buf[0] != 'V' || buf_size < 12) {
163         av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
164         return -1;
165     }
166     comptype = buf[1];
167     switch (comptype) {
168         case NUV_RTJPEG_IN_LZO:
169         case NUV_RTJPEG:
170             keyframe = !buf[2]; break;
171         case NUV_COPY_LAST:
172             keyframe = 0; break;
173         default:
174             keyframe = 1; break;
175     }
176     // skip rest of the frameheader.
177     buf = &buf[12];
178     buf_size -= 12;
179     if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO) {
180         int outlen = c->decomp_size, inlen = buf_size;
181         if (av_lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
182             av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
183         buf = c->decomp_buf;
184         buf_size = c->decomp_size;
185     }
186     if (c->codec_frameheader) {
187         int w, h, q;
188         if (buf_size < 12) {
189             av_log(avctx, AV_LOG_ERROR, "invalid nuv video frame\n");
190             return -1;
191         }
192         w = AV_RL16(&buf[6]);
193         h = AV_RL16(&buf[8]);
194         q = buf[10];
195         if (!codec_reinit(avctx, w, h, q))
196             return -1;
197         buf = &buf[12];
198         buf_size -= 12;
199     }
200
201     if (keyframe && c->pic.data[0])
202         avctx->release_buffer(avctx, &c->pic);
203     c->pic.reference = 3;
204     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
205                           FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
206     result = avctx->reget_buffer(avctx, &c->pic);
207     if (result < 0) {
208         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
209         return -1;
210     }
211
212     c->pic.pict_type = keyframe ? FF_I_TYPE : FF_P_TYPE;
213     c->pic.key_frame = keyframe;
214     // decompress/copy/whatever data
215     switch (comptype) {
216         case NUV_LZO:
217         case NUV_UNCOMPRESSED: {
218             int height = c->height;
219             if (buf_size < c->width * height * 3 / 2) {
220                 av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
221                 height = buf_size / c->width / 3 * 2;
222             }
223             copy_frame(&c->pic, buf, c->width, height);
224             break;
225         }
226         case NUV_RTJPEG_IN_LZO:
227         case NUV_RTJPEG: {
228             rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size);
229             break;
230         }
231         case NUV_BLACK: {
232             memset(c->pic.data[0], 0, c->width * c->height);
233             memset(c->pic.data[1], 128, c->width * c->height / 4);
234             memset(c->pic.data[2], 128, c->width * c->height / 4);
235             break;
236         }
237         case NUV_COPY_LAST: {
238             /* nothing more to do here */
239             break;
240         }
241         default:
242             av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
243             return -1;
244     }
245
246     *picture = c->pic;
247     *data_size = sizeof(AVFrame);
248     return orig_size;
249 }
250
251 static av_cold int decode_init(AVCodecContext *avctx) {
252     NuvContext *c = avctx->priv_data;
253     avctx->pix_fmt = PIX_FMT_YUV420P;
254     c->pic.data[0] = NULL;
255     c->decomp_buf = NULL;
256     c->quality = -1;
257     c->width = 0;
258     c->height = 0;
259     c->codec_frameheader = avctx->codec_tag == MKTAG('R', 'J', 'P', 'G');
260     if (avctx->extradata_size)
261         get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
262     dsputil_init(&c->dsp, avctx);
263     if (!codec_reinit(avctx, avctx->width, avctx->height, -1))
264         return 1;
265     return 0;
266 }
267
268 static av_cold int decode_end(AVCodecContext *avctx) {
269     NuvContext *c = avctx->priv_data;
270     av_freep(&c->decomp_buf);
271     if (c->pic.data[0])
272         avctx->release_buffer(avctx, &c->pic);
273     return 0;
274 }
275
276 AVCodec nuv_decoder = {
277     "nuv",
278     AVMEDIA_TYPE_VIDEO,
279     CODEC_ID_NUV,
280     sizeof(NuvContext),
281     decode_init,
282     NULL,
283     decode_end,
284     decode_frame,
285     CODEC_CAP_DR1,
286     .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo/RTJPEG"),
287 };
288