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