Statistics
| Branch: | Revision:

ffmpeg / libavcodec / nuv.c @ 2029f312

History | View | Annotate | Download (6.71 KB)

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 width, height;
34
    unsigned int decomp_size;
35
    unsigned char* decomp_buf;
36
    uint32_t lq[64], cq[64];
37
    RTJpegContext rtj;
38
    DSPContext dsp;
39
} NuvContext;
40

    
41
/**
42
 * \brief copy frame data from buffer to AVFrame, handling stride.
43
 * \param f destination AVFrame
44
 * \param src source buffer, does not use any line-stride
45
 * \param width width of the video frame
46
 * \param height height of the video frame
47
 */
48
static void copy_frame(AVFrame *f, uint8_t *src,
49
                       int width, int height) {
50
    AVPicture pic;
51
    avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height);
52
    av_picture_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height);
53
}
54

    
55
/**
56
 * \brief extract quantization tables from codec data into our context
57
 */
58
static int get_quant(AVCodecContext *avctx, NuvContext *c,
59
                     uint8_t *buf, int size) {
60
    int i;
61
    if (size < 2 * 64 * 4) {
62
        av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
63
        return -1;
64
    }
65
    for (i = 0; i < 64; i++, buf += 4)
66
        c->lq[i] = AV_RL32(buf);
67
    for (i = 0; i < 64; i++, buf += 4)
68
        c->cq[i] = AV_RL32(buf);
69
    return 0;
70
}
71

    
72
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
73
                        uint8_t *buf, int buf_size) {
74
    NuvContext *c = avctx->priv_data;
75
    AVFrame *picture = data;
76
    int orig_size = buf_size;
77
    enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1',
78
          NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3',
79
          NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype;
80

    
81
    if (buf_size < 12) {
82
        av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
83
        return -1;
84
    }
85

    
86
    if (c->pic.data[0])
87
        avctx->release_buffer(avctx, &c->pic);
88
    c->pic.reference = 1;
89
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
90
                          FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
91
    if (avctx->get_buffer(avctx, &c->pic) < 0) {
92
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
93
        return -1;
94
    }
95

    
96
    // codec data (rtjpeg quant tables)
97
    if (buf[0] == 'D' && buf[1] == 'R') {
98
        int ret;
99
        // skip rest of the frameheader.
100
        buf = &buf[12];
101
        buf_size -= 12;
102
        ret = get_quant(avctx, c, buf, buf_size);
103
        if (ret < 0)
104
            return ret;
105
        rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
106
        return orig_size;
107
    }
108

    
109
    if (buf[0] != 'V' || buf_size < 12) {
110
        av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
111
        return -1;
112
    }
113
    comptype = buf[1];
114
    // skip rest of the frameheader.
115
    buf = &buf[12];
116
    buf_size -= 12;
117

    
118
    c->pic.pict_type = FF_I_TYPE;
119
    c->pic.key_frame = 1;
120
    // decompress/copy/whatever data
121
    switch (comptype) {
122
        case NUV_UNCOMPRESSED: {
123
            int height = c->height;
124
            if (buf_size < c->width * height * 3 / 2) {
125
                av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
126
                height = buf_size / c->width / 3 * 2;
127
            }
128
            copy_frame(&c->pic, buf, c->width, height);
129
            break;
130
        }
131
        case NUV_RTJPEG: {
132
            rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size);
133
            break;
134
        }
135
        case NUV_RTJPEG_IN_LZO: {
136
            int outlen = c->decomp_size, inlen = buf_size;
137
            if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
138
                av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
139
            rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, c->decomp_buf, c->decomp_size);
140
            break;
141
        }
142
        case NUV_LZO: {
143
            int outlen = c->decomp_size, inlen = buf_size;
144
            if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
145
                av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
146
            copy_frame(&c->pic, c->decomp_buf, c->width, c->height);
147
            break;
148
        }
149
        case NUV_BLACK: {
150
            memset(c->pic.data[0], 0, c->width * c->height);
151
            memset(c->pic.data[1], 128, c->width * c->height / 4);
152
            memset(c->pic.data[2], 128, c->width * c->height / 4);
153
            break;
154
        }
155
        case NUV_COPY_LAST: {
156
            c->pic.pict_type = FF_P_TYPE;
157
            c->pic.key_frame = 0;
158
            /* nothing more to do here */
159
            break;
160
        }
161
        default:
162
            av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
163
            return -1;
164
    }
165

    
166
    *picture = c->pic;
167
    *data_size = sizeof(AVFrame);
168
    return orig_size;
169
}
170

    
171
static int decode_init(AVCodecContext *avctx) {
172
    NuvContext *c = avctx->priv_data;
173
    avctx->width = (avctx->width + 1) & ~1;
174
    avctx->height = (avctx->height + 1) & ~1;
175
    if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
176
        return 1;
177
    }
178
    avctx->pix_fmt = PIX_FMT_YUV420P;
179
    c->pic.data[0] = NULL;
180
    c->width = avctx->width;
181
    c->height = avctx->height;
182
    c->decomp_size = c->height * c->width * 3 / 2;
183
    c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING);
184
    if (!c->decomp_buf) {
185
        av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
186
        return 1;
187
    }
188
    dsputil_init(&c->dsp, avctx);
189
    if (avctx->extradata_size)
190
        get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
191
    rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
192
    return 0;
193
}
194

    
195
static int decode_end(AVCodecContext *avctx) {
196
    NuvContext *c = avctx->priv_data;
197
    av_freep(&c->decomp_buf);
198
    if (c->pic.data[0])
199
        avctx->release_buffer(avctx, &c->pic);
200
    return 0;
201
}
202

    
203
AVCodec nuv_decoder = {
204
    "nuv",
205
    CODEC_TYPE_VIDEO,
206
    CODEC_ID_NUV,
207
    sizeof(NuvContext),
208
    decode_init,
209
    NULL,
210
    decode_end,
211
    decode_frame,
212
    CODEC_CAP_DR1,
213
};
214