3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of FFmpeg.
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.
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.
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
33 int codec_frameheader;
36 unsigned int decomp_size;
37 unsigned char* decomp_buf;
38 uint32_t lq[64], cq[64];
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
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
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
72 static void copy_frame(AVFrame *f, const uint8_t *src,
73 int width, int height) {
75 avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height);
76 av_picture_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height);
80 * \brief extract quantization tables from codec data into our context
82 static int get_quant(AVCodecContext *avctx, NuvContext *c,
83 const uint8_t *buf, int size) {
85 if (size < 2 * 64 * 4) {
86 av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
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);
97 * \brief set quantization tables from a quality value
99 static void get_quant_quality(NuvContext *c, int quality) {
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;
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;
113 get_quant_quality(c, quality);
114 if (width != c->width || height != c->height) {
115 if (avcodec_check_dimensions(avctx, height, width) < 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 + LZO_OUTPUT_PADDING);
121 if (!c->decomp_buf) {
122 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
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);
131 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
132 const uint8_t *buf, int buf_size) {
133 NuvContext *c = avctx->priv_data;
134 AVFrame *picture = data;
135 int orig_size = buf_size;
136 enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1',
137 NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3',
138 NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype;
141 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
145 // codec data (rtjpeg quant tables)
146 if (buf[0] == 'D' && buf[1] == 'R') {
148 // skip rest of the frameheader.
151 ret = get_quant(avctx, c, buf, buf_size);
154 rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
158 if (buf[0] != 'V' || buf_size < 12) {
159 av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
163 // skip rest of the frameheader.
166 if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO) {
167 int outlen = c->decomp_size, inlen = buf_size;
168 if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
169 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
171 buf_size = c->decomp_size;
173 if (c->codec_frameheader) {
176 av_log(avctx, AV_LOG_ERROR, "invalid nuv video frame\n");
179 w = AV_RL16(&buf[6]);
180 h = AV_RL16(&buf[8]);
182 if (!codec_reinit(avctx, w, h, q))
189 avctx->release_buffer(avctx, &c->pic);
190 c->pic.reference = 1;
191 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
192 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
193 if (avctx->get_buffer(avctx, &c->pic) < 0) {
194 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
198 c->pic.pict_type = FF_I_TYPE;
199 c->pic.key_frame = 1;
200 // decompress/copy/whatever data
203 case NUV_UNCOMPRESSED: {
204 int height = c->height;
205 if (buf_size < c->width * height * 3 / 2) {
206 av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
207 height = buf_size / c->width / 3 * 2;
209 copy_frame(&c->pic, buf, c->width, height);
212 case NUV_RTJPEG_IN_LZO:
214 rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size);
218 memset(c->pic.data[0], 0, c->width * c->height);
219 memset(c->pic.data[1], 128, c->width * c->height / 4);
220 memset(c->pic.data[2], 128, c->width * c->height / 4);
223 case NUV_COPY_LAST: {
224 c->pic.pict_type = FF_P_TYPE;
225 c->pic.key_frame = 0;
226 /* nothing more to do here */
230 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
235 *data_size = sizeof(AVFrame);
239 static av_cold int decode_init(AVCodecContext *avctx) {
240 NuvContext *c = avctx->priv_data;
241 avctx->pix_fmt = PIX_FMT_YUV420P;
242 c->pic.data[0] = NULL;
243 c->decomp_buf = NULL;
247 c->codec_frameheader = avctx->codec_tag == MKTAG('R', 'J', 'P', 'G');
248 if (avctx->extradata_size)
249 get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
250 dsputil_init(&c->dsp, avctx);
251 if (!codec_reinit(avctx, avctx->width, avctx->height, -1))
256 static av_cold int decode_end(AVCodecContext *avctx) {
257 NuvContext *c = avctx->priv_data;
258 av_freep(&c->decomp_buf);
260 avctx->release_buffer(avctx, &c->pic);
264 AVCodec nuv_decoder = {
274 .long_name = "NuppelVideo",